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