summaryrefslogtreecommitdiff
path: root/components/drivers/graphic/graphic_primary.c
blob: 411931196faa829c9707ad3988053b8aeaf85fcd (plain)
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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "graphic.primary"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

static struct rt_graphic_device *primary_gdev = RT_NULL;

#define framebuffer_drift(plane, x, y, bpp) \
    ((plane)->framebuffer + (x) * ((bpp) / 8) + (y) * (plane)->line_length)

#define fixup_dir(a1, a2) \
    if ((a1) > (a2)) { (a1) ^= (a2); (a2) ^= (a1); (a1) ^= (a2); }

static void graphic_primary_set_pixel_8bit(const char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint8_t *)framebuffer_drift(plane, x, y, 8) = *(rt_uint8_t *)pixel;
}

static void graphic_primary_get_pixel_8bit(char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint8_t *)pixel = *(rt_uint8_t *)framebuffer_drift(plane, x, y, 8);
}

static void graphic_primary_draw_hline_8bit(const char *pixel, int x1, int x2, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(x1, x2);
    rt_memset(framebuffer_drift(plane, x1, y, 8), *(rt_uint8_t *)pixel, x2 - x1);
}

static void graphic_primary_draw_vline_8bit(const char *pixel, int x, int y1, int y2)
{
    rt_uint8_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(y1, y2);
    fb = framebuffer_drift(plane, x, y1, 8);

    for (; y1 < y2; ++y1)
    {
        *fb = *(rt_uint8_t *)pixel;
        fb += plane->line_length;
    }
}

static void graphic_primary_blit_line_8bit(const char *pixel, int x, int y, rt_size_t size)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    rt_memcpy(framebuffer_drift(plane, x, y, 8), pixel, size);
}

static struct rt_device_graphic_ops graphic_primary_8bit_ops =
{
    .set_pixel = graphic_primary_set_pixel_8bit,
    .get_pixel = graphic_primary_get_pixel_8bit,
    .draw_hline = graphic_primary_draw_hline_8bit,
    .draw_vline = graphic_primary_draw_vline_8bit,
    .blit_line = graphic_primary_blit_line_8bit,
};

static void graphic_primary_set_pixel_16bit(const char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint16_t *)framebuffer_drift(plane, x, y, 16) = *(rt_uint16_t *)pixel;
}

static void graphic_primary_get_pixel_16bit(char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint16_t *)pixel = *(rt_uint16_t *)framebuffer_drift(plane, x, y, 16);
}

static void graphic_primary_draw_hline_16bit(const char *pixel, int x1, int x2, int y)
{
    rt_uint16_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(x1, x2);

    fb = framebuffer_drift(plane, x1, y, 16);

    for (; x1 < x2; ++x1)
    {
        *fb++ = *(rt_uint16_t *)pixel;
    }
}

static void graphic_primary_draw_vline_16bit(const char *pixel, int x, int y1, int y2)
{
    rt_uint16_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(y1, y2);

    fb = framebuffer_drift(plane, x, y1, 16);

    for (; y1 < y2; ++y1)
    {
        *fb = *(rt_uint16_t *)pixel;
        fb = (void *)fb + plane->line_length;
    }
}

static void graphic_primary_blit_line_16bit(const char *pixel, int x, int y, rt_size_t size)
{
    rt_uint16_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fb = framebuffer_drift(plane, x, y, 16);

    while (size --> 0)
    {
        *fb++ = *(rt_uint16_t *)pixel;
        pixel += 2;
    }
}

static struct rt_device_graphic_ops graphic_primary_16bit_ops =
{
    .set_pixel = graphic_primary_set_pixel_16bit,
    .get_pixel = graphic_primary_get_pixel_16bit,
    .draw_hline = graphic_primary_draw_hline_16bit,
    .draw_vline = graphic_primary_draw_vline_16bit,
    .blit_line = graphic_primary_blit_line_16bit,
};

static void graphic_primary_set_pixel_24bit(const char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    rt_memcpy(framebuffer_drift(plane, x, y, 24), pixel, 3);
}

static void graphic_primary_get_pixel_24bit(char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    rt_memcpy(pixel, framebuffer_drift(plane, x, y, 24), 3);
}

static void graphic_primary_draw_hline_24bit(const char *pixel, int x1, int x2, int y)
{
    rt_uint8_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(x1, x2);

    fb = framebuffer_drift(plane, x1, y, 24);

    for (; x1 < x2; ++x1)
    {
        *fb++ = ((rt_uint8_t *)pixel)[0];
        *fb++ = ((rt_uint8_t *)pixel)[1];
        *fb++ = ((rt_uint8_t *)pixel)[2];
    }
}

static void graphic_primary_draw_vline_24bit(const char *pixel, int x, int y1, int y2)
{
    rt_uint8_t *fb;
    rt_size_t xlate;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(y1, y2);

    fb = framebuffer_drift(plane, x, y1, 24);
    xlate = plane->line_length - 3;

    for (; y1 < y2; ++y1)
    {
        *fb++ = ((rt_uint8_t *)pixel)[0];
        *fb++ = ((rt_uint8_t *)pixel)[1];
        *fb++ = ((rt_uint8_t *)pixel)[2];

        fb = (void *)fb + xlate;
    }
}

static void graphic_primary_blit_line_24bit(const char *pixel, int x, int y, rt_size_t size)
{
    rt_uint8_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fb = framebuffer_drift(plane, x, y, 24);

    while (size --> 0)
    {
        *fb++ = *(rt_uint8_t *)pixel++;
        *fb++ = *(rt_uint8_t *)pixel++;
        *fb++ = *(rt_uint8_t *)pixel++;
    }
}

static struct rt_device_graphic_ops graphic_primary_24bit_ops =
{
    .set_pixel = graphic_primary_set_pixel_24bit,
    .get_pixel = graphic_primary_get_pixel_24bit,
    .draw_hline = graphic_primary_draw_hline_24bit,
    .draw_vline = graphic_primary_draw_vline_24bit,
    .blit_line = graphic_primary_blit_line_24bit,
};

static void graphic_primary_set_pixel_32bit(const char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint32_t *)framebuffer_drift(plane, x, y, 32) = *(rt_uint32_t *)pixel;
}

static void graphic_primary_get_pixel_32bit(char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint32_t *)pixel = *(rt_uint32_t *)framebuffer_drift(plane, x, y, 32);
}

static void graphic_primary_draw_hline_32bit(const char *pixel, int x1, int x2, int y)
{
    rt_uint32_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(x1, x2);

    fb = framebuffer_drift(plane, x1, y, 32);

    for (; x1 < x2; ++x1)
    {
        *fb++ = *(rt_uint32_t *)pixel;
    }
}

static void graphic_primary_draw_vline_32bit(const char *pixel, int x, int y1, int y2)
{
    rt_uint32_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(y1, y2);

    fb = framebuffer_drift(plane, x, y1, 32);

    for (; y1 < y2; ++y1)
    {
        *fb = *(rt_uint32_t *)pixel;
        fb = (void *)fb + plane->line_length;
    }
}

static void graphic_primary_blit_line_32bit(const char *pixel, int x, int y, rt_size_t size)
{
    rt_uint32_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fb = framebuffer_drift(plane, x, y, 32);

    while (size --> 0)
    {
        *fb++ = *(rt_uint32_t *)pixel;
        pixel += 4;
    }
}

static struct rt_device_graphic_ops graphic_primary_32bit_ops =
{
    .set_pixel = graphic_primary_set_pixel_32bit,
    .get_pixel = graphic_primary_get_pixel_32bit,
    .draw_hline = graphic_primary_draw_hline_32bit,
    .draw_vline = graphic_primary_draw_vline_32bit,
    .blit_line = graphic_primary_blit_line_32bit,
};

static void graphic_primary_set_pixel_64bit(const char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint64_t *)framebuffer_drift(plane, x, y, 64) = *(rt_uint64_t *)pixel;
}

static void graphic_primary_get_pixel_64bit(char *pixel, int x, int y)
{
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    *(rt_uint64_t *)pixel = *(rt_uint64_t *)framebuffer_drift(plane, x, y, 64);
}

static void graphic_primary_draw_hline_64bit(const char *pixel, int x1, int x2, int y)
{
    rt_uint64_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(x1, x2);

    fb = framebuffer_drift(plane, x1, y, 64);

    for (; x1 < x2; ++x1)
    {
        *fb++ = *(rt_uint64_t *)pixel;
    }
}

static void graphic_primary_draw_vline_64bit(const char *pixel, int x, int y1, int y2)
{
    rt_uint64_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fixup_dir(y1, y2);

    fb = framebuffer_drift(plane, x, y1, 64);

    for (; y1 < y2; ++y1)
    {
        *fb = *(rt_uint64_t *)pixel;
        fb = (void *)fb + plane->line_length;
    }
}

static void graphic_primary_blit_line_64bit(const char *pixel, int x, int y, rt_size_t size)
{
    rt_uint64_t *fb;
    struct rt_graphic_plane *plane = primary_gdev->primary_plane;

    fb = framebuffer_drift(plane, x, y, 64);

    while (size --> 0)
    {
        *fb++ = *(rt_uint64_t *)pixel;
        pixel += 8;
    }
}

static struct rt_device_graphic_ops graphic_primary_64bit_ops =
{
    .set_pixel = graphic_primary_set_pixel_64bit,
    .get_pixel = graphic_primary_get_pixel_64bit,
    .draw_hline = graphic_primary_draw_hline_64bit,
    .draw_vline = graphic_primary_draw_vline_64bit,
    .blit_line = graphic_primary_blit_line_64bit,
};

struct rt_device_graphic_ops *rt_graphic_device_switch_primary(struct rt_graphic_device *gdev)
{
    primary_gdev = gdev;

    RT_ASSERT(primary_gdev != RT_NULL);
    RT_ASSERT(primary_gdev->primary_plane != RT_NULL);

    switch (rt_graphic_mode_bpp(primary_gdev->primary_plane->mode))
    {
    case 32: return &graphic_primary_32bit_ops;
    case 24: return &graphic_primary_24bit_ops;
    case 16: return &graphic_primary_16bit_ops;
    case 64: return &graphic_primary_64bit_ops;
    case 8:  return &graphic_primary_8bit_ops;
    default:
        LOG_E("What the fuck format(%u)", primary_gdev->primary_plane->mode);
        RT_ASSERT(0);
        break;
    }

    return RT_NULL;
}