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