summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/osd.c
blob: d2e0fe3e50999f21735c861bfef7b4bf99cfc12d (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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Floating on-screen display
*
* Copyright (C) 2012 Michael Sevakis
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "plugin.h"
#include "grey.h"
#include "osd.h"

#if 1
#undef DEBUGF
#define DEBUGF(...)
#endif

#if defined(SIMULATOR) && LCD_DEPTH < 4
/* Sim isn't using --ffunction-sections thus greylib references will happen
   here even if not using this with greylib on a grayscale display, which
   demands that a struct _grey_info exist. */
#ifndef _WIN32
__attribute__((weak))
#endif /* _WIN32 */
    struct _grey_info _grey_info;
#endif /* defined(SIMULATOR) && LCD_DEPTH < 4 */

/* At this time: assumes use of the default viewport for normal drawing */

/* If multiple OSD's are wanted, could convert to caller-allocated */
struct osd
{
    enum osd_status
    {
        OSD_DISABLED = 0,       /* Disabled entirely */
        OSD_HIDDEN,             /* Hidden from view */
        OSD_VISIBLE,            /* Visible on screen */
        OSD_ERASED,             /* Erased in preparation for regular drawing */
    } status;                   /* View status */
    struct viewport vp;         /* Clipping viewport */
    int lcd_bitmap_stride;      /* Stride of LCD bitmap */
    void *lcd_bitmap_data;      /* Backbuffer framebuffer data */
    int back_bitmap_stride;     /* Stride of backbuffer bitmap */
    void *back_bitmap_data;     /* LCD framebuffer data */
    int maxwidth;               /* How wide may it be at most? */
    int maxheight;              /* How high may it be at most? */
    long timeout;               /* Current popup stay duration */
    long hide_tick;             /* Tick when it should be hidden */
    osd_draw_cb_fn_t draw_cb;   /* Draw update callback */
    /* Functions to factilitate interface compatibility of OSD types */
    void * (*init_buffers)(struct osd *osd, unsigned flags, void *buf,
                           size_t *bufsize);
    void (*set_viewport_pos)(struct viewport *vp, int x, int y, int width,
                             int height);
    void (*lcd_update)(void);
    void (*lcd_update_rect)(int x, int y, int width, int height);
    void (*lcd_set_viewport)(struct viewport *vp);
    void (*lcd_set_framebuffer)(void *buf);
    void (*lcd_framebuffer_set_pos)(int x, int y, int width, int height);
    void (*lcd_bitmap_part)(const void *src, int src_x, int src_y,
                            int stride, int x, int y, int width, int height);
};

static struct osd native_osd;
#if LCD_DEPTH < 4
static struct osd grey_osd;
#endif

/* Framebuffer allocation macros */
#if LCD_DEPTH == 1
#  if LCD_PIXELFORMAT == HORIZONTAL_PACKING
#    define _OSD_WIDTH2BYTES(w)  (((w)+7)/8)
#    define _OSD_BYTES2WIDTH(b)  ((b)*8)
#  elif LCD_PIXELFORMAT == VERTICAL_PACKING
#    define _OSD_HEIGHT2BYTES(h) (((h)+7)/8)
#    define _OSD_BYTES2HEIGHT(b) ((b)*8)
#  else
#    error Unknown 1-bit format; please define macros
#  endif /* LCD_PIXELFORMAT */
#elif LCD_DEPTH == 2
#  if LCD_PIXELFORMAT == HORIZONTAL_PACKING
#    define _OSD_WIDTH2BYTES(w)  (((w)+3)/4)
#    define _OSD_BYTES2WIDTH(b)  ((b)*4)
#  elif LCD_PIXELFORMAT == VERTICAL_PACKING
#    define _OSD_HEIGHT2BYTES(h) (((h)+3)/4)
#    define _OSD_BYTES2HEIGHT(b) ((b)*4)
#  elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
#    define _OSD_HEIGHT2BYTES(h) (((h)+7)/8*2)
#    define _OSD_BYTES2HEIGHT(b) ((b)/2*8)
#  else
#    error Unknown 2-bit format; please define macros
#  endif /* LCD_PIXELFORMAT */
#elif LCD_DEPTH == 16
#  if defined(LCD_STRIDEFORMAT) && LCD_STRIDEFORMAT == VERTICAL_STRIDE
#    define _OSD_HEIGHT2BYTES(h)   ((h)*2)
#    define _OSD_BYTES2HEIGHT(b)   ((b)/2)
#  else /* !defined(LCD_STRIDEFORMAT) || LCD_STRIDEFORMAT != VERTICAL_STRIDE */
#    define _OSD_WIDTH2BYTES(w)    ((w)*2)
#    define _OSD_BYTES2WIDTH(b)    ((b)/2)
#  endif /* end stride type selection */
#elif LCD_DEPTH == 24
#    define _OSD_WIDTH2BYTES(w)    ((w)*3)
#    define _OSD_BYTES2WIDTH(b)    ((b)/3)
#elif LCD_DEPTH == 32
#    define _OSD_WIDTH2BYTES(w)    ((w)*4)
#    define _OSD_BYTES2WIDTH(b)    ((b)/4)
#else /* other LCD depth */
#  error Unknown LCD depth; please define macros
#endif /* LCD_DEPTH */
/* Set defaults if not defined differently */
#ifndef _OSD_WIDTH2BYTES
#  define _OSD_WIDTH2BYTES(w)    (w)
#endif
#ifndef _OSD_BYTES2WIDTH
#  define _OSD_BYTES2WIDTH(b)    (b)
#endif
#ifndef _OSD_HEIGHT2BYTES
#  define _OSD_HEIGHT2BYTES(h)   (h)
#endif
#ifndef _OSD_BYTES2HEIGHT
#  define _OSD_BYTES2HEIGHT(b)   (b)
#endif
#ifndef _OSD_BUFSIZE
#  define _OSD_BUFSIZE(w, h)     (_OSD_WIDTH2BYTES(w)*_OSD_HEIGHT2BYTES(h))
#endif

static void _osd_destroy(struct osd *osd);
static bool _osd_show(struct osd *osd, unsigned flags);


/** Native LCD routines **/

/* Create a bitmap framebuffer from a buffer */
static void * _osd_lcd_init_buffers(struct osd *osd, unsigned flags,
                                    void *buf, size_t *bufsize)
{
    /* Used as dest, the LCD functions cannot deal with alternate
       strides as of now - the stride guides the calulations. If
       that is no longer the case, then width or height can be
       used instead (and less memory needed for a small surface!).
       IOW: crappiness means one dimension is non-negotiable.
     */
    DEBUGF("OSD: in(buf=%p bufsize=%lu)\n", buf,
           (unsigned long)*bufsize);

    rb->viewport_set_fullscreen(&osd->vp, SCREEN_MAIN);

#if defined(LCD_STRIDEFORMAT) && LCD_STRIDEFORMAT == VERTICAL_STRIDE
    int colbytes = _OSD_HEIGHT2BYTES(LCD_HEIGHT);
    int bytecols = *bufsize / colbytes;
    int w = _OSD_BYTES2WIDTH(bytecols);
    int h = _OSD_BYTES2HEIGHT(colbytes);

    if (flags & OSD_INIT_MAJOR_HEIGHT)
    {
        if (w == 0 || ((flags & OSD_INIT_MINOR_MIN) && w < osd->maxwidth))
        {
            DEBUGF("OSD: not enough buffer\n");
            return NULL; /* not enough buffer */
        }

        if ((flags & OSD_INIT_MINOR_MAX) && w > osd->maxwidth)
            w = osd->maxwidth;
    }
    else /* OSD_INIT_MAJOR_WIDTH implied */
    {
        if (w == 0 || w < osd->maxwidth)
        {
            DEBUGF("OSD: not enough buffer\n");
            return NULL; /* not enough buffer */
        }
        else if (w > osd->maxwidth)
        {
            w = osd->maxwidth;
        }
    }

    w = _OSD_BYTES2WIDTH(_OSD_WIDTH2BYTES(w));
    osd->lcd_bitmap_stride = _OSD_BYTES2HEIGHT(_OSD_HEIGHT2BYTES(LCD_HEIGHT));
    osd->back_bitmap_stride = h;
#else /* !defined(LCD_STRIDEFORMAT) || LCD_STRIDEFORMAT != VERTICAL_STRIDE */
    int rowbytes = _OSD_WIDTH2BYTES(LCD_WIDTH);
    int byterows = *bufsize / rowbytes;
    int w = _OSD_BYTES2WIDTH(rowbytes);
    int h = _OSD_BYTES2HEIGHT(byterows);

    if (flags & OSD_INIT_MAJOR_HEIGHT)
    {
        if (h == 0 || h < osd->maxheight)
        {
            DEBUGF("OSD: not enough buffer\n");
            return NULL;
        }
        else if (h > osd->maxheight)
        {
            h = osd->maxheight;
        }
    }
    else /* OSD_INIT_MAJOR_WIDTH implied */
    {
        if (h == 0 || ((flags & OSD_INIT_MINOR_MIN) && h < osd->maxheight))
        {
            DEBUGF("OSD: not enough buffer\n");
            return NULL;
        }

        if ((flags & OSD_INIT_MINOR_MAX) && h > osd->maxheight)
            h = osd->maxheight;
    }

    h = _OSD_BYTES2HEIGHT(_OSD_HEIGHT2BYTES(h));
    osd->lcd_bitmap_stride = _OSD_BYTES2WIDTH(_OSD_WIDTH2BYTES(LCD_WIDTH));
    osd->back_bitmap_stride = w;
#endif /* end stride type selection */

    osd->lcd_bitmap_data = (void *)rb->lcd_framebuffer;
    osd->back_bitmap_data = buf;

    osd->maxwidth = w;
    osd->maxheight = h;
    *bufsize = _OSD_BUFSIZE(w, h);

    DEBUGF("OSD: addr(fb=%p bb=%p)\n", osd->lcd_bitmap_data,
           osd->back_bitmap_data);
    DEBUGF("OSD: w=%d h=%d bufsz=%lu\n", w, h, (unsigned long)*bufsize);

    return buf;
}

/* Set viewport coordinates */
static void _osd_lcd_viewport_set_pos(
    struct viewport *vp, int x, int y, int width, int height)
{
    vp->x = x;
    vp->y = y;
    vp->width = width;
    vp->height = height;
}


#if LCD_DEPTH < 4
/** Greylib LCD routines **/

/* Create a greylib bitmap framebuffer from a buffer */
static void * _osd_grey_init_buffers(struct osd *osd, unsigned flags,
                                     void *buf, size_t *bufsize)
{
    int w, h;

    DEBUGF("OSD (grey): in(buf=%p bufsize=%lu)\n", buf,
           (unsigned long)*bufsize);

    grey_viewport_set_fullscreen(&osd->vp, SCREEN_MAIN);

    if (flags & OSD_INIT_MAJOR_HEIGHT)
    {
        h = osd->maxheight;
        w = *bufsize / h;

        if (w == 0 || ((flags & OSD_INIT_MINOR_MIN) && w < osd->maxwidth))
        {
            DEBUGF("OSD (grey): Not enough buffer\n");
            return NULL;
        }

        if ((flags & OSD_INIT_MINOR_MAX) && w > osd->maxwidth)
            w = osd->maxwidth;
    }
    else /* OSD_INIT_MAJOR_WIDTH implied */
    {
        w = osd->maxwidth;
        h = *bufsize / w;

        if (h == 0 || ((flags & OSD_INIT_MINOR_MIN) && h < osd->maxheight))
        {
            DEBUGF("OSD (grey): Not enough buffer\n");
            return NULL;
        }

        if ((flags & OSD_INIT_MINOR_MAX) && h > osd->maxheight)
            h = osd->maxheight;
    }

    /* Have to peek into _grey_info a bit */
    osd->lcd_bitmap_stride = _grey_info.width;
    osd->lcd_bitmap_data = _grey_info.buffer;
    osd->back_bitmap_stride = w;
    osd->back_bitmap_data = buf;

    osd->maxwidth = w;
    osd->maxheight = h;
    *bufsize = w * h;

    DEBUGF("OSD (grey): addr(fb=%p bb=%p)\n", osd->lcd_bitmap_data,
           osd->back_bitmap_data);
    DEBUGF("OSD (grey): w=%d h=%d bufsz=%lu\n", w, h, (unsigned long)*bufsize);

    return buf;
}
#endif /* LCD_DEPTH < 4*/


/** Common LCD routines **/

/* Draw the OSD image portion using the callback */
static void _osd_draw_osd_rect(struct osd *osd, int x, int y,
                               int width, int height)
{
    osd->lcd_set_viewport(&osd->vp);
    osd->draw_cb(x, y, width, height);
    osd->lcd_set_viewport(NULL);
}

/* Draw the OSD image using the callback */
static void _osd_draw_osd(struct osd *osd)
{
    _osd_draw_osd_rect(osd, 0, 0, osd->vp.width, osd->vp.height);
}

static void _osd_update_viewport(struct osd *osd)
{
    osd->lcd_update_rect(osd->vp.x, osd->vp.y, osd->vp.width,
                         osd->vp.height);
}

/* Sync the backbuffer to the framebuffer image */
static void _osd_update_back_buffer(struct osd *osd)
{
    /* Assume it's starting with default viewport for now */
    osd->lcd_set_framebuffer(osd->back_bitmap_data);
#if LCD_DEPTH < 4
    if (osd->lcd_framebuffer_set_pos)
        osd->lcd_framebuffer_set_pos(0, 0, osd->maxwidth, osd->maxheight);
#endif /* LCD_DEPTH < 4 */
    osd->lcd_bitmap_part(osd->lcd_bitmap_data, osd->vp.x, osd->vp.y,
                         osd->lcd_bitmap_stride, 0, 0, osd->vp.width,
                         osd->vp.height);
    /* Assume it was on default framebuffer for now */
    osd->lcd_set_framebuffer(NULL);
}

/* Erase the OSD to restore the framebuffer image */
static void _osd_erase_osd(struct osd *osd)
{
    osd->lcd_bitmap_part(osd->back_bitmap_data, 0, 0, osd->back_bitmap_stride,
                         osd->vp.x, osd->vp.y, osd->vp.width, osd->vp.height);
}

/* Initialized the OSD and set its backbuffer */
static bool _osd_init(struct osd *osd, unsigned flags, void *backbuf,
                      size_t backbuf_size, osd_draw_cb_fn_t draw_cb,
                      int *width, int *height, size_t *bufused)
{
    _osd_destroy(osd);

    if (!draw_cb)
        return false;

    if (!backbuf)
        return false;

    void *backbuf_orig = backbuf; /* Save in case of ptr advance */
    ALIGN_BUFFER(backbuf, backbuf_size, MAX(FB_DATA_SZ, 4));

    if (!backbuf_size)
        return false;

    if (flags & OSD_INIT_MAJOR_HEIGHT)
    {
        if (!height || *height <= 0)
            return false;

        if ((flags & (OSD_INIT_MINOR_MIN | OSD_INIT_MINOR_MAX)) &&
            (!width || *width <= 0))
        {
            return false;
        }
    }
    else
    {
        if (!width || *width <= 0)
            return false;

        if ((flags & (OSD_INIT_MINOR_MIN | OSD_INIT_MINOR_MAX)) &&
            (!height || *height <= 0))
        {
            return false;
        }
    }

    /* Store requested sizes in max(width|height) */
    if (width)
        osd->maxwidth = *width;
    else
        osd->maxwidth = LCD_WIDTH;

    if (height)
        osd->maxheight = *height;
    else
        osd->maxheight = LCD_HEIGHT;

    if (!osd->init_buffers(osd, flags, backbuf, &backbuf_size))
    {
        osd->maxwidth = osd->maxheight = 0;
        return false;
    }

    osd->draw_cb = draw_cb;

    if (bufused)
        *bufused = backbuf_size + (backbuf_orig - backbuf);

    if (width)
        *width = osd->maxwidth;

    if (height)
        *height = osd->maxheight;

    /* Set the default position to the whole thing */
    osd->set_viewport_pos(&osd->vp, 0, 0, osd->maxwidth, osd->maxheight);

    osd->status = OSD_HIDDEN; /* Ready when you are */

    return true;
}

static void _osd_destroy(struct osd *osd)
{
    _osd_show(osd, OSD_HIDE);

    /* Set to essential defaults */
    osd->status = OSD_DISABLED;
    osd->set_viewport_pos(&osd->vp, 0, 0, 0, 0);
    osd->maxwidth = osd->maxheight = 0;
    osd->timeout = 0;
}

/* Redraw the entire OSD */
static bool _osd_update(struct osd *osd)
{
    if (osd->status != OSD_VISIBLE)
        return false;

    _osd_draw_osd(osd);
    _osd_update_viewport(osd);
    return true;
}

/* Show/Hide the OSD on screen */
static bool _osd_show(struct osd *osd, unsigned flags)
{
    if (flags & OSD_SHOW)
    {
        switch (osd->status)
        {
        case OSD_DISABLED:
            break; /* No change */

        case OSD_HIDDEN:
            _osd_update_back_buffer(osd);
            osd->status = OSD_VISIBLE;
            _osd_update(osd);
            osd->hide_tick = *rb->current_tick + osd->timeout;
            break;

        case OSD_VISIBLE:
            if (flags & OSD_UPDATENOW)
                _osd_update(osd);
            /* Fall-through */
        case OSD_ERASED:
            osd->hide_tick = *rb->current_tick + osd->timeout;
            return true;
        }
    }
    else
    {
        switch (osd->status)
        {
        case OSD_DISABLED:
        case OSD_HIDDEN:
            break;

        case OSD_VISIBLE:
            _osd_erase_osd(osd);
            _osd_update_viewport(osd);
        /* Fall-through */
        case OSD_ERASED:
            osd->status = OSD_HIDDEN;
            return true;
        }
    }

    return false;
}

/* Redraw part of the OSD (viewport-relative coordinates) */
static bool _osd_update_rect(struct osd *osd, int x, int y, int width,
                             int height)
{
    if (osd->status != OSD_VISIBLE)
        return false;

    _osd_draw_osd_rect(osd, x, y, width, height);

    int vp_x = osd->vp.x;
    int vp_w = osd->vp.width;

    if (x + width > vp_w)
        width = vp_w - x;

    if (x < 0)
    {
        width += x;
        x = 0;
    }

    if (width <= 0)
        return false;

    int vp_y = osd->vp.y;
    int vp_h = osd->vp.height;

    if (y + height > vp_h)
        height = vp_h - y;

    if (y < 0)
    {
        height += y;
        y = 0;
    }

    if (height <= 0)
        return false;

    osd->lcd_update_rect(vp_x + x, vp_y + y, width, height);

    return true;
}

/* Set a new screen location and size (screen coordinates) */
static bool _osd_update_pos(struct osd *osd, int x, int y, int width,
                            int height)
{
    if (osd->status == OSD_DISABLED)
        return false;

    if (width < 0)
        width = 0;
    else if (width > osd->maxwidth)
        width = osd->maxwidth;

    if (height < 0)
        height = 0;
    else if (height > osd->maxheight)
        height = osd->maxheight;

    int vp_x = osd->vp.x;
    int vp_y = osd->vp.y;
    int vp_w = osd->vp.width;
    int vp_h = osd->vp.height;

    if (x == vp_x && y == vp_y && width == vp_w && height == vp_h)
        return false; /* No change */

    if (osd->status != OSD_VISIBLE)
    {
        /* Not visible - just update pos */
        osd->set_viewport_pos(&osd->vp, x, y, width, height);
        return false;
    }

    /* Visible area has changed */
    _osd_erase_osd(osd);

    /* Update the smallest rectangle that encloses both the old and new
       regions to make the change free of flicker (they may overlap) */
    int xu = MIN(vp_x, x);
    int yu = MIN(vp_y, y);
    int wu = MAX(vp_x + vp_w, x + width) - xu;
    int hu = MAX(vp_y + vp_h, y + height) - yu;

    osd->set_viewport_pos(&osd->vp, x, y, width, height);
    _osd_update_back_buffer(osd);
    _osd_draw_osd(osd);
    osd->lcd_update_rect(xu, yu, wu, hu);

    return true;
}

/* Call periodically to have the OSD timeout and hide itself */
static void _osd_monitor_timeout(struct osd *osd)
{
    if (osd->status <= OSD_HIDDEN)
        return; /* Already hidden/disabled */

    if (osd->timeout > 0 && TIME_AFTER(*rb->current_tick, osd->hide_tick))
        _osd_show(osd, OSD_HIDE);
}

/* Set the OSD timeout value. <= 0 = never timeout */
static void _osd_set_timeout(struct osd *osd, long timeout)
{
    if (osd->status == OSD_DISABLED)
        return;

    osd->timeout = timeout;
    _osd_monitor_timeout(osd);
}

/* Use the OSD viewport context */
static inline struct viewport * _osd_get_viewport(struct osd *osd)
{
    return &osd->vp;
}

/* Get the maximum dimensions calculated by osd_init() */
static void _osd_get_max_dims(struct osd *osd,
                              int *maxwidth, int *maxheight)
{
    if (maxwidth)
        *maxwidth = osd->maxwidth;

    if (maxheight)
        *maxheight = osd->maxheight;
}

/* Is the OSD enabled? */
static inline bool _osd_enabled(struct osd *osd)
{
    return osd->status != OSD_DISABLED;
}


/** LCD update substitutes **/

/* Prepare LCD framebuffer for regular drawing */
static inline void _osd_lcd_update_prepare(struct osd *osd)
{
    if (osd->status == OSD_VISIBLE)
    {
        osd->status = OSD_ERASED;
        _osd_erase_osd(osd);
    }
}

/* Update the whole screen */
static inline void _osd_lcd_update(struct osd *osd)
{
    if (osd->status == OSD_ERASED)
    {
        /* Save the screen image underneath and restore the OSD image */
        osd->status = OSD_VISIBLE;
        _osd_update_back_buffer(osd);
        _osd_draw_osd(osd);
    }

    osd->lcd_update();
}

/* Update a part of the screen */
static void _osd_lcd_update_rect(struct osd *osd,
        int x, int y, int width, int height)
{
    if (osd->status == OSD_ERASED)
    {
        /* Save the screen image underneath and restore the OSD image */
        osd->status = OSD_VISIBLE;
        _osd_update_back_buffer(osd);
        _osd_draw_osd(osd);
    }

    osd->lcd_update_rect(x, y, width, height);
}

/* Native LCD, public */
bool osd_init(unsigned flags, void *backbuf, size_t backbuf_size,
              osd_draw_cb_fn_t draw_cb, int *width, int *height,
              size_t *bufused)
{
    native_osd.init_buffers = _osd_lcd_init_buffers;
    native_osd.set_viewport_pos = _osd_lcd_viewport_set_pos;
    native_osd.lcd_update = rb->lcd_update;
    native_osd.lcd_update_rect = rb->lcd_update_rect;
    native_osd.lcd_set_viewport = rb->lcd_set_viewport;
    native_osd.lcd_set_framebuffer = (void *)rb->lcd_set_framebuffer;
#if LCD_DEPTH < 4
    native_osd.lcd_framebuffer_set_pos = NULL;
#endif /* LCD_DEPTH < 4 */
    native_osd.lcd_bitmap_part = (void *)rb->lcd_bitmap_part;

    return _osd_init(&native_osd, flags, backbuf, backbuf_size, draw_cb,
                     width, height, bufused);
}

void osd_destroy(void)
{
    return _osd_destroy(&native_osd);
}

bool osd_show(unsigned flags)
{
    return _osd_show(&native_osd, flags);
}

bool osd_update(void)
{
    return _osd_update(&native_osd);
}

bool osd_update_rect(int x, int y, int width, int height)
{
    return _osd_update_rect(&native_osd, x, y, width, height);
}

bool osd_update_pos(int x, int y, int width, int height)
{
    return _osd_update_pos(&native_osd, x, y, width, height);
}

void osd_monitor_timeout(void)
{
    _osd_monitor_timeout(&native_osd);
}

void osd_set_timeout(long timeout)
{
    _osd_set_timeout(&native_osd, timeout);
}

struct viewport * osd_get_viewport(void)
{
    return _osd_get_viewport(&native_osd);
}

void osd_get_max_dims(int *maxwidth, int *maxheight)
{
    _osd_get_max_dims(&native_osd, maxwidth, maxheight);
}

bool osd_enabled(void)
{
    return _osd_enabled(&native_osd);
}

void osd_lcd_update_prepare(void)
{
    _osd_lcd_update_prepare(&native_osd);
}


void osd_lcd_update(void)
{
    _osd_lcd_update(&native_osd);
}

void osd_lcd_update_rect(int x, int y, int width, int height)
{
    _osd_lcd_update_rect(&native_osd, x, y, width, height);
}

#if LCD_DEPTH < 4
/* Greylib LCD, public */
bool osd_grey_init(unsigned flags, void *backbuf, size_t backbuf_size,
                   osd_draw_cb_fn_t draw_cb, int *width, int *height,
                   size_t *bufused)
{
    grey_osd.init_buffers = _osd_grey_init_buffers;
    grey_osd.set_viewport_pos = grey_viewport_set_pos;
    grey_osd.lcd_update = grey_update;
    grey_osd.lcd_update_rect = grey_update_rect;
    grey_osd.lcd_set_viewport = grey_set_viewport;
    grey_osd.lcd_set_framebuffer = (void *)grey_set_framebuffer;
    grey_osd.lcd_framebuffer_set_pos = grey_framebuffer_set_pos;
    grey_osd.lcd_bitmap_part = (void *)grey_gray_bitmap_part;

    return _osd_init(&grey_osd, flags, backbuf, backbuf_size, draw_cb,
                     width, height, bufused);
}

void osd_grey_destroy(void)
{
    return _osd_destroy(&grey_osd);
}

bool osd_grey_show(unsigned flags)
{
    return _osd_show(&grey_osd, flags);
}

bool osd_grey_update(void)
{
    return _osd_update(&grey_osd);
}

bool osd_grey_update_rect(int x, int y, int width, int height)
{
    return _osd_update_rect(&grey_osd, x, y, width, height);
}

bool osd_grey_update_pos(int x, int y, int width, int height)
{
    return _osd_update_pos(&grey_osd, x, y, width, height);
}

void osd_grey_monitor_timeout(void)
{
    _osd_monitor_timeout(&grey_osd);
}

void osd_grey_set_timeout(long timeout)
{
    _osd_set_timeout(&grey_osd, timeout);
}

struct viewport * osd_grey_get_viewport(void)
{
    return _osd_get_viewport(&grey_osd);
}

void osd_grey_get_max_dims(int *maxwidth, int *maxheight)
{
    _osd_get_max_dims(&grey_osd, maxwidth, maxheight);
}

bool osd_grey_enabled(void)
{
    return _osd_enabled(&grey_osd);
}

void osd_grey_lcd_update_prepare(void)
{
    _osd_lcd_update_prepare(&grey_osd);
}

void osd_grey_lcd_update(void)
{
    _osd_lcd_update(&grey_osd);
}

void osd_grey_lcd_update_rect(int x, int y, int width, int height)
{
    _osd_lcd_update_rect(&grey_osd, x, y, width, height);
}
#endif /* LCD_DEPTH < 4 */