Browse Source

Fixed multithreaded issues with texture loading

flabbet 8 months ago
parent
commit
a2956c302a

+ 0 - 16
src/PixiEditor/Models/IO/Importer.cs

@@ -151,22 +151,6 @@ internal class Importer : ObservableObject
         }
         }
     }
     }
 
 
-    public static Texture GetPreviewTexture(string path)
-    {
-        if (!IsSupportedFile(path))
-        {
-            throw new InvalidFileTypeException(new LocalizedString("FILE_EXTENSION_NOT_SUPPORTED",
-                Path.GetExtension(path)));
-        }
-
-        if (Path.GetExtension(path) != ".pixi")
-            return Texture.Load(path);
-
-        using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
-
-        return Texture.Load(PixiParser.ReadPreview(fileStream));
-    }
-
     public static Surface GetPreviewSurface(string path)
     public static Surface GetPreviewSurface(string path)
     {
     {
         if (!IsSupportedFile(path))
         if (!IsSupportedFile(path))

+ 30 - 53
src/PixiEditor/Views/Visuals/PixiFilePreviewImage.cs

@@ -6,6 +6,7 @@ using PixiEditor.Helpers;
 using PixiEditor.Models;
 using PixiEditor.Models;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.IO;
 using Drawie.Numerics;
 using Drawie.Numerics;
+using PixiEditor.Parser;
 
 
 namespace PixiEditor.Views.Visuals;
 namespace PixiEditor.Views.Visuals;
 
 
@@ -50,11 +51,33 @@ internal class PixiFilePreviewImage : TextureControl
         Task.Run(() => LoadImage(path));
         Task.Run(() => LoadImage(path));
     }
     }
 
 
-    private void LoadImage(string path)
+    private async Task LoadImage(string path)
     {
     {
-        var surface = LoadPreviewTexture(path);
+        string fileExtension = Path.GetExtension(path);
 
 
-        Dispatcher.UIThread.Post(() => SetImage(surface));
+        byte[] imageBytes;
+
+        bool isPixi = fileExtension == ".pixi";
+        if (isPixi)
+        {
+            await using FileStream fileStream = File.OpenRead(path);
+            imageBytes = await PixiParser.ReadPreviewAsync(fileStream);
+        }
+        else if (SupportedFilesHelper.IsExtensionSupported(fileExtension) &&
+                 SupportedFilesHelper.IsRasterFormat(fileExtension))
+        {
+            imageBytes = await File.ReadAllBytesAsync(path);
+        }
+        else
+        {
+            return;
+        }
+
+        Dispatcher.UIThread.Post(() =>
+        {
+            var surface = LoadTexture(imageBytes);
+            SetImage(surface);
+        });
     }
     }
 
 
     private void SetImage(Texture? texture)
     private void SetImage(Texture? texture)
@@ -78,58 +101,13 @@ internal class PixiFilePreviewImage : TextureControl
         previewImage.RunLoadImage();
         previewImage.RunLoadImage();
     }
     }
 
 
-    private Texture? LoadPreviewTexture(string filePath)
-    {
-        if (!File.Exists(filePath))
-        {
-            return null;
-        }
-
-        var fileExtension = Path.GetExtension(filePath);
-
-        if (fileExtension == ".pixi")
-        {
-            return LoadPixiPreview(filePath);
-        }
-
-        if (SupportedFilesHelper.IsExtensionSupported(fileExtension) && SupportedFilesHelper.IsRasterFormat(fileExtension))
-        {
-            return LoadNonPixiPreview(filePath);
-        }
-
-        return null;
-
-    }
-
-    private Texture LoadPixiPreview(string filePath)
-    {
-        try
-        {
-            var loaded = Importer.GetPreviewTexture(filePath);
-
-            if (loaded.Size is { X: <= Constants.MaxPreviewWidth, Y: <= Constants.MaxPreviewHeight })
-            {
-                return loaded;
-            }
-
-            var downscaled = DownscaleSurface(loaded);
-            loaded.Dispose();
-            return downscaled;
-        }
-        catch
-        {
-            SetCorrupt();
-            return null;
-        }
-    }
-
-    private Texture LoadNonPixiPreview(string filePath)
+    private Texture LoadTexture(byte[] textureBytes)
     {
     {
         Texture loaded = null;
         Texture loaded = null;
 
 
         try
         try
         {
         {
-            loaded = Texture.Load(filePath);
+            loaded = Texture.Load(textureBytes);
         }
         }
         catch (RecoverableException)
         catch (RecoverableException)
         {
         {
@@ -147,7 +125,6 @@ internal class PixiFilePreviewImage : TextureControl
         var downscaled = DownscaleSurface(loaded);
         var downscaled = DownscaleSurface(loaded);
         loaded.Dispose();
         loaded.Dispose();
         return downscaled;
         return downscaled;
-
     }
     }
 
 
     private static Texture DownscaleSurface(Texture surface)
     private static Texture DownscaleSurface(Texture surface)
@@ -157,13 +134,13 @@ internal class PixiFilePreviewImage : TextureControl
             Constants.MaxPreviewHeight / (double)surface.Size.Y);
             Constants.MaxPreviewHeight / (double)surface.Size.Y);
 
 
         var newSize = new VecI((int)(surface.Size.X * factor), (int)(surface.Size.Y * factor));
         var newSize = new VecI((int)(surface.Size.X * factor), (int)(surface.Size.Y * factor));
-        
+
         var scaledBitmap = surface.Resize(newSize, ResizeMethod.HighQuality);
         var scaledBitmap = surface.Resize(newSize, ResizeMethod.HighQuality);
 
 
         surface.Dispose();
         surface.Dispose();
         return scaledBitmap;
         return scaledBitmap;
     }
     }
-    
+
     // TODO: This does not actually set the dot to gray
     // TODO: This does not actually set the dot to gray
     void SetCorrupt()
     void SetCorrupt()
     {
     {

+ 0 - 2
src/PixiEditor/Views/Windows/HelloTherePopup.axaml

@@ -14,8 +14,6 @@
                          xmlns:newsFeed1="clr-namespace:PixiEditor.Views.NewsFeed"
                          xmlns:newsFeed1="clr-namespace:PixiEditor.Views.NewsFeed"
                          xmlns:dialogs="clr-namespace:PixiEditor.Views.Dialogs"
                          xmlns:dialogs="clr-namespace:PixiEditor.Views.Dialogs"
                          xmlns:visuals="clr-namespace:PixiEditor.Views.Visuals"
                          xmlns:visuals="clr-namespace:PixiEditor.Views.Visuals"
-                         xmlns:skiaSharp="clr-namespace:SkiaSharp;assembly=SkiaSharp"
-                         xmlns:ui1="clr-namespace:PixiEditor.Helpers.UI"
                          xmlns:windows="clr-namespace:PixiEditor.Views.Windows"
                          xmlns:windows="clr-namespace:PixiEditor.Views.Windows"
                          mc:Ignorable="d"
                          mc:Ignorable="d"
                          Title="Hello there!" Height="680" Width="982" MinHeight="500" MinWidth="600"
                          Title="Hello there!" Height="680" Width="982" MinHeight="500" MinWidth="600"