Skip to content

Commit e9225b5

Browse files
committed
[win32] Use complex zoom context in Image
This commit extends the internal zoom used in Image in the windows implementation. This is preparatory work to provide a consistent behavior independent of the auto scale mode, when an Image is drawn with a GC
1 parent c606298 commit e9225b5

File tree

1 file changed

+62
-42
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+62
-42
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,12 @@ private ImageData applyGrayImageData(ImageData data, int pHeight, int pWidth) {
803803
return newData;
804804
}
805805

806-
private ImageHandle getImageMetadata(int zoom) {
806+
private ImageHandle getImageMetadata(ZoomContext zoomContext) {
807+
int zoom = zoomContext.targetZoom;
807808
if (zoomLevelToImageHandle.get(zoom) != null) {
808809
return zoomLevelToImageHandle.get(zoom);
809810
}
810-
return imageProvider.newImageHandle(zoom);
811+
return imageProvider.newImageHandle(zoomContext);
811812
}
812813

813814

@@ -831,7 +832,7 @@ public static long win32_getHandle (Image image, int zoom) {
831832
if(image.isDisposed()) {
832833
return 0L;
833834
}
834-
return image.getImageMetadata(zoom).handle;
835+
return image.getImageMetadata(new ZoomContext(zoom)).handle;
835836
}
836837

837838
/**
@@ -1265,7 +1266,7 @@ public ImageData getImageData (int zoom) {
12651266
if (zoomLevelToImageHandle.containsKey(zoom)) {
12661267
return zoomLevelToImageHandle.get(zoom).getImageData();
12671268
}
1268-
return this.imageProvider.newImageData(zoom);
1269+
return this.imageProvider.newImageData(new ZoomContext(zoom));
12691270
}
12701271

12711272

@@ -1882,7 +1883,7 @@ public String toString () {
18821883

18831884
<T> T applyUsingAnyHandle(Function<ImageHandle, T> function) {
18841885
if (zoomLevelToImageHandle.isEmpty()) {
1885-
ImageHandle temporaryHandle = this.imageProvider.newImageHandle(DPIUtil.getDeviceZoom());
1886+
ImageHandle temporaryHandle = this.imageProvider.newImageHandle(new ZoomContext(DPIUtil.getDeviceZoom()));
18861887
try {
18871888
return function.apply(temporaryHandle);
18881889
} finally {
@@ -1913,6 +1914,20 @@ public static Image win32_new(Device device, int type, long handle, int nativeZo
19131914
return new Image(device, type, handle, nativeZoom);
19141915
}
19151916

1917+
private static class ZoomContext {
1918+
private int targetZoom;
1919+
private int nativeZoom;
1920+
1921+
private ZoomContext(int targetZoom) {
1922+
this(targetZoom, targetZoom);
1923+
}
1924+
1925+
private ZoomContext(int targetZoom, int nativeZoom) {
1926+
this.targetZoom = targetZoom;
1927+
this.nativeZoom = nativeZoom;
1928+
}
1929+
}
1930+
19161931
private abstract class AbstractImageProviderWrapper {
19171932

19181933
protected abstract Rectangle getBounds(int zoom);
@@ -1925,30 +1940,30 @@ public Collection<Integer> getPreservedZoomLevels() {
19251940
return Collections.emptySet();
19261941
}
19271942

1928-
abstract ImageData newImageData(int zoom);
1943+
abstract ImageData newImageData(ZoomContext zoomContext);
19291944

19301945
abstract AbstractImageProviderWrapper createCopy(Image image);
19311946

19321947
ImageData getScaledImageData (int zoom) {
19331948
TreeSet<Integer> availableZooms = new TreeSet<>(zoomLevelToImageHandle.keySet());
19341949
int closestZoom = Optional.ofNullable(availableZooms.higher(zoom)).orElse(availableZooms.lower(zoom));
1935-
return DPIUtil.scaleImageData(device, getImageMetadata(closestZoom).getImageData(), zoom, closestZoom);
1950+
return DPIUtil.scaleImageData(device, getImageMetadata(new ZoomContext(closestZoom)).getImageData(), zoom, closestZoom);
19361951
}
19371952

1938-
protected ImageHandle newImageHandle(int zoom) {
1939-
ImageData resizedData = getImageData(zoom);
1940-
return newImageHandle(resizedData, zoom);
1953+
protected ImageHandle newImageHandle(ZoomContext zoomContext) {
1954+
ImageData resizedData = getImageData(zoomContext.targetZoom);
1955+
return newImageHandle(resizedData, zoomContext);
19411956
}
19421957

1943-
protected final ImageHandle newImageHandle(ImageData data, int zoom) {
1958+
protected final ImageHandle newImageHandle(ImageData data, ZoomContext zoomContext) {
19441959
if (type == SWT.ICON && data.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
19451960
// If the original type was an icon with transparency mask and re-scaling leads
19461961
// to image data without transparency mask, this will create invalid images
19471962
// so this fallback will "repair" the image data by explicitly passing
19481963
// the transparency mask created from the scaled image data
1949-
return initIconHandle(device, data, data.getTransparencyMask(), zoom);
1964+
return initIconHandle(device, data, data.getTransparencyMask(), zoomContext.targetZoom);
19501965
} else {
1951-
return init(data, zoom);
1966+
return init(data, zoomContext.targetZoom);
19521967
}
19531968
}
19541969
}
@@ -1977,8 +1992,8 @@ protected Rectangle getBounds(int zoom) {
19771992
}
19781993

19791994
@Override
1980-
ImageData newImageData(int zoom) {
1981-
return getScaledImageData(zoom);
1995+
ImageData newImageData(ZoomContext zoomContext) {
1996+
return getScaledImageData(zoomContext.targetZoom);
19821997
}
19831998

19841999
@Override
@@ -2000,32 +2015,32 @@ private abstract class ImageFromImageDataProviderWrapper extends AbstractImagePr
20002015
void initImage() {
20012016
// As the init call configured some Image attributes (e.g. type)
20022017
// it must be called
2003-
newImageData(100);
2018+
newImageData(new ZoomContext(100));
20042019
}
20052020

20062021
@Override
2007-
ImageData newImageData(int zoom) {
2022+
ImageData newImageData(ZoomContext zoomContext) {
20082023
Function<Integer, ImageData> imageDataRetrieval = zoomToRetrieve -> {
2009-
ImageHandle handle = initializeHandleFromSource(zoomToRetrieve);
2024+
ImageHandle handle = initializeHandleFromSource(zoomContext);
20102025
ImageData data = handle.getImageData();
20112026
handle.destroy();
20122027
return data;
20132028
};
2014-
return cachedImageData.computeIfAbsent(zoom, imageDataRetrieval);
2029+
return cachedImageData.computeIfAbsent(zoomContext.targetZoom, imageDataRetrieval);
20152030
}
20162031

20172032
@Override
2018-
protected ImageHandle newImageHandle(int zoom) {
2019-
ImageData cachedData = cachedImageData.remove(zoom);
2033+
protected ImageHandle newImageHandle(ZoomContext zoomContext) {
2034+
ImageData cachedData = cachedImageData.remove(zoomContext.targetZoom);
20202035
if (cachedData != null) {
2021-
return newImageHandle(cachedData, zoom);
2036+
return newImageHandle(cachedData, zoomContext);
20222037
}
2023-
return initializeHandleFromSource(zoom);
2038+
return initializeHandleFromSource(zoomContext);
20242039
}
20252040

2026-
private ImageHandle initializeHandleFromSource(int zoom) {
2027-
ElementAtZoom<ImageData> imageDataAtZoom = loadImageData(zoom);
2028-
ImageData imageData = DPIUtil.scaleImageData(device, imageDataAtZoom.element(), zoom, imageDataAtZoom.zoom());
2041+
private ImageHandle initializeHandleFromSource(ZoomContext zoom) {
2042+
ElementAtZoom<ImageData> imageDataAtZoom = loadImageData(zoom.targetZoom);
2043+
ImageData imageData = DPIUtil.scaleImageData(device, imageDataAtZoom.element(), zoom.targetZoom, imageDataAtZoom.zoom());
20292044
imageData = adaptImageDataIfDisabledOrGray(imageData);
20302045
return newImageHandle(imageData, zoom);
20312046
}
@@ -2159,20 +2174,22 @@ protected Rectangle getBounds(int zoom) {
21592174
}
21602175

21612176
@Override
2162-
ImageData newImageData(int zoom) {
2177+
ImageData newImageData(ZoomContext zoomContext) {
2178+
int zoom = zoomContext.targetZoom;
21632179
if (zoomLevelToImageHandle.isEmpty()) {
21642180
return createBaseHandle(zoom).getImageData();
21652181
}
21662182
// if a GC is initialized with an Image (memGC != null), the image data must not be resized, because it would
21672183
// be a destructive operation. Therefor, a new handle is created for the requested zoom
21682184
if (memGC != null) {
2169-
return newImageHandle(zoom).getImageData();
2185+
return newImageHandle(zoomContext).getImageData();
21702186
}
21712187
return getScaledImageData(zoom);
21722188
}
21732189

21742190
@Override
2175-
protected ImageHandle newImageHandle(int zoom) {
2191+
protected ImageHandle newImageHandle(ZoomContext zoomContext) {
2192+
int zoom = zoomContext.targetZoom;
21762193
if (zoomLevelToImageHandle.isEmpty()) {
21772194
return createBaseHandle(zoom);
21782195
}
@@ -2183,7 +2200,7 @@ protected ImageHandle newImageHandle(int zoom) {
21832200
currentGC.refreshFor(new DrawableWrapper(Image.this, zoom), zoom);
21842201
return zoomLevelToImageHandle.get(zoom);
21852202
}
2186-
return super.newImageHandle(zoom);
2203+
return super.newImageHandle(zoomContext);
21872204
}
21882205
private ImageHandle createBaseHandle(int zoom) {
21892206
baseZoom = zoom;
@@ -2272,19 +2289,20 @@ Object getProvider() {
22722289
}
22732290

22742291
@Override
2275-
ImageData newImageData(int zoom) {
2292+
ImageData newImageData(ZoomContext zoomContext) {
22762293
Function<Integer, ImageData> imageDataRetrival = zoomToRetrieve -> {
22772294
ImageHandle handle = initializeHandleFromSource(zoomToRetrieve);
22782295
ImageData data = handle.getImageData();
22792296
handle.destroy();
22802297
return data;
22812298
};
2282-
return cachedImageData.computeIfAbsent(zoom, imageDataRetrival);
2299+
return cachedImageData.computeIfAbsent(zoomContext.targetZoom, imageDataRetrival);
22832300
}
22842301

22852302

22862303
@Override
2287-
protected ImageHandle newImageHandle(int zoom) {
2304+
protected ImageHandle newImageHandle(ZoomContext zoomContext) {
2305+
int zoom = zoomContext.targetZoom;
22882306
ImageData cachedData = cachedImageData.remove(zoom);
22892307
if (cachedData != null) {
22902308
return init(cachedData, zoom);
@@ -2313,7 +2331,7 @@ private class ImageFileNameProviderWrapper extends BaseImageProviderWrapper<Imag
23132331
super(provider, ImageFileNameProvider.class);
23142332
// Checks for the contract of the passed provider require
23152333
// checking for valid image data creation
2316-
newImageData(DPIUtil.getDeviceZoom());
2334+
newImageData(new ZoomContext(DPIUtil.getDeviceZoom()));
23172335
}
23182336

23192337
@Override
@@ -2567,7 +2585,7 @@ private class ImageGcDrawerWrapper extends DynamicImageProviderWrapper {
25672585
private ImageGcDrawer drawer;
25682586
private int width;
25692587
private int height;
2570-
private int currentZoom = 100;
2588+
private ZoomContext currentZoom = new ZoomContext(100);
25712589

25722590
ImageGcDrawerWrapper(ImageGcDrawer imageGcDrawer, int width, int height) {
25732591
checkProvider(imageGcDrawer, ImageGcDrawer.class);
@@ -2584,17 +2602,19 @@ protected Rectangle getBounds(int zoom) {
25842602

25852603
@Override
25862604
protected long configureGCData(GCData data) {
2587-
return configureGC(data, currentZoom);
2605+
return configureGC(data, currentZoom.nativeZoom);
25882606
}
25892607

25902608
@Override
2591-
ImageData newImageData(int zoom) {
2592-
return getImageMetadata(zoom).getImageData();
2609+
ImageData newImageData(ZoomContext zoomContext) {
2610+
return getImageMetadata(zoomContext).getImageData();
25932611
}
25942612

25952613
@Override
2596-
protected ImageHandle newImageHandle(int zoom) {
2597-
currentZoom = zoom;
2614+
protected ImageHandle newImageHandle(ZoomContext zoomContext) {
2615+
currentZoom = zoomContext;
2616+
int zoom = zoomContext.targetZoom;
2617+
int nativeZoom = zoomContext.nativeZoom;
25982618
int gcStyle = drawer.getGcStyle();
25992619
Image image;
26002620
if ((gcStyle & SWT.TRANSPARENT) != 0) {
@@ -2607,10 +2627,10 @@ protected ImageHandle newImageHandle(int zoom) {
26072627
} else {
26082628
image = new Image(device, width, height);
26092629
}
2610-
GC gc = new GC(new DrawableWrapper(image, zoom), gcStyle);
2630+
GC gc = new GC(new DrawableWrapper(image, nativeZoom), gcStyle);
26112631
try {
26122632
drawer.drawOn(gc, width, height);
2613-
ImageData imageData = image.getImageMetadata(zoom).getImageData();
2633+
ImageData imageData = image.getImageData(nativeZoom);
26142634
drawer.postProcess(imageData);
26152635
ImageData newData = adaptImageDataIfDisabledOrGray(imageData);
26162636
return init(newData, zoom);

0 commit comments

Comments
 (0)