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__anond9f340800111::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
getWindowSize(xcb_connection_t * connection,xcb_window_t window)90 VkExtent2D getWindowSize(xcb_connection_t *connection, xcb_window_t window)
91 {
92 VkExtent2D windowExtent = { 0, 0 };
93 xcb_generic_error_t *error = nullptr;
94 auto geom = libXcb->xcb_get_geometry_reply(connection, libXcb->xcb_get_geometry(connection, window), &error);
95 if(error)
96 {
97 free(error);
98 }
99 else
100 {
101 windowExtent.width = static_cast<uint32_t>(geom->width);
102 windowExtent.height = static_cast<uint32_t>(geom->height);
103 }
104 free(geom);
105 return windowExtent;
106 }
107
108 } // anonymous namespace
109
110 namespace vk {
111
hasLibXCB()112 bool XcbSurfaceKHR::hasLibXCB()
113 {
114 return libXcb;
115 }
116
XcbSurfaceKHR(const VkXcbSurfaceCreateInfoKHR * pCreateInfo,void * mem)117 XcbSurfaceKHR::XcbSurfaceKHR(const VkXcbSurfaceCreateInfoKHR *pCreateInfo, void *mem)
118 : connection(pCreateInfo->connection)
119 , window(pCreateInfo->window)
120 {
121 }
122
destroySurface(const VkAllocationCallbacks * pAllocator)123 void XcbSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
124 {
125 }
126
ComputeRequiredAllocationSize(const VkXcbSurfaceCreateInfoKHR * pCreateInfo)127 size_t XcbSurfaceKHR::ComputeRequiredAllocationSize(const VkXcbSurfaceCreateInfoKHR *pCreateInfo)
128 {
129 return 0;
130 }
131
getSurfaceCapabilities(VkSurfaceCapabilitiesKHR * pSurfaceCapabilities) const132 VkResult XcbSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const
133 {
134 setCommonSurfaceCapabilities(pSurfaceCapabilities);
135
136 VkExtent2D extent = getWindowSize(connection, window);
137
138 pSurfaceCapabilities->currentExtent = extent;
139 pSurfaceCapabilities->minImageExtent = extent;
140 pSurfaceCapabilities->maxImageExtent = extent;
141 return VK_SUCCESS;
142 }
143
attachImage(PresentImage * image)144 void XcbSurfaceKHR::attachImage(PresentImage *image)
145 {
146 auto gc = libXcb->xcb_generate_id(connection);
147
148 uint32_t values[2] = { 0, 0xffffffff };
149 libXcb->xcb_create_gc(connection, gc, window, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values);
150
151 graphicsContexts[image] = gc;
152 }
153
detachImage(PresentImage * image)154 void XcbSurfaceKHR::detachImage(PresentImage *image)
155 {
156 auto it = graphicsContexts.find(image);
157 if(it != graphicsContexts.end())
158 {
159 libXcb->xcb_free_gc(connection, it->second);
160 graphicsContexts.erase(it);
161 }
162 }
163
present(PresentImage * image)164 VkResult XcbSurfaceKHR::present(PresentImage *image)
165 {
166 auto it = graphicsContexts.find(image);
167 if(it != graphicsContexts.end())
168 {
169 VkExtent2D windowExtent = getWindowSize(connection, window);
170 const VkExtent3D &extent = image->getImage()->getExtent();
171
172 if(windowExtent.width != extent.width || windowExtent.height != extent.height)
173 {
174 return VK_ERROR_OUT_OF_DATE_KHR;
175 }
176
177 // TODO: Convert image if not RGB888.
178 int stride = image->getImage()->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
179 auto buffer = reinterpret_cast<uint8_t *>(image->getImageMemory()->getOffsetPointer(0));
180 size_t bufferSize = extent.height * stride;
181 constexpr int depth = 24; // TODO: Actually use window display depth.
182
183 libXcb->xcb_put_image(
184 connection,
185 XCB_IMAGE_FORMAT_Z_PIXMAP,
186 window,
187 it->second,
188 extent.width,
189 extent.height,
190 0, 0, // dst x, y
191 0, // left_pad
192 depth,
193 bufferSize, // data_len
194 buffer // data
195 );
196
197 libXcb->xcb_flush(connection);
198 }
199
200 return VK_SUCCESS;
201 }
202
203 } // namespace vk