1 /**
2 * @file lv_draw_img.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_draw_sw.h"
10 #include "../lv_img_cache.h"
11 #include "../../hal/lv_hal_disp.h"
12 #include "../../misc/lv_log.h"
13 #include "../../core/lv_refr.h"
14 #include "../../misc/lv_mem.h"
15 #include "../../misc/lv_math.h"
16
17 /*********************
18 * DEFINES
19 *********************/
20 #define MAX_BUF_SIZE (uint32_t) lv_disp_get_hor_res(_lv_refr_get_disp_refreshing())
21
22 /**********************
23 * TYPEDEFS
24 **********************/
25
26 /**********************
27 * STATIC PROTOTYPES
28 **********************/
29 static void convert_cb(const lv_area_t * dest_area, const void * src_buf, lv_coord_t src_w, lv_coord_t src_h,
30 lv_coord_t src_stride, const lv_draw_img_dsc_t * draw_dsc, lv_img_cf_t cf, lv_color_t * cbuf, lv_opa_t * abuf);
31
32 /**********************
33 * STATIC VARIABLES
34 **********************/
35
36 /**********************
37 * MACROS
38 **********************/
39
40 /**********************
41 * GLOBAL FUNCTIONS
42 **********************/
43
44
lv_draw_sw_img_decoded(struct _lv_draw_ctx_t * draw_ctx,const lv_draw_img_dsc_t * draw_dsc,const lv_area_t * coords,const uint8_t * src_buf,lv_img_cf_t cf)45 LV_ATTRIBUTE_FAST_MEM void lv_draw_sw_img_decoded(struct _lv_draw_ctx_t * draw_ctx, const lv_draw_img_dsc_t * draw_dsc,
46 const lv_area_t * coords, const uint8_t * src_buf, lv_img_cf_t cf)
47 {
48 /*Use the clip area as draw area*/
49 lv_area_t draw_area;
50 lv_area_copy(&draw_area, draw_ctx->clip_area);
51
52 bool mask_any = lv_draw_mask_is_any(&draw_area);
53 bool transform = draw_dsc->angle != 0 || draw_dsc->zoom != LV_IMG_ZOOM_NONE ? true : false;
54
55 lv_area_t blend_area;
56 lv_draw_sw_blend_dsc_t blend_dsc;
57
58 lv_memset_00(&blend_dsc, sizeof(lv_draw_sw_blend_dsc_t));
59 blend_dsc.opa = draw_dsc->opa;
60 blend_dsc.blend_mode = draw_dsc->blend_mode;
61 blend_dsc.blend_area = &blend_area;
62
63 /*The simplest case just copy the pixels into the draw_buf*/
64 if(!mask_any && !transform && cf == LV_IMG_CF_TRUE_COLOR && draw_dsc->recolor_opa == LV_OPA_TRANSP) {
65 blend_dsc.src_buf = (const lv_color_t *)src_buf;
66
67 blend_dsc.blend_area = coords;
68 lv_draw_sw_blend(draw_ctx, &blend_dsc);
69 }
70 else if(!mask_any && !transform && cf == LV_IMG_CF_ALPHA_8BIT) {
71 lv_area_t clipped_coords;
72 if(!_lv_area_intersect(&clipped_coords, coords, draw_ctx->clip_area)) return;
73
74 blend_dsc.mask_buf = (lv_opa_t *)src_buf;
75 blend_dsc.mask_area = coords;
76 blend_dsc.src_buf = NULL;
77 blend_dsc.color = draw_dsc->recolor;
78 blend_dsc.mask_res = LV_DRAW_MASK_RES_CHANGED;
79
80 blend_dsc.blend_area = coords;
81 lv_draw_sw_blend(draw_ctx, &blend_dsc);
82 }
83 #if LV_COLOR_DEPTH == 16
84 else if(!mask_any && !transform && cf == LV_IMG_CF_RGB565A8 && draw_dsc->recolor_opa == LV_OPA_TRANSP) {
85 lv_coord_t src_w = lv_area_get_width(coords);
86 lv_coord_t src_h = lv_area_get_height(coords);
87 blend_dsc.src_buf = (const lv_color_t *)src_buf;
88 blend_dsc.mask_buf = (lv_opa_t *)src_buf;
89 blend_dsc.mask_buf += sizeof(lv_color_t) * src_w * src_h;
90 blend_dsc.blend_area = coords;
91 blend_dsc.mask_area = coords;
92 blend_dsc.mask_res = LV_DRAW_MASK_RES_CHANGED;
93 lv_draw_sw_blend(draw_ctx, &blend_dsc);
94 }
95 #endif
96 /*In the other cases every pixel need to be checked one-by-one*/
97 else {
98 blend_area.x1 = draw_ctx->clip_area->x1;
99 blend_area.x2 = draw_ctx->clip_area->x2;
100 blend_area.y1 = draw_ctx->clip_area->y1;
101 blend_area.y2 = draw_ctx->clip_area->y2;
102
103 lv_coord_t src_w = lv_area_get_width(coords);
104 lv_coord_t src_h = lv_area_get_height(coords);
105 lv_coord_t blend_h = lv_area_get_height(&blend_area);
106 lv_coord_t blend_w = lv_area_get_width(&blend_area);
107
108 uint32_t max_buf_size = MAX_BUF_SIZE;
109 uint32_t blend_size = lv_area_get_size(&blend_area);
110 uint32_t buf_h;
111 uint32_t buf_w = blend_w;
112 if(blend_size <= max_buf_size) {
113 buf_h = blend_h;
114 }
115 else {
116 /*Round to full lines*/
117 buf_h = max_buf_size / blend_w;
118 }
119
120 /*Create buffers and masks*/
121 uint32_t buf_size = buf_w * buf_h;
122
123 lv_color_t * rgb_buf = lv_mem_buf_get(buf_size * sizeof(lv_color_t));
124 lv_opa_t * mask_buf = lv_mem_buf_get(buf_size);
125 blend_dsc.mask_buf = mask_buf;
126 blend_dsc.mask_area = &blend_area;
127 blend_dsc.mask_res = LV_DRAW_MASK_RES_CHANGED;
128 blend_dsc.src_buf = rgb_buf;
129 lv_coord_t y_last = blend_area.y2;
130 blend_area.y2 = blend_area.y1 + buf_h - 1;
131
132 lv_draw_mask_res_t mask_res_def = (cf != LV_IMG_CF_TRUE_COLOR || draw_dsc->angle ||
133 draw_dsc->zoom != LV_IMG_ZOOM_NONE) ?
134 LV_DRAW_MASK_RES_CHANGED : LV_DRAW_MASK_RES_FULL_COVER;
135 blend_dsc.mask_res = mask_res_def;
136
137 while(blend_area.y1 <= y_last) {
138 /*Apply transformations if any or separate the channels*/
139 lv_area_t transform_area;
140 lv_area_copy(&transform_area, &blend_area);
141 lv_area_move(&transform_area, -coords->x1, -coords->y1);
142 if(transform) {
143 lv_draw_transform(draw_ctx, &transform_area, src_buf, src_w, src_h, src_w,
144 draw_dsc, cf, rgb_buf, mask_buf);
145 }
146 else {
147 convert_cb(&transform_area, src_buf, src_w, src_h, src_w, draw_dsc, cf, rgb_buf, mask_buf);
148 }
149
150 /*Apply recolor*/
151 if(draw_dsc->recolor_opa > LV_OPA_MIN) {
152 uint16_t premult_v[3];
153 lv_opa_t recolor_opa = draw_dsc->recolor_opa;
154 lv_color_t recolor = draw_dsc->recolor;
155 lv_color_premult(recolor, recolor_opa, premult_v);
156 recolor_opa = 255 - recolor_opa;
157 uint32_t i;
158 for(i = 0; i < buf_size; i++) {
159 rgb_buf[i] = lv_color_mix_premult(premult_v, rgb_buf[i], recolor_opa);
160 }
161 }
162 #if LV_DRAW_COMPLEX
163 /*Apply the masks if any*/
164 if(mask_any) {
165 lv_coord_t y;
166 lv_opa_t * mask_buf_tmp = mask_buf;
167 for(y = blend_area.y1; y <= blend_area.y2; y++) {
168 lv_draw_mask_res_t mask_res_line;
169 mask_res_line = lv_draw_mask_apply(mask_buf_tmp, blend_area.x1, y, blend_w);
170
171 if(mask_res_line == LV_DRAW_MASK_RES_TRANSP) {
172 lv_memset_00(mask_buf_tmp, blend_w);
173 blend_dsc.mask_res = LV_DRAW_MASK_RES_CHANGED;
174 }
175 else if(mask_res_line == LV_DRAW_MASK_RES_CHANGED) {
176 blend_dsc.mask_res = LV_DRAW_MASK_RES_CHANGED;
177 }
178 mask_buf_tmp += blend_w;
179 }
180 }
181 #endif
182
183 /*Blend*/
184 lv_draw_sw_blend(draw_ctx, &blend_dsc);
185
186 /*Go the the next lines*/
187 blend_area.y1 = blend_area.y2 + 1;
188 blend_area.y2 = blend_area.y1 + buf_h - 1;
189 if(blend_area.y2 > y_last) blend_area.y2 = y_last;
190 }
191
192 lv_mem_buf_release(mask_buf);
193 lv_mem_buf_release(rgb_buf);
194 }
195 }
196
197 /**********************
198 * STATIC FUNCTIONS
199 **********************/
200
201 /* Separate the image channels to RGB and Alpha to match LV_COLOR_DEPTH settings*/
convert_cb(const lv_area_t * dest_area,const void * src_buf,lv_coord_t src_w,lv_coord_t src_h,lv_coord_t src_stride,const lv_draw_img_dsc_t * draw_dsc,lv_img_cf_t cf,lv_color_t * cbuf,lv_opa_t * abuf)202 static void convert_cb(const lv_area_t * dest_area, const void * src_buf, lv_coord_t src_w, lv_coord_t src_h,
203 lv_coord_t src_stride, const lv_draw_img_dsc_t * draw_dsc, lv_img_cf_t cf, lv_color_t * cbuf, lv_opa_t * abuf)
204 {
205 LV_UNUSED(draw_dsc);
206 LV_UNUSED(src_h);
207 LV_UNUSED(src_w);
208
209 const uint8_t * src_tmp8 = (const uint8_t *)src_buf;
210 lv_coord_t y;
211 lv_coord_t x;
212
213 if(cf == LV_IMG_CF_TRUE_COLOR || cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
214 uint32_t px_cnt = lv_area_get_size(dest_area);
215 lv_memset_ff(abuf, px_cnt);
216
217 src_tmp8 += (src_stride * dest_area->y1 * sizeof(lv_color_t)) + dest_area->x1 * sizeof(lv_color_t);
218 uint32_t dest_w = lv_area_get_width(dest_area);
219 uint32_t dest_w_byte = dest_w * sizeof(lv_color_t);
220
221 lv_coord_t src_stride_byte = src_stride * sizeof(lv_color_t);
222 lv_color_t * cbuf_tmp = cbuf;
223 for(y = dest_area->y1; y <= dest_area->y2; y++) {
224 lv_memcpy(cbuf_tmp, src_tmp8, dest_w_byte);
225 src_tmp8 += src_stride_byte;
226 cbuf_tmp += dest_w;
227 }
228
229 /*Make "holes" for with Chroma keying*/
230 if(cf == LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED) {
231 uint32_t i;
232 lv_color_t chk = LV_COLOR_CHROMA_KEY;
233 #if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
234 uint8_t * cbuf_uint = (uint8_t *)cbuf;
235 uint8_t chk_v = chk.full;
236 #elif LV_COLOR_DEPTH == 16
237 uint16_t * cbuf_uint = (uint16_t *)cbuf;
238 uint16_t chk_v = chk.full;
239 #elif LV_COLOR_DEPTH == 32
240 uint32_t * cbuf_uint = (uint32_t *)cbuf;
241 uint32_t chk_v = chk.full;
242 #endif
243 for(i = 0; i < px_cnt; i++) {
244 if(chk_v == cbuf_uint[i]) abuf[i] = 0x00;
245 }
246 }
247 }
248 else if(cf == LV_IMG_CF_TRUE_COLOR_ALPHA) {
249 src_tmp8 += (src_stride * dest_area->y1 * LV_IMG_PX_SIZE_ALPHA_BYTE) + dest_area->x1 * LV_IMG_PX_SIZE_ALPHA_BYTE;
250
251 lv_coord_t src_new_line_step_px = (src_stride - lv_area_get_width(dest_area));
252 lv_coord_t src_new_line_step_byte = src_new_line_step_px * LV_IMG_PX_SIZE_ALPHA_BYTE;
253
254 lv_coord_t dest_h = lv_area_get_height(dest_area);
255 lv_coord_t dest_w = lv_area_get_width(dest_area);
256 for(y = 0; y < dest_h; y++) {
257 for(x = 0; x < dest_w; x++) {
258 abuf[x] = src_tmp8[LV_IMG_PX_SIZE_ALPHA_BYTE - 1];
259 #if LV_COLOR_DEPTH == 8 || LV_COLOR_DEPTH == 1
260 cbuf[x].full = *src_tmp8;
261 #elif LV_COLOR_DEPTH == 16
262 cbuf[x].full = *src_tmp8 + ((*(src_tmp8 + 1)) << 8);
263 #elif LV_COLOR_DEPTH == 32
264 cbuf[x] = *((lv_color_t *) src_tmp8);
265 cbuf[x].ch.alpha = 0xff;
266 #endif
267 src_tmp8 += LV_IMG_PX_SIZE_ALPHA_BYTE;
268
269 }
270 cbuf += dest_w;
271 abuf += dest_w;
272 src_tmp8 += src_new_line_step_byte;
273 }
274 }
275 else if(cf == LV_IMG_CF_RGB565A8) {
276 src_tmp8 += (src_stride * dest_area->y1 * sizeof(lv_color_t)) + dest_area->x1 * sizeof(lv_color_t);
277
278 lv_coord_t src_stride_byte = src_stride * sizeof(lv_color_t);
279
280 lv_coord_t dest_h = lv_area_get_height(dest_area);
281 lv_coord_t dest_w = lv_area_get_width(dest_area);
282 for(y = 0; y < dest_h; y++) {
283 lv_memcpy(cbuf, src_tmp8, dest_w * sizeof(lv_color_t));
284 cbuf += dest_w;
285 src_tmp8 += src_stride_byte;
286 }
287
288 src_tmp8 = (const uint8_t *)src_buf;
289 src_tmp8 += sizeof(lv_color_t) * src_w * src_h;
290 src_tmp8 += src_stride * dest_area->y1 + dest_area->x1;
291 for(y = 0; y < dest_h; y++) {
292 lv_memcpy(abuf, src_tmp8, dest_w);
293 abuf += dest_w;
294 src_tmp8 += src_stride;
295 }
296 }
297 }
298