1 /**
2  * @file lv_canvas.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_canvas.h"
10 #include "../misc/lv_assert.h"
11 #include "../misc/lv_math.h"
12 #include "../draw/lv_draw.h"
13 #include "../core/lv_refr.h"
14 
15 #if LV_USE_CANVAS != 0
16 
17 #include "../draw/sw/lv_draw_sw.h"
18 
19 /*********************
20  *      DEFINES
21  *********************/
22 #define MY_CLASS &lv_canvas_class
23 
24 /**********************
25  *      TYPEDEFS
26  **********************/
27 
28 /**********************
29  *  STATIC PROTOTYPES
30  **********************/
31 static void lv_canvas_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
32 static void lv_canvas_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
33 static void init_fake_disp(lv_obj_t * canvas, lv_disp_t * disp, lv_disp_drv_t * drv, lv_area_t * clip_area);
34 static void deinit_fake_disp(lv_obj_t * canvas, lv_disp_t * disp);
35 
36 /**********************
37  *  STATIC VARIABLES
38  **********************/
39 const lv_obj_class_t lv_canvas_class = {
40     .constructor_cb = lv_canvas_constructor,
41     .destructor_cb = lv_canvas_destructor,
42     .instance_size = sizeof(lv_canvas_t),
43     .base_class = &lv_img_class
44 };
45 
46 /**********************
47  *      MACROS
48  **********************/
49 
50 /**********************
51  *   GLOBAL FUNCTIONS
52  **********************/
53 
lv_canvas_create(lv_obj_t * parent)54 lv_obj_t * lv_canvas_create(lv_obj_t * parent)
55 {
56     LV_LOG_INFO("begin");
57     lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
58     lv_obj_class_init_obj(obj);
59     return obj;
60 }
61 
62 /*=====================
63  * Setter functions
64  *====================*/
65 
lv_canvas_set_buffer(lv_obj_t * obj,void * buf,lv_coord_t w,lv_coord_t h,lv_img_cf_t cf)66 void lv_canvas_set_buffer(lv_obj_t * obj, void * buf, lv_coord_t w, lv_coord_t h, lv_img_cf_t cf)
67 {
68     LV_ASSERT_OBJ(obj, MY_CLASS);
69     LV_ASSERT_NULL(buf);
70 
71     lv_canvas_t * canvas = (lv_canvas_t *)obj;
72 
73     canvas->dsc.header.cf = cf;
74     canvas->dsc.header.w  = w;
75     canvas->dsc.header.h  = h;
76     canvas->dsc.data      = buf;
77 
78     lv_img_set_src(obj, &canvas->dsc);
79     lv_img_cache_invalidate_src(&canvas->dsc);
80 }
81 
lv_canvas_set_px_color(lv_obj_t * obj,lv_coord_t x,lv_coord_t y,lv_color_t c)82 void lv_canvas_set_px_color(lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_color_t c)
83 {
84     LV_ASSERT_OBJ(obj, MY_CLASS);
85 
86     lv_canvas_t * canvas = (lv_canvas_t *)obj;
87 
88     lv_img_buf_set_px_color(&canvas->dsc, x, y, c);
89     lv_obj_invalidate(obj);
90 }
91 
lv_canvas_set_px_opa(lv_obj_t * obj,lv_coord_t x,lv_coord_t y,lv_opa_t opa)92 void lv_canvas_set_px_opa(lv_obj_t * obj, lv_coord_t x, lv_coord_t y, lv_opa_t opa)
93 {
94     LV_ASSERT_OBJ(obj, MY_CLASS);
95 
96     lv_canvas_t * canvas = (lv_canvas_t *)obj;
97 
98     lv_img_buf_set_px_alpha(&canvas->dsc, x, y, opa);
99     lv_obj_invalidate(obj);
100 }
101 
lv_canvas_set_palette(lv_obj_t * obj,uint8_t id,lv_color_t c)102 void lv_canvas_set_palette(lv_obj_t * obj, uint8_t id, lv_color_t c)
103 {
104     LV_ASSERT_OBJ(obj, MY_CLASS);
105 
106     lv_canvas_t * canvas = (lv_canvas_t *)obj;
107 
108     lv_img_buf_set_palette(&canvas->dsc, id, c);
109     lv_obj_invalidate(obj);
110 }
111 
112 /*=====================
113  * Getter functions
114  *====================*/
115 
lv_canvas_get_px(lv_obj_t * obj,lv_coord_t x,lv_coord_t y)116 lv_color_t lv_canvas_get_px(lv_obj_t * obj, lv_coord_t x, lv_coord_t y)
117 {
118     LV_ASSERT_OBJ(obj, MY_CLASS);
119 
120     lv_canvas_t * canvas = (lv_canvas_t *)obj;
121     lv_color_t color = lv_obj_get_style_img_recolor(obj, LV_PART_MAIN);
122 
123     return lv_img_buf_get_px_color(&canvas->dsc, x, y, color);
124 }
125 
lv_canvas_get_img(lv_obj_t * obj)126 lv_img_dsc_t * lv_canvas_get_img(lv_obj_t * obj)
127 {
128     LV_ASSERT_OBJ(obj, MY_CLASS);
129 
130     lv_canvas_t * canvas = (lv_canvas_t *)obj;
131     return &canvas->dsc;
132 }
133 
134 /*=====================
135  * Other functions
136  *====================*/
137 
lv_canvas_copy_buf(lv_obj_t * obj,const void * to_copy,lv_coord_t x,lv_coord_t y,lv_coord_t w,lv_coord_t h)138 void lv_canvas_copy_buf(lv_obj_t * obj, const void * to_copy, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv_coord_t h)
139 {
140     LV_ASSERT_OBJ(obj, MY_CLASS);
141     LV_ASSERT_NULL(to_copy);
142 
143     lv_canvas_t * canvas = (lv_canvas_t *)obj;
144 
145     if(x + w - 1 >= (lv_coord_t)canvas->dsc.header.w || y + h - 1 >= (lv_coord_t)canvas->dsc.header.h) {
146         LV_LOG_WARN("lv_canvas_copy_buf: x or y out of the canvas");
147         return;
148     }
149 
150     uint32_t px_size   = lv_img_cf_get_px_size(canvas->dsc.header.cf) >> 3;
151     uint32_t px        = canvas->dsc.header.w * y * px_size + x * px_size;
152     uint8_t * to_copy8 = (uint8_t *)to_copy;
153     lv_coord_t i;
154     for(i = 0; i < h; i++) {
155         lv_memcpy((void *)&canvas->dsc.data[px], to_copy8, w * px_size);
156         px += canvas->dsc.header.w * px_size;
157         to_copy8 += w * px_size;
158     }
159 }
160 
lv_canvas_transform(lv_obj_t * obj,lv_img_dsc_t * src_img,int16_t angle,uint16_t zoom,lv_coord_t offset_x,lv_coord_t offset_y,int32_t pivot_x,int32_t pivot_y,bool antialias)161 void lv_canvas_transform(lv_obj_t * obj, lv_img_dsc_t * src_img, int16_t angle, uint16_t zoom, lv_coord_t offset_x,
162                          lv_coord_t offset_y,
163                          int32_t pivot_x, int32_t pivot_y, bool antialias)
164 {
165 #if LV_DRAW_COMPLEX
166     LV_ASSERT_OBJ(obj, MY_CLASS);
167     LV_ASSERT_NULL(src_img);
168 
169     lv_canvas_t * canvas = (lv_canvas_t *)obj;
170     lv_img_dsc_t * dest_img = &canvas->dsc;
171 
172     int32_t x;
173     int32_t y;
174 
175     lv_draw_img_dsc_t draw_dsc;
176     lv_draw_img_dsc_init(&draw_dsc);
177     draw_dsc.angle = angle;
178     draw_dsc.zoom = zoom;
179     draw_dsc.pivot.x = pivot_x;
180     draw_dsc.pivot.y = pivot_y;
181     draw_dsc.antialias = antialias;
182 
183     lv_area_t dest_area;
184     dest_area.x1 = -offset_x;
185     dest_area.x2 = dest_area.x1 + dest_img->header.w - 1;
186     dest_area.y1 = -offset_y;
187     dest_area.y2 = -offset_y;
188 
189     lv_color_t * cbuf = lv_mem_alloc(dest_img->header.w * sizeof(lv_color_t));
190     lv_opa_t * abuf = lv_mem_alloc(dest_img->header.w * sizeof(lv_opa_t));
191     for(y = 0; y < dest_img->header.h; y++) {
192         if(y + offset_y >= 0) {
193             lv_draw_sw_transform(NULL, &dest_area, src_img->data, src_img->header.w, src_img->header.h, src_img->header.w,
194                                  &draw_dsc, canvas->dsc.header.cf, cbuf, abuf);
195 
196             for(x = 0; x < dest_img->header.w; x++) {
197                 if(abuf[x]) {
198                     lv_img_buf_set_px_color(dest_img, x, y, cbuf[x]);
199                     lv_img_buf_set_px_alpha(dest_img, x, y, abuf[x]);
200                 }
201             }
202 
203             dest_area.y1++;
204             dest_area.y2++;
205         }
206     }
207     lv_mem_free(cbuf);
208     lv_mem_free(abuf);
209 
210     lv_obj_invalidate(obj);
211 
212 #else
213     LV_UNUSED(obj);
214     LV_UNUSED(src_img);
215     LV_UNUSED(angle);
216     LV_UNUSED(zoom);
217     LV_UNUSED(offset_x);
218     LV_UNUSED(offset_y);
219     LV_UNUSED(pivot_x);
220     LV_UNUSED(pivot_y);
221     LV_UNUSED(antialias);
222     LV_LOG_WARN("Can't transform canvas with LV_DRAW_COMPLEX == 0");
223 #endif
224 }
225 
lv_canvas_blur_hor(lv_obj_t * obj,const lv_area_t * area,uint16_t r)226 void lv_canvas_blur_hor(lv_obj_t * obj, const lv_area_t * area, uint16_t r)
227 {
228     LV_ASSERT_OBJ(obj, MY_CLASS);
229 
230     if(r == 0) return;
231 
232     lv_canvas_t * canvas = (lv_canvas_t *)obj;
233 
234     lv_area_t a;
235     if(area) {
236         lv_area_copy(&a, area);
237         if(a.x1 < 0) a.x1 = 0;
238         if(a.y1 < 0) a.y1 = 0;
239         if(a.x2 > canvas->dsc.header.w - 1) a.x2 = canvas->dsc.header.w - 1;
240         if(a.y2 > canvas->dsc.header.h - 1) a.y2 = canvas->dsc.header.h - 1;
241     }
242     else {
243         a.x1 = 0;
244         a.y1 = 0;
245         a.x2 = canvas->dsc.header.w - 1;
246         a.y2 = canvas->dsc.header.h - 1;
247     }
248 
249     lv_color_t color = lv_obj_get_style_img_recolor(obj, LV_PART_MAIN);
250 
251     uint16_t r_back = r / 2;
252     uint16_t r_front = r / 2;
253 
254     if((r & 0x1) == 0) r_back--;
255 
256     bool has_alpha = lv_img_cf_has_alpha(canvas->dsc.header.cf);
257 
258     lv_coord_t line_w = lv_img_buf_get_img_size(canvas->dsc.header.w, 1, canvas->dsc.header.cf);
259     uint8_t * line_buf = lv_mem_buf_get(line_w);
260 
261     lv_img_dsc_t line_img;
262     line_img.data = line_buf;
263     line_img.header.always_zero = 0;
264     line_img.header.w = canvas->dsc.header.w;
265     line_img.header.h = 1;
266     line_img.header.cf = canvas->dsc.header.cf;
267 
268     lv_coord_t x;
269     lv_coord_t y;
270     lv_coord_t x_safe;
271 
272     for(y = a.y1; y <= a.y2; y++) {
273         uint32_t asum = 0;
274         uint32_t rsum = 0;
275         uint32_t gsum = 0;
276         uint32_t bsum = 0;
277 
278         lv_color_t c;
279         lv_opa_t opa = LV_OPA_TRANSP;
280         lv_memcpy(line_buf, &canvas->dsc.data[y * line_w], line_w);
281 
282         for(x = a.x1 - r_back; x <= a.x1 + r_front; x++) {
283             x_safe = x < 0 ? 0 : x;
284             x_safe = x_safe > canvas->dsc.header.w - 1 ? canvas->dsc.header.w - 1 : x_safe;
285 
286             c = lv_img_buf_get_px_color(&line_img, x_safe, 0, color);
287             if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, x_safe, 0);
288 
289             rsum += c.ch.red;
290 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
291             gsum += (c.ch.green_h << 3) + c.ch.green_l;
292 #else
293             gsum += c.ch.green;
294 #endif
295             bsum += c.ch.blue;
296             if(has_alpha) asum += opa;
297         }
298 
299         /*Just to indicate that the px is visible*/
300         if(has_alpha == false) asum = LV_OPA_COVER;
301 
302         for(x = a.x1; x <= a.x2; x++) {
303 
304             if(asum) {
305                 c.ch.red = rsum / r;
306 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
307                 uint8_t gtmp = gsum / r;
308                 c.ch.green_h = gtmp >> 3;
309                 c.ch.green_l = gtmp & 0x7;
310 #else
311                 c.ch.green = gsum / r;
312 #endif
313                 c.ch.blue = bsum / r;
314                 if(has_alpha) opa = asum / r;
315 
316                 lv_img_buf_set_px_color(&canvas->dsc, x, y, c);
317             }
318             if(has_alpha) lv_img_buf_set_px_alpha(&canvas->dsc, x, y, opa);
319 
320             x_safe = x - r_back;
321             x_safe = x_safe < 0 ? 0 : x_safe;
322             c = lv_img_buf_get_px_color(&line_img, x_safe, 0, color);
323             if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, x_safe, 0);
324 
325             rsum -= c.ch.red;
326 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
327             gsum -= (c.ch.green_h << 3) + c.ch.green_l;
328 #else
329             gsum -= c.ch.green;
330 #endif
331             bsum -= c.ch.blue;
332             if(has_alpha) asum -= opa;
333 
334             x_safe = x + 1 + r_front;
335             x_safe = x_safe > canvas->dsc.header.w - 1 ? canvas->dsc.header.w - 1 : x_safe;
336             c = lv_img_buf_get_px_color(&line_img, x_safe, 0, lv_color_white());
337             if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, x_safe, 0);
338 
339             rsum += c.ch.red;
340 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
341             gsum += (c.ch.green_h << 3) + c.ch.green_l;
342 #else
343             gsum += c.ch.green;
344 #endif
345             bsum += c.ch.blue;
346             if(has_alpha) asum += opa;
347         }
348     }
349     lv_obj_invalidate(obj);
350 
351     lv_mem_buf_release(line_buf);
352 }
353 
lv_canvas_blur_ver(lv_obj_t * obj,const lv_area_t * area,uint16_t r)354 void lv_canvas_blur_ver(lv_obj_t * obj, const lv_area_t * area, uint16_t r)
355 {
356     LV_ASSERT_OBJ(obj, MY_CLASS);
357 
358     if(r == 0) return;
359 
360     lv_canvas_t * canvas = (lv_canvas_t *)obj;
361 
362     lv_area_t a;
363     if(area) {
364         lv_area_copy(&a, area);
365         if(a.x1 < 0) a.x1 = 0;
366         if(a.y1 < 0) a.y1 = 0;
367         if(a.x2 > canvas->dsc.header.w - 1) a.x2 = canvas->dsc.header.w - 1;
368         if(a.y2 > canvas->dsc.header.h - 1) a.y2 = canvas->dsc.header.h - 1;
369     }
370     else {
371         a.x1 = 0;
372         a.y1 = 0;
373         a.x2 = canvas->dsc.header.w - 1;
374         a.y2 = canvas->dsc.header.h - 1;
375     }
376 
377     lv_color_t color = lv_obj_get_style_img_recolor(obj, LV_PART_MAIN);
378 
379     uint16_t r_back = r / 2;
380     uint16_t r_front = r / 2;
381 
382     if((r & 0x1) == 0) r_back--;
383 
384     bool has_alpha = lv_img_cf_has_alpha(canvas->dsc.header.cf);
385     lv_coord_t col_w = lv_img_buf_get_img_size(1, canvas->dsc.header.h, canvas->dsc.header.cf);
386     uint8_t * col_buf = lv_mem_buf_get(col_w);
387     lv_img_dsc_t line_img;
388 
389     line_img.data = col_buf;
390     line_img.header.always_zero = 0;
391     line_img.header.w = 1;
392     line_img.header.h = canvas->dsc.header.h;
393     line_img.header.cf = canvas->dsc.header.cf;
394 
395     lv_coord_t x;
396     lv_coord_t y;
397     lv_coord_t y_safe;
398 
399     for(x = a.x1; x <= a.x2; x++) {
400         uint32_t asum = 0;
401         uint32_t rsum = 0;
402         uint32_t gsum = 0;
403         uint32_t bsum = 0;
404 
405         lv_color_t c;
406         lv_opa_t opa = LV_OPA_COVER;
407 
408         for(y = a.y1 - r_back; y <= a.y1 + r_front; y++) {
409             y_safe = y < 0 ? 0 : y;
410             y_safe = y_safe > canvas->dsc.header.h - 1 ? canvas->dsc.header.h - 1 : y_safe;
411 
412             c = lv_img_buf_get_px_color(&canvas->dsc, x, y_safe, color);
413             if(has_alpha) opa = lv_img_buf_get_px_alpha(&canvas->dsc, x, y_safe);
414 
415             lv_img_buf_set_px_color(&line_img, 0, y_safe, c);
416             if(has_alpha) lv_img_buf_set_px_alpha(&line_img, 0, y_safe, opa);
417 
418             rsum += c.ch.red;
419 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
420             gsum += (c.ch.green_h << 3) + c.ch.green_l;
421 #else
422             gsum += c.ch.green;
423 #endif
424             bsum += c.ch.blue;
425             if(has_alpha) asum += opa;
426         }
427 
428         /*Just to indicate that the px is visible*/
429         if(has_alpha == false) asum = LV_OPA_COVER;
430 
431         for(y = a.y1; y <= a.y2; y++) {
432             if(asum) {
433                 c.ch.red = rsum / r;
434 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
435                 uint8_t gtmp = gsum / r;
436                 c.ch.green_h = gtmp >> 3;
437                 c.ch.green_l = gtmp & 0x7;
438 #else
439                 c.ch.green = gsum / r;
440 #endif
441                 c.ch.blue = bsum / r;
442                 if(has_alpha) opa = asum / r;
443 
444                 lv_img_buf_set_px_color(&canvas->dsc, x, y, c);
445             }
446             if(has_alpha) lv_img_buf_set_px_alpha(&canvas->dsc, x, y, opa);
447 
448             y_safe = y - r_back;
449             y_safe = y_safe < 0 ? 0 : y_safe;
450             c = lv_img_buf_get_px_color(&line_img, 0, y_safe, color);
451             if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, 0, y_safe);
452 
453             rsum -= c.ch.red;
454 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
455             gsum -= (c.ch.green_h << 3) + c.ch.green_l;
456 #else
457             gsum -= c.ch.green;
458 #endif
459             bsum -= c.ch.blue;
460             if(has_alpha) asum -= opa;
461 
462             y_safe = y + 1 + r_front;
463             y_safe = y_safe > canvas->dsc.header.h - 1 ? canvas->dsc.header.h - 1 : y_safe;
464 
465             c = lv_img_buf_get_px_color(&canvas->dsc, x, y_safe, color);
466             if(has_alpha) opa = lv_img_buf_get_px_alpha(&canvas->dsc, x, y_safe);
467 
468             lv_img_buf_set_px_color(&line_img, 0, y_safe, c);
469             if(has_alpha) lv_img_buf_set_px_alpha(&line_img, 0, y_safe, opa);
470 
471             rsum += c.ch.red;
472 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
473             gsum += (c.ch.green_h << 3) + c.ch.green_l;
474 #else
475             gsum += c.ch.green;
476 #endif
477             bsum += c.ch.blue;
478             if(has_alpha) asum += opa;
479         }
480     }
481 
482     lv_obj_invalidate(obj);
483 
484     lv_mem_buf_release(col_buf);
485 }
486 
lv_canvas_fill_bg(lv_obj_t * canvas,lv_color_t color,lv_opa_t opa)487 void lv_canvas_fill_bg(lv_obj_t * canvas, lv_color_t color, lv_opa_t opa)
488 {
489     LV_ASSERT_OBJ(canvas, MY_CLASS);
490 
491     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
492 
493     if(dsc->header.cf == LV_IMG_CF_INDEXED_1BIT) {
494         uint32_t row_byte_cnt = (dsc->header.w + 7) >> 3;
495         /*+8 skip the palette*/
496         lv_memset((uint8_t *)dsc->data + 8, color.full ? 0xff : 0x00, row_byte_cnt * dsc->header.h);
497     }
498     else if(dsc->header.cf == LV_IMG_CF_ALPHA_1BIT) {
499         uint32_t row_byte_cnt = (dsc->header.w + 7) >> 3;
500         lv_memset((uint8_t *)dsc->data, opa > LV_OPA_50 ? 0xff : 0x00, row_byte_cnt * dsc->header.h);
501     }
502     else {
503         uint32_t x;
504         uint32_t y;
505         for(y = 0; y < dsc->header.h; y++) {
506             for(x = 0; x < dsc->header.w; x++) {
507                 lv_img_buf_set_px_color(dsc, x, y, color);
508                 lv_img_buf_set_px_alpha(dsc, x, y, opa);
509             }
510         }
511     }
512 
513     lv_obj_invalidate(canvas);
514 }
515 
lv_canvas_draw_rect(lv_obj_t * canvas,lv_coord_t x,lv_coord_t y,lv_coord_t w,lv_coord_t h,const lv_draw_rect_dsc_t * draw_dsc)516 void lv_canvas_draw_rect(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv_coord_t h,
517                          const lv_draw_rect_dsc_t * draw_dsc)
518 {
519     LV_ASSERT_OBJ(canvas, MY_CLASS);
520 
521     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
522 
523     if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
524         LV_LOG_WARN("lv_canvas_draw_rect: can't draw to LV_IMG_CF_INDEXED canvas");
525         return;
526     }
527 
528     /*Create a dummy display to fool the lv_draw function.
529      *It will think it draws to real screen.*/
530     lv_disp_t fake_disp;
531     lv_disp_drv_t driver;
532     lv_area_t clip_area;
533     init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
534 
535     lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
536     _lv_refr_set_disp_refreshing(&fake_disp);
537 
538     /*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
539     lv_color_t ctransp = LV_COLOR_CHROMA_KEY;
540     if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
541        draw_dsc->bg_color.full == ctransp.full) {
542         fake_disp.driver->antialiasing = 0;
543     }
544 
545     lv_area_t coords;
546     coords.x1 = x;
547     coords.y1 = y;
548     coords.x2 = x + w - 1;
549     coords.y2 = y + h - 1;
550 
551     lv_draw_rect(driver.draw_ctx, draw_dsc, &coords);
552 
553     _lv_refr_set_disp_refreshing(refr_ori);
554 
555     deinit_fake_disp(canvas, &fake_disp);
556 
557     lv_obj_invalidate(canvas);
558 }
559 
lv_canvas_draw_text(lv_obj_t * canvas,lv_coord_t x,lv_coord_t y,lv_coord_t max_w,lv_draw_label_dsc_t * draw_dsc,const char * txt)560 void lv_canvas_draw_text(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t max_w,
561                          lv_draw_label_dsc_t * draw_dsc, const char * txt)
562 {
563     LV_ASSERT_OBJ(canvas, MY_CLASS);
564 
565     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
566 
567     if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
568         LV_LOG_WARN("lv_canvas_draw_text: can't draw to LV_IMG_CF_INDEXED canvas");
569         return;
570     }
571 
572     /*Create a dummy display to fool the lv_draw function.
573      *It will think it draws to real screen.*/
574     lv_disp_t fake_disp;
575     lv_disp_drv_t driver;
576     lv_area_t clip_area;
577     init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
578 
579     lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
580     _lv_refr_set_disp_refreshing(&fake_disp);
581 
582     lv_area_t coords;
583     coords.x1 = x;
584     coords.y1 = y;
585     coords.x2 = x + max_w - 1;
586     coords.y2 = dsc->header.h - 1;
587     lv_draw_label(driver.draw_ctx, draw_dsc, &coords, txt, NULL);
588 
589     _lv_refr_set_disp_refreshing(refr_ori);
590 
591     deinit_fake_disp(canvas, &fake_disp);
592 
593     lv_obj_invalidate(canvas);
594 }
595 
lv_canvas_draw_img(lv_obj_t * canvas,lv_coord_t x,lv_coord_t y,const void * src,const lv_draw_img_dsc_t * draw_dsc)596 void lv_canvas_draw_img(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, const void * src,
597                         const lv_draw_img_dsc_t * draw_dsc)
598 {
599     LV_ASSERT_OBJ(canvas, MY_CLASS);
600 
601     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
602 
603     if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
604         LV_LOG_WARN("lv_canvas_draw_img: can't draw to LV_IMG_CF_INDEXED canvas");
605         return;
606     }
607 
608     lv_img_header_t header;
609     lv_res_t res = lv_img_decoder_get_info(src, &header);
610     if(res != LV_RES_OK) {
611         LV_LOG_WARN("lv_canvas_draw_img: Couldn't get the image data.");
612         return;
613     }
614     /*Create a dummy display to fool the lv_draw function.
615      *It will think it draws to real screen.*/
616     lv_disp_t fake_disp;
617     lv_disp_drv_t driver;
618     lv_area_t clip_area;
619     init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
620 
621     lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
622     _lv_refr_set_disp_refreshing(&fake_disp);
623 
624     lv_area_t coords;
625     coords.x1 = x;
626     coords.y1 = y;
627     coords.x2 = x + header.w - 1;
628     coords.y2 = y + header.h - 1;
629 
630     lv_draw_img(driver.draw_ctx, draw_dsc, &coords, src);
631 
632     _lv_refr_set_disp_refreshing(refr_ori);
633 
634     deinit_fake_disp(canvas, &fake_disp);
635 
636     lv_obj_invalidate(canvas);
637 }
638 
lv_canvas_draw_line(lv_obj_t * canvas,const lv_point_t points[],uint32_t point_cnt,const lv_draw_line_dsc_t * draw_dsc)639 void lv_canvas_draw_line(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt,
640                          const lv_draw_line_dsc_t * draw_dsc)
641 {
642     LV_ASSERT_OBJ(canvas, MY_CLASS);
643 
644     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
645 
646     if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
647         LV_LOG_WARN("lv_canvas_draw_line: can't draw to LV_IMG_CF_INDEXED canvas");
648         return;
649     }
650 
651     /*Create a dummy display to fool the lv_draw function.
652      *It will think it draws to real screen.*/
653     lv_disp_t fake_disp;
654     lv_disp_drv_t driver;
655     lv_area_t clip_area;
656     init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
657 
658     lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
659     _lv_refr_set_disp_refreshing(&fake_disp);
660 
661 
662     /*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
663     lv_color_t ctransp = LV_COLOR_CHROMA_KEY;
664     if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
665        draw_dsc->color.full == ctransp.full) {
666         fake_disp.driver->antialiasing = 0;
667     }
668 
669     uint32_t i;
670     for(i = 0; i < point_cnt - 1; i++) {
671         lv_draw_line(driver.draw_ctx, draw_dsc, &points[i], &points[i + 1]);
672     }
673 
674     _lv_refr_set_disp_refreshing(refr_ori);
675 
676     deinit_fake_disp(canvas, &fake_disp);
677 
678     lv_obj_invalidate(canvas);
679 }
680 
lv_canvas_draw_polygon(lv_obj_t * canvas,const lv_point_t points[],uint32_t point_cnt,const lv_draw_rect_dsc_t * draw_dsc)681 void lv_canvas_draw_polygon(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt,
682                             const lv_draw_rect_dsc_t * draw_dsc)
683 {
684     LV_ASSERT_OBJ(canvas, MY_CLASS);
685 
686     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
687 
688     if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
689         LV_LOG_WARN("lv_canvas_draw_polygon: can't draw to LV_IMG_CF_INDEXED canvas");
690         return;
691     }
692 
693     /*Create a dummy display to fool the lv_draw function.
694      *It will think it draws to real screen.*/
695     lv_disp_t fake_disp;
696     lv_disp_drv_t driver;
697     lv_area_t clip_area;
698     init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
699 
700     lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
701     _lv_refr_set_disp_refreshing(&fake_disp);
702 
703     /*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
704     lv_color_t ctransp = LV_COLOR_CHROMA_KEY;
705     if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
706        draw_dsc->bg_color.full == ctransp.full) {
707         fake_disp.driver->antialiasing = 0;
708     }
709 
710     lv_draw_polygon(driver.draw_ctx, draw_dsc, points, point_cnt);
711 
712     _lv_refr_set_disp_refreshing(refr_ori);
713 
714     deinit_fake_disp(canvas, &fake_disp);
715 
716     lv_obj_invalidate(canvas);
717 }
718 
lv_canvas_draw_arc(lv_obj_t * canvas,lv_coord_t x,lv_coord_t y,lv_coord_t r,int32_t start_angle,int32_t end_angle,const lv_draw_arc_dsc_t * draw_dsc)719 void lv_canvas_draw_arc(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t r, int32_t start_angle,
720                         int32_t end_angle, const lv_draw_arc_dsc_t * draw_dsc)
721 {
722 #if LV_DRAW_COMPLEX
723     LV_ASSERT_OBJ(canvas, MY_CLASS);
724 
725     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
726 
727     if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
728         LV_LOG_WARN("lv_canvas_draw_arc: can't draw to LV_IMG_CF_INDEXED canvas");
729         return;
730     }
731 
732     /*Create a dummy display to fool the lv_draw function.
733      *It will think it draws to real screen.*/
734     lv_disp_t fake_disp;
735     lv_disp_drv_t driver;
736     lv_area_t clip_area;
737     init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
738 
739     lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
740     _lv_refr_set_disp_refreshing(&fake_disp);
741 
742     lv_point_t p = {x, y};
743     lv_draw_arc(driver.draw_ctx, draw_dsc, &p, r,  start_angle, end_angle);
744 
745     _lv_refr_set_disp_refreshing(refr_ori);
746 
747     deinit_fake_disp(canvas, &fake_disp);
748 
749     lv_obj_invalidate(canvas);
750 #else
751     LV_UNUSED(canvas);
752     LV_UNUSED(x);
753     LV_UNUSED(y);
754     LV_UNUSED(r);
755     LV_UNUSED(start_angle);
756     LV_UNUSED(end_angle);
757     LV_UNUSED(draw_dsc);
758     LV_LOG_WARN("Can't draw arc with LV_DRAW_COMPLEX == 0");
759 #endif
760 }
761 
762 /**********************
763  *   STATIC FUNCTIONS
764  **********************/
765 
lv_canvas_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)766 static void lv_canvas_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
767 {
768     LV_UNUSED(class_p);
769     LV_TRACE_OBJ_CREATE("begin");
770 
771     lv_canvas_t * canvas = (lv_canvas_t *)obj;
772 
773     canvas->dsc.header.always_zero = 0;
774     canvas->dsc.header.cf          = LV_IMG_CF_TRUE_COLOR;
775     canvas->dsc.header.h           = 0;
776     canvas->dsc.header.w           = 0;
777     canvas->dsc.data_size          = 0;
778     canvas->dsc.data               = NULL;
779 
780     lv_img_set_src(obj, &canvas->dsc);
781 
782     LV_TRACE_OBJ_CREATE("finished");
783 }
784 
lv_canvas_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)785 static void lv_canvas_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
786 {
787     LV_UNUSED(class_p);
788     LV_TRACE_OBJ_CREATE("begin");
789 
790     lv_canvas_t * canvas = (lv_canvas_t *)obj;
791     lv_img_cache_invalidate_src(&canvas->dsc);
792 }
793 
794 
init_fake_disp(lv_obj_t * canvas,lv_disp_t * disp,lv_disp_drv_t * drv,lv_area_t * clip_area)795 static void init_fake_disp(lv_obj_t * canvas, lv_disp_t * disp, lv_disp_drv_t * drv, lv_area_t * clip_area)
796 {
797     lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
798 
799     clip_area->x1 = 0;
800     clip_area->x2 = dsc->header.w - 1;
801     clip_area->y1 = 0;
802     clip_area->y2 = dsc->header.h - 1;
803 
804     /*Allocate the fake driver on the stack as the entire display doesn't outlive this function*/
805     lv_memset_00(disp, sizeof(lv_disp_t));
806     disp->driver = drv;
807 
808     lv_disp_drv_init(disp->driver);
809     disp->driver->hor_res = dsc->header.w;
810     disp->driver->ver_res = dsc->header.h;
811 
812     lv_draw_ctx_t * draw_ctx = lv_mem_alloc(sizeof(lv_draw_sw_ctx_t));
813     LV_ASSERT_MALLOC(draw_ctx);
814     if(draw_ctx == NULL)  return;
815     lv_draw_sw_init_ctx(drv, draw_ctx);
816     disp->driver->draw_ctx = draw_ctx;
817     draw_ctx->clip_area = clip_area;
818     draw_ctx->buf_area = clip_area;
819     draw_ctx->buf = (void *)dsc->data;
820 
821     lv_disp_drv_use_generic_set_px_cb(disp->driver, dsc->header.cf);
822     if(LV_COLOR_SCREEN_TRANSP && dsc->header.cf != LV_IMG_CF_TRUE_COLOR_ALPHA) {
823         drv->screen_transp = 0;
824     }
825 }
826 
deinit_fake_disp(lv_obj_t * canvas,lv_disp_t * disp)827 static void deinit_fake_disp(lv_obj_t * canvas, lv_disp_t * disp)
828 {
829     LV_UNUSED(canvas);
830     lv_draw_sw_deinit_ctx(disp->driver, disp->driver->draw_ctx);
831     lv_mem_free(disp->driver->draw_ctx);
832 }
833 
834 
835 
836 #endif
837