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