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