blob: 566d55353f7dc07942674bc1d0fdbefb62ecd504 [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
32LayerTracing::LayerTracing(SurfaceFlinger& flinger) : mFlinger(flinger) {
33 mBuffer = std::make_unique<RingBuffer<LayersTraceFileProto, LayersTraceProto>>();
34}
35
36LayerTracing::~LayerTracing() = default;
37
38bool 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
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080048bool LayerTracing::disable(std::string filename) {
Vishnu Nair00b90132021-11-05 14:03:40 -070049 std::scoped_lock lock(mTraceLock);
50 if (!mEnabled) {
51 return false;
52 }
53 mEnabled = false;
54 LayersTraceFileProto fileProto = createTraceFileProto();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080055 mBuffer->writeToFile(fileProto, filename);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080056 mBuffer->reset();
Vishnu Nair00b90132021-11-05 14:03:40 -070057 return true;
58}
59
60bool LayerTracing::isEnabled() const {
61 std::scoped_lock lock(mTraceLock);
62 return mEnabled;
63}
64
65status_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
74void LayerTracing::setTraceFlags(uint32_t flags) {
75 std::scoped_lock lock(mTraceLock);
76 mFlags = flags;
77}
78
79void LayerTracing::setBufferSize(size_t bufferSizeInBytes) {
80 std::scoped_lock lock(mTraceLock);
81 mBufferSizeInBytes = bufferSizeInBytes;
82}
83
84bool LayerTracing::flagIsSet(uint32_t flags) const {
85 return (mFlags & flags) == flags;
86}
87
88LayersTraceFileProto 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);
Kean Mariottic44fdaf2022-07-29 14:20:39 +000092 auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) -
93 systemTime(SYSTEM_TIME_MONOTONIC));
94 fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs);
Vishnu Nair00b90132021-11-05 14:03:40 -070095 return fileProto;
96}
97
98void LayerTracing::dump(std::string& result) const {
99 std::scoped_lock lock(mTraceLock);
100 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
101 mBuffer->dump(result);
102}
103
Pablo Gamitob8775792022-06-10 15:02:42 +0000104void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700105 std::scoped_lock lock(mTraceLock);
106 if (!mEnabled) {
107 return;
108 }
109
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800110 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
111 return;
112 }
113
Vishnu Nair00b90132021-11-05 14:03:40 -0700114 ATRACE_CALL();
115 LayersTraceProto entry;
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800116 entry.set_elapsed_realtime_nanos(time);
117 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
Vishnu Nair00b90132021-11-05 14:03:40 -0700118 entry.set_where(where);
119 LayersProto layers(mFlinger.dumpDrawingStateProto(mFlags));
120
121 if (flagIsSet(LayerTracing::TRACE_EXTRA)) {
122 mFlinger.dumpOffscreenLayersProto(layers);
123 }
124 entry.mutable_layers()->Swap(&layers);
125
126 if (flagIsSet(LayerTracing::TRACE_HWC)) {
127 std::string hwcDump;
128 mFlinger.dumpHwc(hwcDump);
129 entry.set_hwc_blob(hwcDump);
130 }
131 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
132 entry.set_excludes_composition_state(true);
133 }
134 mFlinger.dumpDisplayProto(entry);
Pablo Gamitob8775792022-06-10 15:02:42 +0000135 entry.set_vsync_id(vsyncId);
Greg Kaiserddefa7b2022-06-13 06:19:26 -0700136 mBuffer->emplace(std::move(entry));
Vishnu Nair00b90132021-11-05 14:03:40 -0700137}
138
139} // namespace android