Skip to content

Commit aaad96a

Browse files
Review Changes
1. Making CursorHandleProvider a proper interface and taking out all the methods out of it making them private static and part of Cursor 2. In case of ImageDataProvider. For zoom==100 we directly retrieved the image data from the provider instead of creating a temp image for it. 3. Scaling source, mask, hotspotX, hotspotY before passing it to createHandleFromImageData.
1 parent ac3dd0e commit aaad96a

File tree

1 file changed

+130
-128
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+130
-128
lines changed

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

Lines changed: 130 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -395,138 +395,130 @@ void destroyHandlesExcept(Set<Integer> zoomLevels) {
395395
});
396396
}
397397

398-
private abstract class CursorHandleProvider {
399-
protected abstract long createHandle(Cursor cursor, int zoom);
400-
401-
protected final long createHandleFromImageData(Cursor cursor, int zoom, ImageData source, ImageData mask, int hotSpotX, int hotSpotY) {
402-
long handle;
403-
int scaledHotspotX = Win32DPIUtils.pointToPixel(hotSpotX, zoom);
404-
int scaledHotspotY = Win32DPIUtils.pointToPixel(hotSpotY, zoom);
405-
if (cursor.isIcon) {
406-
handle = setupCursorFromImageData(cursor.device, source, scaledHotspotX, scaledHotspotY);
407-
} else {
408-
ImageData scaledMask = DPIUtil.scaleImageData(cursor.device, mask, zoom, DEFAULT_ZOOM);
409-
handle = setupCursorFromImageData(source, scaledMask, scaledHotspotX, scaledHotspotY);
410-
}
411-
return handle;
412-
}
398+
private static final long createHandleFromImageData(Cursor cursor, int zoom, ImageData source, ImageData mask,
399+
int hotspotX, int hotspotY) {
400+
return cursor.isIcon ? setupCursorFromImageData(cursor.device, source, hotspotX, hotspotY)
401+
: setupCursorFromImageData(source, mask, hotspotX, hotspotY);
402+
}
413403

414-
private final long setupCursorFromImageData(Device device, ImageData source, int hotspotX, int hotspotY) {
415-
if (source == null)
416-
SWT.error(SWT.ERROR_NULL_ARGUMENT);
417-
/* Check the hotspots */
418-
if (hotspotX >= source.width || hotspotX < 0 || hotspotY >= source.height || hotspotY < 0) {
419-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
404+
private static final long setupCursorFromImageData(Device device, ImageData source, int hotspotX, int hotspotY) {
405+
if (source == null)
406+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
407+
/* Check the hotspots */
408+
if (hotspotX >= source.width || hotspotX < 0 || hotspotY >= source.height || hotspotY < 0) {
409+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
410+
}
411+
long hBitmap = 0;
412+
long hMask = 0;
413+
if (source.maskData == null && source.transparentPixel == -1
414+
&& (source.alpha != -1 || source.alphaData != null)) {
415+
PaletteData palette = source.palette;
416+
PaletteData newPalette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
417+
ImageData img = new ImageData(source.width, source.height, 32, newPalette);
418+
if (palette.isDirect) {
419+
ImageData.blit(source.data, source.depth, source.bytesPerLine, source.getByteOrder(), source.width,
420+
source.height, palette.redMask, palette.greenMask, palette.blueMask, img.data, img.depth,
421+
img.bytesPerLine, img.getByteOrder(), img.width, img.height, newPalette.redMask,
422+
newPalette.greenMask, newPalette.blueMask, false, false);
423+
} else {
424+
RGB[] rgbs = palette.getRGBs();
425+
int length = rgbs.length;
426+
byte[] srcReds = new byte[length];
427+
byte[] srcGreens = new byte[length];
428+
byte[] srcBlues = new byte[length];
429+
for (int i = 0; i < rgbs.length; i++) {
430+
RGB rgb = rgbs[i];
431+
if (rgb == null)
432+
continue;
433+
srcReds[i] = (byte) rgb.red;
434+
srcGreens[i] = (byte) rgb.green;
435+
srcBlues[i] = (byte) rgb.blue;
436+
}
437+
ImageData.blit(source.width, source.height, source.data, source.depth, source.bytesPerLine,
438+
source.getByteOrder(), srcReds, srcGreens, srcBlues, img.data, img.depth, img.bytesPerLine,
439+
img.getByteOrder(), newPalette.redMask, newPalette.greenMask, newPalette.blueMask);
420440
}
421-
long hBitmap = 0;
422-
long hMask = 0;
423-
if (source.maskData == null && source.transparentPixel == -1
424-
&& (source.alpha != -1 || source.alphaData != null)) {
425-
PaletteData palette = source.palette;
426-
PaletteData newPalette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
427-
ImageData img = new ImageData(source.width, source.height, 32, newPalette);
428-
if (palette.isDirect) {
429-
ImageData.blit(source.data, source.depth, source.bytesPerLine, source.getByteOrder(), source.width,
430-
source.height, palette.redMask, palette.greenMask, palette.blueMask, img.data, img.depth,
431-
img.bytesPerLine, img.getByteOrder(), img.width, img.height, newPalette.redMask,
432-
newPalette.greenMask, newPalette.blueMask, false, false);
433-
} else {
434-
RGB[] rgbs = palette.getRGBs();
435-
int length = rgbs.length;
436-
byte[] srcReds = new byte[length];
437-
byte[] srcGreens = new byte[length];
438-
byte[] srcBlues = new byte[length];
439-
for (int i = 0; i < rgbs.length; i++) {
440-
RGB rgb = rgbs[i];
441-
if (rgb == null)
442-
continue;
443-
srcReds[i] = (byte) rgb.red;
444-
srcGreens[i] = (byte) rgb.green;
445-
srcBlues[i] = (byte) rgb.blue;
446-
}
447-
ImageData.blit(source.width, source.height, source.data, source.depth, source.bytesPerLine,
448-
source.getByteOrder(), srcReds, srcGreens, srcBlues, img.data, img.depth, img.bytesPerLine,
449-
img.getByteOrder(), newPalette.redMask, newPalette.greenMask, newPalette.blueMask);
441+
hBitmap = Image.createDIB(source.width, source.height, 32);
442+
if (hBitmap == 0)
443+
SWT.error(SWT.ERROR_NO_HANDLES);
444+
BITMAP dibBM = new BITMAP();
445+
OS.GetObject(hBitmap, BITMAP.sizeof, dibBM);
446+
byte[] srcData = img.data;
447+
if (source.alpha != -1) {
448+
for (int i = 3; i < srcData.length; i += 4) {
449+
srcData[i] = (byte) source.alpha;
450450
}
451-
hBitmap = Image.createDIB(source.width, source.height, 32);
452-
if (hBitmap == 0)
453-
SWT.error(SWT.ERROR_NO_HANDLES);
454-
BITMAP dibBM = new BITMAP();
455-
OS.GetObject(hBitmap, BITMAP.sizeof, dibBM);
456-
byte[] srcData = img.data;
457-
if (source.alpha != -1) {
458-
for (int i = 3; i < srcData.length; i += 4) {
459-
srcData[i] = (byte) source.alpha;
460-
}
461-
} else if (source.alphaData != null) {
462-
for (int sp = 3, ap = 0; sp < srcData.length; sp += 4, ap++) {
463-
srcData[sp] = source.alphaData[ap];
464-
}
451+
} else if (source.alphaData != null) {
452+
for (int sp = 3, ap = 0; sp < srcData.length; sp += 4, ap++) {
453+
srcData[sp] = source.alphaData[ap];
465454
}
466-
OS.MoveMemory(dibBM.bmBits, srcData, srcData.length);
467-
hMask = OS.CreateBitmap(source.width, source.height, 1, 1,
468-
new byte[(((source.width + 7) / 8) + 3) / 4 * 4 * source.height]);
469-
if (hMask == 0)
470-
SWT.error(SWT.ERROR_NO_HANDLES);
471-
} else {
472-
ImageData mask = source.getTransparencyMask();
473-
long[] result = Image.initIcon(device, source, mask);
474-
hBitmap = result[0];
475-
hMask = result[1];
476455
}
477-
/* Create the icon */
478-
ICONINFO info = new ICONINFO();
479-
info.fIcon = false;
480-
info.hbmColor = hBitmap;
481-
info.hbmMask = hMask;
482-
info.xHotspot = hotspotX;
483-
info.yHotspot = hotspotY;
484-
long handle = OS.CreateIconIndirect(info);
485-
OS.DeleteObject(hBitmap);
486-
OS.DeleteObject(hMask);
487-
if (handle == 0)
456+
OS.MoveMemory(dibBM.bmBits, srcData, srcData.length);
457+
hMask = OS.CreateBitmap(source.width, source.height, 1, 1,
458+
new byte[(((source.width + 7) / 8) + 3) / 4 * 4 * source.height]);
459+
if (hMask == 0)
488460
SWT.error(SWT.ERROR_NO_HANDLES);
489-
490-
return handle;
461+
} else {
462+
ImageData mask = source.getTransparencyMask();
463+
long[] result = Image.initIcon(device, source, mask);
464+
hBitmap = result[0];
465+
hMask = result[1];
491466
}
467+
/* Create the icon */
468+
ICONINFO info = new ICONINFO();
469+
info.fIcon = false;
470+
info.hbmColor = hBitmap;
471+
info.hbmMask = hMask;
472+
info.xHotspot = hotspotX;
473+
info.yHotspot = hotspotY;
474+
long handle = OS.CreateIconIndirect(info);
475+
OS.DeleteObject(hBitmap);
476+
OS.DeleteObject(hMask);
477+
if (handle == 0)
478+
SWT.error(SWT.ERROR_NO_HANDLES);
479+
480+
return handle;
481+
}
492482

493-
private final long setupCursorFromImageData(ImageData source, ImageData mask, int hotspotX, int hotspotY) {
494-
if (source == null)
483+
private static final long setupCursorFromImageData(ImageData source, ImageData mask, int hotspotX, int hotspotY) {
484+
if (source == null)
485+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
486+
if (mask == null) {
487+
if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
495488
SWT.error(SWT.ERROR_NULL_ARGUMENT);
496-
if (mask == null) {
497-
if (source.getTransparencyType() != SWT.TRANSPARENCY_MASK) {
498-
SWT.error(SWT.ERROR_NULL_ARGUMENT);
499-
}
500-
mask = source.getTransparencyMask();
501-
}
502-
/* Check the bounds. Mask must be the same size as source */
503-
if (mask.width != source.width || mask.height != source.height) {
504-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
505-
}
506-
/* Check the hotspots */
507-
if (hotspotX >= source.width || hotspotX < 0 || hotspotY >= source.height || hotspotY < 0) {
508-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
509489
}
510-
/* Convert depth to 1 */
511-
mask = ImageData.convertMask(mask);
512-
source = ImageData.convertMask(source);
513-
514-
/* Make sure source and mask scanline pad is 2 */
515-
byte[] sourceData = ImageData.convertPad(source.data, source.width, source.height, source.depth,
516-
source.scanlinePad, 2);
517-
byte[] maskData = ImageData.convertPad(mask.data, mask.width, mask.height, mask.depth, mask.scanlinePad, 2);
518-
519-
/* Create the cursor */
520-
long hInst = OS.GetModuleHandle(null);
521-
long handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData);
522-
if (handle == 0)
523-
SWT.error(SWT.ERROR_NO_HANDLES);
524-
return handle;
490+
mask = source.getTransparencyMask();
491+
}
492+
/* Check the bounds. Mask must be the same size as source */
493+
if (mask.width != source.width || mask.height != source.height) {
494+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
495+
}
496+
/* Check the hotspots */
497+
if (hotspotX >= source.width || hotspotX < 0 || hotspotY >= source.height || hotspotY < 0) {
498+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
525499
}
500+
/* Convert depth to 1 */
501+
mask = ImageData.convertMask(mask);
502+
source = ImageData.convertMask(source);
503+
504+
/* Make sure source and mask scanline pad is 2 */
505+
byte[] sourceData = ImageData.convertPad(source.data, source.width, source.height, source.depth,
506+
source.scanlinePad, 2);
507+
byte[] maskData = ImageData.convertPad(mask.data, mask.width, mask.height, mask.depth, mask.scanlinePad, 2);
508+
509+
/* Create the cursor */
510+
long hInst = OS.GetModuleHandle(null);
511+
long handle = OS.CreateCursor(hInst, hotspotX, hotspotY, source.width, source.height, sourceData, maskData);
512+
if (handle == 0)
513+
SWT.error(SWT.ERROR_NO_HANDLES);
514+
return handle;
515+
}
526516

517+
private interface CursorHandleProvider {
518+
long createHandle(Cursor cursor, int zoom);
527519
}
528520

529-
private class StyleCursorHandleProvider extends CursorHandleProvider {
521+
private class StyleCursorHandleProvider implements CursorHandleProvider {
530522
private final int style;
531523

532524
public StyleCursorHandleProvider(int style) {
@@ -536,10 +528,10 @@ public StyleCursorHandleProvider(int style) {
536528
@Override
537529
public long createHandle(Cursor cursor, int zoom) {
538530
// zoom ignored, LoadCursor handles scaling internally
539-
return setupCursorFromStyle(style);
531+
return setupCursorFromStyle(this.style);
540532
}
541533

542-
private final long setupCursorFromStyle(int style) {
534+
private static final long setupCursorFromStyle(int style) {
543535
long lpCursorName = 0;
544536
switch (style) {
545537
case SWT.CURSOR_HAND:
@@ -618,7 +610,7 @@ private final long setupCursorFromStyle(int style) {
618610
}
619611
}
620612

621-
private class ImageDataProviderCursorHandleProvider extends CursorHandleProvider {
613+
private class ImageDataProviderCursorHandleProvider implements CursorHandleProvider {
622614
private final ImageDataProvider provider;
623615
private final int hotspotX;
624616
private final int hotspotY;
@@ -631,17 +623,24 @@ public ImageDataProviderCursorHandleProvider(ImageDataProvider provider, int hot
631623

632624
@Override
633625
public long createHandle(Cursor cursor, int zoom) {
634-
Image tempImage = new Image(cursor.device, this.provider);
635-
ImageData source = tempImage.getImageData(zoom);
636-
tempImage.dispose();
637-
long handle = createHandleFromImageData(cursor, zoom, source, null, this.hotspotX, this.hotspotY);
626+
ImageData source;
627+
if(zoom == DEFAULT_ZOOM) {
628+
source = this.provider.getImageData(DEFAULT_ZOOM);
629+
} else {
630+
Image tempImage = new Image(cursor.device, this.provider);
631+
source = tempImage.getImageData(zoom);
632+
tempImage.dispose();
633+
}
634+
int scaledHotspotX = Win32DPIUtils.pointToPixel(hotspotX, zoom);
635+
int scaledHotspotY = Win32DPIUtils.pointToPixel(hotspotY, zoom);
636+
long handle = createHandleFromImageData(cursor, zoom, source, null, scaledHotspotX, scaledHotspotY);
638637

639638
return handle;
640639
}
641640

642641
}
643642

644-
private class ImageDataCursorHandleProvider extends CursorHandleProvider {
643+
private class ImageDataCursorHandleProvider implements CursorHandleProvider {
645644
private final ImageData source;
646645
private final ImageData mask;
647646
private final int hotspotX;
@@ -656,8 +655,11 @@ public ImageDataCursorHandleProvider(ImageData source, ImageData mask, int hotsp
656655

657656
@Override
658657
public long createHandle(Cursor cursor, int zoom) {
659-
ImageData source = DPIUtil.scaleImageData(cursor.device, this.source, zoom, DEFAULT_ZOOM);
660-
long handle = createHandleFromImageData(cursor, zoom, source, this.mask, this.hotspotX, this.hotspotY);
658+
ImageData scaledSource = DPIUtil.scaleImageData(cursor.device, this.source, zoom, DEFAULT_ZOOM);
659+
ImageData scaledMask = this.mask!=null? DPIUtil.scaleImageData(cursor.device, mask, zoom, DEFAULT_ZOOM) : mask;
660+
int scaledHotspotX = Win32DPIUtils.pointToPixel(hotspotX, zoom);
661+
int scaledHotspotY = Win32DPIUtils.pointToPixel(hotspotY, zoom);
662+
long handle = createHandleFromImageData(cursor, zoom, scaledSource, scaledMask, scaledHotspotX, scaledHotspotY);
661663

662664
return handle;
663665
}

0 commit comments

Comments
 (0)