]> code.delx.au - refind/blob - libeg/screen.c
Added icons and libeg files
[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 "refit_call_wrapper.h"
39
40 #include <efiUgaDraw.h>
41 /* #include <efiGraphicsOutput.h> */
42 #include <efiConsoleControl.h>
43
44 // Console defines and variables
45
46 static EFI_GUID ConsoleControlProtocolGuid = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
47 static EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
48
49 static EFI_GUID UgaDrawProtocolGuid = EFI_UGA_DRAW_PROTOCOL_GUID;
50 static EFI_UGA_DRAW_PROTOCOL *UgaDraw = NULL;
51
52 static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
53 static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
54
55 static BOOLEAN egHasGraphics = FALSE;
56 static UINTN egScreenWidth = 800;
57 static UINTN egScreenHeight = 600;
58
59 //
60 // Screen handling
61 //
62
63 VOID egInitScreen(VOID)
64 {
65 EFI_STATUS Status;
66 UINT32 UGAWidth, UGAHeight, UGADepth, UGARefreshRate;
67
68 // get protocols
69 Status = LibLocateProtocol(&ConsoleControlProtocolGuid, (VOID **) &ConsoleControl);
70 if (EFI_ERROR(Status))
71 ConsoleControl = NULL;
72
73 Status = LibLocateProtocol(&UgaDrawProtocolGuid, (VOID **) &UgaDraw);
74 if (EFI_ERROR(Status))
75 UgaDraw = NULL;
76
77 Status = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **) &GraphicsOutput);
78 if (EFI_ERROR(Status))
79 GraphicsOutput = NULL;
80
81 // get screen size
82 egHasGraphics = FALSE;
83 if (GraphicsOutput != NULL) {
84 egScreenWidth = GraphicsOutput->Mode->Info->HorizontalResolution;
85 egScreenHeight = GraphicsOutput->Mode->Info->VerticalResolution;
86 egHasGraphics = TRUE;
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
91 } else {
92 egScreenWidth = UGAWidth;
93 egScreenHeight = UGAHeight;
94 egHasGraphics = TRUE;
95 }
96 }
97 }
98
99 VOID egGetScreenSize(OUT UINTN *ScreenWidth, OUT UINTN *ScreenHeight)
100 {
101 if (ScreenWidth != NULL)
102 *ScreenWidth = egScreenWidth;
103 if (ScreenHeight != NULL)
104 *ScreenHeight = egScreenHeight;
105 }
106
107 CHAR16 * egScreenDescription(VOID)
108 {
109 if (egHasGraphics) {
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);
116 } else {
117 return L"Internal Error";
118 }
119 } else {
120 return L"Text Console";
121 }
122 }
123
124 BOOLEAN egHasGraphicsMode(VOID)
125 {
126 return egHasGraphics;
127 }
128
129 BOOLEAN egIsGraphicsModeEnabled(VOID)
130 {
131 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
132
133 if (ConsoleControl != NULL) {
134 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
135 return (CurrentMode == EfiConsoleControlScreenGraphics) ? TRUE : FALSE;
136 }
137
138 return FALSE;
139 }
140
141 VOID egSetGraphicsModeEnabled(IN BOOLEAN Enable)
142 {
143 EFI_CONSOLE_CONTROL_SCREEN_MODE CurrentMode;
144 EFI_CONSOLE_CONTROL_SCREEN_MODE NewMode;
145
146 if (ConsoleControl != NULL) {
147 refit_call4_wrapper(ConsoleControl->GetMode, ConsoleControl, &CurrentMode, NULL, NULL);
148
149 NewMode = Enable ? EfiConsoleControlScreenGraphics
150 : EfiConsoleControlScreenText;
151 if (CurrentMode != NewMode)
152 refit_call2_wrapper(ConsoleControl->SetMode, ConsoleControl, NewMode);
153 }
154 }
155
156 //
157 // Drawing to the screen
158 //
159
160 VOID egClearScreen(IN EG_PIXEL *Color)
161 {
162 EFI_UGA_PIXEL FillColor;
163
164 if (!egHasGraphics)
165 return;
166
167 FillColor.Red = Color->r;
168 FillColor.Green = Color->g;
169 FillColor.Blue = Color->b;
170 FillColor.Reserved = 0;
171
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);
181 }
182 }
183
184 VOID egDrawImage(IN EG_IMAGE *Image, IN UINTN ScreenPosX, IN UINTN ScreenPosY)
185 {
186 if (!egHasGraphics)
187 return;
188
189 if (Image->HasAlpha) {
190 Image->HasAlpha = FALSE;
191 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
192 }
193
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);
200 }
201 }
202
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)
207 {
208 if (!egHasGraphics)
209 return;
210
211 egRestrictImageArea(Image, AreaPosX, AreaPosY, &AreaWidth, &AreaHeight);
212 if (AreaWidth == 0)
213 return;
214
215 if (Image->HasAlpha) {
216 Image->HasAlpha = FALSE;
217 egSetPlane(PLPTR(Image, a), 0, Image->Width * Image->Height);
218 }
219
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);
226 }
227 }
228
229 //
230 // Make a screenshot
231 //
232
233 VOID egScreenShot(VOID)
234 {
235 EFI_STATUS Status;
236 EG_IMAGE *Image;
237 UINT8 *FileData;
238 UINTN FileDataLength;
239 UINTN Index;
240
241 if (!egHasGraphics)
242 return;
243
244 // allocate a buffer for the whole screen
245 Image = egCreateImage(egScreenWidth, egScreenHeight, FALSE);
246 if (Image == NULL) {
247 Print(L"Error egCreateImage returned NULL\n");
248 goto bailout_wait;
249 }
250
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);
258 }
259
260 // encode as BMP
261 egEncodeBMP(Image, &FileData, &FileDataLength);
262 egFreeImage(Image);
263 if (FileData == NULL) {
264 Print(L"Error egEncodeBMP returned NULL\n");
265 goto bailout_wait;
266 }
267
268 // save to file on the ESP
269 Status = egSaveFile(NULL, L"screenshot.bmp", FileData, FileDataLength);
270 FreePool(FileData);
271 if (EFI_ERROR(Status)) {
272 Print(L"Error egSaveFile: %x\n", Status);
273 goto bailout_wait;
274 }
275
276 return;
277
278 // DEBUG: switch to text mode
279 bailout_wait:
280 egSetGraphicsModeEnabled(FALSE);
281 refit_call3_wrapper(BS->WaitForEvent, 1, &ST->ConIn->WaitForKey, &Index);
282 }
283
284 /* EOF */