1use alloc::{borrow::ToOwned as _, sync::Arc};
2use core::fmt;
3
4use crate::{
5 hub::{Hub, HubReport},
6 instance::{Instance, Surface},
7 registry::{Registry, RegistryReport},
8 resource_log,
9};
10
11#[derive(Debug, PartialEq, Eq)]
12pub struct GlobalReport {
13 pub surfaces: RegistryReport,
14 pub hub: HubReport,
15}
16
17impl GlobalReport {
18 pub fn surfaces(&self) -> &RegistryReport {
19 &self.surfaces
20 }
21 pub fn hub_report(&self) -> &HubReport {
22 &self.hub
23 }
24}
25
26pub struct Global {
27 pub(crate) surfaces: Registry<Arc<Surface>>,
28 pub(crate) hub: Hub,
29 pub instance: Instance,
31}
32
33impl Global {
34 pub fn new(
35 name: &str,
36 instance_desc: wgt::InstanceDescriptor,
37 telemetry: Option<hal::Telemetry>,
38 ) -> Self {
39 profiling::scope!("Global::new");
40 Self {
41 instance: Instance::new(name, instance_desc, telemetry),
42 surfaces: Registry::new(),
43 hub: Hub::new(),
44 }
45 }
46
47 pub unsafe fn from_hal_instance<A: hal::Api>(name: &str, hal_instance: A::Instance) -> Self {
51 profiling::scope!("Global::new");
52
53 Self {
54 instance: Instance::from_hal_instance::<A>(name.to_owned(), hal_instance),
55 surfaces: Registry::new(),
56 hub: Hub::new(),
57 }
58 }
59
60 pub unsafe fn instance_as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
64 unsafe { self.instance.as_hal::<A>() }
65 }
66
67 pub unsafe fn from_instance(instance: Instance) -> Self {
71 profiling::scope!("Global::new");
72 Self {
73 instance,
74 surfaces: Registry::new(),
75 hub: Hub::new(),
76 }
77 }
78
79 pub fn generate_report(&self) -> GlobalReport {
80 GlobalReport {
81 surfaces: self.surfaces.generate_report(),
82 hub: self.hub.generate_report(),
83 }
84 }
85}
86
87#[cfg(send_sync)]
100mod global_send_sync {
101 use super::Global;
102
103 unsafe impl Send for Global {}
105
106 unsafe impl Sync for Global {}
108
109 fn _global_fields_are_send_sync(global: &Global) {
117 fn _check_bound<T: Send + Sync>(_: &T) {}
118
119 let Global {
120 surfaces,
121 hub,
122 instance,
123 } = global;
124 _check_bound(surfaces);
125 _check_bound(hub);
126 _check_bound(instance);
127 }
128}
129
130impl fmt::Debug for Global {
131 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132 f.debug_struct("Global").finish()
133 }
134}
135
136impl Drop for Global {
137 fn drop(&mut self) {
138 profiling::scope!("Global::drop");
139 resource_log!("Global::drop");
140 }
141}