• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <set>
14 #include <string>
15 
16 #include "angle_gl.h"
17 #include "common/MemoryBuffer.h"
18 #include "common/PackedEnums.h"
19 #include "common/angleutils.h"
20 #include "libANGLE/Caps.h"
21 #include "libANGLE/Constants.h"
22 #include "libANGLE/Context_gl_1_0_autogen.h"
23 #include "libANGLE/Context_gl_1_1_autogen.h"
24 #include "libANGLE/Context_gl_1_2_autogen.h"
25 #include "libANGLE/Context_gl_1_3_autogen.h"
26 #include "libANGLE/Context_gl_1_4_autogen.h"
27 #include "libANGLE/Context_gl_1_5_autogen.h"
28 #include "libANGLE/Context_gl_2_0_autogen.h"
29 #include "libANGLE/Context_gl_2_1_autogen.h"
30 #include "libANGLE/Context_gl_3_0_autogen.h"
31 #include "libANGLE/Context_gl_3_1_autogen.h"
32 #include "libANGLE/Context_gl_3_2_autogen.h"
33 #include "libANGLE/Context_gl_3_3_autogen.h"
34 #include "libANGLE/Context_gl_4_0_autogen.h"
35 #include "libANGLE/Context_gl_4_1_autogen.h"
36 #include "libANGLE/Context_gl_4_2_autogen.h"
37 #include "libANGLE/Context_gl_4_3_autogen.h"
38 #include "libANGLE/Context_gl_4_4_autogen.h"
39 #include "libANGLE/Context_gl_4_5_autogen.h"
40 #include "libANGLE/Context_gl_4_6_autogen.h"
41 #include "libANGLE/Context_gles_1_0_autogen.h"
42 #include "libANGLE/Context_gles_2_0_autogen.h"
43 #include "libANGLE/Context_gles_3_0_autogen.h"
44 #include "libANGLE/Context_gles_3_1_autogen.h"
45 #include "libANGLE/Context_gles_3_2_autogen.h"
46 #include "libANGLE/Context_gles_ext_autogen.h"
47 #include "libANGLE/Error.h"
48 #include "libANGLE/HandleAllocator.h"
49 #include "libANGLE/RefCountObject.h"
50 #include "libANGLE/ResourceManager.h"
51 #include "libANGLE/ResourceMap.h"
52 #include "libANGLE/State.h"
53 #include "libANGLE/VertexAttribute.h"
54 #include "libANGLE/WorkerThread.h"
55 #include "libANGLE/angletypes.h"
56 
57 namespace angle
58 {
59 class FrameCapture;
60 struct FrontendFeatures;
61 }  // namespace angle
62 
63 namespace rx
64 {
65 class ContextImpl;
66 class EGLImplFactory;
67 }  // namespace rx
68 
69 namespace egl
70 {
71 class AttributeMap;
72 class Surface;
73 struct Config;
74 class Thread;
75 }  // namespace egl
76 
77 namespace gl
78 {
79 class Buffer;
80 class Compiler;
81 class FenceNV;
82 class Framebuffer;
83 class GLES1Renderer;
84 class MemoryProgramCache;
85 class MemoryObject;
86 class Program;
87 class ProgramPipeline;
88 class Query;
89 class Renderbuffer;
90 class Sampler;
91 class Semaphore;
92 class Shader;
93 class Sync;
94 class Texture;
95 class TransformFeedback;
96 class VertexArray;
97 struct VertexAttribute;
98 
99 class ErrorSet : angle::NonCopyable
100 {
101   public:
102     explicit ErrorSet(Context *context);
103     ~ErrorSet();
104 
105     bool empty() const;
106     GLenum popError();
107 
108     void handleError(GLenum errorCode,
109                      const char *message,
110                      const char *file,
111                      const char *function,
112                      unsigned int line);
113 
114     void validationError(GLenum errorCode, const char *message);
115 
116   private:
117     Context *mContext;
118     std::set<GLenum> mErrors;
119 };
120 
121 enum class VertexAttribTypeCase
122 {
123     Invalid        = 0,
124     Valid          = 1,
125     ValidSize4Only = 2,
126     ValidSize3or4  = 3,
127 };
128 
129 // Helper class for managing cache variables and state changes.
130 class StateCache final : angle::NonCopyable
131 {
132   public:
133     StateCache();
134     ~StateCache();
135 
136     void initialize(Context *context);
137 
138     // Places that can trigger updateActiveAttribsMask:
139     // 1. onVertexArrayBindingChange.
140     // 2. onProgramExecutableChange.
141     // 3. onVertexArrayStateChange.
142     // 4. onGLES1ClientStateChange.
getActiveBufferedAttribsMask()143     AttributesMask getActiveBufferedAttribsMask() const { return mCachedActiveBufferedAttribsMask; }
getActiveClientAttribsMask()144     AttributesMask getActiveClientAttribsMask() const { return mCachedActiveClientAttribsMask; }
getActiveDefaultAttribsMask()145     AttributesMask getActiveDefaultAttribsMask() const { return mCachedActiveDefaultAttribsMask; }
hasAnyEnabledClientAttrib()146     bool hasAnyEnabledClientAttrib() const { return mCachedHasAnyEnabledClientAttrib; }
hasAnyActiveClientAttrib()147     bool hasAnyActiveClientAttrib() const { return mCachedActiveClientAttribsMask.any(); }
148 
149     // Places that can trigger updateVertexElementLimits:
150     // 1. onVertexArrayBindingChange.
151     // 2. onProgramExecutableChange.
152     // 3. onVertexArrayFormatChange.
153     // 4. onVertexArrayBufferChange.
154     // 5. onVertexArrayStateChange.
getNonInstancedVertexElementLimit()155     GLint64 getNonInstancedVertexElementLimit() const
156     {
157         return mCachedNonInstancedVertexElementLimit;
158     }
getInstancedVertexElementLimit()159     GLint64 getInstancedVertexElementLimit() const { return mCachedInstancedVertexElementLimit; }
160 
161     // Places that can trigger updateBasicDrawStatesError:
162     // 1. onVertexArrayBindingChange.
163     // 2. onProgramExecutableChange.
164     // 3. onVertexArrayBufferContentsChange.
165     // 4. onVertexArrayStateChange.
166     // 5. onVertexArrayBufferStateChange.
167     // 6. onDrawFramebufferChange.
168     // 7. onContextCapChange.
169     // 8. onStencilStateChange.
170     // 9. onDefaultVertexAttributeChange.
171     // 10. onActiveTextureChange.
172     // 11. onQueryChange.
173     // 12. onActiveTransformFeedbackChange.
174     // 13. onUniformBufferStateChange.
175     // 14. onColorMaskChange.
176     // 15. onBufferBindingChange.
177     // 16. onBlendFuncIndexedChange.
hasBasicDrawStatesError(Context * context)178     bool hasBasicDrawStatesError(Context *context) const
179     {
180         if (mCachedBasicDrawStatesError == 0)
181         {
182             return false;
183         }
184         if (mCachedBasicDrawStatesError != kInvalidPointer)
185         {
186             return true;
187         }
188         return getBasicDrawStatesErrorImpl(context) != 0;
189     }
190 
getBasicDrawStatesError(const Context * context)191     intptr_t getBasicDrawStatesError(const Context *context) const
192     {
193         if (mCachedBasicDrawStatesError != kInvalidPointer)
194         {
195             return mCachedBasicDrawStatesError;
196         }
197 
198         return getBasicDrawStatesErrorImpl(context);
199     }
200 
201     // Places that can trigger updateBasicDrawElementsError:
202     // 1. onActiveTransformFeedbackChange.
203     // 2. onVertexArrayBufferStateChange.
204     // 3. onBufferBindingChange.
getBasicDrawElementsError(const Context * context)205     intptr_t getBasicDrawElementsError(const Context *context) const
206     {
207         if (mCachedBasicDrawElementsError != kInvalidPointer)
208         {
209             return mCachedBasicDrawElementsError;
210         }
211 
212         return getBasicDrawElementsErrorImpl(context);
213     }
214 
215     // Places that can trigger updateValidDrawModes:
216     // 1. onProgramExecutableChange.
217     // 2. onActiveTransformFeedbackChange.
isValidDrawMode(PrimitiveMode primitiveMode)218     bool isValidDrawMode(PrimitiveMode primitiveMode) const
219     {
220         return mCachedValidDrawModes[primitiveMode];
221     }
222 
223     // Cannot change except on Context/Extension init.
isValidBindTextureType(TextureType type)224     bool isValidBindTextureType(TextureType type) const
225     {
226         return mCachedValidBindTextureTypes[type];
227     }
228 
229     // Cannot change except on Context/Extension init.
isValidDrawElementsType(DrawElementsType type)230     bool isValidDrawElementsType(DrawElementsType type) const
231     {
232         return mCachedValidDrawElementsTypes[type];
233     }
234 
235     // Places that can trigger updateTransformFeedbackActiveUnpaused:
236     // 1. onActiveTransformFeedbackChange.
isTransformFeedbackActiveUnpaused()237     bool isTransformFeedbackActiveUnpaused() const
238     {
239         return mCachedTransformFeedbackActiveUnpaused;
240     }
241 
242     // Cannot change except on Context/Extension init.
getVertexAttribTypeValidation(VertexAttribType type)243     VertexAttribTypeCase getVertexAttribTypeValidation(VertexAttribType type) const
244     {
245         return mCachedVertexAttribTypesValidation[type];
246     }
247 
getIntegerVertexAttribTypeValidation(VertexAttribType type)248     VertexAttribTypeCase getIntegerVertexAttribTypeValidation(VertexAttribType type) const
249     {
250         return mCachedIntegerVertexAttribTypesValidation[type];
251     }
252 
253     // Places that can trigger updateActiveShaderStorageBufferIndices:
254     // 1. onProgramExecutableChange.
getActiveShaderStorageBufferIndices()255     StorageBuffersMask getActiveShaderStorageBufferIndices() const
256     {
257         return mCachedActiveShaderStorageBufferIndices;
258     }
259 
260     // State change notifications.
261     void onVertexArrayBindingChange(Context *context);
262     void onProgramExecutableChange(Context *context);
263     void onVertexArrayFormatChange(Context *context);
264     void onVertexArrayBufferContentsChange(Context *context);
265     void onVertexArrayStateChange(Context *context);
266     void onVertexArrayBufferStateChange(Context *context);
267     void onGLES1ClientStateChange(Context *context);
268     void onDrawFramebufferChange(Context *context);
269     void onContextCapChange(Context *context);
270     void onStencilStateChange(Context *context);
271     void onDefaultVertexAttributeChange(Context *context);
272     void onActiveTextureChange(Context *context);
273     void onQueryChange(Context *context);
274     void onActiveTransformFeedbackChange(Context *context);
275     void onUniformBufferStateChange(Context *context);
276     void onColorMaskChange(Context *context);
277     void onBufferBindingChange(Context *context);
278     void onBlendFuncIndexedChange(Context *context);
279 
280   private:
281     // Cache update functions.
282     void updateActiveAttribsMask(Context *context);
283     void updateVertexElementLimits(Context *context);
284     void updateVertexElementLimitsImpl(Context *context);
285     void updateValidDrawModes(Context *context);
286     void updateValidBindTextureTypes(Context *context);
287     void updateValidDrawElementsTypes(Context *context);
288     void updateBasicDrawStatesError();
289     void updateBasicDrawElementsError();
290     void updateTransformFeedbackActiveUnpaused(Context *context);
291     void updateVertexAttribTypesValidation(Context *context);
292     void updateActiveShaderStorageBufferIndices(Context *context);
293 
294     void setValidDrawModes(bool pointsOK, bool linesOK, bool trisOK, bool lineAdjOK, bool triAdjOK);
295 
296     intptr_t getBasicDrawStatesErrorImpl(const Context *context) const;
297     intptr_t getBasicDrawElementsErrorImpl(const Context *context) const;
298 
299     static constexpr intptr_t kInvalidPointer = 1;
300 
301     AttributesMask mCachedActiveBufferedAttribsMask;
302     AttributesMask mCachedActiveClientAttribsMask;
303     AttributesMask mCachedActiveDefaultAttribsMask;
304     bool mCachedHasAnyEnabledClientAttrib;
305     GLint64 mCachedNonInstancedVertexElementLimit;
306     GLint64 mCachedInstancedVertexElementLimit;
307     mutable intptr_t mCachedBasicDrawStatesError;
308     mutable intptr_t mCachedBasicDrawElementsError;
309     bool mCachedTransformFeedbackActiveUnpaused;
310     StorageBuffersMask mCachedActiveShaderStorageBufferIndices;
311 
312     // Reserve an extra slot at the end of these maps for invalid enum.
313     angle::PackedEnumMap<PrimitiveMode, bool, angle::EnumSize<PrimitiveMode>() + 1>
314         mCachedValidDrawModes;
315     angle::PackedEnumMap<TextureType, bool, angle::EnumSize<TextureType>() + 1>
316         mCachedValidBindTextureTypes;
317     angle::PackedEnumMap<DrawElementsType, bool, angle::EnumSize<DrawElementsType>() + 1>
318         mCachedValidDrawElementsTypes;
319     angle::PackedEnumMap<VertexAttribType,
320                          VertexAttribTypeCase,
321                          angle::EnumSize<VertexAttribType>() + 1>
322         mCachedVertexAttribTypesValidation;
323     angle::PackedEnumMap<VertexAttribType,
324                          VertexAttribTypeCase,
325                          angle::EnumSize<VertexAttribType>() + 1>
326         mCachedIntegerVertexAttribTypesValidation;
327 };
328 
329 using VertexArrayMap       = ResourceMap<VertexArray, VertexArrayID>;
330 using QueryMap             = ResourceMap<Query, QueryID>;
331 using TransformFeedbackMap = ResourceMap<TransformFeedback, TransformFeedbackID>;
332 
333 class Context final : public egl::LabeledObject, angle::NonCopyable, public angle::ObserverInterface
334 {
335   public:
336     Context(egl::Display *display,
337             const egl::Config *config,
338             const Context *shareContext,
339             TextureManager *shareTextures,
340             MemoryProgramCache *memoryProgramCache,
341             const EGLenum clientType,
342             const egl::AttributeMap &attribs,
343             const egl::DisplayExtensions &displayExtensions,
344             const egl::ClientExtensions &clientExtensions);
345 
346     // Use for debugging.
id()347     ContextID id() const { return mState.getContextID(); }
348 
349     egl::Error onDestroy(const egl::Display *display);
350     ~Context() override;
351 
352     void setLabel(EGLLabelKHR label) override;
353     EGLLabelKHR getLabel() const override;
354 
355     egl::Error makeCurrent(egl::Display *display,
356                            egl::Surface *drawSurface,
357                            egl::Surface *readSurface);
358     egl::Error unMakeCurrent(const egl::Display *display);
359 
360     // These create  and destroy methods are merely pass-throughs to
361     // ResourceManager, which owns these object types
362     BufferID createBuffer();
363     TextureID createTexture();
364     RenderbufferID createRenderbuffer();
365     ProgramPipelineID createProgramPipeline();
366     MemoryObjectID createMemoryObject();
367     SemaphoreID createSemaphore();
368 
369     void deleteBuffer(BufferID buffer);
370     void deleteTexture(TextureID texture);
371     void deleteRenderbuffer(RenderbufferID renderbuffer);
372     void deleteProgramPipeline(ProgramPipelineID pipeline);
373     void deleteMemoryObject(MemoryObjectID memoryObject);
374     void deleteSemaphore(SemaphoreID semaphore);
375 
376     void bindReadFramebuffer(FramebufferID framebufferHandle);
377     void bindDrawFramebuffer(FramebufferID framebufferHandle);
378 
379     Buffer *getBuffer(BufferID handle) const;
380     FenceNV *getFenceNV(FenceNVID handle) const;
381     Sync *getSync(GLsync handle) const;
getTexture(TextureID handle)382     ANGLE_INLINE Texture *getTexture(TextureID handle) const
383     {
384         return mState.mTextureManager->getTexture(handle);
385     }
386 
387     Framebuffer *getFramebuffer(FramebufferID handle) const;
388     Renderbuffer *getRenderbuffer(RenderbufferID handle) const;
389     VertexArray *getVertexArray(VertexArrayID handle) const;
390     Sampler *getSampler(SamplerID handle) const;
391     Query *getOrCreateQuery(QueryID handle, QueryType type);
392     Query *getQuery(QueryID handle) const;
393     TransformFeedback *getTransformFeedback(TransformFeedbackID handle) const;
394     ProgramPipeline *getProgramPipeline(ProgramPipelineID handle) const;
395     MemoryObject *getMemoryObject(MemoryObjectID handle) const;
396     Semaphore *getSemaphore(SemaphoreID handle) const;
397 
398     Texture *getTextureByType(TextureType type) const;
399     Texture *getTextureByTarget(TextureTarget target) const;
400     Texture *getSamplerTexture(unsigned int sampler, TextureType type) const;
401 
402     Compiler *getCompiler() const;
403 
404     bool isVertexArrayGenerated(VertexArrayID vertexArray) const;
405     bool isTransformFeedbackGenerated(TransformFeedbackID transformFeedback) const;
406 
407     void getBooleanvImpl(GLenum pname, GLboolean *params) const;
408     void getFloatvImpl(GLenum pname, GLfloat *params) const;
409     void getIntegervImpl(GLenum pname, GLint *params) const;
410     void getInteger64vImpl(GLenum pname, GLint64 *params) const;
411     void getIntegerVertexAttribImpl(GLenum pname, GLenum attribpname, GLint *params) const;
412     void getVertexAttribivImpl(GLuint index, GLenum pname, GLint *params) const;
413 
414     // Framebuffers are owned by the Context, so these methods do not pass through
415     FramebufferID createFramebuffer();
416     void deleteFramebuffer(FramebufferID framebuffer);
417 
418     bool hasActiveTransformFeedback(ShaderProgramID program) const;
419 
420     // GL emulation: Interface to entry points
421     ANGLE_GL_1_0_CONTEXT_API
422     ANGLE_GL_1_1_CONTEXT_API
423     ANGLE_GL_1_2_CONTEXT_API
424     ANGLE_GL_1_3_CONTEXT_API
425     ANGLE_GL_1_4_CONTEXT_API
426     ANGLE_GL_1_5_CONTEXT_API
427     ANGLE_GL_2_0_CONTEXT_API
428     ANGLE_GL_2_1_CONTEXT_API
429     ANGLE_GL_3_0_CONTEXT_API
430     ANGLE_GL_3_1_CONTEXT_API
431     ANGLE_GL_3_2_CONTEXT_API
432     ANGLE_GL_3_3_CONTEXT_API
433     ANGLE_GL_4_0_CONTEXT_API
434     ANGLE_GL_4_1_CONTEXT_API
435     ANGLE_GL_4_2_CONTEXT_API
436     ANGLE_GL_4_3_CONTEXT_API
437     ANGLE_GL_4_4_CONTEXT_API
438     ANGLE_GL_4_5_CONTEXT_API
439     ANGLE_GL_4_6_CONTEXT_API
440 
441     // GLES emulation: Interface to entry points
442     ANGLE_GLES_1_0_CONTEXT_API
443     ANGLE_GLES_2_0_CONTEXT_API
444     ANGLE_GLES_3_0_CONTEXT_API
445     ANGLE_GLES_3_1_CONTEXT_API
446     ANGLE_GLES_3_2_CONTEXT_API
447     ANGLE_GLES_EXT_CONTEXT_API
448 
449     // Consumes an error.
450     void handleError(GLenum errorCode,
451                      const char *message,
452                      const char *file,
453                      const char *function,
454                      unsigned int line);
455 
456     void validationError(GLenum errorCode, const char *message) const;
457 
458     void markContextLost(GraphicsResetStatus status);
459 
isContextLost()460     bool isContextLost() const { return mContextLost; }
461     void setContextLost();
462 
getGraphicsResetStrategy()463     GLenum getGraphicsResetStrategy() const { return mResetStrategy; }
464     bool isResetNotificationEnabled();
465 
466     const egl::Config *getConfig() const;
467     EGLenum getClientType() const;
468     EGLenum getRenderBuffer() const;
469     EGLenum getContextPriority() const;
470 
471     const GLubyte *getString(GLenum name) const;
472     const GLubyte *getStringi(GLenum name, GLuint index) const;
473 
474     size_t getExtensionStringCount() const;
475 
476     bool isExtensionRequestable(const char *name) const;
477     bool isExtensionDisablable(const char *name) const;
478     size_t getRequestableExtensionStringCount() const;
479     void setExtensionEnabled(const char *name, bool enabled);
480     void reinitializeAfterExtensionsChanged();
481 
getImplementation()482     rx::ContextImpl *getImplementation() const { return mImplementation.get(); }
483 
484     ANGLE_NO_DISCARD bool getScratchBuffer(size_t requestedSizeBytes,
485                                            angle::MemoryBuffer **scratchBufferOut) const;
486     ANGLE_NO_DISCARD bool getZeroFilledBuffer(size_t requstedSizeBytes,
487                                               angle::MemoryBuffer **zeroBufferOut) const;
488     angle::ScratchBuffer *getScratchBuffer() const;
489 
490     angle::Result prepareForCopyImage();
491     angle::Result prepareForDispatch();
492 
getMemoryProgramCache()493     MemoryProgramCache *getMemoryProgramCache() const { return mMemoryProgramCache; }
494 
hasBeenCurrent()495     bool hasBeenCurrent() const { return mHasBeenCurrent; }
getDisplay()496     egl::Display *getDisplay() const { return mDisplay; }
getCurrentDrawSurface()497     egl::Surface *getCurrentDrawSurface() const { return mCurrentDrawSurface; }
getCurrentReadSurface()498     egl::Surface *getCurrentReadSurface() const { return mCurrentReadSurface; }
499 
isRobustResourceInitEnabled()500     bool isRobustResourceInitEnabled() const { return mState.isRobustResourceInitEnabled(); }
501 
502     bool isCurrentTransformFeedback(const TransformFeedback *tf) const;
503 
isCurrentVertexArray(const VertexArray * va)504     bool isCurrentVertexArray(const VertexArray *va) const
505     {
506         return mState.isCurrentVertexArray(va);
507     }
508 
isShared()509     bool isShared() const { return mShared; }
510     // Once a context is setShared() it cannot be undone
setShared()511     void setShared() { mShared = true; }
512 
getState()513     const State &getState() const { return mState; }
getClientMajorVersion()514     GLint getClientMajorVersion() const { return mState.getClientMajorVersion(); }
getClientMinorVersion()515     GLint getClientMinorVersion() const { return mState.getClientMinorVersion(); }
getClientVersion()516     const Version &getClientVersion() const { return mState.getClientVersion(); }
getCaps()517     const Caps &getCaps() const { return mState.getCaps(); }
getTextureCaps()518     const TextureCapsMap &getTextureCaps() const { return mState.getTextureCaps(); }
getExtensions()519     const Extensions &getExtensions() const { return mState.getExtensions(); }
getLimitations()520     const Limitations &getLimitations() const { return mState.getLimitations(); }
521     bool isGLES1() const;
522 
skipValidation()523     bool skipValidation() const
524     {
525         // Ensure we don't skip validation when context becomes lost, since implementations
526         // generally assume a non-lost context, non-null objects, etc.
527         ASSERT(!isContextLost() || !mSkipValidation);
528         return mSkipValidation;
529     }
530 
531     // Specific methods needed for validation.
532     bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const;
533     bool getIndexedQueryParameterInfo(GLenum target, GLenum *type, unsigned int *numParams) const;
534 
getProgramResolveLink(ShaderProgramID handle)535     ANGLE_INLINE Program *getProgramResolveLink(ShaderProgramID handle) const
536     {
537         Program *program = mState.mShaderProgramManager->getProgram(handle);
538         if (program)
539         {
540             program->resolveLink(this);
541         }
542         return program;
543     }
544 
545     Program *getProgramNoResolveLink(ShaderProgramID handle) const;
546     Shader *getShader(ShaderProgramID handle) const;
547 
isTextureGenerated(TextureID texture)548     ANGLE_INLINE bool isTextureGenerated(TextureID texture) const
549     {
550         return mState.mTextureManager->isHandleGenerated(texture);
551     }
552 
isBufferGenerated(BufferID buffer)553     ANGLE_INLINE bool isBufferGenerated(BufferID buffer) const
554     {
555         return mState.mBufferManager->isHandleGenerated(buffer);
556     }
557 
558     bool isRenderbufferGenerated(RenderbufferID renderbuffer) const;
559     bool isFramebufferGenerated(FramebufferID framebuffer) const;
560     bool isProgramPipelineGenerated(ProgramPipelineID pipeline) const;
561     bool isQueryGenerated(QueryID query) const;
562 
563     bool usingDisplayTextureShareGroup() const;
564 
565     // Hack for the special WebGL 1 "DEPTH_STENCIL" internal format.
566     GLenum getConvertedRenderbufferFormat(GLenum internalformat) const;
567 
isWebGL()568     bool isWebGL() const { return mState.isWebGL(); }
isWebGL1()569     bool isWebGL1() const { return mState.isWebGL1(); }
570 
isValidBufferBinding(BufferBinding binding)571     bool isValidBufferBinding(BufferBinding binding) const { return mValidBufferBindings[binding]; }
572 
573     // GLES1 emulation: Renderer level (for validation)
574     int vertexArrayIndex(ClientVertexArrayType type) const;
575     static int TexCoordArrayIndex(unsigned int unit);
576 
577     // GL_KHR_parallel_shader_compile
getWorkerThreadPool()578     std::shared_ptr<angle::WorkerThreadPool> getWorkerThreadPool() const { return mThreadPool; }
579 
getStateCache()580     const StateCache &getStateCache() const { return mStateCache; }
getStateCache()581     StateCache &getStateCache() { return mStateCache; }
582 
583     void onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message) override;
584 
585     void onSamplerUniformChange(size_t textureUnitIndex);
586 
isBufferAccessValidationEnabled()587     bool isBufferAccessValidationEnabled() const { return mBufferAccessValidationEnabled; }
588 
589     const angle::FrontendFeatures &getFrontendFeatures() const;
590 
getFrameCapture()591     angle::FrameCapture *getFrameCapture() { return mFrameCapture.get(); }
592 
getVertexArraysForCapture()593     const VertexArrayMap &getVertexArraysForCapture() const { return mVertexArrayMap; }
getQueriesForCapture()594     const QueryMap &getQueriesForCapture() const { return mQueryMap; }
getTransformFeedbacksForCapture()595     const TransformFeedbackMap &getTransformFeedbacksForCapture() const
596     {
597         return mTransformFeedbackMap;
598     }
599 
600     void onPostSwap() const;
601 
602     Program *getActiveLinkedProgram() const;
603 
604   private:
605     void initialize();
606 
607     bool noopDraw(PrimitiveMode mode, GLsizei count);
608     bool noopDrawInstanced(PrimitiveMode mode, GLsizei count, GLsizei instanceCount);
609 
610     angle::Result prepareForDraw(PrimitiveMode mode);
611     angle::Result prepareForClear(GLbitfield mask);
612     angle::Result prepareForClearBuffer(GLenum buffer, GLint drawbuffer);
613     angle::Result syncState(const State::DirtyBits &bitMask, const State::DirtyObjects &objectMask);
614     angle::Result syncDirtyBits();
615     angle::Result syncDirtyBits(const State::DirtyBits &bitMask);
616     angle::Result syncDirtyObjects(const State::DirtyObjects &objectMask);
617     angle::Result syncStateForReadPixels();
618     angle::Result syncStateForTexImage();
619     angle::Result syncStateForBlit();
620 
621     VertexArray *checkVertexArrayAllocation(VertexArrayID vertexArrayHandle);
622     TransformFeedback *checkTransformFeedbackAllocation(TransformFeedbackID transformFeedback);
623 
624     angle::Result onProgramLink(Program *programObject);
625 
626     void detachBuffer(Buffer *buffer);
627     void detachTexture(TextureID texture);
628     void detachFramebuffer(FramebufferID framebuffer);
629     void detachRenderbuffer(RenderbufferID renderbuffer);
630     void detachVertexArray(VertexArrayID vertexArray);
631     void detachTransformFeedback(TransformFeedbackID transformFeedback);
632     void detachSampler(SamplerID sampler);
633     void detachProgramPipeline(ProgramPipelineID pipeline);
634 
635     egl::Error setDefaultFramebuffer(egl::Surface *drawSurface, egl::Surface *readSurface);
636     egl::Error unsetDefaultFramebuffer();
637 
638     void initRendererString();
639     void initVersionStrings();
640     void initExtensionStrings();
641 
642     Extensions generateSupportedExtensions() const;
643     void initCaps();
644     void updateCaps();
645 
646     gl::LabeledObject *getLabeledObject(GLenum identifier, GLuint name) const;
647     gl::LabeledObject *getLabeledObjectFromPtr(const void *ptr) const;
648 
649     void setUniform1iImpl(Program *program,
650                           UniformLocation location,
651                           GLsizei count,
652                           const GLint *v);
653 
654     State mState;
655     bool mShared;
656     bool mSkipValidation;
657     bool mDisplayTextureShareGroup;
658 
659     // Recorded errors
660     ErrorSet mErrors;
661 
662     // Stores for each buffer binding type whether is it allowed to be used in this context.
663     angle::PackedEnumBitSet<BufferBinding> mValidBufferBindings;
664 
665     std::unique_ptr<rx::ContextImpl> mImplementation;
666 
667     EGLLabelKHR mLabel;
668 
669     // Extensions supported by the implementation plus extensions that are implemented entirely
670     // within the frontend.
671     Extensions mSupportedExtensions;
672 
673     // Shader compiler. Lazily initialized hence the mutable value.
674     mutable BindingPointer<Compiler> mCompiler;
675 
676     const egl::Config *mConfig;
677 
678     TextureMap mZeroTextures;
679 
680     ResourceMap<FenceNV, FenceNVID> mFenceNVMap;
681     HandleAllocator mFenceNVHandleAllocator;
682 
683     QueryMap mQueryMap;
684     HandleAllocator mQueryHandleAllocator;
685 
686     VertexArrayMap mVertexArrayMap;
687     HandleAllocator mVertexArrayHandleAllocator;
688 
689     TransformFeedbackMap mTransformFeedbackMap;
690     HandleAllocator mTransformFeedbackHandleAllocator;
691 
692     const char *mVersionString;
693     const char *mShadingLanguageString;
694     const char *mRendererString;
695     const char *mExtensionString;
696     std::vector<const char *> mExtensionStrings;
697     const char *mRequestableExtensionString;
698     std::vector<const char *> mRequestableExtensionStrings;
699 
700     // GLES1 renderer state
701     std::unique_ptr<GLES1Renderer> mGLES1Renderer;
702 
703     // Current/lost context flags
704     bool mHasBeenCurrent;
705     bool mContextLost;  // Set with setContextLost so that we also set mSkipValidation=false.
706     GraphicsResetStatus mResetStatus;
707     bool mContextLostForced;
708     GLenum mResetStrategy;
709     const bool mRobustAccess;
710     const bool mSurfacelessSupported;
711     const bool mExplicitContextAvailable;
712     egl::Surface *mCurrentDrawSurface;
713     egl::Surface *mCurrentReadSurface;
714     egl::Display *mDisplay;
715     const bool mWebGLContext;
716     bool mBufferAccessValidationEnabled;
717     const bool mExtensionsEnabled;
718     MemoryProgramCache *mMemoryProgramCache;
719 
720     State::DirtyObjects mDrawDirtyObjects;
721 
722     StateCache mStateCache;
723 
724     State::DirtyBits mAllDirtyBits;
725     State::DirtyBits mTexImageDirtyBits;
726     State::DirtyObjects mTexImageDirtyObjects;
727     State::DirtyBits mReadPixelsDirtyBits;
728     State::DirtyObjects mReadPixelsDirtyObjects;
729     State::DirtyBits mClearDirtyBits;
730     State::DirtyObjects mClearDirtyObjects;
731     State::DirtyBits mBlitDirtyBits;
732     State::DirtyObjects mBlitDirtyObjects;
733     State::DirtyBits mComputeDirtyBits;
734     State::DirtyObjects mComputeDirtyObjects;
735     State::DirtyBits mCopyImageDirtyBits;
736     State::DirtyObjects mCopyImageDirtyObjects;
737 
738     // Binding to container objects that use dependent state updates.
739     angle::ObserverBinding mVertexArrayObserverBinding;
740     angle::ObserverBinding mDrawFramebufferObserverBinding;
741     angle::ObserverBinding mReadFramebufferObserverBinding;
742     std::vector<angle::ObserverBinding> mUniformBufferObserverBindings;
743     std::vector<angle::ObserverBinding> mSamplerObserverBindings;
744     std::vector<angle::ObserverBinding> mImageObserverBindings;
745 
746     // Not really a property of context state. The size and contexts change per-api-call.
747     mutable Optional<angle::ScratchBuffer> mScratchBuffer;
748     mutable Optional<angle::ScratchBuffer> mZeroFilledBuffer;
749 
750     std::shared_ptr<angle::WorkerThreadPool> mThreadPool;
751 
752     // Note: we use a raw pointer here so we can exclude frame capture sources from the build.
753     std::unique_ptr<angle::FrameCapture> mFrameCapture;
754 
755     OverlayType mOverlay;
756 };
757 }  // namespace gl
758 
759 #endif  // LIBANGLE_CONTEXT_H_
760