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