Commit 0a24e808 authored by Igor Demin's avatar Igor Demin

Vsync: review feedback

parent 1aecfc1a
...@@ -61,7 +61,7 @@ run { ...@@ -61,7 +61,7 @@ run {
test { test {
systemProperty("skiko.test.screenshots.dir", new File(project.projectDir, "src/test/screenshots").absolutePath) systemProperty("skiko.test.screenshots.dir", new File(project.projectDir, "src/test/screenshots").absolutePath)
// Tests should be determined, so disable scaling. // Tests should be deterministic, so disable scaling.
// On MacOs we need the actual scale, otherwise we will have aliased screenshots because of scaling. // On MacOs we need the actual scale, otherwise we will have aliased screenshots because of scaling.
if (System.getProperty("os.name") != "Mac OS X") { if (System.getProperty("os.name") != "Mac OS X") {
systemProperty("sun.java2d.dpiaware", "false") systemProperty("sun.java2d.dpiaware", "false")
......
...@@ -7,10 +7,7 @@ import java.awt.event.ComponentAdapter ...@@ -7,10 +7,7 @@ import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent import java.awt.event.ComponentEvent
import java.awt.event.MouseEvent import java.awt.event.MouseEvent
import java.awt.event.MouseMotionAdapter import java.awt.event.MouseMotionAdapter
import javax.swing.JButton import javax.swing.*
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.WindowConstants
fun Button(text: String): JButton { fun Button(text: String): JButton {
...@@ -19,7 +16,7 @@ fun Button(text: String): JButton { ...@@ -19,7 +16,7 @@ fun Button(text: String): JButton {
return btn return btn
} }
fun SwingSkia() { fun SwingSkia() = SwingUtilities.invokeLater {
val window = JFrame() val window = JFrame()
window.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE window.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
......
...@@ -8,7 +8,10 @@ import java.awt.Robot ...@@ -8,7 +8,10 @@ import java.awt.Robot
import java.io.File import java.io.File
import javax.imageio.ImageIO import javax.imageio.ImageIO
// TODO macOs has wrong colors. Only white, black, red and green are correct // WARNING!!!
// macOS has wrong colors ([128, 128, 128] isn't [128, 128, 128] on screenshot). Only white, black, red and green are correct.
// So use only these color for cross-platform screenshots tests.
// TODO fix colors on macOS
class ScreenshotTestRule(private val robot: Robot) : TestRule { class ScreenshotTestRule(private val robot: Robot) : TestRule {
private lateinit var testIdentifier: String private lateinit var testIdentifier: String
private val screenshotsDir = File(System.getProperty("skiko.test.screenshots.dir")!!) private val screenshotsDir = File(System.getProperty("skiko.test.screenshots.dir")!!)
......
...@@ -15,9 +15,8 @@ internal class FPSCounter( ...@@ -15,9 +15,8 @@ internal class FPSCounter(
* [value] 0.0 - min, 1.0 - max, 0.5 - median * [value] 0.0 - min, 1.0 - max, 0.5 - median
*/ */
private fun MutableList<Double>.quantile(value: Double) : Double { private fun MutableList<Double>.quantile(value: Double) : Double {
sort()
val index = (value * (size - 1)).toInt() val index = (value * (size - 1)).toInt()
return this[index] return sorted()[index]
} }
fun tick() { fun tick() {
......
...@@ -26,7 +26,7 @@ abstract class HardwareLayer : Canvas() { ...@@ -26,7 +26,7 @@ abstract class HardwareLayer : Canvas() {
private fun checkIsShowing() { private fun checkIsShowing() {
if (!isInit && isShowing) { if (!isInit && isShowing) {
_contentScale = platformOperations.getDpiScale(this) _contentScale = getDpiScale()
init() init()
isInit = true isInit = true
} }
...@@ -38,13 +38,19 @@ abstract class HardwareLayer : Canvas() { ...@@ -38,13 +38,19 @@ abstract class HardwareLayer : Canvas() {
protected open fun contentScaleChanged() = Unit protected open fun contentScaleChanged() = Unit
override fun paint(g: Graphics) { override fun paint(g: Graphics) {
val contentScale = platformOperations.getDpiScale(this) val contentScale = getDpiScale()
if (contentScale != _contentScale) { if (contentScale != _contentScale) {
_contentScale = contentScale _contentScale = contentScale
contentScaleChanged() contentScaleChanged()
} }
} }
private fun getDpiScale(): Float {
val scale = platformOperations.getDpiScale(this)
check(scale > 0) { "HardwareLayer.contentScale isn't positive: $contentScale"}
return scale
}
// Should be called in Swing thread // Should be called in Swing thread
internal abstract suspend fun update(nanoTime: Long) internal abstract suspend fun update(nanoTime: Long)
......
...@@ -42,6 +42,7 @@ open class SkiaLayer : HardwareLayer() { ...@@ -42,6 +42,7 @@ open class SkiaLayer : HardwareLayer() {
private val pictureLock = Any() private val pictureLock = Any()
override fun init() { override fun init() {
super.init()
redrawer = platformOperations.createHardwareRedrawer(this) redrawer = platformOperations.createHardwareRedrawer(this)
redrawer?.syncSize() redrawer?.syncSize()
needRedraw() needRedraw()
......
...@@ -18,9 +18,9 @@ internal class MacOsRedrawer( ...@@ -18,9 +18,9 @@ internal class MacOsRedrawer(
override fun draw() = layer.draw() override fun draw() = layer.draw()
} }
// use separate layer for vsync, because with single layer we cannot asynchronously update layer // use a separate layer for vsync, because with single layer we cannot asynchronously update layer
// `update` is suspend, and runBlocking(Dispatchers.Swing) causes dead lock with AppKit Thread. // `update` is suspend, and runBlocking(Dispatchers.Swing) causes dead lock with AppKit Thread.
// AWT has internal method to avoid dead locks but it is internal (sun.lwawt.macosx.LWCToolkit.invokeAndWait) // AWT has a method to avoid dead locks but it is internal (sun.lwawt.macosx.LWCToolkit.invokeAndWait)
private val vsyncLayer = object : AWTGLLayer(containerLayerPtr) { private val vsyncLayer = object : AWTGLLayer(containerLayerPtr) {
@Volatile @Volatile
private var needDraw: CompletableDeferred<Unit>? = null private var needDraw: CompletableDeferred<Unit>? = null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment