Skip to content

[win32] Refresh GC handle when related image #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

akoch-yatta
Copy link
Contributor

@akoch-yatta akoch-yatta commented Jun 25, 2025

This PR adapts the behavior of the commonly used pattern of a GC that draw onto a plain image. In the windows implementation the monitor specific scaling could lead to issues with this pattern, because during creation time of an GC/Image combo it was not always clear with which zoom the final result will be drawn upon. This commit will redraw the GC on an image handle if a new zoom is requested. The handle for the old zoom of the image will be kept, but if another operation of the GC is executed it will only be applied on the new handle.

How to test

You can use this small windows specific snippet to force an image redraw with a different zoom

package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SnippetGcRedraw {

	public static void main (String [] args) {
		Display display = new Display ();
		Shell shell = new Shell (display);
		shell.setText("GC Redraw");
		shell.setLayout(new FillLayout());
		int width = 150, height = 200;
		Image image = new Image (display, width, height);

		GCData data = new GCData();
		GC gc = GC.win32_new(image, data);
		gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
		gc.fillRectangle (0, 0, width, height);
		gc.drawLine (0, 0, width, height);
		gc.drawLine (0, height, width, 0);
		gc.drawText ("Default Image", 10, 10);
		final Point origin = new Point (0, 0);
		final Canvas canvas = new Canvas (shell, SWT.NONE);
		canvas.addListener (SWT.Paint, e -> {
			GC gc2 = e.gc;
			gc2.getGCData().nativeZoom = 150;
			gc2.drawImage (image, origin.x, origin.y);
			gc2.getGCData().nativeZoom = 200;
			gc2.drawImage (image, origin.x + 300, origin.y);
		});
		shell.setSize (1000, 1000);
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		gc.dispose ();
		image.dispose();
		display.dispose ();
	}
}

Before

If the image was used with a GC the same ImageData were used for all zooms
Screenshot 2025-06-25 080153

After

Left image is drawn for 150%, right is redrawn for 200%
Screenshot 2025-06-25 080338

This PR relies upon #2243 and must be merged after it. Only the latest commit is part of this PR

Copy link
Contributor

github-actions bot commented Jun 25, 2025

Test Results

   545 files  ±0     545 suites  ±0   31m 46s ⏱️ - 2m 2s
 4 403 tests ±0   4 385 ✅ ±0   18 💤 ±0  0 ❌ ±0 
16 733 runs  ±0  16 593 ✅ ±0  140 💤 ±0  0 ❌ ±0 

Results for commit ad24d4a. ± Comparison against base commit e7478cd.

♻️ This comment has been updated with latest results.

@akoch-yatta akoch-yatta linked an issue Jun 25, 2025 that may be closed by this pull request
@akoch-yatta akoch-yatta changed the title Refresh GC handle when related image [win32] Refresh GC handle when related image Jun 25, 2025
This commit adapts the behavior of the commonly used pattern of a GC
that draw onto a plain image. In the windows implementation the monitor
specific scaling could lead to issues with this pattern, because during
creation time of an GC/Image combo it was not always clear with which
zoom the final result will be drawn upon. This commit will redraw the
GC on an image handle if a new zoom is requested. The handle for the
old zoom of the image will be kept, but if another operation of the GC
is exeuted it will only be applied on the new handle.
@akoch-yatta akoch-yatta force-pushed the win32-image-refresh-gc branch from 125853c to ad24d4a Compare June 26, 2025 09:41
Copy link
Contributor

@fedejeanne fedejeanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and it looks good.

I only have one question (#2263 (comment)) but even with that change, the code looks good.

@fedejeanne fedejeanne merged commit 8a8c67d into eclipse-platform:master Jun 27, 2025
17 checks passed
@fedejeanne fedejeanne deleted the win32-image-refresh-gc branch June 27, 2025 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refresh image handle with GC
2 participants