1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "common/graphic_startup.h"
17a3e0fd82Sopenharmony_ci
18a3e0fd82Sopenharmony_ci#include "animator/animator_manager.h"
19a3e0fd82Sopenharmony_ci#include "common/input_device_manager.h"
20a3e0fd82Sopenharmony_ci#include "common/task_manager.h"
21a3e0fd82Sopenharmony_ci#include "core/render_manager.h"
22a3e0fd82Sopenharmony_ci#include "dfx/performance_task.h"
23a3e0fd82Sopenharmony_ci#include "font/ui_font.h"
24a3e0fd82Sopenharmony_ci#if defined(ENABLE_ICU) && ENABLE_ICU
25a3e0fd82Sopenharmony_ci#include "font/ui_line_break.h"
26a3e0fd82Sopenharmony_ci#endif
27a3e0fd82Sopenharmony_ci#if defined(ENABLE_SHAPING) && ENABLE_SHAPING
28a3e0fd82Sopenharmony_ci#include "font/ui_text_shaping.h"
29a3e0fd82Sopenharmony_ci#endif
30a3e0fd82Sopenharmony_ci#include "gfx_utils/file.h"
31a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_log.h"
32a3e0fd82Sopenharmony_ci#include "imgdecode/cache_manager.h"
33a3e0fd82Sopenharmony_ci#ifdef VERSION_STANDARD
34a3e0fd82Sopenharmony_ci#include "dock/ohos/ohos_input_device.h"
35a3e0fd82Sopenharmony_ci#endif
36a3e0fd82Sopenharmony_ci#if defined(ENABLE_WINDOW) && ENABLE_WINDOW
37a3e0fd82Sopenharmony_ci#include "iwindows_manager.h"
38a3e0fd82Sopenharmony_ci#endif
39a3e0fd82Sopenharmony_ci#if defined(ENABLE_GFX_ENGINES) && ENABLE_GFX_ENGINES
40a3e0fd82Sopenharmony_ci#include "hals/gfx_engines.h"
41a3e0fd82Sopenharmony_ci#endif
42a3e0fd82Sopenharmony_ci#include "securec.h"
43a3e0fd82Sopenharmony_ci
44a3e0fd82Sopenharmony_cinamespace OHOS {
45a3e0fd82Sopenharmony_civoid GraphicStartUp::InitFontEngine(uintptr_t cacheMemAddr,
46a3e0fd82Sopenharmony_ci                                    uint32_t cacheMemLen,
47a3e0fd82Sopenharmony_ci                                    const char* dPath,
48a3e0fd82Sopenharmony_ci                                    const char* ttfName)
49a3e0fd82Sopenharmony_ci{
50a3e0fd82Sopenharmony_ci    GRAPHIC_LOGE("GraphicStartUp::InitFontEngine start");
51a3e0fd82Sopenharmony_ci    UIFont* uiFont = UIFont::GetInstance();
52a3e0fd82Sopenharmony_ci    if (uiFont == nullptr) {
53a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("Get UIFont error");
54a3e0fd82Sopenharmony_ci        return;
55a3e0fd82Sopenharmony_ci    }
56a3e0fd82Sopenharmony_ci    uiFont->SetPsramMemory(cacheMemAddr, cacheMemLen);
57a3e0fd82Sopenharmony_ci    int8_t ret = uiFont->SetFontPath(dPath, BaseFont::DYNAMIC_FONT);
58a3e0fd82Sopenharmony_ci    if (ret == INVALID_RET_VALUE) {
59a3e0fd82Sopenharmony_ci        GRAPHIC_LOGW("SetFontPath failed");
60a3e0fd82Sopenharmony_ci    }
61a3e0fd82Sopenharmony_ci    if (uiFont->IsVectorFont()) {
62a3e0fd82Sopenharmony_ci        if (ttfName != nullptr) {
63a3e0fd82Sopenharmony_ci            uint8_t ret2 = uiFont->RegisterFontInfo(ttfName);
64a3e0fd82Sopenharmony_ci            if (ret2 == INVALID_UCHAR_ID) {
65a3e0fd82Sopenharmony_ci                GRAPHIC_LOGW("SetTtfName failed");
66a3e0fd82Sopenharmony_ci            }
67a3e0fd82Sopenharmony_ci        }
68a3e0fd82Sopenharmony_ci    }
69a3e0fd82Sopenharmony_ci    (void)uiFont->SetCurrentLangId(0); // set language
70a3e0fd82Sopenharmony_ci}
71a3e0fd82Sopenharmony_ci
72a3e0fd82Sopenharmony_civoid GraphicStartUp::InitLineBreakEngine(uintptr_t cacheMemAddr, uint32_t cacheMemLen, const char* path,
73a3e0fd82Sopenharmony_ci                                         const char* fileName)
74a3e0fd82Sopenharmony_ci{
75a3e0fd82Sopenharmony_ci#if defined(ENABLE_ICU) && ENABLE_ICU
76a3e0fd82Sopenharmony_ci    if ((path == nullptr) || (fileName == nullptr) || cacheMemLen < OHOS::SHAPING_WORD_DICT_LENGTH) {
77a3e0fd82Sopenharmony_ci        return;
78a3e0fd82Sopenharmony_ci    }
79a3e0fd82Sopenharmony_ci    uint32_t len = static_cast<uint32_t>(strlen(path) + strlen(fileName) + 1);
80a3e0fd82Sopenharmony_ci    char* lineBreakRuleFile = reinterpret_cast<char*>(UIMalloc(len));
81a3e0fd82Sopenharmony_ci    if (lineBreakRuleFile == nullptr) {
82a3e0fd82Sopenharmony_ci        GRAPHIC_LOGW("UIMalloc failed");
83a3e0fd82Sopenharmony_ci        return;
84a3e0fd82Sopenharmony_ci    }
85a3e0fd82Sopenharmony_ci    if (strcpy_s(lineBreakRuleFile, len, path) != EOK) {
86a3e0fd82Sopenharmony_ci        UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
87a3e0fd82Sopenharmony_ci        lineBreakRuleFile = nullptr;
88a3e0fd82Sopenharmony_ci        return;
89a3e0fd82Sopenharmony_ci    }
90a3e0fd82Sopenharmony_ci    if (strcat_s(lineBreakRuleFile, len, fileName) != EOK) {
91a3e0fd82Sopenharmony_ci        UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
92a3e0fd82Sopenharmony_ci        lineBreakRuleFile = nullptr;
93a3e0fd82Sopenharmony_ci        return;
94a3e0fd82Sopenharmony_ci    }
95a3e0fd82Sopenharmony_ci    int32_t fp;
96a3e0fd82Sopenharmony_ci#ifdef _WIN32
97a3e0fd82Sopenharmony_ci    fp = open(reinterpret_cast<const char*>(lineBreakRuleFile), O_RDONLY | O_BINARY);
98a3e0fd82Sopenharmony_ci#else
99a3e0fd82Sopenharmony_ci    fp = open(reinterpret_cast<const char*>(lineBreakRuleFile), O_RDONLY);
100a3e0fd82Sopenharmony_ci#endif
101a3e0fd82Sopenharmony_ci    if (fp < 0) {
102a3e0fd82Sopenharmony_ci        UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
103a3e0fd82Sopenharmony_ci        lineBreakRuleFile = nullptr;
104a3e0fd82Sopenharmony_ci        GRAPHIC_LOGW("Open lineBreak rule file failed");
105a3e0fd82Sopenharmony_ci        return;
106a3e0fd82Sopenharmony_ci    }
107a3e0fd82Sopenharmony_ci    int32_t lineBreakSize = lseek(fp, 0, SEEK_END);
108a3e0fd82Sopenharmony_ci    if (lineBreakSize < 0) {
109a3e0fd82Sopenharmony_ci        UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
110a3e0fd82Sopenharmony_ci        lineBreakRuleFile = nullptr;
111a3e0fd82Sopenharmony_ci        close(fp);
112a3e0fd82Sopenharmony_ci        return;
113a3e0fd82Sopenharmony_ci    }
114a3e0fd82Sopenharmony_ci    lseek(fp, 0, SEEK_SET);
115a3e0fd82Sopenharmony_ci    UILineBreakEngine& lbEngine = UILineBreakEngine::GetInstance();
116a3e0fd82Sopenharmony_ci    lbEngine.SetRuleBinInfo(fp, 0, lineBreakSize);
117a3e0fd82Sopenharmony_ci    lbEngine.SetRuleFileLoadAddr(reinterpret_cast<char*>(cacheMemAddr));
118a3e0fd82Sopenharmony_ci    lbEngine.Init();
119a3e0fd82Sopenharmony_ci    UIFree(reinterpret_cast<void*>(lineBreakRuleFile));
120a3e0fd82Sopenharmony_ci    lineBreakRuleFile = nullptr;
121a3e0fd82Sopenharmony_ci    close(fp);
122a3e0fd82Sopenharmony_ci#endif
123a3e0fd82Sopenharmony_ci}
124a3e0fd82Sopenharmony_ci
125a3e0fd82Sopenharmony_civoid GraphicStartUp::Init()
126a3e0fd82Sopenharmony_ci{
127a3e0fd82Sopenharmony_ci    TaskManager::GetInstance()->SetTaskRun(true);
128a3e0fd82Sopenharmony_ci    DEBUG_PERFORMANCE_TASK_INIT();
129a3e0fd82Sopenharmony_ci
130a3e0fd82Sopenharmony_ci    if (INDEV_READ_PERIOD > 0) {
131a3e0fd82Sopenharmony_ci        InputDeviceManager::GetInstance()->Init();
132a3e0fd82Sopenharmony_ci    }
133a3e0fd82Sopenharmony_ci    AnimatorManager::GetInstance()->Init();
134a3e0fd82Sopenharmony_ci
135a3e0fd82Sopenharmony_ci    StyleDefault::Init();
136a3e0fd82Sopenharmony_ci    RenderManager::GetInstance().Init();
137a3e0fd82Sopenharmony_ci
138a3e0fd82Sopenharmony_ci    CacheManager::GetInstance().Init(IMG_CACHE_SIZE);
139a3e0fd82Sopenharmony_ci#ifdef VERSION_STANDARD
140a3e0fd82Sopenharmony_ci    OHOSInputDevice* input = new OHOSInputDevice();
141a3e0fd82Sopenharmony_ci    if (input == nullptr) {
142a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("new OHOSInputDevice fail");
143a3e0fd82Sopenharmony_ci        return;
144a3e0fd82Sopenharmony_ci    }
145a3e0fd82Sopenharmony_ci    InputDeviceManager::GetInstance()->Add(input);
146a3e0fd82Sopenharmony_ci#endif
147a3e0fd82Sopenharmony_ci
148a3e0fd82Sopenharmony_ci#if defined(ENABLE_WINDOW) && ENABLE_WINDOW
149a3e0fd82Sopenharmony_ci    IWindowsManager::GetInstance()->Init();
150a3e0fd82Sopenharmony_ci#endif
151a3e0fd82Sopenharmony_ci#if defined(ENABLE_GFX_ENGINES) && ENABLE_GFX_ENGINES
152a3e0fd82Sopenharmony_ci    GfxEngines::GetInstance()->InitDriver();
153a3e0fd82Sopenharmony_ci#endif
154a3e0fd82Sopenharmony_ci}
155a3e0fd82Sopenharmony_ci} // namespace OHOS
156