1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 #ifndef _GL_CLIENT_STATE_H_ 17 #define _GL_CLIENT_STATE_H_ 18 19 #define GL_API 20 #ifndef ANDROID 21 #define GL_APIENTRY 22 #define GL_APIENTRYP 23 #endif 24 25 #include <GLES/gl.h> 26 #include <GLES/glext.h> 27 #include <GLES2/gl2.h> 28 #include <GLES2/gl2ext.h> 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include "ErrorLog.h" 33 #include "codec_defs.h" 34 35 class GLClientState { 36 public: 37 typedef enum { 38 VERTEX_LOCATION = 0, 39 NORMAL_LOCATION = 1, 40 COLOR_LOCATION = 2, 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, 51 WEIGHT_LOCATION = 13, 52 LAST_LOCATION = 14 53 } StateLocation; 54 55 typedef struct { 56 GLint enabled; 57 GLint size; 58 GLenum type; 59 GLsizei stride; 60 void *data; 61 GLuint bufferObject; 62 GLenum glConst; 63 unsigned int elementSize; 64 bool enableDirty; // true if any enable state has changed since last draw 65 bool normalized; 66 } VertexAttribState; 67 68 typedef struct { 69 int unpack_alignment; 70 int pack_alignment; 71 } PixelStoreState; 72 73 public: 74 GLClientState(int nLocations = CODEC_MAX_VERTEX_ATTRIBUTES); 75 ~GLClientState(); nLocations()76 int nLocations() { return m_nLocations; } pixelStoreState()77 const PixelStoreState *pixelStoreState() { return &m_pixelStore; } 78 int setPixelStore(GLenum param, GLint value); currentArrayVbo()79 GLuint currentArrayVbo() { return m_currentArrayVbo; } currentIndexVbo()80 GLuint currentIndexVbo() { return m_currentIndexVbo; } 81 void enable(int location, int state); 82 void setState(int location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data); 83 void setBufferObject(int location, GLuint id); 84 const VertexAttribState *getState(int location); 85 const VertexAttribState *getStateAndEnableDirty(int location, bool *enableChanged); 86 int getLocation(GLenum loc); setActiveTexture(int texUnit)87 void setActiveTexture(int texUnit) {m_activeTexture = texUnit; }; getActiveTexture()88 int getActiveTexture() const { return m_activeTexture; } 89 bindBuffer(GLenum target,GLuint id)90 int bindBuffer(GLenum target, GLuint id) 91 { 92 int err = 0; 93 switch(target) { 94 case GL_ARRAY_BUFFER: 95 m_currentArrayVbo = id; 96 break; 97 case GL_ELEMENT_ARRAY_BUFFER: 98 m_currentIndexVbo = id; 99 break; 100 default: 101 err = -1; 102 } 103 return err; 104 } 105 getBuffer(GLenum target)106 int getBuffer(GLenum target) 107 { 108 int ret=0; 109 switch (target) { 110 case GL_ARRAY_BUFFER: 111 ret = m_currentArrayVbo; 112 break; 113 case GL_ELEMENT_ARRAY_BUFFER: 114 ret = m_currentIndexVbo; 115 break; 116 default: 117 ret = -1; 118 } 119 return ret; 120 } 121 size_t pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack) const; 122 setCurrentProgram(GLint program)123 void setCurrentProgram(GLint program) { m_currentProgram = program; } currentProgram()124 GLint currentProgram() const { return m_currentProgram; } 125 126 private: 127 PixelStoreState m_pixelStore; 128 VertexAttribState *m_states; 129 int m_nLocations; 130 GLuint m_currentArrayVbo; 131 GLuint m_currentIndexVbo; 132 int m_activeTexture; 133 GLint m_currentProgram; 134 validLocation(int location)135 bool validLocation(int location) { return (location >= 0 && location < m_nLocations); } 136 public: 137 void getClientStatePointer(GLenum pname, GLvoid** params); 138 139 template <class T> getVertexAttribParameter(GLuint index,GLenum param,T * ptr)140 int getVertexAttribParameter(GLuint index, GLenum param, T *ptr) 141 { 142 bool handled = true; 143 const VertexAttribState *vertexAttrib = getState(index); 144 if (vertexAttrib == NULL) { 145 ERR("getVeterxAttriParameter for non existant index %d\n", index); 146 // set gl error; 147 return handled; 148 } 149 150 switch(param) { 151 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING: 152 *ptr = (T)(vertexAttrib->bufferObject); 153 break; 154 case GL_VERTEX_ATTRIB_ARRAY_ENABLED: 155 *ptr = (T)(vertexAttrib->enabled); 156 break; 157 case GL_VERTEX_ATTRIB_ARRAY_SIZE: 158 *ptr = (T)(vertexAttrib->size); 159 break; 160 case GL_VERTEX_ATTRIB_ARRAY_STRIDE: 161 *ptr = (T)(vertexAttrib->stride); 162 break; 163 case GL_VERTEX_ATTRIB_ARRAY_TYPE: 164 *ptr = (T)(vertexAttrib->type); 165 break; 166 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED: 167 *ptr = (T)(vertexAttrib->normalized); 168 break; 169 case GL_CURRENT_VERTEX_ATTRIB: 170 handled = false; 171 break; 172 default: 173 handled = false; 174 ERR("unknown vertex-attrib parameter param %d\n", param); 175 } 176 return handled; 177 } 178 179 template <class T> getClientStateParameter(GLenum param,T * ptr)180 bool getClientStateParameter(GLenum param, T* ptr) 181 { 182 bool isClientStateParam = false; 183 switch (param) { 184 case GL_CLIENT_ACTIVE_TEXTURE: { 185 GLint tex = getActiveTexture() + GL_TEXTURE0; 186 *ptr = tex; 187 isClientStateParam = true; 188 break; 189 } 190 case GL_VERTEX_ARRAY_SIZE: { 191 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION); 192 *ptr = state->size; 193 isClientStateParam = true; 194 break; 195 } 196 case GL_VERTEX_ARRAY_TYPE: { 197 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION); 198 *ptr = state->type; 199 isClientStateParam = true; 200 break; 201 } 202 case GL_VERTEX_ARRAY_STRIDE: { 203 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION); 204 *ptr = state->stride; 205 isClientStateParam = true; 206 break; 207 } 208 case GL_COLOR_ARRAY_SIZE: { 209 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION); 210 *ptr = state->size; 211 isClientStateParam = true; 212 break; 213 } 214 case GL_COLOR_ARRAY_TYPE: { 215 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION); 216 *ptr = state->type; 217 isClientStateParam = true; 218 break; 219 } 220 case GL_COLOR_ARRAY_STRIDE: { 221 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION); 222 *ptr = state->stride; 223 isClientStateParam = true; 224 break; 225 } 226 case GL_NORMAL_ARRAY_TYPE: { 227 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION); 228 *ptr = state->type; 229 isClientStateParam = true; 230 break; 231 } 232 case GL_NORMAL_ARRAY_STRIDE: { 233 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION); 234 *ptr = state->stride; 235 isClientStateParam = true; 236 break; 237 } 238 case GL_TEXTURE_COORD_ARRAY_SIZE: { 239 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION); 240 *ptr = state->size; 241 isClientStateParam = true; 242 break; 243 } 244 case GL_TEXTURE_COORD_ARRAY_TYPE: { 245 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION); 246 *ptr = state->type; 247 isClientStateParam = true; 248 break; 249 } 250 case GL_TEXTURE_COORD_ARRAY_STRIDE: { 251 const GLClientState::VertexAttribState *state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION); 252 *ptr = state->stride; 253 isClientStateParam = true; 254 break; 255 } 256 case GL_POINT_SIZE_ARRAY_TYPE_OES: { 257 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION); 258 *ptr = state->type; 259 isClientStateParam = true; 260 break; 261 } 262 case GL_POINT_SIZE_ARRAY_STRIDE_OES: { 263 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION); 264 *ptr = state->stride; 265 isClientStateParam = true; 266 break; 267 } 268 case GL_MATRIX_INDEX_ARRAY_SIZE_OES: { 269 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION); 270 *ptr = state->size; 271 isClientStateParam = true; 272 break; 273 } 274 case GL_MATRIX_INDEX_ARRAY_TYPE_OES: { 275 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION); 276 *ptr = state->type; 277 isClientStateParam = true; 278 break; 279 } 280 case GL_MATRIX_INDEX_ARRAY_STRIDE_OES: { 281 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION); 282 *ptr = state->stride; 283 isClientStateParam = true; 284 break; 285 } 286 case GL_WEIGHT_ARRAY_SIZE_OES: { 287 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION); 288 *ptr = state->size; 289 isClientStateParam = true; 290 break; 291 } 292 case GL_WEIGHT_ARRAY_TYPE_OES: { 293 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION); 294 *ptr = state->type; 295 isClientStateParam = true; 296 break; 297 } 298 case GL_WEIGHT_ARRAY_STRIDE_OES: { 299 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION); 300 *ptr = state->stride; 301 isClientStateParam = true; 302 break; 303 } 304 case GL_VERTEX_ARRAY_BUFFER_BINDING: { 305 const GLClientState::VertexAttribState *state = getState(GLClientState::VERTEX_LOCATION); 306 *ptr = state->bufferObject; 307 isClientStateParam = true; 308 break; 309 } 310 case GL_NORMAL_ARRAY_BUFFER_BINDING: { 311 const GLClientState::VertexAttribState *state = getState(GLClientState::NORMAL_LOCATION); 312 *ptr = state->bufferObject; 313 isClientStateParam = true; 314 break; 315 } 316 case GL_COLOR_ARRAY_BUFFER_BINDING: { 317 const GLClientState::VertexAttribState *state = getState(GLClientState::COLOR_LOCATION); 318 *ptr = state->bufferObject; 319 isClientStateParam = true; 320 break; 321 } 322 case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: { 323 const GLClientState::VertexAttribState *state = getState(getActiveTexture()+GLClientState::TEXCOORD0_LOCATION); 324 *ptr = state->bufferObject; 325 isClientStateParam = true; 326 break; 327 } 328 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: { 329 const GLClientState::VertexAttribState *state = getState(GLClientState::POINTSIZE_LOCATION); 330 *ptr = state->bufferObject; 331 isClientStateParam = true; 332 break; 333 } 334 case GL_MATRIX_INDEX_ARRAY_BUFFER_BINDING_OES: { 335 const GLClientState::VertexAttribState *state = getState(GLClientState::MATRIXINDEX_LOCATION); 336 *ptr = state->bufferObject; 337 isClientStateParam = true; 338 break; 339 } 340 case GL_WEIGHT_ARRAY_BUFFER_BINDING_OES: { 341 const GLClientState::VertexAttribState *state = getState(GLClientState::WEIGHT_LOCATION); 342 *ptr = state->bufferObject; 343 isClientStateParam = true; 344 break; 345 } 346 case GL_ARRAY_BUFFER_BINDING: { 347 int buffer = getBuffer(GL_ARRAY_BUFFER); 348 *ptr = buffer; 349 isClientStateParam = true; 350 break; 351 } 352 case GL_ELEMENT_ARRAY_BUFFER_BINDING: { 353 int buffer = getBuffer(GL_ELEMENT_ARRAY_BUFFER); 354 *ptr = buffer; 355 isClientStateParam = true; 356 break; 357 } 358 } 359 return isClientStateParam; 360 } 361 362 }; 363 #endif 364