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 #define GL_API
20 #ifndef ANDROID
21 #define GL_APIENTRY
22 #define GL_APIENTRYP
23 #endif
24 
25 #include "TextureSharedData.h"
26 
27 #include <GLES/gl.h>
28 #include <GLES/glext.h>
29 #include <GLES2/gl2.h>
30 #include <GLES2/gl2ext.h>
31 
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include "ErrorLog.h"
39 #include <utils/KeyedVector.h>
40 #include <utils/List.h>
41 #include <utils/String8.h>
42 #include <utils/threads.h>
43 #include "FixedBuffer.h"
44 #include "IndexRangeCache.h"
45 #include "SmartPtr.h"
46 
47 struct BufferData {
48     BufferData();
49     BufferData(GLsizeiptr size, void * data);
50 
51     // General buffer state
52     GLsizeiptr m_size;
53     GLenum m_usage;
54 
55     // Mapped buffer state
56     bool m_mapped;
57     GLbitfield m_mappedAccess;
58     GLintptr m_mappedOffset;
59     GLsizeiptr m_mappedLength;
60 
61     // Internal bookkeeping
62     FixedBuffer m_fixedBuffer; // actual buffer is shadowed here
63     IndexRangeCache m_indexRangeCache;
64 };
65 
66 class ProgramData {
67 private:
68     typedef struct _IndexInfo {
69         GLint base;
70         GLint size;
71         GLenum type;
72         GLint appBase;
73         GLint hostLocsPerElement;
74         GLuint flags;
75         GLint samplerValue; // only set for sampler uniforms
76     } IndexInfo;
77 
78     GLuint m_numIndexes;
79     IndexInfo* m_Indexes;
80     bool m_initialized;
81     bool m_locShiftWAR;
82 
83     android::Vector<GLuint> m_shaders;
84 
85 public:
86     enum {
87         INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001,
88     };
89 
90     ProgramData();
91     void initProgramData(GLuint numIndexes);
92     bool isInitialized();
93     virtual ~ProgramData();
94     void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type);
95     void setIndexFlags(GLuint index, GLuint flags);
96     GLuint getIndexForLocation(GLint location);
97     GLenum getTypeForLocation(GLint location);
98 
needUniformLocationWAR()99     bool needUniformLocationWAR() const { return m_locShiftWAR; }
100     void setupLocationShiftWAR();
101     GLint locationWARHostToApp(GLint hostLoc, GLint arrIndex);
102     GLint locationWARAppToHost(GLint appLoc);
103 
104     GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target);
105     bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target);
106 
107     bool attachShader(GLuint shader);
108     bool detachShader(GLuint shader);
getNumShaders()109     size_t getNumShaders() const { return m_shaders.size(); }
getShader(size_t i)110     GLuint getShader(size_t i) const { return m_shaders[i]; }
111 };
112 
113 struct ShaderData {
114     typedef android::List<android::String8> StringList;
115     StringList samplerExternalNames;
116     int refcount;
117     std::vector<std::string> sources;
118 };
119 
120 class ShaderProgramData {
121 public:
ShaderProgramData()122     ShaderProgramData() {
123         shaderData = new ShaderData();
124         programData = new ProgramData();
125     }
~ShaderProgramData()126     ~ShaderProgramData() {
127         delete shaderData;
128         delete programData;
129     }
130     ShaderData* shaderData;
131     ProgramData* programData;
132 };
133 
134 class GLSharedGroup {
135 private:
136     SharedTextureDataMap m_textureRecs;
137     android::DefaultKeyedVector<GLuint, BufferData*> m_buffers;
138     android::DefaultKeyedVector<GLuint, ProgramData*> m_programs;
139     android::DefaultKeyedVector<GLuint, ShaderData*> m_shaders;
140     android::DefaultKeyedVector<uint32_t, ShaderProgramData*> m_shaderPrograms;
141     std::map<GLuint, uint32_t> m_shaderProgramIdMap;
142 
143     mutable android::Mutex m_lock;
144 
145     void refShaderDataLocked(ssize_t shaderIdx);
146     void unrefShaderDataLocked(ssize_t shaderIdx);
147 
148     uint32_t m_shaderProgramId;
149 
150 public:
151     GLSharedGroup();
152     ~GLSharedGroup();
153     bool isShaderOrProgramObject(GLuint obj);
154     BufferData * getBufferData(GLuint bufferId);
155     SharedTextureDataMap* getTextureData();
156     void    addBufferData(GLuint bufferId, GLsizeiptr size, void * data);
157     void    updateBufferData(GLuint bufferId, GLsizeiptr size, void * data);
158     void    setBufferUsage(GLuint bufferId, GLenum usage);
159     void    setBufferMapped(GLuint bufferId, bool mapped);
160     GLenum    getBufferUsage(GLuint bufferId);
161     bool    isBufferMapped(GLuint bufferId);
162     GLenum  subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, void * data);
163     void    deleteBufferData(GLuint);
164 
165     bool    isProgram(GLuint program);
166     bool    isProgramInitialized(GLuint program);
167     void    addProgramData(GLuint program);
168     void    initProgramData(GLuint program, GLuint numIndexes);
169     void    attachShader(GLuint program, GLuint shader);
170     void    detachShader(GLuint program, GLuint shader);
171     void    deleteProgramData(GLuint program);
172     void    setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name);
173     GLenum  getProgramUniformType(GLuint program, GLint location);
174     void    setupLocationShiftWAR(GLuint program);
175     GLint   locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex);
176     GLint   locationWARAppToHost(GLuint program, GLint appLoc);
177     bool    needUniformLocationWAR(GLuint program);
178     GLint   getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const;
179     bool    setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target);
180 
181     bool    isShader(GLuint shader);
182     bool    addShaderData(GLuint shader);
183     // caller must hold a reference to the shader as long as it holds the pointer
184     ShaderData* getShaderData(GLuint shader);
185     void    unrefShaderData(GLuint shader);
186 
187     // For separable shader programs.
188     uint32_t addNewShaderProgramData();
189     void associateGLShaderProgram(GLuint shaderProgramName, uint32_t shaderProgramId);
190     ShaderProgramData* getShaderProgramDataById(uint32_t id);
191     ShaderProgramData* getShaderProgramData(GLuint shaderProgramName);
192     void deleteShaderProgramDataById(uint32_t id);
193     void deleteShaderProgramData(GLuint shaderProgramName);
194     void initShaderProgramData(GLuint shaderProgram, GLuint numIndices);
195     void setShaderProgramIndexInfo(GLuint shaderProgram, GLuint index, GLint base, GLint size, GLenum type, const char* name);
196     void setupShaderProgramLocationShiftWAR(GLuint shaderProgram);
197 };
198 
199 typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr;
200 
201 #endif //_GL_SHARED_GROUP_H_
202