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 "refit_call_wrapper.h"
41 #include <efiUgaDraw.h>
42 #include <efiConsoleControl.h>
44 // Console defines and variables
46 static EFI_GUID ConsoleControlProtocolGuid
= EFI_CONSOLE_CONTROL_PROTOCOL_GUID
;
47 static EFI_CONSOLE_CONTROL_PROTOCOL
*ConsoleControl
= NULL
;
49 static EFI_GUID UgaDrawProtocolGuid
= EFI_UGA_DRAW_PROTOCOL_GUID
;
50 static EFI_UGA_DRAW_PROTOCOL
*UgaDraw
= NULL
;
52 static EFI_GUID GraphicsOutputProtocolGuid
= EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
;
53 static EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
= NULL
;
55 static BOOLEAN egHasGraphics
= FALSE
;
56 static UINTN egScreenWidth
= 800;
57 static UINTN egScreenHeight
= 600;
63 VOID
egInitScreen(VOID
)
65 EFI_STATUS Status
= EFI_SUCCESS
;
66 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
69 Status
= LibLocateProtocol(&ConsoleControlProtocolGuid
, (VOID
**) &ConsoleControl
);
70 if (EFI_ERROR(Status
))
71 ConsoleControl
= NULL
;
73 Status
= LibLocateProtocol(&UgaDrawProtocolGuid
, (VOID
**) &UgaDraw
);
74 if (EFI_ERROR(Status
))
77 Status
= LibLocateProtocol(&GraphicsOutputProtocolGuid
, (VOID
**) &GraphicsOutput
);
78 if (EFI_ERROR(Status
))
79 GraphicsOutput
= NULL
;
82 egHasGraphics
= FALSE
;
83 if (GraphicsOutput
!= NULL
) {
84 egScreenWidth
= GraphicsOutput
->Mode
->Info
->HorizontalResolution
;
85 egScreenHeight
= GraphicsOutput
->Mode
->Info
->VerticalResolution
;
87 } else if (UgaDraw
!= NULL
) {
88 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
89 if (EFI_ERROR(Status
)) {
90 UgaDraw
= NULL
; // graphics not available
92 egScreenWidth
= UGAWidth
;
93 egScreenHeight
= UGAHeight
;
99 // Sets the screen resolution to the specified value, if possible.
100 // If the specified value is not valid, displays a warning with the valid
101 // modes on UEFI systems, or silently fails on EFI 1.x systems. Note that
102 // this function attempts to set ANY screen resolution, even 0x0 or
103 // ridiculously large values.
104 // Returns TRUE if successful, FALSE if not.
105 BOOLEAN
egSetScreenSize(IN UINTN ScreenWidth
, IN UINTN ScreenHeight
) {
106 EFI_STATUS Status
= EFI_SUCCESS
;
107 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*Info
;
110 BOOLEAN ModeSet
= FALSE
;
111 UINT32 UGAWidth
, UGAHeight
, UGADepth
, UGARefreshRate
;
113 if (GraphicsOutput
!= NULL
) { // GOP mode (UEFI)
114 // Do a loop through the modes to see if the specified one is available;
115 // and if so, switch to it....
116 while ((Status
== EFI_SUCCESS
) && (!ModeSet
)) {
117 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
118 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
)) &&
119 (Info
->HorizontalResolution
== ScreenWidth
) && (Info
->VerticalResolution
== ScreenHeight
)) {
120 Status
= refit_call2_wrapper(GraphicsOutput
->SetMode
, GraphicsOutput
, ModeNum
);
121 ModeSet
= (Status
== EFI_SUCCESS
);
127 egScreenWidth
= ScreenWidth
;
128 egScreenHeight
= ScreenHeight
;
129 } else {// If unsuccessful, display an error message for the user....
130 Print(L
"Error setting mode %d x %d; using default mode!\nAvailable modes are:\n", ScreenWidth
, ScreenHeight
);
132 Status
= EFI_SUCCESS
;
133 while (Status
== EFI_SUCCESS
) {
134 Status
= refit_call4_wrapper(GraphicsOutput
->QueryMode
, GraphicsOutput
, ModeNum
, &Size
, &Info
);
135 if ((Status
== EFI_SUCCESS
) && (Size
>= sizeof(*Info
))) {
136 Print(L
"Mode %d: %d x %d\n", ModeNum
, Info
->HorizontalResolution
, Info
->VerticalResolution
);
142 } else if (UgaDraw
!= NULL
) { // UGA mode (EFI 1.x)
143 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
144 // in all cases, but I don't know how to probe for alternatives....
145 Status
= refit_call5_wrapper(UgaDraw
->GetMode
, UgaDraw
, &UGAWidth
, &UGAHeight
, &UGADepth
, &UGARefreshRate
);
146 Status
= refit_call5_wrapper(UgaDraw
->SetMode
, UgaDraw
, ScreenWidth
, ScreenHeight
, UGADepth
, UGARefreshRate
);
147 if (Status
== EFI_SUCCESS
) {
148 egScreenWidth
= ScreenWidth
;
149 egScreenHeight
= ScreenHeight
;
152 // TODO: Find a list of supported modes and display it.
153 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
154 // This is just a placeholder until something better can be done....
155 Print(L
"Error setting mode %d x %d; unsupported mode!\n");
159 } // BOOLEAN egSetScreenSize()
161 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
163 if (ScreenWidth
!= NULL
)
164 *ScreenWidth
= egScreenWidth
;
165 if (ScreenHeight
!= NULL
)
166 *ScreenHeight
= egScreenHeight
;
169 CHAR16
* egScreenDescription(VOID
)
172 if (GraphicsOutput
!= NULL
) {
173 return PoolPrint(L
"Graphics Output (UEFI), %dx%d",
174 egScreenWidth
, egScreenHeight
);
175 } else if (UgaDraw
!= NULL
) {
176 return PoolPrint(L
"UGA Draw (EFI 1.10), %dx%d",
177 egScreenWidth
, egScreenHeight
);
179 return L
"Internal Error";
182 return L
"Text Console";
186 BOOLEAN
egHasGraphicsMode(VOID
)
188 return egHasGraphics
;
191 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
193 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
195 if (ConsoleControl
!= NULL
) {
196 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
197 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
203 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
205 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
206 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
208 if (ConsoleControl
!= NULL
) {
209 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
211 NewMode
= Enable
? EfiConsoleControlScreenGraphics
212 : EfiConsoleControlScreenText
;
213 if (CurrentMode
!= NewMode
)
214 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
219 // Drawing to the screen
222 VOID
egClearScreen(IN EG_PIXEL
*Color
)
224 EFI_UGA_PIXEL FillColor
;
229 FillColor
.Red
= Color
->r
;
230 FillColor
.Green
= Color
->g
;
231 FillColor
.Blue
= Color
->b
;
232 FillColor
.Reserved
= 0;
234 if (GraphicsOutput
!= NULL
) {
235 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
236 // layout, and the header from TianoCore actually defines them
237 // to be the same type.
238 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
239 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
240 } else if (UgaDraw
!= NULL
) {
241 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
,
242 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
246 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
251 if (Image
->HasAlpha
) {
252 Image
->HasAlpha
= FALSE
;
253 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
256 if (GraphicsOutput
!= NULL
) {
257 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
258 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
259 } else if (UgaDraw
!= NULL
) {
260 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
261 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
265 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
266 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
267 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
268 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
273 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
277 if (Image
->HasAlpha
) {
278 Image
->HasAlpha
= FALSE
;
279 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
282 if (GraphicsOutput
!= NULL
) {
283 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
284 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
285 } else if (UgaDraw
!= NULL
) {
286 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
287 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
291 // Display a message in the center of the screen, surrounded by a box of the
292 // specified color. For the moment, uses graphics calls only. (It still works
293 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
294 VOID
egDisplayMessage(IN CHAR16
*Text
, EG_PIXEL
*BGColor
) {
295 UINTN BoxWidth
, BoxHeight
;
298 if ((Text
!= NULL
) && (BGColor
!= NULL
)) {
299 BoxWidth
= (StrLen(Text
) + 2) * FONT_CELL_WIDTH
;
300 if (BoxWidth
> egScreenWidth
)
301 BoxWidth
= egScreenWidth
;
302 BoxHeight
= 2 * FONT_CELL_HEIGHT
;
303 Box
= egCreateFilledImage(BoxWidth
, BoxHeight
, FALSE
, BGColor
);
304 egRenderText(Text
, Box
, FONT_CELL_WIDTH
, FONT_CELL_HEIGHT
/ 2);
305 egDrawImage(Box
, (egScreenWidth
- BoxWidth
) / 2, (egScreenHeight
- BoxHeight
) / 2);
306 } // if non-NULL inputs
307 } // VOID egDisplayMessage()
313 VOID
egScreenShot(VOID
)
318 UINTN FileDataLength
;
324 // allocate a buffer for the whole screen
325 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
327 Print(L
"Error egCreateImage returned NULL\n");
331 // get full screen image
332 if (GraphicsOutput
!= NULL
) {
333 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
334 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
335 } else if (UgaDraw
!= NULL
) {
336 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
337 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
341 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
343 if (FileData
== NULL
) {
344 Print(L
"Error egEncodeBMP returned NULL\n");
348 // save to file on the ESP
349 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
351 if (EFI_ERROR(Status
)) {
352 Print(L
"Error egSaveFile: %x\n", Status
);
358 // DEBUG: switch to text mode
360 egSetGraphicsModeEnabled(FALSE
);
361 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);