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