8165626: Improved window framing
authorserb
Mon, 03 Oct 2016 19:55:49 +0300
changeset 44752 97a2817b5a9b
parent 44751 69284bf5d34f
child 44753 37d4270a2a8d
8165626: Improved window framing Reviewed-by: ddehaven, prr, mschoene, ssadetsky
jdk/src/java.desktop/macosx/classes/com/apple/eawt/_AppDockIconHandler.java
jdk/src/java.desktop/macosx/classes/sun/java2d/opengl/CGLLayer.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CFRetainedResource.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CImage.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CMenuItem.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformComponent.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformLWWindow.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformView.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CViewPlatformEmbeddedFrame.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CWarningWindow.java
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTSurfaceLayers.h
jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTSurfaceLayers.m
jdk/test/java/awt/TrayIcon/SystemTrayIconHelper.java
--- a/jdk/src/java.desktop/macosx/classes/com/apple/eawt/_AppDockIconHandler.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/com/apple/eawt/_AppDockIconHandler.java	Mon Oct 03 19:55:49 2016 +0300
@@ -73,8 +73,7 @@
     public void setDockIconImage(final Image image) {
         try {
             final CImage cImage = CImage.createFromImage(image);
-            final long nsImagePtr = getNSImagePtrFrom(cImage);
-            nativeSetDockIconImage(nsImagePtr);
+            cImage.execute(_AppDockIconHandler::nativeSetDockIconImage);
         } catch (final Throwable e) {
             throw new RuntimeException(e);
         }
@@ -101,16 +100,4 @@
     void setDockIconProgress(int value) {
         nativeSetDockIconProgress(value);
     }
-
-    static long getNSImagePtrFrom(final CImage cImage) {
-        if (cImage == null) return 0;
-
-        try {
-            final Field cImagePtrField = CFRetainedResource.class.getDeclaredField("ptr");
-            cImagePtrField.setAccessible(true);
-            return cImagePtrField.getLong(cImage);
-        } catch (final Throwable e) {
-            throw new RuntimeException(e);
-        }
-    }
 }
--- a/jdk/src/java.desktop/macosx/classes/sun/java2d/opengl/CGLLayer.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/java2d/opengl/CGLLayer.java	Mon Oct 03 19:55:49 2016 +0300
@@ -108,7 +108,7 @@
         OGLRenderQueue rq = OGLRenderQueue.getInstance();
         rq.lock();
         try {
-            validate(getPointer(), cglsd);
+            execute(ptr -> validate(ptr, cglsd));
         } finally {
             rq.unlock();
         }
@@ -124,7 +124,7 @@
     private void setScale(final int _scale) {
         if (scale != _scale) {
             scale = _scale;
-            nativeSetScale(getPointer(), scale);
+            execute(ptr -> nativeSetScale(ptr, scale));
         }
     }
 
@@ -138,7 +138,7 @@
         OGLRenderQueue rq = OGLRenderQueue.getInstance();
         rq.lock();
         try {
-            blitTexture(getPointer());
+            execute(ptr -> blitTexture(ptr));
         } finally {
             rq.unlock();
         }
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CFRetainedResource.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CFRetainedResource.java	Mon Oct 03 19:55:49 2016 +0300
@@ -25,6 +25,10 @@
 
 package sun.lwawt.macosx;
 
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
 /**
  * Safely holds and disposes of native AppKit resources, using the
  * correct AppKit threading and Objective-C GC semantics.
@@ -36,6 +40,10 @@
     // TODO this pointer should be private and accessed via CFNativeAction class
     protected volatile long ptr;
 
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+    private final Lock writeLock = lock.writeLock();
+    private final Lock readLock = lock.readLock();
+
     /**
      * @param ptr CFRetained native object pointer
      * @param disposeOnAppKitThread is the object needs to be CFReleased on the main thread
@@ -50,21 +58,31 @@
      * @param ptr CFRetained native object pointer
      */
     protected void setPtr(final long ptr) {
-        synchronized (this) {
-            if (this.ptr != 0) dispose();
+        writeLock.lock();
+        try {
+            if (this.ptr != 0) {
+                dispose();
+            }
             this.ptr = ptr;
+        } finally {
+            writeLock.unlock();
         }
     }
 
     /**
-     * Manually CFReleases the native resource
+     * Manually CFReleases the native resource.
      */
     protected void dispose() {
         long oldPtr = 0L;
-        synchronized (this) {
-            if (ptr == 0) return;
+        writeLock.lock();
+        try {
+            if (ptr == 0) {
+                return;
+            }
             oldPtr = ptr;
             ptr = 0;
+        } finally {
+            writeLock.unlock();
         }
 
         nativeCFRelease(oldPtr, disposeOnAppKitThread); // perform outside of the synchronized block
@@ -109,9 +127,14 @@
      *
      * @param  action The native operation
      */
-    public final synchronized void execute(final CFNativeAction action) {
-        if (ptr != 0) {
-            action.run(ptr);
+    public final void execute(final CFNativeAction action) {
+        readLock.lock();
+        try {
+            if (ptr != 0) {
+                action.run(ptr);
+            }
+        } finally {
+            readLock.unlock();
         }
     }
 
@@ -127,9 +150,14 @@
      * @return result of the native operation, usually the native pointer to
      *         some other data
      */
-    final synchronized long executeGet(final CFNativeActionGet action) {
-        if (ptr != 0) {
-            return action.run(ptr);
+    final long executeGet(final CFNativeActionGet action) {
+        readLock.lock();
+        try {
+            if (ptr != 0) {
+                return action.run(ptr);
+            }
+        } finally {
+            readLock.unlock();
         }
         return 0;
     }
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CImage.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CImage.java	Mon Oct 03 19:55:49 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,8 @@
 import java.util.Arrays;
 import java.util.List;
 import java.awt.image.MultiResolutionImage;
+import java.util.concurrent.atomic.AtomicReference;
+
 import sun.awt.image.MultiResolutionCachedImage;
 
 import sun.awt.image.SunWritableRaster;
@@ -254,15 +256,26 @@
 
     /** @return A MultiResolution image created from nsImagePtr, or null. */
     private Image toImage() {
-        if (ptr == 0) return null;
+        if (ptr == 0) {
+            return null;
+        }
 
-        final Dimension2D size = nativeGetNSImageSize(ptr);
+        AtomicReference<Dimension2D> sizeRef = new AtomicReference<>();
+        execute(ptr -> {
+            sizeRef.set(nativeGetNSImageSize(ptr));
+        });
+        final Dimension2D size = sizeRef.get();
+        if (size == null) {
+            return null;
+        }
         final int w = (int)size.getWidth();
         final int h = (int)size.getHeight();
-
-        Dimension2D[] sizes
-                = nativeGetNSImageRepresentationSizes(ptr,
-                        size.getWidth(), size.getHeight());
+        AtomicReference<Dimension2D[]> repRef = new AtomicReference<>();
+        execute(ptr -> {
+            repRef.set(nativeGetNSImageRepresentationSizes(ptr, size.getWidth(),
+                                                           size.getHeight()));
+        });
+        Dimension2D[] sizes = repRef.get();
 
         return sizes == null || sizes.length < 2 ?
                 new MultiResolutionCachedImage(w, h, (width, height)
@@ -275,18 +288,18 @@
         final BufferedImage bimg = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB_PRE);
         final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer();
         final int[] buffer = SunWritableRaster.stealData(dbi, 0);
-        nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight);
+        execute(ptr->nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight));
         SunWritableRaster.markDirty(dbi);
         return bimg;
     }
 
     /** If nsImagePtr != 0 then scale this NSImage. @return *this* */
     CImage resize(final double w, final double h) {
-        if (ptr != 0) nativeSetNSImageSize(ptr, w, h);
+        execute(ptr -> nativeSetNSImageSize(ptr, w, h));
         return this;
     }
 
     void resizeRepresentations(double w, double h) {
-        if (ptr != 0) nativeResizeNSImageRepresentations(ptr, w, h);
+        execute(ptr -> nativeResizeNSImageRepresentations(ptr, w, h));
     }
 }
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CMenuItem.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CMenuItem.java	Mon Oct 03 19:55:49 2016 +0300
@@ -113,7 +113,13 @@
      */
     public final void setImage(final java.awt.Image img) {
         CImage cimg = CImage.getCreator().createFromImage(img);
-        execute(ptr -> nativeSetImage(ptr, cimg == null ? 0L : cimg.ptr));
+        execute(ptr -> {
+            if (cimg == null) {
+                nativeSetImage(ptr, 0L);
+            } else {
+                cimg.execute(imgPtr -> nativeSetImage(ptr, imgPtr));
+            }
+        });
     }
 
     /**
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformComponent.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformComponent.java	Mon Oct 03 19:55:49 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,6 +44,9 @@
         super(0, true);
     }
 
+    /**
+     * Used by JAWT.
+     */
     public long getPointer() {
         return ptr;
     }
@@ -61,7 +64,7 @@
         // translates values from the coordinate system of the top-level window
         // to the coordinate system of the content view
         final Insets insets = platformWindow.getPeer().getInsets();
-        nativeSetBounds(getPointer(), x - insets.left, y - insets.top, w, h);
+        execute(ptr->nativeSetBounds(ptr, x - insets.left, y - insets.top, w, h));
     }
 
     @Override
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformLWWindow.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformLWWindow.java	Mon Oct 03 19:55:49 2016 +0300
@@ -107,11 +107,6 @@
     }
 
     @Override
-    public long getNSWindowPtr() {
-        return 0;
-    }
-
-    @Override
     public SurfaceData getSurfaceData() {
         return null;
     }
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformView.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformView.java	Mon Oct 03 19:55:49 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,9 @@
 
 import java.awt.*;
 import java.awt.geom.Rectangle2D;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
 
 import sun.awt.CGraphicsConfig;
 import sun.awt.CGraphicsEnvironment;
@@ -83,7 +86,7 @@
      * Cocoa coordinates).
      */
     public void setBounds(int x, int y, int width, int height) {
-        CWrapper.NSView.setFrame(ptr, x, y, width, height);
+        execute(ptr->CWrapper.NSView.setFrame(ptr, x, y, width, height));
     }
 
     // REMIND: CGLSurfaceData expects top-level's size
@@ -96,7 +99,7 @@
     }
 
     public void setToolTip(String msg) {
-        CWrapper.NSView.setToolTip(ptr, msg);
+        execute(ptr -> CWrapper.NSView.setToolTip(ptr, msg));
     }
 
     // ----------------------------------------------------------------------
@@ -147,18 +150,25 @@
     }
 
     public void setAutoResizable(boolean toResize) {
-        nativeSetAutoResizable(this.getAWTView(), toResize);
+        execute(ptr -> nativeSetAutoResizable(ptr, toResize));
     }
 
     public boolean isUnderMouse() {
-        return nativeIsViewUnderMouse(getAWTView());
+        AtomicBoolean ref = new AtomicBoolean();
+        execute(ptr -> {
+            ref.set(nativeIsViewUnderMouse(ptr));
+        });
+        return ref.get();
     }
 
     public GraphicsDevice getGraphicsDevice() {
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         CGraphicsEnvironment cge = (CGraphicsEnvironment)ge;
-        int displayID = nativeGetNSViewDisplayID(getAWTView());
-        GraphicsDevice gd = cge.getScreenDevice(displayID);
+        AtomicInteger ref = new AtomicInteger();
+        execute(ptr -> {
+            ref.set(nativeGetNSViewDisplayID(ptr));
+        });
+        GraphicsDevice gd = cge.getScreenDevice(ref.get());
         if (gd == null) {
             // this could possibly happen during device removal
             // use the default screen device in this case
@@ -168,8 +178,15 @@
     }
 
     public Point getLocationOnScreen() {
-        Rectangle r = nativeGetLocationOnScreen(this.getAWTView()).getBounds();
-        return new Point(r.x, r.y);
+        AtomicReference<Rectangle> ref = new AtomicReference<>();
+        execute(ptr -> {
+            ref.set(nativeGetLocationOnScreen(ptr).getBounds());
+        });
+        Rectangle r = ref.get();
+        if (r != null) {
+            return new Point(r.x, r.y);
+        }
+        return new Point(0, 0);
     }
 
     // ----------------------------------------------------------------------
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Mon Oct 03 19:55:49 2016 +0300
@@ -35,6 +35,9 @@
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.swing.*;
 
@@ -201,16 +204,16 @@
             c.setStyleBits(FULLSCREENABLE, fullscrenable);
         }},
         new Property<CPlatformWindow>(WINDOW_SHADOW_REVALIDATE_NOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
-            nativeRevalidateNSWindowShadow(c.getNSWindowPtr());
+            c.execute(ptr -> nativeRevalidateNSWindowShadow(ptr));
         }},
         new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) {
             if (value == null || !(value instanceof java.io.File)) {
-                nativeSetNSWindowRepresentedFilename(c.getNSWindowPtr(), null);
+                c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, null));
                 return;
             }
 
             final String filename = ((java.io.File)value).getAbsolutePath();
-            nativeSetNSWindowRepresentedFilename(c.getNSWindowPtr(), filename);
+            c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, filename));
         }}
     }) {
         @SuppressWarnings("deprecation")
@@ -261,7 +264,6 @@
         contentView = createContentView();
         contentView.initialize(peer, responder);
 
-        final long ownerPtr = owner != null ? owner.getNSWindowPtr() : 0L;
         Rectangle bounds;
         if (!IS(DECORATED, styleBits)) {
             // For undecorated frames the move/resize event does not come if the frame is centered on the screen
@@ -270,9 +272,21 @@
         } else {
             bounds = _peer.constrainBounds(_target.getBounds());
         }
-        final long nativeWindowPtr = nativeCreateNSWindow(contentView.getAWTView(),
-                ownerPtr, styleBits, bounds.x, bounds.y, bounds.width, bounds.height);
-        setPtr(nativeWindowPtr);
+        AtomicLong ref = new AtomicLong();
+        contentView.execute(viewPtr -> {
+            if (owner != null) {
+                owner.execute(ownerPtr -> {
+                    ref.set(nativeCreateNSWindow(viewPtr, ownerPtr, styleBits,
+                                                 bounds.x, bounds.y,
+                                                 bounds.width, bounds.height));
+                });
+            } else {
+                ref.set(nativeCreateNSWindow(viewPtr, 0,
+                                             styleBits, bounds.x, bounds.y,
+                                             bounds.width, bounds.height));
+            }
+        });
+        setPtr(ref.get());
 
         if (target instanceof javax.swing.RootPaneContainer) {
             final javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane();
@@ -436,30 +450,31 @@
 
     // this is the counter-point to -[CWindow _nativeSetStyleBit:]
     private void setStyleBits(final int mask, final boolean value) {
-        nativeSetNSWindowStyleBits(getNSWindowPtr(), mask, value ? mask : 0);
+        execute(ptr -> nativeSetNSWindowStyleBits(ptr, mask, value ? mask : 0));
     }
 
     private native void _toggleFullScreenMode(final long model);
 
     public void toggleFullScreen() {
-        _toggleFullScreenMode(getNSWindowPtr());
+        execute(this::_toggleFullScreenMode);
     }
 
     @Override // PlatformWindow
     public void setMenuBar(MenuBar mb) {
-        final long nsWindowPtr = getNSWindowPtr();
         CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb);
-        if (mbPeer != null) {
-            mbPeer.execute(ptr -> nativeSetNSWindowMenuBar(nsWindowPtr, ptr));
-        } else {
-            nativeSetNSWindowMenuBar(nsWindowPtr, 0);
-        }
+        execute(nsWindowPtr->{
+            if (mbPeer != null) {
+                mbPeer.execute(ptr -> nativeSetNSWindowMenuBar(nsWindowPtr, ptr));
+            } else {
+                nativeSetNSWindowMenuBar(nsWindowPtr, 0);
+            }
+        });
     }
 
     @Override // PlatformWindow
     public void dispose() {
         contentView.dispose();
-        nativeDispose(getNSWindowPtr());
+        execute(CPlatformWindow::nativeDispose);
         CPlatformWindow.super.dispose();
     }
 
@@ -472,7 +487,11 @@
 
     @Override // PlatformWindow
     public Insets getInsets() {
-        return nativeGetNSWindowInsets(getNSWindowPtr());
+        AtomicReference<Insets> ref = new AtomicReference<>();
+        execute(ptr -> {
+            ref.set(nativeGetNSWindowInsets(ptr));
+        });
+        return ref.get() != null ? ref.get() : new Insets(0, 0, 0, 0);
     }
 
     @Override // PlatformWindow
@@ -498,11 +517,11 @@
 
     @Override // PlatformWindow
     public void setBounds(int x, int y, int w, int h) {
-        nativeSetNSWindowBounds(getNSWindowPtr(), x, y, w, h);
+        execute(ptr -> nativeSetNSWindowBounds(ptr, x, y, w, h));
     }
 
     public void setMaximizedBounds(int x, int y, int w, int h) {
-        nativeSetNSWindowStandardFrame(getNSWindowPtr(), x, y, w, h);
+        execute(ptr -> nativeSetNSWindowStandardFrame(ptr, x, y, w, h));
     }
 
     private boolean isMaximized() {
@@ -515,7 +534,7 @@
             return;
         }
         if (!undecorated) {
-            CWrapper.NSWindow.zoom(getNSWindowPtr());
+            execute(CWrapper.NSWindow::zoom);
         } else {
             deliverZoom(true);
 
@@ -534,7 +553,7 @@
             return;
         }
         if (!undecorated) {
-            CWrapper.NSWindow.zoom(getNSWindowPtr());
+            execute(CWrapper.NSWindow::zoom);
         } else {
             deliverZoom(false);
 
@@ -550,8 +569,6 @@
 
     @Override // PlatformWindow
     public void setVisible(boolean visible) {
-        final long nsWindowPtr = getNSWindowPtr();
-
         // Configure stuff
         updateIconImages();
         updateFocusabilityForAutoRequestFocus(false);
@@ -559,7 +576,7 @@
         boolean wasMaximized = isMaximized();
 
         if (visible && target.isLocationByPlatform()) {
-            nativeSetNSWindowLocationByPlatform(getNSWindowPtr());
+            execute(CPlatformWindow::nativeSetNSWindowLocationByPlatform);
         }
 
         // Actually show or hide the window
@@ -567,30 +584,44 @@
         if (blocker == null || !visible) {
             // If it ain't blocked, or is being hidden, go regular way
             if (visible) {
-                CWrapper.NSWindow.makeFirstResponder(nsWindowPtr, contentView.getAWTView());
+                contentView.execute(viewPtr -> {
+                    execute(ptr -> CWrapper.NSWindow.makeFirstResponder(ptr,
+                                                                        viewPtr));
+                });
 
                 boolean isPopup = (target.getType() == Window.Type.POPUP);
-                if (isPopup) {
-                    // Popups in applets don't activate applet's process
-                    CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr);
-                } else {
-                    CWrapper.NSWindow.orderFront(nsWindowPtr);
-                }
+                execute(ptr -> {
+                    if (isPopup) {
+                        // Popups in applets don't activate applet's process
+                        CWrapper.NSWindow.orderFrontRegardless(ptr);
+                    } else {
+                        CWrapper.NSWindow.orderFront(ptr);
+                    }
 
-                boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(nsWindowPtr);
-                if (!isKeyWindow) {
-                    CWrapper.NSWindow.makeKeyWindow(nsWindowPtr);
-                }
+                    boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(ptr);
+                    if (!isKeyWindow) {
+                        CWrapper.NSWindow.makeKeyWindow(ptr);
+                    }
+                });
             } else {
-                // immediately hide the window
-                CWrapper.NSWindow.orderOut(nsWindowPtr);
-                // process the close
-                CWrapper.NSWindow.close(nsWindowPtr);
+                execute(ptr->{
+                    // immediately hide the window
+                    CWrapper.NSWindow.orderOut(ptr);
+                    // process the close
+                    CWrapper.NSWindow.close(ptr);
+                });
             }
         } else {
             // otherwise, put it in a proper z-order
-            CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowBelow,
-                    ((CPlatformWindow)blocker.getPlatformWindow()).getNSWindowPtr());
+            CPlatformWindow bw
+                    = (CPlatformWindow) blocker.getPlatformWindow();
+            bw.execute(blockerPtr -> {
+                execute(ptr -> {
+                    CWrapper.NSWindow.orderWindow(ptr,
+                                                  CWrapper.NSWindow.NSWindowBelow,
+                                                  blockerPtr);
+                });
+            });
         }
         this.visible = visible;
 
@@ -609,7 +640,7 @@
                     }
                     switch (frameState) {
                         case Frame.ICONIFIED:
-                            CWrapper.NSWindow.miniaturize(nsWindowPtr);
+                            execute(CWrapper.NSWindow::miniaturize);
                             break;
                         case Frame.MAXIMIZED_BOTH:
                             maximize();
@@ -633,7 +664,11 @@
         if (visible) {
             // Order myself above my parent
             if (owner != null && owner.isVisible()) {
-                CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowAbove, owner.getNSWindowPtr());
+                owner.execute(ownerPtr -> {
+                    execute(ptr -> {
+                        CWrapper.NSWindow.orderWindow(ptr, CWrapper.NSWindow.NSWindowAbove, ownerPtr);
+                    });
+                });
                 applyWindowLevel(target);
             }
 
@@ -643,7 +678,11 @@
                 if (p instanceof LWWindowPeer) {
                     CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow();
                     if (pw != null && pw.isVisible()) {
-                        CWrapper.NSWindow.orderWindow(pw.getNSWindowPtr(), CWrapper.NSWindow.NSWindowAbove, nsWindowPtr);
+                        pw.execute(childPtr -> {
+                            execute(ptr -> {
+                                CWrapper.NSWindow.orderWindow(childPtr, CWrapper.NSWindow.NSWindowAbove, ptr);
+                            });
+                        });
                         pw.applyWindowLevel(w);
                     }
                 }
@@ -659,25 +698,22 @@
 
     @Override // PlatformWindow
     public void setTitle(String title) {
-        nativeSetNSWindowTitle(getNSWindowPtr(), title);
+        execute(ptr -> nativeSetNSWindowTitle(ptr, title));
     }
 
     // Should be called on every window key property change.
     @Override // PlatformWindow
     public void updateIconImages() {
-        final long nsWindowPtr = getNSWindowPtr();
         final CImage cImage = getImageForTarget();
-        nativeSetNSWindowMinimizedIcon(nsWindowPtr, cImage == null ? 0L : cImage.ptr);
-    }
-
-    public long getNSWindowPtr() {
-        final long nsWindowPtr = ptr;
-        if (nsWindowPtr == 0L) {
-            if(logger.isLoggable(PlatformLogger.Level.FINE)) {
-                logger.fine("NSWindow already disposed?", new Exception("Pointer to native NSWindow is invalid."));
+        execute(ptr -> {
+            if (cImage == null) {
+                nativeSetNSWindowMinimizedIcon(ptr, 0L);
+            } else {
+                cImage.execute(imagePtr -> {
+                    nativeSetNSWindowMinimizedIcon(ptr, imagePtr);
+                });
             }
-        }
-        return nsWindowPtr;
+        });
     }
 
     public SurfaceData getSurfaceData() {
@@ -686,13 +722,11 @@
 
     @Override  // PlatformWindow
     public void toBack() {
-        final long nsWindowPtr = getNSWindowPtr();
-        nativePushNSWindowToBack(nsWindowPtr);
+        execute(CPlatformWindow::nativePushNSWindowToBack);
     }
 
     @Override  // PlatformWindow
     public void toFront() {
-        final long nsWindowPtr = getNSWindowPtr();
         LWCToolkit lwcToolkit = (LWCToolkit) Toolkit.getDefaultToolkit();
         Window w = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
         final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
@@ -702,7 +736,7 @@
             lwcToolkit.activateApplicationIgnoringOtherApps();
         }
         updateFocusabilityForAutoRequestFocus(false);
-        nativePushNSWindowToFront(nsWindowPtr);
+        execute(CPlatformWindow::nativePushNSWindowToFront);
         updateFocusabilityForAutoRequestFocus(true);
     }
 
@@ -729,7 +763,7 @@
 
     @Override
     public void setSizeConstraints(int minW, int minH, int maxW, int maxH) {
-        nativeSetNSWindowMinMax(getNSWindowPtr(), minW, minH, maxW, maxH);
+        execute(ptr -> nativeSetNSWindowMinMax(ptr, minW, minH, maxW, maxH));
     }
 
     @Override
@@ -746,19 +780,22 @@
 
     @Override
     public boolean requestWindowFocus() {
-
-        long ptr = getNSWindowPtr();
-        if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) {
-            CWrapper.NSWindow.makeMainWindow(ptr);
-        }
-        CWrapper.NSWindow.makeKeyAndOrderFront(ptr);
+        execute(ptr -> {
+            if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) {
+                CWrapper.NSWindow.makeMainWindow(ptr);
+            }
+            CWrapper.NSWindow.makeKeyAndOrderFront(ptr);
+        });
         return true;
     }
 
     @Override
     public boolean isActive() {
-        long ptr = getNSWindowPtr();
-        return CWrapper.NSWindow.isKeyWindow(ptr);
+        AtomicBoolean ref = new AtomicBoolean();
+        execute(ptr -> {
+            ref.set(CWrapper.NSWindow.isKeyWindow(ptr));
+        });
+        return ref.get();
     }
 
     @Override
@@ -774,21 +811,21 @@
 
     @Override
     public void setOpacity(float opacity) {
-        CWrapper.NSWindow.setAlphaValue(getNSWindowPtr(), opacity);
+        execute(ptr -> CWrapper.NSWindow.setAlphaValue(ptr, opacity));
     }
 
     @Override
     public void setOpaque(boolean isOpaque) {
-        CWrapper.NSWindow.setOpaque(getNSWindowPtr(), isOpaque);
+        execute(ptr -> CWrapper.NSWindow.setOpaque(ptr, isOpaque));
         boolean isTextured = (peer == null) ? false : peer.isTextured();
         if (!isTextured) {
             if (!isOpaque) {
-                CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), 0);
+                execute(ptr -> CWrapper.NSWindow.setBackgroundColor(ptr, 0));
             } else if (peer != null) {
                 Color color = peer.getBackground();
                 if (color != null) {
                     int rgb = color.getRGB();
-                    CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), rgb);
+                    execute(ptr->CWrapper.NSWindow.setBackgroundColor(ptr, rgb));
                 }
             }
         }
@@ -801,12 +838,12 @@
     @Override
     public void enterFullScreenMode() {
         isFullScreenMode = true;
-        nativeEnterFullScreenMode(getNSWindowPtr());
+        execute(CPlatformWindow::nativeEnterFullScreenMode);
     }
 
     @Override
     public void exitFullScreenMode() {
-        nativeExitFullScreenMode(getNSWindowPtr());
+        execute(CPlatformWindow::nativeExitFullScreenMode);
         isFullScreenMode = false;
     }
 
@@ -825,7 +862,6 @@
         int prevWindowState = peer.getState();
         if (prevWindowState == windowState) return;
 
-        final long nsWindowPtr = getNSWindowPtr();
         if ((windowState & Frame.ICONIFIED) != 0) {
             // Treat all state bit masks with ICONIFIED bit as ICONIFIED state.
             windowState = Frame.ICONIFIED;
@@ -837,18 +873,18 @@
                     // the zoom call toggles between the normal and the max states
                     unmaximize();
                 }
-                CWrapper.NSWindow.miniaturize(nsWindowPtr);
+                execute(CWrapper.NSWindow::miniaturize);
                 break;
             case Frame.MAXIMIZED_BOTH:
                 if (prevWindowState == Frame.ICONIFIED) {
                     // let's return into the normal states first
-                    CWrapper.NSWindow.deminiaturize(nsWindowPtr);
+                    execute(CWrapper.NSWindow::deminiaturize);
                 }
                 maximize();
                 break;
             case Frame.NORMAL:
                 if (prevWindowState == Frame.ICONIFIED) {
-                    CWrapper.NSWindow.deminiaturize(nsWindowPtr);
+                    execute(CWrapper.NSWindow::deminiaturize);
                 } else if (prevWindowState == Frame.MAXIMIZED_BOTH) {
                     // the zoom call toggles between the normal and the max states
                     unmaximize();
@@ -872,15 +908,15 @@
             // We are going to show a modal window. Previously displayed window will be
             // blocked/disabled. So we have to send mouse exited event to it now, since
             // all mouse events are discarded for blocked/disabled windows.
-            nativeSynthesizeMouseEnteredExitedEvents(getNSWindowPtr(), CocoaConstants.NSMouseExited);
+            execute(ptr -> nativeSynthesizeMouseEnteredExitedEvents(ptr, CocoaConstants.NSMouseExited));
         }
 
-        nativeSetEnabled(getNSWindowPtr(), !blocked);
+        execute(ptr -> nativeSetEnabled(ptr, !blocked));
         checkBlockingAndOrder();
     }
 
-    public final void invalidateShadow(){
-        nativeRevalidateNSWindowShadow(getNSWindowPtr());
+    public final void invalidateShadow() {
+        execute(ptr -> nativeRevalidateNSWindowShadow(ptr));
     }
 
     // ----------------------------------------------------------------------
@@ -975,7 +1011,11 @@
 
     protected void deliverMoveResizeEvent(int x, int y, int width, int height,
                                         boolean byUser) {
-        isZoomed = CWrapper.NSWindow.isZoomed(getNSWindowPtr());
+        AtomicBoolean ref = new AtomicBoolean();
+        execute(ptr -> {
+            ref.set(CWrapper.NSWindow.isZoomed(ptr));
+        });
+        isZoomed = ref.get();
         checkZoom();
 
         final Rectangle oldB = nativeBounds;
@@ -1069,11 +1109,11 @@
 
         pWindow.orderAboveSiblings();
 
-        final long nsWindowPtr = pWindow.getNSWindowPtr();
-        CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr);
-        CWrapper.NSWindow.makeKeyAndOrderFront(nsWindowPtr);
-        CWrapper.NSWindow.makeMainWindow(nsWindowPtr);
-
+        pWindow.execute(ptr -> {
+            CWrapper.NSWindow.orderFrontRegardless(ptr);
+            CWrapper.NSWindow.makeKeyAndOrderFront(ptr);
+            CWrapper.NSWindow.makeMainWindow(ptr);
+        });
         return true;
     }
 
@@ -1112,7 +1152,7 @@
         // which is going to become 'main window', are placed above their siblings.
         CPlatformWindow rootOwner = getRootOwner();
         if (rootOwner.isVisible() && !rootOwner.isIconified()) {
-            CWrapper.NSWindow.orderFront(rootOwner.getNSWindowPtr());
+            rootOwner.execute(CWrapper.NSWindow::orderFront);
         }
         // Do not order child windows of iconified owner.
         if (!rootOwner.isIconified()) {
@@ -1139,10 +1179,13 @@
                     // the window should be ordered above its siblings; otherwise the window is just ordered
                     // above its nearest parent.
                     if (pw.isOneOfOwnersOrSelf(this)) {
-                        CWrapper.NSWindow.orderFront(pw.getNSWindowPtr());
+                        pw.execute(CWrapper.NSWindow::orderFront);
                     } else {
-                        CWrapper.NSWindow.orderWindow(pw.getNSWindowPtr(), CWrapper.NSWindow.NSWindowAbove,
-                                pw.owner.getNSWindowPtr());
+                        pw.owner.execute(ownerPtr -> {
+                            pw.execute(ptr -> {
+                                CWrapper.NSWindow.orderWindow(ptr, CWrapper.NSWindow.NSWindowAbove, ownerPtr);
+                            });
+                        });
                     }
                     pw.applyWindowLevel(w);
                 }
@@ -1164,9 +1207,9 @@
 
     protected void applyWindowLevel(Window target) {
         if (target.isAlwaysOnTop() && target.getType() != Window.Type.POPUP) {
-            CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel);
+            execute(ptr->CWrapper.NSWindow.setLevel(ptr, CWrapper.NSWindow.NSFloatingWindowLevel));
         } else if (target.getType() == Window.Type.POPUP) {
-            CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSPopUpMenuWindowLevel);
+            execute(ptr->CWrapper.NSWindow.setLevel(ptr, CWrapper.NSWindow.NSPopUpMenuWindowLevel));
         }
     }
 
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java	Mon Oct 03 19:55:49 2016 +0300
@@ -36,6 +36,7 @@
 import java.awt.peer.TrayIconPeer;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import java.util.concurrent.atomic.AtomicReference;
 
 import static sun.awt.AWTAccessor.*;
 
@@ -91,10 +92,6 @@
         return nativeCreate();
     }
 
-    private long getModel() {
-        return ptr;
-    }
-
     private native long nativeCreate();
 
     //invocation from the AWTTrayIcon.m
@@ -168,7 +165,7 @@
 
     @Override
     public void setToolTip(String tooltip) {
-        nativeSetToolTip(getModel(), tooltip);
+        execute(ptr -> nativeSetToolTip(ptr, tooltip));
     }
 
     //adds tooltip to the NSStatusBar's NSButton.
@@ -197,7 +194,12 @@
         }
 
         CImage cimage = CImage.getCreator().createFromImage(image);
-        setNativeImage(getModel(), cimage.ptr, target.isImageAutoSize());
+        boolean imageAutoSize = target.isImageAutoSize();
+        cimage.execute(imagePtr -> {
+            execute(ptr -> {
+                setNativeImage(ptr, imagePtr, imageAutoSize);
+            });
+        });
     }
 
     private native void setNativeImage(final long model, final long nsimage, final boolean autosize);
@@ -377,7 +379,14 @@
     private void showMessageDialog() {
 
         Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
-        Point2D iconLoc = nativeGetIconLocation(getModel());
+        AtomicReference<Point2D> ref = new AtomicReference<>();
+        execute(ptr -> {
+            ref.set(nativeGetIconLocation(ptr));
+        });
+        Point2D iconLoc = ref.get();
+        if (iconLoc == null) {
+            return;
+        }
 
         int dialogY = (int)iconLoc.getY();
         int dialogX = (int)iconLoc.getX();
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CViewPlatformEmbeddedFrame.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CViewPlatformEmbeddedFrame.java	Mon Oct 03 19:55:49 2016 +0300
@@ -73,13 +73,13 @@
 
     @Override
     public void dispose() {
-        CWrapper.NSView.removeFromSuperview(view.getAWTView());
+        view.execute(CWrapper.NSView::removeFromSuperview);
         view.dispose();
     }
 
     @Override
     public void setVisible(boolean visible) {
-        CWrapper.NSView.setHidden(view.getAWTView(), !visible);
+        view.execute(ptr -> CWrapper.NSView.setHidden(ptr, !visible));
     }
 
     @Override
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CWarningWindow.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CWarningWindow.java	Mon Oct 03 19:55:49 2016 +0300
@@ -204,14 +204,14 @@
     @Override
     public void setVisible(boolean visible) {
         synchronized (lock) {
-            final long nsWindowPtr = getNSWindowPtr();
-
-            // Actually show or hide the window
-            if (visible) {
-                CWrapper.NSWindow.orderFront(nsWindowPtr);
-            } else {
-                CWrapper.NSWindow.orderOut(nsWindowPtr);
-            }
+            execute(ptr -> {
+                // Actually show or hide the window
+                if (visible) {
+                    CWrapper.NSWindow.orderFront(ptr);
+                } else {
+                    CWrapper.NSWindow.orderOut(ptr);
+                }
+            });
 
             this.visible = visible;
 
@@ -219,8 +219,13 @@
             if (visible) {
                 // Order myself above my parent
                 if (owner != null && owner.isVisible()) {
-                    CWrapper.NSWindow.orderWindow(nsWindowPtr,
-                            CWrapper.NSWindow.NSWindowAbove, owner.getNSWindowPtr());
+                    owner.execute(ownerPtr -> {
+                        execute(ptr -> {
+                            CWrapper.NSWindow.orderWindow(ptr,
+                                                          CWrapper.NSWindow.NSWindowAbove,
+                                                          ownerPtr);
+                        });
+                    });
 
                     // do not allow security warning to be obscured by other windows
                     applyWindowLevel(ownerWindow);
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTSurfaceLayers.h	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTSurfaceLayers.h	Mon Oct 03 19:55:49 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -41,6 +41,8 @@
     CALayer *windowLayer;
 }
 
+@property (atomic, retain) CALayer *windowLayer;
+
 - (id) initWithWindowLayer: (CALayer *)windowLayer;
 - (void) setBounds: (CGRect)rect;
 
--- a/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTSurfaceLayers.m	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/src/java.desktop/macosx/native/libawt_lwawt/awt/AWTSurfaceLayers.m	Mon Oct 03 19:55:49 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,11 +38,15 @@
     self = [super init];
     if (self == nil) return self;
 
-    windowLayer = aWindowLayer;
+    self.windowLayer = aWindowLayer;
 
     return self;
 }
 
+- (void) dealloc {
+    self.windowLayer = nil;
+    [super dealloc];
+}
 
 - (CALayer *) layer {
     return layer;
--- a/jdk/test/java/awt/TrayIcon/SystemTrayIconHelper.java	Wed Apr 19 14:28:02 2017 +0200
+++ b/jdk/test/java/awt/TrayIcon/SystemTrayIconHelper.java	Mon Oct 03 19:55:49 2016 +0300
@@ -1,3 +1,26 @@
+/*
+ * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.geom.Point2D;
@@ -86,10 +109,11 @@
 
 
                 Object peer = f_peer.get(icon);
-                Method m_getModel = peer.getClass().getDeclaredMethod(
-                        "getModel");
+                Class<?> superclass = peer.getClass().getSuperclass();
+                System.out.println("superclass = " + superclass);
+                Field m_getModel = superclass.getDeclaredField("ptr");
                 m_getModel.setAccessible(true);
-                long model = (Long) (m_getModel.invoke(peer, new Object[]{}));
+                long model = (Long) m_getModel.get(peer);
                 Method m_getLocation = peer.getClass().getDeclaredMethod(
                         "nativeGetIconLocation", new Class[]{Long.TYPE});
                 m_getLocation.setAccessible(true);