• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 #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 "key_input.h"
32 #define ENABLE_FPS
33 #ifdef ENABLE_ACE
34 #include "product_adapter.h"
35 #endif
36 
37 static const int32_t FONT_MEM_LEN = 40 * 1024;
38 static uint8_t g_fontMemBaseAddr[FONT_MEM_LEN];
39 #if ENABLE_ICU
40 static uint8_t g_icuMemBaseAddr[SHAPING_WORD_DICT_LENGTH];
41 #endif
42 
43 using namespace OHOS;
44 
InitFontEngine()45 static void InitFontEngine()
46 {
47 #if ENABLE_VECTOR_FONT
48     GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(g_fontMemBaseAddr), FONT_MEM_LEN, VECTOR_FONT_DIR,
49                                    DEFAULT_VECTOR_FONT_FILENAME);
50 #endif
51 
52 #if ENABLE_ICU
53     GraphicStartUp::InitLineBreakEngine(reinterpret_cast<uintptr_t>(g_icuMemBaseAddr), SHAPING_WORD_DICT_LENGTH,
54                                         VECTOR_FONT_DIR, DEFAULT_LINE_BREAK_RULE_FILENAME);
55 #endif
56 }
57 
InitImageDecodeAbility()58 static void InitImageDecodeAbility()
59 {
60     uint32_t imageType = IMG_SUPPORT_BITMAP | OHOS::IMG_SUPPORT_JPEG | OHOS::IMG_SUPPORT_PNG;
61     ImageDecodeAbility::GetInstance().SetImageDecodeAbility(imageType);
62 }
63 
InitHal()64 static void InitHal()
65 {
66     DisplayDevice *display = DisplayDevice::GetInstance();
67     BaseGfxEngine::InitGfxEngine(display);
68 #ifdef LOSCFG_DRIVERS_USER_KEY_INPUT
69     KeyInput *key = KeyInput::GetInstance();
70     InputDeviceManager::GetInstance()->Add(key);
71 #endif
72 }
73 
InitUiKit(void)74 void InitUiKit(void)
75 {
76     GraphicStartUp::Init();
77     // init display/input device
78     InitHal();
79     // init font engine
80     InitFontEngine();
81     // init suppot image format
82     InitImageDecodeAbility();
83 }
84 
RunApp(void)85 __attribute__((weak)) void RunApp(void)
86 {
87     GRAPHIC_LOGI("RunApp default");
88 }
89 
90 #ifdef ENABLE_ACE
RenderTEHandler()91 static void RenderTEHandler()
92 {
93 }
94 #endif
95 
UiMainTask(void * arg)96 static void UiMainTask(void *arg)
97 {
98     (void)arg;
99     // init ui kit (hal/fontengine/imagedecode/...)
100     InitUiKit();
101 
102     // run user app
103     RunApp();
104 
105 #ifdef ENABLE_ACE
106     const ACELite::TEHandlingHooks hooks = {RenderTEHandler, NULL};
107     ACELite::ProductAdapter::RegTEHandlers(hooks);
108 #endif
109 #ifdef ENABLE_FPS
110     uint32_t cnt = 0;
111     uint32_t start = HALTick::GetInstance().GetTime();
112 #endif
113     while (1) {
114 #ifdef ENABLE_ACE
115         // Here render all js app in the same task.
116         ACELite::ProductAdapter::DispatchTEMessage();
117 #endif
118 
119         DisplayDevice::GetInstance()->UpdateFBBuffer();
120         uint32_t temp = HALTick::GetInstance().GetTime();
121         TaskManager::GetInstance()->TaskHandler();
122         uint32_t time = HALTick::GetInstance().GetElapseTime(temp);
123         if (time < DEFAULT_TASK_PERIOD) {
124             osDelay(DEFAULT_TASK_PERIOD - time);
125         }
126 #ifdef ENABLE_FPS
127         cnt++;
128         time = HALTick::GetInstance().GetElapseTime(start);
129         int16_t timeout = 1000;
130         if (time >= timeout) {
131             GRAPHIC_LOGD("uitest time=%u, cnt=%u", time, cnt);
132             if (time == 0) {
133                 return;
134             }
135             GRAPHIC_LOGD("uitest %u fps", timeout * cnt / time);
136             start = HALTick::GetInstance().GetTime();
137             cnt = 0;
138         }
139 #endif
140     }
141 }
142 static const int32_t UI_THREAD_STACK_SIZE = 1024 * 32;
143 
UiMain(void)144 void UiMain(void)
145 {
146     osThreadAttr_t attr;
147 
148     attr.name = "display-demo";
149     attr.attr_bits = 0U;
150     attr.cb_mem = NULL;
151     attr.cb_size = 0U;
152     attr.stack_mem = NULL;
153     attr.stack_size = UI_THREAD_STACK_SIZE;
154     attr.priority = osPriorityNormal;
155 
156     if (osThreadNew((osThreadFunc_t)UiMainTask, NULL, &attr) == NULL) {
157         GRAPHIC_LOGE("Failed to create UiMainTask");
158     }
159 }
160