Refactor Viewport and Compositor

This commit is contained in:
Héctor Ramón Jiménez 2020-05-20 20:28:35 +02:00
parent 720e7756f2
commit a1a5fcfd46
24 changed files with 202 additions and 278 deletions

View file

@ -1,5 +1,6 @@
use crate::{window::SwapChain, Renderer, Settings, Target};
use crate::{Renderer, Settings};
use iced_graphics::Viewport;
use iced_native::{futures, mouse};
use raw_window_handle::HasRawWindowHandle;
@ -11,11 +12,11 @@ pub struct Compositor {
format: wgpu::TextureFormat,
}
impl iced_native::window::Compositor for Compositor {
impl iced_graphics::window::Compositor for Compositor {
type Settings = Settings;
type Renderer = Renderer;
type Surface = wgpu::Surface;
type SwapChain = SwapChain;
type SwapChain = wgpu::SwapChain;
fn new(settings: Self::Settings) -> Self {
let (device, queue) = futures::executor::block_on(async {
@ -66,19 +67,28 @@ impl iced_native::window::Compositor for Compositor {
surface: &Self::Surface,
width: u32,
height: u32,
) -> SwapChain {
SwapChain::new(&self.device, surface, self.format, width, height)
) -> Self::SwapChain {
self.device.create_swap_chain(
surface,
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: self.format,
width,
height,
present_mode: wgpu::PresentMode::Mailbox,
},
)
}
fn draw<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
swap_chain: &mut SwapChain,
swap_chain: &mut Self::SwapChain,
viewport: &Viewport,
output: &<Self::Renderer as iced_native::Renderer>::Output,
scale_factor: f64,
overlay: &[T],
) -> mouse::Interaction {
let (frame, viewport) = swap_chain.next_frame().expect("Next frame");
let frame = swap_chain.get_next_texture().expect("Next frame");
let mut encoder = self.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None },
@ -103,12 +113,9 @@ impl iced_native::window::Compositor for Compositor {
let mouse_interaction = renderer.backend_mut().draw(
&mut self.device,
&mut encoder,
Target {
texture: &frame.view,
viewport,
},
&frame.view,
viewport,
output,
scale_factor,
overlay,
);

View file

@ -1,61 +0,0 @@
use crate::Viewport;
/// The rendering target of a window.
///
/// It represents a series of virtual framebuffers with a scale factor.
#[derive(Debug)]
pub struct SwapChain {
raw: wgpu::SwapChain,
viewport: Viewport,
}
impl SwapChain {}
impl SwapChain {
/// Creates a new [`SwapChain`] for the given surface.
///
/// [`SwapChain`]: struct.SwapChain.html
pub fn new(
device: &wgpu::Device,
surface: &wgpu::Surface,
format: wgpu::TextureFormat,
width: u32,
height: u32,
) -> SwapChain {
SwapChain {
raw: new_swap_chain(surface, format, width, height, device),
viewport: Viewport::new(width, height),
}
}
/// Returns the next frame of the [`SwapChain`] alongside its [`Viewport`].
///
/// [`SwapChain`]: struct.SwapChain.html
/// [`Viewport`]: ../struct.Viewport.html
pub fn next_frame(
&mut self,
) -> Result<(wgpu::SwapChainOutput, &Viewport), wgpu::TimeOut> {
let viewport = &self.viewport;
self.raw.get_next_texture().map(|output| (output, viewport))
}
}
fn new_swap_chain(
surface: &wgpu::Surface,
format: wgpu::TextureFormat,
width: u32,
height: u32,
device: &wgpu::Device,
) -> wgpu::SwapChain {
device.create_swap_chain(
&surface,
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format,
width,
height,
present_mode: wgpu::PresentMode::Mailbox,
},
)
}