xref: /aosp_15_r20/external/swiftshader/src/WSI/Win32SurfaceKHR.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker //    http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker 
15*03ce13f7SAndroid Build Coastguard Worker #include "Win32SurfaceKHR.hpp"
16*03ce13f7SAndroid Build Coastguard Worker 
17*03ce13f7SAndroid Build Coastguard Worker #include "System/Debug.hpp"
18*03ce13f7SAndroid Build Coastguard Worker #include "Vulkan/VkDeviceMemory.hpp"
19*03ce13f7SAndroid Build Coastguard Worker 
20*03ce13f7SAndroid Build Coastguard Worker #include <string.h>
21*03ce13f7SAndroid Build Coastguard Worker 
22*03ce13f7SAndroid Build Coastguard Worker namespace {
getWindowSize(HWND hwnd,VkExtent2D & windowSize)23*03ce13f7SAndroid Build Coastguard Worker VkResult getWindowSize(HWND hwnd, VkExtent2D &windowSize)
24*03ce13f7SAndroid Build Coastguard Worker {
25*03ce13f7SAndroid Build Coastguard Worker 	RECT clientRect = {};
26*03ce13f7SAndroid Build Coastguard Worker 	if(!IsWindow(hwnd) || !GetClientRect(hwnd, &clientRect))
27*03ce13f7SAndroid Build Coastguard Worker 	{
28*03ce13f7SAndroid Build Coastguard Worker 		windowSize = { 0, 0 };
29*03ce13f7SAndroid Build Coastguard Worker 		return VK_ERROR_SURFACE_LOST_KHR;
30*03ce13f7SAndroid Build Coastguard Worker 	}
31*03ce13f7SAndroid Build Coastguard Worker 
32*03ce13f7SAndroid Build Coastguard Worker 	windowSize = { static_cast<uint32_t>(clientRect.right - clientRect.left),
33*03ce13f7SAndroid Build Coastguard Worker 		           static_cast<uint32_t>(clientRect.bottom - clientRect.top) };
34*03ce13f7SAndroid Build Coastguard Worker 
35*03ce13f7SAndroid Build Coastguard Worker 	return VK_SUCCESS;
36*03ce13f7SAndroid Build Coastguard Worker }
37*03ce13f7SAndroid Build Coastguard Worker }  // namespace
38*03ce13f7SAndroid Build Coastguard Worker 
39*03ce13f7SAndroid Build Coastguard Worker namespace vk {
40*03ce13f7SAndroid Build Coastguard Worker 
Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR * pCreateInfo,void * mem)41*03ce13f7SAndroid Build Coastguard Worker Win32SurfaceKHR::Win32SurfaceKHR(const VkWin32SurfaceCreateInfoKHR *pCreateInfo, void *mem)
42*03ce13f7SAndroid Build Coastguard Worker     : hwnd(pCreateInfo->hwnd)
43*03ce13f7SAndroid Build Coastguard Worker {
44*03ce13f7SAndroid Build Coastguard Worker 	ASSERT(IsWindow(hwnd) == TRUE);
45*03ce13f7SAndroid Build Coastguard Worker 	windowContext = GetDC(hwnd);
46*03ce13f7SAndroid Build Coastguard Worker }
47*03ce13f7SAndroid Build Coastguard Worker 
destroySurface(const VkAllocationCallbacks * pAllocator)48*03ce13f7SAndroid Build Coastguard Worker void Win32SurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
49*03ce13f7SAndroid Build Coastguard Worker {
50*03ce13f7SAndroid Build Coastguard Worker 	ReleaseDC(hwnd, windowContext);
51*03ce13f7SAndroid Build Coastguard Worker }
52*03ce13f7SAndroid Build Coastguard Worker 
ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR * pCreateInfo)53*03ce13f7SAndroid Build Coastguard Worker size_t Win32SurfaceKHR::ComputeRequiredAllocationSize(const VkWin32SurfaceCreateInfoKHR *pCreateInfo)
54*03ce13f7SAndroid Build Coastguard Worker {
55*03ce13f7SAndroid Build Coastguard Worker 	return 0;
56*03ce13f7SAndroid Build Coastguard Worker }
57*03ce13f7SAndroid Build Coastguard Worker 
getSurfaceCapabilities(const void * pSurfaceInfoPNext,VkSurfaceCapabilitiesKHR * pSurfaceCapabilities,void * pSurfaceCapabilitiesPNext) const58*03ce13f7SAndroid Build Coastguard Worker VkResult Win32SurfaceKHR::getSurfaceCapabilities(const void *pSurfaceInfoPNext, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities, void *pSurfaceCapabilitiesPNext) const
59*03ce13f7SAndroid Build Coastguard Worker {
60*03ce13f7SAndroid Build Coastguard Worker 	VkExtent2D extent;
61*03ce13f7SAndroid Build Coastguard Worker 	VkResult result = getWindowSize(hwnd, extent);
62*03ce13f7SAndroid Build Coastguard Worker 	pSurfaceCapabilities->currentExtent = extent;
63*03ce13f7SAndroid Build Coastguard Worker 	pSurfaceCapabilities->minImageExtent = extent;
64*03ce13f7SAndroid Build Coastguard Worker 	pSurfaceCapabilities->maxImageExtent = extent;
65*03ce13f7SAndroid Build Coastguard Worker 
66*03ce13f7SAndroid Build Coastguard Worker 	setCommonSurfaceCapabilities(pSurfaceInfoPNext, pSurfaceCapabilities, pSurfaceCapabilitiesPNext);
67*03ce13f7SAndroid Build Coastguard Worker 	return VK_SUCCESS;
68*03ce13f7SAndroid Build Coastguard Worker }
69*03ce13f7SAndroid Build Coastguard Worker 
attachImage(PresentImage * image)70*03ce13f7SAndroid Build Coastguard Worker void Win32SurfaceKHR::attachImage(PresentImage *image)
71*03ce13f7SAndroid Build Coastguard Worker {
72*03ce13f7SAndroid Build Coastguard Worker 	// Nothing to do here, the current implementation based on GDI blits on
73*03ce13f7SAndroid Build Coastguard Worker 	// present instead of associating the image with the surface.
74*03ce13f7SAndroid Build Coastguard Worker }
75*03ce13f7SAndroid Build Coastguard Worker 
detachImage(PresentImage * image)76*03ce13f7SAndroid Build Coastguard Worker void Win32SurfaceKHR::detachImage(PresentImage *image)
77*03ce13f7SAndroid Build Coastguard Worker {
78*03ce13f7SAndroid Build Coastguard Worker 	// Nothing to do here, the current implementation based on GDI blits on
79*03ce13f7SAndroid Build Coastguard Worker 	// present instead of associating the image with the surface.
80*03ce13f7SAndroid Build Coastguard Worker }
81*03ce13f7SAndroid Build Coastguard Worker 
present(PresentImage * image)82*03ce13f7SAndroid Build Coastguard Worker VkResult Win32SurfaceKHR::present(PresentImage *image)
83*03ce13f7SAndroid Build Coastguard Worker {
84*03ce13f7SAndroid Build Coastguard Worker 	VkExtent2D windowExtent = {};
85*03ce13f7SAndroid Build Coastguard Worker 	VkResult result = getWindowSize(hwnd, windowExtent);
86*03ce13f7SAndroid Build Coastguard Worker 	if(result != VK_SUCCESS)
87*03ce13f7SAndroid Build Coastguard Worker 	{
88*03ce13f7SAndroid Build Coastguard Worker 		return result;
89*03ce13f7SAndroid Build Coastguard Worker 	}
90*03ce13f7SAndroid Build Coastguard Worker 
91*03ce13f7SAndroid Build Coastguard Worker 	const Image *vkImage = image->getImage();
92*03ce13f7SAndroid Build Coastguard Worker 	const VkExtent3D &extent = vkImage->getExtent();
93*03ce13f7SAndroid Build Coastguard Worker 	if(windowExtent.width != extent.width || windowExtent.height != extent.height)
94*03ce13f7SAndroid Build Coastguard Worker 	{
95*03ce13f7SAndroid Build Coastguard Worker 		return VK_ERROR_OUT_OF_DATE_KHR;
96*03ce13f7SAndroid Build Coastguard Worker 	}
97*03ce13f7SAndroid Build Coastguard Worker 
98*03ce13f7SAndroid Build Coastguard Worker 	int stride = vkImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
99*03ce13f7SAndroid Build Coastguard Worker 	int bytesPerPixel = static_cast<int>(vkImage->getFormat(VK_IMAGE_ASPECT_COLOR_BIT).bytes());
100*03ce13f7SAndroid Build Coastguard Worker 
101*03ce13f7SAndroid Build Coastguard Worker 	BITMAPINFO bitmapInfo = {};
102*03ce13f7SAndroid Build Coastguard Worker 	bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
103*03ce13f7SAndroid Build Coastguard Worker 	bitmapInfo.bmiHeader.biBitCount = bytesPerPixel * 8;
104*03ce13f7SAndroid Build Coastguard Worker 	bitmapInfo.bmiHeader.biPlanes = 1;
105*03ce13f7SAndroid Build Coastguard Worker 	bitmapInfo.bmiHeader.biHeight = -static_cast<LONG>(extent.height);  // Negative for top-down DIB, origin in upper-left corner
106*03ce13f7SAndroid Build Coastguard Worker 	bitmapInfo.bmiHeader.biWidth = stride / bytesPerPixel;
107*03ce13f7SAndroid Build Coastguard Worker 	bitmapInfo.bmiHeader.biCompression = BI_RGB;
108*03ce13f7SAndroid Build Coastguard Worker 
109*03ce13f7SAndroid Build Coastguard Worker 	void *bits = image->getImage()->getTexelPointer({ 0, 0, 0 }, { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 });
110*03ce13f7SAndroid Build Coastguard Worker 	StretchDIBits(windowContext, 0, 0, extent.width, extent.height, 0, 0, extent.width, extent.height, bits, &bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
111*03ce13f7SAndroid Build Coastguard Worker 
112*03ce13f7SAndroid Build Coastguard Worker 	return VK_SUCCESS;
113*03ce13f7SAndroid Build Coastguard Worker }
114*03ce13f7SAndroid Build Coastguard Worker 
115*03ce13f7SAndroid Build Coastguard Worker }  // namespace vk
116