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 #include "emugl/common/id_to_object_map.h" 20 #include "emugl/common/mutex.h" 21 #include "emugl/common/pod_vector.h" 22 #include "emugl/common/smart_ptr.h" 23 24 #define GL_API 25 #ifndef ANDROID 26 #define GL_APIENTRY 27 #define GL_APIENTRYP 28 #endif 29 30 #include <GLES/gl.h> 31 #include <GLES/glext.h> 32 #include <GLES2/gl2.h> 33 #include <GLES2/gl2ext.h> 34 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include "ErrorLog.h" 38 #include "FixedBuffer.h" 39 40 struct BufferData { 41 BufferData(); 42 BufferData(GLsizeiptr size, void * data); 43 GLsizeiptr m_size; 44 FixedBuffer m_fixedBuffer; 45 }; 46 47 class ProgramData { 48 private: 49 typedef struct _IndexInfo { 50 GLint base; 51 GLint size; 52 GLenum type; 53 GLint appBase; 54 GLint hostLocsPerElement; 55 GLuint flags; 56 GLint samplerValue; // only set for sampler uniforms 57 } IndexInfo; 58 59 GLuint m_numIndexes; 60 IndexInfo* m_Indexes; 61 bool m_initialized; 62 bool m_locShiftWAR; 63 64 emugl::PodVector<GLuint> m_shaders; 65 66 public: 67 enum { 68 INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001, 69 }; 70 71 ProgramData(); 72 void initProgramData(GLuint numIndexes); 73 bool isInitialized(); 74 virtual ~ProgramData(); 75 void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type); 76 void setIndexFlags(GLuint index, GLuint flags); 77 GLuint getIndexForLocation(GLint location); 78 GLenum getTypeForLocation(GLint location); 79 needUniformLocationWAR()80 bool needUniformLocationWAR() const { return m_locShiftWAR; } 81 void setupLocationShiftWAR(); 82 GLint locationWARHostToApp(GLint hostLoc, GLint arrIndex); 83 GLint locationWARAppToHost(GLint appLoc); 84 85 GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target); 86 bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target); 87 88 bool attachShader(GLuint shader); 89 bool detachShader(GLuint shader); getNumShaders()90 size_t getNumShaders() const { return m_shaders.size(); } getShader(size_t i)91 GLuint getShader(size_t i) const { return m_shaders[i]; } 92 }; 93 94 struct ShaderData { 95 #if 0 // TODO(digit): Undertand why this is never used? 96 typedef android::List<android::String8> StringList; 97 StringList samplerExternalNames; 98 #endif 99 int refcount; 100 }; 101 102 class GLSharedGroup { 103 private: 104 emugl::IdToObjectMap<BufferData> m_buffers; 105 emugl::IdToObjectMap<ProgramData> m_programs; 106 emugl::IdToObjectMap<ShaderData> m_shaders; 107 mutable emugl::Mutex m_lock; 108 109 void refShaderDataLocked(GLuint shader); 110 void unrefShaderDataLocked(GLuint shader); 111 112 public: 113 GLSharedGroup(); 114 ~GLSharedGroup(); 115 BufferData * getBufferData(GLuint bufferId); 116 void addBufferData(GLuint bufferId, GLsizeiptr size, void * data); 117 void updateBufferData(GLuint bufferId, GLsizeiptr size, void * data); 118 GLenum subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, void * data); 119 void deleteBufferData(GLuint); 120 121 bool isProgram(GLuint program); 122 bool isProgramInitialized(GLuint program); 123 void addProgramData(GLuint program); 124 void initProgramData(GLuint program, GLuint numIndexes); 125 void attachShader(GLuint program, GLuint shader); 126 void detachShader(GLuint program, GLuint shader); 127 void deleteProgramData(GLuint program); 128 void setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name); 129 GLenum getProgramUniformType(GLuint program, GLint location); 130 void setupLocationShiftWAR(GLuint program); 131 GLint locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex); 132 GLint locationWARAppToHost(GLuint program, GLint appLoc); 133 bool needUniformLocationWAR(GLuint program); 134 GLint getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const; 135 bool setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target); 136 137 bool addShaderData(GLuint shader); 138 // caller must hold a reference to the shader as long as it holds the pointer 139 ShaderData* getShaderData(GLuint shader); 140 void unrefShaderData(GLuint shader); 141 }; 142 143 typedef emugl::SmartPtr<GLSharedGroup> GLSharedGroupPtr; 144 145 #endif //_GL_SHARED_GROUP_H_ 146