Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 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> |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 27 | |
| 28 | #include <EGL/egl.h> |
| 29 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 30 | #include "egldefs.h" |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 31 | #include "glestrace.h" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 32 | #include "hooks.h" |
| 33 | #include "Loader.h" |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | namespace android { |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * EGL drivers are called |
| 42 | * |
| 43 | * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so |
| 44 | * |
| 45 | */ |
| 46 | |
| 47 | ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) |
| 48 | |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 49 | /* This function is called to check whether we run inside the emulator, |
| 50 | * and if this is the case whether GLES GPU emulation is supported. |
| 51 | * |
| 52 | * Returned values are: |
| 53 | * -1 -> not running inside the emulator |
| 54 | * 0 -> running inside the emulator, but GPU emulation not supported |
| 55 | * 1 -> running inside the emulator, GPU emulation is supported |
| 56 | * through the "emulation" config. |
| 57 | */ |
| 58 | static int |
| 59 | checkGlesEmulationStatus(void) |
| 60 | { |
| 61 | /* We're going to check for the following kernel parameters: |
| 62 | * |
| 63 | * qemu=1 -> tells us that we run inside the emulator |
| 64 | * android.qemu.gles=<number> -> tells us the GLES GPU emulation status |
| 65 | * |
| 66 | * Note that we will return <number> if we find it. This let us support |
| 67 | * more additionnal emulation modes in the future. |
| 68 | */ |
| 69 | char prop[PROPERTY_VALUE_MAX]; |
| 70 | int result = -1; |
| 71 | |
| 72 | /* First, check for qemu=1 */ |
| 73 | property_get("ro.kernel.qemu",prop,"0"); |
| 74 | if (atoi(prop) != 1) |
| 75 | return -1; |
| 76 | |
| 77 | /* We are in the emulator, get GPU status value */ |
| 78 | property_get("ro.kernel.qemu.gles",prop,"0"); |
| 79 | return atoi(prop); |
| 80 | } |
| 81 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 82 | // ---------------------------------------------------------------------------- |
| 83 | |
| 84 | Loader::driver_t::driver_t(void* gles) |
| 85 | { |
| 86 | dso[0] = gles; |
| 87 | for (size_t i=1 ; i<NELEM(dso) ; i++) |
| 88 | dso[i] = 0; |
| 89 | } |
| 90 | |
| 91 | Loader::driver_t::~driver_t() |
| 92 | { |
| 93 | for (size_t i=0 ; i<NELEM(dso) ; i++) { |
| 94 | if (dso[i]) { |
| 95 | dlclose(dso[i]); |
| 96 | dso[i] = 0; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | status_t Loader::driver_t::set(void* hnd, int32_t api) |
| 102 | { |
| 103 | switch (api) { |
| 104 | case EGL: |
| 105 | dso[0] = hnd; |
| 106 | break; |
| 107 | case GLESv1_CM: |
| 108 | dso[1] = hnd; |
| 109 | break; |
| 110 | case GLESv2: |
| 111 | dso[2] = hnd; |
| 112 | break; |
| 113 | default: |
| 114 | return BAD_INDEX; |
| 115 | } |
| 116 | return NO_ERROR; |
| 117 | } |
| 118 | |
| 119 | // ---------------------------------------------------------------------------- |
| 120 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 121 | Loader::Loader() |
| 122 | { |
| 123 | char line[256]; |
| 124 | char tag[256]; |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 125 | |
| 126 | /* Special case for GLES emulation */ |
| 127 | if (checkGlesEmulationStatus() == 0) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 128 | ALOGD("Emulator without GPU support detected. " |
| 129 | "Fallback to software renderer."); |
| 130 | mDriverTag.setTo("android"); |
David 'Digit' Turner | 80b30c2 | 2011-08-26 17:38:47 +0200 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
| 134 | /* Otherwise, use egl.cfg */ |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 135 | FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r"); |
| 136 | if (cfg == NULL) { |
| 137 | // default config |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 138 | ALOGD("egl.cfg not found, using default config"); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 139 | mDriverTag.setTo("android"); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 140 | } else { |
| 141 | while (fgets(line, 256, cfg)) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 142 | int dpy, impl; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 143 | if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 144 | //ALOGD(">>> %u %u %s", dpy, impl, tag); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 145 | // We only load the h/w accelerated implementation |
| 146 | if (tag != String8("android")) { |
| 147 | mDriverTag = tag; |
| 148 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | fclose(cfg); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | Loader::~Loader() |
| 156 | { |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 157 | GLTrace_stop(); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 160 | void* Loader::open(egl_connection_t* cnx) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 161 | { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 162 | void* dso; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 163 | driver_t* hnd = 0; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 164 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 165 | char const* tag = mDriverTag.string(); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 166 | if (tag) { |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 167 | dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 168 | if (dso) { |
| 169 | hnd = new driver_t(dso); |
| 170 | } else { |
| 171 | // Always load EGL first |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 172 | dso = load_driver("EGL", tag, cnx, EGL); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 173 | if (dso) { |
| 174 | hnd = new driver_t(dso); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 175 | // TODO: make this more automated |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 176 | hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM ); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 177 | hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 ); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 182 | LOG_FATAL_IF(!index && !hnd, |
Mathias Agopian | acdebe3 | 2009-06-03 18:26:58 -0700 | [diff] [blame] | 183 | "couldn't find the default OpenGL ES implementation " |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 184 | "for default display"); |
| 185 | |
| 186 | return (void*)hnd; |
| 187 | } |
| 188 | |
| 189 | status_t Loader::close(void* driver) |
| 190 | { |
| 191 | driver_t* hnd = (driver_t*)driver; |
| 192 | delete hnd; |
| 193 | return NO_ERROR; |
| 194 | } |
| 195 | |
| 196 | void Loader::init_api(void* dso, |
| 197 | char const * const * api, |
| 198 | __eglMustCastToProperFunctionPointerType* curr, |
| 199 | getProcAddressType getProcAddress) |
| 200 | { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame^] | 201 | const ssize_t SIZE = 256; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 202 | char scrap[SIZE]; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 203 | while (*api) { |
| 204 | char const * name = *api; |
| 205 | __eglMustCastToProperFunctionPointerType f = |
| 206 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
| 207 | if (f == NULL) { |
| 208 | // couldn't find the entry-point, use eglGetProcAddress() |
| 209 | f = getProcAddress(name); |
| 210 | } |
| 211 | if (f == NULL) { |
| 212 | // Try without the OES postfix |
| 213 | ssize_t index = ssize_t(strlen(name)) - 3; |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 214 | if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) { |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 215 | strncpy(scrap, name, index); |
| 216 | scrap[index] = 0; |
| 217 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 218 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | if (f == NULL) { |
| 222 | // Try with the OES postfix |
Mathias Agopian | 0ad71a9 | 2011-05-11 20:37:47 -0700 | [diff] [blame] | 223 | ssize_t index = ssize_t(strlen(name)) - 3; |
| 224 | if (index>0 && strcmp(name+index, "OES")) { |
| 225 | snprintf(scrap, SIZE, "%sOES", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 226 | f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 227 | //ALOGD_IF(f, "found <%s> instead", scrap); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | if (f == NULL) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 231 | //ALOGD("%s", name); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 232 | f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * GL_EXT_debug_label is special, we always report it as |
| 236 | * supported, it's handled by GLES_trace. If GLES_trace is not |
| 237 | * enabled, then these are no-ops. |
| 238 | */ |
| 239 | if (!strcmp(name, "glInsertEventMarkerEXT")) { |
| 240 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 241 | } else if (!strcmp(name, "glPushGroupMarkerEXT")) { |
| 242 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 243 | } else if (!strcmp(name, "glPopGroupMarkerEXT")) { |
| 244 | f = (__eglMustCastToProperFunctionPointerType)gl_noop; |
| 245 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 246 | } |
| 247 | *curr++ = f; |
| 248 | api++; |
| 249 | } |
| 250 | } |
| 251 | |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 252 | void *Loader::load_driver(const char* kind, const char *tag, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 253 | egl_connection_t* cnx, uint32_t mask) |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 254 | { |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 255 | char driver_absolute_path[PATH_MAX]; |
| 256 | const char* const search1 = "/vendor/lib/egl/lib%s_%s.so"; |
| 257 | const char* const search2 = "/system/lib/egl/lib%s_%s.so"; |
| 258 | |
| 259 | snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 260 | if (access(driver_absolute_path, R_OK)) { |
Brian Swetland | 2b9e4f6 | 2010-09-20 12:58:15 -0700 | [diff] [blame] | 261 | snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag); |
| 262 | if (access(driver_absolute_path, R_OK)) { |
| 263 | // this happens often, we don't want to log an error |
| 264 | return 0; |
| 265 | } |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 266 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 267 | |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 268 | void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); |
| 269 | if (dso == 0) { |
| 270 | const char* err = dlerror(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 271 | ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 275 | ALOGD("loaded %s", driver_absolute_path); |
Mathias Agopian | baca89c | 2009-08-20 19:09:34 -0700 | [diff] [blame] | 276 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 277 | if (mask & EGL) { |
| 278 | getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); |
| 279 | |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 280 | ALOGE_IF(!getProcAddress, |
Mathias Agopian | 8c17384 | 2009-09-20 16:01:02 -0700 | [diff] [blame] | 281 | "can't find eglGetProcAddress() in %s", driver_absolute_path); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 282 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 283 | egl_t* egl = &cnx->egl; |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 284 | __eglMustCastToProperFunctionPointerType* curr = |
| 285 | (__eglMustCastToProperFunctionPointerType*)egl; |
| 286 | char const * const * api = egl_names; |
| 287 | while (*api) { |
| 288 | char const * name = *api; |
| 289 | __eglMustCastToProperFunctionPointerType f = |
| 290 | (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); |
| 291 | if (f == NULL) { |
| 292 | // couldn't find the entry-point, use eglGetProcAddress() |
| 293 | f = getProcAddress(name); |
| 294 | if (f == NULL) { |
| 295 | f = (__eglMustCastToProperFunctionPointerType)0; |
| 296 | } |
| 297 | } |
| 298 | *curr++ = f; |
| 299 | api++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (mask & GLESv1_CM) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 304 | init_api(dso, gl_names, |
| 305 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame^] | 306 | &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 307 | getProcAddress); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (mask & GLESv2) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 311 | init_api(dso, gl_names, |
| 312 | (__eglMustCastToProperFunctionPointerType*) |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame^] | 313 | &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 314 | getProcAddress); |
| 315 | } |
| 316 | |
| 317 | return dso; |
| 318 | } |
| 319 | |
| 320 | // ---------------------------------------------------------------------------- |
| 321 | }; // namespace android |
| 322 | // ---------------------------------------------------------------------------- |