blob: ec01be7170b03a509a4133d66c1304fd56c495e9 [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
19#include <layerproto/LayerProtoHeader.h>
20#include <utils/Errors.h>
Yichi Chenadc69612018-09-15 14:51:18 +080021#include <utils/String8.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010022
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070023#include <memory>
Adrian Roos1e1a1282017-11-01 19:05:31 +010024#include <mutex>
Yichi Chen9c696ed2018-10-01 22:32:30 +080025#include <queue>
Adrian Roos1e1a1282017-11-01 19:05:31 +010026
27using namespace android::surfaceflinger;
28
29namespace android {
30
Yichi Chen9c696ed2018-10-01 22:32:30 +080031constexpr auto operator""_MB(unsigned long long const num) {
32 return num * 1024 * 1024;
33}
34
Adrian Roos1e1a1282017-11-01 19:05:31 +010035/*
36 * SurfaceTracing records layer states during surface flinging.
37 */
38class SurfaceTracing {
39public:
Yichi Chen9c696ed2018-10-01 22:32:30 +080040 void enable() { enable(kDefaultBufferCapInByte); }
41 void enable(size_t bufferSizeInByte);
Adrian Roos1e1a1282017-11-01 19:05:31 +010042 status_t disable();
Adrian Roos1e1a1282017-11-01 19:05:31 +010043 void traceLayers(const char* where, LayersProto);
Yichi Chen9c696ed2018-10-01 22:32:30 +080044
45 bool isEnabled() const;
Yichi Chenadc69612018-09-15 14:51:18 +080046 void dump(String8& result) const;
Adrian Roos1e1a1282017-11-01 19:05:31 +010047
48private:
Yichi Chen9c696ed2018-10-01 22:32:30 +080049 static constexpr auto kDefaultBufferCapInByte = 100_MB;
50 static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb";
51
52 class LayersTraceBuffer { // ring buffer
53 public:
54 size_t size() const { return mSizeInBytes; }
55 size_t used() const { return mUsedInBytes; }
56 size_t frameCount() const { return mStorage.size(); }
57
58 void reset(size_t newSize);
59 void emplace(LayersTraceProto&& proto);
60 void flush(LayersTraceFileProto* fileProto);
61
62 private:
63 size_t mUsedInBytes = 0U;
64 size_t mSizeInBytes = 0U;
65 std::queue<LayersTraceProto> mStorage;
66 };
Adrian Roos1e1a1282017-11-01 19:05:31 +010067
68 status_t writeProtoFileLocked();
69
70 bool mEnabled = false;
Yichi Chenadc69612018-09-15 14:51:18 +080071 mutable std::mutex mTraceMutex;
Yichi Chen9c696ed2018-10-01 22:32:30 +080072 LayersTraceBuffer mBuffer;
Adrian Roos1e1a1282017-11-01 19:05:31 +010073};
74
75} // namespace android