Context.h (32953B)
1 // 2 // Copyright 2002 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // Context.h: Defines the gl::Context class, managing all GL state and performing 8 // rendering operations. It is the GLES2 specific implementation of EGLContext. 9 10 #ifndef LIBANGLE_CONTEXT_H_ 11 #define LIBANGLE_CONTEXT_H_ 12 13 #include <mutex> 14 #include <set> 15 #include <string> 16 17 #include "angle_gl.h" 18 #include "common/MemoryBuffer.h" 19 #include "common/PackedEnums.h" 20 #include "common/angleutils.h" 21 #include "libANGLE/Caps.h" 22 #include "libANGLE/Constants.h" 23 #include "libANGLE/Context_gl_1_autogen.h" 24 #include "libANGLE/Context_gl_2_autogen.h" 25 #include "libANGLE/Context_gl_3_autogen.h" 26 #include "libANGLE/Context_gl_4_autogen.h" 27 #include "libANGLE/Context_gles_1_0_autogen.h" 28 #include "libANGLE/Context_gles_2_0_autogen.h" 29 #include "libANGLE/Context_gles_3_0_autogen.h" 30 #include "libANGLE/Context_gles_3_1_autogen.h" 31 #include "libANGLE/Context_gles_3_2_autogen.h" 32 #include "libANGLE/Context_gles_ext_autogen.h" 33 #include "libANGLE/Error.h" 34 #include "libANGLE/Framebuffer.h" 35 #include "libANGLE/HandleAllocator.h" 36 #include "libANGLE/RefCountObject.h" 37 #include "libANGLE/ResourceManager.h" 38 #include "libANGLE/ResourceMap.h" 39 #include "libANGLE/State.h" 40 #include "libANGLE/VertexAttribute.h" 41 #include "libANGLE/WorkerThread.h" 42 #include "libANGLE/angletypes.h" 43 44 namespace angle 45 { 46 class FrameCapture; 47 class FrameCaptureShared; 48 struct FrontendFeatures; 49 } // namespace angle 50 51 namespace rx 52 { 53 class ContextImpl; 54 class EGLImplFactory; 55 } // namespace rx 56 57 namespace egl 58 { 59 class AttributeMap; 60 class Surface; 61 struct Config; 62 class Thread; 63 } // namespace egl 64 65 namespace gl 66 { 67 class Buffer; 68 class Compiler; 69 class FenceNV; 70 class GLES1Renderer; 71 class MemoryProgramCache; 72 class MemoryShaderCache; 73 class MemoryObject; 74 class Program; 75 class ProgramPipeline; 76 class Query; 77 class Renderbuffer; 78 class Sampler; 79 class Semaphore; 80 class Shader; 81 class Sync; 82 class Texture; 83 class TransformFeedback; 84 class VertexArray; 85 struct VertexAttribute; 86 87 class ErrorSet : angle::NonCopyable 88 { 89 public: 90 explicit ErrorSet(Context *context); 91 ~ErrorSet(); 92 93 bool empty() const; 94 GLenum popError(); 95 96 void handleError(GLenum errorCode, 97 const char *message, 98 const char *file, 99 const char *function, 100 unsigned int line); 101 102 void validationError(angle::EntryPoint entryPoint, GLenum errorCode, const char *message); 103 104 private: 105 Context *mContext; 106 std::set<GLenum> mErrors; 107 }; 108 109 enum class VertexAttribTypeCase 110 { 111 Invalid = 0, 112 Valid = 1, 113 ValidSize4Only = 2, 114 ValidSize3or4 = 3, 115 }; 116 117 // Helper class for managing cache variables and state changes. 118 class StateCache final : angle::NonCopyable 119 { 120 public: 121 StateCache(); 122 ~StateCache(); 123 124 void initialize(Context *context); 125 126 // Places that can trigger updateActiveAttribsMask: 127 // 1. onVertexArrayBindingChange. 128 // 2. onProgramExecutableChange. 129 // 3. onVertexArrayStateChange. 130 // 4. onGLES1ClientStateChange. 131 AttributesMask getActiveBufferedAttribsMask() const { return mCachedActiveBufferedAttribsMask; } 132 AttributesMask getActiveClientAttribsMask() const { return mCachedActiveClientAttribsMask; } 133 AttributesMask getActiveDefaultAttribsMask() const { return mCachedActiveDefaultAttribsMask; } 134 bool hasAnyEnabledClientAttrib() const { return mCachedHasAnyEnabledClientAttrib; } 135 bool hasAnyActiveClientAttrib() const { return mCachedActiveClientAttribsMask.any(); } 136 137 // Places that can trigger updateVertexElementLimits: 138 // 1. onVertexArrayBindingChange. 139 // 2. onProgramExecutableChange. 140 // 3. onVertexArrayFormatChange. 141 // 4. onVertexArrayBufferChange. 142 // 5. onVertexArrayStateChange. 143 GLint64 getNonInstancedVertexElementLimit() const 144 { 145 return mCachedNonInstancedVertexElementLimit; 146 } 147 GLint64 getInstancedVertexElementLimit() const { return mCachedInstancedVertexElementLimit; } 148 149 // Places that can trigger updateBasicDrawStatesError: 150 // 1. onVertexArrayBindingChange. 151 // 2. onProgramExecutableChange. 152 // 3. onVertexArrayBufferContentsChange. 153 // 4. onVertexArrayStateChange. 154 // 5. onVertexArrayBufferStateChange. 155 // 6. onDrawFramebufferChange. 156 // 7. onContextCapChange. 157 // 8. onStencilStateChange. 158 // 9. onDefaultVertexAttributeChange. 159 // 10. onActiveTextureChange. 160 // 11. onQueryChange. 161 // 12. onActiveTransformFeedbackChange. 162 // 13. onUniformBufferStateChange. 163 // 14. onColorMaskChange. 164 // 15. onBufferBindingChange. 165 // 16. onBlendFuncIndexedChange. 166 bool hasBasicDrawStatesError(Context *context) const 167 { 168 if (mCachedBasicDrawStatesError == 0) 169 { 170 return false; 171 } 172 if (mCachedBasicDrawStatesError != kInvalidPointer) 173 { 174 return true; 175 } 176 return getBasicDrawStatesErrorImpl(context) != 0; 177 } 178 179 intptr_t getBasicDrawStatesError(const Context *context) const 180 { 181 if (mCachedBasicDrawStatesError != kInvalidPointer) 182 { 183 return mCachedBasicDrawStatesError; 184 } 185 186 return getBasicDrawStatesErrorImpl(context); 187 } 188 189 // Places that can trigger updateProgramPipelineError: 190 // 1. onProgramExecutableChange. 191 intptr_t getProgramPipelineError(const Context *context) const 192 { 193 if (mCachedProgramPipelineError != kInvalidPointer) 194 { 195 return mCachedProgramPipelineError; 196 } 197 198 return getProgramPipelineErrorImpl(context); 199 } 200 201 // Places that can trigger updateBasicDrawElementsError: 202 // 1. onActiveTransformFeedbackChange. 203 // 2. onVertexArrayBufferStateChange. 204 // 3. onBufferBindingChange. 205 // 4. onVertexArrayStateChange. 206 // 5. onVertexArrayBindingStateChange. 207 intptr_t getBasicDrawElementsError(const Context *context) const 208 { 209 if (mCachedBasicDrawElementsError != kInvalidPointer) 210 { 211 return mCachedBasicDrawElementsError; 212 } 213 214 return getBasicDrawElementsErrorImpl(context); 215 } 216 217 // Places that can trigger updateValidDrawModes: 218 // 1. onProgramExecutableChange. 219 // 2. onActiveTransformFeedbackChange. 220 bool isValidDrawMode(PrimitiveMode primitiveMode) const 221 { 222 return mCachedValidDrawModes[primitiveMode]; 223 } 224 225 // Cannot change except on Context/Extension init. 226 bool isValidBindTextureType(TextureType type) const 227 { 228 return mCachedValidBindTextureTypes[type]; 229 } 230 231 // Cannot change except on Context/Extension init. 232 bool isValidDrawElementsType(DrawElementsType type) const 233 { 234 return mCachedValidDrawElementsTypes[type]; 235 } 236 237 // Places that can trigger updateTransformFeedbackActiveUnpaused: 238 // 1. onActiveTransformFeedbackChange. 239 bool isTransformFeedbackActiveUnpaused() const 240 { 241 return mCachedTransformFeedbackActiveUnpaused; 242 } 243 244 // Cannot change except on Context/Extension init. 245 VertexAttribTypeCase getVertexAttribTypeValidation(VertexAttribType type) const 246 { 247 return mCachedVertexAttribTypesValidation[type]; 248 } 249 250 VertexAttribTypeCase getIntegerVertexAttribTypeValidation(VertexAttribType type) const 251 { 252 return mCachedIntegerVertexAttribTypesValidation[type]; 253 } 254 255 // Places that can trigger updateActiveShaderStorageBufferIndices: 256 // 1. onProgramExecutableChange. 257 StorageBuffersMask getActiveShaderStorageBufferIndices() const 258 { 259 return mCachedActiveShaderStorageBufferIndices; 260 } 261 262 // Places that can trigger updateActiveImageUnitIndices: 263 // 1. onProgramExecutableChange. 264 const ImageUnitMask &getActiveImageUnitIndices() const { return mCachedActiveImageUnitIndices; } 265 266 // Places that can trigger updateCanDraw: 267 // 1. onProgramExecutableChange. 268 bool getCanDraw() const { return mCachedCanDraw; } 269 270 // State change notifications. 271 void onVertexArrayBindingChange(Context *context); 272 void onProgramExecutableChange(Context *context); 273 void onVertexArrayFormatChange(Context *context); 274 void onVertexArrayBufferContentsChange(Context *context); 275 void onVertexArrayStateChange(Context *context); 276 void onVertexArrayBufferStateChange(Context *context); 277 void onGLES1ClientStateChange(Context *context); 278 void onDrawFramebufferChange(Context *context); 279 void onContextCapChange(Context *context); 280 void onStencilStateChange(Context *context); 281 void onDefaultVertexAttributeChange(Context *context); 282 void onActiveTextureChange(Context *context); 283 void onQueryChange(Context *context); 284 void onActiveTransformFeedbackChange(Context *context); 285 void onUniformBufferStateChange(Context *context); 286 void onAtomicCounterBufferStateChange(Context *context); 287 void onShaderStorageBufferStateChange(Context *context); 288 void onColorMaskChange(Context *context); 289 void onBufferBindingChange(Context *context); 290 void onBlendFuncIndexedChange(Context *context); 291 void onBlendEquationChange(Context *context); 292 293 private: 294 // Cache update functions. 295 void updateActiveAttribsMask(Context *context); 296 void updateVertexElementLimits(Context *context); 297 void updateVertexElementLimitsImpl(Context *context); 298 void updateValidDrawModes(Context *context); 299 void updateValidBindTextureTypes(Context *context); 300 void updateValidDrawElementsTypes(Context *context); 301 void updateBasicDrawStatesError(); 302 void updateProgramPipelineError(); 303 void updateBasicDrawElementsError(); 304 void updateTransformFeedbackActiveUnpaused(Context *context); 305 void updateVertexAttribTypesValidation(Context *context); 306 void updateActiveShaderStorageBufferIndices(Context *context); 307 void updateActiveImageUnitIndices(Context *context); 308 void updateCanDraw(Context *context); 309 310 void setValidDrawModes(bool pointsOK, 311 bool linesOK, 312 bool trisOK, 313 bool lineAdjOK, 314 bool triAdjOK, 315 bool patchOK); 316 317 intptr_t getBasicDrawStatesErrorImpl(const Context *context) const; 318 intptr_t getProgramPipelineErrorImpl(const Context *context) const; 319 intptr_t getBasicDrawElementsErrorImpl(const Context *context) const; 320 321 static constexpr intptr_t kInvalidPointer = 1; 322 323 AttributesMask mCachedActiveBufferedAttribsMask; 324 AttributesMask mCachedActiveClientAttribsMask; 325 AttributesMask mCachedActiveDefaultAttribsMask; 326 bool mCachedHasAnyEnabledClientAttrib; 327 GLint64 mCachedNonInstancedVertexElementLimit; 328 GLint64 mCachedInstancedVertexElementLimit; 329 mutable intptr_t mCachedBasicDrawStatesError; 330 mutable intptr_t mCachedBasicDrawElementsError; 331 // mCachedProgramPipelineError checks only the 332 // current-program-exists subset of mCachedBasicDrawStatesError. 333 // Therefore, mCachedProgramPipelineError follows 334 // mCachedBasicDrawStatesError in that if mCachedBasicDrawStatesError is 335 // no-error, so is mCachedProgramPipelineError. Otherwise, if 336 // mCachedBasicDrawStatesError is in error, the state of 337 // mCachedProgramPipelineError can be no-error or also in error, or 338 // unknown due to early exiting. 339 mutable intptr_t mCachedProgramPipelineError; 340 bool mCachedTransformFeedbackActiveUnpaused; 341 StorageBuffersMask mCachedActiveShaderStorageBufferIndices; 342 ImageUnitMask mCachedActiveImageUnitIndices; 343 344 // Reserve an extra slot at the end of these maps for invalid enum. 345 angle::PackedEnumMap<PrimitiveMode, bool, angle::EnumSize<PrimitiveMode>() + 1> 346 mCachedValidDrawModes; 347 angle::PackedEnumMap<TextureType, bool, angle::EnumSize<TextureType>() + 1> 348 mCachedValidBindTextureTypes; 349 angle::PackedEnumMap<DrawElementsType, bool, angle::EnumSize<DrawElementsType>() + 1> 350 mCachedValidDrawElementsTypes; 351 angle::PackedEnumMap<VertexAttribType, 352 VertexAttribTypeCase, 353 angle::EnumSize<VertexAttribType>() + 1> 354 mCachedVertexAttribTypesValidation; 355 angle::PackedEnumMap<VertexAttribType, 356 VertexAttribTypeCase, 357 angle::EnumSize<VertexAttribType>() + 1> 358 mCachedIntegerVertexAttribTypesValidation; 359 360 bool mCachedCanDraw; 361 }; 362 363 using VertexArrayMap = ResourceMap<VertexArray, VertexArrayID>; 364 using QueryMap = ResourceMap<Query, QueryID>; 365 using TransformFeedbackMap = ResourceMap<TransformFeedback, TransformFeedbackID>; 366 367 class Context final : public egl::LabeledObject, angle::NonCopyable, public angle::ObserverInterface 368 { 369 public: 370 Context(egl::Display *display, 371 const egl::Config *config, 372 const Context *shareContext, 373 TextureManager *shareTextures, 374 SemaphoreManager *shareSemaphores, 375 MemoryProgramCache *memoryProgramCache, 376 MemoryShaderCache *memoryShaderCache, 377 const EGLenum clientType, 378 const egl::AttributeMap &attribs, 379 const egl::DisplayExtensions &displayExtensions, 380 const egl::ClientExtensions &clientExtensions); 381 382 // Use for debugging. 383 ContextID id() const { return mState.getContextID(); } 384 385 egl::Error initialize(); 386 387 egl::Error onDestroy(const egl::Display *display); 388 ~Context() override; 389 390 void setLabel(EGLLabelKHR label) override; 391 EGLLabelKHR getLabel() const override; 392 393 egl::Error makeCurrent(egl::Display *display, 394 egl::Surface *drawSurface, 395 egl::Surface *readSurface); 396 egl::Error unMakeCurrent(const egl::Display *display); 397 398 // These create and destroy methods pass through to ResourceManager, which owns these objects. 399 BufferID createBuffer(); 400 TextureID createTexture(); 401 RenderbufferID createRenderbuffer(); 402 ProgramPipelineID createProgramPipeline(); 403 MemoryObjectID createMemoryObject(); 404 SemaphoreID createSemaphore(); 405 406 void deleteBuffer(BufferID buffer); 407 void deleteTexture(TextureID texture); 408 void deleteRenderbuffer(RenderbufferID renderbuffer); 409 void deleteProgramPipeline(ProgramPipelineID pipeline); 410 void deleteMemoryObject(MemoryObjectID memoryObject); 411 void deleteSemaphore(SemaphoreID semaphore); 412 413 void bindReadFramebuffer(FramebufferID framebufferHandle); 414 void bindDrawFramebuffer(FramebufferID framebufferHandle); 415 416 Buffer *getBuffer(BufferID handle) const; 417 FenceNV *getFenceNV(FenceNVID handle) const; 418 Sync *getSync(GLsync handle) const; 419 ANGLE_INLINE Texture *getTexture(TextureID handle) const 420 { 421 return mState.mTextureManager->getTexture(handle); 422 } 423 424 Framebuffer *getFramebuffer(FramebufferID handle) const; 425 Renderbuffer *getRenderbuffer(RenderbufferID handle) const; 426 VertexArray *getVertexArray(VertexArrayID handle) const; 427 Sampler *getSampler(SamplerID handle) const; 428 Query *getOrCreateQuery(QueryID handle, QueryType type); 429 Query *getQuery(QueryID handle) const; 430 TransformFeedback *getTransformFeedback(TransformFeedbackID handle) const; 431 ProgramPipeline *getProgramPipeline(ProgramPipelineID handle) const; 432 MemoryObject *getMemoryObject(MemoryObjectID handle) const; 433 Semaphore *getSemaphore(SemaphoreID handle) const; 434 435 Texture *getTextureByType(TextureType type) const; 436 Texture *getTextureByTarget(TextureTarget target) const; 437 Texture *getSamplerTexture(unsigned int sampler, TextureType type) const; 438 439 Compiler *getCompiler() const; 440 441 bool isVertexArrayGenerated(VertexArrayID vertexArray) const; 442 bool isTransformFeedbackGenerated(TransformFeedbackID transformFeedback) const; 443 444 bool isExternal() const { return mIsExternal; } 445 bool saveAndRestoreState() const { return mSaveAndRestoreState; } 446 447 void getBooleanvImpl(GLenum pname, GLboolean *params) const; 448 void getFloatvImpl(GLenum pname, GLfloat *params) const; 449 void getIntegervImpl(GLenum pname, GLint *params) const; 450 void getInteger64vImpl(GLenum pname, GLint64 *params) const; 451 void getIntegerVertexAttribImpl(GLenum pname, GLenum attribpname, GLint *params) const; 452 void getVertexAttribivImpl(GLuint index, GLenum pname, GLint *params) const; 453 454 // Framebuffers are owned by the Context, so these methods do not pass through 455 FramebufferID createFramebuffer(); 456 void deleteFramebuffer(FramebufferID framebuffer); 457 458 bool hasActiveTransformFeedback(ShaderProgramID program) const; 459 460 // Desktop GL entry point interface 461 ANGLE_GL_1_CONTEXT_API 462 ANGLE_GL_2_CONTEXT_API 463 ANGLE_GL_3_CONTEXT_API 464 ANGLE_GL_4_CONTEXT_API 465 466 // GLES entry point interface 467 ANGLE_GLES_1_0_CONTEXT_API 468 ANGLE_GLES_2_0_CONTEXT_API 469 ANGLE_GLES_3_0_CONTEXT_API 470 ANGLE_GLES_3_1_CONTEXT_API 471 ANGLE_GLES_3_2_CONTEXT_API 472 ANGLE_GLES_EXT_CONTEXT_API 473 474 angle::Result handleNoopDrawEvent(); 475 476 // Consumes an error. 477 void handleError(GLenum errorCode, 478 const char *message, 479 const char *file, 480 const char *function, 481 unsigned int line); 482 483 void validationError(angle::EntryPoint entryPoint, GLenum errorCode, const char *message) const; 484 ANGLE_FORMAT_PRINTF(4, 5) 485 void validationErrorF(angle::EntryPoint entryPoint, 486 GLenum errorCode, 487 const char *format, 488 ...) const; 489 490 void markContextLost(GraphicsResetStatus status); 491 492 bool isContextLost() const { return mContextLost; } 493 void setContextLost(); 494 495 GLenum getGraphicsResetStrategy() const { return mResetStrategy; } 496 bool isResetNotificationEnabled() const; 497 498 bool isRobustnessEnabled() const; 499 500 const egl::Config *getConfig() const; 501 EGLenum getClientType() const; 502 EGLenum getRenderBuffer() const; 503 EGLenum getContextPriority() const; 504 505 const GLubyte *getString(GLenum name) const; 506 const GLubyte *getStringi(GLenum name, GLuint index) const; 507 508 size_t getExtensionStringCount() const; 509 510 bool isExtensionRequestable(const char *name) const; 511 bool isExtensionDisablable(const char *name) const; 512 size_t getRequestableExtensionStringCount() const; 513 void setExtensionEnabled(const char *name, bool enabled); 514 void reinitializeAfterExtensionsChanged(); 515 516 rx::ContextImpl *getImplementation() const { return mImplementation.get(); } 517 518 [[nodiscard]] bool getScratchBuffer(size_t requestedSizeBytes, 519 angle::MemoryBuffer **scratchBufferOut) const; 520 [[nodiscard]] bool getZeroFilledBuffer(size_t requstedSizeBytes, 521 angle::MemoryBuffer **zeroBufferOut) const; 522 angle::ScratchBuffer *getScratchBuffer() const; 523 524 angle::Result prepareForCopyImage(); 525 angle::Result prepareForDispatch(); 526 angle::Result prepareForInvalidate(GLenum target); 527 528 MemoryProgramCache *getMemoryProgramCache() const { return mMemoryProgramCache; } 529 MemoryShaderCache *getMemoryShaderCache() const { return mMemoryShaderCache; } 530 531 std::mutex &getProgramCacheMutex() const; 532 533 bool hasBeenCurrent() const { return mHasBeenCurrent; } 534 egl::Display *getDisplay() const { return mDisplay; } 535 egl::Surface *getCurrentDrawSurface() const { return mCurrentDrawSurface; } 536 egl::Surface *getCurrentReadSurface() const { return mCurrentReadSurface; } 537 538 bool isRobustResourceInitEnabled() const { return mState.isRobustResourceInitEnabled(); } 539 540 bool isCurrentTransformFeedback(const TransformFeedback *tf) const; 541 542 bool isCurrentVertexArray(const VertexArray *va) const 543 { 544 return mState.isCurrentVertexArray(va); 545 } 546 547 ANGLE_INLINE bool isShared() const { return mShared; } 548 // Once a context is setShared() it cannot be undone 549 void setShared() { mShared = true; } 550 551 const State &getState() const { return mState; } 552 GLint getClientMajorVersion() const { return mState.getClientMajorVersion(); } 553 GLint getClientMinorVersion() const { return mState.getClientMinorVersion(); } 554 const Version &getClientVersion() const { return mState.getClientVersion(); } 555 const Caps &getCaps() const { return mState.getCaps(); } 556 const TextureCapsMap &getTextureCaps() const { return mState.getTextureCaps(); } 557 const Extensions &getExtensions() const { return mState.getExtensions(); } 558 const Limitations &getLimitations() const { return mState.getLimitations(); } 559 bool isGLES1() const; 560 561 bool skipValidation() const 562 { 563 // Ensure we don't skip validation when context becomes lost, since implementations 564 // generally assume a non-lost context, non-null objects, etc. 565 ASSERT(!isContextLost() || !mSkipValidation); 566 return mSkipValidation; 567 } 568 569 // Specific methods needed for validation. 570 bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const; 571 bool getIndexedQueryParameterInfo(GLenum target, GLenum *type, unsigned int *numParams) const; 572 573 ANGLE_INLINE Program *getProgramResolveLink(ShaderProgramID handle) const 574 { 575 Program *program = mState.mShaderProgramManager->getProgram(handle); 576 if (program) 577 { 578 program->resolveLink(this); 579 } 580 return program; 581 } 582 583 Program *getProgramNoResolveLink(ShaderProgramID handle) const; 584 Shader *getShader(ShaderProgramID handle) const; 585 586 ANGLE_INLINE bool isTextureGenerated(TextureID texture) const 587 { 588 return mState.mTextureManager->isHandleGenerated(texture); 589 } 590 591 ANGLE_INLINE bool isBufferGenerated(BufferID buffer) const 592 { 593 return mState.mBufferManager->isHandleGenerated(buffer); 594 } 595 596 bool isRenderbufferGenerated(RenderbufferID renderbuffer) const; 597 bool isFramebufferGenerated(FramebufferID framebuffer) const; 598 bool isProgramPipelineGenerated(ProgramPipelineID pipeline) const; 599 bool isQueryGenerated(QueryID query) const; 600 601 bool usingDisplayTextureShareGroup() const; 602 bool usingDisplaySemaphoreShareGroup() const; 603 604 // Hack for the special WebGL 1 "DEPTH_STENCIL" internal format. 605 GLenum getConvertedRenderbufferFormat(GLenum internalformat) const; 606 607 bool isWebGL() const { return mState.isWebGL(); } 608 bool isWebGL1() const { return mState.isWebGL1(); } 609 610 bool isValidBufferBinding(BufferBinding binding) const { return mValidBufferBindings[binding]; } 611 612 // GLES1 emulation: Renderer level (for validation) 613 int vertexArrayIndex(ClientVertexArrayType type) const; 614 static int TexCoordArrayIndex(unsigned int unit); 615 616 // GL_KHR_parallel_shader_compile 617 std::shared_ptr<angle::WorkerThreadPool> getShaderCompileThreadPool() const 618 { 619 if (mState.mExtensions.parallelShaderCompileKHR) 620 { 621 return mMultiThreadPool; 622 } 623 return mSingleThreadPool; 624 } 625 626 // Generic multithread pool. 627 std::shared_ptr<angle::WorkerThreadPool> getWorkerThreadPool() const 628 { 629 return mMultiThreadPool; 630 } 631 632 const StateCache &getStateCache() const { return mStateCache; } 633 StateCache &getStateCache() { return mStateCache; } 634 635 void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override; 636 637 void onSamplerUniformChange(size_t textureUnitIndex); 638 639 bool isBufferAccessValidationEnabled() const { return mBufferAccessValidationEnabled; } 640 641 const angle::FrontendFeatures &getFrontendFeatures() const; 642 643 angle::FrameCapture *getFrameCapture() const { return mFrameCapture.get(); } 644 645 const VertexArrayMap &getVertexArraysForCapture() const { return mVertexArrayMap; } 646 const QueryMap &getQueriesForCapture() const { return mQueryMap; } 647 const TransformFeedbackMap &getTransformFeedbacksForCapture() const 648 { 649 return mTransformFeedbackMap; 650 } 651 652 void onPreSwap() const; 653 654 Program *getActiveLinkedProgram() const; 655 656 // EGL_ANGLE_power_preference implementation. 657 egl::Error releaseHighPowerGPU(); 658 egl::Error reacquireHighPowerGPU(); 659 void onGPUSwitch(); 660 661 bool noopDraw(PrimitiveMode mode, GLsizei count) const; 662 bool noopDrawInstanced(PrimitiveMode mode, GLsizei count, GLsizei instanceCount) const; 663 664 bool isClearBufferMaskedOut(GLenum buffer, GLint drawbuffer) const; 665 bool noopClearBuffer(GLenum buffer, GLint drawbuffer) const; 666 667 void addRef() const { mRefCount++; } 668 void release() const { mRefCount--; } 669 size_t getRefCount() const { return mRefCount; } 670 671 egl::ShareGroup *getShareGroup() const { return mState.getShareGroup(); } 672 673 bool supportsGeometryOrTesselation() const; 674 void dirtyAllState(); 675 676 bool isDestroyed() const { return mIsDestroyed; } 677 void setIsDestroyed() { mIsDestroyed = true; } 678 679 void setLogicOpEnabled(bool enabled) { mState.setLogicOpEnabled(enabled); } 680 void setLogicOp(LogicalOperation opcode) { mState.setLogicOp(opcode); } 681 682 // Needed by capture serialization logic that works with a "const" Context pointer. 683 void finishImmutable() const; 684 685 const angle::PerfMonitorCounterGroups &getPerfMonitorCounterGroups() const; 686 687 private: 688 void initializeDefaultResources(); 689 690 angle::Result prepareForDraw(PrimitiveMode mode); 691 angle::Result prepareForClear(GLbitfield mask); 692 angle::Result prepareForClearBuffer(GLenum buffer, GLint drawbuffer); 693 angle::Result syncState(const State::DirtyBits &bitMask, 694 const State::DirtyObjects &objectMask, 695 Command command); 696 angle::Result syncDirtyBits(Command command); 697 angle::Result syncDirtyBits(const State::DirtyBits &bitMask, Command command); 698 angle::Result syncDirtyObjects(const State::DirtyObjects &objectMask, Command command); 699 angle::Result syncStateForReadPixels(); 700 angle::Result syncStateForTexImage(); 701 angle::Result syncStateForBlit(GLbitfield mask); 702 angle::Result syncStateForClear(); 703 angle::Result syncTextureForCopy(Texture *texture); 704 705 VertexArray *checkVertexArrayAllocation(VertexArrayID vertexArrayHandle); 706 TransformFeedback *checkTransformFeedbackAllocation(TransformFeedbackID transformFeedback); 707 708 angle::Result onProgramLink(Program *programObject); 709 710 void detachBuffer(Buffer *buffer); 711 void detachTexture(TextureID texture); 712 void detachFramebuffer(FramebufferID framebuffer); 713 void detachRenderbuffer(RenderbufferID renderbuffer); 714 void detachVertexArray(VertexArrayID vertexArray); 715 void detachTransformFeedback(TransformFeedbackID transformFeedback); 716 void detachSampler(SamplerID sampler); 717 void detachProgramPipeline(ProgramPipelineID pipeline); 718 719 egl::Error setDefaultFramebuffer(egl::Surface *drawSurface, egl::Surface *readSurface); 720 egl::Error unsetDefaultFramebuffer(); 721 722 void initRendererString(); 723 void initVersionStrings(); 724 void initExtensionStrings(); 725 726 Extensions generateSupportedExtensions() const; 727 void initCaps(); 728 void updateCaps(); 729 730 gl::LabeledObject *getLabeledObject(GLenum identifier, GLuint name) const; 731 gl::LabeledObject *getLabeledObjectFromPtr(const void *ptr) const; 732 733 void setUniform1iImpl(Program *program, 734 UniformLocation location, 735 GLsizei count, 736 const GLint *v); 737 void renderbufferStorageMultisampleImpl(GLenum target, 738 GLsizei samples, 739 GLenum internalformat, 740 GLsizei width, 741 GLsizei height, 742 MultisamplingMode mode); 743 744 State mState; 745 bool mShared; 746 bool mSkipValidation; 747 bool mDisplayTextureShareGroup; 748 bool mDisplaySemaphoreShareGroup; 749 750 // Recorded errors 751 ErrorSet mErrors; 752 753 // Stores for each buffer binding type whether is it allowed to be used in this context. 754 angle::PackedEnumBitSet<BufferBinding> mValidBufferBindings; 755 756 std::unique_ptr<rx::ContextImpl> mImplementation; 757 758 EGLLabelKHR mLabel; 759 760 // Extensions supported by the implementation plus extensions that are implemented entirely 761 // within the frontend. 762 Extensions mSupportedExtensions; 763 764 // Shader compiler. Lazily initialized hence the mutable value. 765 mutable BindingPointer<Compiler> mCompiler; 766 767 const egl::Config *mConfig; 768 769 TextureMap mZeroTextures; 770 771 ResourceMap<FenceNV, FenceNVID> mFenceNVMap; 772 HandleAllocator mFenceNVHandleAllocator; 773 774 QueryMap mQueryMap; 775 HandleAllocator mQueryHandleAllocator; 776 777 VertexArrayMap mVertexArrayMap; 778 HandleAllocator mVertexArrayHandleAllocator; 779 780 TransformFeedbackMap mTransformFeedbackMap; 781 HandleAllocator mTransformFeedbackHandleAllocator; 782 783 const char *mVersionString; 784 const char *mShadingLanguageString; 785 const char *mRendererString; 786 const char *mExtensionString; 787 std::vector<const char *> mExtensionStrings; 788 const char *mRequestableExtensionString; 789 std::vector<const char *> mRequestableExtensionStrings; 790 791 // GLES1 renderer state 792 std::unique_ptr<GLES1Renderer> mGLES1Renderer; 793 794 // Current/lost context flags 795 bool mHasBeenCurrent; 796 bool mContextLost; // Set with setContextLost so that we also set mSkipValidation=false. 797 GraphicsResetStatus mResetStatus; 798 bool mContextLostForced; 799 GLenum mResetStrategy; 800 const bool mSurfacelessSupported; 801 egl::Surface *mCurrentDrawSurface; 802 egl::Surface *mCurrentReadSurface; 803 egl::Display *mDisplay; 804 const bool mWebGLContext; 805 bool mBufferAccessValidationEnabled; 806 const bool mExtensionsEnabled; 807 MemoryProgramCache *mMemoryProgramCache; 808 MemoryShaderCache *mMemoryShaderCache; 809 810 State::DirtyObjects mDrawDirtyObjects; 811 812 StateCache mStateCache; 813 814 State::DirtyBits mAllDirtyBits; 815 State::DirtyBits mTexImageDirtyBits; 816 State::DirtyObjects mTexImageDirtyObjects; 817 State::DirtyBits mReadPixelsDirtyBits; 818 State::DirtyObjects mReadPixelsDirtyObjects; 819 State::DirtyBits mClearDirtyBits; 820 State::DirtyObjects mClearDirtyObjects; 821 State::DirtyBits mBlitDirtyBits; 822 State::DirtyObjects mBlitDirtyObjects; 823 State::DirtyBits mComputeDirtyBits; 824 State::DirtyObjects mComputeDirtyObjects; 825 State::DirtyBits mCopyImageDirtyBits; 826 State::DirtyObjects mCopyImageDirtyObjects; 827 State::DirtyBits mReadInvalidateDirtyBits; 828 State::DirtyBits mDrawInvalidateDirtyBits; 829 830 // Binding to container objects that use dependent state updates. 831 angle::ObserverBinding mVertexArrayObserverBinding; 832 angle::ObserverBinding mDrawFramebufferObserverBinding; 833 angle::ObserverBinding mReadFramebufferObserverBinding; 834 angle::ObserverBinding mProgramPipelineObserverBinding; 835 std::vector<angle::ObserverBinding> mUniformBufferObserverBindings; 836 std::vector<angle::ObserverBinding> mAtomicCounterBufferObserverBindings; 837 std::vector<angle::ObserverBinding> mShaderStorageBufferObserverBindings; 838 std::vector<angle::ObserverBinding> mSamplerObserverBindings; 839 std::vector<angle::ObserverBinding> mImageObserverBindings; 840 841 // Not really a property of context state. The size and contexts change per-api-call. 842 mutable Optional<angle::ScratchBuffer> mScratchBuffer; 843 mutable Optional<angle::ScratchBuffer> mZeroFilledBuffer; 844 845 // Single-threaded pool may not always be initialized. It currently depends on the extension 846 // GL_KHR_parallel_shader_compile being disabled. 847 std::shared_ptr<angle::WorkerThreadPool> mSingleThreadPool; 848 // Multithreaded pool will always be initialized so it can be used for more generic work. 849 std::shared_ptr<angle::WorkerThreadPool> mMultiThreadPool; 850 851 // Note: we use a raw pointer here so we can exclude frame capture sources from the build. 852 std::unique_ptr<angle::FrameCapture> mFrameCapture; 853 854 // Cache representation of the serialized context string. 855 mutable std::string mCachedSerializedStateString; 856 857 mutable size_t mRefCount; 858 859 OverlayType mOverlay; 860 861 const bool mIsExternal; 862 const bool mSaveAndRestoreState; 863 864 bool mIsDestroyed; 865 866 std::unique_ptr<Framebuffer> mDefaultFramebuffer; 867 }; 868 869 class [[nodiscard]] ScopedContextRef 870 { 871 public: 872 ScopedContextRef(Context *context) : mContext(context) 873 { 874 if (mContext) 875 { 876 mContext->addRef(); 877 } 878 } 879 ~ScopedContextRef() 880 { 881 if (mContext) 882 { 883 mContext->release(); 884 } 885 } 886 887 private: 888 Context *const mContext; 889 }; 890 891 // Thread-local current valid context bound to the thread. 892 #if defined(ANGLE_PLATFORM_APPLE) 893 extern Context *GetCurrentValidContextTLS(); 894 extern void SetCurrentValidContextTLS(Context *context); 895 #else 896 extern thread_local Context *gCurrentValidContext; 897 #endif 898 899 } // namespace gl 900 901 #endif // LIBANGLE_CONTEXT_H_