GopScreen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*++ @file
  2. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2010 - 2011, Apple Inc. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. Module Name:
  6. EmuGopScreen.c
  7. Abstract:
  8. This file produces the graphics abstration of UGA. It is called by
  9. EmuGopDriver.c file which deals with the EFI 1.1 driver model.
  10. This file just does graphics.
  11. **/
  12. #include "Gop.h"
  13. EFI_EVENT mGopScreenExitBootServicesEvent;
  14. GOP_MODE_DATA mGopModeData[] = {
  15. { 800, 600, 0, 0 },
  16. { 640, 480, 0, 0 },
  17. { 720, 400, 0, 0 },
  18. { 1024, 768, 0, 0 },
  19. { 1280, 1024, 0, 0 }
  20. };
  21. /**
  22. Returns information for an available graphics mode that the graphics device
  23. and the set of active video output devices supports.
  24. @param This The EFI_GRAPHICS_OUTPUT_PROTOCOL instance.
  25. @param ModeNumber The mode number to return information on.
  26. @param SizeOfInfo A pointer to the size, in bytes, of the Info buffer.
  27. @param Info A pointer to callee allocated buffer that returns information about ModeNumber.
  28. @retval EFI_SUCCESS Mode information returned.
  29. @retval EFI_BUFFER_TOO_SMALL The Info buffer was too small.
  30. @retval EFI_DEVICE_ERROR A hardware error occurred trying to retrieve the video mode.
  31. @retval EFI_NOT_STARTED Video display is not initialized. Call SetMode ()
  32. @retval EFI_INVALID_PARAMETER One of the input args was NULL.
  33. **/
  34. EFI_STATUS
  35. EFIAPI
  36. EmuGopQuerytMode (
  37. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  38. IN UINT32 ModeNumber,
  39. OUT UINTN *SizeOfInfo,
  40. OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
  41. )
  42. {
  43. GOP_PRIVATE_DATA *Private;
  44. Private = GOP_PRIVATE_DATA_FROM_THIS (This);
  45. if ((Info == NULL) || (SizeOfInfo == NULL) || ((UINTN)ModeNumber >= This->Mode->MaxMode)) {
  46. return EFI_INVALID_PARAMETER;
  47. }
  48. *Info = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION));
  49. if (*Info == NULL) {
  50. return EFI_OUT_OF_RESOURCES;
  51. }
  52. *SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
  53. (*Info)->Version = 0;
  54. (*Info)->HorizontalResolution = Private->ModeData[ModeNumber].HorizontalResolution;
  55. (*Info)->VerticalResolution = Private->ModeData[ModeNumber].VerticalResolution;
  56. (*Info)->PixelFormat = PixelBltOnly;
  57. (*Info)->PixelsPerScanLine = (*Info)->HorizontalResolution;
  58. return EFI_SUCCESS;
  59. }
  60. /**
  61. Set the video device into the specified mode and clears the visible portions of
  62. the output display to black.
  63. @param This The EFI_GRAPHICS_OUTPUT_PROTOCOL instance.
  64. @param ModeNumber Abstraction that defines the current video mode.
  65. @retval EFI_SUCCESS The graphics mode specified by ModeNumber was selected.
  66. @retval EFI_DEVICE_ERROR The device had an error and could not complete the request.
  67. @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.
  68. **/
  69. EFI_STATUS
  70. EFIAPI
  71. EmuGopSetMode (
  72. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  73. IN UINT32 ModeNumber
  74. )
  75. {
  76. EFI_STATUS Status;
  77. GOP_PRIVATE_DATA *Private;
  78. GOP_MODE_DATA *ModeData;
  79. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Fill;
  80. Private = GOP_PRIVATE_DATA_FROM_THIS (This);
  81. if (ModeNumber >= This->Mode->MaxMode) {
  82. return EFI_UNSUPPORTED;
  83. }
  84. ModeData = &Private->ModeData[ModeNumber];
  85. This->Mode->Mode = ModeNumber;
  86. Private->GraphicsOutput.Mode->Info->HorizontalResolution = ModeData->HorizontalResolution;
  87. Private->GraphicsOutput.Mode->Info->VerticalResolution = ModeData->VerticalResolution;
  88. Private->GraphicsOutput.Mode->Info->PixelsPerScanLine = ModeData->HorizontalResolution;
  89. if (Private->HardwareNeedsStarting) {
  90. Status = EmuGopStartWindow (
  91. Private,
  92. ModeData->HorizontalResolution,
  93. ModeData->VerticalResolution,
  94. ModeData->ColorDepth,
  95. ModeData->RefreshRate
  96. );
  97. if (EFI_ERROR (Status)) {
  98. return EFI_DEVICE_ERROR;
  99. }
  100. Private->HardwareNeedsStarting = FALSE;
  101. }
  102. Status = Private->EmuGraphicsWindow->Size (
  103. Private->EmuGraphicsWindow,
  104. ModeData->HorizontalResolution,
  105. ModeData->VerticalResolution
  106. );
  107. Fill.Red = 0;
  108. Fill.Green = 0;
  109. Fill.Blue = 0;
  110. This->Blt (
  111. This,
  112. &Fill,
  113. EfiBltVideoFill,
  114. 0,
  115. 0,
  116. 0,
  117. 0,
  118. ModeData->HorizontalResolution,
  119. ModeData->VerticalResolution,
  120. ModeData->HorizontalResolution * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
  121. );
  122. return EFI_SUCCESS;
  123. }
  124. /**
  125. Blt a rectangle of pixels on the graphics screen. Blt stands for BLock Transfer.
  126. @param This Protocol instance pointer.
  127. @param BltBuffer Buffer containing data to blit into video buffer. This
  128. buffer has a size of Width*Height*sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
  129. @param BltOperation Operation to perform on BlitBuffer and video memory
  130. @param SourceX X coordinate of source for the BltBuffer.
  131. @param SourceY Y coordinate of source for the BltBuffer.
  132. @param DestinationX X coordinate of destination for the BltBuffer.
  133. @param DestinationY Y coordinate of destination for the BltBuffer.
  134. @param Width Width of rectangle in BltBuffer in pixels.
  135. @param Height Hight of rectangle in BltBuffer in pixels.
  136. @param Delta OPTIONAL
  137. @retval EFI_SUCCESS The Blt operation completed.
  138. @retval EFI_INVALID_PARAMETER BltOperation is not valid.
  139. @retval EFI_DEVICE_ERROR A hardware error occurred writting to the video buffer.
  140. **/
  141. EFI_STATUS
  142. EFIAPI
  143. EmuGopBlt (
  144. IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
  145. IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer OPTIONAL,
  146. IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
  147. IN UINTN SourceX,
  148. IN UINTN SourceY,
  149. IN UINTN DestinationX,
  150. IN UINTN DestinationY,
  151. IN UINTN Width,
  152. IN UINTN Height,
  153. IN UINTN Delta OPTIONAL
  154. )
  155. {
  156. GOP_PRIVATE_DATA *Private;
  157. EFI_TPL OriginalTPL;
  158. EFI_STATUS Status;
  159. EMU_GRAPHICS_WINDOWS__BLT_ARGS GopBltArgs;
  160. Private = GOP_PRIVATE_DATA_FROM_THIS (This);
  161. if ((UINT32)BltOperation >= EfiGraphicsOutputBltOperationMax) {
  162. return EFI_INVALID_PARAMETER;
  163. }
  164. if ((Width == 0) || (Height == 0)) {
  165. return EFI_INVALID_PARAMETER;
  166. }
  167. //
  168. // If Delta is zero, then the entire BltBuffer is being used, so Delta
  169. // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
  170. // the number of bytes in each row can be computed.
  171. //
  172. if (Delta == 0) {
  173. Delta = Width * sizeof (EFI_UGA_PIXEL);
  174. }
  175. //
  176. // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
  177. // We would not want a timer based event (Cursor, ...) to come in while we are
  178. // doing this operation.
  179. //
  180. OriginalTPL = gBS->RaiseTPL (TPL_NOTIFY);
  181. //
  182. // Pack UGA Draw protocol parameters to EMU_GRAPHICS_WINDOWS__BLT_ARGS structure to adapt to
  183. // GopBlt() API of Unix UGA IO protocol.
  184. //
  185. GopBltArgs.DestinationX = DestinationX;
  186. GopBltArgs.DestinationY = DestinationY;
  187. GopBltArgs.Height = Height;
  188. GopBltArgs.Width = Width;
  189. GopBltArgs.SourceX = SourceX;
  190. GopBltArgs.SourceY = SourceY;
  191. GopBltArgs.Delta = Delta;
  192. Status = Private->EmuGraphicsWindow->Blt (
  193. Private->EmuGraphicsWindow,
  194. (EFI_UGA_PIXEL *)BltBuffer,
  195. (EFI_UGA_BLT_OPERATION)BltOperation,
  196. &GopBltArgs
  197. );
  198. gBS->RestoreTPL (OriginalTPL);
  199. return Status;
  200. }
  201. //
  202. // Construction and Destruction functions
  203. //
  204. EFI_STATUS
  205. EmuGopSupported (
  206. IN EMU_IO_THUNK_PROTOCOL *EmuIoThunk
  207. )
  208. {
  209. //
  210. // Check to see if the IO abstraction represents a device type we support.
  211. //
  212. // This would be replaced a check of PCI subsystem ID, etc.
  213. //
  214. if (!CompareGuid (EmuIoThunk->Protocol, &gEmuGraphicsWindowProtocolGuid)) {
  215. return EFI_UNSUPPORTED;
  216. }
  217. return EFI_SUCCESS;
  218. }
  219. EFI_STATUS
  220. EmuGopStartWindow (
  221. IN GOP_PRIVATE_DATA *Private,
  222. IN UINT32 HorizontalResolution,
  223. IN UINT32 VerticalResolution,
  224. IN UINT32 ColorDepth,
  225. IN UINT32 RefreshRate
  226. )
  227. {
  228. EFI_STATUS Status;
  229. //
  230. // Register to be notified on exit boot services so we can destroy the window.
  231. //
  232. Status = gBS->CreateEvent (
  233. EVT_SIGNAL_EXIT_BOOT_SERVICES,
  234. TPL_CALLBACK,
  235. ShutdownGopEvent,
  236. Private,
  237. &mGopScreenExitBootServicesEvent
  238. );
  239. Status = Private->EmuIoThunk->Open (Private->EmuIoThunk);
  240. if (!EFI_ERROR (Status)) {
  241. Private->EmuGraphicsWindow = Private->EmuIoThunk->Interface;
  242. // Register callback to support RegisterKeyNotify()
  243. Status = Private->EmuGraphicsWindow->RegisterKeyNotify (
  244. Private->EmuGraphicsWindow,
  245. GopPrivateMakeCallbackFunction,
  246. GopPrivateBreakCallbackFunction,
  247. Private
  248. );
  249. ASSERT_EFI_ERROR (Status);
  250. }
  251. return Status;
  252. }
  253. EFI_STATUS
  254. EmuGopConstructor (
  255. GOP_PRIVATE_DATA *Private
  256. )
  257. {
  258. Private->ModeData = mGopModeData;
  259. Private->GraphicsOutput.QueryMode = EmuGopQuerytMode;
  260. Private->GraphicsOutput.SetMode = EmuGopSetMode;
  261. Private->GraphicsOutput.Blt = EmuGopBlt;
  262. //
  263. // Allocate buffer for Graphics Output Protocol mode information
  264. //
  265. Private->GraphicsOutput.Mode = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE));
  266. if (Private->GraphicsOutput.Mode == NULL) {
  267. return EFI_OUT_OF_RESOURCES;
  268. }
  269. Private->GraphicsOutput.Mode->Info = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION));
  270. if (Private->GraphicsOutput.Mode->Info == NULL) {
  271. return EFI_OUT_OF_RESOURCES;
  272. }
  273. Private->GraphicsOutput.Mode->MaxMode = sizeof (mGopModeData) / sizeof (GOP_MODE_DATA);
  274. //
  275. // Till now, we have no idea about the window size.
  276. //
  277. Private->GraphicsOutput.Mode->Mode = GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER;
  278. Private->GraphicsOutput.Mode->Info->Version = 0;
  279. Private->GraphicsOutput.Mode->Info->HorizontalResolution = 0;
  280. Private->GraphicsOutput.Mode->Info->VerticalResolution = 0;
  281. Private->GraphicsOutput.Mode->Info->PixelFormat = PixelBltOnly;
  282. Private->GraphicsOutput.Mode->SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
  283. Private->GraphicsOutput.Mode->FrameBufferBase = (EFI_PHYSICAL_ADDRESS)(UINTN)NULL;
  284. Private->GraphicsOutput.Mode->FrameBufferSize = 0;
  285. Private->HardwareNeedsStarting = TRUE;
  286. Private->EmuGraphicsWindow = NULL;
  287. EmuGopInitializeSimpleTextInForWindow (Private);
  288. EmuGopInitializeSimplePointerForWindow (Private);
  289. return EFI_SUCCESS;
  290. }
  291. EFI_STATUS
  292. EmuGopDestructor (
  293. GOP_PRIVATE_DATA *Private
  294. )
  295. {
  296. if (!Private->HardwareNeedsStarting) {
  297. Private->EmuIoThunk->Close (Private->EmuIoThunk);
  298. Private->EmuGraphicsWindow = NULL;
  299. }
  300. //
  301. // Free graphics output protocol occupied resource
  302. //
  303. if (Private->GraphicsOutput.Mode != NULL) {
  304. if (Private->GraphicsOutput.Mode->Info != NULL) {
  305. FreePool (Private->GraphicsOutput.Mode->Info);
  306. }
  307. FreePool (Private->GraphicsOutput.Mode);
  308. }
  309. return EFI_SUCCESS;
  310. }
  311. VOID
  312. EFIAPI
  313. ShutdownGopEvent (
  314. IN EFI_EVENT Event,
  315. IN VOID *Context
  316. )
  317. /*++
  318. Routine Description:
  319. This is the UGA screen's callback notification function for exit-boot-services.
  320. All we do here is call EmuGopDestructor().
  321. Arguments:
  322. Event - not used
  323. Context - pointer to the Private structure.
  324. Returns:
  325. None.
  326. **/
  327. {
  328. EmuGopDestructor (Context);
  329. }