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