+/** @file\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+ WinNtGopScreen.c\r
+\r
+Abstract:\r
+\r
+ This file produces the graphics abstration of GOP. It is called by\r
+ WinNtGopDriver.c file which deals with the EFI 1.1 driver model.\r
+ This file just does graphics.\r
+\r
+\r
+**/\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <Uefi.h>\r
+#include <WinNtDxe.h>\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Guid/EventGroup.h>\r
+#include <Protocol/WinNtIo.h>\r
+#include <Protocol/ComponentName.h>\r
+#include <Protocol/SimpleTextIn.h>\r
+#include <Protocol/DriverBinding.h>\r
+#include <Protocol/GraphicsOutput.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include "WinNtGop.h"\r
+\r
+EFI_WIN_NT_THUNK_PROTOCOL *mWinNt;\r
+DWORD mTlsIndex = TLS_OUT_OF_INDEXES;\r
+DWORD mTlsIndexUseCount = 0; // lets us know when we can free mTlsIndex.\r
+static EFI_EVENT mGopScreenExitBootServicesEvent;\r
+GOP_MODE_DATA mGopModeData[] = {\r
+ {800, 600, 0, 0},\r
+ {640, 480, 0, 0},\r
+ {720, 400, 0, 0},\r
+ {1024, 768, 0, 0},\r
+ {1280, 1024, 0, 0}\r
+ };\r
+\r
+EFI_STATUS\r
+WinNtGopStartWindow (\r
+ IN GOP_PRIVATE_DATA *Private,\r
+ IN UINT32 HorizontalResolution,\r
+ IN UINT32 VerticalResolution,\r
+ IN UINT32 ColorDepth,\r
+ IN UINT32 RefreshRate\r
+ );\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+KillNtGopThread (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ );\r
+\r
+//\r
+// GOP Protocol Member Functions\r
+//\r
+\r
+\r
+/**\r
+ Graphics Output protocol interface to get video mode\r
+\r
+ @param This Protocol instance pointer.\r
+ @param ModeNumber The mode number to return information on.\r
+ @param Info Caller allocated buffer that returns information\r
+ about ModeNumber.\r
+ @param SizeOfInfo A pointer to the size, in bytes, of the Info\r
+ buffer.\r
+\r
+ @retval EFI_SUCCESS Mode information returned.\r
+ @retval EFI_BUFFER_TOO_SMALL The Info buffer was too small.\r
+ @retval EFI_DEVICE_ERROR A hardware error occurred trying to retrieve the\r
+ video mode.\r
+ @retval EFI_NOT_STARTED Video display is not initialized. Call SetMode ()\r
+ @retval EFI_INVALID_PARAMETER One of the input args was NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtGopQuerytMode (\r
+ IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,\r
+ IN UINT32 ModeNumber,\r
+ OUT UINTN *SizeOfInfo,\r
+ OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info\r
+ )\r
+{\r
+ GOP_PRIVATE_DATA *Private;\r
+\r
+ Private = GOP_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ if (Info == NULL || SizeOfInfo == NULL || (UINTN) ModeNumber >= This->Mode->MaxMode) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ *Info = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION));\r
+ if (*Info == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ *SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);\r
+\r
+ (*Info)->Version = 0;\r
+ (*Info)->HorizontalResolution = Private->ModeData[ModeNumber].HorizontalResolution;\r
+ (*Info)->VerticalResolution = Private->ModeData[ModeNumber].VerticalResolution;\r
+ (*Info)->PixelFormat = PixelBlueGreenRedReserved8BitPerColor;\r
+ (*Info)->PixelsPerScanLine = (*Info)->HorizontalResolution;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Graphics Output protocol interface to set video mode\r
+\r
+ @param This Protocol instance pointer.\r
+ @param ModeNumber The mode number to be set.\r
+\r
+ @retval EFI_SUCCESS Graphics mode was changed.\r
+ @retval EFI_DEVICE_ERROR The device had an error and could not complete the\r
+ request.\r
+ @retval EFI_UNSUPPORTED ModeNumber is not supported by this device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtGopSetMode (\r
+ IN EFI_GRAPHICS_OUTPUT_PROTOCOL * This,\r
+ IN UINT32 ModeNumber\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ GOP_PRIVATE_DATA *Private;\r
+ GOP_MODE_DATA *ModeData;\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL Fill;\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL *NewFillLine;\r
+ RECT Rect;\r
+ UINTN Size;\r
+ UINTN Width;\r
+ UINTN Height;\r
+\r
+ Private = GOP_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ if (ModeNumber >= This->Mode->MaxMode) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ if (ModeNumber == This->Mode->Mode) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ ModeData = &Private->ModeData[ModeNumber];\r
+ This->Mode->Mode = ModeNumber;\r
+ Private->GraphicsOutput.Mode->Info->HorizontalResolution = ModeData->HorizontalResolution;\r
+ Private->GraphicsOutput.Mode->Info->VerticalResolution = ModeData->VerticalResolution;\r
+ Private->GraphicsOutput.Mode->Info->PixelsPerScanLine = ModeData->HorizontalResolution;\r
+\r
+ if (Private->HardwareNeedsStarting) {\r
+ Status = WinNtGopStartWindow (\r
+ Private,\r
+ ModeData->HorizontalResolution,\r
+ ModeData->VerticalResolution,\r
+ ModeData->ColorDepth,\r
+ ModeData->RefreshRate\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ Private->HardwareNeedsStarting = FALSE;\r
+ } else {\r
+ //\r
+ // Change the resolution and resize of the window\r
+ //\r
+\r
+ //\r
+ // Free the old buffer. We do not save the content of the old buffer since the\r
+ // screen is to be cleared anyway. Clearing the screen is required by the EFI spec.\r
+ // See UEFI spec -EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode()\r
+ //\r
+ Private->WinNtThunk->HeapFree (Private->WinNtThunk->GetProcessHeap (), 0, Private->VirtualScreenInfo);\r
+\r
+ //\r
+ // Allocate DIB frame buffer directly from NT for performance enhancement\r
+ // This buffer is the virtual screen/frame buffer. This buffer is not the\r
+ // same a a frame buffer. The first row of this buffer will be the bottom\r
+ // line of the image. This is an artifact of the way we draw to the screen.\r
+ //\r
+ Size = ModeData->HorizontalResolution * ModeData->VerticalResolution * sizeof (RGBQUAD) + sizeof (BITMAPV4HEADER);\r
+ Private->VirtualScreenInfo = Private->WinNtThunk->HeapAlloc (\r
+ Private->WinNtThunk->GetProcessHeap (),\r
+ HEAP_ZERO_MEMORY,\r
+ Size\r
+ );\r
+\r
+ //\r
+ // Update the virtual screen info data structure\r
+ //\r
+ Private->VirtualScreenInfo->bV4Size = sizeof (BITMAPV4HEADER);\r
+ Private->VirtualScreenInfo->bV4Width = ModeData->HorizontalResolution;\r
+ Private->VirtualScreenInfo->bV4Height = ModeData->VerticalResolution;\r
+ Private->VirtualScreenInfo->bV4Planes = 1;\r
+ Private->VirtualScreenInfo->bV4BitCount = 32;\r
+ //\r
+ // uncompressed\r
+ //\r
+ Private->VirtualScreenInfo->bV4V4Compression = BI_RGB;\r
+\r
+ //\r
+ // The rest of the allocated memory block is the virtual screen buffer\r
+ //\r
+ Private->VirtualScreen = (RGBQUAD *) (Private->VirtualScreenInfo + 1);\r
+\r
+ //\r
+ // Use the AdjuctWindowRect fuction to calculate the real width and height\r
+ // of the new window including the border and caption\r
+ //\r
+ Rect.left = 0;\r
+ Rect.top = 0;\r
+ Rect.right = ModeData->HorizontalResolution;\r
+ Rect.bottom = ModeData->VerticalResolution;\r
+\r
+ Private->WinNtThunk->AdjustWindowRect (&Rect, WS_OVERLAPPEDWINDOW, 0);\r
+\r
+ Width = Rect.right - Rect.left;\r
+ Height = Rect.bottom - Rect.top;\r
+\r
+ //\r
+ // Retrieve the original window position information\r
+ //\r
+ Private->WinNtThunk->GetWindowRect (Private->WindowHandle, &Rect);\r
+\r
+ //\r
+ // Adjust the window size\r
+ //\r
+ Private->WinNtThunk->MoveWindow (Private->WindowHandle, Rect.left, Rect.top, Width, Height, TRUE);\r
+\r
+ }\r
+\r
+ NewFillLine = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * ModeData->HorizontalResolution);\r
+ if (NewFillLine == NULL) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ if (Private->FillLine != NULL) {\r
+ FreePool (Private->FillLine);\r
+ }\r
+\r
+ Private->FillLine = NewFillLine;\r
+\r
+ Fill.Red = 0x00;\r
+ Fill.Green = 0x00;\r
+ Fill.Blue = 0x00;\r
+ This->Blt (\r
+ This,\r
+ &Fill,\r
+ EfiBltVideoFill,\r
+ 0,\r
+ 0,\r
+ 0,\r
+ 0,\r
+ ModeData->HorizontalResolution,\r
+ ModeData->VerticalResolution,\r
+ ModeData->HorizontalResolution * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
+ );\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Blt pixels from the rectangle (Width X Height) formed by the BltBuffer\r
+ onto the graphics screen starting a location (X, Y). (0, 0) is defined as\r
+ the upper left hand side of the screen. (X, Y) can be outside of the\r
+ current screen geometry and the BltBuffer will be cliped when it is\r
+ displayed. X and Y can be negative or positive. If Width or Height is\r
+ bigger than the current video screen the image will be clipped.\r
+\r
+ @param This Protocol instance pointer.\r
+ @param X X location on graphics screen.\r
+ @param Y Y location on the graphics screen.\r
+ @param Width Width of BltBuffer.\r
+ @param Height Hight of BltBuffer\r
+ @param BltOperation Operation to perform on BltBuffer and video memory\r
+ @param BltBuffer Buffer containing data to blt into video buffer.\r
+ This buffer has a size of\r
+ Width*Height*sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL)\r
+ @param SourceX If the BltOperation is a EfiCopyBlt this is the\r
+ source of the copy. For other BLT operations this\r
+ argument is not used.\r
+ @param SourceX If the BltOperation is a EfiCopyBlt this is the\r
+ source of the copy. For other BLT operations this\r
+ argument is not used.\r
+\r
+ @retval EFI_SUCCESS The palette is updated with PaletteArray.\r
+ @retval EFI_INVALID_PARAMETER BltOperation is not valid.\r
+ @retval EFI_DEVICE_ERROR A hardware error occured writting to the video\r
+ buffer.\r
+\r
+**/\r
+// TODO: SourceY - add argument and description to function comment\r
+// TODO: DestinationX - add argument and description to function comment\r
+// TODO: DestinationY - add argument and description to function comment\r
+// TODO: Delta - add argument and description to function comment\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtGopBlt (\r
+ IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,\r
+ IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL\r
+ IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,\r
+ IN UINTN SourceX,\r
+ IN UINTN SourceY,\r
+ IN UINTN DestinationX,\r
+ IN UINTN DestinationY,\r
+ IN UINTN Width,\r
+ IN UINTN Height,\r
+ IN UINTN Delta OPTIONAL\r
+ )\r
+{\r
+ GOP_PRIVATE_DATA *Private;\r
+ EFI_TPL OriginalTPL;\r
+ UINTN DstY;\r
+ UINTN SrcY;\r
+ RGBQUAD *VScreen;\r
+ RGBQUAD *VScreenSrc;\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL *Blt;\r
+ UINTN Index;\r
+ RECT Rect;\r
+ EFI_GRAPHICS_OUTPUT_BLT_PIXEL *FillPixel;\r
+ UINT32 VerticalResolution;\r
+ UINT32 HorizontalResolution;\r
+\r
+ Private = GOP_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ if ((BltOperation < 0) || (BltOperation >= EfiGraphicsOutputBltOperationMax)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (Width == 0 || Height == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ //\r
+ // If Delta is zero, then the entire BltBuffer is being used, so Delta\r
+ // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,\r
+ // the number of bytes in each row can be computed.\r
+ //\r
+ if (Delta == 0) {\r
+ Delta = Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL);\r
+ }\r
+\r
+ //\r
+ // We need to fill the Virtual Screen buffer with the blt data.\r
+ // The virtual screen is upside down, as the first row is the bootom row of\r
+ // the image.\r
+ //\r
+ VerticalResolution = This->Mode->Info->VerticalResolution;\r
+ HorizontalResolution = This->Mode->Info->HorizontalResolution;\r
+ if (BltOperation == EfiBltVideoToBltBuffer) {\r
+\r
+ //\r
+ // Video to BltBuffer: Source is Video, destination is BltBuffer\r
+ //\r
+ if (SourceY + Height > VerticalResolution) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (SourceX + Width > HorizontalResolution) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ //\r
+ // We have to raise to TPL Notify, so we make an atomic write the frame buffer.\r
+ // We would not want a timer based event (Cursor, ...) to come in while we are\r
+ // doing this operation.\r
+ //\r
+ OriginalTPL = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+ for (SrcY = SourceY, DstY = DestinationY; DstY < (Height + DestinationY); SrcY++, DstY++) {\r
+ Blt = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) ((UINT8 *) BltBuffer + (DstY * Delta) + DestinationX * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+ VScreen = &Private->VirtualScreen[(VerticalResolution - SrcY - 1) * HorizontalResolution + SourceX];\r
+ CopyMem (Blt, VScreen, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL) * Width);\r
+ }\r
+ } else {\r
+ //\r
+ // BltBuffer to Video: Source is BltBuffer, destination is Video\r
+ //\r
+ if (DestinationY + Height > VerticalResolution) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (DestinationX + Width > HorizontalResolution) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // We have to raise to TPL Notify, so we make an atomic write the frame buffer.\r
+ // We would not want a timer based event (Cursor, ...) to come in while we are\r
+ // doing this operation.\r
+ //\r
+ OriginalTPL = gBS->RaiseTPL (TPL_NOTIFY);\r
+\r
+ if (BltOperation == EfiBltVideoFill) {\r
+ FillPixel = BltBuffer;\r
+ for (Index = 0; Index < Width; Index++) {\r
+ Private->FillLine[Index] = *FillPixel;\r
+ }\r
+ }\r
+\r
+ for (Index = 0; Index < Height; Index++) {\r
+ if (DestinationY <= SourceY) {\r
+ SrcY = SourceY + Index;\r
+ DstY = DestinationY + Index;\r
+ } else {\r
+ SrcY = SourceY + Height - Index - 1;\r
+ DstY = DestinationY + Height - Index - 1;\r
+ }\r
+\r
+ VScreen = &Private->VirtualScreen[(VerticalResolution - DstY - 1) * HorizontalResolution + DestinationX];\r
+ switch (BltOperation) {\r
+ case EfiBltBufferToVideo:\r
+ Blt = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *) ((UINT8 *) BltBuffer + (SrcY * Delta) + SourceX * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+ CopyMem (VScreen, Blt, Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+ break;\r
+\r
+ case EfiBltVideoToVideo:\r
+ VScreenSrc = &Private->VirtualScreen[(VerticalResolution - SrcY - 1) * HorizontalResolution + SourceX];\r
+ CopyMem (VScreen, VScreenSrc, Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+ break;\r
+\r
+ case EfiBltVideoFill:\r
+ CopyMem (VScreen, Private->FillLine, Width * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ if (BltOperation != EfiBltVideoToBltBuffer) {\r
+ //\r
+ // Mark the area we just blted as Invalid so WM_PAINT will update.\r
+ //\r
+ Rect.left = DestinationX;\r
+ Rect.top = DestinationY;\r
+ Rect.right = DestinationX + Width;\r
+ Rect.bottom = DestinationY + Height;\r
+ Private->WinNtThunk->InvalidateRect (Private->WindowHandle, &Rect, FALSE);\r
+\r
+ //\r
+ // Send the WM_PAINT message to the thread that is drawing the window. We\r
+ // are in the main thread and the window drawing is in a child thread.\r
+ // There is a child thread per window. We have no CriticalSection or Mutex\r
+ // since we write the data and the other thread displays the data. While\r
+ // we may miss some data for a short period of time this is no different than\r
+ // a write combining on writes to a frame buffer.\r
+ //\r
+\r
+ Private->WinNtThunk->UpdateWindow (Private->WindowHandle);\r
+ }\r
+\r
+ gBS->RestoreTPL (OriginalTPL);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+//\r
+// Construction and Destruction functions\r
+//\r
+\r
+\r
+/**\r
+\r
+\r
+ @return None\r
+\r
+**/\r
+// TODO: WinNtIo - add argument and description to function comment\r
+// TODO: EFI_UNSUPPORTED - add return value to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+EFI_STATUS\r
+WinNtGopSupported (\r
+ IN EFI_WIN_NT_IO_PROTOCOL *WinNtIo\r
+ )\r
+{\r
+ //\r
+ // Check to see if the IO abstraction represents a device type we support.\r
+ //\r
+ // This would be replaced a check of PCI subsystem ID, etc.\r
+ //\r
+ if (!CompareGuid (WinNtIo->TypeGuid, &gEfiWinNtGopGuid)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Win32 Windows event handler.\r
+\r
+ See Win32 Book\r
+\r
+ @return See Win32 Book\r
+\r
+**/\r
+// TODO: hwnd - add argument and description to function comment\r
+// TODO: iMsg - add argument and description to function comment\r
+// TODO: wParam - add argument and description to function comment\r
+// TODO: lParam - add argument and description to function comment\r
+LRESULT\r
+CALLBACK\r
+WinNtGopThreadWindowProc (\r
+ IN HWND hwnd,\r
+ IN UINT iMsg,\r
+ IN WPARAM wParam,\r
+ IN LPARAM lParam\r
+ )\r
+{\r
+ GOP_PRIVATE_DATA *Private;\r
+ UINTN Size;\r
+ HDC Handle;\r
+ PAINTSTRUCT PaintStruct;\r
+ LPARAM Index;\r
+ EFI_INPUT_KEY Key;\r
+\r
+ //\r
+ // BugBug - if there are two instances of this DLL in memory (such as is\r
+ // the case for ERM), the correct instance of this function may not be called.\r
+ // This also means that the address of the mTlsIndex value will be wrong, and\r
+ // the value may be wrong too.\r
+ //\r
+\r
+\r
+ //\r
+ // Use mTlsIndex global to get a Thread Local Storage version of Private.\r
+ // This works since each Gop protocol has a unique Private data instance and\r
+ // a unique thread.\r
+ //\r
+ Private = mWinNt->TlsGetValue (mTlsIndex);\r
+ ASSERT (NULL != Private);\r
+\r
+ switch (iMsg) {\r
+ case WM_CREATE:\r
+ Size = Private->GraphicsOutput.Mode->Info->HorizontalResolution * Private->GraphicsOutput.Mode->Info->VerticalResolution * sizeof (RGBQUAD);\r
+\r
+ //\r
+ // Allocate DIB frame buffer directly from NT for performance enhancement\r
+ // This buffer is the virtual screen/frame buffer. This buffer is not the\r
+ // same a a frame buffer. The first fow of this buffer will be the bottom\r
+ // line of the image. This is an artifact of the way we draw to the screen.\r
+ //\r
+ Private->VirtualScreenInfo = Private->WinNtThunk->HeapAlloc (\r
+ Private->WinNtThunk->GetProcessHeap (),\r
+ HEAP_ZERO_MEMORY,\r
+ Size\r
+ );\r
+\r
+ Private->VirtualScreenInfo->bV4Size = sizeof (BITMAPV4HEADER);\r
+ Private->VirtualScreenInfo->bV4Width = Private->GraphicsOutput.Mode->Info->HorizontalResolution;\r
+ Private->VirtualScreenInfo->bV4Height = Private->GraphicsOutput.Mode->Info->VerticalResolution;\r
+ Private->VirtualScreenInfo->bV4Planes = 1;\r
+ Private->VirtualScreenInfo->bV4BitCount = 32;\r
+ //\r
+ // uncompressed\r
+ //\r
+ Private->VirtualScreenInfo->bV4V4Compression = BI_RGB;\r
+ Private->VirtualScreen = (RGBQUAD *) (Private->VirtualScreenInfo + 1);\r
+ return 0;\r
+\r
+ case WM_PAINT:\r
+ //\r
+ // I have not found a way to convert hwnd into a Private context. So for\r
+ // now we use this API to convert hwnd to Private data.\r
+ //\r
+\r
+ Handle = mWinNt->BeginPaint (hwnd, &PaintStruct);\r
+\r
+ mWinNt->SetDIBitsToDevice (\r
+ Handle, // Destination Device Context\r
+ 0, // Destination X - 0\r
+ 0, // Destination Y - 0\r
+ Private->GraphicsOutput.Mode->Info->HorizontalResolution, // Width\r
+ Private->GraphicsOutput.Mode->Info->VerticalResolution, // Height\r
+ 0, // Source X\r
+ 0, // Source Y\r
+ 0, // DIB Start Scan Line\r
+ Private->GraphicsOutput.Mode->Info->VerticalResolution, // Number of scan lines\r
+ Private->VirtualScreen, // Address of array of DIB bits\r
+ (BITMAPINFO *) Private->VirtualScreenInfo, // Address of structure with bitmap info\r
+ DIB_RGB_COLORS // RGB or palette indexes\r
+ );\r
+\r
+ mWinNt->EndPaint (hwnd, &PaintStruct);\r
+ return 0;\r
+\r
+ //\r
+ // F10 and the ALT key do not create a WM_KEYDOWN message, thus this special case\r
+ //\r
+ case WM_SYSKEYDOWN:\r
+ Key.ScanCode = 0;\r
+ switch (wParam) {\r
+ case VK_F10:\r
+ Key.ScanCode = SCAN_F10;\r
+ Key.UnicodeChar = 0;\r
+ GopPrivateAddQ (Private, Key);\r
+ return 0;\r
+ }\r
+ break;\r
+\r
+ case WM_KEYDOWN:\r
+ Key.ScanCode = 0;\r
+ switch (wParam) {\r
+ case VK_HOME: Key.ScanCode = SCAN_HOME; break;\r
+ case VK_END: Key.ScanCode = SCAN_END; break;\r
+ case VK_LEFT: Key.ScanCode = SCAN_LEFT; break;\r
+ case VK_RIGHT: Key.ScanCode = SCAN_RIGHT; break;\r
+ case VK_UP: Key.ScanCode = SCAN_UP; break;\r
+ case VK_DOWN: Key.ScanCode = SCAN_DOWN; break;\r
+ case VK_DELETE: Key.ScanCode = SCAN_DELETE; break;\r
+ case VK_INSERT: Key.ScanCode = SCAN_INSERT; break;\r
+ case VK_PRIOR: Key.ScanCode = SCAN_PAGE_UP; break;\r
+ case VK_NEXT: Key.ScanCode = SCAN_PAGE_DOWN; break;\r
+ case VK_ESCAPE: Key.ScanCode = SCAN_ESC; break;\r
+\r
+ case VK_F1: Key.ScanCode = SCAN_F1; break;\r
+ case VK_F2: Key.ScanCode = SCAN_F2; break;\r
+ case VK_F3: Key.ScanCode = SCAN_F3; break;\r
+ case VK_F4: Key.ScanCode = SCAN_F4; break;\r
+ case VK_F5: Key.ScanCode = SCAN_F5; break;\r
+ case VK_F6: Key.ScanCode = SCAN_F6; break;\r
+ case VK_F7: Key.ScanCode = SCAN_F7; break;\r
+ case VK_F8: Key.ScanCode = SCAN_F8; break;\r
+ case VK_F9: Key.ScanCode = SCAN_F9; break;\r
+ case VK_F11: Key.ScanCode = SCAN_F11; break;\r
+ case VK_F12: Key.ScanCode = SCAN_F12; break;\r
+ }\r
+\r
+ if (Key.ScanCode != 0) {\r
+ Key.UnicodeChar = 0;\r
+ GopPrivateAddQ (Private, Key);\r
+ }\r
+\r
+ return 0;\r
+\r
+ case WM_CHAR:\r
+ //\r
+ // The ESC key also generate WM_CHAR.\r
+ //\r
+ if (wParam == 0x1B) {\r
+ return 0;\r
+ }\r
+\r
+ for (Index = 0; Index < (lParam & 0xffff); Index++) {\r
+ if (wParam != 0) {\r
+ Key.UnicodeChar = (CHAR16) wParam;\r
+ Key.ScanCode = 0;\r
+ GopPrivateAddQ (Private, Key);\r
+ }\r
+ }\r
+\r
+ return 0;\r
+\r
+ case WM_CLOSE:\r
+ //\r
+ // This close message is issued by user, core is not aware of this,\r
+ // so don't release the window display resource, just hide the window.\r
+ //\r
+ Private->WinNtThunk->ShowWindow (Private->WindowHandle, SW_HIDE);\r
+ return 0;\r
+\r
+ case WM_DESTROY:\r
+ mWinNt->DestroyWindow (hwnd);\r
+ mWinNt->PostQuitMessage (0);\r
+\r
+ mWinNt->HeapFree (Private->WinNtThunk->GetProcessHeap (), 0, Private->VirtualScreenInfo);\r
+\r
+ mWinNt->ExitThread (0);\r
+ return 0;\r
+\r
+ default:\r
+ break;\r
+ };\r
+\r
+ return mWinNt->DefWindowProc (hwnd, iMsg, wParam, lParam);\r
+}\r
+\r
+\r
+/**\r
+ This thread simulates the end of WinMain () aplication. Each Winow nededs\r
+ to process it's events. The messages are dispatched to\r
+ WinNtGopThreadWindowProc ().\r
+ Be very careful sine WinNtGopThreadWinMain () and WinNtGopThreadWindowProc ()\r
+ are running in a seperate thread. We have to do this to process the events.\r
+\r
+ @param lpParameter Handle of window to manage.\r
+\r
+ @return if a WM_QUIT message is returned exit.\r
+\r
+**/\r
+DWORD\r
+WINAPI\r
+WinNtGopThreadWinMain (\r
+ LPVOID lpParameter\r
+ )\r
+{\r
+ MSG Message;\r
+ GOP_PRIVATE_DATA *Private;\r
+ ATOM Atom;\r
+ RECT Rect;\r
+\r
+ Private = (GOP_PRIVATE_DATA *) lpParameter;\r
+ ASSERT (NULL != Private);\r
+\r
+ //\r
+ // Since each thread has unique private data, save the private data in Thread\r
+ // Local Storage slot. Then the shared global mTlsIndex can be used to get\r
+ // thread specific context.\r
+ //\r
+ Private->WinNtThunk->TlsSetValue (mTlsIndex, Private);\r
+\r
+ Private->ThreadId = Private->WinNtThunk->GetCurrentThreadId ();\r
+\r
+ Private->WindowsClass.cbSize = sizeof (WNDCLASSEX);\r
+ Private->WindowsClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;\r
+ Private->WindowsClass.lpfnWndProc = WinNtGopThreadWindowProc;\r
+ Private->WindowsClass.cbClsExtra = 0;\r
+ Private->WindowsClass.cbWndExtra = 0;\r
+ Private->WindowsClass.hInstance = NULL;\r
+ Private->WindowsClass.hIcon = Private->WinNtThunk->LoadIcon (NULL, IDI_APPLICATION);\r
+ Private->WindowsClass.hCursor = Private->WinNtThunk->LoadCursor (NULL, IDC_ARROW);\r
+ Private->WindowsClass.hbrBackground = (HBRUSH) COLOR_WINDOW;\r
+ Private->WindowsClass.lpszMenuName = NULL;\r
+ Private->WindowsClass.lpszClassName = WIN_NT_GOP_CLASS_NAME;\r
+ Private->WindowsClass.hIconSm = Private->WinNtThunk->LoadIcon (NULL, IDI_APPLICATION);\r
+\r
+ //\r
+ // This call will fail after the first time, but thats O.K. since we only need\r
+ // WIN_NT_GOP_CLASS_NAME to exist to create the window.\r
+ //\r
+ // Note: Multiple instances of this DLL will use the same instance of this\r
+ // Class, including the callback function, unless the Class is unregistered and\r
+ // successfully registered again.\r
+ //\r
+ Atom = Private->WinNtThunk->RegisterClassEx (&Private->WindowsClass);\r
+\r
+ //\r
+ // Setting Rect values to allow for the AdjustWindowRect to provide\r
+ // us the correct sizes for the client area when doing the CreateWindowEx\r
+ //\r
+ Rect.top = 0;\r
+ Rect.bottom = Private->GraphicsOutput.Mode->Info->VerticalResolution;\r
+ Rect.left = 0;\r
+ Rect.right = Private->GraphicsOutput.Mode->Info->HorizontalResolution;\r
+\r
+ Private->WinNtThunk->AdjustWindowRect (&Rect, WS_OVERLAPPEDWINDOW, 0);\r
+\r
+ Private->WindowHandle = Private->WinNtThunk->CreateWindowEx (\r
+ 0,\r
+ WIN_NT_GOP_CLASS_NAME,\r
+ Private->WindowName,\r
+ WS_OVERLAPPEDWINDOW,\r
+ CW_USEDEFAULT,\r
+ CW_USEDEFAULT,\r
+ Rect.right - Rect.left,\r
+ Rect.bottom - Rect.top,\r
+ NULL,\r
+ NULL,\r
+ NULL,\r
+ &Private\r
+ );\r
+\r
+ //\r
+ // The reset of this thread is the standard winows program. We need a sperate\r
+ // thread since we must process the message loop to make windows act like\r
+ // windows.\r
+ //\r
+\r
+ Private->WinNtThunk->ShowWindow (Private->WindowHandle, SW_SHOW);\r
+ Private->WinNtThunk->UpdateWindow (Private->WindowHandle);\r
+\r
+ //\r
+ // Let the main thread get some work done\r
+ //\r
+ Private->WinNtThunk->ReleaseSemaphore (Private->ThreadInited, 1, NULL);\r
+\r
+ //\r
+ // This is the message loop that all Windows programs need.\r
+ //\r
+ while (Private->WinNtThunk->GetMessage (&Message, Private->WindowHandle, 0, 0)) {\r
+ Private->WinNtThunk->TranslateMessage (&Message);\r
+ Private->WinNtThunk->DispatchMessage (&Message);\r
+ }\r
+\r
+ return Message.wParam;\r
+}\r
+\r
+\r
+/**\r
+ TODO: Add function description\r
+\r
+ @param Private TODO: add argument description\r
+ @param HorizontalResolution TODO: add argument description\r
+ @param VerticalResolution TODO: add argument description\r
+ @param ColorDepth TODO: add argument description\r
+ @param RefreshRate TODO: add argument description\r
+\r
+ @return TODO: add return values\r
+\r
+**/\r
+EFI_STATUS\r
+WinNtGopStartWindow (\r
+ IN GOP_PRIVATE_DATA *Private,\r
+ IN UINT32 HorizontalResolution,\r
+ IN UINT32 VerticalResolution,\r
+ IN UINT32 ColorDepth,\r
+ IN UINT32 RefreshRate\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ DWORD NewThreadId;\r
+\r
+ mWinNt = Private->WinNtThunk;\r
+\r
+ //\r
+ // Initialize a Thread Local Storge variable slot. We use TLS to get the\r
+ // correct Private data instance into the windows thread.\r
+ //\r
+ if (mTlsIndex == TLS_OUT_OF_INDEXES) {\r
+ ASSERT (0 == mTlsIndexUseCount);\r
+ mTlsIndex = Private->WinNtThunk->TlsAlloc ();\r
+ }\r
+\r
+ //\r
+ // always increase the use count!\r
+ //\r
+ mTlsIndexUseCount++;\r
+\r
+ //\r
+ // Register to be notified on exit boot services so we can destroy the window.\r
+ //\r
+ Status = gBS->CreateEvent (\r
+ EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
+ TPL_CALLBACK,\r
+ KillNtGopThread,\r
+ Private,\r
+ &mGopScreenExitBootServicesEvent\r
+ );\r
+\r
+ Private->ThreadInited = Private->WinNtThunk->CreateSemaphore (NULL, 0, 1, NULL);\r
+ Private->ThreadHandle = Private->WinNtThunk->CreateThread (\r
+ NULL,\r
+ 0,\r
+ WinNtGopThreadWinMain,\r
+ (VOID *) Private,\r
+ 0,\r
+ &NewThreadId\r
+ );\r
+\r
+ //\r
+ // The other thread has entered the windows message loop so we can\r
+ // continue our initialization.\r
+ //\r
+ Private->WinNtThunk->WaitForSingleObject (Private->ThreadInited, INFINITE);\r
+ Private->WinNtThunk->CloseHandle (Private->ThreadInited);\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+\r
+\r
+ @return None\r
+\r
+**/\r
+// TODO: Private - add argument and description to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+EFI_STATUS\r
+WinNtGopConstructor (\r
+ GOP_PRIVATE_DATA *Private\r
+ )\r
+{\r
+ Private->ModeData = mGopModeData;\r
+\r
+ Private->GraphicsOutput.QueryMode = WinNtGopQuerytMode;\r
+ Private->GraphicsOutput.SetMode = WinNtGopSetMode;\r
+ Private->GraphicsOutput.Blt = WinNtGopBlt;\r
+\r
+ //\r
+ // Allocate buffer for Graphics Output Protocol mode information\r
+ //\r
+ Private->GraphicsOutput.Mode = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE));\r
+ if (Private->GraphicsOutput.Mode == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ Private->GraphicsOutput.Mode->Info = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION));\r
+ if (Private->GraphicsOutput.Mode->Info == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Private->GraphicsOutput.Mode->MaxMode = sizeof(mGopModeData) / sizeof(GOP_MODE_DATA);\r
+ //\r
+ // Till now, we have no idea about the window size.\r
+ //\r
+ Private->GraphicsOutput.Mode->Mode = GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER;\r
+ Private->GraphicsOutput.Mode->Info->Version = 0;\r
+ Private->GraphicsOutput.Mode->Info->HorizontalResolution = 0;\r
+ Private->GraphicsOutput.Mode->Info->VerticalResolution = 0;\r
+ Private->GraphicsOutput.Mode->Info->PixelFormat = PixelBltOnly;\r
+ Private->GraphicsOutput.Mode->SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);\r
+ Private->GraphicsOutput.Mode->FrameBufferBase = (EFI_PHYSICAL_ADDRESS) NULL;\r
+ Private->GraphicsOutput.Mode->FrameBufferSize = 0;\r
+\r
+ Private->HardwareNeedsStarting = TRUE;\r
+ Private->FillLine = NULL;\r
+\r
+ WinNtGopInitializeSimpleTextInForWindow (Private);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+\r
+\r
+ @return None\r
+\r
+**/\r
+// TODO: Private - add argument and description to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+EFI_STATUS\r
+WinNtGopDestructor (\r
+ GOP_PRIVATE_DATA *Private\r
+ )\r
+{\r
+ UINT32 UnregisterReturn;\r
+\r
+ if (!Private->HardwareNeedsStarting) {\r
+ //\r
+ // BugBug: Shutdown GOP Hardware and any child devices.\r
+ //\r
+ Private->WinNtThunk->SendMessage (Private->WindowHandle, WM_DESTROY, 0, 0);\r
+ Private->WinNtThunk->CloseHandle (Private->ThreadHandle);\r
+\r
+ mTlsIndexUseCount--;\r
+\r
+ //\r
+ // The callback function for another window could still be called,\r
+ // so we need to make sure there are no more users of mTlsIndex.\r
+ //\r
+ if (0 == mTlsIndexUseCount) {\r
+ ASSERT (TLS_OUT_OF_INDEXES != mTlsIndex);\r
+\r
+ Private->WinNtThunk->TlsFree (mTlsIndex);\r
+ mTlsIndex = TLS_OUT_OF_INDEXES;\r
+\r
+ UnregisterReturn = Private->WinNtThunk->UnregisterClass (\r
+ Private->WindowsClass.lpszClassName,\r
+ Private->WindowsClass.hInstance\r
+ );\r
+ }\r
+\r
+ WinNtGopDestroySimpleTextInForWindow (Private);\r
+ }\r
+\r
+ //\r
+ // Free graphics output protocol occupied resource\r
+ //\r
+ if (Private->GraphicsOutput.Mode != NULL) {\r
+ if (Private->GraphicsOutput.Mode->Info != NULL) {\r
+ FreePool (Private->GraphicsOutput.Mode->Info);\r
+ }\r
+ FreePool (Private->GraphicsOutput.Mode);\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ This is the GOP screen's callback notification function for exit-boot-services.\r
+ All we do here is call WinNtGopDestructor().\r
+\r
+ @param Event not used\r
+ @param Context pointer to the Private structure.\r
+\r
+ @return None.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+KillNtGopThread (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ Status = WinNtGopDestructor (Context);\r
+}\r