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 lv_draw_sw_transform(NULL, &dest_area, src_img->data, src_img->header.w, src_img->header.h, src_img->header.w,
193 &draw_dsc, canvas->dsc.header.cf, cbuf, abuf);
194
195 for(x = 0; x < dest_img->header.w; x++) {
196 if(abuf[x]) {
197 lv_img_buf_set_px_color(dest_img, x, y, cbuf[x]);
198 lv_img_buf_set_px_alpha(dest_img, x, y, abuf[x]);
199 }
200 }
201
202 dest_area.y1++;
203 dest_area.y2++;
204 }
205 lv_mem_free(cbuf);
206 lv_mem_free(abuf);
207
208 lv_obj_invalidate(obj);
209
210 #else
211 LV_UNUSED(obj);
212 LV_UNUSED(src_img);
213 LV_UNUSED(angle);
214 LV_UNUSED(zoom);
215 LV_UNUSED(offset_x);
216 LV_UNUSED(offset_y);
217 LV_UNUSED(pivot_x);
218 LV_UNUSED(pivot_y);
219 LV_UNUSED(antialias);
220 LV_LOG_WARN("Can't transform canvas with LV_DRAW_COMPLEX == 0");
221 #endif
222 }
223
lv_canvas_blur_hor(lv_obj_t * obj,const lv_area_t * area,uint16_t r)224 void lv_canvas_blur_hor(lv_obj_t * obj, const lv_area_t * area, uint16_t r)
225 {
226 LV_ASSERT_OBJ(obj, MY_CLASS);
227
228 if(r == 0) return;
229
230 lv_canvas_t * canvas = (lv_canvas_t *)obj;
231
232 lv_area_t a;
233 if(area) {
234 lv_area_copy(&a, area);
235 if(a.x1 < 0) a.x1 = 0;
236 if(a.y1 < 0) a.y1 = 0;
237 if(a.x2 > canvas->dsc.header.w - 1) a.x2 = canvas->dsc.header.w - 1;
238 if(a.y2 > canvas->dsc.header.h - 1) a.y2 = canvas->dsc.header.h - 1;
239 }
240 else {
241 a.x1 = 0;
242 a.y1 = 0;
243 a.x2 = canvas->dsc.header.w - 1;
244 a.y2 = canvas->dsc.header.h - 1;
245 }
246
247 lv_color_t color = lv_obj_get_style_img_recolor(obj, LV_PART_MAIN);
248
249 uint16_t r_back = r / 2;
250 uint16_t r_front = r / 2;
251
252 if((r & 0x1) == 0) r_back--;
253
254 bool has_alpha = lv_img_cf_has_alpha(canvas->dsc.header.cf);
255
256 lv_coord_t line_w = lv_img_buf_get_img_size(canvas->dsc.header.w, 1, canvas->dsc.header.cf);
257 uint8_t * line_buf = lv_mem_buf_get(line_w);
258
259 lv_img_dsc_t line_img;
260 line_img.data = line_buf;
261 line_img.header.always_zero = 0;
262 line_img.header.w = canvas->dsc.header.w;
263 line_img.header.h = 1;
264 line_img.header.cf = canvas->dsc.header.cf;
265
266 lv_coord_t x;
267 lv_coord_t y;
268 lv_coord_t x_safe;
269
270 for(y = a.y1; y <= a.y2; y++) {
271 uint32_t asum = 0;
272 uint32_t rsum = 0;
273 uint32_t gsum = 0;
274 uint32_t bsum = 0;
275
276 lv_color_t c;
277 lv_opa_t opa = LV_OPA_TRANSP;
278 lv_memcpy(line_buf, &canvas->dsc.data[y * line_w], line_w);
279
280 for(x = a.x1 - r_back; x <= a.x1 + r_front; x++) {
281 x_safe = x < 0 ? 0 : x;
282 x_safe = x_safe > canvas->dsc.header.w - 1 ? canvas->dsc.header.w - 1 : x_safe;
283
284 c = lv_img_buf_get_px_color(&line_img, x_safe, 0, color);
285 if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, x_safe, 0);
286
287 rsum += c.ch.red;
288 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
289 gsum += (c.ch.green_h << 3) + c.ch.green_l;
290 #else
291 gsum += c.ch.green;
292 #endif
293 bsum += c.ch.blue;
294 if(has_alpha) asum += opa;
295 }
296
297 /*Just to indicate that the px is visible*/
298 if(has_alpha == false) asum = LV_OPA_COVER;
299
300 for(x = a.x1; x <= a.x2; x++) {
301
302 if(asum) {
303 c.ch.red = rsum / r;
304 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
305 uint8_t gtmp = gsum / r;
306 c.ch.green_h = gtmp >> 3;
307 c.ch.green_l = gtmp & 0x7;
308 #else
309 c.ch.green = gsum / r;
310 #endif
311 c.ch.blue = bsum / r;
312 if(has_alpha) opa = asum / r;
313
314 lv_img_buf_set_px_color(&canvas->dsc, x, y, c);
315 }
316 if(has_alpha) lv_img_buf_set_px_alpha(&canvas->dsc, x, y, opa);
317
318 x_safe = x - r_back;
319 x_safe = x_safe < 0 ? 0 : x_safe;
320 c = lv_img_buf_get_px_color(&line_img, x_safe, 0, color);
321 if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, x_safe, 0);
322
323 rsum -= c.ch.red;
324 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
325 gsum -= (c.ch.green_h << 3) + c.ch.green_l;
326 #else
327 gsum -= c.ch.green;
328 #endif
329 bsum -= c.ch.blue;
330 if(has_alpha) asum -= opa;
331
332 x_safe = x + 1 + r_front;
333 x_safe = x_safe > canvas->dsc.header.w - 1 ? canvas->dsc.header.w - 1 : x_safe;
334 c = lv_img_buf_get_px_color(&line_img, x_safe, 0, lv_color_white());
335 if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, x_safe, 0);
336
337 rsum += c.ch.red;
338 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
339 gsum += (c.ch.green_h << 3) + c.ch.green_l;
340 #else
341 gsum += c.ch.green;
342 #endif
343 bsum += c.ch.blue;
344 if(has_alpha) asum += opa;
345 }
346 }
347 lv_obj_invalidate(obj);
348
349 lv_mem_buf_release(line_buf);
350 }
351
lv_canvas_blur_ver(lv_obj_t * obj,const lv_area_t * area,uint16_t r)352 void lv_canvas_blur_ver(lv_obj_t * obj, const lv_area_t * area, uint16_t r)
353 {
354 LV_ASSERT_OBJ(obj, MY_CLASS);
355
356 if(r == 0) return;
357
358 lv_canvas_t * canvas = (lv_canvas_t *)obj;
359
360 lv_area_t a;
361 if(area) {
362 lv_area_copy(&a, area);
363 if(a.x1 < 0) a.x1 = 0;
364 if(a.y1 < 0) a.y1 = 0;
365 if(a.x2 > canvas->dsc.header.w - 1) a.x2 = canvas->dsc.header.w - 1;
366 if(a.y2 > canvas->dsc.header.h - 1) a.y2 = canvas->dsc.header.h - 1;
367 }
368 else {
369 a.x1 = 0;
370 a.y1 = 0;
371 a.x2 = canvas->dsc.header.w - 1;
372 a.y2 = canvas->dsc.header.h - 1;
373 }
374
375 lv_color_t color = lv_obj_get_style_img_recolor(obj, LV_PART_MAIN);
376
377 uint16_t r_back = r / 2;
378 uint16_t r_front = r / 2;
379
380 if((r & 0x1) == 0) r_back--;
381
382 bool has_alpha = lv_img_cf_has_alpha(canvas->dsc.header.cf);
383 lv_coord_t col_w = lv_img_buf_get_img_size(1, canvas->dsc.header.h, canvas->dsc.header.cf);
384 uint8_t * col_buf = lv_mem_buf_get(col_w);
385 lv_img_dsc_t line_img;
386
387 line_img.data = col_buf;
388 line_img.header.always_zero = 0;
389 line_img.header.w = 1;
390 line_img.header.h = canvas->dsc.header.h;
391 line_img.header.cf = canvas->dsc.header.cf;
392
393 lv_coord_t x;
394 lv_coord_t y;
395 lv_coord_t y_safe;
396
397 for(x = a.x1; x <= a.x2; x++) {
398 uint32_t asum = 0;
399 uint32_t rsum = 0;
400 uint32_t gsum = 0;
401 uint32_t bsum = 0;
402
403 lv_color_t c;
404 lv_opa_t opa = LV_OPA_COVER;
405
406 for(y = a.y1 - r_back; y <= a.y1 + r_front; y++) {
407 y_safe = y < 0 ? 0 : y;
408 y_safe = y_safe > canvas->dsc.header.h - 1 ? canvas->dsc.header.h - 1 : y_safe;
409
410 c = lv_img_buf_get_px_color(&canvas->dsc, x, y_safe, color);
411 if(has_alpha) opa = lv_img_buf_get_px_alpha(&canvas->dsc, x, y_safe);
412
413 lv_img_buf_set_px_color(&line_img, 0, y_safe, c);
414 if(has_alpha) lv_img_buf_set_px_alpha(&line_img, 0, y_safe, opa);
415
416 rsum += c.ch.red;
417 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
418 gsum += (c.ch.green_h << 3) + c.ch.green_l;
419 #else
420 gsum += c.ch.green;
421 #endif
422 bsum += c.ch.blue;
423 if(has_alpha) asum += opa;
424 }
425
426 /*Just to indicate that the px is visible*/
427 if(has_alpha == false) asum = LV_OPA_COVER;
428
429 for(y = a.y1; y <= a.y2; y++) {
430 if(asum) {
431 c.ch.red = rsum / r;
432 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
433 uint8_t gtmp = gsum / r;
434 c.ch.green_h = gtmp >> 3;
435 c.ch.green_l = gtmp & 0x7;
436 #else
437 c.ch.green = gsum / r;
438 #endif
439 c.ch.blue = bsum / r;
440 if(has_alpha) opa = asum / r;
441
442 lv_img_buf_set_px_color(&canvas->dsc, x, y, c);
443 }
444 if(has_alpha) lv_img_buf_set_px_alpha(&canvas->dsc, x, y, opa);
445
446 y_safe = y - r_back;
447 y_safe = y_safe < 0 ? 0 : y_safe;
448 c = lv_img_buf_get_px_color(&line_img, 0, y_safe, color);
449 if(has_alpha) opa = lv_img_buf_get_px_alpha(&line_img, 0, y_safe);
450
451 rsum -= c.ch.red;
452 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
453 gsum -= (c.ch.green_h << 3) + c.ch.green_l;
454 #else
455 gsum -= c.ch.green;
456 #endif
457 bsum -= c.ch.blue;
458 if(has_alpha) asum -= opa;
459
460 y_safe = y + 1 + r_front;
461 y_safe = y_safe > canvas->dsc.header.h - 1 ? canvas->dsc.header.h - 1 : y_safe;
462
463 c = lv_img_buf_get_px_color(&canvas->dsc, x, y_safe, color);
464 if(has_alpha) opa = lv_img_buf_get_px_alpha(&canvas->dsc, x, y_safe);
465
466 lv_img_buf_set_px_color(&line_img, 0, y_safe, c);
467 if(has_alpha) lv_img_buf_set_px_alpha(&line_img, 0, y_safe, opa);
468
469 rsum += c.ch.red;
470 #if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP
471 gsum += (c.ch.green_h << 3) + c.ch.green_l;
472 #else
473 gsum += c.ch.green;
474 #endif
475 bsum += c.ch.blue;
476 if(has_alpha) asum += opa;
477 }
478 }
479
480 lv_obj_invalidate(obj);
481
482 lv_mem_buf_release(col_buf);
483 }
484
lv_canvas_fill_bg(lv_obj_t * canvas,lv_color_t color,lv_opa_t opa)485 void lv_canvas_fill_bg(lv_obj_t * canvas, lv_color_t color, lv_opa_t opa)
486 {
487 LV_ASSERT_OBJ(canvas, MY_CLASS);
488
489 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
490
491 if(dsc->header.cf == LV_IMG_CF_INDEXED_1BIT) {
492 uint32_t row_byte_cnt = (dsc->header.w + 7) >> 3;
493 /*+8 skip the palette*/
494 lv_memset((uint8_t *)dsc->data + 8, color.full ? 0xff : 0x00, row_byte_cnt * dsc->header.h);
495 }
496 else if(dsc->header.cf == LV_IMG_CF_ALPHA_1BIT) {
497 uint32_t row_byte_cnt = (dsc->header.w + 7) >> 3;
498 lv_memset((uint8_t *)dsc->data, opa > LV_OPA_50 ? 0xff : 0x00, row_byte_cnt * dsc->header.h);
499 }
500 else {
501 uint32_t x;
502 uint32_t y;
503 for(y = 0; y < dsc->header.h; y++) {
504 for(x = 0; x < dsc->header.w; x++) {
505 lv_img_buf_set_px_color(dsc, x, y, color);
506 lv_img_buf_set_px_alpha(dsc, x, y, opa);
507 }
508 }
509 }
510
511 lv_obj_invalidate(canvas);
512 }
513
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)514 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,
515 const lv_draw_rect_dsc_t * draw_dsc)
516 {
517 LV_ASSERT_OBJ(canvas, MY_CLASS);
518
519 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
520
521 if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
522 LV_LOG_WARN("lv_canvas_draw_rect: can't draw to LV_IMG_CF_INDEXED canvas");
523 return;
524 }
525
526 /*Create a dummy display to fool the lv_draw function.
527 *It will think it draws to real screen.*/
528 lv_disp_t fake_disp;
529 lv_disp_drv_t driver;
530 lv_area_t clip_area;
531 init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
532
533 lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
534 _lv_refr_set_disp_refreshing(&fake_disp);
535
536 /*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
537 lv_color_t ctransp = LV_COLOR_CHROMA_KEY;
538 if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
539 draw_dsc->bg_color.full == ctransp.full) {
540 fake_disp.driver->antialiasing = 0;
541 }
542
543 lv_area_t coords;
544 coords.x1 = x;
545 coords.y1 = y;
546 coords.x2 = x + w - 1;
547 coords.y2 = y + h - 1;
548
549 lv_draw_rect(driver.draw_ctx, draw_dsc, &coords);
550
551 _lv_refr_set_disp_refreshing(refr_ori);
552
553 deinit_fake_disp(canvas, &fake_disp);
554
555 lv_obj_invalidate(canvas);
556 }
557
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)558 void lv_canvas_draw_text(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, lv_coord_t max_w,
559 lv_draw_label_dsc_t * draw_dsc, const char * txt)
560 {
561 LV_ASSERT_OBJ(canvas, MY_CLASS);
562
563 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
564
565 if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
566 LV_LOG_WARN("lv_canvas_draw_text: can't draw to LV_IMG_CF_INDEXED canvas");
567 return;
568 }
569
570 /*Create a dummy display to fool the lv_draw function.
571 *It will think it draws to real screen.*/
572 lv_disp_t fake_disp;
573 lv_disp_drv_t driver;
574 lv_area_t clip_area;
575 init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
576
577 lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
578 _lv_refr_set_disp_refreshing(&fake_disp);
579
580 lv_area_t coords;
581 coords.x1 = x;
582 coords.y1 = y;
583 coords.x2 = x + max_w - 1;
584 coords.y2 = dsc->header.h - 1;
585 lv_draw_label(driver.draw_ctx, draw_dsc, &coords, txt, NULL);
586
587 _lv_refr_set_disp_refreshing(refr_ori);
588
589 deinit_fake_disp(canvas, &fake_disp);
590
591 lv_obj_invalidate(canvas);
592 }
593
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)594 void lv_canvas_draw_img(lv_obj_t * canvas, lv_coord_t x, lv_coord_t y, const void * src,
595 const lv_draw_img_dsc_t * draw_dsc)
596 {
597 LV_ASSERT_OBJ(canvas, MY_CLASS);
598
599 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
600
601 if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
602 LV_LOG_WARN("lv_canvas_draw_img: can't draw to LV_IMG_CF_INDEXED canvas");
603 return;
604 }
605
606 lv_img_header_t header;
607 lv_res_t res = lv_img_decoder_get_info(src, &header);
608 if(res != LV_RES_OK) {
609 LV_LOG_WARN("lv_canvas_draw_img: Couldn't get the image data.");
610 return;
611 }
612 /*Create a dummy display to fool the lv_draw function.
613 *It will think it draws to real screen.*/
614 lv_disp_t fake_disp;
615 lv_disp_drv_t driver;
616 lv_area_t clip_area;
617 init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
618
619 lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
620 _lv_refr_set_disp_refreshing(&fake_disp);
621
622 lv_area_t coords;
623 coords.x1 = x;
624 coords.y1 = y;
625 coords.x2 = x + header.w - 1;
626 coords.y2 = y + header.h - 1;
627
628 lv_draw_img(driver.draw_ctx, draw_dsc, &coords, src);
629
630 _lv_refr_set_disp_refreshing(refr_ori);
631
632 deinit_fake_disp(canvas, &fake_disp);
633
634 lv_obj_invalidate(canvas);
635 }
636
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)637 void lv_canvas_draw_line(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt,
638 const lv_draw_line_dsc_t * draw_dsc)
639 {
640 LV_ASSERT_OBJ(canvas, MY_CLASS);
641
642 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
643
644 if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
645 LV_LOG_WARN("lv_canvas_draw_line: can't draw to LV_IMG_CF_INDEXED canvas");
646 return;
647 }
648
649 /*Create a dummy display to fool the lv_draw function.
650 *It will think it draws to real screen.*/
651 lv_disp_t fake_disp;
652 lv_disp_drv_t driver;
653 lv_area_t clip_area;
654 init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
655
656 lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
657 _lv_refr_set_disp_refreshing(&fake_disp);
658
659 /*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
660 lv_color_t ctransp = LV_COLOR_CHROMA_KEY;
661 if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
662 draw_dsc->color.full == ctransp.full) {
663 fake_disp.driver->antialiasing = 0;
664 }
665
666 uint32_t i;
667 for(i = 0; i < point_cnt - 1; i++) {
668 lv_draw_line(driver.draw_ctx, draw_dsc, &points[i], &points[i + 1]);
669 }
670
671 _lv_refr_set_disp_refreshing(refr_ori);
672
673 deinit_fake_disp(canvas, &fake_disp);
674
675 lv_obj_invalidate(canvas);
676 }
677
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)678 void lv_canvas_draw_polygon(lv_obj_t * canvas, const lv_point_t points[], uint32_t point_cnt,
679 const lv_draw_rect_dsc_t * draw_dsc)
680 {
681 LV_ASSERT_OBJ(canvas, MY_CLASS);
682
683 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
684
685 if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
686 LV_LOG_WARN("lv_canvas_draw_polygon: can't draw to LV_IMG_CF_INDEXED canvas");
687 return;
688 }
689
690 /*Create a dummy display to fool the lv_draw function.
691 *It will think it draws to real screen.*/
692 lv_disp_t fake_disp;
693 lv_disp_drv_t driver;
694 lv_area_t clip_area;
695 init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
696
697 lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
698 _lv_refr_set_disp_refreshing(&fake_disp);
699
700 /*Disable anti-aliasing if drawing with transparent color to chroma keyed canvas*/
701 lv_color_t ctransp = LV_COLOR_CHROMA_KEY;
702 if(dsc->header.cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED &&
703 draw_dsc->bg_color.full == ctransp.full) {
704 fake_disp.driver->antialiasing = 0;
705 }
706
707 lv_draw_polygon(driver.draw_ctx, draw_dsc, points, point_cnt);
708
709 _lv_refr_set_disp_refreshing(refr_ori);
710
711 deinit_fake_disp(canvas, &fake_disp);
712
713 lv_obj_invalidate(canvas);
714 }
715
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)716 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,
717 int32_t end_angle, const lv_draw_arc_dsc_t * draw_dsc)
718 {
719 #if LV_DRAW_COMPLEX
720 LV_ASSERT_OBJ(canvas, MY_CLASS);
721
722 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
723
724 if(dsc->header.cf >= LV_IMG_CF_INDEXED_1BIT && dsc->header.cf <= LV_IMG_CF_INDEXED_8BIT) {
725 LV_LOG_WARN("lv_canvas_draw_arc: can't draw to LV_IMG_CF_INDEXED canvas");
726 return;
727 }
728
729 /*Create a dummy display to fool the lv_draw function.
730 *It will think it draws to real screen.*/
731 lv_disp_t fake_disp;
732 lv_disp_drv_t driver;
733 lv_area_t clip_area;
734 init_fake_disp(canvas, &fake_disp, &driver, &clip_area);
735
736 lv_disp_t * refr_ori = _lv_refr_get_disp_refreshing();
737 _lv_refr_set_disp_refreshing(&fake_disp);
738
739 lv_point_t p = {x, y};
740 lv_draw_arc(driver.draw_ctx, draw_dsc, &p, r, start_angle, end_angle);
741
742 _lv_refr_set_disp_refreshing(refr_ori);
743
744 deinit_fake_disp(canvas, &fake_disp);
745
746 lv_obj_invalidate(canvas);
747 #else
748 LV_UNUSED(canvas);
749 LV_UNUSED(x);
750 LV_UNUSED(y);
751 LV_UNUSED(r);
752 LV_UNUSED(start_angle);
753 LV_UNUSED(end_angle);
754 LV_UNUSED(draw_dsc);
755 LV_LOG_WARN("Can't draw arc with LV_DRAW_COMPLEX == 0");
756 #endif
757 }
758
759 /**********************
760 * STATIC FUNCTIONS
761 **********************/
762
lv_canvas_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)763 static void lv_canvas_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
764 {
765 LV_UNUSED(class_p);
766 LV_TRACE_OBJ_CREATE("begin");
767
768 lv_canvas_t * canvas = (lv_canvas_t *)obj;
769
770 canvas->dsc.header.always_zero = 0;
771 canvas->dsc.header.cf = LV_IMG_CF_TRUE_COLOR;
772 canvas->dsc.header.h = 0;
773 canvas->dsc.header.w = 0;
774 canvas->dsc.data_size = 0;
775 canvas->dsc.data = NULL;
776
777 lv_img_set_src(obj, &canvas->dsc);
778
779 LV_TRACE_OBJ_CREATE("finished");
780 }
781
lv_canvas_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)782 static void lv_canvas_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
783 {
784 LV_UNUSED(class_p);
785 LV_TRACE_OBJ_CREATE("begin");
786
787 lv_canvas_t * canvas = (lv_canvas_t *)obj;
788 lv_img_cache_invalidate_src(&canvas->dsc);
789 }
790
init_fake_disp(lv_obj_t * canvas,lv_disp_t * disp,lv_disp_drv_t * drv,lv_area_t * clip_area)791 static void init_fake_disp(lv_obj_t * canvas, lv_disp_t * disp, lv_disp_drv_t * drv, lv_area_t * clip_area)
792 {
793 lv_img_dsc_t * dsc = lv_canvas_get_img(canvas);
794
795 clip_area->x1 = 0;
796 clip_area->x2 = dsc->header.w - 1;
797 clip_area->y1 = 0;
798 clip_area->y2 = dsc->header.h - 1;
799
800 /*Allocate the fake driver on the stack as the entire display doesn't outlive this function*/
801 lv_memset_00(disp, sizeof(lv_disp_t));
802 disp->driver = drv;
803
804 lv_disp_drv_init(disp->driver);
805 disp->driver->hor_res = dsc->header.w;
806 disp->driver->ver_res = dsc->header.h;
807
808 lv_draw_ctx_t * draw_ctx = lv_mem_alloc(sizeof(lv_draw_sw_ctx_t));
809 LV_ASSERT_MALLOC(draw_ctx);
810 if(draw_ctx == NULL) return;
811 lv_draw_sw_init_ctx(drv, draw_ctx);
812 disp->driver->draw_ctx = draw_ctx;
813 draw_ctx->clip_area = clip_area;
814 draw_ctx->buf_area = clip_area;
815 draw_ctx->buf = (void *)dsc->data;
816
817 lv_disp_drv_use_generic_set_px_cb(disp->driver, dsc->header.cf);
818 if(LV_COLOR_SCREEN_TRANSP && dsc->header.cf != LV_IMG_CF_TRUE_COLOR_ALPHA) {
819 drv->screen_transp = 0;
820 }
821 }
822
deinit_fake_disp(lv_obj_t * canvas,lv_disp_t * disp)823 static void deinit_fake_disp(lv_obj_t * canvas, lv_disp_t * disp)
824 {
825 LV_UNUSED(canvas);
826 lv_draw_sw_deinit_ctx(disp->driver, disp->driver->draw_ctx);
827 lv_mem_free(disp->driver->draw_ctx);
828 }
829
830 #endif
831