1cb93a386Sopenharmony_ci// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2cb93a386Sopenharmony_ci//
3cb93a386Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4cb93a386Sopenharmony_ci// you may not use this file except in compliance with the License.
5cb93a386Sopenharmony_ci// You may obtain a copy of the License at
6cb93a386Sopenharmony_ci//
7cb93a386Sopenharmony_ci//    http://www.apache.org/licenses/LICENSE-2.0
8cb93a386Sopenharmony_ci//
9cb93a386Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10cb93a386Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11cb93a386Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb93a386Sopenharmony_ci// See the License for the specific language governing permissions and
13cb93a386Sopenharmony_ci// limitations under the License.
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci#include "XlibSurfaceKHR.hpp"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci#include "Vulkan/VkDeviceMemory.hpp"
18cb93a386Sopenharmony_ci#include "Vulkan/VkImage.hpp"
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_cinamespace vk {
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_cibool XlibSurfaceKHR::isSupported()
23cb93a386Sopenharmony_ci{
24cb93a386Sopenharmony_ci	return libX11.isPresent();
25cb93a386Sopenharmony_ci}
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_ciXlibSurfaceKHR::XlibSurfaceKHR(const VkXlibSurfaceCreateInfoKHR *pCreateInfo, void *mem)
28cb93a386Sopenharmony_ci    : pDisplay(pCreateInfo->dpy)
29cb93a386Sopenharmony_ci    , window(pCreateInfo->window)
30cb93a386Sopenharmony_ci{
31cb93a386Sopenharmony_ci	ASSERT(isSupported());
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci	int screen = DefaultScreen(pDisplay);
34cb93a386Sopenharmony_ci	gc = libX11->XDefaultGC(pDisplay, screen);
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci	XVisualInfo xVisual;
37cb93a386Sopenharmony_ci	Status status = libX11->XMatchVisualInfo(pDisplay, screen, 32, TrueColor, &xVisual);
38cb93a386Sopenharmony_ci	bool match = (status != 0 && xVisual.blue_mask == 0xFF);
39cb93a386Sopenharmony_ci	visual = match ? xVisual.visual : libX11->XDefaultVisual(pDisplay, screen);
40cb93a386Sopenharmony_ci}
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_civoid XlibSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
43cb93a386Sopenharmony_ci{
44cb93a386Sopenharmony_ci}
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_cisize_t XlibSurfaceKHR::ComputeRequiredAllocationSize(const VkXlibSurfaceCreateInfoKHR *pCreateInfo)
47cb93a386Sopenharmony_ci{
48cb93a386Sopenharmony_ci	return 0;
49cb93a386Sopenharmony_ci}
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ciVkResult XlibSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
52cb93a386Sopenharmony_ci{
53cb93a386Sopenharmony_ci	setCommonSurfaceCapabilities(pSurfaceCapabilities);
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci	XWindowAttributes attr;
56cb93a386Sopenharmony_ci	libX11->XGetWindowAttributes(pDisplay, window, &attr);
57cb93a386Sopenharmony_ci	VkExtent2D extent = { static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height) };
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ci	pSurfaceCapabilities->currentExtent = extent;
60cb93a386Sopenharmony_ci	pSurfaceCapabilities->minImageExtent = extent;
61cb93a386Sopenharmony_ci	pSurfaceCapabilities->maxImageExtent = extent;
62cb93a386Sopenharmony_ci	return VK_SUCCESS;
63cb93a386Sopenharmony_ci}
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_civoid XlibSurfaceKHR::attachImage(PresentImage *image)
66cb93a386Sopenharmony_ci{
67cb93a386Sopenharmony_ci	XWindowAttributes attr;
68cb93a386Sopenharmony_ci	libX11->XGetWindowAttributes(pDisplay, window, &attr);
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ci	const VkExtent3D &extent = image->getImage()->getExtent();
71cb93a386Sopenharmony_ci
72cb93a386Sopenharmony_ci	int bytes_per_line = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
73cb93a386Sopenharmony_ci	char *buffer = static_cast<char *>(image->getImageMemory()->getOffsetPointer(0));
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci	XImage *xImage = libX11->XCreateImage(pDisplay, visual, attr.depth, ZPixmap, 0, buffer, extent.width, extent.height, 32, bytes_per_line);
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci	imageMap[image] = xImage;
78cb93a386Sopenharmony_ci}
79cb93a386Sopenharmony_ci
80cb93a386Sopenharmony_civoid XlibSurfaceKHR::detachImage(PresentImage *image)
81cb93a386Sopenharmony_ci{
82cb93a386Sopenharmony_ci	auto it = imageMap.find(image);
83cb93a386Sopenharmony_ci	if(it != imageMap.end())
84cb93a386Sopenharmony_ci	{
85cb93a386Sopenharmony_ci		XImage *xImage = it->second;
86cb93a386Sopenharmony_ci		xImage->data = nullptr;  // the XImage does not actually own the buffer
87cb93a386Sopenharmony_ci		XDestroyImage(xImage);
88cb93a386Sopenharmony_ci		imageMap.erase(it);
89cb93a386Sopenharmony_ci	}
90cb93a386Sopenharmony_ci}
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_ciVkResult XlibSurfaceKHR::present(PresentImage *image)
93cb93a386Sopenharmony_ci{
94cb93a386Sopenharmony_ci	auto it = imageMap.find(image);
95cb93a386Sopenharmony_ci	if(it != imageMap.end())
96cb93a386Sopenharmony_ci	{
97cb93a386Sopenharmony_ci		XImage *xImage = it->second;
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci		if(xImage->data)
100cb93a386Sopenharmony_ci		{
101cb93a386Sopenharmony_ci			XWindowAttributes attr;
102cb93a386Sopenharmony_ci			libX11->XGetWindowAttributes(pDisplay, window, &attr);
103cb93a386Sopenharmony_ci			VkExtent2D windowExtent = { static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height) };
104cb93a386Sopenharmony_ci			const VkExtent3D &extent = image->getImage()->getExtent();
105cb93a386Sopenharmony_ci
106cb93a386Sopenharmony_ci			if(windowExtent.width != extent.width || windowExtent.height != extent.height)
107cb93a386Sopenharmony_ci			{
108cb93a386Sopenharmony_ci				return VK_ERROR_OUT_OF_DATE_KHR;
109cb93a386Sopenharmony_ci			}
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci			libX11->XPutImage(pDisplay, window, gc, xImage, 0, 0, 0, 0, extent.width, extent.height);
112cb93a386Sopenharmony_ci		}
113cb93a386Sopenharmony_ci	}
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ci	return VK_SUCCESS;
116cb93a386Sopenharmony_ci}
117cb93a386Sopenharmony_ci
118cb93a386Sopenharmony_ci}  // namespace vk