Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 1 | // Copyright 2019 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "Win32SurfaceKHR.hpp" |
| 16 | |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 17 | #include "System/Debug.hpp" |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 18 | #include "Vulkan/VkDeviceMemory.hpp" |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 19 | |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | VkExtent2D getWindowSize(HWND hwnd) |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 24 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 25 | RECT clientRect = {}; |
| 26 | BOOL status = GetClientRect(hwnd, &clientRect); |
| 27 | ASSERT(status != 0); |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 28 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 29 | int windowWidth = clientRect.right - clientRect.left; |
| 30 | int windowHeight = clientRect.bottom - clientRect.top; |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 31 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 32 | return { static_cast<uint32_t>(windowWidth), static_cast<uint32_t>(windowHeight) }; |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 33 | } |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 34 | } // namespace |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 35 | |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 36 | namespace vk { |
| 37 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 38 | Win32SurfaceKHR::Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR *pCreateInfo, void *mem) |
| 39 | : hwnd(pCreateInfo->hwnd) |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 40 | { |
| 41 | ASSERT(IsWindow(hwnd) == TRUE); |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 42 | windowContext = GetDC(hwnd); |
| 43 | bitmapContext = CreateCompatibleDC(windowContext); |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 44 | lazyCreateFrameBuffer(); |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void Win32SurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator) |
| 48 | { |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 49 | destroyFrameBuffer(); |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 50 | ReleaseDC(hwnd, windowContext); |
| 51 | DeleteDC(bitmapContext); |
| 52 | } |
| 53 | |
| 54 | size_t Win32SurfaceKHR::ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR *pCreateInfo) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | void Win32SurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const |
| 60 | { |
| 61 | SurfaceKHR::getSurfaceCapabilities(pSurfaceCapabilities); |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 62 | VkExtent2D extent = getWindowSize(hwnd); |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 63 | pSurfaceCapabilities->currentExtent = extent; |
| 64 | pSurfaceCapabilities->minImageExtent = extent; |
| 65 | pSurfaceCapabilities->maxImageExtent = extent; |
| 66 | } |
| 67 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 68 | void Win32SurfaceKHR::attachImage(PresentImage *image) |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 69 | { |
| 70 | // Nothing to do here, the current implementation based on GDI blits on |
| 71 | // present instead of associating the image with the surface. |
| 72 | } |
| 73 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 74 | void Win32SurfaceKHR::detachImage(PresentImage *image) |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 75 | { |
| 76 | // Nothing to do here, the current implementation based on GDI blits on |
| 77 | // present instead of associating the image with the surface. |
| 78 | } |
| 79 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 80 | VkResult Win32SurfaceKHR::present(PresentImage *image) |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 81 | { |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 82 | // Recreate frame buffer in case window size has changed |
| 83 | lazyCreateFrameBuffer(); |
| 84 | |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 85 | if(!framebuffer) |
| 86 | { |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 87 | // e.g. window width or height is 0 |
| 88 | return VK_SUCCESS; |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0); |
| 92 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 93 | if(windowExtent.width != extent.width || windowExtent.height != extent.height) |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 94 | { |
| 95 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 96 | } |
| 97 | |
| 98 | VkImageSubresourceLayers subresourceLayers{}; |
| 99 | subresourceLayers.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 100 | subresourceLayers.layerCount = 1; |
| 101 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 102 | image->getImage()->blitToBuffer(subresourceLayers, VkOffset3D{}, extent, reinterpret_cast<uint8_t *>(framebuffer), bitmapRowPitch, 0); |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 103 | |
| 104 | StretchBlt(windowContext, 0, 0, extent.width, extent.height, bitmapContext, 0, 0, extent.width, extent.height, SRCCOPY); |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 105 | |
| 106 | return VK_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | void Win32SurfaceKHR::lazyCreateFrameBuffer() |
| 110 | { |
| 111 | auto currWindowExtent = getWindowSize(hwnd); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 112 | if(currWindowExtent.width == windowExtent.width && currWindowExtent.height == windowExtent.height) |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 113 | { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | windowExtent = currWindowExtent; |
| 118 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 119 | if(framebuffer) |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 120 | { |
| 121 | destroyFrameBuffer(); |
| 122 | } |
| 123 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 124 | if(windowExtent.width == 0 || windowExtent.height == 0) |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 125 | { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | BITMAPINFO bitmapInfo = {}; |
| 130 | bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO); |
| 131 | bitmapInfo.bmiHeader.biBitCount = 32; |
| 132 | bitmapInfo.bmiHeader.biPlanes = 1; |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 133 | bitmapInfo.bmiHeader.biHeight = -static_cast<LONG>(windowExtent.height); // Negative for top-down DIB, origin in upper-left corner |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 134 | bitmapInfo.bmiHeader.biWidth = windowExtent.width; |
| 135 | bitmapInfo.bmiHeader.biCompression = BI_RGB; |
| 136 | |
| 137 | bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &framebuffer, 0, 0); |
| 138 | ASSERT(bitmap != NULL); |
| 139 | SelectObject(bitmapContext, bitmap); |
| 140 | |
| 141 | BITMAP header; |
| 142 | int status = GetObject(bitmap, sizeof(BITMAP), &header); |
| 143 | ASSERT(status != 0); |
| 144 | bitmapRowPitch = static_cast<int>(header.bmWidthBytes); |
| 145 | } |
| 146 | |
| 147 | void Win32SurfaceKHR::destroyFrameBuffer() |
| 148 | { |
| 149 | SelectObject(bitmapContext, NULL); |
| 150 | DeleteObject(bitmap); |
| 151 | bitmap = {}; |
| 152 | bitmapRowPitch = 0; |
| 153 | framebuffer = nullptr; |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 156 | } // namespace vk |