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