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