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