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