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 
17 #include "GLEScmContext.h"
18 #include "GLEScmUtils.h"
19 #include <algorithm>
20 #include <GLcommon/GLutils.h>
21 #include <GLcommon/GLconversion_macros.h>
22 #include <string.h>
23 #include <GLES/gl.h>
24 #include <GLES/glext.h>
25 
26 #include "aemu/base/synchronization/Lock.h"
27 #include "aemu/base/files/StreamSerializing.h"
28 #include "host-common/crash_reporter.h"
29 #include "GLEScmValidate.h"
30 
31 #include <glm/vec3.hpp>
32 #include <glm/vec4.hpp>
33 #include <glm/gtc/matrix_transform.hpp>
34 #include <glm/gtc/type_ptr.hpp>
35 
36 static GLESVersion s_maxGlesVersion = GLES_1_1;
37 
setMaxGlesVersion(GLESVersion version)38 void GLEScmContext::setMaxGlesVersion(GLESVersion version) {
39     s_maxGlesVersion = version;
40 }
41 
init(bool nativeTextureDecompressionEnabled)42 void GLEScmContext::init(bool nativeTextureDecompressionEnabled) {
43     android::base::AutoLock mutex(s_lock);
44     if(!m_initialized) {
45         GLEScontext::init(nativeTextureDecompressionEnabled);
46 
47         addVertexArrayObject(0);
48         setVertexArrayObject(0);
49 
50         m_currVaoState[GL_COLOR_ARRAY]          = new GLESpointer();
51         m_currVaoState[GL_NORMAL_ARRAY]         = new GLESpointer();
52         m_currVaoState[GL_VERTEX_ARRAY]         = new GLESpointer();
53         m_currVaoState[GL_POINT_SIZE_ARRAY_OES] = new GLESpointer();
54 
55         m_texCoords = new GLESpointer[kMaxTextureUnits];
56         m_currVaoState[GL_TEXTURE_COORD_ARRAY]  = &m_texCoords[m_clientActiveTexture];
57 
58         if (isCoreProfile()) {
59             m_coreProfileEngine = new CoreProfileEngine(this);
60         } else if (isGles2Gles()) {
61             m_coreProfileEngine = new CoreProfileEngine(this, true /* gles2gles */);
62         }
63         mColor.type = GL_UNSIGNED_BYTE;
64         mColor.val.ubyteVal[0] = 255;
65         mColor.val.ubyteVal[1] = 255;
66         mColor.val.ubyteVal[2] = 255;
67         mColor.val.ubyteVal[3] = 255;
68         mNormal.type = GL_FLOAT;
69         mNormal.val.floatVal[0] = 0.0f;
70         mNormal.val.floatVal[1] = 0.0f;
71         mNormal.val.floatVal[2] = 1.0f;
72     }
73     m_initialized = true;
74 }
75 
initGlobal(EGLiface * eglIface)76 void GLEScmContext::initGlobal(EGLiface* eglIface) {
77     s_glDispatch.dispatchFuncs(s_maxGlesVersion, eglIface->eglGetGlLibrary(), eglIface->getProcAddress);
78     GLEScontext::initGlobal(eglIface);
79     buildStrings( 1, 1,
80                  (const char*)dispatcher().glGetString(GL_VENDOR),
81                  (const char*)dispatcher().glGetString(GL_RENDERER),
82                  (const char*)dispatcher().glGetString(GL_VERSION),
83                  "OpenGL ES-CM 1.1");
84 }
85 
initDefaultFBO(GLint width,GLint height,GLint colorFormat,GLint depthstencilFormat,GLint multisamples,GLuint * eglSurfaceRBColorId,GLuint * eglSurfaceRBDepthId,GLuint readWidth,GLint readHeight,GLint readColorFormat,GLint readDepthstencilFormat,GLint readMultisamples,GLuint * eglReadSurfaceRBColorId,GLuint * eglReadSurfaceRBDepthId)86 void GLEScmContext::initDefaultFBO(
87         GLint width, GLint height, GLint colorFormat, GLint depthstencilFormat, GLint multisamples,
88         GLuint* eglSurfaceRBColorId, GLuint* eglSurfaceRBDepthId,
89         GLuint readWidth, GLint readHeight, GLint readColorFormat, GLint readDepthstencilFormat, GLint readMultisamples,
90         GLuint* eglReadSurfaceRBColorId, GLuint* eglReadSurfaceRBDepthId) {
91     GLEScontext::initDefaultFBO(
92             width, height, colorFormat, depthstencilFormat, multisamples,
93             eglSurfaceRBColorId, eglSurfaceRBDepthId,
94             readWidth, readHeight, readColorFormat, readDepthstencilFormat, readMultisamples,
95             eglReadSurfaceRBColorId, eglReadSurfaceRBDepthId
96             );
97 }
98 
GLEScmContext(int maj,int min,GlobalNameSpace * globalNameSpace,android::base::Stream * stream)99 GLEScmContext::GLEScmContext(int maj, int min,
100         GlobalNameSpace* globalNameSpace, android::base::Stream* stream)
101     : GLEScontext(globalNameSpace, stream, nullptr) {
102     if (stream) {
103         assert(maj == m_glesMajorVersion);
104         assert(min == m_glesMinorVersion);
105         android::base::loadBuffer(stream, &mProjMatrices);
106         android::base::loadBuffer(stream, &mModelviewMatrices);
107         android::base::loadBuffer(stream, &mTextureMatrices,
108                 [](android::base::Stream* stream) {
109                     MatrixStack matrices;
110                     android::base::loadBuffer(stream, &matrices);
111                     return matrices;
112                 });
113         android::base::loadBuffer(stream, &mTexUnitEnvs,
114                 [](android::base::Stream* stream) {
115                     TexEnv texEnv;
116                     android::base::loadCollection(stream, &texEnv,
117                             [] (android::base::Stream* stream) {
118                                 GLenum idx = stream->getBe32();
119                                 GLValTyped val;
120                                 stream->read(&val, sizeof(GLValTyped));
121                                 return std::make_pair(idx, val);
122                             });
123                     return texEnv;
124                 });
125         android::base::loadBuffer(stream, &mTexGens,
126                 [](android::base::Stream* stream) {
127                     TexEnv texEnv;
128                     android::base::loadCollection(stream, &texEnv,
129                             [] (android::base::Stream* stream) {
130                                 GLenum idx = stream->getBe32();
131                                 GLValTyped val;
132                                 stream->read(&val, sizeof(GLValTyped));
133                                 return std::make_pair(idx, val);
134                             });
135                     return texEnv;
136                 });
137         m_clientActiveTexture = stream->getBe32();
138         if (m_initialized) {
139             mShadeModel = stream->getBe32();
140             stream->read((void*)&mColor, sizeof(mColor));
141             stream->read((void*)&mNormal, sizeof(mNormal));
142             uint32_t size = stream->getBe32();
143             m_texCoords = new GLESpointer[size];
144             for (uint32_t i = 0; i < size; i++) {
145                 m_texCoords[i].onLoad(stream);
146             }
147             m_currVaoState[GL_TEXTURE_COORD_ARRAY] =
148                     &m_texCoords[m_clientActiveTexture];
149         }
150 
151         android::base::loadBufferPtr<GLVal>(stream, mMultiTexCoord);
152         android::base::loadBufferPtr<Material>(stream, &mMaterial);
153         android::base::loadBufferPtr<LightModel>(stream, &mLightModel);
154         android::base::loadBufferPtr<Light>(stream, mLights);
155         android::base::loadBufferPtr<Fog>(stream, &mFog);
156 
157     } else {
158         m_glesMajorVersion = maj;
159         m_glesMinorVersion = min;
160 
161         mProjMatrices.resize(1, glm::mat4());
162         mModelviewMatrices.resize(1, glm::mat4());
163         mTextureMatrices.resize(kMaxTextureUnits, { glm::mat4() });
164         mTexUnitEnvs.resize(kMaxTextureUnits, TexEnv());
165         mTexGens.resize(kMaxTextureUnits, TexEnv());
166 
167         for (int i = 0; i < kMaxTextureUnits; i++) {
168             mTexUnitEnvs[i][GL_TEXTURE_ENV_MODE].val.intVal[0] = GL_MODULATE;
169             mTexUnitEnvs[i][GL_TEXTURE_ENV_MODE].type = GL_INT;
170             mTexUnitEnvs[i][GL_TEXTURE_ENV_COLOR].val.floatVal[0] = 0.2f;
171             mTexUnitEnvs[i][GL_TEXTURE_ENV_COLOR].val.floatVal[1] = 0.4f;
172             mTexUnitEnvs[i][GL_TEXTURE_ENV_COLOR].val.floatVal[2] = 0.8f;
173             mTexUnitEnvs[i][GL_TEXTURE_ENV_COLOR].val.floatVal[3] = 0.7f;
174             mTexUnitEnvs[i][GL_TEXTURE_ENV_COLOR].type = GL_FLOAT;
175             mTexUnitEnvs[i][GL_COMBINE_RGB].val.intVal[0] = GL_REPLACE;
176             mTexUnitEnvs[i][GL_COMBINE_RGB].type = GL_INT;
177             mTexUnitEnvs[i][GL_COMBINE_ALPHA].val.intVal[0] = GL_REPLACE;
178             mTexUnitEnvs[i][GL_COMBINE_ALPHA].type = GL_INT;
179         }
180 
181         // GL_LIGHT0 starts off as white
182         mLights[0].diffuse[0] = 1.0f;
183         mLights[0].diffuse[1] = 1.0f;
184         mLights[0].diffuse[2] = 1.0f;
185         mLights[0].diffuse[3] = 1.0f;
186         mLights[0].specular[0] = 1.0f;
187         mLights[0].specular[1] = 1.0f;
188         mLights[0].specular[2] = 1.0f;
189         mLights[0].specular[3] = 1.0f;
190     }
191 }
192 
193 
setActiveTexture(GLenum tex)194 void GLEScmContext::setActiveTexture(GLenum tex) {
195    m_activeTexture = tex - GL_TEXTURE0;
196 }
197 
setClientActiveTexture(GLenum tex)198 void GLEScmContext::setClientActiveTexture(GLenum tex) {
199    m_clientActiveTexture = tex - GL_TEXTURE0;
200    m_currVaoState[GL_TEXTURE_COORD_ARRAY] = &m_texCoords[m_clientActiveTexture];
201 }
202 
setBindedTexture(GLenum target,unsigned int texture,unsigned int globalTexName)203 void GLEScmContext::setBindedTexture(GLenum target, unsigned int texture, unsigned int globalTexName) {
204     GLEScontext::setBindedTexture(target, texture);
205 }
206 
~GLEScmContext()207 GLEScmContext::~GLEScmContext(){
208     if(m_texCoords){
209         delete[] m_texCoords;
210         m_texCoords = NULL;
211     }
212     if (m_vaoStateMap.size()) {
213         m_currVaoState[GL_TEXTURE_COORD_ARRAY] = NULL;
214     }
215 
216     if (m_coreProfileEngine) {
217         delete m_coreProfileEngine;
218         m_coreProfileEngine = NULL;
219     }
220 }
221 
getMaterialInfo()222 const GLEScmContext::Material& GLEScmContext::getMaterialInfo() {
223     return mMaterial;
224 }
225 
getLightModelInfo()226 const GLEScmContext::LightModel& GLEScmContext::getLightModelInfo() {
227     return mLightModel;
228 }
229 
getLightInfo(uint32_t lightIndex)230 const GLEScmContext::Light& GLEScmContext::getLightInfo(uint32_t lightIndex) {
231     return mLights[lightIndex];
232 }
233 
getFogInfo()234 const GLEScmContext::Fog& GLEScmContext::getFogInfo() {
235     return mFog;
236 }
237 
onSave(android::base::Stream * stream) const238 void GLEScmContext::onSave(android::base::Stream* stream) const {
239     GLEScontext::onSave(stream);
240     android::base::saveBuffer(stream, mProjMatrices);
241     android::base::saveBuffer(stream, mModelviewMatrices);
242     android::base::saveBuffer(stream, mTextureMatrices,
243             [](android::base::Stream* stream, const MatrixStack& matrices) {
244                 android::base::saveBuffer(stream, matrices);
245             });
246     android::base::saveBuffer(stream, mTexUnitEnvs,
247             [](android::base::Stream* stream, const TexEnv& texEnv) {
248                 android::base::saveCollection(stream, texEnv,
249                         [] (android::base::Stream* stream,
250                             const std::pair<GLenum, GLValTyped>& it) {
251                             stream->putBe32(it.first);
252                             stream->write(&it.second, sizeof(GLValTyped));
253                         });
254             });
255     android::base::saveBuffer(stream, mTexGens,
256             [](android::base::Stream* stream, const TexEnv& texEnv) {
257                 android::base::saveCollection(stream, texEnv,
258                         [] (android::base::Stream* stream,
259                             const std::pair<GLenum, GLValTyped>& it) {
260                             stream->putBe32(it.first);
261                             stream->write(&it.second, sizeof(GLValTyped));
262                         });
263             });
264     stream->putBe32(m_clientActiveTexture);
265     if (m_initialized) {
266         stream->putBe32(mShadeModel);
267         stream->write((void*)&mColor, sizeof(mColor));
268         stream->write((void*)&mNormal, sizeof(mNormal));
269         stream->putBe32(kMaxTextureUnits);
270         for (uint32_t i = 0; i < kMaxTextureUnits; i++) {
271             m_texCoords[i].onSave(stream);
272         }
273     }
274 
275     android::base::saveBuffer<GLVal>(stream, mMultiTexCoord, kMaxTextureUnits);
276     android::base::saveBuffer<Material>(stream, &mMaterial, 1);
277     android::base::saveBuffer<LightModel>(stream, &mLightModel, 1);
278     android::base::saveBuffer<Light>(stream, mLights, kMaxLights);
279     android::base::saveBuffer<Fog>(stream, &mFog, 1);
280 }
281 
restoreMatrixStack(const MatrixStack & matrices)282 void GLEScmContext::restoreMatrixStack(const MatrixStack& matrices) {
283     for (size_t i = 0; i < matrices.size(); i++) {
284         if (i > 0) {
285             dispatcher().glPushMatrix();
286         }
287         dispatcher().glLoadMatrixf(&matrices[i][0][0]);
288     }
289 }
290 
postLoadRestoreCtx()291 void GLEScmContext::postLoadRestoreCtx() {
292     if (isInitialized()) {
293         initExtensionString();
294         if (isCoreProfile()) {
295             m_coreProfileEngine = new CoreProfileEngine(this);
296         } else if (isGles2Gles()) {
297             m_coreProfileEngine = new CoreProfileEngine(this, true);
298         }
299         if (!m_coreProfileEngine) {
300             GLDispatch& dispatcher = GLEScontext::dispatcher();
301             dispatcher.glMatrixMode(GL_PROJECTION);
302             restoreMatrixStack(mProjMatrices);
303             dispatcher.glMatrixMode(GL_MODELVIEW);
304             restoreMatrixStack(mModelviewMatrices);
305             dispatcher.glMatrixMode(GL_TEXTURE);
306             for (size_t i = 0; i < mTextureMatrices.size(); i++) {
307                 if (mTextureMatrices[i].size() == 0) {
308                     continue;
309                 }
310                 dispatcher.glActiveTexture(GL_TEXTURE0 + i);
311                 restoreMatrixStack(mTextureMatrices[i]);
312             }
313             for (const auto& array : m_currVaoState) {
314                 if (array.first == GL_TEXTURE_COORD_ARRAY) continue;
315                 array.second->restoreBufferObj(getBufferObj);
316             }
317             for (uint32_t i = 0; i < kMaxTextureUnits; i++) {
318                 m_texCoords[i].restoreBufferObj(getBufferObj);
319             }
320             dispatcher.glMatrixMode(mCurrMatrixMode);
321             dispatcher.glActiveTexture(GL_TEXTURE0 + m_activeTexture);
322             for (const auto& it : *m_currVaoState.it->second.arraysMap) {
323                 if (GLEScmValidate::supportedArrays(it.first) &&
324                         it.first != GL_TEXTURE_COORD_ARRAY) {
325                     if (it.second->isEnable()) {
326                         dispatcher.glEnableClientState(it.first);
327                     } else {
328                         dispatcher.glDisableClientState(it.first);
329                     }
330                 }
331             }
332 
333             for (int i = 0; i < kMaxTextureUnits; i++) {
334                 GLESpointer* texcoord = m_texCoords + i;
335                 dispatcher.glClientActiveTexture(i + GL_TEXTURE0);
336                 if (texcoord->isEnable()) {
337                     dispatcher.glEnableClientState(GL_TEXTURE_COORD_ARRAY);
338                 } else {
339                     dispatcher.glDisableClientState(GL_TEXTURE_COORD_ARRAY);
340                 }
341                 dispatcher.glActiveTexture(i + GL_TEXTURE0);
342                 for (const auto& texEnv : mTexUnitEnvs[i]) {
343                     GLenum target = texEnv.first == GL_POINT_SPRITE_OES ?
344                         GL_COORD_REPLACE_OES : GL_TEXTURE_ENV;
345                     if (texEnv.second.type == GL_INT) {
346                         dispatcher.glTexEnviv(target, texEnv.first,
347                                 texEnv.second.val.intVal);
348                     } else {
349                         assert(texEnv.second.type == GL_FLOAT);
350                         dispatcher.glTexEnvfv(target, texEnv.first,
351                                 texEnv.second.val.floatVal);
352                     }
353                 }
354             }
355             dispatcher.glClientActiveTexture(
356                     m_clientActiveTexture + GL_TEXTURE0);
357             dispatcher.glActiveTexture(m_activeTexture + GL_TEXTURE0);
358             dispatcher.glShadeModel(mShadeModel);
359             switch (mColor.type) {
360                 case GL_FLOAT:
361                     dispatcher.glColor4f(mColor.val.floatVal[0],
362                             mColor.val.floatVal[1],
363                             mColor.val.floatVal[2],
364                             mColor.val.floatVal[3]);
365                     break;
366                 case GL_UNSIGNED_BYTE:
367                     dispatcher.glColor4ub(mColor.val.ubyteVal[0],
368                             mColor.val.ubyteVal[1],
369                             mColor.val.ubyteVal[2],
370                             mColor.val.ubyteVal[3]);
371                     break;
372                 default:
373                     fprintf(stderr, "WARNING: glColor with unknown type 0x%x\n",
374                             mColor.type);
375                     break;
376             }
377             switch (mNormal.type) {
378                 case GL_FLOAT:
379                     dispatcher.glNormal3f(mNormal.val.floatVal[0],
380                             mNormal.val.floatVal[1],
381                             mNormal.val.floatVal[2]);
382                     break;
383                 default:
384                     fprintf(stderr, "WARNING: glNormal with unknown type 0x%x\n",
385                             mNormal.type);
386                     break;
387             }
388 
389             dispatcher.glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mMaterial.ambient);
390             dispatcher.glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mMaterial.diffuse);
391             dispatcher.glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mMaterial.specular);
392             dispatcher.glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mMaterial.emissive);
393             dispatcher.glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mMaterial.specularExponent);
394 
395             dispatcher.glLightModelfv(GL_LIGHT_MODEL_AMBIENT, mLightModel.color);
396             dispatcher.glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, mLightModel.twoSided);
397 
398             for (int i = 0; i < kMaxLights; i++) {
399                 dispatcher.glLightfv(GL_LIGHT0 + i, GL_AMBIENT, mLights[i].ambient);
400                 dispatcher.glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, mLights[i].diffuse);
401                 dispatcher.glLightfv(GL_LIGHT0 + i, GL_SPECULAR, mLights[i].specular);
402                 dispatcher.glLightfv(GL_LIGHT0 + i, GL_POSITION, mLights[i].position);
403                 dispatcher.glLightfv(GL_LIGHT0 + i, GL_SPOT_DIRECTION, mLights[i].direction);
404                 dispatcher.glLightf(GL_LIGHT0 + i, GL_SPOT_EXPONENT, mLights[i].spotlightExponent);
405                 dispatcher.glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, mLights[i].spotlightCutoffAngle);
406                 dispatcher.glLightf(GL_LIGHT0 + i, GL_CONSTANT_ATTENUATION, mLights[i].attenuationConst);
407                 dispatcher.glLightf(GL_LIGHT0 + i, GL_LINEAR_ATTENUATION, mLights[i].attenuationLinear);
408                 dispatcher.glLightf(GL_LIGHT0 + i, GL_QUADRATIC_ATTENUATION, mLights[i].attenuationQuadratic);
409             }
410 
411             dispatcher.glFogf(GL_FOG_MODE, (GLfloat)mFog.mode);
412             dispatcher.glFogf(GL_FOG_DENSITY, mFog.density);
413             dispatcher.glFogf(GL_FOG_START, mFog.start);
414             dispatcher.glFogf(GL_FOG_END, mFog.end);
415             dispatcher.glFogfv(GL_FOG_COLOR, mFog.color);
416         }
417     }
418     GLEScontext::postLoadRestoreCtx();
419 }
420 
421 //setting client side arr
setupArr(const GLvoid * arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized,int index,bool isInt)422 void GLEScmContext::setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized, int index, bool isInt){
423     if( arr == NULL) return;
424     switch(arrayType) {
425         case GL_VERTEX_ARRAY:
426             s_glDispatch.glVertexPointer(size,dataType,stride,arr);
427             break;
428         case GL_NORMAL_ARRAY:
429             s_glDispatch.glNormalPointer(dataType,stride,arr);
430             break;
431         case GL_TEXTURE_COORD_ARRAY:
432             s_glDispatch.glTexCoordPointer(size,dataType,stride,arr);
433             break;
434         case GL_COLOR_ARRAY:
435             s_glDispatch.glColorPointer(size,dataType,stride,arr);
436             break;
437         case GL_POINT_SIZE_ARRAY_OES:
438             m_pointsIndex = index;
439             break;
440     }
441 }
442 
443 
setupArrayPointerHelper(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices,bool direct,GLenum array_id,GLESpointer * p)444 void GLEScmContext::setupArrayPointerHelper(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLenum array_id,GLESpointer* p){
445         unsigned int size = p->getSize();
446         GLenum dataType = p->getType();
447 
448         if(needConvert(cArrs,first,count,type,indices,direct,p,array_id)){
449             //conversion has occured
450             ArrayData currentArr = cArrs.getCurrentArray();
451             setupArr(currentArr.data,array_id,currentArr.type,size,currentArr.stride,GL_FALSE, cArrs.getCurrentIndex());
452             ++cArrs;
453         } else {
454             setupArr(p->getData(),array_id,dataType,size,p->getStride(), GL_FALSE);
455         }
456 }
457 
setupArraysPointers(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices,bool direct,bool * needEnablingPostDraw)458 void GLEScmContext::setupArraysPointers(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct, bool* needEnablingPostDraw) {
459     (void)needEnablingPostDraw;
460     ArraysMap::iterator it;
461     m_pointsIndex = -1;
462 
463     //going over all clients arrays Pointers
464     for ( it=m_currVaoState.begin() ; it != m_currVaoState.end(); ++it) {
465 
466         GLenum array_id   = (*it).first;
467         GLESpointer* p = (*it).second;
468         if(!p->isEnable()) continue;
469         if(array_id == GL_TEXTURE_COORD_ARRAY) continue; //handling textures later
470         setupArrayPointerHelper(cArrs,first,count,type,indices,direct,array_id,p);
471     }
472 
473     unsigned int activeTexture = m_clientActiveTexture + GL_TEXTURE0;
474 
475     //converting all texture coords arrays
476     for(int i=0; i< kMaxTextureUnits;i++) {
477 
478         unsigned int tex = GL_TEXTURE0+i;
479         setClientActiveTexture(tex);
480         s_glDispatch.glClientActiveTexture(tex);
481 
482         GLenum array_id   = GL_TEXTURE_COORD_ARRAY;
483         GLESpointer* p = m_currVaoState[array_id];
484         if(!p->isEnable()) continue;
485         setupArrayPointerHelper(cArrs,first,count,type,indices,direct,array_id,p);
486     }
487 
488     setClientActiveTexture(activeTexture);
489     s_glDispatch.glClientActiveTexture(activeTexture);
490 }
491 
drawPointsData(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices_in,bool isElemsDraw)492 void  GLEScmContext::drawPointsData(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices_in,bool isElemsDraw) {
493     const char  *pointsArr =  NULL;
494     int stride = 0;
495     GLESpointer* p = m_currVaoState[GL_POINT_SIZE_ARRAY_OES];
496 
497     //choosing the right points sizes array source
498     if(m_pointsIndex >= 0) { //point size array was converted
499         pointsArr = (const char*)(cArrs[m_pointsIndex].data);
500         stride = cArrs[m_pointsIndex].stride;
501     } else {
502         pointsArr = static_cast<const char*>(p->getData());
503         stride = p->getStride();
504     }
505 
506     if(stride == 0){
507         stride = sizeof(GLfloat);
508     }
509 
510 
511     if(isElemsDraw) {
512         int tSize = 0;
513         switch (type) {
514             case GL_UNSIGNED_BYTE:
515                 tSize = 1;
516                 break;
517             case GL_UNSIGNED_SHORT:
518                 tSize = 2;
519                 break;
520             case GL_UNSIGNED_INT:
521                 tSize = 4;
522                 break;
523         };
524 
525         int i = 0;
526         while(i<count)
527         {
528             int sStart = i;
529             int sCount = 1;
530 
531 #define INDEX \
532                 (type == GL_UNSIGNED_INT ? \
533                 static_cast<const GLuint*>(indices_in)[i]: \
534                 type == GL_UNSIGNED_SHORT ? \
535                 static_cast<const GLushort*>(indices_in)[i]: \
536                 static_cast<const GLubyte*>(indices_in)[i])
537 
538             GLfloat pSize = *((GLfloat*)(pointsArr+(INDEX*stride)));
539             i++;
540 
541             while(i < count && pSize == *((GLfloat*)(pointsArr+(INDEX*stride))))
542             {
543                 sCount++;
544                 i++;
545             }
546 
547             s_glDispatch.glPointSize(pSize);
548             s_glDispatch.glDrawElements(GL_POINTS, sCount, type, (char*)indices_in+sStart*tSize);
549         }
550     } else {
551         int i = 0;
552         while(i<count)
553         {
554             int sStart = i;
555             int sCount = 1;
556             GLfloat pSize = *((GLfloat*)(pointsArr+((first+i)*stride)));
557             i++;
558 
559             while(i < count && pSize == *((GLfloat*)(pointsArr+((first+i)*stride))))
560             {
561                 sCount++;
562                 i++;
563             }
564 
565             s_glDispatch.glPointSize(pSize);
566             s_glDispatch.glDrawArrays(GL_POINTS, first+sStart, sCount);
567         }
568     }
569 }
570 
drawPointsArrs(GLESConversionArrays & arrs,GLint first,GLsizei count)571 void  GLEScmContext::drawPointsArrs(GLESConversionArrays& arrs,GLint first,GLsizei count) {
572     drawPointsData(arrs,first,count,0,NULL,false);
573 }
574 
drawPointsElems(GLESConversionArrays & arrs,GLsizei count,GLenum type,const GLvoid * indices_in)575 void GLEScmContext::drawPointsElems(GLESConversionArrays& arrs,GLsizei count,GLenum type,const GLvoid* indices_in) {
576     drawPointsData(arrs,0,count,type,indices_in,true);
577 }
578 
doConvert(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices,bool direct,GLESpointer * p,GLenum array_id)579 bool GLEScmContext::doConvert(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) {
580     return needConvert(cArrs, first, count, type, indices, direct, p, array_id);
581 }
582 
getColor(uint32_t count,std::vector<float> & out) const583 void GLEScmContext::getColor(uint32_t count, std::vector<float>& out) const {
584     std::vector<float> vec(4);
585     switch (mColor.type) {
586         case GL_UNSIGNED_BYTE:
587             vec = { mColor.val.ubyteVal[0] / 255.0f,
588                     mColor.val.ubyteVal[1] / 255.0f,
589                     mColor.val.ubyteVal[2] / 255.0f,
590                     mColor.val.ubyteVal[3] / 255.0f, };
591         default:
592             vec = { mColor.val.floatVal[0],
593                     mColor.val.floatVal[1],
594                     mColor.val.floatVal[2],
595                     mColor.val.floatVal[3], };
596     }
597 
598     appendRepeatedVector(count, vec, out);
599 }
600 
getNormal(uint32_t count,std::vector<float> & out) const601 void GLEScmContext::getNormal(uint32_t count, std::vector<float>& out) const {
602     std::vector<float> vec = { mNormal.val.floatVal[0],
603           mNormal.val.floatVal[1],
604           mNormal.val.floatVal[2] };
605 
606     appendRepeatedVector(count, vec, out);
607 }
608 
getMultiTexCoord(uint32_t count,uint32_t index,std::vector<float> & out) const609 void GLEScmContext::getMultiTexCoord(uint32_t count, uint32_t index, std::vector<float>& out) const {
610     // s, t, r, qcomponents
611     std::vector<float> vec = { mMultiTexCoord[index].floatVal[0],
612           mMultiTexCoord[index].floatVal[1],
613           mMultiTexCoord[index].floatVal[2],
614           mMultiTexCoord[index].floatVal[3] };
615 
616     appendRepeatedVector(count, vec, out);
617 }
618 
appendRepeatedVector(uint32_t count,std::vector<float> & in,std::vector<float> & out) const619 void GLEScmContext::appendRepeatedVector(uint32_t count, std::vector<float>& in, std::vector<float>& out) const {
620     size_t previousOutSize = out.size();
621     out.resize(previousOutSize + (count * in.size()));
622     auto it = out.begin() + previousOutSize;
623     for (int i = 0; i < count; i++) {
624         std::copy(in.begin(), in.end(), it);
625         it += in.size();
626     }
627 }
628 
getTextureEnvMode()629 GLenum GLEScmContext::getTextureEnvMode() {
630     return mTexUnitEnvs[m_activeTexture][GL_TEXTURE_ENV_MODE].val.intVal[0];
631 }
632 
getTextureGenMode()633 GLenum GLEScmContext::getTextureGenMode() {
634     return mTexGens[m_activeTexture][GL_TEXTURE_GEN_MODE_OES].val.intVal[0];
635 }
636 
getProjMatrix()637 glm::mat4 GLEScmContext::getProjMatrix() {
638     return mProjMatrices.back();
639 }
640 
getModelviewMatrix()641 glm::mat4 GLEScmContext::getModelviewMatrix() {
642     return mModelviewMatrices.back();
643 }
644 
getTextureMatrix()645 glm::mat4 GLEScmContext::getTextureMatrix() {
646     return mTextureMatrices[m_activeTexture].back();
647 }
648 
currMatrix()649 glm::mat4& GLEScmContext::currMatrix() {
650     return currMatrixStack().back();
651 }
652 
currMatrixStack()653 GLEScmContext::MatrixStack& GLEScmContext::currMatrixStack() {
654     switch (mCurrMatrixMode) {
655     case GL_TEXTURE:
656         return mTextureMatrices[m_activeTexture];
657     case GL_PROJECTION:
658         return mProjMatrices;
659     case GL_MODELVIEW:
660         return mModelviewMatrices;
661     default:
662         break;
663         // emugl_crash_reporter("error: matrix mode set to 0x%x!", mCurrMatrixMode);
664     }
665     // Make compiler happy
666     return mModelviewMatrices;
667 }
668 
needConvert(GLESConversionArrays & cArrs,GLint first,GLsizei count,GLenum type,const GLvoid * indices,bool direct,GLESpointer * p,GLenum array_id)669 bool GLEScmContext::needConvert(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) {
670 
671     bool usingVBO = p->getAttribType() == GLESpointer::BUFFER;
672     GLenum arrType = p->getType();
673     /*
674      conversion is not necessary in the following cases:
675       (*) array type is byte but it is not vertex or texture array
676       (*) array type is not fixed
677     */
678     if((arrType != GL_FIXED) && (arrType != GL_BYTE)) return false;
679     if((arrType == GL_BYTE   && (array_id != GL_VERTEX_ARRAY)) &&
680        (arrType == GL_BYTE   && (array_id != GL_TEXTURE_COORD_ARRAY)) ) return false;
681 
682 
683     bool byteVBO = (arrType == GL_BYTE) && usingVBO;
684     if(byteVBO){
685         p->redirectPointerData();
686     }
687 
688     if(!usingVBO || byteVBO) {
689         if (direct) {
690             convertDirect(cArrs,first,count,array_id,p);
691         } else {
692             convertIndirect(cArrs,count,type,indices,array_id,p);
693         }
694     } else {
695         if (direct) {
696             convertDirectVBO(cArrs,first,count,array_id,p) ;
697         } else {
698             convertIndirectVBO(cArrs,count,type,indices,array_id,p);
699         }
700     }
701     return true;
702 }
703 
getPointer(GLenum arrType)704 const GLESpointer* GLEScmContext::getPointer(GLenum arrType) {
705     GLenum type =
706         arrType == GL_VERTEX_ARRAY_POINTER          ? GL_VERTEX_ARRAY :
707         arrType == GL_NORMAL_ARRAY_POINTER          ? GL_NORMAL_ARRAY :
708         arrType == GL_TEXTURE_COORD_ARRAY_POINTER   ? GL_TEXTURE_COORD_ARRAY :
709         arrType == GL_COLOR_ARRAY_POINTER           ? GL_COLOR_ARRAY :
710         arrType == GL_POINT_SIZE_ARRAY_POINTER_OES  ? GL_POINT_SIZE_ARRAY_OES :
711         0;
712     if(type != 0)
713     {
714         return GLEScontext::getPointer(type);
715     }
716     return NULL;
717 }
718 
initExtensionString()719 void GLEScmContext::initExtensionString() {
720     if (s_glExtensionsGles1Initialized) return;
721     initCapsLocked((const GLubyte*)getHostExtensionsString(&s_glDispatch).c_str(),
722                    m_nativeTextureDecompressionEnabled, s_glSupportGles1);
723     *s_glExtensionsGles1 = "GL_OES_blend_func_separate GL_OES_blend_equation_separate GL_OES_blend_subtract "
724                       "GL_OES_byte_coordinates GL_OES_compressed_paletted_texture GL_OES_point_size_array "
725                       "GL_OES_point_sprite GL_OES_single_precision GL_OES_stencil_wrap GL_OES_texture_env_crossbar "
726                       "GL_OES_texture_mirored_repeat GL_OES_EGL_image GL_OES_element_index_uint GL_OES_draw_texture "
727                       "GL_OES_texture_cube_map GL_OES_draw_texture ";
728     if (s_glSupportGles1.GL_OES_READ_FORMAT)
729         *s_glExtensionsGles1+="GL_OES_read_format ";
730     if (s_glSupportGles1.GL_EXT_FRAMEBUFFER_OBJECT) {
731         *s_glExtensionsGles1+="GL_OES_framebuffer_object GL_OES_depth24 GL_OES_depth32 GL_OES_fbo_render_mipmap "
732                          "GL_OES_rgb8_rgba8 GL_OES_stencil1 GL_OES_stencil4 GL_OES_stencil8 ";
733     }
734     if (s_glSupportGles1.GL_EXT_PACKED_DEPTH_STENCIL)
735         *s_glExtensionsGles1+="GL_OES_packed_depth_stencil ";
736     if (s_glSupportGles1.GL_EXT_TEXTURE_FORMAT_BGRA8888)
737         *s_glExtensionsGles1+="GL_EXT_texture_format_BGRA8888 GL_APPLE_texture_format_BGRA8888 ";
738     if (s_glSupportGles1.GL_ARB_MATRIX_PALETTE && s_glSupportGles1.GL_ARB_VERTEX_BLEND) {
739         *s_glExtensionsGles1+="GL_OES_matrix_palette ";
740         GLint max_palette_matrices=0;
741         GLint max_vertex_units=0;
742         dispatcher().glGetIntegerv(GL_MAX_PALETTE_MATRICES_OES,&max_palette_matrices);
743         dispatcher().glGetIntegerv(GL_MAX_VERTEX_UNITS_OES,&max_vertex_units);
744         if (max_palette_matrices>=32 && max_vertex_units>=4)
745             *s_glExtensionsGles1+="GL_OES_extended_matrix_palette ";
746     }
747     *s_glExtensionsGles1+="GL_OES_compressed_ETC1_RGB8_texture ";
748 
749     s_glExtensionsGles1Initialized = true;
750 }
751 
getMaxTexUnits()752 int GLEScmContext::getMaxTexUnits() {
753     return kMaxTextureUnits;
754 }
755 
glGetBooleanv(GLenum pname,GLboolean * params)756 bool GLEScmContext::glGetBooleanv(GLenum pname, GLboolean *params)
757 {
758     GLint iParam;
759 
760     if(glGetIntegerv(pname, &iParam))
761     {
762         *params = (iParam != 0);
763         return true;
764     }
765 
766     return false;
767 }
768 
glGetFixedv(GLenum pname,GLfixed * params)769 bool GLEScmContext::glGetFixedv(GLenum pname, GLfixed *params)
770 {
771     GLint iParam;
772 
773     if(glGetIntegerv(pname, &iParam))
774     {
775         *params = I2X(iParam);
776         return true;
777     }
778 
779     return false;
780 }
781 
glGetFloatv(GLenum pname,GLfloat * params)782 bool GLEScmContext::glGetFloatv(GLenum pname, GLfloat *params)
783 {
784     GLint iParam;
785 
786     if(glGetIntegerv(pname, &iParam))
787     {
788         *params = (GLfloat)iParam;
789         return true;
790     }
791 
792     return false;
793 }
794 
glGetIntegerv(GLenum pname,GLint * params)795 bool GLEScmContext::glGetIntegerv(GLenum pname, GLint *params)
796 {
797     if(GLEScontext::glGetIntegerv(pname, params))
798         return true;
799 
800     const GLESpointer* ptr = NULL;
801 
802     switch(pname){
803         case GL_VERTEX_ARRAY_BUFFER_BINDING:
804         case GL_VERTEX_ARRAY_SIZE:
805         case GL_VERTEX_ARRAY_STRIDE:
806         case GL_VERTEX_ARRAY_TYPE:
807             ptr = getPointer(GL_VERTEX_ARRAY_POINTER);
808             break;
809 
810         case GL_NORMAL_ARRAY_BUFFER_BINDING:
811         case GL_NORMAL_ARRAY_STRIDE:
812         case GL_NORMAL_ARRAY_TYPE:
813             ptr = getPointer(GL_NORMAL_ARRAY_POINTER);
814             break;
815 
816         case GL_COLOR_ARRAY_BUFFER_BINDING:
817         case GL_COLOR_ARRAY_SIZE:
818         case GL_COLOR_ARRAY_STRIDE:
819         case GL_COLOR_ARRAY_TYPE:
820             ptr = getPointer(GL_COLOR_ARRAY_POINTER);
821             break;
822 
823         case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING:
824         case GL_TEXTURE_COORD_ARRAY_SIZE:
825         case GL_TEXTURE_COORD_ARRAY_STRIDE:
826         case GL_TEXTURE_COORD_ARRAY_TYPE:
827             ptr = getPointer(GL_TEXTURE_COORD_ARRAY_POINTER);
828             break;
829 
830         case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES:
831         case GL_POINT_SIZE_ARRAY_STRIDE_OES:
832         case GL_POINT_SIZE_ARRAY_TYPE_OES:
833             ptr = getPointer(GL_POINT_SIZE_ARRAY_POINTER_OES);
834             break;
835 
836         default:
837             return false;
838     }
839 
840     switch(pname)
841     {
842         case GL_VERTEX_ARRAY_BUFFER_BINDING:
843         case GL_NORMAL_ARRAY_BUFFER_BINDING:
844         case GL_COLOR_ARRAY_BUFFER_BINDING:
845         case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING:
846         case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES:
847             *params = ptr ? ptr->getBufferName() : 0;
848             break;
849 
850         case GL_VERTEX_ARRAY_STRIDE:
851         case GL_NORMAL_ARRAY_STRIDE:
852         case GL_COLOR_ARRAY_STRIDE:
853         case GL_TEXTURE_COORD_ARRAY_STRIDE:
854         case GL_POINT_SIZE_ARRAY_STRIDE_OES:
855             *params = ptr ? ptr->getStride() : 0;
856             break;
857 
858         case GL_VERTEX_ARRAY_SIZE:
859         case GL_COLOR_ARRAY_SIZE:
860         case GL_TEXTURE_COORD_ARRAY_SIZE:
861             *params = ptr ? ptr->getSize() : 0;
862             break;
863 
864         case GL_VERTEX_ARRAY_TYPE:
865         case GL_NORMAL_ARRAY_TYPE:
866         case GL_COLOR_ARRAY_TYPE:
867         case GL_TEXTURE_COORD_ARRAY_TYPE:
868         case GL_POINT_SIZE_ARRAY_TYPE_OES:
869             *params = ptr ? ptr->getType() : 0;
870             break;
871     }
872 
873     return true;
874 }
875 
getErrorCoreProfile()876 GLint GLEScmContext::getErrorCoreProfile() {
877     return core().getAndClearLastError();
878 }
879 
enable(GLenum cap)880 void GLEScmContext::enable(GLenum cap) {
881     setEnable(cap, true);
882 
883     if (m_coreProfileEngine) {
884         core().enable(cap);
885     } else {
886         if (cap==GL_TEXTURE_GEN_STR_OES) {
887             dispatcher().glEnable(GL_TEXTURE_GEN_S);
888             dispatcher().glEnable(GL_TEXTURE_GEN_T);
889             dispatcher().glEnable(GL_TEXTURE_GEN_R);
890         } else {
891             dispatcher().glEnable(cap);
892         }
893     }
894 }
895 
disable(GLenum cap)896 void GLEScmContext::disable(GLenum cap) {
897     setEnable(cap, false);
898 
899     if (m_coreProfileEngine) {
900         core().disable(cap);
901     } else {
902         if (cap==GL_TEXTURE_GEN_STR_OES) {
903             dispatcher().glDisable(GL_TEXTURE_GEN_S);
904             dispatcher().glDisable(GL_TEXTURE_GEN_T);
905             dispatcher().glDisable(GL_TEXTURE_GEN_R);
906         } else {
907             dispatcher().glDisable(cap);
908         }
909     }
910 }
911 
shadeModel(GLenum mode)912 void GLEScmContext::shadeModel(GLenum mode) {
913     mShadeModel = mode;
914 
915     if (m_coreProfileEngine) {
916         core().shadeModel(mode);
917     } else {
918         dispatcher().glShadeModel(mode);
919     }
920 }
921 
matrixMode(GLenum mode)922 void GLEScmContext::matrixMode(GLenum mode) {
923     mCurrMatrixMode = mode;
924 
925     if (m_coreProfileEngine) {
926         core().matrixMode(mode);
927     } else {
928         dispatcher().glMatrixMode(mode);
929     }
930 }
931 
loadIdentity()932 void GLEScmContext::loadIdentity() {
933     currMatrix() = glm::mat4();
934 
935     if (m_coreProfileEngine) {
936         core().loadIdentity();
937     } else {
938         dispatcher().glLoadIdentity();
939     }
940 }
941 
loadMatrixf(const GLfloat * m)942 void GLEScmContext::loadMatrixf(const GLfloat* m) {
943     currMatrix() = glm::make_mat4(m);
944 
945     if (m_coreProfileEngine) {
946         core().loadMatrixf(m);
947     } else {
948         dispatcher().glLoadMatrixf(m);
949     }
950 }
951 
pushMatrix()952 void GLEScmContext::pushMatrix() {
953     if (currMatrixStack().size() >= kMaxMatrixStackSize) {
954         setGLerror(GL_STACK_OVERFLOW);
955         return;
956     }
957     currMatrixStack().emplace_back(currMatrixStack().back());
958 
959     if (m_coreProfileEngine) {
960         core().pushMatrix();
961     } else {
962         dispatcher().glPushMatrix();
963     }
964 }
965 
popMatrix()966 void GLEScmContext::popMatrix() {
967     if (currMatrixStack().size() == 1) {
968         setGLerror(GL_STACK_UNDERFLOW);
969         return;
970     }
971     currMatrixStack().pop_back();
972 
973     if (m_coreProfileEngine) {
974         core().popMatrix();
975     } else {
976         dispatcher().glPopMatrix();
977     }
978 }
979 
multMatrixf(const GLfloat * m)980 void GLEScmContext::multMatrixf(const GLfloat* m) {
981     currMatrix() *= glm::make_mat4(m);
982 
983     if (m_coreProfileEngine) {
984         core().multMatrixf(m);
985     } else {
986         dispatcher().glMultMatrixf(m);
987     }
988 }
989 
orthof(GLfloat left,GLfloat right,GLfloat bottom,GLfloat top,GLfloat zNear,GLfloat zFar)990 void GLEScmContext::orthof(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar) {
991     currMatrix() *= glm::ortho(left, right, bottom, top, zNear, zFar);
992 
993     if (m_coreProfileEngine) {
994         core().orthof(left, right, bottom, top, zNear, zFar);
995     } else {
996         dispatcher().glOrthof(left,right,bottom,top,zNear,zFar);
997     }
998 }
999 
frustumf(GLfloat left,GLfloat right,GLfloat bottom,GLfloat top,GLfloat zNear,GLfloat zFar)1000 void GLEScmContext::frustumf(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar) {
1001     currMatrix() *= glm::frustum(left, right, bottom, top, zNear, zFar);
1002 
1003     if (m_coreProfileEngine) {
1004         core().frustumf(left, right, bottom, top, zNear, zFar);
1005     } else {
1006         dispatcher().glFrustumf(left,right,bottom,top,zNear,zFar);
1007     }
1008 }
1009 
texEnvf(GLenum target,GLenum pname,GLfloat param)1010 void GLEScmContext::texEnvf(GLenum target, GLenum pname, GLfloat param) {
1011     // Assume |target| is GL_TEXTURE_ENV
1012     if (pname == GL_TEXTURE_ENV_MODE) {
1013         texEnvi(target, pname, (GLint)param);
1014     } else {
1015         mTexUnitEnvs[m_activeTexture][pname].val.floatVal[0] = param;
1016         mTexUnitEnvs[m_activeTexture][pname].type = GL_FLOAT;
1017     }
1018 
1019     if (m_coreProfileEngine) {
1020         core().texEnvf(target, pname, param);
1021     } else {
1022         dispatcher().glTexEnvf(target, pname, param);
1023     }
1024 }
1025 
texEnvfv(GLenum target,GLenum pname,const GLfloat * params)1026 void GLEScmContext::texEnvfv(GLenum target, GLenum pname, const GLfloat* params) {
1027     if (pname == GL_TEXTURE_ENV_COLOR) {
1028         for (int i = 0; i < 4; i++) {
1029             mTexUnitEnvs[m_activeTexture][pname].val.floatVal[i] = params[i];
1030             mTexUnitEnvs[m_activeTexture][pname].type = GL_FLOAT;
1031         }
1032     } else {
1033         texEnvf(target, pname, params[0]);
1034     }
1035 
1036     if (m_coreProfileEngine) {
1037         core().texEnvfv(target, pname, params);
1038     } else {
1039         dispatcher().glTexEnvfv(target, pname, params);
1040     }
1041 }
1042 
texEnvi(GLenum target,GLenum pname,GLint param)1043 void GLEScmContext::texEnvi(GLenum target, GLenum pname, GLint param) {
1044     mTexUnitEnvs[m_activeTexture][pname].val.intVal[0] = param;
1045     mTexUnitEnvs[m_activeTexture][pname].type = GL_INT;
1046 
1047     if (m_coreProfileEngine) {
1048         core().texEnvi(target, pname, param);
1049     } else {
1050         dispatcher().glTexEnvi(target, pname, param);
1051     }
1052 }
1053 
texEnviv(GLenum target,GLenum pname,const GLint * params)1054 void GLEScmContext::texEnviv(GLenum target, GLenum pname, const GLint* params) {
1055     mTexUnitEnvs[m_activeTexture][pname].val.intVal[0] = params[0];
1056     mTexUnitEnvs[m_activeTexture][pname].type = GL_INT;
1057 
1058     if (m_coreProfileEngine) {
1059         core().texEnviv(target, pname, params);
1060     } else {
1061         dispatcher().glTexEnviv(target, pname, params);
1062     }
1063 }
1064 
getTexEnvfv(GLenum env,GLenum pname,GLfloat * params)1065 void GLEScmContext::getTexEnvfv(GLenum env, GLenum pname, GLfloat* params) {
1066     *params = mTexUnitEnvs[m_activeTexture][pname].val.floatVal[0];
1067 
1068     if (m_coreProfileEngine) {
1069         core().getTexEnvfv(env, pname, params);
1070     } else {
1071         dispatcher().glGetTexEnvfv(env, pname, params);
1072     }
1073 }
1074 
getTexEnviv(GLenum env,GLenum pname,GLint * params)1075 void GLEScmContext::getTexEnviv(GLenum env, GLenum pname, GLint* params) {
1076     *params = mTexUnitEnvs[m_activeTexture][pname].val.intVal[0];
1077 
1078     if (m_coreProfileEngine) {
1079         core().getTexEnviv(env, pname, params);
1080     } else {
1081         dispatcher().glGetTexEnviv(env, pname, params);
1082     }
1083 }
1084 
texGenf(GLenum coord,GLenum pname,GLfloat param)1085 void GLEScmContext::texGenf(GLenum coord, GLenum pname, GLfloat param) {
1086     mTexGens[m_activeTexture][pname].val.floatVal[0] = param;
1087     mTexGens[m_activeTexture][pname].type = GL_FLOAT;
1088 
1089     if (m_coreProfileEngine) {
1090         core().texGenf(coord, pname, param);
1091     } else {
1092         if (coord == GL_TEXTURE_GEN_STR_OES) {
1093             dispatcher().glTexGenf(GL_S,pname,param);
1094             dispatcher().glTexGenf(GL_T,pname,param);
1095             dispatcher().glTexGenf(GL_R,pname,param);
1096         } else {
1097             dispatcher().glTexGenf(coord,pname,param);
1098         }
1099     }
1100 }
1101 
texGenfv(GLenum coord,GLenum pname,const GLfloat * params)1102 void GLEScmContext::texGenfv(GLenum coord, GLenum pname, const GLfloat* params) {
1103     mTexGens[m_activeTexture][pname].val.floatVal[0] = params[0];
1104     mTexGens[m_activeTexture][pname].type = GL_FLOAT;
1105 
1106     if (m_coreProfileEngine) {
1107         core().texGenfv(coord, pname, params);
1108     } else {
1109         if (coord == GL_TEXTURE_GEN_STR_OES) {
1110             dispatcher().glTexGenfv(GL_S,pname,params);
1111             dispatcher().glTexGenfv(GL_T,pname,params);
1112             dispatcher().glTexGenfv(GL_R,pname,params);
1113         } else {
1114             dispatcher().glTexGenfv(coord,pname,params);
1115         }
1116     }
1117 }
1118 
texGeni(GLenum coord,GLenum pname,GLint param)1119 void GLEScmContext::texGeni(GLenum coord, GLenum pname, GLint param) {
1120     mTexGens[m_activeTexture][pname].val.intVal[0] = param;
1121     mTexGens[m_activeTexture][pname].type = GL_INT;
1122 
1123     if (m_coreProfileEngine) {
1124         core().texGeni(coord, pname, param);
1125     } else {
1126         if (coord == GL_TEXTURE_GEN_STR_OES) {
1127             dispatcher().glTexGeni(GL_S,pname,param);
1128             dispatcher().glTexGeni(GL_T,pname,param);
1129             dispatcher().glTexGeni(GL_R,pname,param);
1130         } else {
1131             dispatcher().glTexGeni(coord,pname,param);
1132         }
1133     }
1134 }
1135 
texGeniv(GLenum coord,GLenum pname,const GLint * params)1136 void GLEScmContext::texGeniv(GLenum coord, GLenum pname, const GLint* params) {
1137     mTexGens[m_activeTexture][pname].val.intVal[0] = params[0];
1138     mTexGens[m_activeTexture][pname].type = GL_INT;
1139 
1140     if (m_coreProfileEngine) {
1141         core().texGeniv(coord, pname, params);
1142     } else {
1143         if (coord == GL_TEXTURE_GEN_STR_OES) {
1144             dispatcher().glTexGeniv(GL_S,pname,params);
1145             dispatcher().glTexGeniv(GL_T,pname,params);
1146             dispatcher().glTexGeniv(GL_R,pname,params);
1147         } else {
1148             dispatcher().glTexGeniv(coord,pname,params);
1149         }
1150     }
1151 }
1152 
getTexGeniv(GLenum coord,GLenum pname,GLint * params)1153 void GLEScmContext::getTexGeniv(GLenum coord, GLenum pname, GLint* params) {
1154     *params = mTexGens[m_activeTexture][pname].val.intVal[0];
1155 
1156     if (m_coreProfileEngine) {
1157         core().getTexGeniv(coord, pname, params);
1158     } else {
1159         if (coord == GL_TEXTURE_GEN_STR_OES) {
1160             GLint state_s = GL_FALSE;
1161             GLint state_t = GL_FALSE;
1162             GLint state_r = GL_FALSE;
1163             dispatcher().glGetTexGeniv(GL_S,pname,&state_s);
1164             dispatcher().glGetTexGeniv(GL_T,pname,&state_t);
1165             dispatcher().glGetTexGeniv(GL_R,pname,&state_r);
1166             *params = state_s && state_t && state_r ? GL_TRUE: GL_FALSE;
1167         } else {
1168             dispatcher().glGetTexGeniv(coord,pname,params);
1169         }
1170     }
1171 }
1172 
getTexGenfv(GLenum coord,GLenum pname,GLfloat * params)1173 void GLEScmContext::getTexGenfv(GLenum coord, GLenum pname, GLfloat* params) {
1174     params[0] = mTexGens[m_activeTexture][pname].val.floatVal[0];
1175     params[1] = mTexGens[m_activeTexture][pname].val.floatVal[1];
1176     params[2] = mTexGens[m_activeTexture][pname].val.floatVal[2];
1177     params[3] = mTexGens[m_activeTexture][pname].val.floatVal[3];
1178 
1179     if (m_coreProfileEngine) {
1180         core().getTexGenfv(coord, pname, params);
1181     } else {
1182         if (coord == GL_TEXTURE_GEN_STR_OES) {
1183             GLfloat state_s = GL_FALSE;
1184             GLfloat state_t = GL_FALSE;
1185             GLfloat state_r = GL_FALSE;
1186             dispatcher().glGetTexGenfv(GL_S,pname,&state_s);
1187             dispatcher().glGetTexGenfv(GL_T,pname,&state_t);
1188             dispatcher().glGetTexGenfv(GL_R,pname,&state_r);
1189             *params = state_s && state_t && state_r ? GL_TRUE: GL_FALSE;
1190         } else {
1191             dispatcher().glGetTexGenfv(coord,pname,params);
1192         }
1193     }
1194 }
1195 
materialf(GLenum face,GLenum pname,GLfloat param)1196 void GLEScmContext::materialf(GLenum face, GLenum pname, GLfloat param) {
1197     if (face != GL_FRONT_AND_BACK) {
1198         fprintf(stderr, "GL_INVALID_ENUM: GLES1's glMaterial(f/x) "
1199                         "only supports GL_FRONT_AND_BACK for materials.\n");
1200         setGLerror(GL_INVALID_ENUM);
1201         return;
1202     }
1203     switch (pname) {
1204     case GL_AMBIENT:
1205     case GL_DIFFUSE:
1206     case GL_AMBIENT_AND_DIFFUSE:
1207     case GL_SPECULAR:
1208     case GL_EMISSION:
1209         fprintf(stderr, "GL_INVALID_ENUM: glMaterial(f/x) only supports "
1210                         "GL_SHININESS for single parameter setting.\n");
1211         setGLerror(GL_INVALID_ENUM);
1212         return;
1213     case GL_SHININESS:
1214         if (param < 0.0f || param > 128.0f) {
1215             fprintf(stderr, "GL_INVALID_VALUE: Invalid specular exponent value %f. "
1216                             "Only range [0.0, 128.0] supported.\n", param);
1217             setGLerror(GL_INVALID_VALUE);
1218             return;
1219         }
1220         mMaterial.specularExponent = param;
1221         break;
1222     default:
1223         fprintf(stderr, "Unknown parameter name 0x%x for glMaterial(f/x)\n", pname);
1224         setGLerror(GL_INVALID_ENUM);
1225         return;
1226     }
1227 
1228     if (!m_coreProfileEngine) {
1229         dispatcher().glMaterialf(face, pname, param);
1230     }
1231 }
1232 
materialfv(GLenum face,GLenum pname,const GLfloat * params)1233 void GLEScmContext::materialfv(GLenum face, GLenum pname, const GLfloat* params) {
1234     if (face != GL_FRONT_AND_BACK) {
1235         fprintf(stderr, "GL_INVALID_ENUM: GLES1's glMaterial(f/x)v "
1236                         "only supports GL_FRONT_AND_BACK for materials.\n");
1237         setGLerror(GL_INVALID_ENUM);
1238         return;
1239     }
1240 
1241     switch (pname) {
1242     case GL_AMBIENT:
1243         memcpy(&mMaterial.ambient, params, 4 * sizeof(GLfloat));
1244         break;
1245     case GL_DIFFUSE:
1246         memcpy(&mMaterial.diffuse, params, 4 * sizeof(GLfloat));
1247         break;
1248     case GL_AMBIENT_AND_DIFFUSE:
1249         memcpy(&mMaterial.ambient, params, 4 * sizeof(GLfloat));
1250         memcpy(&mMaterial.diffuse, params, 4 * sizeof(GLfloat));
1251         break;
1252     case GL_SPECULAR:
1253         memcpy(&mMaterial.specular, params, 4 * sizeof(GLfloat));
1254         break;
1255     case GL_EMISSION:
1256         memcpy(&mMaterial.emissive, params, 4 * sizeof(GLfloat));
1257         break;
1258     case GL_SHININESS:
1259         if (*params < 0.0f || *params > 128.0f) {
1260             fprintf(stderr, "GL_INVALID_VALUE: Invalid specular exponent value %f. "
1261                             "Only range [0.0, 128.0] supported.\n", *params);
1262             setGLerror(GL_INVALID_VALUE);
1263             return;
1264         }
1265         mMaterial.specularExponent = *params;
1266         break;
1267     default:
1268         fprintf(stderr, "Unknown parameter name 0x%x for glMaterial(f/x)v.\n", pname);
1269         setGLerror(GL_INVALID_ENUM);
1270         return;
1271     }
1272 
1273     if (!m_coreProfileEngine) {
1274         dispatcher().glMaterialfv(face, pname, params);
1275     }
1276 }
1277 
getMaterialfv(GLenum face,GLenum pname,GLfloat * params)1278 void GLEScmContext::getMaterialfv(GLenum face, GLenum pname, GLfloat* params) {
1279     if (face != GL_FRONT && face != GL_BACK) {
1280         fprintf(stderr, "GL_INVALID_ENUM: glGetMaterial(f/x)v "
1281                         "must take GL_FRONT or GL_BACK as face argument\n");
1282         setGLerror(GL_INVALID_ENUM);
1283         return;
1284     }
1285 
1286     switch (pname) {
1287     case GL_AMBIENT:
1288         memcpy(params, &mMaterial.ambient, 4 * sizeof(GLfloat));
1289         break;
1290     case GL_DIFFUSE:
1291         memcpy(params, &mMaterial.diffuse, 4 * sizeof(GLfloat));
1292         break;
1293     case GL_SPECULAR:
1294         memcpy(params, &mMaterial.specular, 4 * sizeof(GLfloat));
1295         break;
1296     case GL_EMISSION:
1297         memcpy(params, &mMaterial.emissive, 4 * sizeof(GLfloat));
1298         break;
1299     case GL_SHININESS:
1300         *params = mMaterial.specularExponent;
1301         break;
1302     default:
1303         fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x for glGetMaterial(f/x)v.\n", pname);
1304         setGLerror(GL_INVALID_ENUM);
1305         return;
1306     }
1307 
1308     if (!m_coreProfileEngine) {
1309         dispatcher().glGetMaterialfv(face, pname, params);
1310     }
1311 }
1312 
lightModelf(GLenum pname,GLfloat param)1313 void GLEScmContext::lightModelf(GLenum pname, GLfloat param) {
1314     switch (pname) {
1315         case GL_LIGHT_MODEL_AMBIENT:
1316             fprintf(stderr, "GL_INVALID_ENUM: glLightModelf only supports GL_LIGHT_MODEL_TWO_SIDE.\n");
1317             setGLerror(GL_INVALID_ENUM);
1318             return;
1319         case GL_LIGHT_MODEL_TWO_SIDE:
1320             if (param != 1.0f && param != 0.0f) {
1321                 fprintf(stderr, "GL_INVALID_VALUE: glLightModelf only takes 0 or 1 "
1322                                 "for GL_LIGHT_MODEL_TWO_SIDE, but got %f\n", param);
1323                 setGLerror(GL_INVALID_VALUE);
1324             }
1325             mLightModel.twoSided = param == 1.0f ? true : false;
1326             break;
1327         default:
1328             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x for glLightModel(f/x)v.\n", pname);
1329             setGLerror(GL_INVALID_ENUM);
1330             return;
1331     }
1332 
1333     if (!m_coreProfileEngine) {
1334         dispatcher().glLightModelf(pname, param);
1335     }
1336 }
1337 
lightModelfv(GLenum pname,const GLfloat * params)1338 void GLEScmContext::lightModelfv(GLenum pname, const GLfloat* params) {
1339     switch (pname) {
1340         case GL_LIGHT_MODEL_AMBIENT:
1341             memcpy(&mLightModel.color, params, 4 * sizeof(GLfloat));
1342             break;
1343         case GL_LIGHT_MODEL_TWO_SIDE:
1344             if (*params != 1.0f && *params != 0.0f) {
1345                 fprintf(stderr, "GL_INVALID_VALUE: glLightModelf only takes 0 or 1 "
1346                                 "for GL_LIGHT_MODEL_TWO_SIDE, but got %f\n", *params);
1347                 setGLerror(GL_INVALID_VALUE);
1348             }
1349             mLightModel.twoSided = *params == 1.0f ? true : false;
1350             break;
1351         default:
1352             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x for glLightModel(f/x)v.\n", pname);
1353             setGLerror(GL_INVALID_ENUM);
1354             return;
1355     }
1356 
1357     if (!m_coreProfileEngine) {
1358         dispatcher().glLightModelfv(pname, params);
1359     }
1360 }
1361 
lightf(GLenum light,GLenum pname,GLfloat param)1362 void GLEScmContext::lightf(GLenum light, GLenum pname, GLfloat param) {
1363     uint32_t lightIndex = light - GL_LIGHT0;
1364 
1365     if (lightIndex >= kMaxLights) {
1366         fprintf(stderr, "GL_INVALID_ENUM: Exceeded max lights for glLight(f/x) (wanted %u)\n",
1367                 lightIndex);
1368         setGLerror(GL_INVALID_ENUM);
1369         return;
1370     }
1371 
1372     switch (pname) {
1373         case GL_AMBIENT:
1374         case GL_DIFFUSE:
1375         case GL_SPECULAR:
1376         case GL_POSITION:
1377         case GL_SPOT_DIRECTION:
1378             fprintf(stderr, "GL_INVALID_ENUM: Invalid parameter name 0x%x "
1379                             "for glLight(f/x). Needs glLight(f/x)v.\n", pname);
1380             setGLerror(GL_INVALID_ENUM);
1381             return;
1382         case GL_SPOT_EXPONENT:
1383             mLights[lightIndex].spotlightExponent = param;
1384             break;
1385         case GL_SPOT_CUTOFF:
1386             mLights[lightIndex].spotlightCutoffAngle = param;
1387             break;
1388         case GL_CONSTANT_ATTENUATION:
1389             mLights[lightIndex].attenuationConst = param;
1390             break;
1391         case GL_LINEAR_ATTENUATION:
1392             mLights[lightIndex].attenuationLinear = param;
1393             break;
1394         case GL_QUADRATIC_ATTENUATION:
1395             mLights[lightIndex].attenuationQuadratic = param;
1396             break;
1397         default:
1398             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x "
1399                             "for glLight(f/x).\n", pname);
1400             setGLerror(GL_INVALID_ENUM);
1401             return;
1402     }
1403 
1404     if (!m_coreProfileEngine) {
1405         dispatcher().glLightf(light, pname, param);
1406     }
1407 }
1408 
lightfv(GLenum light,GLenum pname,const GLfloat * params)1409 void GLEScmContext::lightfv(GLenum light, GLenum pname, const GLfloat* params) {
1410     uint32_t lightIndex = light - GL_LIGHT0;
1411 
1412     if (lightIndex >= kMaxLights) {
1413         fprintf(stderr, "GL_INVALID_ENUM: Exceeded max lights for "
1414                         "glLight(f/x)v (wanted %u)\n",
1415                 lightIndex);
1416         setGLerror(GL_INVALID_ENUM);
1417         return;
1418     }
1419 
1420     switch (pname) {
1421         case GL_AMBIENT:
1422             memcpy(&mLights[lightIndex].ambient, params, 4 * sizeof(GLfloat));
1423             break;
1424         case GL_DIFFUSE:
1425             memcpy(&mLights[lightIndex].diffuse, params, 4 * sizeof(GLfloat));
1426             break;
1427         case GL_SPECULAR:
1428             memcpy(&mLights[lightIndex].specular, params, 4 * sizeof(GLfloat));
1429             break;
1430         case GL_POSITION:
1431             memcpy(&mLights[lightIndex].position, params, 4 * sizeof(GLfloat));
1432             break;
1433         case GL_SPOT_DIRECTION:
1434             memcpy(&mLights[lightIndex].direction, params, 3 * sizeof(GLfloat));
1435             break;
1436         case GL_SPOT_EXPONENT:
1437             mLights[lightIndex].spotlightExponent = *params;
1438             break;
1439         case GL_SPOT_CUTOFF:
1440             mLights[lightIndex].spotlightCutoffAngle = *params;
1441             break;
1442         case GL_CONSTANT_ATTENUATION:
1443             mLights[lightIndex].attenuationConst = *params;
1444             break;
1445         case GL_LINEAR_ATTENUATION:
1446             mLights[lightIndex].attenuationLinear = *params;
1447             break;
1448         case GL_QUADRATIC_ATTENUATION:
1449             mLights[lightIndex].attenuationQuadratic = *params;
1450             break;
1451         default:
1452             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x "
1453                             "for glLight(f/x).\n", pname);
1454             setGLerror(GL_INVALID_ENUM);
1455             return;
1456     }
1457 
1458     if (!m_coreProfileEngine) {
1459         dispatcher().glLightfv(light, pname, params);
1460     }
1461 }
1462 
getLightfv(GLenum light,GLenum pname,GLfloat * params)1463 void GLEScmContext::getLightfv(GLenum light, GLenum pname, GLfloat* params) {
1464     uint32_t lightIndex = light - GL_LIGHT0;
1465 
1466     if (lightIndex >= kMaxLights) {
1467         fprintf(stderr, "GL_INVALID_ENUM: Exceeded max lights for "
1468                         "glGetLight(f/x)v (wanted %u)\n",
1469                 lightIndex);
1470         setGLerror(GL_INVALID_ENUM);
1471         return;
1472     }
1473 
1474     switch (pname) {
1475         case GL_AMBIENT:
1476             memcpy(params, &mLights[lightIndex].ambient, 4 * sizeof(GLfloat));
1477             break;
1478         case GL_DIFFUSE:
1479             memcpy(params, &mLights[lightIndex].diffuse, 4 * sizeof(GLfloat));
1480             break;
1481         case GL_SPECULAR:
1482             memcpy(params, &mLights[lightIndex].specular, 4 * sizeof(GLfloat));
1483             break;
1484         case GL_POSITION:
1485             memcpy(params, &mLights[lightIndex].position, 4 * sizeof(GLfloat));
1486             break;
1487         case GL_SPOT_DIRECTION:
1488             memcpy(params, &mLights[lightIndex].direction, 3 * sizeof(GLfloat));
1489             break;
1490         case GL_SPOT_EXPONENT:
1491             *params = mLights[lightIndex].spotlightExponent;
1492             break;
1493         case GL_SPOT_CUTOFF:
1494             *params = mLights[lightIndex].spotlightCutoffAngle;
1495             break;
1496         case GL_CONSTANT_ATTENUATION:
1497             *params = mLights[lightIndex].attenuationConst;
1498             break;
1499         case GL_LINEAR_ATTENUATION:
1500             *params = mLights[lightIndex].attenuationLinear;
1501             break;
1502         case GL_QUADRATIC_ATTENUATION:
1503             *params = mLights[lightIndex].attenuationQuadratic;
1504             break;
1505         default:
1506             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x "
1507                             "for glGetLight(f/x).\n", pname);
1508             setGLerror(GL_INVALID_ENUM);
1509             return;
1510     }
1511 
1512     if (!m_coreProfileEngine) {
1513         dispatcher().glGetLightfv(light, pname, params);
1514     }
1515 }
1516 
multiTexCoord4f(GLenum target,GLfloat s,GLfloat t,GLfloat r,GLfloat q)1517 void GLEScmContext::multiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q) {
1518 
1519     mMultiTexCoord[target - GL_TEXTURE0].floatVal[0] = s;
1520     mMultiTexCoord[target - GL_TEXTURE0].floatVal[1] = t;
1521     mMultiTexCoord[target - GL_TEXTURE0].floatVal[2] = q;
1522     mMultiTexCoord[target - GL_TEXTURE0].floatVal[3] = r;
1523 
1524     if (!m_coreProfileEngine) {
1525         dispatcher().glMultiTexCoord4f(target, s, t, r, q);
1526     }
1527 }
1528 
normal3f(GLfloat nx,GLfloat ny,GLfloat nz)1529 void GLEScmContext::normal3f(GLfloat nx, GLfloat ny, GLfloat nz) {
1530     mNormal.type = GL_FLOAT;
1531     mNormal.val.floatVal[0] = nx;
1532     mNormal.val.floatVal[1] = ny;
1533     mNormal.val.floatVal[2] = nz;
1534 
1535     if (!m_coreProfileEngine) {
1536         dispatcher().glNormal3f(nx, ny, nz);
1537     }
1538 }
1539 
fogf(GLenum pname,GLfloat param)1540 void GLEScmContext::fogf(GLenum pname, GLfloat param) {
1541     switch (pname) {
1542         case GL_FOG_MODE: {
1543             GLenum mode = (GLenum)param;
1544             switch (mode) {
1545                 case GL_EXP:
1546                 case GL_EXP2:
1547                 case GL_LINEAR:
1548                     mFog.mode = mode;
1549                     break;
1550                 default:
1551                     fprintf(stderr, "GL_INVALID_ENUM: Unknown GL_FOG_MODE 0x%x "
1552                                     "for glFog(f/x).\n", mode);
1553                     setGLerror(GL_INVALID_ENUM);
1554                     break;
1555             }
1556             break;
1557         }
1558         case GL_FOG_DENSITY:
1559             if (param < 0.0f) {
1560                 fprintf(stderr, "GL_INVALID_VALUE: glFog(f/x): GL_FOG_DENSITY "
1561                                 "needs to be nonnegative, but got %f\n", param);
1562                 setGLerror(GL_INVALID_VALUE);
1563                 return;
1564             }
1565             mFog.density = param;
1566             break;
1567         case GL_FOG_START:
1568             mFog.start = param;
1569             break;
1570         case GL_FOG_END:
1571             mFog.end = param;
1572             break;
1573         case GL_FOG_COLOR:
1574             fprintf(stderr, "GL_INVALID_ENUM: GL_FOG_COLOR not allowed for glFog(f/x).\n");
1575             setGLerror(GL_INVALID_ENUM);
1576             break;
1577         default:
1578             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x "
1579                             "for glFog(f/x).\n", pname);
1580             setGLerror(GL_INVALID_ENUM);
1581             return;
1582     }
1583 
1584     if (!m_coreProfileEngine) {
1585         dispatcher().glFogf(pname, param);
1586     }
1587 }
1588 
fogfv(GLenum pname,const GLfloat * params)1589 void GLEScmContext::fogfv(GLenum pname, const GLfloat* params) {
1590     switch (pname) {
1591         case GL_FOG_MODE: {
1592             GLenum mode = (GLenum)params[0];
1593             switch (mode) {
1594                 case GL_EXP:
1595                 case GL_EXP2:
1596                 case GL_LINEAR:
1597                     mFog.mode = mode;
1598                     break;
1599                 default:
1600                     fprintf(stderr, "GL_INVALID_ENUM: Unknown GL_FOG_MODE 0x%x "
1601                                     "for glFog(f/x)v.\n", mode);
1602                     setGLerror(GL_INVALID_ENUM);
1603                     break;
1604             }
1605             break;
1606         }
1607         case GL_FOG_DENSITY:
1608             if (params[0] < 0.0f) {
1609                 fprintf(stderr, "GL_INVALID_VALUE: glFog(f/x)v: GL_FOG_DENSITY "
1610                                 "needs to be nonnegative, but got %f\n", params[0]);
1611                 setGLerror(GL_INVALID_VALUE);
1612                 return;
1613             }
1614             mFog.density = params[0];
1615             break;
1616         case GL_FOG_START:
1617             mFog.start = params[0];
1618             break;
1619         case GL_FOG_END:
1620             mFog.end = params[0];
1621             break;
1622         case GL_FOG_COLOR:
1623             memcpy(&mFog.color, params, 4 * sizeof(GLfloat));
1624             break;
1625         default:
1626             fprintf(stderr, "GL_INVALID_ENUM: Unknown parameter name 0x%x "
1627                             "for glFog(f/x)v.\n", pname);
1628             setGLerror(GL_INVALID_ENUM);
1629             return;
1630     }
1631 
1632     if (!m_coreProfileEngine) {
1633         dispatcher().glFogfv(pname, params);
1634     }
1635 }
1636 
enableClientState(GLenum clientState)1637 void GLEScmContext::enableClientState(GLenum clientState) {
1638     if (m_coreProfileEngine) {
1639         core().enableClientState(clientState);
1640     } else {
1641         dispatcher().glEnableClientState(clientState);
1642     }
1643 }
1644 
disableClientState(GLenum clientState)1645 void GLEScmContext::disableClientState(GLenum clientState) {
1646     if (m_coreProfileEngine) {
1647         core().disableClientState(clientState);
1648     } else {
1649         dispatcher().glDisableClientState(clientState);
1650     }
1651 }
1652 
drawTexOES(float x,float y,float z,float width,float height)1653 void GLEScmContext::drawTexOES(float x, float y, float z, float width, float height) {
1654     if (m_coreProfileEngine) {
1655         core().drawTexOES(x, y, z, width, height);
1656     } else {
1657         auto& gl = dispatcher();
1658 
1659         int numClipPlanes;
1660 
1661         GLint viewport[4] = {};
1662         z = (z>1 ? 1 : (z<0 ?  0 : z));
1663 
1664         float vertices[4*3] = {
1665             x , y, z,
1666             x , static_cast<float>(y+height), z,
1667             static_cast<float>(x+width), static_cast<float>(y+height), z,
1668             static_cast<float>(x+width), y, z
1669         };
1670         GLfloat texels[getMaxTexUnits()][4*2];
1671         memset((void*)texels, 0, getMaxTexUnits()*4*2*sizeof(GLfloat));
1672 
1673         gl.glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
1674         gl.glPushAttrib(GL_TRANSFORM_BIT);
1675 
1676         //setup projection matrix to draw in viewport aligned coordinates
1677         gl.glMatrixMode(GL_PROJECTION);
1678         gl.glPushMatrix();
1679         gl.glLoadIdentity();
1680         gl.glGetIntegerv(GL_VIEWPORT,viewport);
1681         gl.glOrtho(viewport[0],viewport[0] + viewport[2],viewport[1],viewport[1]+viewport[3],0,-1);
1682         //setup texture matrix
1683         gl.glMatrixMode(GL_TEXTURE);
1684         gl.glPushMatrix();
1685         gl.glLoadIdentity();
1686         //setup modelview matrix
1687         gl.glMatrixMode(GL_MODELVIEW);
1688         gl.glPushMatrix();
1689         gl.glLoadIdentity();
1690         //backup vbo's
1691         int array_buffer,element_array_buffer;
1692         gl.glGetIntegerv(GL_ARRAY_BUFFER_BINDING,&array_buffer);
1693         gl.glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING,&element_array_buffer);
1694         gl.glBindBuffer(GL_ARRAY_BUFFER,0);
1695         gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
1696 
1697         //disable clip planes
1698         gl.glGetIntegerv(GL_MAX_CLIP_PLANES,&numClipPlanes);
1699         for (int i=0;i<numClipPlanes;++i)
1700             gl.glDisable(GL_CLIP_PLANE0+i);
1701 
1702         int nTexPtrs = 0;
1703         for (int i=0;i<getMaxTexUnits();++i) {
1704             if (isTextureUnitEnabled(GL_TEXTURE0+i)) {
1705                 TextureData * texData = NULL;
1706                 unsigned int texname = getBindedTexture(GL_TEXTURE0+i,GL_TEXTURE_2D);
1707                 ObjectLocalName tex = getTextureLocalName(GL_TEXTURE_2D,texname);
1708                 gl.glClientActiveTexture(GL_TEXTURE0+i);
1709                 auto objData = shareGroup()->getObjectData(
1710                         NamedObjectType::TEXTURE, tex);
1711                 if (objData) {
1712                     texData = (TextureData*)objData;
1713                     //calculate texels
1714                     texels[i][0] = (float)(texData->crop_rect[0])/(float)(texData->width);
1715                     texels[i][1] = (float)(texData->crop_rect[1])/(float)(texData->height);
1716 
1717                     texels[i][2] = (float)(texData->crop_rect[0])/(float)(texData->width);
1718                     texels[i][3] = (float)(texData->crop_rect[3]+texData->crop_rect[1])/(float)(texData->height);
1719 
1720                     texels[i][4] = (float)(texData->crop_rect[2]+texData->crop_rect[0])/(float)(texData->width);
1721                     texels[i][5] = (float)(texData->crop_rect[3]+texData->crop_rect[1])/(float)(texData->height);
1722 
1723                     texels[i][6] = (float)(texData->crop_rect[2]+texData->crop_rect[0])/(float)(texData->width);
1724                     texels[i][7] = (float)(texData->crop_rect[1])/(float)(texData->height);
1725 
1726                     gl.glTexCoordPointer(2,GL_FLOAT,0,texels[i]);
1727                     nTexPtrs++;
1728                 }
1729             }
1730         }
1731 
1732         if (nTexPtrs>0) {
1733             //draw rectangle - only if we have some textures enabled & ready
1734             gl.glEnableClientState(GL_VERTEX_ARRAY);
1735             gl.glVertexPointer(3,GL_FLOAT,0,vertices);
1736             gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1737             gl.glDrawArrays(GL_TRIANGLE_FAN,0,4);
1738         }
1739 
1740         //restore vbo's
1741         gl.glBindBuffer(GL_ARRAY_BUFFER,array_buffer);
1742         gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,element_array_buffer);
1743 
1744         //restore matrix state
1745 
1746         gl.glMatrixMode(GL_MODELVIEW);
1747         gl.glPopMatrix();
1748         gl.glMatrixMode(GL_TEXTURE);
1749         gl.glPopMatrix();
1750         gl.glMatrixMode(GL_PROJECTION);
1751         gl.glPopMatrix();
1752 
1753         gl.glPopAttrib();
1754         gl.glPopClientAttrib();
1755     }
1756 }
1757 
rotatef(float angle,float x,float y,float z)1758 void GLEScmContext::rotatef(float angle, float x, float y, float z) {
1759     glm::mat4 rot = glm::rotate(glm::mat4(), 3.14159265358979f / 180.0f * angle, glm::vec3(x, y, z));
1760     currMatrix() *= rot;
1761 
1762     if (m_coreProfileEngine) {
1763         core().rotatef(angle, x, y, z);
1764     } else {
1765         dispatcher().glRotatef(angle, x, y, z);
1766     }
1767 }
1768 
scalef(float x,float y,float z)1769 void GLEScmContext::scalef(float x, float y, float z) {
1770     glm::mat4 scale = glm::scale(glm::mat4(), glm::vec3(x, y, z));
1771     currMatrix() *= scale;
1772 
1773     if (m_coreProfileEngine) {
1774         core().scalef(x, y, z);
1775     } else {
1776         dispatcher().glScalef(x, y, z);
1777     }
1778 }
1779 
translatef(float x,float y,float z)1780 void GLEScmContext::translatef(float x, float y, float z) {
1781     glm::mat4 tr = glm::translate(glm::mat4(), glm::vec3(x, y, z));
1782     currMatrix() *= tr;
1783 
1784     if (m_coreProfileEngine) {
1785         core().translatef(x, y, z);
1786     } else {
1787         dispatcher().glTranslatef(x, y, z);
1788     }
1789 }
1790 
color4f(GLfloat red,GLfloat green,GLfloat blue,GLfloat alpha)1791 void GLEScmContext::color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
1792 
1793     mColor.type = GL_FLOAT;
1794     mColor.val.floatVal[0] = red;
1795     mColor.val.floatVal[1] = green;
1796     mColor.val.floatVal[2] = blue;
1797     mColor.val.floatVal[3] = alpha;
1798 
1799     if (m_coreProfileEngine) {
1800         core().color4f(red,green,blue,alpha);
1801     } else{
1802         dispatcher().glColor4f(red,green,blue,alpha);
1803     }
1804 }
1805 
color4ub(GLubyte red,GLubyte green,GLubyte blue,GLubyte alpha)1806 void GLEScmContext::color4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha) {
1807 
1808     mColor.type = GL_UNSIGNED_BYTE;
1809     mColor.val.ubyteVal[0] = red;
1810     mColor.val.ubyteVal[1] = green;
1811     mColor.val.ubyteVal[2] = blue;
1812     mColor.val.ubyteVal[3] = alpha;
1813 
1814     if (m_coreProfileEngine) {
1815         core().color4ub(red,green,blue,alpha);
1816     } else{
1817         dispatcher().glColor4ub(red,green,blue,alpha);
1818     }
1819 }
1820 
drawArrays(GLenum mode,GLint first,GLsizei count)1821 void GLEScmContext::drawArrays(GLenum mode, GLint first, GLsizei count) {
1822     if (!isArrEnabled(GL_VERTEX_ARRAY)) return;
1823 
1824     drawValidate();
1825 
1826     GLuint prev_vbo;
1827     GLuint prev_ibo;
1828     dispatcher().glGetIntegerv(GL_ARRAY_BUFFER_BINDING,
1829             (GLint*)&prev_vbo);
1830     dispatcher().glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING,
1831             (GLint*)&prev_ibo);
1832     dispatcher().glBindBuffer(GL_ARRAY_BUFFER, 0);
1833     dispatcher().glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1834 
1835     if (m_coreProfileEngine) {
1836         ArraysMap::iterator it;
1837         m_pointsIndex = -1;
1838 
1839         // going over all clients arrays Pointers
1840         for (it = m_currVaoState.begin();
1841              it != m_currVaoState.end(); ++it) {
1842             GLenum array_id = (*it).first;
1843             GLESpointer* p  = (*it).second;
1844             if (array_id == GL_VERTEX_ARRAY ||
1845                 array_id == GL_NORMAL_ARRAY ||
1846                 array_id == GL_COLOR_ARRAY ||
1847                 array_id == GL_POINT_SIZE_ARRAY_OES ||
1848                 array_id == GL_TEXTURE_COORD_ARRAY) {
1849                 core().setupArrayForDraw(array_id, p, first, count, false, 0, nullptr);
1850             }
1851         }
1852 
1853         GLenum activeTexture = m_clientActiveTexture + GL_TEXTURE0;
1854         setClientActiveTexture(activeTexture);
1855         core().clientActiveTexture(activeTexture);
1856         core().drawArrays(mode, first, count);
1857     } else {
1858         GLESConversionArrays tmpArrs;
1859 
1860         setupArraysPointers(tmpArrs,first,count,0,NULL,true,nullptr);
1861 
1862         if (mode == GL_POINTS && isArrEnabled(GL_POINT_SIZE_ARRAY_OES)){
1863             drawPointsArrs(tmpArrs,first,count);
1864         } else {
1865             dispatcher().glDrawArrays(mode,first,count);
1866         }
1867     }
1868     dispatcher().glBindBuffer(GL_ARRAY_BUFFER, prev_vbo);
1869     dispatcher().glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prev_ibo);
1870 }
1871 
drawElements(GLenum mode,GLsizei count,GLenum type,const GLvoid * indices)1872 void GLEScmContext::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices) {
1873     if (!isArrEnabled(GL_VERTEX_ARRAY)) return;
1874 
1875     drawValidate();
1876 
1877     if(isBindedBuffer(GL_ELEMENT_ARRAY_BUFFER)) { // if vbo is binded take the indices from the vbo
1878         const unsigned char* buf = static_cast<unsigned char *>(getBindedBuffer(GL_ELEMENT_ARRAY_BUFFER));
1879         indices = buf + SafeUIntFromPointer(indices);
1880     }
1881 
1882     GLuint prev_vbo;
1883     GLuint prev_ibo;
1884     dispatcher().glGetIntegerv(GL_ARRAY_BUFFER_BINDING,
1885             (GLint*)&prev_vbo);
1886     dispatcher().glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING,
1887             (GLint*)&prev_ibo);
1888     dispatcher().glBindBuffer(GL_ARRAY_BUFFER, 0);
1889     dispatcher().glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1890 
1891     if (m_coreProfileEngine) {
1892         ArraysMap::iterator it;
1893         m_pointsIndex = -1;
1894 
1895         // going over all clients arrays Pointers
1896         for (it = m_currVaoState.begin();
1897              it != m_currVaoState.end(); ++it) {
1898             GLenum array_id = (*it).first;
1899             GLESpointer* p  = (*it).second;
1900             if (array_id == GL_VERTEX_ARRAY ||
1901                 array_id == GL_NORMAL_ARRAY ||
1902                 array_id == GL_COLOR_ARRAY ||
1903                 array_id == GL_POINT_SIZE_ARRAY_OES ||
1904                 array_id == GL_TEXTURE_COORD_ARRAY) {
1905                 core().setupArrayForDraw(array_id, p, 0, count, true, type, indices);
1906             }
1907         }
1908 
1909         GLenum activeTexture = m_clientActiveTexture + GL_TEXTURE0;
1910         setClientActiveTexture(activeTexture);
1911         core().clientActiveTexture(activeTexture);
1912         core().drawElements(mode, count, type, indices);
1913     } else {
1914         GLESConversionArrays tmpArrs;
1915 
1916         setupArraysPointers(tmpArrs,0,count,type,indices,false,nullptr);
1917         if(mode == GL_POINTS && isArrEnabled(GL_POINT_SIZE_ARRAY_OES)){
1918             drawPointsElems(tmpArrs,count,type,indices);
1919         }
1920         else{
1921             dispatcher().glDrawElements(mode,count,type,indices);
1922         }
1923     }
1924     dispatcher().glBindBuffer(GL_ARRAY_BUFFER, prev_vbo);
1925     dispatcher().glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prev_ibo);
1926 }
1927 
clientActiveTexture(GLenum texture)1928 void GLEScmContext::clientActiveTexture(GLenum texture) {
1929     setClientActiveTexture(texture);
1930     if (m_coreProfileEngine) {
1931         core().clientActiveTexture(texture);
1932     } else {
1933         dispatcher().glClientActiveTexture(texture);
1934     }
1935 }
1936