Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 1 | // Copyright 2019 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 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 | |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 17 | #include "Vulkan/VkDeviceMemory.hpp" |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 18 | #include "Vulkan/VkImage.hpp" |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 19 | |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 20 | namespace vk { |
| 21 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 22 | XlibSurfaceKHR::XlibSurfaceKHR(const VkXlibSurfaceCreateInfoKHR *pCreateInfo, void *mem) |
| 23 | : pDisplay(pCreateInfo->dpy) |
| 24 | , window(pCreateInfo->window) |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 25 | { |
| 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); |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 31 | bool match = (status != 0 && xVisual.blue_mask == 0xFF); |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 32 | visual = match ? xVisual.visual : libX11->XDefaultVisual(pDisplay, screen); |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 35 | void XlibSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator) |
| 36 | { |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 39 | size_t XlibSurfaceKHR::ComputeRequiredAllocationSize(const VkXlibSurfaceCreateInfoKHR *pCreateInfo) |
| 40 | { |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 44 | void XlibSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const |
| 45 | { |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 46 | SurfaceKHR::getSurfaceCapabilities(pSurfaceCapabilities); |
| 47 | |
| 48 | XWindowAttributes attr; |
| 49 | libX11->XGetWindowAttributes(pDisplay, window, &attr); |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 50 | VkExtent2D extent = { static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height) }; |
Hernan Liatis | c6eb41b | 2019-02-22 11:12:59 -0800 | [diff] [blame] | 51 | |
| 52 | pSurfaceCapabilities->currentExtent = extent; |
| 53 | pSurfaceCapabilities->minImageExtent = extent; |
| 54 | pSurfaceCapabilities->maxImageExtent = extent; |
| 55 | } |
| 56 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 57 | void XlibSurfaceKHR::attachImage(PresentImage *image) |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 58 | { |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 59 | XWindowAttributes attr; |
| 60 | libX11->XGetWindowAttributes(pDisplay, window, &attr); |
| 61 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 62 | VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0); |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 63 | |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 64 | int bytes_per_line = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0); |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 65 | char *buffer = static_cast<char *>(image->getImageMemory()->getOffsetPointer(0)); |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 66 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 67 | XImage *xImage = libX11->XCreateImage(pDisplay, visual, attr.depth, ZPixmap, 0, buffer, extent.width, extent.height, 32, bytes_per_line); |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 68 | |
| 69 | imageMap[image] = xImage; |
| 70 | } |
| 71 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 72 | void XlibSurfaceKHR::detachImage(PresentImage *image) |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 73 | { |
| 74 | auto it = imageMap.find(image); |
| 75 | if(it != imageMap.end()) |
| 76 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 77 | XImage *xImage = it->second; |
| 78 | xImage->data = nullptr; // the XImage does not actually own the buffer |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 79 | XDestroyImage(xImage); |
| 80 | imageMap.erase(image); |
| 81 | } |
| 82 | } |
| 83 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 84 | VkResult XlibSurfaceKHR::present(PresentImage *image) |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 85 | { |
| 86 | auto it = imageMap.find(image); |
| 87 | if(it != imageMap.end()) |
| 88 | { |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 89 | XImage *xImage = it->second; |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 90 | |
| 91 | if(xImage->data) |
| 92 | { |
Jonah Ryan-Davis | f2637d0 | 2019-11-25 14:04:55 -0500 | [diff] [blame] | 93 | XWindowAttributes attr; |
| 94 | libX11->XGetWindowAttributes(pDisplay, window, &attr); |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 95 | VkExtent2D windowExtent = { static_cast<uint32_t>(attr.width), static_cast<uint32_t>(attr.height) }; |
Alexis Hetu | 63ae924 | 2019-06-06 13:52:15 -0400 | [diff] [blame] | 96 | VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0); |
Jonah Ryan-Davis | f2637d0 | 2019-11-25 14:04:55 -0500 | [diff] [blame] | 97 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 98 | if(windowExtent.width != extent.width || windowExtent.height != extent.height) |
Jonah Ryan-Davis | f2637d0 | 2019-11-25 14:04:55 -0500 | [diff] [blame] | 99 | { |
| 100 | return VK_ERROR_OUT_OF_DATE_KHR; |
| 101 | } |
| 102 | |
Hernan Liatis | f945a5e | 2019-03-06 15:31:04 -0800 | [diff] [blame] | 103 | libX11->XPutImage(pDisplay, window, gc, xImage, 0, 0, 0, 0, extent.width, extent.height); |
| 104 | } |
| 105 | } |
Antonio Maiorano | 26c6c4a | 2019-10-17 15:33:47 -0400 | [diff] [blame] | 106 | |
| 107 | return VK_SUCCESS; |
Hernan Liatis | 6b12a50 | 2019-03-01 15:06:13 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Ben Clayton | 45c697a | 2019-12-17 20:38:03 +0000 | [diff] [blame^] | 110 | } // namespace vk |