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

@ -13,6 +13,7 @@ font-icons = []
[dependencies]
bytemuck = "1.2"
glam = "0.8"
raw-window-handle = "0.3"
[dependencies.iced_native]
version = "0.2"

View file

@ -7,7 +7,7 @@ use crate::{
};
pub struct Layer<'a> {
pub bounds: Rectangle<u32>,
pub bounds: Rectangle,
pub quads: Vec<Quad>,
pub meshes: Vec<Mesh<'a>>,
pub text: Vec<Text<'a>>,
@ -15,7 +15,7 @@ pub struct Layer<'a> {
}
impl<'a> Layer<'a> {
pub fn new(bounds: Rectangle<u32>) -> Self {
pub fn new(bounds: Rectangle) -> Self {
Self {
bounds,
quads: Vec::new(),
@ -26,14 +26,8 @@ impl<'a> Layer<'a> {
}
pub fn overlay(lines: &'a [impl AsRef<str>], viewport: &Viewport) -> Self {
let (width, height) = viewport.dimensions();
let mut overlay = Layer::new(Rectangle {
x: 0,
y: 0,
width,
height,
});
let mut overlay =
Layer::new(Rectangle::with_size(viewport.logical_size()));
for (i, line) in lines.iter().enumerate() {
let text = Text {
@ -61,28 +55,14 @@ impl<'a> Layer<'a> {
overlay
}
pub(crate) fn intersection(
&self,
rectangle: Rectangle,
) -> Option<Rectangle<u32>> {
let layer_bounds: Rectangle<f32> = self.bounds.into();
layer_bounds.intersection(&rectangle).map(Into::into)
}
pub fn generate(
primitive: &'a Primitive,
viewport: &Viewport,
) -> Vec<Self> {
let mut layers = Vec::new();
let (width, height) = viewport.dimensions();
let first_layer =
Layer::new(Rectangle::with_size(viewport.logical_size()));
layers.push(Layer::new(Rectangle {
x: 0,
y: 0,
width,
height,
}));
let mut layers = vec![first_layer];
Self::process_primitive(&mut layers, Vector::new(0.0, 0.0), primitive);
@ -156,11 +136,11 @@ impl<'a> Layer<'a> {
);
// Only draw visible content
if let Some(clip_bounds) = layer.intersection(bounds) {
if let Some(clip_bounds) = layer.bounds.intersection(&bounds) {
layer.meshes.push(Mesh {
origin: Point::new(translation.x, translation.y),
buffers,
clip_bounds: clip_bounds.into(),
clip_bounds,
});
}
}
@ -170,12 +150,13 @@ impl<'a> Layer<'a> {
content,
} => {
let layer = layers.last_mut().unwrap();
let translated_bounds = *bounds + translation;
// Only draw visible content
if let Some(clip_bounds) =
layer.intersection(*bounds + translation)
layer.bounds.intersection(&translated_bounds)
{
let clip_layer = Layer::new(clip_bounds.into());
let clip_layer = Layer::new(clip_bounds);
let new_layer = Layer::new(layer.bounds);
layers.push(clip_layer);
@ -236,7 +217,7 @@ pub struct Quad {
pub struct Mesh<'a> {
pub origin: Point,
pub buffers: &'a triangle::Mesh2D,
pub clip_bounds: Rectangle<u32>,
pub clip_bounds: Rectangle<f32>,
}
#[derive(Debug, Clone, Copy)]

View file

@ -10,6 +10,7 @@ pub mod backend;
pub mod font;
pub mod layer;
pub mod triangle;
pub mod window;
#[doc(no_inline)]
pub use widget::*;

View file

@ -1,33 +1,48 @@
use crate::Transformation;
use crate::{Size, Transformation};
/// A viewing region for displaying computer graphics.
#[derive(Debug)]
pub struct Viewport {
width: u32,
height: u32,
transformation: Transformation,
physical_size: Size<u32>,
logical_size: Size<f32>,
scale_factor: f64,
projection: Transformation,
}
impl Viewport {
/// Creates a new [`Viewport`] with the given dimensions.
pub fn new(width: u32, height: u32) -> Viewport {
/// Creates a new [`Viewport`] with the given physical dimensions and scale
/// factor.
///
/// [`Viewport`]: struct.Viewport.html
pub fn with_physical_size(size: Size<u32>, scale_factor: f64) -> Viewport {
Viewport {
width,
height,
transformation: Transformation::orthographic(width, height),
physical_size: size,
logical_size: Size::new(
(size.width as f64 / scale_factor) as f32,
(size.height as f64 / scale_factor) as f32,
),
scale_factor,
projection: Transformation::orthographic(size.width, size.height),
}
}
pub fn height(&self) -> u32 {
self.height
pub fn physical_size(&self) -> Size<u32> {
self.physical_size
}
/// Returns the dimensions of the [`Viewport`].
pub fn dimensions(&self) -> (u32, u32) {
(self.width, self.height)
pub fn physical_height(&self) -> u32 {
self.physical_size.height
}
pub fn transformation(&self) -> Transformation {
self.transformation
pub fn logical_size(&self) -> Size<f32> {
self.logical_size
}
pub fn scale_factor(&self) -> f64 {
self.scale_factor
}
pub fn projection(&self) -> Transformation {
self.projection
}
}

3
graphics/src/window.rs Normal file
View file

@ -0,0 +1,3 @@
mod compositor;
pub use compositor::Compositor;

View file

@ -0,0 +1,60 @@
use crate::Viewport;
use iced_native::mouse;
use raw_window_handle::HasRawWindowHandle;
/// A graphics compositor that can draw to windows.
pub trait Compositor: Sized {
/// The settings of the backend.
type Settings: Default + Clone;
/// The iced renderer of the backend.
type Renderer: iced_native::Renderer;
/// The surface of the backend.
type Surface;
/// The swap chain of the backend.
type SwapChain;
/// Creates a new [`Backend`].
///
/// [`Backend`]: trait.Backend.html
fn new(settings: Self::Settings) -> Self;
/// Crates a new [`Surface`] for the given window.
///
/// [`Surface`]: #associatedtype.Surface
fn create_surface<W: HasRawWindowHandle>(
&mut self,
window: &W,
) -> Self::Surface;
/// Crates a new [`Renderer`].
///
/// [`Renderer`]: #associatedtype.Renderer
fn create_renderer(&mut self, settings: Self::Settings) -> Self::Renderer;
/// Crates a new [`SwapChain`] for the given [`Surface`].
///
/// [`SwapChain`]: #associatedtype.SwapChain
/// [`Surface`]: #associatedtype.Surface
fn create_swap_chain(
&mut self,
surface: &Self::Surface,
width: u32,
height: u32,
) -> Self::SwapChain;
/// Draws the output primitives to the next frame of the given [`SwapChain`].
///
/// [`SwapChain`]: #associatedtype.SwapChain
/// [`Surface`]: #associatedtype.Surface
fn draw<T: AsRef<str>>(
&mut self,
renderer: &mut Self::Renderer,
swap_chain: &mut Self::SwapChain,
viewport: &Viewport,
output: &<Self::Renderer as iced_native::Renderer>::Output,
overlay: &[T],
) -> mouse::Interaction;
}