Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d7a73c60 authored by Stefan Andonian's avatar Stefan Andonian
Browse files

Revert "Revert "Used ElapsedRealtimeNanos instead of Choreographer times...""

This reverts commit eb297699.

Bug: 287509119
Test: Fix found. Now pushing the same code back up again.
Change-Id: I334f823ef1b78c5634331c447e65c4c81324f305
parent fa1e28d3
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.os.ParcelFileDescriptor
import android.os.Process
import android.provider.Settings
import android.util.Log
import android.view.Choreographer
import android.window.IDumpCallback
import androidx.annotation.AnyThread
import androidx.annotation.VisibleForTesting
@@ -40,8 +39,8 @@ private val TAG = SettingsAwareViewCapture::class.java.simpleName
 */
class SettingsAwareViewCapture
@VisibleForTesting
internal constructor(private val context: Context, choreographer: Choreographer, executor: Executor)
    : ViewCapture(DEFAULT_MEMORY_SIZE, DEFAULT_INIT_POOL_SIZE, choreographer, executor) {
internal constructor(private val context: Context, executor: Executor)
    : ViewCapture(DEFAULT_MEMORY_SIZE, DEFAULT_INIT_POOL_SIZE, executor) {
    /** Dumps all the active view captures to the wm trace directory via LauncherAppService */
    private val mDumpCallback: IDumpCallback.Stub = object : IDumpCallback.Stub() {
        override fun onDump(out: ParcelFileDescriptor) {
@@ -91,7 +90,7 @@ internal constructor(private val context: Context, choreographer: Choreographer,
        fun getInstance(context: Context): ViewCapture = when {
            INSTANCE != null -> INSTANCE!!
            Looper.myLooper() == Looper.getMainLooper() -> SettingsAwareViewCapture(
                    context.applicationContext, Choreographer.getInstance(),
                    context.applicationContext,
                    createAndStartNewLooperExecutor("SAViewCapture",
                    Process.THREAD_PRIORITY_FOREGROUND)).also { INSTANCE = it }
            else -> try {
+0 −2
Original line number Diff line number Diff line
package com.android.app.viewcapture

import android.os.Process
import android.view.Choreographer

open class SimpleViewCapture(threadName: String) : ViewCapture(DEFAULT_MEMORY_SIZE, DEFAULT_INIT_POOL_SIZE,
    MAIN_EXECUTOR.submit { Choreographer.getInstance() }.get(),
    createAndStartNewLooperExecutor(threadName, Process.THREAD_PRIORITY_FOREGROUND))
 No newline at end of file
+9 −9
Original line number Diff line number Diff line
@@ -24,10 +24,10 @@ import android.content.res.Resources;
import android.media.permission.SafeCloseable;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.os.Trace;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.Choreographer;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
@@ -54,6 +54,7 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Predicate;

@@ -83,16 +84,13 @@ public abstract class ViewCapture {
    private final List<WindowListener> mListeners = new ArrayList<>();

    protected final Executor mBgExecutor;
    private final Choreographer mChoreographer;

    // Pool used for capturing view tree on the UI thread.
    private ViewRef mPool = new ViewRef();
    private boolean mIsEnabled = true;

    protected ViewCapture(int memorySize, int initPoolSize, Choreographer choreographer,
            Executor bgExecutor) {
    protected ViewCapture(int memorySize, int initPoolSize, Executor bgExecutor) {
        mMemorySize = memorySize;
        mChoreographer = choreographer;
        mBgExecutor = bgExecutor;
        mBgExecutor.execute(() -> initPool(initPoolSize));
    }
@@ -189,6 +187,8 @@ public abstract class ViewCapture {
                .setPackage(context.getPackageName())
                .addAllWindowData(getWindowData(context, classList, l -> l.mIsActive).get())
                .addAllClassname(toStringList(classList))
                .setRealToElapsedTimeOffsetNanos(TimeUnit.MILLISECONDS
                        .toNanos(System.currentTimeMillis()) - SystemClock.elapsedRealtimeNanos())
                .build();
    }

@@ -289,7 +289,7 @@ public abstract class ViewCapture {
            ViewRef captured = mViewRef.next;
            if (captured != null) {
                captured.callback = mCaptureCallback;
                captured.choreographerTimeNanos = mChoreographer.getFrameTimeNanos();
                captured.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos();
                mBgExecutor.execute(captured);
            }
            mIsFirstFrame = false;
@@ -302,12 +302,12 @@ public abstract class ViewCapture {
         */
        @WorkerThread
        private void captureViewPropertiesBg(ViewRef viewRefStart) {
            long choreographerTimeNanos = viewRefStart.choreographerTimeNanos;
            long elapsedRealtimeNanos = viewRefStart.elapsedRealtimeNanos;
            mFrameIndexBg++;
            if (mFrameIndexBg >= mMemorySize) {
                mFrameIndexBg = 0;
            }
            mFrameTimesNanosBg[mFrameIndexBg] = choreographerTimeNanos;
            mFrameTimesNanosBg[mFrameIndexBg] = elapsedRealtimeNanos;

            ViewPropertyRef recycle = mNodesBg[mFrameIndexBg];

@@ -555,7 +555,7 @@ public abstract class ViewCapture {
        public ViewRef next;

        public Consumer<ViewRef> callback = null;
        public long choreographerTimeNanos = 0;
        public long elapsedRealtimeNanos = 0;

        public void transferTo(ViewPropertyRef out) {
            out.childCount = this.childCount;
+5 −1
Original line number Diff line number Diff line
@@ -34,6 +34,10 @@ message ExportedData {
  repeated WindowData windowData = 2;
  optional string package = 3;
  repeated string classname = 4;

  /* offset between real-time clock and elapsed time clock in nanoseconds.
     Calculated as: 1000000 * System.currentTimeMillis() - SystemClock.elapsedRealtimeNanos() */
  optional fixed64 real_to_elapsed_time_offset_nanos = 5;
}

message WindowData {
@@ -47,7 +51,7 @@ message MotionWindowData {
}

message FrameData {
  optional int64 timestamp = 1; // choreographer timestamp in nanoseconds
  optional int64 timestamp = 1; // unit is elapsed realtime nanos
  optional ViewNode node = 2;
}

+2 −4
Original line number Diff line number Diff line
@@ -50,8 +50,7 @@ class SettingsAwareViewCaptureTest {
        Settings.Global.putInt(context.contentResolver, VIEW_CAPTURE_ENABLED, 0)

        activityScenarioRule.scenario.onActivity { activity ->
            val viewCapture: ViewCapture =
                SettingsAwareViewCapture(context, Choreographer.getInstance(), MAIN_EXECUTOR)
            val viewCapture: ViewCapture = SettingsAwareViewCapture(context, MAIN_EXECUTOR)
            val rootView: View = activity.findViewById(android.R.id.content)

            val closeable: SafeCloseable = viewCapture.startCapture(rootView, "rootViewId")
@@ -70,8 +69,7 @@ class SettingsAwareViewCaptureTest {
        Settings.Global.putInt(context.contentResolver, VIEW_CAPTURE_ENABLED, 1)

        activityScenarioRule.scenario.onActivity { activity ->
            val viewCapture: ViewCapture =
                SettingsAwareViewCapture(context, Choreographer.getInstance(), MAIN_EXECUTOR)
            val viewCapture: ViewCapture = SettingsAwareViewCapture(context, MAIN_EXECUTOR)
            val rootView: View = activity.findViewById(android.R.id.content)

            val closeable: SafeCloseable = viewCapture.startCapture(rootView, "rootViewId")
Loading