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