Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linux dpi #128

Merged
merged 4 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/SkijaInjectSample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ val additionalArguments = mutableMapOf<String, String>()

val casualRun = tasks.named<JavaExec>("run") {
systemProperty("skiko.fps.enabled", "true")
systemProperty("skiko.linux.autodpi", "true")
systemProperty("skiko.hardwareInfo.enabled", "true")
jvmArgs?.add("-ea")
// Use systemProperty("skiko.library.path", "/tmp") to test loader.
Expand Down
27 changes: 22 additions & 5 deletions skiko/src/jvmMain/cpp/linux/drawlayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,33 @@ extern "C"
return 1;
}

double getDpiScale() {
Display *display = XOpenDisplay(nullptr);
if (display != nullptr) {
double result = getDpiScaleByDisplay(display);
XCloseDisplay(display);
return result;
} else {
return 1;
}
}

JNIEXPORT jfloat JNICALL Java_org_jetbrains_skiko_PlatformOperationsKt_linuxGetDpiScaleNative(JNIEnv *env, jobject properties, jlong platformInfoPtr)
{
JAWT_X11DrawingSurfaceInfo *dsi_x11 = fromJavaPointer<JAWT_X11DrawingSurfaceInfo *>(platformInfoPtr);
return (float) getDpiScaleByDisplay(dsi_x11->display);
}

JNIEXPORT jint JNICALL Java_org_jetbrains_skiko_SystemThemeKt_getCurrentSystemTheme(JNIEnv *env, jobject topLevel)
{
// Unknown.
return 2;
}
JNIEXPORT jint JNICALL Java_org_jetbrains_skiko_SystemThemeKt_getCurrentSystemTheme(JNIEnv *env, jobject topLevel)
{
// Unknown.
return 2;
}


JNIEXPORT jfloat JNICALL Java_org_jetbrains_skiko_SetupKt_linuxGetSystemDpiScale(JNIEnv *env, jobject layer)
{
return (float) getDpiScale();
}

} // extern "C"
6 changes: 1 addition & 5 deletions skiko/src/jvmMain/kotlin/org/jetbrains/skiko/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ object Library {
}

// TODO move properties to SkikoProperties
Setup.init(
System.getProperty("skiko.rendering.noerasebackground") != "false",
System.getProperty("skiko.rendering.laf.global") == "true",
System.getProperty("skiko.rendering.useScreenMenuBar") != "false"
)
Setup.init()

try {
// Init code executed after library was loaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal val platformOperations: PlatformOperations by lazy {
// Maybe we will apply contentScale manually on all platforms?)

// see also comment for HardwareLayer.checkContentScale
// return component.useDrawingSurfacePlatformInfo(::linuxGetDpiScaleNative)
// return (component as HardwareLayer).useDrawingSurfacePlatformInfo(::linuxGetDpiScaleNative)
igordmn marked this conversation as resolved.
Show resolved Hide resolved
}

override fun createRedrawer(
Expand Down
18 changes: 14 additions & 4 deletions skiko/src/jvmMain/kotlin/org/jetbrains/skiko/Setup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ package org.jetbrains.skiko
import javax.swing.UIManager

object Setup {
private var isInited = false
fun init(
noEraseBackground: Boolean = true,
globalLAF: Boolean = false,
useScreenMenuBar: Boolean = true
noEraseBackground: Boolean = System.getProperty("skiko.rendering.noerasebackground") != "false",
globalLAF: Boolean = System.getProperty("skiko.rendering.laf.global") == "true",
useScreenMenuBar: Boolean = System.getProperty("skiko.rendering.useScreenMenuBar") != "false",
autoLinuxDpi: Boolean = System.getProperty("skiko.linux.autodpi") == "true"
) {
if (hostOs == OS.Linux && autoLinuxDpi) {
val scale = linuxGetSystemDpiScale()
System.setProperty("sun.java2d.uiScale.enabled", "true")
System.setProperty("sun.java2d.uiScale", "$scale")
}
if (noEraseBackground) {
// we have to set this property to avoid render flickering.
System.setProperty("sun.awt.noerasebackground", "true")
Expand All @@ -23,5 +30,8 @@ object Setup {
} catch (e: UnsupportedOperationException) {
// Not all platforms allow this.
}
isInited = true
igordmn marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

private external fun linuxGetSystemDpiScale(): Float