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