blob: ecdeabe52802dd57f1aef7b9be2828cd5d699572 [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"
30#include "RingBuffer.h"
31
32namespace android {
33
Vishnu Nair81750622023-03-08 15:02:06 -080034LayerTracing::LayerTracing()
35 : mBuffer(std::make_unique<RingBuffer<LayersTraceFileProto, LayersTraceProto>>()) {}
Vishnu Nair00b90132021-11-05 14:03:40 -070036
37LayerTracing::~LayerTracing() = default;
38
39bool 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 Naircb565332023-03-14 21:10:55 -070049bool LayerTracing::disable(std::string filename, bool writeToFile) {
Vishnu Nair00b90132021-11-05 14:03:40 -070050 std::scoped_lock lock(mTraceLock);
51 if (!mEnabled) {
52 return false;
53 }
54 mEnabled = false;
Vishnu Naircb565332023-03-14 21:10:55 -070055 if (writeToFile) {
56 LayersTraceFileProto fileProto = createTraceFileProto();
57 mBuffer->writeToFile(fileProto, filename);
58 }
Vishnu Nair0cc69e12021-11-18 09:05:49 -080059 mBuffer->reset();
Vishnu Nair00b90132021-11-05 14:03:40 -070060 return true;
61}
62
Vishnu Naircb565332023-03-14 21:10:55 -070063void 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 Nair00b90132021-11-05 14:03:40 -070070bool LayerTracing::isEnabled() const {
71 std::scoped_lock lock(mTraceLock);
72 return mEnabled;
73}
74
Vishnu Naircb565332023-03-14 21:10:55 -070075status_t LayerTracing::writeToFile(std::string filename) {
Vishnu Nair00b90132021-11-05 14:03:40 -070076 std::scoped_lock lock(mTraceLock);
77 if (!mEnabled) {
78 return STATUS_OK;
79 }
80 LayersTraceFileProto fileProto = createTraceFileProto();
Vishnu Naircb565332023-03-14 21:10:55 -070081 return mBuffer->writeToFile(fileProto, filename);
Vishnu Nair00b90132021-11-05 14:03:40 -070082}
83
84void LayerTracing::setTraceFlags(uint32_t flags) {
85 std::scoped_lock lock(mTraceLock);
86 mFlags = flags;
87}
88
89void LayerTracing::setBufferSize(size_t bufferSizeInBytes) {
90 std::scoped_lock lock(mTraceLock);
91 mBufferSizeInBytes = bufferSizeInBytes;
92}
93
94bool LayerTracing::flagIsSet(uint32_t flags) const {
95 return (mFlags & flags) == flags;
96}
Vishnu Nair81750622023-03-08 15:02:06 -080097uint32_t LayerTracing::getFlags() const {
98 return mFlags;
99}
Vishnu Nair00b90132021-11-05 14:03:40 -0700100
Vishnu Nair81750622023-03-08 15:02:06 -0800101LayersTraceFileProto LayerTracing::createTraceFileProto() {
Vishnu Nair00b90132021-11-05 14:03:40 -0700102 LayersTraceFileProto fileProto;
103 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
104 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
Kean Mariottic44fdaf2022-07-29 14:20:39 +0000105 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 Nair00b90132021-11-05 14:03:40 -0700108 return fileProto;
109}
110
111void 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 Nair81750622023-03-08 15:02:06 -0800117void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId,
118 LayersProto* layers, std::string hwcDump,
119 google::protobuf::RepeatedPtrField<DisplayProto>* displays) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700120 std::scoped_lock lock(mTraceLock);
121 if (!mEnabled) {
122 return;
123 }
124
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800125 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
126 return;
127 }
128
Vishnu Nair00b90132021-11-05 14:03:40 -0700129 ATRACE_CALL();
130 LayersTraceProto entry;
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800131 entry.set_elapsed_realtime_nanos(time);
132 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
Vishnu Nair00b90132021-11-05 14:03:40 -0700133 entry.set_where(where);
Vishnu Nair81750622023-03-08 15:02:06 -0800134 entry.mutable_layers()->Swap(layers);
Vishnu Nair00b90132021-11-05 14:03:40 -0700135
136 if (flagIsSet(LayerTracing::TRACE_HWC)) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700137 entry.set_hwc_blob(hwcDump);
138 }
139 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
140 entry.set_excludes_composition_state(true);
141 }
Vishnu Nair81750622023-03-08 15:02:06 -0800142 entry.mutable_displays()->Swap(displays);
Pablo Gamitob8775792022-06-10 15:02:42 +0000143 entry.set_vsync_id(vsyncId);
Greg Kaiserddefa7b2022-06-13 06:19:26 -0700144 mBuffer->emplace(std::move(entry));
Vishnu Nair00b90132021-11-05 14:03:40 -0700145}
146
147} // namespace android