• 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 
644     bool m_transformFeedbackActive;
645     bool m_transformFeedbackUnpaused;
646     uint32_t m_transformFeedbackVaryingsCountForLinking;
647 
648     HostDriverCaps m_hostDriverCaps;
649     bool m_extensions_set;
650     std::string m_extensions;
651     bool m_has_color_buffer_float_extension;
652     bool m_has_color_buffer_half_float_extension;
653     std::vector<BufferBinding> m_indexedTransformFeedbackBuffers;
654     std::vector<BufferBinding> m_indexedUniformBuffers;
655     std::vector<BufferBinding> m_indexedAtomicCounterBuffers;
656     std::vector<BufferBinding> m_indexedShaderStorageBuffers;
657     int m_log2MaxTextureSize;
658 
659     int m_glesMajorVersion;
660     int m_glesMinorVersion;
661     int m_activeTexture;
662     GLint m_currentProgram;
663     GLint m_currentShaderProgram;
664     ProgramPipelineMap m_programPipelines;
665 
666     enum TextureTarget {
667         TEXTURE_2D = 0,
668         TEXTURE_EXTERNAL = 1,
669         TEXTURE_CUBE_MAP = 2,
670         TEXTURE_2D_ARRAY = 3,
671         TEXTURE_3D = 4,
672         TEXTURE_2D_MULTISAMPLE = 5,
673         TEXTURE_TARGET_COUNT
674     };
675     struct TextureUnit {
676         unsigned int enables;
677         GLuint texture[TEXTURE_TARGET_COUNT];
678         GLuint boundSampler;
679     };
680     struct TextureState {
681         TextureUnit unit[MAX_TEXTURE_UNITS];
682         TextureUnit* activeUnit;
683         // Initialized from shared group.
684         SharedTextureDataMap* textureRecs;
685     };
686     TextureState m_tex;
687 
688     // State tracking of cube map definitions.
689     // Currently used only for driver workarounds
690     // when using GL_LUMINANCE and defining cube maps with
691     // glCopyTexImage2D.
692     struct CubeMapDef {
693         GLuint id;
694         GLenum target;
695         GLint level;
696         GLenum internalformat;
697     };
698     struct CubeMapDefCompare {
operatorCubeMapDefCompare699         bool operator() (const CubeMapDef& a,
700                          const CubeMapDef& b) const {
701             if (a.id != b.id) return a.id < b.id;
702             if (a.target != b.target) return a.target < b.target;
703             if (a.level != b.level) return a.level < b.level;
704             if (a.internalformat != b.internalformat)
705                 return a.internalformat < b.internalformat;
706             return false;
707         }
708     };
709     std::set<CubeMapDef, CubeMapDefCompare> m_cubeMapDefs;
710     void writeCopyTexImageState(GLenum target, GLint level,
711                                 GLenum internalformat);
712     GLenum copyTexImageNeededTarget(GLenum target, GLint level,
713                                     GLenum internalformat);
714 
715     struct RboState {
716         std::shared_ptr<RboProps> boundRenderbuffer;
717         // Connects to share group.
718         // Expected that share group lifetime outlives this context.
719         RenderbufferInfo* rboData;
720     };
721     RboState mRboState;
722     void addFreshRenderbuffer(GLuint name);
723 
724     struct FboState {
725         GLuint boundDrawFramebuffer;
726         GLuint boundReadFramebuffer;
727         size_t boundFramebufferIndex;
728         std::map<GLuint, FboProps> fboData;
729         GLenum drawFboCheckStatus;
730         GLenum readFboCheckStatus;
731     };
732     FboState mFboState;
733     void addFreshFramebuffer(GLuint name);
734     FboProps& boundFboProps(GLenum target);
735     const FboProps& boundFboProps_const(GLenum target) const;
736 
737     // Querying framebuffer format
738     GLenum queryTexType(GLuint name) const;
739     GLsizei queryTexSamples(GLuint name) const;
740 
741     static int compareTexId(const void* pid, const void* prec);
742     TextureRec* addTextureRec(GLuint id, GLenum target);
743     std::shared_ptr<TextureRec> getTextureRec(GLuint id) const;
744     TextureRec* getTextureRecPtr(GLuint id) const;
745     TextureRec* getTextureRecPtrLocked(GLuint id) const;
746 
747 public:
748     bool isTexture(GLuint name) const;
749     bool isTextureWithStorage(GLuint name) const;
750     bool isTextureWithTarget(GLuint name) const;
751     bool isTextureCubeMap(GLuint name) const;
752     bool isRenderbuffer(GLuint name) const;
753     bool isRenderbufferThatWasBound(GLuint name) const;
754 
755     void getClientStatePointer(GLenum pname, GLvoid** params);
756 
757     template <class T>
getVertexAttribParameter(GLuint index,GLenum param,T * ptr)758     int getVertexAttribParameter(GLuint index, GLenum param, T *ptr)
759     {
760         bool handled = true;
761         const VertexAttribState& vertexAttrib = getState(index);
762         const BufferBinding& vertexAttribBufferBinding =
763             m_currVaoState.bufferBindings_const()[vertexAttrib.bindingindex];
764 
765         switch(param) {
766 #define GL_VERTEX_ATTRIB_BINDING 0x82D4
767         case GL_VERTEX_ATTRIB_BINDING:
768             *ptr = (T)vertexAttrib.bindingindex;
769             break;
770 #define GL_VERTEX_ATTRIB_RELATIVE_OFFSET 0x82D5
771         case GL_VERTEX_ATTRIB_RELATIVE_OFFSET:
772             *ptr = (T)vertexAttrib.reloffset;
773             break;
774         case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
775             *ptr = (T)(vertexAttribBufferBinding.buffer);
776             break;
777         case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
778             *ptr = (T)(vertexAttrib.enabled);
779             break;
780 #define GL_VERTEX_ATTRIB_ARRAY_INTEGER 0x88FD
781         case GL_VERTEX_ATTRIB_ARRAY_INTEGER:
782             *ptr = (T)(vertexAttrib.isInt);
783             break;
784         case GL_VERTEX_ATTRIB_ARRAY_SIZE:
785             *ptr = (T)(vertexAttrib.size);
786             break;
787         case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
788             *ptr = (T)(vertexAttribBufferBinding.stride);
789             break;
790         case GL_VERTEX_ATTRIB_ARRAY_TYPE:
791             *ptr = (T)(vertexAttrib.type);
792             break;
793         case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
794             *ptr = (T)(vertexAttrib.normalized);
795             break;
796         case GL_CURRENT_VERTEX_ATTRIB:
797             handled = false;
798             break;
799         default:
800             handled = false;
801             ERR("unknown vertex-attrib parameter param %d\n", param);
802         }
803         return handled;
804     }
805 
806     template <class T>
getClientStateParameter(GLenum param,T * out)807     bool getClientStateParameter(GLenum param, T* out)
808     {
809         bool isClientStateParam = false;
810         switch (param) {
811         case GL_CLIENT_ACTIVE_TEXTURE: {
812             GLint tex = getActiveTexture() + GL_TEXTURE0;
813             *out = tex;
814             isClientStateParam = true;
815             break;
816             }
817         case GL_VERTEX_ARRAY_SIZE: {
818             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
819             *out = state.size;
820             isClientStateParam = true;
821             break;
822             }
823         case GL_VERTEX_ARRAY_TYPE: {
824             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
825             *out = state.type;
826             isClientStateParam = true;
827             break;
828             }
829         case GL_VERTEX_ARRAY_STRIDE: {
830             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
831             *out = state.stride;
832             isClientStateParam = true;
833             break;
834             }
835         case GL_COLOR_ARRAY_SIZE: {
836             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
837             *out = state.size;
838             isClientStateParam = true;
839             break;
840             }
841         case GL_COLOR_ARRAY_TYPE: {
842             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
843             *out = state.type;
844             isClientStateParam = true;
845             break;
846             }
847         case GL_COLOR_ARRAY_STRIDE: {
848             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
849             *out = state.stride;
850             isClientStateParam = true;
851             break;
852             }
853         case GL_NORMAL_ARRAY_TYPE: {
854             const GLClientState::VertexAttribState& state = getState(GLClientState::NORMAL_LOCATION);
855             *out = state.type;
856             isClientStateParam = true;
857             break;
858             }
859         case GL_NORMAL_ARRAY_STRIDE: {
860             const GLClientState::VertexAttribState& state = getState(GLClientState::NORMAL_LOCATION);
861             *out = state.stride;
862             isClientStateParam = true;
863             break;
864             }
865         case GL_TEXTURE_COORD_ARRAY_SIZE: {
866             const GLClientState::VertexAttribState& state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
867             *out = state.size;
868             isClientStateParam = true;
869             break;
870             }
871         case GL_TEXTURE_COORD_ARRAY_TYPE: {
872             const GLClientState::VertexAttribState& state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
873             *out = state.type;
874             isClientStateParam = true;
875             break;
876             }
877         case GL_TEXTURE_COORD_ARRAY_STRIDE: {
878             const GLClientState::VertexAttribState& state = getState(getActiveTexture() + GLClientState::TEXCOORD0_LOCATION);
879             *out = state.stride;
880             isClientStateParam = true;
881             break;
882             }
883         case GL_POINT_SIZE_ARRAY_TYPE_OES: {
884             const GLClientState::VertexAttribState& state = getState(GLClientState::POINTSIZE_LOCATION);
885             *out = state.type;
886             isClientStateParam = true;
887             break;
888             }
889         case GL_POINT_SIZE_ARRAY_STRIDE_OES: {
890             const GLClientState::VertexAttribState& state = getState(GLClientState::POINTSIZE_LOCATION);
891             *out = state.stride;
892             isClientStateParam = true;
893             break;
894             }
895         case GL_MATRIX_INDEX_ARRAY_SIZE_OES: {
896             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
897             *out = state.size;
898             isClientStateParam = true;
899             break;
900             }
901         case GL_MATRIX_INDEX_ARRAY_TYPE_OES: {
902             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
903             *out = state.type;
904             isClientStateParam = true;
905             break;
906             }
907         case GL_MATRIX_INDEX_ARRAY_STRIDE_OES: {
908             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
909             *out = state.stride;
910             isClientStateParam = true;
911             break;
912             }
913         case GL_WEIGHT_ARRAY_SIZE_OES: {
914             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
915             *out = state.size;
916             isClientStateParam = true;
917             break;
918             }
919         case GL_WEIGHT_ARRAY_TYPE_OES: {
920             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
921             *out = state.type;
922             isClientStateParam = true;
923             break;
924             }
925         case GL_WEIGHT_ARRAY_STRIDE_OES: {
926             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
927             *out = state.stride;
928             isClientStateParam = true;
929             break;
930             }
931         case GL_VERTEX_ARRAY_BUFFER_BINDING: {
932             const GLClientState::VertexAttribState& state = getState(GLClientState::VERTEX_LOCATION);
933             *out = state.bufferObject;
934             isClientStateParam = true;
935             break;
936             }
937         case GL_NORMAL_ARRAY_BUFFER_BINDING: {
938             const GLClientState::VertexAttribState& state = getState(GLClientState::NORMAL_LOCATION);
939             *out = state.bufferObject;
940             isClientStateParam = true;
941             break;
942             }
943         case GL_COLOR_ARRAY_BUFFER_BINDING: {
944             const GLClientState::VertexAttribState& state = getState(GLClientState::COLOR_LOCATION);
945             *out = state.bufferObject;
946             isClientStateParam = true;
947             break;
948             }
949         case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: {
950             const GLClientState::VertexAttribState& state = getState(getActiveTexture()+GLClientState::TEXCOORD0_LOCATION);
951             *out = state.bufferObject;
952             isClientStateParam = true;
953             break;
954             }
955         case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: {
956             const GLClientState::VertexAttribState& state = getState(GLClientState::POINTSIZE_LOCATION);
957             *out = state.bufferObject;
958             isClientStateParam = true;
959             break;
960             }
961         case GL_MATRIX_INDEX_ARRAY_BUFFER_BINDING_OES: {
962             const GLClientState::VertexAttribState& state = getState(GLClientState::MATRIXINDEX_LOCATION);
963             *out = state.bufferObject;
964             isClientStateParam = true;
965             break;
966             }
967         case GL_WEIGHT_ARRAY_BUFFER_BINDING_OES: {
968             const GLClientState::VertexAttribState& state = getState(GLClientState::WEIGHT_LOCATION);
969             *out = state.bufferObject;
970             isClientStateParam = true;
971             break;
972             }
973         case GL_ARRAY_BUFFER_BINDING: {
974             int buffer = getBuffer(GL_ARRAY_BUFFER);
975             *out = buffer;
976             isClientStateParam = true;
977             break;
978             }
979         case GL_ELEMENT_ARRAY_BUFFER_BINDING: {
980             int buffer = getBuffer(GL_ELEMENT_ARRAY_BUFFER);
981             *out = buffer;
982             isClientStateParam = true;
983             break;
984             }
985         case GL_MAX_VERTEX_ATTRIBS: {
986             *out = CODEC_MAX_VERTEX_ATTRIBUTES;
987             isClientStateParam = true;
988             break;
989         }
990         case GL_FRAMEBUFFER_BINDING:
991         // also case GL_DRAW_FRAMEBUFFER_BINDING:
992             *out = (T)mFboState.boundDrawFramebuffer;
993             isClientStateParam = true;
994             break;
995         case 0x8CAA: // GL_READ_FRAMEBUFFER_BINDING
996             *out = (T)mFboState.boundReadFramebuffer;
997             isClientStateParam = true;
998             break;
999         case GL_STENCIL_TEST:
1000             *out = (T)state_GL_STENCIL_TEST;
1001             isClientStateParam = true;
1002             break;
1003         case GL_STENCIL_FUNC:
1004             *out = (T)state_GL_STENCIL_FUNC;
1005             isClientStateParam = true;
1006             break;
1007         case GL_STENCIL_VALUE_MASK:
1008             *out = (T)state_GL_STENCIL_VALUE_MASK;
1009             isClientStateParam = true;
1010             break;
1011         case GL_STENCIL_REF:
1012             *out = (T)state_GL_STENCIL_REF;
1013             isClientStateParam = true;
1014             break;
1015         case GL_STENCIL_FAIL:
1016             *out = (T)state_GL_STENCIL_FAIL;
1017             isClientStateParam = true;
1018             break;
1019         case GL_STENCIL_PASS_DEPTH_FAIL:
1020             *out = (T)state_GL_STENCIL_PASS_DEPTH_FAIL;
1021             isClientStateParam = true;
1022             break;
1023         case GL_STENCIL_PASS_DEPTH_PASS:
1024             *out = (T)state_GL_STENCIL_PASS_DEPTH_PASS;
1025             isClientStateParam = true;
1026             break;
1027         case GL_STENCIL_BACK_FUNC:
1028             *out = (T)state_GL_STENCIL_BACK_FUNC;
1029             isClientStateParam = true;
1030             break;
1031         case GL_STENCIL_BACK_VALUE_MASK:
1032             *out = (T)state_GL_STENCIL_BACK_VALUE_MASK;
1033             isClientStateParam = true;
1034             break;
1035         case GL_STENCIL_BACK_REF:
1036             *out = (T)state_GL_STENCIL_BACK_REF;
1037             isClientStateParam = true;
1038             break;
1039         case GL_STENCIL_BACK_FAIL:
1040             *out = (T)state_GL_STENCIL_BACK_FAIL;
1041             isClientStateParam = true;
1042             break;
1043         case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1044             *out = (T)state_GL_STENCIL_BACK_PASS_DEPTH_FAIL;
1045             isClientStateParam = true;
1046             break;
1047         case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1048             *out = (T)state_GL_STENCIL_BACK_PASS_DEPTH_PASS;
1049             isClientStateParam = true;
1050             break;
1051         case GL_STENCIL_WRITEMASK:
1052             *out = (T)state_GL_STENCIL_WRITEMASK;
1053             isClientStateParam = true;
1054             break;
1055         case GL_STENCIL_BACK_WRITEMASK:
1056             *out = (T)state_GL_STENCIL_BACK_WRITEMASK;
1057             isClientStateParam = true;
1058             break;
1059         case GL_STENCIL_CLEAR_VALUE:
1060             *out = (T)state_GL_STENCIL_CLEAR_VALUE;
1061             isClientStateParam = true;
1062             break;
1063         }
1064         return isClientStateParam;
1065     }
1066 
1067 };
1068 #endif
1069