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