blob: fd919af9998a7b6fc424fbd47cb6bbc34f30f55e [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>
21
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070022#include <memory>
Adrian Roos1e1a1282017-11-01 19:05:31 +010023#include <mutex>
Yichi Chen9c696ed2018-10-01 22:32:30 +080024#include <queue>
Adrian Roos1e1a1282017-11-01 19:05:31 +010025
26using namespace android::surfaceflinger;
27
28namespace android {
29
Yichi Chen9c696ed2018-10-01 22:32:30 +080030constexpr auto operator""_MB(unsigned long long const num) {
31 return num * 1024 * 1024;
32}
33
Adrian Roos1e1a1282017-11-01 19:05:31 +010034/*
35 * SurfaceTracing records layer states during surface flinging.
36 */
37class SurfaceTracing {
38public:
Yichi Chen9c696ed2018-10-01 22:32:30 +080039 void enable() { enable(kDefaultBufferCapInByte); }
40 void enable(size_t bufferSizeInByte);
Adrian Roos1e1a1282017-11-01 19:05:31 +010041 status_t disable();
Adrian Roos1e1a1282017-11-01 19:05:31 +010042 void traceLayers(const char* where, LayersProto);
Yichi Chen9c696ed2018-10-01 22:32:30 +080043
44 bool isEnabled() const;
Yiwei Zhang5434a782018-12-05 18:06:32 -080045 void dump(std::string& result) const;
Adrian Roos1e1a1282017-11-01 19:05:31 +010046
47private:
Yichi Chen9c696ed2018-10-01 22:32:30 +080048 static constexpr auto kDefaultBufferCapInByte = 100_MB;
49 static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb";
50
51 class LayersTraceBuffer { // ring buffer
52 public:
53 size_t size() const { return mSizeInBytes; }
54 size_t used() const { return mUsedInBytes; }
55 size_t frameCount() const { return mStorage.size(); }
56
57 void reset(size_t newSize);
58 void emplace(LayersTraceProto&& proto);
59 void flush(LayersTraceFileProto* fileProto);
60
61 private:
62 size_t mUsedInBytes = 0U;
63 size_t mSizeInBytes = 0U;
64 std::queue<LayersTraceProto> mStorage;
65 };
Adrian Roos1e1a1282017-11-01 19:05:31 +010066
67 status_t writeProtoFileLocked();
68
69 bool mEnabled = false;
Yichi Chenadc69612018-09-15 14:51:18 +080070 mutable std::mutex mTraceMutex;
Yichi Chen9c696ed2018-10-01 22:32:30 +080071 LayersTraceBuffer mBuffer;
Adrian Roos1e1a1282017-11-01 19:05:31 +010072};
73
74} // namespace android