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 <GLES/gl.h>
26 #include <GLES/glext.h>
27 #include <GLES2/gl2.h>
28 #include <GLES2/gl2ext.h>
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "ErrorLog.h"
33 #include <utils/KeyedVector.h>
34 #include <utils/List.h>
35 #include <utils/String8.h>
36 #include <utils/threads.h>
37 #include "FixedBuffer.h"
38 #include "SmartPtr.h"
39 
40 struct BufferData {
41     BufferData();
42     BufferData(GLsizeiptr size, void * data);
43     GLsizeiptr  m_size;
44     FixedBuffer m_fixedBuffer;
45 };
46 
47 class ProgramData {
48 private:
49     typedef struct _IndexInfo {
50         GLint base;
51         GLint size;
52         GLenum type;
53         GLint appBase;
54         GLint hostLocsPerElement;
55         GLuint flags;
56         GLint samplerValue; // only set for sampler uniforms
57     } IndexInfo;
58 
59     GLuint m_numIndexes;
60     IndexInfo* m_Indexes;
61     bool m_initialized;
62     bool m_locShiftWAR;
63 
64     android::Vector<GLuint> m_shaders;
65 
66 public:
67     enum {
68         INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001,
69     };
70 
71     ProgramData();
72     void initProgramData(GLuint numIndexes);
73     bool isInitialized();
74     virtual ~ProgramData();
75     void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type);
76     void setIndexFlags(GLuint index, GLuint flags);
77     GLuint getIndexForLocation(GLint location);
78     GLenum getTypeForLocation(GLint location);
79 
needUniformLocationWAR()80     bool needUniformLocationWAR() const { return m_locShiftWAR; }
81     void setupLocationShiftWAR();
82     GLint locationWARHostToApp(GLint hostLoc, GLint arrIndex);
83     GLint locationWARAppToHost(GLint appLoc);
84 
85     GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target);
86     bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target);
87 
88     bool attachShader(GLuint shader);
89     bool detachShader(GLuint shader);
getNumShaders()90     size_t getNumShaders() const { return m_shaders.size(); }
getShader(size_t i)91     GLuint getShader(size_t i) const { return m_shaders[i]; }
92 };
93 
94 struct ShaderData {
95     typedef android::List<android::String8> StringList;
96     StringList samplerExternalNames;
97     int refcount;
98 };
99 
100 class GLSharedGroup {
101 private:
102     android::DefaultKeyedVector<GLuint, BufferData*> m_buffers;
103     android::DefaultKeyedVector<GLuint, ProgramData*> m_programs;
104     android::DefaultKeyedVector<GLuint, ShaderData*> m_shaders;
105     mutable android::Mutex m_lock;
106 
107     void refShaderDataLocked(ssize_t shaderIdx);
108     void unrefShaderDataLocked(ssize_t shaderIdx);
109 
110 public:
111     GLSharedGroup();
112     ~GLSharedGroup();
113     BufferData * getBufferData(GLuint bufferId);
114     void    addBufferData(GLuint bufferId, GLsizeiptr size, void * data);
115     void    updateBufferData(GLuint bufferId, GLsizeiptr size, void * data);
116     GLenum  subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, void * data);
117     void    deleteBufferData(GLuint);
118 
119     bool    isProgram(GLuint program);
120     bool    isProgramInitialized(GLuint program);
121     void    addProgramData(GLuint program);
122     void    initProgramData(GLuint program, GLuint numIndexes);
123     void    attachShader(GLuint program, GLuint shader);
124     void    detachShader(GLuint program, GLuint shader);
125     void    deleteProgramData(GLuint program);
126     void    setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name);
127     GLenum  getProgramUniformType(GLuint program, GLint location);
128     void    setupLocationShiftWAR(GLuint program);
129     GLint   locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex);
130     GLint   locationWARAppToHost(GLuint program, GLint appLoc);
131     bool    needUniformLocationWAR(GLuint program);
132     GLint   getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const;
133     bool    setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target);
134 
135     bool    addShaderData(GLuint shader);
136     // caller must hold a reference to the shader as long as it holds the pointer
137     ShaderData* getShaderData(GLuint shader);
138     void    unrefShaderData(GLuint shader);
139 };
140 
141 typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr;
142 
143 #endif //_GL_SHARED_GROUP_H_
144