]> code.delx.au - refind/blob - libeg/screen.c
TianoCore build support; new use_graphics_for refind.conf token
[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 #include "libegint.h"
38 #include "../refind/screen.h"
39 #include "../include/refit_call_wrapper.h"
40
41 #include <efiUgaDraw.h>
42 #include <efiConsoleControl.h>
43
44 #ifndef __MAKEWITH_GNUEFI
45 #define LibLocateProtocol EfiLibLocateProtocol
46 #endif
47
48 // Console defines and variables
49
50 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
51 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
52
53 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
54 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
55
56 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
57 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
58
59 static BOOLEAN egHasGraphics = FALSE;
60 static UINTN egScreenWidth = 800;
61 static UINTN egScreenHeight = 600;
62
63 //
64 // Screen handling
65 //
66
67 VOID egInitScreen(VOID)
68 {
69 EFI_STATUS Status = EFI_SUCCESS;
70 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
71
72 // get protocols
73 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
74 if (EFI_ERROR(Status))
75 ConsoleControl = NULL;
76
77 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
78 if (EFI_ERROR(Status))
79 UgaDraw = NULL;
80
81 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
82 if (EFI_ERROR(Status))
83 GraphicsOutput = NULL;
84
85 // get screen size
86 egHasGraphics = FALSE;
87 if (GraphicsOutput != NULL) {
88 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
89 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
90 egHasGraphics = TRUE;
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
95 } else {
96 egScreenWidth = UGAWidth;
97 egScreenHeight = UGAHeight;
98 egHasGraphics = TRUE;
99 }
100 }
101 }
102
103 // Sets the screen resolution to the specified value, if possible.
104 // If the specified value is not valid, displays a warning with the valid
105 // modes on UEFI systems, or silently fails on EFI 1.x systems. Note that
106 // this function attempts to set ANY screen resolution, even 0x0 or
107 // ridiculously large values.
108 // Returns TRUE if successful, FALSE if not.
109 BOOLEAN egSetScreenSize(IN UINTN ScreenWidth, IN UINTN ScreenHeight) {
110 EFI_STATUS Status = EFI_SUCCESS;
111 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
112 UINT32 ModeNum = 0;
113 UINTN Size;
114 BOOLEAN ModeSet = FALSE;
115 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
116
117 if (GraphicsOutput != NULL) { // GOP mode (UEFI)
118 // Do a loop through the modes to see if the specified one is available;
119 // and if so, switch to it....
120 while ((Status == EFI_SUCCESS) && (!ModeSet)) {
121 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
122 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info)) &&
123 (Info->HorizontalResolution == ScreenWidth) && (Info->VerticalResolution == ScreenHeight)) {
124 Status = refit_call2_wrapper(GraphicsOutput->SetMode, GraphicsOutput, ModeNum);
125 ModeSet = (Status == EFI_SUCCESS);
126 } // if
127 ModeNum++;
128 } // while()
129
130 if (ModeSet) {
131 egScreenWidth = ScreenWidth;
132 egScreenHeight = ScreenHeight;
133 } else {// If unsuccessful, display an error message for the user....
134 Print(L"Error setting mode %d x %d; using default mode!\nAvailable modes are:\n", ScreenWidth, ScreenHeight);
135 ModeNum = 0;
136 Status = EFI_SUCCESS;
137 while (Status == EFI_SUCCESS) {
138 Status = refit_call4_wrapper(GraphicsOutput->QueryMode, GraphicsOutput, ModeNum, &Size, &Info);
139 if ((Status == EFI_SUCCESS) && (Size >= sizeof(*Info))) {
140 Print(L"Mode %d: %d x %d\n", ModeNum, Info->HorizontalResolution, Info->VerticalResolution);
141 } // else
142 ModeNum++;
143 } // while()
144 PauseForKey();
145 } // if()
146 } else if (UgaDraw != NULL) { // UGA mode (EFI 1.x)
147 // Try to use current color depth & refresh rate for new mode. Maybe not the best choice
148 // in all cases, but I don't know how to probe for alternatives....
149 Status = refit_call5_wrapper(UgaDraw->GetMode, UgaDraw, &UGAWidth, &UGAHeight, &UGADepth, &UGARefreshRate);
150 Status = refit_call5_wrapper(UgaDraw->SetMode, UgaDraw, ScreenWidth, ScreenHeight, UGADepth, UGARefreshRate);
151 if (Status == EFI_SUCCESS) {
152 egScreenWidth = ScreenWidth;
153 egScreenHeight = ScreenHeight;
154 ModeSet = TRUE;
155 } else {
156 // TODO: Find a list of supported modes and display it.
157 // NOTE: Below doesn't actually appear unless we explicitly switch to text mode.
158 // This is just a placeholder until something better can be done....
159 Print(L"Error setting mode %d x %d; unsupported mode!\n");
160 } // if/else
161 } // if/else if
162 return (ModeSet);
163 } // BOOLEAN egSetScreenSize()
164
165 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
166 {
167 if (ScreenWidth != NULL)
168 *ScreenWidth = egScreenWidth;
169 if (ScreenHeight != NULL)
170 *ScreenHeight = egScreenHeight;
171 }
172
173 CHAR16 * egScreenDescription(VOID)
174 {
175 CHAR16 *Temp;
176
177 if (egHasGraphics) {
178 if (GraphicsOutput != NULL) {
179 Temp = AllocateZeroPool(256 * sizeof(CHAR16));
180 SPrint(Temp, 255, L"Graphics Output (UEFI), %dx%d", egScreenWidth, egScreenHeight);
181 return Temp;
182 } else if (UgaDraw != NULL) {
183 Temp = AllocateZeroPool(256 * sizeof(CHAR16));
184 SPrint(Temp, 255, L"UGA Draw (EFI 1.10), %dx%d", egScreenWidth, egScreenHeight);
185 return Temp;
186 } else {
187 return L"Internal Error";
188 }
189 } else {
190 return L"Text Console";
191 }
192 }
193
194 BOOLEAN egHasGraphicsMode(VOID)
195 {
196 return egHasGraphics;
197 }
198
199 BOOLEAN egIsGraphicsModeEnabled(VOID)
200 {
201 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
202
203 if (ConsoleControl != NULL) {
204 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
205 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
206 }
207
208 return FALSE;
209 }
210
211 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
212 {
213 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
214 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
215
216 if (ConsoleControl != NULL) {
217 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
218
219 NewMode = Enable ? EfiConsoleControlScreenGraphics
220 : EfiConsoleControlScreenText;
221 if (CurrentMode != NewMode)
222 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
223 }
224 }
225
226 //
227 // Drawing to the screen
228 //
229
230 VOID egClearScreen(IN EG_PIXEL *Color)
231 {
232 EFI_UGA_PIXEL FillColor;
233
234 if (!egHasGraphics)
235 return;
236
237 FillColor.Red = Color->r;
238 FillColor.Green = Color->g;
239 FillColor.Blue = Color->b;
240 FillColor.Reserved = 0;
241
242 if (GraphicsOutput != NULL) {
243 // EFI_GRAPHICS_OUTPUT_BLT_PIXEL and EFI_UGA_PIXEL have the same
244 // layout, and the header from TianoCore actually defines them
245 // to be the same type.
246 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)&FillColor, EfiBltVideoFill,
247 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
248 } else if (UgaDraw != NULL) {
249 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, &FillColor, EfiUgaVideoFill,
250 0, 0, 0, 0, egScreenWidth, egScreenHeight, 0);
251 }
252 }
253
254 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
255 {
256 if (!egHasGraphics)
257 return;
258
259 if (Image->HasAlpha) {
260 Image->HasAlpha = FALSE;
261 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
262 }
263
264 if (GraphicsOutput != NULL) {
265 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
266 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
267 } else if (UgaDraw != NULL) {
268 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
269 0, 0, ScreenPosX, ScreenPosY, Image->Width, Image->Height, 0);
270 }
271 }
272
273 VOID egDrawImageArea(IN EG_IMAGE *Image,
274 IN UINTN AreaPosX, IN UINTN AreaPosY,
275 IN UINTN AreaWidth, IN UINTN AreaHeight,
276 IN UINTN ScreenPosX, IN UINTN ScreenPosY)
277 {
278 if (!egHasGraphics)
279 return;
280
281 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
282 if (AreaWidth == 0)
283 return;
284
285 if (Image->HasAlpha) {
286 Image->HasAlpha = FALSE;
287 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
288 }
289
290 if (GraphicsOutput != NULL) {
291 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltBufferToVideo,
292 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
293 } else if (UgaDraw != NULL) {
294 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaBltBufferToVideo,
295 AreaPosX, AreaPosY, ScreenPosX, ScreenPosY, AreaWidth, AreaHeight, Image->Width * 4);
296 }
297 }
298
299 // Display a message in the center of the screen, surrounded by a box of the
300 // specified color. For the moment, uses graphics calls only. (It still works
301 // in text mode on GOP/UEFI systems, but not on UGA/EFI 1.x systems.)
302 VOID egDisplayMessage(IN CHAR16 *Text, EG_PIXEL *BGColor) {
303 UINTN BoxWidth, BoxHeight;
304 EG_IMAGE *Box;
305
306 if ((Text != NULL) && (BGColor != NULL)) {
307 BoxWidth = (StrLen(Text) + 2) * FONT_CELL_WIDTH;
308 if (BoxWidth > egScreenWidth)
309 BoxWidth = egScreenWidth;
310 BoxHeight = 2 * FONT_CELL_HEIGHT;
311 Box = egCreateFilledImage(BoxWidth, BoxHeight, FALSE, BGColor);
312 egRenderText(Text, Box, FONT_CELL_WIDTH, FONT_CELL_HEIGHT / 2);
313 egDrawImage(Box, (egScreenWidth - BoxWidth) / 2, (egScreenHeight - BoxHeight) / 2);
314 } // if non-NULL inputs
315 } // VOID egDisplayMessage()
316
317 //
318 // Make a screenshot
319 //
320
321 VOID egScreenShot(VOID)
322 {
323 EFI_STATUS Status;
324 EG_IMAGE *Image;
325 UINT8 *FileData;
326 UINTN FileDataLength;
327 UINTN Index;
328
329 if (!egHasGraphics)
330 return;
331
332 // allocate a buffer for the whole screen
333 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
334 if (Image == NULL) {
335 Print(L"Error egCreateImage returned NULL\n");
336 goto bailout_wait;
337 }
338
339 // get full screen image
340 if (GraphicsOutput != NULL) {
341 refit_call10_wrapper(GraphicsOutput->Blt, GraphicsOutput, (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)Image->PixelData, EfiBltVideoToBltBuffer,
342 0, 0, 0, 0, Image->Width, Image->Height, 0);
343 } else if (UgaDraw != NULL) {
344 refit_call10_wrapper(UgaDraw->Blt, UgaDraw, (EFI_UGA_PIXEL *)Image->PixelData, EfiUgaVideoToBltBuffer,
345 0, 0, 0, 0, Image->Width, Image->Height, 0);
346 }
347
348 // encode as BMP
349 egEncodeBMP(Image, &FileData, &FileDataLength);
350 egFreeImage(Image);
351 if (FileData == NULL) {
352 Print(L"Error egEncodeBMP returned NULL\n");
353 goto bailout_wait;
354 }
355
356 // save to file on the ESP
357 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
358 FreePool(FileData);
359 if (EFI_ERROR(Status)) {
360 Print(L"Error egSaveFile: %x\n", Status);
361 goto bailout_wait;
362 }
363
364 return;
365
366 // DEBUG: switch to text mode
367 bailout_wait:
368 egSetGraphicsModeEnabled(FALSE);
369 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
370 }
371
372 /* EOF */