Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1 | /* |
| 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 Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 22 | #include <memory> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 23 | #include <mutex> |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 24 | #include <queue> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 25 | |
| 26 | using namespace android::surfaceflinger; |
| 27 | |
| 28 | namespace android { |
| 29 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 30 | constexpr auto operator""_MB(unsigned long long const num) { |
| 31 | return num * 1024 * 1024; |
| 32 | } |
| 33 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 34 | /* |
| 35 | * SurfaceTracing records layer states during surface flinging. |
| 36 | */ |
| 37 | class SurfaceTracing { |
| 38 | public: |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 39 | void enable() { enable(kDefaultBufferCapInByte); } |
| 40 | void enable(size_t bufferSizeInByte); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 41 | status_t disable(); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 42 | void traceLayers(const char* where, LayersProto); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 43 | |
| 44 | bool isEnabled() const; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame^] | 45 | void dump(std::string& result) const; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 46 | |
| 47 | private: |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 48 | 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 Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 66 | |
| 67 | status_t writeProtoFileLocked(); |
| 68 | |
| 69 | bool mEnabled = false; |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 70 | mutable std::mutex mTraceMutex; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 71 | LayersTraceBuffer mBuffer; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace android |