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