Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background image #2419

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 21 additions & 62 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
shlex = "1.1.0"
simple_moving_average = "0.1.2"
skia-safe = { version = "0.68.0", features = ["gl", "textlayout"] }
skia-safe = { version = "0.69.0", features = ["gl", "textlayout"] }
spin_sleep = "1.1.1"
strum = { version = "0.25.0", features = ["derive"] }
swash = "0.1.8"
Expand Down
21 changes: 18 additions & 3 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
};

use log::error;
use skia_safe::{Canvas, Point, Rect};
use skia_safe::{Canvas, Data, Image, Point, Rect};
use winit::event::Event;

use crate::{
Expand Down Expand Up @@ -93,6 +93,7 @@ pub struct Renderer {
cursor_renderer: CursorRenderer,
pub grid_renderer: GridRenderer,
current_mode: EditorMode,
background_image: Option<Image>,

rendered_windows: HashMap<u64, RenderedWindow>,
pub window_regions: Vec<WindowDrawDetails>,
Expand Down Expand Up @@ -124,6 +125,13 @@ impl Renderer {

let profiler = profiler::Profiler::new(12.0);

let image = if window_settings.background_image.is_empty() {
None
} else {
let skia_data =
Data::from_filename(std::path::Path::new(&window_settings.background_image));
skia_data.and_then(|data| Image::from_encoded(data))
};
mysty0 marked this conversation as resolved.
Show resolved Hide resolved
Renderer {
rendered_windows,
cursor_renderer,
Expand All @@ -133,6 +141,7 @@ impl Renderer {
profiler,
os_scale_factor,
user_scale_factor,
background_image: image,
}
}

Expand All @@ -148,12 +157,14 @@ impl Renderer {
self.cursor_renderer.prepare_frame()
}

pub fn draw_frame(&mut self, root_canvas: &Canvas, dt: f32) {
pub fn draw_frame(&mut self, root_canvas: &Canvas, dt: f32, width: f32, height: f32) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be able to use Canvas::base_layer_size instead of passing width and height by parameter?

(fwiw you can look up the things available in skia_safe on skia.org, e.g. [SkCanvas::getBaseLayerSize] corresponds to Canvas::base_layer_size. The docs for skia_safe are available directly by running cargo doc --open locally)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest renderer changes using base_layer_size makes things much more cleaner however it doesn't cover full screen. There is some space left in the bottom that isn't covered by the image.
image
I don't think it's very critical but it would be nice if image could cover the whole screen. Do you know how this can be achived?

tracy_zone!("renderer_draw_frame");
let default_background = self.grid_renderer.get_default_background();
let font_dimensions = self.grid_renderer.font_dimensions;

let transparency = { SETTINGS.get::<WindowSettings>().transparency };
let background_transparency =
{ SETTINGS.get::<WindowSettings>().background_transparency } * transparency;
root_canvas.clear(default_background.with_a((255.0 * transparency) as u8));
root_canvas.save();
root_canvas.reset_matrix();
Expand Down Expand Up @@ -184,13 +195,17 @@ impl Renderer {
let settings = SETTINGS.get::<RendererSettings>();
let mut floating_rects = Vec::new();

let screen_rect = Rect::from_xywh(0.0, 0.0, width, height);
self.window_regions = windows
.into_iter()
.map(|window| {
window.draw(
root_canvas,
&settings,
default_background.with_a((255.0 * transparency) as u8),
(transparency * 255.0) as u8,
default_background.with_a((255.0 * background_transparency) as u8),
self.background_image.as_ref(),
&screen_rect,
font_dimensions,
&mut floating_rects,
)
Expand Down
55 changes: 49 additions & 6 deletions src/renderer/rendered_window.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{cell::RefCell, ops::Range, rc::Rc, sync::Arc};

use skia_safe::{
canvas::SaveLayerRec,
canvas::{SaveLayerRec, SrcRectConstraint},
image_filters::blur,
scalar,
utils::shadow_utils::{draw_shadow, ShadowFlags},
BlendMode, Canvas, ClipOp, Color, Contains, Matrix, Paint, Path, Picture, PictureRecorder,
Point, Point3, Rect,
BlendMode, Canvas, ClipOp, Color, Contains, Image, Matrix, Paint, Path, Picture,
PictureRecorder, Point, Point3, Rect,
};

use crate::{
Expand Down Expand Up @@ -295,7 +295,7 @@ impl RenderedWindow {
);

let mut background_paint = Paint::default();
background_paint.set_blend_mode(BlendMode::Src);
background_paint.set_blend_mode(BlendMode::SrcOver);
background_paint.set_alpha(default_background.a());

let save_layer_rec = SaveLayerRec::default()
Expand All @@ -319,6 +319,7 @@ impl RenderedWindow {
canvas.draw_picture(background_picture, Some(matrix), None);
}
}

canvas.restore();
canvas.restore();

Expand Down Expand Up @@ -353,11 +354,44 @@ impl RenderedWindow {
.any(|line| line.borrow().has_transparency)
}

fn draw_window_background_image(
paint: &Paint,
canvas: &Canvas,
image: &Image,
window_rect: &Rect,
screen_rect: &Rect,
) {
let image_width = image.width() as f32;
let image_height = image.height() as f32;

// Calculate the scaling factors based on the full screen size
let scale_x = image_width / screen_rect.width();
let scale_y = image_height / screen_rect.height();

// Calculate the portion of the image that corresponds to the window's position
let src_x = scale_x * (window_rect.left() - screen_rect.left());
let src_y = scale_y * (window_rect.top() - screen_rect.top());
let src_width = scale_x * window_rect.width();
let src_height = scale_y * window_rect.height();

let src_rect = Rect::from_xywh(src_x, src_y, src_width, src_height);
mysty0 marked this conversation as resolved.
Show resolved Hide resolved

canvas.draw_image_rect(
image,
Some((&src_rect, SrcRectConstraint::Strict)),
&window_rect,
&paint,
);
}

pub fn draw(
&mut self,
root_canvas: &Canvas,
settings: &RendererSettings,
transparency: u8,
default_background: Color,
background_image: Option<&Image>,
screen_rect: &Rect,
font_dimensions: Dimensions,
previous_floating_rects: &mut Vec<Rect>,
) -> WindowDrawDetails {
Expand Down Expand Up @@ -418,7 +452,7 @@ impl RenderedWindow {
) {
let paint = Paint::default()
.set_anti_alias(false)
.set_blend_mode(BlendMode::Src)
.set_blend_mode(BlendMode::Overlay)
.to_owned();
let save_layer_rec = SaveLayerRec::default()
.backdrop(&blur)
Expand All @@ -431,7 +465,7 @@ impl RenderedWindow {

let paint = Paint::default()
.set_anti_alias(false)
.set_color(Color::from_argb(255, 255, 255, default_background.a()))
.set_color(Color::from_argb(transparency, 255, 255, 255))
.set_blend_mode(if self.anchor_info.is_some() {
BlendMode::SrcOver
} else {
Expand All @@ -441,6 +475,15 @@ impl RenderedWindow {

let save_layer_rec = SaveLayerRec::default().bounds(&pixel_region).paint(&paint);
root_canvas.save_layer(&save_layer_rec);
if let Some(background) = background_image {
Self::draw_window_background_image(
&paint,
&root_canvas,
&background,
&pixel_region,
&screen_rect,
);
}
self.draw_surface(
root_canvas,
&pixel_region,
Expand Down
8 changes: 8 additions & 0 deletions src/window/renderer.rs
mysty0 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ impl SkiaRenderer {
pub fn resize(&mut self, windowed_context: &WindowedContext) {
self.surface = create_surface(windowed_context, &mut self.gr_context, self.fb_info);
}

pub fn width(&self) -> i32 {
self.surface.width()
}

pub fn height(&self) -> i32 {
self.surface.height()
}
}
Loading
Loading