Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame^] | 1 | /* |
| 2 | ** Copyright 2018, 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 "egl_layers.h" |
| 18 | |
| 19 | #include <EGL/egl.h> |
| 20 | #include <android-base/file.h> |
| 21 | #include <android-base/strings.h> |
| 22 | #include <android/dlext.h> |
| 23 | #include <cutils/properties.h> |
| 24 | #include <dlfcn.h> |
| 25 | #include <graphicsenv/GraphicsEnv.h> |
| 26 | #include <log/log.h> |
| 27 | #include <nativebridge/native_bridge.h> |
| 28 | #include <nativeloader/native_loader.h> |
| 29 | #include <sys/prctl.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // GLES Layers |
| 34 | // |
| 35 | // - Layer discovery - |
| 36 | // 1. Check for debug layer list from GraphicsEnv |
| 37 | // 2. If none enabled, check system properties |
| 38 | // |
| 39 | // - Layer initializing - |
| 40 | // TODO: ADD DETAIL ABOUT NEW INTERFACES |
| 41 | // - InitializeLayer (provided by layer, called by loader) |
| 42 | // - GetLayerProcAddress (provided by layer, called by loader) |
| 43 | // - getNextLayerProcAddress (provided by loader, called by layer) |
| 44 | // |
| 45 | // 1. Walk through defs for egl and each gl version |
| 46 | // 2. Call GetLayerProcAddress passing the name and the target hook entry point |
| 47 | // - This tells the layer the next point in the chain it should call |
| 48 | // 3. Replace the hook with the layer's entry point |
| 49 | // - All entryoints will be present, anything unsupported by the driver will |
| 50 | // have gl_unimplemented |
| 51 | // |
| 52 | // - Extension layering - |
| 53 | // Not all functions are known to Android, so libEGL handles extensions. |
| 54 | // They are looked up by applications using eglGetProcAddress |
| 55 | // Layers can look them up with getNextLayerProcAddress |
| 56 | |
| 57 | const int kFuncCount = sizeof(platform_impl_t) / sizeof(char*) + sizeof(egl_t) / sizeof(char*) + |
| 58 | sizeof(gl_hooks_t) / sizeof(char*); |
| 59 | |
| 60 | typedef struct FunctionTable { |
| 61 | EGLFuncPointer x[kFuncCount]; |
| 62 | EGLFuncPointer& operator[](int i) { return x[i]; } |
| 63 | } FunctionTable; |
| 64 | |
| 65 | // TODO: Move these to class |
| 66 | std::unordered_map<std::string, int> func_indices; |
| 67 | // func_indices.reserve(kFuncCount); |
| 68 | |
| 69 | std::unordered_map<int, std::string> func_names; |
| 70 | // func_names.reserve(kFuncCount); |
| 71 | |
| 72 | std::vector<FunctionTable> layer_functions; |
| 73 | |
| 74 | const void* getNextLayerProcAddress(void* layer_id, const char* name) { |
| 75 | // Use layer_id to find funcs for layer below current |
| 76 | // This is the same key provided in InitializeLayer |
| 77 | auto next_layer_funcs = reinterpret_cast<FunctionTable*>(layer_id); |
| 78 | EGLFuncPointer val; |
| 79 | |
| 80 | if (func_indices.find(name) == func_indices.end()) { |
| 81 | // No entry for this function - it is an extension |
| 82 | // call down the GPA chain directly to the impl |
| 83 | ALOGV("getNextLayerProcAddress servicing %s", name); |
| 84 | |
| 85 | // Look up which GPA we should use |
| 86 | int gpaIndex = func_indices["eglGetProcAddress"]; |
| 87 | EGLFuncPointer gpaNext = (*next_layer_funcs)[gpaIndex]; |
| 88 | |
| 89 | ALOGV("Calling down the GPA chain (%llu) for %s", (unsigned long long)gpaNext, name); |
| 90 | |
| 91 | // Call it for the requested function |
| 92 | typedef void* (*PFNEGLGETPROCADDRESSPROC)(const char*); |
| 93 | PFNEGLGETPROCADDRESSPROC next = reinterpret_cast<PFNEGLGETPROCADDRESSPROC>(gpaNext); |
| 94 | |
| 95 | val = reinterpret_cast<EGLFuncPointer>(next(name)); |
| 96 | ALOGV("Got back %llu for %s", (unsigned long long)val, name); |
| 97 | |
| 98 | // We should store it now, but to do that, we need to move func_idx to the class so we can |
| 99 | // increment it separately |
| 100 | // TODO: Move func_idx to class and store the result of GPA |
| 101 | return reinterpret_cast<void*>(val); |
| 102 | } |
| 103 | |
| 104 | // int index = func_indices[name]; |
| 105 | // val = (*next_layer_funcs)[index]; |
| 106 | // return reinterpret_cast<void*>(val); |
| 107 | return reinterpret_cast<void*>((*next_layer_funcs)[func_indices[name]]); |
| 108 | } |
| 109 | |
| 110 | void SetupFuncMaps(FunctionTable& functions, char const* const* entries, EGLFuncPointer* curr, |
| 111 | int& func_idx) { |
| 112 | while (*entries) { |
| 113 | const char* name = *entries; |
| 114 | |
| 115 | // Some names overlap, only fill with initial entry |
| 116 | // This does mean that some indices will not be used |
| 117 | if (func_indices.find(name) == func_indices.end()) { |
| 118 | func_names[func_idx] = name; |
| 119 | func_indices[name] = func_idx; |
| 120 | } |
| 121 | |
| 122 | // Populate layer_functions once with initial value |
| 123 | // These values will arrive in priority order, starting with platform entries |
| 124 | if (functions[func_idx] == nullptr) { |
| 125 | functions[func_idx] = *curr; |
| 126 | } |
| 127 | |
| 128 | entries++; |
| 129 | curr++; |
| 130 | func_idx++; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | LayerLoader& LayerLoader::getInstance() { |
| 135 | // This function is mutex protected in egl_init_drivers_locked and eglGetProcAddressImpl |
| 136 | static LayerLoader layer_loader; |
| 137 | |
| 138 | if (!layer_loader.layers_loaded_) layer_loader.LoadLayers(); |
| 139 | |
| 140 | return layer_loader; |
| 141 | } |
| 142 | |
| 143 | const char kSystemLayerLibraryDir[] = "/data/local/debug/gles"; |
| 144 | |
| 145 | std::string LayerLoader::GetDebugLayers() { |
| 146 | // Layers can be specified at the Java level in GraphicsEnvironemnt |
| 147 | // gpu_debug_layers = layer1:layer2:layerN |
| 148 | std::string debug_layers = android_getDebugLayers(); |
| 149 | |
| 150 | if (debug_layers.empty()) { |
| 151 | // Only check system properties if Java settings are empty |
| 152 | char prop[PROPERTY_VALUE_MAX]; |
| 153 | property_get("debug.gles.layers", prop, ""); |
| 154 | debug_layers = prop; |
| 155 | } |
| 156 | |
| 157 | return debug_layers; |
| 158 | } |
| 159 | |
| 160 | EGLFuncPointer LayerLoader::ApplyLayer(layer_setup_func layer_setup, const char* name, |
| 161 | EGLFuncPointer next) { |
| 162 | // Walk through our list of LayerSetup functions (they will already be in reverse order) to |
| 163 | // build up a call chain from the driver |
| 164 | |
| 165 | EGLFuncPointer layer_entry = next; |
| 166 | |
| 167 | layer_entry = layer_setup(name, layer_entry); |
| 168 | |
| 169 | if (next != layer_entry) { |
| 170 | ALOGV("We succeeded, replacing hook (%llu) with layer entry (%llu), for %s", |
| 171 | (unsigned long long)next, (unsigned long long)layer_entry, name); |
| 172 | } |
| 173 | |
| 174 | return layer_entry; |
| 175 | } |
| 176 | |
| 177 | EGLFuncPointer LayerLoader::ApplyLayers(const char* name, EGLFuncPointer next) { |
| 178 | if (!layers_loaded_ || layer_setup_.empty()) return next; |
| 179 | |
| 180 | ALOGV("ApplyLayers called for %s with next (%llu), current_layer_ (%i)", name, |
| 181 | (unsigned long long)next, current_layer_); |
| 182 | |
| 183 | EGLFuncPointer val = next; |
| 184 | |
| 185 | // Only ApplyLayers for layers that have been setup, not all layers yet |
| 186 | for (unsigned i = 0; i < current_layer_; i++) { |
| 187 | ALOGV("ApplyLayers: Calling ApplyLayer with i = %i for %s with next (%llu)", i, name, |
| 188 | (unsigned long long)next); |
| 189 | val = ApplyLayer(layer_setup_[i], name, val); |
| 190 | } |
| 191 | |
| 192 | ALOGV("ApplyLayers returning %llu for %s", (unsigned long long)val, name); |
| 193 | |
| 194 | return val; |
| 195 | } |
| 196 | |
| 197 | void LayerLoader::LayerPlatformEntries(layer_setup_func layer_setup, EGLFuncPointer* curr, |
| 198 | char const* const* entries) { |
| 199 | while (*entries) { |
| 200 | char const* name = *entries; |
| 201 | |
| 202 | EGLFuncPointer prev = *curr; |
| 203 | |
| 204 | // Pass the existing entry point into the layer, replace the call with return value |
| 205 | *curr = ApplyLayer(layer_setup, name, *curr); |
| 206 | |
| 207 | if (prev != *curr) { |
| 208 | ALOGV("LayerPlatformEntries: Replaced (%llu) with platform entry (%llu), for %s", |
| 209 | (unsigned long long)prev, (unsigned long long)*curr, name); |
| 210 | } else { |
| 211 | ALOGV("LayerPlatformEntries: No change(%llu) for %s, which means layer did not " |
| 212 | "intercept", |
| 213 | (unsigned long long)prev, name); |
| 214 | } |
| 215 | |
| 216 | curr++; |
| 217 | entries++; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void LayerLoader::LayerDriverEntries(layer_setup_func layer_setup, EGLFuncPointer* curr, |
| 222 | char const* const* entries) { |
| 223 | while (*entries) { |
| 224 | char const* name = *entries; |
| 225 | EGLFuncPointer prev = *curr; |
| 226 | |
| 227 | // Only apply layers to driver entries if not handled by the platform |
| 228 | if (FindPlatformImplAddr(name) == nullptr) { |
| 229 | // Pass the existing entry point into the layer, replace the call with return value |
| 230 | *curr = ApplyLayer(layer_setup, name, *prev); |
| 231 | |
| 232 | if (prev != *curr) { |
| 233 | ALOGV("LayerDriverEntries: Replaced (%llu) with platform entry (%llu), for %s", |
| 234 | (unsigned long long)prev, (unsigned long long)*curr, name); |
| 235 | } |
| 236 | |
| 237 | } else { |
| 238 | ALOGV("LayerDriverEntries: Skipped (%llu) for %s", (unsigned long long)prev, name); |
| 239 | } |
| 240 | |
| 241 | curr++; |
| 242 | entries++; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | bool LayerLoader::Initialized() { |
| 247 | return initialized_; |
| 248 | } |
| 249 | |
| 250 | void LayerLoader::InitLayers(egl_connection_t* cnx) { |
| 251 | if (!layers_loaded_) return; |
| 252 | |
| 253 | if (initialized_) return; |
| 254 | |
| 255 | if (layer_setup_.empty()) { |
| 256 | initialized_ = true; |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // Include the driver in layer_functions |
| 261 | layer_functions.resize(layer_setup_.size() + 1); |
| 262 | |
| 263 | // Walk through the initial lists and create layer_functions[0] |
| 264 | int func_idx = 0; |
| 265 | char const* const* entries; |
| 266 | EGLFuncPointer* curr; |
| 267 | |
| 268 | entries = platform_names; |
| 269 | curr = reinterpret_cast<EGLFuncPointer*>(&cnx->platform); |
| 270 | SetupFuncMaps(layer_functions[0], entries, curr, func_idx); |
| 271 | ALOGV("InitLayers: func_idx after platform_names: %i", func_idx); |
| 272 | |
| 273 | entries = egl_names; |
| 274 | curr = reinterpret_cast<EGLFuncPointer*>(&cnx->egl); |
| 275 | SetupFuncMaps(layer_functions[0], entries, curr, func_idx); |
| 276 | ALOGV("InitLayers: func_idx after egl_names: %i", func_idx); |
| 277 | |
| 278 | entries = gl_names; |
| 279 | curr = reinterpret_cast<EGLFuncPointer*>(&cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl); |
| 280 | SetupFuncMaps(layer_functions[0], entries, curr, func_idx); |
| 281 | ALOGV("InitLayers: func_idx after gl_names: %i", func_idx); |
| 282 | |
| 283 | // Walk through each layer's entry points per API, starting just above the driver |
| 284 | for (current_layer_ = 0; current_layer_ < layer_setup_.size(); current_layer_++) { |
| 285 | // Init the layer with a key that points to layer just below it |
| 286 | layer_init_[current_layer_](reinterpret_cast<void*>(&layer_functions[current_layer_]), |
| 287 | reinterpret_cast<PFNEGLGETNEXTLAYERPROCADDRESSPROC>( |
| 288 | getNextLayerProcAddress)); |
| 289 | |
| 290 | // Check functions implemented by the platform |
| 291 | func_idx = 0; |
| 292 | entries = platform_names; |
| 293 | curr = reinterpret_cast<EGLFuncPointer*>(&cnx->platform); |
| 294 | LayerPlatformEntries(layer_setup_[current_layer_], curr, entries); |
| 295 | |
| 296 | // Populate next function table after layers have been applied |
| 297 | SetupFuncMaps(layer_functions[current_layer_ + 1], entries, curr, func_idx); |
| 298 | |
| 299 | // EGL |
| 300 | entries = egl_names; |
| 301 | curr = reinterpret_cast<EGLFuncPointer*>(&cnx->egl); |
| 302 | LayerDriverEntries(layer_setup_[current_layer_], curr, entries); |
| 303 | |
| 304 | // Populate next function table after layers have been applied |
| 305 | SetupFuncMaps(layer_functions[current_layer_ + 1], entries, curr, func_idx); |
| 306 | |
| 307 | // GLES 2+ |
| 308 | // NOTE: We route calls to GLESv2 hooks, not GLESv1, so layering does not support GLES 1.x |
| 309 | // If it were added in the future, a different layer initialization model would be needed, |
| 310 | // that defers loading GLES entrypoints until after eglMakeCurrent, so two phase |
| 311 | // initialization. |
| 312 | entries = gl_names; |
| 313 | curr = reinterpret_cast<EGLFuncPointer*>(&cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl); |
| 314 | LayerDriverEntries(layer_setup_[current_layer_], curr, entries); |
| 315 | |
| 316 | // Populate next function table after layers have been applied |
| 317 | SetupFuncMaps(layer_functions[current_layer_ + 1], entries, curr, func_idx); |
| 318 | } |
| 319 | |
| 320 | // We only want to apply layers once |
| 321 | initialized_ = true; |
| 322 | } |
| 323 | |
| 324 | void LayerLoader::LoadLayers() { |
| 325 | std::string debug_layers = GetDebugLayers(); |
| 326 | |
| 327 | // If no layers are specified, we're done |
| 328 | if (debug_layers.empty()) return; |
| 329 | |
| 330 | // Only enable the system search path for non-user builds |
| 331 | std::string system_path; |
| 332 | if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 333 | system_path = kSystemLayerLibraryDir; |
| 334 | } |
| 335 | |
| 336 | ALOGI("Debug layer list: %s", debug_layers.c_str()); |
| 337 | std::vector<std::string> layers = android::base::Split(debug_layers, ":"); |
| 338 | |
| 339 | // Load the layers in reverse order so we start with the driver's entrypoint and work our way up |
| 340 | for (int32_t i = layers.size() - 1; i >= 0; i--) { |
| 341 | // Check each layer path for the layer |
| 342 | std::vector<std::string> paths = android::base::Split(android_getLayerPaths(), ":"); |
| 343 | |
| 344 | if (!system_path.empty()) { |
| 345 | // Prepend the system paths so they override other layers |
| 346 | auto it = paths.begin(); |
| 347 | paths.insert(it, system_path); |
| 348 | } |
| 349 | |
| 350 | bool layer_found = false; |
| 351 | for (uint32_t j = 0; j < paths.size() && !layer_found; j++) { |
| 352 | std::string layer; |
| 353 | |
| 354 | ALOGI("Searching %s for GLES layers", paths[j].c_str()); |
| 355 | |
| 356 | // Realpath will return null for non-existent files |
| 357 | android::base::Realpath(paths[j] + "/" + layers[i], &layer); |
| 358 | |
| 359 | if (!layer.empty()) { |
| 360 | layer_found = true; |
| 361 | ALOGI("GLES layer found: %s", layer.c_str()); |
| 362 | |
| 363 | // Load the layer |
| 364 | // |
| 365 | // TODO: This code is common with Vulkan loader, refactor |
| 366 | // |
| 367 | // Libraries in the system layer library dir can't be loaded into |
| 368 | // the application namespace. That causes compatibility problems, since |
| 369 | // any symbol dependencies will be resolved by system libraries. They |
| 370 | // can't safely use libc++_shared, for example. Which is one reason |
| 371 | // (among several) we only allow them in non-user builds. |
| 372 | void* handle = nullptr; |
| 373 | auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace(); |
| 374 | if (app_namespace && !android::base::StartsWith(layer, kSystemLayerLibraryDir)) { |
| 375 | bool native_bridge = false; |
| 376 | std::string error_message; |
| 377 | handle = OpenNativeLibrary(app_namespace, layer.c_str(), &native_bridge, |
| 378 | &error_message); |
| 379 | if (!handle) { |
| 380 | ALOGE("Failed to load layer %s with error: %s", layer.c_str(), |
| 381 | error_message.c_str()); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | } else { |
| 386 | handle = dlopen(layer.c_str(), RTLD_NOW | RTLD_LOCAL); |
| 387 | } |
| 388 | |
| 389 | if (handle) { |
| 390 | ALOGV("Loaded layer handle (%llu) for layer %s", (unsigned long long)handle, |
| 391 | layers[i].c_str()); |
| 392 | } else { |
| 393 | // If the layer is found but can't be loaded, try setenforce 0 |
| 394 | const char* dlsym_error = dlerror(); |
| 395 | ALOGE("Failed to load layer %s with error: %s", layer.c_str(), dlsym_error); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | // Find the layer's Initialize function |
| 400 | std::string init_func = "InitializeLayer"; |
| 401 | ALOGV("Looking for entrypoint %s", init_func.c_str()); |
| 402 | |
| 403 | layer_init_func LayerInit = |
| 404 | reinterpret_cast<layer_init_func>(dlsym(handle, init_func.c_str())); |
| 405 | if (LayerInit) { |
| 406 | ALOGV("Found %s for layer %s", init_func.c_str(), layer.c_str()); |
| 407 | layer_init_.push_back(LayerInit); |
| 408 | } else { |
| 409 | ALOGE("Failed to dlsym %s for layer %s", init_func.c_str(), layer.c_str()); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | // Find the layer's setup function |
| 414 | std::string setup_func = "GetLayerProcAddress"; |
| 415 | ALOGV("Looking for entrypoint %s", setup_func.c_str()); |
| 416 | |
| 417 | layer_setup_func LayerSetup = |
| 418 | reinterpret_cast<layer_setup_func>(dlsym(handle, setup_func.c_str())); |
| 419 | if (LayerSetup) { |
| 420 | ALOGV("Found %s for layer %s", setup_func.c_str(), layer.c_str()); |
| 421 | layer_setup_.push_back(LayerSetup); |
| 422 | } else { |
| 423 | ALOGE("Failed to dlsym %s for layer %s", setup_func.c_str(), layer.c_str()); |
| 424 | return; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | // Track this so we only attempt to load these once |
| 430 | layers_loaded_ = true; |
| 431 | } |
| 432 | |
| 433 | } // namespace android |