-
Notifications
You must be signed in to change notification settings - Fork 0
/
lv_draw_buf.c
571 lines (483 loc) · 18.3 KB
/
lv_draw_buf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
* @file lv_draw_buf.c
*
*/
/*********************
* INCLUDES
*********************/
#include "../misc/lv_types.h"
#include "lv_draw_buf.h"
#include "../stdlib/lv_string.h"
#include "../core/lv_global.h"
/*********************
* DEFINES
*********************/
#define default_handlers LV_GLOBAL_DEFAULT()->draw_buf_handlers
#define font_draw_buf_handlers LV_GLOBAL_DEFAULT()->font_draw_buf_handlers
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void * buf_malloc(size_t size, lv_color_format_t color_format);
static void buf_free(void * buf);
static void * buf_align(void * buf, lv_color_format_t color_format);
static void * draw_buf_malloc(const lv_draw_buf_handlers_t * handler, size_t size_bytes,
lv_color_format_t color_format);
static void draw_buf_free(const lv_draw_buf_handlers_t * handler, void * buf);
static uint32_t width_to_stride(uint32_t w, lv_color_format_t color_format);
static uint32_t _calculate_draw_buf_size(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void _lv_draw_buf_init_handlers(void)
{
lv_draw_buf_init_with_default_handlers(&default_handlers);
lv_draw_buf_init_with_default_handlers(&font_draw_buf_handlers);
}
void lv_draw_buf_init_with_default_handlers(lv_draw_buf_handlers_t * handlers)
{
lv_draw_buf_init_handlers(handlers, buf_malloc, buf_free, buf_align, NULL, width_to_stride);
}
void lv_draw_buf_init_handlers(lv_draw_buf_handlers_t * handlers,
lv_draw_buf_malloc_cb buf_malloc_cb,
lv_draw_buf_free_cb buf_free_cb,
lv_draw_buf_align_cb align_pointer_cb,
lv_draw_buf_invalidate_cache_cb invalidate_cache_cb,
lv_draw_buf_width_to_stride_cb width_to_stride_cb)
{
lv_memzero(handlers, sizeof(lv_draw_buf_handlers_t));
handlers->buf_malloc_cb = buf_malloc_cb;
handlers->buf_free_cb = buf_free_cb;
handlers->align_pointer_cb = align_pointer_cb;
handlers->invalidate_cache_cb = invalidate_cache_cb;
handlers->width_to_stride_cb = width_to_stride_cb;
}
lv_draw_buf_handlers_t * lv_draw_buf_get_handlers(void)
{
return &default_handlers;
}
uint32_t lv_draw_buf_width_to_stride(uint32_t w, lv_color_format_t color_format)
{
return lv_draw_buf_width_to_stride_user(&default_handlers, w, color_format);
}
uint32_t lv_draw_buf_width_to_stride_user(const lv_draw_buf_handlers_t * handlers, uint32_t w,
lv_color_format_t color_format)
{
if(handlers->width_to_stride_cb) return handlers->width_to_stride_cb(w, color_format);
else return 0;
}
void * lv_draw_buf_align(void * data, lv_color_format_t color_format)
{
return lv_draw_buf_align_user(&default_handlers, data, color_format);
}
void * lv_draw_buf_align_user(const lv_draw_buf_handlers_t * handlers, void * data, lv_color_format_t color_format)
{
if(handlers->align_pointer_cb) return handlers->align_pointer_cb(data, color_format);
else return NULL;
}
void lv_draw_buf_invalidate_cache(const lv_draw_buf_t * draw_buf, const lv_area_t * area)
{
lv_draw_buf_invalidate_cache_user(&default_handlers, draw_buf, area);
}
void lv_draw_buf_invalidate_cache_user(const lv_draw_buf_handlers_t * handlers, const lv_draw_buf_t * draw_buf,
const lv_area_t * area)
{
if(handlers->invalidate_cache_cb) {
LV_ASSERT_NULL(draw_buf);
const lv_image_header_t * header = &draw_buf->header;
lv_area_t full;
if(area == NULL) {
full = (lv_area_t) {
0, 0, header->w - 1, header->h - 1
};
area = &full;
}
handlers->invalidate_cache_cb(draw_buf, area);
}
}
void lv_draw_buf_clear(lv_draw_buf_t * draw_buf, const lv_area_t * a)
{
LV_ASSERT_NULL(draw_buf);
if(a && lv_area_get_width(a) < 0) return;
if(a && lv_area_get_height(a) < 0) return;
const lv_image_header_t * header = &draw_buf->header;
uint32_t stride = header->stride;
if(a == NULL) {
lv_memzero(draw_buf->data, header->h * stride);
}
else {
uint8_t * bufc;
uint32_t line_length;
int32_t start_y, end_y;
uint8_t px_size = lv_color_format_get_size(header->cf);
bufc = lv_draw_buf_goto_xy(draw_buf, a->x1, a->y1);
line_length = lv_area_get_width(a) * px_size;
start_y = a->y1;
end_y = a->y2;
for(; start_y <= end_y; start_y++) {
lv_memzero(bufc, line_length);
bufc += stride;
}
}
}
void lv_draw_buf_copy(lv_draw_buf_t * dest, const lv_area_t * dest_area,
const lv_draw_buf_t * src, const lv_area_t * src_area)
{
uint8_t * dest_bufc;
uint8_t * src_bufc;
int32_t line_width;
/*Source and dest color format must be same. Color conversion is not supported yet.*/
LV_ASSERT_FORMAT_MSG(dest->header.cf == src->header.cf, "Color format mismatch: %d != %d",
dest->header.cf, src->header.cf);
if(dest_area == NULL) line_width = dest->header.w;
else line_width = lv_area_get_width(dest_area);
/* For indexed image, copy the palette if we are copying full image area*/
if(dest_area == NULL || src_area == NULL) {
if(LV_COLOR_FORMAT_IS_INDEXED(dest->header.cf)) {
lv_memcpy(dest->data, src->data, LV_COLOR_INDEXED_PALETTE_SIZE(dest->header.cf) * sizeof(lv_color32_t));
}
}
/*Check source and dest area have same width*/
if((src_area == NULL && line_width != src->header.w) || \
(src_area != NULL && line_width != lv_area_get_width(src_area))) {
LV_ASSERT_MSG(0, "Source and destination areas have different width");
return;
}
if(src_area) src_bufc = lv_draw_buf_goto_xy(src, src_area->x1, src_area->y1);
else src_bufc = lv_draw_buf_goto_xy(src, 0, 0);
if(dest_area) dest_bufc = lv_draw_buf_goto_xy(dest, dest_area->x1, dest_area->y1);
else dest_bufc = lv_draw_buf_goto_xy(dest, 0, 0);
int32_t start_y, end_y;
if(dest_area) {
start_y = dest_area->y1;
end_y = dest_area->y2;
}
else {
start_y = 0;
end_y = dest->header.h - 1;
}
uint32_t dest_stride = dest->header.stride;
uint32_t src_stride = src->header.stride;
uint32_t line_bytes = (line_width * lv_color_format_get_bpp(dest->header.cf) + 7) >> 3;
for(; start_y <= end_y; start_y++) {
lv_memcpy(dest_bufc, src_bufc, line_bytes);
dest_bufc += dest_stride;
src_bufc += src_stride;
}
}
lv_result_t lv_draw_buf_init(lv_draw_buf_t * draw_buf, uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride,
void * data, uint32_t data_size)
{
LV_ASSERT_NULL(draw_buf);
if(draw_buf == NULL) return LV_RESULT_INVALID;
lv_memzero(draw_buf, sizeof(lv_draw_buf_t));
if(stride == 0) stride = lv_draw_buf_width_to_stride(w, cf);
if(stride * h > data_size) {
LV_LOG_WARN("Data size too small, required: %" LV_PRId32 ", provided: %" LV_PRId32, stride * h,
data_size);
return LV_RESULT_INVALID;
}
lv_image_header_t * header = &draw_buf->header;
header->w = w;
header->h = h;
header->cf = cf;
header->stride = stride;
header->flags = 0;
header->magic = LV_IMAGE_HEADER_MAGIC;
draw_buf->data = lv_draw_buf_align(data, cf);
draw_buf->unaligned_data = data;
draw_buf->data_size = data_size;
if(draw_buf->data != draw_buf->unaligned_data) {
LV_LOG_WARN("Data is not aligned, ignored");
}
return LV_RESULT_OK;
}
lv_draw_buf_t * lv_draw_buf_create(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride)
{
return lv_draw_buf_create_user(&default_handlers, w, h, cf, stride);
}
lv_draw_buf_t * lv_draw_buf_create_user(const lv_draw_buf_handlers_t * handlers, uint32_t w, uint32_t h,
lv_color_format_t cf, uint32_t stride)
{
lv_draw_buf_t * draw_buf = lv_malloc_zeroed(sizeof(lv_draw_buf_t));
LV_ASSERT_MALLOC(draw_buf);
if(draw_buf == NULL) return NULL;
if(stride == 0) stride = lv_draw_buf_width_to_stride(w, cf);
uint32_t size = _calculate_draw_buf_size(w, h, cf, stride);
void * buf = draw_buf_malloc(handlers, size, cf);
/*Do not assert here as LVGL or the app might just want to try creating a draw_buf*/
if(buf == NULL) {
LV_LOG_WARN("No memory: %"LV_PRIu32"x%"LV_PRIu32", cf: %d, stride: %"LV_PRIu32", %"LV_PRIu32"Byte, ",
w, h, cf, stride, size);
lv_free(draw_buf);
return NULL;
}
draw_buf->header.w = w;
draw_buf->header.h = h;
draw_buf->header.cf = cf;
draw_buf->header.flags = LV_IMAGE_FLAGS_MODIFIABLE | LV_IMAGE_FLAGS_ALLOCATED;
draw_buf->header.stride = stride;
draw_buf->header.magic = LV_IMAGE_HEADER_MAGIC;
draw_buf->data = lv_draw_buf_align(buf, cf);
draw_buf->unaligned_data = buf;
draw_buf->data_size = size;
return draw_buf;
}
lv_draw_buf_t * lv_draw_buf_dup(const lv_draw_buf_t * draw_buf)
{
const lv_image_header_t * header = &draw_buf->header;
lv_draw_buf_t * new_buf = lv_draw_buf_create(header->w, header->h, header->cf, header->stride);
if(new_buf == NULL) return NULL;
new_buf->header.flags = draw_buf->header.flags;
new_buf->header.flags |= LV_IMAGE_FLAGS_MODIFIABLE | LV_IMAGE_FLAGS_ALLOCATED;
/*Choose the smaller size to copy*/
uint32_t size = LV_MIN(draw_buf->data_size, new_buf->data_size);
/*Copy image data*/
lv_memcpy(new_buf->data, draw_buf->data, size);
return new_buf;
}
lv_draw_buf_t * lv_draw_buf_reshape(lv_draw_buf_t * draw_buf, lv_color_format_t cf, uint32_t w, uint32_t h,
uint32_t stride)
{
if(draw_buf == NULL) return NULL;
/*If color format is unknown, keep using the original color format.*/
if(cf == LV_COLOR_FORMAT_UNKNOWN) cf = draw_buf->header.cf;
if(stride == 0) stride = lv_draw_buf_width_to_stride(w, cf);
uint32_t size = _calculate_draw_buf_size(w, h, cf, stride);
if(size > draw_buf->data_size) {
LV_LOG_TRACE("Draw buf too small for new shape");
return NULL;
}
draw_buf->header.cf = cf;
draw_buf->header.w = w;
draw_buf->header.h = h;
draw_buf->header.stride = stride;
return draw_buf;
}
void lv_draw_buf_destroy(lv_draw_buf_t * buf)
{
lv_draw_buf_destroy_user(&default_handlers, buf);
}
void lv_draw_buf_destroy_user(const lv_draw_buf_handlers_t * handlers, lv_draw_buf_t * buf)
{
LV_ASSERT_NULL(buf);
if(buf == NULL) return;
if(buf->header.flags & LV_IMAGE_FLAGS_ALLOCATED) {
draw_buf_free(handlers, buf->unaligned_data);
lv_free(buf);
}
else {
LV_LOG_ERROR("draw buffer is not allocated, ignored");
}
}
void * lv_draw_buf_goto_xy(const lv_draw_buf_t * buf, uint32_t x, uint32_t y)
{
LV_ASSERT_NULL(buf);
if(buf == NULL) return NULL;
uint8_t * data = buf->data;
/*Skip palette*/
data += LV_COLOR_INDEXED_PALETTE_SIZE(buf->header.cf) * sizeof(lv_color32_t);
data += buf->header.stride * y;
if(x == 0) return data;
return data + x * lv_color_format_get_size(buf->header.cf);
}
lv_result_t lv_draw_buf_adjust_stride(lv_draw_buf_t * src, uint32_t stride)
{
LV_ASSERT_NULL(src);
LV_ASSERT_NULL(src->data);
if(src == NULL) return LV_RESULT_INVALID;
if(src->data == NULL) return LV_RESULT_INVALID;
const lv_image_header_t * header = &src->header;
uint32_t w = header->w;
uint32_t h = header->h;
/*Use global stride*/
if(stride == 0) stride = lv_draw_buf_width_to_stride(w, header->cf);
/*Check if stride already match*/
if(header->stride == stride) return LV_RESULT_OK;
/*Calculate the minimal stride allowed from bpp*/
uint32_t bpp = lv_color_format_get_bpp(header->cf);
uint32_t min_stride = (w * bpp + 7) >> 3;
if(stride < min_stride) {
LV_LOG_WARN("New stride is too small. min: %" LV_PRId32, min_stride);
return LV_RESULT_INVALID;
}
/*Check if buffer has enough space. */
uint32_t new_size = _calculate_draw_buf_size(w, h, header->cf, stride);
if(new_size > src->data_size) {
return LV_RESULT_INVALID;
}
uint32_t offset = LV_COLOR_INDEXED_PALETTE_SIZE(header->cf) * 4;
if(stride > header->stride) {
/*Copy from the last line to the first*/
uint8_t * src_data = src->data + offset + header->stride * (h - 1);
uint8_t * dst_data = src->data + offset + stride * (h - 1);
for(uint32_t y = 0; y < h; y++) {
lv_memmove(dst_data, src_data, min_stride);
src_data -= header->stride;
dst_data -= stride;
}
}
else {
/*Copy from the first line to the last*/
uint8_t * src_data = src->data + offset;
uint8_t * dst_data = src->data + offset;
for(uint32_t y = 0; y < h; y++) {
lv_memmove(dst_data, src_data, min_stride);
src_data += header->stride;
dst_data += stride;
}
}
src->header.stride = stride;
return LV_RESULT_OK;
}
lv_result_t lv_draw_buf_premultiply(lv_draw_buf_t * draw_buf)
{
LV_ASSERT_NULL(draw_buf);
if(draw_buf == NULL) return LV_RESULT_INVALID;
if(draw_buf->header.flags & LV_IMAGE_FLAGS_PREMULTIPLIED) return LV_RESULT_INVALID;
if((draw_buf->header.flags & LV_IMAGE_FLAGS_MODIFIABLE) == 0) {
LV_LOG_WARN("draw buf is not modifiable: 0x%04x", draw_buf->header.flags);
return LV_RESULT_INVALID;
}
/*Premultiply color with alpha, do case by case by judging color format*/
lv_color_format_t cf = draw_buf->header.cf;
if(LV_COLOR_FORMAT_IS_INDEXED(cf)) {
int size = LV_COLOR_INDEXED_PALETTE_SIZE(cf);
lv_color32_t * palette = (lv_color32_t *)draw_buf->data;
for(int i = 0; i < size; i++) {
lv_color_premultiply(&palette[i]);
}
}
else if(cf == LV_COLOR_FORMAT_ARGB8888) {
uint32_t h = draw_buf->header.h;
uint32_t w = draw_buf->header.w;
uint32_t stride = draw_buf->header.stride;
uint8_t * line = (uint8_t *)draw_buf->data;
for(uint32_t y = 0; y < h; y++) {
lv_color32_t * pixel = (lv_color32_t *)line;
for(uint32_t x = 0; x < w; x++) {
lv_color_premultiply(pixel);
pixel++;
}
line += stride;
}
}
else if(cf == LV_COLOR_FORMAT_RGB565A8) {
uint32_t h = draw_buf->header.h;
uint32_t w = draw_buf->header.w;
uint32_t stride = draw_buf->header.stride;
uint32_t alpha_stride = stride / 2;
uint8_t * line = (uint8_t *)draw_buf->data;
lv_opa_t * alpha = (lv_opa_t *)(line + stride * h);
for(uint32_t y = 0; y < h; y++) {
lv_color16_t * pixel = (lv_color16_t *)line;
for(uint32_t x = 0; x < w; x++) {
lv_color16_premultiply(pixel, alpha[x]);
pixel++;
}
line += stride;
alpha += alpha_stride;
}
}
else if(cf == LV_COLOR_FORMAT_ARGB8565) {
uint32_t h = draw_buf->header.h;
uint32_t w = draw_buf->header.w;
uint32_t stride = draw_buf->header.stride;
uint8_t * line = (uint8_t *)draw_buf->data;
for(uint32_t y = 0; y < h; y++) {
uint8_t * pixel = line;
for(uint32_t x = 0; x < w; x++) {
uint8_t alpha = pixel[2];
lv_color16_premultiply((lv_color16_t *)pixel, alpha);
pixel += 3;
}
line += stride;
}
}
else if(LV_COLOR_FORMAT_IS_ALPHA_ONLY(cf)) {
/*Pass*/
}
else {
LV_LOG_WARN("draw buf has no alpha, cf: %d", cf);
}
draw_buf->header.flags |= LV_IMAGE_FLAGS_PREMULTIPLIED;
return LV_RESULT_OK;
}
void lv_draw_buf_set_palette(lv_draw_buf_t * draw_buf, uint8_t index, lv_color32_t color)
{
LV_ASSERT_NULL(draw_buf);
if(draw_buf == NULL) return;
if(!LV_COLOR_FORMAT_IS_INDEXED(draw_buf->header.cf)) {
LV_LOG_WARN("Not indexed color format");
return;
}
uint8_t * buf = (uint8_t *)draw_buf->data;
lv_memcpy(&buf[index * sizeof(color)], &color, sizeof(color));
}
/**********************
* STATIC FUNCTIONS
**********************/
static void * buf_malloc(size_t size_bytes, lv_color_format_t color_format)
{
LV_UNUSED(color_format);
/*Allocate larger memory to be sure it can be aligned as needed*/
size_bytes += LV_DRAW_BUF_ALIGN - 1;
return lv_malloc(size_bytes);
}
static void buf_free(void * buf)
{
lv_free(buf);
}
static void * buf_align(void * buf, lv_color_format_t color_format)
{
LV_UNUSED(color_format);
uint8_t * buf_u8 = buf;
if(buf_u8) {
buf_u8 += LV_DRAW_BUF_ALIGN - 1;
buf_u8 = (uint8_t *)((lv_uintptr_t) buf_u8 & ~(LV_DRAW_BUF_ALIGN - 1));
}
return buf_u8;
}
static uint32_t width_to_stride(uint32_t w, lv_color_format_t color_format)
{
uint32_t width_byte;
width_byte = w * lv_color_format_get_bpp(color_format);
width_byte = (width_byte + 7) >> 3; /*Round up*/
return (width_byte + LV_DRAW_BUF_STRIDE_ALIGN - 1) & ~(LV_DRAW_BUF_STRIDE_ALIGN - 1);
}
static void * draw_buf_malloc(const lv_draw_buf_handlers_t * handlers, size_t size_bytes,
lv_color_format_t color_format)
{
if(handlers->buf_malloc_cb) return handlers->buf_malloc_cb(size_bytes, color_format);
else return NULL;
}
static void draw_buf_free(const lv_draw_buf_handlers_t * handlers, void * buf)
{
if(handlers->buf_free_cb)
handlers->buf_free_cb(buf);
}
/**
* For given width, height, color format, and stride, calculate the size needed for a new draw buffer.
*/
static uint32_t _calculate_draw_buf_size(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride)
{
uint32_t size;
if(stride == 0) stride = lv_draw_buf_width_to_stride(w, cf);
size = stride * h;
if(cf == LV_COLOR_FORMAT_RGB565A8) {
size += (stride / 2) * h; /*A8 mask*/
}
else if(LV_COLOR_FORMAT_IS_INDEXED(cf)) {
/*@todo we have to include palette right before image data*/
size += LV_COLOR_INDEXED_PALETTE_SIZE(cf) * 4;
}
return size;
}