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