blob: b92d50b423b9b5e3359bc235356b0ec29547c800 [file] [log] [blame]
Vishnu Nair00b90132021-11-05 14:03:40 -07001/*
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 Naircb565332023-03-14 21:10:55 -070021#include <filesystem>
22
Vishnu Nair00b90132021-11-05 14:03:40 -070023#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"
John Reck2a3d29d2023-08-17 17:45:01 -040030#include "TransactionRingBuffer.h"
Vishnu Nair00b90132021-11-05 14:03:40 -070031
32namespace android {
33
Vishnu Nair81750622023-03-08 15:02:06 -080034LayerTracing::LayerTracing()
John Reck2a3d29d2023-08-17 17:45:01 -040035 : mBuffer(std::make_unique<TransactionRingBuffer<LayersTraceFileProto, LayersTraceProto>>()) {
36}
Vishnu Nair00b90132021-11-05 14:03:40 -070037
38LayerTracing::~LayerTracing() = default;
39
40bool LayerTracing::enable() {
41 std::scoped_lock lock(mTraceLock);
42 if (mEnabled) {
43 return false;
44 }
45 mBuffer->setSize(mBufferSizeInBytes);
46 mEnabled = true;
47 return true;
48}
49
Vishnu Naircb565332023-03-14 21:10:55 -070050bool LayerTracing::disable(std::string filename, bool writeToFile) {
Vishnu Nair00b90132021-11-05 14:03:40 -070051 std::scoped_lock lock(mTraceLock);
52 if (!mEnabled) {
53 return false;
54 }
55 mEnabled = false;
Vishnu Naircb565332023-03-14 21:10:55 -070056 if (writeToFile) {
57 LayersTraceFileProto fileProto = createTraceFileProto();
58 mBuffer->writeToFile(fileProto, filename);
59 }
Vishnu Nair0cc69e12021-11-18 09:05:49 -080060 mBuffer->reset();
Vishnu Nair00b90132021-11-05 14:03:40 -070061 return true;
62}
63
Vishnu Naircb565332023-03-14 21:10:55 -070064void LayerTracing::appendToStream(std::ofstream& out) {
65 std::scoped_lock lock(mTraceLock);
66 LayersTraceFileProto fileProto = createTraceFileProto();
67 mBuffer->appendToStream(fileProto, out);
68 mBuffer->reset();
69}
70
Vishnu Nair00b90132021-11-05 14:03:40 -070071bool LayerTracing::isEnabled() const {
72 std::scoped_lock lock(mTraceLock);
73 return mEnabled;
74}
75
Vishnu Naircb565332023-03-14 21:10:55 -070076status_t LayerTracing::writeToFile(std::string filename) {
Vishnu Nair00b90132021-11-05 14:03:40 -070077 std::scoped_lock lock(mTraceLock);
78 if (!mEnabled) {
79 return STATUS_OK;
80 }
81 LayersTraceFileProto fileProto = createTraceFileProto();
Vishnu Naircb565332023-03-14 21:10:55 -070082 return mBuffer->writeToFile(fileProto, filename);
Vishnu Nair00b90132021-11-05 14:03:40 -070083}
84
85void LayerTracing::setTraceFlags(uint32_t flags) {
86 std::scoped_lock lock(mTraceLock);
87 mFlags = flags;
88}
89
90void LayerTracing::setBufferSize(size_t bufferSizeInBytes) {
91 std::scoped_lock lock(mTraceLock);
92 mBufferSizeInBytes = bufferSizeInBytes;
93}
94
95bool LayerTracing::flagIsSet(uint32_t flags) const {
96 return (mFlags & flags) == flags;
97}
Vishnu Nair81750622023-03-08 15:02:06 -080098uint32_t LayerTracing::getFlags() const {
99 return mFlags;
100}
Vishnu Nair00b90132021-11-05 14:03:40 -0700101
Vishnu Nair81750622023-03-08 15:02:06 -0800102LayersTraceFileProto LayerTracing::createTraceFileProto() {
Vishnu Nair00b90132021-11-05 14:03:40 -0700103 LayersTraceFileProto fileProto;
104 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
105 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
Kean Mariottic44fdaf2022-07-29 14:20:39 +0000106 auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) -
107 systemTime(SYSTEM_TIME_MONOTONIC));
108 fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs);
Vishnu Nair00b90132021-11-05 14:03:40 -0700109 return fileProto;
110}
111
112void LayerTracing::dump(std::string& result) const {
113 std::scoped_lock lock(mTraceLock);
114 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
115 mBuffer->dump(result);
116}
117
Vishnu Nair81750622023-03-08 15:02:06 -0800118void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId,
119 LayersProto* layers, std::string hwcDump,
120 google::protobuf::RepeatedPtrField<DisplayProto>* displays) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700121 std::scoped_lock lock(mTraceLock);
122 if (!mEnabled) {
123 return;
124 }
125
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800126 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
127 return;
128 }
129
Vishnu Nair00b90132021-11-05 14:03:40 -0700130 ATRACE_CALL();
131 LayersTraceProto entry;
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800132 entry.set_elapsed_realtime_nanos(time);
133 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
Vishnu Nair00b90132021-11-05 14:03:40 -0700134 entry.set_where(where);
Vishnu Nair81750622023-03-08 15:02:06 -0800135 entry.mutable_layers()->Swap(layers);
Vishnu Nair00b90132021-11-05 14:03:40 -0700136
137 if (flagIsSet(LayerTracing::TRACE_HWC)) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700138 entry.set_hwc_blob(hwcDump);
139 }
140 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
141 entry.set_excludes_composition_state(true);
142 }
Vishnu Nair81750622023-03-08 15:02:06 -0800143 entry.mutable_displays()->Swap(displays);
Pablo Gamitob8775792022-06-10 15:02:42 +0000144 entry.set_vsync_id(vsyncId);
Greg Kaiserddefa7b2022-06-13 06:19:26 -0700145 mBuffer->emplace(std::move(entry));
Vishnu Nair00b90132021-11-05 14:03:40 -0700146}
147
148} // namespace android