[gnome-music/wip/mschraal/artcache-use-bytearray] artcache: Pass bytes for texture creation




commit 3e6e8c459d99a9ca8961fdbba874f3347da97aa7
Author: Marinus Schraal <mschraal gnome org>
Date:   Mon Mar 14 20:32:05 2022 +0100

    artcache: Pass bytes for texture creation
    
    Previously Music made a pixbuf for art and passed that to texture
    creation, since 4.6 it is possible to pass a bytes object directly.

 gnomemusic/artcache.py | 47 +++++++++++++++++++++++++++++++----------------
 meson.build            |  2 +-
 2 files changed, 32 insertions(+), 17 deletions(-)
---
diff --git a/gnomemusic/artcache.py b/gnomemusic/artcache.py
index bb7d9c635..f97a33cd6 100644
--- a/gnomemusic/artcache.py
+++ b/gnomemusic/artcache.py
@@ -22,7 +22,7 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
-from gi.repository import Gdk, GdkPixbuf, Gio, Gtk, GLib, GObject
+from gi.repository import Gdk, Gio, Gtk, GLib, GObject
 
 from gnomemusic.coreartist import CoreArtist
 from gnomemusic.coverpaintable import CoverPaintable
@@ -44,6 +44,7 @@ class ArtCache(GObject.GObject):
         "finished": (GObject.SignalFlags.RUN_FIRST, None, (object, ))
     }
 
+    _chunksize = 32768
     _log = MusicLogger()
 
     def __init__(self, widget: Gtk.Widget) -> None:
@@ -56,6 +57,7 @@ class ArtCache(GObject.GObject):
         self._size = ArtSize.SMALL
         self._widget = widget
 
+        self._bytearray = bytearray()
         self._coreobject = None
         self._icon_type = DefaultIconType.ALBUM
         self._paintable = None
@@ -97,26 +99,39 @@ class ArtCache(GObject.GObject):
             self.emit("finished", self._paintable)
             return
 
-        GdkPixbuf.Pixbuf.new_from_stream_async(
-            stream, None, self._pixbuf_loaded, None)
+        stream.read_bytes_async(
+            self._chunksize, GLib.PRIORITY_DEFAULT_IDLE, None,
+            self._read_bytes_async_cb, None)
 
-    def _pixbuf_loaded(self, stream, result, data):
+    def _read_bytes_async_cb(self, stream, result, data):
         try:
-            pixbuf = GdkPixbuf.Pixbuf.new_from_stream_finish(result)
+            gbytes = stream.read_bytes_finish(result)
         except GLib.Error as error:
-            self._log.warning(
-                "Error: {}, {}".format(error.domain, error.message))
-            self.emit("finished", self._paintable)
+            stream.close_async(
+                GLib.PRIORITY_DEFAULT_IDLE, None, self._close_stream, None)
             return
 
-        texture = Gdk.Texture.new_for_pixbuf(pixbuf)
-        if texture:
-            self._paintable = CoverPaintable(
-                self._size, self._widget, icon_type=self._icon_type,
-                texture=texture)
-
-        stream.close_async(
-            GLib.PRIORITY_DEFAULT_IDLE, None, self._close_stream, None)
+        gbytes_size = gbytes.get_size()
+        if gbytes_size > 0:
+            self._bytearray += gbytes.unref_to_data()
+
+            stream.read_bytes_async(
+                self._chunksize, GLib.PRIORITY_DEFAULT_IDLE, None,
+                self._read_bytes_async_cb, None)
+        else:
+            # FIXME: Use GTask to load textures async.
+            # See pygobject#114 for bytes conversion.
+            texture = Gdk.Texture.new_from_bytes(
+                GLib.Bytes(bytes(self._bytearray)))
+            if texture:
+                self._paintable = CoverPaintable(
+                    self._size, self._widget, icon_type=self._icon_type,
+                    texture=texture)
+
+            self._bytearray = bytearray()
+
+            stream.close_async(
+                GLib.PRIORITY_DEFAULT_IDLE, None, self._close_stream, None)
 
     def _close_stream(self, stream, result, data):
         try:
diff --git a/meson.build b/meson.build
index 4ae5b135b..92b8a204d 100644
--- a/meson.build
+++ b/meson.build
@@ -44,7 +44,7 @@ PKGLIB_DIR = join_paths(get_option('prefix'), get_option('libdir'), APPLICATION_
 dependency('glib-2.0', version: '>= 2.67.1')
 dependency('goa-1.0', version: '>= 3.35.90')
 dependency('gobject-introspection-1.0', version: '>= 1.35.0')
-dependency('gtk4', version: '>= 4.5.0')
+dependency('gtk4', version: '>= 4.6.0')
 dependency('libadwaita-1', version: '>= 1.0')
 dependency('libmediaart-2.0', version: '>= 1.9.1')
 dependency('libsoup-2.4')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]