1 // Copyright 2021 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 "libXCB.hpp"
16
17 #include "System/SharedLibrary.hpp"
18
19 #include <memory>
20
LibXcbExports(void * libxcb,void * libshm)21 LibXcbExports::LibXcbExports(void *libxcb, void *libshm)
22 {
23 getFuncAddress(libxcb, "xcb_create_gc", &xcb_create_gc);
24 getFuncAddress(libxcb, "xcb_flush", &xcb_flush);
25 getFuncAddress(libxcb, "xcb_free_gc", &xcb_free_gc);
26 getFuncAddress(libxcb, "xcb_generate_id", &xcb_generate_id);
27 getFuncAddress(libxcb, "xcb_get_geometry", &xcb_get_geometry);
28 getFuncAddress(libxcb, "xcb_get_geometry_reply", &xcb_get_geometry_reply);
29 getFuncAddress(libxcb, "xcb_put_image", &xcb_put_image);
30 getFuncAddress(libxcb, "xcb_copy_area", &xcb_copy_area);
31 getFuncAddress(libxcb, "xcb_free_pixmap", &xcb_free_pixmap);
32 getFuncAddress(libxcb, "xcb_get_extension_data", &xcb_get_extension_data);
33
34 getFuncAddress(libshm, "xcb_shm_query_version", &xcb_shm_query_version);
35 getFuncAddress(libshm, "xcb_shm_query_version_reply", &xcb_shm_query_version_reply);
36 getFuncAddress(libshm, "xcb_shm_attach", &xcb_shm_attach);
37 getFuncAddress(libshm, "xcb_shm_detach", &xcb_shm_detach);
38 getFuncAddress(libshm, "xcb_shm_create_pixmap", &xcb_shm_create_pixmap);
39 xcb_shm_id = (xcb_extension_t *)getProcAddress(libshm, "xcb_shm_id");
40 }
41
operator ->()42 LibXcbExports *LibXCB::operator->()
43 {
44 return loadExports();
45 }
46
loadExports()47 LibXcbExports *LibXCB::loadExports()
48 {
49 static LibXcbExports exports = [] {
50 void *libxcb = nullptr;
51 void *libshm = nullptr;
52 if(getProcAddress(RTLD_DEFAULT, "xcb_create_gc")) // Search the global scope for pre-loaded XCB library.
53 {
54 libxcb = RTLD_DEFAULT;
55 }
56 else
57 {
58 libxcb = loadLibrary("libxcb.so.1");
59 }
60
61 if(getProcAddress(RTLD_DEFAULT, "xcb_shm_query_version")) // Search the global scope for pre-loaded XCB library.
62 {
63 libshm = RTLD_DEFAULT;
64 }
65 else
66 {
67 libshm = loadLibrary("libxcb-shm.so.0");
68 }
69
70 return LibXcbExports(libxcb, libshm);
71 }();
72
73 return exports.xcb_create_gc ? &exports : nullptr;
74 }
75
76 LibXCB libXCB;
77