blob: 4b9f7778edb64eeee0bd9f212f00dbfb9a659690 [file] [log] [blame]
Adrian Roos1e1a1282017-11-01 19:05:31 +01001/*
2 * Copyright 2017 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#pragma once
18
Alec Mouri5793c7d2020-03-10 19:55:50 -070019#include <android-base/thread_annotations.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010020#include <layerproto/LayerProtoHeader.h>
21#include <utils/Errors.h>
Nataniel Borges2b796da2019-02-15 13:32:18 -080022#include <utils/StrongPointer.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010023
Nataniel Borges2b796da2019-02-15 13:32:18 -080024#include <condition_variable>
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070025#include <memory>
Adrian Roos1e1a1282017-11-01 19:05:31 +010026#include <mutex>
Yichi Chen9c696ed2018-10-01 22:32:30 +080027#include <queue>
Nataniel Borges2b796da2019-02-15 13:32:18 -080028#include <thread>
Adrian Roos1e1a1282017-11-01 19:05:31 +010029
Alec Mouri5793c7d2020-03-10 19:55:50 -070030#include "DisplayDevice.h"
31
Adrian Roos1e1a1282017-11-01 19:05:31 +010032using namespace android::surfaceflinger;
33
34namespace android {
35
Nataniel Borges2b796da2019-02-15 13:32:18 -080036class SurfaceFlinger;
37
Yichi Chen9c696ed2018-10-01 22:32:30 +080038constexpr auto operator""_MB(unsigned long long const num) {
39 return num * 1024 * 1024;
40}
Adrian Roos1e1a1282017-11-01 19:05:31 +010041/*
42 * SurfaceTracing records layer states during surface flinging.
43 */
44class SurfaceTracing {
45public:
Dominik Laskowski9dab3432019-03-27 13:21:10 -070046 explicit SurfaceTracing(SurfaceFlinger& flinger);
Nataniel Borges2b796da2019-02-15 13:32:18 -080047 void enable();
48 bool disable();
49 status_t writeToFile();
Yichi Chen9c696ed2018-10-01 22:32:30 +080050 bool isEnabled() const;
Nataniel Borges2b796da2019-02-15 13:32:18 -080051 void notify(const char* where);
52
53 void setBufferSize(size_t bufferSizeInByte);
54 void writeToFileAsync();
Yiwei Zhang5434a782018-12-05 18:06:32 -080055 void dump(std::string& result) const;
Adrian Roos1e1a1282017-11-01 19:05:31 +010056
Vishnu Nair9245d3b2019-03-22 13:38:56 -070057 enum : uint32_t {
58 TRACE_CRITICAL = 1 << 0,
59 TRACE_INPUT = 1 << 1,
60 TRACE_EXTRA = 1 << 2,
Alec Mouri6b9e9912020-01-21 10:50:24 -080061 TRACE_HWC = 1 << 3,
Vishnu Nair9245d3b2019-03-22 13:38:56 -070062 TRACE_ALL = 0xffffffff
63 };
64 void setTraceFlags(uint32_t flags);
65
Adrian Roos1e1a1282017-11-01 19:05:31 +010066private:
Robert Delgado626670f2019-06-27 15:55:27 -070067 static constexpr auto kDefaultBufferCapInByte = 5_MB;
Yichi Chen9c696ed2018-10-01 22:32:30 +080068 static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb";
69
70 class LayersTraceBuffer { // ring buffer
71 public:
72 size_t size() const { return mSizeInBytes; }
73 size_t used() const { return mUsedInBytes; }
74 size_t frameCount() const { return mStorage.size(); }
75
Nataniel Borges2b796da2019-02-15 13:32:18 -080076 void setSize(size_t newSize) { mSizeInBytes = newSize; }
Yichi Chen9c696ed2018-10-01 22:32:30 +080077 void reset(size_t newSize);
78 void emplace(LayersTraceProto&& proto);
79 void flush(LayersTraceFileProto* fileProto);
80
81 private:
82 size_t mUsedInBytes = 0U;
83 size_t mSizeInBytes = 0U;
84 std::queue<LayersTraceProto> mStorage;
85 };
Adrian Roos1e1a1282017-11-01 19:05:31 +010086
Nataniel Borges2b796da2019-02-15 13:32:18 -080087 void mainLoop();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070088 void addFirstEntry();
89 LayersTraceProto traceWhenNotified();
Alec Mouri5793c7d2020-03-10 19:55:50 -070090 LayersTraceProto traceLayersLocked(const char* where,
91 const sp<const DisplayDevice>& displayDevice)
92 REQUIRES(mSfLock);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070093
94 // Returns true if trace is enabled.
95 bool addTraceToBuffer(LayersTraceProto& entry);
Nataniel Borges2b796da2019-02-15 13:32:18 -080096 void writeProtoFileLocked() REQUIRES(mTraceLock);
Adrian Roos1e1a1282017-11-01 19:05:31 +010097
Alec Mouri5793c7d2020-03-10 19:55:50 -070098 SurfaceFlinger& mFlinger;
Nataniel Borges2b796da2019-02-15 13:32:18 -080099 status_t mLastErr = NO_ERROR;
100 std::thread mThread;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700101 std::condition_variable mCanStartTrace;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800102
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700103 std::mutex& mSfLock;
104 uint32_t mTraceFlags GUARDED_BY(mSfLock) = TRACE_ALL;
105 const char* mWhere GUARDED_BY(mSfLock) = "";
106
107 mutable std::mutex mTraceLock;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800108 LayersTraceBuffer mBuffer GUARDED_BY(mTraceLock);
109 size_t mBufferSize GUARDED_BY(mTraceLock) = kDefaultBufferCapInByte;
110 bool mEnabled GUARDED_BY(mTraceLock) = false;
111 bool mWriteToFile GUARDED_BY(mTraceLock) = false;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100112};
113
114} // namespace android