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     auto* grallocHelper = hostCon->grallocHelper();                  \
50     if (!grallocHelper) {                                            \
51         ALOGE("egl: Failed to get grallocHelper\n");                 \
52         return ret;                                                  \
53     }                                                                \
54     auto* anwHelper = hostCon->anwHelper();                          \
55     if (!anwHelper) {                                                \
56         ALOGE("egl: Failed to get anwHelper\n");                     \
57         return ret;                                                  \
58     }
59 
60 //GL extensions
glEGLImageTargetTexture2DOES(void * self,GLenum target,GLeglImageOES img)61 void glEGLImageTargetTexture2DOES(void * self, GLenum target, GLeglImageOES img)
62 {
63     (void)self;
64     (void)target;
65 
66     DBG("glEGLImageTargetTexture2DOES v2 target=%#x img=%p\n", target, img);
67 
68     EGLImage_t *image = (EGLImage_t*)img;
69     GLeglImageOES hostImage = reinterpret_cast<GLeglImageOES>((intptr_t)image->host_egl_image);
70 
71     GET_CONTEXT;
72     DEFINE_AND_VALIDATE_HOST_CONNECTION();
73 
74     if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
75         EGLClientBuffer buffer = image->buffer;
76 
77         if (!anwHelper->isValid(buffer)) {
78             ALOGE("Invalid native buffer.");
79             return;
80         }
81 
82         ctx->override2DTextureTarget(target);
83         ctx->associateEGLImage(target, hostImage, image->width, image->height);
84 
85         const int hostHandle = anwHelper->getHostHandle(buffer, grallocHelper);
86         rcEnc->rcBindTexture(rcEnc, hostHandle);
87         ctx->restore2DTextureTarget(target);
88     } else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
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         DEFINE_AND_VALIDATE_HOST_CONNECTION();
108 
109         EGLClientBuffer buffer = image->buffer;
110         if (!anwHelper->isValid(buffer)) {
111             ALOGE("Invalid native buffer.");
112             return;
113         }
114 
115         GET_CONTEXT;
116         ctx->associateEGLImage(target, hostImage, image->width, image->height);
117 
118         const int hostHandle = anwHelper->getHostHandle(buffer, grallocHelper);
119         rcEnc->rcBindRenderbuffer(rcEnc, hostHandle);
120     } else {
121         //TODO
122     }
123 
124     return;
125 }
126 
getProcAddress(const char * procname)127 void * getProcAddress(const char * procname)
128 {
129     // search in GL function table
130     for (int i=0; i<gl2_num_funcs; i++) {
131         if (!strcmp(gl2_funcs_by_name[i].name, procname)) {
132             return gl2_funcs_by_name[i].proc;
133         }
134     }
135     return NULL;
136 }
137 
finish()138 void finish()
139 {
140     glFinish();
141 }
142 
getIntegerv(unsigned int pname,int * param)143 void getIntegerv(unsigned int pname, int* param)
144 {
145     glGetIntegerv((GLenum)pname, (GLint*)param);
146 }
147 
my_glGetString(void * self,GLenum name)148 const GLubyte *my_glGetString (void *self, GLenum name)
149 {
150     (void)self;
151 
152     //see ref in https://www.khronos.org/opengles/sdk/docs/man
153     //name in glGetString can be one of the following five values
154     switch (name) {
155         case GL_VERSION:
156         case GL_VENDOR:
157         case GL_RENDERER:
158         case GL_SHADING_LANGUAGE_VERSION:
159         case GL_EXTENSIONS:
160             if (s_egl) {
161                 return (const GLubyte*)s_egl->getGLString(name);
162             }
163             break;
164         default:
165             GET_CONTEXT;
166             ctx->setError(GL_INVALID_ENUM);
167             break;
168     }
169     return NULL;
170 }
171 
init()172 void init()
173 {
174     GET_CONTEXT;
175     ctx->m_glEGLImageTargetTexture2DOES_enc = ctx->glEGLImageTargetTexture2DOES;
176     ctx->glEGLImageTargetTexture2DOES = &glEGLImageTargetTexture2DOES;
177     ctx->glEGLImageTargetRenderbufferStorageOES = &glEGLImageTargetRenderbufferStorageOES;
178     ctx->glGetString = &my_glGetString;
179 }
180 extern "C" {
init_emul_gles(EGLClient_eglInterface * eglIface)181 EGLClient_glesInterface * init_emul_gles(EGLClient_eglInterface *eglIface)
182 {
183     s_egl = eglIface;
184 
185     if (!s_gl) {
186         s_gl = new EGLClient_glesInterface();
187         s_gl->getProcAddress = getProcAddress;
188         s_gl->finish = finish;
189         s_gl->init = init;
190         s_gl->getIntegerv = getIntegerv;
191     }
192 
193     return s_gl;
194 }
195 } //extern
196