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