1 // Copyright 2018 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 "XcbSurfaceKHR.hpp"
16
17 #include "Vulkan/VkDeviceMemory.hpp"
18 #include "Vulkan/VkImage.hpp"
19
20 #include "System/SharedLibrary.hpp"
21
22 #include <memory>
23
24 namespace {
25
26 template<typename FPTR>
getFuncAddress(void * lib,const char * name,FPTR * out)27 void getFuncAddress(void *lib, const char *name, FPTR *out)
28 {
29 *out = reinterpret_cast<FPTR>(getProcAddress(lib, name));
30 }
31
32 struct LibXcbExports
33 {
LibXcbExports__anon19f9d3240111::LibXcbExports34 LibXcbExports(void *lib)
35 {
36 getFuncAddress(lib, "xcb_create_gc", &xcb_create_gc);
37 getFuncAddress(lib, "xcb_flush", &xcb_flush);
38 getFuncAddress(lib, "xcb_free_gc", &xcb_free_gc);
39 getFuncAddress(lib, "xcb_generate_id", &xcb_generate_id);
40 getFuncAddress(lib, "xcb_get_geometry", &xcb_get_geometry);
41 getFuncAddress(lib, "xcb_get_geometry_reply", &xcb_get_geometry_reply);
42 getFuncAddress(lib, "xcb_put_image", &xcb_put_image);
43 }
44
45 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);
46 int (*xcb_flush)(xcb_connection_t *c);
47 xcb_void_cookie_t (*xcb_free_gc)(xcb_connection_t *c, xcb_gcontext_t gc);
48 uint32_t (*xcb_generate_id)(xcb_connection_t *c);
49 xcb_get_geometry_cookie_t (*xcb_get_geometry)(xcb_connection_t *c, xcb_drawable_t drawable);
50 xcb_get_geometry_reply_t *(*xcb_get_geometry_reply)(xcb_connection_t *c, xcb_get_geometry_cookie_t cookie, xcb_generic_error_t **e);
51 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);
52 };
53
54 class LibXcb
55 {
56 public:
operator bool()57 operator bool()
58 {
59 return loadExports();
60 }
61
operator ->()62 LibXcbExports *operator->()
63 {
64 return loadExports();
65 }
66
67 private:
loadExports()68 LibXcbExports *loadExports()
69 {
70 static auto exports = [] {
71 if(getProcAddress(RTLD_DEFAULT, "xcb_create_gc"))
72 {
73 return std::make_unique<LibXcbExports>(RTLD_DEFAULT);
74 }
75
76 if(auto lib = loadLibrary("libxcb.so.1"))
77 {
78 return std::make_unique<LibXcbExports>(lib);
79 }
80
81 return std::unique_ptr<LibXcbExports>();
82 }();
83
84 return exports.get();
85 }
86 };
87
88 LibXcb libXcb;
89
90 } // anonymous namespace
91
92 namespace vk {
93
XcbSurfaceKHR(const VkXcbSurfaceCreateInfoKHR * pCreateInfo,void * mem)94 XcbSurfaceKHR::XcbSurfaceKHR(const VkXcbSurfaceCreateInfoKHR *pCreateInfo, void *mem)
95 : connection(pCreateInfo->connection)
96 , window(pCreateInfo->window)
97 {
98 }
99
destroySurface(const VkAllocationCallbacks * pAllocator)100 void XcbSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
101 {
102 }
103
ComputeRequiredAllocationSize(const VkXcbSurfaceCreateInfoKHR * pCreateInfo)104 size_t XcbSurfaceKHR::ComputeRequiredAllocationSize(const VkXcbSurfaceCreateInfoKHR *pCreateInfo)
105 {
106 return 0;
107 }
108
getSurfaceCapabilities(VkSurfaceCapabilitiesKHR * pSurfaceCapabilities) const109 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
attachImage(PresentImage * image)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
detachImage(PresentImage * image)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
present(PresentImage * image)142 VkResult XcbSurfaceKHR::present(PresentImage *image)
143 {
144 auto it = graphicsContexts.find(image);
145 if(it != graphicsContexts.end())
146 {
147 auto geom = libXcb->xcb_get_geometry_reply(connection, libXcb->xcb_get_geometry(connection, window), nullptr);
148 VkExtent2D windowExtent = { static_cast<uint32_t>(geom->width), static_cast<uint32_t>(geom->height) };
149 free(geom);
150 VkExtent3D extent = image->getImage()->getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
151
152 if(windowExtent.width != extent.width || windowExtent.height != extent.height)
153 {
154 return VK_ERROR_OUT_OF_DATE_KHR;
155 }
156
157 // TODO: Convert image if not RGB888.
158 int stride = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
159 auto buffer = reinterpret_cast<uint8_t *>(image->getImageMemory()->getOffsetPointer(0));
160 size_t bufferSize = extent.height * stride;
161 constexpr int depth = 24; // TODO: Actually use window display depth.
162
163 libXcb->xcb_put_image(
164 connection,
165 XCB_IMAGE_FORMAT_Z_PIXMAP,
166 window,
167 it->second,
168 extent.width,
169 extent.height,
170 0, 0, // dst x, y
171 0, // left_pad
172 depth,
173 bufferSize, // data_len
174 buffer // data
175 );
176
177 libXcb->xcb_flush(connection);
178 }
179
180 return VK_SUCCESS;
181 }
182
183 } // namespace vk