1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "egl_core.h"
16 
17 #include <mutex>
18 
19 #include "egl_defs.h"
20 #include "egl_pre_initializer.h"
21 #include "egl_wrapper_layer.h"
22 #include "egl_wrapper_loader.h"
23 #if USE_IGRAPHICS_EXTENDS_HOOKS
24 #include "egl_wrapper_hook.h"
25 #endif
26 #include "wrapper_log.h"
27 
28 namespace OHOS {
29 EglWrapperDispatchTable gWrapperHook;
30 GlHookTable gGlHookNoContext;
31 #if USE_IGRAPHICS_EXTENDS_HOOKS
32 GlHookTable g_glHookCSDR;
33 GlHookTable g_glHookSingle;
34 #endif
35 
36 #undef CALL_HOOK_API
37 #define CALL_HOOK_API(...)
38 
39 #undef CALL_HOOK_API_RET
40 #define CALL_HOOK_API_RET CALL_HOOK_API
41 
42 #undef HOOK_API_ENTRY
43 #define HOOK_API_ENTRY(r, api, ...) #api,
44 
45 char const * const gWrapperApiNames[EGL_API_NUM] = {
46 #include "wrapper_hook_entries.in"
47     nullptr
48 };
49 
50 char const * const gEglApiNames[EGL_API_NUM] = {
51 #include "egl_hook_entries.in"
52     nullptr
53 };
54 
55 char const * const gGlApiNames1[GL_API_NUM] = {
56 #include "gl1_hook_entries.in"
57     nullptr
58 };
59 
60 char const * const gGlApiNames2[GL_API_NUM] = {
61 #include "gl2_hook_entries.in"
62     nullptr
63 };
64 
65 char const * const gGlApiNames3[GL_API_NUM] = {
66 #include "gl3_hook_entries.in"
67     nullptr
68 };
69 
70 using namespace OHOS;
71 
72 static std::mutex gInitMutex;
73 static EglPreInitializer preInitializer;
74 
WrapperHookTableInit()75 void WrapperHookTableInit() noexcept
76 {
77     WLOGD("");
78     char const * const *apiName = gWrapperApiNames;
79     EglWrapperFuncPointer *curr = reinterpret_cast<EglWrapperFuncPointer*>(&gWrapperHook.wrapper);
80     while (*apiName) {
81         std::string name = *apiName;
82         EglWrapperFuncPointer addr = FindEglWrapperApi(name);
83         if (addr == nullptr) {
84             WLOGW("No addr found in wrapper entries lookup table for %{public}s", *apiName);
85         }
86         *curr++ = addr;
87         apiName++;
88     }
89 }
90 
EglCoreInit()91 bool EglCoreInit()
92 {
93     std::lock_guard<std::mutex> lock(gInitMutex);
94 
95     if (gWrapperHook.isLoad) {
96         return true;
97     }
98 
99     if (!preInitializer.InitStat()) {
100         WLOGE("preInit Error.");
101         return false;
102     }
103 
104     WrapperHookTableInit();
105     EglWrapperLoader& loader(EglWrapperLoader::GetInstance());
106     if (!loader.Load(&gWrapperHook)) {
107         WLOGE("EglWrapperLoader Load Failed.");
108         return false;
109     }
110 
111     EglWrapperLayer& layer(EglWrapperLayer::GetInstance());
112     if (!layer.Init(&gWrapperHook)) {
113         WLOGE("EglWrapperLayer Init Failed.");
114     }
115 
116 #if USE_IGRAPHICS_EXTENDS_HOOKS
117     if (!layer.GetIGraphicsLogicStatus()) {
118         return true;
119     }
120 
121     EglWrapperHook& hookLayer(EglWrapperHook::GetInstance());
122     if (!hookLayer.Hook(&gWrapperHook)) {
123         WLOGE("EglWrapperHookLayer init Failed!");
124     } else {
125         WLOGI("EglWrapperHookLayer init Success!");
126     }
127 #endif
128     return true;
129 }
130 }; // namespace OHOS
131