Bug 467687 - SWT: Invalid thread access in GC.drawImage
authorSravan Kumar Lakkimsetti <sravankumarl@in.ibm.com>
Wed, 20 May 2015 17:27:04 +0000 (22:57 +0530)
committerLakshmi Shanmugam <lshanmug@in.ibm.com>
Thu, 21 May 2015 10:37:49 +0000 (06:37 -0400)
On Windows created a new method getActualDPI() that works in non ui
threads as well and using this to get the dpi

On linux modified the getActualdpi() method to not use getbounds call
which is actually not required as we need on only x and y of the
display. these are always 0, 0 in all cases of getBounds.

Updated with review comments

Change-Id: I7154ca7a2493fc21d1e38388a2f19c885ce9e4bf
Signed-off-by: Sravan Kumar Lakkimsetti <sravankumarl@in.ibm.com>
bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java
bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

index 53783d1..6fd1bce 100644 (file)
@@ -955,10 +955,15 @@ static long /*int*/ XIOErrorProc (long /*int*/ xDisplay) {
        return 0;
 }
 
-int getActualDPI () {
+/**
+ * Returns DPI in x direction. In the modern monitors DPI for
+ * X and Y directions is same.
+ *
+ * @return the horizontal DPI
+ */
+int _getDPIx () {
        long /*int*/ screen = OS.gdk_screen_get_default();
-       Rectangle rect = getBounds();
-       int monitor = OS.gdk_screen_get_monitor_at_point(screen, rect.x, rect.y);
+       int monitor = OS.gdk_screen_get_monitor_at_point(screen, 0, 0);
 
        GdkRectangle dest = new GdkRectangle ();
        OS.gdk_screen_get_monitor_geometry(screen, monitor, dest);
index 0191a6c..e4c5450 100644 (file)
@@ -762,7 +762,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
 }
 
 int getDeviceZoom () {
-       return DPIUtil.mapDPIToZoom (device.getActualDPI ());
+       return DPIUtil.mapDPIToZoom (device._getDPIx ());
 }
 
 /**
@@ -771,10 +771,10 @@ int getDeviceZoom () {
  * @return true if image is refreshed
  */
 boolean refreshImageForZoom () {
-       int deviceZoomLevel = getDeviceZoom();
        boolean refreshed = false;
-       if (deviceZoomLevel != currentDeviceZoom) {
-               if (imageFileNameProvider != null) {
+       if (imageFileNameProvider != null) {
+               int deviceZoomLevel = getDeviceZoom();
+               if (deviceZoomLevel != currentDeviceZoom) {
                        boolean[] found = new boolean[1];
                        String filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoomLevel, found);
                        /* Avoid re-creating the fall-back image, when current zoom is already 100% */
@@ -786,7 +786,11 @@ boolean refreshImageForZoom () {
                                init ();
                                refreshed = true;
                        }
-               } else if (imageDataProvider != null) {
+                       currentDeviceZoom = deviceZoomLevel;
+               }
+       } else if (imageDataProvider != null) {
+               int deviceZoomLevel = getDeviceZoom();
+               if (deviceZoomLevel != currentDeviceZoom) {
                        boolean[] found = new boolean[1];
                        ImageData data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, deviceZoomLevel, found);
                        /* Avoid re-creating the fall-back image, when current zoom is already 100% */
@@ -797,8 +801,8 @@ boolean refreshImageForZoom () {
                                init();
                                refreshed = true;
                        }
+                       currentDeviceZoom = deviceZoomLevel;
                }
-               currentDeviceZoom = deviceZoomLevel;
        }
        return refreshed;
 }
index c71e47f..af0b77b 100644 (file)
 package org.eclipse.swt.graphics;
 
  
+import org.eclipse.swt.*;
 import org.eclipse.swt.internal.*;
 import org.eclipse.swt.internal.gdip.*;
 import org.eclipse.swt.internal.win32.*;
-import org.eclipse.swt.*;
 
 /**
  * This class is the abstract superclass of all device objects,
@@ -472,6 +472,18 @@ public Point getDPI () {
 }
 
 /**
+ * Returns DPI in x direction. In the modern monitors DPI for
+ * X and Y directions is same.
+ *
+ * @return the horizontal DPI
+ */
+int _getDPIx () {
+       long /*int*/ hDC = internal_new_GC (null);
+       int dpi = OS.GetDeviceCaps (hDC, OS.LOGPIXELSX);
+       internal_dispose_GC (hDC, null);
+       return dpi;
+}
+/**
  * Returns <code>FontData</code> objects which describe
  * the fonts that match the given arguments. If the
  * <code>faceName</code> is null, all fonts will be returned.
index 8e1f8b9..c13f175 100644 (file)
@@ -708,7 +708,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
 }
 
 int getDeviceZoom () {
-       return DPIUtil.mapDPIToZoom (device.getDPI ().x);
+       return DPIUtil.mapDPIToZoom (device._getDPIx ());
 }
 
 /**
@@ -717,10 +717,10 @@ int getDeviceZoom () {
  * @return true if image is refreshed
  */
 boolean refreshImageForZoom () {
-       int deviceZoomLevel = getDeviceZoom();
        boolean refreshed = false;
-       if (deviceZoomLevel != currentDeviceZoom) {
-               if (imageFileNameProvider != null) {
+       if (imageFileNameProvider != null) {
+               int deviceZoomLevel = getDeviceZoom();
+               if (deviceZoomLevel != currentDeviceZoom) {
                        boolean[] found = new boolean[1];
                        String filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoomLevel, found);
                        /* Avoid re-creating the fall-back image, when current zoom is already 100% */
@@ -732,7 +732,11 @@ boolean refreshImageForZoom () {
                                init();
                                refreshed = true;
                        }
-               } else if (imageDataProvider != null) {
+                       currentDeviceZoom = deviceZoomLevel;
+               }
+       } else if (imageDataProvider != null) {
+               int deviceZoomLevel = getDeviceZoom();
+               if (deviceZoomLevel != currentDeviceZoom) {
                        boolean[] found = new boolean[1];
                        ImageData data = DPIUtil.validateAndGetImageDataAtZoom (imageDataProvider, deviceZoomLevel, found);
                        /* Avoid re-creating the fall-back image, when current zoom is already 100% */
@@ -743,8 +747,8 @@ boolean refreshImageForZoom () {
                                init();
                                refreshed = true;
                        }
+                       currentDeviceZoom = deviceZoomLevel;
                }
-               currentDeviceZoom = deviceZoomLevel;
        }
        return refreshed;
 }