1 /**
2 * @file lv_draw.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include "lv_draw.h"
13 #include "../lv_misc/lv_math.h"
14 #include "../lv_misc/lv_log.h"
15 #include "../lv_misc/lv_math.h"
16 #include "../lv_misc/lv_mem.h"
17 #include "../lv_misc/lv_gc.h"
18
19 #if defined(LV_GC_INCLUDE)
20 #include LV_GC_INCLUDE
21 #endif /* LV_ENABLE_GC */
22
23 /*********************
24 * DEFINES
25 *********************/
26
27 /**********************
28 * TYPEDEFS
29 **********************/
30
31 /**********************
32 * STATIC PROTOTYPES
33 **********************/
34
35 /**********************
36 * STATIC VARIABLES
37 **********************/
38 static uint32_t draw_buf_size = 0;
39
40 /**********************
41 * MACROS
42 **********************/
43
44 /**********************
45 * GLOBAL FUNCTIONS
46 **********************/
47
48 /**
49 * Give a buffer with the given to use during drawing.
50 * Be careful to not use the buffer while other processes are using it.
51 * @param size the required size
52 */
lv_draw_get_buf(uint32_t size)53 void * lv_draw_get_buf(uint32_t size)
54 {
55 if(size <= draw_buf_size) return LV_GC_ROOT(_lv_draw_buf);
56
57 LV_LOG_TRACE("lv_draw_get_buf: allocate");
58
59 draw_buf_size = size;
60
61 if(LV_GC_ROOT(_lv_draw_buf) == NULL) {
62 LV_GC_ROOT(_lv_draw_buf) = lv_mem_alloc(size);
63 lv_mem_assert(LV_GC_ROOT(_lv_draw_buf));
64 return LV_GC_ROOT(_lv_draw_buf);
65 }
66
67 LV_GC_ROOT(_lv_draw_buf) = lv_mem_realloc(LV_GC_ROOT(_lv_draw_buf), size);
68 lv_mem_assert(LV_GC_ROOT(_lv_draw_buf));
69 return LV_GC_ROOT(_lv_draw_buf);
70 }
71
72 /**
73 * Free the draw buffer
74 */
lv_draw_free_buf(void)75 void lv_draw_free_buf(void)
76 {
77 if(LV_GC_ROOT(_lv_draw_buf)) {
78 lv_mem_free(LV_GC_ROOT(_lv_draw_buf));
79 LV_GC_ROOT(_lv_draw_buf) = NULL;
80 draw_buf_size = 0;
81 }
82 }
83
84 #if LV_ANTIALIAS
85
86 /**
87 * Get the opacity of a pixel based it's position in a line segment
88 * @param seg segment length
89 * @param px_id position of of a pixel which opacity should be get [0..seg-1]
90 * @param base_opa the base opacity
91 * @return the opacity of the given pixel
92 */
lv_draw_aa_get_opa(lv_coord_t seg,lv_coord_t px_id,lv_opa_t base_opa)93 lv_opa_t lv_draw_aa_get_opa(lv_coord_t seg, lv_coord_t px_id, lv_opa_t base_opa)
94 {
95 /* How to calculate the opacity of pixels on the edges which makes the anti-aliasing?
96 * For example we have a line like this (y = -0.5 * x):
97 *
98 * | _ _
99 * * * |
100 *
101 * Anti-aliased pixels come to the '*' characters
102 * Calculate what percentage of the pixels should be covered if real line (not rasterized) would
103 * be drawn:
104 * 1. A real line should start on (0;0) and end on (2;1)
105 * 2. So the line intersection coordinates on the first pixel: (0;0) (1;0.5) -> 25% covered
106 * pixel in average
107 * 3. For the second pixel: (1;0.5) (2;1) -> 75% covered pixel in average
108 * 4. The equation: (px_id * 2 + 1) / (segment_width * 2)
109 * segment_width: the line segment which is being anti-aliased (was 2 in the
110 * example) px_id: pixel ID from 0 to (segment_width - 1) result: [0..1] coverage of the pixel
111 */
112
113 /*Accelerate the common segment sizes to avoid division*/
114 static const lv_opa_t seg1[1] = {128};
115 static const lv_opa_t seg2[2] = {64, 192};
116 static const lv_opa_t seg3[3] = {42, 128, 212};
117 static const lv_opa_t seg4[4] = {32, 96, 159, 223};
118 static const lv_opa_t seg5[5] = {26, 76, 128, 178, 230};
119 static const lv_opa_t seg6[6] = {21, 64, 106, 148, 191, 234};
120 static const lv_opa_t seg7[7] = {18, 55, 91, 128, 164, 200, 237};
121 static const lv_opa_t seg8[8] = {16, 48, 80, 112, 143, 175, 207, 239};
122
123 static const lv_opa_t * seg_map[] = {seg1, seg2, seg3, seg4, seg5, seg6, seg7, seg8};
124
125 if(seg == 0)
126 return LV_OPA_TRANSP;
127 else if(seg < 8)
128 return (uint32_t)((uint32_t)seg_map[seg - 1][px_id] * base_opa) >> 8;
129 else {
130 return ((px_id * 2 + 1) * base_opa) / (2 * seg);
131 }
132 }
133
134 /**
135 * Add a vertical anti-aliasing segment (pixels with decreasing opacity)
136 * @param x start point x coordinate
137 * @param y start point y coordinate
138 * @param length length of segment (negative value to start from 0 opacity)
139 * @param mask draw only in this area
140 * @param color color of pixels
141 * @param opa maximum opacity
142 */
lv_draw_aa_ver_seg(lv_coord_t x,lv_coord_t y,lv_coord_t length,const lv_area_t * mask,lv_color_t color,lv_opa_t opa)143 void lv_draw_aa_ver_seg(lv_coord_t x, lv_coord_t y, lv_coord_t length, const lv_area_t * mask, lv_color_t color,
144 lv_opa_t opa)
145 {
146 bool aa_inv = false;
147 if(length < 0) {
148 aa_inv = true;
149 length = -length;
150 }
151
152 lv_coord_t i;
153 for(i = 0; i < length; i++) {
154 lv_opa_t px_opa = lv_draw_aa_get_opa(length, i, opa);
155 if(aa_inv) px_opa = opa - px_opa;
156 lv_draw_px(x, y + i, mask, color, px_opa);
157 }
158 }
159
160 /**
161 * Add a horizontal anti-aliasing segment (pixels with decreasing opacity)
162 * @param x start point x coordinate
163 * @param y start point y coordinate
164 * @param length length of segment (negative value to start from 0 opacity)
165 * @param mask draw only in this area
166 * @param color color of pixels
167 * @param opa maximum opacity
168 */
lv_draw_aa_hor_seg(lv_coord_t x,lv_coord_t y,lv_coord_t length,const lv_area_t * mask,lv_color_t color,lv_opa_t opa)169 void lv_draw_aa_hor_seg(lv_coord_t x, lv_coord_t y, lv_coord_t length, const lv_area_t * mask, lv_color_t color,
170 lv_opa_t opa)
171 {
172 bool aa_inv = false;
173 if(length < 0) {
174 aa_inv = true;
175 length = -length;
176 }
177
178 lv_coord_t i;
179 for(i = 0; i < length; i++) {
180 lv_opa_t px_opa = lv_draw_aa_get_opa(length, i, opa);
181 if(aa_inv) px_opa = opa - px_opa;
182 lv_draw_px(x + i, y, mask, color, px_opa);
183 }
184 }
185
186 #endif
187
188 /**********************
189 * STATIC FUNCTIONS
190 **********************/
191