blob: 9ef92ea9c6f5798ba64a38e497690a3295dd607c [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>
Lingfeng Yangb3dc29f2017-01-09 13:25:31 -080033#include <string>
34#include <vector>
35
keunyoungb85b2752013-03-08 12:28:03 -080036#include <stdio.h>
37#include <stdlib.h>
38#include "ErrorLog.h"
keunyoungb85b2752013-03-08 12:28:03 -080039#include <utils/threads.h>
40#include "FixedBuffer.h"
Roman Kiryanovbe1c0c22018-10-02 18:07:14 -070041#include "auto_goldfish_dma_context.h"
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070042#include "IndexRangeCache.h"
keunyoungb85b2752013-03-08 12:28:03 -080043#include "SmartPtr.h"
44
45struct BufferData {
46 BufferData();
Roman Kiryanovfb903d62018-09-19 13:38:53 -070047 BufferData(GLsizeiptr size, const void* data);
Lingfeng Yangf654f3f2017-01-09 13:12:33 -080048
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;
Roman Kiryanovbe1c0c22018-10-02 18:07:14 -070058 uint64_t m_guest_paddr;
Lingfeng Yangf654f3f2017-01-09 13:12:33 -080059
60 // Internal bookkeeping
61 FixedBuffer m_fixedBuffer; // actual buffer is shadowed here
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070062 IndexRangeCache m_indexRangeCache;
Roman Kiryanovb6fce5b2018-09-27 16:18:58 -070063
64 // DMA support
Roman Kiryanovbe1c0c22018-10-02 18:07:14 -070065 AutoGoldfishDmaContext dma_buffer;
keunyoungb85b2752013-03-08 12:28:03 -080066};
67
68class ProgramData {
69private:
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
Lingfeng Yang44209df2018-09-21 10:04:17 -070085 std::vector<GLuint> m_shaders;
keunyoungb85b2752013-03-08 12:28:03 -080086
87public:
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
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);
111 size_t getNumShaders() const { return m_shaders.size(); }
112 GLuint getShader(size_t i) const { return m_shaders[i]; }
113};
114
115struct ShaderData {
Lingfeng Yang44209df2018-09-21 10:04:17 -0700116 typedef std::vector<std::string> StringList;
keunyoungb85b2752013-03-08 12:28:03 -0800117 StringList samplerExternalNames;
118 int refcount;
Lingfeng Yangb3dc29f2017-01-09 13:25:31 -0800119 std::vector<std::string> sources;
120};
121
122class ShaderProgramData {
123public:
Lingfeng Yang44209df2018-09-21 10:04:17 -0700124 ShaderData shaderData;
125 ProgramData programData;
keunyoungb85b2752013-03-08 12:28:03 -0800126};
127
128class GLSharedGroup {
129private:
Lingfeng Yang74e29292017-01-10 14:54:38 -0800130 SharedTextureDataMap m_textureRecs;
Lingfeng Yang44209df2018-09-21 10:04:17 -0700131 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;
Lingfeng Yangb3dc29f2017-01-09 13:25:31 -0800135 std::map<GLuint, uint32_t> m_shaderProgramIdMap;
136
keunyoungb85b2752013-03-08 12:28:03 -0800137 mutable android::Mutex m_lock;
138
Lingfeng Yang44209df2018-09-21 10:04:17 -0700139 void refShaderDataLocked(GLuint shader);
140 void unrefShaderDataLocked(GLuint shader);
keunyoungb85b2752013-03-08 12:28:03 -0800141
Lingfeng Yangb3dc29f2017-01-09 13:25:31 -0800142 uint32_t m_shaderProgramId;
143
keunyoungb85b2752013-03-08 12:28:03 -0800144public:
145 GLSharedGroup();
146 ~GLSharedGroup();
Lingfeng Yangb3dc29f2017-01-09 13:25:31 -0800147 bool isShaderOrProgramObject(GLuint obj);
keunyoungb85b2752013-03-08 12:28:03 -0800148 BufferData * getBufferData(GLuint bufferId);
Lingfeng Yang74e29292017-01-10 14:54:38 -0800149 SharedTextureDataMap* getTextureData();
Roman Kiryanovfb903d62018-09-19 13:38:53 -0700150 void addBufferData(GLuint bufferId, GLsizeiptr size, const void* data);
151 void updateBufferData(GLuint bufferId, GLsizeiptr size, const void* data);
Lingfeng Yangf654f3f2017-01-09 13:12:33 -0800152 void setBufferUsage(GLuint bufferId, GLenum usage);
153 void setBufferMapped(GLuint bufferId, bool mapped);
154 GLenum getBufferUsage(GLuint bufferId);
155 bool isBufferMapped(GLuint bufferId);
Roman Kiryanovfb903d62018-09-19 13:38:53 -0700156 GLenum subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, const void* data);
keunyoungb85b2752013-03-08 12:28:03 -0800157 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
Yahan Zhoub99406c2016-11-22 14:03:56 -0800175 bool isShader(GLuint shader);
keunyoungb85b2752013-03-08 12:28:03 -0800176 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);
Lingfeng Yangb3dc29f2017-01-09 13:25:31 -0800180
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);
keunyoungb85b2752013-03-08 12:28:03 -0800191};
192
193typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr;
194
195#endif //_GL_SHARED_GROUP_H_