From 336d681451b6ada5105054ddbf94af70705c2e62 Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Wed, 15 Sep 2021 08:29:45 -0700 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/21309: add profiler markers on global layout. --- .../java/org/mozilla/fenix/HomeActivity.kt | 1 + .../org/mozilla/fenix/perf/ProfilerMarkers.kt | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) 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) + } +}