1 /*
2 * Copyright 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 
17 #include "GL2Decoder.h"
18 #include <EGL/egl.h>
19 #include <GLES2/gl2.h>
20 #include <GLES2/gl2ext.h>
21 
SafePointerFromUInt(GLuint value)22 static inline void* SafePointerFromUInt(GLuint value) {
23   return (void*)(uintptr_t)value;
24 }
25 
GL2Decoder()26 GL2Decoder::GL2Decoder()
27 {
28     m_contextData = NULL;
29     m_GL2library = NULL;
30 }
31 
~GL2Decoder()32 GL2Decoder::~GL2Decoder()
33 {
34     delete m_GL2library;
35 }
36 
s_getProc(const char * name,void * userData)37 void *GL2Decoder::s_getProc(const char *name, void *userData)
38 {
39     GL2Decoder *ctx = (GL2Decoder *) userData;
40 
41     if (ctx == NULL || ctx->m_GL2library == NULL) {
42         return NULL;
43     }
44 
45     void *func = NULL;
46 #ifdef USE_EGL_GETPROCADDRESS
47     func = (void *) eglGetProcAddress(name);
48 #endif
49     if (func == NULL) {
50         func = (void *) ctx->m_GL2library->findSymbol(name);
51     }
52     return func;
53 }
54 
initGL(get_proc_func_t getProcFunc,void * getProcFuncData)55 int GL2Decoder::initGL(get_proc_func_t getProcFunc, void *getProcFuncData)
56 {
57     if (getProcFunc == NULL) {
58         const char *libname = GLES2_LIBNAME;
59         if (getenv(GLES2_LIBNAME_VAR) != NULL) {
60             libname = getenv(GLES2_LIBNAME_VAR);
61         }
62 
63         m_GL2library = osUtils::dynLibrary::open(libname);
64         if (m_GL2library == NULL) {
65             fprintf(stderr, "%s: Couldn't find %s \n", __FUNCTION__, libname);
66             return -1;
67         }
68         this->initDispatchByName(s_getProc, this);
69     } else {
70         this->initDispatchByName(getProcFunc, getProcFuncData);
71     }
72 
73     set_glGetCompressedTextureFormats(s_glGetCompressedTextureFormats);
74     set_glVertexAttribPointerData(s_glVertexAttribPointerData);
75     set_glVertexAttribPointerOffset(s_glVertexAttribPointerOffset);
76 
77     set_glDrawElementsOffset(s_glDrawElementsOffset);
78     set_glDrawElementsData(s_glDrawElementsData);
79     set_glShaderString(s_glShaderString);
80     set_glFinishRoundTrip(s_glFinishRoundTrip);
81     return 0;
82 
83 }
84 
s_glFinishRoundTrip(void * self)85 int GL2Decoder::s_glFinishRoundTrip(void *self)
86 {
87     GL2Decoder *ctx = (GL2Decoder *)self;
88     ctx->glFinish();
89     return 0;
90 }
91 
s_glGetCompressedTextureFormats(void * self,int count,GLint * formats)92 void GL2Decoder::s_glGetCompressedTextureFormats(void *self, int count, GLint *formats)
93 {
94     GL2Decoder *ctx = (GL2Decoder *) self;
95 
96     int nFormats;
97     ctx->glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &nFormats);
98     if (nFormats > count) {
99         fprintf(stderr, "%s: GetCompressedTextureFormats: The requested number of formats does not match the number that is reported by OpenGL\n", __FUNCTION__);
100     } else {
101         ctx->glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats);
102     }
103 }
104 
s_glVertexAttribPointerData(void * self,GLuint indx,GLint size,GLenum type,GLboolean normalized,GLsizei stride,void * data,GLuint datalen)105 void GL2Decoder::s_glVertexAttribPointerData(void *self, GLuint indx, GLint size, GLenum type,
106                                              GLboolean normalized, GLsizei stride,  void * data, GLuint datalen)
107 {
108     GL2Decoder *ctx = (GL2Decoder *) self;
109     if (ctx->m_contextData != NULL) {
110         ctx->m_contextData->storePointerData(indx, data, datalen);
111         // note - the stride of the data is always zero when it comes out of the codec.
112         // See gl2.attrib for the packing function call.
113         ctx->glVertexAttribPointer(indx, size, type, normalized, 0, ctx->m_contextData->pointerData(indx));
114     }
115 }
116 
s_glVertexAttribPointerOffset(void * self,GLuint indx,GLint size,GLenum type,GLboolean normalized,GLsizei stride,GLuint data)117 void GL2Decoder::s_glVertexAttribPointerOffset(void *self, GLuint indx, GLint size, GLenum type,
118                                                GLboolean normalized, GLsizei stride,  GLuint data)
119 {
120     GL2Decoder *ctx = (GL2Decoder *) self;
121     ctx->glVertexAttribPointer(indx, size, type, normalized, stride, SafePointerFromUInt(data));
122 }
123 
124 
s_glDrawElementsData(void * self,GLenum mode,GLsizei count,GLenum type,void * data,GLuint datalen)125 void GL2Decoder::s_glDrawElementsData(void *self, GLenum mode, GLsizei count, GLenum type, void * data, GLuint datalen)
126 {
127     GL2Decoder *ctx = (GL2Decoder *)self;
128     ctx->glDrawElements(mode, count, type, data);
129 }
130 
131 
s_glDrawElementsOffset(void * self,GLenum mode,GLsizei count,GLenum type,GLuint offset)132 void GL2Decoder::s_glDrawElementsOffset(void *self, GLenum mode, GLsizei count, GLenum type, GLuint offset)
133 {
134     GL2Decoder *ctx = (GL2Decoder *)self;
135     ctx->glDrawElements(mode, count, type, SafePointerFromUInt(offset));
136 }
137 
s_glShaderString(void * self,GLuint shader,const GLchar * string,GLsizei len)138 void GL2Decoder::s_glShaderString(void *self, GLuint shader, const GLchar* string, GLsizei len)
139 {
140     GL2Decoder *ctx = (GL2Decoder *)self;
141     ctx->glShaderSource(shader, 1, &string, NULL);
142 }
143