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