diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 400fcd14b3..0cc45d46b6 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -207,6 +207,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { binding = ActivityHomeBinding.inflate(layoutInflater) setContentView(binding.root) + ProfilerMarkers.addListenerForOnGlobalLayout(components.core.engine, this, binding.root) // Must be after we set the content view if (isVisuallyComplete) { diff --git a/app/src/main/java/org/mozilla/fenix/perf/ProfilerMarkers.kt b/app/src/main/java/org/mozilla/fenix/perf/ProfilerMarkers.kt index 783992303f..35f4196e3d 100644 --- a/app/src/main/java/org/mozilla/fenix/perf/ProfilerMarkers.kt +++ b/app/src/main/java/org/mozilla/fenix/perf/ProfilerMarkers.kt @@ -4,9 +4,12 @@ package org.mozilla.fenix.perf +import android.app.Activity import android.view.View +import android.view.ViewTreeObserver import androidx.core.view.doOnPreDraw import mozilla.components.concept.base.profiler.Profiler +import mozilla.components.concept.engine.Engine /** * A container for functions for when adding a profiler marker is less readable @@ -14,9 +17,27 @@ import mozilla.components.concept.base.profiler.Profiler */ object ProfilerMarkers { + fun addListenerForOnGlobalLayout(engine: Engine, activity: Activity, rootView: View) { + // We define the listener in a non-anonymous class to avoid memory leaks with the activity. + val listener = MarkerGlobalLayoutListener(engine, activity::class.simpleName ?: "null") + rootView.viewTreeObserver.addOnGlobalLayoutListener(listener) + } + fun homeActivityOnStart(rootContainer: View, profiler: Profiler?) { rootContainer.doOnPreDraw { profiler?.addMarker("onPreDraw", "expected first frame via HomeActivity.onStart") } } } + +/** + * A global layout listener that adds a profiler marker on global layout. + */ +class MarkerGlobalLayoutListener( + private val engine: Engine, + private val activityName: String, +) : ViewTreeObserver.OnGlobalLayoutListener { + override fun onGlobalLayout() { + engine.profiler?.addMarker("onGlobalLayout", activityName) + } +}