Skip to content

Commit

Permalink
WIP raw-window-handle 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Jan 17, 2024
1 parent ff268c8 commit 7289b60
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 102 deletions.
18 changes: 12 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ thiserror.workspace = true
image.workspace = true
image.optional = true

winit.workspace = true

[profile.release-opt]
inherits = "release"
codegen-units = 1
Expand Down Expand Up @@ -126,7 +128,10 @@ bytemuck = { version = "1.0", features = ["derive"] }
cosmic-text = "0.10"
futures = "0.3"
glam = "0.24"
glyphon = "0.4"
# glyphon = "0.4"
# TODO update for wgpu 0.19
# https://github.com/grovesNL/glyphon/pull/80
glyphon = { git = "https://github.com/EggShark/glyphon" }
guillotiere = "0.6"
half = "2.2"
image = "0.24"
Expand All @@ -140,12 +145,12 @@ once_cell = "1.0"
ouroboros = "0.17"
palette = "0.7"
qrcode = { version = "0.12", default-features = false }
raw-window-handle = "0.5"
raw-window-handle = "0.6"
resvg = "0.36"
rustc-hash = "1.0"
smol = "1.0"
smol_str = "0.2"
softbuffer = "0.3.4"
softbuffer = "0.4.1"
syntect = "5.1"
sysinfo = "0.28"
thiserror = "1.0"
Expand All @@ -158,7 +163,8 @@ wasm-bindgen-futures = "0.4"
wasm-timer = "0.2"
web-sys = "0.3"
web-time = "0.2"
wgpu = "0.18"
wgpu = "0.19"
winapi = "0.3"
window_clipboard = "0.3"
winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5", features = ["rwh_05"] }
# window_clipboard = "0.3"
window_clipboard = { git = "https://github.com/ids1024/window_clipboard", branch = "raw-window-handle-0.6" }
winit = { git = "https://github.com/iced-rs/winit.git", rev = "b91e39ece2c0d378c3b80da7f3ab50e17bb798a5", features = ["rwh_06"] }
12 changes: 6 additions & 6 deletions graphics/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::{Error, Viewport};

use iced_core::Color;

use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use thiserror::Error;

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

Expand All @@ -19,9 +19,9 @@ pub trait Compositor: Sized {
type Surface;

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

/// Creates a [`Self::Renderer`] for the [`Compositor`].
Expand All @@ -30,9 +30,9 @@ pub trait Compositor: Sized {
/// Crates a new [`Surface`] for the given window.
///
/// [`Surface`]: Self::Surface
fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
fn create_surface(
&mut self,
window: &W,
window: W,
width: u32,
height: u32,
) -> Self::Surface;
Expand Down
51 changes: 29 additions & 22 deletions renderer/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,44 @@ use crate::graphics::compositor::{Information, SurfaceError};
use crate::graphics::{Error, Viewport};
use crate::{Renderer, Settings};

use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::env;

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

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

impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
// XXX Clone bound
// XXX Send/Sync?
// 'static?
impl<
W: Clone + Send + Sync + HasWindowHandle + HasDisplayHandle + 'static,
Theme,
> crate::graphics::Compositor<W> for Compositor<W, Theme>
{
type Settings = Settings;
type Renderer = Renderer<Theme>;
type Surface = Surface;
type Surface = Surface<W>;

fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
fn new(
settings: Self::Settings,
compatible_window: Option<&W>,
compatible_window: Option<W>,
) -> Result<Self, Error> {
let candidates =
Candidate::list_from_env().unwrap_or(Candidate::default_list());

let mut error = Error::GraphicsAdapterNotFound;

for candidate in candidates {
match candidate.build(settings, compatible_window) {
match candidate.build(settings, compatible_window.clone()) {
Ok(compositor) => return Ok(compositor),
Err(new_error) => {
error = new_error;
Expand All @@ -56,12 +63,12 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
}
}

fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
fn create_surface(
&mut self,
window: &W,
window: W,
width: u32,
height: u32,
) -> Surface {
) -> Surface<W> {
match self {
Self::TinySkia(compositor) => Surface::TinySkia(
compositor.create_surface(window, width, height),
Expand All @@ -75,7 +82,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {

fn configure_surface(
&mut self,
surface: &mut Surface,
surface: &mut Surface<W>,
width: u32,
height: u32,
) {
Expand Down Expand Up @@ -114,7 +121,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
(
Self::TinySkia(_compositor),
crate::Renderer::TinySkia(renderer),
Surface::TinySkia(surface),
Surface::TinySkia(ref mut surface),
) => renderer.with_primitives(|backend, primitives| {
iced_tiny_skia::window::compositor::present(
backend,
Expand All @@ -129,7 +136,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
(
Self::Wgpu(compositor),
crate::Renderer::Wgpu(renderer),
Surface::Wgpu(surface),
Surface::Wgpu(ref mut surface),
) => renderer.with_primitives(|backend, primitives| {
iced_wgpu::window::compositor::present(
compositor,
Expand Down Expand Up @@ -161,7 +168,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
(
Self::TinySkia(_compositor),
Renderer::TinySkia(renderer),
Surface::TinySkia(surface),
Surface::TinySkia(ref mut surface),
) => renderer.with_primitives(|backend, primitives| {
iced_tiny_skia::window::compositor::screenshot(
surface,
Expand Down Expand Up @@ -226,11 +233,11 @@ impl Candidate {
)
}

fn build<Theme, W: HasRawWindowHandle + HasRawDisplayHandle>(
fn build<Theme, W: HasWindowHandle + HasDisplayHandle + Send + Sync>(
self,
settings: Settings,
_compatible_window: Option<&W>,
) -> Result<Compositor<Theme>, Error> {
_compatible_window: Option<W>,
) -> Result<Compositor<W, Theme>, Error> {
match self {
Self::TinySkia => {
let compositor = iced_tiny_skia::window::compositor::new(
Expand Down
7 changes: 6 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Build interactive cross-platform applications.
use crate::{Command, Element, Executor, Settings, Subscription};

use std::sync::Arc;

pub use crate::style::application::{Appearance, StyleSheet};

/// An interactive cross-platform application.
Expand Down Expand Up @@ -208,7 +210,10 @@ pub trait Application: Sized {
Ok(crate::shell::application::run::<
Instance<Self>,
Self::Executor,
crate::renderer::Compositor<Self::Theme>,
crate::renderer::Compositor<
Arc<winit::window::Window>,
Self::Theme,
>,
>(settings.into(), renderer_settings)?)
}
}
Expand Down
56 changes: 29 additions & 27 deletions tiny_skia/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ use crate::graphics::damage;
use crate::graphics::{Error, Viewport};
use crate::{Backend, Primitive, Renderer, Settings};

use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::collections::VecDeque;
use std::marker::PhantomData;
use std::num::NonZeroU32;

pub struct Compositor<Theme> {
context: Option<softbuffer::Context>,
pub struct Compositor<W: HasDisplayHandle + HasWindowHandle, Theme> {
context: Option<softbuffer::Context<W>>,
settings: Settings,
_theme: PhantomData<Theme>,
}

pub struct Surface {
window: softbuffer::Surface,
pub struct Surface<W: HasDisplayHandle + HasWindowHandle> {
window: softbuffer::Surface<W, W>,
clip_mask: tiny_skia::Mask,
// Primitives of existing buffers, by decreasing age
primitives: VecDeque<Vec<Primitive>>,
background_color: Color,
}

impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
// XXX avoid clone bound?
impl<W: HasDisplayHandle + HasWindowHandle + Clone, Theme>
crate::graphics::Compositor<W> for Compositor<W, Theme>
{
type Settings = Settings;
type Renderer = Renderer<Theme>;
type Surface = Surface;
type Surface = Surface<W>;

fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(
fn new(
settings: Self::Settings,
compatible_window: Option<&W>,
compatible_window: Option<W>,
) -> Result<Self, Error> {
Ok(new(settings, compatible_window))
}
Expand All @@ -43,20 +46,19 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
)
}

fn create_surface<W: HasRawWindowHandle + HasRawDisplayHandle>(
fn create_surface(
&mut self,
window: &W,
window: W,
width: u32,
height: u32,
) -> Surface {
#[allow(unsafe_code)]
) -> Surface<W> {
let window = if let Some(context) = self.context.as_ref() {
unsafe { softbuffer::Surface::new(context, window) }
softbuffer::Surface::new(context, window)
.expect("Create softbuffer surface for window")
} else {
let context = unsafe { softbuffer::Context::new(window) }
let context = softbuffer::Context::new(window.clone())
.expect("Create softbuffer context for window");
unsafe { softbuffer::Surface::new(&context, window) }
softbuffer::Surface::new(&context, window)
.expect("Create softbuffer surface for window")
};

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

fn configure_surface(
&mut self,
surface: &mut Surface,
surface: &mut Surface<W>,
width: u32,
height: u32,
) {
Expand All @@ -90,7 +92,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
fn present<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
surface: &mut Self::Surface,
surface: &mut Surface<W>,
viewport: &Viewport,
background_color: Color,
overlay: &[T],
Expand Down Expand Up @@ -128,23 +130,23 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> {
}
}

pub fn new<W: HasRawWindowHandle + HasRawDisplayHandle, Theme>(
pub fn new<W: HasWindowHandle + HasDisplayHandle, Theme>(
settings: Settings,
compatible_window: Option<&W>,
) -> Compositor<Theme> {
compatible_window: Option<W>,
) -> Compositor<W, Theme> {
#[allow(unsafe_code)]
let context = compatible_window
.and_then(|w| unsafe { softbuffer::Context::new(w) }.ok());
let context =
compatible_window.and_then(|w| softbuffer::Context::new(w).ok());
Compositor {
context,
settings,
_theme: PhantomData,
}
}

pub fn present<T: AsRef<str>>(
pub fn present<W: HasDisplayHandle + HasWindowHandle, T: AsRef<str>>(
backend: &mut Backend,
surface: &mut Surface,
surface: &mut Surface<W>,
primitives: &[Primitive],
viewport: &Viewport,
background_color: Color,
Expand Down Expand Up @@ -216,8 +218,8 @@ pub fn present<T: AsRef<str>>(
buffer.present().map_err(|_| compositor::SurfaceError::Lost)
}

pub fn screenshot<T: AsRef<str>>(
surface: &mut Surface,
pub fn screenshot<W: HasDisplayHandle + HasWindowHandle, T: AsRef<str>>(
surface: &mut Surface<W>,
backend: &mut Backend,
primitives: &[Primitive],
viewport: &Viewport,
Expand Down
Loading

0 comments on commit 7289b60

Please sign in to comment.