Implemented a pixbuf cache mechanism.

Since gdk_pixbuf::Pixbuf is refference counted and every episode,
use the cover of the Podcast Feed/Show, We can only create a Pixbuf
cover per show and pass around the Rc pointer.

GObjects do not implement Send trait, so SendCell is a way around that.
Also lazy_static requires Sync trait, so that's what the mutexes are.
This commit is contained in:
Jordan Petridis
2017-12-21 17:36:07 +02:00
parent 74a6e5814a
commit e416bca963
5 changed files with 42 additions and 1 deletions
+2
View File
@@ -12,11 +12,13 @@ gdk = "0.7.0"
gdk-pixbuf = "0.3.0"
gio = "0.3.0"
glib = "0.4.0"
lazy_static = "1.0.0"
log = "0.3.8"
loggerv = "0.6.0"
open = "1.2.1"
rayon = "0.9.0"
regex = "0.2.3"
send-cell = "0.1.2"
[dependencies.diesel]
features = ["sqlite"]
+3
View File
@@ -12,10 +12,13 @@ extern crate dissolve;
extern crate hammond_data;
extern crate hammond_downloader;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate loggerv;
extern crate open;
extern crate regex;
extern crate send_cell;
// extern crate rayon;
// use rayon::prelude::*;
+24 -1
View File
@@ -1,3 +1,4 @@
use send_cell::SendCell;
use glib;
use gdk_pixbuf::Pixbuf;
@@ -8,7 +9,9 @@ use hammond_downloader::downloader;
use std::thread;
use std::cell::RefCell;
use std::sync::mpsc::{channel, Receiver};
use std::sync::Mutex;
use std::rc::Rc;
use std::collections::HashMap;
use content::Content;
@@ -59,10 +62,30 @@ fn refresh_podcasts_view() -> glib::Continue {
glib::Continue(false)
}
lazy_static! {
static ref CACHED_PIXBUFS: Mutex<HashMap<(i32, u32), Mutex<SendCell<Pixbuf>>>> = {
Mutex::new(HashMap::new())
};
}
// FIXME: use something that would just scale?
pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Option<Pixbuf> {
let mut hashmap = CACHED_PIXBUFS.lock().unwrap();
{
let res = hashmap.get(&(pd.id(), size));
if let Some(px) = res {
let m = px.lock().unwrap();
return Some(m.clone().into_inner());
}
}
let img_path = downloader::cache_image(pd)?;
Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true).ok()
let px = Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true).ok();
if let Some(px) = px {
hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone())));
return Some(px);
}
None
}
#[cfg(test)]