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