• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
29 #ifdef OPENGL_WRAPPER_ENABLE_GL4
CheckIfNeedOpengl()30 bool CheckIfNeedOpengl()
31 {
32     const char* needOpenglEnv = "NEED_OPENGL";
33     const char* needOpenglEnvValue = getenv(needOpenglEnv);
34     if (needOpenglEnvValue && std::string(needOpenglEnvValue) == "1") {
35         return true;
36     }
37     WLOGI("Failed to get env NEED_OPENGL or the value of NEED_OPENGL is not 1");
38     return false;
39 }
40 #endif
41 }
42 
43 namespace OHOS {
44 EglWrapperDispatchTable gWrapperHook;
45 GlHookTable gGlHookNoContext;
46 #if USE_IGRAPHICS_EXTENDS_HOOKS
47 GlHookTable g_glHookCSDR;
48 GlHookTable g_glHookSingle;
49 #endif
50 
51 #undef CALL_HOOK_API
52 #define CALL_HOOK_API(...)
53 
54 #undef CALL_HOOK_API_RET
55 #define CALL_HOOK_API_RET CALL_HOOK_API
56 
57 #undef HOOK_API_ENTRY
58 #define HOOK_API_ENTRY(r, api, ...) #api,
59 
60 char const * const gWrapperApiNames[EGL_API_NUM] = {
61 #include "wrapper_hook_entries.in"
62     nullptr
63 };
64 
65 char const * const gEglApiNames[EGL_API_NUM] = {
66 #include "egl_hook_entries.in"
67     nullptr
68 };
69 
70 char const * const gGlApiNames1[GLES_API_NUM] = {
71 #include "gl1_hook_entries.in"
72     nullptr
73 };
74 
75 char const * const gGlApiNames2[GLES_API_NUM] = {
76 #include "gl2_hook_entries.in"
77     nullptr
78 };
79 
80 char const * const gGlApiNames3[GLES_API_NUM] = {
81 #include "gl3_hook_entries.in"
82     nullptr
83 };
84 #ifdef OPENGL_WRAPPER_ENABLE_GL4
85 char const * const gGlApiNames4[OPENGL_API_NUM] = {
86 #include "gl4_hook_entries.in"
87     nullptr
88 };
89 #endif
90 using namespace OHOS;
91 
92 static std::mutex gInitMutex;
93 static EglPreInitializer preInitializer;
94 
WrapperHookTableInit()95 void WrapperHookTableInit() noexcept
96 {
97     WLOGD("");
98     char const * const *apiName = gWrapperApiNames;
99     EglWrapperFuncPointer *curr = reinterpret_cast<EglWrapperFuncPointer*>(&gWrapperHook.wrapper);
100     while (*apiName) {
101         std::string name = *apiName;
102         EglWrapperFuncPointer addr = FindEglWrapperApi(name);
103         if (addr == nullptr) {
104             WLOGW("No addr found in wrapper entries lookup table for %{public}s", *apiName);
105         }
106         *curr++ = addr;
107         apiName++;
108     }
109 }
110 
EglCoreInit()111 bool EglCoreInit()
112 {
113     std::lock_guard<std::mutex> lock(gInitMutex);
114 
115     if (gWrapperHook.isLoad) {
116         return true;
117     }
118 
119     if (!preInitializer.InitStat()) {
120         WLOGE("preInit Error.");
121         return false;
122     }
123 
124 #ifdef OPENGL_WRAPPER_ENABLE_GL4
125     gWrapperHook.useMesa = CheckIfNeedOpengl();
126     if (gWrapperHook.useMesa) {
127         ThreadPrivateDataCtl::SetGlHookTable(&OHOS::gWrapperHook.gl);
128         EglWrapperLoader& loader(EglWrapperLoader::GetInstance());
129         if (!loader.Load(&gWrapperHook)) {
130             WLOGE("EglWrapperLoader Load Failed.");
131             return false;
132         }
133         return true;
134     }
135 #endif
136 
137     WrapperHookTableInit();
138     EglWrapperLoader& loader(EglWrapperLoader::GetInstance());
139     if (!loader.Load(&gWrapperHook)) {
140         WLOGE("EglWrapperLoader Load Failed.");
141         return false;
142     }
143 
144     EglWrapperLayer& layer(EglWrapperLayer::GetInstance());
145     if (!layer.Init(&gWrapperHook)) {
146         WLOGE("EglWrapperLayer Init Failed.");
147     }
148 
149 #if USE_IGRAPHICS_EXTENDS_HOOKS
150     if (!layer.GetIGraphicsLogicStatus()) {
151         return true;
152     }
153 
154     EglWrapperHook& hookLayer(EglWrapperHook::GetInstance());
155     if (!hookLayer.Hook(&gWrapperHook)) {
156         WLOGE("EglWrapperHookLayer init Failed!");
157     } else {
158         WLOGI("EglWrapperHookLayer init Success!");
159     }
160 #endif
161     return true;
162 }
163 }; // namespace OHOS
164