From c7ff67d0fca9698fbfe7ece93af974a41e50d76c Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 6 Jan 2021 10:19:39 +0100 Subject: [PATCH] Move the ItemRenderer to the item_rendering module --- sixtyfps_runtime/corelib/graphics.rs | 2 +- sixtyfps_runtime/corelib/item_rendering.rs | 40 ++++++++++++++--- sixtyfps_runtime/corelib/items.rs | 44 ++++--------------- sixtyfps_runtime/corelib/items/image.rs | 3 +- sixtyfps_runtime/corelib/items/text.rs | 4 +- sixtyfps_runtime/rendering_backends/gl/lib.rs | 4 +- .../rendering_backends/qt/widgets.rs | 4 +- 7 files changed, 50 insertions(+), 51 deletions(-) diff --git a/sixtyfps_runtime/corelib/graphics.rs b/sixtyfps_runtime/corelib/graphics.rs index f7a6c8275..1eaea29b9 100644 --- a/sixtyfps_runtime/corelib/graphics.rs +++ b/sixtyfps_runtime/corelib/graphics.rs @@ -289,7 +289,7 @@ pub type RenderingCache = vec_arena::Arena>; /// 1. A series of low-level rendering primitives can be rendered into a frame, that's started using [GraphicsBackend::new_frame]. /// The low-level rendering primitives are intended to be fast and ready for rendering. pub trait GraphicsBackend: Sized { - type ItemRenderer: crate::items::ItemRenderer; + type ItemRenderer: crate::item_rendering::ItemRenderer; fn new_renderer(&mut self, clear_color: &Color) -> Self::ItemRenderer; fn flush_renderer(&mut self, renderer: Self::ItemRenderer); diff --git a/sixtyfps_runtime/corelib/item_rendering.rs b/sixtyfps_runtime/corelib/item_rendering.rs index fb518cc08..20ba6b5d5 100644 --- a/sixtyfps_runtime/corelib/item_rendering.rs +++ b/sixtyfps_runtime/corelib/item_rendering.rs @@ -11,10 +11,12 @@ LICENSE END */ //! module for rendering the tree of items use super::graphics::{GraphicsBackend, RenderingCache}; -use super::items::ItemRef; +use super::items::*; use crate::component::ComponentRc; +use crate::graphics::{Point, Rect}; use crate::item_tree::ItemVisitorResult; use crate::slice::Slice; +use core::pin::Pin; use std::cell::{Cell, RefCell}; /// This structure must be present in items that are Rendered and contains information. @@ -61,8 +63,6 @@ pub(crate) fn render_component_items( renderer: &mut Backend::ItemRenderer, origin: crate::graphics::Point, ) { - use crate::items::ItemRenderer; - let renderer = RefCell::new(renderer); crate::item_tree::visit_items_with_post_visit( @@ -71,10 +71,8 @@ pub(crate) fn render_component_items( |_, item, _, translation| { let saved_clip = renderer.borrow_mut().clip_rects(); - item.as_ref().render( - *translation, - &mut (*renderer.borrow_mut() as &mut dyn crate::items::ItemRenderer), - ); + item.as_ref() + .render(*translation, &mut (*renderer.borrow_mut() as &mut dyn ItemRenderer)); let origin = item.as_ref().geometry().origin; let translation = *translation + euclid::Vector2D::new(origin.x, origin.y); @@ -97,3 +95,31 @@ pub(crate) fn free_item_rendering_data<'a, Backend: GraphicsBackend>( renderer.borrow().release_item_graphics_cache(cached_rendering_data) } } + +pub trait ItemRenderer { + /// will draw a rectangle in (pos.x + rect.x) + fn draw_rectangle(&mut self, pos: Point, rect: Pin<&Rectangle>); + fn draw_border_rectangle(&mut self, pos: Point, rect: Pin<&BorderRectangle>); + fn draw_image(&mut self, pos: Point, image: Pin<&Image>); + fn draw_clipped_image(&mut self, pos: Point, image: Pin<&ClippedImage>); + fn draw_text(&mut self, pos: Point, text: Pin<&Text>); + fn draw_text_input(&mut self, pos: Point, text_input: Pin<&TextInput>); + fn draw_path(&mut self, pos: Point, path: Pin<&Path>); + fn combine_clip(&mut self, pos: Point, clip: &Pin<&Clip>); + fn clip_rects(&self) -> SharedVector; + fn reset_clip(&mut self, rects: SharedVector); + + /// Returns the scale factor + fn scale_factor(&self) -> f32; + + /// Draw a pixmap in position indicated by the `pos`. + /// The pixmap will be taken from cache if the cache is valid, otherwise, update_fn will be called + /// with a callback that need to be called once with `fn (width, height, data)` where data are the + /// argb premultiplied pixel values + fn draw_cached_pixmap( + &mut self, + item_cache: &CachedRenderingData, + pos: Point, + update_fn: &dyn Fn(&mut dyn FnMut(u32, u32, &[u8])), + ); +} diff --git a/sixtyfps_runtime/corelib/items.rs b/sixtyfps_runtime/corelib/items.rs index 19e767a54..490ed2902 100644 --- a/sixtyfps_runtime/corelib/items.rs +++ b/sixtyfps_runtime/corelib/items.rs @@ -45,37 +45,9 @@ pub use text::*; mod image; pub use self::image::*; -pub trait ItemRenderer { - /// will draw a rectangle in (pos.x + rect.x) - fn draw_rectangle(&mut self, pos: Point, rect: Pin<&Rectangle>); - fn draw_border_rectangle(&mut self, pos: Point, rect: Pin<&BorderRectangle>); - fn draw_image(&mut self, pos: Point, image: Pin<&Image>); - fn draw_clipped_image(&mut self, pos: Point, image: Pin<&ClippedImage>); - fn draw_text(&mut self, pos: Point, text: Pin<&Text>); - fn draw_text_input(&mut self, pos: Point, text_input: Pin<&TextInput>); - fn draw_path(&mut self, pos: Point, path: Pin<&Path>); - fn combine_clip(&mut self, pos: Point, clip: &Pin<&Clip>); - fn clip_rects(&self) -> SharedVector; - fn reset_clip(&mut self, rects: SharedVector); - - /// Returns the scale factor - fn scale_factor(&self) -> f32; - - /// Draw a pixmap in position indicated by the `pos`. - /// The pixmap will be taken from cache if the cache is valid, otherwise, update_fn will be called - /// with a callback that need to be called once with `fn (width, height, data)` where data are the - /// argb premultiplied pixel values - fn draw_cached_pixmap( - &mut self, - item_cache: &CachedRenderingData, - pos: Point, - update_fn: &dyn Fn(&mut dyn FnMut(u32, u32, &[u8])), - ); -} - /// Alias for `&mut dyn ItemRenderer`. Required so cbingen generates the ItemVTable /// despite the presence of trait object -type ItemRendererRef<'a> = &'a mut dyn ItemRenderer; +type ItemRendererRef<'a> = &'a mut dyn crate::item_rendering::ItemRenderer; /// Items are the nodes in the render tree. #[vtable] @@ -207,7 +179,7 @@ impl Item for Rectangle { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) { + fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) { (*backend).draw_rectangle(pos, self) } } @@ -272,7 +244,7 @@ impl Item for BorderRectangle { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) { + fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) { (*backend).draw_border_rectangle(pos, self) } } @@ -384,7 +356,7 @@ impl Item for TouchArea { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, _pos: Point, _backend: &mut &mut dyn ItemRenderer) {} + fn render(self: Pin<&Self>, _pos: Point, _backend: &mut ItemRendererRef) {} } impl ItemConsts for TouchArea { @@ -443,7 +415,7 @@ impl Item for Clip { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) { + fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) { (*backend).combine_clip(pos, &self) } } @@ -506,7 +478,7 @@ impl Item for Path { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, pos: Point, backend: &mut &mut dyn ItemRenderer) { + fn render(self: Pin<&Self>, pos: Point, backend: &mut ItemRendererRef) { (*backend).draw_path(pos, self) } } @@ -580,7 +552,7 @@ impl Item for Flickable { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, _pos: Point, _backend: &mut &mut dyn ItemRenderer) {} + fn render(self: Pin<&Self>, _pos: Point, _backend: &mut ItemRendererRef) {} } impl ItemConsts for Flickable { @@ -686,7 +658,7 @@ impl Item for Window { fn focus_event(self: Pin<&Self>, _: &FocusEvent, _window: &ComponentWindow) {} - fn render(self: Pin<&Self>, _pos: Point, _backend: &mut &mut dyn ItemRenderer) {} + fn render(self: Pin<&Self>, _pos: Point, _backend: &mut ItemRendererRef) {} } impl ItemConsts for Window { diff --git a/sixtyfps_runtime/corelib/items/image.rs b/sixtyfps_runtime/corelib/items/image.rs index d1970cc12..af52aa512 100644 --- a/sixtyfps_runtime/corelib/items/image.rs +++ b/sixtyfps_runtime/corelib/items/image.rs @@ -19,11 +19,12 @@ When adding an item or a property, it needs to be kept in sync with different pl - For the C++ code (new item only): the cbindgen.rs to export the new item, and the `using` declaration in sixtyfps.h - Don't forget to update the documentation */ -use super::{Item, ItemConsts, ItemRc, ItemRenderer}; +use super::{Item, ItemConsts, ItemRc}; use crate::eventloop::ComponentWindow; use crate::graphics::{Point, Rect, Resource}; use crate::input::{FocusEvent, InputEventResult, KeyEvent, KeyEventResult, MouseEvent}; use crate::item_rendering::CachedRenderingData; +use crate::item_rendering::ItemRenderer; use crate::layout::LayoutInfo; #[cfg(feature = "rtti")] use crate::rtti::*; diff --git a/sixtyfps_runtime/corelib/items/text.rs b/sixtyfps_runtime/corelib/items/text.rs index 49e3e0f7a..7a3ec1dae 100644 --- a/sixtyfps_runtime/corelib/items/text.rs +++ b/sixtyfps_runtime/corelib/items/text.rs @@ -20,7 +20,7 @@ When adding an item or a property, it needs to be kept in sync with different pl - Don't forget to update the documentation */ -use super::{Item, ItemConsts, ItemRc, ItemRenderer}; +use super::{Item, ItemConsts, ItemRc}; use crate::eventloop::ComponentWindow; use crate::font::HasFont; use crate::graphics::{Color, Point, Rect}; @@ -28,7 +28,7 @@ use crate::input::{ FocusEvent, InputEventResult, KeyEvent, KeyEventResult, KeyboardModifiers, MouseEvent, MouseEventType, }; -use crate::item_rendering::CachedRenderingData; +use crate::item_rendering::{CachedRenderingData, ItemRenderer}; use crate::layout::LayoutInfo; #[cfg(feature = "rtti")] use crate::rtti::*; diff --git a/sixtyfps_runtime/rendering_backends/gl/lib.rs b/sixtyfps_runtime/rendering_backends/gl/lib.rs index 7dfefb855..6894b803a 100644 --- a/sixtyfps_runtime/rendering_backends/gl/lib.rs +++ b/sixtyfps_runtime/rendering_backends/gl/lib.rs @@ -10,12 +10,12 @@ LICENSE END */ use std::{cell::RefCell, rc::Rc}; +use sixtyfps_corelib::eventloop::ComponentWindow; use sixtyfps_corelib::graphics::{ Color, GraphicsBackend, GraphicsWindow, Point, Rect, RenderingCache, Resource, }; -use sixtyfps_corelib::item_rendering::CachedRenderingData; +use sixtyfps_corelib::item_rendering::{CachedRenderingData, ItemRenderer}; use sixtyfps_corelib::items::Item; -use sixtyfps_corelib::{eventloop::ComponentWindow, items::ItemRenderer}; use sixtyfps_corelib::{Property, SharedVector}; type CanvasRc = Rc>>; diff --git a/sixtyfps_runtime/rendering_backends/qt/widgets.rs b/sixtyfps_runtime/rendering_backends/qt/widgets.rs index cd67970ce..9830cfc9b 100644 --- a/sixtyfps_runtime/rendering_backends/qt/widgets.rs +++ b/sixtyfps_runtime/rendering_backends/qt/widgets.rs @@ -32,8 +32,8 @@ use sixtyfps_corelib::graphics::{Point, Rect}; use sixtyfps_corelib::input::{ FocusEvent, InputEventResult, KeyEvent, KeyEventResult, MouseEvent, MouseEventType, }; -use sixtyfps_corelib::item_rendering::CachedRenderingData; -use sixtyfps_corelib::items::{Item, ItemConsts, ItemRc, ItemRenderer, ItemVTable}; +use sixtyfps_corelib::item_rendering::{CachedRenderingData, ItemRenderer}; +use sixtyfps_corelib::items::{Item, ItemConsts, ItemRc, ItemVTable}; use sixtyfps_corelib::layout::LayoutInfo; use sixtyfps_corelib::rtti::*; use sixtyfps_corelib::{Callback, ItemVTable_static, Property, SharedString, SharedVector};