Better performance for animated images

This commit is contained in:
kramo
2023-04-11 15:56:51 +02:00
parent 0da5f94ea3
commit dc9a91eac3

View File

@@ -17,15 +17,14 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import GdkPixbuf, GLib from gi.repository import GdkPixbuf, Gio, GLib
from .save_cover import resize_animation
class GameCover: class GameCover:
pixbuf = None pixbuf = None
path = None path = None
animation = None animation = None
anim_iter = None
placeholder_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale( placeholder_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale(
"/hu/kramo/Cartridges/library_placeholder.svg", 400, 600, False "/hu/kramo/Cartridges/library_placeholder.svg", 400, 600, False
@@ -35,6 +34,16 @@ class GameCover:
self.picture = picture self.picture = picture
self.new_pixbuf(pixbuf, path) self.new_pixbuf(pixbuf, path)
# Wrap the function in another one as Gio.Task.run_in_thread does not allow for passing args
def create_func(self, path):
self.animation = GdkPixbuf.PixbufAnimation.new_from_file(str(path))
self.anim_iter = self.animation.get_iter()
def wrapper(task, *_unused):
self.update_animation((task, self.animation))
return wrapper
def new_pixbuf(self, pixbuf=None, path=None): def new_pixbuf(self, pixbuf=None, path=None):
self.animation = None self.animation = None
self.pixbuf = None self.pixbuf = None
@@ -45,10 +54,9 @@ class GameCover:
if path: if path:
if str(path).rsplit(".", maxsplit=1)[-1] == "gif": if str(path).rsplit(".", maxsplit=1)[-1] == "gif":
self.path = resize_animation(path) self.path = path
self.animation = GdkPixbuf.PixbufAnimation.new_from_file(str(self.path)) task = Gio.Task.new(None, None, None)
self.anim_iter = self.animation.get_iter() task.run_in_thread(self.create_func(self.path))
self.update_animation(self.animation)
else: else:
self.path = path self.path = path
self.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( self.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
@@ -70,8 +78,8 @@ class GameCover:
def set_pixbuf(self, pixbuf): def set_pixbuf(self, pixbuf):
self.picture.set_pixbuf(pixbuf) self.picture.set_pixbuf(pixbuf)
def update_animation(self, animation): def update_animation(self, data):
if self.animation == animation: if self.animation == data[1]:
self.anim_iter.advance() self.anim_iter.advance()
self.set_pixbuf(self.anim_iter.get_pixbuf()) self.set_pixbuf(self.anim_iter.get_pixbuf())
@@ -80,5 +88,7 @@ class GameCover:
GLib.timeout_add( GLib.timeout_add(
20 if delay_time < 20 else delay_time, 20 if delay_time < 20 else delay_time,
self.update_animation, self.update_animation,
animation, data,
) )
else:
data[0].return_value(False)