-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdcs_lcd_draw.c
More file actions
275 lines (221 loc) · 7.97 KB
/
dcs_lcd_draw.c
File metadata and controls
275 lines (221 loc) · 7.97 KB
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
/*
* This file is part of AtomGL.
*
* Copyright 2020-2026 Davide Bettio <davide@uninstall.it>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "dcs_lcd_draw.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <utils.h>
#include "dcs_lcd_color.h"
#include "font_data.h"
int dcs_lcd_find_max_line_len(const struct DCSLCDScreen *screen,
BaseDisplayItem items[], size_t items_len, int xpos, int ypos)
{
int line_len = screen->w - xpos;
for (size_t i = 0; i < items_len; i++) {
BaseDisplayItem *item = &items[i];
if ((xpos < item->x) && (ypos >= item->y) && (ypos < item->y + item->height)) {
int len_to_item = item->x - xpos;
line_len = (line_len > len_to_item) ? len_to_item : line_len;
}
}
return line_len;
}
int dcs_lcd_draw_image_x(const struct DCSLCDScreen *screen,
int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
uint16_t bgcolor = 0;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor = rgba8888_color_to_rgb565(item->brcolor);
visible_bg = true;
} else {
visible_bg = false;
}
int width = item->width;
const char *data = item->data.image_data.pix;
int drawn_pixels = 0;
uint32_t *pixels = ((uint32_t *) data) + (ypos - y) * width + (xpos - x);
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint32_t img_pixel = READ_32_UNALIGNED(pixels);
uint8_t alpha = rgba8888_get_alpha(img_pixel);
if (alpha == 0xFF) {
uint16_t color = uint32_color_to_surface(img_pixel);
pixmem16[drawn_pixels] = color;
} else if (visible_bg) {
uint16_t color = rgba8888_color_to_rgb565(img_pixel);
uint16_t blended = alpha_blend_rgb565(color, bgcolor, alpha);
pixmem16[drawn_pixels] = rgb565_color_to_surface(blended);
} else {
return drawn_pixels;
}
drawn_pixels++;
pixels++;
}
return drawn_pixels;
}
int dcs_lcd_draw_rect_x(const struct DCSLCDScreen *screen,
int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int width = item->width;
uint16_t color = uint32_color_to_surface(item->brcolor);
int drawn_pixels = 0;
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
pixmem16[drawn_pixels] = color;
drawn_pixels++;
}
return drawn_pixels;
}
int dcs_lcd_draw_text_x(const struct DCSLCDScreen *screen,
int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
uint16_t fgcolor = uint32_color_to_surface(item->data.text_data.fgcolor);
uint16_t bgcolor;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor = uint32_color_to_surface(item->brcolor);
visible_bg = true;
} else {
visible_bg = false;
}
char *text = (char *) item->data.text_data.text;
int width = item->width;
int drawn_pixels = 0;
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
int char_index = j / CHAR_WIDTH;
char c = text[char_index];
unsigned const char *glyph = fontdata + ((unsigned char) c) * 16;
unsigned char row = glyph[ypos - y];
bool opaque;
int k = j % CHAR_WIDTH;
if (row & (1 << (7 - k))) {
opaque = true;
} else {
opaque = false;
}
if (opaque) {
pixmem16[drawn_pixels] = fgcolor;
} else if (visible_bg) {
pixmem16[drawn_pixels] = bgcolor;
} else {
return drawn_pixels;
}
drawn_pixels++;
}
return drawn_pixels;
}
int dcs_lcd_draw_scaled_cropped_img_x(const struct DCSLCDScreen *screen,
int xpos, int ypos, int max_line_len, BaseDisplayItem *item)
{
int x = item->x;
int y = item->y;
uint16_t bgcolor = 0;
bool visible_bg;
if (item->brcolor != 0) {
bgcolor = rgba8888_color_to_rgb565(item->brcolor);
visible_bg = true;
} else {
visible_bg = false;
}
int width = item->width;
const char *data = item->data.image_data_with_size.pix;
int drawn_pixels = 0;
int y_scale = item->y_scale;
int x_scale = item->x_scale;
int img_width = item->data.image_data_with_size.width;
int source_x = item->source_x;
int source_y = item->source_y;
uint32_t *pixels = ((uint32_t *) data) + (source_y + ((ypos - y) / y_scale)) * img_width + source_x + ((xpos - x) / x_scale);
uint16_t *pixmem16 = (uint16_t *) (((uint8_t *) screen->pixels) + xpos * sizeof(uint16_t));
if (source_x + (width / x_scale) > img_width) {
width = (img_width - source_x) * x_scale;
}
if (width > xpos - x + max_line_len) {
width = xpos - x + max_line_len;
}
for (int j = xpos - x; j < width; j++) {
uint32_t img_pixel = READ_32_UNALIGNED(pixels);
uint8_t alpha = rgba8888_get_alpha(img_pixel);
if (alpha == 0xFF) {
uint16_t color = uint32_color_to_surface(img_pixel);
pixmem16[drawn_pixels] = color;
} else if (visible_bg) {
uint16_t color = rgba8888_color_to_rgb565(img_pixel);
uint16_t blended = alpha_blend_rgb565(color, bgcolor, alpha);
pixmem16[drawn_pixels] = rgb565_color_to_surface(blended);
} else {
return drawn_pixels;
}
drawn_pixels++;
pixels = ((uint32_t *) data) + (source_y + ((ypos - y) / y_scale)) * img_width + source_x + ((j + 1) / x_scale);
}
return drawn_pixels;
}
int dcs_lcd_draw_x(const struct DCSLCDScreen *screen,
int xpos, int ypos, BaseDisplayItem items[], size_t items_len)
{
bool below = false;
for (size_t i = 0; i < items_len; i++) {
BaseDisplayItem *item = &items[i];
if ((xpos < item->x) || (xpos >= item->x + item->width) || (ypos < item->y) || (ypos >= item->y + item->height)) {
continue;
}
int max_line_len = below ? 1 : dcs_lcd_find_max_line_len(screen, items, i, xpos, ypos);
int drawn_pixels = 0;
switch (items[i].primitive) {
case PrimitiveImage:
drawn_pixels = dcs_lcd_draw_image_x(screen, xpos, ypos, max_line_len, item);
break;
case PrimitiveRect:
drawn_pixels = dcs_lcd_draw_rect_x(screen, xpos, ypos, max_line_len, item);
break;
case PrimitiveScaledCroppedImage:
drawn_pixels = dcs_lcd_draw_scaled_cropped_img_x(screen, xpos, ypos, max_line_len, item);
break;
case PrimitiveText:
drawn_pixels = dcs_lcd_draw_text_x(screen, xpos, ypos, max_line_len, item);
break;
default: {
fprintf(stderr, "unexpected display list command.\n");
}
}
if (drawn_pixels != 0) {
return drawn_pixels;
}
below = true;
}
return 1;
}