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 "XlibSurfaceKHR.hpp"
16
17 #include "Vulkan/VkDeviceMemory.hpp"
18 #include "Vulkan/VkImage.hpp"
19
20 namespace vk {
21
isSupported()22 bool XlibSurfaceKHR::isSupported()
23 {
24 return libX11.isPresent();
25 }
26
XlibSurfaceKHR(const VkXlibSurfaceCreateInfoKHR *pCreateInfo, void *mem)27 XlibSurfaceKHR::XlibSurfaceKHR(const VkXlibSurfaceCreateInfoKHR *pCreateInfo, void *mem)
28 : pDisplay(pCreateInfo->dpy)
29 , window(pCreateInfo->window)
30 {
31 ASSERT(isSupported());
32
33 int screen = DefaultScreen(pDisplay);
34 gc = libX11->XDefaultGC(pDisplay, screen);
35
36 XVisualInfo xVisual;
37 Status status = libX11->XMatchVisualInfo(pDisplay, screen, 32, TrueColor, &xVisual);
38 bool match = (status != 0 && xVisual.blue_mask == 0xFF);
39 visual = match ? xVisual.visual : libX11->XDefaultVisual(pDisplay, screen);
40 }
41
destroySurface(const VkAllocationCallbacks *pAllocator)42 void XlibSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
43 {
44 }
45
ComputeRequiredAllocationSize(const VkXlibSurfaceCreateInfoKHR *pCreateInfo)46 size_t XlibSurfaceKHR::ComputeRequiredAllocationSize(const VkXlibSurfaceCreateInfoKHR *pCreateInfo)
47 {
48 return 0;
49 }
50
getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const51 VkResult XlibSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
52 {
53 setCommonSurfaceCapabilities(pSurfaceCapabilities);
54
55 XWindowAttributes attr;
56 libX11->XGetWindowAttributes(pDisplay, window, &attr);
57 VkExtent2D extent = { static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height) };
58
59 pSurfaceCapabilities->currentExtent = extent;
60 pSurfaceCapabilities->minImageExtent = extent;
61 pSurfaceCapabilities->maxImageExtent = extent;
62 return VK_SUCCESS;
63 }
64
attachImage(PresentImage *image)65 void XlibSurfaceKHR::attachImage(PresentImage *image)
66 {
67 XWindowAttributes attr;
68 libX11->XGetWindowAttributes(pDisplay, window, &attr);
69
70 const VkExtent3D &extent = image->getImage()->getExtent();
71
72 int bytes_per_line = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
73 char *buffer = static_cast<char *>(image->getImageMemory()->getOffsetPointer(0));
74
75 XImage *xImage = libX11->XCreateImage(pDisplay, visual, attr.depth, ZPixmap, 0, buffer, extent.width, extent.height, 32, bytes_per_line);
76
77 imageMap[image] = xImage;
78 }
79
detachImage(PresentImage *image)80 void XlibSurfaceKHR::detachImage(PresentImage *image)
81 {
82 auto it = imageMap.find(image);
83 if(it != imageMap.end())
84 {
85 XImage *xImage = it->second;
86 xImage->data = nullptr; // the XImage does not actually own the buffer
87 XDestroyImage(xImage);
88 imageMap.erase(it);
89 }
90 }
91
present(PresentImage *image)92 VkResult XlibSurfaceKHR::present(PresentImage *image)
93 {
94 auto it = imageMap.find(image);
95 if(it != imageMap.end())
96 {
97 XImage *xImage = it->second;
98
99 if(xImage->data)
100 {
101 XWindowAttributes attr;
102 libX11->XGetWindowAttributes(pDisplay, window, &attr);
103 VkExtent2D windowExtent = { static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height) };
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 libX11->XPutImage(pDisplay, window, gc, xImage, 0, 0, 0, 0, extent.width, extent.height);
112 }
113 }
114
115 return VK_SUCCESS;
116 }
117
118 } // namespace vk