blob: 212884cb6dbc5e01cfc410d5b9927010a6423788 [file] [log] [blame]
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -04001// 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
Peng Huang87c78ac2021-12-23 10:49:44 +000021LibXcbExports::LibXcbExports(void *libxcb, void *libshm)
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040022{
Peng Huang87c78ac2021-12-23 10:49:44 +000023 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);
Thomas Larochef45f8d02022-01-27 10:27:42 +010032 getFuncAddress(libxcb, "xcb_get_extension_data", &xcb_get_extension_data);
Peng Huang87c78ac2021-12-23 10:49:44 +000033
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);
Thomas Larochef45f8d02022-01-27 10:27:42 +010039 xcb_shm_id = (xcb_extension_t *)getProcAddress(libshm, "xcb_shm_id");
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040040}
41
42LibXcbExports *LibXCB::operator->()
43{
44 return loadExports();
45}
46
47LibXcbExports *LibXCB::loadExports()
48{
Nicolas Capensf12305c2021-11-24 15:01:45 -050049 static LibXcbExports exports = [] {
Peng Huang87c78ac2021-12-23 10:49:44 +000050 void *libxcb = nullptr;
51 void *libshm = nullptr;
Nicolas Capensf12305c2021-11-24 15:01:45 -050052 if(getProcAddress(RTLD_DEFAULT, "xcb_create_gc")) // Search the global scope for pre-loaded XCB library.
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040053 {
Peng Huang87c78ac2021-12-23 10:49:44 +000054 libxcb = RTLD_DEFAULT;
55 }
56 else
Nicolas Capens598c7cd2022-04-05 00:27:05 -040057 {
Peng Huang87c78ac2021-12-23 10:49:44 +000058 libxcb = loadLibrary("libxcb.so.1");
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040059 }
60
Peng Huang87c78ac2021-12-23 10:49:44 +000061 if(getProcAddress(RTLD_DEFAULT, "xcb_shm_query_version")) // Search the global scope for pre-loaded XCB library.
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040062 {
Peng Huang87c78ac2021-12-23 10:49:44 +000063 libshm = RTLD_DEFAULT;
64 }
65 else
Nicolas Capens598c7cd2022-04-05 00:27:05 -040066 {
Peng Huang87c78ac2021-12-23 10:49:44 +000067 libshm = loadLibrary("libxcb-shm.so.0");
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040068 }
69
Peng Huang87c78ac2021-12-23 10:49:44 +000070 return LibXcbExports(libxcb, libshm);
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040071 }();
72
Nicolas Capensf12305c2021-11-24 15:01:45 -050073 return exports.xcb_create_gc ? &exports : nullptr;
Nicolas Capensfc8dd5b2021-10-25 12:06:17 -040074}
75
76LibXCB libXCB;