LcdGraphicsOutputBlt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /** @file
  2. Copyright (c) 2011-2013, ARM Ltd. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <PiDxe.h>
  6. #include <Library/BaseMemoryLib.h>
  7. #include <Library/DevicePathLib.h>
  8. #include <Library/UefiBootServicesTableLib.h>
  9. #include <Library/UefiRuntimeServicesTableLib.h>
  10. #include <Library/MemoryAllocationLib.h>
  11. #include <Guid/GlobalVariable.h>
  12. #include "LcdGraphicsOutputDxe.h"
  13. extern BOOLEAN mDisplayInitialized;
  14. //
  15. // Function Definitions
  16. //
  17. STATIC
  18. EFI_STATUS
  19. VideoCopyNoHorizontalOverlap (
  20. IN UINTN BitsPerPixel,
  21. IN volatile VOID *FrameBufferBase,
  22. IN UINT32 HorizontalResolution,
  23. IN UINTN SourceX,
  24. IN UINTN SourceY,
  25. IN UINTN DestinationX,
  26. IN UINTN DestinationY,
  27. IN UINTN Width,
  28. IN UINTN Height
  29. )
  30. {
  31. EFI_STATUS Status = EFI_SUCCESS;
  32. UINTN SourceLine;
  33. UINTN DestinationLine;
  34. UINTN WidthInBytes;
  35. UINTN LineCount;
  36. INTN Step;
  37. VOID *SourceAddr;
  38. VOID *DestinationAddr;
  39. if( DestinationY <= SourceY ) {
  40. // scrolling up (or horizontally but without overlap)
  41. SourceLine = SourceY;
  42. DestinationLine = DestinationY;
  43. Step = 1;
  44. } else {
  45. // scrolling down
  46. SourceLine = SourceY + Height;
  47. DestinationLine = DestinationY + Height;
  48. Step = -1;
  49. }
  50. switch (BitsPerPixel) {
  51. case LCD_BITS_PER_PIXEL_24:
  52. WidthInBytes = Width * 4;
  53. for( LineCount = 0; LineCount < Height; LineCount++ ) {
  54. // Update the start addresses of source & destination using 32bit pointer arithmetic
  55. SourceAddr = (VOID *)((UINT32 *)FrameBufferBase + SourceLine * HorizontalResolution + SourceX );
  56. DestinationAddr = (VOID *)((UINT32 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationX);
  57. // Copy the entire line Y from video ram to the temp buffer
  58. CopyMem( DestinationAddr, SourceAddr, WidthInBytes);
  59. // Update the line numbers
  60. SourceLine += Step;
  61. DestinationLine += Step;
  62. }
  63. break;
  64. case LCD_BITS_PER_PIXEL_16_555:
  65. case LCD_BITS_PER_PIXEL_16_565:
  66. case LCD_BITS_PER_PIXEL_12_444:
  67. WidthInBytes = Width * 2;
  68. for( LineCount = 0; LineCount < Height; LineCount++ ) {
  69. // Update the start addresses of source & destination using 16bit pointer arithmetic
  70. SourceAddr = (VOID *)((UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourceX );
  71. DestinationAddr = (VOID *)((UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationX);
  72. // Copy the entire line Y from video ram to the temp buffer
  73. CopyMem( DestinationAddr, SourceAddr, WidthInBytes);
  74. // Update the line numbers
  75. SourceLine += Step;
  76. DestinationLine += Step;
  77. }
  78. break;
  79. case LCD_BITS_PER_PIXEL_8:
  80. case LCD_BITS_PER_PIXEL_4:
  81. case LCD_BITS_PER_PIXEL_2:
  82. case LCD_BITS_PER_PIXEL_1:
  83. default:
  84. // Can't handle this case
  85. DEBUG((DEBUG_ERROR, "ArmVeGraphics_Blt: EfiBltVideoToVideo: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
  86. Status = EFI_INVALID_PARAMETER;
  87. goto EXIT;
  88. // break;
  89. }
  90. EXIT:
  91. return Status;
  92. }
  93. STATIC
  94. EFI_STATUS
  95. VideoCopyHorizontalOverlap (
  96. IN UINTN BitsPerPixel,
  97. IN volatile VOID *FrameBufferBase,
  98. UINT32 HorizontalResolution,
  99. IN UINTN SourceX,
  100. IN UINTN SourceY,
  101. IN UINTN DestinationX,
  102. IN UINTN DestinationY,
  103. IN UINTN Width,
  104. IN UINTN Height
  105. )
  106. {
  107. EFI_STATUS Status = EFI_SUCCESS;
  108. UINT32 *PixelBuffer32bit;
  109. UINT32 *SourcePixel32bit;
  110. UINT32 *DestinationPixel32bit;
  111. UINT16 *PixelBuffer16bit;
  112. UINT16 *SourcePixel16bit;
  113. UINT16 *DestinationPixel16bit;
  114. UINT32 SourcePixelY;
  115. UINT32 DestinationPixelY;
  116. UINTN SizeIn32Bits;
  117. UINTN SizeIn16Bits;
  118. switch (BitsPerPixel) {
  119. case LCD_BITS_PER_PIXEL_24:
  120. // Allocate a temporary buffer
  121. PixelBuffer32bit = (UINT32 *) AllocatePool((Height * Width) * sizeof(UINT32));
  122. if (PixelBuffer32bit == NULL) {
  123. Status = EFI_OUT_OF_RESOURCES;
  124. goto EXIT;
  125. }
  126. SizeIn32Bits = Width * 4;
  127. // Copy from the video ram (source region) to a temp buffer
  128. for (SourcePixelY = SourceY, DestinationPixel32bit = PixelBuffer32bit;
  129. SourcePixelY < SourceY + Height;
  130. SourcePixelY++, DestinationPixel32bit += Width)
  131. {
  132. // Update the start address of line Y (source)
  133. SourcePixel32bit = (UINT32 *)FrameBufferBase + SourcePixelY * HorizontalResolution + SourceX;
  134. // Copy the entire line Y from video ram to the temp buffer
  135. CopyMem( (VOID *)DestinationPixel32bit, (CONST VOID *)SourcePixel32bit, SizeIn32Bits);
  136. }
  137. // Copy from the temp buffer to the video ram (destination region)
  138. for (DestinationPixelY = DestinationY, SourcePixel32bit = PixelBuffer32bit;
  139. DestinationPixelY < DestinationY + Height;
  140. DestinationPixelY++, SourcePixel32bit += Width)
  141. {
  142. // Update the start address of line Y (target)
  143. DestinationPixel32bit = (UINT32 *)FrameBufferBase + DestinationPixelY * HorizontalResolution + DestinationX;
  144. // Copy the entire line Y from the temp buffer to video ram
  145. CopyMem( (VOID *)DestinationPixel32bit, (CONST VOID *)SourcePixel32bit, SizeIn32Bits);
  146. }
  147. // Free up the allocated memory
  148. FreePool((VOID *) PixelBuffer32bit);
  149. break;
  150. case LCD_BITS_PER_PIXEL_16_555:
  151. case LCD_BITS_PER_PIXEL_16_565:
  152. case LCD_BITS_PER_PIXEL_12_444:
  153. // Allocate a temporary buffer
  154. PixelBuffer16bit = (UINT16 *) AllocatePool((Height * Width) * sizeof(UINT16));
  155. if (PixelBuffer16bit == NULL) {
  156. Status = EFI_OUT_OF_RESOURCES;
  157. goto EXIT;
  158. }
  159. // Access each pixel inside the source area of the Video Memory and copy it to the temp buffer
  160. SizeIn16Bits = Width * 2;
  161. for (SourcePixelY = SourceY, DestinationPixel16bit = PixelBuffer16bit;
  162. SourcePixelY < SourceY + Height;
  163. SourcePixelY++, DestinationPixel16bit += Width)
  164. {
  165. // Calculate the source address:
  166. SourcePixel16bit = (UINT16 *)FrameBufferBase + SourcePixelY * HorizontalResolution + SourceX;
  167. // Copy the entire line Y from Video to the temp buffer
  168. CopyMem( (VOID *)DestinationPixel16bit, (CONST VOID *)SourcePixel16bit, SizeIn16Bits);
  169. }
  170. // Copy from the temp buffer into the destination area of the Video Memory
  171. for (DestinationPixelY = DestinationY, SourcePixel16bit = PixelBuffer16bit;
  172. DestinationPixelY < DestinationY + Height;
  173. DestinationPixelY++, SourcePixel16bit += Width)
  174. {
  175. // Calculate the target address:
  176. DestinationPixel16bit = (UINT16 *)FrameBufferBase + (DestinationPixelY * HorizontalResolution + DestinationX);
  177. // Copy the entire line Y from the temp buffer to Video
  178. CopyMem( (VOID *)DestinationPixel16bit, (CONST VOID *)SourcePixel16bit, SizeIn16Bits);
  179. }
  180. // Free the allocated memory
  181. FreePool((VOID *) PixelBuffer16bit);
  182. break;
  183. case LCD_BITS_PER_PIXEL_8:
  184. case LCD_BITS_PER_PIXEL_4:
  185. case LCD_BITS_PER_PIXEL_2:
  186. case LCD_BITS_PER_PIXEL_1:
  187. default:
  188. // Can't handle this case
  189. DEBUG((DEBUG_ERROR, "ArmVeGraphics_Blt: EfiBltVideoToVideo: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
  190. Status = EFI_INVALID_PARAMETER;
  191. goto EXIT;
  192. // break;
  193. }
  194. EXIT:
  195. return Status;
  196. }
  197. STATIC
  198. EFI_STATUS
  199. BltVideoFill (
  200. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  201. IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *EfiSourcePixel, OPTIONAL
  202. IN UINTN SourceX,
  203. IN UINTN SourceY,
  204. IN UINTN DestinationX,
  205. IN UINTN DestinationY,
  206. IN UINTN Width,
  207. IN UINTN Height,
  208. IN UINTN Delta OPTIONAL // Number of BYTES in a row of the BltBuffer
  209. )
  210. {
  211. EFI_PIXEL_BITMASK* PixelInformation;
  212. EFI_STATUS Status;
  213. UINT32 HorizontalResolution;
  214. LCD_BPP BitsPerPixel;
  215. VOID *FrameBufferBase;
  216. VOID *DestinationAddr;
  217. UINT16 *DestinationPixel16bit;
  218. UINT16 Pixel16bit;
  219. UINT32 DestinationPixelX;
  220. UINT32 DestinationLine;
  221. UINTN WidthInBytes;
  222. Status = EFI_SUCCESS;
  223. PixelInformation = &This->Mode->Info->PixelInformation;
  224. FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));
  225. HorizontalResolution = This->Mode->Info->HorizontalResolution;
  226. LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);
  227. switch (BitsPerPixel) {
  228. case LCD_BITS_PER_PIXEL_24:
  229. WidthInBytes = Width * 4;
  230. // Copy the SourcePixel into every pixel inside the target rectangle
  231. for (DestinationLine = DestinationY;
  232. DestinationLine < DestinationY + Height;
  233. DestinationLine++)
  234. {
  235. // Calculate the target address using 32bit pointer arithmetic:
  236. DestinationAddr = (VOID *)((UINT32 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationX);
  237. // Fill the entire line
  238. SetMem32 (DestinationAddr, WidthInBytes, *((UINT32 *)EfiSourcePixel));
  239. }
  240. break;
  241. case LCD_BITS_PER_PIXEL_16_555:
  242. // Convert the EFI pixel at the start of the BltBuffer(0,0) into a video display pixel
  243. Pixel16bit = (UINT16) (
  244. ( (EfiSourcePixel->Red << 7) & PixelInformation->RedMask )
  245. | ( (EfiSourcePixel->Green << 2) & PixelInformation->GreenMask )
  246. | ( (EfiSourcePixel->Blue >> 3) & PixelInformation->BlueMask )
  247. // | ( 0 & PixelInformation->ReservedMask )
  248. );
  249. // Copy the SourcePixel into every pixel inside the target rectangle
  250. for (DestinationLine = DestinationY;
  251. DestinationLine < DestinationY + Height;
  252. DestinationLine++)
  253. {
  254. for (DestinationPixelX = DestinationX;
  255. DestinationPixelX < DestinationX + Width;
  256. DestinationPixelX++)
  257. {
  258. // Calculate the target address:
  259. DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;
  260. // Copy the pixel into the new target
  261. *DestinationPixel16bit = Pixel16bit;
  262. }
  263. }
  264. break;
  265. case LCD_BITS_PER_PIXEL_16_565:
  266. // Convert the EFI pixel at the start of the BltBuffer(0,0) into a video display pixel
  267. Pixel16bit = (UINT16) (
  268. ( (EfiSourcePixel->Red << 8) & PixelInformation->RedMask )
  269. | ( (EfiSourcePixel->Green << 3) & PixelInformation->GreenMask )
  270. | ( (EfiSourcePixel->Blue >> 3) & PixelInformation->BlueMask )
  271. );
  272. // Copy the SourcePixel into every pixel inside the target rectangle
  273. for (DestinationLine = DestinationY;
  274. DestinationLine < DestinationY + Height;
  275. DestinationLine++)
  276. {
  277. for (DestinationPixelX = DestinationX;
  278. DestinationPixelX < DestinationX + Width;
  279. DestinationPixelX++)
  280. {
  281. // Calculate the target address:
  282. DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;
  283. // Copy the pixel into the new target
  284. *DestinationPixel16bit = Pixel16bit;
  285. }
  286. }
  287. break;
  288. case LCD_BITS_PER_PIXEL_12_444:
  289. // Convert the EFI pixel at the start of the BltBuffer(0,0) into a video display pixel
  290. Pixel16bit = (UINT16) (
  291. ( (EfiSourcePixel->Red >> 4) & PixelInformation->RedMask )
  292. | ( (EfiSourcePixel->Green ) & PixelInformation->GreenMask )
  293. | ( (EfiSourcePixel->Blue << 4) & PixelInformation->BlueMask )
  294. );
  295. // Copy the SourcePixel into every pixel inside the target rectangle
  296. for (DestinationLine = DestinationY;
  297. DestinationLine < DestinationY + Height;
  298. DestinationLine++)
  299. {
  300. for (DestinationPixelX = DestinationX;
  301. DestinationPixelX < DestinationX + Width;
  302. DestinationPixelX++)
  303. {
  304. // Calculate the target address:
  305. DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;
  306. // Copy the pixel into the new target
  307. *DestinationPixel16bit = Pixel16bit;
  308. }
  309. }
  310. break;
  311. case LCD_BITS_PER_PIXEL_8:
  312. case LCD_BITS_PER_PIXEL_4:
  313. case LCD_BITS_PER_PIXEL_2:
  314. case LCD_BITS_PER_PIXEL_1:
  315. default:
  316. // Can't handle this case
  317. DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: EfiBltVideoFill: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
  318. Status = EFI_INVALID_PARAMETER;
  319. break;
  320. }
  321. return Status;
  322. }
  323. STATIC
  324. EFI_STATUS
  325. BltVideoToBltBuffer (
  326. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  327. IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
  328. IN UINTN SourceX,
  329. IN UINTN SourceY,
  330. IN UINTN DestinationX,
  331. IN UINTN DestinationY,
  332. IN UINTN Width,
  333. IN UINTN Height,
  334. IN UINTN Delta OPTIONAL // Number of BYTES in a row of the BltBuffer
  335. )
  336. {
  337. EFI_STATUS Status;
  338. UINT32 HorizontalResolution;
  339. LCD_BPP BitsPerPixel;
  340. EFI_PIXEL_BITMASK *PixelInformation;
  341. EFI_GRAPHICS_OUTPUT_BLT_PIXEL *EfiDestinationPixel;
  342. VOID *FrameBufferBase;
  343. VOID *SourceAddr;
  344. VOID *DestinationAddr;
  345. UINT16 *SourcePixel16bit;
  346. UINT16 Pixel16bit;
  347. UINT32 SourcePixelX;
  348. UINT32 SourceLine;
  349. UINT32 DestinationPixelX;
  350. UINT32 DestinationLine;
  351. UINT32 BltBufferHorizontalResolution;
  352. UINTN WidthInBytes;
  353. Status = EFI_SUCCESS;
  354. PixelInformation = &This->Mode->Info->PixelInformation;
  355. HorizontalResolution = This->Mode->Info->HorizontalResolution;
  356. FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));
  357. if(( Delta != 0 ) && ( Delta != Width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
  358. // Delta is not zero and it is different from the width.
  359. // Divide it by the size of a pixel to find out the buffer's horizontal resolution.
  360. BltBufferHorizontalResolution = (UINT32) (Delta / sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
  361. } else {
  362. BltBufferHorizontalResolution = Width;
  363. }
  364. LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);
  365. switch (BitsPerPixel) {
  366. case LCD_BITS_PER_PIXEL_24:
  367. WidthInBytes = Width * 4;
  368. // Access each line inside the Video Memory
  369. for (SourceLine = SourceY, DestinationLine = DestinationY;
  370. SourceLine < SourceY + Height;
  371. SourceLine++, DestinationLine++)
  372. {
  373. // Calculate the source and target addresses using 32bit pointer arithmetic:
  374. SourceAddr = (VOID *)((UINT32 *)FrameBufferBase + SourceLine * HorizontalResolution + SourceX );
  375. DestinationAddr = (VOID *)((UINT32 *)BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationX);
  376. // Copy the entire line
  377. CopyMem( DestinationAddr, SourceAddr, WidthInBytes);
  378. }
  379. break;
  380. case LCD_BITS_PER_PIXEL_16_555:
  381. // Access each pixel inside the Video Memory
  382. for (SourceLine = SourceY, DestinationLine = DestinationY;
  383. SourceLine < SourceY + Height;
  384. SourceLine++, DestinationLine++)
  385. {
  386. for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
  387. SourcePixelX < SourceX + Width;
  388. SourcePixelX++, DestinationPixelX++)
  389. {
  390. // Calculate the source and target addresses:
  391. SourcePixel16bit = (UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourcePixelX;
  392. EfiDestinationPixel = BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationPixelX;
  393. // Snapshot the pixel from the video buffer once, to speed up the operation.
  394. // If we were dereferencing the pointer, as it is volatile, we would perform 3 memory read operations.
  395. Pixel16bit = *SourcePixel16bit;
  396. // Copy the pixel into the new target
  397. EfiDestinationPixel->Red = (UINT8) ( (Pixel16bit & PixelInformation->RedMask ) >> 7 );
  398. EfiDestinationPixel->Green = (UINT8) ( (Pixel16bit & PixelInformation->GreenMask ) >> 2);
  399. EfiDestinationPixel->Blue = (UINT8) ( (Pixel16bit & PixelInformation->BlueMask ) << 3 );
  400. // EfiDestinationPixel->Reserved = (UINT8) 0;
  401. }
  402. }
  403. break;
  404. case LCD_BITS_PER_PIXEL_16_565:
  405. // Access each pixel inside the Video Memory
  406. for (SourceLine = SourceY, DestinationLine = DestinationY;
  407. SourceLine < SourceY + Height;
  408. SourceLine++, DestinationLine++)
  409. {
  410. for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
  411. SourcePixelX < SourceX + Width;
  412. SourcePixelX++, DestinationPixelX++)
  413. {
  414. // Calculate the source and target addresses:
  415. SourcePixel16bit = (UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourcePixelX;
  416. EfiDestinationPixel = BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationPixelX;
  417. // Snapshot the pixel from the video buffer once, to speed up the operation.
  418. // If we were dereferencing the pointer, as it is volatile, we would perform 3 memory read operations.
  419. Pixel16bit = *SourcePixel16bit;
  420. // Copy the pixel into the new target
  421. // There is no info for the Reserved byte, so we set it to zero
  422. EfiDestinationPixel->Red = (UINT8) ( (Pixel16bit & PixelInformation->RedMask ) >> 8 );
  423. EfiDestinationPixel->Green = (UINT8) ( (Pixel16bit & PixelInformation->GreenMask ) >> 3);
  424. EfiDestinationPixel->Blue = (UINT8) ( (Pixel16bit & PixelInformation->BlueMask ) << 3 );
  425. // EfiDestinationPixel->Reserved = (UINT8) 0;
  426. }
  427. }
  428. break;
  429. case LCD_BITS_PER_PIXEL_12_444:
  430. // Access each pixel inside the Video Memory
  431. for (SourceLine = SourceY, DestinationLine = DestinationY;
  432. SourceLine < SourceY + Height;
  433. SourceLine++, DestinationLine++)
  434. {
  435. for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
  436. SourcePixelX < SourceX + Width;
  437. SourcePixelX++, DestinationPixelX++)
  438. {
  439. // Calculate the source and target addresses:
  440. SourcePixel16bit = (UINT16 *)FrameBufferBase + SourceLine * HorizontalResolution + SourcePixelX;
  441. EfiDestinationPixel = BltBuffer + DestinationLine * BltBufferHorizontalResolution + DestinationPixelX;
  442. // Snapshot the pixel from the video buffer once, to speed up the operation.
  443. // If we were dereferencing the pointer, as it is volatile, we would perform 3 memory read operations.
  444. Pixel16bit = *SourcePixel16bit;
  445. // Copy the pixel into the new target
  446. EfiDestinationPixel->Red = (UINT8) ( (Pixel16bit & PixelInformation->RedMask ) >> 4 );
  447. EfiDestinationPixel->Green = (UINT8) ( (Pixel16bit & PixelInformation->GreenMask ) );
  448. EfiDestinationPixel->Blue = (UINT8) ( (Pixel16bit & PixelInformation->BlueMask ) << 4 );
  449. // EfiDestinationPixel->Reserved = (UINT8) 0;
  450. }
  451. }
  452. break;
  453. case LCD_BITS_PER_PIXEL_8:
  454. case LCD_BITS_PER_PIXEL_4:
  455. case LCD_BITS_PER_PIXEL_2:
  456. case LCD_BITS_PER_PIXEL_1:
  457. default:
  458. // Can't handle this case
  459. DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: EfiBltVideoToBltBuffer: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
  460. Status = EFI_INVALID_PARAMETER;
  461. break;
  462. }
  463. return Status;
  464. }
  465. STATIC
  466. EFI_STATUS
  467. BltBufferToVideo (
  468. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  469. IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
  470. IN UINTN SourceX,
  471. IN UINTN SourceY,
  472. IN UINTN DestinationX,
  473. IN UINTN DestinationY,
  474. IN UINTN Width,
  475. IN UINTN Height,
  476. IN UINTN Delta OPTIONAL // Number of BYTES in a row of the BltBuffer
  477. )
  478. {
  479. EFI_STATUS Status;
  480. UINT32 HorizontalResolution;
  481. LCD_BPP BitsPerPixel;
  482. EFI_PIXEL_BITMASK *PixelInformation;
  483. EFI_GRAPHICS_OUTPUT_BLT_PIXEL *EfiSourcePixel;
  484. VOID *FrameBufferBase;
  485. VOID *SourceAddr;
  486. VOID *DestinationAddr;
  487. UINT16 *DestinationPixel16bit;
  488. UINT32 SourcePixelX;
  489. UINT32 SourceLine;
  490. UINT32 DestinationPixelX;
  491. UINT32 DestinationLine;
  492. UINT32 BltBufferHorizontalResolution;
  493. UINTN WidthInBytes;
  494. Status = EFI_SUCCESS;
  495. PixelInformation = &This->Mode->Info->PixelInformation;
  496. HorizontalResolution = This->Mode->Info->HorizontalResolution;
  497. FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));
  498. if(( Delta != 0 ) && ( Delta != Width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL))) {
  499. // Delta is not zero and it is different from the width.
  500. // Divide it by the size of a pixel to find out the buffer's horizontal resolution.
  501. BltBufferHorizontalResolution = (UINT32) (Delta / sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL));
  502. } else {
  503. BltBufferHorizontalResolution = Width;
  504. }
  505. LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);
  506. switch (BitsPerPixel) {
  507. case LCD_BITS_PER_PIXEL_24:
  508. WidthInBytes = Width * 4;
  509. // Access each pixel inside the BltBuffer Memory
  510. for (SourceLine = SourceY, DestinationLine = DestinationY;
  511. SourceLine < SourceY + Height;
  512. SourceLine++, DestinationLine++)
  513. {
  514. // Calculate the source and target addresses using 32bit pointer arithmetic:
  515. SourceAddr = (VOID *)((UINT32 *)BltBuffer + SourceLine * BltBufferHorizontalResolution + SourceX );
  516. DestinationAddr = (VOID *)((UINT32 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationX);
  517. // Copy the entire row Y
  518. CopyMem( DestinationAddr, SourceAddr, WidthInBytes);
  519. }
  520. break;
  521. case LCD_BITS_PER_PIXEL_16_555:
  522. // Access each pixel inside the BltBuffer Memory
  523. for (SourceLine = SourceY, DestinationLine = DestinationY;
  524. SourceLine < SourceY + Height;
  525. SourceLine++, DestinationLine++) {
  526. for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
  527. SourcePixelX < SourceX + Width;
  528. SourcePixelX++, DestinationPixelX++)
  529. {
  530. // Calculate the source and target addresses:
  531. EfiSourcePixel = BltBuffer + SourceLine * BltBufferHorizontalResolution + SourcePixelX;
  532. DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;
  533. // Copy the pixel into the new target
  534. // Only the most significant bits will be copied across:
  535. // To convert from 8 bits to 5 bits per pixel we throw away the 3 least significant bits
  536. *DestinationPixel16bit = (UINT16) (
  537. ( (EfiSourcePixel->Red << 7) & PixelInformation->RedMask )
  538. | ( (EfiSourcePixel->Green << 2) & PixelInformation->GreenMask )
  539. | ( (EfiSourcePixel->Blue >> 3) & PixelInformation->BlueMask )
  540. // | ( 0 & PixelInformation->ReservedMask )
  541. );
  542. }
  543. }
  544. break;
  545. case LCD_BITS_PER_PIXEL_16_565:
  546. // Access each pixel inside the BltBuffer Memory
  547. for (SourceLine = SourceY, DestinationLine = DestinationY;
  548. SourceLine < SourceY + Height;
  549. SourceLine++, DestinationLine++) {
  550. for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
  551. SourcePixelX < SourceX + Width;
  552. SourcePixelX++, DestinationPixelX++)
  553. {
  554. // Calculate the source and target addresses:
  555. EfiSourcePixel = BltBuffer + SourceLine * BltBufferHorizontalResolution + SourcePixelX;
  556. DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;
  557. // Copy the pixel into the new target
  558. // Only the most significant bits will be copied across:
  559. // To convert from 8 bits to 5 or 6 bits per pixel we throw away the 3 or 2 least significant bits
  560. // There is no room for the Reserved byte so we ignore that completely
  561. *DestinationPixel16bit = (UINT16) (
  562. ( (EfiSourcePixel->Red << 8) & PixelInformation->RedMask )
  563. | ( (EfiSourcePixel->Green << 3) & PixelInformation->GreenMask )
  564. | ( (EfiSourcePixel->Blue >> 3) & PixelInformation->BlueMask )
  565. );
  566. }
  567. }
  568. break;
  569. case LCD_BITS_PER_PIXEL_12_444:
  570. // Access each pixel inside the BltBuffer Memory
  571. for (SourceLine = SourceY, DestinationLine = DestinationY;
  572. SourceLine < SourceY + Height;
  573. SourceLine++, DestinationLine++) {
  574. for (SourcePixelX = SourceX, DestinationPixelX = DestinationX;
  575. SourcePixelX < SourceX + Width;
  576. SourcePixelX++, DestinationPixelX++)
  577. {
  578. // Calculate the source and target addresses:
  579. EfiSourcePixel = BltBuffer + SourceLine * BltBufferHorizontalResolution + SourcePixelX;
  580. DestinationPixel16bit = (UINT16 *)FrameBufferBase + DestinationLine * HorizontalResolution + DestinationPixelX;
  581. // Copy the pixel into the new target
  582. // Only the most significant bits will be copied across:
  583. // To convert from 8 bits to 5 bits per pixel we throw away the 3 least significant bits
  584. *DestinationPixel16bit = (UINT16) (
  585. ( (EfiSourcePixel->Red << 4) & PixelInformation->RedMask )
  586. | ( (EfiSourcePixel->Green ) & PixelInformation->GreenMask )
  587. | ( (EfiSourcePixel->Blue >> 4) & PixelInformation->BlueMask )
  588. // | ( 0 & PixelInformation->ReservedMask )
  589. );
  590. }
  591. }
  592. break;
  593. case LCD_BITS_PER_PIXEL_8:
  594. case LCD_BITS_PER_PIXEL_4:
  595. case LCD_BITS_PER_PIXEL_2:
  596. case LCD_BITS_PER_PIXEL_1:
  597. default:
  598. // Can't handle this case
  599. DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: EfiBltBufferToVideo: INVALID Number of Bits Per Pixel: %d\n", BitsPerPixel));
  600. Status = EFI_INVALID_PARAMETER;
  601. break;
  602. }
  603. return Status;
  604. }
  605. STATIC
  606. EFI_STATUS
  607. BltVideoToVideo (
  608. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  609. IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
  610. IN UINTN SourceX,
  611. IN UINTN SourceY,
  612. IN UINTN DestinationX,
  613. IN UINTN DestinationY,
  614. IN UINTN Width,
  615. IN UINTN Height,
  616. IN UINTN Delta OPTIONAL // Number of BYTES in a row of the BltBuffer
  617. )
  618. {
  619. EFI_STATUS Status;
  620. UINT32 HorizontalResolution;
  621. LCD_BPP BitsPerPixel;
  622. VOID *FrameBufferBase;
  623. HorizontalResolution = This->Mode->Info->HorizontalResolution;
  624. FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));
  625. //
  626. // BltVideo to BltVideo:
  627. //
  628. // Source is the Video Memory,
  629. // Destination is the Video Memory
  630. LcdPlatformGetBpp (This->Mode->Mode,&BitsPerPixel);
  631. FrameBufferBase = (UINTN *)((UINTN)(This->Mode->FrameBufferBase));
  632. // The UEFI spec currently states:
  633. // "There is no limitation on the overlapping of the source and destination rectangles"
  634. // Therefore, we must be careful to avoid overwriting the source data
  635. if( SourceY == DestinationY ) {
  636. // Copying within the same height, e.g. horizontal shift
  637. if( SourceX == DestinationX ) {
  638. // Nothing to do
  639. Status = EFI_SUCCESS;
  640. } else if( ((SourceX>DestinationX)?(SourceX - DestinationX):(DestinationX - SourceX)) < Width ) {
  641. // There is overlap
  642. Status = VideoCopyHorizontalOverlap (BitsPerPixel, FrameBufferBase, HorizontalResolution, SourceX, SourceY, DestinationX, DestinationY, Width, Height );
  643. } else {
  644. // No overlap
  645. Status = VideoCopyNoHorizontalOverlap (BitsPerPixel, FrameBufferBase, HorizontalResolution, SourceX, SourceY, DestinationX, DestinationY, Width, Height );
  646. }
  647. } else {
  648. // Copying from different heights
  649. Status = VideoCopyNoHorizontalOverlap (BitsPerPixel, FrameBufferBase, HorizontalResolution, SourceX, SourceY, DestinationX, DestinationY, Width, Height );
  650. }
  651. return Status;
  652. }
  653. /***************************************
  654. * GraphicsOutput Protocol function, mapping to
  655. * EFI_GRAPHICS_OUTPUT_PROTOCOL.Blt
  656. *
  657. * PRESUMES: 1 pixel = 4 bytes (32bits)
  658. * ***************************************/
  659. EFI_STATUS
  660. EFIAPI
  661. LcdGraphicsBlt (
  662. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  663. IN OUT EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
  664. IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
  665. IN UINTN SourceX,
  666. IN UINTN SourceY,
  667. IN UINTN DestinationX,
  668. IN UINTN DestinationY,
  669. IN UINTN Width,
  670. IN UINTN Height,
  671. IN UINTN Delta OPTIONAL // Number of BYTES in a row of the BltBuffer
  672. )
  673. {
  674. EFI_STATUS Status;
  675. UINT32 HorizontalResolution;
  676. UINT32 VerticalResolution;
  677. LCD_INSTANCE* Instance;
  678. Instance = LCD_INSTANCE_FROM_GOP_THIS(This);
  679. // Setup the hardware if not already done
  680. if (!mDisplayInitialized) {
  681. Status = InitializeDisplay (Instance);
  682. if (EFI_ERROR(Status)) {
  683. goto EXIT;
  684. }
  685. }
  686. HorizontalResolution = This->Mode->Info->HorizontalResolution;
  687. VerticalResolution = This->Mode->Info->VerticalResolution;
  688. DEBUG((DEBUG_INFO, "LcdGraphicsBlt (BltOperation:%d,DestX:%d,DestY:%d,Width:%d,Height:%d) res(%d,%d)\n",
  689. BltOperation,DestinationX,DestinationY,Width,Height,HorizontalResolution,VerticalResolution));
  690. // Check we have reasonable parameters
  691. if (Width == 0 || Height == 0) {
  692. DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: ERROR - Invalid dimension: Zero size area.\n" ));
  693. Status = EFI_INVALID_PARAMETER;
  694. goto EXIT;
  695. }
  696. if ((BltOperation == EfiBltVideoFill) || (BltOperation == EfiBltBufferToVideo) || (BltOperation == EfiBltVideoToBltBuffer)) {
  697. ASSERT( BltBuffer != NULL);
  698. }
  699. /*if ((DestinationX >= HorizontalResolution) || (DestinationY >= VerticalResolution)) {
  700. DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: ERROR - Invalid destination.\n" ));
  701. Status = EFI_INVALID_PARAMETER;
  702. goto EXIT;
  703. }*/
  704. // If we are reading data out of the video buffer, check that the source area is within the display limits
  705. if ((BltOperation == EfiBltVideoToBltBuffer) || (BltOperation == EfiBltVideoToVideo)) {
  706. if ((SourceY + Height > VerticalResolution) || (SourceX + Width > HorizontalResolution)) {
  707. DEBUG((DEBUG_INFO, "LcdGraphicsBlt: ERROR - Invalid source resolution.\n" ));
  708. DEBUG((DEBUG_INFO, " - SourceY=%d + Height=%d > VerticalResolution=%d.\n", SourceY, Height, VerticalResolution ));
  709. DEBUG((DEBUG_INFO, " - SourceX=%d + Width=%d > HorizontalResolution=%d.\n", SourceX, Width, HorizontalResolution ));
  710. Status = EFI_INVALID_PARAMETER;
  711. goto EXIT;
  712. }
  713. }
  714. // If we are writing data into the video buffer, that the destination area is within the display limits
  715. if ((BltOperation == EfiBltVideoFill) || (BltOperation == EfiBltBufferToVideo) || (BltOperation == EfiBltVideoToVideo)) {
  716. if ((DestinationY + Height > VerticalResolution) || (DestinationX + Width > HorizontalResolution)) {
  717. DEBUG((DEBUG_INFO, "LcdGraphicsBlt: ERROR - Invalid destination resolution.\n" ));
  718. DEBUG((DEBUG_INFO, " - DestinationY=%d + Height=%d > VerticalResolution=%d.\n", DestinationY, Height, VerticalResolution ));
  719. DEBUG((DEBUG_INFO, " - DestinationX=%d + Width=%d > HorizontalResolution=%d.\n", DestinationX, Width, HorizontalResolution ));
  720. Status = EFI_INVALID_PARAMETER;
  721. goto EXIT;
  722. }
  723. }
  724. //
  725. // Perform the Block Transfer Operation
  726. //
  727. switch (BltOperation) {
  728. case EfiBltVideoFill:
  729. Status = BltVideoFill (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
  730. break;
  731. case EfiBltVideoToBltBuffer:
  732. Status = BltVideoToBltBuffer (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
  733. break;
  734. case EfiBltBufferToVideo:
  735. Status = BltBufferToVideo (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
  736. break;
  737. case EfiBltVideoToVideo:
  738. Status = BltVideoToVideo (This, BltBuffer, SourceX, SourceY, DestinationX, DestinationY, Width, Height, Delta);
  739. break;
  740. case EfiGraphicsOutputBltOperationMax:
  741. default:
  742. DEBUG((DEBUG_ERROR, "LcdGraphicsBlt: Invalid Operation\n"));
  743. Status = EFI_INVALID_PARAMETER;
  744. break;
  745. }
  746. EXIT:
  747. return Status;
  748. }