BltLibSample.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /** @file
  2. Example program using BltLib
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Uefi.h>
  7. #include <Library/BltLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/UefiLib.h>
  10. #include <Library/UefiApplicationEntryPoint.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. UINT64
  13. ReadTimestamp (
  14. VOID
  15. )
  16. {
  17. #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
  18. return AsmReadTsc ();
  19. #else
  20. #error ReadTimestamp not supported for this architecture!
  21. #endif
  22. }
  23. UINT32
  24. Rand32 (
  25. VOID
  26. )
  27. {
  28. UINTN Found;
  29. INTN Bits;
  30. UINT64 Tsc1;
  31. UINT64 Tsc2;
  32. UINT64 TscBits;
  33. UINT32 R32;
  34. R32 = 0;
  35. Found = 0;
  36. Tsc1 = ReadTimestamp ();
  37. Tsc2 = ReadTimestamp ();
  38. do {
  39. Tsc2 = ReadTimestamp ();
  40. TscBits = Tsc2 ^ Tsc1;
  41. Bits = HighBitSet64 (TscBits);
  42. if (Bits > 0) {
  43. Bits = Bits - 1;
  44. }
  45. R32 = (UINT32)((R32 << Bits) |
  46. RShiftU64 (LShiftU64 (TscBits, (UINTN) (64 - Bits)), (UINTN) (64 - Bits)));
  47. Found = Found + Bits;
  48. } while (Found < 32);
  49. return R32;
  50. }
  51. VOID
  52. TestFills (
  53. VOID
  54. )
  55. {
  56. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color;
  57. UINTN Loop;
  58. UINTN X;
  59. UINTN Y;
  60. UINTN W;
  61. UINTN H;
  62. UINTN Width;
  63. UINTN Height;
  64. BltLibGetSizes (&Width, &Height);
  65. for (Loop = 0; Loop < 10000; Loop++) {
  66. W = Width - (Rand32 () % Width);
  67. H = Height - (Rand32 () % Height);
  68. if (W != Width) {
  69. X = Rand32 () % (Width - W);
  70. } else {
  71. X = 0;
  72. }
  73. if (H != Height) {
  74. Y = Rand32 () % (Height - H);
  75. } else {
  76. Y = 0;
  77. }
  78. *(UINT32*) (&Color) = Rand32 () & 0xffffff;
  79. BltLibVideoFill (&Color, X, Y, W, H);
  80. }
  81. }
  82. VOID
  83. TestColor1 (
  84. VOID
  85. )
  86. {
  87. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color;
  88. UINTN X;
  89. UINTN Y;
  90. UINTN Width;
  91. UINTN Height;
  92. BltLibGetSizes (&Width, &Height);
  93. *(UINT32*) (&Color) = 0;
  94. for (Y = 0; Y < Height; Y++) {
  95. for (X = 0; X < Width; X++) {
  96. Color.Red = (UINT8) ((X * 0x100) / Width);
  97. Color.Green = (UINT8) ((Y * 0x100) / Height);
  98. Color.Blue = (UINT8) ((Y * 0x100) / Height);
  99. BltLibVideoFill (&Color, X, Y, 1, 1);
  100. }
  101. }
  102. }
  103. UINT32
  104. Uint32SqRt (
  105. IN UINT32 Uint32
  106. )
  107. {
  108. UINT32 Mask;
  109. UINT32 SqRt;
  110. UINT32 SqRtMask;
  111. UINT32 Squared;
  112. if (Uint32 == 0) {
  113. return 0;
  114. }
  115. for (SqRt = 0, Mask = (UINT32) (1 << (HighBitSet32 (Uint32) / 2));
  116. Mask != 0;
  117. Mask = Mask >> 1
  118. ) {
  119. SqRtMask = SqRt | Mask;
  120. //DEBUG ((EFI_D_INFO, "Uint32=0x%x SqRtMask=0x%x\n", Uint32, SqRtMask));
  121. Squared = (UINT32) (SqRtMask * SqRtMask);
  122. if (Squared > Uint32) {
  123. continue;
  124. } else if (Squared < Uint32) {
  125. SqRt = SqRtMask;
  126. } else {
  127. return SqRtMask;
  128. }
  129. }
  130. return SqRt;
  131. }
  132. UINT32
  133. Uint32Dist (
  134. IN UINTN X,
  135. IN UINTN Y
  136. )
  137. {
  138. return Uint32SqRt ((UINT32) ((X * X) + (Y * Y)));
  139. }
  140. UINT8
  141. GetTriColor (
  142. IN UINTN ColorDist,
  143. IN UINTN TriWidth
  144. )
  145. {
  146. return (UINT8) (((TriWidth - ColorDist) * 0x100) / TriWidth);
  147. //return (((TriWidth * TriWidth - ColorDist * ColorDist) * 0x100) / (TriWidth * TriWidth));
  148. }
  149. VOID
  150. TestColor (
  151. VOID
  152. )
  153. {
  154. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Color;
  155. UINTN X, Y;
  156. UINTN X1, X2, X3;
  157. UINTN Y1, Y2;
  158. UINTN LineWidth, TriWidth, ScreenWidth;
  159. UINTN TriHeight, ScreenHeight;
  160. UINT32 ColorDist;
  161. BltLibGetSizes (&ScreenWidth, &ScreenHeight);
  162. *(UINT32*) (&Color) = 0;
  163. BltLibVideoFill (&Color, 0, 0, ScreenWidth, ScreenHeight);
  164. TriWidth = (UINTN) DivU64x32 (
  165. MultU64x32 (11547005, (UINT32) ScreenHeight),
  166. 10000000
  167. );
  168. TriHeight = (UINTN) DivU64x32 (
  169. MultU64x32 (8660254, (UINT32) ScreenWidth),
  170. 10000000
  171. );
  172. if (TriWidth > ScreenWidth) {
  173. DEBUG ((EFI_D_INFO, "TriWidth at %d was too big\n", TriWidth));
  174. TriWidth = ScreenWidth;
  175. } else if (TriHeight > ScreenHeight) {
  176. DEBUG ((EFI_D_INFO, "TriHeight at %d was too big\n", TriHeight));
  177. TriHeight = ScreenHeight;
  178. }
  179. DEBUG ((EFI_D_INFO, "Triangle Width: %d; Height: %d\n", TriWidth, TriHeight));
  180. X1 = (ScreenWidth - TriWidth) / 2;
  181. X3 = X1 + TriWidth - 1;
  182. X2 = (X1 + X3) / 2;
  183. Y2 = (ScreenHeight - TriHeight) / 2;
  184. Y1 = Y2 + TriHeight - 1;
  185. for (Y = Y2; Y <= Y1; Y++) {
  186. LineWidth =
  187. (UINTN) DivU64x32 (
  188. MultU64x32 (11547005, (UINT32) (Y - Y2)),
  189. 20000000
  190. );
  191. for (X = X2 - LineWidth; X < (X2 + LineWidth); X++) {
  192. ColorDist = Uint32Dist(X - X1, Y1 - Y);
  193. Color.Red = GetTriColor (ColorDist, TriWidth);
  194. ColorDist = Uint32Dist((X < X2) ? X2 - X : X - X2, Y - Y2);
  195. Color.Green = GetTriColor (ColorDist, TriWidth);
  196. ColorDist = Uint32Dist(X3 - X, Y1 - Y);
  197. Color.Blue = GetTriColor (ColorDist, TriWidth);
  198. BltLibVideoFill (&Color, X, Y, 1, 1);
  199. }
  200. }
  201. }
  202. /**
  203. The user Entry Point for Application. The user code starts with this function
  204. as the real entry point for the application.
  205. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  206. @param[in] SystemTable A pointer to the EFI System Table.
  207. @retval EFI_SUCCESS The entry point is executed successfully.
  208. @retval other Some error occurs when executing this entry point.
  209. **/
  210. EFI_STATUS
  211. EFIAPI
  212. UefiMain (
  213. IN EFI_HANDLE ImageHandle,
  214. IN EFI_SYSTEM_TABLE *SystemTable
  215. )
  216. {
  217. EFI_STATUS Status;
  218. EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
  219. Status = gBS->HandleProtocol (
  220. gST->ConsoleOutHandle,
  221. &gEfiGraphicsOutputProtocolGuid,
  222. (VOID **) &Gop
  223. );
  224. if (EFI_ERROR (Status)) {
  225. return Status;
  226. }
  227. Status = BltLibConfigure (
  228. (VOID*)(UINTN) Gop->Mode->FrameBufferBase,
  229. Gop->Mode->Info
  230. );
  231. if (EFI_ERROR (Status)) {
  232. return Status;
  233. }
  234. TestFills ();
  235. TestColor ();
  236. return EFI_SUCCESS;
  237. }