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 
24 #include <android/dlext.h>
25 #include <dlfcn.h>
26 
27 #include <EGL/egldefs.h>
28 #include "egl_platform_entries.h"
29 
30 #include <nativebridge/native_bridge.h>
31 #include <nativeloader/native_loader.h>
32 
33 typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
34 
35 namespace android {
36 
37 class LayerLoader {
38 public:
39     static LayerLoader& getInstance();
~LayerLoader()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:
LayerLoader()62     LayerLoader() : layers_loaded_(false), initialized_(false), current_layer_(0), dlhandle_(nullptr), native_bridge_(false){};
63     bool layers_loaded_;
64     bool initialized_;
65     unsigned current_layer_;
66     void* dlhandle_;
67     bool native_bridge_;
68 
69     template<typename Func = void*>
GetTrampoline(const char * name)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     }
77 };
78 
79 }; // namespace android
80 
81 #endif // ANDROID_EGL_LAYERS_H
82