2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "GLClientState.h"
22 #include <cutils/log.h>
25 #define MAX(a, b) ((a) < (b) ? (b) : (a))
28 GLClientState::GLClientState(int nLocations)
30 if (nLocations < LAST_LOCATION) {
31 nLocations = LAST_LOCATION;
33 m_nLocations = nLocations;
34 m_states = new VertexAttribState[m_nLocations];
35 for (int i = 0; i < m_nLocations; i++) {
36 m_states[i].enabled = 0;
37 m_states[i].enableDirty = false;
40 m_currentArrayVbo = 0;
41 m_currentIndexVbo = 0;
43 m_states[VERTEX_LOCATION].glConst = GL_VERTEX_ARRAY;
44 m_states[NORMAL_LOCATION].glConst = GL_NORMAL_ARRAY;
45 m_states[COLOR_LOCATION].glConst = GL_COLOR_ARRAY;
46 m_states[POINTSIZE_LOCATION].glConst = GL_POINT_SIZE_ARRAY_OES;
47 m_states[TEXCOORD0_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
48 m_states[TEXCOORD1_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
49 m_states[TEXCOORD2_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
50 m_states[TEXCOORD3_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
51 m_states[TEXCOORD4_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
52 m_states[TEXCOORD5_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
53 m_states[TEXCOORD6_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
54 m_states[TEXCOORD7_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
55 m_states[MATRIXINDEX_LOCATION].glConst = GL_MATRIX_INDEX_ARRAY_OES;
56 m_states[WEIGHT_LOCATION].glConst = GL_WEIGHT_ARRAY_OES;
60 m_pixelStore.unpack_alignment = 4;
61 m_pixelStore.pack_alignment = 4;
63 memset(m_tex.unit, 0, sizeof(m_tex.unit));
64 m_tex.activeUnit = &m_tex.unit[0];
65 m_tex.textures = NULL;
66 m_tex.numTextures = 0;
67 m_tex.allocTextures = 0;
69 m_maxVertexAttribsDirty = true;
72 GLClientState::~GLClientState()
77 void GLClientState::enable(int location, int state)
79 if (!validLocation(location)) {
83 m_states[location].enableDirty |= (state != m_states[location].enabled);
84 m_states[location].enabled = state;
87 void GLClientState::setState(int location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data)
89 if (!validLocation(location)) {
92 m_states[location].size = size;
93 m_states[location].type = type;
94 m_states[location].stride = stride;
95 m_states[location].data = (void*)data;
96 m_states[location].bufferObject = m_currentArrayVbo;
97 m_states[location].elementSize = size ? (glSizeof(type) * size) : 0;
98 m_states[location].normalized = normalized;
101 void GLClientState::setBufferObject(int location, GLuint id)
103 if (!validLocation(location)) {
107 m_states[location].bufferObject = id;
110 const GLClientState::VertexAttribState * GLClientState::getState(int location)
112 if (!validLocation(location)) {
115 return & m_states[location];
118 const GLClientState::VertexAttribState * GLClientState::getStateAndEnableDirty(int location, bool *enableChanged)
120 if (!validLocation(location)) {
125 *enableChanged = m_states[location].enableDirty;
128 m_states[location].enableDirty = false;
129 return & m_states[location];
132 int GLClientState::getLocation(GLenum loc)
137 case GL_VERTEX_ARRAY:
138 retval = int(VERTEX_LOCATION);
140 case GL_NORMAL_ARRAY:
141 retval = int(NORMAL_LOCATION);
144 retval = int(COLOR_LOCATION);
146 case GL_POINT_SIZE_ARRAY_OES:
147 retval = int(POINTSIZE_LOCATION);
149 case GL_TEXTURE_COORD_ARRAY:
150 retval = int (TEXCOORD0_LOCATION + m_activeTexture);
152 case GL_MATRIX_INDEX_ARRAY_OES:
153 retval = int (MATRIXINDEX_LOCATION);
155 case GL_WEIGHT_ARRAY_OES:
156 retval = int (WEIGHT_LOCATION);
164 void GLClientState::getClientStatePointer(GLenum pname, GLvoid** params)
166 const GLClientState::VertexAttribState *state = NULL;
168 case GL_VERTEX_ARRAY_POINTER: {
169 state = getState(GLClientState::VERTEX_LOCATION);
172 case GL_NORMAL_ARRAY_POINTER: {
173 state = getState(GLClientState::NORMAL_LOCATION);
176 case GL_COLOR_ARRAY_POINTER: {
177 state = getState(GLClientState::COLOR_LOCATION);
180 case GL_TEXTURE_COORD_ARRAY_POINTER: {
181 state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
184 case GL_POINT_SIZE_ARRAY_POINTER_OES: {
185 state = getState(GLClientState::POINTSIZE_LOCATION);
188 case GL_MATRIX_INDEX_ARRAY_POINTER_OES: {
189 state = getState(GLClientState::MATRIXINDEX_LOCATION);
192 case GL_WEIGHT_ARRAY_POINTER_OES: {
193 state = getState(GLClientState::WEIGHT_LOCATION);
198 *params = state->data;
201 int GLClientState::setPixelStore(GLenum param, GLint value)
205 case GL_UNPACK_ALIGNMENT:
206 if (value == 1 || value == 2 || value == 4 || value == 8) {
207 m_pixelStore.unpack_alignment = value;
209 retval = GL_INVALID_VALUE;
212 case GL_PACK_ALIGNMENT:
213 if (value == 1 || value == 2 || value == 4 || value == 8) {
214 m_pixelStore.pack_alignment = value;
216 retval = GL_INVALID_VALUE;
220 retval = GL_INVALID_ENUM;
228 size_t GLClientState::pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const
230 if (width <= 0 || height <= 0) return 0;
232 int pixelsize = glUtilsPixelBitSize(format, type) >> 3;
234 int alignment = pack ? m_pixelStore.pack_alignment : m_pixelStore.unpack_alignment;
236 if (pixelsize == 0 ) {
237 ERR("unknown pixel size: width: %d height: %d format: %d type: %d pack: %d align: %d\n",
238 width, height, format, type, pack, alignment);
240 size_t linesize = pixelsize * width;
241 size_t aligned_linesize = int(linesize / alignment) * alignment;
242 if (aligned_linesize < linesize) {
243 aligned_linesize += alignment;
245 return aligned_linesize * height;
248 GLenum GLClientState::setActiveTextureUnit(GLenum texture)
250 GLuint unit = texture - GL_TEXTURE0;
251 if (unit >= MAX_TEXTURE_UNITS) {
252 return GL_INVALID_ENUM;
254 m_tex.activeUnit = &m_tex.unit[unit];
258 GLenum GLClientState::getActiveTextureUnit() const
260 return GL_TEXTURE0 + (m_tex.activeUnit - &m_tex.unit[0]);
263 void GLClientState::enableTextureTarget(GLenum target)
267 m_tex.activeUnit->enables |= (1u << TEXTURE_2D);
269 case GL_TEXTURE_EXTERNAL_OES:
270 m_tex.activeUnit->enables |= (1u << TEXTURE_EXTERNAL);
275 void GLClientState::disableTextureTarget(GLenum target)
279 m_tex.activeUnit->enables &= ~(1u << TEXTURE_2D);
281 case GL_TEXTURE_EXTERNAL_OES:
282 m_tex.activeUnit->enables &= ~(1u << TEXTURE_EXTERNAL);
287 GLenum GLClientState::getPriorityEnabledTarget(GLenum allDisabled) const
289 unsigned int enables = m_tex.activeUnit->enables;
290 if (enables & (1u << TEXTURE_EXTERNAL)) {
291 return GL_TEXTURE_EXTERNAL_OES;
292 } else if (enables & (1u << TEXTURE_2D)) {
293 return GL_TEXTURE_2D;
299 int GLClientState::compareTexId(const void* pid, const void* prec)
301 const GLuint* id = (const GLuint*)pid;
302 const TextureRec* rec = (const TextureRec*)prec;
303 return (GLint)(*id) - (GLint)rec->id;
306 GLenum GLClientState::bindTexture(GLenum target, GLuint texture,
309 GLboolean first = GL_FALSE;
310 TextureRec* texrec = NULL;
312 if (m_tex.textures) {
313 texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
314 m_tex.numTextures, sizeof(TextureRec), compareTexId);
317 if (!(texrec = addTextureRec(texture, target))) {
318 return GL_OUT_OF_MEMORY;
322 if (target != texrec->target) {
323 return GL_INVALID_OPERATION;
329 m_tex.activeUnit->texture[TEXTURE_2D] = texture;
331 case GL_TEXTURE_EXTERNAL_OES:
332 m_tex.activeUnit->texture[TEXTURE_EXTERNAL] = texture;
343 GLClientState::TextureRec* GLClientState::addTextureRec(GLuint id,
346 if (m_tex.numTextures == m_tex.allocTextures) {
347 const GLuint MAX_TEXTURES = 0xFFFFFFFFu;
350 if (MAX_TEXTURES - m_tex.allocTextures >= m_tex.allocTextures) {
351 newAlloc = MAX(4, 2 * m_tex.allocTextures);
353 if (m_tex.allocTextures == MAX_TEXTURES) {
356 newAlloc = MAX_TEXTURES;
359 TextureRec* newTextures = (TextureRec*)realloc(m_tex.textures,
360 newAlloc * sizeof(TextureRec));
365 m_tex.textures = newTextures;
366 m_tex.allocTextures = newAlloc;
369 TextureRec* tex = m_tex.textures + m_tex.numTextures;
370 TextureRec* prev = tex - 1;
371 while (tex != m_tex.textures && id < prev->id) {
375 tex->target = target;
381 GLuint GLClientState::getBoundTexture(GLenum target) const
385 return m_tex.activeUnit->texture[TEXTURE_2D];
386 case GL_TEXTURE_EXTERNAL_OES:
387 return m_tex.activeUnit->texture[TEXTURE_EXTERNAL];
393 void GLClientState::deleteTextures(GLsizei n, const GLuint* textures)
395 // Updating the textures array could be made more efficient when deleting
397 // - compacting the array could be done in a single pass once the deleted
398 // textures are marked, or
399 // - could swap deleted textures to the end and re-sort.
401 for (const GLuint* texture = textures; texture != textures + n; texture++) {
402 texrec = (TextureRec*)bsearch(texture, m_tex.textures,
403 m_tex.numTextures, sizeof(TextureRec), compareTexId);
405 const TextureRec* end = m_tex.textures + m_tex.numTextures;
406 memmove(texrec, texrec + 1,
407 (end - texrec - 1) * sizeof(TextureRec));
410 for (TextureUnit* unit = m_tex.unit;
411 unit != m_tex.unit + MAX_TEXTURE_UNITS;
414 if (unit->texture[TEXTURE_2D] == *texture) {
415 unit->texture[TEXTURE_2D] = 0;
416 } else if (unit->texture[TEXTURE_EXTERNAL] == *texture) {
417 unit->texture[TEXTURE_EXTERNAL] = 0;