summaryrefslogtreecommitdiff
path: root/PLUGINS/src/dvbhddevice/hdffosd.c
blob: 254f2af7729ff7588a2c1d79d883a29539ffa9e7 (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
/*
 * hdffosd.c: Implementation of the DVB HD Full Featured On Screen Display
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: hdffosd.c 1.9 2011/04/17 11:20:22 kls Exp $
 */

#include "hdffosd.h"
#include <linux/dvb/osd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include "hdffcmd.h"
#include "setup.h"

#define MAX_NUM_FONTFACES   8
#define MAX_NUM_FONTS       8
#define MAX_BITMAP_SIZE     (1024*1024)

typedef struct _tFontFace
{
    cString Name;
    uint32_t Handle;
} tFontFace;

typedef struct _tFont
{
    uint32_t hFontFace;
    int Size;
    uint32_t Handle;
} tFont;

class cHdffOsd : public cOsd
{
private:
    HDFF::cHdffCmdIf * mHdffCmdIf;
    int mLeft;
    int mTop;
    int mDispWidth;
    int mDispHeight;
    bool shown;
    bool mChanged;
    bool mBitmapModified;
    uint32_t mDisplay;
    tFontFace mFontFaces[MAX_NUM_FONTFACES];
    tFont mFonts[MAX_NUM_FONTS];
    uint32_t mBitmapPalette;
    uint32_t mBitmapColors[256];
    uint32_t mBitmapNumColors;

protected:
    virtual void SetActive(bool On);
public:
    cHdffOsd(int Left, int Top, HDFF::cHdffCmdIf * pHdffCmdIf, uint Level);
    virtual ~cHdffOsd();
    cBitmap *GetBitmap(int Area);
    virtual eOsdError CanHandleAreas(const tArea *Areas, int NumAreas);
    virtual eOsdError SetAreas(const tArea *Areas, int NumAreas);
    virtual void SaveRegion(int x1, int y1, int x2, int y2);
    virtual void RestoreRegion(void);
    virtual void DrawPixel(int x, int y, tColor Color);
    virtual void DrawBitmap(int x, int y, const cBitmap &Bitmap, tColor ColorFg = 0, tColor ColorBg = 0, bool ReplacePalette = false, bool Overlay = false);
    virtual void DrawText(int x, int y, const char *s, tColor ColorFg, tColor ColorBg, const cFont *Font, int Width = 0, int Height = 0, int Alignment = taDefault);
    virtual void DrawRectangle(int x1, int y1, int x2, int y2, tColor Color);
    virtual void DrawEllipse(int x1, int y1, int x2, int y2, tColor Color, int Quadrants = 0);
    virtual void DrawSlope(int x1, int y1, int x2, int y2, tColor Color, int Type);
    virtual void Flush(void);
};

cHdffOsd::cHdffOsd(int Left, int Top, HDFF::cHdffCmdIf * pHdffCmdIf, uint Level)
:   cOsd(Left, Top, Level)
{
    double pixelAspect;
    HDFF::tOsdConfig config;

    //printf("cHdffOsd %d, %d, %d\n", Left, Top, Level);
    mHdffCmdIf = pHdffCmdIf;
    mLeft = Left;
    mTop = Top;
    shown = false;
    mChanged = false;
    mBitmapModified = false;
    mBitmapPalette = InvalidHandle;
    config.FontKerning = false;
    config.FontAntialiasing = Setup.AntiAlias ? true : false;
    mHdffCmdIf->CmdOsdConfigure(&config);

    gHdffSetup.GetOsdSize(mDispWidth, mDispHeight, pixelAspect);
    mDisplay = mHdffCmdIf->CmdOsdCreateDisplay(mDispWidth, mDispHeight, HDFF::colorTypeARGB8888);
    mHdffCmdIf->CmdOsdSetDisplayOutputRectangle(mDisplay, 0, 0, SizeFullScreen, SizeFullScreen);
    for (int i = 0; i < MAX_NUM_FONTFACES; i++)
    {
        mFontFaces[i].Name = "";
        mFontFaces[i].Handle = InvalidHandle;
    }
    for (int i = 0; i < MAX_NUM_FONTS; i++)
    {
        mFonts[i].hFontFace = InvalidHandle;
        mFonts[i].Size = 0;
        mFonts[i].Handle = InvalidHandle;
    }
}

cHdffOsd::~cHdffOsd()
{
    //printf("~cHdffOsd %d %d\n", mLeft, mTop);
    SetActive(false);

    for (int i = 0; i < MAX_NUM_FONTS; i++)
    {
        if (mFonts[i].Handle == InvalidHandle)
            break;
        mHdffCmdIf->CmdOsdDeleteFont(mFonts[i].Handle);
    }
    for (int i = 0; i < MAX_NUM_FONTFACES; i++)
    {
        if (mFontFaces[i].Handle == InvalidHandle)
            break;
        mHdffCmdIf->CmdOsdDeleteFontFace(mFontFaces[i].Handle);
    }

    if (mBitmapPalette != InvalidHandle)
        mHdffCmdIf->CmdOsdDeletePalette(mBitmapPalette);
    mHdffCmdIf->CmdOsdDrawRectangle(mDisplay, 0, 0, mDispWidth, mDispHeight, 0);
    mHdffCmdIf->CmdOsdRenderDisplay(mDisplay);
    mHdffCmdIf->CmdOsdDeleteDisplay(mDisplay);
}

cBitmap * cHdffOsd::GetBitmap(int Area)
{
    //printf("GetBitmap %d\n", Area);
    mChanged = true;
    mBitmapModified = true;
    return cOsd::GetBitmap(Area);
}

eOsdError cHdffOsd::CanHandleAreas(const tArea *Areas, int NumAreas)
{
    eOsdError Result = cOsd::CanHandleAreas(Areas, NumAreas);
    if (Result == oeOk)
    {
        for (int i = 0; i < NumAreas; i++)
        {
            if (Areas[i].bpp != 1 && Areas[i].bpp != 2 && Areas[i].bpp != 4 && Areas[i].bpp != 8)
                return oeBppNotSupported;
        }
    }
    return Result;
}

eOsdError cHdffOsd::SetAreas(const tArea *Areas, int NumAreas)
{
    for (int i = 0; i < NumAreas; i++)
    {
        //printf("SetAreas %d: %d %d %d %d %d\n", i, Areas[i].x1, Areas[i].y1, Areas[i].x2, Areas[i].y2, Areas[i].bpp);
    }
    mHdffCmdIf->CmdOsdDrawRectangle(mDisplay, 0, 0, mDispWidth, mDispHeight, 0);
    mHdffCmdIf->CmdOsdRenderDisplay(mDisplay);
    return cOsd::SetAreas(Areas, NumAreas);
}

void cHdffOsd::SetActive(bool On)
{
    if (On != Active())
    {
        cOsd::SetActive(On);
        if (On)
        {
        }
        else if (shown)
        {
            shown = false;
        }
    }
}

void cHdffOsd::SaveRegion(int x1, int y1, int x2, int y2)
{
    mHdffCmdIf->CmdOsdSaveRegion(mDisplay, mLeft + x1, mTop + y1, x2 - x1 + 1, y2 - y1 + 1);
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::RestoreRegion(void)
{
    mHdffCmdIf->CmdOsdRestoreRegion(mDisplay);
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::DrawPixel(int x, int y, tColor Color)
{
    //printf("DrawPixel\n");
    mBitmapModified = false;
}

void cHdffOsd::DrawBitmap(int x, int y, const cBitmap &Bitmap, tColor ColorFg, tColor ColorBg, bool ReplacePalette, bool Overlay)
{
    //printf("DrawBitmap %d %d %d\n", x, y, Overlay);
    int i;
    int numColors;
    const tColor * colors = Bitmap.Colors(numColors);

    for (i = 0; i < numColors; i++)
    {
        mBitmapColors[i] = colors[i];
        if (ColorFg || ColorBg)
        {
            if (i == 0)
                mBitmapColors[i] = ColorBg;
            else if (i == 1)
                mBitmapColors[i] = ColorFg;
        }
    }
    if (mBitmapPalette == InvalidHandle)
    {
        mBitmapPalette = mHdffCmdIf->CmdOsdCreatePalette(HDFF::colorTypeClut8,
                HDFF::colorFormatARGB, numColors, mBitmapColors);
    }
    else
    {
        mHdffCmdIf->CmdOsdSetPaletteColors(mBitmapPalette,
                HDFF::colorFormatARGB, 0, numColors, mBitmapColors);
    }
    mHdffCmdIf->CmdOsdDrawBitmap(mDisplay, mLeft + x, mTop + y,
        (uint8_t *) Bitmap.Data(0, 0), Bitmap.Width(), Bitmap.Height(),
        Bitmap.Width() * Bitmap.Height(), HDFF::colorTypeClut8, mBitmapPalette);
#if 0
    uint32_t * tmpBitmap = new uint32_t[Bitmap.Width() * Bitmap.Height()];
    for (int ix = 0; ix < Bitmap.Width(); ix++)
    {
        for (int iy = 0; iy < Bitmap.Height(); iy++)
        {
            const tIndex * pixel = Bitmap.Data(ix, iy);
            tColor color = Bitmap.Color(*pixel);
            if (!Overlay || *pixel != 0)
            {
                if (ColorFg || ColorBg)
                {
                    if (*pixel == 0)
                        color = ColorBg;
                    else if (*pixel == 1)
                        color = ColorFg;
                }
                tmpBitmap[Bitmap.Width() * iy + ix] = color;
            }
        }
    }
    mHdffCmdIf->CmdOsdDrawBitmap(mDisplay, mLeft + x, mTop + y,
        (uint8_t *) tmpBitmap, Bitmap.Width(), Bitmap.Height(),
        Bitmap.Width() * Bitmap.Height() * 4, HDFF::colorTypeARGB8888, InvalidHandle);
    delete[] tmpBitmap;
#endif
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::DrawText(int x, int y, const char *s, tColor ColorFg, tColor ColorBg, const cFont *Font, int Width, int Height, int Alignment)
{
    int w = Font->Width(s);
    int h = Font->Height();
    int limit = 0;
    int cw = Width ? Width : w;
    int ch = Height ? Height : h;
    int i;
    int size = Font->Size();
    tFontFace * pFontFace;
    tFont * pFont;

    if (ColorBg != clrTransparent)
        mHdffCmdIf->CmdOsdDrawRectangle(mDisplay, mLeft + x, mTop + y, cw, ch, ColorBg);

    if (s == NULL)
        return;

    pFontFace = NULL;
    for (i = 0; i < MAX_NUM_FONTFACES; i++)
    {
        if (mFontFaces[i].Handle == InvalidHandle)
            break;

        if (strcmp(mFontFaces[i].Name, Font->FontName()) == 0)
        {
            pFontFace = &mFontFaces[i];
            break;
        }
    }
    if (pFontFace == NULL)
    {
        if (i < MAX_NUM_FONTFACES)
        {
            cString fontFileName = Font->FontName();
            FILE * fp = fopen(fontFileName, "rb");
            if (fp)
            {
                fseek(fp, 0, SEEK_END);
                long fileSize = ftell(fp);
                fseek(fp, 0, SEEK_SET);
                if (fileSize > 0)
                {
                    uint8_t * buffer = new uint8_t[fileSize];
                    if (buffer)
                    {
                        if (fread(buffer, fileSize, 1, fp) == 1)
                        {
                            mFontFaces[i].Handle = mHdffCmdIf->CmdOsdCreateFontFace(buffer, fileSize);
                            if (mFontFaces[i].Handle != InvalidHandle)
                            {
                                mFontFaces[i].Name = Font->FontName();
                                pFontFace = &mFontFaces[i];
                            }
                        }
                        delete[] buffer;
                    }
                }
                fclose(fp);
            }
        }
    }
    if (pFontFace == NULL)
        return;

    pFont = NULL;
    for (i = 0; i < MAX_NUM_FONTS; i++)
    {
        if (mFonts[i].Handle == InvalidHandle)
            break;

        if (mFonts[i].hFontFace == pFontFace->Handle
          && mFonts[i].Size == size)
        {
            pFont = &mFonts[i];
            break;
        }
    }
    if (pFont == NULL)
    {
        if (i < MAX_NUM_FONTS)
        {
            mFonts[i].Handle = mHdffCmdIf->CmdOsdCreateFont(pFontFace->Handle, size);
            if (mFonts[i].Handle != InvalidHandle)
            {
                mFonts[i].hFontFace = pFontFace->Handle;
                mFonts[i].Size = size;
                pFont = &mFonts[i];
            }
        }
    }
    if (pFont == NULL)
        return;

    mHdffCmdIf->CmdOsdSetDisplayClippingArea(mDisplay, true, mLeft + x, mTop + y, cw, ch);

    if (Width || Height)
    {
        limit = x + cw;// - mLeft;
        if (Width)
        {
            if ((Alignment & taLeft) != 0)
                ;
            else if ((Alignment & taRight) != 0)
            {
                if (w < Width)
                    x += Width - w;
            }
            else
            { // taCentered
                if (w < Width)
                    x += (Width - w) / 2;
            }
        }
        if (Height)
        {
            if ((Alignment & taTop) != 0)
                ;
            else if ((Alignment & taBottom) != 0)
            {
                if (h < Height)
                    y += Height - h;
            }
            else
            { // taCentered
                if (h < Height)
                    y += (Height - h) / 2;
            }
        }
    }
    //x -= mLeft;
    //y -= mTop;
    {
        uint16_t tmp[1000];
        uint16_t len = 0;
        while (*s && (len < (sizeof(tmp) - 1)))
        {
            int sl = Utf8CharLen(s);
            uint sym = Utf8CharGet(s, sl);
            s += sl;
            tmp[len] = sym;
            len++;
        }
        tmp[len] = 0;
        mHdffCmdIf->CmdOsdDrawTextW(mDisplay, pFont->Handle, x + mLeft, y + mTop + h, tmp, ColorFg);
    }
    //mHdffCmdIf->CmdOsdDrawText(mDisplay, pFont->Handle, x + mLeft, y + mTop + h - 7, s, ColorFg);
    mHdffCmdIf->CmdOsdSetDisplayClippingArea(mDisplay, false, 0, 0, 0, 0);
    //Font->DrawText(this, x, y, s, ColorFg, ColorBg, limit);
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::DrawRectangle(int x1, int y1, int x2, int y2, tColor Color)
{
    mHdffCmdIf->CmdOsdDrawRectangle(mDisplay, mLeft + x1, mTop + y1, x2 - x1 + 1, y2 - y1 + 1, Color);
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::DrawEllipse(int x1, int y1, int x2, int y2, tColor Color, int Quadrants)
{
    uint32_t flags;
    int cx;
    int cy;
    int rx;
    int ry;

    switch (abs(Quadrants))
    {
        case 1:
            if (Quadrants > 0)
                flags = HDFF::drawQuarterTopRight;
            else
                flags = HDFF::drawQuarterTopRightInverted;
            cx = x1;
            cy = y2;
            rx = x2 - x1;
            ry = y2 - y1;
            break;
        case 2:
            if (Quadrants > 0)
                flags = HDFF::drawQuarterTopLeft;
            else
                flags = HDFF::drawQuarterTopLeftInverted;
            cx = x2;
            cy = y2;
            rx = x2 - x1;
            ry = y2 - y1;
            break;
        case 3:
            if (Quadrants > 0)
                flags = HDFF::drawQuarterBottomLeft;
            else
                flags = HDFF::drawQuarterBottomLeftInverted;
            cx = x2;
            cy = y1;
            rx = x2 - x1;
            ry = y2 - y1;
            break;
        case 4:
            if (Quadrants > 0)
                flags = HDFF::drawQuarterBottomRight;
            else
                flags = HDFF::drawQuarterBottomRightInverted;
            cx = x1;
            cy = y1;
            rx = x2 - x1;
            ry = y2 - y1;
            break;
        case 5:
            flags = HDFF::drawHalfRight;
            cx = x1;
            cy = (y1 + y2) / 2;
            rx = x2 - x1;
            ry = (y2 - y1) / 2;
            break;
        case 6:
            flags = HDFF::drawHalfTop;
            cx = (x1 + x2) / 2;
            cy = y2;
            rx = (x2 - x1) / 2;
            ry = y2 - y1;
            break;
        case 7:
            flags = HDFF::drawHalfLeft;
            cx = x2;
            cy = (y1 + y2) / 2;
            rx = x2 - x1;
            ry = (y2 - y1) / 2;
            break;
        case 8:
            flags = HDFF::drawHalfBottom;
            cx = (x1 + x2) / 2;
            cy = y1;
            rx = (x2 - x1) / 2;
            ry = y2 - y1;
            break;
        default:
            flags = HDFF::drawFull;
            cx = (x1 + x2) / 2;
            cy = (y1 + y2) / 2;
            rx = (x2 - x1) / 2;
            ry = (y2 - y1) / 2;
            break;
    }
    mHdffCmdIf->CmdOsdDrawEllipse(mDisplay, mLeft + cx, mTop + cy, rx, ry, Color, flags);
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::DrawSlope(int x1, int y1, int x2, int y2, tColor Color, int Type)
{
    //printf("DrawSlope\n");
    mChanged = true;
    mBitmapModified = false;
}

void cHdffOsd::Flush(void)
{
    if (!Active())
        return;

    if (!mChanged)
        return;

    //printf("Flush\n");
    if (mBitmapModified)
    {
        cBitmap *Bitmap;
        for (int i = 0; (Bitmap = GetBitmap(i)) != NULL; i++)
        {
            DrawBitmap(0, 0, *Bitmap);
        }
    }

    mHdffCmdIf->CmdOsdRenderDisplay(mDisplay);

    mChanged = false;
    mBitmapModified = false;
}


class cHdffOsdRaw : public cOsd
{
private:
    HDFF::cHdffCmdIf * mHdffCmdIf;
    int mDispWidth;
    int mDispHeight;
    bool shown;
    uint32_t mDisplay;
    uint32_t mBitmapPalette;
    uint32_t mBitmapColors[256];
    uint32_t mBitmapNumColors;

protected:
    virtual void SetActive(bool On);
public:
    cHdffOsdRaw(int Left, int Top, HDFF::cHdffCmdIf * pHdffCmdIf, uint Level);
    virtual ~cHdffOsdRaw();
    virtual eOsdError CanHandleAreas(const tArea *Areas, int NumAreas);
    virtual eOsdError SetAreas(const tArea *Areas, int NumAreas);
    virtual void Flush(void);
};

cHdffOsdRaw::cHdffOsdRaw(int Left, int Top, HDFF::cHdffCmdIf * pHdffCmdIf, uint Level)
:   cOsd(Left, Top, Level)
{
    double pixelAspect;

    //printf("cHdffOsdRaw %d, %d, %d\n", Left, Top, Level);
    mHdffCmdIf = pHdffCmdIf;
    shown = false;
    mBitmapPalette = InvalidHandle;

    gHdffSetup.GetOsdSize(mDispWidth, mDispHeight, pixelAspect);
    mDisplay = mHdffCmdIf->CmdOsdCreateDisplay(mDispWidth, mDispHeight, HDFF::colorTypeARGB8888);
    mHdffCmdIf->CmdOsdSetDisplayOutputRectangle(mDisplay, 0, 0, SizeFullScreen, SizeFullScreen);
}

cHdffOsdRaw::~cHdffOsdRaw()
{
    //printf("~cHdffOsdRaw %d %d\n", Left(), Top());
    SetActive(false);

    if (mBitmapPalette != InvalidHandle)
        mHdffCmdIf->CmdOsdDeletePalette(mBitmapPalette);
    mHdffCmdIf->CmdOsdDrawRectangle(mDisplay, 0, 0, mDispWidth, mDispHeight, 0);
    mHdffCmdIf->CmdOsdRenderDisplay(mDisplay);
    mHdffCmdIf->CmdOsdDeleteDisplay(mDisplay);
}

void cHdffOsdRaw::SetActive(bool On)
{
    if (On != Active())
    {
        cOsd::SetActive(On);
        if (On)
        {
        }
        else if (shown)
        {
            shown = false;
        }
    }
}

eOsdError cHdffOsdRaw::CanHandleAreas(const tArea *Areas, int NumAreas)
{
    eOsdError Result = cOsd::CanHandleAreas(Areas, NumAreas);
    if (Result == oeOk)
    {
        for (int i = 0; i < NumAreas; i++)
        {
            if (Areas[i].bpp != 1 && Areas[i].bpp != 2 && Areas[i].bpp != 4 && Areas[i].bpp != 8
                && (Areas[i].bpp != 32 || !gHdffSetup.TrueColorOsd))
                return oeBppNotSupported;
        }
    }
    return Result;
}

eOsdError cHdffOsdRaw::SetAreas(const tArea *Areas, int NumAreas)
{
    for (int i = 0; i < NumAreas; i++)
    {
        //printf("SetAreas %d: %d %d %d %d %d\n", i, Areas[i].x1, Areas[i].y1, Areas[i].x2, Areas[i].y2, Areas[i].bpp);
    }
    mHdffCmdIf->CmdOsdDrawRectangle(mDisplay, 0, 0, mDispWidth, mDispHeight, 0);
    mHdffCmdIf->CmdOsdRenderDisplay(mDisplay);
    return cOsd::SetAreas(Areas, NumAreas);
}

void cHdffOsdRaw::Flush(void)
{
    if (!Active())
        return;
    //struct timeval start;
    //struct timeval end;
    //struct timezone timeZone;
    //gettimeofday(&start, &timeZone);

    bool render = false;
    if (IsTrueColor())
    {
        LOCK_PIXMAPS;
        while (cPixmapMemory *pm = RenderPixmaps())
        {
            int w = pm->ViewPort().Width();
            int h = pm->ViewPort().Height();
            int d = w * sizeof(tColor);
            int Chunk = MAX_BITMAP_SIZE / w / sizeof(tColor);
            if (Chunk > h)
                Chunk = h;
            for (int y = 0; y < h; y += Chunk)
            {
                 int hc = Chunk;
                 if (y + hc > h)
                     hc = h - y;
                 mHdffCmdIf->CmdOsdDrawBitmap(mDisplay,
                     Left() + pm->ViewPort().X(), Top() + pm->ViewPort().Y() + y,
                     pm->Data() + y * d, w, hc, hc * d,
                     HDFF::colorTypeARGB8888, InvalidHandle);
            }
            delete pm;
            render = true;
        }
    }
    else
    {
        uint8_t * buffer = new uint8_t[MAX_BITMAP_SIZE];
        if (!buffer)
            return;
        cBitmap * bitmap;
        for (int i = 0; (bitmap = GetBitmap(i)) != NULL; i++)
        {
            int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
            if (!shown || bitmap->Dirty(x1, y1, x2, y2))
            {
                if (!shown)
                {
                    x2 = bitmap->Width() - 1;
                    y2 = bitmap->Height() - 1;
                }
                // commit colors:
                int numColors;
                const tColor * colors = bitmap->Colors(numColors);
                if (colors)
                {
                    for (int c = 0; c < numColors; c++)
                        mBitmapColors[c] = colors[c];
                    if (mBitmapPalette == InvalidHandle)
                    {
                        mBitmapPalette = mHdffCmdIf->CmdOsdCreatePalette(HDFF::colorTypeClut8,
                            HDFF::colorFormatARGB, numColors, mBitmapColors);
                    }
                    else
                    {
                        mHdffCmdIf->CmdOsdSetPaletteColors(mBitmapPalette,
                            HDFF::colorFormatARGB, 0, numColors, mBitmapColors);
                    }
                }
                // commit modified data:
                int width = x2 - x1 + 1;
                int height = y2 - y1 + 1;
                int chunk = MAX_BITMAP_SIZE / width;
                if (chunk > height)
                    chunk = height;
                for (int y = 0; y < height; y += chunk)
                {
                    int hc = chunk;
                    if (y + hc > height)
                        hc = height - y;
                    for (int r = 0; r < hc; r++)
                        memcpy(buffer + r * width, bitmap->Data(x1, y1 + y + r), width);
                    mHdffCmdIf->CmdOsdDrawBitmap(mDisplay,
                        Left() + bitmap->X0() + x1, Top() + bitmap->Y0() + y1 + y,
                        buffer, width, hc, hc * width,
                        HDFF::colorTypeClut8, mBitmapPalette);
                }
                render = true;
            }
            bitmap->Clean();
        }
        delete[] buffer;
    }
    if (render)
    {
        mHdffCmdIf->CmdOsdRenderDisplay(mDisplay);
        //gettimeofday(&end, &timeZone);
        //int timeNeeded = end.tv_usec - start.tv_usec;
        //timeNeeded += (end.tv_sec - start.tv_sec) * 1000000;
        //printf("time = %d\n", timeNeeded);
    }
    shown = true;
}




cHdffOsdProvider::cHdffOsdProvider(HDFF::cHdffCmdIf * HdffCmdIf)
{
    mHdffCmdIf = HdffCmdIf;
}

cOsd *cHdffOsdProvider::CreateOsd(int Left, int Top, uint Level)
{
    //printf("CreateOsd %d %d %d\n", Left, Top, Level);
    if (gHdffSetup.HighLevelOsd)
        return new cHdffOsd(Left, Top, mHdffCmdIf, Level);
    else
        return new cHdffOsdRaw(Left, Top, mHdffCmdIf, Level);
}

bool cHdffOsdProvider::ProvidesTrueColor(void)
{
    return gHdffSetup.TrueColorOsd && !gHdffSetup.HighLevelOsd;
}