-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve tracking on choreographer frame ends
## Behavior changes Whenever possible (i.e. any API != 28), we'll now detect the end of a frame by spying on main thread messages and running code right when the main thread message is done running and before a new main thread message starts running, by leveraging `Looper.getMainLooper().setMessageLogging { }`. We're also detecting whether we're in a frame or not by looking at the name of the runnable, rather than by creating a stacktrace. ## Non backward compatible API changes - Renamed `MainThreadMessageSpy.startTracing()` and `MainThreadMessageSpy.stopTracing()` to `MainThreadMessageSpy.addTracer()` and `MainThreadMessageSpy.removeTracer()`. ## New APIs - `MainThreadMessageSpy.startSpyingMainThreadDispatching()` and `MainThreadMessageSpy.stopSpyingMainThreadDispatching()`, `MainThreadMessageSpy.currentMessageAsString`, `MainThreadMessageSpy.enabled` and `MainThreadMessageSpy.onCurrentMessageFinished()` - `MainThreadMessageScopedLazy` (`prop by mainThreadMessageScopedLazy {}`) property for main thread scoped lazy properties that get cleared at the end of each main thread message. - `Choreographers.postOnFrameRendered()`, `Choreographers.postOnWindowFrameRendered()` and `Choreographers.isInChoreographerFrame` - `InputEventTrigger.rendered`, `InputEventTrigger.renderedUptime` and `InputEventTrigger.onInputEventFrameRendered`
- Loading branch information
Showing
21 changed files
with
490 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
papa-main-trace/src/main/java/papa/MainThreadMessageScopedLazy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package papa | ||
|
||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KProperty | ||
|
||
class MainThreadMessageScopedLazy<T>(val provider: () -> T) : ReadOnlyProperty<Any?, T> { | ||
|
||
private var valueOrNull: T? = null | ||
|
||
override fun getValue( | ||
thisRef: Any?, | ||
property: KProperty<*> | ||
): T { | ||
check(MainThreadMessageSpy.enabled) { | ||
"Can't use a MainThreadMessageScopedLazy when MainThreadMessageSpy is not enabled." | ||
} | ||
val value = provider() | ||
valueOrNull = value | ||
MainThreadMessageSpy.onCurrentMessageFinished { | ||
valueOrNull = null | ||
} | ||
return value | ||
} | ||
} | ||
|
||
fun <T> mainThreadMessageScopedLazy(provider: () -> T) = MainThreadMessageScopedLazy(provider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
papa/src/androidTest/java/papa/test/MainThreadMessageSpyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package papa.test | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import papa.MainThreadMessageSpy | ||
import papa.internal.mainHandler | ||
import papa.internal.postAtFrontOfQueueAsync | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit.SECONDS | ||
|
||
// TODO Skip these tests on version 28 / have other tests that check it's disabled | ||
class MainThreadMessageSpyTest { | ||
|
||
@Test fun current_message_set_to_runnable_to_string() { | ||
val runnableRan = CountDownLatch(1) | ||
var runnableCurrentMessageAsString: String? = null | ||
|
||
class MyRunnable : Runnable { | ||
override fun run() { | ||
runnableCurrentMessageAsString = MainThreadMessageSpy.currentMessageAsString | ||
runnableRan.countDown() | ||
} | ||
|
||
override fun toString(): String { | ||
return "Baguette" | ||
} | ||
} | ||
|
||
mainHandler.post(MyRunnable()) | ||
check(runnableRan.await(5, SECONDS)) | ||
|
||
assertThat(runnableCurrentMessageAsString).doesNotContain("MyRunnable") | ||
assertThat(runnableCurrentMessageAsString).contains("Baguette") | ||
} | ||
|
||
@Test fun onCurrentMessageFinished_runs_at_end_of_current_post() { | ||
val runnablesRan = CountDownLatch(3) | ||
val runOrder = mutableListOf<String>() | ||
|
||
mainHandler.post { | ||
mainHandler.postAtFrontOfQueueAsync { | ||
runOrder += "second post" | ||
runnablesRan.countDown() | ||
} | ||
MainThreadMessageSpy.onCurrentMessageFinished { | ||
runOrder += "first post finished" | ||
runnablesRan.countDown() | ||
} | ||
runOrder += "first post" | ||
runnablesRan.countDown() | ||
} | ||
check(runnablesRan.await(5, SECONDS)) | ||
|
||
assertThat(runOrder) | ||
.containsExactly("first post", "first post finished", "second post") | ||
.inOrder() | ||
} | ||
} |
Oops, something went wrong.