1 /**
2  * @file lv_draw_vglite_line.c
3  *
4  */
5 
6 /**
7  * MIT License
8  *
9  * Copyright 2022, 2023 NXP
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights to
14  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
15  * the Software, and to permit persons to whom the Software is furnished to do so,
16  * subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next paragraph)
19  * shall be included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
22  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
26  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  */
29 
30 /*********************
31  *      INCLUDES
32  *********************/
33 
34 #include "lv_draw_vglite_line.h"
35 
36 #if LV_USE_GPU_NXP_VG_LITE
37 #include "lv_vglite_buf.h"
38 #include <math.h>
39 
40 /*********************
41  *      DEFINES
42  *********************/
43 
44 /**********************
45  *      TYPEDEFS
46  **********************/
47 
48 /**********************
49  *  STATIC PROTOTYPES
50  **********************/
51 
52 /**********************
53  *  STATIC VARIABLES
54  **********************/
55 
56 /**********************
57  *      MACROS
58  **********************/
59 
60 /**********************
61  *   GLOBAL FUNCTIONS
62  **********************/
63 
lv_gpu_nxp_vglite_draw_line(const lv_point_t * point1,const lv_point_t * point2,const lv_area_t * clip_area,const lv_draw_line_dsc_t * dsc)64 lv_res_t lv_gpu_nxp_vglite_draw_line(const lv_point_t * point1, const lv_point_t * point2,
65                                      const lv_area_t * clip_area, const lv_draw_line_dsc_t * dsc)
66 {
67     vg_lite_error_t err = VG_LITE_SUCCESS;
68     vg_lite_path_t path;
69     vg_lite_color_t vgcol; /* vglite takes ABGR */
70     vg_lite_buffer_t * vgbuf = lv_vglite_get_dest_buf();
71     vg_lite_cap_style_t cap_style = (dsc->round_start || dsc->round_end) ? VG_LITE_CAP_ROUND : VG_LITE_CAP_BUTT;
72     vg_lite_join_style_t join_style = (dsc->round_start || dsc->round_end) ? VG_LITE_JOIN_ROUND : VG_LITE_JOIN_MITER;
73 
74     bool is_dashed = (dsc->dash_width && dsc->dash_gap);
75 
76     vg_lite_float_t stroke_dash_pattern[2] = {0, 0};
77     uint32_t stroke_dash_count = 0;
78     vg_lite_float_t stroke_dash_phase = 0;
79     if(is_dashed) {
80         stroke_dash_pattern[0] = (vg_lite_float_t)dsc->dash_width;
81         stroke_dash_pattern[1] = (vg_lite_float_t)dsc->dash_gap;
82         stroke_dash_count = sizeof(stroke_dash_pattern) / sizeof(vg_lite_float_t);
83         stroke_dash_phase = (vg_lite_float_t)dsc->dash_width / 2;
84     }
85 
86     /* Choose vglite blend mode based on given lvgl blend mode */
87     vg_lite_blend_t vglite_blend_mode = lv_vglite_get_blend_mode(dsc->blend_mode);
88 
89     /*** Init path ***/
90     lv_coord_t width = dsc->width;
91 
92     int32_t line_path[] = { /*VG line path*/
93         VLC_OP_MOVE, point1->x, point1->y,
94         VLC_OP_LINE, point2->x, point2->y,
95         VLC_OP_END
96     };
97 
98     err = vg_lite_init_path(&path, VG_LITE_S32, VG_LITE_HIGH, sizeof(line_path), line_path,
99                             (vg_lite_float_t)clip_area->x1, (vg_lite_float_t)clip_area->y1,
100                             ((vg_lite_float_t)clip_area->x2) + 1.0f, ((vg_lite_float_t)clip_area->y2) + 1.0f);
101     VG_LITE_ERR_RETURN_INV(err, "Init path failed.");
102 
103     vg_lite_matrix_t matrix;
104     vg_lite_identity(&matrix);
105 
106     lv_color32_t col32 = { .full = lv_color_to32(dsc->color) }; /*Convert color to RGBA8888*/
107     vg_lite_buffer_format_t color_format = LV_COLOR_DEPTH == 16 ? VG_LITE_BGRA8888 : VG_LITE_ABGR8888;
108     if(lv_vglite_premult_and_swizzle(&vgcol, col32, dsc->opa, color_format) != LV_RES_OK)
109         VG_LITE_RETURN_INV("Premultiplication and swizzle failed.");
110 
111     /*** Draw line ***/
112     err = vg_lite_set_draw_path_type(&path, VG_LITE_DRAW_STROKE_PATH);
113     VG_LITE_ERR_RETURN_INV(err, "Set draw path type failed.");
114 
115     err = vg_lite_set_stroke(&path, cap_style, join_style, width, 8, stroke_dash_pattern, stroke_dash_count,
116                              stroke_dash_phase, vgcol);
117     VG_LITE_ERR_RETURN_INV(err, "Set stroke failed.");
118 
119     err = vg_lite_update_stroke(&path);
120     VG_LITE_ERR_RETURN_INV(err, "Update stroke failed.");
121 
122     lv_vglite_set_scissor(clip_area);
123 
124     err = vg_lite_draw(vgbuf, &path, VG_LITE_FILL_NON_ZERO, &matrix, vglite_blend_mode, vgcol);
125     VG_LITE_ERR_RETURN_INV(err, "Draw line failed.");
126 
127     if(lv_vglite_run() != LV_RES_OK)
128         VG_LITE_RETURN_INV("Run failed.");
129 
130     lv_vglite_disable_scissor();
131 
132     err = vg_lite_clear_path(&path);
133     VG_LITE_ERR_RETURN_INV(err, "Clear path failed.");
134 
135     return LV_RES_OK;
136 }
137 
138 /**********************
139  *   STATIC FUNCTIONS
140  **********************/
141 
142 #endif /*LV_USE_GPU_NXP_VG_LITE*/
143