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 "refit_call_wrapper.h"
40 #include <efiUgaDraw.h>
41 /* #include <efiGraphicsOutput.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
)
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 VOID
egGetScreenSize(OUT UINTN
*ScreenWidth
, OUT UINTN
*ScreenHeight
)
101 if (ScreenWidth
!= NULL
)
102 *ScreenWidth
= egScreenWidth
;
103 if (ScreenHeight
!= NULL
)
104 *ScreenHeight
= egScreenHeight
;
107 CHAR16
* egScreenDescription(VOID
)
110 if (GraphicsOutput
!= NULL
) {
111 return PoolPrint(L
"Graphics Output (UEFI), %dx%d",
112 egScreenWidth
, egScreenHeight
);
113 } else if (UgaDraw
!= NULL
) {
114 return PoolPrint(L
"UGA Draw (EFI 1.10), %dx%d",
115 egScreenWidth
, egScreenHeight
);
117 return L
"Internal Error";
120 return L
"Text Console";
124 BOOLEAN
egHasGraphicsMode(VOID
)
126 return egHasGraphics
;
129 BOOLEAN
egIsGraphicsModeEnabled(VOID
)
131 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
133 if (ConsoleControl
!= NULL
) {
134 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
135 return (CurrentMode
== EfiConsoleControlScreenGraphics
) ? TRUE
: FALSE
;
141 VOID
egSetGraphicsModeEnabled(IN BOOLEAN Enable
)
143 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode
;
144 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode
;
146 if (ConsoleControl
!= NULL
) {
147 refit_call4_wrapper(ConsoleControl
->GetMode
, ConsoleControl
, &CurrentMode
, NULL
, NULL
);
149 NewMode
= Enable
? EfiConsoleControlScreenGraphics
150 : EfiConsoleControlScreenText
;
151 if (CurrentMode
!= NewMode
)
152 refit_call2_wrapper(ConsoleControl
->SetMode
, ConsoleControl
, NewMode
);
157 // Drawing to the screen
160 VOID
egClearScreen(IN EG_PIXEL
*Color
)
162 EFI_UGA_PIXEL FillColor
;
167 FillColor
.Red
= Color
->r
;
168 FillColor
.Green
= Color
->g
;
169 FillColor
.Blue
= Color
->b
;
170 FillColor
.Reserved
= 0;
172 if (GraphicsOutput
!= NULL
) {
173 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
174 // layout, and the header from TianoCore actually defines them
175 // to be the same type.
176 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)&FillColor
, EfiBltVideoFill
,
177 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
178 } else if (UgaDraw
!= NULL
) {
179 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, &FillColor
, EfiUgaVideoFill
,
180 0, 0, 0, 0, egScreenWidth
, egScreenHeight
, 0);
184 VOID
egDrawImage(IN EG_IMAGE
*Image
, IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
189 if (Image
->HasAlpha
) {
190 Image
->HasAlpha
= FALSE
;
191 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
194 if (GraphicsOutput
!= NULL
) {
195 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
196 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
197 } else if (UgaDraw
!= NULL
) {
198 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
199 0, 0, ScreenPosX
, ScreenPosY
, Image
->Width
, Image
->Height
, 0);
203 VOID
egDrawImageArea(IN EG_IMAGE
*Image
,
204 IN UINTN AreaPosX
, IN UINTN AreaPosY
,
205 IN UINTN AreaWidth
, IN UINTN AreaHeight
,
206 IN UINTN ScreenPosX
, IN UINTN ScreenPosY
)
211 egRestrictImageArea(Image
, AreaPosX
, AreaPosY
, &AreaWidth
, &AreaHeight
);
215 if (Image
->HasAlpha
) {
216 Image
->HasAlpha
= FALSE
;
217 egSetPlane(PLPTR(Image
, a
), 0, Image
->Width
* Image
->Height
);
220 if (GraphicsOutput
!= NULL
) {
221 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltBufferToVideo
,
222 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
223 } else if (UgaDraw
!= NULL
) {
224 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaBltBufferToVideo
,
225 AreaPosX
, AreaPosY
, ScreenPosX
, ScreenPosY
, AreaWidth
, AreaHeight
, Image
->Width
* 4);
233 VOID
egScreenShot(VOID
)
238 UINTN FileDataLength
;
244 // allocate a buffer for the whole screen
245 Image
= egCreateImage(egScreenWidth
, egScreenHeight
, FALSE
);
247 Print(L
"Error egCreateImage returned NULL\n");
251 // get full screen image
252 if (GraphicsOutput
!= NULL
) {
253 refit_call10_wrapper(GraphicsOutput
->Blt
, GraphicsOutput
, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*)Image
->PixelData
, EfiBltVideoToBltBuffer
,
254 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
255 } else if (UgaDraw
!= NULL
) {
256 refit_call10_wrapper(UgaDraw
->Blt
, UgaDraw
, (EFI_UGA_PIXEL
*)Image
->PixelData
, EfiUgaVideoToBltBuffer
,
257 0, 0, 0, 0, Image
->Width
, Image
->Height
, 0);
261 egEncodeBMP(Image
, &FileData
, &FileDataLength
);
263 if (FileData
== NULL
) {
264 Print(L
"Error egEncodeBMP returned NULL\n");
268 // save to file on the ESP
269 Status
= egSaveFile(NULL
, L
"screenshot.bmp", FileData
, FileDataLength
);
271 if (EFI_ERROR(Status
)) {
272 Print(L
"Error egSaveFile: %x\n", Status
);
278 // DEBUG: switch to text mode
280 egSetGraphicsModeEnabled(FALSE
);
281 refit_call3_wrapper(BS
->WaitForEvent
, 1, &ST
->ConIn
->WaitForKey
, &Index
);