1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2021 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Worker #include "libXCB.hpp"
16*03ce13f7SAndroid Build Coastguard Worker
17*03ce13f7SAndroid Build Coastguard Worker #include "System/SharedLibrary.hpp"
18*03ce13f7SAndroid Build Coastguard Worker
19*03ce13f7SAndroid Build Coastguard Worker #include <memory>
20*03ce13f7SAndroid Build Coastguard Worker
LibXcbExports(void * libxcb,void * libshm)21*03ce13f7SAndroid Build Coastguard Worker LibXcbExports::LibXcbExports(void *libxcb, void *libshm)
22*03ce13f7SAndroid Build Coastguard Worker {
23*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_create_gc", &xcb_create_gc);
24*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_flush", &xcb_flush);
25*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_free_gc", &xcb_free_gc);
26*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_generate_id", &xcb_generate_id);
27*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_get_geometry", &xcb_get_geometry);
28*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_get_geometry_reply", &xcb_get_geometry_reply);
29*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_put_image", &xcb_put_image);
30*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_copy_area", &xcb_copy_area);
31*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_free_pixmap", &xcb_free_pixmap);
32*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_get_extension_data", &xcb_get_extension_data);
33*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_connection_has_error", &xcb_connection_has_error);
34*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libxcb, "xcb_get_maximum_request_length", &xcb_get_maximum_request_length);
35*03ce13f7SAndroid Build Coastguard Worker
36*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libshm, "xcb_shm_query_version", &xcb_shm_query_version);
37*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libshm, "xcb_shm_query_version_reply", &xcb_shm_query_version_reply);
38*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libshm, "xcb_shm_attach", &xcb_shm_attach);
39*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libshm, "xcb_shm_detach", &xcb_shm_detach);
40*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libshm, "xcb_shm_create_pixmap", &xcb_shm_create_pixmap);
41*03ce13f7SAndroid Build Coastguard Worker xcb_shm_id = (xcb_extension_t *)getProcAddress(libshm, "xcb_shm_id");
42*03ce13f7SAndroid Build Coastguard Worker }
43*03ce13f7SAndroid Build Coastguard Worker
operator ->()44*03ce13f7SAndroid Build Coastguard Worker LibXcbExports *LibXCB::operator->()
45*03ce13f7SAndroid Build Coastguard Worker {
46*03ce13f7SAndroid Build Coastguard Worker return loadExports();
47*03ce13f7SAndroid Build Coastguard Worker }
48*03ce13f7SAndroid Build Coastguard Worker
loadExports()49*03ce13f7SAndroid Build Coastguard Worker LibXcbExports *LibXCB::loadExports()
50*03ce13f7SAndroid Build Coastguard Worker {
51*03ce13f7SAndroid Build Coastguard Worker static LibXcbExports exports = [] {
52*03ce13f7SAndroid Build Coastguard Worker void *libxcb = nullptr;
53*03ce13f7SAndroid Build Coastguard Worker void *libshm = nullptr;
54*03ce13f7SAndroid Build Coastguard Worker if(getProcAddress(RTLD_DEFAULT, "xcb_create_gc")) // Search the global scope for pre-loaded XCB library.
55*03ce13f7SAndroid Build Coastguard Worker {
56*03ce13f7SAndroid Build Coastguard Worker libxcb = RTLD_DEFAULT;
57*03ce13f7SAndroid Build Coastguard Worker }
58*03ce13f7SAndroid Build Coastguard Worker else
59*03ce13f7SAndroid Build Coastguard Worker {
60*03ce13f7SAndroid Build Coastguard Worker libxcb = loadLibrary("libxcb.so.1");
61*03ce13f7SAndroid Build Coastguard Worker }
62*03ce13f7SAndroid Build Coastguard Worker
63*03ce13f7SAndroid Build Coastguard Worker if(getProcAddress(RTLD_DEFAULT, "xcb_shm_query_version")) // Search the global scope for pre-loaded XCB library.
64*03ce13f7SAndroid Build Coastguard Worker {
65*03ce13f7SAndroid Build Coastguard Worker libshm = RTLD_DEFAULT;
66*03ce13f7SAndroid Build Coastguard Worker }
67*03ce13f7SAndroid Build Coastguard Worker else
68*03ce13f7SAndroid Build Coastguard Worker {
69*03ce13f7SAndroid Build Coastguard Worker libshm = loadLibrary("libxcb-shm.so.0");
70*03ce13f7SAndroid Build Coastguard Worker }
71*03ce13f7SAndroid Build Coastguard Worker
72*03ce13f7SAndroid Build Coastguard Worker return LibXcbExports(libxcb, libshm);
73*03ce13f7SAndroid Build Coastguard Worker }();
74*03ce13f7SAndroid Build Coastguard Worker
75*03ce13f7SAndroid Build Coastguard Worker return exports.xcb_create_gc ? &exports : nullptr;
76*03ce13f7SAndroid Build Coastguard Worker }
77*03ce13f7SAndroid Build Coastguard Worker
78*03ce13f7SAndroid Build Coastguard Worker LibXCB libXCB;
79