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