Skip to content

Fix stack overflow in DPIUtil.autoScaleImageData #2358

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

Conversation

arunjose696
Copy link
Contributor

@arunjose696 arunjose696 commented Jul 30, 2025

Fixes #2353

In PR #2249, a strict check was introduced that ImageData should be linearly scaled for
zoom levels. And imageDataProviders in SWT repo not following this strict checks were modified (eg to return only imageData at 100%).

However, this change broke DPIUtil.autoScaleImageData. That function creates an Image using the system zoom level, draws it on a GC at scaled dimensions, and then requests ImageData at 100% zoom from the image. Since the ImageDataProvider was now restricted to 100% zoom only, requesting imageData at other zoom levels during creation of image at system zoom level triggered recursive calls, causing a stack overflow error.

The current commit reverts the behavior of the ImageDataProvider used in DPIUtil.autoScaleImageData to return the same ImageData at all zoom levels.
We also Disable the strict zoom check in this specific case, because the scaling is done by GC.drawImage() and the ImageData used to create the image at system zoom level is expected to be same as the passed imageData.

Steps to reproduce

Execute Issue0445_HiDPISmoothScaling when your device zoom is something other than 200%. This would give stackoverflow error without this change

Copy link
Contributor

github-actions bot commented Jul 30, 2025

Test Results

   546 files  ±0     546 suites  ±0   29m 23s ⏱️ - 5m 2s
 4 425 tests +8   4 408 ✅ +8   17 💤 ±0  0 ❌ ±0 
16 746 runs  +8  16 619 ✅ +8  127 💤 ±0  0 ❌ ±0 

Results for commit a2cf98c. ± Comparison against base commit 7f19dc5.

♻️ This comment has been updated with latest results.

@arunjose696 arunjose696 force-pushed the arunjose696/374/AutoScaleStackoverflow branch 3 times, most recently from 739ebce to d3d106c Compare July 30, 2025 12:31
@arunjose696 arunjose696 force-pushed the arunjose696/374/AutoScaleStackoverflow branch 3 times, most recently from 8dcb140 to b1e2397 Compare July 30, 2025 13:33
@arunjose696 arunjose696 marked this pull request as ready for review July 30, 2025 13:35
@arunjose696 arunjose696 force-pushed the arunjose696/374/AutoScaleStackoverflow branch from b1e2397 to a2deff9 Compare July 30, 2025 13:39
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'm not able to judge the technical side of this PR but isn't it correct to say that it fixes #2353 ? If it does then the commit text needs to be adapted.

@arunjose696 arunjose696 force-pushed the arunjose696/374/AutoScaleStackoverflow branch from a2deff9 to 0b95661 Compare July 30, 2025 15:50
@arunjose696
Copy link
Contributor Author

I'm not able to judge the technical side of this PR but isn't it correct to say that it fixes #2353 ? If it does then the commit text needs to be adapted.

Yes this issue fixes #2353 . Now I have mentioned breifly mentioned this issue in the commit text.

@fedejeanne
Copy link
Contributor

Thank you for adding the information. I'm not able to judge this PR at a technical level (maybe @akoch-yatta / @HeikoKlare can) but I can tell that at least this PR does get rid of the stack overflow, as it promises. ✔️

@arunjose696
Copy link
Contributor Author

The failing test seems to be unrelated to the change. I have raised an issue about the failures #2363

Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

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

The change is sound, thank you!

I only have two minor comments.

@@ -394,6 +392,10 @@ public static void setMonitorSpecificScaling(boolean activate) {
System.setProperty(SWT_AUTOSCALE_UPDATE_ON_RUNTIME, Boolean.toString(activate));
}

public static void setAutoScaleValue(String autoScaleValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add a JavaDoc that states that this method is only supposed to be called in tests and may lead to unintended effects if called in production.

"1.0, 200, false", "2.0, 200, false", "2.0, quarter, false", })
public void autoScaleImageData0(float scaleFactor, String autoScale, boolean monitorSpecificScaling) {
DPIUtil.setMonitorSpecificScaling(monitorSpecificScaling);
DPIUtil.setAutoScaleValue(autoScale);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the value is only initialized on class loading (i.e., at JVM startup), changing this value in a test without resetting it will leave the JVM in an unexpected state for the subsequent tests. To avoid that, the original value should be stored and reapplied after executing the test. This has to be done in the finally block, as otherwise a test failure would still leave behind an unexpected system state.

@HeikoKlare HeikoKlare force-pushed the arunjose696/374/AutoScaleStackoverflow branch 2 times, most recently from f273153 to bd1e417 Compare August 3, 2025 12:15
Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

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

I just saw that we already have DPIUtil#runWithAutoScaleValue() which can be used to run some code with a specific autoScale value making the setAutoScaleValue method for test purposes obsolete. I have adapted the test implementation with a usage of that method and removed the obsolete new method from DPIUtil, which renders my previous comments obsolete.

@arunjose696 please check if the change is fine for you.

@arunjose696 arunjose696 force-pushed the arunjose696/374/AutoScaleStackoverflow branch from bd1e417 to 5d514a4 Compare August 4, 2025 11:00
Recently, a strict check was introduced that ImageData should be
linearly scaled for other zoom levels. ImageDataProviders inside SWT
not following this strict checks were modified (e.g., to return only
image data at 100%).

However, this change broke DPIUtil.autoScaleImageData. That function
creates an Image using the system zoom level, draws it on a GC at scaled
dimensions, and then requests ImageData at 100% zoom from the image.
Since the ImageDataProvider was now restricted to 100% zoom only,
requesting imageData at other zoom levels while creating an image at
system zoom level triggered recursive calls, causing a stack overflow
error.

This commit reverts the behavior of the ImageDataProvider used in
DPIUtil.autoScaleImageData to return the same ImageData at all zoom
levels.
We also disable the strict zoom check in this specific case, because the
scaling is done by GC.drawImage() and the ImageData used to create the
is expected to be same.

Fixes eclipse-platform#2353
@arunjose696 arunjose696 force-pushed the arunjose696/374/AutoScaleStackoverflow branch from 5d514a4 to a2cf98c Compare August 4, 2025 11:44
@arunjose696
Copy link
Contributor Author

arunjose696 commented Aug 4, 2025

@arunjose696 please check if the change is fine for you.

The new changes look good to me, Additionally I have added image disposals for the new images created which I missed.

@HeikoKlare
Copy link
Contributor

The new changes look good to me, Additionally I have added image disposals for the new images created which I missed.

Great, thanks!

@HeikoKlare HeikoKlare merged commit 007426f into eclipse-platform:master Aug 4, 2025
17 checks passed
@HeikoKlare HeikoKlare deleted the arunjose696/374/AutoScaleStackoverflow branch August 4, 2025 15:56
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.

StackOverflowError in Image when auto-scaling differs from device zoom StackOverflowError in Image when auto-scaling differs from device zoom
3 participants