blob: 2918f7cd377709808c3da62da320e5098ec5fce7 [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
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
30namespace android {
31
Vishnu Nair81750622023-03-08 15:02:06 -080032LayerTracing::LayerTracing()
33 : mBuffer(std::make_unique<RingBuffer<LayersTraceFileProto, LayersTraceProto>>()) {}
Vishnu Nair00b90132021-11-05 14:03:40 -070034
35LayerTracing::~LayerTracing() = default;
36
37bool LayerTracing::enable() {
38 std::scoped_lock lock(mTraceLock);
39 if (mEnabled) {
40 return false;
41 }
42 mBuffer->setSize(mBufferSizeInBytes);
43 mEnabled = true;
44 return true;
45}
46
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080047bool LayerTracing::disable(std::string filename) {
Vishnu Nair00b90132021-11-05 14:03:40 -070048 std::scoped_lock lock(mTraceLock);
49 if (!mEnabled) {
50 return false;
51 }
52 mEnabled = false;
53 LayersTraceFileProto fileProto = createTraceFileProto();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080054 mBuffer->writeToFile(fileProto, filename);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080055 mBuffer->reset();
Vishnu Nair00b90132021-11-05 14:03:40 -070056 return true;
57}
58
59bool LayerTracing::isEnabled() const {
60 std::scoped_lock lock(mTraceLock);
61 return mEnabled;
62}
63
64status_t LayerTracing::writeToFile() {
65 std::scoped_lock lock(mTraceLock);
66 if (!mEnabled) {
67 return STATUS_OK;
68 }
69 LayersTraceFileProto fileProto = createTraceFileProto();
70 return mBuffer->writeToFile(fileProto, FILE_NAME);
71}
72
73void LayerTracing::setTraceFlags(uint32_t flags) {
74 std::scoped_lock lock(mTraceLock);
75 mFlags = flags;
76}
77
78void LayerTracing::setBufferSize(size_t bufferSizeInBytes) {
79 std::scoped_lock lock(mTraceLock);
80 mBufferSizeInBytes = bufferSizeInBytes;
81}
82
83bool LayerTracing::flagIsSet(uint32_t flags) const {
84 return (mFlags & flags) == flags;
85}
Vishnu Nair81750622023-03-08 15:02:06 -080086uint32_t LayerTracing::getFlags() const {
87 return mFlags;
88}
Vishnu Nair00b90132021-11-05 14:03:40 -070089
Vishnu Nair81750622023-03-08 15:02:06 -080090LayersTraceFileProto LayerTracing::createTraceFileProto() {
Vishnu Nair00b90132021-11-05 14:03:40 -070091 LayersTraceFileProto fileProto;
92 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
93 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
Kean Mariottic44fdaf2022-07-29 14:20:39 +000094 auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) -
95 systemTime(SYSTEM_TIME_MONOTONIC));
96 fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs);
Vishnu Nair00b90132021-11-05 14:03:40 -070097 return fileProto;
98}
99
100void LayerTracing::dump(std::string& result) const {
101 std::scoped_lock lock(mTraceLock);
102 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
103 mBuffer->dump(result);
104}
105
Vishnu Nair81750622023-03-08 15:02:06 -0800106void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId,
107 LayersProto* layers, std::string hwcDump,
108 google::protobuf::RepeatedPtrField<DisplayProto>* displays) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700109 std::scoped_lock lock(mTraceLock);
110 if (!mEnabled) {
111 return;
112 }
113
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800114 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
115 return;
116 }
117
Vishnu Nair00b90132021-11-05 14:03:40 -0700118 ATRACE_CALL();
119 LayersTraceProto entry;
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800120 entry.set_elapsed_realtime_nanos(time);
121 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
Vishnu Nair00b90132021-11-05 14:03:40 -0700122 entry.set_where(where);
Vishnu Nair81750622023-03-08 15:02:06 -0800123 entry.mutable_layers()->Swap(layers);
Vishnu Nair00b90132021-11-05 14:03:40 -0700124
125 if (flagIsSet(LayerTracing::TRACE_HWC)) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700126 entry.set_hwc_blob(hwcDump);
127 }
128 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
129 entry.set_excludes_composition_state(true);
130 }
Vishnu Nair81750622023-03-08 15:02:06 -0800131 entry.mutable_displays()->Swap(displays);
Pablo Gamitob8775792022-06-10 15:02:42 +0000132 entry.set_vsync_id(vsyncId);
Greg Kaiserddefa7b2022-06-13 06:19:26 -0700133 mBuffer->emplace(std::move(entry));
Vishnu Nair00b90132021-11-05 14:03:40 -0700134}
135
136} // namespace android