]> code.delx.au - refind/blob - libeg/screen.c
Merge branch 'master' of git://git.code.sf.net/p/refind/code
[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 VOID egInitScreen(VOID)
77 {
78 EFI_STATUS Status = EFI_SUCCESS;
79 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
80
81 // get protocols
82 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
83 if (EFI_ERROR(Status))
84 ConsoleControl = NULL;
85
86 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
87 if (EFI_ERROR(Status))
88 UgaDraw = NULL;
89
90 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
91 if (EFI_ERROR(Status))
92 GraphicsOutput = NULL;
93
94 // get screen size
95 egHasGraphics = FALSE;
96 if (GraphicsOutput != NULL) {
97 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
98 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
99 egHasGraphics = TRUE;
100 } else if (UgaDraw != NULL) {
101 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
102 if (EFI_ERROR(Status)) {
103 UgaDraw = NULL; // graphics not available
104 } else {
105 egScreenWidth = UGAWidth;
106 egScreenHeight = UGAHeight;
107 egHasGraphics = TRUE;
108 }
109 }
110 }
111
112 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
113 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
114 // number rather than a horizontal resolution. If the specified resolution is not
115 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
116 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
117 // resolution, even 0x0 or ridiculously large values.
118 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
119 // These values are unchanged upon failure.
120 // Returns TRUE if successful, FALSE if not.
121 BOOLEAN egSetScreenSize(IN OUT UINTN *ScreenWidth, IN OUT UINTN *ScreenHeight) {
122 EFI_STATUS Status = EFI_SUCCESS;
123 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
124 UINT32 ModeNum = 0;
125 UINTN Size;
126 BOOLEAN ModeSet = FALSE;
127 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
128
129 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
130 if (*ScreenHeight == 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
131 if (*ScreenWidth == GraphicsOutput->Mode->Mode) { // user requested current mode; do nothing
132 ModeSet = TRUE;
133 *ScreenWidth = Info->HorizontalResolution;
134 *ScreenHeight = Info->VerticalResolution;
135 } else {
136 ModeNum = (UINT32) *ScreenWidth;
137 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
138 if (Status == EFI_SUCCESS) {
139 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
140 if (Status == EFI_SUCCESS) {
141 ModeSet = TRUE;
142 *ScreenWidth = Info->HorizontalResolution;
143 *ScreenHeight = Info->VerticalResolution;
144 } // if set mode OK
145 } // if queried mode OK
146 } // if/else
147
148 // User specified width & height; must find mode -- but only if change is required....
149 } else {
150 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, GraphicsOutput->Mode->Mode, &Size, &Info);
151 if ((Status == EFI_SUCCESS) && (Info->HorizontalResolution == *ScreenWidth) &&
152 (Info->VerticalResolution == *ScreenHeight)) {
153 ModeSet = TRUE; // user requested current mode; do nothing
154 } else {
155 // Do a loop through the modes to see if the specified one is available;
156 // and if so, switch to it....
157 do {
158 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
159 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
160 (Info->HorizontalResolution == *ScreenWidth) && (Info->VerticalResolution == *ScreenHeight)) {
161 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
162 ModeSet = (Status == EFI_SUCCESS);
163 } // if
164 } while ((++ModeNum < GraphicsOutput->Mode->MaxMode) && !ModeSet);
165 } // if/else
166 } // if/else
167
168 if (ModeSet) {
169 egScreenWidth = *ScreenWidth;
170 egScreenHeight = *ScreenHeight;
171 } else {// If unsuccessful, display an error message for the user....
172 SwitchToText(FALSE);
173 Print(L"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth, *ScreenHeight);
174 ModeNum = 0;
175 do {
176 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
177 if ((Status == EFI_SUCCESS) && (Info != NULL)) {
178 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
179 } // else
180 } while (++ModeNum < GraphicsOutput->Mode->MaxMode);
181 PauseForKey();
182 SwitchToGraphics();
183 } // if()
184
185 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
186 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
187 // in all cases, but I don't know how to probe for alternatives....
188 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
189 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, *ScreenWidth, *ScreenHeight, UGADepth, UGARefreshRate);
190 if (Status == EFI_SUCCESS) {
191 egScreenWidth = *ScreenWidth;
192 egScreenHeight = *ScreenHeight;
193 ModeSet = TRUE;
194 } else {
195 // TODO: Find a list of supported modes and display it.
196 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
197 // This is just a placeholder until something better can be done....
198 Print(L"Error setting graphics mode %d x %d; unsupported mode!\n");
199 } // if/else
200 } // if/else if
201 return (ModeSet);
202 } // BOOLEAN egSetScreenSize()
203
204 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
205 {
206 if (ScreenWidth != NULL)
207 *ScreenWidth = egScreenWidth;
208 if (ScreenHeight != NULL)
209 *ScreenHeight = egScreenHeight;
210 }
211
212 // Set a text mode
213 // Returns the mode that was actually set.
214 UINT32 egSetTextMode(UINT32 RequestedMode) {
215 UINTN i = 0, Width, Height;
216 UINT32 UsedMode = ST->ConOut->Mode->Mode;
217 EFI_STATUS Status;
218
219 if (RequestedMode != ST->ConOut->Mode->Mode) {
220 SwitchToGraphics();
221 Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, RequestedMode);
222 if (Status == EFI_SUCCESS) {
223 UsedMode = RequestedMode;
224 } else {
225 SwitchToText(FALSE);
226 Print(L"Error setting text mode %d; available modes are:\n", RequestedMode);
227 do {
228 Status = refit_call4_wrapper(ST->ConOut->QueryMode, ST->ConOut, i, &Width, &Height);
229 if (Status == EFI_SUCCESS)
230 Print(L"Mode %d: %d x %d\n", i, Width, Height);
231 } while (++i < ST->ConOut->Mode->MaxMode);
232
233 PauseForKey();
234 SwitchToGraphics();
235 } // if/else successful change
236 } // if need to change mode
237 return UsedMode;
238 } // UINT32 egSetTextMode()
239
240 CHAR16 * egScreenDescription(VOID)
241 {
242 CHAR16 *GraphicsInfo, *TextInfo = NULL;
243
244 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
245 if (GraphicsInfo == NULL)
246 return L"memory allocation error";
247
248 if (egHasGraphics) {
249 if (GraphicsOutput != NULL) {
250 SPrint(GraphicsInfo, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
251 } else if (UgaDraw != NULL) {
252 GraphicsInfo = AllocateZeroPool(256 * sizeof(CHAR16));
253 SPrint(GraphicsInfo, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
254 } else {
255 MyFreePool(GraphicsInfo);
256 MyFreePool(TextInfo);
257 return L"Internal Error";
258 }
259 if (!AllowGraphicsMode) { // graphics-capable HW, but in text mode
260 TextInfo = AllocateZeroPool(256 * sizeof(CHAR16));
261 SPrint(TextInfo, 255, L"(in %dx%d text mode)", ConWidth, ConHeight);
262 MergeStrings(&GraphicsInfo, TextInfo, L' ');
263 }
264 } else {
265 SPrint(GraphicsInfo, 255, L"Text-foo console, %dx%d", ConWidth, ConHeight);
266 }
267 MyFreePool(TextInfo);
268 return GraphicsInfo;
269 }
270
271 BOOLEAN egHasGraphicsMode(VOID)
272 {
273 return egHasGraphics;
274 }
275
276 BOOLEAN egIsGraphicsModeEnabled(VOID)
277 {
278 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
279
280 if (ConsoleControl != NULL) {
281 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
282 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
283 }
284
285 return FALSE;
286 }
287
288 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
289 {
290 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
291 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
292
293 if (ConsoleControl != NULL) {
294 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
295
296 NewMode = Enable ? EfiConsoleControlScreenGraphics
297 : EfiConsoleControlScreenText;
298 if (CurrentMode != NewMode)
299 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
300 }
301 }
302
303 //
304 // Drawing to the screen
305 //
306
307 VOID egClearScreen(IN EG_PIXEL *Color)
308 {
309 EFI_UGA_PIXEL FillColor;
310
311 if (!egHasGraphics)
312 return;
313
314 if (Color != NULL) {
315 FillColor.Red = Color->r;
316 FillColor.Green = Color->g;
317 FillColor.Blue = Color->b;
318 } else {
319 FillColor.Red = 0x0;
320 FillColor.Green = 0x0;
321 FillColor.Blue = 0x0;
322 }
323 FillColor.Reserved = 0;
324
325 if (GraphicsOutput != NULL) {
326 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
327 // layout, and the header from TianoCore actually defines them
328 // to be the same type.
329 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
330 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
331 } else if (UgaDraw != NULL) {
332 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill, 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
333 }
334 }
335
336 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
337 {
338 if (!egHasGraphics)
339 return;
340
341 if (Image->HasAlpha) {
342 Image->HasAlpha = FALSE;
343 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
344 }
345
346 if (GraphicsOutput != NULL) {
347 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
348 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
349 } else if (UgaDraw != NULL) {
350 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
351 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
352 }
353 }
354
355 VOID egDrawImageArea(IN EG_IMAGE *Image,
356 IN UINTN AreaPosX, IN UINTN AreaPosY,
357 IN UINTN AreaWidth, IN UINTN AreaHeight,
358 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
359 {
360 if (!egHasGraphics)
361 return;
362
363 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
364 if (AreaWidth == 0)
365 return;
366
367 if (Image->HasAlpha) {
368 Image->HasAlpha = FALSE;
369 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
370 }
371
372 if (GraphicsOutput != NULL) {
373 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
374 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
375 } else if (UgaDraw != NULL) {
376 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
377 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
378 }
379 }
380
381 // Display a message in the center of the screen, surrounded by a box of the
382 // specified color. For the moment, uses graphics calls only. (It still works
383 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
384 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
385 UINTN BoxWidth, BoxHeight;
386 EG_IMAGE *Box;
387
388 if ((Text != NULL) && (BGColor != NULL)) {
389 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
390 if (BoxWidth > egScreenWidth)
391 BoxWidth = egScreenWidth;
392 BoxHeight = 2 * FONT_CELL_HEIGHT;
393 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
394 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
395 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
396 } // if non-NULL inputs
397 } // VOID egDisplayMessage()
398
399 //
400 // Make a screenshot
401 //
402
403 VOID egScreenShot(VOID)
404 {
405 EFI_STATUS Status;
406 EG_IMAGE *Image;
407 UINT8 *FileData;
408 UINTN FileDataLength;
409 UINTN Index;
410
411 if (!egHasGraphics)
412 return;
413
414 // allocate a buffer for the whole screen
415 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
416 if (Image == NULL) {
417 Print(L"Error egCreateImage returned NULL\n");
418 goto bailout_wait;
419 }
420
421 // get full screen image
422 if (GraphicsOutput != NULL) {
423 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
424 0, 0, 0, 0, Image->Width, Image->Height, 0);
425 } else if (UgaDraw != NULL) {
426 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
427 0, 0, 0, 0, Image->Width, Image->Height, 0);
428 }
429
430 // encode as BMP
431 egEncodeBMP(Image, &FileData, &FileDataLength);
432 egFreeImage(Image);
433 if (FileData == NULL) {
434 Print(L"Error egEncodeBMP returned NULL\n");
435 goto bailout_wait;
436 }
437
438 // save to file on the ESP
439 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
440 FreePool(FileData);
441 if (EFI_ERROR(Status)) {
442 Print(L"Error egSaveFile: %x\n", Status);
443 goto bailout_wait;
444 }
445
446 return;
447
448 // DEBUG: switch to text mode
449 bailout_wait:
450 egSetGraphicsModeEnabled(FALSE);
451 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
452 }
453
454 /* EOF */