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_SHARED_GROUP_H_
17 #define _GL_SHARED_GROUP_H_
18 
19 #include <GLES/gl.h>
20 #include <GLES/glext.h>
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include "IndexRangeCache.h"
32 #include "StateTrackingSupport.h"
33 #include "TextureSharedData.h"
34 
35 using gfxstream::guest::AutoLock;
36 using gfxstream::guest::Lock;
37 
38 namespace gfxstream {
39 namespace guest {
40 
41 struct BufferData {
42     BufferData();
43     BufferData(GLsizeiptr size, const void* data);
44 
45     // General buffer state
46     GLsizeiptr m_size;
47     GLenum m_usage;
48 
49     // Mapped buffer state
50     bool m_mapped;
51     GLbitfield m_mappedAccess;
52     GLintptr m_mappedOffset;
53     GLsizeiptr m_mappedLength;
54     uint64_t m_guest_paddr;
55 
56     // Internal bookkeeping
57     std::vector<char> m_fixedBuffer; // actual buffer is shadowed here
58     IndexRangeCache m_indexRangeCache;
59 };
60 
61 class ProgramData {
62 private:
63     typedef struct _IndexInfo {
64         GLint base;
65         GLint size;
66         GLenum type;
67         GLint hostLocsPerElement;
68         GLuint flags;
69         GLint samplerValue; // only set for sampler uniforms
70     } IndexInfo;
71 
72     typedef struct _AttribInfo {
73         GLint attribLoc;
74         GLint size;
75         GLenum type;
76     } AttribInfo;
77 
78     GLuint m_numIndexes;
79     GLuint m_numAttributes;
80     IndexInfo* m_Indexes;
81     AttribInfo* m_attribIndexes;
82     bool m_initialized;
83 
84     std::vector<GLuint> m_shaders;
85     std::vector<GLenum> m_shaderTypes;
86 
87     uint32_t m_refcount;
88     GLint m_linkStatus;
89 
90     uint32_t m_activeUniformBlockCount;
91     uint32_t m_transformFeedbackVaryingsCount;;
92 
93 public:
94     enum {
95         INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001,
96     };
97 
98     ProgramData();
99     void initProgramData(GLuint numIndexes, GLuint numAttributes);
100     bool isInitialized();
101     virtual ~ProgramData();
102     void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type);
103     void setAttribInfo(GLuint index, GLint base, GLint size, GLenum type);
104     void setIndexFlags(GLuint index, GLuint flags);
105     GLuint getIndexForLocation(GLint location);
106     GLenum getTypeForLocation(GLint location);
107     bool isValidUniformLocation(GLint location);
108     void getExternalSamplerUniformIndices(std::vector<GLuint>* outIndices);
109 
110     GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target);
111     bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target);
112 
113     bool attachShader(GLuint shader, GLenum shaderType);
114     bool detachShader(GLuint shader);
getNumShaders()115     size_t getNumShaders() const { return m_shaders.size(); }
getShader(size_t i)116     GLuint getShader(size_t i) const { return m_shaders[i]; }
117 
incRef()118     void incRef() { ++m_refcount; }
decRef()119     bool decRef() {
120         --m_refcount;
121         return 0 == m_refcount;
122     }
123 
124     UniformValidationInfo compileValidationInfo(bool* error) const;
125     AttribValidationInfo compileAttribValidationInfo(bool* error) const;
setLinkStatus(GLint status)126     void setLinkStatus(GLint status) { m_linkStatus = status; }
getLinkStatus()127     GLint getLinkStatus() { return m_linkStatus; }
128 
setActiveUniformBlockCount(uint32_t count)129     void setActiveUniformBlockCount(uint32_t count) {
130         m_activeUniformBlockCount = count;
131     }
132 
getActiveUniformBlockCount()133     uint32_t getActiveUniformBlockCount() const {
134         return m_activeUniformBlockCount;
135     }
136 
setTransformFeedbackVaryingsCount(uint32_t count)137     void setTransformFeedbackVaryingsCount(uint32_t count) {
138         m_transformFeedbackVaryingsCount = count;
139     }
140 
getTransformFeedbackVaryingsCount()141     uint32_t getTransformFeedbackVaryingsCount() const {
142         return m_transformFeedbackVaryingsCount;
143     }
144 
getActiveUniformsCount()145     GLuint getActiveUniformsCount() const {
146         return m_numIndexes;
147     }
148 
getActiveAttributesCount()149     GLuint getActiveAttributesCount() const {
150         return m_numAttributes;
151     }
152 };
153 
154 struct ShaderData {
155     typedef std::vector<std::string> StringList;
156     StringList samplerExternalNames;
157     int refcount;
158     std::vector<std::string> sources;
159     GLenum shaderType;
160 };
161 
162 class ShaderProgramData {
163 public:
164     ShaderData shaderData;
165     ProgramData programData;
166 };
167 
168 class GLSharedGroup {
169 private:
170     SharedTextureDataMap m_textureRecs;
171     std::map<GLuint, BufferData*> m_buffers;
172     std::map<GLuint, ProgramData*> m_programs;
173     std::map<GLuint, ShaderData*> m_shaders;
174     std::map<uint32_t, ShaderProgramData*> m_shaderPrograms;
175     std::map<GLuint, uint32_t> m_shaderProgramIdMap;
176     RenderbufferInfo m_renderbufferInfo;
177     SamplerInfo m_samplerInfo;
178 
179     Lock m_lock;
180 
181     void refShaderDataLocked(GLuint shader);
182     void unrefShaderDataLocked(GLuint shader);
183 
184     uint32_t m_shaderProgramId;
185 
186     ProgramData* getProgramDataLocked(GLuint program);
187 public:
188     GLSharedGroup();
189     ~GLSharedGroup();
190     bool isShaderOrProgramObject(GLuint obj);
191     BufferData * getBufferData(GLuint bufferId);
192     SharedTextureDataMap* getTextureData();
193     RenderbufferInfo* getRenderbufferInfo();
194     SamplerInfo* getSamplerInfo();
195     void    addBufferData(GLuint bufferId, GLsizeiptr size, const void* data);
196     void    updateBufferData(GLuint bufferId, GLsizeiptr size, const void* data);
197     void    setBufferUsage(GLuint bufferId, GLenum usage);
198     void    setBufferMapped(GLuint bufferId, bool mapped);
199     GLenum    getBufferUsage(GLuint bufferId);
200     bool    isBufferMapped(GLuint bufferId);
201     GLenum  subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, const void* data);
202     void    deleteBufferData(GLuint);
203 
204     bool    isProgram(GLuint program);
205     bool    isProgramInitialized(GLuint program);
206     void    addProgramData(GLuint program);
207     void    initProgramData(GLuint program, GLuint numIndexes, GLuint numAttributes);
208     void    refProgramData(GLuint program);
209     void    onUseProgram(GLuint previous, GLuint next);
210     bool    attachShader(GLuint program, GLuint shader);
211     bool    detachShader(GLuint program, GLuint shader);
212     bool    detachShaderLocked(GLuint program, GLuint shader);
213     void    deleteProgramData(GLuint program);
214     void    deleteProgramDataLocked(GLuint program);
215     void    setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name);
216     void    setProgramIndexFlag(GLuint program, GLuint index, GLuint flags);
217     void    setProgramAttribInfo(GLuint program, GLuint index, GLint attribLoc, GLint size, GLenum type, const char* name);
218     GLenum  getProgramUniformType(GLuint program, GLint location);
219     GLint   getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target);
220     bool    setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target);
221     bool    isProgramUniformLocationValid(GLuint program, GLint location);
222     bool    getExternalSamplerUniformIndices(GLuint program, std::vector<GLuint>* outIndices);
223 
224     bool    isShader(GLuint shader);
225     bool    addShaderData(GLuint shader, GLenum shaderType);
226     // caller must hold a reference to the shader as long as it holds the pointer
227     ShaderData* getShaderData(GLuint shader);
228     void    unrefShaderData(GLuint shader);
229 
230     // For separable shader programs.
231     uint32_t addNewShaderProgramData();
232     void associateGLShaderProgram(GLuint shaderProgramName, uint32_t shaderProgramId);
233     ShaderProgramData* getShaderProgramDataById(uint32_t id);
234     ShaderProgramData* getShaderProgramData(GLuint shaderProgramName);
235     void deleteShaderProgramDataById(uint32_t id);
236     void deleteShaderProgramData(GLuint shaderProgramName);
237     void initShaderProgramData(GLuint shaderProgram, GLuint numIndices, GLuint numAttributes);
238     void setShaderProgramIndexInfo(GLuint shaderProgram, GLuint index, GLint base, GLint size, GLenum type, const char* name);
239 
240     // Validation info
241     UniformValidationInfo getUniformValidationInfo(GLuint program);
242     AttribValidationInfo getAttribValidationInfo(GLuint program);
243 
244     void setProgramLinkStatus(GLuint program, GLint linkStatus);
245     GLint getProgramLinkStatus(GLuint program);
246 
247     void setActiveUniformBlockCountForProgram(GLuint program, GLint numBlocks);
248     GLint getActiveUniformBlockCount(GLuint program);
249 
250     void setTransformFeedbackVaryingsCountForProgram(GLuint program, GLint count);
251     GLint getTransformFeedbackVaryingsCountForProgram(GLuint program);
252 
253     int getActiveUniformsCountForProgram(GLuint program);
254     int getActiveAttributesCountForProgram(GLuint program);
255 };
256 
257 typedef std::shared_ptr<GLSharedGroup> GLSharedGroupPtr;
258 
259 }  // namespace guest
260 }  // namespace gfxstream
261 
262 #endif //_GL_SHARED_GROUP_H_
263