Skip to content

Commit e1b6447

Browse files
akoch-yattaHeikoKlare
authored andcommitted
[win32] Reduce calls to Image#win32_getHandle(int)
This commit reduces the calls to Image#win32_getHandle(int) in GC. When drawing an image with a GC multiple draw... methods are calling each other for that operation. All those methods are refetching the handle via Image#win32_getHandle(int) with getZoom(). To support use cases where a method uses a different zoom to get a handle to draw the handle is now fetched in the top most methods and are passed to the other methods directly.
1 parent 1d59663 commit e1b6447

File tree

1 file changed

+35
-32
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+35
-32
lines changed

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

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -985,20 +985,20 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei
985985
if (image == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
986986
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
987987

988-
int deviceZoom = getZoom();
989-
Rectangle src = DPIUtil.scaleUp(drawable, new Rectangle(srcX, srcY, srcWidth, srcHeight), deviceZoom);
990-
Rectangle dest = DPIUtil.scaleUp(drawable, new Rectangle(destX, destY, destWidth, destHeight), deviceZoom);
991-
if (deviceZoom != 100) {
988+
int imageZoom = getZoom();
989+
Rectangle src = DPIUtil.scaleUp(drawable, new Rectangle(srcX, srcY, srcWidth, srcHeight), imageZoom);
990+
Rectangle dest = DPIUtil.scaleUp(drawable, new Rectangle(destX, destY, destWidth, destHeight), imageZoom);
991+
if (imageZoom != 100) {
992992
/*
993993
* This is a HACK! Due to rounding errors at fractional scale factors,
994994
* the coordinates may be slightly off. The workaround is to restrict
995995
* coordinates to the allowed bounds.
996996
*/
997-
Rectangle b = image.getBounds(deviceZoom);
997+
Rectangle b = image.getBounds(imageZoom);
998998
int errX = src.x + src.width - b.width;
999999
int errY = src.y + src.height - b.height;
10001000
if (errX != 0 || errY != 0) {
1001-
if (errX <= deviceZoom / 100 && errY <= deviceZoom / 100) {
1001+
if (errX <= imageZoom / 100 && errY <= imageZoom / 100) {
10021002
src.intersect(b);
10031003
} else {
10041004
SWT.error (SWT.ERROR_INVALID_ARGUMENT);
@@ -1009,12 +1009,14 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei
10091009
}
10101010

10111011
void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
1012+
int imageZoom = getZoom();
10121013
if (data.gdipGraphics != 0) {
10131014
//TODO - cache bitmap
1014-
long [] gdipImage = srcImage.createGdipImage(getZoom());
1015+
long [] gdipImage = srcImage.createGdipImage(imageZoom);
10151016
long img = gdipImage[0];
10161017
int imgWidth = Gdip.Image_GetWidth(img);
10171018
int imgHeight = Gdip.Image_GetHeight(img);
1019+
10181020
if (simple) {
10191021
srcWidth = destWidth = imgWidth;
10201022
srcHeight = destHeight = imgHeight;
@@ -1065,17 +1067,18 @@ void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight,
10651067
}
10661068
return;
10671069
}
1070+
long imageHandle = Image.win32_getHandle(srcImage, imageZoom);
10681071
switch (srcImage.type) {
10691072
case SWT.BITMAP:
1070-
drawBitmap(srcImage, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
1073+
drawBitmap(srcImage, imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
10711074
break;
10721075
case SWT.ICON:
1073-
drawIcon(srcImage, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
1076+
drawIcon(imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
10741077
break;
10751078
}
10761079
}
10771080

1078-
void drawIcon(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
1081+
void drawIcon(long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
10791082
int technology = OS.GetDeviceCaps(handle, OS.TECHNOLOGY);
10801083

10811084
boolean drawIcon = true;
@@ -1099,14 +1102,14 @@ void drawIcon(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, i
10991102
/* Simple case: no stretching, entire icon */
11001103
if (simple && technology != OS.DT_RASPRINTER && drawIcon) {
11011104
if (offsetX != 0 || offsetY != 0) OS.SetWindowOrgEx(handle, 0, 0, null);
1102-
OS.DrawIconEx(handle, destX - offsetX, destY - offsetY, Image.win32_getHandle(srcImage, getZoom()), 0, 0, 0, 0, flags);
1105+
OS.DrawIconEx(handle, destX - offsetX, destY - offsetY, imageHandle, 0, 0, 0, 0, flags);
11031106
if (offsetX != 0 || offsetY != 0) OS.SetWindowOrgEx(handle, offsetX, offsetY, null);
11041107
return;
11051108
}
11061109

11071110
/* Get the icon info */
11081111
ICONINFO srcIconInfo = new ICONINFO();
1109-
OS.GetIconInfo(Image.win32_getHandle(srcImage, getZoom()), srcIconInfo);
1112+
OS.GetIconInfo(imageHandle, srcIconInfo);
11101113

11111114
/* Get the icon width and height */
11121115
long hBitmap = srcIconInfo.hbmColor;
@@ -1128,11 +1131,11 @@ void drawIcon(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, i
11281131
srcWidth == destWidth && srcHeight == destHeight &&
11291132
srcWidth == iconWidth && srcHeight == iconHeight;
11301133
if (!drawIcon) {
1131-
drawBitmapMask(srcImage, srcIconInfo.hbmColor, srcIconInfo.hbmMask, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple, iconWidth, iconHeight, false);
1134+
drawBitmapMask(srcIconInfo.hbmColor, srcIconInfo.hbmMask, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple, iconWidth, iconHeight, false);
11321135
} else if (simple && technology != OS.DT_RASPRINTER) {
11331136
/* Simple case: no stretching, entire icon */
11341137
if (offsetX != 0 || offsetY != 0) OS.SetWindowOrgEx(handle, 0, 0, null);
1135-
OS.DrawIconEx(handle, destX - offsetX, destY - offsetY, Image.win32_getHandle(srcImage, getZoom()), 0, 0, 0, 0, flags);
1138+
OS.DrawIconEx(handle, destX - offsetX, destY - offsetY, imageHandle, 0, 0, 0, 0, flags);
11361139
if (offsetX != 0 || offsetY != 0) OS.SetWindowOrgEx(handle, offsetX, offsetY, null);
11371140
} else {
11381141
/* Create the icon info and HDC's */
@@ -1205,9 +1208,9 @@ void drawIcon(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, i
12051208
if (failed) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
12061209
}
12071210

1208-
void drawBitmap(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
1211+
void drawBitmap(Image srcImage, long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
12091212
BITMAP bm = new BITMAP();
1210-
OS.GetObject(Image.win32_getHandle(srcImage, getZoom()), BITMAP.sizeof, bm);
1213+
OS.GetObject(imageHandle, BITMAP.sizeof, bm);
12111214
int imgWidth = bm.bmWidth;
12121215
int imgHeight = bm.bmHeight;
12131216
if (simple) {
@@ -1235,27 +1238,27 @@ void drawBitmap(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight,
12351238
boolean isDib = bm.bmBits != 0;
12361239
int depth = bm.bmPlanes * bm.bmBitsPixel;
12371240
if (isDib && depth == 32) {
1238-
drawBitmapAlpha(srcImage, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
1241+
drawBitmapAlpha(imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
12391242
} else if (srcImage.transparentPixel != -1) {
1240-
drawBitmapTransparent(srcImage, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple, bm, imgWidth, imgHeight);
1243+
drawBitmapTransparent(srcImage, imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple, bm, imgWidth, imgHeight);
12411244
} else {
1242-
drawBitmapColor(srcImage, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
1245+
drawBitmapColor(imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
12431246
}
12441247
if (mustRestore) {
1245-
long hOldBitmap = OS.SelectObject(memGC.handle, Image.win32_getHandle(srcImage, getZoom()));
1248+
long hOldBitmap = OS.SelectObject(memGC.handle, imageHandle);
12461249
memGC.data.hNullBitmap = hOldBitmap;
12471250
}
12481251
}
12491252

1250-
void drawBitmapAlpha(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
1253+
void drawBitmapAlpha(long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
12511254
boolean alphaBlendSupport = true;
12521255
boolean isPrinter = OS.GetDeviceCaps(handle, OS.TECHNOLOGY) == OS.DT_RASPRINTER;
12531256
int sourceAlpha = -1;
12541257
if (isPrinter) {
12551258
int caps = OS.GetDeviceCaps(handle, OS.SHADEBLENDCAPS);
12561259
if (caps != 0) {
12571260
long srcHdc = OS.CreateCompatibleDC(handle);
1258-
long oldSrcBitmap = OS.SelectObject(srcHdc, Image.win32_getHandle(srcImage, getZoom()));
1261+
long oldSrcBitmap = OS.SelectObject(srcHdc, imageHandle);
12591262
long memDib = Image.createDIB(srcWidth, srcHeight, 32);
12601263
if (memDib == 0) SWT.error(SWT.ERROR_NO_HANDLES);
12611264
long memHdc = OS.CreateCompatibleDC(handle);
@@ -1282,7 +1285,7 @@ void drawBitmapAlpha(Image srcImage, int srcX, int srcY, int srcWidth, int srcHe
12821285
if (sourceAlpha != -1) {
12831286
if (sourceAlpha == 0) return;
12841287
if (sourceAlpha == 255) {
1285-
drawBitmapColor(srcImage, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
1288+
drawBitmapColor(imageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
12861289
return;
12871290
}
12881291
alphaBlendSupport = (caps & OS.SB_CONST_ALPHA) != 0;
@@ -1296,7 +1299,7 @@ void drawBitmapAlpha(Image srcImage, int srcX, int srcY, int srcWidth, int srcHe
12961299
BLENDFUNCTION blend = new BLENDFUNCTION();
12971300
blend.BlendOp = OS.AC_SRC_OVER;
12981301
long srcHdc = OS.CreateCompatibleDC(handle);
1299-
long oldSrcBitmap = OS.SelectObject(srcHdc, Image.win32_getHandle(srcImage, getZoom()));
1302+
long oldSrcBitmap = OS.SelectObject(srcHdc, imageHandle);
13001303
blend.SourceConstantAlpha = (byte)sourceAlpha;
13011304
blend.AlphaFormat = OS.AC_SRC_ALPHA;
13021305
OS.AlphaBlend(handle, destX, destY, destWidth, destHeight, srcHdc, srcX, srcY, srcWidth, srcHeight, blend);
@@ -1329,7 +1332,7 @@ void drawBitmapAlpha(Image srcImage, int srcX, int srcY, int srcWidth, int srcHe
13291332

13301333
/* Create resources */
13311334
long srcHdc = OS.CreateCompatibleDC(handle);
1332-
long oldSrcBitmap = OS.SelectObject(srcHdc, Image.win32_getHandle(srcImage, getZoom()));
1335+
long oldSrcBitmap = OS.SelectObject(srcHdc, imageHandle);
13331336
long memHdc = OS.CreateCompatibleDC(handle);
13341337
long memDib = Image.createDIB(Math.max(srcWidth, destWidth), Math.max(srcHeight, destHeight), 32);
13351338
if (memDib == 0) SWT.error(SWT.ERROR_NO_HANDLES);
@@ -1445,7 +1448,7 @@ void drawBitmapTransparentByClipping(long srcHdc, long maskHdc, int srcX, int sr
14451448
OS.DeleteObject(rgn);
14461449
}
14471450

1448-
void drawBitmapMask(Image srcImage, long srcColor, long srcMask, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple, int imgWidth, int imgHeight, boolean offscreen) {
1451+
void drawBitmapMask(long srcColor, long srcMask, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple, int imgWidth, int imgHeight, boolean offscreen) {
14491452
int srcColorY = srcY;
14501453
if (srcColor == 0) {
14511454
srcColor = srcMask;
@@ -1497,11 +1500,11 @@ void drawBitmapMask(Image srcImage, long srcColor, long srcMask, int srcX, int s
14971500
OS.DeleteDC(srcHdc);
14981501
}
14991502

1500-
void drawBitmapTransparent(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple, BITMAP bm, int imgWidth, int imgHeight) {
1503+
void drawBitmapTransparent(Image srcImage, long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple, BITMAP bm, int imgWidth, int imgHeight) {
15011504

15021505
/* Find the RGB values for the transparent pixel. */
15031506
boolean isDib = bm.bmBits != 0;
1504-
long hBitmap = Image.win32_getHandle(srcImage, getZoom());
1507+
long hBitmap = imageHandle;
15051508
long srcHdc = OS.CreateCompatibleDC(handle);
15061509
long oldSrcBitmap = OS.SelectObject(srcHdc, hBitmap);
15071510
byte[] originalColors = null;
@@ -1546,7 +1549,7 @@ void drawBitmapTransparent(Image srcImage, int srcX, int srcY, int srcWidth, int
15461549
bmiHeader.biBitCount = bm.bmBitsPixel;
15471550
byte[] bmi = new byte[BITMAPINFOHEADER.sizeof + numColors * 4];
15481551
OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof);
1549-
OS.GetDIBits(srcHdc, Image.win32_getHandle(srcImage, getZoom()), 0, 0, null, bmi, OS.DIB_RGB_COLORS);
1552+
OS.GetDIBits(srcHdc, imageHandle, 0, 0, null, bmi, OS.DIB_RGB_COLORS);
15501553
int offset = BITMAPINFOHEADER.sizeof + 4 * srcImage.transparentPixel;
15511554
transRed = bmi[offset + 2] & 0xFF;
15521555
transGreen = bmi[offset + 1] & 0xFF;
@@ -1619,13 +1622,13 @@ void drawBitmapTransparent(Image srcImage, int srcX, int srcY, int srcWidth, int
16191622
OS.DeleteObject(maskBitmap);
16201623
}
16211624
OS.SelectObject(srcHdc, oldSrcBitmap);
1622-
if (hBitmap != Image.win32_getHandle(srcImage, getZoom())) OS.DeleteObject(hBitmap);
1625+
if (hBitmap != imageHandle) OS.DeleteObject(hBitmap);
16231626
OS.DeleteDC(srcHdc);
16241627
}
16251628

1626-
void drawBitmapColor(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
1629+
void drawBitmapColor(long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
16271630
long srcHdc = OS.CreateCompatibleDC(handle);
1628-
long oldSrcBitmap = OS.SelectObject(srcHdc, Image.win32_getHandle(srcImage, getZoom()));
1631+
long oldSrcBitmap = OS.SelectObject(srcHdc, imageHandle);
16291632
int dwRop = OS.GetROP2(handle) == OS.R2_XORPEN ? OS.SRCINVERT : OS.SRCCOPY;
16301633
if (!simple && (srcWidth != destWidth || srcHeight != destHeight)) {
16311634
int mode = OS.SetStretchBltMode(handle, OS.COLORONCOLOR);

0 commit comments

Comments
 (0)