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> |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 21 | #include <utils/StrongPointer.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 22 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
| 24 | #include <condition_variable> |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 25 | #include <memory> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 26 | #include <mutex> |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 27 | #include <queue> |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 28 | #include <thread> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 29 | |
| 30 | using namespace android::surfaceflinger; |
| 31 | |
| 32 | namespace android { |
| 33 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 34 | class SurfaceFlinger; |
| 35 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 36 | constexpr auto operator""_MB(unsigned long long const num) { |
| 37 | return num * 1024 * 1024; |
| 38 | } |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 39 | /* |
| 40 | * SurfaceTracing records layer states during surface flinging. |
| 41 | */ |
| 42 | class SurfaceTracing { |
| 43 | public: |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 44 | SurfaceTracing(SurfaceFlinger& flinger) : mFlinger(flinger) {} |
| 45 | void enable(); |
| 46 | bool disable(); |
| 47 | status_t writeToFile(); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 48 | bool isEnabled() const; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 49 | void notify(const char* where); |
| 50 | |
| 51 | void setBufferSize(size_t bufferSizeInByte); |
| 52 | void writeToFileAsync(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 53 | void dump(std::string& result) const; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 54 | |
| 55 | private: |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 56 | static constexpr auto kDefaultBufferCapInByte = 100_MB; |
| 57 | static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb"; |
| 58 | |
| 59 | class LayersTraceBuffer { // ring buffer |
| 60 | public: |
| 61 | size_t size() const { return mSizeInBytes; } |
| 62 | size_t used() const { return mUsedInBytes; } |
| 63 | size_t frameCount() const { return mStorage.size(); } |
| 64 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 65 | void setSize(size_t newSize) { mSizeInBytes = newSize; } |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 66 | void reset(size_t newSize); |
| 67 | void emplace(LayersTraceProto&& proto); |
| 68 | void flush(LayersTraceFileProto* fileProto); |
| 69 | |
| 70 | private: |
| 71 | size_t mUsedInBytes = 0U; |
| 72 | size_t mSizeInBytes = 0U; |
| 73 | std::queue<LayersTraceProto> mStorage; |
| 74 | }; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 75 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 76 | void mainLoop(); |
| 77 | void traceLayers(const char* where); |
| 78 | LayersTraceProto traceLayersLocked(const char* where); |
| 79 | void writeProtoFileLocked() REQUIRES(mTraceLock); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 80 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 81 | const SurfaceFlinger& mFlinger; |
| 82 | |
Vishnu Nair | b015948 | 2019-03-18 12:48:46 -0700 | [diff] [blame] | 83 | const char* mWhere = ""; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 84 | status_t mLastErr = NO_ERROR; |
| 85 | std::thread mThread; |
| 86 | std::condition_variable mConditionalVariable; |
| 87 | mutable std::mutex mTraceLock; |
| 88 | |
| 89 | LayersTraceBuffer mBuffer GUARDED_BY(mTraceLock); |
| 90 | size_t mBufferSize GUARDED_BY(mTraceLock) = kDefaultBufferCapInByte; |
| 91 | bool mEnabled GUARDED_BY(mTraceLock) = false; |
| 92 | bool mWriteToFile GUARDED_BY(mTraceLock) = false; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace android |