keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 | |
Lingfeng Yang | 74e2929 | 2017-01-10 14:54:38 -0800 | [diff] [blame] | 25 | #include "TextureSharedData.h" |
| 26 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 27 | #include <GLES/gl.h> |
| 28 | #include <GLES/glext.h> |
| 29 | #include <GLES2/gl2.h> |
| 30 | #include <GLES2/gl2ext.h> |
| 31 | |
Lingfeng Yang | 74e2929 | 2017-01-10 14:54:38 -0800 | [diff] [blame] | 32 | #include <map> |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include "ErrorLog.h" |
| 36 | #include <utils/KeyedVector.h> |
| 37 | #include <utils/List.h> |
| 38 | #include <utils/String8.h> |
| 39 | #include <utils/threads.h> |
| 40 | #include "FixedBuffer.h" |
Lingfeng Yang | 8e2b6e0 | 2016-10-14 11:20:45 -0700 | [diff] [blame] | 41 | #include "IndexRangeCache.h" |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 42 | #include "SmartPtr.h" |
| 43 | |
| 44 | struct BufferData { |
| 45 | BufferData(); |
| 46 | BufferData(GLsizeiptr size, void * data); |
Lingfeng Yang | f654f3f | 2017-01-09 13:12:33 -0800 | [diff] [blame] | 47 | |
| 48 | // General buffer state |
| 49 | GLsizeiptr m_size; |
| 50 | GLenum m_usage; |
| 51 | |
| 52 | // Mapped buffer state |
| 53 | bool m_mapped; |
| 54 | GLbitfield m_mappedAccess; |
| 55 | GLintptr m_mappedOffset; |
| 56 | GLsizeiptr m_mappedLength; |
| 57 | |
| 58 | // Internal bookkeeping |
| 59 | FixedBuffer m_fixedBuffer; // actual buffer is shadowed here |
Lingfeng Yang | 8e2b6e0 | 2016-10-14 11:20:45 -0700 | [diff] [blame] | 60 | IndexRangeCache m_indexRangeCache; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class ProgramData { |
| 64 | private: |
| 65 | typedef struct _IndexInfo { |
| 66 | GLint base; |
| 67 | GLint size; |
| 68 | GLenum type; |
| 69 | GLint appBase; |
| 70 | GLint hostLocsPerElement; |
| 71 | GLuint flags; |
| 72 | GLint samplerValue; // only set for sampler uniforms |
| 73 | } IndexInfo; |
| 74 | |
| 75 | GLuint m_numIndexes; |
| 76 | IndexInfo* m_Indexes; |
| 77 | bool m_initialized; |
| 78 | bool m_locShiftWAR; |
| 79 | |
| 80 | android::Vector<GLuint> m_shaders; |
| 81 | |
| 82 | public: |
| 83 | enum { |
| 84 | INDEX_FLAG_SAMPLER_EXTERNAL = 0x00000001, |
| 85 | }; |
| 86 | |
| 87 | ProgramData(); |
| 88 | void initProgramData(GLuint numIndexes); |
| 89 | bool isInitialized(); |
| 90 | virtual ~ProgramData(); |
| 91 | void setIndexInfo(GLuint index, GLint base, GLint size, GLenum type); |
| 92 | void setIndexFlags(GLuint index, GLuint flags); |
| 93 | GLuint getIndexForLocation(GLint location); |
| 94 | GLenum getTypeForLocation(GLint location); |
| 95 | |
| 96 | bool needUniformLocationWAR() const { return m_locShiftWAR; } |
| 97 | void setupLocationShiftWAR(); |
| 98 | GLint locationWARHostToApp(GLint hostLoc, GLint arrIndex); |
| 99 | GLint locationWARAppToHost(GLint appLoc); |
| 100 | |
| 101 | GLint getNextSamplerUniform(GLint index, GLint* val, GLenum* target); |
| 102 | bool setSamplerUniform(GLint appLoc, GLint val, GLenum* target); |
| 103 | |
| 104 | bool attachShader(GLuint shader); |
| 105 | bool detachShader(GLuint shader); |
| 106 | size_t getNumShaders() const { return m_shaders.size(); } |
| 107 | GLuint getShader(size_t i) const { return m_shaders[i]; } |
| 108 | }; |
| 109 | |
| 110 | struct ShaderData { |
| 111 | typedef android::List<android::String8> StringList; |
| 112 | StringList samplerExternalNames; |
| 113 | int refcount; |
| 114 | }; |
| 115 | |
| 116 | class GLSharedGroup { |
| 117 | private: |
Lingfeng Yang | 74e2929 | 2017-01-10 14:54:38 -0800 | [diff] [blame] | 118 | SharedTextureDataMap m_textureRecs; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 119 | android::DefaultKeyedVector<GLuint, BufferData*> m_buffers; |
| 120 | android::DefaultKeyedVector<GLuint, ProgramData*> m_programs; |
| 121 | android::DefaultKeyedVector<GLuint, ShaderData*> m_shaders; |
| 122 | mutable android::Mutex m_lock; |
| 123 | |
| 124 | void refShaderDataLocked(ssize_t shaderIdx); |
| 125 | void unrefShaderDataLocked(ssize_t shaderIdx); |
| 126 | |
| 127 | public: |
| 128 | GLSharedGroup(); |
| 129 | ~GLSharedGroup(); |
Tina Zhang | 7a84f65 | 2014-12-04 12:37:30 +0800 | [diff] [blame] | 130 | bool isObject(GLuint obj); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 131 | BufferData * getBufferData(GLuint bufferId); |
Lingfeng Yang | 74e2929 | 2017-01-10 14:54:38 -0800 | [diff] [blame] | 132 | SharedTextureDataMap* getTextureData(); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 133 | void addBufferData(GLuint bufferId, GLsizeiptr size, void * data); |
| 134 | void updateBufferData(GLuint bufferId, GLsizeiptr size, void * data); |
Lingfeng Yang | f654f3f | 2017-01-09 13:12:33 -0800 | [diff] [blame] | 135 | void setBufferUsage(GLuint bufferId, GLenum usage); |
| 136 | void setBufferMapped(GLuint bufferId, bool mapped); |
| 137 | GLenum getBufferUsage(GLuint bufferId); |
| 138 | bool isBufferMapped(GLuint bufferId); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 139 | GLenum subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, void * data); |
| 140 | void deleteBufferData(GLuint); |
| 141 | |
| 142 | bool isProgram(GLuint program); |
| 143 | bool isProgramInitialized(GLuint program); |
| 144 | void addProgramData(GLuint program); |
| 145 | void initProgramData(GLuint program, GLuint numIndexes); |
| 146 | void attachShader(GLuint program, GLuint shader); |
| 147 | void detachShader(GLuint program, GLuint shader); |
| 148 | void deleteProgramData(GLuint program); |
| 149 | void setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name); |
| 150 | GLenum getProgramUniformType(GLuint program, GLint location); |
| 151 | void setupLocationShiftWAR(GLuint program); |
| 152 | GLint locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex); |
| 153 | GLint locationWARAppToHost(GLuint program, GLint appLoc); |
| 154 | bool needUniformLocationWAR(GLuint program); |
| 155 | GLint getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const; |
| 156 | bool setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target); |
| 157 | |
Yahan Zhou | b99406c | 2016-11-22 14:03:56 -0800 | [diff] [blame] | 158 | bool isShader(GLuint shader); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 159 | bool addShaderData(GLuint shader); |
| 160 | // caller must hold a reference to the shader as long as it holds the pointer |
| 161 | ShaderData* getShaderData(GLuint shader); |
| 162 | void unrefShaderData(GLuint shader); |
| 163 | }; |
| 164 | |
| 165 | typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr; |
| 166 | |
| 167 | #endif //_GL_SHARED_GROUP_H_ |