Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
try {
init (data, 100);
init ();
StrictChecks.runIfStrictChecksEnabled(() -> {
DPIUtil.validateLinearScaling(imageDataProvider);
});
ImageData data2x = imageDataProvider.getImageData (200);
if (data2x != null) {
alphaInfo_200 = new AlphaInfo();
Expand Down Expand Up @@ -1822,13 +1825,18 @@ public String toString () {
* @noreference This method is not intended to be referenced by clients.
*/
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage (imageToDraw, 0, 0, CocoaDPIUtil.pixelToPoint (width), CocoaDPIUtil.pixelToPoint (height),
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
*/
0, 0, Math.round (CocoaDPIUtil.pixelToPoint (width * scaleFactor)), Math.round (CocoaDPIUtil.pixelToPoint (height * scaleFactor)));
imageToDraw.dispose();
StrictChecks.runWithStrictChecksDisabled(() -> {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage(imageToDraw, 0, 0, CocoaDPIUtil.pixelToPoint(width), CocoaDPIUtil.pixelToPoint(height),
/*
* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but
* avoiding rounding errors. Nevertheless, we still have some rounding errors
* due to the point-based API GC#drawImage(..).
*/
0, 0, Math.round(CocoaDPIUtil.pixelToPoint(width * scaleFactor)),
Math.round(CocoaDPIUtil.pixelToPoint(height * scaleFactor)));
imageToDraw.dispose();
});
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,26 @@ public static int mapZoomToDPI (int zoom) {
return roundedDpi;
}

public static void validateLinearScaling(ImageDataProvider provider) {
final int baseZoom = 100;
final int scaledZoom = 200;
final int scaleFactor = scaledZoom / baseZoom;
ImageData baseImageData = provider.getImageData(baseZoom);
ImageData scaledImageData = provider.getImageData(scaledZoom);

if (scaledImageData == null) {
return;
}

if (scaledImageData.width != scaleFactor * baseImageData.width
|| scaledImageData.height != scaleFactor * baseImageData.height) {
System.err.println(String.format(
"***WARNING: ImageData should be linearly scaled across zooms but size is (%d, %d) at 100%% and (%d, %d) at 200%%.",
baseImageData.width, baseImageData.height, scaledImageData.width, scaledImageData.height));
new Error().printStackTrace(System.err);
}
}

/**
* Represents an element, such as some image data, at a specific zoom level.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2025 Yatta Solutions
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.internal;

public final class StrictChecks {

private static final boolean STRICT_CHECKS_ENABLED = System
.getProperty("org.eclipse.swt.internal.enableStrictChecks") != null;

private static boolean temporarilyDisabled = false;

private StrictChecks() {
}

public static void runIfStrictChecksEnabled(Runnable runnable) {
if (STRICT_CHECKS_ENABLED && !temporarilyDisabled) {
runnable.run();
}
}

public static void runWithStrictChecksDisabled(Runnable runnable) {
temporarilyDisabled = true;
try {
runnable.run();
} finally {
temporarilyDisabled = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
currentDeviceZoom = DPIUtil.getDeviceZoom();
initFromImageDataProvider(currentDeviceZoom);
init ();
StrictChecks.runIfStrictChecksEnabled(() -> {
DPIUtil.validateLinearScaling(imageDataProvider);
});
}

/**
Expand Down Expand Up @@ -1583,13 +1586,17 @@ public String toString () {
* @noreference This method is not intended to be referenced by clients.
*/
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage (imageToDraw, 0, 0, width, height,
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
*/
0, 0, Math.round (width * scaleFactor), Math.round (height * scaleFactor));
imageToDraw.dispose();
StrictChecks.runWithStrictChecksDisabled(() -> {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage(imageToDraw, 0, 0, width, height,
/*
* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but
* avoiding rounding errors. Nevertheless, we still have some rounding errors
* due to the point-based API GC#drawImage(..).
*/
0, 0, Math.round(width * scaleFactor), Math.round(height * scaleFactor));
imageToDraw.dispose();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@
*/
public class Display extends Device implements Executor {

static boolean strictChecks = System.getProperty("org.eclipse.swt.internal.enableStrictChecks") != null;

private static final int SLOT_IN_USE = -2;
private static final int LAST_TABLE_INDEX = -1;

Expand Down Expand Up @@ -880,12 +878,12 @@ void addWidget (long handle, Widget widget) {
widgetTable = newWidgetTable;
}
int index = freeSlot + 1;
if(strictChecks) {
StrictChecks.runIfStrictChecksEnabled(() -> {
long data = OS.g_object_get_qdata (handle, SWT_OBJECT_INDEX);
if(data > 0 && data != index) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, ". Potential leak of " + widget + debugInfoForIndex(data - 1));
}
}
});
OS.g_object_set_qdata (handle, SWT_OBJECT_INDEX, index);
int oldSlot = freeSlot;
freeSlot = indexTable[oldSlot];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3258,14 +3258,14 @@ void deregister () {
if(shellHandle != 0 && !(disposed instanceof Shell)) {
SWT.error(SWT.ERROR_INVALID_RETURN_VALUE, null, ". Wrong widgetTable entry: " + disposed + " removed for shell: " + this + display.dumpWidgetTableInfo());
}
if(Display.strictChecks) {
StrictChecks.runIfStrictChecksEnabled(() -> {
Shell[] shells = display.getShells();
for (Shell shell : shells) {
if(shell == this) {
SWT.error(SWT.ERROR_INVALID_RETURN_VALUE, null, ". Disposed shell still in the widgetTable: " + this + display.dumpWidgetTableInfo());
}
}
}
});
}

boolean requiresUngrab () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
*/
public abstract class Device implements Drawable {

static boolean strictChecks = System.getProperty("org.eclipse.swt.internal.enableStrictChecks") != null;

/* Debugging */
public static boolean DEBUG;
boolean debug = DEBUG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ private void validateGCState() {
}

void checkGC(int mask) {
if (Device.strictChecks) {
StrictChecks.runIfStrictChecksEnabled(() -> {
validateGCState();
}
});
int state = data.state;
if ((state & mask) == mask) return;
state = (state ^ mask) & mask;
Expand Down Expand Up @@ -4402,10 +4402,10 @@ private void init(Drawable drawable, GCData data, long hDC) {
}

private static int extractZoom(long hDC) {
if (Device.strictChecks) {
StrictChecks.runIfStrictChecksEnabled(() -> {
System.err.println("***WARNING: GC is initialized with a missing zoom. This indicates an "
+ "incompatible custom Drawable implementation.");
}
});
long hwnd = OS.WindowFromDC(hDC);
long parentWindow = OS.GetAncestor(hwnd, OS.GA_ROOT);
long monitorParent = OS.MonitorFromWindow(parentWindow, OS.MONITOR_DEFAULTTONEAREST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,33 +612,13 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
": ImageDataProvider [" + imageDataProvider + "] returns null ImageData at 100% zoom.");
}
if (Device.strictChecks) {
validateLinearScaling(imageDataProvider);
}
StrictChecks.runIfStrictChecksEnabled(() -> {
DPIUtil.validateLinearScaling(imageDataProvider);
});
init();
this.device.registerResourceWithZoomSupport(this);
}

private void validateLinearScaling(ImageDataProvider provider) {
final int baseZoom = 100;
final int scaledZoom = 200;
final int scaleFactor = scaledZoom / baseZoom;
ImageData baseImageData = provider.getImageData(baseZoom);
ImageData scaledImageData = provider.getImageData(scaledZoom);

if (scaledImageData == null) {
return;
}

if (scaledImageData.width != scaleFactor * baseImageData.width
|| scaledImageData.height != scaleFactor * baseImageData.height) {
System.err.println(String.format(
"***WARNING: ImageData should be linearly scaled across zooms but size is (%d, %d) at 100%% and (%d, %d) at 200%%.",
baseImageData.width, baseImageData.height, scaledImageData.width, scaledImageData.height));
new Error().printStackTrace(System.err);
}
}

/**
* The provided ImageGcDrawer will be called on demand whenever a new variant of the
* Image for an additional zoom is required. Depending on the OS-specific implementation
Expand Down Expand Up @@ -851,13 +831,13 @@ public static long win32_getHandle (Image image, int zoom) {
* @noreference This method is not intended to be referenced by clients.
*/
public static void drawScaled(GC gc, ImageData imageData, int width, int height, float scaleFactor) {
boolean originalStrictChecks = Device.strictChecks;
Device.strictChecks = false;
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage (imageToDraw, 0, 0, width, height,
0, 0, Math.round (width * scaleFactor), Math.round (height * scaleFactor), false);
Device.strictChecks = originalStrictChecks;
imageToDraw.dispose();

StrictChecks.runWithStrictChecksDisabled(() -> {
Image imageToDraw = new Image(gc.device, (ImageDataProvider) zoom -> imageData);
gc.drawImage(imageToDraw, 0, 0, width, height, 0, 0, Math.round(width * scaleFactor),
Math.round(height * scaleFactor), false);
imageToDraw.dispose();
});
}

long [] createGdipImage(Integer zoom) {
Expand Down Expand Up @@ -1753,9 +1733,9 @@ private long configureGC(GCData data, ZoomContext zoomContext) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}

if (Device.strictChecks) {
StrictChecks.runIfStrictChecksEnabled(() -> {
checkImageTypeForValidCustomDrawing(zoomContext.targetZoom());
}
});
/* Create a compatible HDC for the device */
long hDC = device.internal_new_GC(null);
long imageDC = OS.CreateCompatibleDC(hDC);
Expand Down
Loading