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

@ -47,12 +47,10 @@ impl Backend {
gl: &glow::Context,
viewport: &Viewport,
(primitive, mouse_interaction): &(Primitive, mouse::Interaction),
scale_factor: f64,
overlay_text: &[T],
) -> mouse::Interaction {
let (width, height) = viewport.dimensions();
let scale_factor = scale_factor as f32;
let transformation = viewport.transformation();
let viewport_size = viewport.physical_size();
let projection = viewport.projection();
let mut layers = Layer::generate(primitive, viewport);
layers.push(Layer::overlay(overlay_text, viewport));
@ -60,12 +58,11 @@ impl Backend {
for layer in layers {
self.flush(
gl,
viewport,
scale_factor,
transformation,
viewport.scale_factor() as f32,
projection,
&layer,
width,
height,
viewport_size.width,
viewport_size.height,
);
}
@ -75,19 +72,18 @@ impl Backend {
fn flush(
&mut self,
gl: &glow::Context,
viewport: &Viewport,
scale_factor: f32,
transformation: Transformation,
layer: &Layer<'_>,
target_width: u32,
target_height: u32,
) {
let bounds = layer.bounds * scale_factor;
let bounds = (layer.bounds * scale_factor).round();
if !layer.quads.is_empty() {
self.quad_pipeline.draw(
gl,
viewport,
target_height,
&layer.quads,
transformation,
scale_factor,
@ -175,8 +171,7 @@ impl Backend {
transformation,
glow_glyph::Region {
x: bounds.x,
y: viewport.height()
- (bounds.y + bounds.height).min(viewport.height()),
y: target_height - (bounds.y + bounds.height),
width: bounds.width,
height: bounds.height,
},

View file

@ -1,4 +1,4 @@
use crate::{Transformation, Viewport};
use crate::Transformation;
use glow::HasContext;
use iced_graphics::layer;
use iced_native::Rectangle;
@ -54,7 +54,7 @@ impl Pipeline {
pub fn draw(
&mut self,
gl: &glow::Context,
viewport: &Viewport,
target_height: u32,
instances: &[layer::Quad],
transformation: Transformation,
scale: f32,
@ -64,9 +64,7 @@ impl Pipeline {
gl.enable(glow::SCISSOR_TEST);
gl.scissor(
bounds.x as i32,
(viewport.height()
- (bounds.y + bounds.height).min(viewport.height()))
as i32,
(target_height - (bounds.y + bounds.height)) as i32,
bounds.width as i32,
bounds.height as i32,
);

View file

@ -13,11 +13,11 @@ pub struct Compositor {
gl: Option<glow::Context>,
}
impl iced_native::window::Compositor for Compositor {
impl iced_graphics::window::Compositor for Compositor {
type Settings = Settings;
type Renderer = Renderer;
type Surface = ();
type SwapChain = Viewport;
type SwapChain = ();
fn new(_settings: Self::Settings) -> Self {
let connection = surfman::Connection::new().expect("Create connection");
@ -133,16 +133,14 @@ impl iced_native::window::Compositor for Compositor {
gl.enable(glow::BLEND);
gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);
}
Viewport::new(width, height)
}
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,
scale_factor: f64,
overlay: &[T],
) -> mouse::Interaction {
let gl = self.gl.as_ref().unwrap();
@ -151,13 +149,7 @@ impl iced_native::window::Compositor for Compositor {
gl.clear(glow::COLOR_BUFFER_BIT);
}
let mouse = renderer.backend_mut().draw(
gl,
swap_chain,
output,
scale_factor,
overlay,
);
let mouse = renderer.backend_mut().draw(gl, viewport, output, overlay);
{
let mut surface = self