Răsfoiți Sursa

app: use QOI files for video loading

darkfi 9 luni în urmă
părinte
comite
f5037e7d0d
4 a modificat fișierele cu 17 adăugiri și 24 ștergeri
  1. 1 0
      bin/app/Cargo.lock
  2. 1 0
      bin/app/Cargo.toml
  3. 3 3
      bin/app/src/app/schema/mod.rs
  4. 12 21
      bin/app/src/ui/video.rs

+ 1 - 0
bin/app/Cargo.lock

@@ -1495,6 +1495,7 @@ dependencies = [
  "parking_lot 0.12.5",
  "parley",
  "peniko",
+ "qoi",
  "rand 0.8.5",
  "regex",
  "semver",

+ 1 - 0
bin/app/Cargo.toml

@@ -18,6 +18,7 @@ harfbuzz-sys = { git = "https://github.com/narodnik/rust-harfbuzz2", features =
 freetype-rs = { version = "0.37.0", features = ["bundled"] }
 
 image = "0.25.5"
+qoi = "0.4.1"
 log = { version = "0.4.27", features = ["release_max_level_info"] }
 glam = "0.29.2"
 #zmq = "0.10.0"

+ 3 - 3
bin/app/src/app/schema/mod.rs

@@ -56,7 +56,7 @@ mod ui_consts {
     use std::path::PathBuf;
 
     //pub const BG_PATH: &str = "bg.png";
-    pub const VID_PATH: &str = "forest/forest_{frame}.png";
+    pub const VID_PATH: &str = "forest6/forest_02cjpg{frame}.qoi";
     pub use super::android_ui_consts::*;
 
     pub fn get_chatdb_path() -> PathBuf {
@@ -81,7 +81,7 @@ mod desktop_paths {
     use std::path::PathBuf;
 
     //pub const BG_PATH: &str = "assets/bg.png";
-    pub const VID_PATH: &str = "assets/forest/forest_{frame}.png";
+    pub const VID_PATH: &str = "assets/forest6/forest_02cjpg{frame}.qoi";
 
     pub fn get_chatdb_path() -> PathBuf {
         dirs::data_local_dir().unwrap().join("darkfi/app/chatdb")
@@ -278,7 +278,7 @@ pub async fn make(app: &App, window: SceneNodePtr, i18n_fish: &I18nBabelFish) {
         node.set_property_u32(atom, Role::App, "z_index", 0).unwrap();
         //let node = node.setup(|me| Image::new(me, app.render_api.clone())).await;
         //layer_node.link(node);
-        node.set_property_u32(atom, Role::App, "length", 357).unwrap();
+        node.set_property_u32(atom, Role::App, "length", 150).unwrap();
         let node = node.setup(|me| Video::new(me, app.render_api.clone(), app.ex.clone())).await;
         layer_node.link(node);
 

+ 12 - 21
bin/app/src/ui/video.rs

@@ -17,15 +17,11 @@
  */
 
 use async_trait::async_trait;
-use image::ImageReader;
 use parking_lot::Mutex as SyncMutex;
 use rand::{rngs::OsRng, Rng};
-use std::{
-    io::Cursor,
-    sync::{
-        atomic::{AtomicBool, Ordering},
-        Arc,
-    },
+use std::sync::{
+    atomic::{AtomicBool, Ordering},
+    Arc,
 };
 
 use crate::{
@@ -42,9 +38,10 @@ use crate::{
 
 use super::{DrawTrace, DrawUpdate, OnModify, UIObject};
 
-pub const N_LOADERS: usize = 4;
+pub const N_LOADERS: usize = 6;
 
 macro_rules! t { ($($arg:tt)*) => { trace!(target: "ui::video", $($arg)*); } }
+macro_rules! d { ($($arg:tt)*) => { debug!(target: "ui::video", $($arg)*); } }
 
 pub type VideoPtr = Arc<Video>;
 
@@ -112,7 +109,7 @@ impl Video {
             textures_pub,
             textures_sub,
             vid_data: Arc::new(SyncMutex::new(None)),
-            _load_handles: SyncMutex::new([const { None }; 4]),
+            _load_handles: SyncMutex::new([const { None }; N_LOADERS]),
 
             rect,
             uv,
@@ -162,7 +159,7 @@ impl Video {
             self.textures_pub.clone().set_capacity(vid_len);
         }
 
-        let mut handles = [const { None }; 4];
+        let mut handles = [const { None }; N_LOADERS];
         for thread_idx in 0..N_LOADERS {
             let path_fmt = path_fmt.clone();
             let render_api = self.render_api.clone();
@@ -171,13 +168,13 @@ impl Video {
             let textures_pub = self.textures_pub.clone();
 
             let handle = std::thread::spawn(move || {
+                let now = std::time::Instant::now();
                 let mut frame_idx = thread_idx;
                 while frame_idx < vid_len {
                     // Stop loading instantly
                     if stop_load.load(Ordering::Relaxed) {
                         return
                     }
-                    //t!("frame_idx = {frame_idx} [thread={thread_idx}]");
                     let path = path_fmt.replace("{frame}", &format!("{frame_idx:#03}"));
                     let texture = Self::load_texture(path, &render_api);
                     // Make editing textures array and broadcasting an atomic op
@@ -193,6 +190,7 @@ impl Video {
 
                     frame_idx += N_LOADERS;
                 }
+                d!("thread {thread_idx} finished took {:?}", now.elapsed());
             });
             handles[thread_idx] = Some(handle);
         }
@@ -210,16 +208,9 @@ impl Video {
                 panic!("Resource not found! {e}");
             }
         });
-        let data = std::mem::take(&mut *data.lock());
-        let img =
-            ImageReader::new(Cursor::new(data)).with_guessed_format().unwrap().decode().unwrap();
-        let img = img.to_rgba8();
-
-        //let img = image::ImageReader::open(path).unwrap().decode().unwrap().to_rgba8();
-
-        let width = img.width() as u16;
-        let height = img.height() as u16;
-        let bmp = img.into_raw();
+        let (header, bmp) = qoi::decode_to_vec(&*data.lock()).unwrap();
+        let width = header.width as u16;
+        let height = header.height as u16;
 
         render_api.new_texture(width, height, bmp, gfxtag!("img"))
     }