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_fill(lv_draw_unit_t * draw_unit,const lv_draw_fill_dsc_t * dsc,const lv_area_t * coords)42 void lv_draw_nema_gfx_fill(lv_draw_unit_t * draw_unit, const lv_draw_fill_dsc_t * dsc, const lv_area_t * coords)
43 {
44     if(dsc->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     lv_area_t rel_coords;
50     lv_area_copy(&rel_coords, coords);
51     lv_area_move(&rel_coords, -layer->buf_area.x1, -layer->buf_area.y1);
52 
53     lv_area_t rel_clip_area;
54     lv_area_copy(&rel_clip_area, draw_unit->clip_area);
55     lv_area_move(&rel_clip_area, -layer->buf_area.x1, -layer->buf_area.y1);
56 
57     nema_set_clip(rel_clip_area.x1, rel_clip_area.y1, lv_area_get_width(&rel_clip_area),
58                   lv_area_get_height(&rel_clip_area));
59 
60     lv_area_t clipped_coords;
61     if(!lv_area_intersect(&clipped_coords, &rel_coords, &rel_clip_area))
62         return; /*Fully clipped, nothing to do*/
63 
64     lv_color_format_t dst_cf = layer->draw_buf->header.cf;
65     uint32_t dst_nema_cf = lv_nemagfx_cf_to_nema(dst_cf);
66 
67     nema_bind_dst_tex((uintptr_t)NEMA_VIRT2PHYS(layer->draw_buf->data), lv_area_get_width(&(layer->buf_area)),
68                       lv_area_get_height(&(layer->buf_area)), dst_nema_cf,
69                       lv_area_get_width(&(layer->buf_area))*lv_color_format_get_size(dst_cf));
70 
71     int32_t coords_bg_w = lv_area_get_width(&rel_coords);
72     int32_t coords_bg_h = lv_area_get_height(&rel_coords);
73     int32_t short_side = LV_MIN(coords_bg_w, coords_bg_h);
74     int32_t radius = LV_MIN(dsc->radius, short_side >> 1);
75 
76     if((dsc->grad.dir == (lv_grad_dir_t)LV_GRAD_DIR_NONE)) {
77 
78         lv_color32_t col32 = lv_color_to_32(dsc->color, dsc->opa);
79         uint32_t bg_color = nema_rgba(col32.red, col32.green, col32.blue, col32.alpha);
80 
81         if(col32.alpha < 255U) {
82             nema_set_blend_fill(NEMA_BL_SRC_OVER);
83             bg_color = nema_premultiply_rgba(bg_color);
84         }
85         else {
86             nema_set_blend_fill(NEMA_BL_SRC);
87         }
88 
89         if(radius > 0.f)
90             nema_fill_rounded_rect_aa(rel_coords.x1, rel_coords.y1, coords_bg_w, coords_bg_h, radius, bg_color);
91         else
92             nema_fill_rect(rel_coords.x1, rel_coords.y1, coords_bg_w, coords_bg_h, bg_color);
93     }
94 #if LV_USE_NEMA_VG
95     else {
96 
97         nema_vg_paint_clear(draw_nema_gfx_unit->paint);
98 
99         nema_vg_paint_set_type(draw_nema_gfx_unit->paint, NEMA_VG_PAINT_GRAD_LINEAR);
100         nema_vg_set_blend(NEMA_BL_SRC_OVER | NEMA_BLOP_SRC_PREMULT);
101 
102         float stops[LV_GRADIENT_MAX_STOPS];
103         color_var_t colors[LV_GRADIENT_MAX_STOPS];
104 
105         uint32_t cnt = LV_MAX(dsc->grad.stops_count, LV_GRADIENT_MAX_STOPS);
106 
107         for(uint8_t i = 0; i < cnt; i++) {
108             stops[i] = (float)(dsc->grad.stops[i].frac) / 255.f;
109             colors[i].a = LV_OPA_MIX2(dsc->grad.stops[i].opa, dsc->opa);
110             colors[i].r = dsc->grad.stops[i].color.red;
111             colors[i].g = dsc->grad.stops[i].color.green;
112             colors[i].b = dsc->grad.stops[i].color.blue;
113         }
114 
115         nema_vg_grad_set(draw_nema_gfx_unit->gradient, cnt, stops, colors);
116 
117         float x0, y0, x1, y1;
118 
119         if(dsc->grad.dir == LV_GRAD_DIR_HOR) {
120             x0 = rel_coords.x1;
121             x1 = rel_coords.x2;
122             y0 = rel_coords.y1;
123             y1 = rel_coords.y1;
124         }
125         else {
126             x0 = rel_coords.x1;
127             x1 = rel_coords.x1;
128             y0 = rel_coords.y1;
129             y1 = rel_coords.y2;
130         }
131 
132         nema_vg_paint_set_grad_linear(draw_nema_gfx_unit->paint, draw_nema_gfx_unit->gradient, x0, y0, x1, y1,
133                                       NEMA_TEX_CLAMP | NEMA_FILTER_BL);
134 
135         if(radius > 0.f)
136             nema_vg_draw_rounded_rect(rel_coords.x1, rel_coords.y1, coords_bg_w, coords_bg_h, radius, radius, NULL,
137                                       draw_nema_gfx_unit->paint);
138         else
139             nema_vg_draw_rect(rel_coords.x1, rel_coords.y1, coords_bg_w, coords_bg_h, NULL, draw_nema_gfx_unit->paint);
140     }
141 #endif
142 
143     nema_cl_submit(&(draw_nema_gfx_unit->cl));
144 
145 }
146 #endif
147