1 /*
2 * Copyright 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 #ifndef GLES_CONTEXT_H
18 #define GLES_CONTEXT_H
19 
20 #include "aemu/base/containers/Lookup.h"
21 #include "aemu/base/files/Stream.h"
22 #include "aemu/base/synchronization/Lock.h"
23 
24 #include "GLDispatch.h"
25 #include "GLESpointer.h"
26 #include "ObjectNameSpace.h"
27 #include "ShareGroup.h"
28 
29 #include <memory>
30 #include <string>
31 #include <unordered_map>
32 #include <vector>
33 #include <functional>
34 
35 static constexpr int kMaxVertexAttributes = 16;
36 
37 typedef std::unordered_map<GLenum,GLESpointer*>  ArraysMap;
38 
39 enum TextureTarget {
40 TEXTURE_2D,
41 TEXTURE_CUBE_MAP,
42 TEXTURE_2D_ARRAY,
43 TEXTURE_3D,
44 TEXTURE_2D_MULTISAMPLE,
45 TEXTURE_BUFFER,
46 NUM_TEXTURE_TARGETS
47 };
48 
49 typedef struct _textureTargetState {
50     GLuint texture;
51     GLboolean enabled;
52 } textureTargetState;
53 
54 typedef textureTargetState textureUnitState[NUM_TEXTURE_TARGETS];
55 
56 class Version{
57 public:
58     explicit Version(int major = 0,int minor = 0,int release = 0);
59     Version(const char* versionString);
60     Version(const Version& ver);
61     bool operator<(const Version& ver) const;
62     Version& operator=(const Version& ver);
63 private:
64     int m_major;
65     int m_minor;
66     int m_release;
67 };
68 
69 struct GLSupport {
70     int  maxLights = 0;
71     int  maxVertexAttribs = 0;
72     int  maxClipPlane = 0;
73     int  maxTexUnits = 0;
74     int  maxTexImageUnits = 0;
75     int  maxTexSize = 0;
76     int  maxCombinedTexImageUnits = 0;
77 
78     int  maxTransformFeedbackSeparateAttribs = 0;
79     int  maxUniformBufferBindings = 0;
80     int  maxAtomicCounterBufferBindings = 0;
81     int  maxShaderStorageBufferBindings = 0;
82     int  maxVertexAttribBindings = 0;
83 
84     int maxDrawBuffers = 1;
85 
86     Version glslVersion;
87     bool GL_EXT_TEXTURE_FORMAT_BGRA8888 = false;
88     bool GL_EXT_FRAMEBUFFER_OBJECT = false;
89     bool GL_ARB_VERTEX_BLEND = false;
90     bool GL_ARB_MATRIX_PALETTE = false;
91     bool GL_EXT_PACKED_DEPTH_STENCIL = false;
92     bool GL_OES_READ_FORMAT = false;
93     bool GL_ARB_HALF_FLOAT_PIXEL = false;
94     bool GL_NV_HALF_FLOAT = false;
95     bool GL_ARB_HALF_FLOAT_VERTEX = false;
96     bool GL_SGIS_GENERATE_MIPMAP = false;
97     bool GL_ARB_ES2_COMPATIBILITY = false;
98     bool GL_OES_STANDARD_DERIVATIVES = false;
99     bool GL_OES_TEXTURE_NPOT = false;
100     bool GL_OES_RGB8_RGBA8 = false;
101 
102     bool ext_GL_OVR_multiview2 = false;
103     bool ext_GL_EXT_multiview_texture_multisample = false;
104 
105     bool ext_GL_OES_texture_buffer = false;
106 
107     bool ext_GL_EXT_color_buffer_float = false;
108     bool ext_GL_EXT_color_buffer_half_float = false;
109     bool ext_GL_EXT_shader_framebuffer_fetch = false;
110     bool ext_GL_EXT_texture_buffer = false;
111     bool ext_GL_EXT_draw_buffers_indexed = false;
112     bool ext_GL_EXT_clip_cull_distance = false;
113 
114     bool ext_GL_EXT_memory_object = false;
115     bool ext_GL_EXT_semaphore = false;
116 
117     bool ext_GL_KHR_texture_compression_astc_ldr = false;
118 
textureBufferAnyGLSupport119     bool textureBufferAny() const { return ext_GL_OES_texture_buffer || ext_GL_EXT_texture_buffer; }
120 
121     bool hasEtc2Support = false;
122     bool hasAstcSupport = false;
123     bool hasBptcSupport = false;
124     bool hasS3tcSupport = false;
125     bool hasRgtcSupport = false;
126 };
127 
128 struct ArrayData {
129     void*        data = nullptr;
130     GLenum       type = 0;
131     unsigned int stride = 0;
132     bool         allocated = false;
133 };
134 
135 struct BlendState {
136     GLboolean bEnable = GL_FALSE;
137     GLenum blendEquationRgb = GL_FUNC_ADD;
138     GLenum blendEquationAlpha = GL_FUNC_ADD;
139 
140     GLenum blendSrcRgb = GL_ONE;
141     GLenum blendDstRgb = GL_ZERO;
142     GLenum blendSrcAlpha = GL_ONE;
143     GLenum blendDstAlpha = GL_ZERO;
144     GLboolean colorMaskR = GL_TRUE;
145     GLboolean colorMaskG = GL_TRUE;
146     GLboolean colorMaskB = GL_TRUE;
147     GLboolean colorMaskA = GL_TRUE;
148 };
149 
150 struct BufferBinding {
151     GLuint buffer = 0;
152     GLintptr offset = 0;
153     GLsizeiptr size = 0;
154     GLintptr stride = 0;
155     GLuint divisor = 0;
156     bool isBindBase = false;
157     void onLoad(android::base::Stream* stream);
158     void onSave(android::base::Stream* stream) const;
159 };
160 
161 typedef std::vector<GLESpointer> VertexAttribInfoVector;
162 typedef std::vector<BufferBinding> VertexAttribBindingVector;
163 
164 struct VAOState {
VAOStateVAOState165     VAOState() : VAOState(0, NULL, 0) { }
VAOStateVAOState166     VAOState(GLuint ibo, ArraysMap* arr, int numVertexAttribBindings) :
167         element_array_buffer_binding(ibo),
168         vertexAttribInfo(numVertexAttribBindings),
169         bindingState(numVertexAttribBindings),
170         bufferBacked(false),
171         everBound(false),
172         legacy(arr != nullptr),
173         arraysMap(arr) { }
174     VAOState(android::base::Stream* stream);
175     GLuint element_array_buffer_binding;
176     VertexAttribInfoVector vertexAttribInfo;
177     VertexAttribBindingVector bindingState;
178     bool bufferBacked;
179     bool everBound;
180     bool legacy = false;
181     std::unique_ptr<ArraysMap> arraysMap;
182     void onSave(android::base::Stream* stream) const;
183 };
184 
185 typedef std::unordered_map<GLuint, VAOState> VAOStateMap;
186 
187 struct VAOStateRef {
VAOStateRefVAOStateRef188     VAOStateRef() { }
VAOStateRefVAOStateRef189     VAOStateRef(VAOStateMap::iterator iter) : it(iter) { }
vaoIdVAOStateRef190     GLuint vaoId() const { return it->first; }
iboIdVAOStateRef191     GLuint& iboId() { return it->second.element_array_buffer_binding; }
192 
attribInfo_constVAOStateRef193     const VertexAttribInfoVector& attribInfo_const() const {
194         return it->second.vertexAttribInfo;
195     }
196 
attribInfoVAOStateRef197     VertexAttribInfoVector& attribInfo() {
198         return it->second.vertexAttribInfo;
199     }
200 
beginVAOStateRef201     ArraysMap::iterator begin() {
202         return it->second.arraysMap->begin();
203     }
endVAOStateRef204     ArraysMap::iterator end() {
205         return it->second.arraysMap->end();
206     }
findVAOStateRef207     ArraysMap::iterator find(GLenum arrType) {
208         return it->second.arraysMap->find(arrType);
209     }
210     GLESpointer*& operator[](size_t k) {
211         ArraysMap* map = it->second.arraysMap.get();
212         return (*map)[k];
213     }
bufferBindingsVAOStateRef214     VertexAttribBindingVector& bufferBindings() {
215         return it->second.bindingState;
216     }
setEverBoundVAOStateRef217     void setEverBound() {
218         it->second.everBound = true;
219     }
isEverBoundVAOStateRef220     bool isEverBound() {
221         return it->second.everBound;
222     }
223     VAOStateMap::iterator it;
224 };
225 
226 class FramebufferData;
227 
228 class GLESConversionArrays
229 {
230 public:
231     void setArr(void* data,unsigned int stride,GLenum type);
232     void allocArr(unsigned int size,GLenum type);
233     ArrayData& operator[](int i);
234     void* getCurrentData();
235     ArrayData& getCurrentArray();
236     unsigned int getCurrentIndex();
237     void operator++();
238 
239     ~GLESConversionArrays();
240 private:
241     std::unordered_map<GLenum,ArrayData> m_arrays;
242     unsigned int m_current = 0;
243 };
244 
245 
246 class GLEScontext{
247 public:
248     GLEScontext();
249     GLEScontext(GlobalNameSpace* globalNameSpace, android::base::Stream* stream,
250             GlLibrary* glLib);
251     virtual void init(bool nativeTextureDecompressionEnabled);
252     static void initGlobal(EGLiface* eglIface);
253     GLenum getGLerror();
254     void setGLerror(GLenum err);
setShareGroup(ShareGroupPtr grp)255     void setShareGroup(ShareGroupPtr grp){m_shareGroup = std::move(grp);};
shareGroup()256     const ShareGroupPtr& shareGroup() const { return m_shareGroup; }
257     virtual void setActiveTexture(GLenum tex);
getActiveTextureUnit()258     unsigned int getActiveTextureUnit() const { return m_activeTexture; }
259     unsigned int getBindedTexture(GLenum target);
260     unsigned int getBindedTexture(GLenum unit,GLenum target);
261     void setBindedTexture(GLenum target,unsigned int tex);
262     bool isTextureUnitEnabled(GLenum unit);
263     void setTextureEnabled(GLenum target, GLenum enable);
264     ObjectLocalName getDefaultTextureName(GLenum target);
265     ObjectLocalName getTextureLocalName(GLenum target, unsigned int tex);
isInitialized()266     bool isInitialized() { return m_initialized; };
267     bool needRestore();
268 
269     bool  isArrEnabled(GLenum);
270     virtual void  enableArr(GLenum arr,bool enable);
271 
272     void addVertexArrayObjects(GLsizei n, GLuint* arrays);
273     void removeVertexArrayObjects(GLsizei n, const GLuint* arrays);
274     bool setVertexArrayObject(GLuint array);
275     void setVAOEverBound();
276     GLuint getVertexArrayObject() const;
277     bool vertexAttributesBufferBacked();
278     const GLvoid* setPointer(GLenum arrType,GLint size,GLenum type,GLsizei stride,const GLvoid* data, GLsizei dataSize, bool normalize = false, bool isInt = false);
279     virtual const GLESpointer* getPointer(GLenum arrType);
280     virtual void setupArraysPointers(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct, bool* needEnablingPostDraw) = 0;
281 
282     static void prepareCoreProfileEmulatedTexture(TextureData* texData, bool is3d, GLenum target,
283                                                   GLenum format, GLenum type,
284                                                   GLint* internalformat_out, GLenum* format_out);
285 
286     GLuint bindBuffer(GLenum target,GLuint buffer); // returns global name for dispatcher
287     virtual void bindIndexedBuffer(GLenum target,
288                                    GLuint index,
289                                    GLuint buffer,
290                                    GLintptr offset,
291                                    GLsizeiptr size,
292                                    GLintptr stride = 0,
293                                    bool isBindBase = false);
294     virtual void bindIndexedBuffer(GLenum target, GLuint index, GLuint buffer);
295     virtual void unbindBuffer(GLuint buffer);
296     bool isBuffer(GLuint buffer);
297     bool isBindedBuffer(GLenum target);
298     GLvoid* getBindedBuffer(GLenum target);
299     GLuint getBuffer(GLenum target);
300     virtual GLuint getIndexedBuffer(GLenum target, GLuint index);
301     void getBufferSize(GLenum target,GLint* param);
302     void getBufferSizeById(GLuint buffer,GLint* param);
303     void getBufferUsage(GLenum target,GLint* param);
304     bool setBufferData(GLenum target,GLsizeiptr size,const GLvoid* data,GLenum usage);
305     bool setBufferSubData(GLenum target,GLintptr offset,GLsizeiptr size,const GLvoid* data);
306     const char * getExtensionString(bool isGles1);
307     const char * getVendorString(bool isGles1) const;
308     const char * getRendererString(bool isGles1) const;
309     const char * getVersionString(bool isGles1) const;
310     void getGlobalLock();
311     void releaseGlobalLock();
312     virtual const GLSupport*  getCaps() const = 0;
getCapsGlobal()313     static GLSupport* getCapsGlobal(){return &s_glSupport;};
vulkanInteropSupported()314     static bool vulkanInteropSupported() {
315         return s_glSupport.ext_GL_EXT_memory_object &&
316                s_glSupport.ext_GL_EXT_semaphore;
317     }
shaderFramebufferFetchSupported()318     static bool shaderFramebufferFetchSupported() {
319         return s_glSupport.ext_GL_EXT_shader_framebuffer_fetch;
320     }
321     virtual ~GLEScontext();
322     virtual int getMaxTexUnits() = 0;
getMaxCombinedTexUnits()323     virtual int getMaxCombinedTexUnits() { return getMaxTexUnits(); }
324     virtual void drawValidate(void);
325 
326     // Default FBO emulation. Do not call this from GLEScontext context;
327     // it needs dynamic dispatch (from GLEScmContext or GLESv2Context DLLs)
328     // to pick up on the right functions.
329     virtual void initDefaultFBO(
330             GLint width, GLint height, GLint colorFormat, GLint depthstencilFormat, GLint multisamples,
331             GLuint* eglSurfaceRBColorId, GLuint* eglSurfaceRBDepthId,
332             GLuint readWidth, GLint readHeight, GLint readColorFormat, GLint readDepthStencilFormat, GLint readMultisamples,
333             GLuint* eglReadSurfaceRBColorId, GLuint* eglReadSurfaceRBDepthId);
334     void initEmulatedEGLSurface(GLint width, GLint height,
335                              GLint colorFormat, GLint depthstencilFormat, GLint multisamples,
336                              GLuint rboColor, GLuint rboDepth);
337 
getDefaultFBOGlobalName()338     GLuint getDefaultFBOGlobalName() const { return m_defaultFBO; }
isDefaultFBOBound(GLenum target)339     bool isDefaultFBOBound(GLenum target) const { return !getFramebufferBinding(target); }
hasEmulatedDefaultFBO()340     bool hasEmulatedDefaultFBO() const { return m_defaultFBO != 0; }
341 
getDefaultFBOColorFormat()342     int getDefaultFBOColorFormat() const { return m_defaultFBOColorFormat; }
getDefaultFBOWidth()343     int getDefaultFBOWidth() const { return m_defaultFBOWidth; }
getDefaultFBOHeight()344     int getDefaultFBOHeight() const { return m_defaultFBOHeight; }
getDefaultFBOMultisamples()345     int getDefaultFBOMultisamples() const { return m_defaultFBOSamples; }
346 
setRenderbufferBinding(GLuint rb)347     void setRenderbufferBinding(GLuint rb) { m_renderbuffer = rb; }
getRenderbufferBinding()348     GLuint getRenderbufferBinding() const { return m_renderbuffer; }
setFramebufferBinding(GLenum target,GLuint fb)349     void setFramebufferBinding(GLenum target, GLuint fb) {
350         switch (target) {
351         case GL_READ_FRAMEBUFFER:
352             m_readFramebuffer = fb;
353             break;
354         case GL_DRAW_FRAMEBUFFER:
355             m_drawFramebuffer = fb;
356             break;
357         case GL_FRAMEBUFFER:
358             m_readFramebuffer = fb;
359             m_drawFramebuffer = fb;
360             break;
361         default:
362             m_drawFramebuffer = fb;
363             break;
364         }
365     }
getFramebufferBinding(GLenum target)366     GLuint getFramebufferBinding(GLenum target) const {
367         switch (target) {
368         case GL_READ_FRAMEBUFFER:
369             return m_readFramebuffer;
370         case GL_DRAW_FRAMEBUFFER:
371         case GL_FRAMEBUFFER:
372             return m_drawFramebuffer;
373         }
374         return m_drawFramebuffer;
375     }
376 
377     void setEnable(GLenum item, bool isEnable);
378     void setEnablei(GLenum cap, GLuint index, bool isEnable);
379     bool isEnabled(GLenum item) const;
380     void setBlendEquationSeparate(GLenum modeRGB, GLenum modeAlpha);
381     void setBlendEquationSeparatei(GLuint buf, GLenum modeRGB, GLenum modeAlpha);
382     void setBlendFuncSeparate(GLenum srcRGB, GLenum dstRGB,
383             GLenum srcAlpha, GLenum dstAlpha);
384     void setBlendFuncSeparatei(GLenum buf, GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha);
385     void setPixelStorei(GLenum pname, GLint param);
386 
387     void setViewport(GLint x, GLint y, GLsizei width, GLsizei height);
388     void getViewport(GLint* params);
389     void setPolygonOffset(GLfloat factor, GLfloat units);
390     void setScissor(GLint x, GLint y, GLsizei width, GLsizei height);
391     void setCullFace(GLenum mode);
392     void setFrontFace(GLenum mode);
393 
394     void setDepthFunc(GLenum func);
395     void setDepthMask(GLboolean flag);
396     void setDepthRangef(GLclampf zNear, GLclampf zFar);
397     void setLineWidth(GLfloat lineWidth);
398     void setSampleCoverage(GLclampf value, GLboolean invert);
399 
400     void setStencilFuncSeparate(GLenum face, GLenum func, GLint ref,
401             GLuint mask);
402     void setStencilMaskSeparate(GLenum face, GLuint mask);
403     void setStencilOpSeparate(GLenum face, GLenum fail, GLenum zfail,
404             GLenum zpass);
405 
406     void setColorMask(GLboolean red, GLboolean green, GLboolean blue,
407             GLboolean alpha);
408 
409     void setColorMaski(GLuint buf, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
410 
411     void setClearColor(GLclampf red, GLclampf green, GLclampf blue,
412             GLclampf alpha);
413     void setClearDepth(GLclampf depth);
414     void setClearStencil(GLint s);
415 
416     // Core profile doesn't support GL_GENERATE_MIPMAP_HINT,
417     // so just emulate it here with no-ops.
setHint(GLenum target,GLenum mode)418     void setHint(GLenum target, GLenum mode) {
419         m_hints[target] = mode;
420     }
getHint(GLenum target)421     GLenum getHint(GLenum target) const {
422         return android::base::findOrDefault(m_hints, target, GL_DONT_CARE);
423     }
424 
dispatcher()425     static GLDispatch& dispatcher(){return s_glDispatch;};
426     static EGLiface* eglIface();
427     static void initEglIface(EGLiface* iface);
428 
getMaxLights()429     static int getMaxLights(){return s_glSupport.maxLights;}
getMaxClipPlanes()430     static int getMaxClipPlanes(){return s_glSupport.maxClipPlane;}
getMaxTexSize()431     static int getMaxTexSize(){return s_glSupport.maxTexSize;}
glslVersion()432     static Version glslVersion(){return s_glSupport.glslVersion;}
isAutoMipmapSupported()433     static bool isAutoMipmapSupported(){return s_glSupport.GL_SGIS_GENERATE_MIPMAP;}
434     static TextureTarget GLTextureTargetToLocal(GLenum target);
435     static unsigned int findMaxIndex(GLsizei count,GLenum type,const GLvoid* indices);
436 
437     virtual bool glGetIntegerv(GLenum pname, GLint *params);
438     virtual bool glGetBooleanv(GLenum pname, GLboolean *params);
439     virtual bool glGetFloatv(GLenum pname, GLfloat *params);
440     virtual bool glGetFixedv(GLenum pname, GLfixed *params);
441 
getMajorVersion()442     int getMajorVersion() const { return m_glesMajorVersion; }
getMinorVersion()443     int getMinorVersion() const { return m_glesMinorVersion; }
444 
445     // FBO
446     void initFBONameSpace(GlobalNameSpace* globalNameSpace,
447             android::base::Stream* stream);
448     bool isFBO(ObjectLocalName p_localName);
449     ObjectLocalName genFBOName(ObjectLocalName p_localName = 0,
450             bool genLocal = 0);
451     void setFBOData(ObjectLocalName p_localName, ObjectDataPtr data);
452     void setDefaultFBODrawBuffer(GLenum buffer);
453     void setDefaultFBOReadBuffer(GLenum buffer);
454     void deleteFBO(ObjectLocalName p_localName);
455     FramebufferData* getFBOData(ObjectLocalName p_localName) const;
456     ObjectDataPtr getFBODataPtr(ObjectLocalName p_localName) const;
457     unsigned int getFBOGlobalName(ObjectLocalName p_localName) const;
458     ObjectLocalName getFBOLocalName(unsigned int p_globalName) const;
459     int queryCurrFboBits(ObjectLocalName localFboName, GLenum pname);
460 
461     // Texture emulation
462     void copyTexImageWithEmulation(
463         TextureData* texData,
464         bool isSubImage,
465         GLenum target,
466         GLint level,
467         GLenum internalformat,
468         GLint xoffset, GLint yoffset,
469         GLint x, GLint y,
470         GLsizei width, GLsizei height,
471         GLint border);
472 
473     // Primitive restart emulation
474     void setPrimitiveRestartEnabled(bool enabled);
primitiveRestartEnabled()475     bool primitiveRestartEnabled() const {
476         return m_primitiveRestartEnabled;
477     }
478     void updatePrimitiveRestartIndex(GLenum type);
479 
480     bool isVAO(ObjectLocalName p_localName);
481     ObjectLocalName genVAOName(ObjectLocalName p_localName = 0,
482             bool genLocal = 0);
483     void deleteVAO(ObjectLocalName p_localName);
484     unsigned int getVAOGlobalName(ObjectLocalName p_localName);
485     ObjectLocalName getVAOLocalName(unsigned int p_globalName);
486 
487     // Snapshot save
488     virtual void onSave(android::base::Stream* stream) const;
489     virtual void postSave(android::base::Stream* stream) const;
490     virtual ObjectDataPtr loadObject(NamedObjectType type,
491             ObjectLocalName localName, android::base::Stream* stream) const;
492     // postLoad is triggered after setting up ShareGroup
493     virtual void postLoad();
494     virtual void restore();
495 
isCoreProfile()496     bool isCoreProfile() const { return m_coreProfile; }
setCoreProfile(bool core)497     void setCoreProfile(bool core) { m_coreProfile = core; }
498 
499     // Utility functions for emulation
500     static GLuint compileAndValidateCoreShader(GLenum shaderType, const char* src);
501     static GLuint linkAndValidateProgram(GLuint vshader, GLuint fshader);
502 
contextNeedsRestore()503     bool contextNeedsRestore() const {
504         return m_needRestoreFromSnapshot;
505     }
506 
507     void blitFromReadBufferToTextureFlipped(GLuint globalTexObj,
508                                             GLuint width, GLuint height,
509                                             GLint internalFormat, GLenum format, GLenum type);
510     void blitFromReadBufferToEGLImage(EGLImage image, GLint internalFormat, int width, int height);
511 
512 protected:
513     void initDefaultFboImpl(
514         GLint width, GLint height,
515         GLint colorFormat, GLint depthstencilFormat,
516         GLint multisamples,
517         GLuint* eglSurfaceRBColorId,
518         GLuint* eglSurfaceRBDepthId);
519 
520     virtual void postLoadRestoreShareGroup();
521     virtual void postLoadRestoreCtx();
522 
523     static void buildStrings(int major, int minor, const char* baseVendor, const char* baseRenderer, const char* baseVersion, const char* version);
524 
525     void freeVAOState();
526     virtual void addVertexArrayObject(GLuint array);
527     void removeVertexArrayObject(GLuint array);
528 
529     virtual bool needConvert(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) = 0;
530     void convertDirect(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum array_id,GLESpointer* p);
531     void convertDirectVBO(GLESConversionArrays& fArrs,GLint first,GLsizei count,GLenum array_id,GLESpointer* p);
532     void convertIndirect(GLESConversionArrays& fArrs,GLsizei count,GLenum type,const GLvoid* indices,GLenum array_id,GLESpointer* p);
533     void convertIndirectVBO(GLESConversionArrays& fArrs,GLsizei count,GLenum indices_type,const GLvoid* indices,GLenum array_id,GLESpointer* p);
534     static void initCapsLocked(const GLubyte * extensionString, bool nativeTextureDecompressionEnabled, GLSupport& glSupport);
535     virtual void initExtensionString() =0;
536 
537     bool                  m_needRestoreFromSnapshot = false;
538     static android::base::Lock   s_lock;
539     static GLDispatch     s_glDispatch;
540     bool                  m_initialized = false;
541     unsigned int          m_activeTexture = 0;
542 
543     VAOStateMap           m_vaoStateMap;
544     VAOStateRef           m_currVaoState;
545     // Buffer binding state
546     GLuint m_copyReadBuffer = 0;
547     GLuint m_copyWriteBuffer = 0;
548     GLuint m_pixelPackBuffer = 0;
549     GLuint m_pixelUnpackBuffer = 0;
550     GLuint m_transformFeedbackBuffer = 0;
551     GLuint m_uniformBuffer = 0;
552     GLuint m_atomicCounterBuffer = 0;
553     GLuint m_dispatchIndirectBuffer = 0;
554     GLuint m_drawIndirectBuffer = 0;
555     GLuint m_shaderStorageBuffer = 0;
556     GLuint m_textureBuffer = 0;
557     std::vector<BufferBinding> m_indexedTransformFeedbackBuffers;
558     std::vector<BufferBinding> m_indexedUniformBuffers;
559     std::vector<BufferBinding> m_indexedAtomicCounterBuffers;
560     std::vector<BufferBinding> m_indexedShaderStorageBuffers;
561 
562     bool m_isViewport = false;
563     GLint m_viewportX = 0;
564     GLint m_viewportY = 0;
565     GLsizei m_viewportWidth = 0;
566     GLsizei m_viewportHeight = 0;
567 
568     GLfloat m_polygonOffsetFactor = 0.0f;
569     GLfloat m_polygonOffsetUnits = 0.0f;
570 
571     bool m_isScissor = false;
572     GLint m_scissorX = 0;
573     GLint m_scissorY = 0;
574     GLsizei m_scissorWidth = 0;
575     GLsizei m_scissorHeight = 0;
576 
577     std::unordered_map<GLenum, bool> m_glEnableList = std::unordered_map<GLenum, bool>();
578 
579     std::vector<BlendState> m_blendStates;
580 
581     std::unordered_map<GLenum, GLint> m_glPixelStoreiList;
582 
583     GLenum m_cullFace = GL_BACK;
584     GLenum m_frontFace = GL_CCW;
585 
586     GLenum m_depthFunc = GL_LESS;
587     GLboolean m_depthMask = GL_TRUE;
588     GLclampf m_zNear = 0.0f;
589     GLclampf m_zFar = 1.0f;
590 
591     GLfloat m_lineWidth = 1.0f;
592 
593     GLclampf m_sampleCoverageVal = 1.0f;
594     GLboolean m_sampleCoverageInvert = GL_FALSE;
595 
596     enum {
597         StencilFront = 0,
598         StencilBack
599     };
600     struct {
601         GLenum m_func = GL_ALWAYS;
602         GLint m_ref = 0;
603         GLuint m_funcMask = -1; // all bits set to 1
604         GLuint m_writeMask = -1; // all bits set to 1
605         GLenum m_sfail = GL_KEEP;
606         GLenum m_dpfail = GL_KEEP;
607         GLenum m_dppass = GL_KEEP;
608     } m_stencilStates[2];
609 
610     GLclampf m_clearColorR = 0.0f;
611     GLclampf m_clearColorG = 0.0f;
612     GLclampf m_clearColorB = 0.0f;
613     GLclampf m_clearColorA = 0.0f;
614 
615     GLclampf m_clearDepth = 1.0f;
616     GLint m_clearStencil = 0;
617 
618     // we may run with multiple gles version contexts.
619     // for Angle based driver, es31 feature still not completed
620     // only enabled with application requires es3.1
621     // the default context version is still es3.0.
622     // this is temporary patch. we can remove this patch if Angle ES3.1 feature completed.
623     static std::string*   s_glExtensionsGles1;
624     static bool           s_glExtensionsGles1Initialized;
625     static std::string*   s_glExtensionsGles31;
626     static bool           s_glExtensionsGles31Initialized;
627     static std::string*   s_glExtensions;
628     static bool           s_glExtensionsInitialized;
629 
630     // for ES1.1
631     static GLSupport      s_glSupportGles1;
632     // Common for ES2.0+
633     static GLSupport      s_glSupport;
634     // Special for ES3.1
635     static GLSupport      s_glSupportGles31;
636 
637     int m_glesMajorVersion = 1;
638     int m_glesMinorVersion = 0;
639 
640     ShareGroupPtr         m_shareGroup;
641 
642     // Default FBO per-context state
643     GLuint m_defaultFBO = 0;
644     GLuint m_defaultReadFBO = 0;
645     GLuint m_defaultFboRBColor = 0;
646     GLuint m_defaultFboRBDepth = 0;
647     GLuint m_defaultReadFboRBColor = 0;
648     GLuint m_defaultReadFboRBDepth = 0;
649     GLint m_defaultFBOWidth = 0;
650     GLint m_defaultFBOHeight = 0;
651     GLint m_defaultFBOColorFormat = 0;
652     GLint m_defaultFBODepthFormat = 0;
653     GLint m_defaultFBOStencilFormat = 0;
654     GLint m_defaultFBOSamples = 0;
655     GLenum m_defaultFBODrawBuffer = GL_COLOR_ATTACHMENT0;
656     GLenum m_defaultFBOReadBuffer = GL_COLOR_ATTACHMENT0;
657 
658     // Texture emulation state
659     void initTexImageEmulation();
660     GLuint m_textureEmulationFBO = 0;
661     GLuint m_textureEmulationTextures[2] = {};
662     GLuint m_textureEmulationProg = 0;
663     GLuint m_textureEmulationVAO = 0;
664     GLuint m_textureEmulationVBO = 0;
665     GLuint m_textureEmulationSamplerLoc = 0;
666 
667     std::function<GLESbuffer*(GLuint)> getBufferObj
668             = [this] (GLuint bufferName) -> GLESbuffer* {
669                 return (GLESbuffer*)m_shareGroup->getObjectData(
670                     NamedObjectType::VERTEXBUFFER,
671                     (ObjectLocalName)bufferName);
672             };
673 
674     GLuint m_useProgram = 0;
675 
676     bool m_nativeTextureDecompressionEnabled = false;
677 
678 private:
679 
680     GLenum                m_glError = GL_NO_ERROR;
681     int                   m_maxTexUnits;
682     unsigned int          m_maxUsedTexUnit = 0;
683     textureUnitState*     m_texState = nullptr;
684     unsigned int          m_arrayBuffer = 0;
685     unsigned int          m_elementBuffer = 0;
686     GLuint                m_renderbuffer = 0;
687     GLuint                m_drawFramebuffer = 0;
688     GLuint                m_readFramebuffer = 0;
689 
690     static std::string    s_glVendorGles1;
691     static std::string    s_glRendererGles1;
692     static std::string    s_glVersionGles1;
693 
694     static std::string    s_glVendorGles31;
695     static std::string    s_glRendererGles31;
696     static std::string    s_glVersionGles31;
697 
698     static std::string    s_glVendor;
699     static std::string    s_glRenderer;
700     static std::string    s_glVersion;
701 
702     NameSpace* m_fboNameSpace = nullptr;
703     // m_vaoNameSpace is an empty shell that holds the names but not the data
704     // TODO(yahan): consider moving the data into it?
705     NameSpace* m_vaoNameSpace = nullptr;
706 
707     bool m_coreProfile = false;
708 
709     std::unordered_map<GLenum, GLenum> m_hints;
710 
711     bool m_primitiveRestartEnabled = false;
712 
713     struct ImageBlitState {
714         GLuint program = 0;
715         GLuint samplerLoc = 0;
716 
717         GLuint vao = 0;
718         GLuint vbo = 0;
719         GLuint ibo = 0;
720 
721         GLuint fbo = 0;
722         GLuint resolveFbo = 0;
723         GLuint tex = 0;
724 
725         uint32_t width = 0;
726         uint32_t height = 0;
727         GLint internalFormat = 0;
728         uint32_t samples = 0;
729         uint32_t prevSamples = 0;
730 
731         GLuint eglImageTex = 0;
732     };
733 
734     ImageBlitState m_blitState = {};
735     GLint getReadBufferSamples();
736     GLint getReadBufferInternalFormat();
737     void getReadBufferDimensions(GLint* width, GLint* height);
738     void setupImageBlitState();
739     bool setupImageBlitForTexture(uint32_t width, uint32_t height,
740                                   GLint internalFormat);
741 };
742 
743 std::string getHostExtensionsString(GLDispatch* dispatch);
744 
745 #endif
746 
747