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