Skip to content

Use the existing image to initialize the GC in ImageGcDrawerWrapper #2238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -380,14 +380,6 @@ public Image(Device device, ImageData data) {
this.device.registerResourceWithZoomSupport(this);
}

private Image(Device device, ImageData data, int zoom) {
super(device);
if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.imageProvider = new PlainImageDataProviderWrapper(data, zoom);
init();
this.device.registerResourceWithZoomSupport(this);
}

/**
* Constructs an instance of this class, whose type is
* <code>SWT.ICON</code>, from the two given <code>ImageData</code>
Expand Down Expand Up @@ -1957,6 +1949,46 @@ protected final ImageHandle newImageHandle(ImageData data, ZoomContext zoomConte
return init(data, zoomContext.targetZoom());
}
}

protected final ImageHandle createHandle(int width, int height, int zoom) {
long handle = initHandle(width, height, zoom);
ImageHandle imageHandle = new ImageHandle(handle, zoom);
zoomLevelToImageHandle.put(zoom, imageHandle);
return imageHandle;
}

private long initHandle(int width, int height, int zoom) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
long hDC = device.internal_new_GC(null);
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
/*
* Feature in Windows. CreateCompatibleBitmap() may fail
* for large images. The fix is to create a DIB section
* in that case.
*/
if (newHandle == 0) {
int bits = OS.GetDeviceCaps(hDC, OS.BITSPIXEL);
int planes = OS.GetDeviceCaps(hDC, OS.PLANES);
int depth = bits * planes;
if (depth < 16) depth = 16;
if (depth > 24) depth = 24;
newHandle = createDIB(scaledWidth, scaledHeight, depth);
}
if (newHandle != 0) {
long memDC = OS.CreateCompatibleDC(hDC);
long hOldBitmap = OS.SelectObject(memDC, newHandle);
OS.PatBlt(memDC, 0, 0, scaledWidth, scaledHeight, OS.PATCOPY);
OS.SelectObject(memDC, hOldBitmap);
OS.DeleteDC(memDC);
}
device.internal_dispose_GC(hDC, null);
if (newHandle == 0) {
SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError());
}
return newHandle;
}
}

private class ExistingImageHandleProviderWrapper extends AbstractImageProviderWrapper {
Expand Down Expand Up @@ -2196,7 +2228,7 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
if (memGC.getZoom() != targetZoom) {
GC currentGC = memGC;
memGC = null;
createHandle(targetZoom);
createHandle(this.width, this.height, targetZoom);
currentGC.refreshFor(new DrawableWrapper(Image.this, zoomContext));
}
return zoomLevelToImageHandle.get(targetZoom);
Expand All @@ -2205,47 +2237,7 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
}
private ImageHandle createBaseHandle(int zoom) {
baseZoom = zoom;
return createHandle(zoom);
}

private ImageHandle createHandle(int zoom) {
long handle = initHandle(zoom);
ImageHandle imageHandle = new ImageHandle(handle, zoom);
zoomLevelToImageHandle.put(zoom, imageHandle);
return imageHandle;
}

private long initHandle(int zoom) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
long hDC = device.internal_new_GC(null);
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
/*
* Feature in Windows. CreateCompatibleBitmap() may fail
* for large images. The fix is to create a DIB section
* in that case.
*/
if (newHandle == 0) {
int bits = OS.GetDeviceCaps(hDC, OS.BITSPIXEL);
int planes = OS.GetDeviceCaps(hDC, OS.PLANES);
int depth = bits * planes;
if (depth < 16) depth = 16;
if (depth > 24) depth = 24;
newHandle = createDIB(scaledWidth, scaledHeight, depth);
}
if (newHandle != 0) {
long memDC = OS.CreateCompatibleDC(hDC);
long hOldBitmap = OS.SelectObject(memDC, newHandle);
OS.PatBlt(memDC, 0, 0, scaledWidth, scaledHeight, OS.PATCOPY);
OS.SelectObject(memDC, hOldBitmap);
OS.DeleteDC(memDC);
}
device.internal_dispose_GC(hDC, null);
if (newHandle == 0) {
SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError());
}
return newHandle;
return createHandle(this.width, this.height, zoom);
}

@Override
Expand Down Expand Up @@ -2621,27 +2613,26 @@ protected ImageHandle newImageHandle(ZoomContext zoomContext) {
currentZoom = zoomContext;
int targetZoom = zoomContext.targetZoom();
int gcStyle = drawer.getGcStyle();
Image image;
if ((gcStyle & SWT.TRANSPARENT) != 0) {
int scaledHeight = Win32DPIUtils.pointToPixel(height, targetZoom);
int scaledWidth = Win32DPIUtils.pointToPixel(width, targetZoom);
/* Create a 24 bit image data with alpha channel */
final ImageData resultData = new ImageData (scaledWidth, scaledHeight, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
resultData.alphaData = new byte [scaledWidth * scaledHeight];
image = new Image(device, resultData, targetZoom);
init(resultData, targetZoom);
} else {
image = new Image(device, width, height);
createHandle(width, height, targetZoom);
}
GC gc = new GC(new DrawableWrapper(image, zoomContext), gcStyle);
GC gc = new GC(new DrawableWrapper(Image.this, zoomContext), gcStyle);
try {
drawer.drawOn(gc, width, height);
ImageData imageData = image.getImageData(targetZoom);
ImageData imageData = Image.this.getImageData(targetZoom);
drawer.postProcess(imageData);
ImageData newData = adaptImageDataIfDisabledOrGray(imageData);
return init(newData, targetZoom);
zoomLevelToImageHandle.get(targetZoom).destroy();
init(imageData, targetZoom);
return zoomLevelToImageHandle.get(targetZoom);
} finally {
gc.dispose();
image.dispose();
}
}

Expand Down
Loading