Debian Codegraph @

lvgl/src/draw/nxp/pxp/lv_draw_pxp.c

raw symbols SCIP
1 /**
2 * @file lv_draw_pxp.c
3 *
4 */
5
6 /**
7 * Copyright 2022-2024 NXP
8 *
9 * SPDX-License-Identifier: MIT
10 */
11
12 /*********************
13 * INCLUDES
14 *********************/
15
16 #include "lv_draw_pxp.h"
17
18 #if LV_USE_PXP
19 #if LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP
20 #include "lv_pxp_cfg.h"
21 #include "lv_pxp_utils.h"
22
23 #if LV_USE_PARALLEL_DRAW_DEBUG
24 #include "../../../core/lv_global.h"
25 #endif
26
27 /*********************
28 * DEFINES
29 *********************/
30
31 #define DRAW_UNIT_ID_PXP 3
32
33 /**********************
34 * TYPEDEFS
35 **********************/
36
37 /**********************
38 * STATIC PROTOTYPES
39 **********************/
40
41 /*
42 * Evaluate a task and set the score and preferred PXP unit.
43 * Return 1 if task is preferred, 0 otherwise (task is not supported).
44 */
45 static int32_t _pxp_evaluate(lv_draw_unit_t * draw_unit, lv_draw_task_t * task);
46
47 /*
48 * Dispatch a task to the PXP unit.
49 * Return 1 if task was dispatched, 0 otherwise (task not supported).
50 */
51 static int32_t _pxp_dispatch(lv_draw_unit_t * draw_unit, lv_layer_t * layer);
52
53 /*
54 * Delete the PXP draw unit.
55 */
56 static int32_t _pxp_delete(lv_draw_unit_t * draw_unit);
57
58 #if LV_USE_PXP_DRAW_THREAD
59 static void _pxp_render_thread_cb(void * ptr);
60 #endif
61
62 static void _pxp_execute_drawing(lv_draw_pxp_unit_t * u);
63
64 /**********************
65 * STATIC PROTOTYPES
66 **********************/
67
68 /**********************
69 * STATIC VARIABLES
70 **********************/
71
72 #if LV_USE_PARALLEL_DRAW_DEBUG
73 #define _draw_info LV_GLOBAL_DEFAULT()->draw_info
74 #endif
75
76 /**********************
77 * MACROS
78 **********************/
79
80 /**********************
81 * GLOBAL FUNCTIONS
82 **********************/
83
84 void lv_draw_pxp_init(void)
85 {
86 lv_pxp_init();
87
88 #if LV_USE_DRAW_PXP
89 lv_draw_buf_pxp_init_handlers();
90
91 lv_draw_pxp_unit_t * draw_pxp_unit = lv_draw_create_unit(sizeof(lv_draw_pxp_unit_t));
92 draw_pxp_unit->base_unit.evaluate_cb = _pxp_evaluate;
93 draw_pxp_unit->base_unit.dispatch_cb = _pxp_dispatch;
94 draw_pxp_unit->base_unit.delete_cb = _pxp_delete;
95 draw_pxp_unit->base_unit.name = "NXP_PXP";
96
97 #if LV_USE_PXP_DRAW_THREAD
98 lv_thread_init(&draw_pxp_unit->thread, "pxpdraw", LV_DRAW_THREAD_PRIO, _pxp_render_thread_cb, 2 * 1024, draw_pxp_unit);
99 #endif
100 #endif /*LV_USE_DRAW_PXP*/
101 }
102
103 void lv_draw_pxp_deinit(void)
104 {
105 lv_pxp_deinit();
106 }
107
108 void lv_draw_pxp_rotate(const void * src_buf, void * dest_buf, int32_t src_width, int32_t src_height,
109 int32_t src_stride, int32_t dest_stride, lv_display_rotation_t rotation,
110 lv_color_format_t cf)
111 {
112 lv_pxp_reset();
113
114 /* Convert rotation angle
115 * To be in sync with CPU, the received angle is counterclockwise
116 * and the PXP constants are for clockwise rotation
117 *
118 * counterclockwise clockwise
119 * LV_DISPLAY_ROTATION_90 -> kPXP_Rotate270
120 * LV_DISPLAY_ROTATION_270 -> kPXP_Rotate90
121 */
122 pxp_rotate_degree_t pxp_rotation;
123 switch(rotation) {
124 case LV_DISPLAY_ROTATION_0:
125 pxp_rotation = kPXP_Rotate0;
126 break;
127 case LV_DISPLAY_ROTATION_270:
128 pxp_rotation = kPXP_Rotate90;
129 break;
130 case LV_DISPLAY_ROTATION_180:
131 pxp_rotation = kPXP_Rotate180;
132 break;
133 case LV_DISPLAY_ROTATION_90:
134 pxp_rotation = kPXP_Rotate270;
135 break;
136 default:
137 pxp_rotation = kPXP_Rotate0;
138 break;
139 }
140 PXP_SetRotateConfig(PXP_ID, kPXP_RotateOutputBuffer, pxp_rotation, kPXP_FlipDisable);
141
142 /*Simple blit, no effect - Disable PS buffer*/
143 PXP_SetProcessSurfacePosition(PXP_ID, 0xFFFFU, 0xFFFFU, 0U, 0U);
144
145 /*AS buffer - source image*/
146 pxp_as_buffer_config_t asBufferConfig = {
147 .pixelFormat = pxp_get_as_px_format(cf),
148 .bufferAddr = (uint32_t)src_buf,
149 .pitchBytes = src_stride
150 };
151 PXP_SetAlphaSurfaceBufferConfig(PXP_ID, &asBufferConfig);
152 PXP_SetAlphaSurfacePosition(PXP_ID, 0U, 0U, src_width - 1U, src_height - 1U);
153 PXP_EnableAlphaSurfaceOverlayColorKey(PXP_ID, false);
154
155 /*Output buffer.*/
156 pxp_output_buffer_config_t outputBufferConfig = {
157 .pixelFormat = pxp_get_out_px_format(cf),
158 .interlacedMode = kPXP_OutputProgressive,
159 .buffer0Addr = (uint32_t)dest_buf,
160 .buffer1Addr = (uint32_t)0U,
161 .pitchBytes = dest_stride,
162 .width = src_width,
163 .height = src_height
164 };
165 PXP_SetOutputBufferConfig(PXP_ID, &outputBufferConfig);
166
167 lv_pxp_run();
168 }
169
170 /**********************
171 * STATIC FUNCTIONS
172 **********************/
173 #if LV_USE_DRAW_PXP
174 static inline bool _pxp_src_cf_supported(lv_color_format_t cf)
175 {
176 bool is_cf_supported = false;
177
178 switch(cf) {
179 case LV_COLOR_FORMAT_RGB565:
180 case LV_COLOR_FORMAT_ARGB8888:
181 case LV_COLOR_FORMAT_XRGB8888:
182 is_cf_supported = true;
183 break;
184 default:
185 break;
186 }
187
188 return is_cf_supported;
189 }
190
191 static inline bool _pxp_dest_cf_supported(lv_color_format_t cf)
192 {
193 bool is_cf_supported = false;
194
195 switch(cf) {
196 case LV_COLOR_FORMAT_RGB565:
197 case LV_COLOR_FORMAT_RGB888:
198 case LV_COLOR_FORMAT_ARGB8888:
199 case LV_COLOR_FORMAT_XRGB8888:
200 is_cf_supported = true;
201 break;
202 default:
203 break;
204 }
205
206 return is_cf_supported;
207 }
208
209 static bool _pxp_draw_img_supported(const lv_draw_image_dsc_t * draw_dsc)
210 {
211 const lv_image_dsc_t * img_dsc = draw_dsc->src;
212
213 bool has_recolor = (draw_dsc->recolor_opa > LV_OPA_MIN);
214 bool has_transform = (draw_dsc->rotation != 0 || draw_dsc->scale_x != LV_SCALE_NONE ||
215 draw_dsc->scale_y != LV_SCALE_NONE);
216
217 /* Recolor and transformation are not supported at the same time. */
218 if(has_recolor && has_transform)
219 return false;
220
221 bool has_opa = (draw_dsc->opa < (lv_opa_t)LV_OPA_MAX);
222 bool src_has_alpha = (img_dsc->header.cf == LV_COLOR_FORMAT_ARGB8888);
223
224 /*
225 * Recolor or transformation for images w/ opa or alpha channel can't
226 * be obtained in a single PXP configuration. Two steps are required.
227 */
228 if((has_recolor || has_transform) && (has_opa || src_has_alpha))
229 return false;
230
231 /* PXP can only rotate at 90x angles. */
232 if(draw_dsc->rotation % 900)
233 return false;
234
235 /*
236 * PXP is set to process 16x16 blocks to optimize the system for memory
237 * bandwidth and image processing time.
238 * The output engine essentially truncates any output pixels after the
239 * desired number of pixels has been written.
240 * When rotating a source image and the output is not divisible by the block
241 * size, the incorrect pixels could be truncated and the final output image
242 * can look shifted.
243 *
244 * No combination of rotate with flip, scaling or decimation is possible
245 * if buffer is unaligned.
246 */
247 if(has_transform && (img_dsc->header.w % 16 || img_dsc->header.h % 16))
248 return false;
249
250 return true;
251 }
252
253 static int32_t _pxp_evaluate(lv_draw_unit_t * u, lv_draw_task_t * t)
254 {
255 LV_UNUSED(u);
256
257 const lv_draw_dsc_base_t * draw_dsc_base = (lv_draw_dsc_base_t *) t->draw_dsc;
258
259 if(!_pxp_dest_cf_supported(draw_dsc_base->layer->color_format))
260 return 0;
261
262 switch(t->type) {
263 case LV_DRAW_TASK_TYPE_FILL: {
264 const lv_draw_fill_dsc_t * draw_dsc = (lv_draw_fill_dsc_t *) t->draw_dsc;
265
266 /* Most simple case: just a plain rectangle (no radius, no gradient). */
267 if((draw_dsc->radius != 0) || (draw_dsc->grad.dir != (lv_grad_dir_t)LV_GRAD_DIR_NONE))
268 return 0;
269
270 if(t->preference_score > 70) {
271 t->preference_score = 70;
272 t->preferred_draw_unit_id = DRAW_UNIT_ID_PXP;
273 }
274 return 1;
275 }
276
277 case LV_DRAW_TASK_TYPE_LAYER: {
278 const lv_draw_image_dsc_t * draw_dsc = (lv_draw_image_dsc_t *) t->draw_dsc;
279 lv_layer_t * layer_to_draw = (lv_layer_t *)draw_dsc->src;
280
281 if(!_pxp_src_cf_supported(layer_to_draw->color_format))
282 return 0;
283
284 if(!_pxp_draw_img_supported(draw_dsc))
285 return 0;
286
287 if(t->preference_score > 70) {
288 t->preference_score = 70;
289 t->preferred_draw_unit_id = DRAW_UNIT_ID_PXP;
290 }
291 return 1;
292 }
293
294 case LV_DRAW_TASK_TYPE_IMAGE: {
295 lv_draw_image_dsc_t * draw_dsc = (lv_draw_image_dsc_t *) t->draw_dsc;
296 const lv_image_dsc_t * img_dsc = draw_dsc->src;
297
298 if(img_dsc->header.cf >= LV_COLOR_FORMAT_PROPRIETARY_START)
299 return 0;
300
301 if((!_pxp_src_cf_supported(img_dsc->header.cf)) ||
302 (!pxp_buf_aligned(img_dsc->data, img_dsc->header.stride)))
303 return 0;
304
305 if(!_pxp_draw_img_supported(draw_dsc))
306 return 0;
307
308 if(t->preference_score > 70) {
309 t->preference_score = 70;
310 t->preferred_draw_unit_id = DRAW_UNIT_ID_PXP;
311 }
312 return 1;
313 }
314 default:
315 return 0;
316 }
317
318 return 0;
319 }
320
321 static int32_t _pxp_dispatch(lv_draw_unit_t * draw_unit, lv_layer_t * layer)
322 {
323 lv_draw_pxp_unit_t * draw_pxp_unit = (lv_draw_pxp_unit_t *) draw_unit;
324
325 /* Return immediately if it's busy with draw task. */
326 if(draw_pxp_unit->task_act)
327 return 0;
328
329 /* Try to get an ready to draw. */
330 lv_draw_task_t * t = lv_draw_get_available_task(layer, NULL, DRAW_UNIT_ID_PXP);
331
332 if(t == NULL || t->preferred_draw_unit_id != DRAW_UNIT_ID_PXP)
333 return LV_DRAW_UNIT_IDLE;
334
335 if(lv_draw_layer_alloc_buf(layer) == NULL)
336 return LV_DRAW_UNIT_IDLE;
337
338 t->state = LV_DRAW_TASK_STATE_IN_PROGRESS;
339 draw_pxp_unit->task_act = t;
340
341 #if LV_USE_PXP_DRAW_THREAD
342 /* Let the render thread work. */
343 if(draw_pxp_unit->inited)
344 lv_thread_sync_signal(&draw_pxp_unit->sync);
345 #else
346 _pxp_execute_drawing(draw_pxp_unit);
347
348 draw_pxp_unit->task_act->state = LV_DRAW_TASK_STATE_FINISHED;
349 draw_pxp_unit->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 #endif
354
355 return 1;
356 }
357
358 static int32_t _pxp_delete(lv_draw_unit_t * draw_unit)
359 {
360 #if LV_USE_PXP_DRAW_THREAD
361 lv_draw_pxp_unit_t * draw_pxp_unit = (lv_draw_pxp_unit_t *) draw_unit;
362
363 LV_LOG_INFO("Cancel PXP draw thread.");
364 draw_pxp_unit->exit_status = true;
365
366 if(draw_pxp_unit->inited)
367 lv_thread_sync_signal(&draw_pxp_unit->sync);
368
369 lv_result_t res = lv_thread_delete(&draw_pxp_unit->thread);
370
371 return res;
372 #else
373 LV_UNUSED(draw_unit);
374
375 return 0;
376 #endif
377 }
378
379 static void _pxp_execute_drawing(lv_draw_pxp_unit_t * u)
380 {
381 lv_draw_task_t * t = u->task_act;
382 lv_layer_t * layer = t->target_layer;
383 lv_draw_buf_t * draw_buf = layer->draw_buf;
384
385 lv_area_t draw_area;
386 if(!lv_area_intersect(&draw_area, &t->area, &t->clip_area))
387 return; /*Fully clipped, nothing to do*/
388
389 /* Make area relative to the buffer */
390 lv_area_move(&draw_area, -layer->buf_area.x1, -layer->buf_area.y1);
391
392 /* Invalidate only the drawing area */
393 lv_draw_buf_invalidate_cache(draw_buf, &draw_area);
394
395 #if LV_USE_PARALLEL_DRAW_DEBUG
396 t->draw_unit = &u->base_unit;
397 #endif
398
399 switch(t->type) {
400 case LV_DRAW_TASK_TYPE_FILL:
401 lv_draw_pxp_fill(t);
402 break;
403 case LV_DRAW_TASK_TYPE_IMAGE:
404 lv_draw_pxp_img(t);
405 break;
406 case LV_DRAW_TASK_TYPE_LAYER:
407 lv_draw_pxp_layer(t);
408 break;
409 default:
410 break;
411 }
412
413 #if LV_USE_PARALLEL_DRAW_DEBUG
414 /*Layers manage it for themselves*/
415 if(t->type != LV_DRAW_TASK_TYPE_LAYER) {
416 lv_area_t draw_area;
417 if(!lv_area_intersect(&draw_area, &t->area, &t->clip_area))
418 return;
419
420 int32_t idx = u->base_unit.idx;
421
422 lv_draw_rect_dsc_t rect_dsc;
423 lv_draw_rect_dsc_init(&rect_dsc);
424 rect_dsc.bg_color = lv_palette_main(idx % LV_PALETTE_LAST);
425 rect_dsc.border_color = rect_dsc.bg_color;
426 rect_dsc.bg_opa = LV_OPA_10;
427 rect_dsc.border_opa = LV_OPA_80;
428 rect_dsc.border_width = 1;
429 lv_draw_sw_fill((lv_draw_unit_t *)u, &rect_dsc, &draw_area);
430
431 lv_point_t txt_size;
432 lv_text_get_size_attributes(&txt_size, "W", LV_FONT_DEFAULT, 0, 0, 100, LV_TEXT_FLAG_NONE);
433
434 lv_area_t txt_area;
435 txt_area.x1 = draw_area.x1;
436 txt_area.y1 = draw_area.y1;
437 txt_area.x2 = draw_area.x1 + txt_size.x - 1;
438 txt_area.y2 = draw_area.y1 + txt_size.y - 1;
439
440 lv_draw_rect_dsc_init(&rect_dsc);
441 rect_dsc.bg_color = lv_color_white();
442 lv_draw_sw_fill((lv_draw_unit_t *)u, &rect_dsc, &txt_area);
443
444 char buf[8];
445 lv_snprintf(buf, sizeof(buf), "%d", idx);
446 lv_draw_label_dsc_t label_dsc;
447 lv_draw_label_dsc_init(&label_dsc);
448 label_dsc.color = lv_color_black();
449 label_dsc.text = buf;
450 lv_draw_sw_label((lv_draw_unit_t *)u, &label_dsc, &txt_area);
451 }
452 #endif
453 }
454
455 #if LV_USE_PXP_DRAW_THREAD
456 static void _pxp_render_thread_cb(void * ptr)
457 {
458 lv_draw_pxp_unit_t * u = ptr;
459
460 lv_thread_sync_init(&u->sync);
461 u->inited = true;
462
463 while(1) {
464 /* Wait for sync if there is no task set. */
465 while(u->task_act == NULL) {
466 if(u->exit_status)
467 break;
468
469 lv_thread_sync_wait(&u->sync);
470 }
471
472 if(u->exit_status) {
473 LV_LOG_INFO("Ready to exit PXP draw thread.");
474 break;
475 }
476
477 _pxp_execute_drawing(u);
478
479 /* Signal the ready state to dispatcher. */
480 u->task_act->state = LV_DRAW_TASK_STATE_FINISHED;
481
482 /* Cleanup. */
483 u->task_act = NULL;
484
485 /* The draw unit is free now. Request a new dispatching as it can get a new task. */
486 lv_draw_dispatch_request();
487 }
488
489 u->inited = false;
490 lv_thread_sync_delete(&u->sync);
491 LV_LOG_INFO("Exit PXP draw thread.");
492 }
493 #endif
494 #endif /*LV_USE_DRAW_PXP*/
495 #endif /*LV_USE_DRAW_PXP || LV_USE_ROTATE_PXP*/
496 #endif /*LV_USE_PXP*/