wgpu_core/
global.rs
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(name: &str, instance_desc: &wgt::InstanceDescriptor) -> Self {
35 profiling::scope!("Global::new");
36 Self {
37 instance: Instance::new(name, instance_desc),
38 surfaces: Registry::new(),
39 hub: Hub::new(),
40 }
41 }
42
43 pub unsafe fn from_hal_instance<A: hal::Api>(name: &str, hal_instance: A::Instance) -> Self {
47 profiling::scope!("Global::new");
48
49 Self {
50 instance: Instance::from_hal_instance::<A>(name.to_owned(), hal_instance),
51 surfaces: Registry::new(),
52 hub: Hub::new(),
53 }
54 }
55
56 pub unsafe fn instance_as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
60 unsafe { self.instance.as_hal::<A>() }
61 }
62
63 pub unsafe fn from_instance(instance: Instance) -> Self {
67 profiling::scope!("Global::new");
68 Self {
69 instance,
70 surfaces: Registry::new(),
71 hub: Hub::new(),
72 }
73 }
74
75 pub fn generate_report(&self) -> GlobalReport {
76 GlobalReport {
77 surfaces: self.surfaces.generate_report(),
78 hub: self.hub.generate_report(),
79 }
80 }
81}
82
83impl fmt::Debug for Global {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 f.debug_struct("Global").finish()
86 }
87}
88
89impl Drop for Global {
90 fn drop(&mut self) {
91 profiling::scope!("Global::drop");
92 resource_log!("Global::drop");
93 }
94}
95
96#[cfg(send_sync)]
97fn _test_send_sync(global: &Global) {
98 fn test_internal<T: Send + Sync>(_: T) {}
99 test_internal(global)
100}