• 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)23 VkExtent2D getWindowSize(HWND hwnd)
24 {
25 	RECT clientRect = {};
26 	BOOL status = GetClientRect(hwnd, &clientRect);
27 	ASSERT(status != 0);
28 
29 	int windowWidth = clientRect.right - clientRect.left;
30 	int windowHeight = clientRect.bottom - clientRect.top;
31 
32 	return { static_cast<uint32_t>(windowWidth), static_cast<uint32_t>(windowHeight) };
33 }
34 }  // namespace
35 
36 namespace vk {
37 
Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR * pCreateInfo,void * mem)38 Win32SurfaceKHR::Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR *pCreateInfo, void *mem)
39     : hwnd(pCreateInfo->hwnd)
40 {
41 	ASSERT(IsWindow(hwnd) == TRUE);
42 	windowContext = GetDC(hwnd);
43 	bitmapContext = CreateCompatibleDC(windowContext);
44 	lazyCreateFrameBuffer();
45 }
46 
destroySurface(const VkAllocationCallbacks * pAllocator)47 void Win32SurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
48 {
49 	destroyFrameBuffer();
50 	ReleaseDC(hwnd, windowContext);
51 	DeleteDC(bitmapContext);
52 }
53 
ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR * pCreateInfo)54 size_t Win32SurfaceKHR::ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR *pCreateInfo)
55 {
56 	return 0;
57 }
58 
getSurfaceCapabilities(VkSurfaceCapabilitiesKHR * pSurfaceCapabilities) const59 void Win32SurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
60 {
61 	SurfaceKHR::getSurfaceCapabilities(pSurfaceCapabilities);
62 	VkExtent2D extent = getWindowSize(hwnd);
63 	pSurfaceCapabilities->currentExtent = extent;
64 	pSurfaceCapabilities->minImageExtent = extent;
65 	pSurfaceCapabilities->maxImageExtent = extent;
66 }
67 
attachImage(PresentImage * image)68 void Win32SurfaceKHR::attachImage(PresentImage *image)
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 
detachImage(PresentImage * image)74 void Win32SurfaceKHR::detachImage(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 
present(PresentImage * image)80 VkResult Win32SurfaceKHR::present(PresentImage *image)
81 {
82 	// Recreate frame buffer in case window size has changed
83 	lazyCreateFrameBuffer();
84 
85 	if(!framebuffer)
86 	{
87 		// e.g. window width or height is 0
88 		return VK_SUCCESS;
89 	}
90 
91 	VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
92 
93 	if(windowExtent.width != extent.width || windowExtent.height != extent.height)
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 
102 	image->getImage()->blitToBuffer(subresourceLayers, VkOffset3D{}, extent, reinterpret_cast<uint8_t *>(framebuffer), bitmapRowPitch, 0);
103 
104 	StretchBlt(windowContext, 0, 0, extent.width, extent.height, bitmapContext, 0, 0, extent.width, extent.height, SRCCOPY);
105 
106 	return VK_SUCCESS;
107 }
108 
lazyCreateFrameBuffer()109 void Win32SurfaceKHR::lazyCreateFrameBuffer()
110 {
111 	auto currWindowExtent = getWindowSize(hwnd);
112 	if(currWindowExtent.width == windowExtent.width && currWindowExtent.height == windowExtent.height)
113 	{
114 		return;
115 	}
116 
117 	windowExtent = currWindowExtent;
118 
119 	if(framebuffer)
120 	{
121 		destroyFrameBuffer();
122 	}
123 
124 	if(windowExtent.width == 0 || windowExtent.height == 0)
125 	{
126 		return;
127 	}
128 
129 	BITMAPINFO bitmapInfo = {};
130 	bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
131 	bitmapInfo.bmiHeader.biBitCount = 32;
132 	bitmapInfo.bmiHeader.biPlanes = 1;
133 	bitmapInfo.bmiHeader.biHeight = -static_cast<LONG>(windowExtent.height);  // Negative for top-down DIB, origin in upper-left corner
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 
destroyFrameBuffer()147 void Win32SurfaceKHR::destroyFrameBuffer()
148 {
149 	SelectObject(bitmapContext, NULL);
150 	DeleteObject(bitmap);
151 	bitmap = {};
152 	bitmapRowPitch = 0;
153 	framebuffer = nullptr;
154 }
155 
156 }  // namespace vk