1 /**
2  * @file lv_draw_vglite_triangle.c
3  *
4  */
5 
6 /**
7  * Copyright 2023-2024 NXP
8  *
9  * SPDX-License-Identifier: MIT
10  */
11 
12 /*********************
13  *      INCLUDES
14  *********************/
15 
16 #include "lv_draw_vglite.h"
17 
18 #if LV_USE_DRAW_VGLITE
19 #include "lv_vglite_buf.h"
20 #include "lv_vglite_path.h"
21 #include "lv_vglite_utils.h"
22 
23 #include "../../../stdlib/lv_string.h"
24 
25 /*********************
26  *      DEFINES
27  *********************/
28 
29 /**********************
30  *      TYPEDEFS
31  **********************/
32 
33 /**********************
34  *  STATIC PROTOTYPES
35  **********************/
36 
37 /**
38  * Draw triangle shape with effects (opacity, gradient)
39  *
40  * @param[in] coords Coordinates of the triangle (relative to dest buff)
41  * @param[in] clip_area Clipping area with relative coordinates to dest buff
42  * @param[in] dsc Description of the triangle
43  *
44  */
45 static void _vglite_draw_triangle(const lv_area_t * coords, const lv_area_t * clip_area,
46                                   const lv_draw_triangle_dsc_t * dsc);
47 
48 /**********************
49  *  STATIC VARIABLES
50  **********************/
51 
52 /**********************
53  *      MACROS
54  **********************/
55 
56 /**********************
57  *   GLOBAL FUNCTIONS
58  **********************/
59 
lv_draw_vglite_triangle(lv_draw_unit_t * draw_unit,const lv_draw_triangle_dsc_t * dsc)60 void lv_draw_vglite_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc)
61 {
62     if(dsc->bg_opa <= (lv_opa_t)LV_OPA_MIN)
63         return;
64 
65     lv_layer_t * layer = draw_unit->target_layer;
66     lv_area_t clip_area;
67     lv_area_copy(&clip_area, draw_unit->clip_area);
68     lv_area_move(&clip_area, -layer->buf_area.x1, -layer->buf_area.y1);
69 
70     lv_area_t coords;
71     coords.x1 = (int32_t)LV_MIN3(dsc->p[0].x, dsc->p[1].x, dsc->p[2].x);
72     coords.y1 = (int32_t)LV_MIN3(dsc->p[0].y, dsc->p[1].y, dsc->p[2].y);
73     coords.x2 = (int32_t)LV_MAX3(dsc->p[0].x, dsc->p[1].x, dsc->p[2].x);
74     coords.y2 = (int32_t)LV_MAX3(dsc->p[0].y, dsc->p[1].y, dsc->p[2].y);
75 
76     lv_area_move(&coords, -layer->buf_area.x1, -layer->buf_area.y1);
77 
78     lv_area_t clipped_coords;
79     if(!lv_area_intersect(&clipped_coords, &coords, &clip_area))
80         return; /* Fully clipped, nothing to do */
81 
82     _vglite_draw_triangle(&coords, &clip_area, dsc);
83 }
84 
85 /**********************
86  *   STATIC FUNCTIONS
87  **********************/
88 
_vglite_draw_triangle(const lv_area_t * coords,const lv_area_t * clip_area,const lv_draw_triangle_dsc_t * dsc)89 static void _vglite_draw_triangle(const lv_area_t * coords, const lv_area_t * clip_area,
90                                   const lv_draw_triangle_dsc_t * dsc)
91 {
92     vg_lite_buffer_t * vgbuf = vglite_get_dest_buf();
93 
94     lv_area_t tri_area;
95     tri_area.x1 = (int32_t)LV_MIN3(dsc->p[0].x, dsc->p[1].x, dsc->p[2].x);
96     tri_area.y1 = (int32_t)LV_MIN3(dsc->p[0].y, dsc->p[1].y, dsc->p[2].y);
97     tri_area.x2 = (int32_t)LV_MAX3(dsc->p[0].x, dsc->p[1].x, dsc->p[2].x);
98     tri_area.y2 = (int32_t)LV_MAX3(dsc->p[0].y, dsc->p[1].y, dsc->p[2].y);
99 
100     uint32_t width = lv_area_get_width(&tri_area);
101     uint32_t height = lv_area_get_height(&tri_area);
102 
103     /* Init path */
104     int32_t triangle_path[] = { /*VG line path*/
105         VLC_OP_MOVE, dsc->p[0].x, dsc->p[0].y,
106         VLC_OP_LINE, dsc->p[1].x, dsc->p[1].y,
107         VLC_OP_LINE, dsc->p[2].x, dsc->p[2].y,
108         VLC_OP_LINE, dsc->p[0].x, dsc->p[0].y,
109         VLC_OP_END
110     };
111 
112     vg_lite_path_t path;
113     VGLITE_CHECK_ERROR(vg_lite_init_path(&path, VG_LITE_S32, VG_LITE_HIGH, sizeof(triangle_path), triangle_path,
114                                          (vg_lite_float_t)clip_area->x1, (vg_lite_float_t)clip_area->y1,
115                                          ((vg_lite_float_t)clip_area->x2) + 1.0f, ((vg_lite_float_t)clip_area->y2) + 1.0f));
116 
117     /* Init Color */
118     lv_color32_t col32 = lv_color_to_32(dsc->bg_color, dsc->bg_opa);
119     vg_lite_color_t vgcol = vglite_get_color(col32, false);
120 
121     vg_lite_linear_gradient_t gradient;
122     bool has_gradient = (dsc->bg_grad.dir != (lv_grad_dir_t)LV_GRAD_DIR_NONE);
123 
124     /* Init Gradient*/
125     if(has_gradient) {
126         vg_lite_matrix_t * grad_matrix;
127 
128         vg_lite_uint32_t colors[LV_GRADIENT_MAX_STOPS];
129         vg_lite_uint32_t stops[LV_GRADIENT_MAX_STOPS];
130         lv_color32_t col32[LV_GRADIENT_MAX_STOPS];
131 
132         /* Gradient Setup */
133         vg_lite_uint32_t cnt = LV_MAX(dsc->bg_grad.stops_count, LV_GRADIENT_MAX_STOPS);
134         lv_opa_t bg_opa;
135 
136         for(uint8_t i = 0; i < cnt; i++) {
137             stops[i] = dsc->bg_grad.stops[i].frac;
138             bg_opa = LV_OPA_MIX2(dsc->bg_grad.stops[i].opa, dsc->bg_opa);
139 
140             col32[i] = lv_color_to_32(dsc->bg_grad.stops[i].color, bg_opa);
141             colors[i] = vglite_get_color(col32[i], true);
142         }
143 
144         lv_memzero(&gradient, sizeof(vg_lite_linear_gradient_t));
145 
146         VGLITE_CHECK_ERROR(vg_lite_init_grad(&gradient));
147 
148         VGLITE_CHECK_ERROR(vg_lite_set_grad(&gradient, cnt, colors, stops));
149 
150         VGLITE_CHECK_ERROR(vg_lite_update_grad(&gradient));
151 
152         grad_matrix = vg_lite_get_grad_matrix(&gradient);
153         vg_lite_identity(grad_matrix);
154         vg_lite_translate((float)coords->x1, (float)coords->y1, grad_matrix);
155 
156         if(dsc->bg_grad.dir == (lv_grad_dir_t)LV_GRAD_DIR_VER) {
157             vg_lite_scale(1.0f, (float)height / 256.0f, grad_matrix);
158             vg_lite_rotate(90.0f, grad_matrix);
159         }
160         else { /*LV_GRAD_DIR_HOR*/
161             vg_lite_scale((float)width / 256.0f, 1.0f, grad_matrix);
162         }
163 
164         VGLITE_CHECK_ERROR(vg_lite_draw_gradient(vgbuf, &path, VG_LITE_FILL_EVEN_ODD, NULL, &gradient,
165                                                  VG_LITE_BLEND_SRC_OVER));
166     }
167     else {
168         VGLITE_CHECK_ERROR(vg_lite_draw(vgbuf, &path, VG_LITE_FILL_EVEN_ODD, NULL, VG_LITE_BLEND_SRC_OVER, vgcol));
169     }
170 
171     vglite_run();
172 
173     VGLITE_CHECK_ERROR(vg_lite_clear_path(&path));
174 
175     if(has_gradient)
176         VGLITE_CHECK_ERROR(vg_lite_clear_grad(&gradient));
177 }
178 
179 #endif /*LV_USE_DRAW_VGLITE*/
180