1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 "ui_main.h"
16 #include "cmsis_os.h"
17 #include "pthread.h"
18 #include "core/render_manager.h"
19 #include "common/graphic_startup.h"
20 #include "common/image_decode_ability.h"
21 #include "common/input_device_manager.h"
22 #include "common/task_manager.h"
23 #include "display_device.h"
24 #include "engines/gfx/gfx_engine_manager.h"
25 #include "font/ui_font.h"
26 #include "font/ui_font_header.h"
27 #include "font/ui_font_vector.h"
28 #include "gfx_utils/graphic_log.h"
29 #include "graphic_config.h"
30 #include "hal_tick.h"
31 #include "touch_input.h"
32
33 #define ENABLE_FPS
34 #ifdef ENABLE_ACE
35 #include "product_adapter.h"
36 #endif
37
38 #define FONT_MEM_LEN (512 * 1024)
39 static uint8_t g_fontMemBaseAddr[FONT_MEM_LEN];
40 #if ENABLE_ICU
41 static uint8_t g_icuMemBaseAddr[SHAPING_WORD_DICT_LENGTH];
42 #endif
43
44 using namespace OHOS;
45
InitFontEngine()46 static void InitFontEngine()
47 {
48 #if ENABLE_VECTOR_FONT
49 GRAPHIC_LOGD("InitFontEngine fontMemSize: 0x%x", FONT_MEM_LEN);
50 GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(g_fontMemBaseAddr),
51 FONT_MEM_LEN, VECTOR_FONT_DIR, DEFAULT_VECTOR_FONT_FILENAME);
52 #endif
53
54 #if ENABLE_ICU
55 GraphicStartUp::InitLineBreakEngine(reinterpret_cast<uintptr_t>(g_icuMemBaseAddr), SHAPING_WORD_DICT_LENGTH,
56 VECTOR_FONT_DIR, DEFAULT_LINE_BREAK_RULE_FILENAME);
57 #endif
58 }
59
InitImageDecodeAbility()60 static void InitImageDecodeAbility()
61 {
62 uint32_t imageType = IMG_SUPPORT_BITMAP | OHOS::IMG_SUPPORT_JPEG | OHOS::IMG_SUPPORT_PNG;
63 ImageDecodeAbility::GetInstance().SetImageDecodeAbility(imageType);
64 }
65
InitHal()66 static void InitHal()
67 {
68 DisplayDevice *display = DisplayDevice::GetInstance();
69 BaseGfxEngine::InitGfxEngine(display);
70
71 TouchInput *touch = TouchInput::GetInstance();
72 InputDeviceManager::GetInstance()->Add(touch);
73 }
74
InitUiKit(void)75 void InitUiKit(void)
76 {
77 GraphicStartUp::Init();
78 // init display/input device
79 InitHal();
80 // init font engine
81 InitFontEngine();
82 // init suppot image format
83 InitImageDecodeAbility();
84 }
85
RunApp(void)86 __attribute__((weak)) void RunApp(void)
87 {
88 GRAPHIC_LOGI("RunApp default");
89 }
90
91 #ifdef ENABLE_ACE
RenderTEHandler()92 static void RenderTEHandler()
93 {
94 }
95 #endif
96
97 static constexpr uint32_t UI_MAIN_TASK_DELAY = 5000;
98 static constexpr uint32_t ONE_SECOND = 1000;
UiMainTask(void * arg)99 static void *UiMainTask(void *arg)
100 {
101 (void)arg;
102
103 (void)pthread_setname_np(pthread_self(), "UiMain");
104 osDelay(UI_MAIN_TASK_DELAY);
105 InitUiKit();
106 RunApp();
107
108 #ifdef ENABLE_ACE
109 const ACELite::TEHandlingHooks hooks = {RenderTEHandler, nullptr};
110 ACELite::ProductAdapter::RegTEHandlers(hooks);
111 #endif
112 #if ENABLE_FPS_SUPPORT
113 uint32_t start = HALTick::GetInstance().GetTime();
114 #endif
115 while (1) {
116 #ifdef ENABLE_ACE
117 // Here render all js app in the same task.
118 ACELite::ProductAdapter::DispatchTEMessage();
119 #endif
120 #if FULLY_RENDER
121 DisplayDevice::GetInstance()->UpdateFBBuffer();
122 #endif
123 uint32_t temp = HALTick::GetInstance().GetTime();
124 TaskManager::GetInstance()->TaskHandler();
125 uint32_t time = HALTick::GetInstance().GetElapseTime(temp);
126 if (time < DEFAULT_TASK_PERIOD) {
127 osDelay(DEFAULT_TASK_PERIOD - time);
128 }
129 #if ENABLE_FPS_SUPPORT
130 if (HALTick::GetInstance().GetElapseTime(start) >= ONE_SECOND) {
131 GRAPHIC_LOGD("%u fps", (uint32_t)RenderManager::GetInstance().GetFPS());
132 start = HALTick::GetInstance().GetTime();
133 }
134 #endif
135 }
136 return nullptr;
137 }
138
139 #define UI_THREAD_STACK_SIZE (1024 * 32)
UiMain(void)140 void UiMain(void)
141 {
142 struct sched_param param = {0};
143
144 pthread_t thread;
145 pthread_attr_t attr;
146 (void)pthread_attr_init(&attr);
147 (void)pthread_attr_setstacksize(&attr, UI_THREAD_STACK_SIZE);
148 param.sched_priority = 15; // 15: UiMainTask priority
149 (void)pthread_attr_setschedparam(&attr, ¶m);
150 (void)pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
151 if (pthread_create(&thread, &attr, UiMainTask, nullptr) != 0) {
152 GRAPHIC_LOGE("Failed to create UiMainTask");
153 }
154 }
155