-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathbackend_dispatch.rs
More file actions
200 lines (179 loc) · 6.79 KB
/
backend_dispatch.rs
File metadata and controls
200 lines (179 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Implements `buffer_interface::*` traits for enums dispatching to backends
use crate::{backend_interface::*, backends, InitError, Rect, SoftBufferError};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::num::NonZeroU32;
#[cfg(any(wayland_platform, x11_platform, kms_platform))]
use std::sync::Arc;
/// A macro for creating the enum used to statically dispatch to the platform-specific implementation.
macro_rules! make_dispatch {
(
<$dgen: ident, $wgen: ident> =>
$(
$(#[$attr:meta])*
$name: ident
($context_inner: ty, $surface_inner: ty, $buffer_inner: ty),
)*
) => {
#[derive(Clone)]
pub(crate) enum ContextDispatch<$dgen> {
$(
$(#[$attr])*
$name($context_inner),
)*
}
impl<D: HasDisplayHandle> ContextDispatch<D> {
pub fn variant_name(&self) -> &'static str {
match self {
$(
$(#[$attr])*
Self::$name(_) => stringify!($name),
)*
}
}
}
impl<D: HasDisplayHandle> ContextInterface<D> for ContextDispatch<D> {
fn new(mut display: D) -> Result<Self, InitError<D>>
where
D: Sized,
{
$(
$(#[$attr])*
match <$context_inner as ContextInterface<D>>::new(display) {
Ok(x) => {
return Ok(Self::$name(x));
}
Err(InitError::Unsupported(d)) => display = d,
Err(InitError::Failure(f)) => return Err(InitError::Failure(f)),
}
)*
Err(InitError::Unsupported(display))
}
}
#[allow(clippy::large_enum_variant)] // it's boxed anyways
pub(crate) enum SurfaceDispatch<$dgen, $wgen> {
$(
$(#[$attr])*
$name($surface_inner),
)*
}
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for SurfaceDispatch<D, W> {
type Context = ContextDispatch<D>;
type Buffer<'a> = BufferDispatch<'a, D, W> where Self: 'a;
fn new(window: W, display: &Self::Context) -> Result<Self, InitError<W>>
where
W: Sized,
Self: Sized {
match display {
$(
$(#[$attr])*
ContextDispatch::$name(inner) => Ok(Self::$name(<$surface_inner>::new(window, inner)?)),
)*
}
}
fn window(&self) -> &W {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.window(),
)*
}
}
fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.resize(width, height),
)*
}
}
fn buffer_mut(&mut self) -> Result<BufferDispatch<'_, D, W>, SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => Ok(BufferDispatch::$name(inner.buffer_mut()?)),
)*
}
}
fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.fetch(),
)*
}
}
}
pub(crate) enum BufferDispatch<'a, $dgen, $wgen> {
$(
$(#[$attr])*
$name($buffer_inner),
)*
}
impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
#[inline]
fn pixels(&self) -> &[u32] {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.pixels(),
)*
}
}
#[inline]
fn pixels_mut(&mut self) -> &mut [u32] {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.pixels_mut(),
)*
}
}
fn age(&self) -> u8 {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.age(),
)*
}
}
fn present(self) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.present(),
)*
}
}
fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.present_with_damage(damage),
)*
}
}
}
};
}
// XXX empty enum with generic bound is invalid?
make_dispatch! {
<D, W> =>
#[cfg(target_os = "android")]
Android(D, backends::android::AndroidImpl<D, W>, backends::android::BufferImpl<'a, D, W>),
#[cfg(x11_platform)]
X11(Arc<backends::x11::X11DisplayImpl<D>>, backends::x11::X11Impl<D, W>, backends::x11::BufferImpl<'a, D, W>),
#[cfg(wayland_platform)]
Wayland(Arc<backends::wayland::WaylandDisplayImpl<D>>, backends::wayland::WaylandImpl<D, W>, backends::wayland::BufferImpl<'a, D, W>),
#[cfg(kms_platform)]
Kms(Arc<backends::kms::KmsDisplayImpl<D>>, backends::kms::KmsImpl<D, W>, backends::kms::BufferImpl<'a, D, W>),
#[cfg(target_os = "windows")]
Win32(D, backends::win32::Win32Impl<D, W>, backends::win32::BufferImpl<'a, D, W>),
#[cfg(target_vendor = "apple")]
CoreGraphics(D, backends::cg::CGImpl<D, W>, backends::cg::BufferImpl<'a, D, W>),
#[cfg(target_arch = "wasm32")]
Web(backends::web::WebDisplayImpl<D>, backends::web::WebImpl<D, W>, backends::web::BufferImpl<'a, D, W>),
#[cfg(target_os = "redox")]
Orbital(D, backends::orbital::OrbitalImpl<D, W>, backends::orbital::BufferImpl<'a, D, W>),
#[cfg(target_os = "uefi")]
Uefi(backends::uefi::UefiDisplayImpl<D>, backends::uefi::UefiImpl<D, W>, backends::uefi::BufferImpl<'a, D, W>),
}