Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #ifndef ANDROID_EGL_LAYERS_H |
| 18 | #define ANDROID_EGL_LAYERS_H |
| 19 | |
| 20 | #include <string> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
| 23 | |
Dmytro Chystiakov | 3e4db84 | 2019-11-07 12:09:36 -0800 | [diff] [blame] | 24 | #include <android/dlext.h> |
| 25 | #include <dlfcn.h> |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 26 | |
Dmytro Chystiakov | 3e4db84 | 2019-11-07 12:09:36 -0800 | [diff] [blame] | 27 | #include <EGL/egldefs.h> |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 28 | #include "egl_platform_entries.h" |
| 29 | |
Dmytro Chystiakov | 3e4db84 | 2019-11-07 12:09:36 -0800 | [diff] [blame] | 30 | #include <nativebridge/native_bridge.h> |
| 31 | #include <nativeloader/native_loader.h> |
| 32 | |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 33 | typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer; |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | class LayerLoader { |
| 38 | public: |
| 39 | static LayerLoader& getInstance(); |
| 40 | ~LayerLoader(){}; |
| 41 | |
| 42 | typedef void* (*PFNEGLGETNEXTLAYERPROCADDRESSPROC)(void*, const char*); |
| 43 | typedef EGLFuncPointer (*layer_init_func)( |
| 44 | const void* layer_id, PFNEGLGETNEXTLAYERPROCADDRESSPROC get_next_layer_proc_address); |
| 45 | typedef EGLFuncPointer (*layer_setup_func)(const char* name, EGLFuncPointer next); |
| 46 | |
| 47 | void LoadLayers(); |
| 48 | void InitLayers(egl_connection_t*); |
| 49 | void LayerPlatformEntries(layer_setup_func layer_setup, EGLFuncPointer*, char const* const*); |
| 50 | void LayerDriverEntries(layer_setup_func layer_setup, EGLFuncPointer*, char const* const*); |
| 51 | bool Initialized(); |
| 52 | std::string GetDebugLayers(); |
| 53 | |
| 54 | EGLFuncPointer GetGpaNext(unsigned i); |
| 55 | EGLFuncPointer ApplyLayer(layer_setup_func layer_setup, const char* name, EGLFuncPointer next); |
| 56 | EGLFuncPointer ApplyLayers(const char* name, EGLFuncPointer next); |
| 57 | |
| 58 | std::vector<layer_init_func> layer_init_; |
| 59 | std::vector<layer_setup_func> layer_setup_; |
| 60 | |
| 61 | private: |
Dmytro Chystiakov | 3e4db84 | 2019-11-07 12:09:36 -0800 | [diff] [blame] | 62 | LayerLoader() : layers_loaded_(false), initialized_(false), current_layer_(0), dlhandle_(nullptr), native_bridge_(false){}; |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 63 | bool layers_loaded_; |
| 64 | bool initialized_; |
| 65 | unsigned current_layer_; |
Dmytro Chystiakov | 3e4db84 | 2019-11-07 12:09:36 -0800 | [diff] [blame] | 66 | void* dlhandle_; |
| 67 | bool native_bridge_; |
| 68 | |
| 69 | template<typename Func = void*> |
| 70 | Func GetTrampoline(const char* name) const { |
| 71 | if (native_bridge_) { |
| 72 | return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline( |
| 73 | dlhandle_, name, nullptr, 0)); |
| 74 | } |
| 75 | return reinterpret_cast<Func>(dlsym(dlhandle_, name)); |
| 76 | } |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | }; // namespace android |
| 80 | |
| 81 | #endif // ANDROID_EGL_LAYERS_H |