Loader.cpp revision 1cadb25da1ed875bdd078270e642966724a0c39a
1/* 2 ** Copyright 2007, 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 <ctype.h> 18#include <stdlib.h> 19#include <stdio.h> 20#include <string.h> 21#include <errno.h> 22#include <dlfcn.h> 23#include <limits.h> 24 25#include <cutils/log.h> 26 27#include <EGL/egl.h> 28 29#include "egldefs.h" 30#include "glesv2dbg.h" 31#include "hooks.h" 32#include "Loader.h" 33 34// ---------------------------------------------------------------------------- 35namespace android { 36// ---------------------------------------------------------------------------- 37 38 39/* 40 * EGL drivers are called 41 * 42 * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so 43 * 44 */ 45 46ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) 47 48// ---------------------------------------------------------------------------- 49 50Loader::driver_t::driver_t(void* gles) 51{ 52 dso[0] = gles; 53 for (size_t i=1 ; i<NELEM(dso) ; i++) 54 dso[i] = 0; 55} 56 57Loader::driver_t::~driver_t() 58{ 59 for (size_t i=0 ; i<NELEM(dso) ; i++) { 60 if (dso[i]) { 61 dlclose(dso[i]); 62 dso[i] = 0; 63 } 64 } 65} 66 67status_t Loader::driver_t::set(void* hnd, int32_t api) 68{ 69 switch (api) { 70 case EGL: 71 dso[0] = hnd; 72 break; 73 case GLESv1_CM: 74 dso[1] = hnd; 75 break; 76 case GLESv2: 77 dso[2] = hnd; 78 break; 79 default: 80 return BAD_INDEX; 81 } 82 return NO_ERROR; 83} 84 85// ---------------------------------------------------------------------------- 86 87Loader::entry_t::entry_t(int dpy, int impl, const char* tag) 88 : dpy(dpy), impl(impl), tag(tag) { 89} 90 91// ---------------------------------------------------------------------------- 92 93Loader::Loader() 94{ 95 char line[256]; 96 char tag[256]; 97 FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r"); 98 if (cfg == NULL) { 99 // default config 100 LOGD("egl.cfg not found, using default config"); 101 gConfig.add( entry_t(0, 0, "android") ); 102 } else { 103 while (fgets(line, 256, cfg)) { 104 int dpy; 105 int impl; 106 if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) { 107 //LOGD(">>> %u %u %s", dpy, impl, tag); 108 gConfig.add( entry_t(dpy, impl, tag) ); 109 } 110 } 111 fclose(cfg); 112 } 113} 114 115Loader::~Loader() 116{ 117 StopDebugServer(); 118} 119 120const char* Loader::getTag(int dpy, int impl) 121{ 122 const Vector<entry_t>& cfgs(gConfig); 123 const size_t c = cfgs.size(); 124 for (size_t i=0 ; i<c ; i++) { 125 if (dpy == cfgs[i].dpy) 126 if (impl == cfgs[i].impl) 127 return cfgs[i].tag.string(); 128 } 129 return 0; 130} 131 132void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx) 133{ 134 /* 135 * TODO: if we don't find display/0, then use 0/0 136 * (0/0 should always work) 137 */ 138 139 void* dso; 140 int index = int(display); 141 driver_t* hnd = 0; 142 143 char const* tag = getTag(index, impl); 144 if (tag) { 145 dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2); 146 if (dso) { 147 hnd = new driver_t(dso); 148 } else { 149 // Always load EGL first 150 dso = load_driver("EGL", tag, cnx, EGL); 151 if (dso) { 152 hnd = new driver_t(dso); 153 154 // TODO: make this more automated 155 hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM ); 156 157 hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 ); 158 } 159 } 160 } 161 162 LOG_FATAL_IF(!index && !impl && !hnd, 163 "couldn't find the default OpenGL ES implementation " 164 "for default display"); 165 166 return (void*)hnd; 167} 168 169status_t Loader::close(void* driver) 170{ 171 driver_t* hnd = (driver_t*)driver; 172 delete hnd; 173 return NO_ERROR; 174} 175 176void Loader::init_api(void* dso, 177 char const * const * api, 178 __eglMustCastToProperFunctionPointerType* curr, 179 getProcAddressType getProcAddress) 180{ 181 const size_t SIZE = 256; 182 char scrap[SIZE]; 183 while (*api) { 184 char const * name = *api; 185 __eglMustCastToProperFunctionPointerType f = 186 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); 187 if (f == NULL) { 188 // couldn't find the entry-point, use eglGetProcAddress() 189 f = getProcAddress(name); 190 } 191 if (f == NULL) { 192 // Try without the OES postfix 193 ssize_t index = ssize_t(strlen(name)) - 3; 194 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { 195 strncpy(scrap, name, index); 196 scrap[index] = 0; 197 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); 198 //LOGD_IF(f, "found <%s> instead", scrap); 199 } 200 } 201 if (f == NULL) { 202 // Try with the OES postfix 203 ssize_t index = ssize_t(strlen(name)) - 3; 204 if (index>0 && strcmp(name+index, "OES")) { 205 snprintf(scrap, SIZE, "%sOES", name); 206 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); 207 //LOGD_IF(f, "found <%s> instead", scrap); 208 } 209 } 210 if (f == NULL) { 211 //LOGD("%s", name); 212 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; 213 } 214 *curr++ = f; 215 api++; 216 } 217} 218 219void *Loader::load_driver(const char* kind, const char *tag, 220 egl_connection_t* cnx, uint32_t mask) 221{ 222 char driver_absolute_path[PATH_MAX]; 223 const char* const search1 = "/vendor/lib/egl/lib%s_%s.so"; 224 const char* const search2 = "/system/lib/egl/lib%s_%s.so"; 225 226 snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag); 227 if (access(driver_absolute_path, R_OK)) { 228 snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag); 229 if (access(driver_absolute_path, R_OK)) { 230 // this happens often, we don't want to log an error 231 return 0; 232 } 233 } 234 235 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); 236 if (dso == 0) { 237 const char* err = dlerror(); 238 LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); 239 return 0; 240 } 241 242 LOGD("loaded %s", driver_absolute_path); 243 244 if (mask & EGL) { 245 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); 246 247 LOGE_IF(!getProcAddress, 248 "can't find eglGetProcAddress() in %s", driver_absolute_path); 249 250 egl_t* egl = &cnx->egl; 251 __eglMustCastToProperFunctionPointerType* curr = 252 (__eglMustCastToProperFunctionPointerType*)egl; 253 char const * const * api = egl_names; 254 while (*api) { 255 char const * name = *api; 256 __eglMustCastToProperFunctionPointerType f = 257 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); 258 if (f == NULL) { 259 // couldn't find the entry-point, use eglGetProcAddress() 260 f = getProcAddress(name); 261 if (f == NULL) { 262 f = (__eglMustCastToProperFunctionPointerType)0; 263 } 264 } 265 *curr++ = f; 266 api++; 267 } 268 } 269 270 if (mask & GLESv1_CM) { 271 init_api(dso, gl_names, 272 (__eglMustCastToProperFunctionPointerType*) 273 &cnx->hooks[GLESv1_INDEX]->gl, 274 getProcAddress); 275 } 276 277 if (mask & GLESv2) { 278 init_api(dso, gl_names, 279 (__eglMustCastToProperFunctionPointerType*) 280 &cnx->hooks[GLESv2_INDEX]->gl, 281 getProcAddress); 282 } 283 284 return dso; 285} 286 287// ---------------------------------------------------------------------------- 288}; // namespace android 289// ---------------------------------------------------------------------------- 290