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 "EGLClientIface.h"
18 #include "HostConnection.h"
19 #include "GLEncoder.h"
20 #include "GLES/gl.h"
21 #include "GLES/glext.h"
22 #include "ErrorLog.h"
23 #include "ThreadInfo.h"
24 #include "EGLImage.h"
25
26
27 //XXX: fix this macro to get the context from fast tls path
28 #define GET_CONTEXT GLEncoder * ctx = getEGLThreadInfo()->hostConn->glEncoder();
29
30 #include "gl_entry.cpp"
31
32 //The functions table
33 #include "gl_ftable.h"
34
35 static EGLClient_eglInterface * s_egl = NULL;
36 static EGLClient_glesInterface * s_gl = NULL;
37
38 #define DEFINE_AND_VALIDATE_HOST_CONNECTION(ret) \
39 HostConnection *hostCon = HostConnection::get(); \
40 if (!hostCon) { \
41 ALOGE("egl: Failed to get host connection\n"); \
42 return ret; \
43 } \
44 renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \
45 if (!rcEnc) { \
46 ALOGE("egl: Failed to get renderControl encoder context\n"); \
47 return ret; \
48 } \
49 Gralloc *grallocHelper = hostCon->grallocHelper(); \
50 if (!grallocHelper) { \
51 ALOGE("egl: Failed to get grallocHelper\n"); \
52 return ret; \
53 }
54
55 //GL extensions
glEGLImageTargetTexture2DOES(void * self,GLenum target,GLeglImageOES img)56 void glEGLImageTargetTexture2DOES(void * self, GLenum target, GLeglImageOES img)
57 {
58 (void)self;
59
60 DBG("glEGLImageTargetTexture2DOES v1 target=%#x img=%p", target, img);
61
62 EGLImage_t *image = (EGLImage_t*)img;
63
64 if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
65 //TODO: check error - we don't have a way to set gl error
66 android_native_buffer_t* native_buffer = image->native_buffer;
67
68 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
69 return;
70 }
71
72 if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
73 return;
74 }
75
76 GET_CONTEXT;
77 DEFINE_AND_VALIDATE_HOST_CONNECTION();
78
79 ctx->override2DTextureTarget(target);
80 rcEnc->rcBindTexture(rcEnc,
81 grallocHelper->getHostHandle(native_buffer->handle));
82 ctx->restore2DTextureTarget();
83 }
84 else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
85 GET_CONTEXT;
86 ctx->override2DTextureTarget(target);
87 GLeglImageOES hostImage = reinterpret_cast<GLeglImageOES>((intptr_t)image->host_egl_image);
88 ctx->m_glEGLImageTargetTexture2DOES_enc(self, target, hostImage);
89 ctx->restore2DTextureTarget();
90 }
91 }
92
glEGLImageTargetRenderbufferStorageOES(void * self,GLenum target,GLeglImageOES img)93 void glEGLImageTargetRenderbufferStorageOES(void *self, GLenum target, GLeglImageOES img)
94 {
95 (void)self;
96 (void)target;
97
98 DBG("glEGLImageTargetRenderbufferStorageOES v1 image=%p\n", img);
99 //TODO: check error - we don't have a way to set gl error
100 EGLImage_t *image = (EGLImage_t*)img;
101
102 if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
103 android_native_buffer_t* native_buffer = ((EGLImage_t*)image)->native_buffer;
104
105 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
106 return;
107 }
108
109 if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
110 return;
111 }
112
113 DEFINE_AND_VALIDATE_HOST_CONNECTION();
114 rcEnc->rcBindRenderbuffer(rcEnc,
115 grallocHelper->getHostHandle(native_buffer->handle));
116 } else {
117 //TODO
118 }
119
120 return;
121 }
122
getProcAddress(const char * procname)123 void * getProcAddress(const char * procname)
124 {
125 // search in GL function table
126 for (int i=0; i<gl_num_funcs; i++) {
127 if (!strcmp(gl_funcs_by_name[i].name, procname)) {
128 return gl_funcs_by_name[i].proc;
129 }
130 }
131 return NULL;
132 }
133
finish()134 void finish()
135 {
136 glFinish();
137 }
138
getIntegerv(unsigned int pname,int * param)139 void getIntegerv(unsigned int pname, int* param)
140 {
141 glGetIntegerv((GLenum)pname, (GLint*)param);
142 }
143
my_glGetString(void * self,GLenum name)144 const GLubyte *my_glGetString (void *self, GLenum name)
145 {
146 (void)self;
147
148 //see ref in https://www.khronos.org/opengles/sdk/docs/man
149 //name in glGetString can be one of the following five values
150 switch (name) {
151 case GL_VERSION:
152 case GL_VENDOR:
153 case GL_RENDERER:
154 case GL_SHADING_LANGUAGE_VERSION:
155 case GL_EXTENSIONS:
156 if (s_egl) {
157 return (const GLubyte*)s_egl->getGLString(name);
158 }
159 break;
160 default:
161 GET_CONTEXT;
162 ctx->setError(GL_INVALID_ENUM);
163 break;
164 }
165 return NULL;
166 }
167
init()168 void init()
169 {
170 GET_CONTEXT;
171 ctx->m_glEGLImageTargetTexture2DOES_enc = ctx->glEGLImageTargetTexture2DOES;
172 ctx->glEGLImageTargetTexture2DOES = &glEGLImageTargetTexture2DOES;
173 ctx->glEGLImageTargetRenderbufferStorageOES = &glEGLImageTargetRenderbufferStorageOES;
174 ctx->glGetString = &my_glGetString;
175 }
176
177 extern "C" {
init_emul_gles(EGLClient_eglInterface * eglIface)178 EGLClient_glesInterface * init_emul_gles(EGLClient_eglInterface *eglIface)
179 {
180 s_egl = eglIface;
181
182 if (!s_gl) {
183 s_gl = new EGLClient_glesInterface();
184 s_gl->getProcAddress = getProcAddress;
185 s_gl->finish = finish;
186 s_gl->init = init;
187 s_gl->getIntegerv = getIntegerv;
188 }
189
190 return s_gl;
191 }
192 } //extern
193
194
195