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 #include "../wrapper_log.h" 24 25 namespace OHOS { 26 EglWrapperDispatchTable gWrapperHook; 27 GlHookTable gGlHookNoContext; 28 29 #undef CALL_HOOK_API 30 #define CALL_HOOK_API(...) 31 32 #undef CALL_HOOK_API_RET 33 #define CALL_HOOK_API_RET CALL_HOOK_API 34 35 #undef HOOK_API_ENTRY 36 #define HOOK_API_ENTRY(r, api, ...) #api, 37 38 char const * const gWrapperApiNames[EGL_API_NUM] = { 39 #include "../wrapper_hook_entries.in" 40 nullptr 41 }; 42 43 char const * const gEglApiNames[EGL_API_NUM] = { 44 #include "../egl_hook_entries.in" 45 nullptr 46 }; 47 48 char const * const gGlApiNames1[GL_API_NUM] = { 49 #include "../gl1_hook_entries.in" 50 nullptr 51 }; 52 53 char const * const gGlApiNames2[GL_API_NUM] = { 54 #include "../gl2_hook_entries.in" 55 nullptr 56 }; 57 58 char const * const gGlApiNames3[GL_API_NUM] = { 59 #include "../gl3_hook_entries.in" 60 nullptr 61 }; 62 63 using namespace OHOS; 64 65 static std::mutex gInitMutex; 66 static EglPreInitializer preInitializer; 67 WrapperHookTableInit()68void WrapperHookTableInit() noexcept 69 { 70 WLOGD(""); 71 char const * const *apiName = gWrapperApiNames; 72 EglWrapperFuncPointer *curr = reinterpret_cast<EglWrapperFuncPointer*>(&gWrapperHook.wrapper); 73 while (*apiName) { 74 std::string name = *apiName; 75 EglWrapperFuncPointer addr = FindEglWrapperApi(name); 76 if (addr == nullptr) { 77 WLOGW("No addr found in wrapper entries lookup table for %{public}s", *apiName); 78 } 79 *curr++ = addr; 80 apiName++; 81 } 82 } 83 EglCoreInit()84bool EglCoreInit() 85 { 86 std::lock_guard<std::mutex> lock(gInitMutex); 87 88 if (gWrapperHook.isLoad) { 89 return true; 90 } 91 92 if (!preInitializer.InitStat()) { 93 WLOGE("preInit Error."); 94 return false; 95 } 96 97 WrapperHookTableInit(); 98 EglWrapperLoader& loader(EglWrapperLoader::GetInstance()); 99 if (!loader.Load(&gWrapperHook)) { 100 WLOGE("EglWrapperLoader Load Failed."); 101 return false; 102 } 103 104 EglWrapperLayer& layer(EglWrapperLayer::GetInstance()); 105 if (!layer.Init(&gWrapperHook)) { 106 WLOGE("EglWrapperLayer Init Failed."); 107 } 108 109 return true; 110 } 111 }; // namespace OHOS 112