xref: /aosp_15_r20/external/swiftshader/src/WSI/libWaylandClient.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 "libWaylandClient.hpp"
16 
17 #include "System/SharedLibrary.hpp"
18 
19 #include <memory>
20 
LibWaylandClientExports(void * libwl)21 LibWaylandClientExports::LibWaylandClientExports(void *libwl)
22 {
23 	getFuncAddress(libwl, "wl_display_dispatch", &wl_display_dispatch);
24 	getFuncAddress(libwl, "wl_display_get_registry", &wl_display_get_registry);
25 	getFuncAddress(libwl, "wl_display_roundtrip", &wl_display_roundtrip);
26 	getFuncAddress(libwl, "wl_display_sync", &wl_display_sync);
27 
28 	getFuncAddress(libwl, "wl_registry_add_listener", &wl_registry_add_listener);
29 	getFuncAddress(libwl, "wl_registry_bind", &wl_registry_bind);
30 
31 	getFuncAddress(libwl, "wl_buffer_destroy", &wl_buffer_destroy);
32 	getFuncAddress(libwl, "wl_shm_create_pool", &wl_shm_create_pool);
33 	getFuncAddress(libwl, "wl_shm_pool_create_buffer", &wl_shm_pool_create_buffer);
34 	getFuncAddress(libwl, "wl_shm_pool_destroy", &wl_shm_pool_destroy);
35 
36 	getFuncAddress(libwl, "wl_surface_attach", &wl_surface_attach);
37 	getFuncAddress(libwl, "wl_surface_damage", &wl_surface_damage);
38 	getFuncAddress(libwl, "wl_surface_commit", &wl_surface_commit);
39 
40 	wl_shm_interface = reinterpret_cast<const wl_interface *>(getProcAddress(libwl, "wl_shm_interface"));
41 }
42 
operator ->()43 LibWaylandClientExports *LibWaylandClient::operator->()
44 {
45 	return loadExports();
46 }
47 
loadExports()48 LibWaylandClientExports *LibWaylandClient::loadExports()
49 {
50 	static LibWaylandClientExports exports = [] {
51 		void *libwl = nullptr;
52 
53 		if(getProcAddress(RTLD_DEFAULT, "wl_display_dispatch"))  // Search the global scope for pre-loaded Wayland client library.
54 		{
55 			libwl = RTLD_DEFAULT;
56 		}
57 		else
58 		{
59 			libwl = loadLibrary("libwayland-client.so.0");
60 		}
61 
62 		return LibWaylandClientExports(libwl);
63 	}();
64 
65 	return exports.wl_display_dispatch ? &exports : nullptr;
66 }
67 
68 LibWaylandClient libWaylandClient;
69