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 "EGLImage.h"
19 #include "GL2Encoder.h"
20 #include "GLES/gl.h"
21 #include "GLES/glext.h"
22 #include "HostConnection.h"
23 #include "ThreadInfo.h"
24 
25 //XXX: fix this macro to get the context from fast tls path
26 #define GET_CONTEXT GL2Encoder * ctx = getEGLThreadInfo()->hostConn->gl2Encoder();
27 
28 #include "gl2_entry.cpp"
29 
30 //The functions table
31 #include "gl2_ftable.h"
32 
33 
34 static EGLClient_eglInterface * s_egl = NULL;
35 static EGLClient_glesInterface * s_gl = NULL;
36 
37 #define DEFINE_AND_VALIDATE_HOST_CONNECTION(ret)                     \
38     HostConnection* hostCon = HostConnection::get();                 \
39     if (!hostCon) {                                                  \
40         ALOGE("egl: Failed to get host connection\n");               \
41         return ret;                                                  \
42     }                                                                \
43     renderControl_encoder_context_t* rcEnc = hostCon->rcEncoder();   \
44     if (!rcEnc) {                                                    \
45         ALOGE("egl: Failed to get renderControl encoder context\n"); \
46         return ret;                                                  \
47     }                                                                \
48     auto* grallocHelper = hostCon->grallocHelper();                  \
49     if (!grallocHelper) {                                            \
50         ALOGE("egl: Failed to get grallocHelper\n");                 \
51         return ret;                                                  \
52     }                                                                \
53     auto* anwHelper = hostCon->anwHelper();                          \
54     if (!anwHelper) {                                                \
55         ALOGE("egl: Failed to get anwHelper\n");                     \
56         return ret;                                                  \
57     }
58 
59 //GL extensions
glEGLImageTargetTexture2DOES(void * self,GLenum target,GLeglImageOES img)60 void glEGLImageTargetTexture2DOES(void * self, GLenum target, GLeglImageOES img)
61 {
62     (void)self;
63     (void)target;
64 
65     EGLImage_t *image = (EGLImage_t*)img;
66     GLeglImageOES hostImage = reinterpret_cast<GLeglImageOES>((intptr_t)image->host_egl_image);
67 
68     GET_CONTEXT;
69     DEFINE_AND_VALIDATE_HOST_CONNECTION();
70 
71     if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
72         EGLClientBuffer buffer = image->buffer;
73 
74         if (!anwHelper->isValid(buffer)) {
75             ALOGE("Invalid native buffer.");
76             return;
77         }
78 
79         ctx->override2DTextureTarget(target);
80         ctx->associateEGLImage(target, hostImage, image->width, image->height);
81 
82         const int hostHandle = anwHelper->getHostHandle(buffer, grallocHelper);
83         rcEnc->rcBindTexture(rcEnc, hostHandle);
84         ctx->restore2DTextureTarget(target);
85     } else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
86         ctx->override2DTextureTarget(target);
87         ctx->associateEGLImage(target, hostImage, image->width, image->height);
88         ctx->m_glEGLImageTargetTexture2DOES_enc(self, GL_TEXTURE_2D, hostImage);
89         ctx->restore2DTextureTarget(target);
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     //TODO: check error - we don't have a way to set gl error
99     EGLImage_t *image = (EGLImage_t*)img;
100     GLeglImageOES hostImage = reinterpret_cast<GLeglImageOES>((intptr_t)image->host_egl_image);
101 
102     if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
103         DEFINE_AND_VALIDATE_HOST_CONNECTION();
104 
105         EGLClientBuffer buffer = image->buffer;
106         if (!anwHelper->isValid(buffer)) {
107             ALOGE("Invalid native buffer.");
108             return;
109         }
110 
111         GET_CONTEXT;
112         ctx->associateEGLImage(target, hostImage, image->width, image->height);
113 
114         const int hostHandle = anwHelper->getHostHandle(buffer, grallocHelper);
115         rcEnc->rcBindRenderbuffer(rcEnc, hostHandle);
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<gl2_num_funcs; i++) {
127         if (!strcmp(gl2_funcs_by_name[i].name, procname)) {
128             return gl2_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 extern "C" {
init_emul_gles(EGLClient_eglInterface * eglIface)177 EGLClient_glesInterface * init_emul_gles(EGLClient_eglInterface *eglIface)
178 {
179     s_egl = eglIface;
180 
181     if (!s_gl) {
182         s_gl = new EGLClient_glesInterface();
183         s_gl->getProcAddress = getProcAddress;
184         s_gl->finish = finish;
185         s_gl->init = init;
186         s_gl->getIntegerv = getIntegerv;
187     }
188 
189     return s_gl;
190 }
191 } //extern
192