Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
akoch-yatta committed Jan 17, 2025
1 parent ba58796 commit 3cd4ddf
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public final class Image extends Resource implements Drawable {
*/
private ImageDataProvider imageDataProvider;

/**
* ImageGcDrawer to provide a callback to draw on a GC for various zoom levels
*/
private ImageGcDrawer imageGcDrawer;

/**
* Style flag used to differentiate normal, gray-scale and disabled images based
* on image data providers. Without this, a normal and a disabled image of the
Expand Down Expand Up @@ -384,8 +389,9 @@ public Image(Device device, Image srcImage, int flag) {

imageFileNameProvider = srcImage.imageFileNameProvider;
imageDataProvider = srcImage.imageDataProvider;
imageGcDrawer = srcImage.imageGcDrawer;
this.styleFlag = srcImage.styleFlag | flag;
if (imageFileNameProvider != null || imageDataProvider != null) {
if (imageFileNameProvider != null || imageDataProvider != null ||srcImage.imageGcDrawer != null) {
/* If source image has 200% representation then create the 200% representation for the new image & apply flag */
NSBitmapImageRep rep200 = srcImage.getRepresentation (200);
if (rep200 != null) createRepFromSourceAndApplyFlag(rep200, srcWidth * 2, srcHeight * 2, flag);
Expand Down Expand Up @@ -843,6 +849,62 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
}
}

/**
* The provided ImageGcDrawer will be called on demand whenever a new variant of the
* Image for an additional zoom is required. Depending on the OS specific implementation
* these calls will be done during the instantiation or later when a new variant is
* requested
* <p>
*
* @param device the device on which to create the image
* @param imageGcDrawer the ImageGcDrawer object to be called when a new image variant
* for another zoom is required.
* @param width the width of the new image in points
* @param height the height of the new image in points
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
* <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
* </ul>
* @since 3.129
*/
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
super(device);
if (imageGcDrawer == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.imageGcDrawer = imageGcDrawer;
ImageData data = drawWithImageGcDrawer(imageGcDrawer, width, height, 100);
if (data == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
init (data);
init ();
ImageData data2x = drawWithImageGcDrawer(imageGcDrawer, width, height, 200);
if (data2x != null) {
alphaInfo_200 = new AlphaInfo();
NSBitmapImageRep rep = createRepresentation (data2x, alphaInfo_200);
handle.addRepresentation(rep);
rep.release();
}
} finally {
if (pool != null) pool.release();
}
}

private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width, int height, int zoom) {
Image image = new Image(device, width, height);
GC gc = new GC(image);
try {
imageGcDrawer.drawOn(gc);
ImageData imageData = image.getImageData(zoom);
imageGcDrawer.postProcess(imageData);
return imageData;
} finally {
gc.dispose();
image.dispose();
}
}

private AlphaInfo _getAlphaInfoAtCurrentZoom (NSBitmapImageRep rep) {
int deviceZoom = DPIUtil.getDeviceZoom();
if (deviceZoom != 100 && (imageFileNameProvider != null || imageDataProvider != null)) {
Expand Down Expand Up @@ -1121,6 +1183,8 @@ public boolean equals (Object object) {
return styleFlag == image.styleFlag && imageDataProvider.equals (image.imageDataProvider);
} else if (imageFileNameProvider != null && image.imageFileNameProvider != null) {
return styleFlag == image.styleFlag && imageFileNameProvider.equals (image.imageFileNameProvider);
} else if (imageGcDrawer != null && image.imageGcDrawer != null) {
return styleFlag == image.styleFlag && imageGcDrawer.equals (image.imageGcDrawer);
} else {
return handle == image.handle;
}
Expand Down Expand Up @@ -1357,6 +1421,8 @@ public int hashCode () {
return imageDataProvider.hashCode();
} else if (imageFileNameProvider != null) {
return imageFileNameProvider.hashCode();
} else if (imageGcDrawer != null) {
return imageGcDrawer.hashCode();
} else {
return handle != null ? (int)handle.id : 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ public final class Image extends Resource implements Drawable {
*/
private ImageDataProvider imageDataProvider;

/**
* ImageGcDrawer to provide a callback to draw on a GC for various zoom levels
*/
private ImageGcDrawer imageGcDrawer;

/**
* Style flag used to differentiate normal, gray-scale and disabled images based
* on image data providers. Without this, a normal and a disabled image of the
Expand Down Expand Up @@ -263,6 +268,7 @@ public Image(Device device, Image srcImage, int flag) {
this.type = srcImage.type;
this.imageDataProvider = srcImage.imageDataProvider;
this.imageFileNameProvider = srcImage.imageFileNameProvider;
this.imageGcDrawer = srcImage.imageGcDrawer;
this.styleFlag = srcImage.styleFlag | flag;
this.currentDeviceZoom = srcImage.currentDeviceZoom;

Expand Down Expand Up @@ -661,6 +667,34 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
init ();
}

/**
* The provided ImageGcDrawer will be called on demand whenever a new variant of the
* Image for an additional zoom is required. Depending on the OS specific implementation
* these calls will be done during the instantiation or later when a new variant is
* requested
* <p>
*
* @param device the device on which to create the image
* @param imageGcDrawer the ImageGcDrawer object to be called when a new image variant
* for another zoom is required.
* @param width the width of the new image in points
* @param height the height of the new image in points
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
* <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
* </ul>
* @since 3.129
*/
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
super(device);
this.imageGcDrawer = imageGcDrawer;
currentDeviceZoom = DPIUtil.getDeviceZoom();
ImageData imageData = drawWithImageGcDrawer(currentDeviceZoom);
init (imageData);
init ();
}

/**
* Refreshes the image for the current device scale factor.
* <p>
Expand Down Expand Up @@ -722,6 +756,17 @@ boolean refreshImageForZoom () {
refreshed = true;
currentDeviceZoom = deviceZoomLevel;
}
} else if (imageGcDrawer != null) {
int deviceZoomLevel = deviceZoom;
if (deviceZoomLevel != currentDeviceZoom) {
ImageData data = drawWithImageGcDrawer(deviceZoomLevel);
/* Release current native resources */
destroy ();
init(data);
init();
refreshed = true;
currentDeviceZoom = deviceZoomLevel;
}
} else {
if (!DPIUtil.useCairoAutoScale()) {
int deviceZoomLevel = deviceZoom;
Expand Down Expand Up @@ -904,6 +949,8 @@ public boolean equals (Object object) {
return (styleFlag == image.styleFlag) && imageDataProvider.equals (image.imageDataProvider);
} else if (imageFileNameProvider != null && image.imageFileNameProvider != null) {
return (styleFlag == image.styleFlag) && imageFileNameProvider.equals (image.imageFileNameProvider);
} else if (imageGcDrawer != null && image.imageGcDrawer != null) {
return styleFlag == image.styleFlag && imageGcDrawer.equals (image.imageGcDrawer);
} else {
return surface == image.surface;
}
Expand Down Expand Up @@ -1110,11 +1157,33 @@ public ImageData getImageData (int zoom) {
} else if (imageFileNameProvider != null) {
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom());
} else if (imageGcDrawer != null) {
return drawWithImageGcDrawer(zoom);
} else {
return DPIUtil.scaleImageData (device, getImageDataAtCurrentZoom (), zoom, currentDeviceZoom);
}
}



private ImageData drawWithImageGcDrawer(int zoom) {
if (this.imageGcDrawer != null) {
Image image = new Image(device, width, height);
GC gc = new GC(image);
try {
imageGcDrawer.drawOn(gc);
ImageData imageData = image.getImageData(zoom);
imageGcDrawer.postProcess(imageData);
return imageData;
} finally {
gc.dispose();
image.dispose();
}
}
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, ": ImageGcDrawer [" + imageGcDrawer + "] is null.");
return null;
}

/**
* Invokes platform specific functionality to allocate a new image.
* <p>
Expand Down Expand Up @@ -1179,6 +1248,8 @@ public int hashCode () {
return imageDataProvider.hashCode();
} else if (imageFileNameProvider != null) {
return imageFileNameProvider.hashCode();
} else if (imageGcDrawer != null) {
return imageGcDrawer.hashCode();
} else {
return (int)surface;
}
Expand Down

0 comments on commit 3cd4ddf

Please sign in to comment.