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

Hook up CADisplayLink for a frame pulse on iOS #990

Merged
merged 1 commit into from
Apr 27, 2023
Merged
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
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.treehouse

import kotlinx.cinterop.ExportObjCClass
import kotlinx.cinterop.ObjCAction
import platform.Foundation.NSRunLoop
import platform.Foundation.NSSelectorFromString
import platform.QuartzCore.CADisplayLink
import platform.darwin.NSObject

@ExportObjCClass
internal class DisplayLinkTarget(
private val callback: DisplayLinkTarget.() -> Unit,
) : NSObject() {
private val displayLink: CADisplayLink = CADisplayLink.displayLinkWithTarget(
target = this,
selector = NSSelectorFromString(this::onFrame.name),
)

/** This function must be public to be a valid candidate for [NSSelectorString]. */
@ObjCAction
public fun onFrame() {
callback(this)
}

fun subscribe() {
displayLink.addToRunLoop(
NSRunLoop.currentRunLoop,
NSRunLoop.currentRunLoop.currentMode,
)
}

fun unsubscribe() {
displayLink.removeFromRunLoop(
NSRunLoop.currentRunLoop,
NSRunLoop.currentRunLoop.currentMode,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.treehouse

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

public class IosDisplayLinkClock : FrameClock {
private lateinit var scope: CoroutineScope
private lateinit var dispatchers: TreehouseDispatchers
private lateinit var displayLinkTarget: DisplayLinkTarget

/** Non-null if we're expecting a call to [tickClock]. */
private var appLifecycle: AppLifecycle? = null

override fun start(scope: CoroutineScope, dispatchers: TreehouseDispatchers) {
this.scope = scope
this.dispatchers = dispatchers
this.displayLinkTarget = DisplayLinkTarget {
unsubscribe()
scope.launch(dispatchers.zipline) {
appLifecycle?.sendFrame(0L)
appLifecycle = null
}
}
}

override fun requestFrame(appLifecycle: AppLifecycle) {
this.appLifecycle = appLifecycle
this.displayLinkTarget.subscribe()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public fun TreehouseAppFactory(
dispatchers = IosTreehouseDispatchers(),
eventListener = eventListener,
httpClient = httpClient,
frameClock = FixedDelayFrameClock(),
frameClock = IosDisplayLinkClock(),
manifestVerifier = manifestVerifier,
embeddedDir = embeddedDir,
embeddedFileSystem = embeddedFileSystem,
Expand Down