Skip to content

Commit

Permalink
8305578: X11GraphicsDevice.pGetBounds() is slow in remote X11 sessions
Browse files Browse the repository at this point in the history
Reviewed-by: avu, serb
  • Loading branch information
Maxim Kartashev authored and Alexey Ushakov committed May 24, 2023
1 parent 544978c commit d7245f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
35 changes: 32 additions & 3 deletions src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2023, 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
Expand Down Expand Up @@ -116,6 +116,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
Expand Down Expand Up @@ -358,6 +359,11 @@ public void dispatchEvent(XEvent ev) {
} finally {
awtLock();
}
} else {
final XAtom XA_NET_WORKAREA = XAtom.get("_NET_WORKAREA");
final boolean rootWindowWorkareaResized = (ev.get_type() == XConstants.PropertyNotify
&& ev.get_xproperty().get_atom() == XA_NET_WORKAREA.getAtom());
if (rootWindowWorkareaResized) resetScreenInsetsCache();
}
}
});
Expand Down Expand Up @@ -843,8 +849,7 @@ private static Rectangle getWorkArea(long root, int scale)
* When two screens overlap and the first contains a dock(*****), then
* _NET_WORKAREA may start at point x1,y1 and end at point x2,y2.
*/
@Override
public Insets getScreenInsets(final GraphicsConfiguration gc) {
private Insets getScreenInsetsImpl(final GraphicsConfiguration gc) {
GraphicsDevice gd = gc.getDevice();
XNETProtocol np = XWM.getWM().getNETProtocol();
if (np == null || !(gd instanceof X11GraphicsDevice) || !np.active()) {
Expand Down Expand Up @@ -872,6 +877,30 @@ public Insets getScreenInsets(final GraphicsConfiguration gc) {
}
}

private void resetScreenInsetsCache() {
final GraphicsDevice[] devices = ((X11GraphicsEnvironment)GraphicsEnvironment.
getLocalGraphicsEnvironment()).getScreenDevices();
for (var gd : devices) {
((X11GraphicsDevice)gd).resetInsets();
}
}

@Override
public Insets getScreenInsets(final GraphicsConfiguration gc) {
final X11GraphicsDevice device = (X11GraphicsDevice) gc.getDevice();
Insets insets = device.getInsets();
if (insets == null) {
synchronized (device) {
insets = device.getInsets();
if (insets == null) {
insets = getScreenInsetsImpl(gc);
device.setInsets(insets);
}
}
}
return (Insets) insets.clone();
}

/*
* The current implementation of disabling background erasing for
* canvases is that we don't set any native background color
Expand Down
28 changes: 26 additions & 2 deletions src/java.desktop/unix/classes/sun/awt/X11GraphicsDevice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, 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
Expand Down Expand Up @@ -30,13 +30,15 @@
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Window;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;

import sun.awt.util.ThreadGroupUtils;
import sun.java2d.SunGraphicsEnvironment;
Expand Down Expand Up @@ -66,12 +68,15 @@ public final class X11GraphicsDevice extends GraphicsDevice
private static Boolean xrandrExtSupported;
private SunDisplayChanger topLevels = new SunDisplayChanger();
private DisplayMode origDisplayMode;
private volatile Rectangle bounds;
private volatile Insets insets;
private boolean shutdownHookRegistered;
private int scale;

public X11GraphicsDevice(int screennum) {
this.screen = screennum;
this.scale = initScaleFactor();
this.bounds = getBoundsImpl();
}

/**
Expand Down Expand Up @@ -118,7 +123,7 @@ public int scaleDown(int x) {
return Region.clipRound(x / (double)getScaleFactor());
}

public Rectangle getBounds() {
private Rectangle getBoundsImpl() {
Rectangle rect = pGetBounds(getScreen());
if (getScaleFactor() != 1) {
rect.x = scaleDown(rect.x);
Expand All @@ -129,6 +134,23 @@ public Rectangle getBounds() {
return rect;
}

public Rectangle getBounds() {
return bounds.getBounds();
}

public Insets getInsets() {
return insets;
}

public void setInsets(Insets newInsets) {
Objects.requireNonNull(newInsets);
insets = newInsets;
}

public void resetInsets() {
insets = null;
}

/**
* Returns the identification string associated with this graphics
* device.
Expand Down Expand Up @@ -516,6 +538,8 @@ private synchronized DisplayMode getMatchingDisplayMode(DisplayMode dm) {
@Override
public synchronized void displayChanged() {
scale = initScaleFactor();
bounds = getBoundsImpl();
insets = null;
// On X11 the visuals do not change, and therefore we don't need
// to reset the defaultConfig, config, doubleBufferVisuals,
// neither do we need to reset the native data.
Expand Down

1 comment on commit d7245f7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.