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