• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef _GL_CLIENT_STATE_H_
17 #define _GL_CLIENT_STATE_H_
18 
19 #define GL_API
20 #ifndef ANDROID
21 #define GL_APIENTRY
22 #define GL_APIENTRYP
23 #endif
24 
25 #ifdef GFXSTREAM
26 #include "StateTrackingSupport.h"
27 #endif
28 
29 #include "TextureSharedData.h"
30 
31 #include <GLES/gl.h>
32 #include <GLES/glext.h>
33 #include <GLES2/gl2.h>
34 #include <GLES2/gl2ext.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include "ErrorLog.h"
39 #include "codec_defs.h"
40 
41 #include <vector>
42 #include <map>
43 #include <set>
44 #include <string>
45 
46 // Caps of host driver that make it easy to validate stuff
47 struct HostDriverCaps {
48     // ES 2
49     int max_vertex_attribs;
50     int max_combined_texture_image_units;
51     int max_color_attachments;
52 
53     int max_texture_size;
54     int max_texture_size_cube_map;
55     int max_renderbuffer_size;
56 
57     // ES 3.0
58     int max_draw_buffers;
59 
60     int ubo_offset_alignment;
61     int max_uniform_buffer_bindings;
62     int max_transform_feedback_separate_attribs;
63 
64     int max_texture_size_3d;
65     int max_array_texture_layers;
66 
67     // ES 3.1
68     int max_atomic_counter_buffer_bindings;
69     int max_shader_storage_buffer_bindings;
70     int max_vertex_attrib_bindings;
71     int max_vertex_attrib_stride;
72     int ssbo_offset_alignment;
73 };
74 
75 // Tracking framebuffer objects:
76 // which framebuffer is bound,
77 // and which texture names
78 // are currently bound to which attachment points.
79 struct FboProps {
80     GLuint name;
81     bool previouslyBound;
82     bool completenessDirty;
83     GLenum cachedCompleteness;
84     std::vector<GLuint> colorAttachmenti_textures;
85     std::vector<GLint> colorAttachmenti_texture_levels;
86     std::vector<GLint> colorAttachmenti_texture_layers;
87 
88     GLint depthAttachment_texture_level;
89     GLint depthAttachment_texture_layer;
90     GLint stencilAttachment_texture_level;
91     GLint stencilAttachment_texture_layer;
92 
93     GLuint depthAttachment_texture;
94     GLuint stencilAttachment_texture;
95     GLuint depthstencilAttachment_texture;
96 
97     std::vector<bool> colorAttachmenti_hasTex;
98     bool depthAttachment_hasTexObj;
99     bool stencilAttachment_hasTexObj;
100     bool depthstencilAttachment_hasTexObj;
101 
102     std::vector<GLuint> colorAttachmenti_rbos;
103     GLuint depthAttachment_rbo;
104     GLuint stencilAttachment_rbo;
105     GLuint depthstencilAttachment_rbo;
106 
107     std::vector<bool> colorAttachmenti_hasRbo;
108     bool depthAttachment_hasRbo;
109     bool stencilAttachment_hasRbo;
110     bool depthstencilAttachment_hasRbo;
111 
112     GLuint defaultWidth;
113     GLuint defaultHeight;
114 };
115 
116 // Enum for describing whether a framebuffer attachment
117 // is a texture or renderbuffer.
118 enum FboAttachmentType {
119     FBO_ATTACHMENT_RENDERBUFFER = 0,
120     FBO_ATTACHMENT_TEXTURE = 1,
121     FBO_ATTACHMENT_NONE = 2
122 };
123 
124 // Tracking FBO format
125 struct FboFormatInfo {
126     FboAttachmentType type;
127     GLenum rb_format;
128     GLsizei rb_multisamples;
129     bool rb_external;
130 
131     GLint tex_internalformat;
132     GLenum tex_format;
133     GLenum tex_type;
134     GLsizei tex_multisamples;
135     GLint tex_level;
136     GLint tex_layer;
137     bool tex_external;
138 };
139 
140 class GLClientState {
141 public:
142     // TODO: Unify everything in here
143     typedef enum {
144         Buffer,
145         TransformFeedback,
146         Sampler,
147         Query,
148     } ObjectType;
149 
150     typedef enum {
151         VERTEX_LOCATION = 0,
152         NORMAL_LOCATION = 1,
153         COLOR_LOCATION = 2,
154         POINTSIZE_LOCATION = 3,
155         TEXCOORD0_LOCATION = 4,
156         TEXCOORD1_LOCATION = 5,
157         TEXCOORD2_LOCATION = 6,
158         TEXCOORD3_LOCATION = 7,
159         TEXCOORD4_LOCATION = 8,
160         TEXCOORD5_LOCATION = 9,
161         TEXCOORD6_LOCATION = 10,
162         TEXCOORD7_LOCATION = 11,
163         MATRIXINDEX_LOCATION = 12,
164         WEIGHT_LOCATION = 13,
165         LAST_LOCATION = 14
166     } StateLocation;
167 
168     typedef struct {
169         GLint enabled;
170         GLint size;
171         GLenum type;
172         GLsizei stride;
173         void *data;
174         GLuint reloffset;
175         GLuint bufferObject;
176         GLenum glConst;
177         unsigned int elementSize;
178         bool enableDirty;  // true if any enable state has changed since last draw
179         bool normalized;
180         GLuint divisor;
181         bool isInt;
182         int bindingindex;
183     } VertexAttribState;
184 
185     struct BufferBinding {
186         GLintptr offset;
187         GLintptr stride;
188         GLintptr effectiveStride;
189         GLsizeiptr size;
190         GLuint buffer;
191         GLuint divisor;
192         GLint vertexAttribLoc;
193     };
194 
195     typedef std::vector<VertexAttribState> VertexAttribStateVector;
196     typedef std::vector<BufferBinding> VertexAttribBindingVector;
197 
198     struct VAOState {
VAOStateVAOState199         VAOState(GLuint ibo, int nLoc, int nBindings) :
200             attribState(nLoc),
201             bindingState(nBindings),
202             element_array_buffer_binding(ibo),
203             element_array_buffer_binding_lastEncode(ibo) { }
204         VertexAttribStateVector attribState;
205         VertexAttribBindingVector bindingState;
206         GLuint element_array_buffer_binding;
207         GLuint element_array_buffer_binding_lastEncode;
208         int attributesNeedingUpdateForDraw[CODEC_MAX_VERTEX_ATTRIBUTES];
209         int numAttributesNeedingUpdateForDraw;
210     };
211 
212     typedef std::map<GLuint, VAOState> VAOStateMap;
213     struct VAOStateRef {
VAOStateRefVAOStateRef214         VAOStateRef() { }
VAOStateRefVAOStateRef215         VAOStateRef(
216                 VAOStateMap::iterator iter) : it(iter) { }
vaoStateVAOStateRef217         VAOState& vaoState() { return it->second; }
218         VertexAttribState& operator[](size_t k) { return it->second.attribState[k]; }
bufferBindingVAOStateRef219         BufferBinding& bufferBinding(size_t k) { return it->second.bindingState[k]; }
bufferBindingsVAOStateRef220         VertexAttribBindingVector& bufferBindings() { return it->second.bindingState; }
bufferBindings_constVAOStateRef221         const VertexAttribBindingVector& bufferBindings_const() const { return it->second.bindingState; }
vaoIdVAOStateRef222         GLuint vaoId() const { return it->first; }
iboIdVAOStateRef223         GLuint& iboId() { return it->second.element_array_buffer_binding; }
iboIdLastEncodeVAOStateRef224         GLuint& iboIdLastEncode() { return it->second.element_array_buffer_binding_lastEncode; }
225         VAOStateMap::iterator it;
226     };
227 
228     typedef struct {
229         int unpack_alignment;
230 
231         int unpack_row_length;
232         int unpack_image_height;
233         int unpack_skip_pixels;
234         int unpack_skip_rows;
235         int unpack_skip_images;
236 
237         int pack_alignment;
238 
239         int pack_row_length;
240         int pack_skip_pixels;
241         int pack_skip_rows;
242     } PixelStoreState;
243 
244     enum {
245         MAX_TEXTURE_UNITS = 256,
246     };
247 
248 public:
249     GLClientState();
250     GLClientState(int majorVersion, int minorVersion);
251     ~GLClientState();
nLocations()252     int nLocations() { return CODEC_MAX_VERTEX_ATTRIBUTES; }
pixelStoreState()253     const PixelStoreState *pixelStoreState() { return &m_pixelStore; }
254     int setPixelStore(GLenum param, GLint value);
currentVertexArrayObject()255     GLuint currentVertexArrayObject() const { return m_currVaoState.vaoId(); }
currentVertexBufferBindings()256     const VertexAttribBindingVector& currentVertexBufferBindings() const {
257         return m_currVaoState.bufferBindings_const();
258     }
259 
currentArrayVbo()260     GLuint currentArrayVbo() { return m_arrayBuffer; }
currentIndexVbo()261     GLuint currentIndexVbo() { return m_currVaoState.iboId(); }
262     void enable(int location, int state);
263     // Vertex array objects and vertex attributes
264     void addVertexArrayObjects(GLsizei n, GLuint* arrays);
265     void removeVertexArrayObjects(GLsizei n, const GLuint* arrays);
266     void addVertexArrayObject(GLuint name);
267     void removeVertexArrayObject(GLuint name);
268     void setVertexArrayObject(GLuint vao);
269     bool isVertexArrayObject(GLuint vao) const;
270     void setVertexAttribState(int  location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data, bool isInt = false);
271     void setVertexBindingDivisor(int bindingindex, GLuint divisor);
272     const BufferBinding& getCurrAttributeBindingInfo(int attribindex);
273     void setVertexAttribBinding(int attribindex, int bindingindex);
274     void setVertexAttribFormat(int location, int size, GLenum type, GLboolean normalized, GLuint reloffset, bool isInt = false);
275     void getVBOUsage(bool* hasClientArrays, bool* hasVBOs);
276     const VertexAttribState& getState(int location);
277     const VertexAttribState& getStateAndEnableDirty(int location, bool *enableChanged);
278     void updateEnableDirtyArrayForDraw();
279     VAOState& currentVaoState();
280     int getLocation(GLenum loc);
setActiveTexture(int texUnit)281     void setActiveTexture(int texUnit) {m_activeTexture = texUnit; };
getActiveTexture()282     int getActiveTexture() const { return m_activeTexture; }
283 
284     void addBuffer(GLuint id);
285     void removeBuffer(GLuint id);
286     bool bufferIdExists(GLuint id) const;
287     void unBindBuffer(GLuint id);
288 
289     void setBufferHostMapDirty(GLuint id, bool dirty);
290     bool isBufferHostMapDirty(GLuint id) const;
291 
292     void setExistence(ObjectType type, bool exists, GLsizei count, const GLuint* ids);
293     bool queryExistence(ObjectType type, GLuint id) const;
294     bool samplerExists(GLuint id) const;
295     bool tryBind(GLenum target, GLuint id);
296     bool isBoundTargetValid(GLenum target);
297     bool isQueryBound(GLenum target);
298     bool isQueryObjectActive(GLuint id);
299     void setLastQueryTarget(GLenum target, GLuint id);
300     GLenum getLastQueryTarget(GLuint id);
301 
302     static void onFenceCreated(GLsync sync);
303     static void onFenceDestroyed(GLsync sync);
304     static bool fenceExists(GLsync sync);
305 
306     void setBoundPixelPackBufferDirtyForHostMap();
307     void setBoundTransformFeedbackBuffersDirtyForHostMap();
308     void setBoundShaderStorageBuffersDirtyForHostMap();
309     void setBoundAtomicCounterBuffersDirtyForHostMap();
310 
311     int bindBuffer(GLenum target, GLuint id);
312     void bindIndexedBuffer(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size, GLintptr stride, GLintptr effectiveStride);
313     int getMaxIndexedBufferBindings(GLenum target) const;
314     bool isNonIndexedBindNoOp(GLenum target, GLuint buffer);
315     bool isIndexedBindNoOp(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size, GLintptr stride, GLintptr effectiveStride);
316 
317     int getMaxTextureSize() const;
318     int getMaxTextureSize3D() const;
319     int getMaxTextureSizeCubeMap() const;
320     int getLog2MaxTextureSize() const;
321 
322     void postDraw();
323     void postReadPixels();
324     void postDispatchCompute();
325 
326     bool shouldSkipHostMapBuffer(GLenum target);
327     void onHostMappedBuffer(GLenum target);
328 
329     int getBuffer(GLenum target);
330     GLuint getLastEncodedBufferBind(GLenum target);
331     void setLastEncodedBufferBind(GLenum target, GLuint id);
332 
333     size_t pixelDataSize(GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, int pack) const;
334     size_t pboNeededDataSize(GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, int pack) const;
335     size_t clearBufferNumElts(GLenum buffer) const;
336     void getPackingOffsets2D(GLsizei width, GLsizei height, GLenum format, GLenum type, int* bpp, int* startOffset, int* pixelRowSize, int* totalRowSize, int* skipRows) const;
337     void getUnpackingOffsets2D(GLsizei width, GLsizei height, GLenum format, GLenum type, int* bpp, int* startOffset, int* pixelRowSize, int* totalRowSize, int* skipRows) const;
338     void getUnpackingOffsets3D(GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, int* bpp, int* startOffset, int* pixelRowSize, int* totalRowSize, int* pixelImageSize, int* totalImageSize, int* skipRows, int* skipImages) const;
339 
setCurrentProgram(GLint program)340     void setCurrentProgram(GLint program) { m_currentProgram = program; }
setCurrentShaderProgram(GLint program)341     void setCurrentShaderProgram(GLint program) { m_currentShaderProgram = program; }
currentProgram()342     GLint currentProgram() const { return m_currentProgram; }
currentShaderProgram()343     GLint currentShaderProgram() const { return m_currentShaderProgram; }
344 
345     struct UniformBlockInfoKey {
346         GLuint program;
347         GLuint uniformBlockIndex;
348     };
349     struct UniformBlockInfoKeyCompare {
operatorUniformBlockInfoKeyCompare350         bool operator() (const UniformBlockInfoKey& a,
351                          const UniformBlockInfoKey& b) const {
352             if (a.program != b.program) return a.program < b.program;
353             if (a.uniformBlockIndex != b.uniformBlockIndex) return a.uniformBlockIndex < b.uniformBlockIndex;
354             return false;
355         }
356     };
357     struct UniformBlockUniformInfo {
358         size_t numActiveUniforms;
359     };
360 
361     typedef std::map<UniformBlockInfoKey, UniformBlockUniformInfo, UniformBlockInfoKeyCompare> UniformBlockInfoMap;
362     UniformBlockInfoMap m_uniformBlockInfoMap;
363 
364     void setNumActiveUniformsInUniformBlock(GLuint program, GLuint uniformBlockIndex, GLint numActiveUniforms);
365     size_t numActiveUniformsInUniformBlock(GLuint program, GLuint uniformBlockIndex) const;
366 
367     typedef std::map<GLuint, GLuint> ProgramPipelineMap;
368     typedef ProgramPipelineMap::iterator ProgramPipelineIterator;
369     void associateProgramWithPipeline(GLuint program, GLuint pipeline);
370     ProgramPipelineIterator programPipelineBegin();
371     ProgramPipelineIterator programPipelineEnd();
372 
373     /* OES_EGL_image_external
374      *
375      * These functions manipulate GL state which interacts with the
376      * OES_EGL_image_external extension, to support client-side emulation on
377      * top of host implementations that don't have it.
378      *
379      * Most of these calls should only be used with TEXTURE_2D or
380      * TEXTURE_EXTERNAL_OES texture targets; TEXTURE_CUBE_MAP or other extension
381      * targets should bypass this. An exception is bindTexture(), which should
382      * see all glBindTexture() calls for any target.
383      */
384 
385     // glActiveTexture(GL_TEXTURE0 + i)
386     // Sets the active texture unit. Up to MAX_TEXTURE_UNITS are supported.
387     GLenum setActiveTextureUnit(GLenum texture);
388     GLenum getActiveTextureUnit() const;
389 
390     // glEnable(GL_TEXTURE_(2D|EXTERNAL_OES))
391     void enableTextureTarget(GLenum target);
392 
393     // glDisable(GL_TEXTURE_(2D|EXTERNAL_OES))
394     void disableTextureTarget(GLenum target);
395 
396     bool bindSampler(GLuint unit, GLuint sampler);
397     bool isSamplerBindNoOp(GLuint unit, GLuint sampler);
398     void onDeleteSamplers(GLsizei n, const GLuint* samplers);
399 
400     // Implements the target priority logic:
401     // * Return GL_TEXTURE_EXTERNAL_OES if enabled, else
402     // * Return GL_TEXTURE_2D if enabled, else
403     // * Return the allDisabled value.
404     // For some cases passing GL_TEXTURE_2D for allDisabled makes callee code
405     // simpler; for other cases passing a recognizable enum like GL_ZERO or
406     // GL_INVALID_ENUM is appropriate.
407     GLenum getPriorityEnabledTarget(GLenum allDisabled) const;
408 
409     // glBindTexture(GL_TEXTURE_*, ...)
410     // Set the target binding of the active texture unit to texture. Returns
411     // GL_NO_ERROR on success or GL_INVALID_OPERATION if the texture has
412     // previously been bound to a different target. If firstUse is not NULL,
413     // it is set to indicate whether this is the first use of the texture.
414     // For accurate error detection, bindTexture should be called for *all*
415     // targets, not just 2D and EXTERNAL_OES.
416     GLenum bindTexture(GLenum target, GLuint texture, GLboolean* firstUse);
417     void setBoundEGLImage(GLenum target, GLeglImageOES image, int width, int height);
418 
419     // Return the texture currently bound to GL_TEXTURE_(2D|EXTERNAL_OES).
420     GLuint getBoundTexture(GLenum target) const;
421     // Return bound framebuffer for target
422     GLuint getBoundFramebuffer(GLenum target) const;
423 
424     // Check framebuffer completeness
425     GLenum checkFramebufferCompleteness(GLenum target);
426     // |currentSamples|: threads through the current sample count of attachments so far,
427     // for validating consistent number of samples across attachments
428     GLenum checkFramebufferAttachmentCompleteness(GLenum target, GLenum attachment, int* currentSamples) const;
429 
430     // Other publicly-visible texture queries
431     GLenum queryTexLastBoundTarget(GLuint name) const;
432     GLenum queryTexFormat(GLuint name) const;
433     GLint queryTexInternalFormat(GLuint name) const;
434     GLsizei queryTexWidth(GLsizei level, GLuint name) const;
435     GLsizei queryTexHeight(GLsizei level, GLuint name) const;
436     GLsizei queryTexDepth(GLsizei level, GLuint name) const;
437     bool queryTexEGLImageBacked(GLuint name) const;
438 
439     // For AMD GPUs, it is easy for the emulator to segfault
440     // (esp. in dEQP) when a cube map is defined using glCopyTexImage2D
441     // and uses GL_LUMINANCE as internal format.
442     // In particular, the segfault happens when negative components of
443     // cube maps are defined before positive ones,
444     // This procedure checks internal state to see if we have defined
445     // the positive component of a cube map already. If not, it returns
446     // which positive component needs to be defined first.
447     // If there is no need for the extra definition, 0 is returned.
448     GLenum copyTexImageLuminanceCubeMapAMDWorkaround(GLenum target, GLint level,
449                                                      GLenum internalformat);
450 
451     // Tracks the format of the currently bound texture.
452     // This is to pass dEQP tests for fbo completeness.
453     void setBoundTextureInternalFormat(GLenum target, GLint format);
454     void setBoundTextureFormat(GLenum target, GLenum format);
455     void setBoundTextureType(GLenum target, GLenum type);
456     void setBoundTextureDims(GLenum target, GLenum cubetarget, GLsizei level, GLsizei width, GLsizei height, GLsizei depth);
457     void setBoundTextureSamples(GLenum target, GLsizei samples);
458     void addTextureCubeMapImage(GLenum stateTarget, GLenum cubeTarget);
459 
460     // glTexStorage2D disallows any change in texture format after it is set for a particular texture.
461     void setBoundTextureImmutableFormat(GLenum target);
462     bool isBoundTextureImmutableFormat(GLenum target) const;
463     bool isBoundTextureComplete(GLenum target) const;
464 
465     // glDeleteTextures(...)
466     // Remove references to the to-be-deleted textures.
467     void deleteTextures(GLsizei n, const GLuint* textures);
468 
469     // Render buffer objects
470     void addRenderbuffers(GLsizei n, GLuint* renderbuffers);
471     void removeRenderbuffers(GLsizei n, const GLuint* renderbuffers);
472     bool usedRenderbufferName(GLuint name) const;
473     void bindRenderbuffer(GLenum target, GLuint name);
474     GLuint boundRenderbuffer() const;
475     void setBoundRenderbufferFormat(GLenum format);
476     void setBoundRenderbufferSamples(GLsizei samples);
477     void setBoundRenderbufferDimensions(GLsizei width, GLsizei height);
478     void setBoundRenderbufferEGLImageBacked();
479 
480     // Frame buffer objects
481     void addFramebuffers(GLsizei n, GLuint* framebuffers);
482     void removeFramebuffers(GLsizei n, const GLuint* framebuffers);
483     bool usedFramebufferName(GLuint name) const;
484     void bindFramebuffer(GLenum target, GLuint name);
485     void setCheckFramebufferStatus(GLenum target, GLenum status);
486     void setFramebufferParameter(GLenum target, GLenum pname, GLint param);
487     GLenum getCheckFramebufferStatus(GLenum target) const;
488     GLuint boundFramebuffer(GLenum target) const;
489 
490     // Texture object -> FBO
491     void attachTextureObject(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer);
492     GLuint getFboAttachmentTextureId(GLenum target, GLenum attachment) const;
493 
494     // RBO -> FBO
495     void detachRbo(GLuint renderbuffer);
496     void detachRboFromFbo(GLenum target, GLenum attachment, GLuint renderbuffer);
497     void attachRbo(GLenum target, GLenum attachment, GLuint renderbuffer);
498     GLuint getFboAttachmentRboId(GLenum target, GLenum attachment) const;
499 
500     // FBO attachments in general
501     bool attachmentHasObject(GLenum target, GLenum attachment) const;
502     GLuint objectOfAttachment(GLenum target, GLenum attachment) const;
503 
504     // Dirty FBO completeness
505     void setFboCompletenessDirtyForTexture(GLuint texture);
506     void setFboCompletenessDirtyForRbo(GLuint rbo_name);
507 
508     // Transform feedback state
509     void setTransformFeedbackActive(bool active);
510     void setTransformFeedbackUnpaused(bool unpaused);
511     void setTransformFeedbackVaryingsCountForLinking(uint32_t count);
512     bool getTransformFeedbackActive() const;
513     bool getTransformFeedbackUnpaused() const;
514     bool getTransformFeedbackActiveUnpaused() const;
515     uint32_t getTransformFeedbackVaryingsCountForLinking() const;
516 
517     // Stencil state
518     void stencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask);
519     void stencilMaskSeparate(GLenum face, GLuint mask);
520     void stencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);
521 
522     void setTextureData(SharedTextureDataMap* sharedTexData);
523     void setRenderbufferInfo(RenderbufferInfo* rbInfo);
524     void setSamplerInfo(SamplerInfo* samplerInfo);
525 
526     bool compressedTexImageSizeCompatible(GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLsizei imageSize);
527     // set eglsurface property on default framebuffer
528     // if coming from eglMakeCurrent
529     void fromMakeCurrent();
530     // set indexed buffer state.
531     // We need to query the underlying OpenGL to get
532     // accurate values for indexed buffers
533     // and # render targets.
534     void initFromCaps(
535         const HostDriverCaps& caps);
536     bool needsInitFromCaps() const;
537     void setExtensions(const std::string& extensions);
538     bool hasExtension(const char* ext) const;
539 
540     // Queries the format backing the current framebuffer.
541     // Type differs depending on whether the attachment
542     // is a texture or renderbuffer.
543     void getBoundFramebufferFormat(
544             GLenum target,
545             GLenum attachment,
546             FboFormatInfo* res_info) const;
547     FboAttachmentType getBoundFramebufferAttachmentType(
548             GLenum target,
549             GLenum attachment) const;
550     int getMaxColorAttachments() const;
551     int getMaxDrawBuffers() const;
552 
553     // Uniform/attribute validation info
554     UniformValidationInfo currentUniformValidationInfo;
555     AttribValidationInfo currentAttribValidationInfo;;
556 
557     // Uniform validation api
558     void validateUniform(bool isFloat, bool isUnsigned, GLint columns, GLint rows, GLint location, GLsizei count, GLenum* err);
559     // Attrib validation
560     bool isAttribIndexUsedByProgram(int attribIndex);
561 
562     // Fast access to some enables and stencil related glGet's
563     bool state_GL_STENCIL_TEST;
564     GLenum state_GL_STENCIL_FUNC;
565     unsigned int state_GL_STENCIL_VALUE_MASK;
566     int state_GL_STENCIL_REF;
567     GLenum state_GL_STENCIL_FAIL;
568     GLenum state_GL_STENCIL_PASS_DEPTH_FAIL;
569     GLenum state_GL_STENCIL_PASS_DEPTH_PASS;
570     GLenum state_GL_STENCIL_BACK_FUNC;
571     unsigned int state_GL_STENCIL_BACK_VALUE_MASK;
572     int state_GL_STENCIL_BACK_REF;
573     GLenum state_GL_STENCIL_BACK_FAIL;
574     GLenum state_GL_STENCIL_BACK_PASS_DEPTH_FAIL;
575     GLenum state_GL_STENCIL_BACK_PASS_DEPTH_PASS;
576     unsigned int state_GL_STENCIL_WRITEMASK;
577     unsigned int state_GL_STENCIL_BACK_WRITEMASK;
578     int state_GL_STENCIL_CLEAR_VALUE;
579 private:
580     void init();
581     bool m_initialized;
582     PixelStoreState m_pixelStore;
583 
584 #ifdef GFXSTREAM
585     using DirtyMap = PredicateMap<uint32_t, true>;
586 
587     ExistenceMap mBufferIds;
588     ExistenceMap mTransformFeedbackIds;
589     SamplerInfo* mSamplerInfo;
590     ExistenceMap mQueryIds;
591     LastQueryTargetInfo mLastQueryTargets;
592 
593     // Bound query target validity and tracking
594     struct BoundTargetInfo {
595         GLuint id;
596         bool valid;
597     };
598 
599     // Transform feedback
600     BoundTargetInfo mBoundTransformFeedbackValidity;
601 
602     // Queries
603     // GL_ANY_SAMPLES_PASSED
604     BoundTargetInfo mBoundQueryValidity_AnySamplesPassed;
605     // GL_ANY_SAMPLES_PASSED_CONSERVATIVE
606     BoundTargetInfo mBoundQueryValidity_AnySamplesPassedConservative;
607     // GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
608     BoundTargetInfo mBoundQueryValidity_TransformFeedbackPrimitivesWritten;
609 
610     // Dirty maps
611     DirtyMap mHostMappedBufferDirty;
612 #else
613     std::set<GLuint> mBufferIds;
614 #endif
615 
616     // GL_ARRAY_BUFFER_BINDING is separate from VAO state
617     GLuint m_arrayBuffer;
618     GLuint m_arrayBuffer_lastEncode;
619     VAOStateMap m_vaoMap;
620     VAOStateRef m_currVaoState;
621 
622     uint16_t m_attribEnableCache;
623     uint16_t m_vaoAttribBindingCacheInvalid;
624     uint16_t m_vaoAttribBindingHasClientArrayCache;
625     uint16_t m_vaoAttribBindingHasVboCache;
626     uint8_t m_noClientArraysCache;
627 
628     // Other buffer id's, other targets
629     GLuint m_copyReadBuffer;
630     GLuint m_copyWriteBuffer;
631 
632     GLuint m_pixelPackBuffer;
633     GLuint m_pixelUnpackBuffer;
634 
635     GLuint m_transformFeedbackBuffer;
636     GLuint m_uniformBuffer;
637 
638     GLuint m_atomicCounterBuffer;
639     GLuint m_dispatchIndirectBuffer;
640     GLuint m_drawIndirectBuffer;
641     GLuint m_shaderStorageBuffer;
642 
643     bool m_transformFeedbackActive;
644     bool m_transformFeedbackUnpaused;
645     uint32_t m_transformFeedbackVaryingsCountForLinking;
646 
647     HostDriverCaps m_hostDriverCaps;
648     bool m_extensions_set;
649     std::string m_extensions;
650     bool m_has_color_buffer_float_extension;
651     bool m_has_color_buffer_half_float_extension;
652     std::vector<BufferBinding> m_indexedTransformFeedbackBuffers;
653     std::vector<BufferBinding> m_indexedUniformBuffers;
654     std::vector<BufferBinding> m_indexedAtomicCounterBuffers;
655     std::vector<BufferBinding> m_indexedShaderStorageBuffers;
656     int m_log2MaxTextureSize;
657 
658     int m_glesMajorVersion;
659     int m_glesMinorVersion;
660     int m_activeTexture;
661     GLint m_currentProgram;
662     GLint m_currentShaderProgram;
663     ProgramPipelineMap m_programPipelines;
664 
665     enum TextureTarget {
666         TEXTURE_2D = 0,
667         TEXTURE_EXTERNAL = 1,
668         TEXTURE_CUBE_MAP = 2,
669         TEXTURE_2D_ARRAY = 3,
670         TEXTURE_3D = 4,
671         TEXTURE_2D_MULTISAMPLE = 5,
672         TEXTURE_TARGET_COUNT
673     };
674     struct TextureUnit {
675         unsigned int enables;
676         GLuint texture[TEXTURE_TARGET_COUNT];
677         GLuint boundSampler;
678     };
679     struct TextureState {
680         TextureUnit unit[MAX_TEXTURE_UNITS];
681         TextureUnit* activeUnit;
682         // Initialized from shared group.
683         SharedTextureDataMap* textureRecs;
684     };
685     TextureState m_tex;
686 
687     // State tracking of cube map definitions.
688     // Currently used only for driver workarounds
689     // when using GL_LUMINANCE and defining cube maps with
690     // glCopyTexImage2D.
691     struct CubeMapDef {
692         GLuint id;
693         GLenum target;
694         GLint level;
695         GLenum internalformat;
696     };
697     struct CubeMapDefCompare {
operatorCubeMapDefCompare698         bool operator() (const CubeMapDef& a,
699                          const CubeMapDef& b) const {
700             if (a.id != b.id) return a.id < b.id;
701             if (a.target != b.target) return a.target < b.target;
702             if (a.level != b.level) return a.level < b.level;
703             if (a.internalformat != b.internalformat)
704                 return a.internalformat < b.internalformat;
705             return false;
706         }
707     };
708     std::set<CubeMapDef, CubeMapDefCompare> m_cubeMapDefs;
709     void writeCopyTexImageState(GLenum target, GLint level,
710                                 GLenum internalformat);
711     GLenum copyTexImageNeededTarget(GLenum target, GLint level,
712                                     GLenum internalformat);
713 
714     struct RboState {
715         GLuint boundRenderbuffer;
716         // Connects to share group.
717         // Expected that share group lifetime outlives this context.
718         RenderbufferInfo* rboData;
719     };
720     RboState mRboState;
721     void addFreshRenderbuffer(GLuint name);
722 
723     struct FboState {
724         GLuint boundDrawFramebuffer;
725         GLuint boundReadFramebuffer;
726         size_t boundFramebufferIndex;
727         std::map<GLuint, FboProps> fboData;
728         GLenum drawFboCheckStatus;
729         GLenum readFboCheckStatus;
730     };
731     FboState mFboState;
732     void addFreshFramebuffer(GLuint name);
733     FboProps& boundFboProps(GLenum target);
734     const FboProps& boundFboProps_const(GLenum target) const;
735 
736     // Querying framebuffer format
737     GLenum queryRboFormat(GLuint name) const;
738     GLsizei queryRboSamples(GLuint name) const;
739     GLsizei queryRboWidth(GLuint name) const;
740     GLsizei queryRboHeight(GLuint name) const;
741     bool queryRboEGLImageBacked(GLuint name) const;
742     GLenum queryTexType(GLuint name) const;
743     GLsizei queryTexSamples(GLuint name) const;
744 
745     static int compareTexId(const void* pid, const void* prec);
746     TextureRec* addTextureRec(GLuint id, GLenum target);
747     TextureRec* getTextureRec(GLuint id) const;
748 
749 public:
750     bool isTexture(GLuint name) const;
751     bool isTextureWithStorage(GLuint name) const;
752     bool isTextureWithTarget(GLuint name) const;
753     bool isTextureCubeMap(GLuint name) const;
754     bool isRenderbuffer(GLuint name) const;
755     bool isRenderbufferThatWasBound(GLuint name) const;
756 
757     void getClientStatePointer(GLenum pname, GLvoid** params);
758 
759     template <class T>
getVertexAttribParameter(GLuint index,GLenum param,T * ptr)760     int getVertexAttribParameter(GLuint index, GLenum param, T *ptr)
761     {
762         bool handled = true;
763         const VertexAttribState& vertexAttrib = getState(index);
764         const BufferBinding& vertexAttribBufferBinding =
765             m_currVaoState.bufferBindings_const()[vertexAttrib.bindingindex];
766 
767         switch(param) {
768 #define GL_VERTEX_ATTRIB_BINDING 0x82D4
769         case GL_VERTEX_ATTRIB_BINDING:
770             *ptr = (T)vertexAttrib.bindingindex;
771             break;
772 #define GL_VERTEX_ATTRIB_RELATIVE_OFFSET 0x82D5
773         case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
774             *ptr = (T)vertexAttrib.reloffset;
775             break;
776         case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
777             *ptr = (T)(vertexAttribBufferBinding.buffer);
778             break;
779         case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
780             *ptr = (T)(vertexAttrib.enabled);
781             break;
782 #define GL_VERTEX_ATTRIB_ARRAY_INTEGER 0x88FD
783         case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
784             *ptr = (T)(vertexAttrib.isInt);
785             break;
786         case GL_VERTEX_ATTRIB_ARRAY_SIZE:
787             *ptr = (T)(vertexAttrib.size);
788             break;
789         case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
790             *ptr = (T)(vertexAttribBufferBinding.stride);
791             break;
792         case GL_VERTEX_ATTRIB_ARRAY_TYPE:
793             *ptr = (T)(vertexAttrib.type);
794             break;
795         case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
796             *ptr = (T)(vertexAttrib.normalized);
797             break;
798         case GL_CURRENT_VERTEX_ATTRIB:
799             handled = false;
800             break;
801         default:
802             handled = false;
803             ERR("unknown vertex-attrib parameter param %d\n", param);
804         }
805         return handled;
806     }
807 
808     template <class T>
getClientStateParameter(GLenum param,T * out)809     bool getClientStateParameter(GLenum param, T* out)
810     {
811         bool isClientStateParam = false;
812         switch (param) {
813         case GL_CLIENT_ACTIVE_TEXTURE: {
814             GLint tex = getActiveTexture() + GL_TEXTURE0;
815             *out = tex;
816             isClientStateParam = true;
817             break;
818             }
819         case GL_VERTEX_ARRAY_SIZE: {
820             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
821             *out = state.size;
822             isClientStateParam = true;
823             break;
824             }
825         case GL_VERTEX_ARRAY_TYPE: {
826             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
827             *out = state.type;
828             isClientStateParam = true;
829             break;
830             }
831         case GL_VERTEX_ARRAY_STRIDE: {
832             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
833             *out = state.stride;
834             isClientStateParam = true;
835             break;
836             }
837         case GL_COLOR_ARRAY_SIZE: {
838             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
839             *out = state.size;
840             isClientStateParam = true;
841             break;
842             }
843         case GL_COLOR_ARRAY_TYPE: {
844             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
845             *out = state.type;
846             isClientStateParam = true;
847             break;
848             }
849         case GL_COLOR_ARRAY_STRIDE: {
850             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
851             *out = state.stride;
852             isClientStateParam = true;
853             break;
854             }
855         case GL_NORMAL_ARRAY_TYPE: {
856             const GLClientState::VertexAttribState& state = getState(GLClientState::NORMAL_LOCATION);
857             *out = state.type;
858             isClientStateParam = true;
859             break;
860             }
861         case GL_NORMAL_ARRAY_STRIDE: {
862             const GLClientState::VertexAttribState& state = getState(GLClientState::NORMAL_LOCATION);
863             *out = state.stride;
864             isClientStateParam = true;
865             break;
866             }
867         case GL_TEXTURE_COORD_ARRAY_SIZE: {
868             const GLClientState::VertexAttribState& state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
869             *out = state.size;
870             isClientStateParam = true;
871             break;
872             }
873         case GL_TEXTURE_COORD_ARRAY_TYPE: {
874             const GLClientState::VertexAttribState& state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
875             *out = state.type;
876             isClientStateParam = true;
877             break;
878             }
879         case GL_TEXTURE_COORD_ARRAY_STRIDE: {
880             const GLClientState::VertexAttribState& state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
881             *out = state.stride;
882             isClientStateParam = true;
883             break;
884             }
885         case GL_POINT_SIZE_ARRAY_TYPE_OES: {
886             const GLClientState::VertexAttribState& state = getState(GLClientState::POINTSIZE_LOCATION);
887             *out = state.type;
888             isClientStateParam = true;
889             break;
890             }
891         case GL_POINT_SIZE_ARRAY_STRIDE_OES: {
892             const GLClientState::VertexAttribState& state = getState(GLClientState::POINTSIZE_LOCATION);
893             *out = state.stride;
894             isClientStateParam = true;
895             break;
896             }
897         case GL_MATRIX_INDEX_ARRAY_SIZE_OES: {
898             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
899             *out = state.size;
900             isClientStateParam = true;
901             break;
902             }
903         case GL_MATRIX_INDEX_ARRAY_TYPE_OES: {
904             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
905             *out = state.type;
906             isClientStateParam = true;
907             break;
908             }
909         case GL_MATRIX_INDEX_ARRAY_STRIDE_OES: {
910             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
911             *out = state.stride;
912             isClientStateParam = true;
913             break;
914             }
915         case GL_WEIGHT_ARRAY_SIZE_OES: {
916             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
917             *out = state.size;
918             isClientStateParam = true;
919             break;
920             }
921         case GL_WEIGHT_ARRAY_TYPE_OES: {
922             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
923             *out = state.type;
924             isClientStateParam = true;
925             break;
926             }
927         case GL_WEIGHT_ARRAY_STRIDE_OES: {
928             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
929             *out = state.stride;
930             isClientStateParam = true;
931             break;
932             }
933         case GL_VERTEX_ARRAY_BUFFER_BINDING: {
934             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
935             *out = state.bufferObject;
936             isClientStateParam = true;
937             break;
938             }
939         case GL_NORMAL_ARRAY_BUFFER_BINDING: {
940             const GLClientState::VertexAttribState& state = getState(GLClientState::NORMAL_LOCATION);
941             *out = state.bufferObject;
942             isClientStateParam = true;
943             break;
944             }
945         case GL_COLOR_ARRAY_BUFFER_BINDING: {
946             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
947             *out = state.bufferObject;
948             isClientStateParam = true;
949             break;
950             }
951         case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: {
952             const GLClientState::VertexAttribState& state = getState(getActiveTexture()+GLClientState::TEXCOORD0_LOCATION);
953             *out = state.bufferObject;
954             isClientStateParam = true;
955             break;
956             }
957         case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: {
958             const GLClientState::VertexAttribState& state = getState(GLClientState::POINTSIZE_LOCATION);
959             *out = state.bufferObject;
960             isClientStateParam = true;
961             break;
962             }
963         case GL_MATRIX_INDEX_ARRAY_BUFFER_BINDING_OES: {
964             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
965             *out = state.bufferObject;
966             isClientStateParam = true;
967             break;
968             }
969         case GL_WEIGHT_ARRAY_BUFFER_BINDING_OES: {
970             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
971             *out = state.bufferObject;
972             isClientStateParam = true;
973             break;
974             }
975         case GL_ARRAY_BUFFER_BINDING: {
976             int buffer = getBuffer(GL_ARRAY_BUFFER);
977             *out = buffer;
978             isClientStateParam = true;
979             break;
980             }
981         case GL_ELEMENT_ARRAY_BUFFER_BINDING: {
982             int buffer = getBuffer(GL_ELEMENT_ARRAY_BUFFER);
983             *out = buffer;
984             isClientStateParam = true;
985             break;
986             }
987         case GL_MAX_VERTEX_ATTRIBS: {
988             *out = CODEC_MAX_VERTEX_ATTRIBUTES;
989             isClientStateParam = true;
990             break;
991         }
992         case GL_FRAMEBUFFER_BINDING:
993         // also case GL_DRAW_FRAMEBUFFER_BINDING:
994             *out = (T)mFboState.boundDrawFramebuffer;
995             isClientStateParam = true;
996             break;
997         case 0x8CAA: // GL_READ_FRAMEBUFFER_BINDING
998             *out = (T)mFboState.boundReadFramebuffer;
999             isClientStateParam = true;
1000             break;
1001         case GL_STENCIL_TEST:
1002             *out = (T)state_GL_STENCIL_TEST;
1003             isClientStateParam = true;
1004             break;
1005         case GL_STENCIL_FUNC:
1006             *out = (T)state_GL_STENCIL_FUNC;
1007             isClientStateParam = true;
1008             break;
1009         case GL_STENCIL_VALUE_MASK:
1010             *out = (T)state_GL_STENCIL_VALUE_MASK;
1011             isClientStateParam = true;
1012             break;
1013         case GL_STENCIL_REF:
1014             *out = (T)state_GL_STENCIL_REF;
1015             isClientStateParam = true;
1016             break;
1017         case GL_STENCIL_FAIL:
1018             *out = (T)state_GL_STENCIL_FAIL;
1019             isClientStateParam = true;
1020             break;
1021         case GL_STENCIL_PASS_DEPTH_FAIL:
1022             *out = (T)state_GL_STENCIL_PASS_DEPTH_FAIL;
1023             isClientStateParam = true;
1024             break;
1025         case GL_STENCIL_PASS_DEPTH_PASS:
1026             *out = (T)state_GL_STENCIL_PASS_DEPTH_PASS;
1027             isClientStateParam = true;
1028             break;
1029         case GL_STENCIL_BACK_FUNC:
1030             *out = (T)state_GL_STENCIL_BACK_FUNC;
1031             isClientStateParam = true;
1032             break;
1033         case GL_STENCIL_BACK_VALUE_MASK:
1034             *out = (T)state_GL_STENCIL_BACK_VALUE_MASK;
1035             isClientStateParam = true;
1036             break;
1037         case GL_STENCIL_BACK_REF:
1038             *out = (T)state_GL_STENCIL_BACK_REF;
1039             isClientStateParam = true;
1040             break;
1041         case GL_STENCIL_BACK_FAIL:
1042             *out = (T)state_GL_STENCIL_BACK_FAIL;
1043             isClientStateParam = true;
1044             break;
1045         case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1046             *out = (T)state_GL_STENCIL_BACK_PASS_DEPTH_FAIL;
1047             isClientStateParam = true;
1048             break;
1049         case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1050             *out = (T)state_GL_STENCIL_BACK_PASS_DEPTH_PASS;
1051             isClientStateParam = true;
1052             break;
1053         case GL_STENCIL_WRITEMASK:
1054             *out = (T)state_GL_STENCIL_WRITEMASK;
1055             isClientStateParam = true;
1056             break;
1057         case GL_STENCIL_BACK_WRITEMASK:
1058             *out = (T)state_GL_STENCIL_BACK_WRITEMASK;
1059             isClientStateParam = true;
1060             break;
1061         case GL_STENCIL_CLEAR_VALUE:
1062             *out = (T)state_GL_STENCIL_CLEAR_VALUE;
1063             isClientStateParam = true;
1064             break;
1065         }
1066         return isClientStateParam;
1067     }
1068 
1069 };
1070 #endif
1071