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
17 #include "System/Debug.hpp"
18 #include "Vulkan/VkDeviceMemory.hpp"
19
20 #include <string.h>
21
22 namespace {
getWindowSize(HWND hwnd)23 VkExtent2D getWindowSize(HWND hwnd)
24 {
25 ASSERT(IsWindow(hwnd) == TRUE);
26
27 RECT clientRect = {};
28 BOOL status = GetClientRect(hwnd, &clientRect);
29 ASSERT(status != 0);
30
31 int windowWidth = clientRect.right - clientRect.left;
32 int windowHeight = clientRect.bottom - clientRect.top;
33
34 return { static_cast<uint32_t>(windowWidth), static_cast<uint32_t>(windowHeight) };
35 }
36 } // namespace
37
38 namespace vk {
39
Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR * pCreateInfo,void * mem)40 Win32SurfaceKHR::Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR *pCreateInfo, void *mem)
41 : hwnd(pCreateInfo->hwnd)
42 {
43 ASSERT(IsWindow(hwnd) == TRUE);
44 windowContext = GetDC(hwnd);
45 bitmapContext = CreateCompatibleDC(windowContext);
46 lazyCreateFrameBuffer();
47 }
48
destroySurface(const VkAllocationCallbacks * pAllocator)49 void Win32SurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
50 {
51 destroyFrameBuffer();
52 ReleaseDC(hwnd, windowContext);
53 DeleteDC(bitmapContext);
54 }
55
ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR * pCreateInfo)56 size_t Win32SurfaceKHR::ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR *pCreateInfo)
57 {
58 return 0;
59 }
60
getSurfaceCapabilities(VkSurfaceCapabilitiesKHR * pSurfaceCapabilities) const61 VkResult Win32SurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
62 {
63 setCommonSurfaceCapabilities(pSurfaceCapabilities);
64
65 if(!IsWindow(hwnd))
66 {
67 VkExtent2D extent = { 0, 0 };
68 pSurfaceCapabilities->currentExtent = extent;
69 pSurfaceCapabilities->minImageExtent = extent;
70 pSurfaceCapabilities->maxImageExtent = extent;
71 return VK_ERROR_SURFACE_LOST_KHR;
72 }
73
74 VkExtent2D extent = getWindowSize(hwnd);
75 pSurfaceCapabilities->currentExtent = extent;
76 pSurfaceCapabilities->minImageExtent = extent;
77 pSurfaceCapabilities->maxImageExtent = extent;
78 return VK_SUCCESS;
79 }
80
attachImage(PresentImage * image)81 void Win32SurfaceKHR::attachImage(PresentImage *image)
82 {
83 // Nothing to do here, the current implementation based on GDI blits on
84 // present instead of associating the image with the surface.
85 }
86
detachImage(PresentImage * image)87 void Win32SurfaceKHR::detachImage(PresentImage *image)
88 {
89 // Nothing to do here, the current implementation based on GDI blits on
90 // present instead of associating the image with the surface.
91 }
92
present(PresentImage * image)93 VkResult Win32SurfaceKHR::present(PresentImage *image)
94 {
95 // Recreate frame buffer in case window size has changed
96 lazyCreateFrameBuffer();
97
98 if(!framebuffer)
99 {
100 // e.g. window width or height is 0
101 return VK_SUCCESS;
102 }
103
104 const VkExtent3D &extent = image->getImage()->getExtent();
105
106 if(windowExtent.width != extent.width || windowExtent.height != extent.height)
107 {
108 return VK_ERROR_OUT_OF_DATE_KHR;
109 }
110
111 image->getImage()->copyTo(reinterpret_cast<uint8_t *>(framebuffer), bitmapRowPitch);
112
113 StretchBlt(windowContext, 0, 0, extent.width, extent.height, bitmapContext, 0, 0, extent.width, extent.height, SRCCOPY);
114
115 return VK_SUCCESS;
116 }
117
lazyCreateFrameBuffer()118 void Win32SurfaceKHR::lazyCreateFrameBuffer()
119 {
120 auto currWindowExtent = getWindowSize(hwnd);
121 if(currWindowExtent.width == windowExtent.width && currWindowExtent.height == windowExtent.height)
122 {
123 return;
124 }
125
126 windowExtent = currWindowExtent;
127
128 if(framebuffer)
129 {
130 destroyFrameBuffer();
131 }
132
133 if(windowExtent.width == 0 || windowExtent.height == 0)
134 {
135 return;
136 }
137
138 BITMAPINFO bitmapInfo = {};
139 bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
140 bitmapInfo.bmiHeader.biBitCount = 32;
141 bitmapInfo.bmiHeader.biPlanes = 1;
142 bitmapInfo.bmiHeader.biHeight = -static_cast<LONG>(windowExtent.height); // Negative for top-down DIB, origin in upper-left corner
143 bitmapInfo.bmiHeader.biWidth = windowExtent.width;
144 bitmapInfo.bmiHeader.biCompression = BI_RGB;
145
146 bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &framebuffer, 0, 0);
147 ASSERT(bitmap != NULL);
148 SelectObject(bitmapContext, bitmap);
149
150 BITMAP header;
151 int status = GetObject(bitmap, sizeof(BITMAP), &header);
152 ASSERT(status != 0);
153 bitmapRowPitch = static_cast<int>(header.bmWidthBytes);
154 }
155
destroyFrameBuffer()156 void Win32SurfaceKHR::destroyFrameBuffer()
157 {
158 SelectObject(bitmapContext, NULL);
159 DeleteObject(bitmap);
160 bitmap = {};
161 bitmapRowPitch = 0;
162 framebuffer = nullptr;
163 }
164
165 } // namespace vk