JBR-4619 Window content scale wrong after disconnecting external display / waking OS from sleep

Use viewDidChangeBackingProperties notification to adjust a window layer scale
This commit is contained in:
Alexey Ushakov
2022-07-06 19:24:25 +02:00
committed by jbrbot
parent ab4b770a5f
commit 9b52164930
5 changed files with 39 additions and 7 deletions

View File

@@ -63,7 +63,7 @@ public class MTLLayer extends CFLayer {
}
}
public SurfaceData replaceSurfaceData() {
public SurfaceData replaceSurfaceData(int scale) {
if (getBounds().isEmpty()) {
surfaceData = NullSurfaceData.theInstance;
return surfaceData;
@@ -73,7 +73,10 @@ public class MTLLayer extends CFLayer {
// and blits the buffer to the layer surface (in display callback)
MTLGraphicsConfig gc = (MTLGraphicsConfig)getGraphicsConfiguration();
surfaceData = gc.createSurfaceData(this);
setScale(gc.getDevice().getScaleFactor());
if (scale <= 0) {
scale = gc.getDevice().getScaleFactor();
}
setScale(scale);
if (peer != null) {
Insets insets = peer.getInsets();
execute(ptr -> nativeSetInsets(ptr, insets.top, insets.left));

View File

@@ -58,7 +58,7 @@ public class CGLLayer extends CFLayer {
}
}
public SurfaceData replaceSurfaceData() {
public SurfaceData replaceSurfaceData(int scale) {
if (getBounds().isEmpty()) {
surfaceData = NullSurfaceData.theInstance;
return surfaceData;
@@ -72,7 +72,10 @@ public class CGLLayer extends CFLayer {
return surfaceData;
}
surfaceData = gc.createSurfaceData(this);
setScale(gc.getDevice().getScaleFactor());
if (scale <= 0) {
scale = gc.getDevice().getScaleFactor();
}
setScale(scale);
// the layer holds a reference to the buffer, which in
// turn has a reference back to this layer
if (surfaceData instanceof CGLSurfaceData) {

View File

@@ -43,7 +43,11 @@ public abstract class CFLayer extends CFRetainedResource {
super(ptr, disposeOnAppKitThread);
}
public abstract SurfaceData replaceSurfaceData();
public abstract SurfaceData replaceSurfaceData(int scale);
public SurfaceData replaceSurfaceData() {
return replaceSurfaceData(0);
}
@Override
public void dispose() {

View File

@@ -41,8 +41,11 @@ import sun.lwawt.LWWindowPeer;
import sun.java2d.SurfaceData;
import sun.java2d.opengl.CGLLayer;
import sun.util.logging.PlatformLogger;
public class CPlatformView extends CFRetainedResource {
private static final PlatformLogger logger =
PlatformLogger.getLogger(CPlatformView.class.getName());
private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);
private static native void nativeSetAutoResizable(long awtView, boolean toResize);
private static native int nativeGetNSViewDisplayID(long awtView);
@@ -211,4 +214,13 @@ public class CPlatformView extends CFRetainedResource {
private void deliverWindowDidExposeEvent() {
peer.notifyExpose(peer.getSize());
}
private void deliverChangeBackingProperties(float scale) {
if (logger.isLoggable(PlatformLogger.Level.FINE)) {
logger.fine("Changed backing properties, scale = " + scale);
}
if (scale > 0) {
windowLayer.replaceSurfaceData(Math.round(scale));
}
}
}

View File

@@ -1499,8 +1499,18 @@ static jclass jc_CInputMethod = NULL;
/******************************** END NSTextInputClient Protocol ********************************/
- (void)viewDidChangeBackingProperties {
JNIEnv *env = [ThreadUtilities getJNIEnv];
if (self.window.backingScaleFactor > 0) {
self.layer.contentsScale = self.window.backingScaleFactor;
DECLARE_CLASS(jc_CPlatformView, "sun/lwawt/macosx/CPlatformView");
DECLARE_METHOD(deliverChangeBackingProperties, jc_CPlatformView, "deliverChangeBackingProperties", "(F)V");
jobject jlocal = (*env)->NewLocalRef(env, m_cPlatformView);
(*env)->CallVoidMethod(env, jlocal, deliverChangeBackingProperties, self.window.backingScaleFactor);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, jlocal);
}
}
@end // AWTView