SimpleKernel  0.0.1
graphics.cpp
Go to the documentation of this file.
1 
17 #include <stdexcept>
18 
19 #include "load_elf.h"
20 #include "ostream.hpp"
21 
23  auto status = LibLocateProtocol(&GraphicsOutputProtocol,
24  reinterpret_cast<void **>(&gop));
25  if (EFI_ERROR(status)) {
26  debug << L"Could not locate GOP: " << status << ostream::endl;
27  throw std::runtime_error("EFI_ERROR(status)");
28  }
29  if (gop == nullptr) {
30  debug << L"LibLocateProtocol(GraphicsOutputProtocol, &gop) returned "
31  << status << " but gop is nullptr" << ostream::endl;
32 
33  throw std::runtime_error("gop == nullptr");
34  }
35 }
36 
37 void Graphics::set_mode(EFI_GRAPHICS_PIXEL_FORMAT _format, uint32_t _width,
38  uint32_t _height) const {
39  EFI_STATUS status = EFI_SUCCESS;
40 
41  for (uint32_t i = 0; i < gop->Mode->MaxMode; i++) {
42  EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *mode_info = nullptr;
43  uint64_t mode_info_size = 0;
44  status = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &mode_info_size,
45  &mode_info);
46  if (EFI_ERROR(status)) {
47  debug << L"QueryMode failed: " << status << ostream::endl;
48  throw std::runtime_error("EFI_ERROR(status)");
49  }
50 
51  if ((mode_info->PixelFormat == _format) &&
52  (mode_info->HorizontalResolution == _width) &&
53  (mode_info->VerticalResolution == _height)) {
54  status = uefi_call_wrapper(gop->SetMode, 2, gop, i);
55  if (EFI_ERROR(status)) {
56  debug << L"SetMode failed: " << status << ostream::endl;
57  throw std::runtime_error("EFI_ERROR(status)");
58  }
59  }
60  status = uefi_call_wrapper(gBS->FreePool, 1, mode_info);
61  if (EFI_ERROR(status)) {
62  debug << L"FreePool failed: " << status << ostream::endl;
63  throw std::runtime_error("EFI_ERROR(status)");
64  }
65  }
66 
67  debug << L"Current Mode: " << gop->Mode->Mode << L", Version: "
68  << ostream::hex_x << gop->Mode->Info->Version << L", Format: "
69  << gop->Mode->Info->PixelFormat << L", Horizontal: "
70  << gop->Mode->Info->HorizontalResolution << L", Vertical: "
71  << gop->Mode->Info->VerticalResolution << L", ScanLine: "
72  << gop->Mode->Info->PixelsPerScanLine << L", FrameBufferBase: "
73  << ostream::hex_X << gop->Mode->FrameBufferBase
74  << L", FrameBufferSize: " << ostream::hex_X
75  << gop->Mode->FrameBufferSize << ostream::endl;
76 }
77 
78 void Graphics::print_info() const {
79  debug << L"Current Mode: " << gop->Mode->Mode << L", Version: "
80  << ostream::hex_x << gop->Mode->Info->Version << L", Format: "
81  << gop->Mode->Info->PixelFormat << L", Horizontal: "
82  << gop->Mode->Info->HorizontalResolution << L", Vertical: "
83  << gop->Mode->Info->VerticalResolution << L", ScanLine: "
84  << gop->Mode->Info->PixelsPerScanLine << L", FrameBufferBase: "
85  << ostream::hex_X << gop->Mode->FrameBufferBase
86  << L", FrameBufferSize: " << ostream::hex_X
87  << gop->Mode->FrameBufferSize << ostream::endl;
88 
89  for (uint32_t i = 0; i < gop->Mode->MaxMode; i++) {
90  EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *mode_info = nullptr;
91  uint64_t mode_info_size = 0;
92  auto status = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &mode_info_size,
93  &mode_info);
94  if (EFI_ERROR(status)) {
95  debug << L"QueryMode failed: " << status << ostream::endl;
96  throw std::runtime_error("EFI_ERROR(status)");
97  }
98 
99  debug << L"Mode: " << i << L", Version: " << ostream::hex_x
100  << gop->Mode->Info->Version << L", Format: "
101  << gop->Mode->Info->PixelFormat << L", Horizontal: "
102  << gop->Mode->Info->HorizontalResolution << L", Vertical: "
103  << gop->Mode->Info->VerticalResolution << L", ScanLine: "
104  << gop->Mode->Info->PixelsPerScanLine << ostream::endl;
105 
106  status = uefi_call_wrapper(gBS->FreePool, 1, mode_info);
107  if (EFI_ERROR(status)) {
108  debug << L"FreePool failed: " << status << ostream::endl;
109  throw std::runtime_error("EFI_ERROR(status)");
110  }
111  }
112 }
void set_mode(EFI_GRAPHICS_PIXEL_FORMAT _format=PixelBlueGreenRedReserved8BitPerColor, uint32_t _width=DEFAULT_WIDTH, uint32_t _height=DEFAULT_HEIGHT) const
Definition: graphics.cpp:37
EFI_GRAPHICS_OUTPUT_PROTOCOL * gop
Definition: load_elf.h:81
void print_info() const
Definition: graphics.cpp:78
Graphics()
Definition: graphics.cpp:22
static auto endl(ostream &_ostream) -> ostream &
Definition: ostream.cpp:42
static auto hex_x(ostream &_ostream) -> ostream &
Definition: ostream.cpp:32
static auto hex_X(ostream &_ostream) -> ostream &
Definition: ostream.cpp:37
load_elf h
ostream hpp
static ostream debug
全局输出流
Definition: ostream.hpp:194