blob: 0ebca020d7e23a7e60785ed333f3778b242432fc [file] [log] [blame]
Nicolas Capensd3545372019-08-09 13:59:18 -04001// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
Hernan Liatisc6eb41b2019-02-22 11:12:59 -08004// 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
Hernan Liatis6b12a502019-03-01 15:06:13 -080017#include "Vulkan/VkDeviceMemory.hpp"
Alexis Hetu63ae9242019-06-06 13:52:15 -040018#include "Vulkan/VkImage.hpp"
Hernan Liatis6b12a502019-03-01 15:06:13 -080019
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080020namespace vk {
21
22XlibSurfaceKHR::XlibSurfaceKHR(const VkXlibSurfaceCreateInfoKHR *pCreateInfo, void *mem) :
Alexis Hetu63ae9242019-06-06 13:52:15 -040023 pDisplay(pCreateInfo->dpy),
24 window(pCreateInfo->window)
Hernan Liatis6b12a502019-03-01 15:06:13 -080025{
26 int screen = DefaultScreen(pDisplay);
27 gc = libX11->XDefaultGC(pDisplay, screen);
28
29 XVisualInfo xVisual;
30 Status status = libX11->XMatchVisualInfo(pDisplay, screen, 32, TrueColor, &xVisual);
31 bool match = (status != 0 && xVisual.blue_mask ==0xFF);
Hernan Liatisf945a5e2019-03-06 15:31:04 -080032 visual = match ? xVisual.visual : libX11->XDefaultVisual(pDisplay, screen);
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080033}
34
Hernan Liatis6b12a502019-03-01 15:06:13 -080035void XlibSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
36{
Hernan Liatisf945a5e2019-03-06 15:31:04 -080037
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080038}
39
Hernan Liatis6b12a502019-03-01 15:06:13 -080040size_t XlibSurfaceKHR::ComputeRequiredAllocationSize(const VkXlibSurfaceCreateInfoKHR *pCreateInfo)
41{
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080042 return 0;
43}
44
Hernan Liatis6b12a502019-03-01 15:06:13 -080045void XlibSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
46{
Hernan Liatisc6eb41b2019-02-22 11:12:59 -080047 SurfaceKHR::getSurfaceCapabilities(pSurfaceCapabilities);
48
49 XWindowAttributes attr;
50 libX11->XGetWindowAttributes(pDisplay, window, &attr);
51 VkExtent2D extent = {static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height)};
52
53 pSurfaceCapabilities->currentExtent = extent;
54 pSurfaceCapabilities->minImageExtent = extent;
55 pSurfaceCapabilities->maxImageExtent = extent;
56}
57
Hernan Liatisf945a5e2019-03-06 15:31:04 -080058void XlibSurfaceKHR::attachImage(PresentImage* image)
Hernan Liatis6b12a502019-03-01 15:06:13 -080059{
Hernan Liatis6b12a502019-03-01 15:06:13 -080060 XWindowAttributes attr;
61 libX11->XGetWindowAttributes(pDisplay, window, &attr);
62
Alexis Hetu63ae9242019-06-06 13:52:15 -040063 VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
Hernan Liatis6b12a502019-03-01 15:06:13 -080064
Alexis Hetu63ae9242019-06-06 13:52:15 -040065 int bytes_per_line = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
66 char* buffer = static_cast<char*>(image->getImageMemory()->getOffsetPointer(0));
Hernan Liatisf945a5e2019-03-06 15:31:04 -080067
68 XImage* xImage = libX11->XCreateImage(pDisplay, visual, attr.depth, ZPixmap, 0, buffer, extent.width, extent.height, 32, bytes_per_line);
69
70 imageMap[image] = xImage;
71}
72
73void XlibSurfaceKHR::detachImage(PresentImage* image)
74{
75 auto it = imageMap.find(image);
76 if(it != imageMap.end())
77 {
78 XImage* xImage = it->second;
79 xImage->data = nullptr; // the XImage does not actually own the buffer
80 XDestroyImage(xImage);
81 imageMap.erase(image);
82 }
83}
84
85void XlibSurfaceKHR::present(PresentImage* image)
86{
87 auto it = imageMap.find(image);
88 if(it != imageMap.end())
89 {
90 XImage* xImage = it->second;
91
92 if(xImage->data)
93 {
Alexis Hetu63ae9242019-06-06 13:52:15 -040094 VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
Hernan Liatisf945a5e2019-03-06 15:31:04 -080095 libX11->XPutImage(pDisplay, window, gc, xImage, 0, 0, 0, 0, extent.width, extent.height);
96 }
97 }
Hernan Liatis6b12a502019-03-01 15:06:13 -080098}
99
Hernan Liatisc6eb41b2019-02-22 11:12:59 -0800100}