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 "FixedBuffer.h" 41 #include "auto_goldfish_dma_context.h" 42 #include "IndexRangeCache.h" 43 #include "SmartPtr.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 FixedBuffer 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 appBase; 75 GLint hostLocsPerElement; 76 GLuint flags; 77 GLint samplerValue; // only set for sampler uniforms 78 } IndexInfo; 79 80 GLuint m_numIndexes; 81 IndexInfo* m_Indexes; 82 bool m_initialized; 83 bool m_locShiftWAR; 84 85 std::vector<GLuint> m_shaders; 86 87 public: 88 enum { 89 INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001, 90 }; 91 92 ProgramData(); 93 void initProgramData(GLuint numIndexes); 94 bool isInitialized(); 95 virtual ~ProgramData(); 96 void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type); 97 void setIndexFlags(GLuint index, GLuint flags); 98 GLuint getIndexForLocation(GLint location); 99 GLenum getTypeForLocation(GLint location); 100 needUniformLocationWAR()101 bool needUniformLocationWAR() const { return m_locShiftWAR; } 102 void setupLocationShiftWAR(); 103 GLint locationWARHostToApp(GLint hostLoc, GLint arrIndex); 104 GLint locationWARAppToHost(GLint appLoc); 105 106 GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target); 107 bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target); 108 109 bool attachShader(GLuint shader); 110 bool detachShader(GLuint shader); getNumShaders()111 size_t getNumShaders() const { return m_shaders.size(); } getShader(size_t i)112 GLuint getShader(size_t i) const { return m_shaders[i]; } 113 }; 114 115 struct ShaderData { 116 typedef std::vector<std::string> StringList; 117 StringList samplerExternalNames; 118 int refcount; 119 std::vector<std::string> sources; 120 }; 121 122 class ShaderProgramData { 123 public: 124 ShaderData shaderData; 125 ProgramData programData; 126 }; 127 128 class GLSharedGroup { 129 private: 130 SharedTextureDataMap m_textureRecs; 131 std::map<GLuint, BufferData*> m_buffers; 132 std::map<GLuint, ProgramData*> m_programs; 133 std::map<GLuint, ShaderData*> m_shaders; 134 std::map<uint32_t, ShaderProgramData*> m_shaderPrograms; 135 std::map<GLuint, uint32_t> m_shaderProgramIdMap; 136 137 mutable android::Mutex m_lock; 138 139 void refShaderDataLocked(GLuint shader); 140 void unrefShaderDataLocked(GLuint shader); 141 142 uint32_t m_shaderProgramId; 143 144 public: 145 GLSharedGroup(); 146 ~GLSharedGroup(); 147 bool isShaderOrProgramObject(GLuint obj); 148 BufferData * getBufferData(GLuint bufferId); 149 SharedTextureDataMap* getTextureData(); 150 void addBufferData(GLuint bufferId, GLsizeiptr size, const void* data); 151 void updateBufferData(GLuint bufferId, GLsizeiptr size, const void* data); 152 void setBufferUsage(GLuint bufferId, GLenum usage); 153 void setBufferMapped(GLuint bufferId, bool mapped); 154 GLenum getBufferUsage(GLuint bufferId); 155 bool isBufferMapped(GLuint bufferId); 156 GLenum subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, const void* data); 157 void deleteBufferData(GLuint); 158 159 bool isProgram(GLuint program); 160 bool isProgramInitialized(GLuint program); 161 void addProgramData(GLuint program); 162 void initProgramData(GLuint program, GLuint numIndexes); 163 void attachShader(GLuint program, GLuint shader); 164 void detachShader(GLuint program, GLuint shader); 165 void deleteProgramData(GLuint program); 166 void setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name); 167 GLenum getProgramUniformType(GLuint program, GLint location); 168 void setupLocationShiftWAR(GLuint program); 169 GLint locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex); 170 GLint locationWARAppToHost(GLuint program, GLint appLoc); 171 bool needUniformLocationWAR(GLuint program); 172 GLint getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const; 173 bool setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target); 174 175 bool isShader(GLuint shader); 176 bool addShaderData(GLuint shader); 177 // caller must hold a reference to the shader as long as it holds the pointer 178 ShaderData* getShaderData(GLuint shader); 179 void unrefShaderData(GLuint shader); 180 181 // For separable shader programs. 182 uint32_t addNewShaderProgramData(); 183 void associateGLShaderProgram(GLuint shaderProgramName, uint32_t shaderProgramId); 184 ShaderProgramData* getShaderProgramDataById(uint32_t id); 185 ShaderProgramData* getShaderProgramData(GLuint shaderProgramName); 186 void deleteShaderProgramDataById(uint32_t id); 187 void deleteShaderProgramData(GLuint shaderProgramName); 188 void initShaderProgramData(GLuint shaderProgram, GLuint numIndices); 189 void setShaderProgramIndexInfo(GLuint shaderProgram, GLuint index, GLint base, GLint size, GLenum type, const char* name); 190 void setupShaderProgramLocationShiftWAR(GLuint shaderProgram); 191 }; 192 193 typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr; 194 195 #endif //_GL_SHARED_GROUP_H_ 196