Skip to content

Commit 4e882a9

Browse files
Use the existing image to initialize the GC in ImageGcDrawerWrapper
In ImageGCDrawerWrapper#newImageHandle, we create a new image with existing data to initialize the GC, instead we could use the existing image object and the base handle should be created as it is done in PlainImageDataProvider.
1 parent 97855ad commit 4e882a9

File tree

1 file changed

+49
-58
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+49
-58
lines changed

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

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,6 @@ public Image(Device device, ImageData data) {
380380
this.device.registerResourceWithZoomSupport(this);
381381
}
382382

383-
private Image(Device device, ImageData data, int zoom) {
384-
super(device);
385-
if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
386-
this.imageProvider = new PlainImageDataProviderWrapper(data, zoom);
387-
init();
388-
this.device.registerResourceWithZoomSupport(this);
389-
}
390-
391383
/**
392384
* Constructs an instance of this class, whose type is
393385
* <code>SWT.ICON</code>, from the two given <code>ImageData</code>
@@ -1951,6 +1943,46 @@ protected final ImageHandle newImageHandle(ImageData data, int zoom) {
19511943
return init(data, zoom);
19521944
}
19531945
}
1946+
1947+
protected final ImageHandle createHandle(int width, int height, int zoom) {
1948+
long handle = initHandle(width, height, zoom);
1949+
ImageHandle imageHandle = new ImageHandle(handle, zoom);
1950+
zoomLevelToImageHandle.put(zoom, imageHandle);
1951+
return imageHandle;
1952+
}
1953+
1954+
private long initHandle(int width, int height, int zoom) {
1955+
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
1956+
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
1957+
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
1958+
long hDC = device.internal_new_GC(null);
1959+
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
1960+
/*
1961+
* Feature in Windows. CreateCompatibleBitmap() may fail
1962+
* for large images. The fix is to create a DIB section
1963+
* in that case.
1964+
*/
1965+
if (newHandle == 0) {
1966+
int bits = OS.GetDeviceCaps(hDC, OS.BITSPIXEL);
1967+
int planes = OS.GetDeviceCaps(hDC, OS.PLANES);
1968+
int depth = bits * planes;
1969+
if (depth < 16) depth = 16;
1970+
if (depth > 24) depth = 24;
1971+
newHandle = createDIB(scaledWidth, scaledHeight, depth);
1972+
}
1973+
if (newHandle != 0) {
1974+
long memDC = OS.CreateCompatibleDC(hDC);
1975+
long hOldBitmap = OS.SelectObject(memDC, newHandle);
1976+
OS.PatBlt(memDC, 0, 0, scaledWidth, scaledHeight, OS.PATCOPY);
1977+
OS.SelectObject(memDC, hOldBitmap);
1978+
OS.DeleteDC(memDC);
1979+
}
1980+
device.internal_dispose_GC(hDC, null);
1981+
if (newHandle == 0) {
1982+
SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError());
1983+
}
1984+
return newHandle;
1985+
}
19541986
}
19551987

19561988
private class ExistingImageHandleProviderWrapper extends AbstractImageProviderWrapper {
@@ -2179,55 +2211,15 @@ protected ImageHandle newImageHandle(int zoom) {
21792211
if (memGC != null) {
21802212
GC currentGC = memGC;
21812213
memGC = null;
2182-
createHandle(zoom);
2214+
createHandle(this.width, this.height, zoom);
21832215
currentGC.refreshFor(new DrawableWrapper(Image.this, zoom), zoom);
21842216
return zoomLevelToImageHandle.get(zoom);
21852217
}
21862218
return super.newImageHandle(zoom);
21872219
}
21882220
private ImageHandle createBaseHandle(int zoom) {
21892221
baseZoom = zoom;
2190-
return createHandle(zoom);
2191-
}
2192-
2193-
private ImageHandle createHandle(int zoom) {
2194-
long handle = initHandle(zoom);
2195-
ImageHandle imageHandle = new ImageHandle(handle, zoom);
2196-
zoomLevelToImageHandle.put(zoom, imageHandle);
2197-
return imageHandle;
2198-
}
2199-
2200-
private long initHandle(int zoom) {
2201-
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
2202-
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
2203-
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
2204-
long hDC = device.internal_new_GC(null);
2205-
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
2206-
/*
2207-
* Feature in Windows. CreateCompatibleBitmap() may fail
2208-
* for large images. The fix is to create a DIB section
2209-
* in that case.
2210-
*/
2211-
if (newHandle == 0) {
2212-
int bits = OS.GetDeviceCaps(hDC, OS.BITSPIXEL);
2213-
int planes = OS.GetDeviceCaps(hDC, OS.PLANES);
2214-
int depth = bits * planes;
2215-
if (depth < 16) depth = 16;
2216-
if (depth > 24) depth = 24;
2217-
newHandle = createDIB(scaledWidth, scaledHeight, depth);
2218-
}
2219-
if (newHandle != 0) {
2220-
long memDC = OS.CreateCompatibleDC(hDC);
2221-
long hOldBitmap = OS.SelectObject(memDC, newHandle);
2222-
OS.PatBlt(memDC, 0, 0, scaledWidth, scaledHeight, OS.PATCOPY);
2223-
OS.SelectObject(memDC, hOldBitmap);
2224-
OS.DeleteDC(memDC);
2225-
}
2226-
device.internal_dispose_GC(hDC, null);
2227-
if (newHandle == 0) {
2228-
SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError());
2229-
}
2230-
return newHandle;
2222+
return createHandle(this.width, this.height, zoom);
22312223
}
22322224

22332225
@Override
@@ -2596,27 +2588,26 @@ ImageData newImageData(int zoom) {
25962588
protected ImageHandle newImageHandle(int zoom) {
25972589
currentZoom = zoom;
25982590
int gcStyle = drawer.getGcStyle();
2599-
Image image;
26002591
if ((gcStyle & SWT.TRANSPARENT) != 0) {
26012592
int scaledHeight = Win32DPIUtils.pointToPixel(height, zoom);
26022593
int scaledWidth = Win32DPIUtils.pointToPixel(width, zoom);
26032594
/* Create a 24 bit image data with alpha channel */
26042595
final ImageData resultData = new ImageData (scaledWidth, scaledHeight, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
26052596
resultData.alphaData = new byte [scaledWidth * scaledHeight];
2606-
image = new Image(device, resultData, zoom);
2597+
init(resultData, zoom);
26072598
} else {
2608-
image = new Image(device, width, height);
2599+
createHandle(width, height, zoom);
26092600
}
2610-
GC gc = new GC(new DrawableWrapper(image, zoom), gcStyle);
2601+
GC gc = new GC(new DrawableWrapper(Image.this, zoom), gcStyle);
26112602
try {
26122603
drawer.drawOn(gc, width, height);
2613-
ImageData imageData = image.getImageMetadata(zoom).getImageData();
2604+
ImageData imageData = Image.this.getImageMetadata(zoom).getImageData();
26142605
drawer.postProcess(imageData);
2615-
ImageData newData = adaptImageDataIfDisabledOrGray(imageData);
2616-
return init(newData, zoom);
2606+
zoomLevelToImageHandle.get(zoom).destroy();
2607+
init(imageData, zoom);
2608+
return zoomLevelToImageHandle.get(zoom);
26172609
} finally {
26182610
gc.dispose();
2619-
image.dispose();
26202611
}
26212612
}
26222613

0 commit comments

Comments
 (0)