1/*
2 * Copyright (c) 2022 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 "disp_dev.h"
17#include "input_dev.h"
18#include "cmsis_os2.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 "engines/gfx/soft_engine.h"
24#include "font/ui_font.h"
25#include "font/ui_font_header.h"
26#include "font/ui_font_vector.h"
27#include "gfx_utils/graphic_log.h"
28#include "graphic_config.h"
29#include "hal_tick.h"
30#include "ui_adapter.h"
31
32#define ENABLE_FPS
33#ifdef ENABLE_ACE
34#include "product_adapter.h"
35#endif
36
37#define FONT_MEM_LEN (512 * 1024)
38
39static uint32_t g_fontMemBaseAddr[OHOS::MIN_FONT_PSRAM_LENGTH / 4];
40
41using namespace OHOS;
42
43static void InitFontEngine(void)
44{
45    GraphicStartUp::InitFontEngine(
46        reinterpret_cast<uintptr_t>(g_fontMemBaseAddr),
47        MIN_FONT_PSRAM_LENGTH,
48        VECTOR_FONT_DIR,
49        DEFAULT_VECTOR_FONT_FILENAME);
50}
51
52static void InitImageDecodeAbility(void)
53{
54    uint32_t imageType = IMG_SUPPORT_BITMAP | OHOS::IMG_SUPPORT_JPEG | OHOS::IMG_SUPPORT_PNG;
55    ImageDecodeAbility::GetInstance().SetImageDecodeAbility(imageType);
56}
57
58static void InitHal(void)
59{
60    DispDev *display = DispDev::GetInstance();
61    SoftEngine::InitGfxEngine(display);
62
63    InputDev *input = InputDev::GetInstance();
64    InputDeviceManager::GetInstance()->Add(input);
65}
66
67void UiAdapterInit(void)
68{
69    GraphicStartUp::Init();
70    InitHal();
71    InitFontEngine();
72    InitImageDecodeAbility();
73}
74
75__attribute__((weak)) void RunApp(void)
76{
77    GRAPHIC_LOGI("RunApp default");
78}
79
80#ifdef ENABLE_ACE
81static void RenderTEHandler(void)
82{
83}
84#endif
85
86static void UiAdapterTask(void *arg)
87{
88    (void)arg;
89
90    static constexpr uint32_t UI_TASK_DELAY = 100; // 1 sec delay for services init finish
91    osDelay(UI_TASK_DELAY);
92
93    UiAdapterInit();
94    RunApp();
95
96#ifdef ENABLE_ACE
97    const ACELite::TEHandlingHooks hooks = {RenderTEHandler, nullptr};
98    ACELite::ProductAdapter::RegTEHandlers(hooks);
99#endif
100#ifdef ENABLE_FPS
101    uint32_t cnt = 0;
102    uint32_t start = HALTick::GetInstance().GetTime();
103#endif
104
105    while (1) {
106#ifdef ENABLE_ACE
107        // Here render all js app in the same task.
108        ACELite::ProductAdapter::DispatchTEMessage();
109#endif
110        DispDev::GetInstance()->UpdateFBBuffer();
111        uint32_t temp = HALTick::GetInstance().GetTime();
112        TaskManager* inst = TaskManager::GetInstance();
113        inst->TaskHandler();
114        uint32_t time = HALTick::GetInstance().GetElapseTime(temp);
115        if (time < DEFAULT_TASK_PERIOD) {
116            osDelay(DEFAULT_TASK_PERIOD - time);
117        }
118#ifdef ENABLE_FPS
119        const int TICKS_OF_SEC = 1000;
120        cnt++;
121        time = HALTick::GetInstance().GetElapseTime(start);
122        if (time >= TICKS_OF_SEC && time != 0) {
123            start = HALTick::GetInstance().GetTime();
124            cnt = 0;
125        }
126#endif
127    }
128}
129
130void UiAdapterRun(void)
131{
132    const int UI_TASK_SIZE = (32 * 1024);
133
134    osThreadAttr_t attr = {0};
135    attr.stack_size = UI_TASK_SIZE;
136    attr.priority = osPriorityNormal;
137    attr.name = "UiAdapterThread";
138    if (osThreadNew((osThreadFunc_t)UiAdapterTask, NULL, &attr) == NULL) {
139        GRAPHIC_LOGE("Failed to create UiAdapterTask");
140    }
141}
142