Optimize rendering by pre-loading resources #231

Merged
savanni merged 2 commits from otg/resource-loader into main 2024-04-09 13:56:03 +00:00
4 changed files with 72 additions and 41 deletions
Showing only changes of commit 0aecaee760 - Show all commits

View File

@ -59,7 +59,7 @@ pub struct AppWindow {
// anything else. I'll have to look into this later.
settings_view_model: Arc<RwLock<Option<SettingsView>>>,
resources: Rc<ResourceManager>,
resources: ResourceManager,
}
impl AppWindow {
@ -79,7 +79,7 @@ impl AppWindow {
overlay,
core,
settings_view_model: Default::default(),
resources: Rc::new(resources),
resources,
};
let home = s.setup_home();
@ -91,7 +91,7 @@ impl AppWindow {
pub fn open_game_review(&self, game_record: GameRecord) {
let header = adw::HeaderBar::new();
let game_review = GameReview::new(self.core.clone(), game_record);
let game_review = GameReview::new(self.core.clone(), game_record, self.resources.clone());
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)

View File

@ -35,7 +35,7 @@ You should have received a copy of the GNU General Public License along with On
// Now, we know what kind of object we have for the current board representation. Let's make use of
// that.
use crate::perftrace;
use crate::{perftrace, Resource, ResourceManager};
use gio::resources_lookup_data;
use glib::Object;
@ -56,6 +56,7 @@ const MARGIN: i32 = 20;
#[derive(Default)]
pub struct GobanPrivate {
board_state: Rc<RefCell<otg_core::Goban>>,
resource_manager: Rc<RefCell<Option<ResourceManager>>>,
}
impl GobanPrivate {}
@ -88,10 +89,11 @@ glib::wrapper! {
}
impl Goban {
pub fn new(board_state: otg_core::Goban) -> Self {
pub fn new(board_state: otg_core::Goban, resources: ResourceManager) -> Self {
let s: Self = Object::builder().build();
*s.imp().board_state.borrow_mut() = board_state;
*s.imp().resource_manager.borrow_mut() = Some(resources);
s.set_width_request(WIDTH);
s.set_height_request(HEIGHT);
@ -107,19 +109,42 @@ impl Goban {
fn redraw(&self, ctx: &cairo::Context, width: i32, height: i32) {
println!("{} x {}", width, height);
/*
let background = load_pixbuf(
"/com/luminescent-dreams/otg-gtk/wood_texture.jpg",
false,
WIDTH + 40,
HEIGHT + 40,
);
*/
let background = self
.imp()
.resource_manager
.borrow()
.as_ref()
.and_then(|r| r.resource("/com/luminescent-dreams/otg-gtk/wood_texture.jpg"));
let black_texture = self
.imp()
.resource_manager
.borrow()
.as_ref()
.and_then(|r| r.resource("/com/luminescent-dreams/otg-gtk/black_stone.png"));
let white_texture = self
.imp()
.resource_manager
.borrow()
.as_ref()
.and_then(|r| r.resource("/com/luminescent-dreams/otg-gtk/white_stone.png"));
match background {
Ok(Some(ref background)) => {
Some(Resource::Image(ref background)) => {
ctx.set_source_pixbuf(background, 0., 0.);
ctx.paint().expect("paint should never fail");
}
Ok(None) | Err(_) => ctx.set_source_rgb(0.7, 0.7, 0.7),
None => ctx.set_source_rgb(0.7, 0.7, 0.7),
}
let board = self.imp().board_state.borrow();
@ -129,7 +154,14 @@ impl Goban {
let hspace_between = ((width - 40) as f64) / ((board.size.width - 1) as f64);
let vspace_between = ((height - 40) as f64) / ((board.size.height - 1) as f64);
let pen = Pen::new(MARGIN as f64, MARGIN as f64, hspace_between, vspace_between);
let pen = Pen::new(
MARGIN as f64,
MARGIN as f64,
hspace_between,
vspace_between,
black_texture,
white_texture,
);
(0..board.size.width).for_each(|col| {
ctx.move_to(
@ -179,35 +211,27 @@ struct Pen {
y_offset: f64,
hspace_between: f64,
vspace_between: f64,
black_stone: Pixbuf,
white_stone: Pixbuf,
black_stone: Option<Pixbuf>,
white_stone: Option<Pixbuf>,
}
impl Pen {
fn new(x_offset: f64, y_offset: f64, hspace_between: f64, vspace_between: f64) -> Self {
let radius = (hspace_between / 2. - 2.) as i32;
let black_stone = load_pixbuf(
"/com/luminescent-dreams/otg-gtk/black_stone.png",
true,
512,
512,
)
.unwrap()
.unwrap();
let black_stone = black_stone
.scale_simple(radius * 2, radius * 2, InterpType::Nearest)
.unwrap();
let white_stone = load_pixbuf(
"/com/luminescent-dreams/otg-gtk/white_stone.png",
true,
512,
512,
)
.unwrap()
.unwrap();
let white_stone = white_stone
.scale_simple(radius * 2, radius * 2, InterpType::Nearest)
.unwrap();
fn new(
x_offset: f64,
y_offset: f64,
hspace_between: f64,
vspace_between: f64,
black_stone: Option<Resource>,
white_stone: Option<Resource>,
) -> Self {
let black_stone = match black_stone {
Some(Resource::Image(img)) => Some(img),
_ => None,
};
let white_stone = match white_stone {
Some(Resource::Image(img)) => Some(img),
_ => None,
};
Pen {
x_offset,
y_offset,
@ -232,8 +256,14 @@ impl Pen {
fn stone(&self, ctx: &cairo::Context, row: u8, col: u8, color: Color, _liberties: Option<u8>) {
let (x_loc, y_loc) = self.stone_location(row, col);
match color {
Color::White => ctx.set_source_pixbuf(&self.white_stone, x_loc, y_loc),
Color::Black => ctx.set_source_pixbuf(&self.black_stone, x_loc, y_loc),
Color::White => match self.white_stone {
Some(ref white_stone) => ctx.set_source_pixbuf(&white_stone, x_loc, y_loc),
None => ctx.set_source_rgb(0.9, 0.9, 0.9),
},
Color::Black => match self.black_stone {
Some(ref black_stone) => ctx.set_source_pixbuf(&black_stone, x_loc, y_loc),
None => ctx.set_source_rgb(0.0, 0.0, 0.0),
},
}
ctx.paint().expect("paint should never fail");
/*

View File

@ -44,6 +44,7 @@ pub enum Resource {
Image(Pixbuf),
}
#[derive(Clone)]
pub struct ResourceManager {
resources: Rc<RefCell<HashMap<String, Resource>>>,
}

View File

@ -22,7 +22,9 @@ You should have received a copy of the GNU General Public License along with On
// I'll get all of the information about the game from the core, and then render everything in the
// UI. So this will be a heavy lift on the UI side.
use crate::{components::{Goban, PlayerCard, ReviewTree}, CoreApi};
use crate::{
components::{Goban, PlayerCard, ReviewTree}, CoreApi, ResourceManager
};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use otg_core::Color;
@ -31,8 +33,6 @@ use sgf::GameRecord;
#[derive(Default)]
pub struct GameReviewPrivate {}
#[glib::object_subclass]
impl ObjectSubclass for GameReviewPrivate {
const NAME: &'static str = "GameReview";
@ -49,7 +49,7 @@ glib::wrapper! {
}
impl GameReview {
pub fn new(_api: CoreApi, record: GameRecord) -> Self {
pub fn new(_api: CoreApi, record: GameRecord, resources: ResourceManager) -> Self {
let s: Self = Object::builder().build();
// It's actually really bad to be just throwing away errors. Panics make everyone unhappy.
@ -58,7 +58,7 @@ impl GameReview {
let board_repr = otg_core::Goban::default()
.apply_moves(record.mainline())
.unwrap();
let board = Goban::new(board_repr);
let board = Goban::new(board_repr, resources);
/*
s.attach(&board, 0, 0, 2, 2);