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

Reuse cached MTLCommandQueue across multiple MetalRedrawer's #1127

Merged
merged 2 commits into from
Feb 21, 2024
Merged
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 @@ -37,6 +37,8 @@ import org.jetbrains.skia.Rect
import platform.Foundation.NSLock
import platform.Foundation.NSRunLoopCommonModes
import platform.Foundation.NSTimeInterval
import platform.Metal.MTLCommandQueueProtocol
import platform.Metal.MTLDeviceProtocol
import platform.UIKit.UIApplication
import platform.UIKit.UIApplicationState

Expand Down Expand Up @@ -197,8 +199,7 @@ internal class MetalRedrawer(
@Suppress("USELESS_CAST")
private val device = metalLayer.device as platform.Metal.MTLDeviceProtocol?
?: throw IllegalStateException("CAMetalLayer.device can not be null")
private val queue = device.newCommandQueue()
?: throw IllegalStateException("Couldn't create Metal command queue")
private val queue = getCachedCommandQueue(device)
private val context = DirectContext.makeMetal(device.objcPtr(), queue.objcPtr())
private var lastRenderTimestamp: NSTimeInterval = CACurrentMediaTime()
private val pictureRecorder = PictureRecorder()
Expand Down Expand Up @@ -300,6 +301,8 @@ internal class MetalRedrawer(
fun dispose() {
check(caDisplayLink != null) { "MetalRedrawer.dispose() was called more than once" }

releaseCachedCommandQueue(queue)

applicationStateListener.dispose()

caDisplayLink?.invalidate()
Expand Down Expand Up @@ -461,6 +464,46 @@ internal class MetalRedrawer(
companion object {
private val renderingDispatchQueue =
dispatch_queue_create("RenderingDispatchQueue", null)

private class CachedCommandQueue(
Copy link
Member

Choose a reason for hiding this comment

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

I'd move it out from companion object and expose methods instead of public properties

val queue: MTLCommandQueueProtocol,
var refCount: Int = 1
Copy link
Member

Choose a reason for hiding this comment

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

atomic?

Copy link
Author

Choose a reason for hiding this comment

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

Assumed to be used from the main thread.

)

/**
* Cached command queue record. Assumed to be associated with default MTLDevice.
*/
private var cachedCommandQueue: CachedCommandQueue? = null

/**
* Get an existing command queue associated with the device or create a new one and cache it.
* Assumed to be run on the main thread.
*/
private fun getCachedCommandQueue(device: MTLDeviceProtocol): MTLCommandQueueProtocol {
val cached = cachedCommandQueue
if (cached != null) {
cached.refCount++
return cached.queue
} else {
val queue = device.newCommandQueue() ?: throw IllegalStateException("MTLDevice.newCommandQueue() returned null")
cachedCommandQueue = CachedCommandQueue(queue)
return queue
}
}

/**
* Release the cached command queue. Release the cache if refCount reaches 0.
* Assumed to be run on the main thread.
*/
private fun releaseCachedCommandQueue(queue: MTLCommandQueueProtocol) {
val cached = cachedCommandQueue ?: return
if (cached.queue == queue) {
cached.refCount--
if (cached.refCount == 0) {
cachedCommandQueue = null
}
}
}
}
}

Expand Down
Loading