Skip to content

Commit 46abc11

Browse files
committed
Add method take the window handle from a Surface
1 parent 9a7c9ca commit 46abc11

11 files changed

Lines changed: 136 additions & 43 deletions

File tree

‎src/backend_dispatch.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ macro_rules! make_dispatch {
9999
}
100100
}
101101

102+
fn into_window(self) -> W {
103+
match self {
104+
$(
105+
$(#[$attr])*
106+
Self::$name(inner) => inner.into_window(),
107+
)*
108+
}
109+
}
110+
102111
#[inline]
103112
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
104113
match self {

‎src/backend_interface.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHand
2626
/// Get the inner window handle.
2727
fn window(&self) -> &W;
2828

29+
/// Get the inner window handle and drop the surface.
30+
fn into_window(self) -> W;
31+
2932
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool;
3033

3134
/// Reconfigure the internal buffer(s).

‎src/backends/android.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
5252
&self.window
5353
}
5454

55+
#[inline]
56+
fn into_window(self) -> W {
57+
self.window
58+
}
59+
5560
#[inline]
5661
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
5762
matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored)

‎src/backends/cg.rs‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Observer {
100100
}
101101

102102
#[derive(Debug)]
103-
pub struct CGImpl<D, W> {
103+
struct CGResources<D> {
104104
/// Our layer.
105105
layer: SendCALayer,
106106
/// The layer that our layer was created from.
@@ -118,11 +118,18 @@ pub struct CGImpl<D, W> {
118118
width: u32,
119119
/// The height of the buffer.
120120
height: u32,
121-
window_handle: W,
122121
_display: PhantomData<D>,
123122
}
124123

125-
impl<D, W> Drop for CGImpl<D, W> {
124+
#[derive(Debug)]
125+
pub struct CGImpl<D, W> {
126+
/// Resources needed for the surface.
127+
resources: CGResources<D>,
128+
/// The window associated with the surface.
129+
window_handle: W,
130+
}
131+
132+
impl<D> Drop for CGResources<D> {
126133
fn drop(&mut self) {
127134
// SAFETY: Registered in `new`, must be removed before the observer is deallocated.
128135
unsafe {
@@ -265,7 +272,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
265272
let width = (size.width * scale_factor) as u32;
266273
let height = (size.height * scale_factor) as u32;
267274

268-
Ok(Self {
275+
let resources = CGResources {
269276
layer: SendCALayer(layer),
270277
root_layer: SendCALayer(root_layer),
271278
observer,
@@ -274,6 +281,10 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
274281
width,
275282
height,
276283
_display: PhantomData,
284+
};
285+
286+
Ok(Self {
287+
resources,
277288
window_handle: window_src,
278289
})
279290
}
@@ -283,6 +294,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
283294
&self.window_handle
284295
}
285296

297+
#[inline]
298+
fn into_window(self) -> W {
299+
self.window_handle
300+
}
301+
286302
#[inline]
287303
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
288304
// Premultiplied doesn't seem to work, at least not with transparent windows.
@@ -299,36 +315,36 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
299315
alpha_mode: AlphaMode,
300316
) -> Result<(), SoftBufferError> {
301317
let opaque = matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored);
302-
self.layer.setOpaque(opaque);
318+
self.resources.layer.setOpaque(opaque);
303319
// TODO: Set opaque-ness on root layer too? Is that our responsibility, or Winit's?
304-
// self.root_layer.setOpaque(opaque);
320+
// self.resources.root_layer.setOpaque(opaque);
305321

306322
let width = width.get();
307323
let height = height.get();
308324

309325
// TODO: Is this check desirable?
310-
if self.width == width && self.height == height {
326+
if self.resources.width == width && self.resources.height == height {
311327
return Ok(());
312328
}
313329

314330
// Recreate buffer. It's fine to release the old one, `CALayer.contents` is going to keep
315331
// a reference to it around as long as it's still in use.
316-
self.buffer = Buffer::new(width, height);
317-
self.width = width;
318-
self.height = height;
332+
self.resources.buffer = Buffer::new(width, height);
333+
self.resources.width = width;
334+
self.resources.height = height;
319335

320336
Ok(())
321337
}
322338

323339
fn next_buffer(&mut self, alpha_mode: AlphaMode) -> Result<BufferImpl<'_>, SoftBufferError> {
324340
// Unlocked in `present_with_damage` or the buffer's `Drop`.
325-
self.buffer.info().lock();
341+
self.resources.buffer.info().lock();
326342

327343
Ok(BufferImpl {
328-
buffer: &mut self.buffer,
329-
width: self.width,
330-
height: self.height,
331-
color_space: &self.color_space,
344+
buffer: &mut self.resources.buffer,
345+
width: self.resources.width,
346+
height: self.resources.height,
347+
color_space: &self.resources.color_space,
332348
alpha_info: match (alpha_mode, cfg!(target_endian = "little")) {
333349
(AlphaMode::Opaque | AlphaMode::Ignored, true) => CGImageAlphaInfo::NoneSkipFirst,
334350
(AlphaMode::Opaque | AlphaMode::Ignored, false) => CGImageAlphaInfo::NoneSkipLast,
@@ -337,7 +353,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
337353
(AlphaMode::Postmultiplied, true) => CGImageAlphaInfo::First,
338354
(AlphaMode::Postmultiplied, false) => CGImageAlphaInfo::Last,
339355
},
340-
layer: &mut self.layer,
356+
layer: &mut self.resources.layer,
341357
})
342358
}
343359
}

‎src/backends/kms.rs‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<D: HasDisplayHandle + ?Sized> ContextInterface<D> for Arc<KmsDisplayImpl<D>
7070

7171
/// All the necessary types for the Drm/Kms backend.
7272
#[derive(Debug)]
73-
pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
73+
struct KmsResources<D: ?Sized> {
7474
/// The display implementation.
7575
display: Arc<KmsDisplayImpl<D>>,
7676

@@ -82,6 +82,13 @@ pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
8282

8383
/// The dumb buffer we're using as a buffer.
8484
buffer: Option<Buffers>,
85+
}
86+
87+
/// Drm/Kms backend resources and the window handle.
88+
#[derive(Debug)]
89+
pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
90+
/// Resources necessary for the Drm/Kms backend.
91+
resources: KmsResources<D>,
8592

8693
/// Window handle that we are keeping around.
8794
window_handle: W,
@@ -211,11 +218,15 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
211218
.map(|info| info.handle())
212219
.collect::<Vec<_>>();
213220

214-
Ok(Self {
221+
let resources = KmsResources {
215222
crtc,
216223
connectors,
217224
display: display.clone(),
218225
buffer: None,
226+
};
227+
228+
Ok(Self {
229+
resources,
219230
window_handle: window,
220231
})
221232
}
@@ -225,6 +236,11 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
225236
&self.window_handle
226237
}
227238

239+
#[inline]
240+
fn into_window(self) -> W {
241+
self.window_handle
242+
}
243+
228244
#[inline]
229245
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
230246
matches!(
@@ -240,7 +256,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
240256
alpha_mode: AlphaMode,
241257
) -> Result<(), SoftBufferError> {
242258
// Don't resize if we don't have to.
243-
if let Some(buffer) = &self.buffer {
259+
if let Some(buffer) = &self.resources.buffer {
244260
let (buffer_width, buffer_height) = buffer.size();
245261
if buffer_width == width && buffer_height == height {
246262
return Ok(());
@@ -254,10 +270,10 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
254270
};
255271

256272
// Create a new buffer set.
257-
let front_buffer = SharedBuffer::new(&self.display, width, height, format)?;
258-
let back_buffer = SharedBuffer::new(&self.display, width, height, format)?;
273+
let front_buffer = SharedBuffer::new(&self.resources.display, width, height, format)?;
274+
let back_buffer = SharedBuffer::new(&self.resources.display, width, height, format)?;
259275

260-
self.buffer = Some(Buffers {
276+
self.resources.buffer = Some(Buffers {
261277
first_is_front: true,
262278
buffers: [front_buffer, back_buffer],
263279
});
@@ -274,6 +290,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
274290
fn next_buffer(&mut self, _alpha_mode: AlphaMode) -> Result<BufferImpl<'_>, SoftBufferError> {
275291
// Map the dumb buffer.
276292
let set = self
293+
.resources
277294
.buffer
278295
.as_mut()
279296
.expect("Must set size of surface before calling `next_buffer()`");
@@ -292,6 +309,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
292309
let back_age = &mut back_buffer.age;
293310

294311
let mapping = self
312+
.resources
295313
.display
296314
.device
297315
.map_dumb_buffer(&mut front_buffer.db)
@@ -302,15 +320,15 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
302320
size,
303321
first_is_front: &mut set.first_is_front,
304322
front_fb,
305-
crtc_handle: self.crtc.handle(),
306-
device: self.display.device.clone(),
323+
crtc_handle: self.resources.crtc.handle(),
324+
device: self.resources.display.device.clone(),
307325
front_age,
308326
back_age,
309327
})
310328
}
311329
}
312330

313-
impl<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
331+
impl<D: ?Sized> Drop for KmsResources<D> {
314332
fn drop(&mut self) {
315333
// Map the CRTC to the information that was there before.
316334
self.display

‎src/backends/orbital.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Orbital
104104
&self.window_handle
105105
}
106106

107+
#[inline]
108+
fn into_window(self) -> W {
109+
self.window_handle
110+
}
111+
107112
#[inline]
108113
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
109114
matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored)

‎src/backends/wayland/mod.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
133133
&self.window_handle
134134
}
135135

136+
#[inline]
137+
fn into_window(self) -> W {
138+
self.window_handle
139+
}
140+
136141
#[inline]
137142
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
138143
matches!(

‎src/backends/web.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for WebImpl
166166
&self.window_handle
167167
}
168168

169+
#[inline]
170+
fn into_window(self) -> W {
171+
self.window_handle
172+
}
173+
169174
#[inline]
170175
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
171176
matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Postmultiplied)

0 commit comments

Comments
 (0)