Skip to content

Commit abc68c9

Browse files
Use the existing image to initialize the GC in ImageGcDrawerWrapper
For Windows, 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. For GTK and Cocoa, We use init methods to create new handles instead of creating a new Image object.
1 parent 508ad4a commit abc68c9

File tree

3 files changed

+57
-70
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT

3 files changed

+57
-70
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -903,24 +903,22 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
903903

904904
private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width, int height, int zoom) {
905905
int gcStyle = imageGcDrawer.getGcStyle();
906-
Image image;
907906
if ((gcStyle & SWT.TRANSPARENT) != 0) {
908907
/* Create a 24 bit image data with alpha channel */
909908
final ImageData resultData = new ImageData (width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
910909
resultData.alphaData = new byte [width * height];
911-
image = new Image(device, resultData);
910+
init(resultData, zoom);
912911
} else {
913-
image = new Image(device, width, height);
912+
init(width, height);
914913
}
915-
GC gc = new GC(image, gcStyle);
914+
GC gc = new GC(Image.this, gcStyle);
916915
try {
917916
imageGcDrawer.drawOn(gc, width, height);
918-
ImageData imageData = image.getImageData(zoom);
917+
ImageData imageData = Image.this.getImageData(zoom);
919918
imageGcDrawer.postProcess(imageData);
920919
return imageData;
921920
} finally {
922921
gc.dispose();
923-
image.dispose();
924922
}
925923
}
926924

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,24 +1169,22 @@ public ImageData getImageData (int zoom) {
11691169

11701170
private ImageData drawWithImageGcDrawer(int width, int height, int zoom) {
11711171
int gcStyle = imageGcDrawer.getGcStyle();
1172-
Image image;
11731172
if ((gcStyle & SWT.TRANSPARENT) != 0) {
11741173
/* Create a 24 bit image data with alpha channel */
11751174
final ImageData resultData = new ImageData(width, height, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
11761175
resultData.alphaData = new byte [width * height];
1177-
image = new Image(device, resultData, zoom);
1176+
init(resultData, zoom);
11781177
} else {
1179-
image = new Image(device, width, height);
1178+
init(width, height);
11801179
}
1181-
GC gc = new GC(image, gcStyle);
1180+
GC gc = new GC(Image.this, gcStyle);
11821181
try {
11831182
imageGcDrawer.drawOn(gc, width, height);
1184-
ImageData imageData = image.getImageData(zoom);
1183+
ImageData imageData = Image.this.getImageData(zoom);
11851184
imageGcDrawer.postProcess(imageData);
11861185
return imageData;
11871186
} finally {
11881187
gc.dispose();
1189-
image.dispose();
11901188
}
11911189
}
11921190

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>
@@ -1957,6 +1949,46 @@ protected final ImageHandle newImageHandle(ImageData data, ZoomContext zoomConte
19571949
return init(data, zoomContext.targetZoom());
19581950
}
19591951
}
1952+
1953+
protected final ImageHandle createHandle(int width, int height, int zoom) {
1954+
long handle = initHandle(width, height, zoom);
1955+
ImageHandle imageHandle = new ImageHandle(handle, zoom);
1956+
zoomLevelToImageHandle.put(zoom, imageHandle);
1957+
return imageHandle;
1958+
}
1959+
1960+
private long initHandle(int width, int height, int zoom) {
1961+
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
1962+
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
1963+
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
1964+
long hDC = device.internal_new_GC(null);
1965+
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
1966+
/*
1967+
* Feature in Windows. CreateCompatibleBitmap() may fail
1968+
* for large images. The fix is to create a DIB section
1969+
* in that case.
1970+
*/
1971+
if (newHandle == 0) {
1972+
int bits = OS.GetDeviceCaps(hDC, OS.BITSPIXEL);
1973+
int planes = OS.GetDeviceCaps(hDC, OS.PLANES);
1974+
int depth = bits * planes;
1975+
if (depth < 16) depth = 16;
1976+
if (depth > 24) depth = 24;
1977+
newHandle = createDIB(scaledWidth, scaledHeight, depth);
1978+
}
1979+
if (newHandle != 0) {
1980+
long memDC = OS.CreateCompatibleDC(hDC);
1981+
long hOldBitmap = OS.SelectObject(memDC, newHandle);
1982+
OS.PatBlt(memDC, 0, 0, scaledWidth, scaledHeight, OS.PATCOPY);
1983+
OS.SelectObject(memDC, hOldBitmap);
1984+
OS.DeleteDC(memDC);
1985+
}
1986+
device.internal_dispose_GC(hDC, null);
1987+
if (newHandle == 0) {
1988+
SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError());
1989+
}
1990+
return newHandle;
1991+
}
19601992
}
19611993

19621994
private class ExistingImageHandleProviderWrapper extends AbstractImageProviderWrapper {
@@ -2196,7 +2228,7 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
21962228
if (memGC.getZoom() != targetZoom) {
21972229
GC currentGC = memGC;
21982230
memGC = null;
2199-
createHandle(targetZoom);
2231+
createHandle(this.width, this.height, targetZoom);
22002232
currentGC.refreshFor(new DrawableWrapper(Image.this, zoomContext));
22012233
}
22022234
return zoomLevelToImageHandle.get(targetZoom);
@@ -2205,47 +2237,7 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
22052237
}
22062238
private ImageHandle createBaseHandle(int zoom) {
22072239
baseZoom = zoom;
2208-
return createHandle(zoom);
2209-
}
2210-
2211-
private ImageHandle createHandle(int zoom) {
2212-
long handle = initHandle(zoom);
2213-
ImageHandle imageHandle = new ImageHandle(handle, zoom);
2214-
zoomLevelToImageHandle.put(zoom, imageHandle);
2215-
return imageHandle;
2216-
}
2217-
2218-
private long initHandle(int zoom) {
2219-
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
2220-
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
2221-
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
2222-
long hDC = device.internal_new_GC(null);
2223-
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
2224-
/*
2225-
* Feature in Windows. CreateCompatibleBitmap() may fail
2226-
* for large images. The fix is to create a DIB section
2227-
* in that case.
2228-
*/
2229-
if (newHandle == 0) {
2230-
int bits = OS.GetDeviceCaps(hDC, OS.BITSPIXEL);
2231-
int planes = OS.GetDeviceCaps(hDC, OS.PLANES);
2232-
int depth = bits * planes;
2233-
if (depth < 16) depth = 16;
2234-
if (depth > 24) depth = 24;
2235-
newHandle = createDIB(scaledWidth, scaledHeight, depth);
2236-
}
2237-
if (newHandle != 0) {
2238-
long memDC = OS.CreateCompatibleDC(hDC);
2239-
long hOldBitmap = OS.SelectObject(memDC, newHandle);
2240-
OS.PatBlt(memDC, 0, 0, scaledWidth, scaledHeight, OS.PATCOPY);
2241-
OS.SelectObject(memDC, hOldBitmap);
2242-
OS.DeleteDC(memDC);
2243-
}
2244-
device.internal_dispose_GC(hDC, null);
2245-
if (newHandle == 0) {
2246-
SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError());
2247-
}
2248-
return newHandle;
2240+
return createHandle(this.width, this.height, zoom);
22492241
}
22502242

22512243
@Override
@@ -2621,27 +2613,26 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
26212613
currentZoom = zoomContext;
26222614
int targetZoom = zoomContext.targetZoom();
26232615
int gcStyle = drawer.getGcStyle();
2624-
Image image;
26252616
if ((gcStyle & SWT.TRANSPARENT) != 0) {
26262617
int scaledHeight = Win32DPIUtils.pointToPixel(height, targetZoom);
26272618
int scaledWidth = Win32DPIUtils.pointToPixel(width, targetZoom);
26282619
/* Create a 24 bit image data with alpha channel */
26292620
final ImageData resultData = new ImageData (scaledWidth, scaledHeight, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
26302621
resultData.alphaData = new byte [scaledWidth * scaledHeight];
2631-
image = new Image(device, resultData, targetZoom);
2622+
init(resultData, targetZoom);
26322623
} else {
2633-
image = new Image(device, width, height);
2624+
createHandle(width, height, targetZoom);
26342625
}
2635-
GC gc = new GC(new DrawableWrapper(image, zoomContext), gcStyle);
2626+
GC gc = new GC(new DrawableWrapper(Image.this, zoomContext), gcStyle);
26362627
try {
26372628
drawer.drawOn(gc, width, height);
2638-
ImageData imageData = image.getImageData(targetZoom);
2629+
ImageData imageData = Image.this.getImageData(targetZoom);
26392630
drawer.postProcess(imageData);
2640-
ImageData newData = adaptImageDataIfDisabledOrGray(imageData);
2641-
return init(newData, targetZoom);
2631+
zoomLevelToImageHandle.get(targetZoom).destroy();
2632+
init(imageData, targetZoom);
2633+
return zoomLevelToImageHandle.get(targetZoom);
26422634
} finally {
26432635
gc.dispose();
2644-
image.dispose();
26452636
}
26462637
}
26472638

0 commit comments

Comments
 (0)