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 "GLEncoder.h"
18 #include "FixedBuffer.h"
19 #include <cutils/log.h>
23 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 static GLubyte *gVendorString= (GLubyte *) "Android";
27 static GLubyte *gRendererString= (GLubyte *) "Android HW-GLES 1.0";
28 static GLubyte *gVersionString= (GLubyte *) "OpenGL ES-CM 1.0";
29 static GLubyte *gExtensionsString= (GLubyte *) "GL_OES_EGL_image_external ";
31 #define SET_ERROR_IF(condition,err) if((condition)) { \
32 ALOGE("%s:%s:%d GL error 0x%x\n", __FILE__, __FUNCTION__, __LINE__, err); \
38 #define RET_AND_SET_ERROR_IF(condition,err,ret) if((condition)) { \
39 ALOGE("%s:%s:%d GL error 0x%x\n", __FILE__, __FUNCTION__, __LINE__, err); \
44 GLenum GLEncoder::s_glGetError(void * self)
46 GLEncoder *ctx = (GLEncoder *)self;
47 GLenum err = ctx->getError();
48 if(err != GL_NO_ERROR) {
49 ctx->setError(GL_NO_ERROR);
53 return ctx->m_glGetError_enc(self);
57 GLint * GLEncoder::getCompressedTextureFormats()
59 if (m_compressedTextureFormats == NULL) {
60 this->glGetIntegerv(this, GL_NUM_COMPRESSED_TEXTURE_FORMATS,
61 &m_num_compressedTextureFormats);
62 if (m_num_compressedTextureFormats > 0) {
63 // get number of texture formats;
64 m_compressedTextureFormats = new GLint[m_num_compressedTextureFormats];
65 this->glGetCompressedTextureFormats(this, m_num_compressedTextureFormats, m_compressedTextureFormats);
68 return m_compressedTextureFormats;
71 void GLEncoder::s_glGetIntegerv(void *self, GLenum param, GLint *ptr)
73 GLEncoder *ctx = (GLEncoder *)self;
74 assert(ctx->m_state != NULL);
75 GLClientState* state = ctx->m_state;
78 case GL_COMPRESSED_TEXTURE_FORMATS: {
79 GLint * compressedTextureFormats = ctx->getCompressedTextureFormats();
80 if (ctx->m_num_compressedTextureFormats > 0 &&
81 compressedTextureFormats != NULL) {
82 memcpy(ptr, compressedTextureFormats,
83 ctx->m_num_compressedTextureFormats * sizeof(GLint));
88 case GL_MAX_TEXTURE_UNITS:
89 ctx->m_glGetIntegerv_enc(self, param, ptr);
90 *ptr = MIN(*ptr, GLClientState::MAX_TEXTURE_UNITS);
93 case GL_TEXTURE_BINDING_2D:
94 *ptr = state->getBoundTexture(GL_TEXTURE_2D);
97 case GL_TEXTURE_BINDING_EXTERNAL_OES:
98 *ptr = state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES);
102 if (!state->getClientStateParameter<GLint>(param,ptr)) {
103 ctx->m_glGetIntegerv_enc(self, param, ptr);
109 void GLEncoder::s_glGetFloatv(void *self, GLenum param, GLfloat *ptr)
111 GLEncoder *ctx = (GLEncoder *)self;
112 assert(ctx->m_state != NULL);
113 GLClientState* state = ctx->m_state;
116 case GL_COMPRESSED_TEXTURE_FORMATS: {
117 GLint * compressedTextureFormats = ctx->getCompressedTextureFormats();
118 if (ctx->m_num_compressedTextureFormats > 0 &&
119 compressedTextureFormats != NULL) {
120 for (int i = 0; i < ctx->m_num_compressedTextureFormats; i++) {
121 ptr[i] = (GLfloat) compressedTextureFormats[i];
127 case GL_MAX_TEXTURE_UNITS:
128 ctx->m_glGetFloatv_enc(self, param, ptr);
129 *ptr = MIN(*ptr, (GLfloat)GLClientState::MAX_TEXTURE_UNITS);
132 case GL_TEXTURE_BINDING_2D:
133 *ptr = (GLfloat)state->getBoundTexture(GL_TEXTURE_2D);
136 case GL_TEXTURE_BINDING_EXTERNAL_OES:
137 *ptr = (GLfloat)state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES);
141 if (!state->getClientStateParameter<GLfloat>(param,ptr)) {
142 ctx->m_glGetFloatv_enc(self, param, ptr);
148 void GLEncoder::s_glGetFixedv(void *self, GLenum param, GLfixed *ptr)
150 GLEncoder *ctx = (GLEncoder *)self;
151 assert(ctx->m_state != NULL);
152 GLClientState* state = ctx->m_state;
155 case GL_COMPRESSED_TEXTURE_FORMATS: {
156 GLint * compressedTextureFormats = ctx->getCompressedTextureFormats();
157 if (ctx->m_num_compressedTextureFormats > 0 &&
158 compressedTextureFormats != NULL) {
159 for (int i = 0; i < ctx->m_num_compressedTextureFormats; i++) {
160 ptr[i] = compressedTextureFormats[i] << 16;
166 case GL_MAX_TEXTURE_UNITS:
167 ctx->m_glGetFixedv_enc(self, param, ptr);
168 *ptr = MIN(*ptr, GLClientState::MAX_TEXTURE_UNITS << 16);
171 case GL_TEXTURE_BINDING_2D:
172 *ptr = state->getBoundTexture(GL_TEXTURE_2D) << 16;
175 case GL_TEXTURE_BINDING_EXTERNAL_OES:
176 *ptr = state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES) << 16;
180 if (!state->getClientStateParameter<GLfixed>(param,ptr)) {
181 ctx->m_glGetFixedv_enc(self, param, ptr);
187 void GLEncoder::s_glGetBooleanv(void *self, GLenum param, GLboolean *ptr)
189 GLEncoder *ctx = (GLEncoder *)self;
190 assert(ctx->m_state != NULL);
191 GLClientState* state = ctx->m_state;
194 case GL_COMPRESSED_TEXTURE_FORMATS: {
195 GLint* compressedTextureFormats = ctx->getCompressedTextureFormats();
196 if (ctx->m_num_compressedTextureFormats > 0 &&
197 compressedTextureFormats != NULL) {
198 for (int i = 0; i < ctx->m_num_compressedTextureFormats; i++) {
199 ptr[i] = compressedTextureFormats[i] != 0 ? GL_TRUE : GL_FALSE;
205 case GL_TEXTURE_BINDING_2D:
206 *ptr = state->getBoundTexture(GL_TEXTURE_2D) != 0 ? GL_TRUE : GL_FALSE;
209 case GL_TEXTURE_BINDING_EXTERNAL_OES:
210 *ptr = state->getBoundTexture(GL_TEXTURE_EXTERNAL_OES) != 0
211 ? GL_TRUE : GL_FALSE;
215 if (!state->getClientStateParameter<GLboolean>(param,ptr)) {
216 ctx->m_glGetBooleanv_enc(self, param, ptr);
222 void GLEncoder::s_glGetPointerv(void * self, GLenum param, GLvoid **params)
224 GLEncoder * ctx = (GLEncoder *) self;
225 assert(ctx->m_state != NULL);
226 ctx->m_state->getClientStatePointer(param,params);
229 void GLEncoder::s_glFlush(void *self)
231 GLEncoder *ctx = (GLEncoder *)self;
232 ctx->m_glFlush_enc(self);
233 ctx->m_stream->flush();
236 const GLubyte *GLEncoder::s_glGetString(void *self, GLenum name)
240 GLubyte *retval = (GLubyte *) "";
243 retval = gVendorString;
246 retval = gRendererString;
249 retval = gVersionString;
252 retval = gExtensionsString;
258 void GLEncoder::s_glPixelStorei(void *self, GLenum param, GLint value)
260 GLEncoder *ctx = (GLEncoder *)self;
261 ctx->m_glPixelStorei_enc(ctx, param, value);
262 ALOG_ASSERT(ctx->m_state, "GLEncoder::s_glPixelStorei");
263 ctx->m_state->setPixelStore(param, value);
266 void GLEncoder::s_glVertexPointer(void *self, int size, GLenum type, GLsizei stride, const void *data)
268 GLEncoder *ctx = (GLEncoder *)self;
269 assert(ctx->m_state != NULL);
270 ctx->m_state->setState(GLClientState::VERTEX_LOCATION, size, type, false, stride, data);
273 void GLEncoder::s_glNormalPointer(void *self, GLenum type, GLsizei stride, const void *data)
275 GLEncoder *ctx = (GLEncoder *)self;
276 assert(ctx->m_state != NULL);
277 ctx->m_state->setState(GLClientState::NORMAL_LOCATION, 3, type, false, stride, data);
280 void GLEncoder::s_glColorPointer(void *self, int size, GLenum type, GLsizei stride, const void *data)
282 GLEncoder *ctx = (GLEncoder *)self;
283 assert(ctx->m_state != NULL);
284 ctx->m_state->setState(GLClientState::COLOR_LOCATION, size, type, false, stride, data);
287 void GLEncoder::s_glPointSizePointerOES(void *self, GLenum type, GLsizei stride, const void *data)
289 GLEncoder *ctx = (GLEncoder *)self;
290 assert(ctx->m_state != NULL);
291 ctx->m_state->setState(GLClientState::POINTSIZE_LOCATION, 1, type, false, stride, data);
294 void GLEncoder::s_glClientActiveTexture(void *self, GLenum texture)
296 GLEncoder *ctx = (GLEncoder *)self;
297 assert(ctx->m_state != NULL);
298 ctx->m_state->setActiveTexture(texture - GL_TEXTURE0);
301 void GLEncoder::s_glTexCoordPointer(void *self, int size, GLenum type, GLsizei stride, const void *data)
303 GLEncoder *ctx = (GLEncoder *)self;
304 assert(ctx->m_state != NULL);
305 int loc = ctx->m_state->getLocation(GL_TEXTURE_COORD_ARRAY);
306 ctx->m_state->setState(loc, size, type, false, stride, data);
309 void GLEncoder::s_glMatrixIndexPointerOES(void *self, int size, GLenum type, GLsizei stride, const void * data)
311 GLEncoder *ctx = (GLEncoder *)self;
312 assert(ctx->m_state != NULL);
313 int loc = ctx->m_state->getLocation(GL_MATRIX_INDEX_ARRAY_OES);
314 ctx->m_state->setState(loc, size, type, false, stride, data);
317 void GLEncoder::s_glWeightPointerOES(void * self, int size, GLenum type, GLsizei stride, const void * data)
319 GLEncoder *ctx = (GLEncoder *)self;
320 assert(ctx->m_state != NULL);
321 int loc = ctx->m_state->getLocation(GL_WEIGHT_ARRAY_OES);
322 ctx->m_state->setState(loc, size, type, false, stride, data);
325 void GLEncoder::s_glEnableClientState(void *self, GLenum state)
327 GLEncoder *ctx = (GLEncoder *) self;
328 assert(ctx->m_state != NULL);
329 int loc = ctx->m_state->getLocation(state);
330 ctx->m_state->enable(loc, 1);
333 void GLEncoder::s_glDisableClientState(void *self, GLenum state)
335 GLEncoder *ctx = (GLEncoder *) self;
336 assert(ctx->m_state != NULL);
337 int loc = ctx->m_state->getLocation(state);
338 ctx->m_state->enable(loc, 0);
341 GLboolean GLEncoder::s_glIsEnabled(void *self, GLenum cap)
343 GLEncoder *ctx = (GLEncoder *) self;
344 assert(ctx->m_state != NULL);
345 int loc = ctx->m_state->getLocation(cap);
346 const GLClientState::VertexAttribState *state = ctx->m_state->getState(loc);
349 return state->enabled;
351 return ctx->m_glIsEnabled_enc(self,cap);
354 void GLEncoder::s_glBindBuffer(void *self, GLenum target, GLuint id)
356 GLEncoder *ctx = (GLEncoder *) self;
357 assert(ctx->m_state != NULL);
358 ctx->m_state->bindBuffer(target, id);
359 // TODO set error state if needed;
360 ctx->m_glBindBuffer_enc(self, target, id);
363 void GLEncoder::s_glBufferData(void * self, GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage)
365 GLEncoder *ctx = (GLEncoder *) self;
366 GLuint bufferId = ctx->m_state->getBuffer(target);
367 SET_ERROR_IF(bufferId==0, GL_INVALID_OPERATION);
368 SET_ERROR_IF(size<0, GL_INVALID_VALUE);
370 ctx->m_shared->updateBufferData(bufferId, size, (void*)data);
371 ctx->m_glBufferData_enc(self, target, size, data, usage);
374 void GLEncoder::s_glBufferSubData(void * self, GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid * data)
376 GLEncoder *ctx = (GLEncoder *) self;
377 GLuint bufferId = ctx->m_state->getBuffer(target);
378 SET_ERROR_IF(bufferId==0, GL_INVALID_OPERATION);
380 GLenum res = ctx->m_shared->subUpdateBufferData(bufferId, offset, size, (void*)data);
381 SET_ERROR_IF(res, res);
383 ctx->m_glBufferSubData_enc(self, target, offset, size, data);
386 void GLEncoder::s_glDeleteBuffers(void * self, GLsizei n, const GLuint * buffers)
388 GLEncoder *ctx = (GLEncoder *) self;
389 SET_ERROR_IF(n<0, GL_INVALID_VALUE);
390 for (int i=0; i<n; i++) {
391 ctx->m_shared->deleteBufferData(buffers[i]);
392 ctx->m_glDeleteBuffers_enc(self,1,&buffers[i]);
396 void GLEncoder::sendVertexData(unsigned int first, unsigned int count)
398 assert(m_state != NULL);
399 for (int i = 0; i < GLClientState::LAST_LOCATION; i++) {
401 const GLClientState::VertexAttribState *state = m_state->getStateAndEnableDirty(i, &enableDirty);
403 // do not process if state not valid
404 if (!state) continue;
406 // do not send disable state if state was already disabled
407 if (!enableDirty && !state->enabled) continue;
409 if ( i >= GLClientState::TEXCOORD0_LOCATION &&
410 i <= GLClientState::TEXCOORD7_LOCATION ) {
411 m_glClientActiveTexture_enc(this, GL_TEXTURE0 + i - GLClientState::TEXCOORD0_LOCATION);
414 if (state->enabled) {
417 m_glEnableClientState_enc(this, state->glConst);
419 unsigned int datalen = state->elementSize * count;
420 int stride = state->stride;
421 if (stride == 0) stride = state->elementSize;
422 int firstIndex = stride * first;
424 this->m_glBindBuffer_enc(this, GL_ARRAY_BUFFER, state->bufferObject);
425 if (state->bufferObject == 0) {
428 case GLClientState::VERTEX_LOCATION:
429 this->glVertexPointerData(this, state->size, state->type, state->stride,
430 (unsigned char *)state->data + firstIndex, datalen);
432 case GLClientState::NORMAL_LOCATION:
433 this->glNormalPointerData(this, state->type, state->stride,
434 (unsigned char *)state->data + firstIndex, datalen);
436 case GLClientState::COLOR_LOCATION:
437 this->glColorPointerData(this, state->size, state->type, state->stride,
438 (unsigned char *)state->data + firstIndex, datalen);
440 case GLClientState::TEXCOORD0_LOCATION:
441 case GLClientState::TEXCOORD1_LOCATION:
442 case GLClientState::TEXCOORD2_LOCATION:
443 case GLClientState::TEXCOORD3_LOCATION:
444 case GLClientState::TEXCOORD4_LOCATION:
445 case GLClientState::TEXCOORD5_LOCATION:
446 case GLClientState::TEXCOORD6_LOCATION:
447 case GLClientState::TEXCOORD7_LOCATION:
448 this->glTexCoordPointerData(this, i - GLClientState::TEXCOORD0_LOCATION, state->size, state->type, state->stride,
449 (unsigned char *)state->data + firstIndex, datalen);
451 case GLClientState::POINTSIZE_LOCATION:
452 this->glPointSizePointerData(this, state->type, state->stride,
453 (unsigned char *) state->data + firstIndex, datalen);
455 case GLClientState::WEIGHT_LOCATION:
456 this->glWeightPointerData(this, state->size, state->type, state->stride,
457 (unsigned char * ) state->data + firstIndex, datalen);
459 case GLClientState::MATRIXINDEX_LOCATION:
460 this->glMatrixIndexPointerData(this, state->size, state->type, state->stride,
461 (unsigned char *)state->data + firstIndex, datalen);
467 case GLClientState::VERTEX_LOCATION:
468 this->glVertexPointerOffset(this, state->size, state->type, state->stride,
469 (uintptr_t)state->data + firstIndex);
471 case GLClientState::NORMAL_LOCATION:
472 this->glNormalPointerOffset(this, state->type, state->stride,
473 (uintptr_t)state->data + firstIndex);
475 case GLClientState::POINTSIZE_LOCATION:
476 this->glPointSizePointerOffset(this, state->type, state->stride,
477 (uintptr_t)state->data + firstIndex);
479 case GLClientState::COLOR_LOCATION:
480 this->glColorPointerOffset(this, state->size, state->type, state->stride,
481 (uintptr_t)state->data + firstIndex);
483 case GLClientState::TEXCOORD0_LOCATION:
484 case GLClientState::TEXCOORD1_LOCATION:
485 case GLClientState::TEXCOORD2_LOCATION:
486 case GLClientState::TEXCOORD3_LOCATION:
487 case GLClientState::TEXCOORD4_LOCATION:
488 case GLClientState::TEXCOORD5_LOCATION:
489 case GLClientState::TEXCOORD6_LOCATION:
490 case GLClientState::TEXCOORD7_LOCATION:
491 this->glTexCoordPointerOffset(this, state->size, state->type, state->stride,
492 (uintptr_t)state->data + firstIndex);
494 case GLClientState::WEIGHT_LOCATION:
495 this->glWeightPointerOffset(this,state->size,state->type,state->stride,
496 (uintptr_t)state->data+firstIndex);
498 case GLClientState::MATRIXINDEX_LOCATION:
499 this->glMatrixIndexPointerOffset(this,state->size,state->type,state->stride,
500 (uintptr_t)state->data+firstIndex);
504 this->m_glBindBuffer_enc(this, GL_ARRAY_BUFFER, m_state->currentArrayVbo());
506 this->m_glDisableClientState_enc(this, state->glConst);
511 void GLEncoder::s_glDrawArrays(void *self, GLenum mode, GLint first, GLsizei count)
513 GLEncoder *ctx = (GLEncoder *)self;
515 bool has_arrays = false;
516 for (int i = 0; i < GLClientState::LAST_LOCATION; i++) {
517 const GLClientState::VertexAttribState *state = ctx->m_state->getState(i);
518 if (state->enabled) {
519 if (state->bufferObject || state->data) {
522 ALOGE("glDrawArrays: a vertex attribute array is enabled with no data bound\n");
523 ctx->setError(GL_INVALID_OPERATION);
529 ALOGE("glDrawArrays: no data bound to the command - ignoring\n");
533 ctx->sendVertexData(first, count);
534 ctx->m_glDrawArrays_enc(ctx, mode, /*first*/ 0, count);
537 void GLEncoder::s_glDrawElements(void *self, GLenum mode, GLsizei count, GLenum type, const void *indices)
540 GLEncoder *ctx = (GLEncoder *)self;
541 assert(ctx->m_state != NULL);
542 SET_ERROR_IF(count<0, GL_INVALID_VALUE);
544 bool has_immediate_arrays = false;
545 bool has_indirect_arrays = false;
547 for (int i = 0; i < GLClientState::LAST_LOCATION; i++) {
548 const GLClientState::VertexAttribState *state = ctx->m_state->getState(i);
549 if (state->enabled) {
550 if (state->bufferObject != 0) {
551 has_indirect_arrays = true;
552 } else if (state->data) {
553 has_immediate_arrays = true;
555 ALOGE("glDrawElements: a vertex attribute array is enabled with no data bound\n");
556 ctx->setError(GL_INVALID_OPERATION);
562 if (!has_immediate_arrays && !has_indirect_arrays) {
563 ALOGE("glDrawElements: no data bound to the command - ignoring\n");
567 bool adjustIndices = true;
568 if (ctx->m_state->currentIndexVbo() != 0) {
569 if (!has_immediate_arrays) {
570 ctx->sendVertexData(0, count);
571 ctx->m_glBindBuffer_enc(self, GL_ELEMENT_ARRAY_BUFFER, ctx->m_state->currentIndexVbo());
572 ctx->glDrawElementsOffset(ctx, mode, count, type, (uintptr_t)indices);
573 adjustIndices = false;
575 BufferData * buf = ctx->m_shared->getBufferData(ctx->m_state->currentIndexVbo());
576 ctx->m_glBindBuffer_enc(self, GL_ELEMENT_ARRAY_BUFFER, 0);
577 indices = (void*)((GLintptr)buf->m_fixedBuffer.ptr() + (GLintptr)indices);
581 void *adjustedIndices = (void*)indices;
582 int minIndex = 0, maxIndex = 0;
586 case GL_UNSIGNED_BYTE:
587 GLUtils::minmax<unsigned char>((unsigned char *)indices, count, &minIndex, &maxIndex);
589 adjustedIndices = ctx->m_fixedBuffer.alloc(glSizeof(type) * count);
590 GLUtils::shiftIndices<unsigned char>((unsigned char *)indices,
591 (unsigned char *)adjustedIndices,
596 case GL_UNSIGNED_SHORT:
597 GLUtils::minmax<unsigned short>((unsigned short *)indices, count, &minIndex, &maxIndex);
599 adjustedIndices = ctx->m_fixedBuffer.alloc(glSizeof(type) * count);
600 GLUtils::shiftIndices<unsigned short>((unsigned short *)indices,
601 (unsigned short *)adjustedIndices,
606 case GL_UNSIGNED_INT:
607 GLUtils::minmax<unsigned int>((unsigned int *)indices, count, &minIndex, &maxIndex);
609 adjustedIndices = ctx->m_fixedBuffer.alloc(glSizeof(type) * count);
610 GLUtils::shiftIndices<unsigned int>((unsigned int *)indices,
611 (unsigned int *)adjustedIndices,
616 ALOGE("unsupported index buffer type %d\n", type);
618 if (has_indirect_arrays || 1) {
619 ctx->sendVertexData(minIndex, maxIndex - minIndex + 1);
620 ctx->glDrawElementsData(ctx, mode, count, type, adjustedIndices,
621 count * glSizeof(type));
622 // XXX - OPTIMIZATION (see the other else branch) should be implemented
623 if(!has_indirect_arrays) {
624 //ALOGD("unoptimized drawelements !!!\n");
627 // we are all direct arrays and immidate mode index array -
628 // rebuild the arrays and the index array;
629 ALOGE("glDrawElements: direct index & direct buffer data - will be implemented in later versions;\n");
634 void GLEncoder::s_glActiveTexture(void* self, GLenum texture)
636 GLEncoder* ctx = (GLEncoder*)self;
637 GLClientState* state = ctx->m_state;
640 if ((err = state->setActiveTextureUnit(texture)) != GL_NO_ERROR) {
641 ALOGE("%s:%s:%d GL error %#x\n", __FILE__, __FUNCTION__, __LINE__, err);
646 ctx->m_glActiveTexture_enc(ctx, texture);
649 void GLEncoder::s_glBindTexture(void* self, GLenum target, GLuint texture)
651 GLEncoder* ctx = (GLEncoder*)self;
652 GLClientState* state = ctx->m_state;
656 if ((err = state->bindTexture(target, texture, &firstUse)) != GL_NO_ERROR) {
657 ALOGE("%s:%s:%d GL error %#x\n", __FILE__, __FUNCTION__, __LINE__, err);
662 if (target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES) {
663 ctx->m_glBindTexture_enc(ctx, target, texture);
667 GLenum priorityTarget = state->getPriorityEnabledTarget(GL_TEXTURE_2D);
669 if (target == GL_TEXTURE_EXTERNAL_OES && firstUse) {
670 // set TEXTURE_EXTERNAL_OES default states which differ from TEXTURE_2D
671 ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D, texture);
672 ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D,
673 GL_TEXTURE_MIN_FILTER, GL_LINEAR);
674 ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D,
675 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
676 ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D,
677 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
679 if (target != priorityTarget) {
680 ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D,
681 state->getBoundTexture(GL_TEXTURE_2D));
685 if (target == priorityTarget) {
686 ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D, texture);
690 void GLEncoder::s_glDeleteTextures(void* self, GLsizei n, const GLuint* textures)
692 GLEncoder* ctx = (GLEncoder*)self;
693 GLClientState* state = ctx->m_state;
695 state->deleteTextures(n, textures);
696 ctx->m_glDeleteTextures_enc(ctx, n, textures);
699 void GLEncoder::s_glDisable(void* self, GLenum cap)
701 GLEncoder* ctx = (GLEncoder*)self;
702 GLClientState* state = ctx->m_state;
704 if (cap == GL_TEXTURE_2D || cap == GL_TEXTURE_EXTERNAL_OES) {
705 GLenum prevTarget = state->getPriorityEnabledTarget(GL_INVALID_ENUM);
706 state->disableTextureTarget(cap);
707 GLenum currTarget = state->getPriorityEnabledTarget(GL_INVALID_ENUM);
709 if (prevTarget != currTarget) {
710 if (currTarget == GL_INVALID_ENUM) {
711 ctx->m_glDisable_enc(ctx, GL_TEXTURE_2D);
712 currTarget = GL_TEXTURE_2D;
714 // maintain the invariant that when TEXTURE_EXTERNAL_OES is
715 // disabled, the TEXTURE_2D binding is active, even if
716 // TEXTURE_2D is also disabled.
717 ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D,
718 state->getBoundTexture(currTarget));
722 ctx->m_glDisable_enc(ctx, cap);
726 void GLEncoder::s_glEnable(void* self, GLenum cap)
728 GLEncoder* ctx = (GLEncoder*)self;
729 GLClientState* state = ctx->m_state;
731 if (cap == GL_TEXTURE_2D || cap == GL_TEXTURE_EXTERNAL_OES) {
732 GLenum prevTarget = state->getPriorityEnabledTarget(GL_INVALID_ENUM);
733 state->enableTextureTarget(cap);
734 GLenum currTarget = state->getPriorityEnabledTarget(GL_INVALID_ENUM);
736 if (prevTarget != currTarget) {
737 if (prevTarget == GL_INVALID_ENUM) {
738 ctx->m_glEnable_enc(ctx, GL_TEXTURE_2D);
740 if (currTarget == GL_TEXTURE_EXTERNAL_OES) {
741 ctx->m_glBindTexture_enc(ctx, GL_TEXTURE_2D,
742 state->getBoundTexture(currTarget));
747 ctx->m_glEnable_enc(ctx, cap);
751 void GLEncoder::s_glGetTexParameterfv(void* self,
752 GLenum target, GLenum pname, GLfloat* params)
754 GLEncoder* ctx = (GLEncoder*)self;
755 const GLClientState* state = ctx->m_state;
757 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
758 ctx->override2DTextureTarget(target);
759 ctx->m_glGetTexParameterfv_enc(ctx, GL_TEXTURE_2D, pname, params);
760 ctx->restore2DTextureTarget();
762 ctx->m_glGetTexParameterfv_enc(ctx, target, pname, params);
766 void GLEncoder::s_glGetTexParameteriv(void* self,
767 GLenum target, GLenum pname, GLint* params)
769 GLEncoder* ctx = (GLEncoder*)self;
770 const GLClientState* state = ctx->m_state;
773 case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
778 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
779 ctx->override2DTextureTarget(target);
780 ctx->m_glGetTexParameteriv_enc(ctx, GL_TEXTURE_2D, pname, params);
781 ctx->restore2DTextureTarget();
783 ctx->m_glGetTexParameteriv_enc(ctx, target, pname, params);
789 void GLEncoder::s_glGetTexParameterxv(void* self,
790 GLenum target, GLenum pname, GLfixed* params)
792 GLEncoder* ctx = (GLEncoder*)self;
793 const GLClientState* state = ctx->m_state;
795 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
796 ctx->override2DTextureTarget(target);
797 ctx->m_glGetTexParameterxv_enc(ctx, GL_TEXTURE_2D, pname, params);
798 ctx->restore2DTextureTarget();
800 ctx->m_glGetTexParameterxv_enc(ctx, target, pname, params);
804 static bool isValidTextureExternalParam(GLenum pname, GLenum param)
807 case GL_TEXTURE_MIN_FILTER:
808 case GL_TEXTURE_MAG_FILTER:
809 return param == GL_NEAREST || param == GL_LINEAR;
811 case GL_TEXTURE_WRAP_S:
812 case GL_TEXTURE_WRAP_T:
813 return param == GL_CLAMP_TO_EDGE;
815 case GL_GENERATE_MIPMAP:
816 return param == GL_FALSE;
823 void GLEncoder::s_glTexParameterf(void* self,
824 GLenum target, GLenum pname, GLfloat param)
826 GLEncoder* ctx = (GLEncoder*)self;
827 const GLClientState* state = ctx->m_state;
829 SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
830 !isValidTextureExternalParam(pname, (GLenum)param)),
833 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
834 ctx->override2DTextureTarget(target);
835 ctx->m_glTexParameterf_enc(ctx, GL_TEXTURE_2D, pname, param);
836 ctx->restore2DTextureTarget();
838 ctx->m_glTexParameterf_enc(ctx, target, pname, param);
842 void GLEncoder::s_glTexParameterfv(void* self,
843 GLenum target, GLenum pname, const GLfloat* params)
845 GLEncoder* ctx = (GLEncoder*)self;
846 const GLClientState* state = ctx->m_state;
848 SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
849 !isValidTextureExternalParam(pname, (GLenum)params[0])),
852 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
853 ctx->override2DTextureTarget(target);
854 ctx->m_glTexParameterfv_enc(ctx, GL_TEXTURE_2D, pname, params);
855 ctx->restore2DTextureTarget();
857 ctx->m_glTexParameterfv_enc(ctx, target, pname, params);
861 void GLEncoder::s_glTexParameteri(void* self,
862 GLenum target, GLenum pname, GLint param)
864 GLEncoder* ctx = (GLEncoder*)self;
865 const GLClientState* state = ctx->m_state;
867 SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
868 !isValidTextureExternalParam(pname, (GLenum)param)),
871 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
872 ctx->override2DTextureTarget(target);
873 ctx->m_glTexParameteri_enc(ctx, GL_TEXTURE_2D, pname, param);
874 ctx->restore2DTextureTarget();
876 ctx->m_glTexParameteri_enc(ctx, target, pname, param);
880 void GLEncoder::s_glTexParameterx(void* self,
881 GLenum target, GLenum pname, GLfixed param)
883 GLEncoder* ctx = (GLEncoder*)self;
884 const GLClientState* state = ctx->m_state;
886 SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
887 !isValidTextureExternalParam(pname, (GLenum)param)),
890 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
891 ctx->override2DTextureTarget(target);
892 ctx->m_glTexParameterx_enc(ctx, GL_TEXTURE_2D, pname, param);
893 ctx->restore2DTextureTarget();
895 ctx->m_glTexParameterx_enc(ctx, target, pname, param);
899 void GLEncoder::s_glTexParameteriv(void* self,
900 GLenum target, GLenum pname, const GLint* params)
902 GLEncoder* ctx = (GLEncoder*)self;
903 const GLClientState* state = ctx->m_state;
905 SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
906 !isValidTextureExternalParam(pname, (GLenum)params[0])),
909 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
910 ctx->override2DTextureTarget(target);
911 ctx->m_glTexParameteriv_enc(ctx, GL_TEXTURE_2D, pname, params);
912 ctx->restore2DTextureTarget();
914 ctx->m_glTexParameteriv_enc(ctx, target, pname, params);
918 void GLEncoder::s_glTexParameterxv(void* self,
919 GLenum target, GLenum pname, const GLfixed* params)
921 GLEncoder* ctx = (GLEncoder*)self;
922 const GLClientState* state = ctx->m_state;
924 SET_ERROR_IF((target == GL_TEXTURE_EXTERNAL_OES &&
925 !isValidTextureExternalParam(pname, (GLenum)params[0])),
928 if (target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) {
929 ctx->override2DTextureTarget(target);
930 ctx->m_glTexParameterxv_enc(ctx, GL_TEXTURE_2D, pname, params);
931 ctx->restore2DTextureTarget();
933 ctx->m_glTexParameterxv_enc(ctx, target, pname, params);
937 void GLEncoder::override2DTextureTarget(GLenum target)
939 if ((target == GL_TEXTURE_2D || target == GL_TEXTURE_EXTERNAL_OES) &&
940 target != m_state->getPriorityEnabledTarget(GL_TEXTURE_2D)) {
941 m_glBindTexture_enc(this, GL_TEXTURE_2D,
942 m_state->getBoundTexture(target));
946 void GLEncoder::restore2DTextureTarget()
948 GLenum priorityTarget = m_state->getPriorityEnabledTarget(GL_TEXTURE_2D);
949 m_glBindTexture_enc(this, GL_TEXTURE_2D,
950 m_state->getBoundTexture(priorityTarget));
953 GLEncoder::GLEncoder(IOStream *stream, ChecksumCalculator *protocol)
954 : gl_encoder_context_t(stream, protocol)
956 m_initialized = false;
958 m_error = GL_NO_ERROR;
959 m_num_compressedTextureFormats = 0;
960 m_compressedTextureFormats = NULL;
963 #define OVERRIDE(name) m_##name##_enc = this-> name ; this-> name = &s_##name
966 OVERRIDE(glPixelStorei);
967 OVERRIDE(glVertexPointer);
968 OVERRIDE(glNormalPointer);
969 OVERRIDE(glColorPointer);
970 OVERRIDE(glPointSizePointerOES);
971 OVERRIDE(glClientActiveTexture);
972 OVERRIDE(glTexCoordPointer);
973 OVERRIDE(glMatrixIndexPointerOES);
974 OVERRIDE(glWeightPointerOES);
976 OVERRIDE(glGetIntegerv);
977 OVERRIDE(glGetFloatv);
978 OVERRIDE(glGetBooleanv);
979 OVERRIDE(glGetFixedv);
980 OVERRIDE(glGetPointerv);
982 OVERRIDE(glBindBuffer);
983 OVERRIDE(glBufferData);
984 OVERRIDE(glBufferSubData);
985 OVERRIDE(glDeleteBuffers);
987 OVERRIDE(glEnableClientState);
988 OVERRIDE(glDisableClientState);
989 OVERRIDE(glIsEnabled);
990 OVERRIDE(glDrawArrays);
991 OVERRIDE(glDrawElements);
993 this->glGetString = s_glGetString;
994 this->glFinish = s_glFinish;
996 OVERRIDE(glGetError);
998 OVERRIDE(glActiveTexture);
999 OVERRIDE(glBindTexture);
1000 OVERRIDE(glDeleteTextures);
1001 OVERRIDE(glDisable);
1003 OVERRIDE(glGetTexParameterfv);
1004 OVERRIDE(glGetTexParameteriv);
1005 OVERRIDE(glGetTexParameterxv);
1006 OVERRIDE(glTexParameterf);
1007 OVERRIDE(glTexParameterfv);
1008 OVERRIDE(glTexParameteri);
1009 OVERRIDE(glTexParameterx);
1010 OVERRIDE(glTexParameteriv);
1011 OVERRIDE(glTexParameterxv);
1014 GLEncoder::~GLEncoder()
1016 delete [] m_compressedTextureFormats;
1019 size_t GLEncoder::pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack)
1021 assert(m_state != NULL);
1022 return m_state->pixelDataSize(width, height, format, type, pack);
1025 void GLEncoder::s_glFinish(void *self)
1027 GLEncoder *ctx = (GLEncoder *)self;
1028 ctx->glFinishRoundTrip(self);