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 "../include/refit_call_wrapper.h"
41 #include <efiUgaDraw.h>
42 #include <efiConsoleControl.h>
44 #ifndef __MAKEWITH_GNUEFI
45 #define LibLocateProtocol EfiLibLocateProtocol
48 // Console defines and variables
50 static EFI_GUID ConsoleControlProtocolGuid
= EFI_CONSOLE_CONTROL_PROTOCOL_GUID
;
51 static EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
= NULL
;
53 static EFI_GUID UgaDrawProtocolGuid
= EFI_UGA_DRAW_PROTOCOL_GUID
;
54 static EFI_UGA_DRAW_PROTOCOL
*UgaDraw
= NULL
;
56 static EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
57 static EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
59 static BOOLEAN egHasGraphics
= FALSE
;
60 static UINTN egScreenWidth
= 800;
61 static UINTN egScreenHeight
= 600;
67 VOID
egInitScreen(VOID
)
69 EFI_STATUS Status
= EFI_SUCCESS
;
70 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
73 Status
= LibLocateProtocol(&ConsoleControlProtocolGuid
, (VOID
**) &ConsoleControl
);
74 if (EFI_ERROR(Status
))
75 ConsoleControl
= NULL
;
77 Status
= LibLocateProtocol(&UgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
78 if (EFI_ERROR(Status
))
81 Status
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
82 if (EFI_ERROR(Status
))
83 GraphicsOutput
= NULL
;
86 egHasGraphics
= FALSE
;
87 if (GraphicsOutput
!= NULL
) {
88 egScreenWidth
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
89 egScreenHeight
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
91 } else if (UgaDraw
!= NULL
) {
92 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
93 if (EFI_ERROR(Status
)) {
94 UgaDraw
= NULL
; // graphics not available
96 egScreenWidth
= UGAWidth
;
97 egScreenHeight
= UGAHeight
;
103 // // Returns current graphics mode number
104 // UINT32 egGetGraphicsMode(VOID) {
105 // UINT32 retval = 0;
107 // if (GraphicsOutput != NULL) {
108 // retval = GraphicsOutput->Mode->Mode;
112 // } // UINT32 egGetGraphicsMode()
114 // Sets the screen resolution to the specified value, if possible.
115 // If the specified value is not valid, displays a warning with the valid
116 // modes on UEFI systems, or silently fails on EFI 1.x systems. Note that
117 // this function attempts to set ANY screen resolution, even 0x0 or
118 // ridiculously large values.
119 // Returns TRUE if successful, FALSE if not.
120 BOOLEAN
egSetScreenSize(IN UINTN ScreenWidth
, IN UINTN ScreenHeight
) {
121 EFI_STATUS Status
= EFI_SUCCESS
;
122 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
125 BOOLEAN ModeSet
= FALSE
;
126 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
128 if (GraphicsOutput
!= NULL
) { // GOP mode (UEFI)
129 // Do a loop through the modes to see if the specified one is available;
130 // and if so, switch to it....
131 while ((Status
== EFI_SUCCESS
) && (!ModeSet
)) {
132 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
133 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
)) &&
134 (Info
->HorizontalResolution
== ScreenWidth
) && (Info
->VerticalResolution
== ScreenHeight
)) {
135 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
136 // if (Status == EFI_SUCCESS)
137 // Status = refit_call2_wrapper(ST->ConOut->SetMode, ST->ConOut, ModeNum);
138 ModeSet
= (Status
== EFI_SUCCESS
);
144 egScreenWidth
= ScreenWidth
;
145 egScreenHeight
= ScreenHeight
;
146 } else {// If unsuccessful, display an error message for the user....
148 Print(L
"Error setting mode %d x %d; using default mode!\nAvailable modes are:\n", ScreenWidth
, ScreenHeight
);
150 Status
= EFI_SUCCESS
;
151 while (Status
== EFI_SUCCESS
) {
152 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
153 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
))) {
154 Print(L
"Mode %d: %d x %d\n", ModeNum
, Info
->HorizontalResolution
, Info
->VerticalResolution
);
161 } else if (UgaDraw
!= NULL
) { // UGA mode (EFI 1.x)
162 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
163 // in all cases, but I don't know how to probe for alternatives....
164 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
165 Status
= refit_call5_wrapper(UgaDraw
->SetMode
, UgaDraw
, ScreenWidth
, ScreenHeight
, UGADepth
, UGARefreshRate
);
166 if (Status
== EFI_SUCCESS
) {
167 egScreenWidth
= ScreenWidth
;
168 egScreenHeight
= ScreenHeight
;
171 // TODO: Find a list of supported modes and display it.
172 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
173 // This is just a placeholder until something better can be done....
174 Print(L
"Error setting mode %d x %d; unsupported mode!\n");
178 } // BOOLEAN egSetScreenSize()
180 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
182 if (ScreenWidth
!= NULL
)
183 *ScreenWidth
= egScreenWidth
;
184 if (ScreenHeight
!= NULL
)
185 *ScreenHeight
= egScreenHeight
;
188 CHAR16
* egScreenDescription(VOID
)
193 if (GraphicsOutput
!= NULL
) {
194 Temp
= AllocateZeroPool(256 * sizeof(CHAR16
));
195 SPrint(Temp
, 255, L
"Graphics Output (UEFI), %dx%d", egScreenWidth
, egScreenHeight
);
197 } else if (UgaDraw
!= NULL
) {
198 Temp
= AllocateZeroPool(256 * sizeof(CHAR16
));
199 SPrint(Temp
, 255, L
"UGA Draw (EFI 1.10), %dx%d", egScreenWidth
, egScreenHeight
);
202 return L
"Internal Error";
205 return L
"Text Console";
209 BOOLEAN
egHasGraphicsMode(VOID
)
211 return egHasGraphics
;
214 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
216 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
218 if (ConsoleControl
!= NULL
) {
219 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
220 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
226 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
228 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
229 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
231 if (ConsoleControl
!= NULL
) {
232 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
234 NewMode
= Enable
? EfiConsoleControlScreenGraphics
235 : EfiConsoleControlScreenText
;
236 if (CurrentMode
!= NewMode
)
237 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
242 // Drawing to the screen
245 VOID
egClearScreen(IN EG_PIXEL
*Color
)
247 EFI_UGA_PIXEL FillColor
;
252 FillColor
.Red
= Color
->r
;
253 FillColor
.Green
= Color
->g
;
254 FillColor
.Blue
= Color
->b
;
255 FillColor
.Reserved
= 0;
257 if (GraphicsOutput
!= NULL
) {
258 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
259 // layout, and the header from TianoCore actually defines them
260 // to be the same type.
261 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
262 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
263 } else if (UgaDraw
!= NULL
) {
264 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
,
265 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
269 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
274 if (Image
->HasAlpha
) {
275 Image
->HasAlpha
= FALSE
;
276 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
279 if (GraphicsOutput
!= NULL
) {
280 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
281 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
282 } else if (UgaDraw
!= NULL
) {
283 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
284 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
288 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
289 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
290 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
291 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
296 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
300 if (Image
->HasAlpha
) {
301 Image
->HasAlpha
= FALSE
;
302 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
305 if (GraphicsOutput
!= NULL
) {
306 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
307 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
308 } else if (UgaDraw
!= NULL
) {
309 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
310 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
314 // Display a message in the center of the screen, surrounded by a box of the
315 // specified color. For the moment, uses graphics calls only. (It still works
316 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
317 VOID
egDisplayMessage(IN CHAR16
*Text
, EG_PIXEL
*BGColor
) {
318 UINTN BoxWidth
, BoxHeight
;
321 if ((Text
!= NULL
) && (BGColor
!= NULL
)) {
322 BoxWidth
= (StrLen(Text
) + 2) * FONT_CELL_WIDTH
;
323 if (BoxWidth
> egScreenWidth
)
324 BoxWidth
= egScreenWidth
;
325 BoxHeight
= 2 * FONT_CELL_HEIGHT
;
326 Box
= egCreateFilledImage(BoxWidth
, BoxHeight
, FALSE
, BGColor
);
327 egRenderText(Text
, Box
, FONT_CELL_WIDTH
, FONT_CELL_HEIGHT
/ 2);
328 egDrawImage(Box
, (egScreenWidth
- BoxWidth
) / 2, (egScreenHeight
- BoxHeight
) / 2);
329 } // if non-NULL inputs
330 } // VOID egDisplayMessage()
336 VOID
egScreenShot(VOID
)
341 UINTN FileDataLength
;
347 // allocate a buffer for the whole screen
348 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
350 Print(L
"Error egCreateImage returned NULL\n");
354 // get full screen image
355 if (GraphicsOutput
!= NULL
) {
356 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
357 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
358 } else if (UgaDraw
!= NULL
) {
359 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
360 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
364 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
366 if (FileData
== NULL
) {
367 Print(L
"Error egEncodeBMP returned NULL\n");
371 // save to file on the ESP
372 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
374 if (EFI_ERROR(Status
)) {
375 Print(L
"Error egSaveFile: %x\n", Status
);
381 // DEBUG: switch to text mode
383 egSetGraphicsModeEnabled(FALSE
);
384 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);