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 high CPU usage when window is occluded on macOS #1003

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,9 @@ internal class MetalRedrawer(

// When window is not visible - it doesn't make sense to redraw fast to avoid battery drain.
if (isWindowOccluded) {
withTimeoutOrNull(300) {
// If the window becomes non-occluded, stop waiting immediately
@Suppress("ControlFlowWithEmptyBody")
while (windowOcclusionStateChannel.receive()) { }
}
// If the window becomes non-occluded, stop waiting immediately
@Suppress("ControlFlowWithEmptyBody")
while (windowOcclusionStateChannel.receive()) { }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import kotlinx.cinterop.autoreleasepool
import kotlinx.cinterop.objcPtr
import kotlinx.cinterop.usePinned
import kotlinx.cinterop.useContents
import kotlinx.coroutines.channels.Channel
import org.jetbrains.skia.BackendRenderTarget
import org.jetbrains.skia.DirectContext
import org.jetbrains.skiko.FrameDispatcher
import org.jetbrains.skiko.SkikoDispatchers
import org.jetbrains.skiko.SkiaLayer
import org.jetbrains.skiko.context.ContextHandler
import org.jetbrains.skiko.context.MacOsMetalContextHandler
import platform.AppKit.NSWindowDidChangeOcclusionStateNotification
import platform.AppKit.NSWindowOcclusionStateVisible
import platform.CoreGraphics.CGColorCreate
import platform.CoreGraphics.CGColorSpaceCreateDeviceRGB
import platform.CoreGraphics.CGContextRef
Expand All @@ -26,6 +29,11 @@ import platform.QuartzCore.kCALayerHeightSizable
import platform.QuartzCore.kCALayerWidthSizable
import kotlin.system.getTimeNanos
import platform.CoreGraphics.CGSizeMake
import platform.Foundation.NSNotification
import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSOperationQueue
import platform.darwin.NSObjectProtocol
import kotlin.concurrent.Volatile

/**
* Metal [Redrawer] implementation for MacOs.
Expand All @@ -43,9 +51,24 @@ internal class MacOsMetalRedrawer(
private val queue = device.newCommandQueue() ?: throw IllegalStateException("Couldn't create Metal command queue")
private var currentDrawable: CAMetalDrawableProtocol? = null
private val metalLayer = MetalLayer()
private val occlusionObserver: NSObjectProtocol
private val windowOcclusionStateChannel = Channel<Boolean>(Channel.CONFLATED)
@Volatile private var isWindowOccluded = false

init {
metalLayer.init(skiaLayer, contextHandler, device)

val window = skiaLayer.nsView.window!!
occlusionObserver = NSNotificationCenter.defaultCenter.addObserverForName(
name = NSWindowDidChangeOcclusionStateNotification,
`object` = window,
queue = NSOperationQueue.mainQueue,
usingBlock = { notification: NSNotification? ->
val isOccluded = window.occlusionState and NSWindowOcclusionStateVisible == 0uL
isWindowOccluded = isOccluded
windowOcclusionStateChannel.trySend(isOccluded)
}
)
}

private val frameDispatcher = FrameDispatcher(SkikoDispatchers.Main) {
Expand All @@ -72,6 +95,7 @@ internal class MacOsMetalRedrawer(
override fun dispose() {
if (!isDisposed) {
metalLayer.dispose()
NSNotificationCenter.defaultCenter.removeObserver(occlusionObserver)
isDisposed = true
}
}
Expand Down Expand Up @@ -115,17 +139,28 @@ internal class MacOsMetalRedrawer(
*/
override fun redrawImmediately() {
check(!isDisposed) { "MetalRedrawer is disposed" }
draw()
autoreleasepool {
if (!isDisposed) {
skiaLayer.update(getTimeNanos())
contextHandler.draw()
}
}
}

private fun draw() {
// TODO: maybe make flush async as in JVM version.
private suspend fun draw() {
autoreleasepool {
if (!isDisposed) {
skiaLayer.update(getTimeNanos())
contextHandler.draw()
}
}

// When window is not visible - it doesn't make sense to redraw fast to avoid battery drain.
if (isWindowOccluded) {
// If the window becomes non-occluded, stop waiting immediately
@Suppress("ControlFlowWithEmptyBody")
while (windowOcclusionStateChannel.receive()) { }
}
}

fun finishFrame() {
Expand Down