• 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 <memory>
44 #include <set>
45 #include <string>
46 
47 // Caps of host driver that make it easy to validate stuff
48 struct HostDriverCaps {
49     // ES 2
50     int max_vertex_attribs;
51     int max_combined_texture_image_units;
52     int max_color_attachments;
53 
54     int max_texture_size;
55     int max_texture_size_cube_map;
56     int max_renderbuffer_size;
57 
58     // ES 3.0
59     int max_draw_buffers;
60 
61     int ubo_offset_alignment;
62     int max_uniform_buffer_bindings;
63     int max_transform_feedback_separate_attribs;
64 
65     int max_texture_size_3d;
66     int max_array_texture_layers;
67 
68     // ES 3.1
69     int max_atomic_counter_buffer_bindings;
70     int max_shader_storage_buffer_bindings;
71     int max_vertex_attrib_bindings;
72     int max_vertex_attrib_stride;
73     int ssbo_offset_alignment;
74 };
75 
76 // Tracking framebuffer objects:
77 // which framebuffer is bound,
78 // and which texture names
79 // are currently bound to which attachment points.
80 struct FboProps {
81     GLuint name;
82     bool previouslyBound;
83     bool completenessDirty;
84     GLenum cachedCompleteness;
85     std::vector<std::shared_ptr<TextureRec>> colorAttachmenti_textures;
86     std::vector<GLint> colorAttachmenti_texture_levels;
87     std::vector<GLint> colorAttachmenti_texture_layers;
88 
89     GLint depthAttachment_texture_level;
90     GLint depthAttachment_texture_layer;
91     GLint stencilAttachment_texture_level;
92     GLint stencilAttachment_texture_layer;
93 
94     std::shared_ptr<TextureRec> depthAttachment_texture;
95     std::shared_ptr<TextureRec> stencilAttachment_texture;
96     std::shared_ptr<TextureRec> depthstencilAttachment_texture;
97 
98     std::vector<bool> colorAttachmenti_hasTex;
99     bool depthAttachment_hasTexObj;
100     bool stencilAttachment_hasTexObj;
101     bool depthstencilAttachment_hasTexObj;
102 
103     std::vector<std::shared_ptr<RboProps>> colorAttachmenti_rbos;
104     std::shared_ptr<RboProps> depthAttachment_rbo = 0;
105     std::shared_ptr<RboProps> stencilAttachment_rbo = 0;
106     std::shared_ptr<RboProps> depthstencilAttachment_rbo = 0;
107 
108     std::vector<bool> colorAttachmenti_hasRbo;
109     bool depthAttachment_hasRbo = false;
110     bool stencilAttachment_hasRbo = false;
111     bool depthstencilAttachment_hasRbo = false;
112 
113     GLuint defaultWidth;
114     GLuint defaultHeight;
115 };
116 
117 // Enum for describing whether a framebuffer attachment
118 // is a texture or renderbuffer.
119 enum FboAttachmentType {
120     FBO_ATTACHMENT_RENDERBUFFER = 0,
121     FBO_ATTACHMENT_TEXTURE = 1,
122     FBO_ATTACHMENT_NONE = 2
123 };
124 
125 // Tracking FBO format
126 struct FboFormatInfo {
127     FboAttachmentType type;
128     GLenum rb_format;
129     GLsizei rb_multisamples;
130     bool rb_external;
131 
132     GLint tex_internalformat;
133     GLenum tex_format;
134     GLenum tex_type;
135     GLsizei tex_multisamples;
136     GLint tex_level;
137     GLint tex_layer;
138     bool tex_external;
139 };
140 
141 class GLClientState {
142 public:
143     // TODO: Unify everything in here
144     typedef enum {
145         Buffer,
146         TransformFeedback,
147         Sampler,
148         Query,
149     } ObjectType;
150 
151     typedef enum {
152         VERTEX_LOCATION = 0,
153         NORMAL_LOCATION = 1,
154         COLOR_LOCATION = 2,
155         POINTSIZE_LOCATION = 3,
156         TEXCOORD0_LOCATION = 4,
157         TEXCOORD1_LOCATION = 5,
158         TEXCOORD2_LOCATION = 6,
159         TEXCOORD3_LOCATION = 7,
160         TEXCOORD4_LOCATION = 8,
161         TEXCOORD5_LOCATION = 9,
162         TEXCOORD6_LOCATION = 10,
163         TEXCOORD7_LOCATION = 11,
164         MATRIXINDEX_LOCATION = 12,
165         WEIGHT_LOCATION = 13,
166         LAST_LOCATION = 14
167     } StateLocation;
168 
169     typedef struct {
170         GLint enabled;
171         GLint size;
172         GLenum type;
173         GLsizei stride;
174         void *data;
175         GLuint reloffset;
176         GLuint bufferObject;
177         GLenum glConst;
178         unsigned int elementSize;
179         bool enableDirty;  // true if any enable state has changed since last draw
180         bool normalized;
181         GLuint divisor;
182         bool isInt;
183         int bindingindex;
184     } VertexAttribState;
185 
186     struct BufferBinding {
187         GLintptr offset;
188         GLintptr stride;
189         GLintptr effectiveStride;
190         GLsizeiptr size;
191         GLuint buffer;
192         GLuint divisor;
193         GLint vertexAttribLoc;
194     };
195 
196     typedef std::vector<VertexAttribState> VertexAttribStateVector;
197     typedef std::vector<BufferBinding> VertexAttribBindingVector;
198 
199     struct VAOState {
VAOStateVAOState200         VAOState(GLuint ibo, int nLoc, int nBindings) :
201             attribState(nLoc),
202             bindingState(nBindings),
203             element_array_buffer_binding(ibo),
204             element_array_buffer_binding_lastEncode(ibo) { }
205         VertexAttribStateVector attribState;
206         VertexAttribBindingVector bindingState;
207         GLuint element_array_buffer_binding;
208         GLuint element_array_buffer_binding_lastEncode;
209         int attributesNeedingUpdateForDraw[CODEC_MAX_VERTEX_ATTRIBUTES];
210         int numAttributesNeedingUpdateForDraw;
211     };
212 
213     typedef std::map<GLuint, VAOState> VAOStateMap;
214     struct VAOStateRef {
VAOStateRefVAOStateRef215         VAOStateRef() { }
VAOStateRefVAOStateRef216         VAOStateRef(
217                 VAOStateMap::iterator iter) : it(iter) { }
vaoStateVAOStateRef218         VAOState& vaoState() { return it->second; }
219         VertexAttribState& operator[](size_t k) { return it->second.attribState[k]; }
bufferBindingVAOStateRef220         BufferBinding& bufferBinding(size_t k) { return it->second.bindingState[k]; }
bufferBindingsVAOStateRef221         VertexAttribBindingVector& bufferBindings() { return it->second.bindingState; }
bufferBindings_constVAOStateRef222         const VertexAttribBindingVector& bufferBindings_const() const { return it->second.bindingState; }
vaoIdVAOStateRef223         GLuint vaoId() const { return it->first; }
iboIdVAOStateRef224         GLuint& iboId() { return it->second.element_array_buffer_binding; }
iboIdLastEncodeVAOStateRef225         GLuint& iboIdLastEncode() { return it->second.element_array_buffer_binding_lastEncode; }
226         VAOStateMap::iterator it;
227     };
228 
229     typedef struct {
230         int unpack_alignment;
231 
232         int unpack_row_length;
233         int unpack_image_height;
234         int unpack_skip_pixels;
235         int unpack_skip_rows;
236         int unpack_skip_images;
237 
238         int pack_alignment;
239 
240         int pack_row_length;
241         int pack_skip_pixels;
242         int pack_skip_rows;
243     } PixelStoreState;
244 
245     enum {
246         MAX_TEXTURE_UNITS = 256,
247     };
248 
249 public:
250     GLClientState();
251     GLClientState(int majorVersion, int minorVersion);
252     ~GLClientState();
nLocations()253     int nLocations() { return CODEC_MAX_VERTEX_ATTRIBUTES; }
pixelStoreState()254     const PixelStoreState *pixelStoreState() { return &m_pixelStore; }
255     int setPixelStore(GLenum param, GLint value);
currentVertexArrayObject()256     GLuint currentVertexArrayObject() const { return m_currVaoState.vaoId(); }
currentVertexBufferBindings()257     const VertexAttribBindingVector& currentVertexBufferBindings() const {
258         return m_currVaoState.bufferBindings_const();
259     }
260 
currentArrayVbo()261     GLuint currentArrayVbo() { return m_arrayBuffer; }
currentIndexVbo()262     GLuint currentIndexVbo() { return m_currVaoState.iboId(); }
263     void enable(int location, int state);
264     // Vertex array objects and vertex attributes
265     void addVertexArrayObjects(GLsizei n, GLuint* arrays);
266     void removeVertexArrayObjects(GLsizei n, const GLuint* arrays);
267     void addVertexArrayObject(GLuint name);
268     void removeVertexArrayObject(GLuint name);
269     void setVertexArrayObject(GLuint vao);
270     bool isVertexArrayObject(GLuint vao) const;
271     void setVertexAttribState(int  location, int size, GLenum type, GLboolean normalized, GLsizei stride, const void *data, bool isInt = false);
272     void setVertexBindingDivisor(int bindingindex, GLuint divisor);
273     const BufferBinding& getCurrAttributeBindingInfo(int attribindex);
274     void setVertexAttribBinding(int attribindex, int bindingindex);
275     void setVertexAttribFormat(int location, int size, GLenum type, GLboolean normalized, GLuint reloffset, bool isInt = false);
276     void getVBOUsage(bool* hasClientArrays, bool* hasVBOs);
277     const VertexAttribState& getState(int location);
278     const VertexAttribState& getStateAndEnableDirty(int location, bool *enableChanged);
279     void updateEnableDirtyArrayForDraw();
280     VAOState& currentVaoState();
281     int getLocation(GLenum loc);
setActiveTexture(int texUnit)282     void setActiveTexture(int texUnit) {m_activeTexture = texUnit; };
getActiveTexture()283     int getActiveTexture() const { return m_activeTexture; }
284 
285     void addBuffer(GLuint id);
286     void removeBuffer(GLuint id);
287     bool bufferIdExists(GLuint id) const;
288     void unBindBuffer(GLuint id);
289 
290     void setBufferHostMapDirty(GLuint id, bool dirty);
291     bool isBufferHostMapDirty(GLuint id) const;
292 
293     void setExistence(ObjectType type, bool exists, GLsizei count, const GLuint* ids);
294     bool queryExistence(ObjectType type, GLuint id) const;
295     bool samplerExists(GLuint id) const;
296     bool tryBind(GLenum target, GLuint id);
297     bool isBoundTargetValid(GLenum target);
298     bool isQueryBound(GLenum target);
299     bool isQueryObjectActive(GLuint id);
300     void setLastQueryTarget(GLenum target, GLuint id);
301     GLenum getLastQueryTarget(GLuint id);
302 
303     static void onFenceCreated(GLsync sync);
304     static void onFenceDestroyed(GLsync sync);
305     static bool fenceExists(GLsync sync);
306 
307     void setBoundPixelPackBufferDirtyForHostMap();
308     void setBoundTransformFeedbackBuffersDirtyForHostMap();
309     void setBoundShaderStorageBuffersDirtyForHostMap();
310     void setBoundAtomicCounterBuffersDirtyForHostMap();
311 
312     int bindBuffer(GLenum target, GLuint id);
313     void bindIndexedBuffer(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size, GLintptr stride, GLintptr effectiveStride);
314     int getMaxIndexedBufferBindings(GLenum target) const;
315     bool isNonIndexedBindNoOp(GLenum target, GLuint buffer);
316     bool isIndexedBindNoOp(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size, GLintptr stride, GLintptr effectiveStride);
317 
318     int getMaxTextureSize() const;
319     int getMaxTextureSize3D() const;
320     int getMaxTextureSizeCubeMap() const;
321     int getLog2MaxTextureSize() const;
322 
323     void postDraw();
324     void postReadPixels();
325     void postDispatchCompute();
326 
327     bool shouldSkipHostMapBuffer(GLenum target);
328     void onHostMappedBuffer(GLenum target);
329 
330     int getBuffer(GLenum target);
331     GLuint getLastEncodedBufferBind(GLenum target);
332     void setLastEncodedBufferBind(GLenum target, GLuint id);
333 
334     size_t pixelDataSize(GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, int pack) const;
335     size_t pboNeededDataSize(GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, int pack, int ignoreTrailing = 0) const;
336     size_t clearBufferNumElts(GLenum buffer) const;
337     void getPackingOffsets2D(GLsizei width, GLsizei height, GLenum format, GLenum type, int* bpp, int* startOffset, int* pixelRowSize, int* totalRowSize, int* skipRows) const;
338     void getUnpackingOffsets2D(GLsizei width, GLsizei height, GLenum format, GLenum type, int* bpp, int* startOffset, int* pixelRowSize, int* totalRowSize, int* skipRows) const;
339     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;
340 
setCurrentProgram(GLint program)341     void setCurrentProgram(GLint program) { m_currentProgram = program; }
setCurrentShaderProgram(GLint program)342     void setCurrentShaderProgram(GLint program) { m_currentShaderProgram = program; }
currentProgram()343     GLint currentProgram() const { return m_currentProgram; }
currentShaderProgram()344     GLint currentShaderProgram() const { return m_currentShaderProgram; }
345 
346     struct UniformBlockInfoKey {
347         GLuint program;
348         GLuint uniformBlockIndex;
349     };
350     struct UniformBlockInfoKeyCompare {
operatorUniformBlockInfoKeyCompare351         bool operator() (const UniformBlockInfoKey& a,
352                          const UniformBlockInfoKey& b) const {
353             if (a.program != b.program) return a.program < b.program;
354             if (a.uniformBlockIndex != b.uniformBlockIndex) return a.uniformBlockIndex < b.uniformBlockIndex;
355             return false;
356         }
357     };
358     struct UniformBlockUniformInfo {
359         size_t numActiveUniforms;
360     };
361 
362     typedef std::map<UniformBlockInfoKey, UniformBlockUniformInfo, UniformBlockInfoKeyCompare> UniformBlockInfoMap;
363     UniformBlockInfoMap m_uniformBlockInfoMap;
364 
365     void setNumActiveUniformsInUniformBlock(GLuint program, GLuint uniformBlockIndex, GLint numActiveUniforms);
366     size_t numActiveUniformsInUniformBlock(GLuint program, GLuint uniformBlockIndex) const;
367 
368     typedef std::map<GLuint, GLuint> ProgramPipelineMap;
369     typedef ProgramPipelineMap::iterator ProgramPipelineIterator;
370     void associateProgramWithPipeline(GLuint program, GLuint pipeline);
371     ProgramPipelineIterator programPipelineBegin();
372     ProgramPipelineIterator programPipelineEnd();
373 
374     /* OES_EGL_image_external
375      *
376      * These functions manipulate GL state which interacts with the
377      * OES_EGL_image_external extension, to support client-side emulation on
378      * top of host implementations that don't have it.
379      *
380      * Most of these calls should only be used with TEXTURE_2D or
381      * TEXTURE_EXTERNAL_OES texture targets; TEXTURE_CUBE_MAP or other extension
382      * targets should bypass this. An exception is bindTexture(), which should
383      * see all glBindTexture() calls for any target.
384      */
385 
386     // glActiveTexture(GL_TEXTURE0 + i)
387     // Sets the active texture unit. Up to MAX_TEXTURE_UNITS are supported.
388     GLenum setActiveTextureUnit(GLenum texture);
389     GLenum getActiveTextureUnit() const;
390 
391     // glEnable(GL_TEXTURE_(2D|EXTERNAL_OES))
392     void enableTextureTarget(GLenum target);
393 
394     // glDisable(GL_TEXTURE_(2D|EXTERNAL_OES))
395     void disableTextureTarget(GLenum target);
396 
397     bool bindSampler(GLuint unit, GLuint sampler);
398     bool isSamplerBindNoOp(GLuint unit, GLuint sampler);
399     void onDeleteSamplers(GLsizei n, const GLuint* samplers);
400 
401     // Implements the target priority logic:
402     // * Return GL_TEXTURE_EXTERNAL_OES if enabled, else
403     // * Return GL_TEXTURE_2D if enabled, else
404     // * Return the allDisabled value.
405     // For some cases passing GL_TEXTURE_2D for allDisabled makes callee code
406     // simpler; for other cases passing a recognizable enum like GL_ZERO or
407     // GL_INVALID_ENUM is appropriate.
408     GLenum getPriorityEnabledTarget(GLenum allDisabled) const;
409 
410     // glBindTexture(GL_TEXTURE_*, ...)
411     // Set the target binding of the active texture unit to texture. Returns
412     // GL_NO_ERROR on success or GL_INVALID_OPERATION if the texture has
413     // previously been bound to a different target. If firstUse is not NULL,
414     // it is set to indicate whether this is the first use of the texture.
415     // For accurate error detection, bindTexture should be called for *all*
416     // targets, not just 2D and EXTERNAL_OES.
417     GLenum bindTexture(GLenum target, GLuint texture, GLboolean* firstUse);
418     void setBoundEGLImage(GLenum target, GLeglImageOES image, int width, int height);
419 
420     // Return the texture currently bound to GL_TEXTURE_(2D|EXTERNAL_OES).
421     GLuint getBoundTexture(GLenum target) const;
422     // Return bound framebuffer for target
423     GLuint getBoundFramebuffer(GLenum target) const;
424 
425     // Check framebuffer completeness
426     GLenum checkFramebufferCompleteness(GLenum target);
427     // |currentSamples|: threads through the current sample count of attachments so far,
428     // for validating consistent number of samples across attachments
429     GLenum checkFramebufferAttachmentCompleteness(GLenum target, GLenum attachment, int* currentSamples) const;
430 
431     // Other publicly-visible texture queries
432     GLenum queryTexLastBoundTarget(GLuint name) const;
433     GLenum queryTexFormat(GLuint name) const;
434     GLint queryTexInternalFormat(GLuint name) const;
435     GLsizei queryTexWidth(GLsizei level, GLuint name) const;
436     GLsizei queryTexHeight(GLsizei level, GLuint name) const;
437     GLsizei queryTexDepth(GLsizei level, GLuint name) const;
438     bool queryTexEGLImageBacked(GLuint name) const;
439 
440     // For AMD GPUs, it is easy for the emulator to segfault
441     // (esp. in dEQP) when a cube map is defined using glCopyTexImage2D
442     // and uses GL_LUMINANCE as internal format.
443     // In particular, the segfault happens when negative components of
444     // cube maps are defined before positive ones,
445     // This procedure checks internal state to see if we have defined
446     // the positive component of a cube map already. If not, it returns
447     // which positive component needs to be defined first.
448     // If there is no need for the extra definition, 0 is returned.
449     GLenum copyTexImageLuminanceCubeMapAMDWorkaround(GLenum target, GLint level,
450                                                      GLenum internalformat);
451 
452     // Tracks the format of the currently bound texture.
453     // This is to pass dEQP tests for fbo completeness.
454     void setBoundTextureInternalFormat(GLenum target, GLint format);
455     void setBoundTextureFormat(GLenum target, GLenum format);
456     void setBoundTextureType(GLenum target, GLenum type);
457     void setBoundTextureDims(GLenum target, GLenum cubetarget, GLsizei level, GLsizei width, GLsizei height, GLsizei depth);
458     void setBoundTextureSamples(GLenum target, GLsizei samples);
459     void addTextureCubeMapImage(GLenum stateTarget, GLenum cubeTarget);
460 
461     // glTexStorage2D disallows any change in texture format after it is set for a particular texture.
462     void setBoundTextureImmutableFormat(GLenum target);
463     bool isBoundTextureImmutableFormat(GLenum target) const;
464     bool isBoundTextureComplete(GLenum target) const;
465 
466     // glDeleteTextures(...)
467     // Remove references to the to-be-deleted textures.
468     void deleteTextures(GLsizei n, const GLuint* textures);
469 
470     // Render buffer objects
471     void addRenderbuffers(GLsizei n, GLuint* renderbuffers);
472     void removeRenderbuffers(GLsizei n, const GLuint* renderbuffers);
473     bool usedRenderbufferName(GLuint name) const;
474     void bindRenderbuffer(GLenum target, GLuint name);
475     GLuint boundRenderbuffer() const;
476     void setBoundRenderbufferFormat(GLenum format);
477     void setBoundRenderbufferSamples(GLsizei samples);
478     void setBoundRenderbufferDimensions(GLsizei width, GLsizei height);
479     void setBoundRenderbufferEGLImageBacked();
480 
481     // Frame buffer objects
482     void addFramebuffers(GLsizei n, GLuint* framebuffers);
483     void removeFramebuffers(GLsizei n, const GLuint* framebuffers);
484     bool usedFramebufferName(GLuint name) const;
485     void bindFramebuffer(GLenum target, GLuint name);
486     void setCheckFramebufferStatus(GLenum target, GLenum status);
487     void setFramebufferParameter(GLenum target, GLenum pname, GLint param);
488     GLenum getCheckFramebufferStatus(GLenum target) const;
489     GLuint boundFramebuffer(GLenum target) const;
490 
491     // Texture object -> FBO
492     void attachTextureObject(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer);
493     std::shared_ptr<TextureRec> getFboAttachmentTexture(GLenum target, GLenum attachment) const;
494 
495     // RBO -> FBO
496     void detachRbo(GLuint renderbuffer);
497     void detachRboFromFbo(GLenum target, GLenum attachment, GLuint renderbuffer);
498     void attachRbo(GLenum target, GLenum attachment, GLuint renderbuffer);
499     std::shared_ptr<RboProps> getFboAttachmentRbo(GLenum target, GLenum attachment) const;
500 
501     // FBO attachments in general
502     bool attachmentHasObject(GLenum target, GLenum attachment) const;
503     bool depthStencilHasSameObject(GLenum target) const;
504 
505     // Dirty FBO completeness
506     void setFboCompletenessDirtyForTexture(GLuint texture);
507     void setFboCompletenessDirtyForRbo(std::shared_ptr<RboProps> rbo);
508 
509     // Transform feedback state
510     void setTransformFeedbackActive(bool active);
511     void setTransformFeedbackUnpaused(bool unpaused);
512     void setTransformFeedbackVaryingsCountForLinking(uint32_t count);
513     bool getTransformFeedbackActive() const;
514     bool getTransformFeedbackUnpaused() const;
515     bool getTransformFeedbackActiveUnpaused() const;
516     uint32_t getTransformFeedbackVaryingsCountForLinking() const;
517 
518     // Stencil state
519     void stencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask);
520     void stencilMaskSeparate(GLenum face, GLuint mask);
521     void stencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass);
522 
523     void setTextureData(SharedTextureDataMap* sharedTexData);
524     void setRenderbufferInfo(RenderbufferInfo* rbInfo);
525     void setSamplerInfo(SamplerInfo* samplerInfo);
526 
527     bool compressedTexImageSizeCompatible(GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLsizei imageSize);
528     // set eglsurface property on default framebuffer
529     // if coming from eglMakeCurrent
530     void fromMakeCurrent();
531     // set indexed buffer state.
532     // We need to query the underlying OpenGL to get
533     // accurate values for indexed buffers
534     // and # render targets.
535     void initFromCaps(
536         const HostDriverCaps& caps);
537     bool needsInitFromCaps() const;
538     void setExtensions(const std::string& extensions);
539     bool hasExtension(const char* ext) const;
540 
541     // Queries the format backing the current framebuffer.
542     // Type differs depending on whether the attachment
543     // is a texture or renderbuffer.
544     void getBoundFramebufferFormat(
545             GLenum target,
546             GLenum attachment,
547             FboFormatInfo* res_info) const;
548     FboAttachmentType getBoundFramebufferAttachmentType(
549             GLenum target,
550             GLenum attachment) const;
551     int getMaxColorAttachments() const;
552     int getMaxDrawBuffers() const;
553 
554     // Uniform/attribute validation info
555     UniformValidationInfo currentUniformValidationInfo;
556     AttribValidationInfo currentAttribValidationInfo;;
557 
558     // Uniform validation api
559     void validateUniform(bool isFloat, bool isUnsigned, GLint columns, GLint rows, GLint location, GLsizei count, GLenum* err);
560     // Attrib validation
561     bool isAttribIndexUsedByProgram(int attribIndex);
562 
563     // Fast access to some enables and stencil related glGet's
564     bool state_GL_STENCIL_TEST;
565     GLenum state_GL_STENCIL_FUNC;
566     unsigned int state_GL_STENCIL_VALUE_MASK;
567     int state_GL_STENCIL_REF;
568     GLenum state_GL_STENCIL_FAIL;
569     GLenum state_GL_STENCIL_PASS_DEPTH_FAIL;
570     GLenum state_GL_STENCIL_PASS_DEPTH_PASS;
571     GLenum state_GL_STENCIL_BACK_FUNC;
572     unsigned int state_GL_STENCIL_BACK_VALUE_MASK;
573     int state_GL_STENCIL_BACK_REF;
574     GLenum state_GL_STENCIL_BACK_FAIL;
575     GLenum state_GL_STENCIL_BACK_PASS_DEPTH_FAIL;
576     GLenum state_GL_STENCIL_BACK_PASS_DEPTH_PASS;
577     unsigned int state_GL_STENCIL_WRITEMASK;
578     unsigned int state_GL_STENCIL_BACK_WRITEMASK;
579     int state_GL_STENCIL_CLEAR_VALUE;
580 private:
581     void init();
582     bool m_initialized;
583     PixelStoreState m_pixelStore;
584 
585 #ifdef GFXSTREAM
586     using DirtyMap = PredicateMap<uint32_t, true>;
587 
588     ExistenceMap mBufferIds;
589     ExistenceMap mTransformFeedbackIds;
590     SamplerInfo* mSamplerInfo;
591     ExistenceMap mQueryIds;
592     LastQueryTargetInfo mLastQueryTargets;
593 
594     // Bound query target validity and tracking
595     struct BoundTargetInfo {
596         GLuint id;
597         bool valid;
598     };
599 
600     // Transform feedback
601     BoundTargetInfo mBoundTransformFeedbackValidity;
602 
603     // Queries
604     // GL_ANY_SAMPLES_PASSED
605     BoundTargetInfo mBoundQueryValidity_AnySamplesPassed;
606     // GL_ANY_SAMPLES_PASSED_CONSERVATIVE
607     BoundTargetInfo mBoundQueryValidity_AnySamplesPassedConservative;
608     // GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN
609     BoundTargetInfo mBoundQueryValidity_TransformFeedbackPrimitivesWritten;
610 
611     // Dirty maps
612     DirtyMap mHostMappedBufferDirty;
613 #else
614     std::set<GLuint> mBufferIds;
615 #endif
616 
617     // GL_ARRAY_BUFFER_BINDING is separate from VAO state
618     GLuint m_arrayBuffer;
619     GLuint m_arrayBuffer_lastEncode;
620     VAOStateMap m_vaoMap;
621     VAOStateRef m_currVaoState;
622 
623     uint16_t m_attribEnableCache;
624     uint16_t m_vaoAttribBindingCacheInvalid;
625     uint16_t m_vaoAttribBindingHasClientArrayCache;
626     uint16_t m_vaoAttribBindingHasVboCache;
627     uint8_t m_noClientArraysCache;
628 
629     // Other buffer id's, other targets
630     GLuint m_copyReadBuffer;
631     GLuint m_copyWriteBuffer;
632 
633     GLuint m_pixelPackBuffer;
634     GLuint m_pixelUnpackBuffer;
635 
636     GLuint m_transformFeedbackBuffer;
637     GLuint m_uniformBuffer;
638 
639     GLuint m_atomicCounterBuffer;
640     GLuint m_dispatchIndirectBuffer;
641     GLuint m_drawIndirectBuffer;
642     GLuint m_shaderStorageBuffer;
643     GLuint m_textureBuffer;
644 
645     bool m_transformFeedbackActive;
646     bool m_transformFeedbackUnpaused;
647     uint32_t m_transformFeedbackVaryingsCountForLinking;
648 
649     HostDriverCaps m_hostDriverCaps;
650     bool m_extensions_set;
651     std::string m_extensions;
652     bool m_has_color_buffer_float_extension;
653     bool m_has_color_buffer_half_float_extension;
654     std::vector<BufferBinding> m_indexedTransformFeedbackBuffers;
655     std::vector<BufferBinding> m_indexedUniformBuffers;
656     std::vector<BufferBinding> m_indexedAtomicCounterBuffers;
657     std::vector<BufferBinding> m_indexedShaderStorageBuffers;
658     int m_log2MaxTextureSize;
659 
660     int m_glesMajorVersion;
661     int m_glesMinorVersion;
662     int m_activeTexture;
663     GLint m_currentProgram;
664     GLint m_currentShaderProgram;
665     ProgramPipelineMap m_programPipelines;
666 
667     enum TextureTarget {
668         TEXTURE_2D = 0,
669         TEXTURE_EXTERNAL = 1,
670         TEXTURE_CUBE_MAP = 2,
671         TEXTURE_2D_ARRAY = 3,
672         TEXTURE_3D = 4,
673         TEXTURE_2D_MULTISAMPLE = 5,
674         TEXTURE_BUFFER = 6,
675         TEXTURE_TARGET_COUNT
676     };
677     struct TextureUnit {
678         unsigned int enables;
679         GLuint texture[TEXTURE_TARGET_COUNT];
680         GLuint boundSampler;
681     };
682     struct TextureState {
683         TextureUnit unit[MAX_TEXTURE_UNITS];
684         TextureUnit* activeUnit;
685         // Initialized from shared group.
686         SharedTextureDataMap* textureRecs;
687     };
688     TextureState m_tex;
689 
690     // State tracking of cube map definitions.
691     // Currently used only for driver workarounds
692     // when using GL_LUMINANCE and defining cube maps with
693     // glCopyTexImage2D.
694     struct CubeMapDef {
695         GLuint id;
696         GLenum target;
697         GLint level;
698         GLenum internalformat;
699     };
700     struct CubeMapDefCompare {
operatorCubeMapDefCompare701         bool operator() (const CubeMapDef& a,
702                          const CubeMapDef& b) const {
703             if (a.id != b.id) return a.id < b.id;
704             if (a.target != b.target) return a.target < b.target;
705             if (a.level != b.level) return a.level < b.level;
706             if (a.internalformat != b.internalformat)
707                 return a.internalformat < b.internalformat;
708             return false;
709         }
710     };
711     std::set<CubeMapDef, CubeMapDefCompare> m_cubeMapDefs;
712     void writeCopyTexImageState(GLenum target, GLint level,
713                                 GLenum internalformat);
714     GLenum copyTexImageNeededTarget(GLenum target, GLint level,
715                                     GLenum internalformat);
716 
717     struct RboState {
718         std::shared_ptr<RboProps> boundRenderbuffer;
719         // Connects to share group.
720         // Expected that share group lifetime outlives this context.
721         RenderbufferInfo* rboData;
722     };
723     RboState mRboState;
724     void addFreshRenderbuffer(GLuint name);
725 
726     struct FboState {
727         GLuint boundDrawFramebuffer;
728         GLuint boundReadFramebuffer;
729         size_t boundFramebufferIndex;
730         std::map<GLuint, FboProps> fboData;
731         GLenum drawFboCheckStatus;
732         GLenum readFboCheckStatus;
733     };
734     FboState mFboState;
735     void addFreshFramebuffer(GLuint name);
736     FboProps& boundFboProps(GLenum target);
737     const FboProps& boundFboProps_const(GLenum target) const;
738 
739     // Querying framebuffer format
740     GLenum queryTexType(GLuint name) const;
741     GLsizei queryTexSamples(GLuint name) const;
742 
743     static int compareTexId(const void* pid, const void* prec);
744     TextureRec* addTextureRec(GLuint id, GLenum target);
745     std::shared_ptr<TextureRec> getTextureRec(GLuint id) const;
746     TextureRec* getTextureRecPtr(GLuint id) const;
747     TextureRec* getTextureRecPtrLocked(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