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 "GLDecoder.h"
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <EGL/egl.h>
21 #include <GLES/gl.h>
22 #include <GLES/glext.h>
23
SafePointerFromUInt(GLuint value)24 static inline void* SafePointerFromUInt(GLuint value) {
25 return (void*)(uintptr_t)value;
26 }
27
GLDecoder()28 GLDecoder::GLDecoder()
29 {
30 m_contextData = NULL;
31 m_glesDso = NULL;
32 }
33
~GLDecoder()34 GLDecoder::~GLDecoder()
35 {
36 if (m_glesDso != NULL) {
37 delete m_glesDso;
38 }
39 }
40
41
initGL(get_proc_func_t getProcFunc,void * getProcFuncData)42 int GLDecoder::initGL(get_proc_func_t getProcFunc, void *getProcFuncData)
43 {
44 if (getProcFunc == NULL) {
45 const char *libname = GLES_LIBNAME;
46 if (getenv(GLES_LIBNAME_VAR) != NULL) {
47 libname = getenv(GLES_LIBNAME_VAR);
48 }
49
50 m_glesDso = osUtils::dynLibrary::open(libname);
51 if (m_glesDso == NULL) {
52 fprintf(stderr, "Couldn't find %s \n", GLES_LIBNAME);
53 return -1;
54 }
55
56 this->initDispatchByName(s_getProc, this);
57 } else {
58 this->initDispatchByName(getProcFunc, getProcFuncData);
59 }
60
61 set_glGetCompressedTextureFormats(s_glGetCompressedTextureFormats);
62 set_glVertexPointerOffset(s_glVertexPointerOffset);
63 set_glColorPointerOffset(s_glColorPointerOffset);
64 set_glNormalPointerOffset(s_glNormalPointerOffset);
65 set_glTexCoordPointerOffset(s_glTexCoordPointerOffset);
66 set_glPointSizePointerOffset(s_glPointSizePointerOffset);
67 set_glWeightPointerOffset(s_glWeightPointerOffset);
68 set_glMatrixIndexPointerOffset(s_glMatrixIndexPointerOffset);
69
70 set_glVertexPointerData(s_glVertexPointerData);
71 set_glColorPointerData(s_glColorPointerData);
72 set_glNormalPointerData(s_glNormalPointerData);
73 set_glTexCoordPointerData(s_glTexCoordPointerData);
74 set_glPointSizePointerData(s_glPointSizePointerData);
75 set_glWeightPointerData(s_glWeightPointerData);
76 set_glMatrixIndexPointerData(s_glMatrixIndexPointerData);
77
78 set_glDrawElementsOffset(s_glDrawElementsOffset);
79 set_glDrawElementsData(s_glDrawElementsData);
80 set_glFinishRoundTrip(s_glFinishRoundTrip);
81
82 return 0;
83 }
84
s_glFinishRoundTrip(void * self)85 int GLDecoder::s_glFinishRoundTrip(void *self)
86 {
87 GLDecoder *ctx = (GLDecoder *)self;
88 ctx->glFinish();
89 return 0;
90 }
91
s_glVertexPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)92 void GLDecoder::s_glVertexPointerOffset(void *self, GLint size, GLenum type, GLsizei stride, GLuint offset)
93 {
94 GLDecoder *ctx = (GLDecoder *)self;
95 ctx->glVertexPointer(size, type, stride, SafePointerFromUInt(offset));
96 }
97
s_glColorPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)98 void GLDecoder::s_glColorPointerOffset(void *self, GLint size, GLenum type, GLsizei stride, GLuint offset)
99 {
100 GLDecoder *ctx = (GLDecoder *)self;
101 ctx->glColorPointer(size, type, stride, SafePointerFromUInt(offset));
102 }
103
s_glTexCoordPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)104 void GLDecoder::s_glTexCoordPointerOffset(void *self, GLint size, GLenum type, GLsizei stride, GLuint offset)
105 {
106 GLDecoder *ctx = (GLDecoder *)self;
107 ctx->glTexCoordPointer(size, type, stride, SafePointerFromUInt(offset));
108 }
109
s_glNormalPointerOffset(void * self,GLenum type,GLsizei stride,GLuint offset)110 void GLDecoder::s_glNormalPointerOffset(void *self, GLenum type, GLsizei stride, GLuint offset)
111 {
112 GLDecoder *ctx = (GLDecoder *)self;
113 ctx->glNormalPointer(type, stride, SafePointerFromUInt(offset));
114 }
115
s_glPointSizePointerOffset(void * self,GLenum type,GLsizei stride,GLuint offset)116 void GLDecoder::s_glPointSizePointerOffset(void *self, GLenum type, GLsizei stride, GLuint offset)
117 {
118 GLDecoder *ctx = (GLDecoder *)self;
119 ctx->glPointSizePointerOES(type, stride, SafePointerFromUInt(offset));
120 }
121
s_glWeightPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)122 void GLDecoder::s_glWeightPointerOffset(void * self, GLint size, GLenum type, GLsizei stride, GLuint offset)
123 {
124 GLDecoder *ctx = (GLDecoder *)self;
125 ctx->glWeightPointerOES(size, type, stride, SafePointerFromUInt(offset));
126 }
127
s_glMatrixIndexPointerOffset(void * self,GLint size,GLenum type,GLsizei stride,GLuint offset)128 void GLDecoder::s_glMatrixIndexPointerOffset(void * self, GLint size, GLenum type, GLsizei stride, GLuint offset)
129 {
130 GLDecoder *ctx = (GLDecoder *)self;
131 ctx->glMatrixIndexPointerOES(size, type, stride, SafePointerFromUInt(offset));
132 }
133
134
135
136 #define STORE_POINTER_DATA_OR_ABORT(location) \
137 if (ctx->m_contextData != NULL) { \
138 ctx->m_contextData->storePointerData((location), data, datalen); \
139 } else { \
140 return; \
141 }
142
s_glVertexPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)143 void GLDecoder::s_glVertexPointerData(void *self, GLint size, GLenum type, GLsizei stride, void *data, GLuint datalen)
144 {
145 GLDecoder *ctx = (GLDecoder *)self;
146
147 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::VERTEX_LOCATION);
148
149 ctx->glVertexPointer(size, type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::VERTEX_LOCATION));
150 }
151
s_glColorPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)152 void GLDecoder::s_glColorPointerData(void *self, GLint size, GLenum type, GLsizei stride, void *data, GLuint datalen)
153 {
154 GLDecoder *ctx = (GLDecoder *)self;
155
156 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::COLOR_LOCATION);
157
158 ctx->glColorPointer(size, type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::COLOR_LOCATION));
159 }
160
s_glTexCoordPointerData(void * self,GLint unit,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)161 void GLDecoder::s_glTexCoordPointerData(void *self, GLint unit, GLint size, GLenum type, GLsizei stride, void *data, GLuint datalen)
162 {
163 GLDecoder *ctx = (GLDecoder *)self;
164 STORE_POINTER_DATA_OR_ABORT((GLDecoderContextData::PointerDataLocation)
165 (GLDecoderContextData::TEXCOORD0_LOCATION + unit));
166
167 ctx->glTexCoordPointer(size, type, 0,
168 ctx->m_contextData->pointerData((GLDecoderContextData::PointerDataLocation)
169 (GLDecoderContextData::TEXCOORD0_LOCATION + unit)));
170 }
171
s_glNormalPointerData(void * self,GLenum type,GLsizei stride,void * data,GLuint datalen)172 void GLDecoder::s_glNormalPointerData(void *self, GLenum type, GLsizei stride, void *data, GLuint datalen)
173 {
174 GLDecoder *ctx = (GLDecoder *)self;
175
176 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::NORMAL_LOCATION);
177
178 ctx->glNormalPointer(type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::NORMAL_LOCATION));
179 }
180
s_glPointSizePointerData(void * self,GLenum type,GLsizei stride,void * data,GLuint datalen)181 void GLDecoder::s_glPointSizePointerData(void *self, GLenum type, GLsizei stride, void *data, GLuint datalen)
182 {
183 GLDecoder *ctx = (GLDecoder *)self;
184
185 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::POINTSIZE_LOCATION);
186
187 ctx->glPointSizePointerOES(type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::POINTSIZE_LOCATION));
188 }
189
s_glWeightPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)190 void GLDecoder::s_glWeightPointerData(void * self, GLint size, GLenum type, GLsizei stride, void * data, GLuint datalen)
191 {
192 GLDecoder *ctx = (GLDecoder *)self;
193
194 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::WEIGHT_LOCATION);
195
196 ctx->glWeightPointerOES(size, type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::WEIGHT_LOCATION));
197 }
198
s_glMatrixIndexPointerData(void * self,GLint size,GLenum type,GLsizei stride,void * data,GLuint datalen)199 void GLDecoder::s_glMatrixIndexPointerData(void * self, GLint size, GLenum type, GLsizei stride, void * data, GLuint datalen)
200 {
201 GLDecoder *ctx = (GLDecoder *)self;
202
203 STORE_POINTER_DATA_OR_ABORT(GLDecoderContextData::MATRIXINDEX_LOCATION);
204
205 ctx->glMatrixIndexPointerOES(size, type, 0, ctx->m_contextData->pointerData(GLDecoderContextData::MATRIXINDEX_LOCATION));
206 }
207
s_glDrawElementsOffset(void * self,GLenum mode,GLsizei count,GLenum type,GLuint offset)208 void GLDecoder::s_glDrawElementsOffset(void *self, GLenum mode, GLsizei count, GLenum type, GLuint offset)
209 {
210 GLDecoder *ctx = (GLDecoder *)self;
211 ctx->glDrawElements(mode, count, type, SafePointerFromUInt(offset));
212 }
213
s_glDrawElementsData(void * self,GLenum mode,GLsizei count,GLenum type,void * data,GLuint datalen)214 void GLDecoder::s_glDrawElementsData(void *self, GLenum mode, GLsizei count, GLenum type, void * data, GLuint datalen)
215 {
216 GLDecoder *ctx = (GLDecoder *)self;
217 ctx->glDrawElements(mode, count, type, data);
218 }
219
s_glGetCompressedTextureFormats(void * self,GLint count,GLint * data)220 void GLDecoder::s_glGetCompressedTextureFormats(void *self, GLint count, GLint *data)
221 {
222 GLDecoder *ctx = (GLDecoder *) self;
223 ctx->glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, data);
224 }
225
s_getProc(const char * name,void * userData)226 void *GLDecoder::s_getProc(const char *name, void *userData)
227 {
228 GLDecoder *ctx = (GLDecoder *)userData;
229
230 if (ctx == NULL || ctx->m_glesDso == NULL) {
231 return NULL;
232 }
233
234 void *func = NULL;
235 #ifdef USE_EGL_GETPROCADDRESS
236 func = (void *) eglGetProcAddress(name);
237 #endif
238 if (func == NULL) {
239 func = (void *)(ctx->m_glesDso->findSymbol(name));
240 }
241 return func;
242 }
243