Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 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 | |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 21 | LibXcbExports::LibXcbExports(void *libxcb, void *libshm) |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 22 | { |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 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); |
Thomas Laroche | f45f8d0 | 2022-01-27 10:27:42 +0100 | [diff] [blame] | 32 | getFuncAddress(libxcb, "xcb_get_extension_data", &xcb_get_extension_data); |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 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); |
Thomas Laroche | f45f8d0 | 2022-01-27 10:27:42 +0100 | [diff] [blame] | 39 | xcb_shm_id = (xcb_extension_t *)getProcAddress(libshm, "xcb_shm_id"); |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | LibXcbExports *LibXCB::operator->() |
| 43 | { |
| 44 | return loadExports(); |
| 45 | } |
| 46 | |
| 47 | LibXcbExports *LibXCB::loadExports() |
| 48 | { |
Nicolas Capens | f12305c | 2021-11-24 15:01:45 -0500 | [diff] [blame] | 49 | static LibXcbExports exports = [] { |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 50 | void *libxcb = nullptr; |
| 51 | void *libshm = nullptr; |
Nicolas Capens | f12305c | 2021-11-24 15:01:45 -0500 | [diff] [blame] | 52 | if(getProcAddress(RTLD_DEFAULT, "xcb_create_gc")) // Search the global scope for pre-loaded XCB library. |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 53 | { |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 54 | libxcb = RTLD_DEFAULT; |
| 55 | } |
| 56 | else |
Nicolas Capens | 598c7cd | 2022-04-05 00:27:05 -0400 | [diff] [blame] | 57 | { |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 58 | libxcb = loadLibrary("libxcb.so.1"); |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 61 | if(getProcAddress(RTLD_DEFAULT, "xcb_shm_query_version")) // Search the global scope for pre-loaded XCB library. |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 62 | { |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 63 | libshm = RTLD_DEFAULT; |
| 64 | } |
| 65 | else |
Nicolas Capens | 598c7cd | 2022-04-05 00:27:05 -0400 | [diff] [blame] | 66 | { |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 67 | libshm = loadLibrary("libxcb-shm.so.0"); |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Peng Huang | 87c78ac | 2021-12-23 10:49:44 +0000 | [diff] [blame] | 70 | return LibXcbExports(libxcb, libshm); |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 71 | }(); |
| 72 | |
Nicolas Capens | f12305c | 2021-11-24 15:01:45 -0500 | [diff] [blame] | 73 | return exports.xcb_create_gc ? &exports : nullptr; |
Nicolas Capens | fc8dd5b | 2021-10-25 12:06:17 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | LibXCB libXCB; |