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.nano.LauncherTraceFileProto.MagicNumber.MAGIC_NUMBER_H;
20 import static com.android.launcher3.tracing.nano.LauncherTraceFileProto.MagicNumber.MAGIC_NUMBER_L;
21 
22 import android.content.Context;
23 import android.os.SystemClock;
24 
25 import com.android.launcher3.tracing.nano.LauncherTraceProto;
26 import com.android.launcher3.tracing.nano.LauncherTraceEntryProto;
27 import com.android.launcher3.tracing.nano.LauncherTraceFileProto;
28 import com.android.launcher3.util.MainThreadInitializedObject;
29 import com.android.systemui.shared.tracing.FrameProtoTracer;
30 import com.android.systemui.shared.tracing.FrameProtoTracer.ProtoTraceParams;
31 import com.android.systemui.shared.tracing.ProtoTraceable;
32 import com.google.protobuf.nano.MessageNano;
33 
34 import java.io.File;
35 import java.io.FileDescriptor;
36 import java.io.PrintWriter;
37 import java.util.ArrayList;
38 import java.util.Queue;
39 
40 
41 /**
42  * Controller for coordinating winscope proto tracing.
43  */
44 public class ProtoTracer implements ProtoTraceParams<MessageNano,
45         LauncherTraceFileProto, LauncherTraceEntryProto, LauncherTraceProto> {
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 = ((long) MAGIC_NUMBER_H << 32) | MAGIC_NUMBER_L;
52 
53     private final Context mContext;
54     private final FrameProtoTracer<MessageNano,
55             LauncherTraceFileProto, LauncherTraceEntryProto, LauncherTraceProto> mProtoTracer;
56 
ProtoTracer(Context context)57     public ProtoTracer(Context context) {
58         mContext = context;
59         mProtoTracer = new FrameProtoTracer<>(this);
60     }
61 
62     @Override
getTraceFile()63     public File getTraceFile() {
64         return new File(mContext.getFilesDir(), "launcher_trace.pb");
65     }
66 
67     @Override
getEncapsulatingTraceProto()68     public LauncherTraceFileProto getEncapsulatingTraceProto() {
69         return new LauncherTraceFileProto();
70     }
71 
72     @Override
updateBufferProto(LauncherTraceEntryProto reuseObj, ArrayList<ProtoTraceable<LauncherTraceProto>> traceables)73     public LauncherTraceEntryProto updateBufferProto(LauncherTraceEntryProto reuseObj,
74             ArrayList<ProtoTraceable<LauncherTraceProto>> traceables) {
75         LauncherTraceEntryProto proto = reuseObj != null
76                 ? reuseObj
77                 : new LauncherTraceEntryProto();
78         proto.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos();
79         proto.launcher = proto.launcher != null ? proto.launcher : new LauncherTraceProto();
80         for (ProtoTraceable t : traceables) {
81             t.writeToProto(proto.launcher);
82         }
83         return proto;
84     }
85 
86     @Override
serializeEncapsulatingProto(LauncherTraceFileProto encapsulatingProto, Queue<LauncherTraceEntryProto> buffer)87     public byte[] serializeEncapsulatingProto(LauncherTraceFileProto encapsulatingProto,
88             Queue<LauncherTraceEntryProto> buffer) {
89         encapsulatingProto.magicNumber = MAGIC_NUMBER_VALUE;
90         encapsulatingProto.entry = buffer.toArray(new LauncherTraceEntryProto[0]);
91         return MessageNano.toByteArray(encapsulatingProto);
92     }
93 
94     @Override
getProtoBytes(MessageNano proto)95     public byte[] getProtoBytes(MessageNano proto) {
96         return MessageNano.toByteArray(proto);
97     }
98 
99     @Override
getProtoSize(MessageNano proto)100     public int getProtoSize(MessageNano proto) {
101         return proto.getCachedSize();
102     }
103 
start()104     public void start() {
105         mProtoTracer.start();
106     }
107 
stop()108     public void stop() {
109         mProtoTracer.stop();
110     }
111 
add(ProtoTraceable<LauncherTraceProto> traceable)112     public void add(ProtoTraceable<LauncherTraceProto> traceable) {
113         mProtoTracer.add(traceable);
114     }
115 
remove(ProtoTraceable<LauncherTraceProto> traceable)116     public void remove(ProtoTraceable<LauncherTraceProto> traceable) {
117         mProtoTracer.remove(traceable);
118     }
119 
scheduleFrameUpdate()120     public void scheduleFrameUpdate() {
121         mProtoTracer.scheduleFrameUpdate();
122     }
123 
update()124     public void update() {
125         mProtoTracer.update();
126     }
127 }
128