1 /**
2  * MIT License
3  *
4  * -----------------------------------------------------------------------------
5  * Copyright (c) 2008-24 Think Silicon Single Member PC
6  * -----------------------------------------------------------------------------
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights to
11  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12  * the Software, and to permit persons to whom the Software is furnished to do so,
13  * subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next paragraph)
16  * shall be included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
22  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
23  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26 
27 /**
28  * @file lv_draw_nema_gfx_fill.c
29  *
30  */
31 
32 /*********************
33  *      INCLUDES
34  *********************/
35 #include "lv_draw_nema_gfx.h"
36 
37 #if LV_USE_NEMA_GFX
38 
39 /**********************
40  *   GLOBAL FUNCTIONS
41  **********************/
lv_draw_nema_gfx_triangle(lv_draw_unit_t * draw_unit,const lv_draw_triangle_dsc_t * dsc)42 void lv_draw_nema_gfx_triangle(lv_draw_unit_t * draw_unit, const lv_draw_triangle_dsc_t * dsc)
43 {
44     if(dsc->bg_opa <= LV_OPA_MIN) return;
45 
46     lv_draw_nema_gfx_unit_t * draw_nema_gfx_unit = (lv_draw_nema_gfx_unit_t *)draw_unit;
47 
48     lv_layer_t * layer = draw_unit->target_layer;
49 
50     lv_area_t rel_clip_area;
51     lv_area_copy(&rel_clip_area, draw_unit->clip_area);
52     lv_area_move(&rel_clip_area, -layer->buf_area.x1, -layer->buf_area.y1);
53 
54     lv_area_t coords;
55     coords.x1 = (int32_t)LV_MIN3(dsc->p[0].x, dsc->p[1].x, dsc->p[2].x);
56     coords.y1 = (int32_t)LV_MIN3(dsc->p[0].y, dsc->p[1].y, dsc->p[2].y);
57     coords.x2 = (int32_t)LV_MAX3(dsc->p[0].x, dsc->p[1].x, dsc->p[2].x);
58     coords.y2 = (int32_t)LV_MAX3(dsc->p[0].y, dsc->p[1].y, dsc->p[2].y);
59 
60     lv_area_move(&coords, -layer->buf_area.x1, -layer->buf_area.y1);
61 
62     lv_area_t clipped_coords;
63     if(!lv_area_intersect(&clipped_coords, &coords, &rel_clip_area))
64         return; /* Fully clipped, nothing to do */
65 
66     nema_set_clip(rel_clip_area.x1, rel_clip_area.y1, lv_area_get_width(&rel_clip_area),
67                   lv_area_get_height(&rel_clip_area));
68 
69     lv_color_format_t dst_cf = layer->draw_buf->header.cf;
70     uint32_t dst_nema_cf = lv_nemagfx_cf_to_nema(dst_cf);
71 
72     nema_bind_dst_tex((uintptr_t)NEMA_VIRT2PHYS(layer->draw_buf->data), lv_area_get_width(&(layer->buf_area)),
73                       lv_area_get_height(&(layer->buf_area)), dst_nema_cf,
74                       lv_area_get_width(&(layer->buf_area))*lv_color_format_get_size(dst_cf));
75 
76     if(dsc->bg_grad.dir == (lv_grad_dir_t)LV_GRAD_DIR_NONE) {
77 
78         lv_color32_t col32 = lv_color_to_32(dsc->bg_color, dsc->bg_opa);
79 
80         if(col32.alpha < 255U) {
81             nema_set_blend_fill(NEMA_BL_SIMPLE);
82         }
83         else {
84             nema_set_blend_fill(NEMA_BL_SRC);
85         }
86 
87         uint32_t bg_color = nema_rgba(col32.red, col32.green, col32.blue, col32.alpha);
88 
89         nema_enable_aa(1, 1, 1, 0);
90         nema_fill_triangle(dsc->p[0].x, dsc->p[0].y, dsc->p[1].x, dsc->p[1].y, dsc->p[2].x, dsc->p[2].y, bg_color);
91     }
92 #if LV_USE_NEMA_VG
93     else {
94 
95         nema_vg_path_clear(draw_nema_gfx_unit->path);
96         nema_vg_paint_clear(draw_nema_gfx_unit->paint);
97 
98         nema_vg_paint_set_type(draw_nema_gfx_unit->paint, NEMA_VG_PAINT_GRAD_LINEAR);
99         nema_vg_set_blend(NEMA_BL_SRC_OVER | NEMA_BLOP_SRC_PREMULT);
100         nema_vg_set_fill_rule(NEMA_VG_FILL_EVEN_ODD);
101 
102 
103         float stops[LV_GRADIENT_MAX_STOPS];
104         color_var_t colors[LV_GRADIENT_MAX_STOPS];
105 
106         uint32_t cnt = LV_MAX(dsc->bg_grad.stops_count, LV_GRADIENT_MAX_STOPS);
107 
108         for(uint8_t i = 0; i < cnt; i++) {
109             stops[i] = (float)(dsc->bg_grad.stops[i].frac) / 255.f;
110             colors[i].a = dsc->bg_grad.stops[i].opa;
111             colors[i].r = dsc->bg_grad.stops[i].color.red;
112             colors[i].g = dsc->bg_grad.stops[i].color.green;
113             colors[i].b = dsc->bg_grad.stops[i].color.blue;
114         }
115 
116         nema_vg_grad_set(draw_nema_gfx_unit->gradient, cnt, stops, colors);
117 
118         //Calculate Bounding Box
119         float min_x = LV_MIN(dsc->p[0].x, dsc->p[1].x);
120         min_x = LV_MIN(dsc->p[2].x, min_x);
121 
122         float min_y = LV_MIN(dsc->p[0].y, dsc->p[1].y);
123         min_y = LV_MIN(dsc->p[2].y, min_y);
124 
125         float max_x = LV_MAX(dsc->p[0].x, dsc->p[1].x);
126         max_x = LV_MAX(dsc->p[2].x, max_x);
127 
128         float max_y = LV_MAX(dsc->p[0].y, dsc->p[1].y);
129         max_y = LV_MAX(dsc->p[2].y, max_y);
130 
131         float x0, x1, y0, y1;
132 
133         if(dsc->bg_grad.dir == LV_GRAD_DIR_HOR) {
134             x0 = min_x;
135             x1 = max_x;
136             y0 = min_y;
137             y1 = min_y;
138         }
139         else {
140             x0 = min_x;
141             x1 = min_x;
142             y0 = min_y;
143             y1 = max_y;
144         }
145 
146         y0 -= (float) layer->buf_area.y1;
147         y1 -= (float) layer->buf_area.y1;
148         x0 -= (float) layer->buf_area.x1;
149         x1 -= (float) layer->buf_area.x1;
150 
151         uint8_t cmds_polygon[] = {NEMA_VG_PRIM_MOVE, NEMA_VG_PRIM_POLYGON};
152         float triangle_coords[] = {dsc->p[0].x - layer->buf_area.x1, dsc->p[0].y - layer->buf_area.y1,
153                                    4.0f,
154                                    dsc->p[1].x - layer->buf_area.x1, dsc->p[1].y - layer->buf_area.y1,
155                                    dsc->p[2].x - layer->buf_area.x1, dsc->p[2].y - layer->buf_area.y1
156                                   };
157         nema_enable_aa(0, 0, 0, 0);
158         nema_vg_paint_set_grad_linear(draw_nema_gfx_unit->paint, draw_nema_gfx_unit->gradient, x0, y0, x1, y1,
159                                       NEMA_TEX_CLAMP | NEMA_FILTER_BL);
160         nema_vg_path_set_shape(draw_nema_gfx_unit->path, 2, cmds_polygon, 7, triangle_coords);
161         nema_vg_draw_path(draw_nema_gfx_unit->path, draw_nema_gfx_unit->paint);
162     }
163 #endif
164 
165     nema_cl_submit(&(draw_nema_gfx_unit->cl));
166 
167 }
168 #endif
169