Ben Clayton | a9af883 | 2019-08-14 13:09:43 +0100 | [diff] [blame^] | 1 | // you may not use this file except in compliance with the License. |
| 2 | // You may obtain a copy of the License at |
| 3 | // |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | // |
| 6 | // Unless required by applicable law or agreed to in writing, software |
| 7 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | // See the License for the specific language governing permissions and |
| 10 | // limitations under the License. |
| 11 | |
| 12 | #include "XcbSurfaceKHR.hpp" |
| 13 | |
| 14 | #include "Vulkan/VkDeviceMemory.hpp" |
| 15 | #include "Vulkan/VkImage.hpp" |
| 16 | |
| 17 | #include "System/SharedLibrary.hpp" |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | template <typename FPTR> |
| 25 | void getFuncAddress(void *lib, const char *name, FPTR *out) |
| 26 | { |
| 27 | *out = reinterpret_cast<FPTR>(getProcAddress(lib, name)); |
| 28 | } |
| 29 | |
| 30 | struct LibXcbExports |
| 31 | { |
| 32 | LibXcbExports(void *lib) |
| 33 | { |
| 34 | getFuncAddress(lib, "xcb_create_gc", &xcb_create_gc); |
| 35 | getFuncAddress(lib, "xcb_flush", &xcb_flush); |
| 36 | getFuncAddress(lib, "xcb_free_gc", &xcb_free_gc); |
| 37 | getFuncAddress(lib, "xcb_generate_id", &xcb_generate_id); |
| 38 | getFuncAddress(lib, "xcb_get_geometry", &xcb_get_geometry); |
| 39 | getFuncAddress(lib, "xcb_get_geometry_reply", &xcb_get_geometry_reply); |
| 40 | getFuncAddress(lib, "xcb_put_image", &xcb_put_image); |
| 41 | } |
| 42 | |
| 43 | xcb_void_cookie_t (*xcb_create_gc)(xcb_connection_t *c, xcb_gcontext_t cid, xcb_drawable_t drawable, uint32_t value_mask, const void *value_list); |
| 44 | int (*xcb_flush)(xcb_connection_t *c); |
| 45 | xcb_void_cookie_t (*xcb_free_gc)(xcb_connection_t *c, xcb_gcontext_t gc); |
| 46 | uint32_t (*xcb_generate_id)(xcb_connection_t *c); |
| 47 | xcb_get_geometry_cookie_t (*xcb_get_geometry)(xcb_connection_t *c, xcb_drawable_t drawable); |
| 48 | xcb_get_geometry_reply_t* (*xcb_get_geometry_reply)(xcb_connection_t *c, xcb_get_geometry_cookie_t cookie, xcb_generic_error_t **e); |
| 49 | xcb_void_cookie_t (*xcb_put_image)(xcb_connection_t *c, uint8_t format, xcb_drawable_t drawable, xcb_gcontext_t gc, uint16_t width,uint16_t height, int16_t dst_x, int16_t dst_y, uint8_t left_pad, uint8_t depth, uint32_t data_len, const uint8_t* data); |
| 50 | }; |
| 51 | |
| 52 | class LibXcb |
| 53 | { |
| 54 | public: |
| 55 | operator bool() |
| 56 | { |
| 57 | return loadExports(); |
| 58 | } |
| 59 | |
| 60 | LibXcbExports *operator->() |
| 61 | { |
| 62 | return loadExports(); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | LibXcbExports *loadExports() |
| 67 | { |
| 68 | static auto exports = [] |
| 69 | { |
| 70 | if (getProcAddress(RTLD_DEFAULT, "xcb_create_gc")) |
| 71 | { |
| 72 | return std::unique_ptr<LibXcbExports>(new LibXcbExports(RTLD_DEFAULT)); |
| 73 | } |
| 74 | |
| 75 | if (auto lib = loadLibrary("libXcb.so")) |
| 76 | { |
| 77 | return std::unique_ptr<LibXcbExports>(new LibXcbExports(lib)); |
| 78 | } |
| 79 | |
| 80 | return std::unique_ptr<LibXcbExports>(); |
| 81 | }(); |
| 82 | |
| 83 | return exports.get(); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | LibXcb libXcb; |
| 88 | |
| 89 | } // anonymous namespace |
| 90 | |
| 91 | namespace vk { |
| 92 | |
| 93 | XcbSurfaceKHR::XcbSurfaceKHR(const VkXcbSurfaceCreateInfoKHR *pCreateInfo, void *mem) : |
| 94 | connection(pCreateInfo->connection), |
| 95 | window(pCreateInfo->window) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | void XcbSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator) |
| 100 | { |
| 101 | |
| 102 | } |
| 103 | |
| 104 | size_t XcbSurfaceKHR::ComputeRequiredAllocationSize(const VkXcbSurfaceCreateInfoKHR *pCreateInfo) |
| 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void XcbSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const |
| 110 | { |
| 111 | SurfaceKHR::getSurfaceCapabilities(pSurfaceCapabilities); |
| 112 | |
| 113 | auto geom = libXcb->xcb_get_geometry_reply(connection, libXcb->xcb_get_geometry(connection, window), nullptr); |
| 114 | VkExtent2D extent = {static_cast<uint32_t>(geom->width), static_cast<uint32_t>(geom->height)}; |
| 115 | free(geom); |
| 116 | |
| 117 | pSurfaceCapabilities->currentExtent = extent; |
| 118 | pSurfaceCapabilities->minImageExtent = extent; |
| 119 | pSurfaceCapabilities->maxImageExtent = extent; |
| 120 | } |
| 121 | |
| 122 | void XcbSurfaceKHR::attachImage(PresentImage* image) |
| 123 | { |
| 124 | auto gc = libXcb->xcb_generate_id(connection); |
| 125 | |
| 126 | uint32_t values[2] = {0, 0xffffffff}; |
| 127 | libXcb->xcb_create_gc(connection, gc, window, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values); |
| 128 | |
| 129 | graphicsContexts[image] = gc; |
| 130 | } |
| 131 | |
| 132 | void XcbSurfaceKHR::detachImage(PresentImage* image) |
| 133 | { |
| 134 | auto it = graphicsContexts.find(image); |
| 135 | if(it != graphicsContexts.end()) |
| 136 | { |
| 137 | libXcb->xcb_free_gc(connection, it->second); |
| 138 | graphicsContexts.erase(image); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void XcbSurfaceKHR::present(PresentImage* image) |
| 143 | { |
| 144 | auto it = graphicsContexts.find(image); |
| 145 | if(it != graphicsContexts.end()) |
| 146 | { |
| 147 | // TODO: Convert image if not RGB888. |
| 148 | VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0); |
| 149 | int stride = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0); |
| 150 | auto buffer = reinterpret_cast<uint8_t*>(image->getImageMemory()->getOffsetPointer(0)); |
| 151 | size_t bufferSize = extent.height * stride; |
| 152 | constexpr int depth = 24; // TODO: Actually use window display depth. |
| 153 | |
| 154 | libXcb->xcb_put_image( |
| 155 | connection, |
| 156 | XCB_IMAGE_FORMAT_Z_PIXMAP, |
| 157 | window, |
| 158 | it->second, |
| 159 | extent.width, |
| 160 | extent.height, |
| 161 | 0, 0, // dst x, y |
| 162 | 0, // left_pad |
| 163 | depth, |
| 164 | bufferSize, // data_len |
| 165 | buffer // data |
| 166 | ); |
| 167 | |
| 168 | libXcb->xcb_flush(connection); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |