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