1e41f4b71Sopenharmony_ci# Small-System Graphics Framework Integration
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe small-system graphics modules run in OpenHarmony as a framework. Therefore, you only need to adapt to and implement APIs at the OpenHarmony HDF layer. The graphics subsystem can run on different platforms. For example, when developing an application on Windows or macOS, you can use Qt Creator for simple page layout, development, and debugging. The graphics framework has been adapted to the Windows and macOS platforms. To integrate the graphics framework into an existing project independently, you need to carry out the following adaptation operations:
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci1. Initialize the graphics engine.
6e41f4b71Sopenharmony_ci2. Adapt to the display.
7e41f4b71Sopenharmony_ci3. Adapt to the input device.
8e41f4b71Sopenharmony_ci4. Initialize the font engine.
9e41f4b71Sopenharmony_ci5. Interconnect to screen flush.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciFor details, see [OpenHarmony Small-System Graphics Simulator Adaptation Implementation](https://gitee.com/openharmony/arkui_ui_lite/tree/master/tools/qt/simulator).
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci### Initializing the Graphics Engine
14e41f4b71Sopenharmony_ciInitialize UI tasks, rendering modules, animation modules, and default styles.
15e41f4b71Sopenharmony_ci```c++
16e41f4b71Sopenharmony_ci// graphic_startup.h
17e41f4b71Sopenharmony_ciGraphicStartUp::Init();
18e41f4b71Sopenharmony_ci```
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci### Adapting to the Display
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciSet the screen size, connect to basic component rendering, obtain the graphics rendering buffer, and refresh the graphics rendering data to the screen for display.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciDuring display layer adaptation, note that the classes to inherit and implement vary according to hardware rendering and software rendering. The **BaseGfxEngine** class in [gfx_engine_manager.h](https://gitee.com/openharmony/arkui_ui_lite/blob/master/interfaces/innerkits/engines/gfx/gfx_engine_manager.h) is a pure virtual implementation. It defines only functions and does not contain any implementation. It can be used as the parent class of hardware rendering. The **SoftEngine** class in [soft_engine.h](https://gitee.com/openharmony/arkui_ui_lite/blob/master/interfaces/innerkits/engines/gfx/soft_engine.h) inherits from **BaseGfxEngine** and implements the functions of **BaseGfxEngine** at the software layer. It can be used as the parent class of software rendering.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ciThe **BaseGfxEngine** class has three types of functions:
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciType 1: functions used to obtain the display memory, apply for the buffer, and release the buffer.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciType 2: basic rendering functions, such as line drawing, **Blit**, and **Fill**.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciType 3: display functions, which are called to send the rendered content to the display.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ciThe functions for obtaining the display memory and the display functions must be implemented for different platforms. The second type of function is implemented by software by default in the graphics UI framework, that is, **SoftEngine** of **soft_engine.h**. You can inherit from **SoftEngine** to extend software rendering. If hardware acceleration is required on different platforms, for example, DMA2D, you can inherit from **BaseGfxEngine** of **gfx_engine_manager.h**, implement all pure virtual functions, and carry out scalability adaptation.
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciThe code for interconnecting to the graphics functions is as follows:
37e41f4b71Sopenharmony_ci```c++
38e41f4b71Sopenharmony_ci// gfx_engine_manager.h
39e41f4b71Sopenharmony_civirtual void DrawArc(BufferInfo& dst,
40e41f4b71Sopenharmony_ci                        ArcInfo& arcInfo,
41e41f4b71Sopenharmony_ci                        const Rect& mask,
42e41f4b71Sopenharmony_ci                        const Style& style,
43e41f4b71Sopenharmony_ci                        OpacityType opacity,
44e41f4b71Sopenharmony_ci                        uint8_t cap) = 0;
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_civirtual void DrawLine(BufferInfo& dst,
47e41f4b71Sopenharmony_ci                        const Point& start,
48e41f4b71Sopenharmony_ci                        const Point& end,
49e41f4b71Sopenharmony_ci                        const Rect& mask,
50e41f4b71Sopenharmony_ci                        int16_t width,
51e41f4b71Sopenharmony_ci                        ColorType color,
52e41f4b71Sopenharmony_ci                        OpacityType opacity) = 0;
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_civirtual void DrawLetter(BufferInfo& gfxDstBuffer,
55e41f4b71Sopenharmony_ci                        const uint8_t* fontMap,
56e41f4b71Sopenharmony_ci                        const Rect& fontRect,
57e41f4b71Sopenharmony_ci                        const Rect& subRect,
58e41f4b71Sopenharmony_ci                        const uint8_t fontWeight,
59e41f4b71Sopenharmony_ci                        const ColorType& color,
60e41f4b71Sopenharmony_ci                        const OpacityType opa) = 0;
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_civirtual void DrawCubicBezier(BufferInfo& dst,
63e41f4b71Sopenharmony_ci                                const Point& start,
64e41f4b71Sopenharmony_ci                                const Point& control1,
65e41f4b71Sopenharmony_ci                                const Point& control2,
66e41f4b71Sopenharmony_ci                                const Point& end,
67e41f4b71Sopenharmony_ci                                const Rect& mask,
68e41f4b71Sopenharmony_ci                                int16_t width,
69e41f4b71Sopenharmony_ci                                ColorType color,
70e41f4b71Sopenharmony_ci                                OpacityType opacity) = 0;
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_civirtual void
73e41f4b71Sopenharmony_ci    DrawRect(BufferInfo& dst, const Rect& rect, const Rect& dirtyRect, const Style& style, OpacityType opacity) = 0;
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_civirtual void DrawTransform(BufferInfo& dst,
76e41f4b71Sopenharmony_ci                            const Rect& mask,
77e41f4b71Sopenharmony_ci                            const Point& position,
78e41f4b71Sopenharmony_ci                            ColorType color,
79e41f4b71Sopenharmony_ci                            OpacityType opacity,
80e41f4b71Sopenharmony_ci                            const TransformMap& transMap,
81e41f4b71Sopenharmony_ci                            const TransformDataInfo& dataInfo) = 0;
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci// x/y: center of a circle
84e41f4b71Sopenharmony_civirtual void ClipCircle(const ImageInfo* info, float x, float y, float radius) = 0;
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_civirtual void Blit(BufferInfo& dst,
87e41f4b71Sopenharmony_ci                    const Point& dstPos,
88e41f4b71Sopenharmony_ci                    const BufferInfo& src,
89e41f4b71Sopenharmony_ci                    const Rect& subRect,
90e41f4b71Sopenharmony_ci                    const BlendOption& blendOption) = 0;
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_civirtual void Fill(BufferInfo& dst, const Rect& fillArea, const ColorType color, const OpacityType opacity) = 0;
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_civirtual void DrawPath(BufferInfo& dst,
95e41f4b71Sopenharmony_ci                        void* param,
96e41f4b71Sopenharmony_ci                        const Paint& paint,
97e41f4b71Sopenharmony_ci                        const Rect& rect,
98e41f4b71Sopenharmony_ci                        const Rect& invalidatedArea,
99e41f4b71Sopenharmony_ci                        const Style& style) = 0;
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_civirtual void FillPath(BufferInfo& dst,
102e41f4b71Sopenharmony_ci                        void* param,
103e41f4b71Sopenharmony_ci                        const Paint& paint,
104e41f4b71Sopenharmony_ci                        const Rect& rect,
105e41f4b71Sopenharmony_ci                        const Rect& invalidatedArea,
106e41f4b71Sopenharmony_ci                        const Style& style) = 0;
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_civirtual uint8_t* AllocBuffer(uint32_t size, uint32_t usage) = 0;
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_civirtual void FreeBuffer(uint8_t* buffer, uint32_t usage) = 0;
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_civirtual BufferInfo* GetFBBufferInfo()
113e41f4b71Sopenharmony_ci{
114e41f4b71Sopenharmony_ci    return nullptr;
115e41f4b71Sopenharmony_ci}
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_civirtual void AdjustLineStride(BufferInfo& info) {}
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_civirtual void Flush(const Rect& flushRect) {}
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_civirtual uint16_t GetScreenWidth()
122e41f4b71Sopenharmony_ci{
123e41f4b71Sopenharmony_ci    return screenWidth_;
124e41f4b71Sopenharmony_ci}
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_civirtual uint16_t GetScreenHeight()
127e41f4b71Sopenharmony_ci{
128e41f4b71Sopenharmony_ci    return screenHeight_;
129e41f4b71Sopenharmony_ci}
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_civirtual void SetScreenShape(ScreenShape screenShape)
132e41f4b71Sopenharmony_ci{
133e41f4b71Sopenharmony_ci    screenShape_ = screenShape;
134e41f4b71Sopenharmony_ci}
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_civirtual ScreenShape GetScreenShape()
137e41f4b71Sopenharmony_ci{
138e41f4b71Sopenharmony_ci    return screenShape_;
139e41f4b71Sopenharmony_ci}
140e41f4b71Sopenharmony_ci```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci### Adapting to the Input Device
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ciThe graphics framework supports touch, key, and rotation devices. Currently, you must inherit from **InputDevice** to implement the **Read** function for all input devices.
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ciFor touch devices, inherit from the **PointerInputDevice** class to implement the **Read** function. The x and y coordinates and pressed status need to be returned.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ciFor key input devices, inherit from the **KeyInputDevice** class to implement the **Read** function. The key ID and key status need to be set.
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ciFor rotation input devices, inherit from the **RotateInputDevice** class to implement the **Read** function. The rotate value needs to be set.
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciThe code for interconnecting to the **InputDevice** instance is as follows:
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci```c++
155e41f4b71Sopenharmony_ci// Read function in input_device.h
156e41f4b71Sopenharmony_ci/**
157e41f4b71Sopenharmony_ci * @brief Reads data from hardware. You should override this to set data. *
158e41f4b71Sopenharmony_ci * @param [out] Indicates device data. *
159e41f4b71Sopenharmony_ci * @returns Returns no more data to read if true.
160e41f4b71Sopenharmony_ci */
161e41f4b71Sopenharmony_civirtual bool Read(DeviceData& data) = 0;
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci// Inherit from and implement the Read function of the InputDevice base class. The following uses a touch event as an example:
164e41f4b71Sopenharmony_ciclass TouchInput : public OHOS::PointerInputDevice {
165e41f4b71Sopenharmony_cipublic:
166e41f4b71Sopenharmony_ci    TouchInput() {}
167e41f4b71Sopenharmony_ci    virtual TouchInput() {}
168e41f4b71Sopenharmony_ci    // Implement the read function.
169e41f4b71Sopenharmony_ci    bool Read(OHOS::DeviceData& data) override
170e41f4b71Sopenharmony_ci    {
171e41f4b71Sopenharmony_ci        // Set the position and state. You should update the value when it is being touched.
172e41f4b71Sopenharmony_ci        data.point.x = g_lastX;
173e41f4b71Sopenharmony_ci        data.point.y = g_lastY;
174e41f4b71Sopenharmony_ci        data.state = g_leftButtonDown ? STATE_PRESS : STATE_RELEASE;
175e41f4b71Sopenharmony_ci        return false;
176e41f4b71Sopenharmony_ci    }
177e41f4b71Sopenharmony_ci};
178e41f4b71Sopenharmony_ci```
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci### Initializing the Font Engine
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ciFonts are classified into dot matrix fonts and vector fonts.
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ciFor dot matrix fonts, use the font packaging tool to generate a **font.bin** file. The tool supports packaging of Chinese and English fonts. For details about the supported font sizes and font IDs, see **ui_text_language.h** generated by the tool.
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ciFor vector fonts, **DEFAULT_VECTOR_FONT_FILENAME** is registered by default. To use other fonts, call **RegisterFontInfo** to register the required font files. Vector font parsing and layout depend on the third-party open-source software FreeType and ICU. To support complex languages such as Arabic, you must introduce the third-party open-source software HarfBuzz and enable **ENABLE_SHAPING** and **ENABLE_ICU**.
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ciThe code of font engine initialization is as follows:
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci```c++
193e41f4b71Sopenharmony_ci// graphic_config.h
194e41f4b71Sopenharmony_ci#define DEFAULT_VECTOR_FONT_FILENAME      "SourceHanSansSC-Regular.otf"
195e41f4b71Sopenharmony_ci// Vector font switch
196e41f4b71Sopenharmony_ci#define ENABLE_VECTOR_FONT                1
197e41f4b71Sopenharmony_ci// Dot-matrix font switch
198e41f4b71Sopenharmony_ci#define ENABLE_BITMAP_FONT                0
199e41f4b71Sopenharmony_ci#define ENABLE_ICU                        0
200e41f4b71Sopenharmony_ci#define ENABLE_SHAPING                    0
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci// ui_font.h
203e41f4b71Sopenharmony_ciuint8_t RegisterFontInfo(const char* ttfName, uint8_t shaping = 0)
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci// graphic_startup.h
206e41f4b71Sopenharmony_cistatic void InitFontEngine(uintptr_t psramAddr, uint32_t psramLen, const char* dPath, const char* ttfName);
207e41f4b71Sopenharmony_ci```
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci### Interconnecting to Screen Flush
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ciPeriodically call **TaskHandler** based on the screen hardware refresh signal (similar to Vsync).
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ciThe code for interconnecting to screen flush is as follows:
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci```c++
216e41f4b71Sopenharmony_ciTaskManager::GetInstance()->TaskHandler();
217e41f4b71Sopenharmony_ci```
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci### Sample Code
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ciThe macro definitions related to interconnection are described as follows:
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci```c++
224e41f4b71Sopenharmony_ci// graphic_config.h
225e41f4b71Sopenharmony_ci// By default, macro definitions of different levels of devices are defined. For lightweight devices, enable the VERSION_LITE macro.
226e41f4b71Sopenharmony_ci/**
227e41f4b71Sopenharmony_ci * Defines three graphics library versions: lightweight, standard, and extended versions.
228e41f4b71Sopenharmony_ci * The three versions have different requirements on the memory and hardware.
229e41f4b71Sopenharmony_ci * The standard version is enabled by default.
230e41f4b71Sopenharmony_ci *
231e41f4b71Sopenharmony_ci * The macros of the versions are defined as follows:
232e41f4b71Sopenharmony_ci * Name                | Version Description
233e41f4b71Sopenharmony_ci * ------------------- | ----------
234e41f4b71Sopenharmony_ci * VERSION_LITE        | Lightweight version
235e41f4b71Sopenharmony_ci * VERSION_STANDARD    | Standard version
236e41f4b71Sopenharmony_ci * VERSION_EXTENDED    | Extended version
237e41f4b71Sopenharmony_ci */
238e41f4b71Sopenharmony_ci#ifdef _LITEOS
239e41f4b71Sopenharmony_ci#define VERSION_LITE
240e41f4b71Sopenharmony_ci#elif defined _WIN32 || defined __APPLE__
241e41f4b71Sopenharmony_ci#define VERSION_LITE
242e41f4b71Sopenharmony_ci#else
243e41f4b71Sopenharmony_ci#define VERSION_STANDARD
244e41f4b71Sopenharmony_ci#endif
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci// Disable window synthesis. Enabling window synthesis depends on the WMS window synthesis service.
247e41f4b71Sopenharmony_ci/**
248e41f4b71Sopenharmony_ci * @brief Multi-window, which is disabled by default on WIN32.
249e41f4b71Sopenharmony_ci */
250e41f4b71Sopenharmony_ci#define ENABLE_WINDOW                     0
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci// Disable the support for images in PNG and JPEG formats. To enable the support, introduce a third-party library.
253e41f4b71Sopenharmony_ci#define ENABLE_JPEG_AND_PNG               0
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci// Hardware acceleration. If hardware acceleration is disabled, CPU soft rendering is used by default. You can disable this feature first and enable it after the page is displayed.
256e41f4b71Sopenharmony_ci/**
257e41f4b71Sopenharmony_ci * @brief Graphics rendering hardware acceleration, which is disabled by default on WIN32.
258e41f4b71Sopenharmony_ci */
259e41f4b71Sopenharmony_ci#define ENABLE_HARDWARE_ACCELERATION     0
260e41f4b71Sopenharmony_ci```
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ciThe code snippet is as follows:
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci```c++
265e41f4b71Sopenharmony_ciusing namespace OHOS;
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ciint main(int argc, char** argv)
268e41f4b71Sopenharmony_ci{
269e41f4b71Sopenharmony_ci    // Initialize the graphic engine.
270e41f4b71Sopenharmony_ci    GraphicStartUp::Init();
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci    // Initialize the display and input device.
273e41f4b71Sopenharmony_ci    InitHal();
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci    // Initialize the font engine.
276e41f4b71Sopenharmony_ci    InitFontEngine();
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci    // Run your application code.
279e41f4b71Sopenharmony_ci    RunApp();
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci    // Use while loop to simulate the hardware flush callback.
282e41f4b71Sopenharmony_ci    // You should call *TaskHandler* in screen flush signal callback (like Vsync).
283e41f4b71Sopenharmony_ci    while (1) {
284e41f4b71Sopenharmony_ci        TaskManager::GetInstance()->TaskHandler();
285e41f4b71Sopenharmony_ci        Sleep(DEFAULT_TASK_PERIOD);
286e41f4b71Sopenharmony_ci    }
287e41f4b71Sopenharmony_ci    return 0;
288e41f4b71Sopenharmony_ci}
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci// Below is the memory pool.
291e41f4b71Sopenharmony_cistatic uint8_t g_fontPsramBaseAddr[MIN_FONT_PSRAM_LENGTH];
292e41f4b71Sopenharmony_ci#if ENABLE_SHAPING
293e41f4b71Sopenharmony_cistatic uint8_t g_shapePsramBaseAddr[MIN_SHAPING_PSRAM_LENGTH];
294e41f4b71Sopenharmony_ci#else
295e41f4b71Sopenharmony_cistatic uint8_t* g_shapePsramBaseAddr = nullptr;
296e41f4b71Sopenharmony_ci#endif
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_cistatic void InitFontEngine()
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci{
301e41f4b71Sopenharmony_ci#if ENABLE_VECTOR_FONT
302e41f4b71Sopenharmony_ci    GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(g_fontMemBaseAddr), MIN_FONT_PSRAM_LENGTH, VECTOR_FONT_DIR, DEFAULT_VECTOR_FONT_FILENAME);
303e41f4b71Sopenharmony_ci#else
304e41f4b71Sopenharmony_ci    BitmapFontInit();
305e41f4b71Sopenharmony_ci    std::string dPath(_pgmptr);
306e41f4b71Sopenharmony_ci    size_t len = dPath.size();
307e41f4b71Sopenharmony_ci    size_t pos = dPath.find_last_of('\\');
308e41f4b71Sopenharmony_ci    dPath.replace((pos + 1), (len - pos), "..\\..\\simulator\\font\\font.bin");
309e41f4b71Sopenharmony_ci    GraphicStartUp::InitFontEngine(reinterpret_cast<uintptr_t>(g_fontMemBaseAddr), MIN_FONT_PSRAM_LENGTH, dPath.c_str(), nullptr);
310e41f4b71Sopenharmony_ci#endif
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ci#if ENABLE_ICU
313e41f4b71Sopenharmony_ci    GraphicStartUp::InitLineBreakEngine(reinterpret_cast<uintptr_t>(g_icuMemBaseAddr), SHAPING_WORD_DICT_LENGTH,
314e41f4b71Sopenharmony_ci VECTOR_FONT_DIR, DEFAULT_LINE_BREAK_RULE_FILENAME);
315e41f4b71Sopenharmony_ci#endif
316e41f4b71Sopenharmony_ci}
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ci// Display adapter
319e41f4b71Sopenharmony_ciclass SDLMonitorGfxEngine : public BaseGfxEngine {
320e41f4b71Sopenharmony_cipublic:
321e41f4b71Sopenharmony_ci    BufferInfo* GetFBBufferInfo() override
322e41f4b71Sopenharmony_ci    {
323e41f4b71Sopenharmony_ci        static BufferInfo* bufferInfo = nullptr;
324e41f4b71Sopenharmony_ci        if (bufferInfo == nullptr) {
325e41f4b71Sopenharmony_ci            bufferInfo = new BufferInfo;
326e41f4b71Sopenharmony_ci            bufferInfo->rect = {0, 0, HORIZONTAL_RESOLUTION - 1, VERTICAL_RESOLUTION - 1};
327e41f4b71Sopenharmony_ci            bufferInfo->mode = ARGB8888;
328e41f4b71Sopenharmony_ci            bufferInfo->color = 0x44;
329e41f4b71Sopenharmony_ci            bufferInfo->virAddr = GetFramBuff();
330e41f4b71Sopenharmony_ci            bufferInfo->phyAddr = bufferInfo->virAddr;
331e41f4b71Sopenharmony_ci            // 4: bpp
332e41f4b71Sopenharmony_ci            bufferInfo->stride = HORIZONTAL_RESOLUTION * 4;
333e41f4b71Sopenharmony_ci            bufferInfo->width = HORIZONTAL_RESOLUTION;
334e41f4b71Sopenharmony_ci            bufferInfo->height = VERTICAL_RESOLUTION;
335e41f4b71Sopenharmony_ci        }
336e41f4b71Sopenharmony_ci
337e41f4b71Sopenharmony_ci        return bufferInfo;
338e41f4b71Sopenharmony_ci    }
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_ci    void Flush() override
341e41f4b71Sopenharmony_ci    {
342e41f4b71Sopenharmony_ci        MonitorRenderFinish();
343e41f4b71Sopenharmony_ci    }
344e41f4b71Sopenharmony_ci};
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_ciclass TouchInput : public OHOS::PointerInputDevice {
347e41f4b71Sopenharmony_cipublic:
348e41f4b71Sopenharmony_ci    TouchInput() {}
349e41f4b71Sopenharmony_ci    virtual TouchInput() {}
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci    // Implement the read function.
352e41f4b71Sopenharmony_ci    bool Read(OHOS::DeviceData& data) override
353e41f4b71Sopenharmony_ci    {
354e41f4b71Sopenharmony_ci        // Set the position x, y, and state. You should update the
355e41f4b71Sopenharmony_ci        // g_lastX/g_lastY/g_leftButtonDown when it is being touched.
356e41f4b71Sopenharmony_ci        data.point.x = g_lastX;
357e41f4b71Sopenharmony_ci        data.point.y = g_lastY;
358e41f4b71Sopenharmony_ci        data.state = g_leftButtonDown ? STATE_PRESS : STATE_RELEASE;
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci        return false;
361e41f4b71Sopenharmony_ci    }
362e41f4b71Sopenharmony_ci};
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ciclass KeyInput : public OHOS::KeyInputDevice {
365e41f4b71Sopenharmony_cipublic:
366e41f4b71Sopenharmony_ci    KeyInput();
367e41f4b71Sopenharmony_ci    virtual ~KeyInput() {}
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci    // Implement the read function.
370e41f4b71Sopenharmony_ci    bool Read(OHOS::DeviceData& data) override
371e41f4b71Sopenharmony_ci    {
372e41f4b71Sopenharmony_ci        data.keyId = g_lastKeyId;
373e41f4b71Sopenharmony_ci        data.state = g_lastState;
374e41f4b71Sopenharmony_ci        g_lastState = INVALID_KEY_STATE;
375e41f4b71Sopenharmony_ci        return false;
376e41f4b71Sopenharmony_ci    }
377e41f4b71Sopenharmony_ci};
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_ci// Other device to add, if required.
380e41f4b71Sopenharmony_ciclass XXInput : public OHOS::XXInputDevice {
381e41f4b71Sopenharmony_cipublic:
382e41f4b71Sopenharmony_ci    KeyInput();
383e41f4b71Sopenharmony_ci    virtual ~KeyInput() {}
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci    // Implement the read function.
386e41f4b71Sopenharmony_ci    bool Read(OHOS::DeviceData& data) override
387e41f4b71Sopenharmony_ci    {
388e41f4b71Sopenharmony_ci        // Set the device data information.
389e41f4b71Sopenharmony_ci    }
390e41f4b71Sopenharmony_ci};
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_cistatic void InitHal()
393e41f4b71Sopenharmony_ci{
394e41f4b71Sopenharmony_ci    // Set up the GFX engine.
395e41f4b71Sopenharmony_ci    BaseGfxEngine::GetInstance()->InitEngine(new SDLMonitorGfxEngine());
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci    // Set up the touch device.
398e41f4b71Sopenharmony_ci    TouchInput* touch = new TouchInput();
399e41f4b71Sopenharmony_ci    InputDeviceManager::GetInstance()->Add(touch);
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci    // Set up the key device if required.
402e41f4b71Sopenharmony_ci    KeyInput* key = new KeyInput();
403e41f4b71Sopenharmony_ci    InputDeviceManager::GetInstance()->Add(key);
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci    // Set up xx device if required.
406e41f4b71Sopenharmony_ci    XXInput* inputXX = new XXInput();
407e41f4b71Sopenharmony_ci    InputDeviceManager::GetInstance()->Add(inputXX);
408e41f4b71Sopenharmony_ci}
409e41f4b71Sopenharmony_ci```
410