From d4d978027b4d877306ab9c7f0d1702b00dc4e0c7 Mon Sep 17 00:00:00 2001 From: Adrien Plazas Date: Sun, 11 Aug 2019 23:18:08 +0200 Subject: [PATCH 1/6] pixdata: Use g_autofree This simplifies the code. --- retro-gtk/retro-pixdata.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/retro-gtk/retro-pixdata.c b/retro-gtk/retro-pixdata.c index b46c7fa..4f2633b 100644 --- a/retro-gtk/retro-pixdata.c +++ b/retro-gtk/retro-pixdata.c @@ -319,8 +319,8 @@ retro_pixdata_to_pixbuf (RetroPixdata *self) rgba8888 *rgba8888_data; GdkPixbuf *pixbuf; gfloat x_dpi; - gchar *x_dpi_string; - gchar *y_dpi_string; + g_autofree gchar *x_dpi_string = NULL; + g_autofree gchar *y_dpi_string = NULL; g_return_val_if_fail (self != NULL, NULL); @@ -348,8 +348,6 @@ retro_pixdata_to_pixbuf (RetroPixdata *self) y_dpi_string = g_strdup_printf ("%g", RETRO_CAIRO_DISPLAY_Y_DPI); gdk_pixbuf_set_option (pixbuf, "x-dpi", x_dpi_string); gdk_pixbuf_set_option (pixbuf, "y-dpi", y_dpi_string); - g_free (y_dpi_string); - g_free (x_dpi_string); return pixbuf; } -- GitLab From 1d0cf94afc55dadcabf0a5251bc23f1722ccc183 Mon Sep 17 00:00:00 2001 From: Alexander Mikhaylenko Date: Wed, 14 Aug 2019 15:45:33 +0500 Subject: [PATCH 2/6] Add RetroPixbuf Add helper functions for working with aspect ratio embedded into pixbufs as the "aspect-ratio" pixbuf option. --- retro-gtk/meson.build | 2 ++ retro-gtk/retro-gtk.h | 1 + retro-gtk/retro-pixbuf.c | 54 ++++++++++++++++++++++++++++++++++++++++ retro-gtk/retro-pixbuf.h | 21 ++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 retro-gtk/retro-pixbuf.c create mode 100644 retro-gtk/retro-pixbuf.h diff --git a/retro-gtk/meson.build b/retro-gtk/meson.build index 12914a2..96d979f 100644 --- a/retro-gtk/meson.build +++ b/retro-gtk/meson.build @@ -33,6 +33,7 @@ retro_gtk_sources = [ 'retro-option.c', 'retro-option-iterator.c', 'retro-pa-player.c', + 'retro-pixbuf.c', 'retro-pixdata.c', 'retro-pixel-format.c', 'retro-rumble-effect.c', @@ -59,6 +60,7 @@ retro_gtk_headers = [ 'retro-module-query.h', 'retro-option.h', 'retro-option-iterator.h', + 'retro-pixbuf.h', 'retro-pixdata.h', 'retro-rumble-effect.h', 'retro-video-filter.h', diff --git a/retro-gtk/retro-gtk.h b/retro-gtk/retro-gtk.h index bcbb818..9cb91cd 100644 --- a/retro-gtk/retro-gtk.h +++ b/retro-gtk/retro-gtk.h @@ -25,6 +25,7 @@ #include "retro-module-query.h" #include "retro-option.h" #include "retro-option-iterator.h" +#include "retro-pixbuf.h" #include "retro-pixdata.h" #include "retro-video-filter.h" diff --git a/retro-gtk/retro-pixbuf.c b/retro-gtk/retro-pixbuf.c new file mode 100644 index 0000000..900417d --- /dev/null +++ b/retro-gtk/retro-pixbuf.c @@ -0,0 +1,54 @@ +// This file is part of retro-gtk. License: GPL-3.0+. + +#include "retro-pixbuf.h" +#include + +/** + * retro_pixbuf_get_aspect_ratio: + * @pixbuf: a #GdkPixbuf + * + * Gets the aspect ratio of @pixbuf by reading the 'aspect-ratio' pixbuf option. + * + * Returns: the aspect ratio, or 0 if the option is not set or its value is invalid. + */ +gfloat +retro_pixbuf_get_aspect_ratio (GdkPixbuf *pixbuf) +{ + const gchar *aspect_ratio_str; + gfloat result = 0.f; + + g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), 0.f); + + aspect_ratio_str = gdk_pixbuf_get_option (pixbuf, "aspect-ratio"); + + if (!aspect_ratio_str) + return 0.f; + + sscanf (aspect_ratio_str, "%g", &result); + + return result; +} + +/** + * retro_pixbuf_set_aspect_ratio: + * @pixbuf: a #GdkPixbuf + * @aspect_ratio: the aspect ratio value + * + * Sets the aspect ratio of @pixbuf by setting the 'aspect-ratio' pixbuf option. + * Use retro_pixbuf_get_aspect_ratio() to retrieve it. + */ +void +retro_pixbuf_set_aspect_ratio (GdkPixbuf *pixbuf, + gfloat aspect_ratio) +{ + g_autofree gchar *aspect_ratio_string = NULL; + + g_return_if_fail (GDK_IS_PIXBUF (pixbuf)); + g_return_if_fail (aspect_ratio > 0.f); + + aspect_ratio_string = g_strdup_printf ("%g", aspect_ratio); + + gdk_pixbuf_remove_option (pixbuf, "aspect-ratio"); + + gdk_pixbuf_set_option (pixbuf, "aspect-ratio", aspect_ratio_string); +} diff --git a/retro-gtk/retro-pixbuf.h b/retro-gtk/retro-pixbuf.h new file mode 100644 index 0000000..fa3d53e --- /dev/null +++ b/retro-gtk/retro-pixbuf.h @@ -0,0 +1,21 @@ +// This file is part of retro-gtk. License: GPL-3.0+. + +#ifndef RETRO_PIXBUF_H +#define RETRO_PIXBUF_H + +#if !defined(__RETRO_GTK_INSIDE__) && !defined(RETRO_GTK_COMPILATION) +# error "Only can be included directly." +#endif + +#include +#include + +G_BEGIN_DECLS + +gfloat retro_pixbuf_get_aspect_ratio (GdkPixbuf *pixbuf); +void retro_pixbuf_set_aspect_ratio (GdkPixbuf *pixbuf, + gfloat aspect_ratio); + +G_END_DECLS + +#endif /* RETRO_PIXBUF_H */ -- GitLab From bef8b62ef67668e6d869ff499590f3e60333033d Mon Sep 17 00:00:00 2001 From: Alexander Mikhaylenko Date: Wed, 14 Aug 2019 15:49:42 +0500 Subject: [PATCH 3/6] retro-pixdata: Use retro_pixbuf_set_aspect_ratio() Deprecate x-dpi and y-dpi in favor of the newly added functions. --- retro-gtk/retro-pixdata.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/retro-gtk/retro-pixdata.c b/retro-gtk/retro-pixdata.c index 4f2633b..b2208d8 100644 --- a/retro-gtk/retro-pixdata.c +++ b/retro-gtk/retro-pixdata.c @@ -3,6 +3,7 @@ #include "retro-pixdata-private.h" #include +#include "retro-pixbuf.h" G_DEFINE_BOXED_TYPE (RetroPixdata, retro_pixdata, retro_pixdata_copy, retro_pixdata_free) @@ -343,12 +344,16 @@ retro_pixdata_to_pixbuf (RetroPixdata *self) self->width * sizeof (rgba8888), pixels_free, NULL); + /* x-dpi and y-dpi are deprecated, retro_pixbuf_get_aspect_ratio() and + * retro_pixbuf_set_aspect_ratio() should be used instead. */ x_dpi = self->aspect_ratio * RETRO_CAIRO_DISPLAY_Y_DPI; x_dpi_string = g_strdup_printf ("%g", x_dpi); y_dpi_string = g_strdup_printf ("%g", RETRO_CAIRO_DISPLAY_Y_DPI); gdk_pixbuf_set_option (pixbuf, "x-dpi", x_dpi_string); gdk_pixbuf_set_option (pixbuf, "y-dpi", y_dpi_string); + retro_pixbuf_set_aspect_ratio (pixbuf, self->aspect_ratio); + return pixbuf; } -- GitLab From 3be1bab4f25630d36ef0c7ffddba19c2ddd80443 Mon Sep 17 00:00:00 2001 From: Alexander Mikhaylenko Date: Tue, 13 Aug 2019 23:40:25 +0500 Subject: [PATCH 4/6] retro-gl-display: Custom aspect ratio for pixbuf Read 'aspect-ratio' options for the GdkPixbuf passed in retro_gl_display_set_pixbuf() and use it instead of the core-provided aspect ratio if it's present. --- retro-gtk/retro-gl-display.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/retro-gtk/retro-gl-display.c b/retro-gtk/retro-gl-display.c index d511f61..d8f563d 100644 --- a/retro-gtk/retro-gl-display.c +++ b/retro-gtk/retro-gl-display.c @@ -4,6 +4,7 @@ #include #include "retro-glsl-filter.h" +#include "retro-pixbuf.h" #include "retro-pixdata.h" struct _RetroGLDisplay @@ -492,11 +493,16 @@ retro_gl_display_get_pixbuf (RetroGLDisplay *self) * @pixbuf: a #GdkPixbuf * * Sets @pixbuf as the currently displayed video frame. + * + * retro_pixbuf_set_aspect_ratio() can be used to specify the aspect ratio for + * the pixbuf. Otherwise the core's aspect ratio will be used. */ void retro_gl_display_set_pixbuf (RetroGLDisplay *self, GdkPixbuf *pixbuf) { + gfloat aspect_ratio; + g_return_if_fail (RETRO_IS_GL_DISPLAY (self)); if (self->pixbuf == pixbuf) @@ -507,6 +513,10 @@ retro_gl_display_set_pixbuf (RetroGLDisplay *self, if (pixbuf != NULL) self->pixbuf = g_object_ref (pixbuf); + aspect_ratio = retro_pixbuf_get_aspect_ratio (pixbuf); + if (aspect_ratio != 0.f) + self->aspect_ratio = aspect_ratio; + gtk_widget_queue_draw (GTK_WIDGET (self)); } -- GitLab From 0aa107b524032934056deec086f8119a8d1fbc46 Mon Sep 17 00:00:00 2001 From: Alexander Mikhaylenko Date: Wed, 14 Aug 2019 13:58:00 +0500 Subject: [PATCH 5/6] retro-cairo-display: Custom aspect ratio for pixbuf Read 'aspect-ratio' options for the GdkPixbuf passed in retro_gl_display_set_pixbuf() and use it instead of the core-provided aspect ratio if it's present. --- retro-gtk/retro-cairo-display.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/retro-gtk/retro-cairo-display.c b/retro-gtk/retro-cairo-display.c index 379f52a..1fb637b 100644 --- a/retro-gtk/retro-cairo-display.c +++ b/retro-gtk/retro-cairo-display.c @@ -2,6 +2,7 @@ #include "retro-cairo-display.h" +#include "retro-pixbuf.h" #include "retro-pixdata.h" struct _RetroCairoDisplay @@ -312,11 +313,16 @@ retro_cairo_display_get_pixbuf (RetroCairoDisplay *self) * @pixbuf: a #GdkPixbuf * * Sets @pixbuf as the currently displayed video frame. + * + * retro_pixbuf_set_aspect_ratio() can be used to specify the aspect ratio for + * the pixbuf. Otherwise the core's aspect ratio will be used. */ void retro_cairo_display_set_pixbuf (RetroCairoDisplay *self, GdkPixbuf *pixbuf) { + gfloat aspect_ratio; + g_return_if_fail (self != NULL); if (self->pixbuf == pixbuf) @@ -327,6 +333,10 @@ retro_cairo_display_set_pixbuf (RetroCairoDisplay *self, if (pixbuf != NULL) self->pixbuf = g_object_ref (pixbuf); + aspect_ratio = retro_pixbuf_get_aspect_ratio (pixbuf); + if (aspect_ratio != 0.f) + self->aspect_ratio = aspect_ratio; + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PIXBUF]); } -- GitLab From 4b3cc26bd5c8395cc485e00f48a5f66176de46bd Mon Sep 17 00:00:00 2001 From: Alexander Mikhaylenko Date: Wed, 14 Aug 2019 13:59:15 +0500 Subject: [PATCH 6/6] retro-core-view: Update retro_core_view_set_pixbuf() docs Reflect the changes in RetroGLDisplay and RetroCairoDisplay in the last two commits. --- retro-gtk/retro-core-view.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/retro-gtk/retro-core-view.c b/retro-gtk/retro-core-view.c index 2866082..682c9f9 100644 --- a/retro-gtk/retro-core-view.c +++ b/retro-gtk/retro-core-view.c @@ -537,6 +537,9 @@ retro_core_view_set_core (RetroCoreView *self, * @pixbuf: a #GdkPixbuf * * Sets @pixbuf as the currently displayed video frame. + * + * retro_pixbuf_set_aspect_ratio() can be used to specify the aspect ratio for + * the pixbuf. Otherwise the core's aspect ratio will be used. */ void retro_core_view_set_pixbuf (RetroCoreView *self, -- GitLab