• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.quickstep.util;
18 
19 import static com.android.launcher3.tracing.LauncherTraceFileProto.MagicNumber.MAGIC_NUMBER_H_VALUE;
20 import static com.android.launcher3.tracing.LauncherTraceFileProto.MagicNumber.MAGIC_NUMBER_L_VALUE;
21 
22 import android.content.Context;
23 import android.os.SystemClock;
24 
25 import android.os.Trace;
26 import com.android.launcher3.tracing.LauncherTraceProto;
27 import com.android.launcher3.tracing.LauncherTraceEntryProto;
28 import com.android.launcher3.tracing.LauncherTraceFileProto;
29 import com.android.launcher3.util.MainThreadInitializedObject;
30 import com.android.systemui.shared.tracing.FrameProtoTracer;
31 import com.android.systemui.shared.tracing.FrameProtoTracer.ProtoTraceParams;
32 import com.android.systemui.shared.tracing.ProtoTraceable;
33 import com.google.protobuf.MessageLite;
34 
35 import java.io.File;
36 import java.util.ArrayList;
37 import java.util.Queue;
38 
39 
40 /**
41  * Controller for coordinating winscope proto tracing.
42  */
43 public class ProtoTracer implements ProtoTraceParams<MessageLite.Builder,
44         LauncherTraceFileProto.Builder, LauncherTraceEntryProto.Builder,
45                 LauncherTraceProto.Builder> {
46 
47     public static final MainThreadInitializedObject<ProtoTracer> INSTANCE =
48             new MainThreadInitializedObject<>(ProtoTracer::new);
49 
50     private static final String TAG = "ProtoTracer";
51     private static final long MAGIC_NUMBER_VALUE =
52             ((long) MAGIC_NUMBER_H_VALUE << 32) | MAGIC_NUMBER_L_VALUE;
53 
54     private final Context mContext;
55     private final FrameProtoTracer<MessageLite.Builder, LauncherTraceFileProto.Builder,
56         LauncherTraceEntryProto.Builder, LauncherTraceProto.Builder> mProtoTracer;
57 
ProtoTracer(Context context)58     public ProtoTracer(Context context) {
59         mContext = context;
60         mProtoTracer = new FrameProtoTracer<>(this);
61     }
62 
63     @Override
getTraceFile()64     public File getTraceFile() {
65         return new File(mContext.getFilesDir(), "launcher_trace.pb");
66     }
67 
68     @Override
getEncapsulatingTraceProto()69     public LauncherTraceFileProto.Builder getEncapsulatingTraceProto() {
70         return LauncherTraceFileProto.newBuilder();
71     }
72 
73     @Override
updateBufferProto( LauncherTraceEntryProto.Builder reuseObj, ArrayList<ProtoTraceable<LauncherTraceProto.Builder>> traceables)74     public LauncherTraceEntryProto.Builder updateBufferProto(
75             LauncherTraceEntryProto.Builder reuseObj,
76             ArrayList<ProtoTraceable<LauncherTraceProto.Builder>> traceables) {
77         Trace.beginSection("ProtoTracer.updateBufferProto");
78         LauncherTraceEntryProto.Builder proto = LauncherTraceEntryProto.newBuilder();
79         proto.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
80         LauncherTraceProto.Builder launcherProto = LauncherTraceProto.newBuilder();
81         for (ProtoTraceable t : traceables) {
82             t.writeToProto(launcherProto);
83         }
84         proto.setLauncher(launcherProto);
85         Trace.endSection();
86         return proto;
87     }
88 
89     @Override
serializeEncapsulatingProto(LauncherTraceFileProto.Builder encapsulatingProto, Queue<LauncherTraceEntryProto.Builder> buffer)90     public byte[] serializeEncapsulatingProto(LauncherTraceFileProto.Builder encapsulatingProto,
91             Queue<LauncherTraceEntryProto.Builder> buffer) {
92         Trace.beginSection("ProtoTracer.serializeEncapsulatingProto");
93         encapsulatingProto.setMagicNumber(MAGIC_NUMBER_VALUE);
94         for (LauncherTraceEntryProto.Builder entry : buffer) {
95             encapsulatingProto.addEntry(entry);
96         }
97         byte[] bytes = encapsulatingProto.build().toByteArray();
98         Trace.endSection();
99         return bytes;
100     }
101 
102     @Override
getProtoBytes(MessageLite.Builder proto)103     public byte[] getProtoBytes(MessageLite.Builder proto) {
104         return proto.build().toByteArray();
105     }
106 
107     @Override
getProtoSize(MessageLite.Builder proto)108     public int getProtoSize(MessageLite.Builder proto) {
109         return proto.build().getSerializedSize();
110     }
111 
start()112     public void start() {
113         mProtoTracer.start();
114     }
115 
stop()116     public void stop() {
117         mProtoTracer.stop();
118     }
119 
add(ProtoTraceable<LauncherTraceProto.Builder> traceable)120     public void add(ProtoTraceable<LauncherTraceProto.Builder> traceable) {
121         mProtoTracer.add(traceable);
122     }
123 
remove(ProtoTraceable<LauncherTraceProto.Builder> traceable)124     public void remove(ProtoTraceable<LauncherTraceProto.Builder> traceable) {
125         mProtoTracer.remove(traceable);
126     }
127 
scheduleFrameUpdate()128     public void scheduleFrameUpdate() {
129         mProtoTracer.scheduleFrameUpdate();
130     }
131 
update()132     public void update() {
133         mProtoTracer.update();
134     }
135 }
136