OSDN Git Service

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