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/threads.h>
40 #include "auto_goldfish_dma_context.h"
41 #include "IndexRangeCache.h"
42 #include "SmartPtr.h"
43 
44 struct BufferData {
45     BufferData();
46     BufferData(GLsizeiptr size, const void* data);
47 
48     // General buffer state
49     GLsizeiptr m_size;
50     GLenum m_usage;
51 
52     // Mapped buffer state
53     bool m_mapped;
54     GLbitfield m_mappedAccess;
55     GLintptr m_mappedOffset;
56     GLsizeiptr m_mappedLength;
57     uint64_t m_guest_paddr;
58 
59     // Internal bookkeeping
60     std::vector<char> m_fixedBuffer; // actual buffer is shadowed here
61     IndexRangeCache m_indexRangeCache;
62 
63     // DMA support
64     AutoGoldfishDmaContext dma_buffer;
65 };
66 
67 class ProgramData {
68 private:
69     typedef struct _IndexInfo {
70         GLint base;
71         GLint size;
72         GLenum type;
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 
82     std::vector<GLuint> m_shaders;
83 
84 public:
85     enum {
86         INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001,
87     };
88 
89     ProgramData();
90     void initProgramData(GLuint numIndexes);
91     bool isInitialized();
92     virtual ~ProgramData();
93     void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type);
94     void setIndexFlags(GLuint index, GLuint flags);
95     GLuint getIndexForLocation(GLint location);
96     GLenum getTypeForLocation(GLint location);
97 
98     GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target);
99     bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target);
100 
101     bool attachShader(GLuint shader);
102     bool detachShader(GLuint shader);
getNumShaders()103     size_t getNumShaders() const { return m_shaders.size(); }
getShader(size_t i)104     GLuint getShader(size_t i) const { return m_shaders[i]; }
105 };
106 
107 struct ShaderData {
108     typedef std::vector<std::string> StringList;
109     StringList samplerExternalNames;
110     int refcount;
111     std::vector<std::string> sources;
112 };
113 
114 class ShaderProgramData {
115 public:
116     ShaderData shaderData;
117     ProgramData programData;
118 };
119 
120 class GLSharedGroup {
121 private:
122     SharedTextureDataMap m_textureRecs;
123     std::map<GLuint, BufferData*> m_buffers;
124     std::map<GLuint, ProgramData*> m_programs;
125     std::map<GLuint, ShaderData*> m_shaders;
126     std::map<uint32_t, ShaderProgramData*> m_shaderPrograms;
127     std::map<GLuint, uint32_t> m_shaderProgramIdMap;
128 
129     mutable android::Mutex m_lock;
130 
131     void refShaderDataLocked(GLuint shader);
132     void unrefShaderDataLocked(GLuint shader);
133 
134     uint32_t m_shaderProgramId;
135 
136 public:
137     GLSharedGroup();
138     ~GLSharedGroup();
139     bool isShaderOrProgramObject(GLuint obj);
140     BufferData * getBufferData(GLuint bufferId);
141     SharedTextureDataMap* getTextureData();
142     void    addBufferData(GLuint bufferId, GLsizeiptr size, const void* data);
143     void    updateBufferData(GLuint bufferId, GLsizeiptr size, const void* data);
144     void    setBufferUsage(GLuint bufferId, GLenum usage);
145     void    setBufferMapped(GLuint bufferId, bool mapped);
146     GLenum    getBufferUsage(GLuint bufferId);
147     bool    isBufferMapped(GLuint bufferId);
148     GLenum  subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, const void* data);
149     void    deleteBufferData(GLuint);
150 
151     bool    isProgram(GLuint program);
152     bool    isProgramInitialized(GLuint program);
153     void    addProgramData(GLuint program);
154     void    initProgramData(GLuint program, GLuint numIndexes);
155     void    attachShader(GLuint program, GLuint shader);
156     void    detachShader(GLuint program, GLuint shader);
157     void    deleteProgramData(GLuint program);
158     void    setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name);
159     GLenum  getProgramUniformType(GLuint program, GLint location);
160     GLint   getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const;
161     bool    setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target);
162 
163     bool    isShader(GLuint shader);
164     bool    addShaderData(GLuint shader);
165     // caller must hold a reference to the shader as long as it holds the pointer
166     ShaderData* getShaderData(GLuint shader);
167     void    unrefShaderData(GLuint shader);
168 
169     // For separable shader programs.
170     uint32_t addNewShaderProgramData();
171     void associateGLShaderProgram(GLuint shaderProgramName, uint32_t shaderProgramId);
172     ShaderProgramData* getShaderProgramDataById(uint32_t id);
173     ShaderProgramData* getShaderProgramData(GLuint shaderProgramName);
174     void deleteShaderProgramDataById(uint32_t id);
175     void deleteShaderProgramData(GLuint shaderProgramName);
176     void initShaderProgramData(GLuint shaderProgram, GLuint numIndices);
177     void setShaderProgramIndexInfo(GLuint shaderProgram, GLuint index, GLint base, GLint size, GLenum type, const char* name);
178 };
179 
180 typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr;
181 
182 #endif //_GL_SHARED_GROUP_H_
183