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 defined(ENABLE_ICU) && ENABLE_ICU
25#include "font/ui_line_break.h"
26#endif
27#if defined(ENABLE_SHAPING) && 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 defined(ENABLE_WINDOW) && ENABLE_WINDOW
37#include "iwindows_manager.h"
38#endif
39#if defined(ENABLE_GFX_ENGINES) && ENABLE_GFX_ENGINES
40#include "hals/gfx_engines.h"
41#endif
42#include "securec.h"
43
44namespace OHOS {
45void GraphicStartUp::InitFontEngine(uintptr_t cacheMemAddr,
46                                    uint32_t cacheMemLen,
47                                    const char* dPath,
48                                    const char* ttfName)
49{
50    GRAPHIC_LOGE("GraphicStartUp::InitFontEngine start");
51    UIFont* uiFont = UIFont::GetInstance();
52    if (uiFont == nullptr) {
53        GRAPHIC_LOGE("Get UIFont error");
54        return;
55    }
56    uiFont->SetPsramMemory(cacheMemAddr, cacheMemLen);
57    int8_t ret = uiFont->SetFontPath(dPath, BaseFont::DYNAMIC_FONT);
58    if (ret == INVALID_RET_VALUE) {
59        GRAPHIC_LOGW("SetFontPath failed");
60    }
61    if (uiFont->IsVectorFont()) {
62        if (ttfName != nullptr) {
63            uint8_t ret2 = uiFont->RegisterFontInfo(ttfName);
64            if (ret2 == INVALID_UCHAR_ID) {
65                GRAPHIC_LOGW("SetTtfName failed");
66            }
67        }
68    }
69    (void)uiFont->SetCurrentLangId(0); // set language
70}
71
72void GraphicStartUp::InitLineBreakEngine(uintptr_t cacheMemAddr, uint32_t cacheMemLen, const char* path,
73                                         const char* fileName)
74{
75#if defined(ENABLE_ICU) && 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
125void 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 defined(ENABLE_WINDOW) && ENABLE_WINDOW
149    IWindowsManager::GetInstance()->Init();
150#endif
151#if defined(ENABLE_GFX_ENGINES) && ENABLE_GFX_ENGINES
152    GfxEngines::GetInstance()->InitDriver();
153#endif
154}
155} // namespace OHOS
156