blob: 370b3b8b72a6907bbe3ddea1ee91065c4a6663c0 [file] [log] [blame]
Adrian Roos1e1a1282017-11-01 19:05:31 +01001/*
2 * Copyright 2017 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#undef LOG_TAG
17#define LOG_TAG "SurfaceTracing"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#include "SurfaceTracing.h"
21
22#include <android-base/file.h>
23#include <log/log.h>
24#include <utils/SystemClock.h>
25#include <utils/Trace.h>
26
27namespace android {
28
29void SurfaceTracing::enable() {
30 if (mEnabled) {
31 return;
32 }
33 ATRACE_CALL();
34 mEnabled = true;
35 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
36
37 mTrace.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
38 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
39}
40
41status_t SurfaceTracing::disable() {
42 if (!mEnabled) {
43 return NO_ERROR;
44 }
45 ATRACE_CALL();
46 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
47 mEnabled = false;
48 status_t err(writeProtoFileLocked());
49 ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied");
50 ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields");
51 mTrace.Clear();
52 return err;
53}
54
Yichi Chenadc69612018-09-15 14:51:18 +080055bool SurfaceTracing::isEnabled() const {
Adrian Roos1e1a1282017-11-01 19:05:31 +010056 return mEnabled;
57}
58
59void SurfaceTracing::traceLayers(const char* where, LayersProto layers) {
60 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Vishnu Nairf770fcc2018-06-12 14:39:30 -070061 if (!mEnabled) {
62 return;
63 }
Adrian Roos1e1a1282017-11-01 19:05:31 +010064 LayersTraceProto* entry = mTrace.add_entry();
65 entry->set_elapsed_realtime_nanos(elapsedRealtimeNano());
66 entry->set_where(where);
67 entry->mutable_layers()->Swap(&layers);
Yichi Chenadc69612018-09-15 14:51:18 +080068
69 constexpr int maxBufferedEntryCount = 3600;
70 if (mTrace.entry_size() >= maxBufferedEntryCount) {
71 // TODO: flush buffered entries without disabling tracing
72 ALOGE("too many buffered frames; force disable tracing");
73 mEnabled = false;
74 writeProtoFileLocked();
75 mTrace.Clear();
76 }
Adrian Roos1e1a1282017-11-01 19:05:31 +010077}
78
79status_t SurfaceTracing::writeProtoFileLocked() {
80 ATRACE_CALL();
81
82 if (!mTrace.IsInitialized()) {
83 return NOT_ENOUGH_DATA;
84 }
85 std::string output;
86 if (!mTrace.SerializeToString(&output)) {
87 return PERMISSION_DENIED;
88 }
89 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
90 return PERMISSION_DENIED;
91 }
92
93 return NO_ERROR;
94}
95
Yichi Chenadc69612018-09-15 14:51:18 +080096void SurfaceTracing::dump(String8& result) const {
97 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
98
99 result.appendFormat("Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
100 result.appendFormat(" number of entries: %d\n", mTrace.entry_size());
101}
102
Adrian Roos1e1a1282017-11-01 19:05:31 +0100103} // namespace android