From a9d6efaf5cf2b7edc4fcadb31ad8c9a493e44487 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 18 Dec 2021 15:37:50 +0900 Subject: [PATCH] Refactor getting album art from handle to display Avoids doing a copy of the album art data. --- src/display.rs | 35 +++++++++++++---------------------- src/mpd_handler.rs | 26 ++++++++------------------ 2 files changed, 21 insertions(+), 40 deletions(-) diff --git a/src/display.rs b/src/display.rs index 86d8acd..d03886c 100644 --- a/src/display.rs +++ b/src/display.rs @@ -180,19 +180,20 @@ impl MPDDisplay { } } - fn get_image_from_data( - &mut self, - ctx: &mut Context, - data: (Vec, String), - ) -> Result<(), String> { + fn get_image_from_data(&mut self, ctx: &mut Context) -> Result<(), String> { + let mpd_handle = self.mpd_handler.clone().unwrap(); + let read_handle = mpd_handle + .try_read() + .map_err(|_| String::from("ERROR get_image_from_data: Failed to get read_handle"))?; + let mut image_format: image::ImageFormat = image::ImageFormat::Png; - match data.1.as_str() { + match read_handle.get_art_type().as_str() { "image/png" => image_format = image::ImageFormat::Png, "image/jpg" | "image/jpeg" => image_format = image::ImageFormat::Jpeg, "image/gif" => image_format = image::ImageFormat::Gif, _ => (), } - let img = ImageReader::with_format(Cursor::new(data.0), image_format) + let img = ImageReader::with_format(Cursor::new(read_handle.get_art_data()), image_format) .decode() .map_err(|e| format!("ERROR: Failed to decode album art image: {}", e))?; let rgba8 = img.to_rgba8(); @@ -487,23 +488,13 @@ impl EventHandler for MPDDisplay { log("Failed to acquire read lock for getting shared data"); } if self.album_art.is_none() { - let album_art_data_result = - MPDHandler::get_art_data(self.mpd_handler.clone().unwrap()); - if let Ok(art_data) = album_art_data_result { - let result = self.get_image_from_data(ctx, art_data); - if let Err(e) = result { - log(e); - self.album_art = None; - self.album_art_draw_transform = None; - } else { - self.get_album_art_transform( - ctx, - !self.opts.do_not_fill_scale_album_art, - ); - } - } else { + let result = self.get_image_from_data(ctx); + if let Err(e) = result { + log(e); self.album_art = None; self.album_art_draw_transform = None; + } else { + self.get_album_art_transform(ctx, !self.opts.do_not_fill_scale_album_art); } } } diff --git a/src/mpd_handler.rs b/src/mpd_handler.rs index b2bac32..364dbc8 100644 --- a/src/mpd_handler.rs +++ b/src/mpd_handler.rs @@ -271,24 +271,6 @@ impl MPDHandler { Ok(s) } - pub fn get_art_data(h: Arc>) -> Result<(Vec, String), ()> { - if let Ok(read_lock) = h.try_read() { - if read_lock.art_data.len() == read_lock.art_data_size { - return Ok((read_lock.art_data.clone(), read_lock.art_data_type.clone())); - } - } - - Err(()) - } - - pub fn can_get_art_data(h: Arc>) -> bool { - if let Ok(read_lock) = h.try_read() { - return read_lock.can_get_album_art || read_lock.can_get_album_art_in_dir; - } - - false - } - pub fn get_current_song_info(h: Arc>) -> Result { if let Ok(read_lock) = h.try_read() { return Ok(InfoFromShared { @@ -332,6 +314,14 @@ impl MPDHandler { } } + pub fn get_art_type(&self) -> String { + self.art_data_type.clone() + } + + pub fn get_art_data(&self) -> &[u8] { + &self.art_data + } + fn handler_loop(h: Arc>) -> Result<(), String> { let mut buf: [u8; BUF_SIZE] = [0; BUF_SIZE]; let mut saved: Vec = Vec::new();