From 59d45a5440aaa46c7dc8f3dc70c8518167c10418 Mon Sep 17 00:00:00 2001
From: Héctor Ramón Jiménez <hector0193@gmail.com>
Date: Wed, 26 Feb 2020 12:34:34 +0100
Subject: Refactor texture atlas

- Split into multiple modules
- Rename some concepts
- Change API details
---
 wgpu/src/image/raster.rs | 27 +++++++++++++++++----------
 wgpu/src/image/vector.rs | 34 +++++++++++++++++-----------------
 2 files changed, 34 insertions(+), 27 deletions(-)

(limited to 'wgpu/src/image')

diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs
index 884dd65a..071d53c8 100644
--- a/wgpu/src/image/raster.rs
+++ b/wgpu/src/image/raster.rs
@@ -1,12 +1,10 @@
-use crate::image::{TextureArray, ImageAllocation};
+use crate::texture::atlas::{self, Atlas};
 use iced_native::image;
-use std::{
-    collections::{HashMap, HashSet},
-};
+use std::collections::{HashMap, HashSet};
 
 pub enum Memory {
     Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
-    Device(ImageAllocation),
+    Device(atlas::Entry),
     NotFound,
     Invalid,
 }
@@ -15,7 +13,7 @@ impl Memory {
     pub fn dimensions(&self) -> (u32, u32) {
         match self {
             Memory::Host(image) => image.dimensions(),
-            Memory::Device(allocation) => allocation.size(),
+            Memory::Device(entry) => entry.size(),
             Memory::NotFound => (1, 1),
             Memory::Invalid => (1, 1),
         }
@@ -78,17 +76,26 @@ impl Cache {
         handle: &image::Handle,
         device: &wgpu::Device,
         encoder: &mut wgpu::CommandEncoder,
-        atlas_array: &mut TextureArray,
-    ) -> &Memory {
+        atlas: &mut Atlas,
+    ) -> Option<&atlas::Entry> {
         let memory = self.load(handle);
 
         if let Memory::Host(image) = memory {
-            let allocation = atlas_array.upload(image, device, encoder);
+            let (width, height) = image.dimensions();
+
+            let allocation =
+                atlas.upload(width, height, &image, device, encoder)?;
+
+            dbg!("Uploaded");
 
             *memory = Memory::Device(allocation);
         }
 
-        memory
+        if let Memory::Device(allocation) = memory {
+            Some(allocation)
+        } else {
+            None
+        }
     }
 
     pub fn trim(&mut self) {
diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs
index 98588e5c..0dabc9ca 100644
--- a/wgpu/src/image/vector.rs
+++ b/wgpu/src/image/vector.rs
@@ -1,8 +1,6 @@
-use crate::image::{TextureArray, ImageAllocation};
+use crate::texture::atlas::{self, Atlas};
 use iced_native::svg;
-use std::{
-    collections::{HashMap, HashSet},
-};
+use std::collections::{HashMap, HashSet};
 
 pub enum Svg {
     Loaded(resvg::usvg::Tree),
@@ -33,7 +31,7 @@ impl std::fmt::Debug for Svg {
 
 pub struct Cache {
     svgs: HashMap<u64, Svg>,
-    rasterized: HashMap<(u64, u32, u32), ImageAllocation>,
+    rasterized: HashMap<(u64, u32, u32), atlas::Entry>,
     svg_hits: HashSet<u64>,
     rasterized_hits: HashSet<(u64, u32, u32)>,
 }
@@ -71,8 +69,8 @@ impl Cache {
         scale: f32,
         device: &wgpu::Device,
         encoder: &mut wgpu::CommandEncoder,
-        texture_array: &mut TextureArray,
-    ) -> Option<&ImageAllocation> {
+        texture_atlas: &mut Atlas,
+    ) -> Option<&atlas::Entry> {
         let id = handle.id();
 
         let (width, height) = (
@@ -104,10 +102,8 @@ impl Cache {
                 let screen_size =
                     resvg::ScreenSize::new(width, height).unwrap();
 
-                let mut canvas = resvg::raqote::DrawTarget::new(
-                    width as i32,
-                    height as i32,
-                );
+                let mut canvas =
+                    resvg::raqote::DrawTarget::new(width as i32, height as i32);
 
                 resvg::backend_raqote::render_to_canvas(
                     tree,
@@ -116,17 +112,21 @@ impl Cache {
                     &mut canvas,
                 );
 
-                let allocation = texture_array.upload(&canvas, device, encoder);
+                let allocation = texture_atlas.upload(
+                    width,
+                    height,
+                    canvas.get_data(),
+                    device,
+                    encoder,
+                )?;
 
                 let _ = self.svg_hits.insert(id);
                 let _ = self.rasterized_hits.insert((id, width, height));
-                let _ = self
-                    .rasterized
-                    .insert((id, width, height), allocation);
+                let _ = self.rasterized.insert((id, width, height), allocation);
 
                 self.rasterized.get(&(id, width, height))
             }
-            Svg::NotFound => None
+            Svg::NotFound => None,
         }
     }
 
@@ -145,4 +145,4 @@ impl std::fmt::Debug for Cache {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(f, "vector::Cache")
     }
-}
\ No newline at end of file
+}
-- 
cgit