]> code.delx.au - refind/blob - libeg/screen.c
24b7652eefe87bceab45abafbf2886ff19a6377c
[refind] / libeg / screen.c
1 /*
2 * libeg/screen.c
3 * Screen handling functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /*
37 * Modifications copyright (c) 2012 Roderick W. Smith
38 *
39 * Modifications distributed under the terms of the GNU General Public
40 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
41 * with this source code or binaries made from it.
42 *
43 */
44
45 #include "libegint.h"
46 #include "../refind/screen.h"
47 #include "../refind/lib.h"
48 #include "../include/refit_call_wrapper.h"
49
50 #include <efiUgaDraw.h>
51 #include <efiConsoleControl.h>
52
53 #ifndef __MAKEWITH_GNUEFI
54 #define LibLocateProtocol EfiLibLocateProtocol
55 #endif
56
57 // Console defines and variables
58
59 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
60 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
61
62 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
63 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
64
65 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
66 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
67
68 static BOOLEAN egHasGraphics = FALSE;
69 static UINTN egScreenWidth = 800;
70 static UINTN egScreenHeight = 600;
71
72 //
73 // Screen handling
74 //
75
76 // Make the necessary system calls to identify the current graphics mode.
77 // Stores the results in the file-global variables egScreenWidth,
78 // egScreenHeight, and egHasGraphics. The first two of these will be
79 // unchanged if neither GraphicsOutput nor UgaDraw is a valid pointer.
80 static VOID egDetermineScreenSize(VOID) {
81 EFI_STATUS Status = EFI_SUCCESS;
82 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
83
84 // get screen size
85 egHasGraphics = FALSE;
86 if (GraphicsOutput != NULL) {
87 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
88 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
89 egHasGraphics = TRUE;
90 } else if (UgaDraw != NULL) {
91 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
92 if (EFI_ERROR(Status)) {
93 UgaDraw = NULL; // graphics not available
94 } else {
95 egScreenWidth = UGAWidth;
96 egScreenHeight = UGAHeight;
97 egHasGraphics = TRUE;
98 }
99 }
100 } // static VOID egDetermineScreenSize()
101
102 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
103 {
104 egDetermineScreenSize();
105
106 if (ScreenWidth != NULL)
107 *ScreenWidth = egScreenWidth;
108 if (ScreenHeight != NULL)
109 *ScreenHeight = egScreenHeight;
110 }
111
112 VOID egInitScreen(VOID)
113 {
114 EFI_STATUS Status = EFI_SUCCESS;
115
116 // get protocols
117 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
118 if (EFI_ERROR(Status))
119 ConsoleControl = NULL;
120
121 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
122 if (EFI_ERROR(Status))
123 UgaDraw = NULL;
124
125 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
126 if (EFI_ERROR(Status))
127 GraphicsOutput = NULL;
128
129 egDetermineScreenSize();
130 }
131
132 // Convert a graphics mode (in *ModeWidth) to a width and height (returned in
133 // *ModeWidth and *Height, respectively).
134 // Returns TRUE if successful, FALSE if not (invalid mode, typically)
135 BOOLEAN egGetResFromMode(UINTN *ModeWidth, UINTN *Height) {
136 UINTN Size;
137 EFI_STATUS Status;
138 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info = NULL;
139
140 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, *ModeWidth, &Size, &Info);
141 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
142 *ModeWidth = Info->HorizontalResolution;
143 *Height = Info->VerticalResolution;
144 return TRUE;
145 }
146 return FALSE;
147 } // BOOLEAN egGetResFromMode()
148
149 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
150 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
151 // number rather than a horizontal resolution. If the specified resolution is not
152 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
153 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
154 // resolution, even 0x0 or ridiculously large values.
155 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
156 // These values are unchanged upon failure.
157 // Returns TRUE if successful, FALSE if not.
158 BOOLEAN egSetScreenSize(IN OUT UINTN *ScreenWidth, IN OUT UINTN *ScreenHeight) {
159 EFI_STATUS Status = EFI_SUCCESS;
160 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
161 UINT32 ModeNum = 0;
162 UINTN Size;
163 BOOLEAN ModeSet = FALSE;
164 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
165
166 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
167 if (*ScreenHeight == 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
168 ModeNum = (UINT32) *ScreenWidth;
169 if (egGetResFromMode(ScreenWidth, ScreenHeight) &&
170 (refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum) == EFI_SUCCESS)) {
171 ModeSet = TRUE;
172 }
173
174 // User specified width & height; must find mode...
175 } else {
176 // Do a loop through the modes to see if the specified one is available;
177 // and if so, switch to it....
178 do {
179 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
180 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
181 (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
182 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
183 ModeSet = (Status == EFI_SUCCESS);
184 } // if
185 } while ((++ModeNum < GraphicsOutput->Mode->MaxMode) && !ModeSet);
186 } // if/else
187
188 if (ModeSet) {
189 egScreenWidth = *ScreenWidth;
190 egScreenHeight = *ScreenHeight;
191 } else {// If unsuccessful, display an error message for the user....
192 SwitchToText(FALSE);
193 Print(L"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth, *ScreenHeight);
194 ModeNum = 0;
195 do {
196 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
197 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
198 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
199 } // else
200 } while (++ModeNum < GraphicsOutput->Mode->MaxMode);
201 PauseForKey();
202 SwitchToGraphics();
203 } // if GOP mode (UEFI)
204
205 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
206 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
207 // in all cases, but I don't know how to probe for alternatives....
208 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
209 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, *ScreenWidth, *ScreenHeight, UGADepth, UGARefreshRate);
210 if (Status == EFI_SUCCESS) {
211 egScreenWidth = *ScreenWidth;
212 egScreenHeight = *ScreenHeight;
213 ModeSet = TRUE;
214 } else {
215 // TODO: Find a list of supported modes and display it.
216 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
217 // This is just a placeholder until something better can be done....
218 Print(L"Error setting graphics mode %d x %d; unsupported mode!\n");
219 } // if/else
220 } // if/else if UGA mode (EFI 1.x)
221 return (ModeSet);
222 } // BOOLEAN egSetScreenSize()
223
224 // Set a text mode.
225 // Returns TRUE if the mode actually changed, FALSE otherwise.
226 // Note that a FALSE return value can mean either an error or no change
227 // necessary.
228 BOOLEAN egSetTextMode(UINT32 RequestedMode) {
229 UINTN i = 0, Width, Height;
230 EFI_STATUS Status;
231 BOOLEAN ChangedIt = FALSE;
232
233 if ((RequestedMode != ST->ConOut->Mode->Mode) && (RequestedMode != DONT_CHANGE_TEXT_MODE)) {
234 // SwitchToGraphics();
235 Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, RequestedMode);
236 if (Status == EFI_SUCCESS) {
237 ChangedIt = TRUE;
238 } else {
239 SwitchToText(FALSE);
240 Print(L"Error setting text mode %d; available modes are:\n", RequestedMode);
241 do {
242 Status = refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, i, &Width, &Height);
243 if (Status == EFI_SUCCESS)
244 Print(L"Mode %d: %d x %d\n", i, Width, Height);
245 } while (++i < ST->ConOut->Mode->MaxMode);
246
247 PauseForKey();
248 SwitchToGraphics();
249 } // if/else successful change
250 } // if need to change mode
251 return ChangedIt;
252 } // BOOLEAN egSetTextMode()
253
254 CHAR16 * egScreenDescription(VOID)
255 {
256 CHAR16 *GraphicsInfo, *TextInfo = NULL;
257
258 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
259 if (GraphicsInfo == NULL)
260 return L"memory allocation error";
261
262 if (egHasGraphics) {
263 if (GraphicsOutput != NULL) {
264 SPrint(GraphicsInfo, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
265 } else if (UgaDraw != NULL) {
266 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
267 SPrint(GraphicsInfo, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
268 } else {
269 MyFreePool(GraphicsInfo);
270 MyFreePool(TextInfo);
271 return L"Internal Error";
272 }
273 if (!AllowGraphicsMode) { // graphics-capable HW, but in text mode
274 TextInfo = AllocateZeroPool(256 * sizeof(CHAR16));
275 SPrint(TextInfo, 255, L"(in %dx%d text mode)", ConWidth, ConHeight);
276 MergeStrings(&GraphicsInfo, TextInfo, L' ');
277 }
278 } else {
279 SPrint(GraphicsInfo, 255, L"Text-foo console, %dx%d", ConWidth, ConHeight);
280 }
281 MyFreePool(TextInfo);
282 return GraphicsInfo;
283 }
284
285 BOOLEAN egHasGraphicsMode(VOID)
286 {
287 return egHasGraphics;
288 }
289
290 BOOLEAN egIsGraphicsModeEnabled(VOID)
291 {
292 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
293
294 if (ConsoleControl != NULL) {
295 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
296 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
297 }
298
299 return FALSE;
300 }
301
302 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
303 {
304 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
305 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
306
307 if (ConsoleControl != NULL) {
308 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
309
310 NewMode = Enable ? EfiConsoleControlScreenGraphics
311 : EfiConsoleControlScreenText;
312 if (CurrentMode != NewMode)
313 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
314 }
315 }
316
317 //
318 // Drawing to the screen
319 //
320
321 VOID egClearScreen(IN EG_PIXEL *Color)
322 {
323 EFI_UGA_PIXEL FillColor;
324
325 if (!egHasGraphics)
326 return;
327
328 if (Color != NULL) {
329 FillColor.Red = Color->r;
330 FillColor.Green = Color->g;
331 FillColor.Blue = Color->b;
332 } else {
333 FillColor.Red = 0x0;
334 FillColor.Green = 0x0;
335 FillColor.Blue = 0x0;
336 }
337 FillColor.Reserved = 0;
338
339 if (GraphicsOutput != NULL) {
340 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
341 // layout, and the header from TianoCore actually defines them
342 // to be the same type.
343 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
344 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
345 } else if (UgaDraw != NULL) {
346 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill, 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
347 }
348 }
349
350 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
351 {
352 if (!egHasGraphics)
353 return;
354
355 if (Image->HasAlpha) {
356 Image->HasAlpha = FALSE;
357 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
358 }
359
360 if (GraphicsOutput != NULL) {
361 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
362 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
363 } else if (UgaDraw != NULL) {
364 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
365 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
366 }
367 }
368
369 VOID egDrawImageArea(IN EG_IMAGE *Image,
370 IN UINTN AreaPosX, IN UINTN AreaPosY,
371 IN UINTN AreaWidth, IN UINTN AreaHeight,
372 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
373 {
374 if (!egHasGraphics)
375 return;
376
377 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
378 if (AreaWidth == 0)
379 return;
380
381 if (Image->HasAlpha) {
382 Image->HasAlpha = FALSE;
383 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
384 }
385
386 if (GraphicsOutput != NULL) {
387 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
388 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
389 } else if (UgaDraw != NULL) {
390 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
391 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
392 }
393 }
394
395 // Display a message in the center of the screen, surrounded by a box of the
396 // specified color. For the moment, uses graphics calls only. (It still works
397 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
398 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
399 UINTN BoxWidth, BoxHeight;
400 EG_IMAGE *Box;
401
402 if ((Text != NULL) && (BGColor != NULL)) {
403 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
404 if (BoxWidth > egScreenWidth)
405 BoxWidth = egScreenWidth;
406 BoxHeight = 2 * FONT_CELL_HEIGHT;
407 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
408 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
409 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
410 } // if non-NULL inputs
411 } // VOID egDisplayMessage()
412
413 //
414 // Make a screenshot
415 //
416
417 VOID egScreenShot(VOID)
418 {
419 EFI_STATUS Status;
420 EG_IMAGE *Image;
421 UINT8 *FileData;
422 UINTN FileDataLength;
423 UINTN Index;
424
425 if (!egHasGraphics)
426 return;
427
428 // allocate a buffer for the whole screen
429 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
430 if (Image == NULL) {
431 Print(L"Error egCreateImage returned NULL\n");
432 goto bailout_wait;
433 }
434
435 // get full screen image
436 if (GraphicsOutput != NULL) {
437 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
438 0, 0, 0, 0, Image->Width, Image->Height, 0);
439 } else if (UgaDraw != NULL) {
440 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
441 0, 0, 0, 0, Image->Width, Image->Height, 0);
442 }
443
444 // encode as BMP
445 egEncodeBMP(Image, &FileData, &FileDataLength);
446 egFreeImage(Image);
447 if (FileData == NULL) {
448 Print(L"Error egEncodeBMP returned NULL\n");
449 goto bailout_wait;
450 }
451
452 // save to file on the ESP
453 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
454 FreePool(FileData);
455 if (EFI_ERROR(Status)) {
456 Print(L"Error egSaveFile: %x\n", Status);
457 goto bailout_wait;
458 }
459
460 return;
461
462 // DEBUG: switch to text mode
463 bailout_wait:
464 egSetGraphicsModeEnabled(FALSE);
465 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
466 }
467
468 /* EOF */