Skip to content

Commit dcc26e7

Browse files
committed
WIP raw-window-handle 0.6
1 parent ff268c8 commit dcc26e7

8 files changed

Lines changed: 141 additions & 99 deletions

File tree

Cargo.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ thiserror.workspace = true
7070
image.workspace = true
7171
image.optional = true
7272

73+
winit.workspace = true
74+
7375
[profile.release-opt]
7476
inherits = "release"
7577
codegen-units = 1
@@ -140,12 +142,12 @@ once_cell = "1.0"
140142
ouroboros = "0.17"
141143
palette = "0.7"
142144
qrcode = { version = "0.12", default-features = false }
143-
raw-window-handle = "0.5"
145+
raw-window-handle = "0.6"
144146
resvg = "0.36"
145147
rustc-hash = "1.0"
146148
smol = "1.0"
147149
smol_str = "0.2"
148-
softbuffer = "0.3.4"
150+
softbuffer = "0.4.1"
149151
syntect = "5.1"
150152
sysinfo = "0.28"
151153
thiserror = "1.0"
@@ -158,7 +160,11 @@ wasm-bindgen-futures = "0.4"
158160
wasm-timer = "0.2"
159161
web-sys = "0.3"
160162
web-time = "0.2"
161-
wgpu = "0.18"
163+
# wgpu = "0.18"
164+
wgpu = { git = "https://github.com/gfx-rs/wgpu" }
162165
winapi = "0.3"
163166
window_clipboard = "0.3"
164-
winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5", features = ["rwh_05"] }
167+
winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5", features = ["rwh_06"] }
168+
169+
[patch.crates-io]
170+
wgpu = { git = "https://github.com/gfx-rs/wgpu" }

graphics/src/compositor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::{Error, Viewport};
44

55
use iced_core::Color;
66

7-
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
7+
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
88
use thiserror::Error;
99

1010
/// A graphics compositor that can draw to windows.
11-
pub trait Compositor: Sized {
11+
pub trait Compositor<W: HasWindowHandle + HasDisplayHandle>: Sized {
1212
/// The settings of the backend.
1313
type Settings: Default;
1414

@@ -19,9 +19,9 @@ pub trait Compositor: Sized {
1919
type Surface;
2020

2121
/// Creates a new [`Compositor`].
22-
fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
22+
fn new(
2323
settings: Self::Settings,
24-
compatible_window: Option<&W>,
24+
compatible_window: Option<W>,
2525
) -> Result<Self, Error>;
2626

2727
/// Creates a [`Self::Renderer`] for the [`Compositor`].
@@ -30,9 +30,9 @@ pub trait Compositor: Sized {
3030
/// Crates a new [`Surface`] for the given window.
3131
///
3232
/// [`Surface`]: Self::Surface
33-
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
33+
fn create_surface(
3434
&mut self,
35-
window: &W,
35+
window: W,
3636
width: u32,
3737
height: u32,
3838
) -> Self::Surface;

renderer/src/compositor.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,44 @@ use crate::graphics::compositor::{Information, SurfaceError};
33
use crate::graphics::{Error, Viewport};
44
use crate::{Renderer, Settings};
55

6-
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
6+
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
77
use std::env;
88

9-
pub enum Compositor<Theme> {
10-
TinySkia(iced_tiny_skia::window::Compositor<Theme>),
9+
pub enum Compositor<W: HasWindowHandle + HasDisplayHandle, Theme> {
10+
TinySkia(iced_tiny_skia::window::Compositor<W, Theme>),
1111
#[cfg(feature = "wgpu")]
12-
Wgpu(iced_wgpu::window::Compositor<Theme>),
12+
Wgpu(iced_wgpu::window::Compositor<W, Theme>),
1313
}
1414

15-
pub enum Surface {
16-
TinySkia(iced_tiny_skia::window::Surface),
15+
pub enum Surface<W: HasWindowHandle + HasDisplayHandle> {
16+
TinySkia(iced_tiny_skia::window::Surface<W>),
1717
#[cfg(feature = "wgpu")]
18-
Wgpu(iced_wgpu::window::Surface),
18+
Wgpu(iced_wgpu::window::Surface<'static>),
1919
}
2020

21-
impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
21+
// XXX Clone bound
22+
// XXX Send/Sync?
23+
// 'static?
24+
impl<
25+
W: Clone + Send + Sync + HasWindowHandle + HasDisplayHandle + 'static,
26+
Theme,
27+
> crate::graphics::Compositor<W> for Compositor<W, Theme>
28+
{
2229
type Settings = Settings;
2330
type Renderer = Renderer<Theme>;
24-
type Surface = Surface;
31+
type Surface = Surface<W>;
2532

26-
fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
33+
fn new(
2734
settings: Self::Settings,
28-
compatible_window: Option<&W>,
35+
compatible_window: Option<W>,
2936
) -> Result<Self, Error> {
3037
let candidates =
3138
Candidate::list_from_env().unwrap_or(Candidate::default_list());
3239

3340
let mut error = Error::GraphicsAdapterNotFound;
3441

3542
for candidate in candidates {
36-
match candidate.build(settings, compatible_window) {
43+
match candidate.build(settings, compatible_window.clone()) {
3744
Ok(compositor) => return Ok(compositor),
3845
Err(new_error) => {
3946
error = new_error;
@@ -56,12 +63,12 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
5663
}
5764
}
5865

59-
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
66+
fn create_surface(
6067
&mut self,
61-
window: &W,
68+
window: W,
6269
width: u32,
6370
height: u32,
64-
) -> Surface {
71+
) -> Surface<W> {
6572
match self {
6673
Self::TinySkia(compositor) => Surface::TinySkia(
6774
compositor.create_surface(window, width, height),
@@ -75,7 +82,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
7582

7683
fn configure_surface(
7784
&mut self,
78-
surface: &mut Surface,
85+
surface: &mut Surface<W>,
7986
width: u32,
8087
height: u32,
8188
) {
@@ -114,7 +121,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
114121
(
115122
Self::TinySkia(_compositor),
116123
crate::Renderer::TinySkia(renderer),
117-
Surface::TinySkia(surface),
124+
Surface::TinySkia(ref mut surface),
118125
) => renderer.with_primitives(|backend, primitives| {
119126
iced_tiny_skia::window::compositor::present(
120127
backend,
@@ -129,7 +136,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
129136
(
130137
Self::Wgpu(compositor),
131138
crate::Renderer::Wgpu(renderer),
132-
Surface::Wgpu(surface),
139+
Surface::Wgpu(ref mut surface),
133140
) => renderer.with_primitives(|backend, primitives| {
134141
iced_wgpu::window::compositor::present(
135142
compositor,
@@ -161,7 +168,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
161168
(
162169
Self::TinySkia(_compositor),
163170
Renderer::TinySkia(renderer),
164-
Surface::TinySkia(surface),
171+
Surface::TinySkia(ref mut surface),
165172
) => renderer.with_primitives(|backend, primitives| {
166173
iced_tiny_skia::window::compositor::screenshot(
167174
surface,
@@ -226,11 +233,11 @@ impl Candidate {
226233
)
227234
}
228235

229-
fn build<Theme, W: HasRawWindowHandle + HasRawDisplayHandle>(
236+
fn build<Theme, W: HasWindowHandle + HasDisplayHandle + Send + Sync>(
230237
self,
231238
settings: Settings,
232-
_compatible_window: Option<&W>,
233-
) -> Result<Compositor<Theme>, Error> {
239+
_compatible_window: Option<W>,
240+
) -> Result<Compositor<W, Theme>, Error> {
234241
match self {
235242
Self::TinySkia => {
236243
let compositor = iced_tiny_skia::window::compositor::new(

src/application.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Build interactive cross-platform applications.
22
use crate::{Command, Element, Executor, Settings, Subscription};
33

4+
use std::sync::Arc;
5+
46
pub use crate::style::application::{Appearance, StyleSheet};
57

68
/// An interactive cross-platform application.
@@ -208,7 +210,10 @@ pub trait Application: Sized {
208210
Ok(crate::shell::application::run::<
209211
Instance<Self>,
210212
Self::Executor,
211-
crate::renderer::Compositor<Self::Theme>,
213+
crate::renderer::Compositor<
214+
Arc<winit::window::Window>,
215+
Self::Theme,
216+
>,
212217
>(settings.into(), renderer_settings)?)
213218
}
214219
}

tiny_skia/src/window/compositor.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,36 @@ use crate::graphics::damage;
44
use crate::graphics::{Error, Viewport};
55
use crate::{Backend, Primitive, Renderer, Settings};
66

7-
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
7+
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
88
use std::collections::VecDeque;
99
use std::marker::PhantomData;
1010
use std::num::NonZeroU32;
1111

12-
pub struct Compositor<Theme> {
13-
context: Option<softbuffer::Context>,
12+
pub struct Compositor<W: HasDisplayHandle + HasWindowHandle, Theme> {
13+
context: Option<softbuffer::Context<W>>,
1414
settings: Settings,
1515
_theme: PhantomData<Theme>,
1616
}
1717

18-
pub struct Surface {
19-
window: softbuffer::Surface,
18+
pub struct Surface<W: HasDisplayHandle + HasWindowHandle> {
19+
window: softbuffer::Surface<W, W>,
2020
clip_mask: tiny_skia::Mask,
2121
// Primitives of existing buffers, by decreasing age
2222
primitives: VecDeque<Vec<Primitive>>,
2323
background_color: Color,
2424
}
2525

26-
impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
26+
// XXX avoid clone bound?
27+
impl<W: HasDisplayHandle + HasWindowHandle + Clone, Theme>
28+
crate::graphics::Compositor<W> for Compositor<W, Theme>
29+
{
2730
type Settings = Settings;
2831
type Renderer = Renderer<Theme>;
29-
type Surface = Surface;
32+
type Surface = Surface<W>;
3033

31-
fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
34+
fn new(
3235
settings: Self::Settings,
33-
compatible_window: Option<&W>,
36+
compatible_window: Option<W>,
3437
) -> Result<Self, Error> {
3538
Ok(new(settings, compatible_window))
3639
}
@@ -43,20 +46,19 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
4346
)
4447
}
4548

46-
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
49+
fn create_surface(
4750
&mut self,
48-
window: &W,
51+
window: W,
4952
width: u32,
5053
height: u32,
51-
) -> Surface {
52-
#[allow(unsafe_code)]
54+
) -> Surface<W> {
5355
let window = if let Some(context) = self.context.as_ref() {
54-
unsafe { softbuffer::Surface::new(context, window) }
56+
softbuffer::Surface::new(context, window)
5557
.expect("Create softbuffer surface for window")
5658
} else {
57-
let context = unsafe { softbuffer::Context::new(window) }
59+
let context = softbuffer::Context::new(window.clone())
5860
.expect("Create softbuffer context for window");
59-
unsafe { softbuffer::Surface::new(&context, window) }
61+
softbuffer::Surface::new(&context, window)
6062
.expect("Create softbuffer surface for window")
6163
};
6264

@@ -71,7 +73,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
7173

7274
fn configure_surface(
7375
&mut self,
74-
surface: &mut Surface,
76+
surface: &mut Surface<W>,
7577
width: u32,
7678
height: u32,
7779
) {
@@ -90,7 +92,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
9092
fn present<T: AsRef<str>>(
9193
&mut self,
9294
renderer: &mut Self::Renderer,
93-
surface: &mut Self::Surface,
95+
surface: &mut Surface<W>,
9496
viewport: &Viewport,
9597
background_color: Color,
9698
overlay: &[T],
@@ -128,23 +130,23 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
128130
}
129131
}
130132

131-
pub fn new<W: HasRawWindowHandle + HasRawDisplayHandle, Theme>(
133+
pub fn new<W: HasWindowHandle + HasDisplayHandle, Theme>(
132134
settings: Settings,
133-
compatible_window: Option<&W>,
134-
) -> Compositor<Theme> {
135+
compatible_window: Option<W>,
136+
) -> Compositor<W, Theme> {
135137
#[allow(unsafe_code)]
136-
let context = compatible_window
137-
.and_then(|w| unsafe { softbuffer::Context::new(w) }.ok());
138+
let context =
139+
compatible_window.and_then(|w| softbuffer::Context::new(w).ok());
138140
Compositor {
139141
context,
140142
settings,
141143
_theme: PhantomData,
142144
}
143145
}
144146

145-
pub fn present<T: AsRef<str>>(
147+
pub fn present<W: HasDisplayHandle + HasWindowHandle, T: AsRef<str>>(
146148
backend: &mut Backend,
147-
surface: &mut Surface,
149+
surface: &mut Surface<W>,
148150
primitives: &[Primitive],
149151
viewport: &Viewport,
150152
background_color: Color,
@@ -216,8 +218,8 @@ pub fn present<T: AsRef<str>>(
216218
buffer.present().map_err(|_| compositor::SurfaceError::Lost)
217219
}
218220

219-
pub fn screenshot<T: AsRef<str>>(
220-
surface: &mut Surface,
221+
pub fn screenshot<W: HasDisplayHandle + HasWindowHandle, T: AsRef<str>>(
222+
surface: &mut Surface<W>,
221223
backend: &mut Backend,
222224
primitives: &[Primitive],
223225
viewport: &Viewport,

0 commit comments

Comments
 (0)