From 0e8d6a76b63c30a5897cae1b12d81709af23bfc1 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 08:27:49 +0200 Subject: [PATCH 01/68] tests: fix GtkBuilder signal callback prototypes Based on the Guix gtk2-fix-builder-test.patch. The callbacks are connected to notify-style signals and must take a GParamSpec pointer, not a GParamSpec by value. This avoids stack/register ABI problems on platforms such as ppc64el. --- gtk/tests/builder.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gtk/tests/builder.c b/gtk/tests/builder.c index ebba220e9..121012704 100644 --- a/gtk/tests/builder.c +++ b/gtk/tests/builder.c @@ -132,7 +132,7 @@ static int object = 0; static int object_after = 0; void /* exported for GtkBuilder */ -signal_normal (GtkWindow *window, GParamSpec spec) +signal_normal (GtkWindow *window, GParamSpec *spec) { g_assert (GTK_IS_WINDOW (window)); g_assert (normal == 0); @@ -142,7 +142,7 @@ signal_normal (GtkWindow *window, GParamSpec spec) } void /* exported for GtkBuilder */ -signal_after (GtkWindow *window, GParamSpec spec) +signal_after (GtkWindow *window, GParamSpec *spec) { g_assert (GTK_IS_WINDOW (window)); g_assert (normal == 1); @@ -152,7 +152,7 @@ signal_after (GtkWindow *window, GParamSpec spec) } void /* exported for GtkBuilder */ -signal_object (GtkButton *button, GParamSpec spec) +signal_object (GtkButton *button, GParamSpec *spec) { g_assert (GTK_IS_BUTTON (button)); g_assert (object == 0); @@ -162,7 +162,7 @@ signal_object (GtkButton *button, GParamSpec spec) } void /* exported for GtkBuilder */ -signal_object_after (GtkButton *button, GParamSpec spec) +signal_object_after (GtkButton *button, GParamSpec *spec) { g_assert (GTK_IS_BUTTON (button)); g_assert (object == 1); @@ -172,28 +172,28 @@ signal_object_after (GtkButton *button, GParamSpec spec) } void /* exported for GtkBuilder */ -signal_first (GtkButton *button, GParamSpec spec) +signal_first (GtkButton *button, GParamSpec *spec) { g_assert (normal == 0); normal = 10; } void /* exported for GtkBuilder */ -signal_second (GtkButton *button, GParamSpec spec) +signal_second (GtkButton *button, GParamSpec *spec) { g_assert (normal == 10); normal = 20; } void /* exported for GtkBuilder */ -signal_extra (GtkButton *button, GParamSpec spec) +signal_extra (GtkButton *button, GParamSpec *spec) { g_assert (normal == 20); normal = 30; } void /* exported for GtkBuilder */ -signal_extra2 (GtkButton *button, GParamSpec spec) +signal_extra2 (GtkButton *button, GParamSpec *spec) { g_assert (normal == 30); normal = 40; From 790b8c2b69c3e6d520e6b53f467fddc2f7bb80cf Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 08:34:50 +0200 Subject: [PATCH 02/68] gtkrc: search XDG data dirs for themes Based on the Guix gtk2-theme-paths.patch by Jookia. Keep the existing ~/.themes and gtk_rc_get_theme_dir() lookup paths, but check g_get_system_data_dirs() in between so profile- and prefix-based theme installations can be found. --- gtk/gtkrc.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gtk/gtkrc.c b/gtk/gtkrc.c index 7bfbea85c..d316a0d90 100644 --- a/gtk/gtkrc.c +++ b/gtk/gtkrc.c @@ -808,6 +808,8 @@ gtk_rc_parse_named (GtkRcContext *context, gchar *path = NULL; const gchar *home_dir; gchar *subpath; + const gchar * const *xdg_data_dirs; + gint i; if (type) subpath = g_strconcat ("gtk-2.0-", type, @@ -829,6 +831,22 @@ gtk_rc_parse_named (GtkRcContext *context, } } + if (!path) + { + xdg_data_dirs = g_get_system_data_dirs (); + for (i = 0; xdg_data_dirs[i]; i++) + { + path = g_build_filename (xdg_data_dirs[i], "themes", name, subpath, NULL); + if (g_file_test (path, G_FILE_TEST_EXISTS)) + break; + else + { + g_free (path); + path = NULL; + } + } + } + if (!path) { gchar *theme_dir = gtk_rc_get_theme_dir (); From d8b26dc0b746228343eb2f1532509b6ac567c041 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 11:25:19 +0200 Subject: [PATCH 03/68] gtktrayicon: use explicit void prototypes for reserved slots Based on the Mageia gtk+-2.24.32-warnings.patch. Declare the reserved GtkTrayIconClass function pointers with explicit void parameter lists to avoid strict-prototype warnings without changing the class layout. --- gtk/gtktrayicon.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gtk/gtktrayicon.h b/gtk/gtktrayicon.h index 9ad1c54f2..ab4eda2f5 100644 --- a/gtk/gtktrayicon.h +++ b/gtk/gtktrayicon.h @@ -46,12 +46,12 @@ struct _GtkTrayIconClass { GtkPlugClass parent_class; - void (*__gtk_reserved1); - void (*__gtk_reserved2); - void (*__gtk_reserved3); - void (*__gtk_reserved4); - void (*__gtk_reserved5); - void (*__gtk_reserved6); + void (*__gtk_reserved1) (void); + void (*__gtk_reserved2) (void); + void (*__gtk_reserved3) (void); + void (*__gtk_reserved4) (void); + void (*__gtk_reserved5) (void); + void (*__gtk_reserved6) (void); }; GType gtk_tray_icon_get_type (void) G_GNUC_CONST; From 292e512bc4a0ba9087dc2732ff51b103eeed2522 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:15:53 +0200 Subject: [PATCH 04/68] build: disable GNU visibility aliases on Apple platforms Based on the MacPorts patch-aliases.diff. Keep the existing GNU visibility alias generation on other platforms, but avoid it on Apple targets where this alias mechanism is not suitable. --- gdk/makegdkalias.pl | 2 +- gtk/makegtkalias.pl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gdk/makegdkalias.pl b/gdk/makegdkalias.pl index 97a6e6007..27646cc0a 100755 --- a/gdk/makegdkalias.pl +++ b/gdk/makegdkalias.pl @@ -14,7 +14,7 @@ print < -#ifdef G_HAVE_GNUC_VISIBILITY +#if defined(G_HAVE_GNUC_VISIBILITY) && !defined(__APPLE__) EOF diff --git a/gtk/makegtkalias.pl b/gtk/makegtkalias.pl index 3f956b05e..67ec7c973 100755 --- a/gtk/makegtkalias.pl +++ b/gtk/makegtkalias.pl @@ -14,7 +14,7 @@ print < -#ifdef G_HAVE_GNUC_VISIBILITY +#if defined(G_HAVE_GNUC_VISIBILITY) && !defined(__APPLE__) EOF From 72dcfc7ca5fc2a78b47b45e3fe67bfe0ed394911 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:16:45 +0200 Subject: [PATCH 05/68] quartz: add newer macOS version constants Based on the MacPorts patch-gdkquartz-osx-versions.diff. Extend the Quartz OS version enum so later macOS-specific code can distinguish Catalina, Big Sur, Monterey, Ventura, Sonoma, and Sequoia while preserving the existing older constants. --- gdk/quartz/gdkquartz.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gdk/quartz/gdkquartz.h b/gdk/quartz/gdkquartz.h index 8206db019..42aac50b9 100644 --- a/gdk/quartz/gdkquartz.h +++ b/gdk/quartz/gdkquartz.h @@ -54,7 +54,13 @@ typedef enum GDK_OSX_SIERRA = 12, GDK_OSX_HIGH_SIERRA = 13, GDK_OSX_MOJAVE = 14, - GDK_OSX_CURRENT = 14, + GDK_OSX_CATALINA = 15, + GDK_OSX_BIG_SUR = 16, + GDK_OSX_MONTEREY = 17, + GDK_OSX_VENTURA = 18, + GDK_OSX_SONOMA = 19, + GDK_OSX_SEQUOIA = 20, + GDK_OSX_CURRENT = 20, GDK_OSX_NEW = 99 } GdkOSXVersion; From 0bc8029032828499c3e8c98488b8c1c89d60254e Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:27:16 +0200 Subject: [PATCH 06/68] quartz: avoid focus locking on Mojave and newer Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. The NSView focus-locking API is deprecated on newer macOS releases and can cause expensive redraw behaviour. Keep the old path for older macOS versions, but avoid lockFocusIfCanDraw/unlockFocus on Mojave and newer. --- gdk/quartz/gdkdrawable-quartz.c | 3 ++- gdk/quartz/gdkwindow-quartz.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/gdk/quartz/gdkdrawable-quartz.c b/gdk/quartz/gdkdrawable-quartz.c index 11dd12737..eb520f08d 100644 --- a/gdk/quartz/gdkdrawable-quartz.c +++ b/gdk/quartz/gdkdrawable-quartz.c @@ -893,7 +893,8 @@ gdk_quartz_drawable_release_context (GdkDrawable *drawable, if (window_impl->in_paint_rect_count == 0) { _gdk_quartz_drawable_flush (drawable); - [window_impl->view unlockFocus]; + if (gdk_quartz_osx_version () < GDK_OSX_MOJAVE) + [window_impl->view unlockFocus]; } } else if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable)) diff --git a/gdk/quartz/gdkwindow-quartz.c b/gdk/quartz/gdkwindow-quartz.c index 6c7a2facb..205fd85e6 100644 --- a/gdk/quartz/gdkwindow-quartz.c +++ b/gdk/quartz/gdkwindow-quartz.c @@ -104,7 +104,8 @@ gdk_window_impl_quartz_get_context (GdkDrawable *drawable, * and for widgets that send fake expose events like the arrow * buttons in spinbuttons or the position marker in rulers. */ - if (window_impl->in_paint_rect_count == 0) + if (window_impl->in_paint_rect_count == 0 && + gdk_quartz_osx_version () < GDK_OSX_MOJAVE) { if (![window_impl->view lockFocusIfCanDraw]) return NULL; From 8b7fda063e32d8fa920ff76d60ced2cc895ffb13 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:29:27 +0200 Subject: [PATCH 07/68] quartz: map macOS 11 and newer to internal OS versions Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. GTK2 represents macOS 10.x releases by their minor version. Read the major version as well and map macOS 11 and newer into the same sequence so Big Sur and later releases can be detected correctly. --- gdk/quartz/gdkglobals-quartz.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/gdk/quartz/gdkglobals-quartz.c b/gdk/quartz/gdkglobals-quartz.c index f013f2830..4e596eef5 100644 --- a/gdk/quartz/gdkglobals-quartz.c +++ b/gdk/quartz/gdkglobals-quartz.c @@ -34,9 +34,20 @@ gdk_quartz_osx_version (void) if (minor == GDK_OSX_UNSUPPORTED) { - OSErr err = Gestalt (gestaltSystemVersionMinor, (SInt32*)&minor); + gint32 major = GDK_OSX_UNSUPPORTED; + OSErr err = Gestalt (gestaltSystemVersionMajor, (SInt32*)&major); g_return_val_if_fail (err == noErr, GDK_OSX_UNSUPPORTED); + + err = Gestalt (gestaltSystemVersionMinor, (SInt32*)&minor); + g_return_val_if_fail (err == noErr, GDK_OSX_UNSUPPORTED); + + /* GTK2 encodes macOS 10.x releases by their minor version. + * Map macOS 11+ into the same sequence: + * 11 -> 16, 12 -> 17, 13 -> 18, ... + */ + if (major > 10) + minor = major + 5; } if (minor < GDK_OSX_MIN) From 21fdf2eeaaa02ee2fc18374593ae3d8b01d17b61 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:32:30 +0200 Subject: [PATCH 08/68] quartz: use CGContext accessor on Yosemite and newer Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. Use the modern NSGraphicsContext CGContext accessor on Yosemite and newer, matching the existing Quartz window code, while preserving graphicsPort for older macOS versions. --- gtk/gtkquartz.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gtk/gtkquartz.c b/gtk/gtkquartz.c index 87ba87745..caf8404f8 100644 --- a/gtk/gtkquartz.c +++ b/gtk/gtkquartz.c @@ -67,7 +67,10 @@ _gtk_quartz_create_image_from_pixbuf (GdkPixbuf *pixbuf) } [nsimage lockFocus]; - context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; + if (gdk_quartz_osx_version () < GDK_OSX_YOSEMITE) + context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; + else + context = [[NSGraphicsContext currentContext] CGContext]; CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image); [nsimage unlockFocus]; From fe45d0944c35268c817aca1be9a5f9d6077f90a7 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:41:36 +0200 Subject: [PATCH 09/68] quartz: avoid screen update suppression on Mojave and newer Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. NSDisableScreenUpdates and NSEnableScreenUpdates are deprecated on newer macOS releases. Keep using them for older macOS versions, but avoid them on Mojave and newer. --- gdk/quartz/gdkwindow-quartz.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gdk/quartz/gdkwindow-quartz.c b/gdk/quartz/gdkwindow-quartz.c index 205fd85e6..5c1bde0cd 100644 --- a/gdk/quartz/gdkwindow-quartz.c +++ b/gdk/quartz/gdkwindow-quartz.c @@ -469,7 +469,8 @@ _gdk_windowing_before_process_all_updates (void) { in_process_all_updates = TRUE; - NSDisableScreenUpdates (); + if (gdk_quartz_osx_version () < GDK_OSX_MOJAVE) + NSDisableScreenUpdates (); } void @@ -503,7 +504,8 @@ _gdk_windowing_after_process_all_updates (void) in_process_all_updates = FALSE; - NSEnableScreenUpdates (); + if (gdk_quartz_osx_version () < GDK_OSX_MOJAVE) + NSEnableScreenUpdates (); } static void From 9d7c7e63d614759cdeafd672dd3d53ccef258825 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:49:48 +0200 Subject: [PATCH 10/68] quartz: request 8-bit layer contents on Big Sur and newer Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. Use a dynamic Objective-C call to request 8-bit layer contents on macOS 11 and newer, avoiding a hard QuartzCore/CALayer compile-time dependency and preserving old behaviour on older macOS releases. --- gdk/quartz/GdkQuartzView.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/gdk/quartz/GdkQuartzView.c b/gdk/quartz/GdkQuartzView.c index 368e040ec..bad737da2 100644 --- a/gdk/quartz/GdkQuartzView.c +++ b/gdk/quartz/GdkQuartzView.c @@ -599,6 +599,25 @@ gdk_screen_get_rgba_colormap (_gdk_screen); } +-(void)viewWillDraw +{ + /* macOS 11 (Big Sur) and newer may use a higher precision backing + * format for layer contents. For GTK2's 8-bit drawing this can add + * avoidable conversion overhead. Use a dynamic Objective-C call here + * instead of a hard QuartzCore/CALayer dependency, so old SDKs and old + * macOS targets keep building and keep their old behaviour. + */ + if (gdk_quartz_osx_version () >= GDK_OSX_BIG_SUR) + { + id layer = [self layer]; + + if (layer && [layer respondsToSelector: @selector(setContentsFormat:)]) + [layer setValue: @"RGBA8Uint" forKey: @"contentsFormat"]; + } + + [super viewWillDraw]; +} + -(void)drawRect: (NSRect)rect { GdkRectangle gdk_rect; From c1fc6ed6424828c3077a083423de93f4029fcbbf Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:54:51 +0200 Subject: [PATCH 11/68] quartz: avoid extra content invalidation on Big Sur and newer Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. Keep the existing Mojave-era setNeedsDisplay workaround for macOS 10.14 and 10.15, but avoid the extra full-window invalidation on Big Sur and newer where it can cause expensive redraw behaviour. --- gdk/quartz/gdkwindow-quartz.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gdk/quartz/gdkwindow-quartz.c b/gdk/quartz/gdkwindow-quartz.c index 5c1bde0cd..38ada80b2 100644 --- a/gdk/quartz/gdkwindow-quartz.c +++ b/gdk/quartz/gdkwindow-quartz.c @@ -491,8 +491,9 @@ _gdk_windowing_after_process_all_updates (void) /* 10.14 needs to be told that the view needs to be redrawn, see * https://gitlab.gnome.org/GNOME/gtk/issues/1479 */ - if (gdk_quartz_osx_version() >= GDK_OSX_MOJAVE) - [[nswindow contentView] setNeedsDisplay:YES]; + if (gdk_quartz_osx_version () >= GDK_OSX_MOJAVE && + gdk_quartz_osx_version () < GDK_OSX_BIG_SUR) + [[nswindow contentView] setNeedsDisplay:YES]; [nswindow enableFlushWindow]; [nswindow flushWindow]; [nswindow release]; From 8d4015ee54b85becc0f2935c3ca6f25905a06508 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 09:59:26 +0200 Subject: [PATCH 12/68] quartz: avoid extra resize-idle updates on Big Sur and newer Based on part of the MacPorts Big Sur performance patch for GIMP issue 5989. Keep the existing gdk_window_process_all_updates() call for non-Quartz targets and for older macOS releases, but avoid this extra resize-idle update path on Big Sur and newer where it can contribute to expensive full-window redraw behaviour. --- gtk/gtkcontainer.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gtk/gtkcontainer.c b/gtk/gtkcontainer.c index 2f7007fd2..e4c98c8c2 100644 --- a/gtk/gtkcontainer.c +++ b/gtk/gtkcontainer.c @@ -40,6 +40,9 @@ #include "gtktoolbar.h" #include #include +#ifdef GDK_WINDOWING_QUARTZ +#include "gdk/quartz/gdkquartz.h" +#endif #include "gtkalias.h" @@ -1357,7 +1360,16 @@ gtk_container_idle_sizer (gpointer data) gtk_container_check_resize (GTK_CONTAINER (widget)); } +#ifdef GDK_WINDOWING_QUARTZ + /* macOS 11 and newer can trigger expensive full-window redraws here. + * Keep the traditional update processing path for older Quartz targets, + * but let the normal GDK update idle handle updates on Big Sur and newer. + */ + if (gdk_quartz_osx_version () < GDK_OSX_BIG_SUR) + gdk_window_process_all_updates (); +#else gdk_window_process_all_updates (); +#endif return FALSE; } From 65f949bcded5e103d370f3cba89027318490e6b9 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 10:06:34 +0200 Subject: [PATCH 13/68] win32: improve mouse window lookup through transparent children Based on the MSYS2 0013_fix_mouse_events.patch. Use CWP_SKIPTRANSPARENT while walking from the desktop window, but stop skipping transparent windows after the first level so transparent child windows are still considered correctly. --- gdk/win32/gdkwindow-win32.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gdk/win32/gdkwindow-win32.c b/gdk/win32/gdkwindow-win32.c index b7ea0404a..1a21a1b67 100644 --- a/gdk/win32/gdkwindow-win32.c +++ b/gdk/win32/gdkwindow-win32.c @@ -2767,6 +2767,10 @@ _gdk_windowing_window_at_pointer (GdkDisplay *display, * WindowFromPoint() can find our windows, we follow similar logic * here, and ignore invisible and disabled windows. */ + UINT cwp_flags = CWP_SKIPDISABLED | + CWP_SKIPINVISIBLE | + CWP_SKIPTRANSPARENT; + hwnd = GetDesktopWindow (); do { window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd); @@ -2777,8 +2781,7 @@ _gdk_windowing_window_at_pointer (GdkDisplay *display, break; screen_to_client (hwnd, screen_pt, &client_pt); - hwndc = ChildWindowFromPointEx (hwnd, client_pt, CWP_SKIPDISABLED | - CWP_SKIPINVISIBLE); + hwndc = ChildWindowFromPointEx (hwnd, client_pt, cwp_flags); /* Verify that we're really inside the client area of the window */ if (hwndc != hwnd) @@ -2789,6 +2792,8 @@ _gdk_windowing_window_at_pointer (GdkDisplay *display, hwndc = hwnd; } + /* Only ignore top-level transparent windows */ + cwp_flags &= ~CWP_SKIPTRANSPARENT; } while (hwndc != hwnd && (hwnd = hwndc, 1)); } From d181fe8fc3d62dab2bc9640c28bd2c44eae24f59 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 10:07:25 +0200 Subject: [PATCH 14/68] icontheme: detect SVG icons by extension on Win32 Based on the MSYS2 0022-icontheme-win32-detect-SVG-files-by-extension.patch. Some Windows systems report .svg files with a generic or incorrect content type. On Win32, fall back to the .svg extension so SVG icons can still be detected. --- gtk/gtkicontheme.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index 0d4435c72..f8aaf9a3b 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -2945,6 +2945,10 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info, if (mime_type && strcmp (mime_type, "image/svg+xml") == 0) is_svg = TRUE; +#ifdef G_OS_WIN32 + else if (content_type && strcmp (content_type, ".svg") == 0) + is_svg = TRUE; +#endif } g_object_unref (file_info); From 4d7fc9558334fc36c7783cbd96f3dcdef3b6846b Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 10:08:01 +0200 Subject: [PATCH 15/68] win32: initialize key state before updating keymap Based on the MSYS2 0023-gdkkeys-win32.c-fix-initialisation-of-key_state-in-u.patch. Clear the key_state array before using it during keymap updates so stale state cannot affect layout handling, including Japanese IME input. --- gdk/win32/gdkkeys-win32.c | 1 + 1 file changed, 1 insertion(+) diff --git a/gdk/win32/gdkkeys-win32.c b/gdk/win32/gdkkeys-win32.c index d8ab30347..9a34083c4 100644 --- a/gdk/win32/gdkkeys-win32.c +++ b/gdk/win32/gdkkeys-win32.c @@ -692,6 +692,7 @@ update_keymap (GdkKeymap *gdk_keymap) if (hkls_len != keymap->layout_handles->len) keymap->keysym_tab = g_renew (guint, keymap->keysym_tab, keysym_tab_size); + memset (key_state, 0, sizeof(key_state)); memset (keymap->keysym_tab, 0, keysym_tab_size); g_array_set_size (keymap->layout_handles, hkls_len); g_array_set_size (keymap->options, hkls_len); From 757774345d1b942dfb056362bd45abfefff4e0df Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 10:09:36 +0200 Subject: [PATCH 16/68] win32: quote icon cache manifest in resource file Based on the MSYS2 0024-gtk-update-icon-cache-rc-quote.patch. Quote the manifest path when generating the gtk-update-icon-cache resource file so windres parses it correctly. --- gtk/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 8f5e496a3..c3c9a1493 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -1139,7 +1139,7 @@ $(GTK_UPDATE_ICON_CACHE_MANIFEST): echo '' ) >$@ $(GTK_UPDATE_ICON_CACHE_RC): - (echo -e '#include \nCREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST '$(GTK_UPDATE_ICON_CACHE_MANIFEST)) >$@ + (echo -e '#include \nCREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "'$(GTK_UPDATE_ICON_CACHE_MANIFEST)'"') >$@ $(GTK_UPDATE_ICON_CACHE_MANIFEST_OBJECT): $(GTK_UPDATE_ICON_CACHE_RC) $(GTK_UPDATE_ICON_CACHE_MANIFEST) $(WINDRES) --input $< --output $@ --output-format=coff From b622340b5b447d0094f5726127d9252278bf39cc Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 11:46:52 +0200 Subject: [PATCH 17/68] print: preserve default-printer wait during status updates Based on the openSUSE gtk2-default-printer.patch. Avoid treating printer status updates as user-driven printer selection changes, so the print dialog can continue waiting for a default printer that may appear later. --- gtk/gtkprintunixdialog.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gtk/gtkprintunixdialog.c b/gtk/gtkprintunixdialog.c index da9010b96..af64a5a37 100644 --- a/gtk/gtkprintunixdialog.c +++ b/gtk/gtkprintunixdialog.c @@ -796,7 +796,9 @@ printer_status_cb (GtkPrintBackend *backend, /* When the pause state change then we need to update sensitive property * of GTK_RESPONSE_OK button inside of selected_printer_changed function. */ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview)); + priv->internal_printer_change = TRUE; selected_printer_changed (selection, dialog); + priv->internal_printer_change = FALSE; if (gtk_print_backend_printer_list_is_done (backend) && gtk_printer_is_default (printer) && From 00177dd11c69e21e78ec92442cdf189cfc840a47 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 10:55:56 +0200 Subject: [PATCH 18/68] docs: use top_srcdir for GDK X11 header reference Based on the MSYS2 0005-Fix-header-ref.all.patch. Use an explicit top_srcdir-relative path for gdkx.h in the GDK reference documentation instead of a fragile relative path. --- docs/reference/gdk/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/gdk/Makefile.am b/docs/reference/gdk/Makefile.am index c0facc151..43a67508c 100644 --- a/docs/reference/gdk/Makefile.am +++ b/docs/reference/gdk/Makefile.am @@ -37,7 +37,7 @@ IGNORE_HFILES= \ # Extra files to add when scanning (relative to $srcdir) EXTRA_HFILES= \ - ../../../gdk/x11/gdkx.h + $(top_srcdir)/gdk/x11/gdkx.h # CFLAGS and LDFLAGS for compiling scan program. Only needed # if $(DOC_MODULE).types is non-empty. From 71023e03331f1b86bcc4af3aba0b9020d0aaf5b6 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 4 Aug 2026 15:34:12 +0200 Subject: [PATCH 19/68] gdk: add endian-aware 30-bit RGB converters Add specialized converters for both 10:10:10 channel layouts and both X image byte orders. Preserve depth-30 padding bits, avoid invalid negative shifts in the generic converter and select endian conversion at compile time. Validated with a real Xvfb 30-bit A/B pixel test and cross-target endian store checks. Derived from downstream commit 02a2c61f2dfc574a88343c81ddf10fc515136533. Co-authored-by: Daemonratte --- gdk/gdkrgb.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index b0ff66833..dddf1991c 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2242,6 +2242,68 @@ gdk_rgb_convert_8880_br (GdkRgbInfo *image_info, GdkImage *image, } } +#define DEFINE_GDK_RGB_CONVERT_101010(name, high, low, to_order) \ +static void \ +name (GdkRgbInfo *image_info, GdkImage *image, \ + gint x0, gint y0, gint width, gint height, \ + const guchar *buf, int rowstride, \ + gint x_align, gint y_align, GdkRgbCmap *cmap) \ +{ \ + int y, w; \ + guchar *obuf, *p; \ + gint bpl; \ + const guchar *bptr, *bp2; \ + guint32 r, g, b; \ + guint32 pixel; \ + guint32 alpha_mask; \ + \ + alpha_mask = image_info->visual->depth == 32 ? 0xc0000000U : 0; \ + \ + bptr = buf; \ + bpl = image->bpl; \ + obuf = ((guchar *) image->mem) + y0 * bpl + x0 * 4; \ + \ + for (y = 0; y < height; y++) \ + { \ + bp2 = bptr; \ + p = obuf; \ + w = width; \ + \ + while (w--) \ + { \ + r = *bp2++; \ + g = *bp2++; \ + b = *bp2++; \ + \ + r = (r << 2) | (r >> 6); \ + g = (g << 2) | (g >> 6); \ + b = (b << 2) | (b >> 6); \ + \ + pixel = alpha_mask | \ + (high << 20) | \ + (g << 10) | \ + low; \ + \ + *((guint32 *) p) = to_order (pixel); \ + p += 4; \ + } \ + \ + bptr += rowstride; \ + obuf += bpl; \ + } \ +} + +DEFINE_GDK_RGB_CONVERT_101010 (gdk_rgb_convert_101010_r_low_lsb, + b, r, GUINT32_TO_LE) +DEFINE_GDK_RGB_CONVERT_101010 (gdk_rgb_convert_101010_b_low_lsb, + r, b, GUINT32_TO_LE) +DEFINE_GDK_RGB_CONVERT_101010 (gdk_rgb_convert_101010_r_low_msb, + b, r, GUINT32_TO_BE) +DEFINE_GDK_RGB_CONVERT_101010 (gdk_rgb_convert_101010_b_low_msb, + r, b, GUINT32_TO_BE) + +#undef DEFINE_GDK_RGB_CONVERT_101010 + /* Generic truecolor/directcolor conversion function. Slow, but these are oddball modes. */ static void @@ -3195,6 +3257,34 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) #endif } #endif + else if (bpp == 32 && (depth == 30 || depth == 32) && + vtype == GDK_VISUAL_TRUE_COLOR && + byte_order == GDK_LSB_FIRST && + red_mask == 0x000003ff && + green_mask == 0x000ffc00 && + blue_mask == 0x3ff00000) + conv = gdk_rgb_convert_101010_r_low_lsb; + else if (bpp == 32 && (depth == 30 || depth == 32) && + vtype == GDK_VISUAL_TRUE_COLOR && + byte_order == GDK_LSB_FIRST && + red_mask == 0x3ff00000 && + green_mask == 0x000ffc00 && + blue_mask == 0x000003ff) + conv = gdk_rgb_convert_101010_b_low_lsb; + else if (bpp == 32 && (depth == 30 || depth == 32) && + vtype == GDK_VISUAL_TRUE_COLOR && + byte_order == GDK_MSB_FIRST && + red_mask == 0x000003ff && + green_mask == 0x000ffc00 && + blue_mask == 0x3ff00000) + conv = gdk_rgb_convert_101010_r_low_msb; + else if (bpp == 32 && (depth == 30 || depth == 32) && + vtype == GDK_VISUAL_TRUE_COLOR && + byte_order == GDK_MSB_FIRST && + red_mask == 0x3ff00000 && + green_mask == 0x000ffc00 && + blue_mask == 0x000003ff) + conv = gdk_rgb_convert_101010_b_low_msb; else if (vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_LSB_FIRST) { conv = gdk_rgb_convert_truecolor_lsb; From be64a902a5dfa76aaca4d66ad85505a76ef4016e Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 4 Aug 2026 17:55:45 +0200 Subject: [PATCH 20/68] gdk: optimize native big-endian RGB565 paths Add native big-endian four-pixel converters for RGB565, grayscale RGB565, and dithered RGB565. Select the block converters only where cross-target code generation is favorable: RGB on PowerPC, s390x, ARMv5+, and AArch64; grayscale on PowerPC, ARMv5+, AArch64, and m68k; and dither on PowerPC. Retain the scalar fallbacks everywhere else. The little-endian machine code remains byte-for-byte unchanged. No runtime endian checks, allocations, or additional image buffers are introduced. Validated with a complete build and test suite, deterministic and randomized big-endian formula tests, exact little-endian .text comparison, and cross-target code-generation checks. --- gdk/gdkrgb.c | 146 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 113 insertions(+), 33 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index dddf1991c..4f2e0d44f 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -1464,19 +1464,33 @@ gdk_rgb_convert_gray8_gray (GdkRgbInfo *image_info, GdkImage *image, } } -#if G_BYTE_ORDER == G_LITTLE_ENDIAN -#define HAIRY_CONVERT_565 + +#if G_BYTE_ORDER == G_BIG_ENDIAN +#if defined(__powerpc__) || defined(__powerpc64__) || \ + defined(__ppc__) || defined(__ppc64__) || \ + defined(__PPC__) || defined(__PPC64__) || \ + defined(__POWERPC__) || defined(_ARCH_PPC) +#define GDK_RGB_FAST_BE565_RGB +#define GDK_RGB_FAST_BE565_GRAY +#define GDK_RGB_FAST_BE565_DITHER +#elif defined(__s390x__) +#define GDK_RGB_FAST_BE565_RGB +#elif defined(__aarch64__) +#define GDK_RGB_FAST_BE565_RGB +#define GDK_RGB_FAST_BE565_GRAY +#elif defined(__arm__) && defined(__ARM_ARCH) && \ + __ARM_ARCH >= 5 +#define GDK_RGB_FAST_BE565_RGB +#define GDK_RGB_FAST_BE565_GRAY +#elif defined(__m68k__) +#define GDK_RGB_FAST_BE565_GRAY +#endif #endif -#ifdef HAIRY_CONVERT_565 /* Render a 24-bit RGB image in buf into the GdkImage, without dithering. - This assumes native byte ordering - what should really be done is to - check whether the image byte_order is consistent with the _ENDIAN - config flag, and if not, use a different function. - - This one is even faster than the one below - its inner loop loads 3 - words (i.e. 4 24-bit pixels), does a lot of shifting and masking, - then writes 2 words. */ + The four-pixel inner loop uses compile-time-specialized native word + layouts for little- and big-endian systems. */ +#if G_BYTE_ORDER == G_LITTLE_ENDIAN || defined(GDK_RGB_FAST_BE565_RGB) static void gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -1520,6 +1534,7 @@ gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image, r1b0g0r0 = ((guint32 *)bp2)[0]; g2r2b1g1 = ((guint32 *)bp2)[1]; b3g3r3b2 = ((guint32 *)bp2)[2]; +#if G_BYTE_ORDER == G_LITTLE_ENDIAN ((guint32 *)obptr)[0] = ((r1b0g0r0 & 0xf8) << 8) | ((r1b0g0r0 & 0xfc00) >> 5) | @@ -1534,6 +1549,22 @@ gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image, ((b3g3r3b2 & 0xf800) << 16) | ((b3g3r3b2 & 0xfc0000) << 3) | ((b3g3r3b2 & 0xf8000000) >> 11); +#else + ((guint32 *)obptr)[0] = + (r1b0g0r0 & 0xf8000000) | + ((r1b0g0r0 & 0x00fc0000) << 3) | + ((r1b0g0r0 & 0x0000f800) << 5) | + ((r1b0g0r0 & 0x000000f8) << 8) | + ((g2r2b1g1 & 0xfc000000) >> 21) | + ((g2r2b1g1 & 0x00f80000) >> 19); + ((guint32 *)obptr)[1] = + ((g2r2b1g1 & 0x0000f800) << 16) | + ((g2r2b1g1 & 0x000000fc) << 19) | + ((b3g3r3b2 & 0xf8000000) >> 11) | + ((b3g3r3b2 & 0x00f80000) >> 8) | + ((b3g3r3b2 & 0x0000fc00) >> 5) | + ((b3g3r3b2 & 0x000000f8) >> 3); +#endif bp2 += 12; obptr += 8; } @@ -1553,27 +1584,6 @@ gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image, } } #else -/* Render a 24-bit RGB image in buf into the GdkImage, without dithering. - This assumes native byte ordering - what should really be done is to - check whether the image byte_order is consistent with the _ENDIAN - config flag, and if not, use a different function. - - This routine is faster than the one included with Gtk 1.0 for a number - of reasons: - - 1. Shifting instead of lookup tables (less memory traffic). - - 2. Much less register pressure, especially because shifts are - in the code. - - 3. A memcpy is avoided (i.e. the transfer function). - - 4. On big-endian architectures, byte swapping is avoided. - - That said, it wouldn't be hard to make it even faster - just make an - inner loop that reads 3 words (i.e. 4 24-bit pixels), does a lot of - shifting and masking, then writes 2 words. -*/ static void gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -1607,7 +1617,7 @@ gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image, } #endif -#ifdef HAIRY_CONVERT_565 +#if G_BYTE_ORDER == G_LITTLE_ENDIAN || defined(GDK_RGB_FAST_BE565_GRAY) static void gdk_rgb_convert_565_gray (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -1645,6 +1655,7 @@ gdk_rgb_convert_565_gray (GdkRgbInfo *image_info, GdkImage *image, guint32 g3g2g1g0; g3g2g1g0 = ((guint32 *)bp2)[0]; +#if G_BYTE_ORDER == G_LITTLE_ENDIAN ((guint32 *)obptr)[0] = ((g3g2g1g0 & 0xf8) << 8) | ((g3g2g1g0 & 0xfc) << 3) | @@ -1659,6 +1670,22 @@ gdk_rgb_convert_565_gray (GdkRgbInfo *image_info, GdkImage *image, (g3g2g1g0 & 0xf8000000) | ((g3g2g1g0 & 0xfc000000) >> 5) | ((g3g2g1g0 & 0xf8000000) >> 11); +#else + ((guint32 *)obptr)[0] = + (g3g2g1g0 & 0xf8000000) | + ((g3g2g1g0 & 0xfc000000) >> 5) | + ((g3g2g1g0 & 0xf8000000) >> 11) | + ((g3g2g1g0 & 0x00f80000) >> 8) | + ((g3g2g1g0 & 0x00fc0000) >> 13) | + ((g3g2g1g0 & 0x00f80000) >> 19); + ((guint32 *)obptr)[1] = + ((g3g2g1g0 & 0x0000f800) << 16) | + ((g3g2g1g0 & 0x0000fc00) << 11) | + ((g3g2g1g0 & 0x0000f800) << 5) | + ((g3g2g1g0 & 0x000000f8) << 8) | + ((g3g2g1g0 & 0x000000fc) << 3) | + ((g3g2g1g0 & 0x000000f8) >> 3); +#endif bp2 += 4; obptr += 8; } @@ -1745,7 +1772,7 @@ gdk_rgb_convert_565_br (GdkRgbInfo *image_info, GdkImage *image, /* Thanks to Ray Lehtiniemi for a patch that resulted in a ~25% speedup in this mode. */ -#ifdef HAIRY_CONVERT_565 +#if G_BYTE_ORDER == G_LITTLE_ENDIAN || defined(GDK_RGB_FAST_BE565_DITHER) static void gdk_rgb_convert_565_d (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -1800,6 +1827,7 @@ gdk_rgb_convert_565_d (GdkRgbInfo *image_info, GdkImage *image, r1b0g0r0 = ((guint32 *)bp2)[0]; g2r2b1g1 = ((guint32 *)bp2)[1]; b3g3r3b2 = ((guint32 *)bp2)[2]; +#if G_BYTE_ORDER == G_LITTLE_ENDIAN rgb02 = ((r1b0g0r0 & 0xff) << 20) + ((r1b0g0r0 & 0xff00) << 2) + @@ -1846,6 +1874,54 @@ gdk_rgb_convert_565_d (GdkRgbInfo *image_info, GdkImage *image, ((rgb13 & 0x0f800000) << 4) | ((rgb13 & 0x0003f000) << 9) | ((rgb13 & 0x000000f8) << 13); +#else + rgb02 = + ((r1b0g0r0 & 0xff000000) >> 4) + + ((r1b0g0r0 & 0x00ff0000) >> 6) + + ((r1b0g0r0 & 0x0000ff00) >> 8) + + dmp[x & (DM_WIDTH - 1)]; + rgb02 += 0x10040100 + - ((rgb02 & 0x1e0001e0) >> 5) + - ((rgb02 & 0x00070000) >> 6); + rgb13 = + ((r1b0g0r0 & 0x000000ff) << 20) + + ((g2r2b1g1 & 0xff000000) >> 14) + + ((g2r2b1g1 & 0x00ff0000) >> 16) + + dmp[(x + 1) & (DM_WIDTH - 1)]; + rgb13 += 0x10040100 + - ((rgb13 & 0x1e0001e0) >> 5) + - ((rgb13 & 0x00070000) >> 6); + ((guint32 *)obptr)[0] = + ((rgb02 & 0x0f800000) << 4) | + ((rgb02 & 0x0003f000) << 9) | + ((rgb02 & 0x000000f8) << 13) | + ((rgb13 & 0x0f800000) >> 12) | + ((rgb13 & 0x0003f000) >> 7) | + ((rgb13 & 0x000000f8) >> 3); + rgb02 = + ((g2r2b1g1 & 0x0000ff00) << 12) + + ((g2r2b1g1 & 0x000000ff) << 10) + + ((b3g3r3b2 & 0xff000000) >> 24) + + dmp[(x + 2) & (DM_WIDTH - 1)]; + rgb02 += 0x10040100 + - ((rgb02 & 0x1e0001e0) >> 5) + - ((rgb02 & 0x00070000) >> 6); + rgb13 = + ((b3g3r3b2 & 0x00ff0000) << 4) + + ((b3g3r3b2 & 0x0000ff00) << 2) + + (b3g3r3b2 & 0x000000ff) + + dmp[(x + 3) & (DM_WIDTH - 1)]; + rgb13 += 0x10040100 + - ((rgb13 & 0x1e0001e0) >> 5) + - ((rgb13 & 0x00070000) >> 6); + ((guint32 *)obptr)[1] = + ((rgb02 & 0x0f800000) << 4) | + ((rgb02 & 0x0003f000) << 9) | + ((rgb02 & 0x000000f8) << 13) | + ((rgb13 & 0x0f800000) >> 12) | + ((rgb13 & 0x0003f000) >> 7) | + ((rgb13 & 0x000000f8) >> 3); +#endif bp2 += 12; obptr += 8; } @@ -1916,6 +1992,10 @@ gdk_rgb_convert_565_d (GdkRgbInfo *image_info, GdkImage *image, } #endif +#undef GDK_RGB_FAST_BE565_RGB +#undef GDK_RGB_FAST_BE565_GRAY +#undef GDK_RGB_FAST_BE565_DITHER + static void gdk_rgb_convert_555 (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, From e92e770558e9ab5a44f8f7f50984412e444e01e9 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 4 Aug 2026 18:46:39 +0200 Subject: [PATCH 21/68] gdk: support 3-bit StaticColor visuals --- gdk/gdkrgb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 4f2e0d44f..cd848e902 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -3431,7 +3431,7 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv = gdk_rgb_convert_gray4_pack; conv_d = gdk_rgb_convert_gray4_d_pack; } - else if (bpp == 4 && depth == 4 && + else if (bpp == 4 && depth <= 4 && depth >= 3 && vtype == GDK_VISUAL_STATIC_COLOR) conv = gdk_rgb_convert_4_pack; From b58fc237658965b313abb751deb71574584161c2 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 4 Aug 2026 19:08:54 +0200 Subject: [PATCH 22/68] gdk: support 2-bit StaticColor visuals --- gdk/gdkrgb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index cd848e902..edb88c6d8 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -3431,7 +3431,7 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv = gdk_rgb_convert_gray4_pack; conv_d = gdk_rgb_convert_gray4_d_pack; } - else if (bpp == 4 && depth <= 4 && depth >= 3 && + else if (bpp == 4 && depth <= 4 && depth >= 2 && vtype == GDK_VISUAL_STATIC_COLOR) conv = gdk_rgb_convert_4_pack; From a923aa3120628bec953ac12624417cd3378da8f3 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 4 Aug 2026 19:41:58 +0200 Subject: [PATCH 23/68] gdk: fix packed StaticColor dithering --- gdk/gdkrgb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index edb88c6d8..3da8c820f 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2697,7 +2697,7 @@ gdk_rgb_convert_4_pack (GdkRgbInfo *image_info, GdkImage *image, r = *bp2++; g = *bp2++; b = *bp2++; - dith = (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) | 3; + dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 3; ix = (((r + dith) & 0x100) >> 2) | (((g + 258 - dith) & 0x100) >> 5) | (((b + dith) & 0x100) >> 8); @@ -2724,7 +2724,7 @@ gdk_rgb_convert_4_pack (GdkRgbInfo *image_info, GdkImage *image, r = *bp2++; g = *bp2++; b = *bp2++; - dith = (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) | 3; + dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 3; ix = (((r + dith) & 0x100) >> 2) | (((g + 258 - dith) & 0x100) >> 5) | (((b + dith) & 0x100) >> 8); From 8984300bbaa4d8f77ebea668b8cbe416d6bc3546 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 4 Aug 2026 20:24:57 +0200 Subject: [PATCH 24/68] gdk: optimize packed StaticColor conversion --- gdk/gdkrgb.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 3da8c820f..e4f63ddaf 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2706,7 +2706,7 @@ gdk_rgb_convert_4_pack (GdkRgbInfo *image_info, GdkImage *image, obptr++; x++; } - while (x < width) + while (x + 1 < width) { r = *bp2++; g = *bp2++; @@ -2717,9 +2717,6 @@ gdk_rgb_convert_4_pack (GdkRgbInfo *image_info, GdkImage *image, (((b + dith) & 0x100) >> 8); pix0 = (colorcube_d[ix]); x++; - if (x == width) - pix1 = (*obptr & 0x0F); - else { r = *bp2++; g = *bp2++; @@ -2733,6 +2730,18 @@ gdk_rgb_convert_4_pack (GdkRgbInfo *image_info, GdkImage *image, } *obptr++ = (pix0 << 4) | pix1; } + if (x < width) + { + r = *bp2++; + g = *bp2++; + b = *bp2++; + dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 3; + ix = (((r + dith) & 0x100) >> 2) | + (((g + 258 - dith) & 0x100) >> 5) | + (((b + dith) & 0x100) >> 8); + pix0 = (colorcube_d[ix]); + *obptr = (pix0 << 4) | (*obptr & 0x0F); + } bptr += rowstride; obuf += bpl; } From 8da0ce4261f6bce6b6612e3d23d6a98985bf40f3 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 5 Aug 2026 12:12:13 +0200 Subject: [PATCH 25/68] gdk: add high-depth RGB image drawing Add RGB48 and RGBX64 drawing APIs with direct conversion for common 15-, 16-, 24-, 30- and 32-bit TrueColor visuals. Preserve the existing GdkRGB path as a fallback for unsupported visuals and maximum dithering. --- docs/reference/gdk/gdk2-sections.txt | 4 + gdk/gdk.symbols | 9 + gdk/gdkrgb.c | 690 +++++++++++++++++++++++++++ gdk/gdkrgb.h | 42 ++ 4 files changed, 745 insertions(+) diff --git a/docs/reference/gdk/gdk2-sections.txt b/docs/reference/gdk/gdk2-sections.txt index c3d6040b7..4987c31ee 100644 --- a/docs/reference/gdk/gdk2-sections.txt +++ b/docs/reference/gdk/gdk2-sections.txt @@ -335,6 +335,10 @@ gdk_draw_indexed_image gdk_draw_gray_image gdk_draw_rgb_32_image gdk_draw_rgb_32_image_dithalign +gdk_draw_rgb_48_image +gdk_draw_rgb_48_image_dithalign +gdk_draw_rgbx_64_image +gdk_draw_rgbx_64_image_dithalign GdkRgbDither diff --git a/gdk/gdk.symbols b/gdk/gdk.symbols index ee1bc0c98..7f553df52 100644 --- a/gdk/gdk.symbols +++ b/gdk/gdk.symbols @@ -1118,6 +1118,15 @@ gdk_draw_rgb_image_dithalign #endif #endif +#if IN_HEADER(__GDK_RGB_H__) +#if IN_FILE(__GDK_RGB_C__) +gdk_draw_rgb_48_image +gdk_draw_rgb_48_image_dithalign +gdk_draw_rgbx_64_image +gdk_draw_rgbx_64_image_dithalign +#endif +#endif + #if IN_HEADER(__GDK_SCREEN_H__) #if IN_FILE(__GDK_SCREEN_C__) gdk_screen_get_type G_GNUC_CONST diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index e4f63ddaf..125999da4 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -3548,6 +3548,696 @@ gdk_rgb_get_info_from_drawable (GdkDrawable *drawable) return gdk_rgb_get_info_from_colormap (cmap); } + +/* High-depth RGB input. */ + +#define GDK_RGB16_STORE_RGB(p, r, g, b, a) G_STMT_START { \ + (p)[0] = (r) >> 8; \ + (p)[1] = (g) >> 8; \ + (p)[2] = (b) >> 8; \ + (p) += 3; \ +} G_STMT_END + +#define GDK_RGB16_STORE_BGR(p, r, g, b, a) G_STMT_START { \ + (p)[0] = (b) >> 8; \ + (p)[1] = (g) >> 8; \ + (p)[2] = (r) >> 8; \ + (p) += 3; \ +} G_STMT_END + +#define GDK_RGB16_STORE_BGRA(p, r, g, b, a) G_STMT_START { \ + (p)[0] = (b) >> 8; \ + (p)[1] = (g) >> 8; \ + (p)[2] = (r) >> 8; \ + (p)[3] = 0xff; \ + (p) += 4; \ +} G_STMT_END + +#define GDK_RGB16_STORE_ARGB(p, r, g, b, a) G_STMT_START { \ + (p)[0] = 0xff; \ + (p)[1] = (r) >> 8; \ + (p)[2] = (g) >> 8; \ + (p)[3] = (b) >> 8; \ + (p) += 4; \ +} G_STMT_END + +#define GDK_RGB16_STORE_RGB0_NATIVE(p, r, g, b, a) G_STMT_START { \ + guint32 pixel__ = (((b) >> 8) << 16) | \ + (((g) >> 8) << 8) | \ + ((r) >> 8); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 4; \ +} G_STMT_END + +#define GDK_RGB16_STORE_565(p, r, g, b, a) G_STMT_START { \ + guint16 pixel__ = ((r) & 0xf800) | \ + (((g) & 0xfc00) >> 5) | \ + ((b) >> 11); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 2; \ +} G_STMT_END + +#define GDK_RGB16_STORE_565_BR(p, r, g, b, a) G_STMT_START { \ + guint16 pixel__ = ((r) & 0xf800) | \ + (((g) & 0xfc00) >> 5) | \ + ((b) >> 11); \ + pixel__ = (pixel__ >> 8) | (pixel__ << 8); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 2; \ +} G_STMT_END + +#define GDK_RGB16_STORE_555(p, r, g, b, a) G_STMT_START { \ + guint16 pixel__ = (((r) & 0xf800) >> 1) | \ + (((g) & 0xf800) >> 6) | \ + ((b) >> 11); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 2; \ +} G_STMT_END + +#define GDK_RGB16_STORE_555_BR(p, r, g, b, a) G_STMT_START { \ + guint16 pixel__ = (((r) & 0xf800) >> 1) | \ + (((g) & 0xf800) >> 6) | \ + ((b) >> 11); \ + pixel__ = (pixel__ >> 8) | (pixel__ << 8); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 2; \ +} G_STMT_END + +#define GDK_RGB16_STORE_101010_R_LOW_LE(p, r, g, b, a) \ +G_STMT_START { \ + guint32 pixel__ = (a) | \ + (((b) >> 6) << 20) | \ + (((g) >> 6) << 10) | \ + ((r) >> 6); \ + pixel__ = GUINT32_TO_LE (pixel__); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 4; \ +} G_STMT_END + +#define GDK_RGB16_STORE_101010_B_LOW_LE(p, r, g, b, a) \ +G_STMT_START { \ + guint32 pixel__ = (a) | \ + (((r) >> 6) << 20) | \ + (((g) >> 6) << 10) | \ + ((b) >> 6); \ + pixel__ = GUINT32_TO_LE (pixel__); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 4; \ +} G_STMT_END + +#define GDK_RGB16_STORE_101010_R_LOW_BE(p, r, g, b, a) \ +G_STMT_START { \ + guint32 pixel__ = (a) | \ + (((b) >> 6) << 20) | \ + (((g) >> 6) << 10) | \ + ((r) >> 6); \ + pixel__ = GUINT32_TO_BE (pixel__); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 4; \ +} G_STMT_END + +#define GDK_RGB16_STORE_101010_B_LOW_BE(p, r, g, b, a) \ +G_STMT_START { \ + guint32 pixel__ = (a) | \ + (((r) >> 6) << 20) | \ + (((g) >> 6) << 10) | \ + ((b) >> 6); \ + pixel__ = GUINT32_TO_BE (pixel__); \ + memcpy ((p), &pixel__, sizeof (pixel__)); \ + (p) += 4; \ +} G_STMT_END + +#define DEFINE_GDK_RGB16_CONVERTER(name, words, pixel_bytes, store) \ +static void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, \ + gint y0, \ + gint width, \ + gint height, \ + const guchar *buf, \ + gint rowstride, \ + gint x_align, \ + gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + const guchar *row = buf; \ + const guint16 *src; \ + guchar *dst_row; \ + guchar *dst; \ + guint32 r, g, b; \ + guint32 alpha_mask; \ + gint x, y; \ + \ + (void) image_info; \ + (void) x_align; \ + (void) y_align; \ + (void) cmap; \ + \ + dst_row = ((guchar *) image->mem) + \ + y0 * image->bpl + x0 * (pixel_bytes); \ + alpha_mask = image->depth == 32 ? 0xc0000000U : 0; \ + (void) alpha_mask; \ + \ + for (y = 0; y < height; y++) \ + { \ + src = (const guint16 *) row; \ + dst = dst_row; \ + \ + for (x = 0; x < width; x++) \ + { \ + r = src[0]; \ + g = src[1]; \ + b = src[2]; \ + src += (words); \ + store (dst, r, g, b, alpha_mask); \ + } \ + \ + row += rowstride; \ + dst_row += image->bpl; \ + } \ +} + +#define DEFINE_GDK_RGB16_PAIR(suffix, pixel_bytes, store) \ + DEFINE_GDK_RGB16_CONVERTER (gdk_rgb48_convert_ ## suffix, \ + 3, pixel_bytes, store) \ + DEFINE_GDK_RGB16_CONVERTER (gdk_rgbx64_convert_ ## suffix, \ + 4, pixel_bytes, store) + +DEFINE_GDK_RGB16_PAIR (rgb, 3, GDK_RGB16_STORE_RGB) +DEFINE_GDK_RGB16_PAIR (bgr, 3, GDK_RGB16_STORE_BGR) +DEFINE_GDK_RGB16_PAIR (bgra, 4, GDK_RGB16_STORE_BGRA) +DEFINE_GDK_RGB16_PAIR (argb, 4, GDK_RGB16_STORE_ARGB) +DEFINE_GDK_RGB16_PAIR (rgb0_native, 4, GDK_RGB16_STORE_RGB0_NATIVE) +DEFINE_GDK_RGB16_PAIR (565, 2, GDK_RGB16_STORE_565) +DEFINE_GDK_RGB16_PAIR (565_br, 2, GDK_RGB16_STORE_565_BR) +DEFINE_GDK_RGB16_PAIR (555, 2, GDK_RGB16_STORE_555) +DEFINE_GDK_RGB16_PAIR (555_br, 2, GDK_RGB16_STORE_555_BR) +DEFINE_GDK_RGB16_PAIR (101010_r_low_le, 4, + GDK_RGB16_STORE_101010_R_LOW_LE) +DEFINE_GDK_RGB16_PAIR (101010_b_low_le, 4, + GDK_RGB16_STORE_101010_B_LOW_LE) +DEFINE_GDK_RGB16_PAIR (101010_r_low_be, 4, + GDK_RGB16_STORE_101010_R_LOW_BE) +DEFINE_GDK_RGB16_PAIR (101010_b_low_be, 4, + GDK_RGB16_STORE_101010_B_LOW_BE) + +#undef DEFINE_GDK_RGB16_PAIR +#undef DEFINE_GDK_RGB16_CONVERTER + +#undef GDK_RGB16_STORE_RGB +#undef GDK_RGB16_STORE_BGR +#undef GDK_RGB16_STORE_BGRA +#undef GDK_RGB16_STORE_ARGB +#undef GDK_RGB16_STORE_RGB0_NATIVE +#undef GDK_RGB16_STORE_565 +#undef GDK_RGB16_STORE_565_BR +#undef GDK_RGB16_STORE_555 +#undef GDK_RGB16_STORE_555_BR +#undef GDK_RGB16_STORE_101010_R_LOW_LE +#undef GDK_RGB16_STORE_101010_B_LOW_LE +#undef GDK_RGB16_STORE_101010_R_LOW_BE +#undef GDK_RGB16_STORE_101010_B_LOW_BE + +static void +gdk_rgb16_to_stage (GdkRgbInfo *image_info, + const guchar *buf, + gint rowstride, + gint width, + gint height, + gint words) +{ + const guchar *row = buf; + const guint16 *src; + guchar *dst_row = gdk_rgb_ensure_stage (image_info); + guchar *dst; + gint x, y; + + for (y = 0; y < height; y++) + { + src = (const guint16 *) row; + dst = dst_row; + + for (x = 0; x < width; x++) + { + *dst++ = src[0] >> 8; + *dst++ = src[1] >> 8; + *dst++ = src[2] >> 8; + src += words; + } + + row += rowstride; + dst_row += STAGE_ROWSTRIDE; + } +} + +static void +gdk_draw_rgb16_fallback (GdkRgbInfo *image_info, + GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + const guchar *buf, + gint words, + gint rowstride, + GdkRgbConvFunc conv, + gint xdith, + gint ydith) +{ + GdkScreen *screen; + GdkImage *image; + const guchar *buf_ptr; + gint depth; + gint x0, y0; + gint xs0, ys0; + gint width1, height1; + + if (image_info->bitmap) + { + if (image_info->own_gc == NULL) + image_info->own_gc = gdk_gc_new (drawable); + + gc = image_info->own_gc; + } + + screen = gdk_drawable_get_screen (drawable); + depth = image_info->visual->depth; + + for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT) + { + height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT); + + for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH) + { + width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH); + + buf_ptr = buf + (gssize) y0 * rowstride + + x0 * words * sizeof (guint16); + + gdk_rgb16_to_stage (image_info, + buf_ptr, + rowstride, + width1, + height1, + words); + + image = _gdk_image_get_scratch (screen, + width1, + height1, + depth, + &xs0, + &ys0); + + conv (image_info, + image, + xs0, ys0, + width1, height1, + image_info->stage_buf, + STAGE_ROWSTRIDE, + x + x0 + xdith, + y + y0 + ydith, + NULL); + +#ifndef DONT_ACTUALLY_DRAW + gdk_draw_image (drawable, + gc, + image, + xs0, ys0, + x + x0, y + y0, + width1, height1); +#endif + } + } +} + +static GdkRgbConvFunc +gdk_rgb16_select_converter (GdkRgbInfo *image_info, + gboolean is_rgbx) +{ + GdkVisual *visual = image_info->visual; + GdkByteOrder byte_order; + guint32 red_mask, green_mask, blue_mask; + gint depth; + gint bits_per_pixel; + gboolean byte_reversed; + gboolean mask_rgb; + gboolean mask_bgr; + +#define SELECT_PAIR(suffix) \ + return is_rgbx ? gdk_rgbx64_convert_ ## suffix \ + : gdk_rgb48_convert_ ## suffix + + if (visual->type != GDK_VISUAL_TRUE_COLOR) + return NULL; + + depth = visual->depth; + byte_order = visual->byte_order; + red_mask = visual->red_mask; + green_mask = visual->green_mask; + blue_mask = visual->blue_mask; + bits_per_pixel = image_info->bpp * 8; + +#if G_BYTE_ORDER == G_BIG_ENDIAN + byte_reversed = byte_order == GDK_LSB_FIRST; +#else + byte_reversed = byte_order == GDK_MSB_FIRST; +#endif + + mask_rgb = red_mask == 0xff0000 && + green_mask == 0x00ff00 && + blue_mask == 0x0000ff; + + mask_bgr = red_mask == 0x0000ff && + green_mask == 0x00ff00 && + blue_mask == 0xff0000; + + if (bits_per_pixel == 16 && depth == 16 && + red_mask == 0xf800 && green_mask == 0x07e0 && + blue_mask == 0x001f) + { + if (byte_reversed) + SELECT_PAIR (565_br); + SELECT_PAIR (565); + } + + if (bits_per_pixel == 16 && depth == 15 && + red_mask == 0x7c00 && green_mask == 0x03e0 && + blue_mask == 0x001f) + { + if (byte_reversed) + SELECT_PAIR (555_br); + SELECT_PAIR (555); + } + + if (bits_per_pixel == 24 && depth == 24 && + ((mask_rgb && byte_order == GDK_LSB_FIRST) || + (mask_bgr && byte_order == GDK_MSB_FIRST))) + SELECT_PAIR (bgr); + + if (bits_per_pixel == 24 && depth == 24 && + ((mask_rgb && byte_order == GDK_MSB_FIRST) || + (mask_bgr && byte_order == GDK_LSB_FIRST))) + SELECT_PAIR (rgb); + + if (bits_per_pixel == 32 && + (depth == 24 || depth == 32) && + mask_rgb && byte_order == GDK_MSB_FIRST) + SELECT_PAIR (argb); + + if (bits_per_pixel == 32 && + (depth == 24 || depth == 32) && + mask_rgb && byte_order == GDK_LSB_FIRST) + SELECT_PAIR (bgra); + +#if G_BYTE_ORDER == G_BIG_ENDIAN + if (bits_per_pixel == 32 && depth == 24 && + mask_bgr && byte_order == GDK_MSB_FIRST) + SELECT_PAIR (rgb0_native); +#else + if (bits_per_pixel == 32 && depth == 24 && + mask_bgr && byte_order == GDK_LSB_FIRST) + SELECT_PAIR (rgb0_native); +#endif + + if (bits_per_pixel == 32 && + (depth == 30 || depth == 32) && + byte_order == GDK_LSB_FIRST && + red_mask == 0x000003ff && + green_mask == 0x000ffc00 && + blue_mask == 0x3ff00000) + SELECT_PAIR (101010_r_low_le); + + if (bits_per_pixel == 32 && + (depth == 30 || depth == 32) && + byte_order == GDK_LSB_FIRST && + red_mask == 0x3ff00000 && + green_mask == 0x000ffc00 && + blue_mask == 0x000003ff) + SELECT_PAIR (101010_b_low_le); + + if (bits_per_pixel == 32 && + (depth == 30 || depth == 32) && + byte_order == GDK_MSB_FIRST && + red_mask == 0x000003ff && + green_mask == 0x000ffc00 && + blue_mask == 0x3ff00000) + SELECT_PAIR (101010_r_low_be); + + if (bits_per_pixel == 32 && + (depth == 30 || depth == 32) && + byte_order == GDK_MSB_FIRST && + red_mask == 0x3ff00000 && + green_mask == 0x000ffc00 && + blue_mask == 0x000003ff) + SELECT_PAIR (101010_b_low_be); + +#undef SELECT_PAIR + return NULL; +} + +static void +gdk_draw_rgb16_image (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *buf, + gint rowstride, + gint xdith, + gint ydith, + gboolean is_rgbx) +{ + GdkRgbInfo *image_info; + GdkRgbConvFunc converter; + gint words; + gboolean dithered; + + g_return_if_fail (GDK_IS_DRAWABLE (drawable)); + g_return_if_fail (GDK_IS_GC (gc)); + g_return_if_fail (buf != NULL); + g_return_if_fail ((((guintptr) buf) & 1) == 0); + g_return_if_fail ((rowstride & 1) == 0); + + if (width <= 0 || height <= 0) + return; + + image_info = gdk_rgb_get_info_from_drawable (drawable); + if (image_info == NULL) + return; + + words = is_rgbx ? 4 : 3; + converter = NULL; + + if (dith == GDK_RGB_DITHER_NONE || + dith == GDK_RGB_DITHER_NORMAL) + converter = gdk_rgb16_select_converter (image_info, is_rgbx); + + if (converter != NULL) + { + gdk_draw_rgb_image_core (image_info, + drawable, + gc, + x, y, + width, height, + (const guchar *) buf, + words * sizeof (guint16), + rowstride, + converter, + NULL, + xdith, ydith); + return; + } + + dithered = + !(dith == GDK_RGB_DITHER_NONE || + (dith == GDK_RGB_DITHER_NORMAL && + !image_info->dith_default)); + + converter = dithered ? image_info->conv_d + : image_info->conv; + + gdk_draw_rgb16_fallback (image_info, + drawable, + gc, + x, y, + width, height, + (const guchar *) buf, + words, + rowstride, + converter, + xdith, ydith); +} + +/** + * gdk_draw_rgb_48_image: + * @drawable: a #GdkDrawable + * @gc: a #GdkGC + * @x: destination X coordinate + * @y: destination Y coordinate + * @width: image width + * @height: image height + * @dith: dithering mode + * @rgb_buf: host-endian RGB data with three 16-bit channels per pixel + * @rowstride: number of bytes between consecutive rows + * + * Draws 48-bit RGB image data. + **/ +void +gdk_draw_rgb_48_image (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgb_buf, + gint rowstride) +{ + gdk_draw_rgb16_image (drawable, + gc, + x, + y, + width, + height, + dith, + rgb_buf, + rowstride, + 0, + 0, + FALSE); +} + +/** + * gdk_draw_rgb_48_image_dithalign: + * @drawable: a #GdkDrawable + * @gc: a #GdkGC + * @x: destination X coordinate + * @y: destination Y coordinate + * @width: image width + * @height: image height + * @dith: dithering mode + * @rgb_buf: host-endian RGB data with three 16-bit channels per pixel + * @rowstride: number of bytes between consecutive rows + * @xdith: X dither offset + * @ydith: Y dither offset + * + * Draws 48-bit RGB image data with explicit dither alignment. + **/ +void +gdk_draw_rgb_48_image_dithalign (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgb_buf, + gint rowstride, + gint xdith, + gint ydith) +{ + gdk_draw_rgb16_image (drawable, + gc, + x, + y, + width, + height, + dith, + rgb_buf, + rowstride, + xdith, + ydith, + FALSE); +} + +/** + * gdk_draw_rgbx_64_image: + * @drawable: a #GdkDrawable + * @gc: a #GdkGC + * @x: destination X coordinate + * @y: destination Y coordinate + * @width: image width + * @height: image height + * @dith: dithering mode + * @rgbx_buf: host-endian RGBX data with four 16-bit channels per pixel + * @rowstride: number of bytes between consecutive rows + * + * Draws 64-bit RGBX image data. The X channel is ignored. + **/ +void +gdk_draw_rgbx_64_image (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgbx_buf, + gint rowstride) +{ + gdk_draw_rgb16_image (drawable, + gc, + x, + y, + width, + height, + dith, + rgbx_buf, + rowstride, + 0, + 0, + TRUE); +} + +/** + * gdk_draw_rgbx_64_image_dithalign: + * @drawable: a #GdkDrawable + * @gc: a #GdkGC + * @x: destination X coordinate + * @y: destination Y coordinate + * @width: image width + * @height: image height + * @dith: dithering mode + * @rgbx_buf: host-endian RGBX data with four 16-bit channels per pixel + * @rowstride: number of bytes between consecutive rows + * @xdith: X dither offset + * @ydith: Y dither offset + * + * Draws 64-bit RGBX image data with explicit dither alignment. + * The X channel is ignored. + **/ +void +gdk_draw_rgbx_64_image_dithalign (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgbx_buf, + gint rowstride, + gint xdith, + gint ydith) +{ + gdk_draw_rgb16_image (drawable, + gc, + x, + y, + width, + height, + dith, + rgbx_buf, + rowstride, + xdith, + ydith, + TRUE); +} + + void gdk_draw_rgb_image (GdkDrawable *drawable, GdkGC *gc, diff --git a/gdk/gdkrgb.h b/gdk/gdkrgb.h index dd71d403f..44c576fb5 100644 --- a/gdk/gdkrgb.h +++ b/gdk/gdkrgb.h @@ -147,6 +147,48 @@ gboolean gdk_rgb_colormap_ditherable (GdkColormap *cmap); #endif #endif /* GDK_DISABLE_DEPRECATED */ +void gdk_draw_rgb_48_image (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgb_buf, + gint rowstride); +void gdk_draw_rgb_48_image_dithalign (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgb_buf, + gint rowstride, + gint xdith, + gint ydith); +void gdk_draw_rgbx_64_image (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgbx_buf, + gint rowstride); +void gdk_draw_rgbx_64_image_dithalign + (GdkDrawable *drawable, + GdkGC *gc, + gint x, + gint y, + gint width, + gint height, + GdkRgbDither dith, + const guint16 *rgbx_buf, + gint rowstride, + gint xdith, + gint ydith); + G_END_DECLS From cab82328f4fdac32ad9834a8abdc887cd411818c Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 5 Aug 2026 14:20:10 +0200 Subject: [PATCH 26/68] gtk: replace deprecated IM module parser helpers Use local compile-time selected parser helpers with a legacy Pango fallback. Avoid temporary string decompression allocations and use unlocked stdio where available. --- configure.ac | 2 +- gtk/gtkimmodule.c | 246 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 211 insertions(+), 37 deletions(-) diff --git a/configure.ac b/configure.ac index 29297423f..101007915 100644 --- a/configure.ac +++ b/configure.ac @@ -469,7 +469,7 @@ if test "x$enable_rebuilds" = "xyes" && \ fi AC_SUBST(REBUILD) -AC_CHECK_FUNCS(lstat mkstemp flockfile getc_unlocked) +AC_CHECK_FUNCS(lstat mkstemp flockfile getc_unlocked _getc_nolock) AC_CHECK_FUNCS(localtime_r) # _NL_TIME_FIRST_WEEKDAY is an enum and not a define diff --git a/gtk/gtkimmodule.c b/gtk/gtkimmodule.c index a3a77c474..07c38f0b4 100644 --- a/gtk/gtkimmodule.c +++ b/gtk/gtkimmodule.c @@ -309,11 +309,186 @@ add_builtin_module (const gchar *module_name, return module; } +/* + * Keep IM module parsing independent of Pango's deprecated helpers. + */ + +/* + * Use unlocked input for the privately owned module-cache stream. + */ +#if defined(HAVE_GETC_UNLOCKED) +#define GTK_IM_MODULE_GETC(stream) getc_unlocked (stream) +#elif defined(HAVE__GETC_NOLOCK) +#define GTK_IM_MODULE_GETC(stream) _getc_nolock (stream) +#else +#define GTK_IM_MODULE_GETC(stream) getc (stream) +#endif + +static gchar * +gtk_im_module_scan_string (const gchar **pos) +{ + const gchar *p; + const gchar *q; + gchar *result; + gchar *out; + gsize len; + + p = *pos; + + while (g_ascii_isspace (*p)) + p++; + + if (*p != '"') + return NULL; + + p++; + q = p; + len = 0; + + while (*q && *q != '"') + { + if (*q == '\\') + { + q++; + + if (!*q) + return NULL; + } + + q++; + len++; + } + + if (*q != '"') + return NULL; + + result = g_malloc (len + 1); + out = result; + + while (p < q) + { + gchar c; + + c = *p++; + + if (c == '\\') + { + c = *p++; + + switch (c) + { + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + default: break; + } + } + + *out++ = c; + } + + *out = '\0'; + *pos = q + 1; + + return result; +} + +static gboolean +gtk_im_module_skip_space (const gchar **pos) +{ + while (g_ascii_isspace (**pos)) + (*pos)++; + + return **pos != '\0'; +} + +static gint +gtk_im_module_read_line (FILE *stream, + GString *str) +{ + gboolean escaped; + gboolean comment; + gint n_read; + gint lines; + gint c; + gint next_c; + + escaped = FALSE; + comment = FALSE; + n_read = 0; + lines = 1; + + g_string_truncate (str, 0); + + while (TRUE) + { + c = GTK_IM_MODULE_GETC (stream); + + if (c == EOF) + { + if (escaped) + g_string_append_c (str, '\\'); + + break; + } + + n_read++; + + if (c == '\r' || c == '\n') + { + next_c = GTK_IM_MODULE_GETC (stream); + + if (next_c != EOF && + !((c == '\r' && next_c == '\n') || + (c == '\n' && next_c == '\r'))) + ungetc (next_c, stream); + + if (escaped) + { + escaped = FALSE; + lines++; + continue; + } + + break; + } + + if (escaped) + { + escaped = FALSE; + + if (c == '#') + g_string_append_c (str, '#'); + else + { + g_string_append_c (str, '\\'); + g_string_append_c (str, c); + } + } + else if (!comment) + { + if (c == '#') + comment = TRUE; + else if (c == '\\') + escaped = TRUE; + else + g_string_append_c (str, c); + } + } + + + return n_read > 0 ? lines : 0; +} + +#undef GTK_IM_MODULE_GETC + static void gtk_im_module_initialize (void) { GString *line_buf = g_string_new (NULL); - GString *tmp_buf = g_string_new (NULL); gchar *filename = gtk_rc_get_im_module_file(); FILE *file; gboolean have_error = FALSE; @@ -387,18 +562,17 @@ gtk_im_module_initialize (void) * we allow no file at all. */ g_string_free (line_buf, TRUE); - g_string_free (tmp_buf, TRUE); g_free (filename); return; } - while (!have_error && pango_read_line (file, line_buf)) + while (!have_error && gtk_im_module_read_line (file, line_buf)) { const char *p; p = line_buf->str; - if (!pango_skip_space (&p)) + if (!gtk_im_module_skip_space (&p)) { /* Blank line marking the end of a module */ @@ -413,61 +587,62 @@ gtk_im_module_initialize (void) } if (!module) - { - /* Read a module location - */ - module = g_object_new (GTK_TYPE_IM_MODULE, NULL); + { + /* Read a module location + */ + module = g_object_new (GTK_TYPE_IM_MODULE, NULL); + module->path = gtk_im_module_scan_string (&p); - if (!pango_scan_string (&p, tmp_buf) || - pango_skip_space (&p)) - { - g_warning ("Error parsing context info in '%s'\n %s", - filename, line_buf->str); - have_error = TRUE; - } + if (!module->path || gtk_im_module_skip_space (&p)) + { + g_warning ("Error parsing context info in '%s'\n %s", + filename, line_buf->str); + have_error = TRUE; + continue; + } - module->path = g_strdup (tmp_buf->str); #ifdef G_OS_WIN32 - correct_libdir_prefix (&module->path); + correct_libdir_prefix (&module->path); #endif - g_type_module_set_name (G_TYPE_MODULE (module), module->path); - } + g_type_module_set_name (G_TYPE_MODULE (module), module->path); + } else { GtkIMContextInfo *info = g_new0 (GtkIMContextInfo, 1); /* Read information about a context type */ - if (!pango_scan_string (&p, tmp_buf)) - goto context_error; - info->context_id = g_strdup (tmp_buf->str); + info->context_id = gtk_im_module_scan_string (&p); + if (!info->context_id) + goto context_error; - if (!pango_scan_string (&p, tmp_buf)) - goto context_error; - info->context_name = g_strdup (tmp_buf->str); + info->context_name = gtk_im_module_scan_string (&p); + if (!info->context_name) + goto context_error; - if (!pango_scan_string (&p, tmp_buf)) - goto context_error; - info->domain = g_strdup (tmp_buf->str); + info->domain = gtk_im_module_scan_string (&p); + if (!info->domain) + goto context_error; - if (!pango_scan_string (&p, tmp_buf)) - goto context_error; - info->domain_dirname = g_strdup (tmp_buf->str); + info->domain_dirname = gtk_im_module_scan_string (&p); + if (!info->domain_dirname) + goto context_error; #ifdef G_OS_WIN32 correct_localedir_prefix ((char **) &info->domain_dirname); #endif - if (!pango_scan_string (&p, tmp_buf)) - goto context_error; - info->default_locales = g_strdup (tmp_buf->str); + info->default_locales = gtk_im_module_scan_string (&p); + if (!info->default_locales) + goto context_error; - if (pango_skip_space (&p)) + if (gtk_im_module_skip_space (&p)) goto context_error; infos = g_slist_prepend (infos, info); continue; context_error: + free_info (info); g_warning ("Error parsing context info in '%s'\n %s", filename, line_buf->str); have_error = TRUE; @@ -491,7 +666,6 @@ gtk_im_module_initialize (void) fclose (file); g_string_free (line_buf, TRUE); - g_string_free (tmp_buf, TRUE); g_free (filename); } From d52c085a279c060402a127c2a15e49a1433ab9d7 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 5 Aug 2026 14:20:10 +0200 Subject: [PATCH 27/68] gtk: reuse cached module path list Avoid computing and allocating the module path list a second time. --- gtk/gtkmodules.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtk/gtkmodules.c b/gtk/gtkmodules.c index da5568ffa..a325b3e56 100644 --- a/gtk/gtkmodules.c +++ b/gtk/gtkmodules.c @@ -192,7 +192,7 @@ _gtk_get_module_path (const gchar *type) result = g_new (gchar *, count * 5 + 1); count = 0; - for (path = get_module_path (); *path; path++) + for (path = paths; *path; path++) { gint use_version, use_host; From 113e081bdc3e2a3e1345eedd9bf86d42681705b8 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 5 Aug 2026 14:20:10 +0200 Subject: [PATCH 28/68] gtk: remove unused IM module query function pointers Keep symbol validation while avoiding unused typed variables and assignments. --- gtk/queryimmodules.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/gtk/queryimmodules.c b/gtk/queryimmodules.c index d20637e10..e118b9c19 100644 --- a/gtk/queryimmodules.c +++ b/gtk/queryimmodules.c @@ -159,9 +159,6 @@ query_module (const char *dir, const char *name, GString *contents) { void (*list) (const GtkIMContextInfo ***contexts, guint *n_contexts); - G_GNUC_UNUSED void (*init) (GTypeModule *type_module); - G_GNUC_UNUSED void (*exit) (void); - G_GNUC_UNUSED GtkIMContext *(*create) (const gchar *context_id); gpointer list_ptr; gpointer init_ptr; @@ -196,9 +193,6 @@ query_module (const char *dir, const char *name, GString *contents) int i; list = list_ptr; - init = init_ptr; - exit = exit_ptr; - create = create_ptr; print_escaped (contents, path); g_string_append_c (contents, '\n'); From a9867d0ccfcb392f0d0627af91231134608c0a7c Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 5 Aug 2026 22:59:33 +0200 Subject: [PATCH 29/68] cups: use modern HTTP connection helpers when available Detect httpConnect2 and httpReconnect2 at configure time. Preserve blocking, encryption, timeout and reconnect semantics while retaining the legacy CUPS APIs as compile-time fallbacks. --- configure.ac | 1 + modules/printbackends/cups/gtkcupsutils.c | 44 ++++++++++++++---- .../printbackends/cups/gtkprintbackendcups.c | 46 +++++++++++++++++-- 3 files changed, 80 insertions(+), 11 deletions(-) diff --git a/configure.ac b/configure.ac index 101007915..c344f7d8a 100644 --- a/configure.ac +++ b/configure.ac @@ -1594,6 +1594,7 @@ else gtk_save_libs="$LIBS" LIBS="$CUPS_LIBS" AC_CHECK_FUNCS(httpGetAuthString) + AC_CHECK_FUNCS(httpConnect2 httpReconnect2) LIBS="$gtk_save_libs" fi fi diff --git a/modules/printbackends/cups/gtkcupsutils.c b/modules/printbackends/cups/gtkcupsutils.c index fdfe21e91..f96b7a5b5 100644 --- a/modules/printbackends/cups/gtkcupsutils.c +++ b/modules/printbackends/cups/gtkcupsutils.c @@ -34,6 +34,11 @@ typedef void (*GtkCupsRequestStateFunc) (GtkCupsRequest *request); +static http_t *gtk_cups_http_connect (const char *host, + int port, + http_encryption_t encryption); +static int gtk_cups_http_reconnect (http_t *http); + static void _connect (GtkCupsRequest *request); static void _post_send (GtkCupsRequest *request); static void _post_write_request (GtkCupsRequest *request); @@ -171,7 +176,7 @@ gtk_cups_request_new_with_username (http_t *connection, else { request->http = NULL; - request->http = httpConnectEncrypt (request->server, + request->http = gtk_cups_http_connect (request->server, ippPort (), cupsEncryption ()); @@ -681,6 +686,29 @@ gtk_cups_request_set_ipp_version (GtkCupsRequest *request, ippSetVersion (request->ipp_request, major, minor); } + +static http_t * +gtk_cups_http_connect (const char *host, + int port, + http_encryption_t encryption) +{ +#ifdef HAVE_HTTPCONNECT2 + return httpConnect2 (host, port, NULL, AF_UNSPEC, encryption, 1, 30000, NULL); +#else + return httpConnectEncrypt (host, port, encryption); +#endif +} + +static int +gtk_cups_http_reconnect (http_t *http) +{ +#ifdef HAVE_HTTPRECONNECT2 + return httpReconnect2 (http, 30000, NULL); +#else + return httpReconnect (http); +#endif +} + static void _connect (GtkCupsRequest *request) { @@ -689,7 +717,7 @@ _connect (GtkCupsRequest *request) if (request->http == NULL) { - request->http = httpConnectEncrypt (request->server, + request->http = gtk_cups_http_connect (request->server, ippPort (), cupsEncryption ()); @@ -744,7 +772,7 @@ _post_send (GtkCupsRequest *request) if (httpPost (request->http, request->resource)) { - if (httpReconnect (request->http)) + if (gtk_cups_http_reconnect (request->http)) { request->state = GTK_CUPS_POST_DONE; request->poll_state = GTK_CUPS_HTTP_IDLE; @@ -1046,7 +1074,7 @@ _post_check (GtkCupsRequest *request) } if (auth_result || - httpReconnect (request->http)) + gtk_cups_http_reconnect (request->http)) { /* if the password has been used, reset password_state * so that we ask for a new one next time around @@ -1105,7 +1133,7 @@ _post_check (GtkCupsRequest *request) request->state = GTK_CUPS_POST_CONNECT; /* Reconnect... */ - httpReconnect (request->http); + gtk_cups_http_reconnect (request->http); /* Upgrade with encryption... */ httpEncryption (request->http, HTTP_ENCRYPT_REQUIRED); @@ -1232,7 +1260,7 @@ _get_send (GtkCupsRequest *request) if (httpGet (request->http, request->resource)) { - if (httpReconnect (request->http)) + if (gtk_cups_http_reconnect (request->http)) { request->state = GTK_CUPS_GET_DONE; request->poll_state = GTK_CUPS_HTTP_IDLE; @@ -1341,7 +1369,7 @@ _get_check (GtkCupsRequest *request) } if (auth_result || - httpReconnect (request->http)) + gtk_cups_http_reconnect (request->http)) { /* if the password has been used, reset password_state * so that we ask for a new one next time around @@ -1373,7 +1401,7 @@ _get_check (GtkCupsRequest *request) request->state = GTK_CUPS_GET_CONNECT; /* Reconnect... */ - httpReconnect (request->http); + gtk_cups_http_reconnect (request->http); /* Upgrade with encryption... */ httpEncryption (request->http, HTTP_ENCRYPT_REQUIRED); diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c index e17d97d21..d07d1bc7a 100644 --- a/modules/printbackends/cups/gtkprintbackendcups.c +++ b/modules/printbackends/cups/gtkprintbackendcups.c @@ -489,6 +489,44 @@ cups_free_print_stream_data (CupsPrintStreamData *data) g_free (data); } + +static http_t * +gtk_cups_backend_http_connect (const char *host, + int port) +{ +#ifdef HAVE_HTTPCONNECT2 + return httpConnect2 (host, + port, + NULL, + AF_UNSPEC, + HTTP_ENCRYPT_IF_REQUESTED, + 1, + 30000, + NULL); +#else + return httpConnect (host, port); +#endif +} + +static http_t * +gtk_cups_backend_http_connect_encrypt (const char *host, + int port, + http_encryption_t encryption) +{ +#ifdef HAVE_HTTPCONNECT2 + return httpConnect2 (host, + port, + NULL, + AF_UNSPEC, + encryption, + 1, + 30000, + NULL); +#else + return httpConnectEncrypt (host, port, encryption); +#endif +} + static void cups_print_cb (GtkPrintBackendCups *print_backend, GtkCupsResult *result, @@ -636,7 +674,9 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend, { http_t *http; - http = httpConnect (cups_printer->hostname, cups_printer->port); + http = gtk_cups_backend_http_connect ( + cups_printer->hostname, + cups_printer->port); if (http) { request = gtk_cups_request_new_with_username (http, @@ -2555,7 +2595,7 @@ cups_request_avahi_printer_info (const gchar *printer_uri, AvahiService *service; http_t *http; - http = httpConnect (host, port); + http = gtk_cups_backend_http_connect (host, port); if (http) { service = (AvahiService *) g_new0 (AvahiService, 1); @@ -3438,7 +3478,7 @@ cups_request_ppd (GtkPrinter *printer) } } - http = httpConnectEncrypt (cups_printer->hostname, + http = gtk_cups_backend_http_connect_encrypt (cups_printer->hostname, cups_printer->port, cupsEncryption ()); From 7c2a1d8016c63cbc8fee62f782ffb80f515e28cd Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Thu, 6 Aug 2026 13:20:18 +0200 Subject: [PATCH 30/68] gail: implement modern AtkValue interface Add compile-time support for the modern AtkValue API available since ATK 2.12 while retaining compatibility with older ATK versions. Implement direct modern value, range, increment and setter methods for adjustments, paned widgets, progress bars, ranges, scale buttons and spin buttons. Keep the implementation private, avoid per-instance memory overhead and preserve the existing exported ABI. --- configure.ac | 5 ++ modules/other/gail/Makefile.am | 1 + modules/other/gail/gailadjustment.c | 82 ++++++++++++---------------- modules/other/gail/gailadjustment.h | 16 ++++++ modules/other/gail/gailpaned.c | 35 ++++++++++++ modules/other/gail/gailprogressbar.c | 18 +++++- modules/other/gail/gailrange.c | 39 +++++++++++-- modules/other/gail/gailscalebutton.c | 27 ++++++++- modules/other/gail/gailspinbutton.c | 31 +++++++++-- modules/other/gail/gailvalue.h | 56 +++++++++++++++++++ 10 files changed, 246 insertions(+), 64 deletions(-) create mode 100644 modules/other/gail/gailvalue.h diff --git a/configure.ac b/configure.ac index c344f7d8a..04536e910 100644 --- a/configure.ac +++ b/configure.ac @@ -399,6 +399,11 @@ PKG_CHECK_MODULES(BASE_DEPENDENCIES, gdk-pixbuf-2.0 >= gdk_pixbuf_required_version dnl fribidi >= fribidi_required_version]) +PKG_CHECK_EXISTS([atk >= 2.12], + [AC_DEFINE([HAVE_ATK_VALUE_2_12], [1], + [Define if ATK provides the 2.12 AtkValue interface])]) + + ## In addition to checking that cairo is present, we also need to ## check that the correct cairo backend is there. E.g. if the GDK ## target is win32 we need the cairo-win32 backend and so on. diff --git a/modules/other/gail/Makefile.am b/modules/other/gail/Makefile.am index 991512dea..1b9a4c6bd 100644 --- a/modules/other/gail/Makefile.am +++ b/modules/other/gail/Makefile.am @@ -72,6 +72,7 @@ libgailincludedir=$(includedir)/gail-1.0/gail gail_private_h_sources = \ gail.h \ + gailvalue.h \ gailadjustment.h \ gailarrow.h \ gailbooleancell.h \ diff --git a/modules/other/gail/gailadjustment.c b/modules/other/gail/gailadjustment.c index f1bb53f2b..021b79357 100644 --- a/modules/other/gail/gailadjustment.c +++ b/modules/other/gail/gailadjustment.c @@ -22,6 +22,7 @@ #include #include #include "gailadjustment.h" +#include "gailvalue.h" static void gail_adjustment_class_init (GailAdjustmentClass *klass); @@ -32,16 +33,25 @@ static void gail_adjustment_real_initialize (AtkObject *obj, static void atk_value_interface_init (AtkValueIface *iface); -static void gail_adjustment_get_current_value (AtkValue *obj, - GValue *value); -static void gail_adjustment_get_maximum_value (AtkValue *obj, - GValue *value); -static void gail_adjustment_get_minimum_value (AtkValue *obj, - GValue *value); -static void gail_adjustment_get_minimum_increment (AtkValue *obj, - GValue *value); -static gboolean gail_adjustment_set_current_value (AtkValue *obj, - const GValue *value); + +#ifdef HAVE_ATK_VALUE_2_12 +GAIL_DEFINE_ADJUSTMENT_VALUE_READERS ( + gail_adjustment, GAIL_ADJUSTMENT (obj)->adjustment, + adjustment->value, adjustment->upper) +GAIL_DEFINE_ADJUSTMENT_VALUE_INCREMENT ( + gail_adjustment, GAIL_ADJUSTMENT (obj)->adjustment, + GAIL_VALUE_MINIMUM_INCREMENT (adjustment)) + +static void +gail_adjustment_set_value (AtkValue *obj, gdouble value) +{ + GtkAdjustment *adjustment; + + adjustment = GAIL_ADJUSTMENT (obj)->adjustment; + if (adjustment != NULL) + gtk_adjustment_set_value (adjustment, value); +} +#endif static void gail_adjustment_destroyed (GtkAdjustment *adjustment, GailAdjustment *gail_adjustment); @@ -105,9 +115,15 @@ atk_value_interface_init (AtkValueIface *iface) iface->get_minimum_value = gail_adjustment_get_minimum_value; iface->get_minimum_increment = gail_adjustment_get_minimum_increment; iface->set_current_value = gail_adjustment_set_current_value; +#ifdef HAVE_ATK_VALUE_2_12 + iface->get_value_and_text = gail_adjustment_get_value_and_text; + iface->get_range = gail_adjustment_get_range; + iface->get_increment = gail_adjustment_get_increment; + iface->set_value = gail_adjustment_set_value; +#endif } -static void +void gail_adjustment_get_current_value (AtkValue *obj, GValue *value) { @@ -127,7 +143,7 @@ gail_adjustment_get_current_value (AtkValue *obj, g_value_set_double (value,current_value); } -static void +void gail_adjustment_get_maximum_value (AtkValue *obj, GValue *value) { @@ -147,7 +163,7 @@ gail_adjustment_get_maximum_value (AtkValue *obj, g_value_set_double (value, maximum_value); } -static void +void gail_adjustment_get_minimum_value (AtkValue *obj, GValue *value) { @@ -167,48 +183,20 @@ gail_adjustment_get_minimum_value (AtkValue *obj, g_value_set_double (value, minimum_value); } -static void -gail_adjustment_get_minimum_increment (AtkValue *obj, - GValue *value) +void +gail_adjustment_get_minimum_increment (AtkValue *obj, GValue *value) { - GtkAdjustment* adjustment; - gdouble minimum_increment; + GtkAdjustment *adjustment = GAIL_ADJUSTMENT (obj)->adjustment; - adjustment = GAIL_ADJUSTMENT (obj)->adjustment; if (adjustment == NULL) - { - /* State is defunct */ return; - } - if (adjustment->step_increment != 0 && - adjustment->page_increment != 0) - { - if (ABS (adjustment->step_increment) < ABS (adjustment->page_increment)) - minimum_increment = adjustment->step_increment; - else - minimum_increment = adjustment->page_increment; - } - else if (adjustment->step_increment == 0 && - adjustment->page_increment == 0) - { - minimum_increment = 0; - } - else if (adjustment->step_increment == 0) - { - minimum_increment = adjustment->page_increment; - } - else - { - minimum_increment = adjustment->step_increment; - } - - memset (value, 0, sizeof (GValue)); + memset (value, 0, sizeof (GValue)); g_value_init (value, G_TYPE_DOUBLE); - g_value_set_double (value, minimum_increment); + g_value_set_double (value, GAIL_VALUE_MINIMUM_INCREMENT (adjustment)); } -static gboolean +gboolean gail_adjustment_set_current_value (AtkValue *obj, const GValue *value) { diff --git a/modules/other/gail/gailadjustment.h b/modules/other/gail/gailadjustment.h index 790534dc9..0135b3d91 100644 --- a/modules/other/gail/gailadjustment.h +++ b/modules/other/gail/gailadjustment.h @@ -50,6 +50,22 @@ struct _GailAdjustmentClass AtkObject *gail_adjustment_new (GtkAdjustment *adjustment); +G_GNUC_INTERNAL void +gail_adjustment_get_current_value (AtkValue *obj, + GValue *value); +G_GNUC_INTERNAL void +gail_adjustment_get_maximum_value (AtkValue *obj, + GValue *value); +G_GNUC_INTERNAL void +gail_adjustment_get_minimum_value (AtkValue *obj, + GValue *value); +G_GNUC_INTERNAL void +gail_adjustment_get_minimum_increment (AtkValue *obj, + GValue *value); +G_GNUC_INTERNAL gboolean +gail_adjustment_set_current_value (AtkValue *obj, + const GValue *value); + G_END_DECLS #endif /* __GAIL_ADJUSTMENT_H__ */ diff --git a/modules/other/gail/gailpaned.c b/modules/other/gail/gailpaned.c index 5ac2d2340..42ac248da 100644 --- a/modules/other/gail/gailpaned.c +++ b/modules/other/gail/gailpaned.c @@ -22,6 +22,7 @@ #include #include #include "gailpaned.h" +#include "gailvalue.h" static void gail_paned_class_init (GailPanedClass *klass); @@ -44,6 +45,35 @@ static void gail_paned_get_minimum_value (AtkValue *obj, static gboolean gail_paned_set_current_value (AtkValue *obj, const GValue *value); +#ifdef HAVE_ATK_VALUE_2_12 +static void +gail_paned_get_value_and_text (AtkValue *obj, gdouble *value, gchar **text) +{ + GtkWidget *widget = GTK_ACCESSIBLE (obj)->widget; + + if (text != NULL) + *text = NULL; + if (value != NULL) + *value = widget != NULL + ? gtk_paned_get_position (GTK_PANED (widget)) : 0.0; +} + +static AtkRange * +gail_paned_get_range (AtkValue *obj) +{ + GtkWidget *widget = GTK_ACCESSIBLE (obj)->widget; + GtkPaned *paned; + + if (widget == NULL) + return NULL; + paned = GTK_PANED (widget); + return atk_range_new (paned->min_position, paned->max_position, NULL); +} + +GAIL_DEFINE_WIDGET_VALUE_SETTER ( + gail_paned, GTK_PANED, gtk_paned_set_position, (gint) value) +#endif + G_DEFINE_TYPE_WITH_CODE (GailPaned, gail_paned, GAIL_TYPE_CONTAINER, G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init)) @@ -112,6 +142,11 @@ atk_value_interface_init (AtkValueIface *iface) iface->get_maximum_value = gail_paned_get_maximum_value; iface->get_minimum_value = gail_paned_get_minimum_value; iface->set_current_value = gail_paned_set_current_value; +#ifdef HAVE_ATK_VALUE_2_12 + iface->get_value_and_text = gail_paned_get_value_and_text; + iface->get_range = gail_paned_get_range; + iface->set_value = gail_paned_set_value; +#endif } static void diff --git a/modules/other/gail/gailprogressbar.c b/modules/other/gail/gailprogressbar.c index d70c139be..6a0df280e 100644 --- a/modules/other/gail/gailprogressbar.c +++ b/modules/other/gail/gailprogressbar.c @@ -26,6 +26,7 @@ #include #include "gailprogressbar.h" +#include "gailvalue.h" #include "gailadjustment.h" static void gail_progress_bar_class_init (GailProgressBarClass *klass); @@ -50,6 +51,13 @@ static void gail_progress_bar_get_minimum_value (AtkValue *obj, static void gail_progress_bar_value_changed (GtkAdjustment *adjustment, gpointer data); +#ifdef HAVE_ATK_VALUE_2_12 +GAIL_DEFINE_ADJUSTMENT_VALUE_READERS ( + gail_progress_bar, + GAIL_VALUE_ADJUSTMENT (GAIL_PROGRESS_BAR (obj)->adjustment), + adjustment->value, adjustment->upper) +#endif + G_DEFINE_TYPE_WITH_CODE (GailProgressBar, gail_progress_bar, GAIL_TYPE_WIDGET, G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init)) @@ -108,6 +116,10 @@ atk_value_interface_init (AtkValueIface *iface) iface->get_current_value = gail_progress_bar_get_current_value; iface->get_maximum_value = gail_progress_bar_get_maximum_value; iface->get_minimum_value = gail_progress_bar_get_minimum_value; +#ifdef HAVE_ATK_VALUE_2_12 + iface->get_value_and_text = gail_progress_bar_get_value_and_text; + iface->get_range = gail_progress_bar_get_range; +#endif } static void @@ -125,7 +137,7 @@ gail_progress_bar_get_current_value (AtkValue *obj, */ return; - atk_value_get_current_value (ATK_VALUE (progress_bar->adjustment), value); + gail_adjustment_get_current_value (ATK_VALUE (progress_bar->adjustment), value); } static void @@ -143,7 +155,7 @@ gail_progress_bar_get_maximum_value (AtkValue *obj, */ return; - atk_value_get_maximum_value (ATK_VALUE (progress_bar->adjustment), value); + gail_adjustment_get_maximum_value (ATK_VALUE (progress_bar->adjustment), value); } static void @@ -161,7 +173,7 @@ gail_progress_bar_get_minimum_value (AtkValue *obj, */ return; - atk_value_get_minimum_value (ATK_VALUE (progress_bar->adjustment), value); + gail_adjustment_get_minimum_value (ATK_VALUE (progress_bar->adjustment), value); } static void diff --git a/modules/other/gail/gailrange.c b/modules/other/gail/gailrange.c index 9a91bb333..8170d86d0 100644 --- a/modules/other/gail/gailrange.c +++ b/modules/other/gail/gailrange.c @@ -23,6 +23,7 @@ #include #include #include "gailrange.h" +#include "gailvalue.h" #include "gailadjustment.h" #include "gail-private-macros.h" @@ -70,6 +71,28 @@ static gboolean gail_range_set_description (AtkAction *action, gint i, const gchar *desc); +#ifdef HAVE_ATK_VALUE_2_12 +#define GAIL_RANGE_ADJUSTMENT(obj) \ + (GTK_ACCESSIBLE (obj)->widget != NULL \ + ? GAIL_VALUE_ADJUSTMENT (GAIL_RANGE (obj)->adjustment) : NULL) +#define GAIL_RANGE_UPPER(obj, adjustment) \ + (gtk_range_get_restrict_to_fill_level ( \ + GTK_RANGE (GTK_ACCESSIBLE (obj)->widget)) \ + ? MIN ((adjustment)->upper - (adjustment)->page_size, \ + gtk_range_get_fill_level ( \ + GTK_RANGE (GTK_ACCESSIBLE (obj)->widget))) \ + : (adjustment)->upper - (adjustment)->page_size) + +GAIL_DEFINE_ADJUSTMENT_VALUE_READERS ( + gail_range, GAIL_RANGE_ADJUSTMENT (obj), adjustment->value, + GAIL_RANGE_UPPER (obj, adjustment)) +GAIL_DEFINE_ADJUSTMENT_VALUE_INCREMENT ( + gail_range, GAIL_RANGE_ADJUSTMENT (obj), + GAIL_VALUE_MINIMUM_INCREMENT (adjustment)) +GAIL_DEFINE_WIDGET_VALUE_SETTER ( + gail_range, GTK_RANGE, gtk_range_set_value, value) +#endif + G_DEFINE_TYPE_WITH_CODE (GailRange, gail_range, GAIL_TYPE_WIDGET, G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init) G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init)) @@ -163,6 +186,12 @@ atk_value_interface_init (AtkValueIface *iface) iface->get_minimum_value = gail_range_get_minimum_value; iface->get_minimum_increment = gail_range_get_minimum_increment; iface->set_current_value = gail_range_set_current_value; +#ifdef HAVE_ATK_VALUE_2_12 + iface->get_value_and_text = gail_range_get_value_and_text; + iface->get_range = gail_range_get_range; + iface->get_increment = gail_range_get_increment; + iface->set_value = gail_range_set_value; +#endif } static void @@ -180,7 +209,7 @@ gail_range_get_current_value (AtkValue *obj, */ return; - atk_value_get_current_value (ATK_VALUE (range->adjustment), value); + gail_adjustment_get_current_value (ATK_VALUE (range->adjustment), value); } static void @@ -201,7 +230,7 @@ gail_range_get_maximum_value (AtkValue *obj, */ return; - atk_value_get_maximum_value (ATK_VALUE (range->adjustment), value); + gail_adjustment_get_maximum_value (ATK_VALUE (range->adjustment), value); gtk_range = GTK_RANGE (gtk_accessible_get_widget (GTK_ACCESSIBLE (range))); g_return_if_fail (gtk_range); @@ -231,7 +260,7 @@ gail_range_get_minimum_value (AtkValue *obj, */ return; - atk_value_get_minimum_value (ATK_VALUE (range->adjustment), value); + gail_adjustment_get_minimum_value (ATK_VALUE (range->adjustment), value); } static void @@ -248,7 +277,7 @@ gail_range_get_minimum_increment (AtkValue *obj, GValue *value) */ return; - atk_value_get_minimum_increment (ATK_VALUE (range->adjustment), value); + gail_adjustment_get_minimum_increment (ATK_VALUE (range->adjustment), value); } static gboolean gail_range_set_current_value (AtkValue *obj, @@ -519,5 +548,3 @@ gail_range_set_description (AtkAction *action, else return FALSE; } - - diff --git a/modules/other/gail/gailscalebutton.c b/modules/other/gail/gailscalebutton.c index f04c69046..75915e43d 100644 --- a/modules/other/gail/gailscalebutton.c +++ b/modules/other/gail/gailscalebutton.c @@ -21,6 +21,7 @@ #include #include "gailscalebutton.h" +#include "gailvalue.h" #include "gailadjustment.h" #include "gail-private-macros.h" @@ -65,6 +66,24 @@ static void gail_scale_button_get_minimum_increment (AtkValue *obj, static gboolean gail_scale_button_set_current_value (AtkValue *obj, const GValue *value); +#ifdef HAVE_ATK_VALUE_2_12 +#define GAIL_SCALE_BUTTON_ADJUSTMENT(obj) \ + (GTK_ACCESSIBLE (obj)->widget != NULL \ + ? gtk_scale_button_get_adjustment ( \ + GTK_SCALE_BUTTON (GTK_ACCESSIBLE (obj)->widget)) : NULL) + +GAIL_DEFINE_ADJUSTMENT_VALUE_READERS ( + gail_scale_button, GAIL_SCALE_BUTTON_ADJUSTMENT (obj), + gtk_scale_button_get_value ( + GTK_SCALE_BUTTON (GTK_ACCESSIBLE (obj)->widget)), + adjustment->upper) +GAIL_DEFINE_ADJUSTMENT_VALUE_INCREMENT ( + gail_scale_button, GAIL_SCALE_BUTTON_ADJUSTMENT (obj), + adjustment->step_increment) +GAIL_DEFINE_WIDGET_VALUE_SETTER ( + gail_scale_button, GTK_SCALE_BUTTON, gtk_scale_button_set_value, value) +#endif + G_DEFINE_TYPE_WITH_CODE (GailScaleButton, gail_scale_button, GAIL_TYPE_BUTTON, G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init) G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init)); @@ -181,6 +200,12 @@ atk_value_interface_init (AtkValueIface *iface) iface->get_minimum_value = gail_scale_button_get_minimum_value; iface->get_minimum_increment = gail_scale_button_get_minimum_increment; iface->set_current_value = gail_scale_button_set_current_value; +#ifdef HAVE_ATK_VALUE_2_12 + iface->get_value_and_text = gail_scale_button_get_value_and_text; + iface->get_range = gail_scale_button_get_range; + iface->get_increment = gail_scale_button_get_increment; + iface->set_value = gail_scale_button_set_value; +#endif } static void @@ -295,5 +320,3 @@ gail_scale_button_notify_gtk (GObject *obj, GAIL_WIDGET_CLASS (gail_scale_button_parent_class)->notify_gtk (obj, pspec); } } - - diff --git a/modules/other/gail/gailspinbutton.c b/modules/other/gail/gailspinbutton.c index 709a413c0..36f2ffa32 100644 --- a/modules/other/gail/gailspinbutton.c +++ b/modules/other/gail/gailspinbutton.c @@ -22,6 +22,7 @@ #include #include #include "gailspinbutton.h" +#include "gailvalue.h" #include "gailadjustment.h" #include "gail-private-macros.h" @@ -49,6 +50,19 @@ static gboolean gail_spin_button_set_current_value (AtkValue *obj, static void gail_spin_button_value_changed (GtkAdjustment *adjustment, gpointer data); +#ifdef HAVE_ATK_VALUE_2_12 +GAIL_DEFINE_ADJUSTMENT_VALUE_READERS ( + gail_spin_button, + GAIL_VALUE_ADJUSTMENT (GAIL_SPIN_BUTTON (obj)->adjustment), + adjustment->value, adjustment->upper) +GAIL_DEFINE_ADJUSTMENT_VALUE_INCREMENT ( + gail_spin_button, + GAIL_VALUE_ADJUSTMENT (GAIL_SPIN_BUTTON (obj)->adjustment), + GAIL_VALUE_MINIMUM_INCREMENT (adjustment)) +GAIL_DEFINE_WIDGET_VALUE_SETTER ( + gail_spin_button, GTK_SPIN_BUTTON, gtk_spin_button_set_value, value) +#endif + G_DEFINE_TYPE_WITH_CODE (GailSpinButton, gail_spin_button, GAIL_TYPE_ENTRY, G_IMPLEMENT_INTERFACE (ATK_TYPE_VALUE, atk_value_interface_init)) @@ -110,6 +124,12 @@ atk_value_interface_init (AtkValueIface *iface) iface->get_minimum_value = gail_spin_button_get_minimum_value; iface->get_minimum_increment = gail_spin_button_get_minimum_increment; iface->set_current_value = gail_spin_button_set_current_value; +#ifdef HAVE_ATK_VALUE_2_12 + iface->get_value_and_text = gail_spin_button_get_value_and_text; + iface->get_range = gail_spin_button_get_range; + iface->get_increment = gail_spin_button_get_increment; + iface->set_value = gail_spin_button_set_value; +#endif } static void @@ -127,7 +147,7 @@ gail_spin_button_get_current_value (AtkValue *obj, */ return; - atk_value_get_current_value (ATK_VALUE (spin_button->adjustment), value); + gail_adjustment_get_current_value (ATK_VALUE (spin_button->adjustment), value); } static void @@ -145,7 +165,7 @@ gail_spin_button_get_maximum_value (AtkValue *obj, */ return; - atk_value_get_maximum_value (ATK_VALUE (spin_button->adjustment), value); + gail_adjustment_get_maximum_value (ATK_VALUE (spin_button->adjustment), value); } static void @@ -163,7 +183,7 @@ gail_spin_button_get_minimum_value (AtkValue *obj, */ return; - atk_value_get_minimum_value (ATK_VALUE (spin_button->adjustment), value); + gail_adjustment_get_minimum_value (ATK_VALUE (spin_button->adjustment), value); } static void @@ -180,7 +200,7 @@ gail_spin_button_get_minimum_increment (AtkValue *obj, GValue *value) */ return; - atk_value_get_minimum_increment (ATK_VALUE (spin_button->adjustment), value); + gail_adjustment_get_minimum_increment (ATK_VALUE (spin_button->adjustment), value); } static gboolean @@ -198,7 +218,7 @@ gail_spin_button_set_current_value (AtkValue *obj, */ return FALSE; - return atk_value_set_current_value (ATK_VALUE (spin_button->adjustment), value); + return gail_adjustment_set_current_value (ATK_VALUE (spin_button->adjustment), value); } static void @@ -264,4 +284,3 @@ gail_spin_button_value_changed (GtkAdjustment *adjustment, g_object_notify (G_OBJECT (spin_button), "accessible-value"); } - diff --git a/modules/other/gail/gailvalue.h b/modules/other/gail/gailvalue.h new file mode 100644 index 000000000..498453424 --- /dev/null +++ b/modules/other/gail/gailvalue.h @@ -0,0 +1,56 @@ +/* GAIL - The GNOME Accessibility Implementation Library + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +#ifndef __GAIL_VALUE_H__ +#define __GAIL_VALUE_H__ + +#include + +#define GAIL_VALUE_ADJUSTMENT(accessible) \ + ((accessible) != NULL ? GAIL_ADJUSTMENT (accessible)->adjustment : NULL) +#define GAIL_VALUE_MINIMUM_INCREMENT(a) \ + ((a)->step_increment == 0.0 ? (a)->page_increment : \ + (a)->page_increment == 0.0 ? (a)->step_increment : \ + ABS ((a)->step_increment) < ABS ((a)->page_increment) \ + ? (a)->step_increment : (a)->page_increment) + +#ifdef HAVE_ATK_VALUE_2_12 +#define GAIL_DEFINE_ADJUSTMENT_VALUE_READERS(prefix, a_expr, value_expr, max_expr) \ +static void prefix##_get_value_and_text (AtkValue *obj, gdouble *value, gchar **text) \ +{ \ + GtkAdjustment *adjustment = (a_expr); \ + if (text != NULL) *text = NULL; \ + if (value != NULL) *value = adjustment != NULL ? (value_expr) : 0.0; \ +} \ +static AtkRange *prefix##_get_range (AtkValue *obj) \ +{ \ + GtkAdjustment *adjustment = (a_expr); \ + return adjustment != NULL \ + ? atk_range_new (adjustment->lower, (max_expr), NULL) : NULL; \ +} + +#define GAIL_DEFINE_ADJUSTMENT_VALUE_INCREMENT(prefix, a_expr, increment_expr) \ +static gdouble prefix##_get_increment (AtkValue *obj) \ +{ \ + GtkAdjustment *adjustment = (a_expr); \ + return adjustment != NULL ? (increment_expr) : 0.0; \ +} + +#define GAIL_DEFINE_WIDGET_VALUE_SETTER(prefix, cast, setter, value_expr) \ +static void prefix##_set_value (AtkValue *obj, gdouble value) \ +{ \ + GtkWidget *widget = GTK_ACCESSIBLE (obj)->widget; \ + if (widget != NULL) setter (cast (widget), (value_expr)); \ +} +#endif + +#endif /* __GAIL_VALUE_H__ */ From dd819f2a46014b3c9a3db8c22aa16baf2c7beee2 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Thu, 6 Aug 2026 13:57:34 +0200 Subject: [PATCH 31/68] gail: update AtkValue test modules Use a shared private compatibility helper in the GAIL test modules. Exercise the modern AtkValue API when ATK 2.12 or newer is available while retaining the legacy test path for older ATK versions. Initialize temporary GValues explicitly and keep the helper symbols private to the test modules. --- modules/other/gail/tests/Makefile.am | 2 + modules/other/gail/tests/ferret.c | 10 +-- modules/other/gail/tests/testpaned.c | 11 +-- modules/other/gail/tests/testvalue.h | 98 +++++++++++++++++++++++++++ modules/other/gail/tests/testvalues.c | 18 ++--- 5 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 modules/other/gail/tests/testvalue.h diff --git a/modules/other/gail/tests/Makefile.am b/modules/other/gail/tests/Makefile.am index 993249558..0f0e02317 100644 --- a/modules/other/gail/tests/Makefile.am +++ b/modules/other/gail/tests/Makefile.am @@ -12,6 +12,8 @@ module_LTLIBRARIES = \ libferret.la endif +noinst_HEADERS = testvalue.h + noinst_LTLIBRARIES = \ libtestaction.la \ libtestbutton.la \ diff --git a/modules/other/gail/tests/ferret.c b/modules/other/gail/tests/ferret.c index 711852af0..39eaef441 100644 --- a/modules/other/gail/tests/ferret.c +++ b/modules/other/gail/tests/ferret.c @@ -16,6 +16,7 @@ #include #include "testlib.h" +#include "testvalue.h" typedef enum { @@ -1389,7 +1390,8 @@ _print_text_attributes (AtkText *aobject) static gint _print_value (AtkValue *aobject) { - GValue *value_back, val; + GValue val = { 0, }; + GValue *value_back; gint group_num; TabNumber tab_n = VALUE; @@ -1398,11 +1400,11 @@ _print_value (AtkValue *aobject) group_num = _print_groupname(tab_n, VALUE_INTERFACE, "Value Interface"); - atk_value_get_current_value(aobject, value_back); + gail_test_value_get_current_value(aobject, value_back); _print_value_type(group_num, "Value", value_back); - atk_value_get_minimum_value(aobject, value_back); + gail_test_value_get_minimum_value(aobject, value_back); _print_value_type(group_num, "Minimum Value", value_back); - atk_value_get_maximum_value(aobject, value_back); + gail_test_value_get_maximum_value(aobject, value_back); _print_value_type(group_num, "Maximum Value", value_back); return(group_num); } diff --git a/modules/other/gail/tests/testpaned.c b/modules/other/gail/tests/testpaned.c index 300031955..7e1f27f1c 100644 --- a/modules/other/gail/tests/testpaned.c +++ b/modules/other/gail/tests/testpaned.c @@ -2,6 +2,7 @@ #include #include #include +#include "testvalue.h" static gint _test_paned (gpointer data); static void _check_paned (AtkObject *obj); @@ -31,17 +32,17 @@ static void _property_change_handler (AtkObject *obj, value = &val; memset (value, 0, sizeof (GValue)); - atk_value_get_current_value (ATK_VALUE (obj), value); + gail_test_value_get_current_value (ATK_VALUE (obj), value); g_return_if_fail (G_VALUE_HOLDS_INT (value)); position = g_value_get_int (value); g_print ("Position is %d previous position was %d\n", position, last_position); last_position = position; - atk_value_get_minimum_value (ATK_VALUE (obj), value); + gail_test_value_get_minimum_value (ATK_VALUE (obj), value); g_return_if_fail (G_VALUE_HOLDS_INT (value)); position = g_value_get_int (value); g_print ("Minimum Value is %d\n", position); - atk_value_get_maximum_value (ATK_VALUE (obj), value); + gail_test_value_get_maximum_value (ATK_VALUE (obj), value); g_return_if_fail (G_VALUE_HOLDS_INT (value)); position = g_value_get_int (value); g_print ("Maximum Value is %d\n", position); @@ -61,14 +62,14 @@ static gint _test_paned (gpointer data) value = &val; memset (value, 0, sizeof (GValue)); - atk_value_get_current_value (ATK_VALUE (obj), value); + gail_test_value_get_current_value (ATK_VALUE (obj), value); g_return_val_if_fail (G_VALUE_HOLDS_INT (value), FALSE); position = g_value_get_int (value); g_print ("Position is : %d\n", position); last_position = position; position *= 2; g_value_set_int (value, position); - atk_value_set_current_value (ATK_VALUE (obj), value); + gail_test_value_set_current_value (ATK_VALUE (obj), value); times++; } if (times < 4) diff --git a/modules/other/gail/tests/testvalue.h b/modules/other/gail/tests/testvalue.h new file mode 100644 index 000000000..735278318 --- /dev/null +++ b/modules/other/gail/tests/testvalue.h @@ -0,0 +1,98 @@ +#ifndef __GAIL_TEST_VALUE_H__ +#define __GAIL_TEST_VALUE_H__ + +#include "config.h" + +#include + +#ifdef HAVE_ATK_VALUE_2_12 + +static void +gail_test_value_store_number (AtkValue *value, + GValue *result, + gdouble number) +{ + if (G_VALUE_TYPE (result) == 0) + g_value_init (result, + atk_object_get_role (ATK_OBJECT (value)) == + ATK_ROLE_SPLIT_PANE ? G_TYPE_INT : G_TYPE_DOUBLE); + + if (G_VALUE_HOLDS_INT (result)) + g_value_set_int (result, (gint) number); + else + { + g_return_if_fail (G_VALUE_HOLDS_DOUBLE (result)); + g_value_set_double (result, number); + } +} + +static void +gail_test_value_get_current_value (AtkValue *value, + GValue *result) +{ + gdouble number; + + atk_value_get_value_and_text (value, &number, NULL); + gail_test_value_store_number (value, result, number); +} + +static void +gail_test_value_get_limit (AtkValue *value, + GValue *result, + gboolean upper) +{ + AtkRange *range; + gdouble number; + + range = atk_value_get_range (value); + g_return_if_fail (range != NULL); + + if (upper) + number = atk_range_get_upper_limit (range); + else + number = atk_range_get_lower_limit (range); + + atk_range_free (range); + gail_test_value_store_number (value, result, number); +} + +#define gail_test_value_get_minimum_value(value, result) \ + gail_test_value_get_limit ((value), (result), FALSE) + +#define gail_test_value_get_maximum_value(value, result) \ + gail_test_value_get_limit ((value), (result), TRUE) + +#define GAIL_TEST_VALUE_NUMBER(value) \ + (G_VALUE_HOLDS_INT (value) \ + ? (gdouble) g_value_get_int (value) \ + : g_value_get_double (value)) + +#define gail_test_value_set_current_value(value, new_value) \ + (atk_value_set_value ((value), \ + GAIL_TEST_VALUE_NUMBER (new_value)), \ + TRUE) + +#else + +#define GAIL_TEST_VALUE_IFACE(value) \ + ATK_VALUE_GET_IFACE (value) + +#define gail_test_value_get_current_value(value, result) \ + (GAIL_TEST_VALUE_IFACE (value)->get_current_value ( \ + (value), (result))) + +#define gail_test_value_get_minimum_value(value, result) \ + (GAIL_TEST_VALUE_IFACE (value)->get_minimum_value ( \ + (value), (result))) + +#define gail_test_value_get_maximum_value(value, result) \ + (GAIL_TEST_VALUE_IFACE (value)->get_maximum_value ( \ + (value), (result))) + +#define gail_test_value_set_current_value(value, new_value) \ + (GAIL_TEST_VALUE_IFACE (value)->set_current_value ( \ + (value), (new_value))) + +#endif + +#endif /* __GAIL_TEST_VALUE_H__ */ diff --git a/modules/other/gail/tests/testvalues.c b/modules/other/gail/tests/testvalues.c index 34d91a25a..37155855c 100644 --- a/modules/other/gail/tests/testvalues.c +++ b/modules/other/gail/tests/testvalues.c @@ -1,6 +1,7 @@ #include #include #include +#include "testvalue.h" static void _traverse_children (AtkObject *obj); static void _add_handler (AtkObject *obj); @@ -14,8 +15,8 @@ static void _value_change_handler (AtkObject *obj, AtkPropertyValues *values) { const gchar *type_name = g_type_name (G_TYPE_FROM_INSTANCE (obj)); - GValue *value_back, val; - + GValue val = { 0, }; + GValue *value_back; value_back = &val; if (!ATK_IS_VALUE (obj)) { @@ -33,17 +34,17 @@ static void _value_change_handler (AtkObject *obj, g_print("Now calling the AtkValue interface functions\n"); - atk_value_get_current_value (ATK_VALUE(obj), value_back); + gail_test_value_get_current_value (ATK_VALUE(obj), value_back); g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back)); g_print ("atk_value_get_current_value returns %f\n", g_value_get_double (value_back) ); - atk_value_get_maximum_value (ATK_VALUE (obj), value_back); + gail_test_value_get_maximum_value (ATK_VALUE (obj), value_back); g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back)); g_print ("atk_value_get_maximum returns %f\n", g_value_get_double (value_back)); - atk_value_get_minimum_value (ATK_VALUE (obj), value_back); + gail_test_value_get_minimum_value (ATK_VALUE (obj), value_back); g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back)); g_print ("atk_value_get_minimum returns %f\n", g_value_get_double (value_back)); @@ -103,7 +104,8 @@ static void _add_handler (AtkObject *obj) static void _set_values (AtkObject *obj) { - GValue *value_back, val; + GValue val = { 0, }; + GValue *value_back; static gint count = 0; gdouble double_value; @@ -125,7 +127,7 @@ static void _set_values (AtkObject *obj) { text = "5.7"; atk_editable_text_set_text_contents(ATK_EDITABLE_TEXT(obj),text); g_print("Set text to %s\n",text); - atk_value_get_current_value(ATK_VALUE(obj), value_back); + gail_test_value_get_current_value(ATK_VALUE(obj), value_back); g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value_back)); g_print("atk_value_get_current_value returns %f\n", g_value_get_double( value_back)); @@ -134,7 +136,7 @@ static void _set_values (AtkObject *obj) { memset (value_back, 0, sizeof (GValue)); g_value_init (value_back, G_TYPE_DOUBLE); g_value_set_double (value_back, 10.0); - if (atk_value_set_current_value (ATK_VALUE (obj), value_back)) + if (gail_test_value_set_current_value (ATK_VALUE (obj), value_back)) { double_value = g_value_get_double (value_back); g_print("atk_value_set_current_value returns %f\n", From 920e0d54953f0d2813159d67dece1751ba549fed Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Fri, 7 Aug 2026 07:38:23 +0200 Subject: [PATCH 32/68] gtk: replace deprecated tooltip timestamp ABI field --- gtk/gtktooltips.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gtk/gtktooltips.h b/gtk/gtktooltips.h index eb1e0a0be..fbafd9939 100644 --- a/gtk/gtktooltips.h +++ b/gtk/gtktooltips.h @@ -74,7 +74,11 @@ struct _GtkTooltips guint have_grab : 1; guint use_sticky_delay : 1; gint timer_tag; - GTimeVal last_popdown; + struct + { + glong tv_sec; + glong tv_usec; + } last_popdown; }; struct _GtkTooltipsClass From 40f7cb303d3b46816fcde9d92a7207e93a9002c1 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 13 May 2026 10:19:49 +0200 Subject: [PATCH 33/68] introspection: pass compiler and GTK DLL path to scanner --- gtk/Makefile.am | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gtk/Makefile.am b/gtk/Makefile.am index c3c9a1493..31cc16730 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -2,6 +2,9 @@ include $(top_srcdir)/Makefile.decl -include $(INTROSPECTION_MAKEFILE) INTROSPECTION_GIRS = +if OS_WIN32 +INTROSPECTION_SCANNER_ENV = PATH="$(builddir):$(builddir)/.libs:$${PATH}" +endif INTROSPECTION_SCANNER_ARGS = \ --warn-all \ --add-include-path=../gdk From b8f0ddc2178bc67b8c9709415680e67dba0f49f0 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 10 Aug 2026 12:05:31 +0200 Subject: [PATCH 34/68] introspection: add GdkWin32 GIR generation --- gdk/Makefile.am | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 1b186afa1..1fc04ae97 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -213,6 +213,11 @@ Gdk_2_0_gir_CFLAGS = \ -I$(top_builddir) \ -I$(top_builddir)/gdk \ $(INCLUDES) + +if OS_WIN32 +Gdk_2_0_gir_CFLAGS += -I$(srcdir)/win32 -I$(srcdir) +endif + INTROSPECTION_GIRS += Gdk-2.0.gir if USE_X11 @@ -268,6 +273,69 @@ introspection_files += $(filter-out x11/gdkx.h, $(x11_introspection_files)) endif # USE_X11 +if OS_WIN32 +win32_introspection_files = \ + win32/gdkapplaunchcontext-win32.c \ + win32/gdkcolor-win32.c \ + win32/gdkcursor-win32.c \ + win32/gdkdisplay-win32.c \ + win32/gdkdnd-win32.c \ + win32/gdkdrawable-win32.c \ + win32/gdkdrawable-win32.h \ + win32/gdkevents-win32.c \ + win32/gdkfont-win32.c \ + win32/gdkgc-win32.c \ + win32/gdkgeometry-win32.c \ + win32/gdkglobals-win32.c \ + win32/gdkimage-win32.c \ + win32/gdkim-win32.c \ + win32/gdkinput.c \ + win32/gdkinput-win32.c \ + win32/gdkinput-win32.h \ + win32/gdkkeys-win32.c \ + win32/gdkmain-win32.c \ + win32/gdkpixmap-win32.c \ + win32/gdkpixmap-win32.h \ + win32/gdkprivate-win32.h \ + win32/gdkproperty-win32.c \ + win32/gdkscreen-win32.c \ + win32/gdkselection-win32.c \ + win32/gdkspawn-win32.c \ + win32/gdktestutils-win32.c \ + win32/gdkvisual-win32.c \ + win32/gdkwin32.h \ + win32/gdkwin32id.c \ + win32/gdkwin32keys.h \ + win32/gdkwindow-win32.c \ + win32/gdkwindow-win32.h + +GdkWin32-2.0.gir: $(gdktargetlib) Gdk-2.0.gir Makefile +GdkWin32_2_0_gir_SCANNERFLAGS = --identifier-prefix=Gdk --symbol-prefix=gdk --include-uninstalled=Gdk-2.0.gir +GdkWin32_2_0_gir_INCLUDES = Gio-2.0 GdkPixbuf-2.0 Pango-1.0 +GdkWin32_2_0_gir_LIBS = $(gdktargetlib) +GdkWin32_2_0_gir_FILES = $(filter-out \ + win32/gdkdrawable-win32.h \ + win32/gdkinput-win32.h \ + win32/gdkpixmap-win32.h \ + win32/gdkprivate-win32.h \ + win32/gdkwindow-win32.h, \ + $(win32_introspection_files)) +GdkWin32_2_0_gir_EXPORT_PACKAGES = gdk-win32-2.0 +GdkWin32_2_0_gir_CFLAGS = \ + -DGDK_COMPILATION \ + -I$(top_srcdir) \ + -I$(top_builddir) \ + -I$(top_builddir)/gdk \ + -I$(srcdir)/win32 \ + -I$(srcdir) \ + $(INCLUDES) \ + -L$(top_builddir)/gdk +INTROSPECTION_GIRS += GdkWin32-2.0.gir + +introspection_files += $(filter-out win32/gdkwin32.h, $(win32_introspection_files)) + +endif # OS_WIN32 + girdir = $(datadir)/gir-1.0 gir_DATA = $(INTROSPECTION_GIRS) From 8859141eb3328822c9aa775301414ef25679ce7e Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 10 Aug 2026 14:08:34 +0200 Subject: [PATCH 35/68] fontsel: localize common font style names --- gtk/gtkfontbutton.c | 3 +- gtk/gtkfontsel.c | 87 ++++++++++++++++++++++++++++++++++++++++++++- gtk/gtkprivate.h | 7 ++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/gtk/gtkfontbutton.c b/gtk/gtkfontbutton.c index 08aea103e..68df02b8d 100644 --- a/gtk/gtkfontbutton.c +++ b/gtk/gtkfontbutton.c @@ -889,7 +889,8 @@ gtk_font_button_update_font_info (GtkFontButton *font_button) if (style == NULL || !g_ascii_strcasecmp (style, "Regular")) family_style = g_strdup (family); else - family_style = g_strdup_printf ("%s %s", family, style); + family_style = g_strdup_printf ("%s %s", family, + _gtk_font_style_name_translate (style)); gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style); diff --git a/gtk/gtkfontsel.c b/gtk/gtkfontsel.c index 701d22841..28ede4eb4 100644 --- a/gtk/gtkfontsel.c +++ b/gtk/gtkfontsel.c @@ -76,6 +76,91 @@ #define DEFAULT_FONT_NAME "Sans 10" +#ifdef ENABLE_NLS +/* Pango uses these standard English names for common font faces. + * Keep the msgids explicit for gettext, but only translate exact matches: + * font-specific face names must remain untouched. + */ +#define FONT_STYLE_ULTRA_LIGHT N_("Ultra-Light") +#define FONT_STYLE_LIGHT N_("Light") +#define FONT_STYLE_MEDIUM N_("Medium") +#define FONT_STYLE_NORMAL N_("Normal") +#define FONT_STYLE_REGULAR N_("Regular") +#define FONT_STYLE_ITALIC N_("Italic") +#define FONT_STYLE_OBLIQUE N_("Oblique") +#define FONT_STYLE_SEMI_BOLD N_("Semi-Bold") +#define FONT_STYLE_BOLD N_("Bold") +#define FONT_STYLE_ULTRA_BOLD N_("Ultra-Bold") +#define FONT_STYLE_HEAVY N_("Heavy") +#define FONT_STYLE_BOLD_ITALIC N_("Bold Italic") + +const gchar * +_gtk_font_style_name_translate (const gchar *name) +{ + switch (name[0]) + { + case 'B': + if (strcmp (name, FONT_STYLE_BOLD) == 0) + return _(FONT_STYLE_BOLD); + if (strcmp (name, FONT_STYLE_BOLD_ITALIC) == 0) + return _(FONT_STYLE_BOLD_ITALIC); + break; + + case 'H': + if (strcmp (name, FONT_STYLE_HEAVY) == 0) + return _(FONT_STYLE_HEAVY); + break; + + case 'I': + if (strcmp (name, FONT_STYLE_ITALIC) == 0) + return _(FONT_STYLE_ITALIC); + break; + + case 'L': + if (strcmp (name, FONT_STYLE_LIGHT) == 0) + return _(FONT_STYLE_LIGHT); + break; + + case 'M': + if (strcmp (name, FONT_STYLE_MEDIUM) == 0) + return _(FONT_STYLE_MEDIUM); + break; + + case 'N': + if (strcmp (name, FONT_STYLE_NORMAL) == 0) + return _(FONT_STYLE_NORMAL); + break; + + case 'O': + if (strcmp (name, FONT_STYLE_OBLIQUE) == 0) + return _(FONT_STYLE_OBLIQUE); + break; + + case 'R': + if (strcmp (name, FONT_STYLE_REGULAR) == 0) + return _(FONT_STYLE_REGULAR); + break; + + case 'S': + if (strcmp (name, FONT_STYLE_SEMI_BOLD) == 0) + return _(FONT_STYLE_SEMI_BOLD); + break; + + case 'U': + if (strcmp (name, FONT_STYLE_ULTRA_LIGHT) == 0) + return _(FONT_STYLE_ULTRA_LIGHT); + if (strcmp (name, FONT_STYLE_ULTRA_BOLD) == 0) + return _(FONT_STYLE_ULTRA_BOLD); + break; + + default: + break; + } + + return name; +} +#endif /* ENABLE_NLS */ + /* This is the initial and maximum height of the preview entry (it expands when large font sizes are selected). Initial height is also the minimum. */ #define INITIAL_PREVIEW_HEIGHT 44 @@ -869,7 +954,7 @@ gtk_font_selection_show_available_styles (GtkFontSelection *fontsel) gtk_list_store_append (model, &iter); gtk_list_store_set (model, &iter, FACE_COLUMN, faces[i], - FACE_NAME_COLUMN, str, + FACE_NAME_COLUMN, _gtk_font_style_name_translate (str), -1); if (i == 0) diff --git a/gtk/gtkprivate.h b/gtk/gtkprivate.h index b2c2df999..cc1b50dfa 100644 --- a/gtk/gtkprivate.h +++ b/gtk/gtkprivate.h @@ -134,6 +134,13 @@ const gchar *_gtk_get_data_prefix (); #endif /* G_OS_WIN32 */ +#ifdef ENABLE_NLS +G_GNUC_INTERNAL +const gchar *_gtk_font_style_name_translate (const gchar *name); +#else +#define _gtk_font_style_name_translate(name) (name) +#endif + gboolean _gtk_fnmatch (const char *pattern, const char *string, gboolean no_leading_period); From 44f6ec9b037cedd350cfb96082f11dd9a003e0b5 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 11 Aug 2026 09:53:27 +0200 Subject: [PATCH 36/68] filechooser: reduce view mode startup overhead Replace the two-item GtkComboBoxText with a lightweight toggle and create icon-view zoom controls only when icon view is first used. Keep the existing icon-view zoom, cache invalidation and settings behavior while avoiding their startup cost for the default list view. Keep the zoom event boxes local instead of storing unused widget pointers. --- gtk/gtkfilechooserdefault.c | 116 ++++++++++++++++++++---------------- gtk/gtkfilechooserprivate.h | 4 +- 2 files changed, 64 insertions(+), 56 deletions(-) diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 2ce2d6c8f..fabf5913e 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -410,7 +410,8 @@ static void load_remove_timer (GtkFileChooserDefault *impl, LoadState new_load_s static void browse_files_center_selected_row (GtkFileChooserDefault *impl); static void view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode); -static void view_mode_combo_box_changed_cb (GtkComboBox *combo, +static void icon_view_scale_create (GtkFileChooserDefault *impl); +static void view_mode_toggle_button_toggled_cb (GtkToggleButton *toggle, GtkFileChooserDefault *impl); static void icon_view_scale_value_changed_cb (GtkRange *range, @@ -5208,12 +5209,22 @@ view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode) return; impl->view_mode = view_mode; - gtk_combo_box_set_active (GTK_COMBO_BOX (impl->view_mode_combo_box), - view_mode); + gtk_toggle_button_set_active ( + GTK_TOGGLE_BUTTON (impl->view_mode_toggle_button), + view_mode == VIEW_MODE_ICON); /* Creating the target view */ if (view_mode == VIEW_MODE_ICON) { + if (!impl->icon_view_scale_hbox) + { + icon_view_scale_create (impl); + gtk_box_pack_end ( + GTK_BOX (impl->browse_path_bar_hbox), + impl->icon_view_scale_hbox, + FALSE, FALSE, 0); + } + create_browse_files_icon_view (impl); impl->browse_files_current_view = impl->browse_files_icon_view; old_view = impl->browse_files_tree_view; @@ -5246,7 +5257,8 @@ view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode) impl->browse_files_icon_view = NULL; impl->list_icon_renderer = NULL; impl->list_name_renderer = NULL; - gtk_widget_hide (impl->icon_view_scale_hbox); + if (impl->icon_view_scale_hbox) + gtk_widget_hide (impl->icon_view_scale_hbox); } else g_assert_not_reached (); @@ -5264,12 +5276,14 @@ view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode) browse_files_center_selected_row (impl); } -/* Callback used when view mode combo box active item is changed */ +/* Callback used when the view mode toggle button is toggled. */ static void -view_mode_combo_box_changed_cb (GtkComboBox *combo, +view_mode_toggle_button_toggled_cb (GtkToggleButton *toggle, GtkFileChooserDefault *impl) { - ViewMode target = gtk_combo_box_get_active (combo); + ViewMode target = gtk_toggle_button_get_active (toggle) + ? VIEW_MODE_ICON + : VIEW_MODE_LIST; view_mode_set (impl, target); } @@ -5698,20 +5712,15 @@ location_button_toggled_cb (GtkToggleButton *toggle, location_mode_set (impl, new_mode, FALSE); } -/* Creates a combo box with two items: List View and Icon View. */ +/* Creates the view mode toggle. */ static void -view_mode_combo_box_create (GtkFileChooserDefault *impl) +view_mode_toggle_button_create (GtkFileChooserDefault *impl) { - impl->view_mode_combo_box = gtk_combo_box_text_new (); - gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(impl->view_mode_combo_box), - _("List View")); /* VIEW_MODE_LIST */ - gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(impl->view_mode_combo_box), - _("Icon View")); /* VIEW_MODE_ICON */ - gtk_combo_box_set_active (GTK_COMBO_BOX(impl->view_mode_combo_box), - VIEW_MODE_LIST); + impl->view_mode_toggle_button = + gtk_toggle_button_new_with_label (_("Icon View")); - g_signal_connect (impl->view_mode_combo_box, "changed", - G_CALLBACK (view_mode_combo_box_changed_cb), impl); + g_signal_connect (impl->view_mode_toggle_button, "toggled", + G_CALLBACK (view_mode_toggle_button_toggled_cb), impl); } /* Creates a hscale for the icon view. */ @@ -5719,46 +5728,48 @@ static void icon_view_scale_create (GtkFileChooserDefault *impl) { GtkObject *adj; + GtkWidget *zoom_icon; GtkWidget *image; impl->icon_view_scale_hbox = gtk_hbox_new (FALSE, 12); - impl->icon_view_scale_zoom_out_icon = gtk_event_box_new (); - gtk_event_box_set_visible_window (GTK_EVENT_BOX (impl->icon_view_scale_zoom_out_icon), FALSE); + zoom_icon = gtk_event_box_new (); + gtk_event_box_set_visible_window (GTK_EVENT_BOX (zoom_icon), FALSE); image = gtk_image_new_from_stock (GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_BUTTON); - gtk_container_add (GTK_CONTAINER (impl->icon_view_scale_zoom_out_icon), image); + gtk_container_add (GTK_CONTAINER (zoom_icon), image); gtk_widget_show (image); - g_object_set_data (G_OBJECT (impl->icon_view_scale_zoom_out_icon), + g_object_set_data (G_OBJECT (zoom_icon), "gtk-file-chooser-zoom-direction", GINT_TO_POINTER (-1)); - gtk_widget_set_tooltip_text (impl->icon_view_scale_zoom_out_icon, _("Zoom out")); - g_signal_connect (impl->icon_view_scale_zoom_out_icon, "button-press-event", + gtk_widget_set_tooltip_text (zoom_icon, _("Zoom out")); + g_signal_connect (zoom_icon, "button-press-event", G_CALLBACK (icon_view_zoom_button_press_cb), impl); - gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->icon_view_scale_zoom_out_icon); - gtk_box_pack_start (GTK_BOX (impl->icon_view_scale_hbox), impl->icon_view_scale_zoom_out_icon, FALSE, FALSE, 0); - gtk_widget_show (impl->icon_view_scale_zoom_out_icon); + gtk_size_group_add_widget (impl->browse_path_bar_size_group, zoom_icon); + gtk_box_pack_start (GTK_BOX (impl->icon_view_scale_hbox), zoom_icon, FALSE, FALSE, 0); + gtk_widget_show (zoom_icon); - adj = gtk_adjustment_new (32, 32, 256, 16, 16, 0); + adj = gtk_adjustment_new (impl->icon_view_icon_size, + 32, 256, 16, 16, 0); impl->icon_view_scale = gtk_hscale_new (GTK_ADJUSTMENT (adj)); gtk_scale_set_draw_value (GTK_SCALE (impl->icon_view_scale), FALSE); gtk_widget_set_size_request (impl->icon_view_scale, 100, -1); gtk_box_pack_start (GTK_BOX (impl->icon_view_scale_hbox), impl->icon_view_scale, FALSE, FALSE, 0); gtk_widget_show (impl->icon_view_scale); - impl->icon_view_scale_zoom_in_icon = gtk_event_box_new (); - gtk_event_box_set_visible_window (GTK_EVENT_BOX (impl->icon_view_scale_zoom_in_icon), FALSE); + zoom_icon = gtk_event_box_new (); + gtk_event_box_set_visible_window (GTK_EVENT_BOX (zoom_icon), FALSE); image = gtk_image_new_from_stock (GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_BUTTON); - gtk_container_add (GTK_CONTAINER (impl->icon_view_scale_zoom_in_icon), image); + gtk_container_add (GTK_CONTAINER (zoom_icon), image); gtk_widget_show (image); - g_object_set_data (G_OBJECT (impl->icon_view_scale_zoom_in_icon), + g_object_set_data (G_OBJECT (zoom_icon), "gtk-file-chooser-zoom-direction", GINT_TO_POINTER (1)); - gtk_widget_set_tooltip_text (impl->icon_view_scale_zoom_in_icon, _("Zoom in")); - g_signal_connect (impl->icon_view_scale_zoom_in_icon, "button-press-event", + gtk_widget_set_tooltip_text (zoom_icon, _("Zoom in")); + g_signal_connect (zoom_icon, "button-press-event", G_CALLBACK (icon_view_zoom_button_press_cb), impl); - gtk_size_group_add_widget (impl->browse_path_bar_size_group, impl->icon_view_scale_zoom_in_icon); - gtk_box_pack_start (GTK_BOX (impl->icon_view_scale_hbox), impl->icon_view_scale_zoom_in_icon, FALSE, FALSE, 0); - gtk_widget_show (impl->icon_view_scale_zoom_in_icon); + gtk_size_group_add_widget (impl->browse_path_bar_size_group, zoom_icon); + gtk_box_pack_start (GTK_BOX (impl->icon_view_scale_hbox), zoom_icon, FALSE, FALSE, 0); + gtk_widget_show (zoom_icon); g_signal_connect (impl->icon_view_scale, "value-changed", G_CALLBACK (icon_view_scale_value_changed_cb), impl); @@ -5883,9 +5894,9 @@ path_bar_widgets_create (GtkFileChooserDefault *impl) impl->browse_path_bar_size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL); gtk_size_group_set_ignore_hidden (impl->browse_path_bar_size_group, FALSE); - /* View mode combo box */ - view_mode_combo_box_create (impl); - gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->view_mode_combo_box, FALSE, FALSE, 0); + /* View mode selector */ + view_mode_toggle_button_create (impl); + gtk_box_pack_start (GTK_BOX (impl->browse_path_bar_hbox), impl->view_mode_toggle_button, FALSE, FALSE, 0); /* Location button */ location_button_create (impl); @@ -5908,10 +5919,6 @@ path_bar_widgets_create (GtkFileChooserDefault *impl) /* Widgets for special modes (recently-used in Open mode, Search mode) */ special_mode_widgets_create (impl); - /* Icon view scale */ - icon_view_scale_create (impl); - gtk_box_pack_end (GTK_BOX (impl->browse_path_bar_hbox), impl->icon_view_scale_hbox, FALSE, FALSE, 0); - /* Create Folder */ impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder")); g_signal_connect (impl->browse_new_folder_button, "clicked", @@ -6477,9 +6484,9 @@ update_appearance (GtkFileChooserDefault *impl) if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) - gtk_widget_show (impl->view_mode_combo_box); + gtk_widget_show (impl->view_mode_toggle_button); else - gtk_widget_hide (impl->view_mode_combo_box); + gtk_widget_hide (impl->view_mode_toggle_button); if (impl->location_entry) _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action); @@ -7043,13 +7050,16 @@ settings_load (GtkFileChooserDefault *impl) location_mode_set (impl, location_mode, TRUE); - g_signal_handlers_block_by_func (impl->icon_view_scale, - G_CALLBACK (icon_view_scale_value_changed_cb), - impl); - gtk_range_set_value (GTK_RANGE (impl->icon_view_scale), icon_view_scale); - g_signal_handlers_unblock_by_func (impl->icon_view_scale, - G_CALLBACK (icon_view_scale_value_changed_cb), - impl); + if (impl->icon_view_scale) + { + g_signal_handlers_block_by_func (impl->icon_view_scale, + G_CALLBACK (icon_view_scale_value_changed_cb), + impl); + gtk_range_set_value (GTK_RANGE (impl->icon_view_scale), icon_view_scale); + g_signal_handlers_unblock_by_func (impl->icon_view_scale, + G_CALLBACK (icon_view_scale_value_changed_cb), + impl); + } view_mode_set (impl, view_mode); diff --git a/gtk/gtkfilechooserprivate.h b/gtk/gtkfilechooserprivate.h index 193dea2e3..6e3456bc6 100644 --- a/gtk/gtkfilechooserprivate.h +++ b/gtk/gtkfilechooserprivate.h @@ -227,11 +227,9 @@ struct _GtkFileChooserDefault GtkWidget *extra_align; GtkWidget *extra_widget; - GtkWidget *view_mode_combo_box; + GtkWidget *view_mode_toggle_button; GtkWidget *icon_view_scale_hbox; GtkWidget *icon_view_scale; - GtkWidget *icon_view_scale_zoom_in_icon; - GtkWidget *icon_view_scale_zoom_out_icon; guint icon_view_scale_timeout_id; guint icon_view_scale_visible_timeout_id; guint icon_view_scale_after_layout_id; From a0aa5c3d61f7543c1690690a9bd33ecf29c15939 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 11 Aug 2026 12:45:22 +0200 Subject: [PATCH 37/68] gdk: add private pixel operation dispatch --- gdk/Makefile.am | 2 + gdk/gdkdraw.c | 150 ++++---------------------------------------- gdk/gdkpixelops.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++ gdk/gdkpixelops.h | 31 ++++++++++ gdk/makefile.msc | 1 + 5 files changed, 201 insertions(+), 137 deletions(-) create mode 100644 gdk/gdkpixelops.c create mode 100644 gdk/gdkpixelops.h diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 1fc04ae97..c598c6abb 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -115,6 +115,7 @@ gdk_built_private_headers = \ gdk_private_headers = \ gdkinternals.h \ gdkintl.h \ + gdkpixelops.h \ gdkpoly-generic.h \ gdkregion-generic.h @@ -129,6 +130,7 @@ gdk_c_sources = \ gdkdisplaymanager.c \ gdkdnd.c \ gdkdraw.c \ + gdkpixelops.c \ gdkevents.c \ gdkfont.c \ gdkgc.c \ diff --git a/gdk/gdkdraw.c b/gdk/gdkdraw.c index b6b0a5953..3d159dd37 100644 --- a/gdk/gdkdraw.c +++ b/gdk/gdkdraw.c @@ -34,6 +34,7 @@ #include "gdkwindow.h" #include "gdkscreen.h" #include "gdkpixbuf.h" +#include "gdkpixelops.h" #include "gdkalias.h" static GdkImage* gdk_drawable_real_get_image (GdkDrawable *drawable, @@ -1403,62 +1404,6 @@ composite (guchar *src_buf, } } -static void -composite_0888 (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; - - while (height--) - { - gint twidth = width; - guchar *p = src; - guchar *q = dest; - - if (dest_byte_order == GDK_LSB_FIRST) - { - while (twidth--) - { - guint t; - - t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80; - q[0] = (t + (t >> 8)) >> 8; - t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80; - q[1] = (t + (t >> 8)) >> 8; - t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80; - q[2] = (t + (t >> 8)) >> 8; - p += 4; - q += 4; - } - } - else - { - while (twidth--) - { - guint t; - - t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80; - q[1] = (t + (t >> 8)) >> 8; - t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80; - q[2] = (t + (t >> 8)) >> 8; - t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80; - q[3] = (t + (t >> 8)) >> 8; - p += 4; - q += 4; - } - } - - src += src_rowstride; - dest += dest_rowstride; - } -} - #ifdef USE_MEDIALIB static void composite_0888_medialib (guchar *src_buf, @@ -1497,82 +1442,6 @@ composite_0888_medialib (guchar *src_buf, } #endif -static void -composite_565 (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; - - while (height--) - { - gint twidth = width; - guchar *p = src; - gushort *q = (gushort *)dest; - - while (twidth--) - { - guchar a = p[3]; - guint tr, tg, tb; - guint tr1, tg1, tb1; - guint tmp = *q; - -#if 1 - /* This is fast, and corresponds to what composite() above does - * if we converted to 8-bit first. - */ - tr = (tmp & 0xf800); - tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tg = (tmp & 0x07e0); - tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tb = (tmp & 0x001f); - tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - - *q = (((tr1 + (tr1 >> 8)) & 0xf800) | - (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | - ((tb1 + (tb1 >> 8)) >> 11)); -#else - /* This version correspond to the result we get with XRENDER - - * a bit of precision is lost since we convert to 8 bit after premultiplying - * instead of at the end - */ - guint tr2, tg2, tb2; - guint tr3, tg3, tb3; - - tr = (tmp & 0xf800); - tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tr2 = a * p[0] + 0x80; - tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8); - - tg = (tmp & 0x07e0); - tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tg2 = a * p[0] + 0x80; - tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8); - - tb = (tmp & 0x001f); - tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - tb2 = a * p[0] + 0x80; - tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8); - - *q = (((tr3 & 0xf8) << 8) | - ((tg3 & 0xfc) << 3) | - ((tb3 >> 3))); -#endif - - p += 4; - q++; - } - - src += src_rowstride; - dest += dest_rowstride; - } -} - /* Implementation of the old vfunc in terms of the new one in case someone calls it directly (which they shouldn't!) */ static void @@ -1728,7 +1597,10 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable, visual->red_mask == 0xf800 && visual->green_mask == 0x07e0 && visual->blue_mask == 0x001f) - composite_func = composite_565; + { + composite_func = _gdk_pixel_ops.composite_565; + goto composite_selected; + } else if (visual->depth == 24 && bits_per_pixel == 32 && visual->red_mask == 0xff0000 && visual->green_mask == 0x00ff00 && @@ -1738,16 +1610,20 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable, if (_gdk_use_medialib ()) composite_func = composite_0888_medialib; else - composite_func = composite_0888; + composite_func = _gdk_pixel_ops.composite_0888; #else - composite_func = composite_0888; + composite_func = _gdk_pixel_ops.composite_0888; #endif + goto composite_selected; } } + goto no_composite; /* We can't use our composite func if we are required to dither */ - if (composite_func && !(dither == GDK_RGB_DITHER_MAX && visual->depth != 24)) + +composite_selected: + if (!(dither == GDK_RGB_DITHER_MAX && visual->depth != 24)) { gint x0, y0; for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT) @@ -1782,7 +1658,7 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable, goto out; } - else +no_composite: { /* No special composition func, convert dest to 24 bit RGB data, composite against * that, and convert back. diff --git a/gdk/gdkpixelops.c b/gdk/gdkpixelops.c new file mode 100644 index 000000000..1dbef2105 --- /dev/null +++ b/gdk/gdkpixelops.c @@ -0,0 +1,154 @@ +/* GDK - The GIMP Drawing Kit + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#include "config.h" + +#include "gdkpixelops.h" + +static void +composite_0888 (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + guchar *q = dest; + + if (dest_byte_order == GDK_LSB_FIRST) + { + while (twidth--) + { + guint t; + + t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80; + q[0] = (t + (t >> 8)) >> 8; + t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80; + q[1] = (t + (t >> 8)) >> 8; + t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80; + q[2] = (t + (t >> 8)) >> 8; + p += 4; + q += 4; + } + } + else + { + while (twidth--) + { + guint t; + + t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80; + q[1] = (t + (t >> 8)) >> 8; + t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80; + q[2] = (t + (t >> 8)) >> 8; + t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80; + q[3] = (t + (t >> 8)) >> 8; + p += 4; + q += 4; + } + } + + src += src_rowstride; + dest += dest_rowstride; + } +} + +static void +composite_565 (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + gushort *q = (gushort *)dest; + + while (twidth--) + { + guchar a = p[3]; + guint tr, tg, tb; + guint tr1, tg1, tb1; + guint tmp = *q; + +#if 1 + /* This is fast, and corresponds to what composite() above does + * if we converted to 8-bit first. + */ + tr = (tmp & 0xf800); + tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; + tg = (tmp & 0x07e0); + tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; + tb = (tmp & 0x001f); + tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; + + *q = (((tr1 + (tr1 >> 8)) & 0xf800) | + (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | + ((tb1 + (tb1 >> 8)) >> 11)); +#else + /* This version correspond to the result we get with XRENDER - + * a bit of precision is lost since we convert to 8 bit after premultiplying + * instead of at the end + */ + guint tr2, tg2, tb2; + guint tr3, tg3, tb3; + + tr = (tmp & 0xf800); + tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; + tr2 = a * p[0] + 0x80; + tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8); + + tg = (tmp & 0x07e0); + tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; + tg2 = a * p[0] + 0x80; + tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8); + + tb = (tmp & 0x001f); + tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; + tb2 = a * p[0] + 0x80; + tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8); + + *q = (((tr3 & 0xf8) << 8) | + ((tg3 & 0xfc) << 3) | + ((tb3 >> 3))); +#endif + + p += 4; + q++; + } + + src += src_rowstride; + dest += dest_rowstride; + } +} + +G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops = { + composite_0888, + composite_565 +}; diff --git a/gdk/gdkpixelops.h b/gdk/gdkpixelops.h new file mode 100644 index 000000000..21713b27b --- /dev/null +++ b/gdk/gdkpixelops.h @@ -0,0 +1,31 @@ +/* GDK - The GIMP Drawing Kit + * + * Private pixel operation dispatch. + */ + +#ifndef __GDK_PIXEL_OPS_H__ +#define __GDK_PIXEL_OPS_H__ + +#include "gdktypes.h" + +G_BEGIN_DECLS + +typedef void (*GdkCompositeFunc) (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +typedef struct +{ + GdkCompositeFunc composite_0888; + GdkCompositeFunc composite_565; +} GdkPixelOps; + +extern G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops; + +G_END_DECLS + +#endif /* __GDK_PIXEL_OPS_H__ */ diff --git a/gdk/makefile.msc b/gdk/makefile.msc index e9f816931..d59957300 100644 --- a/gdk/makefile.msc +++ b/gdk/makefile.msc @@ -72,6 +72,7 @@ gdk_OBJECTS = \ gdkdisplaymanager.obj \ gdkdnd.obj \ gdkdraw.obj \ + gdkpixelops.obj \ gdkenumtypes.obj \ gdkevents.obj \ gdkfont.obj \ From 699d5777f225172d891bb18247fa50adebe423de Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 11 Aug 2026 18:53:26 +0200 Subject: [PATCH 38/68] gdk: separate generic pixel operations --- gdk/Makefile.am | 6 +- gdk/gdkpixelops-generic.c | 149 +++++++++++++++++++++++++++++++++++++ gdk/gdkpixelops.c | 150 +++++--------------------------------- gdk/makefile.msc | 1 + 4 files changed, 173 insertions(+), 133 deletions(-) create mode 100644 gdk/gdkpixelops-generic.c diff --git a/gdk/Makefile.am b/gdk/Makefile.am index c598c6abb..c28571ace 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -119,6 +119,9 @@ gdk_private_headers = \ gdkpoly-generic.h \ gdkregion-generic.h +gdk_pixelops_private_sources = \ + gdkpixelops-generic.c + gdk_c_sources = \ $(medialib_sources) \ gdk.c \ @@ -131,6 +134,7 @@ gdk_c_sources = \ gdkdnd.c \ gdkdraw.c \ gdkpixelops.c \ + $(gdk_pixelops_private_sources) \ gdkevents.c \ gdkfont.c \ gdkgc.c \ @@ -196,7 +200,7 @@ if HAVE_INTROSPECTION base_introspection_files = \ $(filter-out gdkkeysyms-compat.h, $(gdk_public_h_sources)) \ - $(gdk_c_sources) \ + $(filter-out $(gdk_pixelops_private_sources), $(gdk_c_sources)) \ gdkkeynames.c \ gdkenumtypes.c \ gdkenumtypes.h diff --git a/gdk/gdkpixelops-generic.c b/gdk/gdkpixelops-generic.c new file mode 100644 index 000000000..01a97a621 --- /dev/null +++ b/gdk/gdkpixelops-generic.c @@ -0,0 +1,149 @@ +/* GDK - The GIMP Drawing Kit + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#include "config.h" + +#include "gdkpixelops.h" + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + guchar *q = dest; + + if (dest_byte_order == GDK_LSB_FIRST) + { + while (twidth--) + { + guint t; + + t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80; + q[0] = (t + (t >> 8)) >> 8; + t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80; + q[1] = (t + (t >> 8)) >> 8; + t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80; + q[2] = (t + (t >> 8)) >> 8; + p += 4; + q += 4; + } + } + else + { + while (twidth--) + { + guint t; + + t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80; + q[1] = (t + (t >> 8)) >> 8; + t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80; + q[2] = (t + (t >> 8)) >> 8; + t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80; + q[3] = (t + (t >> 8)) >> 8; + p += 4; + q += 4; + } + } + + src += src_rowstride; + dest += dest_rowstride; + } +} + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + gushort *q = (gushort *)dest; + + while (twidth--) + { + guchar a = p[3]; + guint tr, tg, tb; + guint tr1, tg1, tb1; + guint tmp = *q; + +#if 1 + /* This is fast, and corresponds to what composite() above does + * if we converted to 8-bit first. + */ + tr = (tmp & 0xf800); + tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; + tg = (tmp & 0x07e0); + tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; + tb = (tmp & 0x001f); + tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; + + *q = (((tr1 + (tr1 >> 8)) & 0xf800) | + (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | + ((tb1 + (tb1 >> 8)) >> 11)); +#else + /* This version correspond to the result we get with XRENDER - + * a bit of precision is lost since we convert to 8 bit after premultiplying + * instead of at the end + */ + guint tr2, tg2, tb2; + guint tr3, tg3, tb3; + + tr = (tmp & 0xf800); + tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; + tr2 = a * p[0] + 0x80; + tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8); + + tg = (tmp & 0x07e0); + tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; + tg2 = a * p[0] + 0x80; + tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8); + + tb = (tmp & 0x001f); + tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; + tb2 = a * p[0] + 0x80; + tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8); + + *q = (((tr3 & 0xf8) << 8) | + ((tg3 & 0xfc) << 3) | + ((tb3 >> 3))); +#endif + + p += 4; + q++; + } + + src += src_rowstride; + dest += dest_rowstride; + } +} diff --git a/gdk/gdkpixelops.c b/gdk/gdkpixelops.c index 1dbef2105..6292fcac0 100644 --- a/gdk/gdkpixelops.c +++ b/gdk/gdkpixelops.c @@ -16,139 +16,25 @@ #include "gdkpixelops.h" -static void -composite_0888 (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); - while (height--) - { - gint twidth = width; - guchar *p = src; - guchar *q = dest; - - if (dest_byte_order == GDK_LSB_FIRST) - { - while (twidth--) - { - guint t; - - t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80; - q[0] = (t + (t >> 8)) >> 8; - t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80; - q[1] = (t + (t >> 8)) >> 8; - t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80; - q[2] = (t + (t >> 8)) >> 8; - p += 4; - q += 4; - } - } - else - { - while (twidth--) - { - guint t; - - t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80; - q[1] = (t + (t >> 8)) >> 8; - t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80; - q[2] = (t + (t >> 8)) >> 8; - t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80; - q[3] = (t + (t >> 8)) >> 8; - p += 4; - q += 4; - } - } - - src += src_rowstride; - dest += dest_rowstride; - } -} - -static void -composite_565 (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; - - while (height--) - { - gint twidth = width; - guchar *p = src; - gushort *q = (gushort *)dest; - - while (twidth--) - { - guchar a = p[3]; - guint tr, tg, tb; - guint tr1, tg1, tb1; - guint tmp = *q; - -#if 1 - /* This is fast, and corresponds to what composite() above does - * if we converted to 8-bit first. - */ - tr = (tmp & 0xf800); - tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tg = (tmp & 0x07e0); - tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tb = (tmp & 0x001f); - tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - - *q = (((tr1 + (tr1 >> 8)) & 0xf800) | - (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | - ((tb1 + (tb1 >> 8)) >> 11)); -#else - /* This version correspond to the result we get with XRENDER - - * a bit of precision is lost since we convert to 8 bit after premultiplying - * instead of at the end - */ - guint tr2, tg2, tb2; - guint tr3, tg3, tb3; - - tr = (tmp & 0xf800); - tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tr2 = a * p[0] + 0x80; - tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8); - - tg = (tmp & 0x07e0); - tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tg2 = a * p[0] + 0x80; - tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8); - - tb = (tmp & 0x001f); - tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - tb2 = a * p[0] + 0x80; - tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8); - - *q = (((tr3 & 0xf8) << 8) | - ((tg3 & 0xfc) << 3) | - ((tb3 >> 3))); -#endif - - p += 4; - q++; - } - - src += src_rowstride; - dest += dest_rowstride; - } -} +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops = { - composite_0888, - composite_565 + _gdk_pixel_ops_composite_0888_generic, + _gdk_pixel_ops_composite_565_generic }; diff --git a/gdk/makefile.msc b/gdk/makefile.msc index d59957300..93e5d8994 100644 --- a/gdk/makefile.msc +++ b/gdk/makefile.msc @@ -73,6 +73,7 @@ gdk_OBJECTS = \ gdkdnd.obj \ gdkdraw.obj \ gdkpixelops.obj \ + gdkpixelops-generic.obj \ gdkenumtypes.obj \ gdkevents.obj \ gdkfont.obj \ From 1c3b48d6e64eec3352c79d67506f668b3ad3cbdb Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 11 Aug 2026 19:26:17 +0200 Subject: [PATCH 39/68] gdk: select pixelops sources by target architecture --- configure.ac | 14 ++++++++++++++ gdk/Makefile.am | 9 ++++++++- gdk/gdkpixelops-x86.c | 13 +++++++++++++ gdk/makefile.msc | 1 + 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 gdk/gdkpixelops-x86.c diff --git a/configure.ac b/configure.ac index 04536e910..58b30a8b2 100644 --- a/configure.ac +++ b/configure.ac @@ -117,6 +117,20 @@ AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE", AC_CANONICAL_HOST +gdk_pixelops_i386=no + +case "$host_cpu" in + i?86) + gdk_pixelops_i386=yes + AC_DEFINE([HAVE_GDK_PIXELOPS_INIT], [1], + [Define if GDK pixel operations require runtime dispatch initialization]) + ;; +esac + +AM_CONDITIONAL(GDK_PIXELOPS_I386, + test "$gdk_pixelops_i386" = yes) + + MATH_LIB=-lm AC_MSG_CHECKING([for native Win32]) LIB_EXE_MACHINE_FLAG=X86 diff --git a/gdk/Makefile.am b/gdk/Makefile.am index c28571ace..79730e079 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -119,8 +119,15 @@ gdk_private_headers = \ gdkpoly-generic.h \ gdkregion-generic.h +gdk_pixelops_arch_sources = + +if GDK_PIXELOPS_I386 +gdk_pixelops_arch_sources += gdkpixelops-x86.c +endif + gdk_pixelops_private_sources = \ - gdkpixelops-generic.c + gdkpixelops-generic.c \ + $(gdk_pixelops_arch_sources) gdk_c_sources = \ $(medialib_sources) \ diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c new file mode 100644 index 000000000..ee2ba2833 --- /dev/null +++ b/gdk/gdkpixelops-x86.c @@ -0,0 +1,13 @@ +/* GDK - The GIMP Drawing Kit + * + * x86 pixel operation backend. + * + * This file is selected at build time only for x86 targets. + */ + +#include "config.h" + +#if !defined(__i386__) && !defined(__x86_64__) && \ + !defined(_M_IX86) && !defined(_M_X64) +#error "gdkpixelops-x86.c must only be built for an x86 target" +#endif diff --git a/gdk/makefile.msc b/gdk/makefile.msc index 93e5d8994..446182bf5 100644 --- a/gdk/makefile.msc +++ b/gdk/makefile.msc @@ -74,6 +74,7 @@ gdk_OBJECTS = \ gdkdraw.obj \ gdkpixelops.obj \ gdkpixelops-generic.obj \ + gdkpixelops-x86.obj \ gdkenumtypes.obj \ gdkevents.obj \ gdkfont.obj \ From ade749c68ba371d6cd23493b1539e4abcd2f1186 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 12 Aug 2026 10:07:39 +0200 Subject: [PATCH 40/68] gdk: add i386 MMX pixel compositing --- gdk/Makefile.am | 19 +++++-- gdk/gdk.c | 3 + gdk/gdkpixelops-x86-mmx.c | 116 ++++++++++++++++++++++++++++++++++++++ gdk/gdkpixelops-x86.c | 28 +++++++++ gdk/gdkpixelops.h | 6 ++ 5 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 gdk/gdkpixelops-x86-mmx.c diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 79730e079..415df2c88 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -14,6 +14,8 @@ DIST_SUBDIRS = win32 x11 quartz directfb tests CLEANFILES = +noinst_LTLIBRARIES = + EXTRA_DIST += \ keynames.txt \ keyname-table.h \ @@ -120,9 +122,18 @@ gdk_private_headers = \ gdkregion-generic.h gdk_pixelops_arch_sources = +gdk_pixelops_arch_libs = if GDK_PIXELOPS_I386 gdk_pixelops_arch_sources += gdkpixelops-x86.c + +noinst_LTLIBRARIES += libgdk-pixelops-x86-mmx.la +gdk_pixelops_arch_libs += libgdk-pixelops-x86-mmx.la + +libgdk_pixelops_x86_mmx_la_SOURCES = gdkpixelops-x86-mmx.c +libgdk_pixelops_x86_mmx_la_CFLAGS = \ + $(AM_CFLAGS) \ + -mmmx -mno-sse -mno-sse2 -mno-3dnow endif gdk_pixelops_private_sources = \ @@ -187,19 +198,19 @@ common_sources = \ gdkmarshalers.h libgdk_directfb_2_0_la_SOURCES = $(common_sources) -libgdk_directfb_2_0_la_LIBADD = directfb/libgdk-directfb.la $(GDK_DEP_LIBS) +libgdk_directfb_2_0_la_LIBADD = directfb/libgdk-directfb.la $(gdk_pixelops_arch_libs) $(GDK_DEP_LIBS) libgdk_directfb_2_0_la_LDFLAGS = $(LDADD) libgdk_x11_2_0_la_SOURCES = $(common_sources) -libgdk_x11_2_0_la_LIBADD = x11/libgdk-x11.la $(GDK_DEP_LIBS) +libgdk_x11_2_0_la_LIBADD = x11/libgdk-x11.la $(gdk_pixelops_arch_libs) $(GDK_DEP_LIBS) libgdk_x11_2_0_la_LDFLAGS = $(LDADD) libgdk_quartz_2_0_la_SOURCES = $(common_sources) gdkkeynames.c -libgdk_quartz_2_0_la_LIBADD = quartz/libgdk-quartz.la $(GDK_DEP_LIBS) +libgdk_quartz_2_0_la_LIBADD = quartz/libgdk-quartz.la $(gdk_pixelops_arch_libs) $(GDK_DEP_LIBS) libgdk_quartz_2_0_la_LDFLAGS = $(LDADD) libgdk_win32_2_0_la_SOURCES = $(common_sources) gdkkeynames.c -libgdk_win32_2_0_la_LIBADD = win32/libgdk-win32.la $(GDK_DEP_LIBS) +libgdk_win32_2_0_la_LIBADD = win32/libgdk-win32.la $(gdk_pixelops_arch_libs) $(GDK_DEP_LIBS) libgdk_win32_2_0_la_DEPENDENCIES = win32/libgdk-win32.la win32/rc/gdk-win32-res.o gdk.def libgdk_win32_2_0_la_LDFLAGS = -Wl,win32/rc/gdk-win32-res.o -export-symbols $(srcdir)/gdk.def $(LDADD) diff --git a/gdk/gdk.c b/gdk/gdk.c index 63f2b70ef..4c2246808 100644 --- a/gdk/gdk.c +++ b/gdk/gdk.c @@ -32,6 +32,7 @@ #include "gdk.h" #include "gdkinternals.h" #include "gdkintl.h" +#include "gdkpixelops.h" #ifndef HAVE_XCONVERTCASE #include "gdkkeysyms.h" @@ -189,6 +190,8 @@ gdk_pre_parse_libgtk_only (void) { gdk_initialized = TRUE; + _gdk_pixel_ops_init (); + /* We set the fallback program class here, rather than lazily in * gdk_get_program_class, since we don't want -name to override it. */ diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c new file mode 100644 index 000000000..4f97395e5 --- /dev/null +++ b/gdk/gdkpixelops-x86-mmx.c @@ -0,0 +1,116 @@ +/* GDK - The GIMP Drawing Kit + * + * MMX pixel operations for 32-bit x86. + */ + +#include "config.h" + +#include +#include + +#include "gdkpixelops.h" + +#ifndef __i386__ +#error "gdkpixelops-x86-mmx.c requires a 32-bit x86 target" +#endif + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + if (dest_byte_order != GDK_LSB_FIRST) + { + _gdk_pixel_ops_composite_0888_generic (src_buf, + src_rowstride, + dest_buf, + dest_rowstride, + dest_byte_order, + width, + height); + return; + } + + { + const __m64 zero = _mm_setzero_si64 (); + const __m64 ones = _mm_cmpeq_pi16 (zero, zero); + const __m64 ff = _mm_srli_pi16 (ones, 8); + const __m64 bias = _mm_set1_pi16 (0x80); + const __m64 rgb_mask = _mm_srli_si64 (ones, 16); + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + guchar *q = dest; + + while (twidth--) + { + guint32 sp; + guint32 dp; + guint32 result; + __m64 s; + __m64 d; + __m64 a; + __m64 ia; + __m64 t; + __m64 packed; + + memcpy (&sp, p, sizeof sp); + memcpy (&dp, q, sizeof dp); + + sp = ((sp & 0xff00ff00U) | + ((sp & 0x000000ffU) << 16) | + ((sp & 0x00ff0000U) >> 16)); + + s = _mm_cvtsi32_si64 ((int) sp); + d = _mm_cvtsi32_si64 ((int) dp); + + s = _mm_unpacklo_pi8 (s, zero); + d = _mm_unpacklo_pi8 (d, zero); + + a = _mm_srli_si64 (s, 48); + a = _mm_unpacklo_pi16 (a, a); + a = _mm_unpacklo_pi32 (a, a); + a = _mm_and_si64 (a, rgb_mask); + + ia = _mm_sub_pi16 (ff, a); + + t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), + _mm_mullo_pi16 (d, ia)); + + t = _mm_add_pi16 (t, bias); + t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); + t = _mm_srli_pi16 (t, 8); + + packed = _mm_packs_pu16 (t, t); + result = (guint32) _mm_cvtsi64_si32 (packed); + + memcpy (q, &result, sizeof result); + + p += 4; + q += 4; + } + + src += src_rowstride; + dest += dest_rowstride; + } + + _mm_empty (); + } +} diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index ee2ba2833..03d5aabc2 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -7,7 +7,35 @@ #include "config.h" +#include "gdkpixelops.h" + #if !defined(__i386__) && !defined(__x86_64__) && \ !defined(_M_IX86) && !defined(_M_X64) #error "gdkpixelops-x86.c must only be built for an x86 target" #endif + + +#include + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +G_GNUC_INTERNAL void +_gdk_pixel_ops_init (void) +{ + unsigned int eax; + unsigned int ebx; + unsigned int ecx; + unsigned int edx; + + if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) && + (edx & bit_MMX) != 0) + _gdk_pixel_ops.composite_0888 = + _gdk_pixel_ops_composite_0888_mmx; +} diff --git a/gdk/gdkpixelops.h b/gdk/gdkpixelops.h index 21713b27b..c070e6a25 100644 --- a/gdk/gdkpixelops.h +++ b/gdk/gdkpixelops.h @@ -26,6 +26,12 @@ typedef struct extern G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops; +#ifdef HAVE_GDK_PIXELOPS_INIT +G_GNUC_INTERNAL void _gdk_pixel_ops_init (void); +#else +#define _gdk_pixel_ops_init() ((void) 0) +#endif + G_END_DECLS #endif /* __GDK_PIXEL_OPS_H__ */ From fc5ef9a7c3fbaa2718687077433035a4422d986b Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 12 Aug 2026 12:02:30 +0200 Subject: [PATCH 41/68] gdk: add i386 MMX RGB565 compositing --- gdk/gdkpixelops-x86-mmx.c | 143 ++++++++++++++++++++++++++++++++++++++ gdk/gdkpixelops-x86.c | 17 ++++- 2 files changed, 158 insertions(+), 2 deletions(-) diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c index 4f97395e5..b0def2b5e 100644 --- a/gdk/gdkpixelops-x86-mmx.c +++ b/gdk/gdkpixelops-x86-mmx.c @@ -114,3 +114,146 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, _mm_empty (); } } + +static guint32 +expand_565 (guint16 value) +{ + guint32 hi; + + hi = (((guint32) value & 0xf800U) >> 8) | + (((guint32) value & 0x07e0U) << 5) | + (((guint32) value & 0x001fU) << 19); + + return hi | + ((hi >> 5) & 0x00070007U) | + ((hi >> 6) & 0x00000300U); +} + +static guint16 +pack_565 (guint32 rgb) +{ + return (guint16) + (((rgb & 0x000000f8U) << 8) | + ((rgb & 0x0000fc00U) >> 5) | + ((rgb & 0x00f80000U) >> 19)); +} + +static guint32 +blend_565_mmx (__m64 zero, + __m64 ff, + __m64 bias, + guint32 src, + guint32 dest) +{ + __m64 s; + __m64 d; + __m64 a; + __m64 ia; + __m64 t; + guint32 result; + + s = _mm_cvtsi32_si64 ((int) src); + d = _mm_cvtsi32_si64 ((int) dest); + + s = _mm_unpacklo_pi8 (s, zero); + d = _mm_unpacklo_pi8 (d, zero); + + a = _mm_srli_si64 (s, 48); + a = _mm_unpacklo_pi16 (a, a); + a = _mm_unpacklo_pi32 (a, a); + + ia = _mm_sub_pi16 (ff, a); + + t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), + _mm_mullo_pi16 (d, ia)); + + t = _mm_add_pi16 (t, bias); + t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); + t = _mm_srli_pi16 (t, 8); + t = _mm_packs_pu16 (t, t); + + __asm__ ("movd %1,%0" + : "=r" (result) + : "y" (t)); + + return result; +} + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_mmx (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + const __m64 zero = _mm_setzero_si64 (); + const __m64 ones = _mm_cmpeq_pi16 (zero, zero); + const __m64 ff = _mm_srli_pi16 (ones, 8); + const __m64 bias = _mm_set1_pi16 (0x80); + guchar *src = src_buf; + guchar *dest = dest_buf; + + (void) dest_byte_order; + + while (height--) + { + gint pairs = width >> 1; + guchar *p = src; + guchar *q = dest; + + while (pairs--) + { + guint32 sp0; + guint32 sp1; + guint32 dpair; + guint32 dp0; + guint32 dp1; + guint32 rgb0; + guint32 rgb1; + guint32 result; + + memcpy (&sp0, p, sizeof sp0); + memcpy (&sp1, p + 4, sizeof sp1); + memcpy (&dpair, q, sizeof dpair); + + dp0 = expand_565 ((guint16) dpair); + dp1 = expand_565 ((guint16) (dpair >> 16)); + + rgb0 = blend_565_mmx (zero, ff, bias, sp0, dp0); + rgb1 = blend_565_mmx (zero, ff, bias, sp1, dp1); + + result = (guint32) pack_565 (rgb0); + result |= (guint32) pack_565 (rgb1) << 16; + + memcpy (q, &result, sizeof result); + + p += 8; + q += 4; + } + + if (width & 1) + { + guint32 sp; + guint16 dp16; + guint16 result; + guint32 dp; + guint32 rgb; + + memcpy (&sp, p, sizeof sp); + memcpy (&dp16, q, sizeof dp16); + + dp = expand_565 (dp16); + rgb = blend_565_mmx (zero, ff, bias, sp, dp); + result = pack_565 (rgb); + + memcpy (q, &result, sizeof result); + } + + src += src_rowstride; + dest += dest_rowstride; + } + + _mm_empty (); +} diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 03d5aabc2..9bf910d03 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -26,6 +26,15 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, gint width, gint height); +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_mmx (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + G_GNUC_INTERNAL void _gdk_pixel_ops_init (void) { @@ -36,6 +45,10 @@ _gdk_pixel_ops_init (void) if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) && (edx & bit_MMX) != 0) - _gdk_pixel_ops.composite_0888 = - _gdk_pixel_ops_composite_0888_mmx; + { + _gdk_pixel_ops.composite_0888 = + _gdk_pixel_ops_composite_0888_mmx; + _gdk_pixel_ops.composite_565 = + _gdk_pixel_ops_composite_565_mmx; + } } From beb0061b14a60a2c0b631641e9193c5138a84103 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 12 Aug 2026 15:25:14 +0200 Subject: [PATCH 42/68] gdk: add i386 3DNow pixel compositing --- gdk/Makefile.am | 9 ++++++ gdk/gdkpixelops-x86-mmx.c | 62 ++++++++++++++++++++++++++++++++++++--- gdk/gdkpixelops-x86.c | 27 +++++++++++++++++ 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 415df2c88..20da9ee26 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -134,6 +134,15 @@ libgdk_pixelops_x86_mmx_la_SOURCES = gdkpixelops-x86-mmx.c libgdk_pixelops_x86_mmx_la_CFLAGS = \ $(AM_CFLAGS) \ -mmmx -mno-sse -mno-sse2 -mno-3dnow + +noinst_LTLIBRARIES += libgdk-pixelops-x86-3dnow.la +gdk_pixelops_arch_libs += libgdk-pixelops-x86-3dnow.la + +libgdk_pixelops_x86_3dnow_la_SOURCES = gdkpixelops-x86-mmx.c +libgdk_pixelops_x86_3dnow_la_CFLAGS = \ + $(AM_CFLAGS) \ + -mmmx -m3dnow -mno-sse -mno-sse2 \ + -DGDK_PIXELOPS_X86_3DNOW_IMPL endif gdk_pixelops_private_sources = \ diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c index b0def2b5e..9058f07b5 100644 --- a/gdk/gdkpixelops-x86-mmx.c +++ b/gdk/gdkpixelops-x86-mmx.c @@ -8,6 +8,17 @@ #include #include +#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL +#include +#define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_3dnow +#define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_3dnow +#define GDK_PIXELOPS_MMX_EMPTY() _m_femms () +#else +#define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_mmx +#define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_mmx +#define GDK_PIXELOPS_MMX_EMPTY() _mm_empty () +#endif + #include "gdkpixelops.h" #ifndef __i386__ @@ -23,8 +34,37 @@ _gdk_pixel_ops_composite_0888_generic (guchar *src_buf, gint width, gint height); +#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL +static __inline__ __m64 +blend_3dnow (__m64 s, + __m64 d, + __m64 a) +{ + __m64 scaled; + __m64 mask; + __m64 coeff; + __m64 delta; + __m64 correction; + __m64 t; + + scaled = _mm_slli_pi16 (a, 8); + mask = _mm_srai_pi16 (scaled, 15); + + coeff = _mm_add_pi16 (a, scaled); + coeff = _mm_sub_pi16 (coeff, mask); + + delta = _mm_sub_pi16 (s, d); + correction = _mm_and_si64 (delta, mask); + + t = _m_pmulhrw (delta, coeff); + t = _mm_add_pi16 (t, correction); + + return _mm_add_pi16 (t, d); +} +#endif + G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, +GDK_PIXELOPS_COMPOSITE_0888 (guchar *src_buf, gint src_rowstride, guchar *dest_buf, gint dest_rowstride, @@ -47,8 +87,10 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, { const __m64 zero = _mm_setzero_si64 (); const __m64 ones = _mm_cmpeq_pi16 (zero, zero); +#ifndef GDK_PIXELOPS_X86_3DNOW_IMPL const __m64 ff = _mm_srli_pi16 (ones, 8); const __m64 bias = _mm_set1_pi16 (0x80); +#endif const __m64 rgb_mask = _mm_srli_si64 (ones, 16); guchar *src = src_buf; guchar *dest = dest_buf; @@ -67,7 +109,9 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, __m64 s; __m64 d; __m64 a; +#ifndef GDK_PIXELOPS_X86_3DNOW_IMPL __m64 ia; +#endif __m64 t; __m64 packed; @@ -89,6 +133,9 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, a = _mm_unpacklo_pi32 (a, a); a = _mm_and_si64 (a, rgb_mask); +#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL + t = blend_3dnow (s, d, a); +#else ia = _mm_sub_pi16 (ff, a); t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), @@ -97,6 +144,7 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, t = _mm_add_pi16 (t, bias); t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); t = _mm_srli_pi16 (t, 8); +#endif packed = _mm_packs_pu16 (t, t); result = (guint32) _mm_cvtsi64_si32 (packed); @@ -111,7 +159,7 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, dest += dest_rowstride; } - _mm_empty (); + GDK_PIXELOPS_MMX_EMPTY (); } } @@ -148,7 +196,9 @@ blend_565_mmx (__m64 zero, __m64 s; __m64 d; __m64 a; +#ifndef GDK_PIXELOPS_X86_3DNOW_IMPL __m64 ia; +#endif __m64 t; guint32 result; @@ -162,6 +212,9 @@ blend_565_mmx (__m64 zero, a = _mm_unpacklo_pi16 (a, a); a = _mm_unpacklo_pi32 (a, a); +#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL + t = blend_3dnow (s, d, a); +#else ia = _mm_sub_pi16 (ff, a); t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), @@ -170,6 +223,7 @@ blend_565_mmx (__m64 zero, t = _mm_add_pi16 (t, bias); t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); t = _mm_srli_pi16 (t, 8); +#endif t = _mm_packs_pu16 (t, t); __asm__ ("movd %1,%0" @@ -180,7 +234,7 @@ blend_565_mmx (__m64 zero, } G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_565_mmx (guchar *src_buf, +GDK_PIXELOPS_COMPOSITE_565 (guchar *src_buf, gint src_rowstride, guchar *dest_buf, gint dest_rowstride, @@ -255,5 +309,5 @@ _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, dest += dest_rowstride; } - _mm_empty (); + GDK_PIXELOPS_MMX_EMPTY (); } diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 9bf910d03..5328b5c4f 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -35,6 +35,24 @@ _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, gint width, gint height); +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_3dnow (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_3dnow (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + G_GNUC_INTERNAL void _gdk_pixel_ops_init (void) { @@ -50,5 +68,14 @@ _gdk_pixel_ops_init (void) _gdk_pixel_ops_composite_0888_mmx; _gdk_pixel_ops.composite_565 = _gdk_pixel_ops_composite_565_mmx; + + if (__get_cpuid (0x80000001U, &eax, &ebx, &ecx, &edx) && + (edx & bit_3DNOW) != 0) + { + _gdk_pixel_ops.composite_0888 = + _gdk_pixel_ops_composite_0888_3dnow; + _gdk_pixel_ops.composite_565 = + _gdk_pixel_ops_composite_565_3dnow; + } } } From 9561cbac1d1b019375462bf7e12622532fc60339 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 12 Aug 2026 20:02:05 +0200 Subject: [PATCH 43/68] gdk: add i386 SSE pixel compositing --- gdk/Makefile.am | 9 +++++++++ gdk/gdkpixelops-x86-mmx.c | 34 ++++++++++++++++++++++++++++++++++ gdk/gdkpixelops-x86.c | 26 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 20da9ee26..889670e55 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -135,6 +135,15 @@ libgdk_pixelops_x86_mmx_la_CFLAGS = \ $(AM_CFLAGS) \ -mmmx -mno-sse -mno-sse2 -mno-3dnow +noinst_LTLIBRARIES += libgdk-pixelops-x86-sse.la +gdk_pixelops_arch_libs += libgdk-pixelops-x86-sse.la + +libgdk_pixelops_x86_sse_la_SOURCES = gdkpixelops-x86-mmx.c +libgdk_pixelops_x86_sse_la_CFLAGS = \ + $(AM_CFLAGS) \ + -mmmx -mno-sse -mno-sse2 -mno-3dnow \ + -DGDK_PIXELOPS_X86_SSE_IMPL + noinst_LTLIBRARIES += libgdk-pixelops-x86-3dnow.la gdk_pixelops_arch_libs += libgdk-pixelops-x86-3dnow.la diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c index 9058f07b5..bf7cdd8b2 100644 --- a/gdk/gdkpixelops-x86-mmx.c +++ b/gdk/gdkpixelops-x86-mmx.c @@ -13,6 +13,10 @@ #define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_3dnow #define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_3dnow #define GDK_PIXELOPS_MMX_EMPTY() _m_femms () +#elif defined(GDK_PIXELOPS_X86_SSE_IMPL) +#define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_sse +#define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_sse +#define GDK_PIXELOPS_MMX_EMPTY() _mm_empty () #else #define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_mmx #define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_mmx @@ -129,8 +133,13 @@ GDK_PIXELOPS_COMPOSITE_0888 (guchar *src_buf, d = _mm_unpacklo_pi8 (d, zero); a = _mm_srli_si64 (s, 48); + #ifdef GDK_PIXELOPS_X86_SSE_IMPL + __asm__ ("pshufw $0x00,%0,%0" + : "+y" (a)); + #else a = _mm_unpacklo_pi16 (a, a); a = _mm_unpacklo_pi32 (a, a); + #endif a = _mm_and_si64 (a, rgb_mask); #ifdef GDK_PIXELOPS_X86_3DNOW_IMPL @@ -142,8 +151,18 @@ GDK_PIXELOPS_COMPOSITE_0888 (guchar *src_buf, _mm_mullo_pi16 (d, ia)); t = _mm_add_pi16 (t, bias); + #ifdef GDK_PIXELOPS_X86_SSE_IMPL + { + __m64 scale = _mm_set1_pi16 (0x0101); + + __asm__ ("pmulhuw %1,%0" + : "+y" (t) + : "y" (scale)); + } + #else t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); t = _mm_srli_pi16 (t, 8); + #endif #endif packed = _mm_packs_pu16 (t, t); @@ -209,8 +228,13 @@ blend_565_mmx (__m64 zero, d = _mm_unpacklo_pi8 (d, zero); a = _mm_srli_si64 (s, 48); + #ifdef GDK_PIXELOPS_X86_SSE_IMPL + __asm__ ("pshufw $0x00,%0,%0" + : "+y" (a)); + #else a = _mm_unpacklo_pi16 (a, a); a = _mm_unpacklo_pi32 (a, a); + #endif #ifdef GDK_PIXELOPS_X86_3DNOW_IMPL t = blend_3dnow (s, d, a); @@ -221,8 +245,18 @@ blend_565_mmx (__m64 zero, _mm_mullo_pi16 (d, ia)); t = _mm_add_pi16 (t, bias); + #ifdef GDK_PIXELOPS_X86_SSE_IMPL + { + __m64 scale = _mm_set1_pi16 (0x0101); + + __asm__ ("pmulhuw %1,%0" + : "+y" (t) + : "y" (scale)); + } + #else t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); t = _mm_srli_pi16 (t, 8); + #endif #endif t = _mm_packs_pu16 (t, t); diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 5328b5c4f..4f7b3359e 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -35,6 +35,24 @@ _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, gint width, gint height); +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_sse (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_sse (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + G_GNUC_INTERNAL void _gdk_pixel_ops_composite_0888_3dnow (guchar *src_buf, gint src_rowstride, @@ -69,6 +87,14 @@ _gdk_pixel_ops_init (void) _gdk_pixel_ops.composite_565 = _gdk_pixel_ops_composite_565_mmx; + if ((edx & bit_SSE) != 0) + { + _gdk_pixel_ops.composite_0888 = + _gdk_pixel_ops_composite_0888_sse; + _gdk_pixel_ops.composite_565 = + _gdk_pixel_ops_composite_565_sse; + } + if (__get_cpuid (0x80000001U, &eax, &ebx, &ecx, &edx) && (edx & bit_3DNOW) != 0) { From b52ddc3350299399b2e34e2418315f3c608a053a Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 12 Aug 2026 21:10:02 +0200 Subject: [PATCH 44/68] x11: cache pixmap bits per depth --- gdk/x11/gdkdisplay-x11.h | 1 + gdk/x11/gdkimage-x11.c | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/gdk/x11/gdkdisplay-x11.h b/gdk/x11/gdkdisplay-x11.h index 45f5834df..76b2e6d18 100644 --- a/gdk/x11/gdkdisplay-x11.h +++ b/gdk/x11/gdkdisplay-x11.h @@ -78,6 +78,7 @@ struct _GdkDisplayX11 gboolean use_xshm; gboolean have_shm_pixmaps; + guint8 bits_per_depth[33]; GdkTristate have_render; gboolean have_xfixes; gint xfixes_event_base; diff --git a/gdk/x11/gdkimage-x11.c b/gdk/x11/gdkimage-x11.c index 65616271e..2e01c204a 100644 --- a/gdk/x11/gdkimage-x11.c +++ b/gdk/x11/gdkimage-x11.c @@ -787,20 +787,34 @@ gdk_x11_image_get_ximage (GdkImage *image) gint _gdk_windowing_get_bits_for_depth (GdkDisplay *display, - gint depth) + gint depth) { + GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display); XPixmapFormatValues *formats; gint count, i; + gint result = -1; + + if ((guint) depth < G_N_ELEMENTS (display_x11->bits_per_depth) && + display_x11->bits_per_depth[depth] != 0) + return display_x11->bits_per_depth[depth]; formats = XListPixmapFormats (GDK_DISPLAY_XDISPLAY (display), &count); for (i = 0; i < count; i++) - if (formats[i].depth == depth) - { - gint result = formats[i].bits_per_pixel; - XFree (formats); - return result; - } + { + if ((guint) formats[i].depth < + G_N_ELEMENTS (display_x11->bits_per_depth)) + display_x11->bits_per_depth[formats[i].depth] = + formats[i].bits_per_pixel; + + if (formats[i].depth == depth) + result = formats[i].bits_per_pixel; + } + + XFree (formats); + + if (result != -1) + return result; g_assert_not_reached (); return -1; From 7152a3c62ecf53bb38c87cf725b5292b1f0fe9f0 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Thu, 13 Aug 2026 11:24:54 +0200 Subject: [PATCH 45/68] gdk: add i386 SSE2 pixel compositing --- gdk/Makefile.am | 9 + gdk/gdkpixelops-x86-sse2.c | 331 +++++++++++++++++++++++++++++++++++++ gdk/gdkpixelops-x86.c | 16 ++ 3 files changed, 356 insertions(+) create mode 100644 gdk/gdkpixelops-x86-sse2.c diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 889670e55..3c2acb7f3 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -144,6 +144,15 @@ libgdk_pixelops_x86_sse_la_CFLAGS = \ -mmmx -mno-sse -mno-sse2 -mno-3dnow \ -DGDK_PIXELOPS_X86_SSE_IMPL +noinst_LTLIBRARIES += libgdk-pixelops-x86-sse2.la +gdk_pixelops_arch_libs += libgdk-pixelops-x86-sse2.la + +libgdk_pixelops_x86_sse2_la_SOURCES = gdkpixelops-x86-sse2.c +libgdk_pixelops_x86_sse2_la_CFLAGS = \ + $(AM_CFLAGS) \ + -march=i686 -msse2 -mno-sse3 -mno-ssse3 \ + -mno-sse4 -mno-avx -mno-3dnow -mtune=pentium4 + noinst_LTLIBRARIES += libgdk-pixelops-x86-3dnow.la gdk_pixelops_arch_libs += libgdk-pixelops-x86-3dnow.la diff --git a/gdk/gdkpixelops-x86-sse2.c b/gdk/gdkpixelops-x86-sse2.c new file mode 100644 index 000000000..c73ed7fc2 --- /dev/null +++ b/gdk/gdkpixelops-x86-sse2.c @@ -0,0 +1,331 @@ +/* GDK - The GIMP Drawing Kit + * + * SSE2 0888 pixel compositing for 32-bit x86. + */ + +#include "config.h" + +#include "gdkpixelops.h" + +#ifndef __i386__ +#error "gdkpixelops-x86-sse2.c requires a 32-bit x86 target" +#endif + +/* + * Keep this declaration at default visibility. The definition remains + * internal in gdkpixelops-generic.c, allowing compact SSE2 code while + * the final shared object still binds the fallback internally. + */ +void +_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +static const unsigned short asm_rgb_mask[8] + __attribute__ ((aligned (16))) = +{ + 0xffff, 0xffff, 0xffff, 0x0000, + 0xffff, 0xffff, 0xffff, 0x0000 +}; + +static const unsigned short asm_c255[8] + __attribute__ ((aligned (16))) = +{ + 255, 255, 255, 255, + 255, 255, 255, 255 +}; + +static const unsigned short asm_c128[8] + __attribute__ ((aligned (16))) = +{ + 128, 128, 128, 128, + 128, 128, 128, 128 +}; + +static const unsigned short asm_c257[8] + __attribute__ ((aligned (16))) = +{ + 257, 257, 257, 257, + 257, 257, 257, 257 +}; + +static inline void +composite_four_loop_asm (guchar **pp, + guchar **qq, + gint *nn) +{ + guchar *p = *pp; + guchar *q = *qq; + gint n = *nn; + + __asm__ volatile + ( + "1:\n\t" + "cmpl $4, %[n]\n\t" + "jl 2f\n\t" + + "movdqu (%[p]), %%xmm0\n\t" + "movdqu (%[q]), %%xmm1\n\t" + + "movdqa %%xmm0, %%xmm2\n\t" + "pxor %%xmm7, %%xmm7\n\t" + + "punpcklbw %%xmm7, %%xmm0\n\t" + "punpckhbw %%xmm7, %%xmm2\n\t" + + "movdqa %%xmm1, %%xmm3\n\t" + + "punpcklbw %%xmm7, %%xmm1\n\t" + "punpckhbw %%xmm7, %%xmm3\n\t" + + "pshuflw $0xc6, %%xmm0, %%xmm0\n\t" + "pshuflw $0xc6, %%xmm2, %%xmm2\n\t" + + "pshufhw $0xc6, %%xmm0, %%xmm0\n\t" + "pshufhw $0xc6, %%xmm2, %%xmm2\n\t" + + "movdqa %%xmm0, %%xmm4\n\t" + "movdqa %%xmm2, %%xmm5\n\t" + + "pshuflw $0xff, %%xmm4, %%xmm4\n\t" + "pshuflw $0xff, %%xmm5, %%xmm5\n\t" + + "pshufhw $0xff, %%xmm4, %%xmm4\n\t" + "pshufhw $0xff, %%xmm5, %%xmm5\n\t" + + "pand %[mask], %%xmm4\n\t" + "pand %[mask], %%xmm5\n\t" + + "movdqa %%xmm4, %%xmm6\n\t" + "movdqa %%xmm5, %%xmm7\n\t" + + "pxor %[c255], %%xmm6\n\t" + "pxor %[c255], %%xmm7\n\t" + + "pmullw %%xmm4, %%xmm0\n\t" + "pmullw %%xmm5, %%xmm2\n\t" + + "addl $16, %[p]\n\t" + "addl $16, %[q]\n\t" + "subl $4, %[n]\n\t" + + "pmullw %%xmm6, %%xmm1\n\t" + "pmullw %%xmm7, %%xmm3\n\t" + + "paddw %%xmm1, %%xmm0\n\t" + "paddw %%xmm3, %%xmm2\n\t" + + "paddw %[c128], %%xmm0\n\t" + "paddw %[c128], %%xmm2\n\t" + + "pmulhuw %[c257], %%xmm0\n\t" + "pmulhuw %[c257], %%xmm2\n\t" + + "packuswb %%xmm2, %%xmm0\n\t" + + "movdqu %%xmm0, -16(%[q])\n\t" + + "jmp 1b\n\t" + + "2:\n\t" + + : [p] "+r" (p), + [q] "+r" (q), + [n] "+r" (n) + + : [mask] "m" (asm_rgb_mask), + [c255] "m" (asm_c255), + [c128] "m" (asm_c128), + [c257] "m" (asm_c257) + + : "cc", + "memory", + "xmm0", + "xmm1", + "xmm2", + "xmm3", + "xmm4", + "xmm5", + "xmm6", + "xmm7" + ); + + *pp = p; + *qq = q; + *nn = n; +} + +static inline void +composite_two_asm (guchar **pp, + guchar **qq, + gint *nn) +{ + guchar *p = *pp; + guchar *q = *qq; + gint n = *nn; + + __asm__ volatile + ( + "cmpl $2, %[n]\n\t" + "jl 1f\n\t" + + "movq (%[p]), %%xmm0\n\t" + "pxor %%xmm2, %%xmm2\n\t" + "punpcklbw %%xmm2, %%xmm0\n\t" + + "movq (%[q]), %%xmm1\n\t" + "punpcklbw %%xmm2, %%xmm1\n\t" + + "pshuflw $0xc6, %%xmm0, %%xmm0\n\t" + "pshufhw $0xc6, %%xmm0, %%xmm0\n\t" + + "movdqa %%xmm0, %%xmm2\n\t" + "pshuflw $0xff, %%xmm2, %%xmm2\n\t" + "pshufhw $0xff, %%xmm2, %%xmm2\n\t" + "pand %[mask], %%xmm2\n\t" + + "movdqa %%xmm2, %%xmm3\n\t" + "pxor %[c255], %%xmm3\n\t" + + "pmullw %%xmm2, %%xmm0\n\t" + "pmullw %%xmm3, %%xmm1\n\t" + + "addl $8, %[p]\n\t" + "addl $8, %[q]\n\t" + "subl $2, %[n]\n\t" + + "paddw %%xmm1, %%xmm0\n\t" + + "paddw %[c128], %%xmm0\n\t" + "pmulhuw %[c257], %%xmm0\n\t" + + "pxor %%xmm1, %%xmm1\n\t" + "packuswb %%xmm1, %%xmm0\n\t" + + "movq %%xmm0, -8(%[q])\n\t" + + "1:\n\t" + + : [p] "+r" (p), + [q] "+r" (q), + [n] "+r" (n) + + : [mask] "m" (asm_rgb_mask), + [c255] "m" (asm_c255), + [c128] "m" (asm_c128), + [c257] "m" (asm_c257) + + : "cc", + "memory", + "xmm0", + "xmm1", + "xmm2", + "xmm3" + ); + + *pp = p; + *qq = q; + *nn = n; +} + +static inline void +composite_one_asm (guchar *p, + guchar *q) +{ + + __asm__ volatile + ( + "movd (%[p]), %%xmm0\n\t" + "pxor %%xmm2, %%xmm2\n\t" + "punpcklbw %%xmm2, %%xmm0\n\t" + + "movd (%[q]), %%xmm1\n\t" + "punpcklbw %%xmm2, %%xmm1\n\t" + + "pshuflw $0xc6, %%xmm0, %%xmm0\n\t" + + "movdqa %%xmm0, %%xmm2\n\t" + "pshuflw $0xff, %%xmm2, %%xmm2\n\t" + "pand %[mask], %%xmm2\n\t" + + "movdqa %%xmm2, %%xmm3\n\t" + "pxor %[c255], %%xmm3\n\t" + + "pmullw %%xmm2, %%xmm0\n\t" + "pmullw %%xmm3, %%xmm1\n\t" + + "paddw %%xmm1, %%xmm0\n\t" + + "paddw %[c128], %%xmm0\n\t" + "pmulhuw %[c257], %%xmm0\n\t" + + "pxor %%xmm1, %%xmm1\n\t" + "packuswb %%xmm1, %%xmm0\n\t" + + "movd %%xmm0, (%[q])\n\t" + + : + : [p] "r" (p), + [q] "r" (q), + [mask] "m" (asm_rgb_mask), + [c255] "m" (asm_c255), + [c128] "m" (asm_c128), + [c257] "m" (asm_c257) + + : "memory", + "xmm0", + "xmm1", + "xmm2", + "xmm3" + ); +} + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_sse2 (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + if (width <= 0 || height <= 0) + return; + + if (dest_byte_order != GDK_LSB_FIRST) + { + _gdk_pixel_ops_composite_0888_generic + (src_buf, + src_rowstride, + dest_buf, + dest_rowstride, + dest_byte_order, + width, + height); + return; + } + + while (height--) + { + gint n = width; + guchar *p = src; + guchar *q = dest; + + composite_four_loop_asm (&p, &q, &n); + + composite_two_asm (&p, &q, &n); + + if (__builtin_expect (n != 0, 0)) + composite_one_asm (p, q); + + src += src_rowstride; + dest += dest_rowstride; + } +} diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 4f7b3359e..77c44ed7e 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -71,6 +71,15 @@ _gdk_pixel_ops_composite_565_3dnow (guchar *src_buf, gint width, gint height); +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_sse2 (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + G_GNUC_INTERNAL void _gdk_pixel_ops_init (void) { @@ -78,10 +87,13 @@ _gdk_pixel_ops_init (void) unsigned int ebx; unsigned int ecx; unsigned int edx; + unsigned int standard_edx; if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) && (edx & bit_MMX) != 0) { + standard_edx = edx; + _gdk_pixel_ops.composite_0888 = _gdk_pixel_ops_composite_0888_mmx; _gdk_pixel_ops.composite_565 = @@ -103,5 +115,9 @@ _gdk_pixel_ops_init (void) _gdk_pixel_ops.composite_565 = _gdk_pixel_ops_composite_565_3dnow; } + + if ((standard_edx & bit_SSE2) != 0) + _gdk_pixel_ops.composite_0888 = + _gdk_pixel_ops_composite_0888_sse2; } } From 3f8b4e6ed427286daf50a643a37800ef48934eab Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Thu, 13 Aug 2026 16:03:56 +0200 Subject: [PATCH 46/68] gdk: add direct 30-bit alpha compositing Add an exact integer RGB101010 compositor for native 32-bpp depth-30 TrueColor X11 visuals. Bypass XRender only when the drawable exactly matches the supported RGB101010 layout. Keep the existing XRender and fallback paths for other visuals and retain the existing maximum-dither path. Compile the new compositor and selector only for the X11 backend so Win32, Quartz and DirectFB builds carry no unused RGB101010 compositing code or runtime selection branch. --- gdk/gdkdraw.c | 124 ++++++++++++++++++++++++++++++++++++++ gdk/x11/gdkdrawable-x11.c | 22 +++++++ 2 files changed, 146 insertions(+) diff --git a/gdk/gdkdraw.c b/gdk/gdkdraw.c index 3d159dd37..7efe560b4 100644 --- a/gdk/gdkdraw.c +++ b/gdk/gdkdraw.c @@ -25,6 +25,7 @@ */ #include "config.h" +#include #include #include #include @@ -1467,6 +1468,114 @@ gdk_drawable_real_draw_drawable (GdkDrawable *drawable, src); } +#ifdef GDK_WINDOWING_X11 +static inline guint +composite_101010_channel (guint src, + guint alpha, + guint dest) +{ + guint sa; + guint da; + guint numerator; + guint q; + guint threshold; + + sa = src * alpha; + da = dest * (255U - alpha); + + numerator = + (sa << 10) - sa + + (da << 8) - da; + + q = numerator / 64961U; + + threshold = + q * 64961U + + ((q * 511U + 1023U) >> 10); + + q -= numerator < threshold; + + /* + * numerator <= 66520575, therefore q is at most 1024. + * Map only that endpoint to 1023 without a branch. + */ + q -= q >> 10; + + return q; +} + +static void +composite_101010_exact (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src; + guchar *dest; + + (void) dest_byte_order; + + src = src_buf; + dest = dest_buf; + + while (height--) + { + const guchar *p; + guchar *q; + gint x; + + p = src; + q = dest; + + for (x = 0; x < width; x++) + { + guint32 pixel; + guint alpha; + guint r; + guint g; + guint b; + + memcpy (&pixel, q, sizeof pixel); + + alpha = p[3]; + + r = composite_101010_channel ( + p[0], + alpha, + (pixel >> 20) & 0x3ffU); + + g = composite_101010_channel ( + p[1], + alpha, + (pixel >> 10) & 0x3ffU); + + b = composite_101010_channel ( + p[2], + alpha, + pixel & 0x3ffU); + + pixel = + (pixel & 0xc0000000U) | + (r << 20) | + (g << 10) | + b; + + memcpy (q, &pixel, sizeof pixel); + + p += 4; + q += 4; + } + + src += src_rowstride; + dest += dest_rowstride; + } +} + +#endif /* GDK_WINDOWING_X11 */ + static void gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable, GdkGC *gc, @@ -1601,6 +1710,21 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable, composite_func = _gdk_pixel_ops.composite_565; goto composite_selected; } +#ifdef GDK_WINDOWING_X11 + else if (visual->type == GDK_VISUAL_TRUE_COLOR && + visual->byte_order == + (G_BYTE_ORDER == G_BIG_ENDIAN ? + GDK_MSB_FIRST : GDK_LSB_FIRST) && + visual->depth == 30 && + bits_per_pixel == 32 && + visual->red_mask == 0x3ff00000 && + visual->green_mask == 0x000ffc00 && + visual->blue_mask == 0x000003ff) + { + composite_func = composite_101010_exact; + goto composite_selected; + } +#endif /* GDK_WINDOWING_X11 */ else if (visual->depth == 24 && bits_per_pixel == 32 && visual->red_mask == 0xff0000 && visual->green_mask == 0x00ff00 && diff --git a/gdk/x11/gdkdrawable-x11.c b/gdk/x11/gdkdrawable-x11.c index d27a03ebb..18758966b 100644 --- a/gdk/x11/gdkdrawable-x11.c +++ b/gdk/x11/gdkdrawable-x11.c @@ -1462,6 +1462,26 @@ draw_with_pixmaps (GdkDrawable *drawable, } #endif +static gboolean +gdk_x11_drawable_can_composite_101010 (GdkDrawable *drawable) +{ + GdkVisual *visual; + + visual = gdk_drawable_get_visual (drawable); + + return visual != NULL && + visual->type == GDK_VISUAL_TRUE_COLOR && + visual->depth == 30 && + _gdk_windowing_get_bits_for_depth ( + gdk_drawable_get_display (drawable), 30) == 32 && + visual->byte_order == + (G_BYTE_ORDER == G_BIG_ENDIAN ? + GDK_MSB_FIRST : GDK_LSB_FIRST) && + visual->red_mask == 0x3ff00000 && + visual->green_mask == 0x000ffc00 && + visual->blue_mask == 0x000003ff; +} + static void gdk_x11_draw_pixbuf (GdkDrawable *drawable, GdkGC *gc, @@ -1488,6 +1508,8 @@ gdk_x11_draw_pixbuf (GdkDrawable *drawable, if (format_type == GDK_X11_FORMAT_NONE || !gdk_pixbuf_get_has_alpha (pixbuf) || + (gdk_drawable_get_depth (drawable) == 30 && + gdk_x11_drawable_can_composite_101010 (drawable)) || gdk_drawable_get_depth (drawable) == 1 || (dither == GDK_RGB_DITHER_MAX && gdk_drawable_get_depth (drawable) != 24) || gdk_x11_drawable_get_picture (drawable) == None) From 7dd6c04cb8e487663c49de65e109415f646c847b Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Fri, 14 Aug 2026 08:57:01 +0200 Subject: [PATCH 47/68] gdk: optimize native RGB555 conversion Use an aligned four-pixel integer converter for native little-endian RGB555 images. Keep the scalar implementation for narrow, unaligned and big-endian paths. --- gdk/gdkrgb.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 125999da4..e4266849b 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2011,6 +2011,74 @@ gdk_rgb_convert_555 (GdkRgbInfo *image_info, GdkImage *image, bptr = buf; bpl = image->bpl; obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 2; + +#if G_BYTE_ORDER == G_LITTLE_ENDIAN + /* GTK2_NG_WIDE_555 */ + if (width >= 32 && + ((((guintptr) obuf | + (guintptr) bptr | + (guintptr) rowstride | + (guintptr) bpl) & 3) == 0)) + { + for (y = 0; y < height; y++) + { + guchar *obptr; + + bp2 = bptr; + obptr = obuf; + + for (x = 0; x < width - 3; x += 4) + { + guint32 w0; + guint32 w1; + guint32 w2; + + w0 = ((const guint32 *) bp2)[0]; + w1 = ((const guint32 *) bp2)[1]; + w2 = ((const guint32 *) bp2)[2]; + + ((guint32 *) obptr)[0] = + ((w0 & 0x000000f8U) << 7) | + ((w0 & 0x0000f800U) >> 6) | + ((w0 & 0x00f80000U) >> 19) | + ((w0 & 0xf8000000U) >> 1) | + ((w1 & 0x000000f8U) << 18) | + ((w1 & 0x0000f800U) << 5); + + ((guint32 *) obptr)[1] = + ((w1 & 0x00f80000U) >> 9) | + ((w1 & 0xf8000000U) >> 22) | + ((w2 & 0x000000f8U) >> 3) | + ((w2 & 0x0000f800U) << 15) | + ((w2 & 0x00f80000U) << 2) | + ((w2 & 0xf8000000U) >> 11); + + bp2 += 12; + obptr += 8; + } + + for (; x < width; x++) + { + r = *bp2++; + g = *bp2++; + b = *bp2++; + + ((guint16 *) obptr)[0] = + ((r & 0xf8) << 7) | + ((g & 0xf8) << 2) | + (b >> 3); + + obptr += 2; + } + + bptr += rowstride; + obuf += bpl; + } + + return; + } +#endif + for (y = 0; y < height; y++) { bp2 = bptr; From 5715c9a76c5451aedc49ad0a196c7704abe4f1e9 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Fri, 14 Aug 2026 12:08:02 +0200 Subject: [PATCH 48/68] gdk: optimize x86-64 RGB888 conversion --- gdk/gdkrgb.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index e4266849b..8ea11a46f 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2291,10 +2291,21 @@ gdk_rgb_convert_0888 (GdkRgbInfo *image_info, GdkImage *image, w = width; while (w--) { +#if G_BYTE_ORDER == G_LITTLE_ENDIAN && GLIB_SIZEOF_VOID_P == 8 && \ + (defined(__x86_64__) || defined(__amd64__)) + guint32 pixel; + + pixel = 0xff000000U | + ((guint32) bp2[0] << 16) | + ((guint32) bp2[1] << 8) | + (guint32) bp2[2]; + memcpy (p, &pixel, sizeof (pixel)); +#else p[0] = bp2[2]; p[1] = bp2[1]; p[2] = bp2[0]; p[3] = 0xff; +#endif bp2 += 3; p += 4; } From 5cd79214f3eba2362f9e903b62866c4a04ff0f30 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sat, 15 Aug 2026 12:31:04 +0200 Subject: [PATCH 49/68] gdk: fix packed grayscale dither alignment --- gdk/gdkrgb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 8ea11a46f..80cefe11c 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2996,7 +2996,7 @@ gdk_rgb_convert_gray4_d_pack (GdkRgbInfo *image_info, GdkImage *image, g = *bp2++; b = *bp2++; gray = (g + ((b + r) >> 1)) >> 1; - gray += (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) >> prec; + gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec; pix1 = (gray - (gray >> prec)) >> right; *obptr = (*obptr & 0xF0) | pix1; obptr++; @@ -3019,7 +3019,7 @@ gdk_rgb_convert_gray4_d_pack (GdkRgbInfo *image_info, GdkImage *image, g = *bp2++; b = *bp2++; gray = (g + ((b + r) >> 1)) >> 1; - gray += (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) >> prec; + gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec; pix1 = (gray - (gray >> prec)) >> right; x++; } From 54eed58f1467450ae21a9ade0cc9a1d2488d2cbe Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sat, 15 Aug 2026 14:35:23 +0200 Subject: [PATCH 50/68] gdk: support X11 LSBFirst 4-bpp images --- gdk/gdkrgb.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 167 insertions(+), 6 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 80cefe11c..3cb9f2522 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2826,6 +2826,74 @@ gdk_rgb_convert_4_pack (GdkRgbInfo *image_info, GdkImage *image, } } +#ifdef GDK_WINDOWING_X11 +#define GDK_RGB_4_PACK_LSB_PIXEL(pixel) \ + r = *bp2++; g = *bp2++; b = *bp2++; \ + dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 3; \ + ix = (((r + dith) & 0x100) >> 2) | \ + (((g + 258 - dith) & 0x100) >> 5) | \ + (((b + dith) & 0x100) >> 8); \ + pixel = colorcube_d[ix] + +static void +gdk_rgb_convert_4_pack_lsb (GdkRgbInfo *image_info, GdkImage *image, + gint x0, gint y0, gint width, gint height, + const guchar *buf, int rowstride, + gint x_align, gint y_align, + GdkRgbCmap *cmap) +{ + int x, y, ix; + gint bpl; + guchar *obuf, *obptr; + const guchar *bptr, *bp2; + gint r, g, b; + const guchar *dmp; + gint dith; + guchar *colorcube_d = image_info->colorcube_d; + guchar pix0, pix1; + + bptr = buf; + bpl = image->bpl; + obuf = ((guchar *)image->mem) + y0 * bpl + (x0 >> 1); + + for (y = 0; y < height; y++) + { + dmp = DM[(y_align + y) & (DM_HEIGHT - 1)]; + bp2 = bptr; + obptr = obuf; + x = 0; + + if (x0 & 1) + { + GDK_RGB_4_PACK_LSB_PIXEL (pix1); + *obptr = (*obptr & 0x0F) | (pix1 << 4); + obptr++; + x++; + } + + while (x + 1 < width) + { + GDK_RGB_4_PACK_LSB_PIXEL (pix0); + x++; + GDK_RGB_4_PACK_LSB_PIXEL (pix1); + x++; + *obptr++ = (pix1 << 4) | pix0; + } + + if (x < width) + { + GDK_RGB_4_PACK_LSB_PIXEL (pix0); + *obptr = (*obptr & 0xF0) | pix0; + } + + bptr += rowstride; + obuf += bpl; + } +} + +#undef GDK_RGB_4_PACK_LSB_PIXEL +#endif /* GDK_WINDOWING_X11 */ + /* This actually works for depths from 3 to 7 */ static void gdk_rgb_convert_gray4 (GdkRgbInfo *image_info, GdkImage *image, @@ -2874,7 +2942,7 @@ gdk_rgb_convert_gray4_pack (GdkRgbInfo *image_info, GdkImage *image, gint r, g, b; gint shift; guchar pix0, pix1; - /* todo: this is hardcoded to big-endian. Make endian-agile. */ + /* Pixel 0 is stored in the high nibble. */ bptr = buf; bpl = image->bpl; @@ -2920,6 +2988,60 @@ gdk_rgb_convert_gray4_pack (GdkRgbInfo *image_info, GdkImage *image, } } +#ifdef GDK_WINDOWING_X11 +#define GDK_RGB_DEFINE_GRAY4_PACK_LSB(name, DECLS, INIT, ROW, PIXEL) \ +static void \ +name (GdkRgbInfo *image_info, GdkImage *image, \ + gint x0, gint y0, gint width, gint height, \ + const guchar *buf, int rowstride, \ + gint x_align, gint y_align, GdkRgbCmap *cmap) \ +{ \ + int x, y; gint bpl; guchar *obuf, *obptr; \ + const guchar *bptr, *bp2; gint r, g, b; \ + DECLS guchar pix0, pix1; \ + bptr = buf; bpl = image->bpl; \ + obuf = ((guchar *)image->mem) + y0 * bpl + (x0 >> 1); INIT \ + for (y = 0; y < height; y++) \ + { \ + bp2 = bptr; obptr = obuf; ROW x = 0; \ + if (x0 & 1) \ + { \ + PIXEL (pix1); \ + *obptr = (*obptr & 0x0F) | (pix1 << 4); \ + obptr++; x++; \ + } \ + while (x < width) \ + { \ + PIXEL (pix0); x++; \ + if (x == width) \ + pix1 = (*obptr & 0xF0) >> 4; \ + else \ + { PIXEL (pix1); x++; } \ + *obptr++ = (pix1 << 4) | pix0; \ + } \ + bptr += rowstride; obuf += bpl; \ + } \ +} + +#define GDK_RGB_GRAY4_LSB_DECLS gint shift; +#define GDK_RGB_GRAY4_LSB_INIT shift = 9 - image_info->visual->depth; +#define GDK_RGB_GRAY4_LSB_ROW +#define GDK_RGB_GRAY4_LSB_PIXEL(pixel) \ + r = *bp2++; g = *bp2++; b = *bp2++; \ + pixel = (g + ((b + r) >> 1)) >> shift + +GDK_RGB_DEFINE_GRAY4_PACK_LSB (gdk_rgb_convert_gray4_pack_lsb, + GDK_RGB_GRAY4_LSB_DECLS, + GDK_RGB_GRAY4_LSB_INIT, + GDK_RGB_GRAY4_LSB_ROW, + GDK_RGB_GRAY4_LSB_PIXEL) + +#undef GDK_RGB_GRAY4_LSB_PIXEL +#undef GDK_RGB_GRAY4_LSB_ROW +#undef GDK_RGB_GRAY4_LSB_INIT +#undef GDK_RGB_GRAY4_LSB_DECLS +#endif /* GDK_WINDOWING_X11 */ + /* This actually works for depths from 3 to 7 */ static void gdk_rgb_convert_gray4_d (GdkRgbInfo *image_info, GdkImage *image, @@ -2976,7 +3098,7 @@ gdk_rgb_convert_gray4_d_pack (GdkRgbInfo *image_info, GdkImage *image, gint prec, right; gint gray; guchar pix0, pix1; - /* todo: this is hardcoded to big-endian. Make endian-agile. */ + /* Pixel 0 is stored in the high nibble. */ bptr = buf; bpl = image->bpl; @@ -3030,6 +3152,32 @@ gdk_rgb_convert_gray4_d_pack (GdkRgbInfo *image_info, GdkImage *image, } } +#ifdef GDK_WINDOWING_X11 +#define GDK_RGB_GRAY4_D_LSB_DECLS \ + const guchar *dmp; gint prec, right, gray; +#define GDK_RGB_GRAY4_D_LSB_INIT \ + prec = image_info->visual->depth; right = 8 - prec; +#define GDK_RGB_GRAY4_D_LSB_ROW \ + dmp = DM[(y_align + y) & (DM_HEIGHT - 1)]; +#define GDK_RGB_GRAY4_D_LSB_PIXEL(pixel) \ + r = *bp2++; g = *bp2++; b = *bp2++; \ + gray = (g + ((b + r) >> 1)) >> 1; \ + gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec; \ + pixel = (gray - (gray >> prec)) >> right + +GDK_RGB_DEFINE_GRAY4_PACK_LSB (gdk_rgb_convert_gray4_d_pack_lsb, + GDK_RGB_GRAY4_D_LSB_DECLS, + GDK_RGB_GRAY4_D_LSB_INIT, + GDK_RGB_GRAY4_D_LSB_ROW, + GDK_RGB_GRAY4_D_LSB_PIXEL) + +#undef GDK_RGB_GRAY4_D_LSB_PIXEL +#undef GDK_RGB_GRAY4_D_LSB_ROW +#undef GDK_RGB_GRAY4_D_LSB_INIT +#undef GDK_RGB_GRAY4_D_LSB_DECLS +#undef GDK_RGB_DEFINE_GRAY4_PACK_LSB +#endif /* GDK_WINDOWING_X11 */ + static void gdk_rgb_convert_1 (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -3513,15 +3661,28 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv = gdk_rgb_convert_4; } else if (bpp == 4 && depth <= 4 && depth >= 2 && - (vtype == GDK_VISUAL_STATIC_GRAY - || vtype == GDK_VISUAL_GRAYSCALE)) + (vtype == GDK_VISUAL_STATIC_GRAY + || vtype == GDK_VISUAL_GRAYSCALE)) { conv = gdk_rgb_convert_gray4_pack; conv_d = gdk_rgb_convert_gray4_d_pack; +#ifdef GDK_WINDOWING_X11 + if (byte_order == GDK_LSB_FIRST) + { + conv = gdk_rgb_convert_gray4_pack_lsb; + conv_d = gdk_rgb_convert_gray4_d_pack_lsb; + } +#endif } else if (bpp == 4 && depth <= 4 && depth >= 2 && - vtype == GDK_VISUAL_STATIC_COLOR) - conv = gdk_rgb_convert_4_pack; + vtype == GDK_VISUAL_STATIC_COLOR) + { + conv = gdk_rgb_convert_4_pack; +#ifdef GDK_WINDOWING_X11 + if (byte_order == GDK_LSB_FIRST) + conv = gdk_rgb_convert_4_pack_lsb; +#endif + } if (!conv) g_error ("Visual type=%d depth=%d, image bpp=%d, %s first\n" From 7665fbc8748e20a2ebbba1a08f05730020b119ef Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sun, 16 Aug 2026 10:52:39 +0200 Subject: [PATCH 51/68] gdk: support native low-depth Win32 pixmaps Use native DIB sections for 2-8-bit Win32 pixmaps so logical depth, storage layout, GdkImage access, palette handling, Cairo access and HDC drawing remain consistent. Share the manual DIB setup with the 15/16-bit path, preserve native or canonical RGB555/RGB565 channel masks, and use HDC-backed Cairo rendering when the DIB layout cannot be represented directly by a Cairo image surface. Initialize only the BITMAPINFO header and color data that are actually required. --- gdk/win32/gdkimage-win32.c | 38 ++---- gdk/win32/gdkpixmap-win32.c | 231 +++++++++++++++++++++++++++--------- 2 files changed, 185 insertions(+), 84 deletions(-) diff --git a/gdk/win32/gdkimage-win32.c b/gdk/win32/gdkimage-win32.c index 8afa53f35..193a7a37f 100644 --- a/gdk/win32/gdkimage-win32.c +++ b/gdk/win32/gdkimage-win32.c @@ -128,33 +128,11 @@ _gdk_win32_new_image (GdkVisual *visual, image->height = height; image->depth = depth; image->bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_display_get_default (), depth); - switch (depth) - { - case 1: - case 4: - case 5: - case 6: - case 7: - case 8: - image->bpp = 1; - break; - case 15: - case 16: - image->bpp = 2; - break; - case 24: - image->bpp = image->bits_per_pixel / 8; - break; - case 32: - image->bpp = 4; - break; - default: - g_warning ("_gdk_win32_new_image: depth=%d", image->depth); - g_assert_not_reached (); - } - if (depth == 1) + /* Keep logical depth separate from the native DIB storage size. */ + image->bpp = (image->bits_per_pixel + 7) / 8; + if (image->bits_per_pixel == 1) image->bpl = ((width - 1)/32 + 1)*4; - else if (depth == 4) + else if (image->bits_per_pixel == 4) image->bpl = ((width - 1)/8 + 1)*4; else image->bpl = ((width*image->bpp - 1)/4 + 1)*4; @@ -285,10 +263,10 @@ gdk_image_get_pixel (GdkImage *image, if (!(x >= 0 && x < image->width && y >= 0 && y < image->height)) return 0; - if (image->depth == 1) + if (image->bits_per_pixel == 1) return (((guchar *) image->mem)[y * image->bpl + (x >> 3)] & (1 << (7 - (x & 0x7)))) != 0; - if (image->depth == 4) + if (image->bits_per_pixel == 4) { pixelp = (guchar *) image->mem + y * image->bpl + (x >> 1); if (x&1) @@ -334,12 +312,12 @@ gdk_image_put_pixel (GdkImage *image, return; GdiFlush (); - if (image->depth == 1) + if (image->bits_per_pixel == 1) if (pixel & 1) ((guchar *) image->mem)[y * image->bpl + (x >> 3)] |= (1 << (7 - (x & 0x7))); else ((guchar *) image->mem)[y * image->bpl + (x >> 3)] &= ~(1 << (7 - (x & 0x7))); - else if (image->depth == 4) + else if (image->bits_per_pixel == 4) { pixelp = (guchar *) image->mem + y * image->bpl + (x >> 1); diff --git a/gdk/win32/gdkpixmap-win32.c b/gdk/win32/gdkpixmap-win32.c index 08f9e9abb..c483c856f 100644 --- a/gdk/win32/gdkpixmap-win32.c +++ b/gdk/win32/gdkpixmap-win32.c @@ -191,6 +191,12 @@ _gdk_pixmap_new (GdkDrawable *drawable, format = CAIRO_FORMAT_A1; break; + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: case 8: format = CAIRO_FORMAT_A8; break; @@ -228,7 +234,7 @@ _gdk_pixmap_new (GdkDrawable *drawable, gdk_drawable_set_colormap (pixmap, cmap); } - if (depth != 15 && depth != 16) + if (depth == 1 || depth == 24 || depth == 32) { dib_surface = cairo_win32_surface_create_with_dib (format, width, height); if (dib_surface == NULL || @@ -238,8 +244,10 @@ _gdk_pixmap_new (GdkDrawable *drawable, return NULL; } - /* We need to have cairo create the dibsection for us, because - creating a cairo surface from a hdc only works for rgb24 format */ + /* + * Let Cairo own storage for the formats that it represents + * directly on Win32. + */ hdc = cairo_win32_surface_get_dc (dib_surface); /* Get the bitmap from the cairo hdc */ @@ -261,71 +269,186 @@ _gdk_pixmap_new (GdkDrawable *drawable, } else { - /* 16 bpp not supported by win32 cairo surface */ struct { - BITMAPINFOHEADER bmiHeader; - union { - WORD bmiIndices[256]; - DWORD bmiMasks[3]; - RGBQUAD bmiColors[256]; - } u; + BITMAPINFOHEADER bmiHeader; + union { + WORD bmiIndices[256]; + DWORD bmiMasks[3]; + RGBQUAD bmiColors[256]; + } u; } bmi; - UINT iUsage; - HWND hwnd; + GdkColormapPrivateWin32 *cmapp; GdkVisual *visual; + HPALETTE holdpal = NULL; + UINT iUsage = DIB_RGB_COLORS; + HWND hwnd; + gint n_colors = 0; + gint i; - if (GDK_IS_WINDOW (drawable)) - hwnd = GDK_WINDOW_HWND (drawable); - else - hwnd = GetDesktopWindow (); - if ((hdc = GetDC (hwnd)) == NULL) - { - WIN32_GDI_FAILED ("GetDC"); - g_object_unref ((GObject *) pixmap); - return NULL; - } - + /* + * Low-depth pixmaps need their native palette layout while + * 15/16-bpp pixmaps need explicit channel masks. Both use the + * same manually allocated DIB/HDC lifecycle. + */ + memset (&bmi.bmiHeader, 0, sizeof (bmi.bmiHeader)); bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER); bmi.bmiHeader.biWidth = width; bmi.bmiHeader.biHeight = -height; bmi.bmiHeader.biPlanes = 1; - bmi.bmiHeader.biBitCount = 16; - bmi.bmiHeader.biCompression = BI_BITFIELDS; - bmi.bmiHeader.biSizeImage = 0; - bmi.bmiHeader.biXPelsPerMeter = - bmi.bmiHeader.biYPelsPerMeter = 0; - bmi.bmiHeader.biClrUsed = 0; - bmi.bmiHeader.biClrImportant = 0; - iUsage = DIB_RGB_COLORS; - visual = gdk_visual_get_system (); - bmi.u.bmiMasks[0] = visual->red_mask; - bmi.u.bmiMasks[1] = visual->green_mask; - bmi.u.bmiMasks[2] = visual->blue_mask; + if (depth <= 8) + { + bmi.bmiHeader.biBitCount = + _gdk_windowing_get_bits_for_depth ( + gdk_display_get_default (), depth); + bmi.bmiHeader.biCompression = BI_RGB; + + n_colors = 1 << depth; + bmi.bmiHeader.biClrUsed = n_colors; + } + else + { + bmi.bmiHeader.biBitCount = 16; + bmi.bmiHeader.biCompression = BI_BITFIELDS; + + visual = gdk_visual_get_system (); + + if (visual->depth == depth) + { + bmi.u.bmiMasks[0] = visual->red_mask; + bmi.u.bmiMasks[1] = visual->green_mask; + bmi.u.bmiMasks[2] = visual->blue_mask; + } + else + { + bmi.u.bmiMasks[0] = + depth == 15 ? 0x00007C00 : 0x0000F800; + bmi.u.bmiMasks[1] = + depth == 15 ? 0x000003E0 : 0x000007E0; + bmi.u.bmiMasks[2] = 0x0000001F; + } + } + + if (GDK_IS_WINDOW (drawable)) + hwnd = GDK_WINDOW_HWND (drawable); + else + hwnd = GetDesktopWindow (); + + if ((hdc = GetDC (hwnd)) == NULL) + { + WIN32_GDI_FAILED ("GetDC"); + g_object_unref ((GObject *) pixmap); + return NULL; + } + + if (depth <= 8 && drawable_impl->colormap) + { + cmapp = + GDK_WIN32_COLORMAP_DATA ( + drawable_impl->colormap); + + holdpal = + SelectPalette ( + hdc, + cmapp->hpal, + FALSE); + + if (holdpal == NULL) + WIN32_GDI_FAILED ("SelectPalette"); + else + { + if (RealizePalette (hdc) == GDI_ERROR) + WIN32_GDI_FAILED ("RealizePalette"); + + iUsage = DIB_PAL_COLORS; + + for (i = 0; i < n_colors; i++) + bmi.u.bmiIndices[i] = i; + } + } + + /* + * DIB_RGB_COLORS consumes RGBQUAD entries. Only initialize the + * entries actually used; DIB_PAL_COLORS already has its WORD + * indices filled above. + */ + if (depth <= 8 && iUsage == DIB_RGB_COLORS) + memset (bmi.u.bmiColors, 0, + n_colors * sizeof (bmi.u.bmiColors[0])); + + hbitmap = + CreateDIBSection ( + hdc, + (BITMAPINFO *) &bmi, + iUsage, + (PVOID *) &bits, + NULL, + 0); + + if (hbitmap == NULL) + { + WIN32_GDI_FAILED ("CreateDIBSection"); + + if (holdpal) + SelectPalette (hdc, holdpal, FALSE); + + GDI_CALL (ReleaseDC, (hwnd, hdc)); + g_object_unref ((GObject *) pixmap); + return NULL; + } + + if (holdpal) + SelectPalette (hdc, holdpal, FALSE); - if ((hbitmap = CreateDIBSection (hdc, (BITMAPINFO *) &bmi, - iUsage, (PVOID *) &bits, NULL, 0)) == NULL) - { - WIN32_GDI_FAILED ("CreateDIBSection"); - GDI_CALL (ReleaseDC, (hwnd, hdc)); - g_object_unref ((GObject *) pixmap); - return NULL; - } GDI_CALL (ReleaseDC, (hwnd, hdc)); - dib_surface = cairo_image_surface_create_for_data (bits, - format, width, height, - (width * 2 + 3) & ~3); - hdc = CreateCompatibleDC (NULL); - if (!hdc) - { - WIN32_GDI_FAILED ("CreateCompatibleDC"); - g_object_unref ((GObject *) pixmap); - return NULL; - } - SelectObject (hdc, hbitmap); + if (hdc == NULL) + { + WIN32_GDI_FAILED ("CreateCompatibleDC"); + GDI_CALL (DeleteObject, (hbitmap)); + g_object_unref ((GObject *) pixmap); + return NULL; + } + + if (SelectObject (hdc, hbitmap) == NULL) + { + WIN32_GDI_FAILED ("SelectObject"); + GDI_CALL (DeleteDC, (hdc)); + GDI_CALL (DeleteObject, (hbitmap)); + g_object_unref ((GObject *) pixmap); + return NULL; + } + + if (depth != 16 || + bmi.u.bmiMasks[0] != 0x0000F800 || + bmi.u.bmiMasks[1] != 0x000007E0 || + bmi.u.bmiMasks[2] != 0x0000001F) + dib_surface = + cairo_win32_surface_create (hdc); + else + dib_surface = + cairo_image_surface_create_for_data ( + bits, + format, + width, + height, + (width * 2 + 3) & ~3); + + if (dib_surface == NULL || + cairo_surface_status (dib_surface) != + CAIRO_STATUS_SUCCESS) + { + if (dib_surface) + cairo_surface_destroy (dib_surface); + + GDI_CALL (DeleteDC, (hdc)); + GDI_CALL (DeleteObject, (hbitmap)); + g_object_unref ((GObject *) pixmap); + return NULL; + } + pixmap_impl->is_allocated = TRUE; } From 7458a4ffe232f1974241de2a78b10f2285031f72 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 17 Aug 2026 10:31:34 +0200 Subject: [PATCH 52/68] gdk: fix Win32 HDC access for offscreen windows Normalize public windows and pixmaps to their drawable implementations before resolving Win32 HDC access. This makes public GDK_WINDOW_OFFSCREEN drawables resolve through their GdkOffscreenWindow backing drawable instead of treating the generic offscreen implementation as a Win32 drawable. The resulting helper removes the incompatible-pointer warning without increasing object text size. --- gdk/win32/gdkgc-win32.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/gdk/win32/gdkgc-win32.c b/gdk/win32/gdkgc-win32.c index 703e05a58..3981359bd 100644 --- a/gdk/win32/gdkgc-win32.c +++ b/gdk/win32/gdkgc-win32.c @@ -744,17 +744,21 @@ predraw (GdkGC *gc, static GdkDrawableImplWin32 * get_impl_drawable (GdkDrawable *drawable) { - if (GDK_IS_OFFSCREEN_WINDOW (drawable)) - return _gdk_offscreen_window_get_real_drawable (GDK_OFFSCREEN_WINDOW (drawable)); - if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable)) - return GDK_DRAWABLE_IMPL_WIN32(drawable); - else if (GDK_IS_WINDOW (drawable)) - return GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl); - else if (GDK_IS_PIXMAP (drawable)) - return GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl); - else - g_assert_not_reached (); + if (GDK_IS_WINDOW (drawable)) + drawable = GDK_DRAWABLE ((GDK_WINDOW_OBJECT (drawable))->impl); + if (GDK_IS_OFFSCREEN_WINDOW (drawable)) + drawable = + _gdk_offscreen_window_get_real_drawable + (GDK_OFFSCREEN_WINDOW (drawable)); + + if (GDK_IS_PIXMAP (drawable)) + drawable = GDK_DRAWABLE ((GDK_PIXMAP_OBJECT (drawable))->impl); + + if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable)) + return GDK_DRAWABLE_IMPL_WIN32 (drawable); + + g_assert_not_reached (); return NULL; } From b95fcc5221ba1f0c51ec6df6ddf76ab84141ab7a Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 17 Aug 2026 10:43:15 +0200 Subject: [PATCH 53/68] gdk: fix Win32 alternating dash pens Use PS_ALTERNATE only for cosmetic one-pixel alternating dash patterns. Correct the pen-type test so it recognizes cosmetic pens, and inspect the second dash length for two-element patterns instead of checking the first entry twice. Other dash patterns continue to use PS_USERSTYLE. This also removes the inherited boolean-precedence compiler warnings. --- gdk/win32/gdkgc-win32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gdk/win32/gdkgc-win32.c b/gdk/win32/gdkgc-win32.c index 3981359bd..93d7da14e 100644 --- a/gdk/win32/gdkgc-win32.c +++ b/gdk/win32/gdkgc-win32.c @@ -173,10 +173,10 @@ fixup_pen (GdkGCWin32 *win32_gc) win32_gc->pen_num_dashes = 1; } - if (!(win32_gc->pen_style & PS_TYPE_MASK) == PS_GEOMETRIC && + if (!(win32_gc->pen_style & PS_TYPE_MASK) && win32_gc->pen_dashes[0] == 1 && (win32_gc->pen_num_dashes == 1 || - (win32_gc->pen_num_dashes == 2 && win32_gc->pen_dashes[0] == 1))) + (win32_gc->pen_num_dashes == 2 && win32_gc->pen_dashes[1] == 1))) win32_gc->pen_style |= PS_ALTERNATE; else win32_gc->pen_style |= PS_USERSTYLE; From 895937f0438b0866df883faf51e4f5b6e7b175e8 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 18 Aug 2026 13:21:14 +0200 Subject: [PATCH 54/68] gdk: use SSE2 pixel compositing on x86-64 --- configure.ac | 6 ++++++ gdk/Makefile.am | 4 ++++ gdk/gdkpixelops-x86-sse2.c | 20 +++++++++++++------- gdk/gdkpixelops.c | 15 +++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 58b30a8b2..2a116bfd6 100644 --- a/configure.ac +++ b/configure.ac @@ -125,10 +125,16 @@ case "$host_cpu" in AC_DEFINE([HAVE_GDK_PIXELOPS_INIT], [1], [Define if GDK pixel operations require runtime dispatch initialization]) ;; + x86_64) + AC_DEFINE([GDK_PIXELOPS_X86_64], [1], + [Define if x86-64 SSE2 pixel operations are available]) + ;; esac AM_CONDITIONAL(GDK_PIXELOPS_I386, test "$gdk_pixelops_i386" = yes) +AM_CONDITIONAL(GDK_PIXELOPS_X86_64, + test "$host_cpu" = x86_64) MATH_LIB=-lm diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 3c2acb7f3..216437b65 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -163,6 +163,10 @@ libgdk_pixelops_x86_3dnow_la_CFLAGS = \ -DGDK_PIXELOPS_X86_3DNOW_IMPL endif +if GDK_PIXELOPS_X86_64 +gdk_pixelops_arch_sources += gdkpixelops-x86-sse2.c +endif + gdk_pixelops_private_sources = \ gdkpixelops-generic.c \ $(gdk_pixelops_arch_sources) diff --git a/gdk/gdkpixelops-x86-sse2.c b/gdk/gdkpixelops-x86-sse2.c index c73ed7fc2..f502a7e44 100644 --- a/gdk/gdkpixelops-x86-sse2.c +++ b/gdk/gdkpixelops-x86-sse2.c @@ -1,14 +1,20 @@ /* GDK - The GIMP Drawing Kit * - * SSE2 0888 pixel compositing for 32-bit x86. + * SSE2 0888 pixel compositing for x86. */ #include "config.h" #include "gdkpixelops.h" -#ifndef __i386__ -#error "gdkpixelops-x86-sse2.c requires a 32-bit x86 target" +#if !defined(__i386__) && !defined(__x86_64__) +#error "gdkpixelops-x86-sse2.c requires an x86 target" +#endif + +#if defined(__x86_64__) && !defined(__ILP32__) +#define GDK_PIXELOPS_PTR_ADD "addq" +#else +#define GDK_PIXELOPS_PTR_ADD "addl" #endif /* @@ -109,8 +115,8 @@ composite_four_loop_asm (guchar **pp, "pmullw %%xmm4, %%xmm0\n\t" "pmullw %%xmm5, %%xmm2\n\t" - "addl $16, %[p]\n\t" - "addl $16, %[q]\n\t" + GDK_PIXELOPS_PTR_ADD " $16, %[p]\n\t" + GDK_PIXELOPS_PTR_ADD " $16, %[q]\n\t" "subl $4, %[n]\n\t" "pmullw %%xmm6, %%xmm1\n\t" @@ -194,8 +200,8 @@ composite_two_asm (guchar **pp, "pmullw %%xmm2, %%xmm0\n\t" "pmullw %%xmm3, %%xmm1\n\t" - "addl $8, %[p]\n\t" - "addl $8, %[q]\n\t" + GDK_PIXELOPS_PTR_ADD " $8, %[p]\n\t" + GDK_PIXELOPS_PTR_ADD " $8, %[q]\n\t" "subl $2, %[n]\n\t" "paddw %%xmm1, %%xmm0\n\t" diff --git a/gdk/gdkpixelops.c b/gdk/gdkpixelops.c index 6292fcac0..ff405a35f 100644 --- a/gdk/gdkpixelops.c +++ b/gdk/gdkpixelops.c @@ -34,7 +34,22 @@ _gdk_pixel_ops_composite_565_generic (guchar *src_buf, gint width, gint height); +#ifdef GDK_PIXELOPS_X86_64 +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_sse2 (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); +#endif + G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops = { +#ifdef GDK_PIXELOPS_X86_64 + _gdk_pixel_ops_composite_0888_sse2, +#else _gdk_pixel_ops_composite_0888_generic, +#endif _gdk_pixel_ops_composite_565_generic }; From bc16c71aa0709fc768487e2e10bc3427d9ba17b7 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 18 Aug 2026 13:37:22 +0200 Subject: [PATCH 55/68] quartz: support device axis remapping --- gdk/quartz/gdkinput.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/gdk/quartz/gdkinput.c b/gdk/quartz/gdkinput.c index 100574e9e..3ce3f2470 100644 --- a/gdk/quartz/gdkinput.c +++ b/gdk/quartz/gdkinput.c @@ -235,8 +235,6 @@ gdk_device_set_axis_use (GdkDevice *device, guint index, GdkAxisUse use) { -#if 0 - /* Remapping axes is unsupported for now */ g_return_if_fail (device != NULL); g_return_if_fail (index < device->num_axes); @@ -259,7 +257,6 @@ gdk_device_set_axis_use (GdkDevice *device, device->axes[index].max = 1; break; } -#endif } /** From 9dc45513d84afe29a090dcde9d27023fa1b640e0 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 18 Aug 2026 15:04:53 +0200 Subject: [PATCH 56/68] gdk: optimize x86-64 Gray8 conversion Use the equivalent sum-and-shift grayscale expression on x86-64, where it generates shorter and faster code. Keep the existing expression on other targets because the new form regresses i386/Bonnell. --- gdk/gdkrgb.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 3cb9f2522..2d3da3e49 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -1434,7 +1434,12 @@ gdk_rgb_convert_gray8 (GdkRgbInfo *image_info, GdkImage *image, r = *bp2++; g = *bp2++; b = *bp2++; +#if GLIB_SIZEOF_VOID_P == 8 && \ + (defined(__x86_64__) || defined(__amd64__)) + obptr[0] = (r + 2 * g + b) >> 2; +#else obptr[0] = (g + ((b + r) >> 1)) >> 1; +#endif obptr++; } bptr += rowstride; From 8621bd229d2eb084ae9968c863b36a083c8a6ff0 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 18 Aug 2026 17:06:00 +0200 Subject: [PATCH 57/68] gdk: speed up indexed dithering --- gdk/gdkrgb.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 2d3da3e49..278ebcbf6 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -1357,6 +1357,10 @@ gdk_rgb_convert_8_d (GdkRgbInfo *image_info, GdkImage *image, obuf = ((guchar *)image->mem) + y0 * bpl + x0; for (y = 0; y < height; y++) { +#if GLIB_SIZEOF_VOID_P == 8 && \ + (defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__)) + unsigned int dx = (unsigned int) x_align & (DM_WIDTH - 1); +#endif dmp = DM[(y_align + y) & (DM_HEIGHT - 1)]; bp2 = bptr; obptr = obuf; @@ -1365,7 +1369,13 @@ gdk_rgb_convert_8_d (GdkRgbInfo *image_info, GdkImage *image, r = *bp2++; g = *bp2++; b = *bp2++; +#if GLIB_SIZEOF_VOID_P == 8 && \ + (defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__)) + dith = (dmp[dx] << 2) | 7; + dx = (dx + 1) & (DM_WIDTH - 1); +#else dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 7; +#endif r = ((r * rs) + dith) >> 8; g = ((g * gs) + (262 - dith)) >> 8; b = ((b * bs) + dith) >> 8; From 43d974d20f8712717a16fa27ceb7d43c4634f220 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 18 Aug 2026 20:57:39 +0200 Subject: [PATCH 58/68] gdk: speed up RGB32 drawing --- gdk/gdkrgb.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 278ebcbf6..00f994670 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -3247,6 +3247,83 @@ gdk_rgb_ensure_stage (GdkRgbInfo *image_info) return image_info->stage_buf; } +static void +gdk_rgb_convert_32_0888 (GdkRgbInfo *image_info, GdkImage *image, + gint x0, gint y0, gint width, gint height, + const guchar *buf, int rowstride, + gint x_align, gint y_align, GdkRgbCmap *cmap) +{ + int y, w; + guchar *obuf, *p; + gint bpl; + const guchar *bptr, *bp2; + + bptr = buf; + bpl = image->bpl; + obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4; + for (y = 0; y < height; y++) + { + bp2 = bptr; + p = obuf; + w = width; + while (w--) + { +#if G_BYTE_ORDER == G_LITTLE_ENDIAN && GLIB_SIZEOF_VOID_P == 8 && \ + (defined(__x86_64__) || defined(__amd64__)) + guint32 pixel; + + pixel = 0xff000000U | + ((guint32) bp2[0] << 16) | + ((guint32) bp2[1] << 8) | + (guint32) bp2[2]; + memcpy (p, &pixel, sizeof (pixel)); +#else + p[0] = bp2[2]; + p[1] = bp2[1]; + p[2] = bp2[0]; + p[3] = 0xff; +#endif + bp2 += 4; + p += 4; + } + bptr += rowstride; + obuf += bpl; + } +} + +static void +gdk_rgb_convert_32_0888_br (GdkRgbInfo *image_info, GdkImage *image, + gint x0, gint y0, gint width, gint height, + const guchar *buf, int rowstride, + gint x_align, gint y_align, GdkRgbCmap *cmap) +{ + int y, w; + guchar *obuf, *p; + gint bpl; + const guchar *bptr, *bp2; + + bptr = buf; + bpl = image->bpl; + obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4; + for (y = 0; y < height; y++) + { + bp2 = bptr; + p = obuf; + w = width; + while (w--) + { + p[0] = 0xff; + p[1] = bp2[0]; + p[2] = bp2[1]; + p[3] = bp2[2]; + bp2 += 4; + p += 4; + } + bptr += rowstride; + obuf += bpl; + } +} + /* This is slow. Speed me up, please. */ static void gdk_rgb_32_to_stage (GdkRgbInfo *image_info, @@ -3709,6 +3786,16 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) if (conv_d == NULL) conv_d = conv; + if (conv == gdk_rgb_convert_0888) + conv_32 = gdk_rgb_convert_32_0888; + else if (conv == gdk_rgb_convert_0888_br) + conv_32 = gdk_rgb_convert_32_0888_br; + + if (conv_d == gdk_rgb_convert_0888) + conv_32_d = gdk_rgb_convert_32_0888; + else if (conv_d == gdk_rgb_convert_0888_br) + conv_32_d = gdk_rgb_convert_32_0888_br; + image_info->conv = conv; image_info->conv_d = conv_d; From 863f708271c3d6ad8b97a2e01e6b9a6f22a85e61 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 19 Aug 2026 22:23:32 +0200 Subject: [PATCH 59/68] gdk: speed up gray image drawing --- gdk/gdkrgb.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 00f994670..fd2dee57a 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -3415,6 +3415,50 @@ gdk_rgb_gray_to_stage (GdkRgbInfo *image_info, /* Generic gray conversion function - convert to 24bit packed, then go from there. */ +static void +gdk_rgb_convert_gray_0888_native (GdkRgbInfo *image_info, GdkImage *image, + gint x0, gint y0, gint width, gint height, + const guchar *buf, gint rowstride, + gint x_align, gint y_align, GdkRgbCmap *cmap) +{ + gint y, w; + gint bpl = image->bpl; + guchar *obuf = ((guchar *) image->mem) + y0 * bpl + x0 * 4; + + for (y = 0; y < height; y++) + { + const guchar *p = buf; + guchar *q = obuf; + + w = width; + while (w--) + { + guchar g = *p++; + + /* + * Store bytes explicitly here. Word-store performance varies by + * microarchitecture even within one ISA, so keep this generic path + * free of CPU-specific runtime dispatch. + */ +#if G_BYTE_ORDER == G_LITTLE_ENDIAN + q[0] = g; + q[1] = g; + q[2] = g; + q[3] = 0xff; +#else + q[0] = 0xff; + q[1] = g; + q[2] = g; + q[3] = g; +#endif + q += 4; + } + + buf += rowstride; + obuf += bpl; + } +} + static void gdk_rgb_convert_gray_generic (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -3787,14 +3831,34 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv_d = conv; if (conv == gdk_rgb_convert_0888) - conv_32 = gdk_rgb_convert_32_0888; + { + conv_32 = gdk_rgb_convert_32_0888; +#if G_BYTE_ORDER == G_LITTLE_ENDIAN + conv_gray = gdk_rgb_convert_gray_0888_native; +#endif + } else if (conv == gdk_rgb_convert_0888_br) - conv_32 = gdk_rgb_convert_32_0888_br; + { + conv_32 = gdk_rgb_convert_32_0888_br; +#if G_BYTE_ORDER == G_BIG_ENDIAN + conv_gray = gdk_rgb_convert_gray_0888_native; +#endif + } if (conv_d == gdk_rgb_convert_0888) - conv_32_d = gdk_rgb_convert_32_0888; + { + conv_32_d = gdk_rgb_convert_32_0888; +#if G_BYTE_ORDER == G_LITTLE_ENDIAN + conv_gray_d = gdk_rgb_convert_gray_0888_native; +#endif + } else if (conv_d == gdk_rgb_convert_0888_br) - conv_32_d = gdk_rgb_convert_32_0888_br; + { + conv_32_d = gdk_rgb_convert_32_0888_br; +#if G_BYTE_ORDER == G_BIG_ENDIAN + conv_gray_d = gdk_rgb_convert_gray_0888_native; +#endif + } image_info->conv = conv; image_info->conv_d = conv_d; From 8ec3a438fdbe0e39f5bb668029af0b48ab4c075e Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sun, 23 Aug 2026 12:07:19 +0200 Subject: [PATCH 60/68] gdk: consolidate scalar compositing kernels --- gdk/Makefile.am | 1 - gdk/gdkpixelops-generic.c | 149 ------------------------------------- gdk/gdkpixelops-x86-sse2.c | 2 +- gdk/gdkpixelops.c | 122 ++++++++++++++++++++++++++---- 4 files changed, 107 insertions(+), 167 deletions(-) delete mode 100644 gdk/gdkpixelops-generic.c diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 216437b65..c62f07bca 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -168,7 +168,6 @@ gdk_pixelops_arch_sources += gdkpixelops-x86-sse2.c endif gdk_pixelops_private_sources = \ - gdkpixelops-generic.c \ $(gdk_pixelops_arch_sources) gdk_c_sources = \ diff --git a/gdk/gdkpixelops-generic.c b/gdk/gdkpixelops-generic.c deleted file mode 100644 index 01a97a621..000000000 --- a/gdk/gdkpixelops-generic.c +++ /dev/null @@ -1,149 +0,0 @@ -/* GDK - The GIMP Drawing Kit - * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - */ - -#include "config.h" - -#include "gdkpixelops.h" - -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; - - while (height--) - { - gint twidth = width; - guchar *p = src; - guchar *q = dest; - - if (dest_byte_order == GDK_LSB_FIRST) - { - while (twidth--) - { - guint t; - - t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80; - q[0] = (t + (t >> 8)) >> 8; - t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80; - q[1] = (t + (t >> 8)) >> 8; - t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80; - q[2] = (t + (t >> 8)) >> 8; - p += 4; - q += 4; - } - } - else - { - while (twidth--) - { - guint t; - - t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80; - q[1] = (t + (t >> 8)) >> 8; - t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80; - q[2] = (t + (t >> 8)) >> 8; - t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80; - q[3] = (t + (t >> 8)) >> 8; - p += 4; - q += 4; - } - } - - src += src_rowstride; - dest += dest_rowstride; - } -} - -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_565_generic (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; - - while (height--) - { - gint twidth = width; - guchar *p = src; - gushort *q = (gushort *)dest; - - while (twidth--) - { - guchar a = p[3]; - guint tr, tg, tb; - guint tr1, tg1, tb1; - guint tmp = *q; - -#if 1 - /* This is fast, and corresponds to what composite() above does - * if we converted to 8-bit first. - */ - tr = (tmp & 0xf800); - tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tg = (tmp & 0x07e0); - tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tb = (tmp & 0x001f); - tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - - *q = (((tr1 + (tr1 >> 8)) & 0xf800) | - (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | - ((tb1 + (tb1 >> 8)) >> 11)); -#else - /* This version correspond to the result we get with XRENDER - - * a bit of precision is lost since we convert to 8 bit after premultiplying - * instead of at the end - */ - guint tr2, tg2, tb2; - guint tr3, tg3, tb3; - - tr = (tmp & 0xf800); - tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tr2 = a * p[0] + 0x80; - tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8); - - tg = (tmp & 0x07e0); - tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tg2 = a * p[0] + 0x80; - tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8); - - tb = (tmp & 0x001f); - tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - tb2 = a * p[0] + 0x80; - tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8); - - *q = (((tr3 & 0xf8) << 8) | - ((tg3 & 0xfc) << 3) | - ((tb3 >> 3))); -#endif - - p += 4; - q++; - } - - src += src_rowstride; - dest += dest_rowstride; - } -} diff --git a/gdk/gdkpixelops-x86-sse2.c b/gdk/gdkpixelops-x86-sse2.c index f502a7e44..892667902 100644 --- a/gdk/gdkpixelops-x86-sse2.c +++ b/gdk/gdkpixelops-x86-sse2.c @@ -19,7 +19,7 @@ /* * Keep this declaration at default visibility. The definition remains - * internal in gdkpixelops-generic.c, allowing compact SSE2 code while + * internal in gdkpixelops.c, allowing compact SSE2 code while * the final shared object still binds the fallback internally. */ void diff --git a/gdk/gdkpixelops.c b/gdk/gdkpixelops.c index ff405a35f..c9d16da2a 100644 --- a/gdk/gdkpixelops.c +++ b/gdk/gdkpixelops.c @@ -16,23 +16,7 @@ #include "gdkpixelops.h" -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_565_generic (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); #ifdef GDK_PIXELOPS_X86_64 G_GNUC_INTERNAL void @@ -45,6 +29,112 @@ _gdk_pixel_ops_composite_0888_sse2 (guchar *src_buf, gint height); #endif +/* Scalar compositing kernels. */ + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + guchar *q = dest; + + if (dest_byte_order == GDK_LSB_FIRST) + { + while (twidth--) + { + guint t; + + t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80; + q[0] = (t + (t >> 8)) >> 8; + t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80; + q[1] = (t + (t >> 8)) >> 8; + t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80; + q[2] = (t + (t >> 8)) >> 8; + p += 4; + q += 4; + } + } + else + { + while (twidth--) + { + guint t; + + t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80; + q[1] = (t + (t >> 8)) >> 8; + t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80; + q[2] = (t + (t >> 8)) >> 8; + t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80; + q[3] = (t + (t >> 8)) >> 8; + p += 4; + q += 4; + } + } + + src += src_rowstride; + dest += dest_rowstride; + } +} + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + guchar *src = src_buf; + guchar *dest = dest_buf; + + while (height--) + { + gint twidth = width; + guchar *p = src; + gushort *q = (gushort *)dest; + + while (twidth--) + { + guchar a = p[3]; + guint tr, tg, tb; + guint tr1, tg1, tb1; + guint tmp = *q; + + /* This is fast, and corresponds to what composite() above does + * if we converted to 8-bit first. + */ + tr = (tmp & 0xf800); + tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; + tg = (tmp & 0x07e0); + tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; + tb = (tmp & 0x001f); + tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; + + *q = (((tr1 + (tr1 >> 8)) & 0xf800) | + (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | + ((tb1 + (tb1 >> 8)) >> 11)); + + p += 4; + q++; + } + + src += src_rowstride; + dest += dest_rowstride; + } +} + G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops = { #ifdef GDK_PIXELOPS_X86_64 _gdk_pixel_ops_composite_0888_sse2, From 6062c4ebadff05d990798f6b57f6847319886185 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Wed, 26 Aug 2026 11:36:11 +0200 Subject: [PATCH 61/68] gdk: consolidate scalar RGB conversion paths Canonicalize destination-based converter selection, consolidate equivalent scalar conversion loops, and use a shared LUT for common non-dithered indexed targets while retaining generic fallback semantics. --- gdk/gdkrgb.c | 1078 ++++++++++++++++++++++++++------------------------ 1 file changed, 561 insertions(+), 517 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index fd2dee57a..0889e0164 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -46,6 +46,24 @@ typedef struct _GdkRgbInfo GdkRgbInfo; typedef struct _GdkRgbCmapInfo GdkRgbCmapInfo; +typedef enum +{ + GDK_RGB_FORMAT_OTHER = 0, + GDK_RGB_FORMAT_565, + GDK_RGB_FORMAT_565_BR, + GDK_RGB_FORMAT_555, + GDK_RGB_FORMAT_555_BR, + GDK_RGB_FORMAT_888_LSB, + GDK_RGB_FORMAT_888_MSB, + GDK_RGB_FORMAT_0888, + GDK_RGB_FORMAT_0888_BR, + GDK_RGB_FORMAT_RGB0_NATIVE, + GDK_RGB_FORMAT_101010_R_LOW_LE, + GDK_RGB_FORMAT_101010_B_LOW_LE, + GDK_RGB_FORMAT_101010_R_LOW_BE, + GDK_RGB_FORMAT_101010_B_LOW_BE +} GdkRgbFormat; + typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image, gint x0, gint y0, gint width, gint height, @@ -54,33 +72,22 @@ typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image, GdkRgbCmap *cmap); #define STAGE_ROWSTRIDE (GDK_SCRATCH_IMAGE_WIDTH * 3) +#define GDK_RGB_STAGE_ROWS 4 -/* Some of these fields should go, as they're not being used at all. (?) - */ struct _GdkRgbInfo { GdkVisual *visual; GdkColormap *cmap; - guint nred_shades; - guint ngreen_shades; - guint nblue_shades; - guint ngray_shades; - guint nreserved; - - guint bpp; - gint cmap_alloced; - gdouble gamma; - - /* Generally, the stage buffer is used to convert 32bit RGB, gray, - and indexed images into 24 bit packed RGB. */ - guchar *stage_buf; + guint8 nred_shades; + guint8 ngreen_shades; + guint8 nblue_shades; + guint8 bpp; + guint8 format; + guint8 dith_default; + guint8 bitmap; /* set true if in 1 bit per pixel mode */ GdkRgbCmap *gray_cmap; - - gboolean dith_default; - - gboolean bitmap; /* set true if in 1 bit per pixel mode */ GdkGC *own_gc; /* Convert functions */ @@ -94,7 +101,6 @@ struct _GdkRgbInfo GdkRgbConvFunc conv_gray_d; GdkRgbConvFunc conv_indexed; - GdkRgbConvFunc conv_indexed_d; guchar *colorcube; guchar *colorcube_d; @@ -525,7 +531,6 @@ gdk_rgb_free_info (GdkRgbInfo *image_info) { GSList *tmp_list; - g_free (image_info->stage_buf); if (image_info->gray_cmap) gdk_rgb_cmap_free (image_info->gray_cmap); @@ -542,6 +547,7 @@ gdk_rgb_free_info (GdkRgbInfo *image_info) { GdkRgbCmapInfo *cmap_info = tmp_list->data; cmap_info->cmap->info_list = g_slist_remove (cmap_info->cmap->info_list, cmap_info); + tmp_list = tmp_list->next; g_free (cmap_info); } g_slist_free (image_info->cmap_info_list); @@ -563,21 +569,13 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap) image_info = g_new0 (GdkRgbInfo, 1); image_info->visual = visual; - image_info->cmap = NULL; image_info->nred_shades = 6; image_info->ngreen_shades = 6; image_info->nblue_shades = 4; - image_info->ngray_shades = 24; - image_info->nreserved = 0; - image_info->bpp = 0; - image_info->cmap_alloced = FALSE; - image_info->gamma = 1.0; - image_info->stage_buf = NULL; - image_info->own_gc = NULL; image_info->cmap = colormap; @@ -614,12 +612,10 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap) (gdk_rgb_install_cmap || image_info->visual != gdk_screen_get_system_visual (screen))) { image_info->cmap = gdk_colormap_new (image_info->visual, FALSE); - image_info->cmap_alloced = TRUE; } if (!gdk_rgb_do_colormaps (image_info, image_info->cmap != NULL)) { image_info->cmap = gdk_colormap_new (image_info->visual, FALSE); - image_info->cmap_alloced = TRUE; gdk_rgb_do_colormaps (image_info, TRUE); } if (gdk_rgb_verbose) @@ -637,7 +633,6 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap) if (!image_info->cmap) { image_info->cmap = gdk_colormap_new (image_info->visual, FALSE); - image_info->cmap_alloced = TRUE; } gdk_rgb_set_gray_cmap (image_info, image_info->cmap); @@ -654,7 +649,6 @@ gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap) else { image_info->cmap = gdk_colormap_new (image_info->visual, FALSE); - image_info->cmap_alloced = TRUE; } } } @@ -2285,50 +2279,62 @@ gdk_rgb_convert_888_lsb (GdkRgbInfo *image_info, GdkImage *image, /* convert 24-bit packed to 32-bit unpacked */ /* todo: optimize this */ -static void -gdk_rgb_convert_0888 (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, int rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - int y, w; - guchar *obuf, *p; - gint bpl; - const guchar *bptr, *bp2; - - bptr = buf; - bpl = image->bpl; - obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4; - for (y = 0; y < height; y++) - { - bp2 = bptr; - p = obuf; - w = width; - while (w--) - { #if G_BYTE_ORDER == G_LITTLE_ENDIAN && GLIB_SIZEOF_VOID_P == 8 && \ (defined(__x86_64__) || defined(__amd64__)) - guint32 pixel; - - pixel = 0xff000000U | - ((guint32) bp2[0] << 16) | - ((guint32) bp2[1] << 8) | - (guint32) bp2[2]; - memcpy (p, &pixel, sizeof (pixel)); +#define GDK_RGB_STORE_0888(p, bp) G_STMT_START { \ + guint32 pixel; \ + pixel = 0xff000000U | \ + ((guint32) (bp)[0] << 16) | \ + ((guint32) (bp)[1] << 8) | \ + (guint32) (bp)[2]; \ + memcpy ((p), &pixel, sizeof (pixel)); \ +} G_STMT_END #else - p[0] = bp2[2]; - p[1] = bp2[1]; - p[2] = bp2[0]; - p[3] = 0xff; +#define GDK_RGB_STORE_0888(p, bp) G_STMT_START { \ + (p)[0] = (bp)[2]; \ + (p)[1] = (bp)[1]; \ + (p)[2] = (bp)[0]; \ + (p)[3] = 0xff; \ +} G_STMT_END #endif - bp2 += 3; - p += 4; - } - bptr += rowstride; - obuf += bpl; - } + +#define GDK_RGB_STORE_0888_BR(p, bp) G_STMT_START { \ + (p)[0] = 0xff; \ + (p)[1] = (bp)[0]; \ + (p)[2] = (bp)[1]; \ + (p)[3] = (bp)[2]; \ +} G_STMT_END + +#define GDK_RGB_DEFINE_0888_CONVERTER(name, source_stride, store_pixel) \ +static void \ +name (GdkRgbInfo *image_info, GdkImage *image, \ + gint x0, gint y0, gint width, gint height, \ + const guchar *buf, int rowstride, \ + gint x_align, gint y_align, GdkRgbCmap *cmap) \ +{ \ + gint y, w; \ + gint bpl = image->bpl; \ + const guchar *bptr = buf; \ + guchar *obuf = ((guchar *) image->mem) + y0 * bpl + x0 * 4; \ + for (y = 0; y < height; y++) \ + { \ + const guchar *bp2 = bptr; \ + guchar *p = obuf; \ + w = width; \ + while (w--) \ + { \ + store_pixel (p, bp2); \ + bp2 += (source_stride); \ + p += 4; \ + } \ + bptr += rowstride; \ + obuf += bpl; \ + } \ } +GDK_RGB_DEFINE_0888_CONVERTER (gdk_rgb_convert_0888, + 3, GDK_RGB_STORE_0888) + #ifdef USE_MEDIALIB25 /* convert 24-bit packed to 32-bit unpacked */ static void @@ -2352,38 +2358,8 @@ gdk_rgb_convert_0888_medialib (GdkRgbInfo *image_info, GdkImage *image, } #endif -static void -gdk_rgb_convert_0888_br (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, int rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - int y, w; - guchar *obuf, *p; - gint bpl; - const guchar *bptr, *bp2; - - bptr = buf; - bpl = image->bpl; - obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4; - for (y = 0; y < height; y++) - { - bp2 = bptr; - p = obuf; - w = width; - while (w--) - { - p[0] = 0xff; - p[1] = bp2[0]; - p[2] = bp2[1]; - p[3] = bp2[2]; - bp2 += 3; - p += 4; - } - bptr += rowstride; - obuf += bpl; - } -} +GDK_RGB_DEFINE_0888_CONVERTER (gdk_rgb_convert_0888_br, + 3, GDK_RGB_STORE_0888_BR) static void gdk_rgb_convert_8880_br (GdkRgbInfo *image_info, GdkImage *image, @@ -3238,116 +3214,191 @@ gdk_rgb_convert_1 (GdkRgbInfo *image_info, GdkImage *image, } } -/* Returns a pointer to the stage buffer. */ -static guchar * -gdk_rgb_ensure_stage (GdkRgbInfo *image_info) -{ - if (image_info->stage_buf == NULL) - image_info->stage_buf = g_malloc (GDK_SCRATCH_IMAGE_HEIGHT * STAGE_ROWSTRIDE); - return image_info->stage_buf; -} -static void -gdk_rgb_convert_32_0888 (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, int rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - int y, w; - guchar *obuf, *p; - gint bpl; - const guchar *bptr, *bp2; +GDK_RGB_DEFINE_0888_CONVERTER (gdk_rgb_convert_32_0888, + 4, GDK_RGB_STORE_0888) - bptr = buf; - bpl = image->bpl; - obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4; - for (y = 0; y < height; y++) - { - bp2 = bptr; - p = obuf; - w = width; - while (w--) - { -#if G_BYTE_ORDER == G_LITTLE_ENDIAN && GLIB_SIZEOF_VOID_P == 8 && \ - (defined(__x86_64__) || defined(__amd64__)) - guint32 pixel; +GDK_RGB_DEFINE_0888_CONVERTER (gdk_rgb_convert_32_0888_br, + 4, GDK_RGB_STORE_0888_BR) - pixel = 0xff000000U | - ((guint32) bp2[0] << 16) | - ((guint32) bp2[1] << 8) | - (guint32) bp2[2]; - memcpy (p, &pixel, sizeof (pixel)); -#else - p[0] = bp2[2]; - p[1] = bp2[1]; - p[2] = bp2[0]; - p[3] = 0xff; -#endif - bp2 += 4; - p += 4; - } - bptr += rowstride; - obuf += bpl; - } -} - -static void -gdk_rgb_convert_32_0888_br (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, int rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - int y, w; - guchar *obuf, *p; - gint bpl; - const guchar *bptr, *bp2; - - bptr = buf; - bpl = image->bpl; - obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4; - for (y = 0; y < height; y++) - { - bp2 = bptr; - p = obuf; - w = width; - while (w--) - { - p[0] = 0xff; - p[1] = bp2[0]; - p[2] = bp2[1]; - p[3] = bp2[2]; - bp2 += 4; - p += 4; - } - bptr += rowstride; - obuf += bpl; - } -} +#undef GDK_RGB_DEFINE_0888_CONVERTER +#undef GDK_RGB_STORE_0888_BR +#undef GDK_RGB_STORE_0888 /* This is slow. Speed me up, please. */ + +typedef void (*GdkRgbStageFunc) (guchar *stage, + const guchar *buf, + gint rowstride, + gint width, + gint height, + GdkRgbCmap *cmap); + static void -gdk_rgb_32_to_stage (GdkRgbInfo *image_info, - const guchar *buf, gint rowstride, gint width, gint height) +gdk_rgb_convert_staged (GdkRgbInfo *image_info, + GdkImage *image, + gint x0, + gint y0, + gint width, + gint height, + const guchar *buf, + gint rowstride, + gint x_align, + gint y_align, + GdkRgbCmap *cmap, + GdkRgbConvFunc conv, + GdkRgbStageFunc stage_func) +{ + guchar stage[GDK_RGB_STAGE_ROWS * STAGE_ROWSTRIDE]; + gint row; + gint rows; + + for (row = 0; row < height; row += GDK_RGB_STAGE_ROWS) + { + rows = MIN (GDK_RGB_STAGE_ROWS, height - row); + + stage_func (stage, + buf + (gssize) row * rowstride, + rowstride, + width, + rows, + cmap); + + conv (image_info, + image, + x0, y0 + row, + width, rows, + stage, STAGE_ROWSTRIDE, + x_align, y_align + row, + cmap); + } +} + +#define DEFINE_16_CONVERTER(name, prelude, pack, advance) \ +static void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, gint y0, \ + gint width, gint height, \ + const guchar *buf, \ + gint rowstride, \ + gint x_align, gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + gint x, y; \ + gint bpl = image->bpl; \ + const guchar *src = buf; \ + guchar *dst = ((guchar *) image->mem) + y0 * bpl + x0 * 2; \ + \ + for (y = 0; y < height; y++) \ + { \ + const guchar *p = src; \ + guint16 *q = (guint16 *) dst; \ + \ + for (x = 0; x < width; x++) \ + { \ + prelude \ + q[x] = pack; \ + advance \ + } \ + \ + src += rowstride; \ + dst += bpl; \ + } \ +} + +DEFINE_16_CONVERTER ( + gdk_rgb_convert_32_565, + , + ((p[0] & 0xf8) << 8) | + ((p[1] & 0xfc) << 3) | + (p[2] >> 3), + p += 4;) + +#define DEFINE_565_DITHER_CONVERTER(name, load_rgb, advance) \ +static void \ +name (GdkRgbInfo *image_info, GdkImage *image, \ + gint x0, gint y0, gint width, gint height, \ + const guchar *buf, gint rowstride, \ + gint x_align, gint y_align, GdkRgbCmap *cmap) \ +{ \ + gint x, y; \ + gint bpl = image->bpl; \ + const guchar *src = buf; \ + guchar *dst = ((guchar *) image->mem) + y0 * bpl + x0 * 2; \ + \ + for (y = 0; y < height; y++) \ + { \ + const guint32 *dmp = \ + DM_565 + \ + (((y + y_align) & (DM_HEIGHT - 1)) << DM_WIDTH_SHIFT); \ + const guchar *p = src; \ + guint16 *q = (guint16 *) dst; \ + \ + for (x = 0; x < width; x++) \ + { \ + load_rgb \ + rgb += dmp[(x + x_align) & (DM_WIDTH - 1)]; \ + \ + rgb += 0x10040100 \ + - ((rgb & 0x1e0001e0) >> 5) \ + - ((rgb & 0x00070000) >> 6); \ + \ + q[x] = \ + ((rgb & 0x0f800000) >> 12) | \ + ((rgb & 0x0003f000) >> 7) | \ + ((rgb & 0x000000f8) >> 3); \ + \ + advance \ + } \ + \ + src += rowstride; \ + dst += bpl; \ + } \ +} + +DEFINE_565_DITHER_CONVERTER ( + gdk_rgb_convert_32_565_d, + gint32 rgb = (gint32) p[0] << 20; + rgb += (gint32) p[1] << 10; + rgb += p[2];, + p += 4;) + +DEFINE_16_CONVERTER ( + gdk_rgb_convert_32_555, + , + ((p[0] & 0xf8) << 7) | + ((p[1] & 0xf8) << 2) | + (p[2] >> 3), + p += 4;) + + +static void +gdk_rgb_32_to_stage (guchar *stage, + const guchar *buf, gint rowstride, gint width, gint height, + GdkRgbCmap *cmap) { gint x, y; - const guchar *pi_start; - guchar *po_start; + const guchar *pi_start = buf; + guchar *po_start = stage; const guchar *pi; guchar *po; - pi_start = buf; - po_start = gdk_rgb_ensure_stage (image_info); + (void) cmap; + for (y = 0; y < height; y++) { pi = pi_start; po = po_start; + for (x = 0; x < width; x++) - { - *po++ = *pi++; - *po++ = *pi++; - *po++ = *pi++; - pi++; - } + { + *po++ = *pi++; + *po++ = *pi++; + *po++ = *pi++; + pi++; + } + pi_start += rowstride; po_start += STAGE_ROWSTRIDE; } @@ -3355,59 +3406,146 @@ gdk_rgb_32_to_stage (GdkRgbInfo *image_info, /* Generic 32bit RGB conversion function - convert to 24bit packed, then go from there. */ -static void -gdk_rgb_convert_32_generic (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - gdk_rgb_32_to_stage (image_info, buf, rowstride, width, height); - (*image_info->conv) (image_info, image, x0, y0, width, height, - image_info->stage_buf, STAGE_ROWSTRIDE, - x_align, y_align, cmap); +#define DEFINE_STAGED_CONVERTER(name, conv_member, stage_func) \ +static void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, gint y0, \ + gint width, gint height, \ + const guchar *buf, \ + gint rowstride, \ + gint x_align, gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + gdk_rgb_convert_staged (image_info, image, \ + x0, y0, width, height, \ + buf, rowstride, \ + x_align, y_align, \ + cmap, image_info->conv_member, \ + stage_func); \ } -/* Generic 32bit RGB conversion function - convert to 24bit packed, then - go from there. */ -static void -gdk_rgb_convert_32_generic_d (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - gdk_rgb_32_to_stage (image_info, buf, rowstride, width, height); +DEFINE_STAGED_CONVERTER (gdk_rgb_convert_32_generic, + conv, gdk_rgb_32_to_stage) +DEFINE_STAGED_CONVERTER (gdk_rgb_convert_32_generic_d, + conv_d, gdk_rgb_32_to_stage) - (*image_info->conv_d) (image_info, image, x0, y0, width, height, - image_info->stage_buf, STAGE_ROWSTRIDE, - x_align, y_align, cmap); +DEFINE_565_DITHER_CONVERTER ( + gdk_rgb_convert_565_gray_d, + gint32 gray = p[x]; + gint32 rgb = gray << 20; + rgb += gray << 10; + rgb += gray;, + ) + +#undef DEFINE_565_DITHER_CONVERTER + +DEFINE_16_CONVERTER ( + gdk_rgb_convert_gray_555, + guint g = p[x];, + ((g & 0xf8) << 7) | + ((g & 0xf8) << 2) | + (g >> 3), + ) + + +static void +gdk_rgb_convert_indexed_lut (GdkRgbInfo *image_info, GdkImage *image, + gint x0, gint y0, gint width, gint height, + const guchar *buf, gint rowstride, + gint x_align, gint y_align, GdkRgbCmap *cmap) +{ + union { + guint16 u16[256]; + guint32 u32[256]; + } lut; + gint i, y; + gint bpl = image->bpl; + const guchar *src = buf; + guchar *dst = ((guchar *) image->mem) + y0 * bpl + x0 * image_info->bpp; + + if (image_info->bpp == 2) + { + guint is_565 = image_info->format == GDK_RGB_FORMAT_565; + guint red_shift = 9 - is_565; + guint green_shift = 6 - is_565; + guint32 green_mask = 0x00f800 | (is_565 << 10); + + for (i = 0; i < cmap->n_colors; i++) + { + guint32 rgb = cmap->colors[i]; + lut.u16[i] = ((rgb & 0xf80000) >> red_shift) | + ((rgb & green_mask) >> green_shift) | + ((rgb & 0x0000f8) >> 3); + } + + for (y = 0; y < height; y++) + { + const guchar *p = src; + guint16 *q = (guint16 *) dst; + gint x; + + for (x = 0; x < width; x++) + q[x] = lut.u16[p[x]]; + + src += rowstride; + dst += bpl; + } + } + else + { + gboolean little = image_info->format == GDK_RGB_FORMAT_0888; + + for (i = 0; i < cmap->n_colors; i++) + { + guint32 pixel = 0xff000000U | cmap->colors[i]; + lut.u32[i] = little ? GUINT32_TO_LE (pixel) : GUINT32_TO_BE (pixel); + } + + for (y = 0; y < height; y++) + { + const guchar *p = src; + guint32 *q = (guint32 *) dst; + gint x; + + for (x = 0; x < width; x++) + q[x] = lut.u32[p[x]]; + + src += rowstride; + dst += bpl; + } + } } -/* This is slow. Speed me up, please. */ + static void -gdk_rgb_gray_to_stage (GdkRgbInfo *image_info, - const guchar *buf, gint rowstride, gint width, gint height) +gdk_rgb_gray_to_stage (guchar *stage, + const guchar *buf, gint rowstride, gint width, gint height, + GdkRgbCmap *cmap) { gint x, y; - const guchar *pi_start; - guchar *po_start; + const guchar *pi_start = buf; + guchar *po_start = stage; const guchar *pi; guchar *po; guchar gray; - pi_start = buf; - po_start = gdk_rgb_ensure_stage (image_info); + (void) cmap; + for (y = 0; y < height; y++) { pi = pi_start; po = po_start; + for (x = 0; x < width; x++) - { - gray = *pi++; - *po++ = gray; - *po++ = gray; - *po++ = gray; - } + { + gray = *pi++; + *po++ = gray; + *po++ = gray; + *po++ = gray; + } + pi_start += rowstride; po_start += STAGE_ROWSTRIDE; } @@ -3459,31 +3597,11 @@ gdk_rgb_convert_gray_0888_native (GdkRgbInfo *image_info, GdkImage *image, } } -static void -gdk_rgb_convert_gray_generic (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - gdk_rgb_gray_to_stage (image_info, buf, rowstride, width, height); - (*image_info->conv) (image_info, image, x0, y0, width, height, - image_info->stage_buf, STAGE_ROWSTRIDE, - x_align, y_align, cmap); -} - -static void -gdk_rgb_convert_gray_generic_d (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - gdk_rgb_gray_to_stage (image_info, buf, rowstride, width, height); - - (*image_info->conv_d) (image_info, image, x0, y0, width, height, - image_info->stage_buf, STAGE_ROWSTRIDE, - x_align, y_align, cmap); -} +DEFINE_STAGED_CONVERTER (gdk_rgb_convert_gray_generic, + conv, gdk_rgb_gray_to_stage) +DEFINE_STAGED_CONVERTER (gdk_rgb_convert_gray_generic_d, + conv_d, gdk_rgb_gray_to_stage) /* Render grayscale using indexed method. */ static void @@ -3497,45 +3615,34 @@ gdk_rgb_convert_gray_cmap (GdkRgbInfo *image_info, GdkImage *image, x_align, y_align, image_info->gray_cmap); } -#if 0 -static void -gdk_rgb_convert_gray_cmap_d (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - (*image_info->conv_indexed_d) (image_info, image, x0, y0, width, height, - buf, rowstride, - x_align, y_align, image_info->gray_cmap); -} -#endif /* This is slow. Speed me up, please. */ static void -gdk_rgb_indexed_to_stage (GdkRgbInfo *image_info, - const guchar *buf, gint rowstride, gint width, gint height, - GdkRgbCmap *cmap) +gdk_rgb_indexed_to_stage (guchar *stage, + const guchar *buf, gint rowstride, gint width, gint height, + GdkRgbCmap *cmap) { gint x, y; - const guchar *pi_start; - guchar *po_start; + const guchar *pi_start = buf; + guchar *po_start = stage; const guchar *pi; guchar *po; - gint rgb; + guint32 rgb; + - pi_start = buf; - po_start = gdk_rgb_ensure_stage (image_info); for (y = 0; y < height; y++) { pi = pi_start; po = po_start; + for (x = 0; x < width; x++) - { - rgb = cmap->colors[*pi++]; - *po++ = rgb >> 16; - *po++ = (rgb >> 8) & 0xff; - *po++ = rgb & 0xff; - } + { + rgb = cmap->colors[*pi++]; + *po++ = rgb >> 16; + *po++ = (rgb >> 8) & 0xff; + *po++ = rgb & 0xff; + } + pi_start += rowstride; po_start += STAGE_ROWSTRIDE; } @@ -3543,32 +3650,13 @@ gdk_rgb_indexed_to_stage (GdkRgbInfo *image_info, /* Generic gray conversion function - convert to 24bit packed, then go from there. */ -static void -gdk_rgb_convert_indexed_generic (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - gdk_rgb_indexed_to_stage (image_info, buf, rowstride, width, height, cmap); - (*image_info->conv) (image_info, image, x0, y0, width, height, - image_info->stage_buf, STAGE_ROWSTRIDE, - x_align, y_align, cmap); -} +DEFINE_STAGED_CONVERTER (gdk_rgb_convert_indexed_generic, + conv, gdk_rgb_indexed_to_stage) +DEFINE_STAGED_CONVERTER (gdk_rgb_convert_indexed_generic_d, + conv_d, gdk_rgb_indexed_to_stage) -static void -gdk_rgb_convert_indexed_generic_d (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, - GdkRgbCmap *cmap) -{ - gdk_rgb_indexed_to_stage (image_info, buf, rowstride, width, height, cmap); - - (*image_info->conv_d) (image_info, image, x0, y0, width, height, - image_info->stage_buf, STAGE_ROWSTRIDE, - x_align, y_align, cmap); -} +#undef DEFINE_STAGED_CONVERTER /* Select a conversion function based on the visual and a representative image. */ @@ -3582,8 +3670,9 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) GdkRgbConvFunc conv, conv_d; GdkRgbConvFunc conv_32, conv_32_d; GdkRgbConvFunc conv_gray, conv_gray_d; - GdkRgbConvFunc conv_indexed, conv_indexed_d; + GdkRgbConvFunc conv_indexed; gboolean mask_rgb, mask_bgr; + GdkRgbFormat format; GdkScreen *screen = gdk_visual_get_screen (image_info->visual); depth = image_info->visual->depth; @@ -3616,6 +3705,7 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv = NULL; conv_d = NULL; + format = GDK_RGB_FORMAT_OTHER; conv_32 = gdk_rgb_convert_32_generic; conv_32_d = gdk_rgb_convert_32_generic_d; @@ -3624,16 +3714,20 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv_gray_d = gdk_rgb_convert_gray_generic_d; conv_indexed = gdk_rgb_convert_indexed_generic; - conv_indexed_d = gdk_rgb_convert_indexed_generic_d; image_info->dith_default = FALSE; +#define SET_CONV(func, fmt) G_STMT_START { \ + conv = (func); \ + format = (fmt); \ +} G_STMT_END + if (image_info->bitmap) conv = gdk_rgb_convert_1; else if (bpp == 16 && depth == 16 && !byterev && red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f) { - conv = gdk_rgb_convert_565; + SET_CONV (gdk_rgb_convert_565, GDK_RGB_FORMAT_565); conv_d = gdk_rgb_convert_565_d; conv_gray = gdk_rgb_convert_565_gray; gdk_rgb_preprocess_dm_565 (); @@ -3641,32 +3735,32 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) else if (bpp == 16 && depth == 16 && vtype == GDK_VISUAL_TRUE_COLOR && byterev && red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f) - conv = gdk_rgb_convert_565_br; + SET_CONV (gdk_rgb_convert_565_br, GDK_RGB_FORMAT_565_BR); else if (bpp == 16 && depth == 15 && vtype == GDK_VISUAL_TRUE_COLOR && !byterev && red_mask == 0x7c00 && green_mask == 0x3e0 && blue_mask == 0x1f) - conv = gdk_rgb_convert_555; + SET_CONV (gdk_rgb_convert_555, GDK_RGB_FORMAT_555); else if (bpp == 16 && depth == 15 && vtype == GDK_VISUAL_TRUE_COLOR && byterev && red_mask == 0x7c00 && green_mask == 0x3e0 && blue_mask == 0x1f) - conv = gdk_rgb_convert_555_br; + SET_CONV (gdk_rgb_convert_555_br, GDK_RGB_FORMAT_555_BR); /* I'm not 100% sure about the 24bpp tests - but testing will show*/ else if (bpp == 24 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && - ((mask_rgb && byte_order == GDK_LSB_FIRST) || - (mask_bgr && byte_order == GDK_MSB_FIRST))) - conv = gdk_rgb_convert_888_lsb; - else if (bpp == 24 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && - ((mask_rgb && byte_order == GDK_MSB_FIRST) || - (mask_bgr && byte_order == GDK_LSB_FIRST))) - conv = gdk_rgb_convert_888_msb; + (mask_rgb || mask_bgr) && + (byte_order == GDK_LSB_FIRST || byte_order == GDK_MSB_FIRST)) + { + gboolean lsb = mask_rgb == (byte_order == GDK_LSB_FIRST); + SET_CONV (lsb ? gdk_rgb_convert_888_lsb : gdk_rgb_convert_888_msb, + lsb ? GDK_RGB_FORMAT_888_LSB : GDK_RGB_FORMAT_888_MSB); + } else if (bpp == 32 && (depth == 24 || depth == 32) && vtype == GDK_VISUAL_TRUE_COLOR && (mask_rgb && byte_order == GDK_MSB_FIRST)) - conv = gdk_rgb_convert_0888_br; + SET_CONV (gdk_rgb_convert_0888_br, GDK_RGB_FORMAT_0888_BR); else if (bpp == 32 && (depth == 24 || depth == 32) && vtype == GDK_VISUAL_TRUE_COLOR && @@ -3674,69 +3768,47 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) { #ifdef USE_MEDIALIB25 if (_gdk_use_medialib ()) - conv = gdk_rgb_convert_0888_medialib; + SET_CONV (gdk_rgb_convert_0888_medialib, GDK_RGB_FORMAT_0888); else - conv = gdk_rgb_convert_0888; + SET_CONV (gdk_rgb_convert_0888, GDK_RGB_FORMAT_0888); #else - conv = gdk_rgb_convert_0888; + SET_CONV (gdk_rgb_convert_0888, GDK_RGB_FORMAT_0888); #endif } #if G_BYTE_ORDER == G_BIG_ENDIAN else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && (mask_bgr && byte_order == GDK_MSB_FIRST)) - conv = gdk_rgb_convert_8880_br; - else if (bpp == 32 && depth == 32 && vtype == GDK_VISUAL_TRUE_COLOR && - (mask_rgb && byte_order == GDK_LSB_FIRST)) - conv = gdk_rgb_convert_8880_br; - else if (bpp == 32 && depth == 32 && vtype == GDK_VISUAL_TRUE_COLOR && - (mask_rgb && byte_order == GDK_MSB_FIRST)) - conv = gdk_rgb_convert_8880_br; + SET_CONV (gdk_rgb_convert_8880_br, GDK_RGB_FORMAT_RGB0_NATIVE); #else else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && (mask_bgr && byte_order == GDK_LSB_FIRST)) - conv = gdk_rgb_convert_8880_br; - else if (bpp == 32 && depth == 32 && vtype == GDK_VISUAL_TRUE_COLOR && - (mask_rgb && byte_order == GDK_LSB_FIRST)) + SET_CONV (gdk_rgb_convert_8880_br, GDK_RGB_FORMAT_RGB0_NATIVE); +#endif + else if (bpp == 32 && (depth == 30 || depth == 32) && + vtype == GDK_VISUAL_TRUE_COLOR && + green_mask == 0x000ffc00 && + ((red_mask == 0x000003ff && blue_mask == 0x3ff00000) || + (red_mask == 0x3ff00000 && blue_mask == 0x000003ff)) && + (byte_order == GDK_LSB_FIRST || + byte_order == GDK_MSB_FIRST)) { -#ifdef USE_MEDIALIB25 - if (_gdk_use_medialib ()) - conv = gdk_rgb_convert_0888_medialib; + if (red_mask == 0x000003ff) + SET_CONV ((byte_order == GDK_LSB_FIRST + ? gdk_rgb_convert_101010_r_low_lsb + : gdk_rgb_convert_101010_r_low_msb), + (byte_order == GDK_LSB_FIRST + ? GDK_RGB_FORMAT_101010_R_LOW_LE + : GDK_RGB_FORMAT_101010_R_LOW_BE)); else - conv = gdk_rgb_convert_0888; -#else - conv = gdk_rgb_convert_0888; -#endif + SET_CONV ((byte_order == GDK_LSB_FIRST + ? gdk_rgb_convert_101010_b_low_lsb + : gdk_rgb_convert_101010_b_low_msb), + (byte_order == GDK_LSB_FIRST + ? GDK_RGB_FORMAT_101010_B_LOW_LE + : GDK_RGB_FORMAT_101010_B_LOW_BE)); } -#endif - else if (bpp == 32 && (depth == 30 || depth == 32) && - vtype == GDK_VISUAL_TRUE_COLOR && - byte_order == GDK_LSB_FIRST && - red_mask == 0x000003ff && - green_mask == 0x000ffc00 && - blue_mask == 0x3ff00000) - conv = gdk_rgb_convert_101010_r_low_lsb; - else if (bpp == 32 && (depth == 30 || depth == 32) && - vtype == GDK_VISUAL_TRUE_COLOR && - byte_order == GDK_LSB_FIRST && - red_mask == 0x3ff00000 && - green_mask == 0x000ffc00 && - blue_mask == 0x000003ff) - conv = gdk_rgb_convert_101010_b_low_lsb; - else if (bpp == 32 && (depth == 30 || depth == 32) && - vtype == GDK_VISUAL_TRUE_COLOR && - byte_order == GDK_MSB_FIRST && - red_mask == 0x000003ff && - green_mask == 0x000ffc00 && - blue_mask == 0x3ff00000) - conv = gdk_rgb_convert_101010_r_low_msb; - else if (bpp == 32 && (depth == 30 || depth == 32) && - vtype == GDK_VISUAL_TRUE_COLOR && - byte_order == GDK_MSB_FIRST && - red_mask == 0x3ff00000 && - green_mask == 0x000ffc00 && - blue_mask == 0x000003ff) - conv = gdk_rgb_convert_101010_b_low_msb; + else if (vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_LSB_FIRST) { conv = gdk_rgb_convert_truecolor_lsb; @@ -3820,6 +3892,8 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) #endif } +#undef SET_CONV + if (!conv) g_error ("Visual type=%d depth=%d, image bpp=%d, %s first\n" "is not supported by GdkRGB. Please submit a bug report\n" @@ -3830,36 +3904,49 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) if (conv_d == NULL) conv_d = conv; - if (conv == gdk_rgb_convert_0888) + switch (format) { - conv_32 = gdk_rgb_convert_32_0888; -#if G_BYTE_ORDER == G_LITTLE_ENDIAN - conv_gray = gdk_rgb_convert_gray_0888_native; -#endif - } - else if (conv == gdk_rgb_convert_0888_br) - { - conv_32 = gdk_rgb_convert_32_0888_br; -#if G_BYTE_ORDER == G_BIG_ENDIAN - conv_gray = gdk_rgb_convert_gray_0888_native; -#endif - } + case GDK_RGB_FORMAT_555: + conv_32 = gdk_rgb_convert_32_555; + conv_32_d = gdk_rgb_convert_32_555; + conv_gray = gdk_rgb_convert_gray_555; + conv_gray_d = gdk_rgb_convert_gray_555; + conv_indexed = gdk_rgb_convert_indexed_lut; + break; - if (conv_d == gdk_rgb_convert_0888) - { + case GDK_RGB_FORMAT_565: + conv_32 = gdk_rgb_convert_32_565; + conv_32_d = gdk_rgb_convert_32_565_d; + conv_gray_d = gdk_rgb_convert_565_gray_d; + conv_indexed = gdk_rgb_convert_indexed_lut; + break; + + case GDK_RGB_FORMAT_0888: + conv_32 = gdk_rgb_convert_32_0888; conv_32_d = gdk_rgb_convert_32_0888; #if G_BYTE_ORDER == G_LITTLE_ENDIAN + conv_gray = gdk_rgb_convert_gray_0888_native; conv_gray_d = gdk_rgb_convert_gray_0888_native; #endif - } - else if (conv_d == gdk_rgb_convert_0888_br) - { + conv_indexed = gdk_rgb_convert_indexed_lut; + break; + + case GDK_RGB_FORMAT_0888_BR: + conv_32 = gdk_rgb_convert_32_0888_br; conv_32_d = gdk_rgb_convert_32_0888_br; #if G_BYTE_ORDER == G_BIG_ENDIAN + conv_gray = gdk_rgb_convert_gray_0888_native; conv_gray_d = gdk_rgb_convert_gray_0888_native; #endif + conv_indexed = gdk_rgb_convert_indexed_lut; + break; + + default: + break; } + image_info->format = format; + image_info->conv = conv; image_info->conv_d = conv_d; @@ -3870,7 +3957,6 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) image_info->conv_gray_d = conv_gray_d; image_info->conv_indexed = conv_indexed; - image_info->conv_indexed_d = conv_indexed_d; } static void @@ -4165,8 +4251,9 @@ DEFINE_GDK_RGB16_PAIR (101010_b_low_be, 4, #undef GDK_RGB16_STORE_101010_R_LOW_BE #undef GDK_RGB16_STORE_101010_B_LOW_BE + static void -gdk_rgb16_to_stage (GdkRgbInfo *image_info, +gdk_rgb16_to_stage (guchar *stage, const guchar *buf, gint rowstride, gint width, @@ -4175,7 +4262,7 @@ gdk_rgb16_to_stage (GdkRgbInfo *image_info, { const guchar *row = buf; const guint16 *src; - guchar *dst_row = gdk_rgb_ensure_stage (image_info); + guchar *dst_row = stage; guchar *dst; gint x, y; @@ -4197,6 +4284,7 @@ gdk_rgb16_to_stage (GdkRgbInfo *image_info, } } + static void gdk_draw_rgb16_fallback (GdkRgbInfo *image_info, GdkDrawable *drawable, @@ -4212,13 +4300,16 @@ gdk_draw_rgb16_fallback (GdkRgbInfo *image_info, gint xdith, gint ydith) { + guchar stage[GDK_RGB_STAGE_ROWS * STAGE_ROWSTRIDE]; GdkScreen *screen; GdkImage *image; const guchar *buf_ptr; + const guchar *row_ptr; gint depth; gint x0, y0; gint xs0, ys0; gint width1, height1; + gint row0, rows; if (image_info->bitmap) { @@ -4242,13 +4333,6 @@ gdk_draw_rgb16_fallback (GdkRgbInfo *image_info, buf_ptr = buf + (gssize) y0 * rowstride + x0 * words * sizeof (guint16); - gdk_rgb16_to_stage (image_info, - buf_ptr, - rowstride, - width1, - height1, - words); - image = _gdk_image_get_scratch (screen, width1, height1, @@ -4256,15 +4340,28 @@ gdk_draw_rgb16_fallback (GdkRgbInfo *image_info, &xs0, &ys0); - conv (image_info, - image, - xs0, ys0, - width1, height1, - image_info->stage_buf, - STAGE_ROWSTRIDE, - x + x0 + xdith, - y + y0 + ydith, - NULL); + for (row0 = 0; row0 < height1; row0 += GDK_RGB_STAGE_ROWS) + { + rows = MIN (GDK_RGB_STAGE_ROWS, height1 - row0); + row_ptr = buf_ptr + (gssize) row0 * rowstride; + + gdk_rgb16_to_stage (stage, + row_ptr, + rowstride, + width1, + rows, + words); + + conv (image_info, + image, + xs0, ys0 + row0, + width1, rows, + stage, + STAGE_ROWSTRIDE, + x + x0 + xdith, + y + y0 + row0 + ydith, + NULL); + } #ifndef DONT_ACTUALLY_DRAW gdk_draw_image (drawable, @@ -4282,124 +4379,64 @@ static GdkRgbConvFunc gdk_rgb16_select_converter (GdkRgbInfo *image_info, gboolean is_rgbx) { - GdkVisual *visual = image_info->visual; - GdkByteOrder byte_order; - guint32 red_mask, green_mask, blue_mask; - gint depth; - gint bits_per_pixel; - gboolean byte_reversed; - gboolean mask_rgb; - gboolean mask_bgr; - -#define SELECT_PAIR(suffix) \ - return is_rgbx ? gdk_rgbx64_convert_ ## suffix \ +#define SELECT_PAIR(suffix) \ + return is_rgbx ? gdk_rgbx64_convert_ ## suffix \ : gdk_rgb48_convert_ ## suffix - if (visual->type != GDK_VISUAL_TRUE_COLOR) + /* + * Preserve the old rule: DirectColor may share ordinary scalar + * converters, but direct RGB48/RGBX64 kernels are TrueColor-only. + */ + if (image_info->visual->type != GDK_VISUAL_TRUE_COLOR) return NULL; - depth = visual->depth; - byte_order = visual->byte_order; - red_mask = visual->red_mask; - green_mask = visual->green_mask; - blue_mask = visual->blue_mask; - bits_per_pixel = image_info->bpp * 8; - -#if G_BYTE_ORDER == G_BIG_ENDIAN - byte_reversed = byte_order == GDK_LSB_FIRST; -#else - byte_reversed = byte_order == GDK_MSB_FIRST; -#endif - - mask_rgb = red_mask == 0xff0000 && - green_mask == 0x00ff00 && - blue_mask == 0x0000ff; - - mask_bgr = red_mask == 0x0000ff && - green_mask == 0x00ff00 && - blue_mask == 0xff0000; - - if (bits_per_pixel == 16 && depth == 16 && - red_mask == 0xf800 && green_mask == 0x07e0 && - blue_mask == 0x001f) + switch ((GdkRgbFormat) image_info->format) { - if (byte_reversed) - SELECT_PAIR (565_br); + case GDK_RGB_FORMAT_565: SELECT_PAIR (565); - } - if (bits_per_pixel == 16 && depth == 15 && - red_mask == 0x7c00 && green_mask == 0x03e0 && - blue_mask == 0x001f) - { - if (byte_reversed) - SELECT_PAIR (555_br); + case GDK_RGB_FORMAT_565_BR: + SELECT_PAIR (565_br); + + case GDK_RGB_FORMAT_555: SELECT_PAIR (555); + + case GDK_RGB_FORMAT_555_BR: + SELECT_PAIR (555_br); + + case GDK_RGB_FORMAT_888_LSB: + SELECT_PAIR (bgr); + + case GDK_RGB_FORMAT_888_MSB: + SELECT_PAIR (rgb); + + case GDK_RGB_FORMAT_0888: + SELECT_PAIR (bgra); + + case GDK_RGB_FORMAT_0888_BR: + SELECT_PAIR (argb); + + case GDK_RGB_FORMAT_RGB0_NATIVE: + SELECT_PAIR (rgb0_native); + + case GDK_RGB_FORMAT_101010_R_LOW_LE: + SELECT_PAIR (101010_r_low_le); + + case GDK_RGB_FORMAT_101010_B_LOW_LE: + SELECT_PAIR (101010_b_low_le); + + case GDK_RGB_FORMAT_101010_R_LOW_BE: + SELECT_PAIR (101010_r_low_be); + + case GDK_RGB_FORMAT_101010_B_LOW_BE: + SELECT_PAIR (101010_b_low_be); + + default: + break; } - if (bits_per_pixel == 24 && depth == 24 && - ((mask_rgb && byte_order == GDK_LSB_FIRST) || - (mask_bgr && byte_order == GDK_MSB_FIRST))) - SELECT_PAIR (bgr); - - if (bits_per_pixel == 24 && depth == 24 && - ((mask_rgb && byte_order == GDK_MSB_FIRST) || - (mask_bgr && byte_order == GDK_LSB_FIRST))) - SELECT_PAIR (rgb); - - if (bits_per_pixel == 32 && - (depth == 24 || depth == 32) && - mask_rgb && byte_order == GDK_MSB_FIRST) - SELECT_PAIR (argb); - - if (bits_per_pixel == 32 && - (depth == 24 || depth == 32) && - mask_rgb && byte_order == GDK_LSB_FIRST) - SELECT_PAIR (bgra); - -#if G_BYTE_ORDER == G_BIG_ENDIAN - if (bits_per_pixel == 32 && depth == 24 && - mask_bgr && byte_order == GDK_MSB_FIRST) - SELECT_PAIR (rgb0_native); -#else - if (bits_per_pixel == 32 && depth == 24 && - mask_bgr && byte_order == GDK_LSB_FIRST) - SELECT_PAIR (rgb0_native); -#endif - - if (bits_per_pixel == 32 && - (depth == 30 || depth == 32) && - byte_order == GDK_LSB_FIRST && - red_mask == 0x000003ff && - green_mask == 0x000ffc00 && - blue_mask == 0x3ff00000) - SELECT_PAIR (101010_r_low_le); - - if (bits_per_pixel == 32 && - (depth == 30 || depth == 32) && - byte_order == GDK_LSB_FIRST && - red_mask == 0x3ff00000 && - green_mask == 0x000ffc00 && - blue_mask == 0x000003ff) - SELECT_PAIR (101010_b_low_le); - - if (bits_per_pixel == 32 && - (depth == 30 || depth == 32) && - byte_order == GDK_MSB_FIRST && - red_mask == 0x000003ff && - green_mask == 0x000ffc00 && - blue_mask == 0x3ff00000) - SELECT_PAIR (101010_r_low_be); - - if (bits_per_pixel == 32 && - (depth == 30 || depth == 32) && - byte_order == GDK_MSB_FIRST && - red_mask == 0x3ff00000 && - green_mask == 0x000ffc00 && - blue_mask == 0x000003ff) - SELECT_PAIR (101010_b_low_be); - #undef SELECT_PAIR + return NULL; } @@ -4838,6 +4875,7 @@ gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, cmap_info = tmp_list->data; if (cmap_info->image_info == image_info) return cmap_info; + tmp_list = tmp_list->next; } cmap_info = g_new (GdkRgbCmapInfo, 1); @@ -4944,13 +4982,19 @@ gdk_draw_indexed_image (GdkDrawable *drawable, if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL && !image_info->dith_default)) - gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height, - buf, 1, rowstride, - image_info->conv_indexed, cmap, 0, 0); + { + GdkRgbConvFunc conv = image_info->conv_indexed; + + if (image_info->bpp == 2 && width * height <= 64) + conv = gdk_rgb_convert_indexed_generic; + + gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height, + buf, 1, rowstride, conv, cmap, 0, 0); + } else gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height, buf, 1, rowstride, - image_info->conv_indexed_d, cmap, 0, 0); + gdk_rgb_convert_indexed_generic_d, cmap, 0, 0); } gboolean From 6cfb02aebaf82a49912295b7c1796d59fc282969 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Thu, 27 Aug 2026 10:42:59 +0200 Subject: [PATCH 62/68] gdk: add MMX pixel acceleration --- gdk/Makefile.am | 27 --- gdk/gdkdraw.c | 12 ++ gdk/gdkpixelops-x86-mmx.c | 402 +++++++++++++++++--------------------- gdk/gdkpixelops-x86.c | 88 ++------- gdk/gdkpixelops.c | 122 +++++++----- gdk/gdkpixelops.h | 1 + 6 files changed, 280 insertions(+), 372 deletions(-) diff --git a/gdk/Makefile.am b/gdk/Makefile.am index c62f07bca..1370e1a28 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -134,33 +134,6 @@ libgdk_pixelops_x86_mmx_la_SOURCES = gdkpixelops-x86-mmx.c libgdk_pixelops_x86_mmx_la_CFLAGS = \ $(AM_CFLAGS) \ -mmmx -mno-sse -mno-sse2 -mno-3dnow - -noinst_LTLIBRARIES += libgdk-pixelops-x86-sse.la -gdk_pixelops_arch_libs += libgdk-pixelops-x86-sse.la - -libgdk_pixelops_x86_sse_la_SOURCES = gdkpixelops-x86-mmx.c -libgdk_pixelops_x86_sse_la_CFLAGS = \ - $(AM_CFLAGS) \ - -mmmx -mno-sse -mno-sse2 -mno-3dnow \ - -DGDK_PIXELOPS_X86_SSE_IMPL - -noinst_LTLIBRARIES += libgdk-pixelops-x86-sse2.la -gdk_pixelops_arch_libs += libgdk-pixelops-x86-sse2.la - -libgdk_pixelops_x86_sse2_la_SOURCES = gdkpixelops-x86-sse2.c -libgdk_pixelops_x86_sse2_la_CFLAGS = \ - $(AM_CFLAGS) \ - -march=i686 -msse2 -mno-sse3 -mno-ssse3 \ - -mno-sse4 -mno-avx -mno-3dnow -mtune=pentium4 - -noinst_LTLIBRARIES += libgdk-pixelops-x86-3dnow.la -gdk_pixelops_arch_libs += libgdk-pixelops-x86-3dnow.la - -libgdk_pixelops_x86_3dnow_la_SOURCES = gdkpixelops-x86-mmx.c -libgdk_pixelops_x86_3dnow_la_CFLAGS = \ - $(AM_CFLAGS) \ - -mmmx -m3dnow -mno-sse -mno-sse2 \ - -DGDK_PIXELOPS_X86_3DNOW_IMPL endif if GDK_PIXELOPS_X86_64 diff --git a/gdk/gdkdraw.c b/gdk/gdkdraw.c index 7efe560b4..46e7132b5 100644 --- a/gdk/gdkdraw.c +++ b/gdk/gdkdraw.c @@ -1710,6 +1710,18 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable, composite_func = _gdk_pixel_ops.composite_565; goto composite_selected; } + else if (visual->byte_order == + (G_BYTE_ORDER == G_BIG_ENDIAN ? + GDK_MSB_FIRST : GDK_LSB_FIRST) && + visual->depth == 15 && + bits_per_pixel == 16 && + visual->red_mask == 0x7c00 && + visual->green_mask == 0x03e0 && + visual->blue_mask == 0x001f) + { + composite_func = _gdk_pixel_ops.composite_555; + goto composite_selected; + } #ifdef GDK_WINDOWING_X11 else if (visual->type == GDK_VISUAL_TRUE_COLOR && visual->byte_order == diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c index bf7cdd8b2..1d620afd0 100644 --- a/gdk/gdkpixelops-x86-mmx.c +++ b/gdk/gdkpixelops-x86-mmx.c @@ -8,21 +8,6 @@ #include #include -#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL -#include -#define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_3dnow -#define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_3dnow -#define GDK_PIXELOPS_MMX_EMPTY() _m_femms () -#elif defined(GDK_PIXELOPS_X86_SSE_IMPL) -#define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_sse -#define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_sse -#define GDK_PIXELOPS_MMX_EMPTY() _mm_empty () -#else -#define GDK_PIXELOPS_COMPOSITE_0888 _gdk_pixel_ops_composite_0888_mmx -#define GDK_PIXELOPS_COMPOSITE_565 _gdk_pixel_ops_composite_565_mmx -#define GDK_PIXELOPS_MMX_EMPTY() _mm_empty () -#endif - #include "gdkpixelops.h" #ifndef __i386__ @@ -38,37 +23,27 @@ _gdk_pixel_ops_composite_0888_generic (guchar *src_buf, gint width, gint height); -#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL static __inline__ __m64 -blend_3dnow (__m64 s, - __m64 d, - __m64 a) +blend_words_mmx (__m64 s, + __m64 d, + __m64 a, + __m64 ff, + __m64 bias) { - __m64 scaled; - __m64 mask; - __m64 coeff; - __m64 delta; - __m64 correction; __m64 t; - scaled = _mm_slli_pi16 (a, 8); - mask = _mm_srai_pi16 (scaled, 15); + t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), + _mm_mullo_pi16 (d, + _mm_sub_pi16 (ff, a))); - coeff = _mm_add_pi16 (a, scaled); - coeff = _mm_sub_pi16 (coeff, mask); + t = _mm_add_pi16 (t, bias); + t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); - delta = _mm_sub_pi16 (s, d); - correction = _mm_and_si64 (delta, mask); - - t = _m_pmulhrw (delta, coeff); - t = _mm_add_pi16 (t, correction); - - return _mm_add_pi16 (t, d); + return _mm_srli_pi16 (t, 8); } -#endif G_GNUC_INTERNAL void -GDK_PIXELOPS_COMPOSITE_0888 (guchar *src_buf, +_gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, gint src_rowstride, guchar *dest_buf, gint dest_rowstride, @@ -76,6 +51,14 @@ GDK_PIXELOPS_COMPOSITE_0888 (guchar *src_buf, gint width, gint height) { + const __m64 zero = _mm_setzero_si64 (); + const __m64 ones = _mm_cmpeq_pi16 (zero, zero); + const __m64 ff = _mm_srli_pi16 (ones, 8); + const __m64 bias = _mm_set1_pi16 (0x80); + const __m64 rgb_mask = _mm_srli_si64 (ones, 16); + guchar *src = src_buf; + guchar *dest = dest_buf; + if (dest_byte_order != GDK_LSB_FIRST) { _gdk_pixel_ops_composite_0888_generic (src_buf, @@ -88,101 +71,71 @@ GDK_PIXELOPS_COMPOSITE_0888 (guchar *src_buf, return; } - { - const __m64 zero = _mm_setzero_si64 (); - const __m64 ones = _mm_cmpeq_pi16 (zero, zero); -#ifndef GDK_PIXELOPS_X86_3DNOW_IMPL - const __m64 ff = _mm_srli_pi16 (ones, 8); - const __m64 bias = _mm_set1_pi16 (0x80); -#endif - const __m64 rgb_mask = _mm_srli_si64 (ones, 16); - guchar *src = src_buf; - guchar *dest = dest_buf; + while (height--) + { + gint twidth = width; + guchar *p = src; + guchar *q = dest; - while (height--) - { - gint twidth = width; - guchar *p = src; - guchar *q = dest; + while (twidth--) + { + guint32 sp; + guint32 dp; + guint32 result; + __m64 s; + __m64 d; + __m64 a; + __m64 t; - while (twidth--) - { - guint32 sp; - guint32 dp; - guint32 result; - __m64 s; - __m64 d; - __m64 a; -#ifndef GDK_PIXELOPS_X86_3DNOW_IMPL - __m64 ia; -#endif - __m64 t; - __m64 packed; + memcpy (&sp, p, sizeof sp); + memcpy (&dp, q, sizeof dp); - memcpy (&sp, p, sizeof sp); - memcpy (&dp, q, sizeof dp); + sp = ((sp & 0xff00ff00U) | + ((sp & 0x000000ffU) << 16) | + ((sp & 0x00ff0000U) >> 16)); - sp = ((sp & 0xff00ff00U) | - ((sp & 0x000000ffU) << 16) | - ((sp & 0x00ff0000U) >> 16)); + s = _mm_cvtsi32_si64 ((int) sp); + d = _mm_cvtsi32_si64 ((int) dp); - s = _mm_cvtsi32_si64 ((int) sp); - d = _mm_cvtsi32_si64 ((int) dp); + s = _mm_unpacklo_pi8 (s, zero); + d = _mm_unpacklo_pi8 (d, zero); - s = _mm_unpacklo_pi8 (s, zero); - d = _mm_unpacklo_pi8 (d, zero); + a = _mm_srli_si64 (s, 48); + a = _mm_unpacklo_pi16 (a, a); + a = _mm_unpacklo_pi32 (a, a); + a = _mm_and_si64 (a, rgb_mask); - a = _mm_srli_si64 (s, 48); - #ifdef GDK_PIXELOPS_X86_SSE_IMPL - __asm__ ("pshufw $0x00,%0,%0" - : "+y" (a)); - #else - a = _mm_unpacklo_pi16 (a, a); - a = _mm_unpacklo_pi32 (a, a); - #endif - a = _mm_and_si64 (a, rgb_mask); + t = blend_words_mmx (s, d, a, ff, bias); + t = _mm_packs_pu16 (t, t); -#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL - t = blend_3dnow (s, d, a); -#else - ia = _mm_sub_pi16 (ff, a); + result = (guint32) _mm_cvtsi64_si32 (t); + memcpy (q, &result, sizeof result); - t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), - _mm_mullo_pi16 (d, ia)); + p += 4; + q += 4; + } - t = _mm_add_pi16 (t, bias); - #ifdef GDK_PIXELOPS_X86_SSE_IMPL - { - __m64 scale = _mm_set1_pi16 (0x0101); + src += src_rowstride; + dest += dest_rowstride; + } - __asm__ ("pmulhuw %1,%0" - : "+y" (t) - : "y" (scale)); - } - #else - t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); - t = _mm_srli_pi16 (t, 8); - #endif -#endif - - packed = _mm_packs_pu16 (t, t); - result = (guint32) _mm_cvtsi64_si32 (packed); - - memcpy (q, &result, sizeof result); - - p += 4; - q += 4; - } - - src += src_rowstride; - dest += dest_rowstride; - } - - GDK_PIXELOPS_MMX_EMPTY (); - } + _mm_empty (); } -static guint32 +static __inline__ guint32 +expand_555 (guint16 value) +{ + guint32 hi; + + hi = (((guint32) value & 0x7c00U) >> 7) | + (((guint32) value & 0x03e0U) << 6) | + (((guint32) value & 0x001fU) << 19); + + return hi | + ((hi >> 5) & 0x00070707U); +} + +static __inline__ guint32 expand_565 (guint16 value) { guint32 hi; @@ -196,7 +149,16 @@ expand_565 (guint16 value) ((hi >> 6) & 0x00000300U); } -static guint16 +static __inline__ guint16 +pack_555 (guint32 rgb) +{ + return (guint16) + (((rgb & 0x000000f8U) << 7) | + ((rgb & 0x0000f800U) >> 6) | + ((rgb & 0x00f80000U) >> 19)); +} + +static __inline__ guint16 pack_565 (guint32 rgb) { return (guint16) @@ -205,19 +167,17 @@ pack_565 (guint32 rgb) ((rgb & 0x00f80000U) >> 19)); } -static guint32 -blend_565_mmx (__m64 zero, - __m64 ff, - __m64 bias, - guint32 src, - guint32 dest) +static __inline__ guint32 +blend_rgb16_mmx (__m64 zero, + __m64 ff, + __m64 bias, + guint32 src, + guint32 dest) { __m64 s; __m64 d; __m64 a; -#ifndef GDK_PIXELOPS_X86_3DNOW_IMPL __m64 ia; -#endif __m64 t; guint32 result; @@ -228,36 +188,17 @@ blend_565_mmx (__m64 zero, d = _mm_unpacklo_pi8 (d, zero); a = _mm_srli_si64 (s, 48); - #ifdef GDK_PIXELOPS_X86_SSE_IMPL - __asm__ ("pshufw $0x00,%0,%0" - : "+y" (a)); - #else a = _mm_unpacklo_pi16 (a, a); a = _mm_unpacklo_pi32 (a, a); - #endif -#ifdef GDK_PIXELOPS_X86_3DNOW_IMPL - t = blend_3dnow (s, d, a); -#else ia = _mm_sub_pi16 (ff, a); t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), _mm_mullo_pi16 (d, ia)); t = _mm_add_pi16 (t, bias); - #ifdef GDK_PIXELOPS_X86_SSE_IMPL - { - __m64 scale = _mm_set1_pi16 (0x0101); - - __asm__ ("pmulhuw %1,%0" - : "+y" (t) - : "y" (scale)); - } - #else t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); t = _mm_srli_pi16 (t, 8); - #endif -#endif t = _mm_packs_pu16 (t, t); __asm__ ("movd %1,%0" @@ -267,81 +208,94 @@ blend_565_mmx (__m64 zero, return result; } -G_GNUC_INTERNAL void -GDK_PIXELOPS_COMPOSITE_565 (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - const __m64 zero = _mm_setzero_si64 (); - const __m64 ones = _mm_cmpeq_pi16 (zero, zero); - const __m64 ff = _mm_srli_pi16 (ones, 8); - const __m64 bias = _mm_set1_pi16 (0x80); - guchar *src = src_buf; - guchar *dest = dest_buf; - - (void) dest_byte_order; - - while (height--) - { - gint pairs = width >> 1; - guchar *p = src; - guchar *q = dest; - - while (pairs--) - { - guint32 sp0; - guint32 sp1; - guint32 dpair; - guint32 dp0; - guint32 dp1; - guint32 rgb0; - guint32 rgb1; - guint32 result; - - memcpy (&sp0, p, sizeof sp0); - memcpy (&sp1, p + 4, sizeof sp1); - memcpy (&dpair, q, sizeof dpair); - - dp0 = expand_565 ((guint16) dpair); - dp1 = expand_565 ((guint16) (dpair >> 16)); - - rgb0 = blend_565_mmx (zero, ff, bias, sp0, dp0); - rgb1 = blend_565_mmx (zero, ff, bias, sp1, dp1); - - result = (guint32) pack_565 (rgb0); - result |= (guint32) pack_565 (rgb1) << 16; - - memcpy (q, &result, sizeof result); - - p += 8; - q += 4; - } - - if (width & 1) - { - guint32 sp; - guint16 dp16; - guint16 result; - guint32 dp; - guint32 rgb; - - memcpy (&sp, p, sizeof sp); - memcpy (&dp16, q, sizeof dp16); - - dp = expand_565 (dp16); - rgb = blend_565_mmx (zero, ff, bias, sp, dp); - result = pack_565 (rgb); - - memcpy (q, &result, sizeof result); - } - - src += src_rowstride; - dest += dest_rowstride; - } - - GDK_PIXELOPS_MMX_EMPTY (); +#define DEFINE_RGB16_MMX_COMPOSITOR(name, expand_pixel, pack_pixel) \ +G_GNUC_INTERNAL void \ +name (guchar *src_buf, \ + gint src_rowstride, \ + guchar *dest_buf, \ + gint dest_rowstride, \ + GdkByteOrder dest_byte_order, \ + gint width, \ + gint height) \ +{ \ + const __m64 zero = _mm_setzero_si64 (); \ + const __m64 ones = _mm_cmpeq_pi16 (zero, zero); \ + const __m64 ff = _mm_srli_pi16 (ones, 8); \ + const __m64 bias = _mm_set1_pi16 (0x80); \ + guchar *src = src_buf; \ + guchar *dest = dest_buf; \ + \ + (void) dest_byte_order; \ + \ + while (height--) \ + { \ + gint pairs = width >> 1; \ + guchar *p = src; \ + guchar *q = dest; \ + \ + while (pairs--) \ + { \ + guint32 sp0; \ + guint32 sp1; \ + guint32 dpair; \ + guint32 dp0; \ + guint32 dp1; \ + guint32 rgb0; \ + guint32 rgb1; \ + guint32 result; \ + \ + memcpy (&sp0, p, sizeof sp0); \ + memcpy (&sp1, p + 4, sizeof sp1); \ + memcpy (&dpair, q, sizeof dpair); \ + \ + dp0 = expand_pixel ((guint16) dpair); \ + dp1 = expand_pixel ((guint16) (dpair >> 16)); \ + \ + rgb0 = blend_rgb16_mmx (zero, ff, bias, sp0, dp0); \ + rgb1 = blend_rgb16_mmx (zero, ff, bias, sp1, dp1); \ + \ + result = (guint32) pack_pixel (rgb0); \ + result |= (guint32) pack_pixel (rgb1) << 16; \ + \ + memcpy (q, &result, sizeof result); \ + \ + p += 8; \ + q += 4; \ + } \ + \ + if (width & 1) \ + { \ + guint32 sp; \ + guint16 dp16; \ + guint16 result; \ + guint32 dp; \ + guint32 rgb; \ + \ + memcpy (&sp, p, sizeof sp); \ + memcpy (&dp16, q, sizeof dp16); \ + \ + dp = expand_pixel (dp16); \ + rgb = blend_rgb16_mmx (zero, ff, bias, sp, dp); \ + result = pack_pixel (rgb); \ + \ + memcpy (q, &result, sizeof result); \ + } \ + \ + src += src_rowstride; \ + dest += dest_rowstride; \ + } \ + \ + _mm_empty (); \ } + +DEFINE_RGB16_MMX_COMPOSITOR ( + _gdk_pixel_ops_composite_555_mmx, + expand_555, + pack_555) + +DEFINE_RGB16_MMX_COMPOSITOR ( + _gdk_pixel_ops_composite_565_mmx, + expand_565, + pack_565) + +#undef DEFINE_RGB16_MMX_COMPOSITOR diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 77c44ed7e..7c081a951 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -1,20 +1,16 @@ /* GDK - The GIMP Drawing Kit * * x86 pixel operation backend. - * - * This file is selected at build time only for x86 targets. */ #include "config.h" #include "gdkpixelops.h" -#if !defined(__i386__) && !defined(__x86_64__) && \ - !defined(_M_IX86) && !defined(_M_X64) -#error "gdkpixelops-x86.c must only be built for an x86 target" +#ifndef __i386__ +#error "gdkpixelops-x86.c requires a 32-bit x86 target" #endif - #include G_GNUC_INTERNAL void @@ -26,6 +22,15 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, gint width, gint height); +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_555_mmx (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + G_GNUC_INTERNAL void _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, gint src_rowstride, @@ -35,51 +40,6 @@ _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, gint width, gint height); -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_0888_sse (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); - -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_565_sse (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); - -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_0888_3dnow (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); - -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_565_3dnow (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); - -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_0888_sse2 (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height); - G_GNUC_INTERNAL void _gdk_pixel_ops_init (void) { @@ -87,37 +47,15 @@ _gdk_pixel_ops_init (void) unsigned int ebx; unsigned int ecx; unsigned int edx; - unsigned int standard_edx; if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) && (edx & bit_MMX) != 0) { - standard_edx = edx; - _gdk_pixel_ops.composite_0888 = _gdk_pixel_ops_composite_0888_mmx; + _gdk_pixel_ops.composite_555 = + _gdk_pixel_ops_composite_555_mmx; _gdk_pixel_ops.composite_565 = _gdk_pixel_ops_composite_565_mmx; - - if ((edx & bit_SSE) != 0) - { - _gdk_pixel_ops.composite_0888 = - _gdk_pixel_ops_composite_0888_sse; - _gdk_pixel_ops.composite_565 = - _gdk_pixel_ops_composite_565_sse; - } - - if (__get_cpuid (0x80000001U, &eax, &ebx, &ecx, &edx) && - (edx & bit_3DNOW) != 0) - { - _gdk_pixel_ops.composite_0888 = - _gdk_pixel_ops_composite_0888_3dnow; - _gdk_pixel_ops.composite_565 = - _gdk_pixel_ops_composite_565_3dnow; - } - - if ((standard_edx & bit_SSE2) != 0) - _gdk_pixel_ops.composite_0888 = - _gdk_pixel_ops_composite_0888_sse2; } } diff --git a/gdk/gdkpixelops.c b/gdk/gdkpixelops.c index c9d16da2a..c1155ecac 100644 --- a/gdk/gdkpixelops.c +++ b/gdk/gdkpixelops.c @@ -87,59 +87,89 @@ _gdk_pixel_ops_composite_0888_generic (guchar *src_buf, } } -G_GNUC_INTERNAL void -_gdk_pixel_ops_composite_565_generic (guchar *src_buf, - gint src_rowstride, - guchar *dest_buf, - gint dest_rowstride, - GdkByteOrder dest_byte_order, - gint width, - gint height) -{ - guchar *src = src_buf; - guchar *dest = dest_buf; - - while (height--) - { - gint twidth = width; - guchar *p = src; - gushort *q = (gushort *)dest; - - while (twidth--) - { - guchar a = p[3]; - guint tr, tg, tb; - guint tr1, tg1, tb1; - guint tmp = *q; - - /* This is fast, and corresponds to what composite() above does - * if we converted to 8-bit first. - */ - tr = (tmp & 0xf800); - tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80; - tg = (tmp & 0x07e0); - tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80; - tb = (tmp & 0x001f); - tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; - - *q = (((tr1 + (tr1 >> 8)) & 0xf800) | - (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) | - ((tb1 + (tb1 >> 8)) >> 11)); - - p += 4; - q++; - } - - src += src_rowstride; - dest += dest_rowstride; - } +#define DEFINE_RGB16_COMPOSITOR(name, \ + red_mask, green_mask, \ + red_expand, green_expand, \ + red_pack, green_pack) \ +G_GNUC_INTERNAL void \ +name (guchar *src_buf, \ + gint src_rowstride, \ + guchar *dest_buf, \ + gint dest_rowstride, \ + GdkByteOrder dest_byte_order, \ + gint width, \ + gint height) \ +{ \ + guchar *src = src_buf; \ + guchar *dest = dest_buf; \ + \ + (void) dest_byte_order; \ + \ + while (height--) \ + { \ + gint twidth = width; \ + guchar *p = src; \ + gushort *q = (gushort *) dest; \ + \ + while (twidth--) \ + { \ + guint a = p[3]; \ + guint tr; \ + guint tg; \ + guint tb; \ + guint tr1; \ + guint tg1; \ + guint tb1; \ + guint tmp = *q; \ + \ + tr = tmp & (red_mask); \ + tg = tmp & (green_mask); \ + tb = tmp & 0x001f; \ + \ + tr1 = a * p[0] + \ + (255 - a) * (red_expand) + 0x80; \ + tg1 = a * p[1] + \ + (255 - a) * (green_expand) + 0x80; \ + tb1 = a * p[2] + \ + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80; \ + \ + *q = (gushort) ((red_pack) | \ + (green_pack) | \ + ((tb1 + (tb1 >> 8)) >> 11)); \ + \ + p += 4; \ + q++; \ + } \ + \ + src += src_rowstride; \ + dest += dest_rowstride; \ + } \ } +DEFINE_RGB16_COMPOSITOR ( + _gdk_pixel_ops_composite_555_generic, + 0x7c00, 0x03e0, + ((tr >> 7) + (tr >> 12)), + ((tg >> 2) + (tg >> 7)), + (((tr1 + (tr1 >> 8)) & 0xf800) >> 1), + (((tg1 + (tg1 >> 8)) & 0xf800) >> 6)) + +DEFINE_RGB16_COMPOSITOR ( + _gdk_pixel_ops_composite_565_generic, + 0xf800, 0x07e0, + ((tr >> 8) + (tr >> 13)), + ((tg >> 3) + (tg >> 9)), + ((tr1 + (tr1 >> 8)) & 0xf800), + (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5)) + +#undef DEFINE_RGB16_COMPOSITOR + G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops = { #ifdef GDK_PIXELOPS_X86_64 _gdk_pixel_ops_composite_0888_sse2, #else _gdk_pixel_ops_composite_0888_generic, #endif + _gdk_pixel_ops_composite_555_generic, _gdk_pixel_ops_composite_565_generic }; diff --git a/gdk/gdkpixelops.h b/gdk/gdkpixelops.h index c070e6a25..82c3b79af 100644 --- a/gdk/gdkpixelops.h +++ b/gdk/gdkpixelops.h @@ -21,6 +21,7 @@ typedef void (*GdkCompositeFunc) (guchar *src_buf, typedef struct { GdkCompositeFunc composite_0888; + GdkCompositeFunc composite_555; GdkCompositeFunc composite_565; } GdkPixelOps; From 513338248db4c8771a4cf89f9e7b0be26f2da915 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 1 Sep 2026 14:32:14 +0200 Subject: [PATCH 63/68] gdk: canonicalize RGB destination formats --- gdk/gdkrgb.c | 83 ++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 0889e0164..67d3ffc11 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -57,11 +57,11 @@ typedef enum GDK_RGB_FORMAT_888_MSB, GDK_RGB_FORMAT_0888, GDK_RGB_FORMAT_0888_BR, - GDK_RGB_FORMAT_RGB0_NATIVE, - GDK_RGB_FORMAT_101010_R_LOW_LE, - GDK_RGB_FORMAT_101010_B_LOW_LE, - GDK_RGB_FORMAT_101010_R_LOW_BE, - GDK_RGB_FORMAT_101010_B_LOW_BE + GDK_RGB_FORMAT_8880_BR, + GDK_RGB_FORMAT_101010_R_LOW_LSB, + GDK_RGB_FORMAT_101010_B_LOW_LSB, + GDK_RGB_FORMAT_101010_R_LOW_MSB, + GDK_RGB_FORMAT_101010_B_LOW_MSB } GdkRgbFormat; typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image, @@ -3724,7 +3724,8 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) if (image_info->bitmap) conv = gdk_rgb_convert_1; - else if (bpp == 16 && depth == 16 && !byterev && + else if (bpp == 16 && depth == 16 && + vtype == GDK_VISUAL_TRUE_COLOR && !byterev && red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f) { SET_CONV (gdk_rgb_convert_565, GDK_RGB_FORMAT_565); @@ -3779,11 +3780,11 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) #if G_BYTE_ORDER == G_BIG_ENDIAN else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && (mask_bgr && byte_order == GDK_MSB_FIRST)) - SET_CONV (gdk_rgb_convert_8880_br, GDK_RGB_FORMAT_RGB0_NATIVE); + SET_CONV (gdk_rgb_convert_8880_br, GDK_RGB_FORMAT_8880_BR); #else else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && (mask_bgr && byte_order == GDK_LSB_FIRST)) - SET_CONV (gdk_rgb_convert_8880_br, GDK_RGB_FORMAT_RGB0_NATIVE); + SET_CONV (gdk_rgb_convert_8880_br, GDK_RGB_FORMAT_8880_BR); #endif else if (bpp == 32 && (depth == 30 || depth == 32) && vtype == GDK_VISUAL_TRUE_COLOR && @@ -3798,15 +3799,15 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) ? gdk_rgb_convert_101010_r_low_lsb : gdk_rgb_convert_101010_r_low_msb), (byte_order == GDK_LSB_FIRST - ? GDK_RGB_FORMAT_101010_R_LOW_LE - : GDK_RGB_FORMAT_101010_R_LOW_BE)); + ? GDK_RGB_FORMAT_101010_R_LOW_LSB + : GDK_RGB_FORMAT_101010_R_LOW_MSB)); else SET_CONV ((byte_order == GDK_LSB_FIRST ? gdk_rgb_convert_101010_b_low_lsb : gdk_rgb_convert_101010_b_low_msb), (byte_order == GDK_LSB_FIRST - ? GDK_RGB_FORMAT_101010_B_LOW_LE - : GDK_RGB_FORMAT_101010_B_LOW_BE)); + ? GDK_RGB_FORMAT_101010_B_LOW_LSB + : GDK_RGB_FORMAT_101010_B_LOW_MSB)); } else if (vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_LSB_FIRST) @@ -4073,7 +4074,7 @@ gdk_rgb_get_info_from_drawable (GdkDrawable *drawable) (p) += 4; \ } G_STMT_END -#define GDK_RGB16_STORE_RGB0_NATIVE(p, r, g, b, a) G_STMT_START { \ +#define GDK_RGB16_STORE_8880_BR(p, r, g, b, a) G_STMT_START { \ guint32 pixel__ = (((b) >> 8) << 16) | \ (((g) >> 8) << 8) | \ ((r) >> 8); \ @@ -4115,7 +4116,7 @@ gdk_rgb_get_info_from_drawable (GdkDrawable *drawable) (p) += 2; \ } G_STMT_END -#define GDK_RGB16_STORE_101010_R_LOW_LE(p, r, g, b, a) \ +#define GDK_RGB16_STORE_101010_R_LOW_LSB(p, r, g, b, a) \ G_STMT_START { \ guint32 pixel__ = (a) | \ (((b) >> 6) << 20) | \ @@ -4126,7 +4127,7 @@ G_STMT_START { \ (p) += 4; \ } G_STMT_END -#define GDK_RGB16_STORE_101010_B_LOW_LE(p, r, g, b, a) \ +#define GDK_RGB16_STORE_101010_B_LOW_LSB(p, r, g, b, a) \ G_STMT_START { \ guint32 pixel__ = (a) | \ (((r) >> 6) << 20) | \ @@ -4137,7 +4138,7 @@ G_STMT_START { \ (p) += 4; \ } G_STMT_END -#define GDK_RGB16_STORE_101010_R_LOW_BE(p, r, g, b, a) \ +#define GDK_RGB16_STORE_101010_R_LOW_MSB(p, r, g, b, a) \ G_STMT_START { \ guint32 pixel__ = (a) | \ (((b) >> 6) << 20) | \ @@ -4148,7 +4149,7 @@ G_STMT_START { \ (p) += 4; \ } G_STMT_END -#define GDK_RGB16_STORE_101010_B_LOW_BE(p, r, g, b, a) \ +#define GDK_RGB16_STORE_101010_B_LOW_MSB(p, r, g, b, a) \ G_STMT_START { \ guint32 pixel__ = (a) | \ (((r) >> 6) << 20) | \ @@ -4220,19 +4221,19 @@ DEFINE_GDK_RGB16_PAIR (rgb, 3, GDK_RGB16_STORE_RGB) DEFINE_GDK_RGB16_PAIR (bgr, 3, GDK_RGB16_STORE_BGR) DEFINE_GDK_RGB16_PAIR (bgra, 4, GDK_RGB16_STORE_BGRA) DEFINE_GDK_RGB16_PAIR (argb, 4, GDK_RGB16_STORE_ARGB) -DEFINE_GDK_RGB16_PAIR (rgb0_native, 4, GDK_RGB16_STORE_RGB0_NATIVE) +DEFINE_GDK_RGB16_PAIR (8880_br, 4, GDK_RGB16_STORE_8880_BR) DEFINE_GDK_RGB16_PAIR (565, 2, GDK_RGB16_STORE_565) DEFINE_GDK_RGB16_PAIR (565_br, 2, GDK_RGB16_STORE_565_BR) DEFINE_GDK_RGB16_PAIR (555, 2, GDK_RGB16_STORE_555) DEFINE_GDK_RGB16_PAIR (555_br, 2, GDK_RGB16_STORE_555_BR) -DEFINE_GDK_RGB16_PAIR (101010_r_low_le, 4, - GDK_RGB16_STORE_101010_R_LOW_LE) -DEFINE_GDK_RGB16_PAIR (101010_b_low_le, 4, - GDK_RGB16_STORE_101010_B_LOW_LE) -DEFINE_GDK_RGB16_PAIR (101010_r_low_be, 4, - GDK_RGB16_STORE_101010_R_LOW_BE) -DEFINE_GDK_RGB16_PAIR (101010_b_low_be, 4, - GDK_RGB16_STORE_101010_B_LOW_BE) +DEFINE_GDK_RGB16_PAIR (101010_r_low_lsb, 4, + GDK_RGB16_STORE_101010_R_LOW_LSB) +DEFINE_GDK_RGB16_PAIR (101010_b_low_lsb, 4, + GDK_RGB16_STORE_101010_B_LOW_LSB) +DEFINE_GDK_RGB16_PAIR (101010_r_low_msb, 4, + GDK_RGB16_STORE_101010_R_LOW_MSB) +DEFINE_GDK_RGB16_PAIR (101010_b_low_msb, 4, + GDK_RGB16_STORE_101010_B_LOW_MSB) #undef DEFINE_GDK_RGB16_PAIR #undef DEFINE_GDK_RGB16_CONVERTER @@ -4241,15 +4242,15 @@ DEFINE_GDK_RGB16_PAIR (101010_b_low_be, 4, #undef GDK_RGB16_STORE_BGR #undef GDK_RGB16_STORE_BGRA #undef GDK_RGB16_STORE_ARGB -#undef GDK_RGB16_STORE_RGB0_NATIVE +#undef GDK_RGB16_STORE_8880_BR #undef GDK_RGB16_STORE_565 #undef GDK_RGB16_STORE_565_BR #undef GDK_RGB16_STORE_555 #undef GDK_RGB16_STORE_555_BR -#undef GDK_RGB16_STORE_101010_R_LOW_LE -#undef GDK_RGB16_STORE_101010_B_LOW_LE -#undef GDK_RGB16_STORE_101010_R_LOW_BE -#undef GDK_RGB16_STORE_101010_B_LOW_BE +#undef GDK_RGB16_STORE_101010_R_LOW_LSB +#undef GDK_RGB16_STORE_101010_B_LOW_LSB +#undef GDK_RGB16_STORE_101010_R_LOW_MSB +#undef GDK_RGB16_STORE_101010_B_LOW_MSB static void @@ -4416,20 +4417,20 @@ gdk_rgb16_select_converter (GdkRgbInfo *image_info, case GDK_RGB_FORMAT_0888_BR: SELECT_PAIR (argb); - case GDK_RGB_FORMAT_RGB0_NATIVE: - SELECT_PAIR (rgb0_native); + case GDK_RGB_FORMAT_8880_BR: + SELECT_PAIR (8880_br); - case GDK_RGB_FORMAT_101010_R_LOW_LE: - SELECT_PAIR (101010_r_low_le); + case GDK_RGB_FORMAT_101010_R_LOW_LSB: + SELECT_PAIR (101010_r_low_lsb); - case GDK_RGB_FORMAT_101010_B_LOW_LE: - SELECT_PAIR (101010_b_low_le); + case GDK_RGB_FORMAT_101010_B_LOW_LSB: + SELECT_PAIR (101010_b_low_lsb); - case GDK_RGB_FORMAT_101010_R_LOW_BE: - SELECT_PAIR (101010_r_low_be); + case GDK_RGB_FORMAT_101010_R_LOW_MSB: + SELECT_PAIR (101010_r_low_msb); - case GDK_RGB_FORMAT_101010_B_LOW_BE: - SELECT_PAIR (101010_b_low_be); + case GDK_RGB_FORMAT_101010_B_LOW_MSB: + SELECT_PAIR (101010_b_low_msb); default: break; From 57f63a8714e784c97926f54b6b1940b585809c4e Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 1 Sep 2026 14:42:26 +0200 Subject: [PATCH 64/68] gdk: use portable wide stores for 0888 conversion --- gdk/gdkrgb.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index 67d3ffc11..ee7a3d9ce 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -2279,24 +2279,13 @@ gdk_rgb_convert_888_lsb (GdkRgbInfo *image_info, GdkImage *image, /* convert 24-bit packed to 32-bit unpacked */ /* todo: optimize this */ -#if G_BYTE_ORDER == G_LITTLE_ENDIAN && GLIB_SIZEOF_VOID_P == 8 && \ - (defined(__x86_64__) || defined(__amd64__)) #define GDK_RGB_STORE_0888(p, bp) G_STMT_START { \ - guint32 pixel; \ - pixel = 0xff000000U | \ - ((guint32) (bp)[0] << 16) | \ - ((guint32) (bp)[1] << 8) | \ - (guint32) (bp)[2]; \ + guint32 pixel = GUINT32_TO_LE (0xff000000U | \ + ((guint32) (bp)[0] << 16) | \ + ((guint32) (bp)[1] << 8) | \ + (guint32) (bp)[2]); \ memcpy ((p), &pixel, sizeof (pixel)); \ } G_STMT_END -#else -#define GDK_RGB_STORE_0888(p, bp) G_STMT_START { \ - (p)[0] = (bp)[2]; \ - (p)[1] = (bp)[1]; \ - (p)[2] = (bp)[0]; \ - (p)[3] = 0xff; \ -} G_STMT_END -#endif #define GDK_RGB_STORE_0888_BR(p, bp) G_STMT_START { \ (p)[0] = 0xff; \ From e947badaa4292ef1df310e90ce858fb23b118afd Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 1 Sep 2026 15:04:49 +0200 Subject: [PATCH 65/68] gdk: add direct gray format converters --- gdk/gdkrgb.c | 224 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 174 insertions(+), 50 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index ee7a3d9ce..f70950cd2 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -3542,50 +3542,145 @@ gdk_rgb_gray_to_stage (guchar *stage, /* Generic gray conversion function - convert to 24bit packed, then go from there. */ -static void -gdk_rgb_convert_gray_0888_native (GdkRgbInfo *image_info, GdkImage *image, - gint x0, gint y0, gint width, gint height, - const guchar *buf, gint rowstride, - gint x_align, gint y_align, GdkRgbCmap *cmap) -{ - gint y, w; - gint bpl = image->bpl; - guchar *obuf = ((guchar *) image->mem) + y0 * bpl + x0 * 4; - - for (y = 0; y < height; y++) - { - const guchar *p = buf; - guchar *q = obuf; - - w = width; - while (w--) - { - guchar g = *p++; - - /* - * Store bytes explicitly here. Word-store performance varies by - * microarchitecture even within one ISA, so keep this generic path - * free of CPU-specific runtime dispatch. - */ -#if G_BYTE_ORDER == G_LITTLE_ENDIAN - q[0] = g; - q[1] = g; - q[2] = g; - q[3] = 0xff; -#else - q[0] = 0xff; - q[1] = g; - q[2] = g; - q[3] = g; -#endif - q += 4; - } - - buf += rowstride; - obuf += bpl; - } +#define DEFINE_GDK_RGB_GRAY_DIRECT(name, qtype, pixel_bytes, setup, store) \ +static void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, \ + gint y0, \ + gint width, \ + gint height, \ + const guchar *buf, \ + gint rowstride, \ + gint x_align, \ + gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + const guchar *src = buf; \ + guchar *dst = ((guchar *) image->mem) + \ + y0 * image->bpl + x0 * (pixel_bytes); \ + gint x, y; \ + \ + setup \ + \ + (void) x_align; \ + (void) y_align; \ + (void) cmap; \ + \ + for (y = 0; y < height; y++) \ + { \ + const guchar *p = src; \ + qtype *q = (qtype *) dst; \ + \ + for (x = 0; x < width; x++) \ + { \ + guint32 g = *p++; \ + store \ + } \ + \ + src += rowstride; \ + dst += image->bpl; \ + } \ } +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_555_br, + guint16, + 2, + (void) image_info;, + guint16 pixel = + ((g & 0xf8) << 7) | + ((g & 0xf8) << 2) | + (g >> 3); + *q++ = (guint16) ((pixel << 8) | (pixel >> 8));) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_565_br, + guint16, + 2, + (void) image_info;, + guint16 pixel = + ((g & 0xf8) << 8) | + ((g & 0xfc) << 3) | + (g >> 3); + *q++ = (guint16) ((pixel << 8) | (pixel >> 8));) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_888, + guchar, + 3, + (void) image_info;, + q[0] = g; + q[1] = g; + q[2] = g; + q += 3;) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_0888, + guchar, + 4, + (void) image_info;, + q[0] = g; + q[1] = g; + q[2] = g; + q[3] = 0xff; + q += 4;) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_0888_br, + guchar, + 4, + (void) image_info;, + q[0] = 0xff; + q[1] = g; + q[2] = g; + q[3] = g; + q += 4;) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_8880_br, + guint32, + 4, + (void) image_info;, + *q++ = + (g << 16) | + (g << 8) | + g;) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_101010_lsb, + guint32, + 4, + guint32 alpha_mask = + image_info->visual->depth == 32 + ? 0xc0000000U + : 0;, + guint32 g10 = + (g << 2) | (g >> 6); + *q++ = GUINT32_TO_LE ( + alpha_mask | + (g10 << 20) | + (g10 << 10) | + g10);) + +DEFINE_GDK_RGB_GRAY_DIRECT ( + gdk_rgb_convert_gray_101010_msb, + guint32, + 4, + guint32 alpha_mask = + image_info->visual->depth == 32 + ? 0xc0000000U + : 0;, + guint32 g10 = + (g << 2) | (g >> 6); + *q++ = GUINT32_TO_BE ( + alpha_mask | + (g10 << 20) | + (g10 << 10) | + g10);) + +#undef DEFINE_GDK_RGB_GRAY_DIRECT + DEFINE_STAGED_CONVERTER (gdk_rgb_convert_gray_generic, conv, gdk_rgb_gray_to_stage) @@ -3904,6 +3999,11 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv_indexed = gdk_rgb_convert_indexed_lut; break; + case GDK_RGB_FORMAT_555_BR: + conv_gray = gdk_rgb_convert_gray_555_br; + conv_gray_d = gdk_rgb_convert_gray_555_br; + break; + case GDK_RGB_FORMAT_565: conv_32 = gdk_rgb_convert_32_565; conv_32_d = gdk_rgb_convert_32_565_d; @@ -3911,26 +4011,50 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) conv_indexed = gdk_rgb_convert_indexed_lut; break; + case GDK_RGB_FORMAT_565_BR: + conv_gray = gdk_rgb_convert_gray_565_br; + conv_gray_d = gdk_rgb_convert_gray_565_br; + break; + case GDK_RGB_FORMAT_0888: conv_32 = gdk_rgb_convert_32_0888; conv_32_d = gdk_rgb_convert_32_0888; -#if G_BYTE_ORDER == G_LITTLE_ENDIAN - conv_gray = gdk_rgb_convert_gray_0888_native; - conv_gray_d = gdk_rgb_convert_gray_0888_native; -#endif + conv_gray = gdk_rgb_convert_gray_0888; + conv_gray_d = gdk_rgb_convert_gray_0888; conv_indexed = gdk_rgb_convert_indexed_lut; break; case GDK_RGB_FORMAT_0888_BR: conv_32 = gdk_rgb_convert_32_0888_br; conv_32_d = gdk_rgb_convert_32_0888_br; -#if G_BYTE_ORDER == G_BIG_ENDIAN - conv_gray = gdk_rgb_convert_gray_0888_native; - conv_gray_d = gdk_rgb_convert_gray_0888_native; -#endif + conv_gray = gdk_rgb_convert_gray_0888_br; + conv_gray_d = gdk_rgb_convert_gray_0888_br; conv_indexed = gdk_rgb_convert_indexed_lut; break; + case GDK_RGB_FORMAT_888_LSB: + case GDK_RGB_FORMAT_888_MSB: + conv_gray = gdk_rgb_convert_gray_888; + conv_gray_d = gdk_rgb_convert_gray_888; + break; + + case GDK_RGB_FORMAT_8880_BR: + conv_gray = gdk_rgb_convert_gray_8880_br; + conv_gray_d = gdk_rgb_convert_gray_8880_br; + break; + + case GDK_RGB_FORMAT_101010_R_LOW_LSB: + case GDK_RGB_FORMAT_101010_B_LOW_LSB: + conv_gray = gdk_rgb_convert_gray_101010_lsb; + conv_gray_d = gdk_rgb_convert_gray_101010_lsb; + break; + + case GDK_RGB_FORMAT_101010_R_LOW_MSB: + case GDK_RGB_FORMAT_101010_B_LOW_MSB: + conv_gray = gdk_rgb_convert_gray_101010_msb; + conv_gray_d = gdk_rgb_convert_gray_101010_msb; + break; + default: break; } From c3f11d2bf7413cfaf56a9422a8feb15dfdf56a6a Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 1 Sep 2026 15:04:50 +0200 Subject: [PATCH 66/68] gdk: cache native indexed conversion tables --- gdk/gdkrgb.c | 236 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 182 insertions(+), 54 deletions(-) diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index f70950cd2..cc51e8440 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -117,7 +117,7 @@ struct _GdkRgbCmapInfo GdkRgbInfo *image_info; GdkRgbCmap *cmap; - guchar lut[256]; /* For 8-bit modes */ + guchar lut[1]; /* variable-sized private LUT */ }; static GdkRgbCmapInfo *gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, GdkRgbInfo *image_info); @@ -3445,29 +3445,22 @@ gdk_rgb_convert_indexed_lut (GdkRgbInfo *image_info, GdkImage *image, const guchar *buf, gint rowstride, gint x_align, gint y_align, GdkRgbCmap *cmap) { - union { - guint16 u16[256]; - guint32 u32[256]; - } lut; - gint i, y; + GdkRgbCmapInfo *cmap_info; + gint y; gint bpl = image->bpl; const guchar *src = buf; - guchar *dst = ((guchar *) image->mem) + y0 * bpl + x0 * image_info->bpp; + guchar *dst = ((guchar *) image->mem) + + y0 * bpl + x0 * image_info->bpp; + + (void) x_align; + (void) y_align; + + cmap_info = gdk_rgb_cmap_get_info (cmap, image_info); + g_assert (cmap_info != NULL); if (image_info->bpp == 2) { - guint is_565 = image_info->format == GDK_RGB_FORMAT_565; - guint red_shift = 9 - is_565; - guint green_shift = 6 - is_565; - guint32 green_mask = 0x00f800 | (is_565 << 10); - - for (i = 0; i < cmap->n_colors; i++) - { - guint32 rgb = cmap->colors[i]; - lut.u16[i] = ((rgb & 0xf80000) >> red_shift) | - ((rgb & green_mask) >> green_shift) | - ((rgb & 0x0000f8) >> 3); - } + const guint16 *lut = (const guint16 *) cmap_info->lut; for (y = 0; y < height; y++) { @@ -3476,7 +3469,7 @@ gdk_rgb_convert_indexed_lut (GdkRgbInfo *image_info, GdkImage *image, gint x; for (x = 0; x < width; x++) - q[x] = lut.u16[p[x]]; + q[x] = lut[p[x]]; src += rowstride; dst += bpl; @@ -3484,13 +3477,7 @@ gdk_rgb_convert_indexed_lut (GdkRgbInfo *image_info, GdkImage *image, } else { - gboolean little = image_info->format == GDK_RGB_FORMAT_0888; - - for (i = 0; i < cmap->n_colors; i++) - { - guint32 pixel = 0xff000000U | cmap->colors[i]; - lut.u32[i] = little ? GUINT32_TO_LE (pixel) : GUINT32_TO_BE (pixel); - } + const guint32 *lut = (const guint32 *) cmap_info->lut; for (y = 0; y < height; y++) { @@ -3499,7 +3486,7 @@ gdk_rgb_convert_indexed_lut (GdkRgbInfo *image_info, GdkImage *image, gint x; for (x = 0; x < width; x++) - q[x] = lut.u32[p[x]]; + q[x] = lut[p[x]]; src += rowstride; dst += bpl; @@ -3508,6 +3495,61 @@ gdk_rgb_convert_indexed_lut (GdkRgbInfo *image_info, GdkImage *image, } +#define DEFINE_GDK_RGB_INDEXED_888_CONVERTER(name, shift0, shift2) \ +static void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, \ + gint y0, \ + gint width, \ + gint height, \ + const guchar *buf, \ + int rowstride, \ + gint x_align, \ + gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + const guchar *src = buf; \ + guchar *dst = ((guchar *) image->mem) + \ + y0 * image->bpl + x0 * 3; \ + gint x, y; \ + \ + (void) image_info; \ + (void) x_align; \ + (void) y_align; \ + \ + for (y = 0; y < height; y++) \ + { \ + const guchar *p = src; \ + guchar *q = dst; \ + \ + for (x = 0; x < width; x++) \ + { \ + guint32 rgb = cmap->colors[p[x]]; \ + \ + q[0] = rgb >> (shift0); \ + q[1] = rgb >> 8; \ + q[2] = rgb >> (shift2); \ + q += 3; \ + } \ + \ + src += rowstride; \ + dst += image->bpl; \ + } \ +} + +DEFINE_GDK_RGB_INDEXED_888_CONVERTER ( + gdk_rgb_convert_indexed_888_lsb, + 0, + 16) + +DEFINE_GDK_RGB_INDEXED_888_CONVERTER ( + gdk_rgb_convert_indexed_888_msb, + 16, + 0) + +#undef DEFINE_GDK_RGB_INDEXED_888_CONVERTER + static void gdk_rgb_gray_to_stage (guchar *stage, const guchar *buf, gint rowstride, gint width, gint height, @@ -4002,6 +4044,7 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) case GDK_RGB_FORMAT_555_BR: conv_gray = gdk_rgb_convert_gray_555_br; conv_gray_d = gdk_rgb_convert_gray_555_br; + conv_indexed = gdk_rgb_convert_indexed_lut; break; case GDK_RGB_FORMAT_565: @@ -4014,6 +4057,7 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) case GDK_RGB_FORMAT_565_BR: conv_gray = gdk_rgb_convert_gray_565_br; conv_gray_d = gdk_rgb_convert_gray_565_br; + conv_indexed = gdk_rgb_convert_indexed_lut; break; case GDK_RGB_FORMAT_0888: @@ -4033,26 +4077,35 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) break; case GDK_RGB_FORMAT_888_LSB: + conv_gray = gdk_rgb_convert_gray_888; + conv_gray_d = gdk_rgb_convert_gray_888; + conv_indexed = gdk_rgb_convert_indexed_888_lsb; + break; + case GDK_RGB_FORMAT_888_MSB: conv_gray = gdk_rgb_convert_gray_888; conv_gray_d = gdk_rgb_convert_gray_888; + conv_indexed = gdk_rgb_convert_indexed_888_msb; break; case GDK_RGB_FORMAT_8880_BR: conv_gray = gdk_rgb_convert_gray_8880_br; conv_gray_d = gdk_rgb_convert_gray_8880_br; + conv_indexed = gdk_rgb_convert_indexed_lut; break; case GDK_RGB_FORMAT_101010_R_LOW_LSB: case GDK_RGB_FORMAT_101010_B_LOW_LSB: conv_gray = gdk_rgb_convert_gray_101010_lsb; conv_gray_d = gdk_rgb_convert_gray_101010_lsb; + conv_indexed = gdk_rgb_convert_indexed_lut; break; case GDK_RGB_FORMAT_101010_R_LOW_MSB: case GDK_RGB_FORMAT_101010_B_LOW_MSB: conv_gray = gdk_rgb_convert_gray_101010_msb; conv_gray_d = gdk_rgb_convert_gray_101010_msb; + conv_indexed = gdk_rgb_convert_indexed_lut; break; default: @@ -4968,48 +5021,123 @@ gdk_draw_gray_image (GdkDrawable *drawable, static GdkRgbCmapInfo * gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, - GdkRgbInfo *image_info) + GdkRgbInfo *image_info) { GSList *tmp_list; GdkRgbCmapInfo *cmap_info; - int i, j; - guint32 rgb; + gsize lut_bytes; + gboolean lut8; + gint i; - /* We don't need a LUT for TrueColor or DirectColor visuals */ - if (image_info->bpp != 1 || - !(image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR || - image_info->visual->type == GDK_VISUAL_STATIC_COLOR || - image_info->visual->type == GDK_VISUAL_GRAYSCALE)) - return NULL; + lut8 = + image_info->bpp == 1 && + (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR || + image_info->visual->type == GDK_VISUAL_STATIC_COLOR || + image_info->visual->type == GDK_VISUAL_GRAYSCALE); - tmp_list = cmap->info_list; - - while (tmp_list) + if (lut8) + lut_bytes = 256; + else { - cmap_info = tmp_list->data; - if (cmap_info->image_info == image_info) - return cmap_info; - tmp_list = tmp_list->next; + switch (image_info->format) + { + case GDK_RGB_FORMAT_565: + case GDK_RGB_FORMAT_565_BR: + case GDK_RGB_FORMAT_555: + case GDK_RGB_FORMAT_555_BR: + lut_bytes = 256 * sizeof (guint16); + break; + + case GDK_RGB_FORMAT_0888: + case GDK_RGB_FORMAT_0888_BR: + case GDK_RGB_FORMAT_8880_BR: + case GDK_RGB_FORMAT_101010_R_LOW_LSB: + case GDK_RGB_FORMAT_101010_B_LOW_LSB: + case GDK_RGB_FORMAT_101010_R_LOW_MSB: + case GDK_RGB_FORMAT_101010_B_LOW_MSB: + lut_bytes = 256 * sizeof (guint32); + break; + + default: + return NULL; + } } - cmap_info = g_new (GdkRgbCmapInfo, 1); + for (tmp_list = cmap->info_list; + tmp_list; + tmp_list = tmp_list->next) + { + cmap_info = tmp_list->data; + + if (cmap_info->image_info == image_info) + return cmap_info; + } + + cmap_info = + g_malloc (G_STRUCT_OFFSET (GdkRgbCmapInfo, lut) + + lut_bytes); + cmap_info->image_info = image_info; cmap_info->cmap = cmap; - for (i = 0; i < cmap->n_colors; i++) + if (lut8) { - rgb = cmap->colors[i]; - j = ((rgb & 0xf00000) >> 12) | - ((rgb & 0xf000) >> 8) | - ((rgb & 0xf0) >> 4); + for (i = 0; i < cmap->n_colors; i++) + { + guint32 rgb = cmap->colors[i]; + gint j; + + j = ((rgb & 0xf00000) >> 12) | + ((rgb & 0x00f000) >> 8) | + ((rgb & 0x0000f0) >> 4); + #ifdef VERBOSE - g_print ("%d %x %x\n", i, j, image_info->colorcube[j]); + g_print ("%d %x %x\n", + i, j, image_info->colorcube[j]); #endif - cmap_info->lut[i] = image_info->colorcube[j]; + + cmap_info->lut[i] = + image_info->colorcube[j]; + } + } + else + { + GdkImage image; + guchar stage[256 * 3]; + + for (i = 0; i < cmap->n_colors; i++) + { + guint32 rgb = cmap->colors[i]; + + stage[i * 3 + 0] = rgb >> 16; + stage[i * 3 + 1] = rgb >> 8; + stage[i * 3 + 2] = rgb; + } + + memset (&image, 0, sizeof (image)); + + image.mem = cmap_info->lut; + image.bpl = cmap->n_colors * image_info->bpp; + + g_assert (image_info->conv != NULL); + + image_info->conv (image_info, + &image, + 0, 0, + cmap->n_colors, 1, + stage, + cmap->n_colors * 3, + 0, 0, + NULL); } - cmap->info_list = g_slist_prepend (cmap->info_list, cmap_info); - image_info->cmap_info_list = g_slist_prepend (image_info->cmap_info_list, cmap_info); + cmap->info_list = + g_slist_prepend (cmap->info_list, + cmap_info); + + image_info->cmap_info_list = + g_slist_prepend (image_info->cmap_info_list, + cmap_info); return cmap_info; } From b33beefc08ea5a1373b6cf91f9f086e032cfe84f Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Tue, 1 Sep 2026 18:25:28 +0200 Subject: [PATCH 67/68] gdk: add MMX accelerated GdkRGB gray conversion paths --- gdk/Makefile.am | 10 +- gdk/gdkpixelops-x86-mmx.c | 1 + gdk/gdkpixelops-x86.c | 9 + gdk/gdkpixelops.h | 2 + gdk/gdkrgb-x86-mmx.c | 363 ++++++++++++++++++++++++++++++++++++++ gdk/gdkrgb.c | 64 +++++++ 6 files changed, 445 insertions(+), 4 deletions(-) create mode 100644 gdk/gdkrgb-x86-mmx.c diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 1370e1a28..75fb4625d 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -127,11 +127,13 @@ gdk_pixelops_arch_libs = if GDK_PIXELOPS_I386 gdk_pixelops_arch_sources += gdkpixelops-x86.c -noinst_LTLIBRARIES += libgdk-pixelops-x86-mmx.la -gdk_pixelops_arch_libs += libgdk-pixelops-x86-mmx.la +noinst_LTLIBRARIES += libgdk-x86-mmx.la +gdk_pixelops_arch_libs += libgdk-x86-mmx.la -libgdk_pixelops_x86_mmx_la_SOURCES = gdkpixelops-x86-mmx.c -libgdk_pixelops_x86_mmx_la_CFLAGS = \ +libgdk_x86_mmx_la_SOURCES = \ + gdkpixelops-x86-mmx.c \ + gdkrgb-x86-mmx.c +libgdk_x86_mmx_la_CFLAGS = \ $(AM_CFLAGS) \ -mmmx -mno-sse -mno-sse2 -mno-3dnow endif diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c index 1d620afd0..6bc17780e 100644 --- a/gdk/gdkpixelops-x86-mmx.c +++ b/gdk/gdkpixelops-x86-mmx.c @@ -10,6 +10,7 @@ #include "gdkpixelops.h" + #ifndef __i386__ #error "gdkpixelops-x86-mmx.c requires a 32-bit x86 target" #endif diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 7c081a951..1e6f37f2f 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -40,6 +40,14 @@ _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, gint width, gint height); +static gboolean gdk_pixel_ops_mmx_available; + +G_GNUC_INTERNAL gboolean +_gdk_pixel_ops_have_mmx (void) +{ + return gdk_pixel_ops_mmx_available; +} + G_GNUC_INTERNAL void _gdk_pixel_ops_init (void) { @@ -51,6 +59,7 @@ _gdk_pixel_ops_init (void) if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) && (edx & bit_MMX) != 0) { + gdk_pixel_ops_mmx_available = TRUE; _gdk_pixel_ops.composite_0888 = _gdk_pixel_ops_composite_0888_mmx; _gdk_pixel_ops.composite_555 = diff --git a/gdk/gdkpixelops.h b/gdk/gdkpixelops.h index 82c3b79af..2a7e4f896 100644 --- a/gdk/gdkpixelops.h +++ b/gdk/gdkpixelops.h @@ -29,8 +29,10 @@ extern G_GNUC_INTERNAL GdkPixelOps _gdk_pixel_ops; #ifdef HAVE_GDK_PIXELOPS_INIT G_GNUC_INTERNAL void _gdk_pixel_ops_init (void); +G_GNUC_INTERNAL gboolean _gdk_pixel_ops_have_mmx (void); #else #define _gdk_pixel_ops_init() ((void) 0) +#define _gdk_pixel_ops_have_mmx() FALSE #endif G_END_DECLS diff --git a/gdk/gdkrgb-x86-mmx.c b/gdk/gdkrgb-x86-mmx.c new file mode 100644 index 000000000..86c45c9de --- /dev/null +++ b/gdk/gdkrgb-x86-mmx.c @@ -0,0 +1,363 @@ +/* GDK - The GIMP Drawing Kit + * + * MMX GdkRGB conversion kernels for 32-bit x86. + */ + +#include "config.h" + +#include +#include + +#include "gdkimage.h" +#include "gdkrgb.h" + +typedef struct _GdkRgbInfo GdkRgbInfo; + +#ifndef __i386__ +#error "gdkrgb-x86-mmx.c requires a 32-bit x86 target" +#endif + +/* + * GdkRGB Gray conversion MMX kernels. + */ + +static __m64 +gdk_rgb_gray_mmx_load4 (const guchar *p) +{ + guint32 v; + + memcpy (&v, p, sizeof (v)); + + return _mm_cvtsi32_si64 ((gint) v); +} + +static __m64 +gdk_rgb_gray_mmx_load2 (const guchar *p) +{ + guint16 v; + + memcpy (&v, p, sizeof (v)); + + return _mm_cvtsi32_si64 ((gint) v); +} + +static void +gdk_rgb_gray_mmx_store8 (guchar *p, + __m64 v) +{ + memcpy (p, &v, sizeof (v)); +} + +static __m64 +gdk_rgb_gray_mmx_555 (__m64 b) +{ + const __m64 zero = _mm_setzero_si64 (); + __m64 g = _mm_unpacklo_pi8 (b, zero); + __m64 v = _mm_srli_pi16 (g, 3); + + return _mm_or_si64 ( + _mm_or_si64 ( + _mm_slli_pi16 (v, 10), + _mm_slli_pi16 (v, 5)), + v); +} + +static __m64 +gdk_rgb_gray_mmx_565 (__m64 b) +{ + const __m64 zero = _mm_setzero_si64 (); + __m64 g = _mm_unpacklo_pi8 (b, zero); + __m64 v5 = _mm_srli_pi16 (g, 3); + __m64 v6 = _mm_srli_pi16 (g, 2); + + return _mm_or_si64 ( + _mm_or_si64 ( + _mm_slli_pi16 (v5, 11), + _mm_slli_pi16 (v6, 5)), + v5); +} + +static __m64 +gdk_rgb_gray_mmx_swap16 (__m64 v) +{ + return _mm_or_si64 ( + _mm_slli_pi16 (v, 8), + _mm_srli_pi16 (v, 8)); +} + +static guint16 +gdk_rgb_gray_tail_555 (guint g) +{ + return (guint16) + (((g & 0xf8) << 7) | + ((g & 0xf8) << 2) | + (g >> 3)); +} + +static guint16 +gdk_rgb_gray_tail_565 (guint g) +{ + return (guint16) + (((g & 0xf8) << 8) | + ((g & 0xfc) << 3) | + (g >> 3)); +} + +static guint16 +gdk_rgb_gray_tail_swap16 (guint16 v) +{ + return (guint16) ((v << 8) | (v >> 8)); +} + +#define DEFINE_GDK_RGB_GRAY16_MMX(name, vector_value, tail_value) \ +G_GNUC_INTERNAL void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, \ + gint y0, \ + gint width, \ + gint height, \ + const guchar *buf, \ + gint rowstride, \ + gint x_align, \ + gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + const guchar *src = buf; \ + guchar *dst = ((guchar *) image->mem) + \ + y0 * image->bpl + x0 * 2; \ + gint y; \ + \ + (void) image_info; \ + (void) x_align; \ + (void) y_align; \ + (void) cmap; \ + \ + for (y = 0; y < height; y++) \ + { \ + const guchar *p = src; \ + guchar *q = dst; \ + gint x = 0; \ + \ + for (; x + 4 <= width; x += 4) \ + { \ + __m64 out = (vector_value); \ + \ + gdk_rgb_gray_mmx_store8 (q, out); \ + \ + p += 4; \ + q += 8; \ + } \ + \ + for (; x < width; x++) \ + { \ + guint16 out = (tail_value); \ + \ + memcpy (q, &out, sizeof (out)); \ + \ + p++; \ + q += 2; \ + } \ + \ + src += rowstride; \ + dst += image->bpl; \ + } \ + \ + if (width >= 4 && height > 0) \ + _mm_empty (); \ +} + +DEFINE_GDK_RGB_GRAY16_MMX ( + _gdk_rgb_convert_gray_555_mmx, + gdk_rgb_gray_mmx_555 ( + gdk_rgb_gray_mmx_load4 (p)), + gdk_rgb_gray_tail_555 (*p)) + +DEFINE_GDK_RGB_GRAY16_MMX ( + _gdk_rgb_convert_gray_555_br_mmx, + gdk_rgb_gray_mmx_swap16 ( + gdk_rgb_gray_mmx_555 ( + gdk_rgb_gray_mmx_load4 (p))), + gdk_rgb_gray_tail_swap16 ( + gdk_rgb_gray_tail_555 (*p))) + +DEFINE_GDK_RGB_GRAY16_MMX ( + _gdk_rgb_convert_gray_565_mmx, + gdk_rgb_gray_mmx_565 ( + gdk_rgb_gray_mmx_load4 (p)), + gdk_rgb_gray_tail_565 (*p)) + +DEFINE_GDK_RGB_GRAY16_MMX ( + _gdk_rgb_convert_gray_565_br_mmx, + gdk_rgb_gray_mmx_swap16 ( + gdk_rgb_gray_mmx_565 ( + gdk_rgb_gray_mmx_load4 (p))), + gdk_rgb_gray_tail_swap16 ( + gdk_rgb_gray_tail_565 (*p))) + +#undef DEFINE_GDK_RGB_GRAY16_MMX + +/* + * One shared 101010 implementation. + * The two GdkRgbConvFunc wrappers differ only in the alpha mask. + */ + +static __m64 +gdk_rgb_gray_mmx_dwords2 (const guchar *p) +{ + const __m64 zero = _mm_setzero_si64 (); + __m64 b = gdk_rgb_gray_mmx_load2 (p); + __m64 w = _mm_unpacklo_pi8 (b, zero); + + return _mm_unpacklo_pi16 (w, zero); +} + +static void +gdk_rgb_gray_101010_lsb_mmx_impl (GdkImage *image, + gint x0, + gint y0, + gint width, + gint height, + const guchar *buf, + gint rowstride, + guint32 alpha) +{ + if (width < 4) + { + const guchar *tiny_src = buf; + guchar *tiny_dst = + ((guchar *) image->mem) + + y0 * image->bpl + + x0 * 4; + gint tiny_y; + + for (tiny_y = 0; tiny_y < height; tiny_y++) + { + const guchar *p = tiny_src; + guchar *q = tiny_dst; + gint x; + + for (x = 0; x < width; x++) + { + guint32 g = *p++; + guint32 g10 = + (g << 2) | (g >> 6); + guint32 out = + alpha | + (g10 << 20) | + (g10 << 10) | + g10; + + memcpy (q, &out, sizeof (out)); + q += 4; + } + + tiny_src += rowstride; + tiny_dst += image->bpl; + } + + return; + } + + const __m64 alpha2 = + _mm_set_pi32 ((gint) alpha, (gint) alpha); + + const guchar *src = buf; + + guchar *dst = + ((guchar *) image->mem) + + y0 * image->bpl + + x0 * 4; + + gint y; + + for (y = 0; y < height; y++) + { + const guchar *p = src; + guchar *q = dst; + gint x = 0; + + for (; x + 2 <= width; x += 2) + { + __m64 g = + gdk_rgb_gray_mmx_dwords2 (p); + + __m64 g10 = + _mm_or_si64 ( + _mm_slli_pi32 (g, 2), + _mm_srli_pi32 (g, 6)); + + __m64 out = + _mm_or_si64 ( + alpha2, + _mm_or_si64 ( + g10, + _mm_or_si64 ( + _mm_slli_pi32 (g10, 10), + _mm_slli_pi32 (g10, 20)))); + + gdk_rgb_gray_mmx_store8 (q, out); + + p += 2; + q += 8; + } + + for (; x < width; x++) + { + guint32 g = *p++; + guint32 g10 = + (g << 2) | (g >> 6); + + guint32 out = + alpha | + (g10 << 20) | + (g10 << 10) | + g10; + + memcpy (q, &out, sizeof (out)); + q += 4; + } + + src += rowstride; + dst += image->bpl; + } + + if (width >= 2 && height > 0) + _mm_empty (); +} + +#define DEFINE_GDK_RGB_GRAY101010_MMX(name, alpha) \ +G_GNUC_INTERNAL void \ +name (GdkRgbInfo *image_info, \ + GdkImage *image, \ + gint x0, \ + gint y0, \ + gint width, \ + gint height, \ + const guchar *buf, \ + gint rowstride, \ + gint x_align, \ + gint y_align, \ + GdkRgbCmap *cmap) \ +{ \ + (void) image_info; \ + (void) x_align; \ + (void) y_align; \ + (void) cmap; \ + \ + gdk_rgb_gray_101010_lsb_mmx_impl ( \ + image, x0, y0, \ + width, height, \ + buf, rowstride, \ + alpha); \ +} + +DEFINE_GDK_RGB_GRAY101010_MMX ( + _gdk_rgb_convert_gray_101010_lsb_30_mmx, + 0) + +DEFINE_GDK_RGB_GRAY101010_MMX ( + _gdk_rgb_convert_gray_101010_lsb_32_mmx, + 0xc0000000U) + +#undef DEFINE_GDK_RGB_GRAY101010_MMX diff --git a/gdk/gdkrgb.c b/gdk/gdkrgb.c index cc51e8440..7a54ca489 100644 --- a/gdk/gdkrgb.c +++ b/gdk/gdkrgb.c @@ -39,6 +39,7 @@ #include "gdkinternals.h" /* _gdk_windowing_get_bits_for_depth() */ #include "gdkrgb.h" +#include "gdkpixelops.h" #include "gdkscreen.h" #include "gdkalias.h" #include @@ -71,6 +72,25 @@ typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image, gint x_align, gint y_align, GdkRgbCmap *cmap); + +#ifdef HAVE_GDK_PIXELOPS_INIT +#define DECLARE_GDK_RGB_GRAY_MMX(name) \ +G_GNUC_INTERNAL void \ +name (GdkRgbInfo *, GdkImage *, \ + gint, gint, gint, gint, \ + const guchar *, gint, \ + gint, gint, GdkRgbCmap *); + +DECLARE_GDK_RGB_GRAY_MMX (_gdk_rgb_convert_gray_555_mmx) +DECLARE_GDK_RGB_GRAY_MMX (_gdk_rgb_convert_gray_555_br_mmx) +DECLARE_GDK_RGB_GRAY_MMX (_gdk_rgb_convert_gray_565_mmx) +DECLARE_GDK_RGB_GRAY_MMX (_gdk_rgb_convert_gray_565_br_mmx) +DECLARE_GDK_RGB_GRAY_MMX (_gdk_rgb_convert_gray_101010_lsb_30_mmx) +DECLARE_GDK_RGB_GRAY_MMX (_gdk_rgb_convert_gray_101010_lsb_32_mmx) + +#undef DECLARE_GDK_RGB_GRAY_MMX +#endif + #define STAGE_ROWSTRIDE (GDK_SCRATCH_IMAGE_WIDTH * 3) #define GDK_RGB_STAGE_ROWS 4 @@ -4112,6 +4132,50 @@ gdk_rgb_select_conv (GdkRgbInfo *image_info) break; } + +#ifdef HAVE_GDK_PIXELOPS_INIT + if (_gdk_pixel_ops_have_mmx ()) + { + switch (format) + { + case GDK_RGB_FORMAT_555: + conv_gray = + _gdk_rgb_convert_gray_555_mmx; + conv_gray_d = conv_gray; + break; + + case GDK_RGB_FORMAT_555_BR: + conv_gray = + _gdk_rgb_convert_gray_555_br_mmx; + conv_gray_d = conv_gray; + break; + + case GDK_RGB_FORMAT_565: + conv_gray = + _gdk_rgb_convert_gray_565_mmx; + break; + + case GDK_RGB_FORMAT_565_BR: + conv_gray = + _gdk_rgb_convert_gray_565_br_mmx; + conv_gray_d = conv_gray; + break; + + case GDK_RGB_FORMAT_101010_R_LOW_LSB: + case GDK_RGB_FORMAT_101010_B_LOW_LSB: + conv_gray = + depth == 32 + ? _gdk_rgb_convert_gray_101010_lsb_32_mmx + : _gdk_rgb_convert_gray_101010_lsb_30_mmx; + conv_gray_d = conv_gray; + break; + + default: + break; + } + } +#endif + image_info->format = format; image_info->conv = conv; From 99823d458b8a4b3b065bbb9019ce3b4622979061 Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Mon, 14 Sep 2026 06:39:46 +0200 Subject: [PATCH 68/68] gdk: optimize i386 MMX and MMXEXT pixel compositing --- gdk/Makefile.am | 1 + gdk/gdkpixelops-x86-mmx.c | 266 ++++++++++++++++--- gdk/gdkpixelops-x86-mmxext.c | 493 +++++++++++++++++++++++++++++++++++ gdk/gdkpixelops-x86.c | 79 +++++- 4 files changed, 792 insertions(+), 47 deletions(-) create mode 100644 gdk/gdkpixelops-x86-mmxext.c diff --git a/gdk/Makefile.am b/gdk/Makefile.am index 75fb4625d..ca16ee6cf 100644 --- a/gdk/Makefile.am +++ b/gdk/Makefile.am @@ -132,6 +132,7 @@ gdk_pixelops_arch_libs += libgdk-x86-mmx.la libgdk_x86_mmx_la_SOURCES = \ gdkpixelops-x86-mmx.c \ + gdkpixelops-x86-mmxext.c \ gdkrgb-x86-mmx.c libgdk_x86_mmx_la_CFLAGS = \ $(AM_CFLAGS) \ diff --git a/gdk/gdkpixelops-x86-mmx.c b/gdk/gdkpixelops-x86-mmx.c index 6bc17780e..40838b569 100644 --- a/gdk/gdkpixelops-x86-mmx.c +++ b/gdk/gdkpixelops-x86-mmx.c @@ -57,6 +57,20 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, const __m64 ff = _mm_srli_pi16 (ones, 8); const __m64 bias = _mm_set1_pi16 (0x80); const __m64 rgb_mask = _mm_srli_si64 (ones, 16); + + /* + * Pure-MMX masks for: + * + * [ R G B A ] -> [ B G R A ] + * + * No scalar R/B shuffle. + */ + const __m64 r_mask = + _mm_set_pi16 (0, 0, 0, (short) -1); + const __m64 b_mask = + _mm_set_pi16 (0, (short) -1, 0, 0); + const __m64 ga_mask = + _mm_set_pi16 ((short) -1, 0, (short) -1, 0); guchar *src = src_buf; guchar *dest = dest_buf; @@ -87,23 +101,40 @@ _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, __m64 d; __m64 a; __m64 t; + __m64 rb; memcpy (&sp, p, sizeof sp); memcpy (&dp, q, sizeof dp); - sp = ((sp & 0xff00ff00U) | - ((sp & 0x000000ffU) << 16) | - ((sp & 0x00ff0000U) >> 16)); - s = _mm_cvtsi32_si64 ((int) sp); d = _mm_cvtsi32_si64 ((int) dp); s = _mm_unpacklo_pi8 (s, zero); d = _mm_unpacklo_pi8 (d, zero); - a = _mm_srli_si64 (s, 48); - a = _mm_unpacklo_pi16 (a, a); - a = _mm_unpacklo_pi32 (a, a); + /* + * s starts as: + * + * [ R G B A ] + * + * Swap R/B entirely inside base MMX: + * + * [ B G R A ] + */ + rb = + _mm_or_si64 ( + _mm_slli_si64 ( + _mm_and_si64 (s, r_mask), 32), + _mm_srli_si64 ( + _mm_and_si64 (s, b_mask), 32)); + + s = + _mm_or_si64 ( + _mm_and_si64 (s, ga_mask), + rb); + + a = _mm_unpackhi_pi16 (s, s); + a = _mm_unpackhi_pi32 (a, a); a = _mm_and_si64 (a, rgb_mask); t = blend_words_mmx (s, d, a, ff, bias); @@ -150,25 +181,85 @@ expand_565 (guint16 value) ((hi >> 6) & 0x00000300U); } -static __inline__ guint16 -pack_555 (guint32 rgb) -{ - return (guint16) - (((rgb & 0x000000f8U) << 7) | - ((rgb & 0x0000f800U) >> 6) | - ((rgb & 0x00f80000U) >> 19)); -} static __inline__ guint16 -pack_565 (guint32 rgb) +pack_555 (__m64 rgb) { - return (guint16) - (((rgb & 0x000000f8U) << 8) | - ((rgb & 0x0000fc00U) >> 5) | - ((rgb & 0x00f80000U) >> 19)); + const __m64 coeff = + _mm_set_pi16 (0, 1, 32, 1024); + + __m64 v; + __m64 hi; + + /* + * rgb = [ R G B A ] + * + * Common >>3 gives: + * + * [ R5 G5 B5 x ] + */ + v = _mm_srli_pi16 (rgb, 3); + + /* + * Pure MMX PMADDWD: + * + * low32 = R5*1024 + G5*32 + * high32 = B5 + */ + v = _mm_madd_pi16 (v, coeff); + + hi = _mm_srli_si64 (v, 32); + v = _mm_add_pi32 (v, hi); + + return (guint16) _mm_cvtsi64_si32 (v); } -static __inline__ guint32 + +static __inline__ guint16 +pack_565 (__m64 rgb) +{ + const __m64 coeff = + _mm_set_pi16 (0, 1, 64, 2048); + + const __m64 four = + _mm_set_pi16 (0, 0, 0, 4); + + __m64 v; + __m64 hi; + __m64 gbit; + + /* + * Common >>3: + * + * [ R5 G5 B5 x ] + */ + v = _mm_srli_pi16 (rgb, 3); + + /* + * low32 = R5*2048 + G5*64 + * high32 = B5 + */ + v = _mm_madd_pi16 (v, coeff); + + hi = _mm_srli_si64 (v, 32); + v = _mm_add_pi32 (v, hi); + + /* + * Sixth green bit: + * + * (G & 4) << 3 + */ + gbit = _mm_srli_si64 (rgb, 16); + gbit = _mm_and_si64 (gbit, four); + gbit = _mm_slli_pi16 (gbit, 3); + + v = _mm_add_pi32 (v, gbit); + + return (guint16) _mm_cvtsi64_si32 (v); +} + + +static __inline__ __m64 blend_rgb16_mmx (__m64 zero, __m64 ff, __m64 bias, @@ -180,7 +271,6 @@ blend_rgb16_mmx (__m64 zero, __m64 a; __m64 ia; __m64 t; - guint32 result; s = _mm_cvtsi32_si64 ((int) src); d = _mm_cvtsi32_si64 ((int) dest); @@ -188,28 +278,90 @@ blend_rgb16_mmx (__m64 zero, s = _mm_unpacklo_pi8 (s, zero); d = _mm_unpacklo_pi8 (d, zero); - a = _mm_srli_si64 (s, 48); - a = _mm_unpacklo_pi16 (a, a); - a = _mm_unpacklo_pi32 (a, a); + /* + * Pure-MMX alpha broadcast: + * + * A -> [ A A A A ] + */ + a = _mm_unpackhi_pi16 (s, s); + a = _mm_unpackhi_pi32 (a, a); ia = _mm_sub_pi16 (ff, a); - t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), - _mm_mullo_pi16 (d, ia)); + t = _mm_add_pi16 ( + _mm_mullo_pi16 (s, a), + _mm_mullo_pi16 (d, ia)); + /* + * Exact integer /255 using base MMX only. + */ t = _mm_add_pi16 (t, bias); - t = _mm_add_pi16 (t, _mm_srli_pi16 (t, 8)); + t = _mm_add_pi16 ( + t, + _mm_srli_pi16 (t, 8)); + t = _mm_srli_pi16 (t, 8); - t = _mm_packs_pu16 (t, t); - __asm__ ("movd %1,%0" - : "=r" (result) - : "y" (t)); - - return result; + return t; } -#define DEFINE_RGB16_MMX_COMPOSITOR(name, expand_pixel, pack_pixel) \ + +static __inline__ guint32 +blend_rgb16_scalar_tail (guint32 src, + guint32 dest) +{ + guint32 a; + guint32 ia; + guint32 r; + guint32 g; + guint32 b; + + a = src >> 24; + ia = 255U - a; + + r = (src & 0xffU) * a + + (dest & 0xffU) * ia; + + g = ((src >> 8) & 0xffU) * a + + ((dest >> 8) & 0xffU) * ia; + + b = ((src >> 16) & 0xffU) * a + + ((dest >> 16) & 0xffU) * ia; + + r += 128U; + r += r >> 8; + r >>= 8; + + g += 128U; + g += g >> 8; + g >>= 8; + + b += 128U; + b += b >> 8; + b >>= 8; + + return r | (g << 8) | (b << 16); +} + +static __inline__ guint16 +pack_555_scalar_tail (guint32 rgb) +{ + return (guint16) + (((rgb & 0x000000f8U) << 7) | + ((rgb & 0x0000f800U) >> 6) | + ((rgb & 0x00f80000U) >> 19)); +} + +static __inline__ guint16 +pack_565_scalar_tail (guint32 rgb) +{ + return (guint16) + (((rgb & 0x000000f8U) << 8) | + ((rgb & 0x0000fc00U) >> 5) | + ((rgb & 0x00f80000U) >> 19)); +} + +#define DEFINE_RGB16_MMX_COMPOSITOR(name, expand_pixel, pack_pixel, scalar_pack_pixel) \ G_GNUC_INTERNAL void \ name (guchar *src_buf, \ gint src_rowstride, \ @@ -219,6 +371,36 @@ name (guchar *src_buf, \ gint width, \ gint height) \ { \ + if (width == 1) \ + { \ + guchar *sp_row = src_buf; \ + guchar *dp_row = dest_buf; \ + gint h = height; \ + \ + while (h--) \ + { \ + guint32 sp; \ + guint16 dp16; \ + guint32 dp; \ + guint32 rgb; \ + guint16 result; \ + \ + memcpy (&sp, sp_row, sizeof sp); \ + memcpy (&dp16, dp_row, sizeof dp16); \ + \ + dp = expand_pixel (dp16); \ + rgb = blend_rgb16_scalar_tail (sp, dp); \ + result = scalar_pack_pixel (rgb); \ + \ + memcpy (dp_row, &result, sizeof result); \ + \ + sp_row += src_rowstride; \ + dp_row += dest_rowstride; \ + } \ + \ + return; \ + } \ + \ const __m64 zero = _mm_setzero_si64 (); \ const __m64 ones = _mm_cmpeq_pi16 (zero, zero); \ const __m64 ff = _mm_srli_pi16 (ones, 8); \ @@ -241,8 +423,8 @@ name (guchar *src_buf, \ guint32 dpair; \ guint32 dp0; \ guint32 dp1; \ - guint32 rgb0; \ - guint32 rgb1; \ + __m64 rgb0; \ + __m64 rgb1; \ guint32 result; \ \ memcpy (&sp0, p, sizeof sp0); \ @@ -276,8 +458,8 @@ name (guchar *src_buf, \ memcpy (&dp16, q, sizeof dp16); \ \ dp = expand_pixel (dp16); \ - rgb = blend_rgb16_mmx (zero, ff, bias, sp, dp); \ - result = pack_pixel (rgb); \ + rgb = blend_rgb16_scalar_tail (sp, dp); \ + result = scalar_pack_pixel (rgb); \ \ memcpy (q, &result, sizeof result); \ } \ @@ -292,11 +474,13 @@ name (guchar *src_buf, \ DEFINE_RGB16_MMX_COMPOSITOR ( _gdk_pixel_ops_composite_555_mmx, expand_555, - pack_555) + pack_555, + pack_555_scalar_tail) DEFINE_RGB16_MMX_COMPOSITOR ( _gdk_pixel_ops_composite_565_mmx, expand_565, - pack_565) + pack_565, + pack_565_scalar_tail) #undef DEFINE_RGB16_MMX_COMPOSITOR diff --git a/gdk/gdkpixelops-x86-mmxext.c b/gdk/gdkpixelops-x86-mmxext.c new file mode 100644 index 000000000..aea6fa03a --- /dev/null +++ b/gdk/gdkpixelops-x86-mmxext.c @@ -0,0 +1,493 @@ +/* GDK - The GIMP Drawing Kit + * + * MMX pixel operations for 32-bit x86. + */ + +#include "config.h" + +#include +#include + +#include "gdkpixelops.h" + + +#ifndef __i386__ +#error "gdkpixelops-x86-mmx.c requires a 32-bit x86 target" +#endif + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_generic (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +static __inline__ __m64 +blend_words_mmx (__m64 s, + __m64 d, + __m64 a, + __m64 ff, + __m64 bias, + __m64 c257) +{ + __m64 t; + + t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), + _mm_mullo_pi16 (d, + _mm_sub_pi16 (ff, a))); + + t = _mm_add_pi16 (t, bias); + + __asm__ ("pmulhuw %1,%0" + : "+y" (t) + : "y" (c257)); + + return t; +} + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_mmxext (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height) +{ + const __m64 zero = _mm_setzero_si64 (); + const __m64 ones = _mm_cmpeq_pi16 (zero, zero); + const __m64 ff = _mm_srli_pi16 (ones, 8); + const __m64 bias = _mm_set1_pi16 (0x80); + const __m64 c257 = _mm_set1_pi16 (257); + const __m64 rgb_mask = _mm_srli_si64 (ones, 16); + guchar *src = src_buf; + guchar *dest = dest_buf; + + if (dest_byte_order != GDK_LSB_FIRST) + { + _gdk_pixel_ops_composite_0888_generic (src_buf, + src_rowstride, + dest_buf, + dest_rowstride, + dest_byte_order, + width, + height); + return; + } + + while (height--) + { + gint twidth = width; + guchar *p = src; + guchar *q = dest; + + while (twidth--) + { + guint32 sp; + guint32 dp; + guint32 result; + __m64 s; + __m64 d; + __m64 a; + __m64 t; + + memcpy (&sp, p, sizeof sp); + memcpy (&dp, q, sizeof dp); + + s = _mm_cvtsi32_si64 ((int) sp); + d = _mm_cvtsi32_si64 ((int) dp); + + s = _mm_unpacklo_pi8 (s, zero); + d = _mm_unpacklo_pi8 (d, zero); + + /* + * Input lanes after unpack: + * + * [ R G B A ] + * + * Destination LSB layout wants: + * + * [ B G R X ] + * + * Keep the complete 4 x 16-bit vector occupied and do + * both channel ordering and alpha broadcast in MMXEXT. + */ + a = s; + + __asm__ ("pshufw $0xff,%0,%0" + : "+y" (a)); + + __asm__ ("pshufw $0xc6,%0,%0" + : "+y" (s)); + + a = _mm_and_si64 (a, rgb_mask); + + t = blend_words_mmx (s, d, a, ff, bias, c257); + t = _mm_packs_pu16 (t, t); + + result = (guint32) _mm_cvtsi64_si32 (t); + memcpy (q, &result, sizeof result); + + p += 4; + q += 4; + } + + src += src_rowstride; + dest += dest_rowstride; + } + + _mm_empty (); +} + +static __inline__ guint32 +expand_555 (guint16 value) +{ + guint32 hi; + + hi = (((guint32) value & 0x7c00U) >> 7) | + (((guint32) value & 0x03e0U) << 6) | + (((guint32) value & 0x001fU) << 19); + + return hi | + ((hi >> 5) & 0x00070707U); +} + +static __inline__ guint32 +expand_565 (guint16 value) +{ + guint32 hi; + + hi = (((guint32) value & 0xf800U) >> 8) | + (((guint32) value & 0x07e0U) << 5) | + (((guint32) value & 0x001fU) << 19); + + return hi | + ((hi >> 5) & 0x00070007U) | + ((hi >> 6) & 0x00000300U); +} + + +static __inline__ guint16 +pack_555_mmx (__m64 rgb) +{ + const __m64 coeff = + _mm_set_pi16 (0, 1, 32, 1024); + + __m64 v; + __m64 hi; + + /* + * Input lanes: + * [ R G B A ] + * + * After shift: + * [ R5 G5 B5 x ] + */ + v = _mm_srli_pi16 (rgb, 3); + + /* + * PMADDWD: + * + * low32 = R5 * 1024 + G5 * 32 + * high32 = B5 * 1 + x * 0 + */ + v = _mm_madd_pi16 (v, coeff); + + hi = _mm_srli_si64 (v, 32); + v = _mm_add_pi32 (v, hi); + + return (guint16) _mm_cvtsi64_si32 (v); +} + + + +static __inline__ guint16 +pack_565_mmx (__m64 rgb) +{ + const __m64 coeff = + _mm_set_pi16 (0, 1, 64, 2048); + + const __m64 four = + _mm_set_pi16 (0, 0, 0, 4); + + __m64 v; + __m64 hi; + __m64 gbit; + + /* + * Full vector: + * + * rgb = [ R G B A ] + * + * Common shift: + * + * v = [ R5 G5 B5 x ] + */ + v = _mm_srli_pi16 (rgb, 3); + + /* + * low32 = R5 * 2048 + G5 * 64 + * high32 = B5 + */ + v = _mm_madd_pi16 (v, coeff); + + hi = _mm_srli_si64 (v, 32); + v = _mm_add_pi32 (v, hi); + + /* + * Extract original green into the low word. + * + * rgb >> 16 → low word = G + * + * The only green bit not represented by G>>3 is + * original bit 2. + * + * Its final RGB565 contribution is bit 5: + * + * (G & 4) << 3 + */ + gbit = _mm_srli_si64 (rgb, 16); + gbit = _mm_and_si64 (gbit, four); + gbit = _mm_slli_pi16 (gbit, 3); + + v = _mm_add_pi32 (v, gbit); + + return (guint16) _mm_cvtsi64_si32 (v); +} + + +static __inline__ __m64 +blend_rgb16_mmx (__m64 zero, + __m64 ff, + __m64 bias, + __m64 c257, + guint32 src, + guint32 dest) +{ + __m64 s; + __m64 d; + __m64 a; + __m64 ia; + __m64 t; + + s = _mm_cvtsi32_si64 ((int) src); + d = _mm_cvtsi32_si64 ((int) dest); + + /* + * Fully occupied 4 x 16-bit vectors: + * + * s = [ R G B A ] + * d = [ R G B 0 ] + */ + s = _mm_unpacklo_pi8 (s, zero); + d = _mm_unpacklo_pi8 (d, zero); + + /* + * Broadcast A through all four 16-bit lanes. + */ + a = _mm_srli_si64 (s, 48); + + __asm__ ("pshufw $0x00,%0,%0" + : "+y" (a)); + + ia = _mm_sub_pi16 (ff, a); + + t = _mm_add_pi16 (_mm_mullo_pi16 (s, a), + _mm_mullo_pi16 (d, ia)); + + t = _mm_add_pi16 (t, bias); + + /* + * Exact /255 for all four lanes: + * + * ((x + 128) * 257) >> 16 + */ + __asm__ ("pmulhuw %1,%0" + : "+y" (t) + : "y" (c257)); + + return t; +} + + +static __inline__ guint32 +blend_rgb16_scalar_tail (guint32 src, + guint32 dest) +{ + guint32 a; + guint32 ia; + guint32 r; + guint32 g; + guint32 b; + + a = src >> 24; + ia = 255U - a; + + r = (src & 0xffU) * a + + (dest & 0xffU) * ia; + + g = ((src >> 8) & 0xffU) * a + + ((dest >> 8) & 0xffU) * ia; + + b = ((src >> 16) & 0xffU) * a + + ((dest >> 16) & 0xffU) * ia; + + r += 128U; + r += r >> 8; + r >>= 8; + + g += 128U; + g += g >> 8; + g >>= 8; + + b += 128U; + b += b >> 8; + b >>= 8; + + return r | (g << 8) | (b << 16); +} + +static __inline__ guint16 +pack_555_scalar_tail (guint32 rgb) +{ + return (guint16) + (((rgb & 0x000000f8U) << 7) | + ((rgb & 0x0000f800U) >> 6) | + ((rgb & 0x00f80000U) >> 19)); +} + +static __inline__ guint16 +pack_565_scalar_tail (guint32 rgb) +{ + return (guint16) + (((rgb & 0x000000f8U) << 8) | + ((rgb & 0x0000fc00U) >> 5) | + ((rgb & 0x00f80000U) >> 19)); +} + +#define DEFINE_RGB16_MMX_COMPOSITOR(name, expand_pixel, pack_pixel, scalar_pack_pixel) \ +G_GNUC_INTERNAL void \ +name (guchar *src_buf, \ + gint src_rowstride, \ + guchar *dest_buf, \ + gint dest_rowstride, \ + GdkByteOrder dest_byte_order, \ + gint width, \ + gint height) \ +{ \ + if (width == 1) \ + { \ + guchar *sp_row = src_buf; \ + guchar *dp_row = dest_buf; \ + gint h = height; \ + \ + while (h--) \ + { \ + guint32 sp; \ + guint16 dp16; \ + guint32 dp; \ + guint32 rgb; \ + guint16 result; \ + \ + memcpy (&sp, sp_row, sizeof sp); \ + memcpy (&dp16, dp_row, sizeof dp16); \ + \ + dp = expand_pixel (dp16); \ + rgb = blend_rgb16_scalar_tail (sp, dp); \ + result = scalar_pack_pixel (rgb); \ + \ + memcpy (dp_row, &result, sizeof result); \ + \ + sp_row += src_rowstride; \ + dp_row += dest_rowstride; \ + } \ + \ + return; \ + } \ + \ + const __m64 zero = _mm_setzero_si64 (); \ + const __m64 ones = _mm_cmpeq_pi16 (zero, zero); \ + const __m64 ff = _mm_srli_pi16 (ones, 8); \ + const __m64 bias = _mm_set1_pi16 (0x80); \ + const __m64 c257 = _mm_set1_pi16 (257); \ + guchar *src = src_buf; \ + guchar *dest = dest_buf; \ + \ + (void) dest_byte_order; \ + \ + while (height--) \ + { \ + gint pairs = width >> 1; \ + guchar *p = src; \ + guchar *q = dest; \ + \ + while (pairs--) \ + { \ + guint32 sp0; \ + guint32 sp1; \ + guint32 dpair; \ + guint32 dp0; \ + guint32 dp1; \ + __m64 rgb0; \ + __m64 rgb1; \ + guint32 result; \ + \ + memcpy (&sp0, p, sizeof sp0); \ + memcpy (&sp1, p + 4, sizeof sp1); \ + memcpy (&dpair, q, sizeof dpair); \ + \ + dp0 = expand_pixel ((guint16) dpair); \ + dp1 = expand_pixel ((guint16) (dpair >> 16)); \ + \ + rgb0 = blend_rgb16_mmx (zero, ff, bias, c257, sp0, dp0); \ + rgb1 = blend_rgb16_mmx (zero, ff, bias, c257, sp1, dp1); \ + \ + result = (guint32) pack_pixel (rgb0); \ + result |= (guint32) pack_pixel (rgb1) << 16; \ + \ + memcpy (q, &result, sizeof result); \ + \ + p += 8; \ + q += 4; \ + } \ + \ + if (width & 1) \ + { \ + guint32 sp; \ + guint16 dp16; \ + guint16 result; \ + guint32 dp; \ + guint32 rgb; \ + \ + memcpy (&sp, p, sizeof sp); \ + memcpy (&dp16, q, sizeof dp16); \ + \ + dp = expand_pixel (dp16); \ + rgb = blend_rgb16_scalar_tail (sp, dp); \ + result = scalar_pack_pixel (rgb); \ + \ + memcpy (q, &result, sizeof result); \ + } \ + \ + src += src_rowstride; \ + dest += dest_rowstride; \ + } \ + \ + _mm_empty (); \ +} + +DEFINE_RGB16_MMX_COMPOSITOR ( + _gdk_pixel_ops_composite_555_mmxext, + expand_555, + pack_555_mmx, + pack_555_scalar_tail) + +DEFINE_RGB16_MMX_COMPOSITOR ( + _gdk_pixel_ops_composite_565_mmxext, + expand_565, + pack_565_mmx, + pack_565_scalar_tail) + +#undef DEFINE_RGB16_MMX_COMPOSITOR diff --git a/gdk/gdkpixelops-x86.c b/gdk/gdkpixelops-x86.c index 1e6f37f2f..5553a5488 100644 --- a/gdk/gdkpixelops-x86.c +++ b/gdk/gdkpixelops-x86.c @@ -13,6 +13,8 @@ #include +#define GDK_CPUID_AMD_MMXEXT (1U << 22) + G_GNUC_INTERNAL void _gdk_pixel_ops_composite_0888_mmx (guchar *src_buf, gint src_rowstride, @@ -40,6 +42,33 @@ _gdk_pixel_ops_composite_565_mmx (guchar *src_buf, gint width, gint height); +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_0888_mmxext (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_555_mmxext (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + +G_GNUC_INTERNAL void +_gdk_pixel_ops_composite_565_mmxext (guchar *src_buf, + gint src_rowstride, + guchar *dest_buf, + gint dest_rowstride, + GdkByteOrder dest_byte_order, + gint width, + gint height); + static gboolean gdk_pixel_ops_mmx_available; G_GNUC_INTERNAL gboolean @@ -55,16 +84,54 @@ _gdk_pixel_ops_init (void) unsigned int ebx; unsigned int ecx; unsigned int edx; + gboolean enhanced_mmx = FALSE; - if (__get_cpuid (1, &eax, &ebx, &ecx, &edx) && - (edx & bit_MMX) != 0) + if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx) || + (edx & bit_MMX) == 0) + return; + + gdk_pixel_ops_mmx_available = TRUE; + + _gdk_pixel_ops.composite_0888 = + _gdk_pixel_ops_composite_0888_mmx; + _gdk_pixel_ops.composite_555 = + _gdk_pixel_ops_composite_555_mmx; + _gdk_pixel_ops.composite_565 = + _gdk_pixel_ops_composite_565_mmx; + + /* + * Intel CPUs expose the MMX-form PSHUFW/PMULHUW + * instructions together with SSE. + * + * AMD CPUs may expose the same integer-MMX extension + * through extended CPUID bit 22 without SSE. + */ + if ((edx & bit_SSE) != 0) + enhanced_mmx = TRUE; + else if (__get_cpuid_max (0x80000000U, NULL) >= + 0x80000001U) + { + unsigned int ext_eax; + unsigned int ext_ebx; + unsigned int ext_ecx; + unsigned int ext_edx; + + if (__get_cpuid (0x80000001U, + &ext_eax, + &ext_ebx, + &ext_ecx, + &ext_edx) && + (ext_edx & GDK_CPUID_AMD_MMXEXT) != 0) + enhanced_mmx = TRUE; + } + + if (enhanced_mmx) { - gdk_pixel_ops_mmx_available = TRUE; _gdk_pixel_ops.composite_0888 = - _gdk_pixel_ops_composite_0888_mmx; + _gdk_pixel_ops_composite_0888_mmxext; _gdk_pixel_ops.composite_555 = - _gdk_pixel_ops_composite_555_mmx; + _gdk_pixel_ops_composite_555_mmxext; _gdk_pixel_ops.composite_565 = - _gdk_pixel_ops_composite_565_mmx; + _gdk_pixel_ops_composite_565_mmxext; } }