3 * Screen handling functions
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 #include "../refind/screen.h"
39 #include "../refind/lib.h"
40 #include "../include/refit_call_wrapper.h"
42 #include <efiUgaDraw.h>
43 #include <efiConsoleControl.h>
45 #ifndef __MAKEWITH_GNUEFI
46 #define LibLocateProtocol EfiLibLocateProtocol
49 // Console defines and variables
51 static EFI_GUID ConsoleControlProtocolGuid
= EFI_CONSOLE_CONTROL_PROTOCOL_GUID
;
52 static EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
= NULL
;
54 static EFI_GUID UgaDrawProtocolGuid
= EFI_UGA_DRAW_PROTOCOL_GUID
;
55 static EFI_UGA_DRAW_PROTOCOL
*UgaDraw
= NULL
;
57 static EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
58 static EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
60 static BOOLEAN egHasGraphics
= FALSE
;
61 static UINTN egScreenWidth
= 800;
62 static UINTN egScreenHeight
= 600;
68 VOID
egInitScreen(VOID
)
70 EFI_STATUS Status
= EFI_SUCCESS
;
71 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
74 Status
= LibLocateProtocol(&ConsoleControlProtocolGuid
, (VOID
**) &ConsoleControl
);
75 if (EFI_ERROR(Status
))
76 ConsoleControl
= NULL
;
78 Status
= LibLocateProtocol(&UgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
79 if (EFI_ERROR(Status
))
82 Status
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
83 if (EFI_ERROR(Status
))
84 GraphicsOutput
= NULL
;
87 egHasGraphics
= FALSE
;
88 if (GraphicsOutput
!= NULL
) {
89 egScreenWidth
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
90 egScreenHeight
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
92 } else if (UgaDraw
!= NULL
) {
93 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
94 if (EFI_ERROR(Status
)) {
95 UgaDraw
= NULL
; // graphics not available
97 egScreenWidth
= UGAWidth
;
98 egScreenHeight
= UGAHeight
;
104 // Sets the screen resolution to the specified value, if possible. If *ScreenHeight
105 // is 0 and GOP mode is detected, assume that *ScreenWidth contains a GOP mode
106 // number rather than a horizontal resolution. If the specified resolution is not
107 // valid, displays a warning with the valid modes on GOP (UEFI) systems, or silently
108 // fails on UGA (EFI 1.x) systems. Note that this function attempts to set ANY screen
109 // resolution, even 0x0 or ridiculously large values.
110 // Upon success, returns actual screen resolution in *ScreenWidth and *ScreenHeight.
111 // These values are unchanged upon failure.
112 // Returns TRUE if successful, FALSE if not.
113 BOOLEAN
egSetScreenSize(IN OUT UINTN
*ScreenWidth
, IN OUT UINTN
*ScreenHeight
) {
114 EFI_STATUS Status
= EFI_SUCCESS
;
115 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
118 BOOLEAN ModeSet
= FALSE
;
119 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
121 if (GraphicsOutput
!= NULL
) { // GOP mode (UEFI)
122 if (*ScreenHeight
== 0) { // User specified a mode number (stored in *ScreenWidth); use it directly
123 if (*ScreenWidth
== GraphicsOutput
->Mode
->Mode
) { // user requested current mode; do nothing
125 *ScreenWidth
= Info
->HorizontalResolution
;
126 *ScreenHeight
= Info
->VerticalResolution
;
128 ModeNum
= (UINT32
) *ScreenWidth
;
129 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
130 if (Status
== EFI_SUCCESS
) {
131 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
132 if (Status
== EFI_SUCCESS
) {
134 *ScreenWidth
= Info
->HorizontalResolution
;
135 *ScreenHeight
= Info
->VerticalResolution
;
137 } // if queried mode OK
140 // User specified width & height; must find mode -- but only if change is required....
142 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, GraphicsOutput
->Mode
->Mode
, &Size
, &Info
);
143 if ((Status
== EFI_SUCCESS
) && (Info
->HorizontalResolution
== *ScreenWidth
) &&
144 (Info
->VerticalResolution
== *ScreenHeight
)) {
145 ModeSet
= TRUE
; // user requested current mode; do nothing
147 // Do a loop through the modes to see if the specified one is available;
148 // and if so, switch to it....
150 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
151 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
)) &&
152 (Info
->HorizontalResolution
== *ScreenWidth
) && (Info
->VerticalResolution
== *ScreenHeight
)) {
153 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
154 ModeSet
= (Status
== EFI_SUCCESS
);
156 } while ((++ModeNum
< GraphicsOutput
->Mode
->MaxMode
) && !ModeSet
);
161 egScreenWidth
= *ScreenWidth
;
162 egScreenHeight
= *ScreenHeight
;
163 } else {// If unsuccessful, display an error message for the user....
165 Print(L
"Error setting graphics mode %d x %d; using default mode!\nAvailable modes are:\n", *ScreenWidth
, *ScreenHeight
);
168 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
169 if ((Status
== EFI_SUCCESS
) && (Info
!= NULL
)) {
170 Print(L
"Mode %d: %d x %d\n", ModeNum
, Info
->HorizontalResolution
, Info
->VerticalResolution
);
172 } while (++ModeNum
< GraphicsOutput
->Mode
->MaxMode
);
177 } else if (UgaDraw
!= NULL
) { // UGA mode (EFI 1.x)
178 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
179 // in all cases, but I don't know how to probe for alternatives....
180 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
181 Status
= refit_call5_wrapper(UgaDraw
->SetMode
, UgaDraw
, *ScreenWidth
, *ScreenHeight
, UGADepth
, UGARefreshRate
);
182 if (Status
== EFI_SUCCESS
) {
183 egScreenWidth
= *ScreenWidth
;
184 egScreenHeight
= *ScreenHeight
;
187 // TODO: Find a list of supported modes and display it.
188 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
189 // This is just a placeholder until something better can be done....
190 Print(L
"Error setting graphics mode %d x %d; unsupported mode!\n");
194 } // BOOLEAN egSetScreenSize()
196 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
198 if (ScreenWidth
!= NULL
)
199 *ScreenWidth
= egScreenWidth
;
200 if (ScreenHeight
!= NULL
)
201 *ScreenHeight
= egScreenHeight
;
205 // Returns the mode that was actually set.
206 UINT32
egSetTextMode(UINT32 RequestedMode
) {
207 UINTN i
= 0, Width
, Height
;
208 UINT32 UsedMode
= ST
->ConOut
->Mode
->Mode
;
211 if (RequestedMode
!= ST
->ConOut
->Mode
->Mode
) {
212 Status
= refit_call2_wrapper(ST
->ConOut
->SetMode
, ST
->ConOut
, RequestedMode
);
213 if (Status
== EFI_SUCCESS
) {
214 UsedMode
= RequestedMode
;
217 Print(L
"Error setting text mode %d; available modes are:\n", RequestedMode
);
219 Status
= refit_call4_wrapper(ST
->ConOut
->QueryMode
, ST
->ConOut
, i
, &Width
, &Height
);
220 if (Status
== EFI_SUCCESS
)
221 Print(L
"Mode %d: %d x %d\n", i
, Width
, Height
);
222 } while (++i
< ST
->ConOut
->Mode
->MaxMode
);
226 } // if/else successful change
227 } // if need to change mode
229 } // UINT32 egSetTextMode()
231 CHAR16
* egScreenDescription(VOID
)
233 CHAR16
*GraphicsInfo
, *TextInfo
= NULL
;
235 GraphicsInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
236 if (GraphicsInfo
== NULL
)
237 return L
"memory allocation error";
240 if (GraphicsOutput
!= NULL
) {
241 SPrint(GraphicsInfo
, 255, L
"Graphics Output (UEFI), %dx%d", egScreenWidth
, egScreenHeight
);
242 } else if (UgaDraw
!= NULL
) {
243 GraphicsInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
244 SPrint(GraphicsInfo
, 255, L
"UGA Draw (EFI 1.10), %dx%d", egScreenWidth
, egScreenHeight
);
246 MyFreePool(GraphicsInfo
);
247 MyFreePool(TextInfo
);
248 return L
"Internal Error";
250 if (!AllowGraphicsMode
) { // graphics-capable HW, but in text mode
251 TextInfo
= AllocateZeroPool(256 * sizeof(CHAR16
));
252 SPrint(TextInfo
, 255, L
"(in %dx%d text mode)", ConWidth
, ConHeight
);
253 MergeStrings(&GraphicsInfo
, TextInfo
, L
' ');
256 SPrint(GraphicsInfo
, 255, L
"Text-foo console, %dx%d", ConWidth
, ConHeight
);
258 MyFreePool(TextInfo
);
262 BOOLEAN
egHasGraphicsMode(VOID
)
264 return egHasGraphics
;
267 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
269 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
271 if (ConsoleControl
!= NULL
) {
272 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
273 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
279 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
281 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
282 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
284 if (ConsoleControl
!= NULL
) {
285 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
287 NewMode
= Enable
? EfiConsoleControlScreenGraphics
288 : EfiConsoleControlScreenText
;
289 if (CurrentMode
!= NewMode
)
290 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
295 // Drawing to the screen
298 VOID
egClearScreen(IN EG_PIXEL
*Color
)
300 EFI_UGA_PIXEL FillColor
;
306 FillColor
.Red
= Color
->r
;
307 FillColor
.Green
= Color
->g
;
308 FillColor
.Blue
= Color
->b
;
311 FillColor
.Green
= 0x0;
312 FillColor
.Blue
= 0x0;
314 FillColor
.Reserved
= 0;
316 if (GraphicsOutput
!= NULL
) {
317 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
318 // layout, and the header from TianoCore actually defines them
319 // to be the same type.
320 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
321 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
322 } else if (UgaDraw
!= NULL
) {
323 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
, 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
327 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
332 if (Image
->HasAlpha
) {
333 Image
->HasAlpha
= FALSE
;
334 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
337 if (GraphicsOutput
!= NULL
) {
338 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
339 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
340 } else if (UgaDraw
!= NULL
) {
341 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
342 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
346 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
347 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
348 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
349 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
354 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
358 if (Image
->HasAlpha
) {
359 Image
->HasAlpha
= FALSE
;
360 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
363 if (GraphicsOutput
!= NULL
) {
364 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
365 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
366 } else if (UgaDraw
!= NULL
) {
367 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
368 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
372 // Display a message in the center of the screen, surrounded by a box of the
373 // specified color. For the moment, uses graphics calls only. (It still works
374 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
375 VOID
egDisplayMessage(IN CHAR16
*Text
, EG_PIXEL
*BGColor
) {
376 UINTN BoxWidth
, BoxHeight
;
379 if ((Text
!= NULL
) && (BGColor
!= NULL
)) {
380 BoxWidth
= (StrLen(Text
) + 2) * FONT_CELL_WIDTH
;
381 if (BoxWidth
> egScreenWidth
)
382 BoxWidth
= egScreenWidth
;
383 BoxHeight
= 2 * FONT_CELL_HEIGHT
;
384 Box
= egCreateFilledImage(BoxWidth
, BoxHeight
, FALSE
, BGColor
);
385 egRenderText(Text
, Box
, FONT_CELL_WIDTH
, FONT_CELL_HEIGHT
/ 2);
386 egDrawImage(Box
, (egScreenWidth
- BoxWidth
) / 2, (egScreenHeight
- BoxHeight
) / 2);
387 } // if non-NULL inputs
388 } // VOID egDisplayMessage()
394 VOID
egScreenShot(VOID
)
399 UINTN FileDataLength
;
405 // allocate a buffer for the whole screen
406 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
408 Print(L
"Error egCreateImage returned NULL\n");
412 // get full screen image
413 if (GraphicsOutput
!= NULL
) {
414 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
415 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
416 } else if (UgaDraw
!= NULL
) {
417 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
418 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
422 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
424 if (FileData
== NULL
) {
425 Print(L
"Error egEncodeBMP returned NULL\n");
429 // save to file on the ESP
430 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
432 if (EFI_ERROR(Status
)) {
433 Print(L
"Error egSaveFile: %x\n", Status
);
439 // DEBUG: switch to text mode
441 egSetGraphicsModeEnabled(FALSE
);
442 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);