Skip to content

Commit

Permalink
graphics as struct
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Sep 1, 2024
1 parent 0f60160 commit 0d4a7e4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 99 deletions.
8 changes: 4 additions & 4 deletions sugarloaf/src/components/rich_text/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
// Eventually the file had updates to support other features like background-color,
// text color, underline color and etc.

use crate::Graphics;
use crate::sugarloaf::graphics::GraphicRenderRequest;
use crate::components::rich_text::batch::BatchManager;
pub use crate::components::rich_text::batch::{DisplayList, Rect, Vertex};
use crate::components::rich_text::image_cache::glyph::{GlyphCacheSession, GlyphEntry};
pub use crate::components::rich_text::image_cache::{AddImage, ImageId, ImageLocation};
use crate::components::rich_text::image_cache::{ImageCache, ImageData};
use crate::components::rich_text::text::*;
use crate::components::rich_text::GraphicsDataBrush;
use crate::components::rich_text::RenderMediaRequest;
use crate::layout::{FragmentStyleDecoration, Line, SugarDimensions, UnderlineShape};
use crate::SugarCursor;
use crate::{Graphic, GraphicData, GraphicId};
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Compositor {
rect: &SugarDimensions,
line: Line,
last_rendered_graphic: &mut Option<GraphicId>,
render_media_requests: &mut Vec<RenderMediaRequest>,
graphics: &mut Graphics,
) {
let mut px = px;
let subpx_bias = (0.125, 0.);
Expand All @@ -216,7 +216,7 @@ impl Compositor {

for graphic in &cached_run.graphics {
if *last_rendered_graphic != Some(graphic.id) {
render_media_requests.push(RenderMediaRequest {
graphics.top_layer.push(GraphicRenderRequest {
id: graphic.id,
pos_x: run_x - (graphic.offset_x as f32),
pos_y: topline - (graphic.offset_y as f32),
Expand Down
46 changes: 7 additions & 39 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod image_cache;
pub mod text;
pub mod util;

use crate::sugarloaf::graphics::GraphicRenderRequest;
use crate::Graphics;
use crate::components::core::orthographic_projection;
use crate::components::rich_text::image_cache::{GlyphCache, ImageCache, ImageId};
use crate::context::Context;
Expand Down Expand Up @@ -35,15 +37,6 @@ pub const BLEND: Option<wgpu::BlendState> = Some(wgpu::BlendState {
},
});

#[derive(Debug)]
pub struct RenderMediaRequest {
pub id: GraphicId,
pub pos_x: f32,
pub pos_y: f32,
pub width: Option<f32>,
pub height: Option<f32>,
}

pub struct RichTextBrush {
vertex_buffer: wgpu::Buffer,
constant_bind_group: wgpu::BindGroup,
Expand All @@ -61,16 +54,6 @@ pub struct RichTextBrush {
textures_version: usize,
images: ImageCache,
glyphs: GlyphCache,
pub render_media_requests: Vec<RenderMediaRequest>,
}

#[derive(Copy, Clone)]
pub struct GraphicsDataBrush {
image_id: ImageId,
coords: [f32; 4],
has_alpha: bool,
width: usize,
height: usize,
}

impl RichTextBrush {
Expand Down Expand Up @@ -246,7 +229,6 @@ impl RichTextBrush {
});

RichTextBrush {
render_media_requests: Vec::new(),
layout_bind_group,
layout_bind_group_layout,
constant_bind_group,
Expand Down Expand Up @@ -276,6 +258,7 @@ impl RichTextBrush {
&mut self,
context: &mut crate::context::Context,
state: &crate::sugarloaf::state::SugarState,
graphics: &mut Graphics,
) {
// let start = std::time::Instant::now();

Expand All @@ -284,7 +267,6 @@ impl RichTextBrush {
return;
}

self.render_media_requests.clear();
// Render
self.comp.begin();

Expand All @@ -302,7 +284,7 @@ impl RichTextBrush {
state.current.layout.style.screen_position,
font_library,
&state.current.layout.dimensions,
&mut self.render_media_requests,
graphics,
);
self.draw_layout_cache.clear_on_demand();

Expand Down Expand Up @@ -485,7 +467,7 @@ fn draw_layout(
pos: (f32, f32),
font_library: &FontLibraryData,
rect: &SugarDimensions,
render_media_requests: &mut Vec<RenderMediaRequest>,
graphics: &mut Graphics,
) {
// let start = std::time::Instant::now();
let (x, y) = pos;
Expand Down Expand Up @@ -524,8 +506,7 @@ fn draw_layout(
rect,
line,
&mut last_rendered_graphic,
render_media_requests,
// graphics,
graphics,
);
continue;
}
Expand Down Expand Up @@ -585,30 +566,17 @@ fn draw_layout(

if let Some(graphic) = run.media() {
if last_rendered_graphic != Some(graphic.id) {
// if let Some(image_data) = graphics.get(&graphic.id) {
let offset_x = graphic.offset_x as f32;
let offset_y = graphic.offset_y as f32;

render_media_requests.push(RenderMediaRequest {
graphics.top_layer.push(GraphicRenderRequest {
id: graphic.id,
pos_x: run_x - offset_x,
pos_y: style.topline - offset_y,
width: None,
height: None,
});
// comp.draw_image_from_data(
// Rect::new(
// run_x - offset_x,
// style.topline - offset_y,
// image_data.width as f32,
// image_data.height as f32,
// ),
// &image_data.coords,
// image_data.has_alpha,
// );

last_rendered_graphic = Some(graphic.id);
// }
}

cached_run.graphics.insert(graphic);
Expand Down
64 changes: 21 additions & 43 deletions sugarloaf/src/sugarloaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::components::rich_text::RichTextBrush;
use crate::components::text;
use crate::font::{fonts::SugarloafFont, FontLibrary};
use crate::layout::SugarloafLayout;
use crate::sugarloaf::graphics::{GraphicData, GraphicId};
use crate::sugarloaf::graphics::{GraphicData, GraphicId, Graphics};
use crate::sugarloaf::layer::types;
use crate::{context::Context, Content, Object};
use ab_glyph::{self, PxScale};
Expand All @@ -22,12 +22,6 @@ use raw_window_handle::{
use rustc_hash::FxHashMap;
use state::SugarState;

pub struct GraphicDataEntry {
handle: Handle,
width: f32,
height: f32,
}

pub struct Sugarloaf<'a> {
pub ctx: Context<'a>,
text_brush: text::GlyphBrush<()>,
Expand All @@ -36,8 +30,8 @@ pub struct Sugarloaf<'a> {
rich_text_brush: RichTextBrush,
state: state::SugarState,
pub background_color: wgpu::Color,
pub background_image: Option<types::Image>,
graphics: FxHashMap<GraphicId, GraphicDataEntry>,
pub background_image: Option<ImageProperties>,
graphics: Graphics,
}

#[derive(Debug)]
Expand Down Expand Up @@ -147,7 +141,7 @@ impl Sugarloaf<'_> {
rect_brush,
rich_text_brush,
text_brush,
graphics: FxHashMap::default(),
graphics: Graphics::default(),
};

Ok(instance)
Expand Down Expand Up @@ -195,16 +189,16 @@ impl Sugarloaf<'_> {

#[inline]
pub fn set_background_image(&mut self, image: &ImageProperties) -> &mut Self {
let handle = Handle::from_path(image.path.to_owned());
self.background_image = Some(layer::types::Image::Raster {
handle,
bounds: Rectangle {
width: image.width,
height: image.height,
x: image.x,
y: image.y,
},
});
self.background_image = Some(image.clone());
// Some(layer::types::Image::Raster {
// handle,
// bounds: Rectangle {
// width: image.width,
// height: image.height,
// x: image.x,
// y: image.y,
// },
// });
self
}

Expand Down Expand Up @@ -242,22 +236,7 @@ impl Sugarloaf<'_> {

#[inline]
pub fn add_graphic(&mut self, graphic_data: GraphicData) {
if self.graphics.contains_key(&graphic_data.id) {
return;
}

self.graphics.insert(
graphic_data.id,
GraphicDataEntry {
handle: Handle::from_pixels(
graphic_data.width as u32,
graphic_data.height as u32,
graphic_data.pixels,
),
width: graphic_data.width as f32,
height: graphic_data.height as f32,
},
);
self.graphics.insert(graphic_data);
}

#[inline]
Expand All @@ -280,6 +259,7 @@ impl Sugarloaf<'_> {
&mut self.text_brush,
&mut self.rect_brush,
&mut self.ctx,
&mut self.graphics,
) {
self.clean_state();
return;
Expand All @@ -303,9 +283,8 @@ impl Sugarloaf<'_> {
// );
// }

let graphic_requests = self.rich_text_brush.render_media_requests.len();
if graphic_requests > 0 {
for request in &self.rich_text_brush.render_media_requests {
if self.graphics.has_graphics() {
for request in &self.graphics.top_layer {
if let Some(entry) = self.graphics.get(&request.id) {
self.layer_brush.prepare_with_handle(
&mut encoder,
Expand Down Expand Up @@ -342,7 +321,7 @@ impl Sugarloaf<'_> {
self.rich_text_brush
.render(&mut self.ctx, &self.state, &mut rpass);

for request in 0..graphic_requests {
for request in 0..self.graphics.top_layer.len() {
self.layer_brush.render(request, &mut rpass, None);
}

Expand All @@ -352,11 +331,10 @@ impl Sugarloaf<'_> {
self.text_brush.render(&mut self.ctx, &mut rpass);
}

// if self.background_image.is_some() {
if !self.rich_text_brush.render_media_requests.is_empty() {
if self.graphics.has_graphics() {
self.layer_brush.end_frame();
self.graphics.clear();
}
// }

self.ctx.queue.submit(Some(encoder.finish()));
frame.present();
Expand Down
53 changes: 41 additions & 12 deletions sugarloaf/src/sugarloaf/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,66 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use crate::sugarloaf::Handle;
use rustc_hash::FxHashMap;

pub struct GraphicDataEntry {
pub handle: Handle,
pub width: f32,
pub height: f32,
}

#[derive(Debug)]
pub struct GraphicRenderRequest {
pub id: GraphicId,
pub pos_x: f32,
pub pos_y: f32,
pub width: Option<f32>,
pub height: Option<f32>,
}

#[derive(Default)]
pub struct Graphics {
inner: FxHashMap<GraphicId, GraphicData>,
inner: FxHashMap<GraphicId, GraphicDataEntry>,
pub bottom_layer: Vec<GraphicRenderRequest>,
pub top_layer: Vec<GraphicRenderRequest>,
}

impl Graphics {
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
pub fn has_graphics(&self) -> bool {
!self.top_layer.is_empty() || !self.bottom_layer.is_empty()
}

#[inline]
pub fn get_mut(&mut self, id: &GraphicId) -> Option<&mut GraphicData> {
self.inner.get_mut(id)
pub fn clear(&mut self) {
self.top_layer.clear();
self.bottom_layer.clear();
}

#[inline]
pub fn get(&self, id: &GraphicId) -> Option<&GraphicData> {
pub fn get(&self, id: &GraphicId) -> Option<&GraphicDataEntry> {
self.inner.get(id)
}

#[inline]
pub fn keys(&self) -> Vec<GraphicId> {
self.inner.keys().cloned().collect::<Vec<_>>()
}
pub fn insert(&mut self, graphic_data: GraphicData) {
if self.inner.contains_key(&graphic_data.id) {
return;
}

#[inline]
pub fn add(&mut self, graphic_data: GraphicData) {
self.inner.entry(graphic_data.id).or_insert(graphic_data);
self.inner.insert(
graphic_data.id,
GraphicDataEntry {
handle: Handle::from_pixels(
graphic_data.width as u32,
graphic_data.height as u32,
graphic_data.pixels,
),
width: graphic_data.width as f32,
height: graphic_data.height as f32,
},
);
}

#[inline]
Expand Down
Loading

0 comments on commit 0d4a7e4

Please sign in to comment.