Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LayerTracing" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame^] | 21 | #include <filesystem> |
| 22 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 23 | #include <SurfaceFlinger.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <log/log.h> |
| 26 | #include <utils/SystemClock.h> |
| 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | #include "LayerTracing.h" |
| 30 | #include "RingBuffer.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 34 | LayerTracing::LayerTracing() |
| 35 | : mBuffer(std::make_unique<RingBuffer<LayersTraceFileProto, LayersTraceProto>>()) {} |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 36 | |
| 37 | LayerTracing::~LayerTracing() = default; |
| 38 | |
| 39 | bool LayerTracing::enable() { |
| 40 | std::scoped_lock lock(mTraceLock); |
| 41 | if (mEnabled) { |
| 42 | return false; |
| 43 | } |
| 44 | mBuffer->setSize(mBufferSizeInBytes); |
| 45 | mEnabled = true; |
| 46 | return true; |
| 47 | } |
| 48 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame^] | 49 | bool LayerTracing::disable(std::string filename, bool writeToFile) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 50 | std::scoped_lock lock(mTraceLock); |
| 51 | if (!mEnabled) { |
| 52 | return false; |
| 53 | } |
| 54 | mEnabled = false; |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame^] | 55 | if (writeToFile) { |
| 56 | LayersTraceFileProto fileProto = createTraceFileProto(); |
| 57 | mBuffer->writeToFile(fileProto, filename); |
| 58 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 59 | mBuffer->reset(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame^] | 63 | void LayerTracing::appendToStream(std::ofstream& out) { |
| 64 | std::scoped_lock lock(mTraceLock); |
| 65 | LayersTraceFileProto fileProto = createTraceFileProto(); |
| 66 | mBuffer->appendToStream(fileProto, out); |
| 67 | mBuffer->reset(); |
| 68 | } |
| 69 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 70 | bool LayerTracing::isEnabled() const { |
| 71 | std::scoped_lock lock(mTraceLock); |
| 72 | return mEnabled; |
| 73 | } |
| 74 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame^] | 75 | status_t LayerTracing::writeToFile(std::string filename) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 76 | std::scoped_lock lock(mTraceLock); |
| 77 | if (!mEnabled) { |
| 78 | return STATUS_OK; |
| 79 | } |
| 80 | LayersTraceFileProto fileProto = createTraceFileProto(); |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame^] | 81 | return mBuffer->writeToFile(fileProto, filename); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void LayerTracing::setTraceFlags(uint32_t flags) { |
| 85 | std::scoped_lock lock(mTraceLock); |
| 86 | mFlags = flags; |
| 87 | } |
| 88 | |
| 89 | void LayerTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 90 | std::scoped_lock lock(mTraceLock); |
| 91 | mBufferSizeInBytes = bufferSizeInBytes; |
| 92 | } |
| 93 | |
| 94 | bool LayerTracing::flagIsSet(uint32_t flags) const { |
| 95 | return (mFlags & flags) == flags; |
| 96 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 97 | uint32_t LayerTracing::getFlags() const { |
| 98 | return mFlags; |
| 99 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 100 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 101 | LayersTraceFileProto LayerTracing::createTraceFileProto() { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 102 | LayersTraceFileProto fileProto; |
| 103 | fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 104 | LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
Kean Mariotti | c44fdaf | 2022-07-29 14:20:39 +0000 | [diff] [blame] | 105 | auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) - |
| 106 | systemTime(SYSTEM_TIME_MONOTONIC)); |
| 107 | fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 108 | return fileProto; |
| 109 | } |
| 110 | |
| 111 | void LayerTracing::dump(std::string& result) const { |
| 112 | std::scoped_lock lock(mTraceLock); |
| 113 | base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled"); |
| 114 | mBuffer->dump(result); |
| 115 | } |
| 116 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 117 | void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId, |
| 118 | LayersProto* layers, std::string hwcDump, |
| 119 | google::protobuf::RepeatedPtrField<DisplayProto>* displays) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 120 | std::scoped_lock lock(mTraceLock); |
| 121 | if (!mEnabled) { |
| 122 | return; |
| 123 | } |
| 124 | |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 125 | if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) { |
| 126 | return; |
| 127 | } |
| 128 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 129 | ATRACE_CALL(); |
| 130 | LayersTraceProto entry; |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 131 | entry.set_elapsed_realtime_nanos(time); |
| 132 | const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched"; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 133 | entry.set_where(where); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 134 | entry.mutable_layers()->Swap(layers); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 135 | |
| 136 | if (flagIsSet(LayerTracing::TRACE_HWC)) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 137 | entry.set_hwc_blob(hwcDump); |
| 138 | } |
| 139 | if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) { |
| 140 | entry.set_excludes_composition_state(true); |
| 141 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 142 | entry.mutable_displays()->Swap(displays); |
Pablo Gamito | b877579 | 2022-06-10 15:02:42 +0000 | [diff] [blame] | 143 | entry.set_vsync_id(vsyncId); |
Greg Kaiser | ddefa7b | 2022-06-13 06:19:26 -0700 | [diff] [blame] | 144 | mBuffer->emplace(std::move(entry)); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace android |