1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4
5 #include "unity/unity.h"
6 #include "lv_test_helpers.h"
7
setUp(void)8 void setUp(void)
9 {
10 /* Function run before every test */
11 }
12
tearDown(void)13 void tearDown(void)
14 {
15 /* Function run after every test */
16 }
17
test_draw_buf_stride_adjust(void)18 void test_draw_buf_stride_adjust(void)
19 {
20 #if LV_BIN_DECODER_RAM_LOAD == 1
21 const char * color_formats[] = {
22 "I1",
23 "I2",
24 "I4",
25 "I8",
26 #if 0 /* Decoder will convert them to A8 anyway.*/
27 "A1",
28 "A2",
29 "A4",
30 #endif
31 "A8",
32 "RGB565",
33 #if 0 /* RGB565 with alpha is not supported*/
34 "RGB565A8",
35 "ARGB8565",
36 #endif
37 "RGB888",
38 "ARGB8888",
39 "XRGB8888",
40 };
41
42 lv_obj_t * img = lv_image_create(lv_screen_active());
43 lv_obj_center(img);
44
45 const lv_image_decoder_args_t args = {
46 .no_cache = true,
47 .premultiply = false,
48 .stride_align = false,
49 .use_indexed = true,
50 };
51
52 for(unsigned long i = 0; i < sizeof(color_formats) / sizeof(color_formats[0]); i++) {
53 char img_src[256];
54 char ref_image[256];
55 snprintf(img_src, sizeof(img_src), "A:test_images/stride_align1/UNCOMPRESSED/test_%s.bin", color_formats[i]);
56 snprintf(ref_image, sizeof(ref_image), "draw/temp_%s.o", color_formats[i]); /*Use .o file name so git ignores it.*/
57
58 lv_image_set_src(img, img_src);
59 TEST_ASSERT_EQUAL_SCREENSHOT(ref_image); /*Generate the reference image, use .o so git ignore it*/
60
61 lv_image_cache_drop(img_src); /* Image could be added to cache during lv_image_set_src*/
62
63 lv_image_decoder_dsc_t decoder_dsc;
64 lv_result_t res = lv_image_decoder_open(&decoder_dsc, img_src, &args);
65 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
66 lv_draw_buf_t * decoded = lv_draw_buf_dup(decoder_dsc.decoded);
67 TEST_ASSERT_NOT_NULL(decoded);
68
69 const lv_image_header_t header = decoder_dsc.decoded->header;
70 uint32_t image_width = header.w;
71 uint32_t image_height = header.h;
72 uint32_t image_stride = header.stride;
73 uint32_t min_stride = (image_width * lv_color_format_get_bpp(header.cf) + 7) >> 3;
74
75 /*Close the decoder since we copied out the decoded draw buffer*/
76 lv_image_decoder_close(&decoder_dsc);
77
78 /* Shrink stride to below minimal stride(by -1 in code below) should fail */
79 res = lv_draw_buf_adjust_stride(decoded, min_stride - 1);
80 TEST_ASSERT_EQUAL(LV_RESULT_INVALID, res);
81
82 /*Expand the stride should fail if stride is too large that buffer size overflow*/
83 res = lv_draw_buf_adjust_stride(decoded, image_stride + 1);
84 TEST_ASSERT_EQUAL(LV_RESULT_INVALID, res);
85
86 /*Create a larger draw buffer*/
87 lv_draw_buf_t * larger = lv_draw_buf_create(image_width, image_height, header.cf, min_stride + 100);
88
89 /*Copy draw buffer, it should look same.*/
90 lv_draw_buf_copy(larger, NULL, decoded, NULL);
91 lv_image_cache_drop(larger);
92 lv_image_set_src(img, larger);
93 TEST_ASSERT_EQUAL_SCREENSHOT(ref_image); /*The image should still looks same*/
94
95 /* Shrink stride to minimal stride should succeed */
96 res = lv_draw_buf_adjust_stride(larger, min_stride);
97 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
98 lv_image_cache_drop(larger);
99 lv_image_set_src(img, larger);
100 TEST_ASSERT_EQUAL_SCREENSHOT(ref_image); /*Test against with above reference image*/
101
102 /* Expand the stride should work, use a proper stride value should succeed*/
103 res = lv_draw_buf_adjust_stride(larger, min_stride + 20);
104 TEST_ASSERT_EQUAL(LV_RESULT_OK, res);
105 lv_image_cache_drop(larger);
106 lv_image_set_src(img, larger);
107 TEST_ASSERT_EQUAL_SCREENSHOT(ref_image); /*The image should still look same*/
108
109 lv_draw_buf_destroy(larger);
110 lv_draw_buf_destroy(decoded);
111 }
112
113 lv_obj_delete(img);
114 #endif
115 }
116
117 #endif
118