xref: /aosp_15_r20/external/swiftshader/src/WSI/libXCB.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
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 	getFuncAddress(libxcb, "xcb_connection_has_error", &xcb_connection_has_error);
34 	getFuncAddress(libxcb, "xcb_get_maximum_request_length", &xcb_get_maximum_request_length);
35 
36 	getFuncAddress(libshm, "xcb_shm_query_version", &xcb_shm_query_version);
37 	getFuncAddress(libshm, "xcb_shm_query_version_reply", &xcb_shm_query_version_reply);
38 	getFuncAddress(libshm, "xcb_shm_attach", &xcb_shm_attach);
39 	getFuncAddress(libshm, "xcb_shm_detach", &xcb_shm_detach);
40 	getFuncAddress(libshm, "xcb_shm_create_pixmap", &xcb_shm_create_pixmap);
41 	xcb_shm_id = (xcb_extension_t *)getProcAddress(libshm, "xcb_shm_id");
42 }
43 
operator ->()44 LibXcbExports *LibXCB::operator->()
45 {
46 	return loadExports();
47 }
48 
loadExports()49 LibXcbExports *LibXCB::loadExports()
50 {
51 	static LibXcbExports exports = [] {
52 		void *libxcb = nullptr;
53 		void *libshm = nullptr;
54 		if(getProcAddress(RTLD_DEFAULT, "xcb_create_gc"))  // Search the global scope for pre-loaded XCB library.
55 		{
56 			libxcb = RTLD_DEFAULT;
57 		}
58 		else
59 		{
60 			libxcb = loadLibrary("libxcb.so.1");
61 		}
62 
63 		if(getProcAddress(RTLD_DEFAULT, "xcb_shm_query_version"))  // Search the global scope for pre-loaded XCB library.
64 		{
65 			libshm = RTLD_DEFAULT;
66 		}
67 		else
68 		{
69 			libshm = loadLibrary("libxcb-shm.so.0");
70 		}
71 
72 		return LibXcbExports(libxcb, libshm);
73 	}();
74 
75 	return exports.xcb_create_gc ? &exports : nullptr;
76 }
77 
78 LibXCB libXCB;
79