blob: 006efdfd613f6db13efe242ef51ea8b4c5d5c13a [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
48bool LayerTracing::disable() {
49 std::scoped_lock lock(mTraceLock);
50 if (!mEnabled) {
51 return false;
52 }
53 mEnabled = false;
54 LayersTraceFileProto fileProto = createTraceFileProto();
55 mBuffer->writeToFile(fileProto, FILE_NAME);
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);
92 return fileProto;
93}
94
95void LayerTracing::dump(std::string& result) const {
96 std::scoped_lock lock(mTraceLock);
97 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
98 mBuffer->dump(result);
99}
100
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800101void LayerTracing::notify(bool visibleRegionDirty, int64_t time) {
Vishnu Nair00b90132021-11-05 14:03:40 -0700102 std::scoped_lock lock(mTraceLock);
103 if (!mEnabled) {
104 return;
105 }
106
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800107 if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
108 return;
109 }
110
Vishnu Nair00b90132021-11-05 14:03:40 -0700111 ATRACE_CALL();
112 LayersTraceProto entry;
Vishnu Nairb64a3b42022-01-13 15:29:32 -0800113 entry.set_elapsed_realtime_nanos(time);
114 const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
Vishnu Nair00b90132021-11-05 14:03:40 -0700115 entry.set_where(where);
116 LayersProto layers(mFlinger.dumpDrawingStateProto(mFlags));
117
118 if (flagIsSet(LayerTracing::TRACE_EXTRA)) {
119 mFlinger.dumpOffscreenLayersProto(layers);
120 }
121 entry.mutable_layers()->Swap(&layers);
122
123 if (flagIsSet(LayerTracing::TRACE_HWC)) {
124 std::string hwcDump;
125 mFlinger.dumpHwc(hwcDump);
126 entry.set_hwc_blob(hwcDump);
127 }
128 if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
129 entry.set_excludes_composition_state(true);
130 }
131 mFlinger.dumpDisplayProto(entry);
132 mBuffer->emplace(std::move(entry));
133}
134
135} // namespace android