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 #include "GLESv1Decoder.h"
17
18 #include "aemu/base/synchronization/Lock.h"
19
20 #include "OpenGLESDispatch/GLESv1Dispatch.h"
21
22 #include <EGL/egl.h>
23 #include <GLES/gl.h>
24 #include <GLES/glext.h>
25
26 #include <atomic>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 using android::base::AutoLock;
32 using android::base::StaticLock;
33
34 namespace gfxstream {
35 namespace gl {
36
SafePointerFromUInt(GLuint value)37 static inline void* SafePointerFromUInt(GLuint value) {
38 return (void*)(uintptr_t)value;
39 }
40
initDispatch(void * (* getProc)(const char * name,void * userData),void * userData)41 int gles1_decoder_extended_context::initDispatch(
42 void *(*getProc)(const char *name, void *userData), void *userData) {
43 gles1_server_context_t::initDispatchByName(getProc, userData);
44 // The following could be "not-implemented" in
45 // -gpu swiftshader and -gpu angle.
46 // But we should always have them in
47 // -gpu host and -gpu [swiftshader|angle]_indirect.
48 glColorPointerWithDataSize =
49 (glColorPointerWithDataSize_server_proc_t)
50 getProc("glColorPointerWithDataSize", userData);
51 glNormalPointerWithDataSize =
52 (glNormalPointerWithDataSize_server_proc_t)
53 getProc("glNormalPointerWithDataSize", userData);
54 glTexCoordPointerWithDataSize =
55 (glTexCoordPointerWithDataSize_server_proc_t)
56 getProc("glTexCoordPointerWithDataSize", userData);
57 glVertexPointerWithDataSize =
58 (glVertexPointerWithDataSize_server_proc_t)
59 getProc("glVertexPointerWithDataSize", userData);
60 return 0;
61 }
62
63 static StaticLock sLock;
64 static GLESv1Decoder::get_proc_func_t sGetProcFunc;
65 static void* sGetProcFuncData;
66
67 namespace {
68
69 struct ContextTemplateLoader {
ContextTemplateLoadergfxstream::gl::__anonfb14eb7a0111::ContextTemplateLoader70 ContextTemplateLoader() {
71 context.initDispatch(sGetProcFunc, sGetProcFuncData);
72 }
73 gles1_decoder_extended_context context;
74 };
75
76 } // namespace
77
sContextTemplate()78 static ContextTemplateLoader* sContextTemplate() {
79 static ContextTemplateLoader* t = new ContextTemplateLoader;
80 return t;
81 }
82
GLESv1Decoder()83 GLESv1Decoder::GLESv1Decoder()
84 {
85 m_contextData = NULL;
86 m_glesDso = NULL;
87 }
88
~GLESv1Decoder()89 GLESv1Decoder::~GLESv1Decoder()
90 {
91 }
92
initGL(get_proc_func_t getProcFunc,void * getProcFuncData)93 int GLESv1Decoder::initGL(get_proc_func_t getProcFunc, void *getProcFuncData)
94 {
95 AutoLock lock(sLock);
96 sGetProcFunc = getProcFunc;
97 sGetProcFuncData = getProcFuncData;
98 static_cast<gles1_decoder_extended_context&>(*this) = sContextTemplate()->context;
99
100 glGetCompressedTextureFormats = s_glGetCompressedTextureFormats;
101 glVertexPointerOffset = s_glVertexPointerOffset;
102 glColorPointerOffset = s_glColorPointerOffset;
103 glNormalPointerOffset = s_glNormalPointerOffset;
104 glTexCoordPointerOffset = s_glTexCoordPointerOffset;
105 glPointSizePointerOffset = s_glPointSizePointerOffset;
106 glWeightPointerOffset = s_glWeightPointerOffset;
107 glMatrixIndexPointerOffset = s_glMatrixIndexPointerOffset;
108
109 glVertexPointerData = s_glVertexPointerData;
110 glColorPointerData = s_glColorPointerData;
111 glNormalPointerData = s_glNormalPointerData;
112 glTexCoordPointerData = s_glTexCoordPointerData;
113 glPointSizePointerData = s_glPointSizePointerData;
114 glWeightPointerData = s_glWeightPointerData;
115 glMatrixIndexPointerData = s_glMatrixIndexPointerData;
116
117 glDrawElementsOffset = s_glDrawElementsOffset;
118 glDrawElementsData = s_glDrawElementsData;
119 glFinishRoundTrip = s_glFinishRoundTrip;
120
121 glGenBuffers_dec = s_glGenBuffers;
122 glGenTextures_dec = s_glGenTextures;
123
124 glGenFramebuffersOES_dec = s_glGenFramebuffersOES;
125 glGenRenderbuffersOES_dec = s_glGenRenderbuffersOES;
126
127 glGenVertexArraysOES_dec = s_glGenVertexArraysOES;
128
129 glDeleteBuffers_dec = s_glDeleteBuffers;
130 glDeleteTextures_dec = s_glDeleteTextures;
131 glDeleteRenderbuffersOES_dec = s_glDeleteRenderbuffersOES;
132 glDeleteFramebuffersOES_dec = s_glDeleteFramebuffersOES;
133 glDeleteVertexArraysOES_dec = s_glDeleteVertexArraysOES;
134
135 return 0;
136 }
137
s_glFinishRoundTrip(void * self)138 int GLESv1Decoder::s_glFinishRoundTrip(void *self)
139 {
140 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
141 ctx->glFinish();
142 return 0;
143 }
144
s_glVertexPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)145 void GLESv1Decoder::s_glVertexPointerOffset(void *self, GLint size, GLenum type, GLsizei stride, GLuint offset)
146 {
147 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
148 ctx->glVertexPointer(size, type, stride, SafePointerFromUInt(offset));
149 }
150
s_glColorPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)151 void GLESv1Decoder::s_glColorPointerOffset(void *self, GLint size, GLenum type, GLsizei stride, GLuint offset)
152 {
153 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
154 ctx->glColorPointer(size, type, stride, SafePointerFromUInt(offset));
155 }
156
s_glTexCoordPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)157 void GLESv1Decoder::s_glTexCoordPointerOffset(void *self, GLint size, GLenum type, GLsizei stride, GLuint offset)
158 {
159 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
160 ctx->glTexCoordPointer(size, type, stride, SafePointerFromUInt(offset));
161 }
162
s_glNormalPointerOffset(void * self,GLenum type,GLsizei stride,GLuint offset)163 void GLESv1Decoder::s_glNormalPointerOffset(void *self, GLenum type, GLsizei stride, GLuint offset)
164 {
165 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
166 ctx->glNormalPointer(type, stride, SafePointerFromUInt(offset));
167 }
168
s_glPointSizePointerOffset(void * self,GLenum type,GLsizei stride,GLuint offset)169 void GLESv1Decoder::s_glPointSizePointerOffset(void *self, GLenum type, GLsizei stride, GLuint offset)
170 {
171 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
172 ctx->glPointSizePointerOES(type, stride, SafePointerFromUInt(offset));
173 }
174
s_glWeightPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)175 void GLESv1Decoder::s_glWeightPointerOffset(void * self, GLint size, GLenum type, GLsizei stride, GLuint offset)
176 {
177 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
178 ctx->glWeightPointerOES(size, type, stride, SafePointerFromUInt(offset));
179 }
180
s_glMatrixIndexPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)181 void GLESv1Decoder::s_glMatrixIndexPointerOffset(void * self, GLint size, GLenum type, GLsizei stride, GLuint offset)
182 {
183 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
184 ctx->glMatrixIndexPointerOES(size, type, stride, SafePointerFromUInt(offset));
185 }
186
187
188
189 #define STORE_POINTER_DATA_OR_ABORT(location) \
190 if (ctx->m_contextData != NULL) { \
191 ctx->m_contextData->storePointerData((location), data, datalen); \
192 } else { \
193 return; \
194 }
195
s_glVertexPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)196 void GLESv1Decoder::s_glVertexPointerData(void *self, GLint size, GLenum type, GLsizei stride, void *data, GLuint datalen)
197 {
198 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
199
200 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::VERTEX_LOCATION);
201
202 if ((void*)ctx->glVertexPointerWithDataSize != gles1_unimplemented) {
203 ctx->glVertexPointerWithDataSize(size, type, 0,
204 ctx->m_contextData->pointerData(GLDecoderContextData::VERTEX_LOCATION),
205 datalen);
206 } else {
207 assert(0);
208 ctx->glVertexPointer(size, type, 0,
209 ctx->m_contextData->pointerData(GLDecoderContextData::VERTEX_LOCATION));
210 }
211 }
212
s_glColorPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)213 void GLESv1Decoder::s_glColorPointerData(void *self, GLint size, GLenum type, GLsizei stride, void *data, GLuint datalen)
214 {
215 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
216
217 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::COLOR_LOCATION);
218
219 if ((void*)ctx->glColorPointerWithDataSize != gles1_unimplemented) {
220 ctx->glColorPointerWithDataSize(size, type, 0,
221 ctx->m_contextData->pointerData(GLDecoderContextData::COLOR_LOCATION),
222 datalen);
223 } else {
224 assert(0);
225 ctx->glColorPointer(size, type, 0,
226 ctx->m_contextData->pointerData(GLDecoderContextData::COLOR_LOCATION));
227 }
228 }
229
s_glTexCoordPointerData(void * self,GLint unit,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)230 void GLESv1Decoder::s_glTexCoordPointerData(void *self, GLint unit, GLint size, GLenum type, GLsizei stride, void *data, GLuint datalen)
231 {
232 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
233 STORE_POINTER_DATA_OR_ABORT((GLDecoderContextData::PointerDataLocation)
234 (GLDecoderContextData::TEXCOORD0_LOCATION + unit));
235
236 if ((void*)ctx->glTexCoordPointerWithDataSize != gles1_unimplemented) {
237 ctx->glTexCoordPointerWithDataSize(size, type, 0,
238 ctx->m_contextData->pointerData((GLDecoderContextData::PointerDataLocation)
239 (GLDecoderContextData::TEXCOORD0_LOCATION + unit)),
240 datalen);
241 } else {
242 assert(0);
243 ctx->glTexCoordPointer(size, type, 0,
244 ctx->m_contextData->pointerData((GLDecoderContextData::PointerDataLocation)
245 (GLDecoderContextData::TEXCOORD0_LOCATION + unit)));
246 }
247 }
248
s_glNormalPointerData(void * self,GLenum type,GLsizei stride,void * data,GLuint datalen)249 void GLESv1Decoder::s_glNormalPointerData(void *self, GLenum type, GLsizei stride, void *data, GLuint datalen)
250 {
251 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
252
253 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::NORMAL_LOCATION);
254
255 if ((void*)ctx->glNormalPointerWithDataSize != gles1_unimplemented) {
256 ctx->glNormalPointerWithDataSize(type, 0,
257 ctx->m_contextData->pointerData(GLDecoderContextData::NORMAL_LOCATION),
258 datalen);
259 } else {
260 assert(0);
261 ctx->glNormalPointer(type, 0,
262 ctx->m_contextData->pointerData(GLDecoderContextData::NORMAL_LOCATION));
263 }
264 }
265
s_glPointSizePointerData(void * self,GLenum type,GLsizei stride,void * data,GLuint datalen)266 void GLESv1Decoder::s_glPointSizePointerData(void *self, GLenum type, GLsizei stride, void *data, GLuint datalen)
267 {
268 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
269
270 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::POINTSIZE_LOCATION);
271
272 ctx->glPointSizePointerOES(type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::POINTSIZE_LOCATION));
273 }
274
s_glWeightPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)275 void GLESv1Decoder::s_glWeightPointerData(void * self, GLint size, GLenum type, GLsizei stride, void * data, GLuint datalen)
276 {
277 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
278
279 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::WEIGHT_LOCATION);
280
281 ctx->glWeightPointerOES(size, type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::WEIGHT_LOCATION));
282 }
283
s_glMatrixIndexPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)284 void GLESv1Decoder::s_glMatrixIndexPointerData(void * self, GLint size, GLenum type, GLsizei stride, void * data, GLuint datalen)
285 {
286 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
287
288 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::MATRIXINDEX_LOCATION);
289
290 ctx->glMatrixIndexPointerOES(size, type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::MATRIXINDEX_LOCATION));
291 }
292
s_glDrawElementsOffset(void * self,GLenum mode,GLsizei count,GLenum type,GLuint offset)293 void GLESv1Decoder::s_glDrawElementsOffset(void *self, GLenum mode, GLsizei count, GLenum type, GLuint offset)
294 {
295 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
296 ctx->glDrawElements(mode, count, type, SafePointerFromUInt(offset));
297 }
298
s_glDrawElementsData(void * self,GLenum mode,GLsizei count,GLenum type,void * data,GLuint datalen)299 void GLESv1Decoder::s_glDrawElementsData(void *self, GLenum mode, GLsizei count, GLenum type, void * data, GLuint datalen)
300 {
301 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
302 ctx->glDrawElements(mode, count, type, data);
303 }
304
s_glGetCompressedTextureFormats(void * self,GLint count,GLint * data)305 void GLESv1Decoder::s_glGetCompressedTextureFormats(void *self, GLint count, GLint *data)
306 {
307 GLESv1Decoder *ctx = (GLESv1Decoder *) self;
308 ctx->glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, data);
309 }
310
s_glGenBuffers(void * self,GLsizei n,GLuint * buffers)311 void GLESv1Decoder::s_glGenBuffers(void* self, GLsizei n, GLuint* buffers) {
312 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
313 ctx->glGenBuffers(n, buffers);
314 // TODO: Snapshot names
315 }
316
s_glGenTextures(void * self,GLsizei n,GLuint * textures)317 void GLESv1Decoder::s_glGenTextures(void* self, GLsizei n, GLuint* textures) {
318 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
319 ctx->glGenTextures(n, textures);
320 // TODO: Snapshot names
321 }
322
s_glGenRenderbuffersOES(void * self,GLsizei n,GLuint * renderbuffers)323 void GLESv1Decoder::s_glGenRenderbuffersOES(void* self, GLsizei n, GLuint* renderbuffers) {
324 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
325 ctx->glGenRenderbuffersOES(n, renderbuffers);
326 // TODO: Snapshot names
327 }
328
s_glGenFramebuffersOES(void * self,GLsizei n,GLuint * framebuffers)329 void GLESv1Decoder::s_glGenFramebuffersOES(void* self, GLsizei n, GLuint* framebuffers) {
330 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
331 ctx->glGenFramebuffersOES(n, framebuffers);
332 // TODO: Snapshot names
333 }
334
s_glGenVertexArraysOES(void * self,GLsizei n,GLuint * arrays)335 void GLESv1Decoder::s_glGenVertexArraysOES(void* self, GLsizei n, GLuint* arrays) {
336 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
337 ctx->glGenVertexArraysOES(n, arrays);
338 // TODO: Snapshot names
339 }
340
s_glDeleteBuffers(void * self,GLsizei n,const GLuint * buffers)341 void GLESv1Decoder::s_glDeleteBuffers(void* self, GLsizei n, const GLuint *buffers) {
342 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
343 ctx->glDeleteBuffers(n, buffers);
344 // TODO: Snapshot names
345 }
346
s_glDeleteTextures(void * self,GLsizei n,const GLuint * textures)347 void GLESv1Decoder::s_glDeleteTextures(void* self, GLsizei n, const GLuint *textures) {
348 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
349 ctx->glDeleteTextures(n, textures);
350 // TODO: Snapshot names
351 }
352
s_glDeleteRenderbuffersOES(void * self,GLsizei n,const GLuint * renderbuffers)353 void GLESv1Decoder::s_glDeleteRenderbuffersOES(void* self, GLsizei n, const GLuint* renderbuffers) {
354 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
355 ctx->glDeleteRenderbuffersOES(n, renderbuffers);
356 // TODO: Snapshot names
357 }
358
s_glDeleteFramebuffersOES(void * self,GLsizei n,const GLuint * framebuffers)359 void GLESv1Decoder::s_glDeleteFramebuffersOES(void* self, GLsizei n, const GLuint* framebuffers) {
360 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
361 ctx->glDeleteFramebuffersOES(n, framebuffers);
362 // TODO: Snapshot names
363 }
364
s_glDeleteVertexArraysOES(void * self,GLsizei n,const GLuint * arrays)365 void GLESv1Decoder::s_glDeleteVertexArraysOES(void* self, GLsizei n, const GLuint *arrays) {
366 GLESv1Decoder *ctx = (GLESv1Decoder *)self;
367 ctx->glDeleteVertexArraysOES(n, arrays);
368 // TODO: Snapshot names
369 }
370
s_getProc(const char * name,void * userData)371 void *GLESv1Decoder::s_getProc(const char *name, void *userData)
372 {
373 GLESv1Decoder *ctx = (GLESv1Decoder *)userData;
374
375 if (ctx == NULL || ctx->m_glesDso == NULL) {
376 return NULL;
377 }
378
379 void *func = NULL;
380 #ifdef USE_EGL_GETPROCADDRESS
381 func = (void *) eglGetProcAddress(name);
382 #endif
383 if (func == NULL) {
384 func = (void *)(ctx->m_glesDso->findSymbol(name));
385 }
386 return func;
387 }
388
389 } // namespace gl
390 } // namespace gfxstream
391