changeset 7864:e0ac95b3aef1

RT-38485: [Quantum] Cleanup renderLock usage to call QuantumToolkit runWith/WithoutRenderLock
author kcr
date Fri, 29 Aug 2014 15:47:13 -0700
parents 1777099fe570
children e04700b71f72
files modules/graphics/src/main/java/com/sun/javafx/tk/quantum/EmbeddedScene.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassAppletWindow.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassViewEventHandler.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassWindowEventHandler.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/PaintCollector.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/QuantumToolkit.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/ViewScene.java modules/graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java
diffstat 8 files changed, 63 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/EmbeddedScene.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/EmbeddedScene.java	Fri Aug 29 15:47:13 2014 -0700
@@ -80,14 +80,12 @@
     @Override
     public void dispose() {
         assert host != null;
-        ViewPainter.renderLock.lock();
-        try {
+        QuantumToolkit.runWithRenderLock(() -> {
             host.setEmbeddedScene(null);
             host = null;
             updateSceneState();
-        } finally {
-            ViewPainter.renderLock.unlock();
-        }
+            return null;
+        });
         super.dispose();
     }
 
@@ -181,22 +179,24 @@
      * @return 
      */
     @Override
-    public boolean getPixels(IntBuffer dest, int width, int height) {
-        ViewPainter.renderLock.lock();
-        try {
+    public boolean getPixels(final IntBuffer dest, final int width, final int height) {
+        return QuantumToolkit.runWithRenderLock(() -> {
+            int scaledWidth = width;
+            int scaledHeight = height;
+
             // The dest buffer scale factor is expected to match painter.getPixelScaleFactor().
             if (painter.getPixelScaleFactor() != texScaleFactor || texBits == null) {
                 return false;
             }
-            width = (int)Math.round(width * texScaleFactor);
-            height = (int)Math.round(height * texScaleFactor);
+            scaledWidth = (int)Math.round(scaledWidth * texScaleFactor);
+            scaledHeight = (int)Math.round(scaledHeight * texScaleFactor);
         
             dest.rewind();
             texBits.rewind();
             if (dest.capacity() != texBits.capacity()) {
                 // Calculate the intersection of the dest & src images.
-                int w = Math.min(width, texLineStride);
-                int h = Math.min(height, texBits.capacity() / texLineStride);
+                int w = Math.min(scaledWidth, texLineStride);
+                int h = Math.min(scaledHeight, texBits.capacity() / texLineStride);
 
                 // Copy the intersection to the dest.
                 // The backed array of the textureBits may not be available,
@@ -205,16 +205,14 @@
                 for (int i = 0; i < h; i++) {
                     texBits.position(i * texLineStride);
                     texBits.get(linebuf, 0, w);
-                    dest.position(i * width);
+                    dest.position(i * scaledWidth);
                     dest.put(linebuf);
                 }
                 return true;
             }
             dest.put(texBits);
             return true;
-        } finally {
-            ViewPainter.renderLock.unlock();
-        }
+        });
     }
     
     @Override
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassAppletWindow.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassAppletWindow.java	Fri Aug 29 15:47:13 2014 -0700
@@ -121,14 +121,12 @@
     }
 
     void dispose() {
-        ViewPainter.renderLock.lock();
-        try {
+        QuantumToolkit.runWithRenderLock(() -> {
             glassWindow.close();
             //TODO - should update glass scene view state
             //TODO - doesn't matter because we are disposing
-        } finally {
-            ViewPainter.renderLock.unlock();
-        }
+            return null;
+        });
     }
 
     @Override
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassViewEventHandler.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassViewEventHandler.java	Fri Aug 29 15:47:13 2014 -0700
@@ -774,12 +774,10 @@
                     Window w = view.getWindow();
                     scene.sceneListener.changedSize(view.getWidth(), view.getHeight());
                     scene.entireSceneNeedsRepaint();
-                    ViewPainter.renderLock.lock();
-                    try {
+                    QuantumToolkit.runWithRenderLock(() -> {
                         scene.updateSceneState();
-                    } finally {
-                        ViewPainter.renderLock.unlock();
-                    }
+                        return null;
+                    });
                     if (QuantumToolkit.liveResize && w != null && w.isVisible()) {
                         WindowStage stage = scene.getWindowStage();
                         if (stage != null && !stage.isApplet()) {
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassWindowEventHandler.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/GlassWindowEventHandler.java	Fri Aug 29 15:47:13 2014 -0700
@@ -68,15 +68,13 @@
                 stage.stageListener.changedLocation(window.getX(), window.getY());
                 //We need to sync the new x,y for painting
                 if (!Application.GetApplication().hasWindowManager()) {
-                    ViewPainter.renderLock.lock();
-                    try { 
+                    QuantumToolkit.runWithRenderLock(() -> {
                         GlassScene scene = stage.getScene();
                         if (scene != null) {
                             scene.updateSceneState();
                         }
-                    } finally { 
-                        ViewPainter.renderLock.unlock();
-                    }
+                        return null;
+                    });
                 }
                 break;
             case WindowEvent.RESIZE:
@@ -145,13 +143,11 @@
     public void handleScreenChangedEvent(Window window, long time, Screen oldScreen, Screen newScreen) {
         GlassScene scene = stage.getScene();
         if (scene != null) {
-            ViewPainter.renderLock.lock();
-            try { 
+            QuantumToolkit.runWithRenderLock(() -> {
                 scene.entireSceneNeedsRepaint();
                 scene.updateSceneState();
-            } finally { 
-                ViewPainter.renderLock.unlock();
-            }
+                return null;
+            });
         }
 
         QuantumToolkit.runWithoutRenderLock(() -> {
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/PaintCollector.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/PaintCollector.java	Fri Aug 29 15:47:13 2014 -0700
@@ -319,22 +319,15 @@
          QuantumToolkit quantum = (QuantumToolkit)QuantumToolkit.getToolkit();
          quantum.pulse(false);
          final CountDownLatch latch = new CountDownLatch(1);
-         boolean locked =  ViewPainter.renderLock.isHeldByCurrentThread();
-         if (locked) {
-             ViewPainter.renderLock.unlock();
-         }
-         try {
+         QuantumToolkit.runWithoutRenderLock(() -> {
              quantum.addRenderJob(new RenderJob(viewPainter, rj -> latch.countDown()));
              try {
                  latch.await();
              } catch (InterruptedException e) {
                  //Fail silently.  If interrupted, then proceed with the UI ...
              }
-         } finally {
-             if (locked) {
-                 ViewPainter.renderLock.lock();
-             }
-         }
+             return null;
+         });
      }
 
     /**
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/QuantumToolkit.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/QuantumToolkit.java	Fri Aug 29 15:47:13 2014 -0700
@@ -374,6 +374,22 @@
         }
     }
 
+    /**
+     * Runs the specified supplier, first acquiring the renderLock.
+     * The lock is released when done.
+     * @param <T> the type of the return value
+     * @param supplier the supplier to be run
+     * @return the return value from calling supplier.get()
+     */
+    public static <T> T runWithRenderLock(Supplier<T> supplier) {
+        ViewPainter.renderLock.lock();
+        try {
+            return supplier.get();
+        } finally {
+            ViewPainter.renderLock.unlock();
+        }
+    }
+
     boolean hasNativeSystemVsync() {
         return nativeSystemVsync;
     }
@@ -703,15 +719,13 @@
 
         notifyShutdownHooks();
 
-        ViewPainter.renderLock.lock();
-        try {
+        runWithRenderLock(() -> {
             //TODO - should update glass scene view state
             //TODO - doesn't matter because we are exiting
             Application app = Application.GetApplication();
             app.terminate();
-        } finally {
-            ViewPainter.renderLock.unlock();
-        }
+            return null;
+        });
 
         dispose();
 
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/ViewScene.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/ViewScene.java	Fri Aug 29 15:47:13 2014 -0700
@@ -94,14 +94,12 @@
     @Override
     public void dispose() {
         if (platformView != null) {
-            ViewPainter.renderLock.lock();
-            try {
+            QuantumToolkit.runWithRenderLock(() -> {
                 platformView.close();
                 platformView = null;
                 updateSceneState();
-            } finally {
-                ViewPainter.renderLock.unlock();
-            }
+                return null;
+            });
         }
         super.dispose();
     }
--- a/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java	Fri Aug 29 14:28:21 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java	Fri Aug 29 15:47:13 2014 -0700
@@ -245,18 +245,15 @@
         if (scene != null) {
             GlassScene newScene = getViewScene();
             View view = newScene.getPlatformView();
-            ViewPainter.renderLock.lock();
-            try {
+            QuantumToolkit.runWithRenderLock(() -> {
                 platformWindow.setView(view);
                 if (oldScene != null) oldScene.updateSceneState();
                 newScene.updateSceneState();
-            } finally {
-                ViewPainter.renderLock.unlock();
-            }
+                return null;
+            });
             requestFocus();
         } else {
-            ViewPainter.renderLock.lock();
-            try {
+            QuantumToolkit.runWithRenderLock(() -> {
                 // platformWindow can be null here, if this window is owned,
                 // and its owner is being closed.
                 if (platformWindow != null) {
@@ -265,9 +262,8 @@
                 if (oldScene != null) {
                     oldScene.updateSceneState();
                 }
-            } finally {
-                ViewPainter.renderLock.unlock();
-            }
+                return null;
+            });
         }
         if (oldScene != null) {
             ViewPainter painter = ((ViewScene)oldScene).getPainter();
@@ -447,17 +443,15 @@
                 }
             }
         }
-        try {
-            ViewPainter.renderLock.lock();
+        QuantumToolkit.runWithRenderLock(() -> {
             // platformWindow can be null here, if this window is owned,
             // and its owner is being closed.
             if (platformWindow != null) {
                 platformWindow.setVisible(visible);
             }
             super.setVisible(visible);
-        } finally {
-            ViewPainter.renderLock.unlock();
-        }
+            return null;
+        });
         // After setting visible to true on the native window, we block
         // other windows.
         if (visible) {
@@ -710,8 +704,7 @@
     
     @Override public void close() {
         super.close();
-        ViewPainter.renderLock.lock();
-        try {
+        QuantumToolkit.runWithRenderLock(() -> {
             // prevents closing a closed platform window
             if (platformWindow != null) {
                 platformWindows.remove(platformWindow);
@@ -722,9 +715,8 @@
             if (oldScene != null) {
                 oldScene.updateSceneState();
             }
-        } finally {
-            ViewPainter.renderLock.unlock();
-        }
+            return null;
+        });
     }
 
     // setPlatformWindowClosed is only set upon receiving platform window has