8378389: Remove AppContext from the Swing RepaintManager

Reviewed-by: azvegint, serb
This commit is contained in:
Phil Race 2026-03-01 17:35:56 +00:00
parent 0540e980ef
commit 9cf9fbec1f
4 changed files with 32 additions and 54 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2026, 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
@ -4885,8 +4885,7 @@ public abstract class JComponent extends Container implements Serializable,
* @see RepaintManager#addDirtyRegion
*/
public void repaint(long tm, int x, int y, int width, int height) {
RepaintManager.currentManager(SunToolkit.targetToAppContext(this))
.addDirtyRegion(this, x, y, width, height);
RepaintManager.currentManager(this).addDirtyRegion(this, x, y, width, height);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2026, 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
@ -119,8 +119,6 @@ public class RepaintManager
*/
private PaintManager paintManager;
private static final Object repaintManagerKey = RepaintManager.class;
// Whether or not a VolatileImage should be used for double-buffered painting
static boolean volatileImageBufferEnabled = true;
/**
@ -236,8 +234,10 @@ public class RepaintManager
}
}
private static RepaintManager repaintManager;
/**
* Return the RepaintManager for the calling thread given a Component.
* Return the RepaintManager given a Component.
*
* @param c a Component -- unused in the default implementation, but could
* be used by an overridden version to return a different RepaintManager
@ -249,21 +249,15 @@ public class RepaintManager
// component is ever used to determine the current
// RepaintManager, DisplayChangedRunnable will need to be modified
// accordingly.
return currentManager(AppContext.getAppContext());
}
/**
* Returns the RepaintManager for the specified AppContext. If
* a RepaintManager has not been created for the specified
* AppContext this will return null.
*/
static RepaintManager currentManager(AppContext appContext) {
RepaintManager rm = (RepaintManager)appContext.get(repaintManagerKey);
if (rm == null) {
rm = new RepaintManager(BUFFER_STRATEGY_TYPE);
appContext.put(repaintManagerKey, rm);
synchronized (RepaintManager.class) {
RepaintManager rm = repaintManager;
if (rm == null) {
rm = new RepaintManager(BUFFER_STRATEGY_TYPE);
repaintManager = rm;
}
return rm;
}
return rm;
}
/**
@ -282,16 +276,12 @@ public class RepaintManager
/**
* Set the RepaintManager that should be used for the calling
* thread. <b>aRepaintManager</b> will become the current RepaintManager
* for the calling thread's thread group.
* Set the RepaintManager.
* @param aRepaintManager the RepaintManager object to use
*/
public static void setCurrentManager(RepaintManager aRepaintManager) {
if (aRepaintManager != null) {
SwingUtilities.appContextPut(repaintManagerKey, aRepaintManager);
} else {
SwingUtilities.appContextRemove(repaintManagerKey);
synchronized (RepaintManager.class) {
repaintManager = aRepaintManager;
}
}
@ -373,7 +363,7 @@ public class RepaintManager
// Queue a Runnable to invoke paintDirtyRegions and
// validateInvalidComponents.
scheduleProcessingRunnable(SunToolkit.targetToAppContext(invalidComponent));
scheduleProcessingRunnable();
}
@ -460,7 +450,7 @@ public class RepaintManager
// Queue a Runnable to invoke paintDirtyRegions and
// validateInvalidComponents.
scheduleProcessingRunnable(SunToolkit.targetToAppContext(c));
scheduleProcessingRunnable();
}
/**
@ -530,8 +520,7 @@ public class RepaintManager
// This is called from the toolkit thread when a native expose is
// received.
//
void nativeAddDirtyRegion(AppContext appContext, Container c,
int x, int y, int w, int h) {
void nativeAddDirtyRegion(Container c, int x, int y, int w, int h) {
if (w > 0 && h > 0) {
synchronized(this) {
Rectangle dirty = hwDirtyComponents.get(c);
@ -543,7 +532,7 @@ public class RepaintManager
x, y, w, h, dirty));
}
}
scheduleProcessingRunnable(appContext);
scheduleProcessingRunnable();
}
}
@ -551,8 +540,7 @@ public class RepaintManager
// This is called from the toolkit thread when awt needs to run a
// Runnable before we paint.
//
void nativeQueueSurfaceDataRunnable(AppContext appContext,
final Component c, final Runnable r)
void nativeQueueSurfaceDataRunnable(final Component c, final Runnable r)
{
synchronized(this) {
if (runnableList == null) {
@ -560,7 +548,7 @@ public class RepaintManager
}
runnableList.add(r);
}
scheduleProcessingRunnable(appContext);
scheduleProcessingRunnable();
}
/**
@ -1411,11 +1399,11 @@ public class RepaintManager
return paintManager;
}
private void scheduleProcessingRunnable(AppContext context) {
private void scheduleProcessingRunnable() {
if (processingRunnable.markPending()) {
Toolkit tk = Toolkit.getDefaultToolkit();
if (tk instanceof SunToolkit) {
SunToolkit.getSystemEventQueueImplPP(context).
SunToolkit.getSystemEventQueueImplPP().
postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
processingRunnable));
} else {
@ -1733,8 +1721,7 @@ public class RepaintManager
/**
* Listener installed to detect display changes. When display changes,
* schedules a callback to notify all RepaintManagers of the display
* changes. Only one DisplayChangedHandler is ever installed. The
* singleton instance will schedule notification for all AppContexts.
* changes. Only one DisplayChangedHandler is ever installed.
*/
private static final class DisplayChangedHandler implements
DisplayChangedListener {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, 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
@ -28,7 +28,6 @@ import java.awt.Component;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.PaintEvent;
import sun.awt.AppContext;
import sun.awt.SunToolkit;
import sun.awt.event.IgnorePaintEvent;
@ -52,12 +51,10 @@ class SwingPaintEventDispatcher extends sun.awt.PaintEventDispatcher {
public PaintEvent createPaintEvent(Component component, int x, int y,
int w, int h) {
if (component instanceof RootPaneContainer) {
AppContext appContext = SunToolkit.targetToAppContext(component);
RepaintManager rm = RepaintManager.currentManager(appContext);
RepaintManager rm = RepaintManager.currentManager(component);
if (!SHOW_FROM_DOUBLE_BUFFER ||
!rm.show((Container)component, x, y, w, h)) {
rm.nativeAddDirtyRegion(appContext, (Container)component,
x, y, w, h);
rm.nativeAddDirtyRegion((Container)component, x, y, w, h);
}
// For backward compatibility generate an empty paint
// event. Not doing this broke parts of Netbeans.
@ -65,10 +62,8 @@ class SwingPaintEventDispatcher extends sun.awt.PaintEventDispatcher {
new Rectangle(x, y, w, h));
}
else if (component instanceof SwingHeavyWeight) {
AppContext appContext = SunToolkit.targetToAppContext(component);
RepaintManager rm = RepaintManager.currentManager(appContext);
rm.nativeAddDirtyRegion(appContext, (Container)component,
x, y, w, h);
RepaintManager rm = RepaintManager.currentManager(component);
rm.nativeAddDirtyRegion((Container)component, x, y, w, h);
return new IgnorePaintEvent(component, PaintEvent.PAINT,
new Rectangle(x, y, w, h));
}
@ -81,9 +76,7 @@ class SwingPaintEventDispatcher extends sun.awt.PaintEventDispatcher {
public boolean queueSurfaceDataReplacing(Component c, Runnable r) {
if (c instanceof RootPaneContainer) {
AppContext appContext = SunToolkit.targetToAppContext(c);
RepaintManager.currentManager(appContext).
nativeQueueSurfaceDataRunnable(appContext, c, r);
RepaintManager.currentManager(c).nativeQueueSurfaceDataRunnable(c, r);
return true;
}
return super.queueSurfaceDataReplacing(c, r);

View File

@ -1034,8 +1034,7 @@ public abstract class SunToolkit extends Toolkit
return getSystemEventQueueImplPP();
}
// Package private implementation
static EventQueue getSystemEventQueueImplPP() {
public static EventQueue getSystemEventQueueImplPP() {
return getSystemEventQueueImplPP(AppContext.getAppContext());
}