2020-05-19 17:15:44 +02:00
|
|
|
use crate::quad;
|
|
|
|
|
use crate::text;
|
|
|
|
|
use crate::triangle;
|
2020-05-20 20:28:35 +02:00
|
|
|
use crate::{Settings, Transformation};
|
2020-05-19 17:15:44 +02:00
|
|
|
use iced_graphics::backend;
|
2020-05-19 20:30:46 +02:00
|
|
|
use iced_graphics::font;
|
2020-05-19 22:55:12 +02:00
|
|
|
use iced_graphics::layer::Layer;
|
2020-05-20 20:28:35 +02:00
|
|
|
use iced_graphics::{Primitive, Viewport};
|
2020-05-19 17:15:44 +02:00
|
|
|
use iced_native::mouse;
|
2020-05-19 22:55:12 +02:00
|
|
|
use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};
|
2020-02-28 14:38:42 +01:00
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(any(feature = "image_rs", feature = "svg"))]
|
2020-05-19 22:55:12 +02:00
|
|
|
use crate::image;
|
2020-02-28 14:38:42 +01:00
|
|
|
|
2020-05-28 01:37:59 +02:00
|
|
|
/// A [`wgpu`] graphics backend for [`iced`].
|
2019-11-22 22:14:24 +01:00
|
|
|
///
|
|
|
|
|
/// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs
|
2020-05-28 01:37:59 +02:00
|
|
|
/// [`iced`]: https://github.com/hecrj/iced
|
2019-11-22 22:14:24 +01:00
|
|
|
#[derive(Debug)]
|
2020-05-19 17:15:44 +02:00
|
|
|
pub struct Backend {
|
2019-10-07 03:56:16 +02:00
|
|
|
quad_pipeline: quad::Pipeline,
|
2019-11-13 03:54:36 +01:00
|
|
|
text_pipeline: text::Pipeline,
|
2020-02-28 14:38:42 +01:00
|
|
|
triangle_pipeline: triangle::Pipeline,
|
|
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(any(feature = "image_rs", feature = "svg"))]
|
2020-02-28 14:38:42 +01:00
|
|
|
image_pipeline: image::Pipeline,
|
2020-06-19 00:08:28 +02:00
|
|
|
|
|
|
|
|
default_text_size: u16,
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-19 17:15:44 +02:00
|
|
|
impl Backend {
|
2020-05-30 03:03:59 +02:00
|
|
|
/// Creates a new [`Backend`].
|
2021-08-01 20:38:34 +02:00
|
|
|
pub fn new(
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
settings: Settings,
|
|
|
|
|
format: wgpu::TextureFormat,
|
|
|
|
|
) -> Self {
|
2021-07-22 18:21:50 +07:00
|
|
|
let text_pipeline = text::Pipeline::new(
|
|
|
|
|
device,
|
2021-08-01 20:38:34 +02:00
|
|
|
format,
|
2021-07-22 18:21:50 +07:00
|
|
|
settings.default_font,
|
|
|
|
|
settings.text_multithreading,
|
|
|
|
|
);
|
|
|
|
|
|
2021-08-01 20:38:34 +02:00
|
|
|
let quad_pipeline = quad::Pipeline::new(device, format);
|
|
|
|
|
let triangle_pipeline =
|
|
|
|
|
triangle::Pipeline::new(device, format, settings.antialiasing);
|
2019-10-07 03:56:16 +02:00
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(any(feature = "image_rs", feature = "svg"))]
|
2021-08-01 20:38:34 +02:00
|
|
|
let image_pipeline = image::Pipeline::new(device, format);
|
2020-02-28 14:38:42 +01:00
|
|
|
|
2019-10-05 19:22:51 +02:00
|
|
|
Self {
|
2019-10-07 03:56:16 +02:00
|
|
|
quad_pipeline,
|
2019-11-11 06:07:31 +01:00
|
|
|
text_pipeline,
|
2020-01-02 19:25:00 +01:00
|
|
|
triangle_pipeline,
|
2020-02-28 14:38:42 +01:00
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(any(feature = "image_rs", feature = "svg"))]
|
2020-02-28 14:38:42 +01:00
|
|
|
image_pipeline,
|
2020-06-19 00:08:28 +02:00
|
|
|
|
|
|
|
|
default_text_size: settings.default_text_size,
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-30 03:03:59 +02:00
|
|
|
/// Draws the provided primitives in the given `TextureView`.
|
2020-02-09 03:25:13 +01:00
|
|
|
///
|
2020-05-30 03:03:59 +02:00
|
|
|
/// The text provided as overlay will be rendered on top of the primitives.
|
2020-02-09 03:25:13 +01:00
|
|
|
/// This is useful for rendering debug information.
|
|
|
|
|
pub fn draw<T: AsRef<str>>(
|
2019-10-09 05:36:49 +02:00
|
|
|
&mut self,
|
2020-04-16 09:06:05 +03:00
|
|
|
device: &wgpu::Device,
|
2020-08-27 13:03:42 +02:00
|
|
|
staging_belt: &mut wgpu::util::StagingBelt,
|
2020-02-09 03:25:13 +01:00
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
2020-05-20 20:28:35 +02:00
|
|
|
frame: &wgpu::TextureView,
|
|
|
|
|
viewport: &Viewport,
|
2020-04-30 08:16:38 +02:00
|
|
|
(primitive, mouse_interaction): &(Primitive, mouse::Interaction),
|
2020-05-19 22:55:12 +02:00
|
|
|
overlay_text: &[T],
|
2020-04-30 08:16:38 +02:00
|
|
|
) -> mouse::Interaction {
|
2019-10-07 04:05:40 +02:00
|
|
|
log::debug!("Drawing");
|
|
|
|
|
|
2020-05-20 20:28:35 +02:00
|
|
|
let target_size = viewport.physical_size();
|
|
|
|
|
let scale_factor = viewport.scale_factor() as f32;
|
|
|
|
|
let transformation = viewport.projection();
|
2019-10-05 19:22:51 +02:00
|
|
|
|
2020-05-20 20:28:35 +02:00
|
|
|
let mut layers = Layer::generate(primitive, viewport);
|
|
|
|
|
layers.push(Layer::overlay(overlay_text, viewport));
|
2019-10-29 02:00:17 +01:00
|
|
|
|
|
|
|
|
for layer in layers {
|
2020-01-09 03:56:13 +01:00
|
|
|
self.flush(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2020-01-09 03:56:13 +01:00
|
|
|
scale_factor,
|
|
|
|
|
transformation,
|
|
|
|
|
&layer,
|
2020-08-27 13:03:42 +02:00
|
|
|
staging_belt,
|
2020-02-09 03:25:13 +01:00
|
|
|
encoder,
|
2020-05-20 20:28:35 +02:00
|
|
|
&frame,
|
|
|
|
|
target_size.width,
|
|
|
|
|
target_size.height,
|
2020-01-09 03:56:13 +01:00
|
|
|
);
|
2019-10-29 02:00:17 +01:00
|
|
|
}
|
2019-10-23 01:21:23 +02:00
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(any(feature = "image_rs", feature = "svg"))]
|
2019-11-30 02:55:14 +01:00
|
|
|
self.image_pipeline.trim_cache();
|
2019-10-05 19:22:51 +02:00
|
|
|
|
2020-04-30 08:16:38 +02:00
|
|
|
*mouse_interaction
|
2019-10-09 05:36:49 +02:00
|
|
|
}
|
2019-10-11 22:15:39 +02:00
|
|
|
|
2019-10-25 03:47:34 +02:00
|
|
|
fn flush(
|
|
|
|
|
&mut self,
|
2020-04-16 09:06:05 +03:00
|
|
|
device: &wgpu::Device,
|
2020-01-09 03:56:13 +01:00
|
|
|
scale_factor: f32,
|
2019-10-25 03:47:34 +02:00
|
|
|
transformation: Transformation,
|
2019-11-22 22:14:24 +01:00
|
|
|
layer: &Layer<'_>,
|
2020-08-27 13:03:42 +02:00
|
|
|
staging_belt: &mut wgpu::util::StagingBelt,
|
2019-10-25 03:47:34 +02:00
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
|
|
|
|
target: &wgpu::TextureView,
|
2020-02-15 10:08:27 +01:00
|
|
|
target_width: u32,
|
|
|
|
|
target_height: u32,
|
2019-10-25 03:47:34 +02:00
|
|
|
) {
|
2020-06-02 04:38:55 +02:00
|
|
|
let bounds = (layer.bounds * scale_factor).snap();
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2020-04-26 17:09:03 +02:00
|
|
|
if !layer.quads.is_empty() {
|
|
|
|
|
self.quad_pipeline.draw(
|
|
|
|
|
device,
|
2020-08-27 13:03:42 +02:00
|
|
|
staging_belt,
|
2020-04-26 17:09:03 +02:00
|
|
|
encoder,
|
|
|
|
|
&layer.quads,
|
|
|
|
|
transformation,
|
|
|
|
|
scale_factor,
|
|
|
|
|
bounds,
|
|
|
|
|
target,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 15:25:55 -07:00
|
|
|
if !layer.meshes.is_empty() {
|
2020-03-07 23:45:54 +01:00
|
|
|
let scaled = transformation
|
|
|
|
|
* Transformation::scale(scale_factor, scale_factor);
|
2020-01-02 19:25:00 +01:00
|
|
|
|
|
|
|
|
self.triangle_pipeline.draw(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2020-08-27 13:03:42 +02:00
|
|
|
staging_belt,
|
2020-01-02 19:25:00 +01:00
|
|
|
encoder,
|
|
|
|
|
target,
|
2020-02-15 10:08:27 +01:00
|
|
|
target_width,
|
|
|
|
|
target_height,
|
2020-03-07 23:45:54 +01:00
|
|
|
scaled,
|
2020-04-28 04:41:09 +02:00
|
|
|
scale_factor,
|
2020-01-02 19:25:00 +01:00
|
|
|
&layer.meshes,
|
|
|
|
|
);
|
2019-10-29 01:21:28 +01:00
|
|
|
}
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(any(feature = "image_rs", feature = "svg"))]
|
2020-02-28 14:38:42 +01:00
|
|
|
{
|
2020-03-29 15:04:11 +02:00
|
|
|
if !layer.images.is_empty() {
|
2020-03-07 23:45:54 +01:00
|
|
|
let scaled = transformation
|
|
|
|
|
* Transformation::scale(scale_factor, scale_factor);
|
2020-02-28 14:38:42 +01:00
|
|
|
|
|
|
|
|
self.image_pipeline.draw(
|
|
|
|
|
device,
|
2020-08-27 13:41:00 +02:00
|
|
|
staging_belt,
|
2020-02-28 14:38:42 +01:00
|
|
|
encoder,
|
|
|
|
|
&layer.images,
|
2020-03-07 23:45:54 +01:00
|
|
|
scaled,
|
2020-02-28 14:38:42 +01:00
|
|
|
bounds,
|
|
|
|
|
target,
|
|
|
|
|
scale_factor,
|
2019-11-05 05:26:20 +01:00
|
|
|
);
|
2020-02-28 14:38:42 +01:00
|
|
|
}
|
2019-12-06 16:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 15:25:55 -07:00
|
|
|
if !layer.text.is_empty() {
|
2019-10-27 03:11:54 +01:00
|
|
|
for text in layer.text.iter() {
|
2019-11-05 05:26:20 +01:00
|
|
|
// Target physical coordinates directly to avoid blurry text
|
2019-11-10 06:05:20 +01:00
|
|
|
let text = wgpu_glyph::Section {
|
2019-11-14 03:00:33 +01:00
|
|
|
// TODO: We `round` here to avoid rerasterizing text when
|
|
|
|
|
// its position changes slightly. This can make text feel a
|
|
|
|
|
// bit "jumpy". We may be able to do better once we improve
|
|
|
|
|
// our text rendering/caching pipeline.
|
2019-11-05 05:26:20 +01:00
|
|
|
screen_position: (
|
2020-05-19 22:55:12 +02:00
|
|
|
(text.bounds.x * scale_factor).round(),
|
|
|
|
|
(text.bounds.y * scale_factor).round(),
|
2019-11-05 05:26:20 +01:00
|
|
|
),
|
2020-01-09 03:56:13 +01:00
|
|
|
// TODO: Fix precision issues with some scale factors.
|
2019-11-14 03:00:33 +01:00
|
|
|
//
|
|
|
|
|
// The `ceil` here can cause some words to render on the
|
|
|
|
|
// same line when they should not.
|
|
|
|
|
//
|
|
|
|
|
// Ideally, `wgpu_glyph` should be able to compute layout
|
|
|
|
|
// using logical positions, and then apply the proper
|
2020-01-09 03:56:13 +01:00
|
|
|
// scaling when rendering. This would ensure that both
|
|
|
|
|
// measuring and rendering follow the same layout rules.
|
2019-11-14 03:00:33 +01:00
|
|
|
bounds: (
|
2020-05-19 22:55:12 +02:00
|
|
|
(text.bounds.width * scale_factor).ceil(),
|
|
|
|
|
(text.bounds.height * scale_factor).ceil(),
|
2019-11-14 03:00:33 +01:00
|
|
|
),
|
2020-05-29 02:00:28 +02:00
|
|
|
text: vec![wgpu_glyph::Text {
|
|
|
|
|
text: text.content,
|
|
|
|
|
scale: wgpu_glyph::ab_glyph::PxScale {
|
|
|
|
|
x: text.size * scale_factor,
|
|
|
|
|
y: text.size * scale_factor,
|
|
|
|
|
},
|
|
|
|
|
font_id: self.text_pipeline.find_font(text.font),
|
|
|
|
|
extra: wgpu_glyph::Extra {
|
|
|
|
|
color: text.color,
|
|
|
|
|
z: 0.0,
|
|
|
|
|
},
|
|
|
|
|
}],
|
2020-05-19 22:55:12 +02:00
|
|
|
layout: wgpu_glyph::Layout::default()
|
|
|
|
|
.h_align(match text.horizontal_alignment {
|
|
|
|
|
HorizontalAlignment::Left => {
|
|
|
|
|
wgpu_glyph::HorizontalAlign::Left
|
|
|
|
|
}
|
|
|
|
|
HorizontalAlignment::Center => {
|
|
|
|
|
wgpu_glyph::HorizontalAlign::Center
|
|
|
|
|
}
|
|
|
|
|
HorizontalAlignment::Right => {
|
|
|
|
|
wgpu_glyph::HorizontalAlign::Right
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.v_align(match text.vertical_alignment {
|
|
|
|
|
VerticalAlignment::Top => {
|
|
|
|
|
wgpu_glyph::VerticalAlign::Top
|
|
|
|
|
}
|
|
|
|
|
VerticalAlignment::Center => {
|
|
|
|
|
wgpu_glyph::VerticalAlign::Center
|
|
|
|
|
}
|
|
|
|
|
VerticalAlignment::Bottom => {
|
|
|
|
|
wgpu_glyph::VerticalAlign::Bottom
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
..Default::default()
|
2019-11-05 05:26:20 +01:00
|
|
|
};
|
|
|
|
|
|
2019-11-11 06:07:31 +01:00
|
|
|
self.text_pipeline.queue(text);
|
2019-10-27 03:11:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-13 03:54:36 +01:00
|
|
|
self.text_pipeline.draw_queued(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2020-08-27 13:03:42 +02:00
|
|
|
staging_belt,
|
2019-11-13 03:54:36 +01:00
|
|
|
encoder,
|
|
|
|
|
target,
|
|
|
|
|
transformation,
|
|
|
|
|
wgpu_glyph::Region {
|
|
|
|
|
x: bounds.x,
|
|
|
|
|
y: bounds.y,
|
|
|
|
|
width: bounds.width,
|
|
|
|
|
height: bounds.height,
|
|
|
|
|
},
|
|
|
|
|
);
|
2019-10-27 03:11:54 +01:00
|
|
|
}
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 17:15:44 +02:00
|
|
|
impl iced_graphics::Backend for Backend {
|
|
|
|
|
fn trim_measurements(&mut self) {
|
|
|
|
|
self.text_pipeline.trim_measurement_cache()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-11 06:07:31 +01:00
|
|
|
|
2020-05-19 17:15:44 +02:00
|
|
|
impl backend::Text for Backend {
|
2020-05-19 20:30:46 +02:00
|
|
|
const ICON_FONT: Font = font::ICONS;
|
|
|
|
|
const CHECKMARK_ICON: char = font::CHECKMARK_ICON;
|
2020-04-18 19:53:27 +02:00
|
|
|
const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON;
|
2020-05-19 17:15:44 +02:00
|
|
|
|
2020-06-19 00:08:28 +02:00
|
|
|
fn default_size(&self) -> u16 {
|
|
|
|
|
self.default_text_size
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 17:15:44 +02:00
|
|
|
fn measure(
|
|
|
|
|
&self,
|
|
|
|
|
contents: &str,
|
|
|
|
|
size: f32,
|
|
|
|
|
font: Font,
|
|
|
|
|
bounds: Size,
|
|
|
|
|
) -> (f32, f32) {
|
|
|
|
|
self.text_pipeline.measure(contents, size, font, bounds)
|
|
|
|
|
}
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 21:18:22 +03:00
|
|
|
#[cfg(feature = "image_rs")]
|
2020-05-19 17:15:44 +02:00
|
|
|
impl backend::Image for Backend {
|
|
|
|
|
fn dimensions(&self, handle: &iced_native::image::Handle) -> (u32, u32) {
|
|
|
|
|
self.image_pipeline.dimensions(handle)
|
2019-10-13 18:22:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-19 17:15:44 +02:00
|
|
|
#[cfg(feature = "svg")]
|
|
|
|
|
impl backend::Svg for Backend {
|
|
|
|
|
fn viewport_dimensions(
|
|
|
|
|
&self,
|
|
|
|
|
handle: &iced_native::svg::Handle,
|
|
|
|
|
) -> (u32, u32) {
|
|
|
|
|
self.image_pipeline.viewport_dimensions(handle)
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|