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