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;
39 m_currentArrayVbo = 0;
40 m_currentIndexVbo = 0;
42 m_states[VERTEX_LOCATION].glConst = GL_VERTEX_ARRAY;
43 m_states[NORMAL_LOCATION].glConst = GL_NORMAL_ARRAY;
44 m_states[COLOR_LOCATION].glConst = GL_COLOR_ARRAY;
45 m_states[POINTSIZE_LOCATION].glConst = GL_POINT_SIZE_ARRAY_OES;
46 m_states[TEXCOORD0_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
47 m_states[TEXCOORD1_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
48 m_states[TEXCOORD2_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
49 m_states[TEXCOORD3_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
50 m_states[TEXCOORD4_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
51 m_states[TEXCOORD5_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
52 m_states[TEXCOORD6_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
53 m_states[TEXCOORD7_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
54 m_states[MATRIXINDEX_LOCATION].glConst = GL_MATRIX_INDEX_ARRAY_OES;
55 m_states[WEIGHT_LOCATION].glConst = GL_WEIGHT_ARRAY_OES;
59 m_pixelStore.unpack_alignment = 4;
60 m_pixelStore.pack_alignment = 4;
62 memset(m_tex.unit, 0, sizeof(m_tex.unit));
63 m_tex.activeUnit = &m_tex.unit[0];
64 m_tex.textures = NULL;
65 m_tex.numTextures = 0;
66 m_tex.allocTextures = 0;
69 GLClientState::~GLClientState()
74 void GLClientState::enable(int location, int state)
76 if (!validLocation(location)) {
80 m_states[location].enableDirty |= (state != m_states[location].enabled);
81 m_states[location].enabled = state;
84 void GLClientState::setState(int location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data)
86 if (!validLocation(location)) {
89 m_states[location].size = size;
90 m_states[location].type = type;
91 m_states[location].stride = stride;
92 m_states[location].data = (void*)data;
93 m_states[location].bufferObject = m_currentArrayVbo;
94 m_states[location].elementSize = glSizeof(type) * size;
95 m_states[location].normalized = normalized;
98 void GLClientState::setBufferObject(int location, GLuint id)
100 if (!validLocation(location)) {
104 m_states[location].bufferObject = id;
107 const GLClientState::VertexAttribState * GLClientState::getState(int location)
109 if (!validLocation(location)) {
112 return & m_states[location];
115 const GLClientState::VertexAttribState * GLClientState::getStateAndEnableDirty(int location, bool *enableChanged)
117 if (!validLocation(location)) {
122 *enableChanged = m_states[location].enableDirty;
125 m_states[location].enableDirty = false;
126 return & m_states[location];
129 int GLClientState::getLocation(GLenum loc)
134 case GL_VERTEX_ARRAY:
135 retval = int(VERTEX_LOCATION);
137 case GL_NORMAL_ARRAY:
138 retval = int(NORMAL_LOCATION);
141 retval = int(COLOR_LOCATION);
143 case GL_POINT_SIZE_ARRAY_OES:
144 retval = int(POINTSIZE_LOCATION);
146 case GL_TEXTURE_COORD_ARRAY:
147 retval = int (TEXCOORD0_LOCATION + m_activeTexture);
149 case GL_MATRIX_INDEX_ARRAY_OES:
150 retval = int (MATRIXINDEX_LOCATION);
152 case GL_WEIGHT_ARRAY_OES:
153 retval = int (WEIGHT_LOCATION);
161 void GLClientState::getClientStatePointer(GLenum pname, GLvoid** params)
163 const GLClientState::VertexAttribState *state = NULL;
165 case GL_VERTEX_ARRAY_POINTER: {
166 state = getState(GLClientState::VERTEX_LOCATION);
169 case GL_NORMAL_ARRAY_POINTER: {
170 state = getState(GLClientState::NORMAL_LOCATION);
173 case GL_COLOR_ARRAY_POINTER: {
174 state = getState(GLClientState::COLOR_LOCATION);
177 case GL_TEXTURE_COORD_ARRAY_POINTER: {
178 state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
181 case GL_POINT_SIZE_ARRAY_POINTER_OES: {
182 state = getState(GLClientState::POINTSIZE_LOCATION);
185 case GL_MATRIX_INDEX_ARRAY_POINTER_OES: {
186 state = getState(GLClientState::MATRIXINDEX_LOCATION);
189 case GL_WEIGHT_ARRAY_POINTER_OES: {
190 state = getState(GLClientState::WEIGHT_LOCATION);
195 *params = state->data;
198 int GLClientState::setPixelStore(GLenum param, GLint value)
202 case GL_UNPACK_ALIGNMENT:
203 if (value == 1 || value == 2 || value == 4 || value == 8) {
204 m_pixelStore.unpack_alignment = value;
206 retval = GL_INVALID_VALUE;
209 case GL_PACK_ALIGNMENT:
210 if (value == 1 || value == 2 || value == 4 || value == 8) {
211 m_pixelStore.pack_alignment = value;
213 retval = GL_INVALID_VALUE;
217 retval = GL_INVALID_ENUM;
225 size_t GLClientState::pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const
227 int pixelsize = glUtilsPixelBitSize(format, type) >> 3;
229 int alignment = pack ? m_pixelStore.pack_alignment : m_pixelStore.unpack_alignment;
231 if (pixelsize == 0 ) {
232 ERR("unknown pixel size: width: %d height: %d format: %d type: %d pack: %d align: %d\n",
233 width, height, format, type, pack, alignment);
235 size_t linesize = pixelsize * width;
236 size_t aligned_linesize = int(linesize / alignment) * alignment;
237 if (aligned_linesize < linesize) {
238 aligned_linesize += alignment;
240 return aligned_linesize * height;
243 GLenum GLClientState::setActiveTextureUnit(GLenum texture)
245 GLuint unit = texture - GL_TEXTURE0;
246 if (unit >= MAX_TEXTURE_UNITS) {
247 return GL_INVALID_OPERATION;
249 m_tex.activeUnit = &m_tex.unit[unit];
253 GLenum GLClientState::getActiveTextureUnit() const
255 return GL_TEXTURE0 + (m_tex.activeUnit - &m_tex.unit[0]);
258 void GLClientState::enableTextureTarget(GLenum target)
262 m_tex.activeUnit->enables |= (1u << TEXTURE_2D);
264 case GL_TEXTURE_EXTERNAL_OES:
265 m_tex.activeUnit->enables |= (1u << TEXTURE_EXTERNAL);
270 void GLClientState::disableTextureTarget(GLenum target)
274 m_tex.activeUnit->enables &= ~(1u << TEXTURE_2D);
276 case GL_TEXTURE_EXTERNAL_OES:
277 m_tex.activeUnit->enables &= ~(1u << TEXTURE_EXTERNAL);
282 GLenum GLClientState::getPriorityEnabledTarget(GLenum allDisabled) const
284 unsigned int enables = m_tex.activeUnit->enables;
285 if (enables & (1u << TEXTURE_EXTERNAL)) {
286 return GL_TEXTURE_EXTERNAL_OES;
287 } else if (enables & (1u << TEXTURE_2D)) {
288 return GL_TEXTURE_2D;
294 int GLClientState::compareTexId(const void* pid, const void* prec)
296 const GLuint* id = (const GLuint*)pid;
297 const TextureRec* rec = (const TextureRec*)prec;
298 return (GLint)(*id) - (GLint)rec->id;
301 GLenum GLClientState::bindTexture(GLenum target, GLuint texture,
304 GLboolean first = GL_FALSE;
305 TextureRec* texrec = NULL;
307 if (m_tex.textures) {
308 texrec = (TextureRec*)bsearch(&texture, m_tex.textures,
309 m_tex.numTextures, sizeof(TextureRec), compareTexId);
312 if (!(texrec = addTextureRec(texture, target))) {
313 return GL_OUT_OF_MEMORY;
317 if (target != texrec->target) {
318 return GL_INVALID_OPERATION;
324 m_tex.activeUnit->texture[TEXTURE_2D] = texture;
326 case GL_TEXTURE_EXTERNAL_OES:
327 m_tex.activeUnit->texture[TEXTURE_EXTERNAL] = texture;
338 GLClientState::TextureRec* GLClientState::addTextureRec(GLuint id,
341 if (m_tex.numTextures == m_tex.allocTextures) {
342 const GLuint MAX_TEXTURES = 0xFFFFFFFFu;
345 if (MAX_TEXTURES - m_tex.allocTextures >= m_tex.allocTextures) {
346 newAlloc = MAX(4, 2 * m_tex.allocTextures);
348 if (m_tex.allocTextures == MAX_TEXTURES) {
351 newAlloc = MAX_TEXTURES;
354 TextureRec* newTextures = (TextureRec*)realloc(m_tex.textures,
355 newAlloc * sizeof(TextureRec));
360 m_tex.textures = newTextures;
361 m_tex.allocTextures = newAlloc;
364 TextureRec* tex = m_tex.textures + m_tex.numTextures;
365 TextureRec* prev = tex - 1;
366 while (tex != m_tex.textures && id < prev->id) {
370 tex->target = target;
376 GLuint GLClientState::getBoundTexture(GLenum target) const
380 return m_tex.activeUnit->texture[TEXTURE_2D];
381 case GL_TEXTURE_EXTERNAL_OES:
382 return m_tex.activeUnit->texture[TEXTURE_EXTERNAL];
388 void GLClientState::deleteTextures(GLsizei n, const GLuint* textures)
390 // Updating the textures array could be made more efficient when deleting
392 // - compacting the array could be done in a single pass once the deleted
393 // textures are marked, or
394 // - could swap deleted textures to the end and re-sort.
396 for (const GLuint* texture = textures; texture != textures + n; texture++) {
397 texrec = (TextureRec*)bsearch(texture, m_tex.textures,
398 m_tex.numTextures, sizeof(TextureRec), compareTexId);
400 const TextureRec* end = m_tex.textures + m_tex.numTextures;
401 memmove(texrec, texrec + 1,
402 (end - texrec - 1) * sizeof(TextureRec));
405 for (TextureUnit* unit = m_tex.unit;
406 unit != m_tex.unit + MAX_TEXTURE_UNITS;
409 if (unit->texture[TEXTURE_2D] == *texture) {
410 unit->texture[TEXTURE_2D] = 0;
411 } else if (unit->texture[TEXTURE_EXTERNAL] == *texture) {
412 unit->texture[TEXTURE_EXTERNAL] = 0;