OSDN Git Service

Implement eglQueryContext.
[android-x86/external-swiftshader.git] / src / OpenGL / libGLESv2 / Context.h
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Context.h: Defines the Context class, managing all GL state and performing
16 // rendering operations. It is the GLES2 specific implementation of EGLContext.
17
18 #ifndef LIBGLESV2_CONTEXT_H_
19 #define LIBGLESV2_CONTEXT_H_
20
21 #include "ResourceManager.h"
22 #include "Buffer.h"
23 #include "libEGL/Context.hpp"
24 #include "common/NameSpace.hpp"
25 #include "common/Object.hpp"
26 #include "common/Image.hpp"
27 #include "Renderer/Sampler.hpp"
28
29 #include <GLES2/gl2.h>
30 #include <GLES2/gl2ext.h>
31 #include <GLES3/gl3.h>
32 #include <EGL/egl.h>
33
34 #include <map>
35 #include <string>
36
37 namespace egl
38 {
39 class Display;
40 class Surface;
41 class Config;
42 }
43
44 namespace es2
45 {
46 struct TranslatedAttribute;
47 struct TranslatedIndexData;
48
49 class Device;
50 class Shader;
51 class Program;
52 class Texture;
53 class Texture2D;
54 class Texture3D;
55 class Texture2DArray;
56 class TextureCubeMap;
57 class TextureExternal;
58 class Framebuffer;
59 class Renderbuffer;
60 class RenderbufferStorage;
61 class Colorbuffer;
62 class Depthbuffer;
63 class StreamingIndexBuffer;
64 class Stencilbuffer;
65 class DepthStencilbuffer;
66 class VertexDataManager;
67 class IndexDataManager;
68 class Fence;
69 class FenceSync;
70 class Query;
71 class Sampler;
72 class VertexArray;
73 class TransformFeedback;
74
75 enum
76 {
77         MAX_VERTEX_ATTRIBS = sw::MAX_VERTEX_INPUTS,
78         MAX_UNIFORM_VECTORS = 256,   // Device limit
79         MAX_VERTEX_UNIFORM_VECTORS = sw::VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange
80         MAX_VARYING_VECTORS = MIN(sw::MAX_FRAGMENT_INPUTS, sw::MAX_VERTEX_OUTPUTS),
81         MAX_TEXTURE_IMAGE_UNITS = sw::TEXTURE_IMAGE_UNITS,
82         MAX_VERTEX_TEXTURE_IMAGE_UNITS = sw::VERTEX_TEXTURE_IMAGE_UNITS,
83         MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,
84         MAX_FRAGMENT_UNIFORM_VECTORS = sw::FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange
85         MAX_ELEMENT_INDEX = 0x7FFFFFFF,
86         MAX_ELEMENTS_INDICES = 0x7FFFFFFF,
87         MAX_ELEMENTS_VERTICES = 0x7FFFFFFF,
88         MAX_VERTEX_OUTPUT_VECTORS = 16,
89         MAX_FRAGMENT_INPUT_VECTORS = 15,
90         MIN_PROGRAM_TEXEL_OFFSET = sw::MIN_PROGRAM_TEXEL_OFFSET,
91         MAX_PROGRAM_TEXEL_OFFSET = sw::MAX_PROGRAM_TEXEL_OFFSET,
92         MAX_DRAW_BUFFERS = sw::RENDERTARGETS,
93         MAX_COLOR_ATTACHMENTS = MAX(MAX_DRAW_BUFFERS, 8),
94         MAX_FRAGMENT_UNIFORM_BLOCKS = sw::MAX_FRAGMENT_UNIFORM_BLOCKS,
95         MAX_VERTEX_UNIFORM_BLOCKS = sw::MAX_VERTEX_UNIFORM_BLOCKS,
96         MAX_FRAGMENT_UNIFORM_COMPONENTS = sw::FRAGMENT_UNIFORM_VECTORS * 4,
97         MAX_VERTEX_UNIFORM_COMPONENTS = sw::VERTEX_UNIFORM_VECTORS * 4,
98         MAX_UNIFORM_BLOCK_SIZE = sw::MAX_UNIFORM_BLOCK_SIZE,
99         MAX_FRAGMENT_UNIFORM_BLOCKS_COMPONENTS = sw::MAX_FRAGMENT_UNIFORM_BLOCKS * MAX_UNIFORM_BLOCK_SIZE / 4,
100         MAX_VERTEX_UNIFORM_BLOCKS_COMPONENTS = MAX_VERTEX_UNIFORM_BLOCKS * MAX_UNIFORM_BLOCK_SIZE / 4,
101         MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS = MAX_FRAGMENT_UNIFORM_BLOCKS_COMPONENTS + MAX_FRAGMENT_UNIFORM_COMPONENTS,
102         MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS = MAX_VERTEX_UNIFORM_BLOCKS_COMPONENTS + MAX_VERTEX_UNIFORM_COMPONENTS,
103         MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS = 4,
104         MAX_UNIFORM_BUFFER_BINDINGS = sw::MAX_UNIFORM_BUFFER_BINDINGS,
105         UNIFORM_BUFFER_OFFSET_ALIGNMENT = 1,
106         NUM_PROGRAM_BINARY_FORMATS = 0,
107 };
108
109 const GLenum compressedTextureFormats[] =
110 {
111         GL_ETC1_RGB8_OES,
112 #if (S3TC_SUPPORT)
113         GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
114         GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
115         GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE,
116         GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE,
117 #endif
118 #if (GL_ES_VERSION_3_0)
119         GL_COMPRESSED_R11_EAC,
120         GL_COMPRESSED_SIGNED_R11_EAC,
121         GL_COMPRESSED_RG11_EAC,
122         GL_COMPRESSED_SIGNED_RG11_EAC,
123         GL_COMPRESSED_RGB8_ETC2,
124         GL_COMPRESSED_SRGB8_ETC2,
125         GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
126         GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
127         GL_COMPRESSED_RGBA8_ETC2_EAC,
128         GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
129         GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
130         GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
131         GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
132         GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
133         GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
134         GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
135         GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
136         GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
137         GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
138         GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
139         GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
140         GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
141         GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
142         GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
143         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
144         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
145         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
146         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
147         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
148         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
149         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
150         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
151         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
152         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
153         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
154         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
155         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
156         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
157 #endif
158 };
159
160 const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);
161
162 const GLint multisampleCount[] = {4, 2, 1};
163 const GLint NUM_MULTISAMPLE_COUNTS = sizeof(multisampleCount) / sizeof(multisampleCount[0]);
164 const GLint IMPLEMENTATION_MAX_SAMPLES = multisampleCount[0];
165
166 const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f;
167 const float ALIASED_LINE_WIDTH_RANGE_MAX = 1.0f;
168 const float ALIASED_POINT_SIZE_RANGE_MIN = 0.125f;
169 const float ALIASED_POINT_SIZE_RANGE_MAX = 8192.0f;
170 const float MAX_TEXTURE_MAX_ANISOTROPY = 16.0f;
171
172 enum QueryType
173 {
174         QUERY_ANY_SAMPLES_PASSED,
175         QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,
176         QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
177
178         QUERY_TYPE_COUNT
179 };
180
181 struct Color
182 {
183         float red;
184         float green;
185         float blue;
186         float alpha;
187 };
188
189 // Helper structure describing a single vertex attribute
190 class VertexAttribute
191 {
192 public:
193         VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mDivisor(0), mPointer(nullptr), mArrayEnabled(false)
194         {
195                 mCurrentValue[0].f = 0.0f;
196                 mCurrentValue[1].f = 0.0f;
197                 mCurrentValue[2].f = 0.0f;
198                 mCurrentValue[3].f = 1.0f;
199                 mCurrentValueType = GL_FLOAT;
200         }
201
202         int typeSize() const
203         {
204                 switch(mType)
205                 {
206                 case GL_BYTE:           return mSize * sizeof(GLbyte);
207                 case GL_UNSIGNED_BYTE:  return mSize * sizeof(GLubyte);
208                 case GL_SHORT:          return mSize * sizeof(GLshort);
209                 case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort);
210                 case GL_INT:            return mSize * sizeof(GLint);
211                 case GL_UNSIGNED_INT:   return mSize * sizeof(GLuint);
212                 case GL_FIXED:          return mSize * sizeof(GLfixed);
213                 case GL_FLOAT:          return mSize * sizeof(GLfloat);
214                 case GL_HALF_FLOAT:     return mSize * sizeof(GLhalf);
215                 case GL_INT_2_10_10_10_REV:          return sizeof(GLint);
216                 case GL_UNSIGNED_INT_2_10_10_10_REV: return sizeof(GLuint);
217                 default: UNREACHABLE(mType); return mSize * sizeof(GLfloat);
218                 }
219         }
220
221         GLenum currentValueType() const
222         {
223                 return mCurrentValueType;
224         }
225
226         GLsizei stride() const
227         {
228                 return mStride ? mStride : typeSize();
229         }
230
231         inline float getCurrentValueBitsAsFloat(int i) const
232         {
233                 return mCurrentValue[i].f;
234         }
235
236         inline float getCurrentValueF(int i) const
237         {
238                 switch(mCurrentValueType)
239                 {
240                 case GL_FLOAT:        return mCurrentValue[i].f;
241                 case GL_INT:          return static_cast<float>(mCurrentValue[i].i);
242                 case GL_UNSIGNED_INT: return static_cast<float>(mCurrentValue[i].ui);
243                 default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].f;
244                 }
245         }
246
247         inline GLint getCurrentValueI(int i) const
248         {
249                 switch(mCurrentValueType)
250                 {
251                 case GL_FLOAT:        return static_cast<GLint>(mCurrentValue[i].f);
252                 case GL_INT:          return mCurrentValue[i].i;
253                 case GL_UNSIGNED_INT: return static_cast<GLint>(mCurrentValue[i].ui);
254                 default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].i;
255                 }
256         }
257
258         inline GLuint getCurrentValueUI(int i) const
259         {
260                 switch(mCurrentValueType)
261                 {
262                 case GL_FLOAT:        return static_cast<GLuint>(mCurrentValue[i].f);
263                 case GL_INT:          return static_cast<GLuint>(mCurrentValue[i].i);
264                 case GL_UNSIGNED_INT: return mCurrentValue[i].ui;
265                 default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].ui;
266                 }
267         }
268
269         inline void setCurrentValue(const GLfloat *values)
270         {
271                 mCurrentValue[0].f = values[0];
272                 mCurrentValue[1].f = values[1];
273                 mCurrentValue[2].f = values[2];
274                 mCurrentValue[3].f = values[3];
275                 mCurrentValueType = GL_FLOAT;
276         }
277
278         inline void setCurrentValue(const GLint *values)
279         {
280                 mCurrentValue[0].i = values[0];
281                 mCurrentValue[1].i = values[1];
282                 mCurrentValue[2].i = values[2];
283                 mCurrentValue[3].i = values[3];
284                 mCurrentValueType = GL_INT;
285         }
286
287         inline void setCurrentValue(const GLuint *values)
288         {
289                 mCurrentValue[0].ui = values[0];
290                 mCurrentValue[1].ui = values[1];
291                 mCurrentValue[2].ui = values[2];
292                 mCurrentValue[3].ui = values[3];
293                 mCurrentValueType = GL_UNSIGNED_INT;
294         }
295
296         // From glVertexAttribPointer
297         GLenum mType;
298         GLint mSize;
299         bool mNormalized;
300         GLsizei mStride;   // 0 means natural stride
301         GLuint mDivisor;   // From glVertexAttribDivisor
302
303         union
304         {
305                 const void *mPointer;
306                 intptr_t mOffset;
307         };
308
309         gl::BindingPointer<Buffer> mBoundBuffer;   // Captured when glVertexAttribPointer is called.
310
311         bool mArrayEnabled;   // From glEnable/DisableVertexAttribArray
312
313 private:
314         union ValueUnion
315         {
316                 float f;
317                 GLint i;
318                 GLuint ui;
319         };
320
321         ValueUnion mCurrentValue[4];   // From glVertexAttrib
322         GLenum mCurrentValueType;
323 };
324
325 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
326
327 // Helper structure to store all raw state
328 struct State
329 {
330         Color colorClearValue;
331         GLclampf depthClearValue;
332         int stencilClearValue;
333
334         bool cullFaceEnabled;
335         GLenum cullMode;
336         GLenum frontFace;
337         bool depthTestEnabled;
338         GLenum depthFunc;
339         bool blendEnabled;
340         GLenum sourceBlendRGB;
341         GLenum destBlendRGB;
342         GLenum sourceBlendAlpha;
343         GLenum destBlendAlpha;
344         GLenum blendEquationRGB;
345         GLenum blendEquationAlpha;
346         Color blendColor;
347         bool stencilTestEnabled;
348         GLenum stencilFunc;
349         GLint stencilRef;
350         GLuint stencilMask;
351         GLenum stencilFail;
352         GLenum stencilPassDepthFail;
353         GLenum stencilPassDepthPass;
354         GLuint stencilWritemask;
355         GLenum stencilBackFunc;
356         GLint stencilBackRef;
357         GLuint stencilBackMask;
358         GLenum stencilBackFail;
359         GLenum stencilBackPassDepthFail;
360         GLenum stencilBackPassDepthPass;
361         GLuint stencilBackWritemask;
362         bool polygonOffsetFillEnabled;
363         GLfloat polygonOffsetFactor;
364         GLfloat polygonOffsetUnits;
365         bool sampleAlphaToCoverageEnabled;
366         bool sampleCoverageEnabled;
367         GLclampf sampleCoverageValue;
368         bool sampleCoverageInvert;
369         bool scissorTestEnabled;
370         bool ditherEnabled;
371         bool primitiveRestartFixedIndexEnabled;
372         bool rasterizerDiscardEnabled;
373         bool colorLogicOpEnabled;
374         GLenum logicalOperation;
375
376         GLfloat lineWidth;
377
378         GLenum generateMipmapHint;
379         GLenum fragmentShaderDerivativeHint;
380
381         GLint viewportX;
382         GLint viewportY;
383         GLsizei viewportWidth;
384         GLsizei viewportHeight;
385         float zNear;
386         float zFar;
387
388         GLint scissorX;
389         GLint scissorY;
390         GLsizei scissorWidth;
391         GLsizei scissorHeight;
392
393         bool colorMaskRed;
394         bool colorMaskGreen;
395         bool colorMaskBlue;
396         bool colorMaskAlpha;
397         bool depthMask;
398
399         unsigned int activeSampler;   // Active texture unit selector - GL_TEXTURE0
400         gl::BindingPointer<Buffer> arrayBuffer;
401         gl::BindingPointer<Buffer> copyReadBuffer;
402         gl::BindingPointer<Buffer> copyWriteBuffer;
403         gl::BindingPointer<Buffer> pixelPackBuffer;
404         gl::BindingPointer<Buffer> pixelUnpackBuffer;
405         gl::BindingPointer<Buffer> genericUniformBuffer;
406         BufferBinding uniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
407
408         GLuint readFramebuffer;
409         GLuint drawFramebuffer;
410         gl::BindingPointer<Renderbuffer> renderbuffer;
411         GLuint currentProgram;
412         GLuint vertexArray;
413         GLuint transformFeedback;
414         gl::BindingPointer<Sampler> sampler[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
415
416         VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS];
417         gl::BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][MAX_COMBINED_TEXTURE_IMAGE_UNITS];
418         gl::BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT];
419
420         egl::Image::UnpackInfo unpackInfo;
421         GLint packAlignment;
422         GLint packRowLength;
423         GLint packImageHeight;
424         GLint packSkipPixels;
425         GLint packSkipRows;
426         GLint packSkipImages;
427 };
428
429 class [[clang::lto_visibility_public]] Context : public egl::Context
430 {
431 public:
432         Context(egl::Display *display, const Context *shareContext, EGLint clientVersion, const egl::Config *config);
433
434         void makeCurrent(egl::Surface *surface) override;
435         EGLint getClientVersion() const override;
436         EGLint getConfigID() const override;
437
438         void markAllStateDirty();
439
440         // State manipulation
441         void setClearColor(float red, float green, float blue, float alpha);
442         void setClearDepth(float depth);
443         void setClearStencil(int stencil);
444
445         void setCullFaceEnabled(bool enabled);
446         bool isCullFaceEnabled() const;
447         void setCullMode(GLenum mode);
448         void setFrontFace(GLenum front);
449
450         void setDepthTestEnabled(bool enabled);
451         bool isDepthTestEnabled() const;
452         void setDepthFunc(GLenum depthFunc);
453         void setDepthRange(float zNear, float zFar);
454
455         void setBlendEnabled(bool enabled);
456         bool isBlendEnabled() const;
457         void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);
458         void setBlendColor(float red, float green, float blue, float alpha);
459         void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);
460
461         void setStencilTestEnabled(bool enabled);
462         bool isStencilTestEnabled() const;
463         void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);
464         void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);
465         void setStencilWritemask(GLuint stencilWritemask);
466         void setStencilBackWritemask(GLuint stencilBackWritemask);
467         void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);
468         void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);
469
470         void setPolygonOffsetFillEnabled(bool enabled);
471         bool isPolygonOffsetFillEnabled() const;
472         void setPolygonOffsetParams(GLfloat factor, GLfloat units);
473
474         void setSampleAlphaToCoverageEnabled(bool enabled);
475         bool isSampleAlphaToCoverageEnabled() const;
476         void setSampleCoverageEnabled(bool enabled);
477         bool isSampleCoverageEnabled() const;
478         void setSampleCoverageParams(GLclampf value, bool invert);
479
480         void setDitherEnabled(bool enabled);
481         bool isDitherEnabled() const;
482
483         void setPrimitiveRestartFixedIndexEnabled(bool enabled);
484         bool isPrimitiveRestartFixedIndexEnabled() const;
485
486         void setRasterizerDiscardEnabled(bool enabled);
487         bool isRasterizerDiscardEnabled() const;
488
489         void setLineWidth(GLfloat width);
490
491         void setGenerateMipmapHint(GLenum hint);
492         void setFragmentShaderDerivativeHint(GLenum hint);
493
494         void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
495
496         void setScissorTestEnabled(bool enabled);
497         bool isScissorTestEnabled() const;
498         void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
499
500         void setColorMask(bool red, bool green, bool blue, bool alpha);
501         unsigned int getColorMask() const;
502         void setDepthMask(bool mask);
503
504         void setActiveSampler(unsigned int active);
505
506         GLuint getReadFramebufferName() const;
507         GLuint getDrawFramebufferName() const;
508         GLuint getRenderbufferName() const;
509
510         void setFramebufferReadBuffer(GLenum buf);
511         void setFramebufferDrawBuffers(GLsizei n, const GLenum *bufs);
512         GLuint getReadFramebufferColorIndex() const;
513
514         GLuint getActiveQuery(GLenum target) const;
515
516         GLuint getArrayBufferName() const;
517         GLuint getElementArrayBufferName() const;
518
519         void setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled);
520         void setVertexAttribDivisor(unsigned int attribNum, GLuint divisor);
521         const VertexAttribute &getVertexAttribState(unsigned int attribNum) const;
522         void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,
523                                   bool normalized, GLsizei stride, const void *pointer);
524         const void *getVertexAttribPointer(unsigned int attribNum) const;
525
526         const VertexAttributeArray &getVertexArrayAttributes();
527         // Context attribute current values can be queried independently from VAO current values
528         const VertexAttributeArray &getCurrentVertexAttributes();
529
530         void setUnpackAlignment(GLint alignment);
531         void setUnpackRowLength(GLint rowLength);
532         void setUnpackImageHeight(GLint imageHeight);
533         void setUnpackSkipPixels(GLint skipPixels);
534         void setUnpackSkipRows(GLint skipRows);
535         void setUnpackSkipImages(GLint skipImages);
536         const egl::Image::UnpackInfo& getUnpackInfo() const;
537
538         void setPackAlignment(GLint alignment);
539         void setPackRowLength(GLint rowLength);
540         void setPackImageHeight(GLint imageHeight);
541         void setPackSkipPixels(GLint skipPixels);
542         void setPackSkipRows(GLint skipRows);
543         void setPackSkipImages(GLint skipImages);
544
545         // These create and destroy methods are merely pass-throughs to
546         // ResourceManager, which owns these object types
547         GLuint createBuffer();
548         GLuint createShader(GLenum type);
549         GLuint createProgram();
550         GLuint createTexture();
551         GLuint createRenderbuffer();
552         GLuint createSampler();
553         GLsync createFenceSync(GLenum condition, GLbitfield flags);
554
555         void deleteBuffer(GLuint buffer);
556         void deleteShader(GLuint shader);
557         void deleteProgram(GLuint program);
558         void deleteTexture(GLuint texture);
559         void deleteRenderbuffer(GLuint renderbuffer);
560         void deleteSampler(GLuint sampler);
561         void deleteFenceSync(GLsync fenceSync);
562
563         // Framebuffers are owned by the Context, so these methods do not pass through
564         GLuint createFramebuffer();
565         void deleteFramebuffer(GLuint framebuffer);
566
567         // Fences are owned by the Context
568         GLuint createFence();
569         void deleteFence(GLuint fence);
570
571         // Queries are owned by the Context
572         GLuint createQuery();
573         void deleteQuery(GLuint query);
574
575         // Vertex arrays are owned by the Context
576         GLuint createVertexArray();
577         void deleteVertexArray(GLuint array);
578
579         // Transform feedbacks are owned by the Context
580         GLuint createTransformFeedback();
581         void deleteTransformFeedback(GLuint transformFeedback);
582
583         void bindArrayBuffer(GLuint buffer);
584         void bindElementArrayBuffer(GLuint buffer);
585         void bindCopyReadBuffer(GLuint buffer);
586         void bindCopyWriteBuffer(GLuint buffer);
587         void bindPixelPackBuffer(GLuint buffer);
588         void bindPixelUnpackBuffer(GLuint buffer);
589         void bindTransformFeedbackBuffer(GLuint buffer);
590         void bindTexture2D(GLuint texture);
591         void bindTextureCubeMap(GLuint texture);
592         void bindTextureExternal(GLuint texture);
593         void bindTexture3D(GLuint texture);
594         void bindTexture2DArray(GLuint texture);
595         void bindReadFramebuffer(GLuint framebuffer);
596         void bindDrawFramebuffer(GLuint framebuffer);
597         void bindRenderbuffer(GLuint renderbuffer);
598         void bindVertexArray(GLuint array);
599         void bindGenericUniformBuffer(GLuint buffer);
600         void bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size);
601         void bindGenericTransformFeedbackBuffer(GLuint buffer);
602         void bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size);
603         void bindTransformFeedback(GLuint transformFeedback);
604         bool bindSampler(GLuint unit, GLuint sampler);
605         void useProgram(GLuint program);
606
607         void beginQuery(GLenum target, GLuint query);
608         void endQuery(GLenum target);
609
610         void setFramebufferZero(Framebuffer *framebuffer);
611
612         void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
613
614         void setVertexAttrib(GLuint index, const GLfloat *values);
615         void setVertexAttrib(GLuint index, const GLint *values);
616         void setVertexAttrib(GLuint index, const GLuint *values);
617
618         Buffer *getBuffer(GLuint handle) const;
619         Fence *getFence(GLuint handle) const;
620         FenceSync *getFenceSync(GLsync handle) const;
621         Shader *getShader(GLuint handle) const;
622         Program *getProgram(GLuint handle) const;
623         virtual Texture *getTexture(GLuint handle) const;
624         Framebuffer *getFramebuffer(GLuint handle) const;
625         virtual Renderbuffer *getRenderbuffer(GLuint handle) const;
626         Query *getQuery(GLuint handle) const;
627         VertexArray *getVertexArray(GLuint array) const;
628         VertexArray *getCurrentVertexArray() const;
629         bool isVertexArray(GLuint array) const;
630         TransformFeedback *getTransformFeedback(GLuint transformFeedback) const;
631         TransformFeedback *getTransformFeedback() const;
632         Sampler *getSampler(GLuint sampler) const;
633         bool isSampler(GLuint sampler) const;
634
635         Buffer *getArrayBuffer() const;
636         Buffer *getElementArrayBuffer() const;
637         Buffer *getCopyReadBuffer() const;
638         Buffer *getCopyWriteBuffer() const;
639         Buffer *getPixelPackBuffer() const;
640         Buffer *getPixelUnpackBuffer() const;
641         Buffer *getGenericUniformBuffer() const;
642         const GLvoid* getPixels(const GLvoid* data) const;
643         bool getBuffer(GLenum target, es2::Buffer **buffer) const;
644         Program *getCurrentProgram() const;
645         Texture2D *getTexture2D() const;
646         Texture3D *getTexture3D() const;
647         Texture2DArray *getTexture2DArray() const;
648         TextureCubeMap *getTextureCubeMap() const;
649         TextureExternal *getTextureExternal() const;
650         Texture *getSamplerTexture(unsigned int sampler, TextureType type) const;
651         Framebuffer *getReadFramebuffer() const;
652         Framebuffer *getDrawFramebuffer() const;
653
654         bool getFloatv(GLenum pname, GLfloat *params) const;
655         template<typename T> bool getIntegerv(GLenum pname, T *params) const;
656         bool getBooleanv(GLenum pname, GLboolean *params) const;
657         template<typename T> bool getTransformFeedbackiv(GLuint index, GLenum pname, T *param) const;
658         template<typename T> bool getUniformBufferiv(GLuint index, GLenum pname, T *param) const;
659         void samplerParameteri(GLuint sampler, GLenum pname, GLint param);
660         void samplerParameterf(GLuint sampler, GLenum pname, GLfloat param);
661         GLint getSamplerParameteri(GLuint sampler, GLenum pname);
662         GLfloat getSamplerParameterf(GLuint sampler, GLenum pname);
663
664         bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const;
665
666         bool hasZeroDivisor() const;
667
668         void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);
669         void clear(GLbitfield mask);
670         void clearColorBuffer(GLint drawbuffer, const GLint *value);
671         void clearColorBuffer(GLint drawbuffer, const GLuint *value);
672         void clearColorBuffer(GLint drawbuffer, const GLfloat *value);
673         void clearDepthBuffer(const GLfloat value);
674         void clearStencilBuffer(const GLint value);
675         void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount = 1);
676         void drawElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount = 1);
677         void finish() override;
678         void flush();
679
680         void recordInvalidEnum();
681         void recordInvalidValue();
682         void recordInvalidOperation();
683         void recordOutOfMemory();
684         void recordInvalidFramebufferOperation();
685
686         GLenum getError();
687
688         static int getSupportedMultisampleCount(int requested);
689
690         void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
691                              GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
692                              GLbitfield mask, bool filter, bool allowPartialDepthStencilBlit);
693
694         void bindTexImage(egl::Surface *surface) override;
695         EGLenum validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel) override;
696         egl::Image *createSharedImage(EGLenum target, GLuint name, GLuint textureLevel) override;
697         egl::Image *getSharedImage(GLeglImageOES image);
698
699         Device *getDevice();
700
701         const GLubyte *getExtensions(GLuint index, GLuint *numExt = nullptr) const;
702
703 private:
704         virtual ~Context();
705
706         void applyScissor(int width, int height);
707         bool applyRenderTarget();
708         void applyState(GLenum drawMode);
709         GLenum applyVertexBuffer(GLint base, GLint first, GLsizei count, GLsizei instanceId);
710         GLenum applyIndexBuffer(const void *indices, GLuint start, GLuint end, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
711         void applyShaders();
712         void applyTextures();
713         void applyTextures(sw::SamplerType type);
714         void applyTexture(sw::SamplerType type, int sampler, Texture *texture);
715         void clearColorBuffer(GLint drawbuffer, void *value, sw::Format format);
716
717         void detachBuffer(GLuint buffer);
718         void detachTexture(GLuint texture);
719         void detachFramebuffer(GLuint framebuffer);
720         void detachRenderbuffer(GLuint renderbuffer);
721         void detachSampler(GLuint sampler);
722
723         bool cullSkipsDraw(GLenum drawMode);
724         bool isTriangleMode(GLenum drawMode);
725
726         Query *createQuery(GLuint handle, GLenum type);
727
728         const EGLint clientVersion;
729         const egl::Config *const config;
730
731         State mState;
732
733         gl::BindingPointer<Texture2D> mTexture2DZero;
734         gl::BindingPointer<Texture3D> mTexture3DZero;
735         gl::BindingPointer<Texture2DArray> mTexture2DArrayZero;
736         gl::BindingPointer<TextureCubeMap> mTextureCubeMapZero;
737         gl::BindingPointer<TextureExternal> mTextureExternalZero;
738
739         gl::NameSpace<Framebuffer> mFramebufferNameSpace;
740         gl::NameSpace<Fence, 0> mFenceNameSpace;
741         gl::NameSpace<Query> mQueryNameSpace;
742         gl::NameSpace<VertexArray> mVertexArrayNameSpace;
743         gl::NameSpace<TransformFeedback> mTransformFeedbackNameSpace;
744
745         VertexDataManager *mVertexDataManager;
746         IndexDataManager *mIndexDataManager;
747
748         // Recorded errors
749         bool mInvalidEnum;
750         bool mInvalidValue;
751         bool mInvalidOperation;
752         bool mOutOfMemory;
753         bool mInvalidFramebufferOperation;
754
755         bool mHasBeenCurrent;
756
757         unsigned int mAppliedProgramSerial;
758
759         // state caching flags
760         bool mDepthStateDirty;
761         bool mMaskStateDirty;
762         bool mBlendStateDirty;
763         bool mStencilStateDirty;
764         bool mPolygonOffsetStateDirty;
765         bool mSampleStateDirty;
766         bool mFrontFaceDirty;
767         bool mDitherStateDirty;
768
769         Device *device;
770         ResourceManager *mResourceManager;
771 };
772 }
773
774 #endif   // INCLUDE_CONTEXT_H_