Merge "Removing the dependency on LooperExecutor in ViewCapture" into tm-qpr-dev am: 4206239318

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/20237551

Change-Id: I8130c06a6008981980ba4a7bfe4da8b5f348cbde
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
TreeHugger Robot authored and Automerger Merge Worker committed 2022-10-20 17:05:14 +00:00
commit dc24cc87fc
1 file changed
+34 -19
@@ -23,10 +23,9 @@ import static java.util.stream.Collectors.toList;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message;
import android.os.Process; import android.os.Process;
import android.os.SystemClock;
import android.os.Trace; import android.os.Trace;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
@@ -57,7 +56,9 @@ import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Future; import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.function.Consumer;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
/** /**
@@ -83,7 +84,7 @@ public class ViewCapture {
private final List<WindowListener> mListeners = new ArrayList<>(); private final List<WindowListener> mListeners = new ArrayList<>();
private final Context mContext; private final Context mContext;
private final LooperExecutor mExecutor; private final Executor mExecutor;
// Pool used for capturing view tree on the UI thread. // Pool used for capturing view tree on the UI thread.
private ViewRef mPool = new ViewRef(); private ViewRef mPool = new ViewRef();
@@ -156,8 +157,13 @@ public class ViewCapture {
ViewIdProvider idProvider = new ViewIdProvider(mContext.getResources()); ViewIdProvider idProvider = new ViewIdProvider(mContext.getResources());
// Collect all the tasks first so that all the tasks are posted on the executor // Collect all the tasks first so that all the tasks are posted on the executor
List<Pair<String, Future<ExportedData>>> tasks = mListeners.stream() List<Pair<String, FutureTask<ExportedData>>> tasks = mListeners.stream()
.map(l -> Pair.create(l.name, mExecutor.submit(() -> l.dumpToProto(idProvider)))) .map(l -> {
FutureTask<ExportedData> task =
new FutureTask<ExportedData>(() -> l.dumpToProto(idProvider));
mExecutor.execute(task);
return Pair.create(l.name, task);
})
.collect(toList()); .collect(toList());
tasks.forEach(pair -> { tasks.forEach(pair -> {
@@ -187,7 +193,6 @@ public class ViewCapture {
private final View mRoot; private final View mRoot;
public final String name; public final String name;
private final Handler mHandler;
private final ViewRef mViewRef = new ViewRef(); private final ViewRef mViewRef = new ViewRef();
private int mFrameIndexBg = -1; private int mFrameIndexBg = -1;
@@ -196,20 +201,23 @@ public class ViewCapture {
private final ViewPropertyRef[] mNodesBg = new ViewPropertyRef[MEMORY_SIZE]; private final ViewPropertyRef[] mNodesBg = new ViewPropertyRef[MEMORY_SIZE];
private boolean mDestroyed = false; private boolean mDestroyed = false;
private final Consumer<ViewRef> mCaptureCallback = this::captureViewPropertiesBg;
WindowListener(View view, String name) { WindowListener(View view, String name) {
mRoot = view; mRoot = view;
this.name = name; this.name = name;
mHandler = new Handler(mExecutor.getLooper(), this::captureViewPropertiesBg);
} }
@Override @Override
public void onDraw() { public void onDraw() {
Trace.beginSection("view_capture"); Trace.beginSection("view_capture");
captureViewTree(mRoot, mViewRef); captureViewTree(mRoot, mViewRef);
Message m = Message.obtain(mHandler); ViewRef captured = mViewRef.next;
m.obj = mViewRef.next; if (captured != null) {
mHandler.sendMessage(m); captured.callback = mCaptureCallback;
captured.creationTime = SystemClock.uptimeMillis();
mExecutor.execute(captured);
}
mIsFirstFrame = false; mIsFirstFrame = false;
Trace.endSection(); Trace.endSection();
} }
@@ -219,12 +227,8 @@ public class ViewCapture {
* back to the pool * back to the pool
*/ */
@WorkerThread @WorkerThread
private boolean captureViewPropertiesBg(Message msg) { private void captureViewPropertiesBg(ViewRef viewRefStart) {
ViewRef viewRefStart = (ViewRef) msg.obj; long time = viewRefStart.creationTime;
long time = msg.getWhen();
if (viewRefStart == null) {
return false;
}
mFrameIndexBg++; mFrameIndexBg++;
if (mFrameIndexBg >= MEMORY_SIZE) { if (mFrameIndexBg >= MEMORY_SIZE) {
mFrameIndexBg = 0; mFrameIndexBg = 0;
@@ -292,7 +296,6 @@ public class ViewCapture {
viewRefEnd = viewRefEnd.next; viewRefEnd = viewRefEnd.next;
} }
mNodesBg[mFrameIndexBg] = resultStart; mNodesBg[mFrameIndexBg] = resultStart;
return true;
} }
private ViewPropertyRef findInLastFrame(int hashCode) { private ViewPropertyRef findInLastFrame(int hashCode) {
@@ -464,11 +467,14 @@ public class ViewCapture {
} }
} }
private static class ViewRef { private static class ViewRef implements Runnable {
public View view; public View view;
public int childCount = 0; public int childCount = 0;
public ViewRef next; public ViewRef next;
public Consumer<ViewRef> callback = null;
public long creationTime = 0;
public void transferTo(ViewPropertyRef out) { public void transferTo(ViewPropertyRef out) {
out.childCount = this.childCount; out.childCount = this.childCount;
@@ -495,6 +501,15 @@ public class ViewCapture {
out.visibility = view.getVisibility(); out.visibility = view.getVisibility();
out.willNotDraw = view.willNotDraw(); out.willNotDraw = view.willNotDraw();
} }
@Override
public void run() {
Consumer<ViewRef> oldCallback = callback;
callback = null;
if (oldCallback != null) {
oldCallback.accept(this);
}
}
} }
private static final class ViewIdProvider { private static final class ViewIdProvider {