|
18 | 18 |
|
19 | 19 | import java.io.*; |
20 | 20 | import java.util.*; |
| 21 | +import java.util.Map.*; |
21 | 22 | import java.util.function.*; |
22 | 23 |
|
23 | 24 | import org.eclipse.swt.*; |
@@ -1005,40 +1006,30 @@ void destroy () { |
1005 | 1006 | device.deregisterResourceWithZoomSupport(this); |
1006 | 1007 | if (memGC != null) memGC.dispose(); |
1007 | 1008 | this.isDestroyed = true; |
1008 | | - destroyHandle(); |
| 1009 | + destroyHandles(); |
1009 | 1010 | memGC = null; |
1010 | 1011 | } |
1011 | 1012 |
|
1012 | | -private void destroyHandle () { |
1013 | | - for (ImageHandle imageMetadata : zoomLevelToImageHandle.values()) { |
1014 | | - destroyHandle(imageMetadata.handle); |
1015 | | - } |
1016 | | - zoomLevelToImageHandle.clear(); |
| 1013 | +private void destroyHandles() { |
| 1014 | + destroyHandles(__ -> true); |
1017 | 1015 | } |
1018 | 1016 |
|
1019 | 1017 | @Override |
1020 | 1018 | void destroyHandlesExcept(Set<Integer> zoomLevels) { |
1021 | | - zoomLevelToImageHandle.entrySet().removeIf(entry -> { |
1022 | | - final Integer zoom = entry.getKey(); |
1023 | | - if (!zoomLevels.contains(zoom) && zoom != DPIUtil.getZoomForAutoscaleProperty(initialNativeZoom)) { |
1024 | | - destroyHandle(entry.getValue().handle); |
1025 | | - return true; |
1026 | | - } |
1027 | | - return false; |
| 1019 | + destroyHandles(zoom -> { |
| 1020 | + return !zoomLevels.contains(zoom) && zoom != DPIUtil.getZoomForAutoscaleProperty(initialNativeZoom); |
1028 | 1021 | }); |
1029 | 1022 | } |
1030 | | -private void destroyHandleForZoom(int zoom) { |
1031 | | - ImageHandle imageHandle = zoomLevelToImageHandle.remove(zoom); |
1032 | | - if (imageHandle != null) { |
1033 | | - destroyHandle(imageHandle.handle); |
1034 | | - } |
1035 | | -} |
1036 | 1023 |
|
1037 | | -private void destroyHandle(long handle) { |
1038 | | - if (type == SWT.ICON) { |
1039 | | - OS.DestroyIcon (handle); |
1040 | | - } else { |
1041 | | - OS.DeleteObject (handle); |
| 1024 | +private void destroyHandles(Predicate<Integer> filter) { |
| 1025 | + Iterator<Entry<Integer, ImageHandle>> it = zoomLevelToImageHandle.entrySet().iterator(); |
| 1026 | + while (it.hasNext()) { |
| 1027 | + Entry<Integer, ImageHandle> zoomToHandle = it.next(); |
| 1028 | + if (filter.test(zoomToHandle.getKey())) { |
| 1029 | + ImageHandle imageHandle = zoomToHandle.getValue(); |
| 1030 | + it.remove(); |
| 1031 | + imageHandle.destroy(); |
| 1032 | + } |
1042 | 1033 | } |
1043 | 1034 | } |
1044 | 1035 |
|
@@ -1940,7 +1931,7 @@ ImageData newImageData(int zoom) { |
1940 | 1931 | Function<Integer, ImageData> imageDataRetrieval = zoomToRetrieve -> { |
1941 | 1932 | ImageHandle handle = initializeHandleFromSource(zoomToRetrieve); |
1942 | 1933 | ImageData data = handle.getImageData(); |
1943 | | - destroyHandleForZoom(zoomToRetrieve); |
| 1934 | + handle.destroy(); |
1944 | 1935 | return data; |
1945 | 1936 | }; |
1946 | 1937 | return cachedImageData.computeIfAbsent(zoom, imageDataRetrieval); |
@@ -2174,7 +2165,7 @@ ImageData newImageData(int zoom) { |
2174 | 2165 | Function<Integer, ImageData> imageDataRetrival = zoomToRetrieve -> { |
2175 | 2166 | ImageHandle handle = initializeHandleFromSource(zoomToRetrieve); |
2176 | 2167 | ImageData data = handle.getImageData(); |
2177 | | - destroyHandleForZoom(zoomToRetrieve); |
| 2168 | + handle.destroy(); |
2178 | 2169 | return data; |
2179 | 2170 | }; |
2180 | 2171 | return cachedImageData.computeIfAbsent(zoom, imageDataRetrival); |
@@ -2236,7 +2227,7 @@ protected ElementAtZoom<ImageData> loadImageData(int zoom) { |
2236 | 2227 | imageDataAtZoom = ImageDataLoader.load(fileForZoom.element(), fileForZoom.zoom(), zoom); |
2237 | 2228 | } else { |
2238 | 2229 | imageDataAtZoom = new ElementAtZoom<>(nativeInitializedImage.getImageData(), fileForZoom.zoom()); |
2239 | | - destroyHandleForZoom(fileForZoom.zoom()); |
| 2230 | + nativeInitializedImage.destroy(); |
2240 | 2231 | } |
2241 | 2232 | return imageDataAtZoom; |
2242 | 2233 | } |
@@ -2525,7 +2516,7 @@ public boolean equals(Object otherProvider) { |
2525 | 2516 | } |
2526 | 2517 |
|
2527 | 2518 | private class ImageHandle { |
2528 | | - private final long handle; |
| 2519 | + private long handle; |
2529 | 2520 | private final int zoom; |
2530 | 2521 | private int height; |
2531 | 2522 | private int width; |
@@ -2871,5 +2862,14 @@ private boolean isDisposed() { |
2871 | 2862 | return this.handle == 0; |
2872 | 2863 | } |
2873 | 2864 |
|
| 2865 | + private void destroy() { |
| 2866 | + zoomLevelToImageHandle.remove(zoom, this); |
| 2867 | + if (type == SWT.ICON) { |
| 2868 | + OS.DestroyIcon (handle); |
| 2869 | + } else { |
| 2870 | + OS.DeleteObject (handle); |
| 2871 | + } |
| 2872 | + handle = 0; |
| 2873 | + } |
2874 | 2874 | } |
2875 | 2875 | } |
0 commit comments