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 | |
| 21 | #include <SurfaceFlinger.h> |
| 22 | #include <android-base/stringprintf.h> |
| 23 | #include <log/log.h> |
| 24 | #include <utils/SystemClock.h> |
| 25 | #include <utils/Trace.h> |
| 26 | |
| 27 | #include "LayerTracing.h" |
| 28 | #include "RingBuffer.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | LayerTracing::LayerTracing(SurfaceFlinger& flinger) : mFlinger(flinger) { |
| 33 | mBuffer = std::make_unique<RingBuffer<LayersTraceFileProto, LayersTraceProto>>(); |
| 34 | } |
| 35 | |
| 36 | LayerTracing::~LayerTracing() = default; |
| 37 | |
| 38 | bool LayerTracing::enable() { |
| 39 | std::scoped_lock lock(mTraceLock); |
| 40 | if (mEnabled) { |
| 41 | return false; |
| 42 | } |
| 43 | mBuffer->setSize(mBufferSizeInBytes); |
| 44 | mEnabled = true; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool LayerTracing::disable() { |
| 49 | std::scoped_lock lock(mTraceLock); |
| 50 | if (!mEnabled) { |
| 51 | return false; |
| 52 | } |
| 53 | mEnabled = false; |
| 54 | LayersTraceFileProto fileProto = createTraceFileProto(); |
| 55 | mBuffer->writeToFile(fileProto, FILE_NAME); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 56 | mBuffer->reset(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool LayerTracing::isEnabled() const { |
| 61 | std::scoped_lock lock(mTraceLock); |
| 62 | return mEnabled; |
| 63 | } |
| 64 | |
| 65 | status_t LayerTracing::writeToFile() { |
| 66 | std::scoped_lock lock(mTraceLock); |
| 67 | if (!mEnabled) { |
| 68 | return STATUS_OK; |
| 69 | } |
| 70 | LayersTraceFileProto fileProto = createTraceFileProto(); |
| 71 | return mBuffer->writeToFile(fileProto, FILE_NAME); |
| 72 | } |
| 73 | |
| 74 | void LayerTracing::setTraceFlags(uint32_t flags) { |
| 75 | std::scoped_lock lock(mTraceLock); |
| 76 | mFlags = flags; |
| 77 | } |
| 78 | |
| 79 | void LayerTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 80 | std::scoped_lock lock(mTraceLock); |
| 81 | mBufferSizeInBytes = bufferSizeInBytes; |
| 82 | } |
| 83 | |
| 84 | bool LayerTracing::flagIsSet(uint32_t flags) const { |
| 85 | return (mFlags & flags) == flags; |
| 86 | } |
| 87 | |
| 88 | LayersTraceFileProto LayerTracing::createTraceFileProto() const { |
| 89 | LayersTraceFileProto fileProto; |
| 90 | fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 91 | LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
| 92 | return fileProto; |
| 93 | } |
| 94 | |
| 95 | void LayerTracing::dump(std::string& result) const { |
| 96 | std::scoped_lock lock(mTraceLock); |
| 97 | base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled"); |
| 98 | mBuffer->dump(result); |
| 99 | } |
| 100 | |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame^] | 101 | void LayerTracing::notify(bool visibleRegionDirty, int64_t time) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 102 | std::scoped_lock lock(mTraceLock); |
| 103 | if (!mEnabled) { |
| 104 | return; |
| 105 | } |
| 106 | |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame^] | 107 | if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) { |
| 108 | return; |
| 109 | } |
| 110 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 111 | ATRACE_CALL(); |
| 112 | LayersTraceProto entry; |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame^] | 113 | entry.set_elapsed_realtime_nanos(time); |
| 114 | const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched"; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 115 | entry.set_where(where); |
| 116 | LayersProto layers(mFlinger.dumpDrawingStateProto(mFlags)); |
| 117 | |
| 118 | if (flagIsSet(LayerTracing::TRACE_EXTRA)) { |
| 119 | mFlinger.dumpOffscreenLayersProto(layers); |
| 120 | } |
| 121 | entry.mutable_layers()->Swap(&layers); |
| 122 | |
| 123 | if (flagIsSet(LayerTracing::TRACE_HWC)) { |
| 124 | std::string hwcDump; |
| 125 | mFlinger.dumpHwc(hwcDump); |
| 126 | entry.set_hwc_blob(hwcDump); |
| 127 | } |
| 128 | if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) { |
| 129 | entry.set_excludes_composition_state(true); |
| 130 | } |
| 131 | mFlinger.dumpDisplayProto(entry); |
| 132 | mBuffer->emplace(std::move(entry)); |
| 133 | } |
| 134 | |
| 135 | } // namespace android |