Debian Codegraph @

lvgl/src/draw/nxp/g2d/lv_draw_g2d.c

raw symbols SCIP
1 /**
2 * @file lv_draw_g2d.c
3 *
4 */
5
6 /**
7 * Copyright 2024 NXP
8 *
9 * SPDX-License-Identifier: MIT
10 */
11
12 #include "lv_draw_g2d.h"
13
14 #if LV_USE_G2D
15 #include "../../../misc/lv_area_private.h"
16 #include "g2d.h"
17 #include "lv_g2d_buf_map.h"
18 #include "lv_g2d_utils.h"
19
20 /*********************
21 * DEFINES
22 *********************/
23
24 #define DRAW_UNIT_ID_G2D 8
25
26 /**
27 * Enum name differs depending on g2d version (breaking API change, not handled in g2d.h)
28 * Note: enum value is the same in either case
29 * See https://github.com/nxp-imx/imx-g2d-pxp/commit/d7af84b5c8ad161b6898ffabe23918cb59fe2fe9
30 */
31 #if defined(LV_USE_PXP)
32 #if (G2D_VERSION_MAJOR >= 2) && (G2D_VERSION_MINOR < 3)
33 #define G2D_HARDWARE_PXP_V1 G2D_HARDWARE_PXP
34 #endif
35 #else
36 #define G2D_HARDWARE_PXP_V1 0
37 #endif
38
39 /**********************
40 * TYPEDEFS
41 **********************/
42
43 /**********************
44 * STATIC PROTOTYPES
45 **********************/
46
47 /**
48 * Evaluate a task and set the score and preferred G2D unit.
49 * Return 1 if task is preferred, 0 otherwise (task is not supported).
50 */
51 static int32_t _g2d_evaluate(lv_draw_unit_t * draw_unit, lv_draw_task_t * task);
52
53 /**
54 * Dispatch a task to the G2D unit.
55 * Return 1 if task was dispatched, 0 otherwise (task not supported).
56 */
57 static int32_t _g2d_dispatch(lv_draw_unit_t * draw_unit, lv_layer_t * layer);
58
59 /**
60 * Delete the G2D draw unit.
61 */
62 static int32_t _g2d_delete(lv_draw_unit_t * draw_unit);
63
64 #if LV_USE_G2D_DRAW_THREAD
65 static void _g2d_render_thread_cb(void * ptr);
66 #endif
67
68 static void _g2d_execute_drawing(lv_draw_task_t * t);
69
70 /**********************
71 * STATIC VARIABLES
72 **********************/
73
74 static int32_t is_hw_pxp = 0;
75
76 /**********************
77 * MACROS
78 **********************/
79
80 /**********************
81 * GLOBAL FUNCTIONS
82 **********************/
83
84 void lv_draw_g2d_init(void)
85 {
86 lv_draw_buf_g2d_init_handlers();
87 #if LV_USE_DRAW_G2D
88 lv_draw_g2d_unit_t * draw_g2d_unit = lv_draw_create_unit(sizeof(lv_draw_g2d_unit_t));
89 draw_g2d_unit->base_unit.evaluate_cb = _g2d_evaluate;
90 draw_g2d_unit->base_unit.dispatch_cb = _g2d_dispatch;
91 draw_g2d_unit->base_unit.delete_cb = _g2d_delete;
92 draw_g2d_unit->base_unit.name = "G2D";
93
94 #if LV_USE_G2D_DRAW_THREAD
95 lv_draw_sw_thread_dsc_t * thread_dsc = &draw_g2d_unit->thread_dsc;
96 thread_dsc->idx = 0;
97 thread_dsc->draw_unit = (void *) draw_g2d_unit;
98 lv_thread_init(&thread_dsc->thread, "g2ddraw", LV_DRAW_THREAD_PRIO, _g2d_render_thread_cb, LV_DRAW_THREAD_STACK_SIZE,
99 thread_dsc);
100 #endif
101 #endif
102 g2d_create_buf_map();
103 void * handle;
104 LV_ASSERT_MSG(!g2d_open(&handle), "Cannot open G2D handle\r\n");
105 g2d_query_hardware(handle, G2D_HARDWARE_PXP_V1, &is_hw_pxp);
106 g2d_set_handle(handle);
107 }
108
109 void lv_draw_g2d_deinit(void)
110 {
111 g2d_free_buf_map();
112 }
113
114 /**********************
115 * STATIC FUNCTIONS
116 **********************/
117 #if LV_USE_DRAW_G2D
118 static inline bool _g2d_dest_cf_supported(lv_color_format_t cf)
119 {
120 bool is_cf_supported = false;
121
122 switch(cf) {
123 case LV_COLOR_FORMAT_ARGB8888:
124 case LV_COLOR_FORMAT_XRGB8888:
125 is_cf_supported = true;
126 break;
127 case LV_COLOR_FORMAT_RGB565: {
128 if(!is_hw_pxp) {
129 is_cf_supported = true;
130 }
131 }
132 break;
133 default:
134 break;
135 }
136
137 return is_cf_supported;
138 }
139
140 static inline bool _g2d_src_cf_supported(lv_color_format_t cf)
141 {
142 bool is_cf_supported = false;
143
144 switch(cf) {
145 case LV_COLOR_FORMAT_ARGB8888:
146 case LV_COLOR_FORMAT_XRGB8888:
147 is_cf_supported = true;
148 break;
149 case LV_COLOR_FORMAT_RGB565: {
150 if(!is_hw_pxp) {
151 is_cf_supported = true;
152 }
153 }
154 break;
155 default:
156 break;
157 }
158
159 return is_cf_supported;
160 }
161
162 static bool _g2d_draw_img_supported(const lv_draw_image_dsc_t * draw_dsc)
163 {
164 bool has_recolor = (draw_dsc->recolor_opa > LV_OPA_MIN);
165 /* Recolor is not supported. */
166 if(has_recolor)
167 return false;
168
169 /* G2D can only rotate at 90x angles. */
170 if(draw_dsc->rotation % 900)
171 return false;
172
173 return true;
174 }
175
176 static int32_t _g2d_evaluate(lv_draw_unit_t * u, lv_draw_task_t * t)
177 {
178 LV_UNUSED(u);
179
180 const lv_draw_dsc_base_t * draw_dsc_base = (lv_draw_dsc_base_t *) t->draw_dsc;
181 lv_draw_buf_t * draw_buf = draw_dsc_base->layer->draw_buf;
182
183 if(!_g2d_dest_cf_supported(draw_dsc_base->layer->color_format))
184 return 0;
185
186 if(draw_buf && !g2d_search_buf_map(draw_buf->data))
187 return 0;
188
189 switch(t->type) {
190 case LV_DRAW_TASK_TYPE_FILL: {
191 const lv_draw_fill_dsc_t * draw_dsc = (lv_draw_fill_dsc_t *) t->draw_dsc;
192
193 /* Most simple case: just a plain rectangle (no radius, no gradient). */
194 if((draw_dsc->radius != 0) || (draw_dsc->grad.dir != (lv_grad_dir_t)LV_GRAD_DIR_NONE))
195 return 0;
196
197 if(t->preference_score > 70) {
198 t->preference_score = 70;
199 t->preferred_draw_unit_id = DRAW_UNIT_ID_G2D;
200 }
201 return 1;
202 }
203 case LV_DRAW_TASK_TYPE_IMAGE: {
204 /* TODO: Fix issue where images rendered to a different layer don't render in the final layer.*/
205 return 0;
206 #if 0
207 const lv_draw_image_dsc_t * draw_dsc = (lv_draw_image_dsc_t *) t->draw_dsc;
208
209 if(!_g2d_src_cf_supported(draw_dsc->header.cf))
210 return 0;
211
212 if(!_g2d_draw_img_supported(draw_dsc))
213 return 0;
214
215 if(t->preference_score > 70) {
216 t->preference_score = 70;
217 t->preferred_draw_unit_id = DRAW_UNIT_ID_G2D;
218 }
219 return 1;
220 #endif
221 }
222 default:
223 return 0;
224 }
225
226 return 0;
227
228 }
229
230 static int32_t _g2d_dispatch(lv_draw_unit_t * draw_unit, lv_layer_t * layer)
231 {
232 lv_draw_g2d_unit_t * draw_g2d_unit = (lv_draw_g2d_unit_t *) draw_unit;
233
234 #if LV_USE_OS
235 lv_draw_sw_thread_dsc_t * thread_dsc = &draw_g2d_unit->thread_dsc;
236
237 /* Return immediately if it's busy with draw task. */
238 if(thread_dsc->task_act)
239 return 0;
240 #else
241 /* Return immediately if it's busy with draw task. */
242 if(draw_g2d_unit->task_act)
243 return 0;
244 #endif
245
246 /* Try to get an ready to draw. */
247 lv_draw_task_t * t = lv_draw_get_available_task(layer, NULL, DRAW_UNIT_ID_G2D);
248
249 if(t == NULL || t->preferred_draw_unit_id != DRAW_UNIT_ID_G2D)
250 return LV_DRAW_UNIT_IDLE;
251
252 if(lv_draw_layer_alloc_buf(layer) == NULL)
253 return LV_DRAW_UNIT_IDLE;
254
255 t->state = LV_DRAW_TASK_STATE_IN_PROGRESS;
256 t->draw_unit = draw_unit;
257
258 #if LV_USE_G2D_DRAW_THREAD
259 thread_dsc->task_act = t;
260
261 /* Let the render thread work. */
262 if(thread_dsc->inited)
263 lv_thread_sync_signal(&thread_dsc->sync);
264 #else
265 draw_g2d_unit->task_act = t;
266
267 _g2d_execute_drawing(t);
268
269 draw_g2d_unit->task_act->state = LV_DRAW_TASK_STATE_FINISHED;
270 draw_g2d_unit->task_act = NULL;
271
272 /* The draw unit is free now. Request a new dispatching as it can get a new task. */
273 lv_draw_dispatch_request();
274 #endif
275
276 return 1;
277 }
278
279 static int32_t _g2d_delete(lv_draw_unit_t * draw_unit)
280 {
281 lv_result_t res = LV_RESULT_OK;
282
283 #if !LV_USE_G2D_DRAW_THREAD
284 LV_UNUSED(draw_unit);
285 #else
286 lv_draw_g2d_unit_t * draw_g2d_unit = (lv_draw_g2d_unit_t *) draw_unit;
287 lv_draw_sw_thread_dsc_t * thread_dsc = &draw_g2d_unit->thread_dsc;
288 LV_LOG_INFO("Cancel G2D draw thread.");
289 thread_dsc->exit_status = true;
290
291 if(thread_dsc->inited)
292 lv_thread_sync_signal(&thread_dsc->sync);
293
294 res = lv_thread_delete(&thread_dsc->thread);
295 #endif
296 g2d_close(g2d_get_handle());
297
298 return res;
299 }
300
301 static void _g2d_execute_drawing(lv_draw_task_t * t)
302 {
303 lv_layer_t * layer = t->target_layer;
304 lv_draw_buf_t * draw_buf = layer->draw_buf;
305
306 /* Invalidate only the drawing area */
307 lv_draw_buf_invalidate_cache(draw_buf, NULL);
308
309 switch(t->type) {
310 case LV_DRAW_TASK_TYPE_FILL:
311 lv_draw_g2d_fill(t);
312 break;
313 case LV_DRAW_TASK_TYPE_IMAGE:
314 lv_draw_g2d_img(t);
315 break;
316 default:
317 break;
318 }
319 }
320
321 #if LV_USE_G2D_DRAW_THREAD
322 static void _g2d_render_thread_cb(void * ptr)
323 {
324 lv_draw_sw_thread_dsc_t * thread_dsc = ptr;
325
326 lv_thread_sync_init(&thread_dsc->sync);
327 thread_dsc->inited = true;
328
329 while(1) {
330 /* Wait for sync if there is no task set. */
331 while(thread_dsc->task_act == NULL) {
332 if(thread_dsc->exit_status)
333 break;
334
335 lv_thread_sync_wait(&thread_dsc->sync);
336 }
337
338 if(thread_dsc->exit_status) {
339 LV_LOG_INFO("Ready to exit G2D draw thread.");
340 break;
341 }
342
343 _g2d_execute_drawing(thread_dsc->task_act);
344
345 /* Signal the ready state to dispatcher. */
346 thread_dsc->task_act->state = LV_DRAW_TASK_STATE_FINISHED;
347
348 /* Cleanup. */
349 thread_dsc->task_act = NULL;
350
351 /* The draw unit is free now. Request a new dispatching as it can get a new task. */
352 lv_draw_dispatch_request();
353 }
354
355 thread_dsc->inited = false;
356 lv_thread_sync_delete(&thread_dsc->sync);
357 LV_LOG_INFO("Exit G2D draw thread.");
358 }
359 #endif
360
361 #endif /*LV_USE_DRAW_G2D || LV_USE_ROTATE_G2D*/
362 #endif /*LV_USE_G2D*/