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
Diwas Sharma69758342023-09-01 00:40:19 +000021#include <filesystem>
Kean Mariotti22cbec52023-04-20 12:06:29 +000022
Diwas Sharma69758342023-09-01 00:40:19 +000023#include <SurfaceFlinger.h>
24#include <android-base/stringprintf.h>
Kean Mariotti22cbec52023-04-20 12:06:29 +000025#include <log/log.h>
Diwas Sharma69758342023-09-01 00:40:19 +000026#include <utils/SystemClock.h>
Kean Mariotti22cbec52023-04-20 12:06:29 +000027#include <utils/Trace.h>
Vishnu Nair00b90132021-11-05 14:03:40 -070028
Diwas Sharma69758342023-09-01 00:40:19 +000029#include "LayerTracing.h"
30#include "TransactionRingBuffer.h"
31
Vishnu Nair00b90132021-11-05 14:03:40 -070032namespace android {
33
Diwas Sharma69758342023-09-01 00:40:19 +000034LayerTracing::LayerTracing()
35 : mBuffer(std::make_unique<TransactionRingBuffer<LayersTraceFileProto, LayersTraceProto>>()) {
John Reck2a3d29d2023-08-17 17:45:01 -040036}
Vishnu Nair00b90132021-11-05 14:03:40 -070037
Diwas Sharma69758342023-09-01 00:40:19 +000038LayerTracing::~LayerTracing() = default;
Vishnu Nair00b90132021-11-05 14:03:40 -070039
Diwas Sharma69758342023-09-01 00:40:19 +000040bool LayerTracing::enable() {
41 std::scoped_lock lock(mTraceLock);
42 if (mEnabled) {
43 return false;
Vishnu Nair00b90132021-11-05 14:03:40 -070044 }
Diwas Sharma69758342023-09-01 00:40:19 +000045 mBuffer->setSize(mBufferSizeInBytes);
46 mEnabled = true;
47 return true;
Vishnu Nair00b90132021-11-05 14:03:40 -070048}
49
Diwas Sharma69758342023-09-01 00:40:19 +000050bool LayerTracing::disable(std::string filename, bool writeToFile) {
51 std::scoped_lock lock(mTraceLock);
52 if (!mEnabled) {
53 return false;
Vishnu Nair00b90132021-11-05 14:03:40 -070054 }
Diwas Sharma69758342023-09-01 00:40:19 +000055 mEnabled = false;
56 if (writeToFile) {
57 LayersTraceFileProto fileProto = createTraceFileProto();
58 mBuffer->writeToFile(fileProto, filename);
Vishnu Naircb565332023-03-14 21:10:55 -070059 }
Diwas Sharma69758342023-09-01 00:40:19 +000060 mBuffer->reset();
61 return true;
Vishnu Nair00b90132021-11-05 14:03:40 -070062}
63
Diwas Sharma69758342023-09-01 00:40:19 +000064void 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
71bool LayerTracing::isEnabled() const {
72 std::scoped_lock lock(mTraceLock);
73 return mEnabled;
74}
75
76status_t LayerTracing::writeToFile(std::string filename) {
77 std::scoped_lock lock(mTraceLock);
78 if (!mEnabled) {
79 return STATUS_OK;
Vishnu Nair00b90132021-11-05 14:03:40 -070080 }
Diwas Sharma69758342023-09-01 00:40:19 +000081 LayersTraceFileProto fileProto = createTraceFileProto();
82 return mBuffer->writeToFile(fileProto, filename);
Vishnu Nair00b90132021-11-05 14:03:40 -070083}
84
Diwas Sharma69758342023-09-01 00:40:19 +000085void LayerTracing::setTraceFlags(uint32_t flags) {
86 std::scoped_lock lock(mTraceLock);
87 mFlags = flags;
Vishnu Nair00b90132021-11-05 14:03:40 -070088}
89
Diwas Sharma69758342023-09-01 00:40:19 +000090void LayerTracing::setBufferSize(size_t bufferSizeInBytes) {
91 std::scoped_lock lock(mTraceLock);
92 mBufferSizeInBytes = bufferSizeInBytes;
Vishnu Nair00b90132021-11-05 14:03:40 -070093}
94
Diwas Sharma69758342023-09-01 00:40:19 +000095bool LayerTracing::flagIsSet(uint32_t flags) const {
96 return (mFlags & flags) == flags;
97}
98uint32_t LayerTracing::getFlags() const {
99 return mFlags;
Vishnu Nair81750622023-03-08 15:02:06 -0800100}
Vishnu Nair00b90132021-11-05 14:03:40 -0700101
Diwas Sharma69758342023-09-01 00:40:19 +0000102LayersTraceFileProto LayerTracing::createTraceFileProto() {
103 LayersTraceFileProto fileProto;
104 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
105 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
106 auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) -
107 systemTime(SYSTEM_TIME_MONOTONIC));
Kean Mariottic44fdaf2022-07-29 14:20:39 +0000108 fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs);
Vishnu Nair00b90132021-11-05 14:03:40 -0700109 return fileProto;
110}
111
Diwas Sharma69758342023-09-01 00:40:19 +0000112void 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);
Vishnu Nair00b90132021-11-05 14:03:40 -0700116}
117
Diwas Sharma69758342023-09-01 00:40:19 +0000118void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId,
119 LayersProto* layers, std::string hwcDump,
120 google::protobuf::RepeatedPtrField<DisplayProto>* displays) {
121 std::scoped_lock lock(mTraceLock);
122 if (!mEnabled) {
123 return;
Vishnu Nair00b90132021-11-05 14:03:40 -0700124 }
125
Diwas Sharma69758342023-09-01 00:40:19 +0000126 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
127 return;
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800128 }
129
Diwas Sharma69758342023-09-01 00:40:19 +0000130 ATRACE_CALL();
131 LayersTraceProto entry;
132 entry.set_elapsed_realtime_nanos(time);
133 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
134 entry.set_where(where);
135 entry.mutable_layers()->Swap(layers);
136
137 if (flagIsSet(LayerTracing::TRACE_HWC)) {
138 entry.set_hwc_blob(hwcDump);
139 }
140 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
141 entry.set_excludes_composition_state(true);
142 }
143 entry.mutable_displays()->Swap(displays);
144 entry.set_vsync_id(vsyncId);
145 mBuffer->emplace(std::move(entry));
Vishnu Nair00b90132021-11-05 14:03:40 -0700146}
147
148} // namespace android