1 /*
2 * Copyright (c) 2020-2021 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
16 #include "common/graphic_startup.h"
17
18 #include "animator/animator_manager.h"
19 #include "common/input_device_manager.h"
20 #include "common/task_manager.h"
21 #include "core/render_manager.h"
22 #include "dfx/performance_task.h"
23 #include "font/ui_font.h"
24 #if ENABLE_ICU
25 #include "font/ui_line_break.h"
26 #endif
27 #if ENABLE_SHAPING
28 #include "font/ui_text_shaping.h"
29 #endif
30 #include "gfx_utils/file.h"
31 #include "gfx_utils/graphic_log.h"
32 #include "imgdecode/cache_manager.h"
33 #ifdef VERSION_STANDARD
34 #include "dock/ohos/ohos_input_device.h"
35 #endif
36 #if ENABLE_WINDOW
37 #include "iwindows_manager.h"
38 #endif
39 #if ENABLE_GFX_ENGINES
40 #include "hals/gfx_engines.h"
41 #endif
42 #include "securec.h"
43
44 namespace OHOS {
InitFontEngine(uintptr_t cacheMemAddr,uint32_t cacheMemLen,const char * dPath,const char * ttfName)45 void GraphicStartUp::InitFontEngine(uintptr_t cacheMemAddr,
46 uint32_t cacheMemLen,
47 const char* dPath,
48 const char* ttfName)
49 {
50 UIFont* uiFont = UIFont::GetInstance();
51 if (uiFont == nullptr) {
52 GRAPHIC_LOGE("Get UIFont error");
53 return;
54 }
55 uiFont->SetPsramMemory(cacheMemAddr, cacheMemLen);
56 int8_t ret = uiFont->SetFontPath(const_cast<char*>(dPath), nullptr);
57 if (ret == INVALID_RET_VALUE) {
58 GRAPHIC_LOGW("SetFontPath failed");
59 }
60 if (uiFont->IsVectorFont()) {
61 if (ttfName != nullptr) {
62 uint8_t ret2 = uiFont->RegisterFontInfo(ttfName);
63 if (ret2 == INVALID_UCHAR_ID) {
64 GRAPHIC_LOGW("SetTtfName failed");
65 }
66 }
67 } else {
68 (void)uiFont->SetCurrentLangId(0); // set language
69 }
70 }
71
InitLineBreakEngine(uintptr_t cacheMemAddr,uint32_t cacheMemLen,const char * path,const char * fileName)72 void GraphicStartUp::InitLineBreakEngine(uintptr_t cacheMemAddr, uint32_t cacheMemLen, const char* path,
73 const char* fileName)
74 {
75 #if ENABLE_ICU
76 if ((path == nullptr) || (fileName == nullptr) || cacheMemLen < OHOS::SHAPING_WORD_DICT_LENGTH) {
77 return;
78 }
79 uint32_t len = static_cast<uint32_t>(strlen(path) + strlen(fileName) + 1);
80 char* lineBreakRuleFile = reinterpret_cast<char*>(UIMalloc(len));
81 if (lineBreakRuleFile == nullptr) {
82 GRAPHIC_LOGW("UIMalloc failed");
83 return;
84 }
85 if (strcpy_s(lineBreakRuleFile, len, path) != EOK) {
86 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
87 lineBreakRuleFile = nullptr;
88 return;
89 }
90 if (strcat_s(lineBreakRuleFile, len, fileName) != EOK) {
91 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
92 lineBreakRuleFile = nullptr;
93 return;
94 }
95 int32_t fp;
96 #ifdef _WIN32
97 fp = open(reinterpret_cast<const char*>(lineBreakRuleFile), O_RDONLY | O_BINARY);
98 #else
99 fp = open(reinterpret_cast<const char*>(lineBreakRuleFile), O_RDONLY);
100 #endif
101 if (fp < 0) {
102 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
103 lineBreakRuleFile = nullptr;
104 GRAPHIC_LOGW("Open lineBreak rule file failed");
105 return;
106 }
107 int32_t lineBreakSize = lseek(fp, 0, SEEK_END);
108 if (lineBreakSize < 0) {
109 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
110 lineBreakRuleFile = nullptr;
111 close(fp);
112 return;
113 }
114 lseek(fp, 0, SEEK_SET);
115 UILineBreakEngine& lbEngine = UILineBreakEngine::GetInstance();
116 lbEngine.SetRuleBinInfo(fp, 0, lineBreakSize);
117 lbEngine.SetRuleFileLoadAddr(reinterpret_cast<char*>(cacheMemAddr));
118 lbEngine.Init();
119 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
120 lineBreakRuleFile = nullptr;
121 close(fp);
122 #endif
123 }
124
Init()125 void GraphicStartUp::Init()
126 {
127 TaskManager::GetInstance()->SetTaskRun(true);
128 DEBUG_PERFORMANCE_TASK_INIT();
129
130 if (INDEV_READ_PERIOD > 0) {
131 InputDeviceManager::GetInstance()->Init();
132 }
133 AnimatorManager::GetInstance()->Init();
134
135 StyleDefault::Init();
136 RenderManager::GetInstance().Init();
137
138 CacheManager::GetInstance().Init(IMG_CACHE_SIZE);
139 #ifdef VERSION_STANDARD
140 OHOSInputDevice* input = new OHOSInputDevice();
141 if (input == nullptr) {
142 GRAPHIC_LOGE("new OHOSInputDevice fail");
143 return;
144 }
145 InputDeviceManager::GetInstance()->Add(input);
146 #endif
147
148 #if ENABLE_WINDOW
149 IWindowsManager::GetInstance()->Init();
150 #endif
151 #if ENABLE_GFX_ENGINES
152 GfxEngines::GetInstance()->InitDriver();
153 #endif
154 }
155 } // namespace OHOS
156