summaryrefslogtreecommitdiff
path: root/components/gui/src/rtgui_driver.c
blob: fd1523b9a7ace3f9eba596c2649fe7512ca73387 (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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
 * File      : driver.c
 * This file is part of RT-Thread GUI Engine
 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-04     Bernard      first version
 */
#include <rtthread.h>
#include <rtgui/driver.h>
#include <rtgui/region.h>
#include <rtgui/rtgui_system.h>
#include <string.h>

extern const struct rtgui_graphic_driver_ops *rtgui_pixel_device_get_ops(int pixel_format);
extern const struct rtgui_graphic_driver_ops *rtgui_framebuffer_get_ops(int pixel_format);

static struct rtgui_graphic_driver _driver;
static struct rtgui_graphic_driver *_current_driver = &_driver;

#ifdef RTGUI_USING_VFRAMEBUFFER
#ifndef RTGUI_VFB_PIXEL_FMT
#define RTGUI_VFB_PIXEL_FMT     RTGRAPHIC_PIXEL_FORMAT_RGB565
#endif

#include <rtgui/dc.h>
static struct rtgui_graphic_driver _vfb_driver = {0};
static void _graphic_driver_vmode_init(void)
{
    if (_vfb_driver.width != _driver.width || _vfb_driver.height != _driver.height)
    {
        if (_vfb_driver.framebuffer != RT_NULL) rtgui_free((void*)_vfb_driver.framebuffer);

        _vfb_driver.device = RT_NULL;
        _vfb_driver.pixel_format = RTGUI_VFB_PIXEL_FMT;
        _vfb_driver.bits_per_pixel = rtgui_color_get_bits(RTGUI_VFB_PIXEL_FMT);
        _vfb_driver.width  = _driver.width;
        _vfb_driver.height = _driver.height;
        _vfb_driver.pitch  = _driver.width * _UI_BITBYTES(_vfb_driver.bits_per_pixel);
        _vfb_driver.framebuffer = rtgui_malloc(_vfb_driver.height * _vfb_driver.pitch);
        rt_memset(_vfb_driver.framebuffer, 0, _vfb_driver.height * _vfb_driver.pitch);
        _vfb_driver.ext_ops = RT_NULL;
        _vfb_driver.ops = rtgui_framebuffer_get_ops(_vfb_driver.pixel_format);
    }
}

void rtgui_graphic_driver_vmode_enter(void)
{
    rtgui_screen_lock(RT_WAITING_FOREVER);
    _current_driver = &_vfb_driver;
}
RTM_EXPORT(rtgui_graphic_driver_vmode_enter);

void rtgui_graphic_driver_vmode_exit(void)
{
    _current_driver = &_driver;
    rtgui_screen_unlock();
}
RTM_EXPORT(rtgui_graphic_driver_vmode_exit);

rt_bool_t rtgui_graphic_driver_is_vmode(void)
{
    if (_current_driver == &_vfb_driver)
        return RT_TRUE;

    return RT_FALSE;
}
RTM_EXPORT(rtgui_graphic_driver_is_vmode);

struct rtgui_dc*
rtgui_graphic_driver_get_rect_buffer(const struct rtgui_graphic_driver *driver,
                                     struct rtgui_rect *r)
{
    int w, h;
    struct rtgui_dc_buffer *buffer;
    rt_uint8_t *pixel, *dst;
    struct rtgui_rect src, rect;

    /* use virtual framebuffer in default */
    if (driver == RT_NULL) driver = _current_driver;

    if (r == RT_NULL)
    {
        rtgui_graphic_driver_get_rect(driver, &rect);
    }
    else
    {
        rtgui_graphic_driver_get_rect(driver, &src);
        rect = *r;
        rtgui_rect_intersect(&src, &rect);
    }

    w = rtgui_rect_width (rect);
    h = rtgui_rect_height(rect);
    if (!(w && h) || driver->framebuffer == RT_NULL)
        return RT_NULL;

    /* create buffer DC */
    buffer = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(driver->pixel_format, w, h);
    if (buffer == RT_NULL)
        return (struct rtgui_dc*)buffer;

    /* get source pixel */
    pixel = (rt_uint8_t*)driver->framebuffer
            + rect.y1 * driver->pitch
            + rect.x1 * rtgui_color_get_bpp(driver->pixel_format);

    dst = buffer->pixel;

    while (h--)
    {
        rt_memcpy(dst, pixel, buffer->pitch);

        dst += buffer->pitch;
        pixel += driver->pitch;
    }

    return (struct rtgui_dc*)buffer;
}
RTM_EXPORT(rtgui_graphic_driver_get_rect_buffer);
#else
rt_bool_t rtgui_graphic_driver_is_vmode(void)
{
    return RT_FALSE;
}
RTM_EXPORT(rtgui_graphic_driver_is_vmode);
#endif

/* get default driver */
struct rtgui_graphic_driver *rtgui_graphic_driver_get_default(void)
{
    return _current_driver;
}
RTM_EXPORT(rtgui_graphic_driver_get_default);

void rtgui_graphic_driver_get_rect(const struct rtgui_graphic_driver *driver, rtgui_rect_t *rect)
{
    RT_ASSERT(rect != RT_NULL);

    /* use default driver */
    if (driver == RT_NULL)
        driver = _current_driver;

    rect->x1 = rect->y1 = 0;
    rect->x2 = driver->width;
    rect->y2 = driver->height;
}
RTM_EXPORT(rtgui_graphic_driver_get_rect);

rt_err_t rtgui_graphic_set_device(rt_device_t device)
{
    rt_err_t result;
    struct rt_device_graphic_info info;
    struct rtgui_graphic_ext_ops *ext_ops;

    /* get framebuffer address */
    result = rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &info);
    if (result != RT_EOK)
    {
        /* get device information failed */
        return -RT_ERROR;
    }

    /* if the first set graphic device */
    if (_driver.width == 0 || _driver.height == 0)
    {
        rtgui_rect_t rect;

        rtgui_get_mainwin_rect(&rect);
        if (rect.x2 == 0 || rect.y2 == 0)
        {
            rtgui_rect_init(&rect, 0, 0, info.width, info.height);
            /* re-set main-window */
            rtgui_set_mainwin_rect(&rect);
        }
    }

    /* initialize framebuffer driver */
    _driver.device = device;
    _driver.pixel_format = info.pixel_format;
    _driver.bits_per_pixel = info.bits_per_pixel;
    _driver.width = info.width;
    _driver.height = info.height;
    _driver.pitch = _driver.width * _UI_BITBYTES(_driver.bits_per_pixel);
    _driver.framebuffer = info.framebuffer;

    /* get graphic extension operations */
    result = rt_device_control(device, RTGRAPHIC_CTRL_GET_EXT, &ext_ops);
    if (result == RT_EOK)
    {
        _driver.ext_ops = ext_ops;
    }

    if (info.framebuffer != RT_NULL)
    {
        /* is a frame buffer device */
        _driver.ops = rtgui_framebuffer_get_ops(_driver.pixel_format);
    }
    else
    {
        /* is a pixel device */
        _driver.ops = rtgui_pixel_device_get_ops(_driver.pixel_format);
    }

#ifdef RTGUI_USING_HW_CURSOR
    /* set default cursor image */
    rtgui_cursor_set_image(RTGUI_CURSOR_ARROW);
#endif

#ifdef RTGUI_USING_VFRAMEBUFFER
    _graphic_driver_vmode_init();
#endif

    return RT_EOK;
}
RTM_EXPORT(rtgui_graphic_set_device);

/* screen update */
void rtgui_graphic_driver_screen_update(const struct rtgui_graphic_driver *driver, rtgui_rect_t *rect)
{
    if (driver->device != RT_NULL)
    {
        struct rt_device_rect_info rect_info;

        rect_info.x = rect->x1;
        rect_info.y = rect->y1;
        rect_info.width = rect->x2 - rect->x1;
        rect_info.height = rect->y2 - rect->y1;
        rt_device_control(driver->device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
    }
}
RTM_EXPORT(rtgui_graphic_driver_screen_update);

void rtgui_graphic_driver_set_framebuffer(void *fb)
{
    if (_current_driver)
        _current_driver->framebuffer = fb;
    else
        _driver.framebuffer = fb;
}

/* get video frame buffer */
rt_uint8_t *rtgui_graphic_driver_get_framebuffer(const struct rtgui_graphic_driver *driver)
{
    if (driver == RT_NULL) driver = _current_driver;

    return (rt_uint8_t *)driver->framebuffer;
}
RTM_EXPORT(rtgui_graphic_driver_get_framebuffer);

/*
 * FrameBuffer type driver
 */
#define GET_PIXEL(dst, x, y, type)  \
    (type *)((rt_uint8_t*)((dst)->framebuffer) + (y) * (dst)->pitch + (x) * _UI_BITBYTES((dst)->bits_per_pixel))

static void _rgb565_set_pixel(rtgui_color_t *c, int x, int y)
{
    *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565(*c);
}

static void _rgb565_get_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint16_t pixel;

    pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);

    /* get pixel from color */
    *c = rtgui_color_from_565(pixel);
}

static void _rgb565_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    int index;
    rt_uint16_t pixel;
    rt_uint16_t *pixel_ptr;

    /* get pixel from color */
    pixel = rtgui_color_to_565(*c);

    /* get pixel pointer in framebuffer */
    pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);

    for (index = x1; index < x2; index ++)
    {
        *pixel_ptr = pixel;
        pixel_ptr ++;
    }
}

static void _rgb565_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
{
    struct rtgui_graphic_driver *drv;
    rt_uint8_t *dst;
    rt_uint16_t pixel;
    int index;

    drv = rtgui_graphic_get_device();
    pixel = rtgui_color_to_565(*c);
    dst = GET_PIXEL(drv, x, y1, rt_uint8_t);
    for (index = y1; index < y2; index ++)
    {
        *(rt_uint16_t *)dst = pixel;
        dst += drv->pitch;
    }
}

static void _rgb565p_set_pixel(rtgui_color_t *c, int x, int y)
{
    *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t) = rtgui_color_to_565p(*c);
}

static void _rgb565p_get_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint16_t pixel;

    pixel = *GET_PIXEL(rtgui_graphic_get_device(), x, y, rt_uint16_t);

    /* get pixel from color */
    *c = rtgui_color_from_565p(pixel);
}

static void _rgb565p_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    int index;
    rt_uint16_t pixel;
    rt_uint16_t *pixel_ptr;

    /* get pixel from color */
    pixel = rtgui_color_to_565p(*c);

    /* get pixel pointer in framebuffer */
    pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rt_uint16_t);

    for (index = x1; index < x2; index ++)
    {
        *pixel_ptr = pixel;
        pixel_ptr ++;
    }
}

static void _rgb565p_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
{
    struct rtgui_graphic_driver *drv;
    rt_uint8_t *dst;
    rt_uint16_t pixel;
    int index;

    drv = rtgui_graphic_get_device();
    pixel = rtgui_color_to_565p(*c);
    dst = GET_PIXEL(drv, x, y1, rt_uint8_t);
    for (index = y1; index < y2; index ++)
    {
        *(rt_uint16_t *)dst = pixel;
        dst += drv->pitch;
    }
}

static void _argb888_set_pixel(rtgui_color_t *c, int x, int y)
{
    *GET_PIXEL(rtgui_graphic_get_device(), x, y, rtgui_color_t) = *c;
}

static void _argb888_get_pixel(rtgui_color_t *c, int x, int y)
{
    *c = (rtgui_color_t)*GET_PIXEL(rtgui_graphic_get_device(), x, y, rtgui_color_t);
}

static void _argb888_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    int index;
    rtgui_color_t *pixel_ptr;

    /* get pixel pointer in framebuffer */
    pixel_ptr = GET_PIXEL(rtgui_graphic_get_device(), x1, y, rtgui_color_t);

    for (index = x1; index < x2; index++)
    {
        *pixel_ptr = *c;
        pixel_ptr++;
    }
}

static void _argb888_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
{
    struct rtgui_graphic_driver *drv;
    rtgui_color_t *dst;
    int index;

    drv = rtgui_graphic_get_device();
    dst = GET_PIXEL(drv, x, y1, rtgui_color_t);
    for (index = y1; index < y2; index++)
    {
        *dst = *c;
        dst += drv->width;
    }
}

/* draw raw hline */
static void framebuffer_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
{
    struct rtgui_graphic_driver *drv;
    rt_uint8_t *dst;

    drv = rtgui_graphic_get_device();
    dst = GET_PIXEL(drv, x1, y, rt_uint8_t);
    rt_memcpy(dst, pixels,
              (x2 - x1) * _UI_BITBYTES(drv->bits_per_pixel));
}

const struct rtgui_graphic_driver_ops _framebuffer_rgb565_ops =
{
    _rgb565_set_pixel,
    _rgb565_get_pixel,
    _rgb565_draw_hline,
    _rgb565_draw_vline,
    framebuffer_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops _framebuffer_rgb565p_ops =
{
    _rgb565p_set_pixel,
    _rgb565p_get_pixel,
    _rgb565p_draw_hline,
    _rgb565p_draw_vline,
    framebuffer_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops _framebuffer_argb888_ops =
{
    _argb888_set_pixel,
    _argb888_get_pixel,
    _argb888_draw_hline,
    _argb888_draw_vline,
    framebuffer_draw_raw_hline,
};

#define FRAMEBUFFER (drv->framebuffer)
#define MONO_PIXEL(framebuffer, x, y) \
    ((rt_uint8_t**)(framebuffer))[y/8][x]

static void _mono_set_pixel(rtgui_color_t *c, int x, int y)
{
    struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();

    if (*c == white)
        MONO_PIXEL(FRAMEBUFFER, x, y) &= ~(1 << (y % 8));
    else
        MONO_PIXEL(FRAMEBUFFER, x, y) |= (1 << (y % 8));
}

static void _mono_get_pixel(rtgui_color_t *c, int x, int y)
{
    struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();

    if (MONO_PIXEL(FRAMEBUFFER, x, y) & (1 << (y % 8)))
        *c = black;
    else
        *c = white;
}

static void _mono_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    int index;
    struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();

    if (*c == white)
        for (index = x1; index < x2; index ++)
        {
            MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y % 8));
        }
    else
        for (index = x1; index < x2; index ++)
        {
            MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y % 8));
        }
}

static void _mono_draw_vline(rtgui_color_t *c, int x , int y1, int y2)
{
    struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
    int index;

    if (*c == white)
        for (index = y1; index < y2; index ++)
        {
            MONO_PIXEL(FRAMEBUFFER, x, index) &= ~(1 << (index % 8));
        }
    else
        for (index = y1; index < y2; index ++)
        {
            MONO_PIXEL(FRAMEBUFFER, x, index) |= (1 << (index % 8));
        }
}

/* draw raw hline */
static void _mono_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
{
    struct rtgui_graphic_driver *drv = rtgui_graphic_get_device();
    int index;

    for (index = x1; index < x2; index ++)
    {
        if (pixels[index / 8] && (1 << (index % 8)))
            MONO_PIXEL(FRAMEBUFFER, index, y) |= (1 << (y % 8));
        else
            MONO_PIXEL(FRAMEBUFFER, index, y) &= ~(1 << (y % 8));
    }
}

const struct rtgui_graphic_driver_ops _framebuffer_mono_ops =
{
    _mono_set_pixel,
    _mono_get_pixel,
    _mono_draw_hline,
    _mono_draw_vline,
    _mono_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops *rtgui_framebuffer_get_ops(int pixel_format)
{
    switch (pixel_format)
    {
    case RTGRAPHIC_PIXEL_FORMAT_MONO:
        return &_framebuffer_mono_ops;
    case RTGRAPHIC_PIXEL_FORMAT_GRAY4:
        break;
    case RTGRAPHIC_PIXEL_FORMAT_GRAY16:
        break;
    case RTGRAPHIC_PIXEL_FORMAT_RGB565:
        return &_framebuffer_rgb565_ops;
    case RTGRAPHIC_PIXEL_FORMAT_RGB565P:
        return &_framebuffer_rgb565p_ops;
    case RTGRAPHIC_PIXEL_FORMAT_ARGB888:
        return &_framebuffer_argb888_ops;
    default:
        RT_ASSERT(0);
        break;
    }

    return RT_NULL;
}

/*
 * Pixel type driver
 */
#define gfx_device      (rtgui_graphic_get_device()->device)
#define gfx_device_ops  rt_graphix_ops(gfx_device)

static void _pixel_mono_set_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint8_t pixel;

    pixel = rtgui_color_to_mono(*c);
    gfx_device_ops->set_pixel((char *)&pixel, x, y);
}

static void _pixel_rgb565p_set_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint16_t pixel;

    pixel = rtgui_color_to_565p(*c);
    gfx_device_ops->set_pixel((char *)&pixel, x, y);
}

static void _pixel_rgb565_set_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint16_t pixel;

    pixel = rtgui_color_to_565(*c);
    gfx_device_ops->set_pixel((char *)&pixel, x, y);
}

static void _pixel_rgb888_set_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint32_t pixel;

    pixel = rtgui_color_to_888(*c);
    gfx_device_ops->set_pixel((char *)&pixel, x, y);
}

static void _pixel_mono_get_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint8_t pixel;

    gfx_device_ops->get_pixel((char *)&pixel, x, y);
    *c = rtgui_color_from_mono(pixel);
}

static void _pixel_rgb565p_get_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint16_t pixel;

    gfx_device_ops->get_pixel((char *)&pixel, x, y);
    *c = rtgui_color_from_565p(pixel);
}

static void _pixel_rgb565_get_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint16_t pixel;

    gfx_device_ops->get_pixel((char *)&pixel, x, y);
    *c = rtgui_color_from_565(pixel);
}

static void _pixel_rgb888_get_pixel(rtgui_color_t *c, int x, int y)
{
    rt_uint32_t pixel;

    gfx_device_ops->get_pixel((char *)&pixel, x, y);
    *c = rtgui_color_from_888(pixel);
}

static void _pixel_mono_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    rt_uint8_t pixel;

    pixel = rtgui_color_to_mono(*c);
    gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
}

static void _pixel_rgb565p_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    rt_uint16_t pixel;

    pixel = rtgui_color_to_565p(*c);
    gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
}

static void _pixel_rgb565_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    rt_uint16_t pixel;

    pixel = rtgui_color_to_565(*c);
    gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
}

static void _pixel_rgb888_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
{
    rt_uint32_t pixel;

    pixel = rtgui_color_to_888(*c);
    gfx_device_ops->draw_hline((char *)&pixel, x1, x2, y);
}

static void _pixel_mono_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
{
    rt_uint8_t pixel;

    pixel = rtgui_color_to_mono(*c);
    gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
}

static void _pixel_rgb565p_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
{
    rt_uint16_t pixel;

    pixel = rtgui_color_to_565p(*c);
    gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
}

static void _pixel_rgb565_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
{
    rt_uint16_t pixel;

    pixel = rtgui_color_to_565(*c);
    gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
}

static void _pixel_rgb888_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
{
    rt_uint32_t pixel;

    pixel = rtgui_color_to_888(*c);
    gfx_device_ops->draw_vline((char *)&pixel, x, y1, y2);
}

static void _pixel_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
{
    if (x2 > x1)
        gfx_device_ops->blit_line((char *)pixels, x1, y, (x2 - x1));
    else
        gfx_device_ops->blit_line((char *)pixels, x2, y, (x1 - x2));
}

const struct rtgui_graphic_driver_ops _pixel_mono_ops =
{
    _pixel_mono_set_pixel,
    _pixel_mono_get_pixel,
    _pixel_mono_draw_hline,
    _pixel_mono_draw_vline,
    _pixel_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops _pixel_rgb565p_ops =
{
    _pixel_rgb565p_set_pixel,
    _pixel_rgb565p_get_pixel,
    _pixel_rgb565p_draw_hline,
    _pixel_rgb565p_draw_vline,
    _pixel_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops _pixel_rgb565_ops =
{
    _pixel_rgb565_set_pixel,
    _pixel_rgb565_get_pixel,
    _pixel_rgb565_draw_hline,
    _pixel_rgb565_draw_vline,
    _pixel_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops _pixel_rgb888_ops =
{
    _pixel_rgb888_set_pixel,
    _pixel_rgb888_get_pixel,
    _pixel_rgb888_draw_hline,
    _pixel_rgb888_draw_vline,
    _pixel_draw_raw_hline,
};

const struct rtgui_graphic_driver_ops *rtgui_pixel_device_get_ops(int pixel_format)
{
    switch (pixel_format)
    {
    case RTGRAPHIC_PIXEL_FORMAT_MONO:
        return &_pixel_mono_ops;

    case RTGRAPHIC_PIXEL_FORMAT_RGB565:
        return &_pixel_rgb565_ops;

    case RTGRAPHIC_PIXEL_FORMAT_RGB565P:
        return &_pixel_rgb565p_ops;

    case RTGRAPHIC_PIXEL_FORMAT_RGB888:
        return &_pixel_rgb888_ops;
    }

    return RT_NULL;
}

/*
 * Hardware cursor
 */

#ifdef RTGUI_USING_HW_CURSOR
void rtgui_cursor_set_position(rt_uint16_t x, rt_uint16_t y)
{
    rt_uint32_t value;

    if (_current_driver->device != RT_NULL)
    {
        value = (x << 16 | y);
        rt_device_control(_driver.device, RT_DEVICE_CTRL_CURSOR_SET_POSITION, &value);
    }
}

void rtgui_cursor_set_image(enum rtgui_cursor_type type)
{
    rt_uint32_t value;

    if (_current_driver->device != RT_NULL)
    {
        value = type;
        rt_device_control(_driver.device, RT_DEVICE_CTRL_CURSOR_SET_TYPE, &value);
    }
};
#endif