blob: c4c156bce65da5e95024f9dee6b5a3b68312e650 [file] [log] [blame]
Nicolas Capensd3545372019-08-09 13:59:18 -04001// 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 Clayton25e06e02020-02-07 11:19:08 +000017#include "System/Debug.hpp"
Ben Clayton45c697a2019-12-17 20:38:03 +000018#include "Vulkan/VkDeviceMemory.hpp"
Nicolas Capensd3545372019-08-09 13:59:18 -040019
Nicolas Capensd3545372019-08-09 13:59:18 -040020#include <string.h>
21
Ben Clayton45c697a2019-12-17 20:38:03 +000022namespace {
23VkExtent2D getWindowSize(HWND hwnd)
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040024{
Ben Clayton45c697a2019-12-17 20:38:03 +000025 RECT clientRect = {};
26 BOOL status = GetClientRect(hwnd, &clientRect);
27 ASSERT(status != 0);
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040028
Ben Clayton45c697a2019-12-17 20:38:03 +000029 int windowWidth = clientRect.right - clientRect.left;
30 int windowHeight = clientRect.bottom - clientRect.top;
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040031
Ben Clayton45c697a2019-12-17 20:38:03 +000032 return { static_cast<uint32_t>(windowWidth), static_cast<uint32_t>(windowHeight) };
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040033}
Ben Clayton45c697a2019-12-17 20:38:03 +000034} // namespace
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040035
Nicolas Capensd3545372019-08-09 13:59:18 -040036namespace vk {
37
Ben Clayton45c697a2019-12-17 20:38:03 +000038Win32SurfaceKHR::Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR *pCreateInfo, void *mem)
39 : hwnd(pCreateInfo->hwnd)
Nicolas Capensd3545372019-08-09 13:59:18 -040040{
41 ASSERT(IsWindow(hwnd) == TRUE);
Nicolas Capensd3545372019-08-09 13:59:18 -040042 windowContext = GetDC(hwnd);
43 bitmapContext = CreateCompatibleDC(windowContext);
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040044 lazyCreateFrameBuffer();
Nicolas Capensd3545372019-08-09 13:59:18 -040045}
46
47void Win32SurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
48{
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040049 destroyFrameBuffer();
Nicolas Capensd3545372019-08-09 13:59:18 -040050 ReleaseDC(hwnd, windowContext);
51 DeleteDC(bitmapContext);
52}
53
54size_t Win32SurfaceKHR::ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR *pCreateInfo)
55{
56 return 0;
57}
58
59void Win32SurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
60{
61 SurfaceKHR::getSurfaceCapabilities(pSurfaceCapabilities);
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040062 VkExtent2D extent = getWindowSize(hwnd);
Nicolas Capensd3545372019-08-09 13:59:18 -040063 pSurfaceCapabilities->currentExtent = extent;
64 pSurfaceCapabilities->minImageExtent = extent;
65 pSurfaceCapabilities->maxImageExtent = extent;
66}
67
Ben Clayton45c697a2019-12-17 20:38:03 +000068void Win32SurfaceKHR::attachImage(PresentImage *image)
Nicolas Capensd3545372019-08-09 13:59:18 -040069{
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 Clayton45c697a2019-12-17 20:38:03 +000074void Win32SurfaceKHR::detachImage(PresentImage *image)
Nicolas Capensd3545372019-08-09 13:59:18 -040075{
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 Clayton45c697a2019-12-17 20:38:03 +000080VkResult Win32SurfaceKHR::present(PresentImage *image)
Nicolas Capensd3545372019-08-09 13:59:18 -040081{
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040082 // Recreate frame buffer in case window size has changed
83 lazyCreateFrameBuffer();
84
Nicolas Capensd3545372019-08-09 13:59:18 -040085 if(!framebuffer)
86 {
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040087 // e.g. window width or height is 0
88 return VK_SUCCESS;
Nicolas Capensd3545372019-08-09 13:59:18 -040089 }
90
91 VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
92
Nicolas Capens81bc9d92019-12-16 15:05:57 -050093 if(windowExtent.width != extent.width || windowExtent.height != extent.height)
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -040094 {
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 Clayton45c697a2019-12-17 20:38:03 +0000102 image->getImage()->blitToBuffer(subresourceLayers, VkOffset3D{}, extent, reinterpret_cast<uint8_t *>(framebuffer), bitmapRowPitch, 0);
Nicolas Capensd3545372019-08-09 13:59:18 -0400103
104 StretchBlt(windowContext, 0, 0, extent.width, extent.height, bitmapContext, 0, 0, extent.width, extent.height, SRCCOPY);
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -0400105
106 return VK_SUCCESS;
107}
108
109void Win32SurfaceKHR::lazyCreateFrameBuffer()
110{
111 auto currWindowExtent = getWindowSize(hwnd);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500112 if(currWindowExtent.width == windowExtent.width && currWindowExtent.height == windowExtent.height)
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -0400113 {
114 return;
115 }
116
117 windowExtent = currWindowExtent;
118
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500119 if(framebuffer)
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -0400120 {
121 destroyFrameBuffer();
122 }
123
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500124 if(windowExtent.width == 0 || windowExtent.height == 0)
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -0400125 {
126 return;
127 }
128
129 BITMAPINFO bitmapInfo = {};
130 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
131 bitmapInfo.bmiHeader.biBitCount = 32;
132 bitmapInfo.bmiHeader.biPlanes = 1;
Ben Clayton45c697a2019-12-17 20:38:03 +0000133 bitmapInfo.bmiHeader.biHeight = -static_cast<LONG>(windowExtent.height); // Negative for top-down DIB, origin in upper-left corner
Antonio Maiorano26c6c4a2019-10-17 15:33:47 -0400134 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
147void Win32SurfaceKHR::destroyFrameBuffer()
148{
149 SelectObject(bitmapContext, NULL);
150 DeleteObject(bitmap);
151 bitmap = {};
152 bitmapRowPitch = 0;
153 framebuffer = nullptr;
Nicolas Capensd3545372019-08-09 13:59:18 -0400154}
155
Ben Clayton45c697a2019-12-17 20:38:03 +0000156} // namespace vk