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(dPath, BaseFont::DYNAMIC_FONT);
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 }
68 (void)uiFont->SetCurrentLangId(0); // set language
69 }
70
InitLineBreakEngine(uintptr_t cacheMemAddr,uint32_t cacheMemLen,const char * path,const char * fileName)71 void GraphicStartUp::InitLineBreakEngine(uintptr_t cacheMemAddr, uint32_t cacheMemLen, const char* path,
72 const char* fileName)
73 {
74 #if ENABLE_ICU
75 if ((path == nullptr) || (fileName == nullptr) || cacheMemLen < OHOS::SHAPING_WORD_DICT_LENGTH) {
76 return;
77 }
78 uint32_t len = static_cast<uint32_t>(strlen(path) + strlen(fileName) + 1);
79 char* lineBreakRuleFile = reinterpret_cast<char*>(UIMalloc(len));
80 if (lineBreakRuleFile == nullptr) {
81 GRAPHIC_LOGW("UIMalloc failed");
82 return;
83 }
84 if (strcpy_s(lineBreakRuleFile, len, path) != EOK) {
85 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
86 lineBreakRuleFile = nullptr;
87 return;
88 }
89 if (strcat_s(lineBreakRuleFile, len, fileName) != EOK) {
90 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
91 lineBreakRuleFile = nullptr;
92 return;
93 }
94 int32_t fp;
95 #ifdef _WIN32
96 fp = open(reinterpret_cast<const char*>(lineBreakRuleFile), O_RDONLY | O_BINARY);
97 #else
98 fp = open(reinterpret_cast<const char*>(lineBreakRuleFile), O_RDONLY);
99 #endif
100 if (fp < 0) {
101 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
102 lineBreakRuleFile = nullptr;
103 GRAPHIC_LOGW("Open lineBreak rule file failed");
104 return;
105 }
106 int32_t lineBreakSize = lseek(fp, 0, SEEK_END);
107 if (lineBreakSize < 0) {
108 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
109 lineBreakRuleFile = nullptr;
110 close(fp);
111 return;
112 }
113 lseek(fp, 0, SEEK_SET);
114 UILineBreakEngine& lbEngine = UILineBreakEngine::GetInstance();
115 lbEngine.SetRuleBinInfo(fp, 0, lineBreakSize);
116 lbEngine.SetRuleFileLoadAddr(reinterpret_cast<char*>(cacheMemAddr));
117 lbEngine.Init();
118 UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
119 lineBreakRuleFile = nullptr;
120 close(fp);
121 #endif
122 }
123
Init()124 void GraphicStartUp::Init()
125 {
126 TaskManager::GetInstance()->SetTaskRun(true);
127 DEBUG_PERFORMANCE_TASK_INIT();
128
129 if (INDEV_READ_PERIOD > 0) {
130 InputDeviceManager::GetInstance()->Init();
131 }
132 AnimatorManager::GetInstance()->Init();
133
134 StyleDefault::Init();
135 RenderManager::GetInstance().Init();
136
137 CacheManager::GetInstance().Init(IMG_CACHE_SIZE);
138 #ifdef VERSION_STANDARD
139 OHOSInputDevice* input = new OHOSInputDevice();
140 if (input == nullptr) {
141 GRAPHIC_LOGE("new OHOSInputDevice fail");
142 return;
143 }
144 InputDeviceManager::GetInstance()->Add(input);
145 #endif
146
147 #if ENABLE_WINDOW
148 IWindowsManager::GetInstance()->Init();
149 #endif
150 #if ENABLE_GFX_ENGINES
151 GfxEngines::GetInstance()->InitDriver();
152 #endif
153 }
154 } // namespace OHOS
155