blob: d3663faa8031382e6faf5c359208f43169a93043 [file] [log] [blame]
keunyoungb85b2752013-03-08 12:28:03 -08001/*
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 Yang74e29292017-01-10 14:54:38 -080025#include "TextureSharedData.h"
26
keunyoungb85b2752013-03-08 12:28:03 -080027#include <GLES/gl.h>
28#include <GLES/glext.h>
29#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
31
Lingfeng Yang74e29292017-01-10 14:54:38 -080032#include <map>
keunyoungb85b2752013-03-08 12:28:03 -080033#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 Yang8e2b6e02016-10-14 11:20:45 -070041#include "IndexRangeCache.h"
keunyoungb85b2752013-03-08 12:28:03 -080042#include "SmartPtr.h"
43
44struct BufferData {
45 BufferData();
46 BufferData(GLsizeiptr size, void * data);
Lingfeng Yangf654f3f2017-01-09 13:12:33 -080047
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 Yang8e2b6e02016-10-14 11:20:45 -070060 IndexRangeCache m_indexRangeCache;
keunyoungb85b2752013-03-08 12:28:03 -080061};
62
63class ProgramData {
64private:
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
82public:
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
110struct ShaderData {
111 typedef android::List<android::String8> StringList;
112 StringList samplerExternalNames;
113 int refcount;
114};
115
116class GLSharedGroup {
117private:
Lingfeng Yang74e29292017-01-10 14:54:38 -0800118 SharedTextureDataMap m_textureRecs;
keunyoungb85b2752013-03-08 12:28:03 -0800119 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
127public:
128 GLSharedGroup();
129 ~GLSharedGroup();
Tina Zhang7a84f652014-12-04 12:37:30 +0800130 bool isObject(GLuint obj);
keunyoungb85b2752013-03-08 12:28:03 -0800131 BufferData * getBufferData(GLuint bufferId);
Lingfeng Yang74e29292017-01-10 14:54:38 -0800132 SharedTextureDataMap* getTextureData();
keunyoungb85b2752013-03-08 12:28:03 -0800133 void addBufferData(GLuint bufferId, GLsizeiptr size, void * data);
134 void updateBufferData(GLuint bufferId, GLsizeiptr size, void * data);
Lingfeng Yangf654f3f2017-01-09 13:12:33 -0800135 void setBufferUsage(GLuint bufferId, GLenum usage);
136 void setBufferMapped(GLuint bufferId, bool mapped);
137 GLenum getBufferUsage(GLuint bufferId);
138 bool isBufferMapped(GLuint bufferId);
keunyoungb85b2752013-03-08 12:28:03 -0800139 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 Zhoub99406c2016-11-22 14:03:56 -0800158 bool isShader(GLuint shader);
keunyoungb85b2752013-03-08 12:28:03 -0800159 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
165typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr;
166
167#endif //_GL_SHARED_GROUP_H_