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