summaryrefslogtreecommitdiffstats
path: root/ArmPlatformPkg/Drivers/LcdGraphicsOutputDxe/LcdGraphicsOutputBlt.c
blob: c865215ec68ecba1e809108cffec9354f042561e (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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/** @file

 Copyright (c) 2011-2013, ARM Ltd. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent

 **/

#include <PiDxe.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DevicePathLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>

#include <Guid/GlobalVariable.h>

#include "LcdGraphicsOutputDxe.h"

extern BOOLEAN mDisplayInitialized;

//
// Function Definitions
//

STATIC
EFI_STATUS
VideoCopyNoHorizontalOverlap (
  IN UINTN          BitsPerPixel,
  IN volatile VOID  *FrameBufferBase,
  IN UINT32         HorizontalResolution,
  IN UINTN          SourceX,
  IN UINTN          SourceY,
  IN UINTN          DestinationX,
  IN UINTN          DestinationY,
  IN UINTN          Width,
  IN UINTN          Height
)
{
  EFI_STATUS    Status = EFI_SUCCESS;
  UINTN         SourceLine;
  UINTN         DestinationLine;
  UINTN         WidthInBytes;
  UINTN         LineCount;
  INTN          Step;
  VOID          *SourceAddr;
  VOID          *DestinationAddr;

  if( DestinationY <= SourceY ) {
    // scrolling up (or horizontally but without overlap)
    SourceLine       = SourceY;
    DestinationLine  = DestinationY;
    Step             = 1;
  } else {
    // scrolling down
    SourceLine       = SourceY + Height;
    DestinationLine  = DestinationY + Height;
    Step             = -1;
  }

  switch (BitsPerPixel) {

  case LCD_BITS_PER_PIXEL_24:

    WidthInBytes = Width * 4;

    for( LineCount = 0; LineCount < Height; LineCount++ ) {
      // Update the start addresses of source & destination using 32bit pointer arithmetic
      SourceAddr      = (VOID *)((UINT32 *)FrameBufferBase + SourceLine      * HorizontalResolution + SourceX     );
      DestinationAddr = (VOID *)((UINT32 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationX);

      // Copy the entire line Y from video ram to the temp buffer
      CopyMem( DestinationAddr, SourceAddr, WidthInBytes);

      // Update the line numbers
      SourceLine      += Step;
      DestinationLine += Step;
    }
    break;

  case LCD_BITS_PER_PIXEL_16_555:
  case LCD_BITS_PER_PIXEL_16_565:
  case LCD_BITS_PER_PIXEL_12_444:

    WidthInBytes = Width * 2;

    for( LineCount = 0; LineCount < Height; LineCount++ ) {
      // Update the start addresses of source & destination using 16bit pointer arithmetic
      SourceAddr      = (VOID *)((UINT16 *)FrameBufferBase + SourceLine      * HorizontalResolution + SourceX     );
      DestinationAddr = (VOID *)((UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationX);

      // Copy the entire line Y from video ram to the temp buffer
      CopyMem( DestinationAddr, SourceAddr, WidthInBytes);

      // Update the line numbers
      SourceLine      += Step;
      DestinationLine += Step;
    }
    break;

  case LCD_BITS_PER_PIXEL_8:
  case LCD_BITS_PER_PIXEL_4:
  case LCD_BITS_PER_PIXEL_2:
  case LCD_BITS_PER_PIXEL_1:
  default:
    // Can't handle this case
    DEBUG((DEBUG_ERROR, "ArmVeGraphics_Blt: EfiBltVideoToVideo: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
    Status = EFI_INVALID_PARAMETER;
    goto EXIT;
    // break;

  }

  EXIT:
  return Status;
}

STATIC
EFI_STATUS
VideoCopyHorizontalOverlap (
  IN UINTN          BitsPerPixel,
  IN volatile VOID  *FrameBufferBase,
  UINT32            HorizontalResolution,
  IN UINTN          SourceX,
  IN UINTN          SourceY,
  IN UINTN          DestinationX,
  IN UINTN          DestinationY,
  IN UINTN          Width,
  IN UINTN          Height
)
{
  EFI_STATUS      Status = EFI_SUCCESS;

  UINT32 *PixelBuffer32bit;
  UINT32 *SourcePixel32bit;
  UINT32 *DestinationPixel32bit;

  UINT16 *PixelBuffer16bit;
  UINT16 *SourcePixel16bit;
  UINT16 *DestinationPixel16bit;

  UINT32          SourcePixelY;
  UINT32          DestinationPixelY;
  UINTN           SizeIn32Bits;
  UINTN           SizeIn16Bits;

  switch (BitsPerPixel) {

  case LCD_BITS_PER_PIXEL_24:
    // Allocate a temporary buffer

    PixelBuffer32bit = (UINT32 *) AllocatePool((Height * Width) * sizeof(UINT32));

    if (PixelBuffer32bit == NULL) {
      Status = EFI_OUT_OF_RESOURCES;
      goto EXIT;
    }

    SizeIn32Bits = Width * 4;

    // Copy from the video ram (source region) to a temp buffer
    for (SourcePixelY = SourceY, DestinationPixel32bit = PixelBuffer32bit;
         SourcePixelY < SourceY + Height;
         SourcePixelY++, DestinationPixel32bit += Width)
    {
      // Update the start address of line Y (source)
      SourcePixel32bit = (UINT32 *)FrameBufferBase + SourcePixelY * HorizontalResolution + SourceX;

      // Copy the entire line Y from video ram to the temp buffer
      CopyMem( (VOID *)DestinationPixel32bit, (CONST VOID *)SourcePixel32bit, SizeIn32Bits);
    }

    // Copy from the temp buffer to the video ram (destination region)
    for (DestinationPixelY = DestinationY, SourcePixel32bit = PixelBuffer32bit;
         DestinationPixelY < DestinationY + Height;
         DestinationPixelY++, SourcePixel32bit += Width)
    {
      // Update the start address of line Y (target)
      DestinationPixel32bit = (UINT32 *)FrameBufferBase + DestinationPixelY * HorizontalResolution + DestinationX;

      // Copy the entire line Y from the temp buffer to video ram
      CopyMem( (VOID *)DestinationPixel32bit, (CONST VOID *)SourcePixel32bit, SizeIn32Bits);
    }

    // Free up the allocated memory
    FreePool((VOID *) PixelBuffer32bit);

    break;


  case LCD_BITS_PER_PIXEL_16_555:
  case LCD_BITS_PER_PIXEL_16_565:
  case LCD_BITS_PER_PIXEL_12_444:
    // Allocate a temporary buffer
    PixelBuffer16bit = (UINT16 *) AllocatePool((Height * Width) * sizeof(UINT16));

    if (PixelBuffer16bit == NULL) {
      Status = EFI_OUT_OF_RESOURCES;
      goto EXIT;
    }

    // Access each pixel inside the source area of the Video Memory and copy it to the temp buffer

    SizeIn16Bits = Width * 2;

    for (SourcePixelY = SourceY, DestinationPixel16bit = PixelBuffer16bit;
         SourcePixelY < SourceY + Height;
         SourcePixelY++, DestinationPixel16bit += Width)
    {
      // Calculate the source address:
      SourcePixel16bit = (UINT16 *)FrameBufferBase + SourcePixelY * HorizontalResolution + SourceX;

      // Copy the entire line Y from Video to the temp buffer
      CopyMem( (VOID *)DestinationPixel16bit, (CONST VOID *)SourcePixel16bit, SizeIn16Bits);
    }

    // Copy from the temp buffer into the destination area of the Video Memory

    for (DestinationPixelY = DestinationY, SourcePixel16bit = PixelBuffer16bit;
         DestinationPixelY < DestinationY + Height;
         DestinationPixelY++, SourcePixel16bit += Width)
    {
      // Calculate the target address:
      DestinationPixel16bit = (UINT16 *)FrameBufferBase + (DestinationPixelY * HorizontalResolution + DestinationX);

      // Copy the entire line Y from the temp buffer to Video
      CopyMem( (VOID *)DestinationPixel16bit, (CONST VOID *)SourcePixel16bit, SizeIn16Bits);
    }

    // Free the allocated memory
    FreePool((VOID *) PixelBuffer16bit);

    break;


  case LCD_BITS_PER_PIXEL_8:
  case LCD_BITS_PER_PIXEL_4:
  case LCD_BITS_PER_PIXEL_2:
  case LCD_BITS_PER_PIXEL_1:
  default:
    // Can't handle this case
    DEBUG((DEBUG_ERROR, "ArmVeGraphics_Blt: EfiBltVideoToVideo: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
    Status = EFI_INVALID_PARAMETER;
    goto EXIT;
    // break;

  }

EXIT:
  return Status;
}

STATIC
EFI_STATUS
BltVideoFill (
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL        *This,
  IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL   *EfiSourcePixel,     OPTIONAL
  IN UINTN                               SourceX,
  IN UINTN                               SourceY,
  IN UINTN                               DestinationX,
  IN UINTN                               DestinationY,
  IN UINTN                               Width,
  IN UINTN                               Height,
  IN UINTN                               Delta           OPTIONAL   // Number of BYTES in a row of the BltBuffer
  )
{
  EFI_PIXEL_BITMASK*  PixelInformation;
  EFI_STATUS         Status;
  UINT32             HorizontalResolution;
  LCD_BPP            BitsPerPixel;
  VOID            *FrameBufferBase;
  VOID            *DestinationAddr;
  UINT16          *DestinationPixel16bit;
  UINT16          Pixel16bit;
  UINT32          DestinationPixelX;
  UINT32          DestinationLine;
  UINTN           WidthInBytes;

  Status           = EFI_SUCCESS;
  PixelInformation = &This->Mode->Info->PixelInformation;
  FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));
  HorizontalResolution = This->Mode->Info->HorizontalResolution;

  LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);

  switch (BitsPerPixel) {
  case LCD_BITS_PER_PIXEL_24:
    WidthInBytes = Width * 4;

    // Copy the SourcePixel into every pixel inside the target rectangle
    for (DestinationLine = DestinationY;
         DestinationLine < DestinationY + Height;
         DestinationLine++)
    {
      // Calculate the target address using 32bit pointer arithmetic:
      DestinationAddr = (VOID *)((UINT32 *)FrameBufferBase + DestinationLine * HorizontalResolution  + DestinationX);

      // Fill the entire line
      SetMem32 (DestinationAddr, WidthInBytes, *((UINT32 *)EfiSourcePixel));
    }
    break;

  case LCD_BITS_PER_PIXEL_16_555:
    // Convert the EFI pixel at the start of the BltBuffer(0,0) into a video display pixel
    Pixel16bit = (UINT16) (
        ( (EfiSourcePixel->Red      <<  7) & PixelInformation->RedMask      )
      | ( (EfiSourcePixel->Green    <<  2) & PixelInformation->GreenMask    )
      | ( (EfiSourcePixel->Blue     >>  3) & PixelInformation->BlueMask     )
//      | ( 0                           & PixelInformation->ReservedMask )
     );

    // Copy the SourcePixel into every pixel inside the target rectangle
    for (DestinationLine = DestinationY;
         DestinationLine < DestinationY + Height;
         DestinationLine++)
    {
      for (DestinationPixelX = DestinationX;
           DestinationPixelX < DestinationX + Width;
           DestinationPixelX++)
      {
        // Calculate the target address:
        DestinationPixel16bit =  (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;

        // Copy the pixel into the new target
        *DestinationPixel16bit = Pixel16bit;
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_16_565:
    // Convert the EFI pixel at the start of the BltBuffer(0,0) into a video display pixel
    Pixel16bit = (UINT16) (
        ( (EfiSourcePixel->Red      <<  8) & PixelInformation->RedMask      )
      | ( (EfiSourcePixel->Green    <<  3) & PixelInformation->GreenMask    )
      | ( (EfiSourcePixel->Blue     >>  3) & PixelInformation->BlueMask     )
     );

    // Copy the SourcePixel into every pixel inside the target rectangle
    for (DestinationLine = DestinationY;
         DestinationLine < DestinationY + Height;
         DestinationLine++)
    {
      for (DestinationPixelX = DestinationX;
           DestinationPixelX < DestinationX + Width;
           DestinationPixelX++)
      {
        // Calculate the target address:
        DestinationPixel16bit =  (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution  + DestinationPixelX;

        // Copy the pixel into the new target
        *DestinationPixel16bit = Pixel16bit;
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_12_444:
    // Convert the EFI pixel at the start of the BltBuffer(0,0) into a video display pixel
    Pixel16bit = (UINT16) (
        ( (EfiSourcePixel->Red      >> 4) & PixelInformation->RedMask      )
      | ( (EfiSourcePixel->Green        ) & PixelInformation->GreenMask    )
      | ( (EfiSourcePixel->Blue     << 4) & PixelInformation->BlueMask     )
     );

    // Copy the SourcePixel into every pixel inside the target rectangle
    for (DestinationLine = DestinationY;
         DestinationLine < DestinationY + Height;
         DestinationLine++)
    {
      for (DestinationPixelX = DestinationX;
           DestinationPixelX < DestinationX + Width;
           DestinationPixelX++)
      {
        // Calculate the target address:
        DestinationPixel16bit =  (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution  + DestinationPixelX;

        // Copy the pixel into the new target
        *DestinationPixel16bit = Pixel16bit;
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_8:
  case LCD_BITS_PER_PIXEL_4:
  case LCD_BITS_PER_PIXEL_2:
  case LCD_BITS_PER_PIXEL_1:
  default:
    // Can't handle this case
    DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: EfiBltVideoFill: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
    Status = EFI_INVALID_PARAMETER;
    break;
  }

  return Status;
}

STATIC
EFI_STATUS
BltVideoToBltBuffer (
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL        *This,
  IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL   *BltBuffer,     OPTIONAL
  IN UINTN                               SourceX,
  IN UINTN                               SourceY,
  IN UINTN                               DestinationX,
  IN UINTN                               DestinationY,
  IN UINTN                               Width,
  IN UINTN                               Height,
  IN UINTN                               Delta           OPTIONAL   // Number of BYTES in a row of the BltBuffer
  )
{
  EFI_STATUS         Status;
  UINT32             HorizontalResolution;
  LCD_BPP            BitsPerPixel;
  EFI_PIXEL_BITMASK  *PixelInformation;
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *EfiDestinationPixel;
  VOID   *FrameBufferBase;
  VOID            *SourceAddr;
  VOID            *DestinationAddr;
  UINT16 *SourcePixel16bit;
  UINT16          Pixel16bit;
  UINT32          SourcePixelX;
  UINT32          SourceLine;
  UINT32          DestinationPixelX;
  UINT32          DestinationLine;
  UINT32          BltBufferHorizontalResolution;
  UINTN           WidthInBytes;

  Status = EFI_SUCCESS;
  PixelInformation = &This->Mode->Info->PixelInformation;
  HorizontalResolution = This->Mode->Info->HorizontalResolution;
  FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));

  if(( Delta != 0 ) && ( Delta != Width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
    // Delta is not zero and it is different from the width.
    // Divide it by the size of a pixel to find out the buffer's horizontal resolution.
    BltBufferHorizontalResolution = (UINT32) (Delta / sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
  } else {
    BltBufferHorizontalResolution = Width;
  }

  LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);

  switch (BitsPerPixel) {
  case LCD_BITS_PER_PIXEL_24:
    WidthInBytes = Width * 4;

    // Access each line inside the Video Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
         SourceLine < SourceY + Height;
         SourceLine++, DestinationLine++)
    {
      // Calculate the source and target addresses using 32bit pointer arithmetic:
      SourceAddr      = (VOID *)((UINT32 *)FrameBufferBase + SourceLine      * HorizontalResolution          + SourceX     );
      DestinationAddr = (VOID *)((UINT32 *)BltBuffer       + DestinationLine * BltBufferHorizontalResolution + DestinationX);

      // Copy the entire line
      CopyMem( DestinationAddr, SourceAddr, WidthInBytes);
    }
    break;

  case LCD_BITS_PER_PIXEL_16_555:
    // Access each pixel inside the Video Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
         SourceLine < SourceY + Height;
         SourceLine++, DestinationLine++)
    {
      for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
           SourcePixelX < SourceX + Width;
           SourcePixelX++, DestinationPixelX++)
      {
        // Calculate the source and target addresses:
        SourcePixel16bit = (UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourcePixelX;
        EfiDestinationPixel = BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationPixelX;

        // Snapshot the pixel from the video buffer once, to speed up the operation.
        // If we were dereferencing the pointer, as it is volatile, we would perform 3 memory read operations.
        Pixel16bit = *SourcePixel16bit;

        // Copy the pixel into the new target
        EfiDestinationPixel->Red      = (UINT8) ( (Pixel16bit & PixelInformation->RedMask     ) >>  7 );
        EfiDestinationPixel->Green    = (UINT8) ( (Pixel16bit & PixelInformation->GreenMask   ) >>  2);
        EfiDestinationPixel->Blue     = (UINT8) ( (Pixel16bit & PixelInformation->BlueMask    ) <<  3 );
        // EfiDestinationPixel->Reserved = (UINT8) 0;
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_16_565:
    // Access each pixel inside the Video Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
         SourceLine < SourceY + Height;
         SourceLine++, DestinationLine++)
    {
      for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
           SourcePixelX < SourceX + Width;
           SourcePixelX++, DestinationPixelX++)
      {
        // Calculate the source and target addresses:
        SourcePixel16bit = (UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourcePixelX;
        EfiDestinationPixel = BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationPixelX;

        // Snapshot the pixel from the video buffer once, to speed up the operation.
        // If we were dereferencing the pointer, as it is volatile, we would perform 3 memory read operations.
        Pixel16bit = *SourcePixel16bit;

        // Copy the pixel into the new target
        // There is no info for the Reserved byte, so we set it to zero
        EfiDestinationPixel->Red      = (UINT8) ( (Pixel16bit & PixelInformation->RedMask     ) >> 8 );
        EfiDestinationPixel->Green    = (UINT8) ( (Pixel16bit & PixelInformation->GreenMask   ) >> 3);
        EfiDestinationPixel->Blue     = (UINT8) ( (Pixel16bit & PixelInformation->BlueMask    ) << 3 );
        // EfiDestinationPixel->Reserved = (UINT8) 0;
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_12_444:
    // Access each pixel inside the Video Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
         SourceLine < SourceY + Height;
         SourceLine++, DestinationLine++)
    {
      for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
           SourcePixelX < SourceX + Width;
           SourcePixelX++, DestinationPixelX++)
      {
        // Calculate the source and target addresses:
        SourcePixel16bit = (UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourcePixelX;
        EfiDestinationPixel = BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationPixelX;

        // Snapshot the pixel from the video buffer once, to speed up the operation.
        // If we were dereferencing the pointer, as it is volatile, we would perform 3 memory read operations.
        Pixel16bit = *SourcePixel16bit;

        // Copy the pixel into the new target
        EfiDestinationPixel->Red      = (UINT8) ( (Pixel16bit & PixelInformation->RedMask     ) >> 4 );
        EfiDestinationPixel->Green    = (UINT8) ( (Pixel16bit & PixelInformation->GreenMask   )     );
        EfiDestinationPixel->Blue     = (UINT8) ( (Pixel16bit & PixelInformation->BlueMask    ) << 4 );
        // EfiDestinationPixel->Reserved = (UINT8) 0;
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_8:
  case LCD_BITS_PER_PIXEL_4:
  case LCD_BITS_PER_PIXEL_2:
  case LCD_BITS_PER_PIXEL_1:
  default:
    // Can't handle this case
    DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: EfiBltVideoToBltBuffer: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
    Status = EFI_INVALID_PARAMETER;
    break;
  }
  return Status;
}

STATIC
EFI_STATUS
BltBufferToVideo (
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL        *This,
  IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL   *BltBuffer,     OPTIONAL
  IN UINTN                               SourceX,
  IN UINTN                               SourceY,
  IN UINTN                               DestinationX,
  IN UINTN                               DestinationY,
  IN UINTN                               Width,
  IN UINTN                               Height,
  IN UINTN                               Delta           OPTIONAL   // Number of BYTES in a row of the BltBuffer
  )
{
  EFI_STATUS         Status;
  UINT32             HorizontalResolution;
  LCD_BPP            BitsPerPixel;
  EFI_PIXEL_BITMASK  *PixelInformation;
  EFI_GRAPHICS_OUTPUT_BLT_PIXEL *EfiSourcePixel;
  VOID   *FrameBufferBase;
  VOID            *SourceAddr;
  VOID            *DestinationAddr;
  UINT16 *DestinationPixel16bit;
  UINT32          SourcePixelX;
  UINT32          SourceLine;
  UINT32          DestinationPixelX;
  UINT32          DestinationLine;
  UINT32          BltBufferHorizontalResolution;
  UINTN           WidthInBytes;

  Status = EFI_SUCCESS;
  PixelInformation = &This->Mode->Info->PixelInformation;
  HorizontalResolution = This->Mode->Info->HorizontalResolution;
  FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));

  if(( Delta != 0 ) && ( Delta != Width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
    // Delta is not zero and it is different from the width.
    // Divide it by the size of a pixel to find out the buffer's horizontal resolution.
    BltBufferHorizontalResolution = (UINT32) (Delta / sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
  } else {
    BltBufferHorizontalResolution = Width;
  }

  LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);

  switch (BitsPerPixel) {
  case LCD_BITS_PER_PIXEL_24:
    WidthInBytes = Width * 4;

    // Access each pixel inside the BltBuffer Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
       SourceLine < SourceY + Height;
       SourceLine++, DestinationLine++)
    {
      // Calculate the source and target addresses using 32bit pointer arithmetic:
      SourceAddr      = (VOID *)((UINT32 *)BltBuffer       + SourceLine      * BltBufferHorizontalResolution + SourceX     );
      DestinationAddr = (VOID *)((UINT32 *)FrameBufferBase + DestinationLine * HorizontalResolution          + DestinationX);

      // Copy the entire row Y
      CopyMem( DestinationAddr, SourceAddr, WidthInBytes);
    }
    break;

  case LCD_BITS_PER_PIXEL_16_555:
    // Access each pixel inside the BltBuffer Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
       SourceLine < SourceY + Height;
       SourceLine++, DestinationLine++) {

      for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
           SourcePixelX < SourceX + Width;
           SourcePixelX++, DestinationPixelX++)
      {
        // Calculate the source and target addresses:
        EfiSourcePixel  = BltBuffer + SourceLine * BltBufferHorizontalResolution + SourcePixelX;
        DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;

        // Copy the pixel into the new target
        // Only the most significant bits will be copied across:
        // To convert from 8 bits to 5 bits per pixel we throw away the 3 least significant bits
        *DestinationPixel16bit = (UINT16) (
              ( (EfiSourcePixel->Red      <<  7) & PixelInformation->RedMask      )
            | ( (EfiSourcePixel->Green    <<  2) & PixelInformation->GreenMask    )
            | ( (EfiSourcePixel->Blue     >>  3) & PixelInformation->BlueMask     )
      //            | ( 0                                & PixelInformation->ReservedMask )
            );
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_16_565:
    // Access each pixel inside the BltBuffer Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
         SourceLine < SourceY + Height;
         SourceLine++, DestinationLine++) {

      for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
           SourcePixelX < SourceX + Width;
           SourcePixelX++, DestinationPixelX++)
      {
        // Calculate the source and target addresses:
        EfiSourcePixel = BltBuffer + SourceLine * BltBufferHorizontalResolution + SourcePixelX;
        DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;

        // Copy the pixel into the new target
        // Only the most significant bits will be copied across:
        // To convert from 8 bits to 5 or 6 bits per pixel we throw away the 3 or 2  least significant bits
        // There is no room for the Reserved byte so we ignore that completely
        *DestinationPixel16bit = (UINT16) (
              ( (EfiSourcePixel->Red      <<  8) & PixelInformation->RedMask      )
            | ( (EfiSourcePixel->Green    <<  3) & PixelInformation->GreenMask    )
            | ( (EfiSourcePixel->Blue     >>  3) & PixelInformation->BlueMask     )
           );
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_12_444:
    // Access each pixel inside the BltBuffer Memory
    for (SourceLine = SourceY, DestinationLine = DestinationY;
         SourceLine < SourceY + Height;
         SourceLine++, DestinationLine++) {

      for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
           SourcePixelX < SourceX + Width;
           SourcePixelX++, DestinationPixelX++)
      {
        // Calculate the source and target addresses:
        EfiSourcePixel = BltBuffer + SourceLine * BltBufferHorizontalResolution + SourcePixelX;
        DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;

        // Copy the pixel into the new target
        // Only the most significant bits will be copied across:
        // To convert from 8 bits to 5 bits per pixel we throw away the 3 least significant bits
        *DestinationPixel16bit = (UINT16) (
              ( (EfiSourcePixel->Red      << 4) & PixelInformation->RedMask      )
            | ( (EfiSourcePixel->Green        ) & PixelInformation->GreenMask    )
            | ( (EfiSourcePixel->Blue     >> 4) & PixelInformation->BlueMask     )
  //            | ( 0                               & PixelInformation->ReservedMask )
           );
      }
    }
    break;

  case LCD_BITS_PER_PIXEL_8:
  case LCD_BITS_PER_PIXEL_4:
  case LCD_BITS_PER_PIXEL_2:
  case LCD_BITS_PER_PIXEL_1:
  default:
    // Can't handle this case
    DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: EfiBltBufferToVideo: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
    Status = EFI_INVALID_PARAMETER;
    break;
  }
  return Status;
}

STATIC
EFI_STATUS
BltVideoToVideo (
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL        *This,
  IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL   *BltBuffer,     OPTIONAL
  IN UINTN                               SourceX,
  IN UINTN                               SourceY,
  IN UINTN                               DestinationX,
  IN UINTN                               DestinationY,
  IN UINTN                               Width,
  IN UINTN                               Height,
  IN UINTN                               Delta           OPTIONAL   // Number of BYTES in a row of the BltBuffer
  )
{
  EFI_STATUS         Status;
  UINT32             HorizontalResolution;
  LCD_BPP            BitsPerPixel;
  VOID   *FrameBufferBase;

  HorizontalResolution = This->Mode->Info->HorizontalResolution;
  FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));

  //
  // BltVideo to BltVideo:
  //
  //  Source is the Video Memory,
  //  Destination is the Video Memory

  LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);
  FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));

  // The UEFI spec currently states:
  // "There is no limitation on the overlapping of the source and destination rectangles"
  // Therefore, we must be careful to avoid overwriting the source data
  if( SourceY == DestinationY ) {
    // Copying within the same height, e.g. horizontal shift
    if( SourceX == DestinationX ) {
      // Nothing to do
      Status = EFI_SUCCESS;
    } else if( ((SourceX>DestinationX)?(SourceX - DestinationX):(DestinationX - SourceX)) < Width ) {
      // There is overlap
      Status = VideoCopyHorizontalOverlap (BitsPerPixel, FrameBufferBase, HorizontalResolution, SourceX, SourceY, DestinationX, DestinationY, Width, Height );
    } else {
      // No overlap
      Status = VideoCopyNoHorizontalOverlap (BitsPerPixel, FrameBufferBase, HorizontalResolution, SourceX, SourceY, DestinationX, DestinationY, Width, Height );
    }
  } else {
    // Copying from different heights
    Status = VideoCopyNoHorizontalOverlap (BitsPerPixel, FrameBufferBase, HorizontalResolution, SourceX, SourceY, DestinationX, DestinationY, Width, Height );
  }

  return Status;
}

/***************************************
 * GraphicsOutput Protocol function, mapping to
 * EFI_GRAPHICS_OUTPUT_PROTOCOL.Blt
 *
 * PRESUMES: 1 pixel = 4 bytes (32bits)
 *  ***************************************/
EFI_STATUS
EFIAPI
LcdGraphicsBlt (
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL        *This,
  IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL   *BltBuffer,     OPTIONAL
  IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION   BltOperation,
  IN UINTN                               SourceX,
  IN UINTN                               SourceY,
  IN UINTN                               DestinationX,
  IN UINTN                               DestinationY,
  IN UINTN                               Width,
  IN UINTN                               Height,
  IN UINTN                               Delta           OPTIONAL   // Number of BYTES in a row of the BltBuffer
  )
{
  EFI_STATUS         Status;
  UINT32             HorizontalResolution;
  UINT32             VerticalResolution;
  LCD_INSTANCE*      Instance;

  Instance = LCD_INSTANCE_FROM_GOP_THIS(This);

  // Setup the hardware if not already done
  if (!mDisplayInitialized) {
    Status = InitializeDisplay (Instance);
    if (EFI_ERROR(Status)) {
      goto EXIT;
    }
  }

  HorizontalResolution = This->Mode->Info->HorizontalResolution;
  VerticalResolution   = This->Mode->Info->VerticalResolution;

  DEBUG((DEBUG_INFO, "LcdGraphicsBlt (BltOperation:%d,DestX:%d,DestY:%d,Width:%d,Height:%d) res(%d,%d)\n",
      BltOperation,DestinationX,DestinationY,Width,Height,HorizontalResolution,VerticalResolution));

  // Check we have reasonable parameters
  if (Width == 0 || Height == 0) {
    DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: ERROR - Invalid dimension: Zero size area.\n" ));
    Status = EFI_INVALID_PARAMETER;
    goto EXIT;
  }

  if ((BltOperation == EfiBltVideoFill) || (BltOperation == EfiBltBufferToVideo) || (BltOperation == EfiBltVideoToBltBuffer)) {
    ASSERT( BltBuffer != NULL);
  }

  /*if ((DestinationX >= HorizontalResolution) || (DestinationY >= VerticalResolution)) {
    DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: ERROR - Invalid destination.\n" ));
    Status = EFI_INVALID_PARAMETER;
    goto EXIT;
  }*/

  // If we are reading data out of the video buffer, check that the source area is within the display limits
  if ((BltOperation == EfiBltVideoToBltBuffer) || (BltOperation == EfiBltVideoToVideo)) {
    if ((SourceY + Height > VerticalResolution) || (SourceX + Width > HorizontalResolution)) {
      DEBUG((DEBUG_INFO, "LcdGraphicsBlt: ERROR - Invalid source resolution.\n" ));
      DEBUG((DEBUG_INFO, "                      - SourceY=%d + Height=%d > VerticalResolution=%d.\n", SourceY, Height, VerticalResolution ));
      DEBUG((DEBUG_INFO, "                      - SourceX=%d + Width=%d > HorizontalResolution=%d.\n", SourceX, Width, HorizontalResolution ));
      Status = EFI_INVALID_PARAMETER;
      goto EXIT;
    }
  }

  // If we are writing data into the video buffer, that the destination area is within the display limits
  if ((BltOperation == EfiBltVideoFill) || (BltOperation == EfiBltBufferToVideo) || (BltOperation == EfiBltVideoToVideo)) {
    if ((DestinationY + Height > VerticalResolution) || (DestinationX + Width > HorizontalResolution)) {
      DEBUG((DEBUG_INFO, "LcdGraphicsBlt: ERROR - Invalid destination resolution.\n" ));
      DEBUG((DEBUG_INFO, "                      - DestinationY=%d + Height=%d > VerticalResolution=%d.\n", DestinationY, Height, VerticalResolution ));
      DEBUG((DEBUG_INFO, "                      - DestinationX=%d + Width=%d > HorizontalResolution=%d.\n", DestinationX, Width, HorizontalResolution ));
      Status = EFI_INVALID_PARAMETER;
      goto EXIT;
    }
  }

  //
  // Perform the Block Transfer Operation
  //

  switch (BltOperation) {
  case EfiBltVideoFill:
    Status = BltVideoFill (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
    break;

  case EfiBltVideoToBltBuffer:
    Status = BltVideoToBltBuffer (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
    break;

  case EfiBltBufferToVideo:
    Status = BltBufferToVideo (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
    break;

  case EfiBltVideoToVideo:
    Status = BltVideoToVideo (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
    break;

  case EfiGraphicsOutputBltOperationMax:
  default:
    DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: Invalid Operation\n"));
    Status = EFI_INVALID_PARAMETER;
    break;
  }

EXIT:
  return Status;
}