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 #ifndef _GL_CLIENT_STATE_H_
17 #define _GL_CLIENT_STATE_H_
26 #include <GLES/glext.h>
27 #include <GLES2/gl2.h>
28 #include <GLES2/gl2ext.h>
33 #include "codec_defs.h"
41 POINTSIZE_LOCATION = 3,
42 TEXCOORD0_LOCATION = 4,
43 TEXCOORD1_LOCATION = 5,
44 TEXCOORD2_LOCATION = 6,
45 TEXCOORD3_LOCATION = 7,
46 TEXCOORD4_LOCATION = 8,
47 TEXCOORD5_LOCATION = 9,
48 TEXCOORD6_LOCATION = 10,
49 TEXCOORD7_LOCATION = 11,
50 MATRIXINDEX_LOCATION = 12,
63 unsigned int elementSize;
64 bool enableDirty; // true if any enable state has changed since last draw
74 MAX_TEXTURE_UNITS = 32,
78 GLClientState(int nLocations = CODEC_MAX_VERTEX_ATTRIBUTES);
80 int nLocations() { return m_nLocations; }
81 const PixelStoreState *pixelStoreState() { return &m_pixelStore; }
82 int setPixelStore(GLenum param, GLint value);
83 GLuint currentArrayVbo() { return m_currentArrayVbo; }
84 GLuint currentIndexVbo() { return m_currentIndexVbo; }
85 void enable(int location, int state);
86 void setState(int location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data);
87 void setBufferObject(int location, GLuint id);
88 const VertexAttribState *getState(int location);
89 const VertexAttribState *getStateAndEnableDirty(int location, bool *enableChanged);
90 int getLocation(GLenum loc);
91 void setActiveTexture(int texUnit) {m_activeTexture = texUnit; };
92 int getActiveTexture() const { return m_activeTexture; }
94 int bindBuffer(GLenum target, GLuint id)
99 m_currentArrayVbo = id;
101 case GL_ELEMENT_ARRAY_BUFFER:
102 m_currentIndexVbo = id;
110 int getBuffer(GLenum target)
114 case GL_ARRAY_BUFFER:
115 ret = m_currentArrayVbo;
117 case GL_ELEMENT_ARRAY_BUFFER:
118 ret = m_currentIndexVbo;
125 size_t pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const;
127 void setCurrentProgram(GLint program) { m_currentProgram = program; }
128 GLint currentProgram() const { return m_currentProgram; }
130 /* OES_EGL_image_external
132 * These functions manipulate GL state which interacts with the
133 * OES_EGL_image_external extension, to support client-side emulation on
134 * top of host implementations that don't have it.
136 * Most of these calls should only be used with TEXTURE_2D or
137 * TEXTURE_EXTERNAL_OES texture targets; TEXTURE_CUBE_MAP or other extension
138 * targets should bypass this. An exception is bindTexture(), which should
139 * see all glBindTexture() calls for any target.
142 // glActiveTexture(GL_TEXTURE0 + i)
143 // Sets the active texture unit. Up to MAX_TEXTURE_UNITS are supported.
144 GLenum setActiveTextureUnit(GLenum texture);
145 GLenum getActiveTextureUnit() const;
147 // glEnable(GL_TEXTURE_(2D|EXTERNAL_OES))
148 void enableTextureTarget(GLenum target);
150 // glDisable(GL_TEXTURE_(2D|EXTERNAL_OES))
151 void disableTextureTarget(GLenum target);
153 // Implements the target priority logic:
154 // * Return GL_TEXTURE_EXTERNAL_OES if enabled, else
155 // * Return GL_TEXTURE_2D if enabled, else
156 // * Return the allDisabled value.
157 // For some cases passing GL_TEXTURE_2D for allDisabled makes callee code
158 // simpler; for other cases passing a recognizable enum like GL_ZERO or
159 // GL_INVALID_ENUM is appropriate.
160 GLenum getPriorityEnabledTarget(GLenum allDisabled) const;
162 // glBindTexture(GL_TEXTURE_*, ...)
163 // Set the target binding of the active texture unit to texture. Returns
164 // GL_NO_ERROR on success or GL_INVALID_OPERATION if the texture has
165 // previously been bound to a different target. If firstUse is not NULL,
166 // it is set to indicate whether this is the first use of the texture.
167 // For accurate error detection, bindTexture should be called for *all*
168 // targets, not just 2D and EXTERNAL_OES.
169 GLenum bindTexture(GLenum target, GLuint texture, GLboolean* firstUse);
171 // Return the texture currently bound to GL_TEXTURE_(2D|EXTERNAL_OES).
172 GLuint getBoundTexture(GLenum target) const;
174 // glDeleteTextures(...)
175 // Remove references to the to-be-deleted textures.
176 void deleteTextures(GLsizei n, const GLuint* textures);
179 PixelStoreState m_pixelStore;
180 VertexAttribState *m_states;
182 GLuint m_currentArrayVbo;
183 GLuint m_currentIndexVbo;
185 GLint m_currentProgram;
187 bool validLocation(int location) { return (location >= 0 && location < m_nLocations); }
191 TEXTURE_EXTERNAL = 1,
195 unsigned int enables;
196 GLuint texture[TEXTURE_TARGET_COUNT];
202 struct TextureState {
203 TextureUnit unit[MAX_TEXTURE_UNITS];
204 TextureUnit* activeUnit;
205 TextureRec* textures;
207 GLuint allocTextures;
211 static int compareTexId(const void* pid, const void* prec);
212 TextureRec* addTextureRec(GLuint id, GLenum target);
215 void getClientStatePointer(GLenum pname, GLvoid** params);
218 int getVertexAttribParameter(GLuint index, GLenum param, T *ptr)
221 const VertexAttribState *vertexAttrib = getState(index);
222 if (vertexAttrib == NULL) {
223 ERR("getVeterxAttriParameter for non existant index %d\n", index);
229 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
230 *ptr = (T)(vertexAttrib->bufferObject);
232 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
233 *ptr = (T)(vertexAttrib->enabled);
235 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
236 *ptr = (T)(vertexAttrib->size);
238 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
239 *ptr = (T)(vertexAttrib->stride);
241 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
242 *ptr = (T)(vertexAttrib->type);
244 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
245 *ptr = (T)(vertexAttrib->normalized);
247 case GL_CURRENT_VERTEX_ATTRIB:
252 ERR("unknown vertex-attrib parameter param %d\n", param);
258 bool getClientStateParameter(GLenum param, T* ptr)
260 bool isClientStateParam = false;
262 case GL_CLIENT_ACTIVE_TEXTURE: {
263 GLint tex = getActiveTexture() + GL_TEXTURE0;
265 isClientStateParam = true;
268 case GL_VERTEX_ARRAY_SIZE: {
269 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
271 isClientStateParam = true;
274 case GL_VERTEX_ARRAY_TYPE: {
275 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
277 isClientStateParam = true;
280 case GL_VERTEX_ARRAY_STRIDE: {
281 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
282 *ptr = state->stride;
283 isClientStateParam = true;
286 case GL_COLOR_ARRAY_SIZE: {
287 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
289 isClientStateParam = true;
292 case GL_COLOR_ARRAY_TYPE: {
293 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
295 isClientStateParam = true;
298 case GL_COLOR_ARRAY_STRIDE: {
299 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
300 *ptr = state->stride;
301 isClientStateParam = true;
304 case GL_NORMAL_ARRAY_TYPE: {
305 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION);
307 isClientStateParam = true;
310 case GL_NORMAL_ARRAY_STRIDE: {
311 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION);
312 *ptr = state->stride;
313 isClientStateParam = true;
316 case GL_TEXTURE_COORD_ARRAY_SIZE: {
317 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
319 isClientStateParam = true;
322 case GL_TEXTURE_COORD_ARRAY_TYPE: {
323 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
325 isClientStateParam = true;
328 case GL_TEXTURE_COORD_ARRAY_STRIDE: {
329 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
330 *ptr = state->stride;
331 isClientStateParam = true;
334 case GL_POINT_SIZE_ARRAY_TYPE_OES: {
335 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION);
337 isClientStateParam = true;
340 case GL_POINT_SIZE_ARRAY_STRIDE_OES: {
341 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION);
342 *ptr = state->stride;
343 isClientStateParam = true;
346 case GL_MATRIX_INDEX_ARRAY_SIZE_OES: {
347 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
349 isClientStateParam = true;
352 case GL_MATRIX_INDEX_ARRAY_TYPE_OES: {
353 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
355 isClientStateParam = true;
358 case GL_MATRIX_INDEX_ARRAY_STRIDE_OES: {
359 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
360 *ptr = state->stride;
361 isClientStateParam = true;
364 case GL_WEIGHT_ARRAY_SIZE_OES: {
365 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
367 isClientStateParam = true;
370 case GL_WEIGHT_ARRAY_TYPE_OES: {
371 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
373 isClientStateParam = true;
376 case GL_WEIGHT_ARRAY_STRIDE_OES: {
377 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
378 *ptr = state->stride;
379 isClientStateParam = true;
382 case GL_VERTEX_ARRAY_BUFFER_BINDING: {
383 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
384 *ptr = state->bufferObject;
385 isClientStateParam = true;
388 case GL_NORMAL_ARRAY_BUFFER_BINDING: {
389 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION);
390 *ptr = state->bufferObject;
391 isClientStateParam = true;
394 case GL_COLOR_ARRAY_BUFFER_BINDING: {
395 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
396 *ptr = state->bufferObject;
397 isClientStateParam = true;
400 case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: {
401 const GLClientState::VertexAttribState *state = getState(getActiveTexture()+GLClientState::TEXCOORD0_LOCATION);
402 *ptr = state->bufferObject;
403 isClientStateParam = true;
406 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: {
407 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION);
408 *ptr = state->bufferObject;
409 isClientStateParam = true;
412 case GL_MATRIX_INDEX_ARRAY_BUFFER_BINDING_OES: {
413 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
414 *ptr = state->bufferObject;
415 isClientStateParam = true;
418 case GL_WEIGHT_ARRAY_BUFFER_BINDING_OES: {
419 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
420 *ptr = state->bufferObject;
421 isClientStateParam = true;
424 case GL_ARRAY_BUFFER_BINDING: {
425 int buffer = getBuffer(GL_ARRAY_BUFFER);
427 isClientStateParam = true;
430 case GL_ELEMENT_ARRAY_BUFFER_BINDING: {
431 int buffer = getBuffer(GL_ELEMENT_ARRAY_BUFFER);
433 isClientStateParam = true;
437 return isClientStateParam;