• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
50 //GL extensions
glEGLImageTargetTexture2DOES(void * self,GLenum target,GLeglImageOES image)51 void glEGLImageTargetTexture2DOES(void * self, GLenum target, GLeglImageOES image)
52 {
53     (void)self;
54 
55     DBG("glEGLImageTargetTexture2DOES v1 target=%#x image=%p", target, image);
56     //TODO: check error - we don't have a way to set gl error
57     android_native_buffer_t* native_buffer = (android_native_buffer_t*)image;
58 
59     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
60         return;
61     }
62 
63     if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
64         return;
65     }
66 
67     GET_CONTEXT;
68     DEFINE_AND_VALIDATE_HOST_CONNECTION();
69 
70     ctx->override2DTextureTarget(target);
71     rcEnc->rcBindTexture(rcEnc,
72             ((cb_handle_t *)(native_buffer->handle))->hostHandle);
73     ctx->restore2DTextureTarget();
74 
75     return;
76 }
77 
glEGLImageTargetRenderbufferStorageOES(void * self,GLenum target,GLeglImageOES image)78 void glEGLImageTargetRenderbufferStorageOES(void *self, GLenum target, GLeglImageOES image)
79 {
80     (void)self;
81     (void)target;
82 
83     DBG("glEGLImageTargetRenderbufferStorageOES v1 target=%#x image=%p",
84             target, image);
85     //TODO: check error - we don't have a way to set gl error
86     android_native_buffer_t* native_buffer = (android_native_buffer_t*)image;
87 
88     if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) {
89         return;
90     }
91 
92     if (native_buffer->common.version != sizeof(android_native_buffer_t)) {
93         return;
94     }
95 
96     DEFINE_AND_VALIDATE_HOST_CONNECTION();
97     rcEnc->rcBindRenderbuffer(rcEnc,
98             ((cb_handle_t *)(native_buffer->handle))->hostHandle);
99 
100     return;
101 }
102 
getProcAddress(const char * procname)103 void * getProcAddress(const char * procname)
104 {
105     // search in GL function table
106     for (int i=0; i<gl_num_funcs; i++) {
107         if (!strcmp(gl_funcs_by_name[i].name, procname)) {
108             return gl_funcs_by_name[i].proc;
109         }
110     }
111     return NULL;
112 }
113 
finish()114 void finish()
115 {
116     glFinish();
117 }
118 
my_glGetString(void * self,GLenum name)119 const GLubyte *my_glGetString (void *self, GLenum name)
120 {
121     (void)self;
122 
123     if (s_egl) {
124         return (const GLubyte*)s_egl->getGLString(name);
125     }
126     return NULL;
127 }
128 
init()129 void init()
130 {
131     GET_CONTEXT;
132     ctx->glEGLImageTargetTexture2DOES = &glEGLImageTargetTexture2DOES;
133     ctx->glEGLImageTargetRenderbufferStorageOES = &glEGLImageTargetRenderbufferStorageOES;
134     ctx->glGetString = &my_glGetString;
135 }
136 
137 extern "C" {
init_emul_gles(EGLClient_eglInterface * eglIface)138 EGLClient_glesInterface * init_emul_gles(EGLClient_eglInterface *eglIface)
139 {
140     s_egl = eglIface;
141 
142     if (!s_gl) {
143         s_gl = new EGLClient_glesInterface();
144         s_gl->getProcAddress = getProcAddress;
145         s_gl->finish = finish;
146         s_gl->init = init;
147     }
148 
149     return s_gl;
150 }
151 } //extern
152 
153 
154