From b7c4bd07792e4afbe7fd413730f24db495456cbd Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Wed, 11 Jun 2025 15:12:38 -0700 Subject: [PATCH 1/2] 8359266: Delete the usage of AppContext in the GraphicsDevice --- .../classes/java/awt/GraphicsDevice.java | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/src/java.desktop/share/classes/java/awt/GraphicsDevice.java b/src/java.desktop/share/classes/java/awt/GraphicsDevice.java index ed331e4915abb..63b9543014edc 100644 --- a/src/java.desktop/share/classes/java/awt/GraphicsDevice.java +++ b/src/java.desktop/share/classes/java/awt/GraphicsDevice.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2025, 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,7 +27,6 @@ import java.awt.image.ColorModel; -import sun.awt.AppContext; import sun.awt.SunToolkit; /** @@ -75,13 +74,7 @@ */ public abstract class GraphicsDevice { - private Window fullScreenWindow; - private AppContext fullScreenAppContext; // tracks which AppContext - // created the FS window - // this lock is used for making synchronous changes to the AppContext's - // current full screen window - private final Object fsAppContextLock = new Object(); - + private volatile Window fullScreenWindow; private Rectangle windowedModeBounds; /** @@ -303,15 +296,7 @@ public void setFullScreenWindow(Window w) { fullScreenWindow.setBounds(windowedModeBounds); } // Set the full screen window - synchronized (fsAppContextLock) { - // Associate fullscreen window with current AppContext - if (w == null) { - fullScreenAppContext = null; - } else { - fullScreenAppContext = AppContext.getAppContext(); - } - fullScreenWindow = w; - } + fullScreenWindow = w; if (fullScreenWindow != null) { windowedModeBounds = fullScreenWindow.getBounds(); // Note that we use the graphics configuration of the device, @@ -319,7 +304,7 @@ public void setFullScreenWindow(Window w) { // this device. final GraphicsConfiguration gc = getDefaultConfiguration(); final Rectangle screenBounds = gc.getBounds(); - if (SunToolkit.isDispatchThreadForAppContext(fullScreenWindow)) { + if (EventQueue.isDispatchThread()) { // Update graphics configuration here directly and do not wait // asynchronous notification from the peer. Note that // setBounds() will reset a GC, if it was set incorrectly. @@ -342,15 +327,7 @@ public void setFullScreenWindow(Window w) { * @since 1.4 */ public Window getFullScreenWindow() { - Window returnWindow = null; - synchronized (fsAppContextLock) { - // Only return a handle to the current fs window if we are in the - // same AppContext that set the fs window - if (fullScreenAppContext == AppContext.getAppContext()) { - returnWindow = fullScreenWindow; - } - } - return returnWindow; + return fullScreenWindow; } /** From f4856925d0415b8329b12e6213de85ee7d5591ce Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Wed, 11 Jun 2025 23:45:04 -0700 Subject: [PATCH 2/2] Create FullScreenWindowRace.java --- .../GraphicsDevice/FullScreenWindowRace.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/jdk/java/awt/GraphicsDevice/FullScreenWindowRace.java diff --git a/test/jdk/java/awt/GraphicsDevice/FullScreenWindowRace.java b/test/jdk/java/awt/GraphicsDevice/FullScreenWindowRace.java new file mode 100644 index 0000000000000..fcc95546764a6 --- /dev/null +++ b/test/jdk/java/awt/GraphicsDevice/FullScreenWindowRace.java @@ -0,0 +1,65 @@ +/* + * Copyright Amazon.com Inc. 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.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Window; + +/** + * @test + * @key headful + * @bug 8359266 + * @summary Tests for a race condition when setting a full-screen window + */ +public final class FullScreenWindowRace { + + public static void main(String[] args) throws InterruptedException { + Window window = new Window(null); + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice(); + + Thread thread = new Thread(() -> { + while (gd.getFullScreenWindow() == null) { + // Busy wait - can be optimized away without volatile + } + }); + + thread.setDaemon(true); + thread.start(); + + // Give thread some time to start and begin the loop + Thread.sleep(2000); + + gd.setFullScreenWindow(window); + + thread.join(15000); + + boolean alive = thread.isAlive(); + + gd.setFullScreenWindow(null); + window.dispose(); + if (alive) { + throw new RuntimeException("Full screen window is NOT detected!"); + } + } +}