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; }
93 void setMaxVertexAttribs(int val) {
94 m_maxVertexAttribs = val;
95 m_maxVertexAttribsDirty = false;
98 void unBindBuffer(GLuint id)
100 if (m_currentArrayVbo == id) m_currentArrayVbo = 0;
101 else if (m_currentIndexVbo == id) m_currentIndexVbo = 0;
104 int bindBuffer(GLenum target, GLuint id)
108 case GL_ARRAY_BUFFER:
109 m_currentArrayVbo = id;
111 case GL_ELEMENT_ARRAY_BUFFER:
112 m_currentIndexVbo = id;
120 int getBuffer(GLenum target)
124 case GL_ARRAY_BUFFER:
125 ret = m_currentArrayVbo;
127 case GL_ELEMENT_ARRAY_BUFFER:
128 ret = m_currentIndexVbo;
135 size_t pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const;
137 void setCurrentProgram(GLint program) { m_currentProgram = program; }
138 GLint currentProgram() const { return m_currentProgram; }
140 /* OES_EGL_image_external
142 * These functions manipulate GL state which interacts with the
143 * OES_EGL_image_external extension, to support client-side emulation on
144 * top of host implementations that don't have it.
146 * Most of these calls should only be used with TEXTURE_2D or
147 * TEXTURE_EXTERNAL_OES texture targets; TEXTURE_CUBE_MAP or other extension
148 * targets should bypass this. An exception is bindTexture(), which should
149 * see all glBindTexture() calls for any target.
152 // glActiveTexture(GL_TEXTURE0 + i)
153 // Sets the active texture unit. Up to MAX_TEXTURE_UNITS are supported.
154 GLenum setActiveTextureUnit(GLenum texture);
155 GLenum getActiveTextureUnit() const;
157 // glEnable(GL_TEXTURE_(2D|EXTERNAL_OES))
158 void enableTextureTarget(GLenum target);
160 // glDisable(GL_TEXTURE_(2D|EXTERNAL_OES))
161 void disableTextureTarget(GLenum target);
163 // Implements the target priority logic:
164 // * Return GL_TEXTURE_EXTERNAL_OES if enabled, else
165 // * Return GL_TEXTURE_2D if enabled, else
166 // * Return the allDisabled value.
167 // For some cases passing GL_TEXTURE_2D for allDisabled makes callee code
168 // simpler; for other cases passing a recognizable enum like GL_ZERO or
169 // GL_INVALID_ENUM is appropriate.
170 GLenum getPriorityEnabledTarget(GLenum allDisabled) const;
172 // glBindTexture(GL_TEXTURE_*, ...)
173 // Set the target binding of the active texture unit to texture. Returns
174 // GL_NO_ERROR on success or GL_INVALID_OPERATION if the texture has
175 // previously been bound to a different target. If firstUse is not NULL,
176 // it is set to indicate whether this is the first use of the texture.
177 // For accurate error detection, bindTexture should be called for *all*
178 // targets, not just 2D and EXTERNAL_OES.
179 GLenum bindTexture(GLenum target, GLuint texture, GLboolean* firstUse);
181 // Return the texture currently bound to GL_TEXTURE_(2D|EXTERNAL_OES).
182 GLuint getBoundTexture(GLenum target) const;
184 // glDeleteTextures(...)
185 // Remove references to the to-be-deleted textures.
186 void deleteTextures(GLsizei n, const GLuint* textures);
189 PixelStoreState m_pixelStore;
190 VertexAttribState *m_states;
191 int m_maxVertexAttribs;
192 bool m_maxVertexAttribsDirty;
194 GLuint m_currentArrayVbo;
195 GLuint m_currentIndexVbo;
197 GLint m_currentProgram;
199 bool validLocation(int location) { return (location >= 0 && location < m_nLocations); }
203 TEXTURE_EXTERNAL = 1,
207 unsigned int enables;
208 GLuint texture[TEXTURE_TARGET_COUNT];
214 struct TextureState {
215 TextureUnit unit[MAX_TEXTURE_UNITS];
216 TextureUnit* activeUnit;
217 TextureRec* textures;
219 GLuint allocTextures;
223 static int compareTexId(const void* pid, const void* prec);
224 TextureRec* addTextureRec(GLuint id, GLenum target);
227 void getClientStatePointer(GLenum pname, GLvoid** params);
230 int getVertexAttribParameter(GLuint index, GLenum param, T *ptr)
233 const VertexAttribState *vertexAttrib = getState(index);
234 if (vertexAttrib == NULL) {
235 ERR("getVeterxAttriParameter for non existant index %d\n", index);
241 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
242 *ptr = (T)(vertexAttrib->bufferObject);
244 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
245 *ptr = (T)(vertexAttrib->enabled);
247 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
248 *ptr = (T)(vertexAttrib->size);
250 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
251 *ptr = (T)(vertexAttrib->stride);
253 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
254 *ptr = (T)(vertexAttrib->type);
256 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
257 *ptr = (T)(vertexAttrib->normalized);
259 case GL_CURRENT_VERTEX_ATTRIB:
264 ERR("unknown vertex-attrib parameter param %d\n", param);
270 bool getClientStateParameter(GLenum param, T* ptr)
272 bool isClientStateParam = false;
274 case GL_CLIENT_ACTIVE_TEXTURE: {
275 GLint tex = getActiveTexture() + GL_TEXTURE0;
277 isClientStateParam = true;
280 case GL_VERTEX_ARRAY_SIZE: {
281 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
283 isClientStateParam = true;
286 case GL_VERTEX_ARRAY_TYPE: {
287 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
289 isClientStateParam = true;
292 case GL_VERTEX_ARRAY_STRIDE: {
293 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
294 *ptr = state->stride;
295 isClientStateParam = true;
298 case GL_COLOR_ARRAY_SIZE: {
299 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
301 isClientStateParam = true;
304 case GL_COLOR_ARRAY_TYPE: {
305 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
307 isClientStateParam = true;
310 case GL_COLOR_ARRAY_STRIDE: {
311 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
312 *ptr = state->stride;
313 isClientStateParam = true;
316 case GL_NORMAL_ARRAY_TYPE: {
317 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION);
319 isClientStateParam = true;
322 case GL_NORMAL_ARRAY_STRIDE: {
323 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION);
324 *ptr = state->stride;
325 isClientStateParam = true;
328 case GL_TEXTURE_COORD_ARRAY_SIZE: {
329 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
331 isClientStateParam = true;
334 case GL_TEXTURE_COORD_ARRAY_TYPE: {
335 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
337 isClientStateParam = true;
340 case GL_TEXTURE_COORD_ARRAY_STRIDE: {
341 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
342 *ptr = state->stride;
343 isClientStateParam = true;
346 case GL_POINT_SIZE_ARRAY_TYPE_OES: {
347 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION);
349 isClientStateParam = true;
352 case GL_POINT_SIZE_ARRAY_STRIDE_OES: {
353 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION);
354 *ptr = state->stride;
355 isClientStateParam = true;
358 case GL_MATRIX_INDEX_ARRAY_SIZE_OES: {
359 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
361 isClientStateParam = true;
364 case GL_MATRIX_INDEX_ARRAY_TYPE_OES: {
365 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
367 isClientStateParam = true;
370 case GL_MATRIX_INDEX_ARRAY_STRIDE_OES: {
371 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
372 *ptr = state->stride;
373 isClientStateParam = true;
376 case GL_WEIGHT_ARRAY_SIZE_OES: {
377 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
379 isClientStateParam = true;
382 case GL_WEIGHT_ARRAY_TYPE_OES: {
383 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
385 isClientStateParam = true;
388 case GL_WEIGHT_ARRAY_STRIDE_OES: {
389 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
390 *ptr = state->stride;
391 isClientStateParam = true;
394 case GL_VERTEX_ARRAY_BUFFER_BINDING: {
395 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION);
396 *ptr = state->bufferObject;
397 isClientStateParam = true;
400 case GL_NORMAL_ARRAY_BUFFER_BINDING: {
401 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION);
402 *ptr = state->bufferObject;
403 isClientStateParam = true;
406 case GL_COLOR_ARRAY_BUFFER_BINDING: {
407 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION);
408 *ptr = state->bufferObject;
409 isClientStateParam = true;
412 case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: {
413 const GLClientState::VertexAttribState *state = getState(getActiveTexture()+GLClientState::TEXCOORD0_LOCATION);
414 *ptr = state->bufferObject;
415 isClientStateParam = true;
418 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: {
419 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION);
420 *ptr = state->bufferObject;
421 isClientStateParam = true;
424 case GL_MATRIX_INDEX_ARRAY_BUFFER_BINDING_OES: {
425 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION);
426 *ptr = state->bufferObject;
427 isClientStateParam = true;
430 case GL_WEIGHT_ARRAY_BUFFER_BINDING_OES: {
431 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION);
432 *ptr = state->bufferObject;
433 isClientStateParam = true;
436 case GL_ARRAY_BUFFER_BINDING: {
437 int buffer = getBuffer(GL_ARRAY_BUFFER);
439 isClientStateParam = true;
442 case GL_ELEMENT_ARRAY_BUFFER_BINDING: {
443 int buffer = getBuffer(GL_ELEMENT_ARRAY_BUFFER);
445 isClientStateParam = true;
448 case GL_MAX_VERTEX_ATTRIBS: {
449 if (m_maxVertexAttribsDirty) {
450 isClientStateParam = false;
452 *ptr = m_maxVertexAttribs;
453 isClientStateParam = true;
458 return isClientStateParam;