blob: 67dcd06187df0b485a804b35c4e4eb600b5531c6 [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() {
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070030 ATRACE_CALL();
31 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
32
Adrian Roos1e1a1282017-11-01 19:05:31 +010033 if (mEnabled) {
34 return;
35 }
Adrian Roos1e1a1282017-11-01 19:05:31 +010036 mEnabled = true;
Adrian Roos1e1a1282017-11-01 19:05:31 +010037
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070038 mTrace = std::make_unique<LayersTraceFileProto>();
39 mTrace->set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
40 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
Adrian Roos1e1a1282017-11-01 19:05:31 +010041}
42
43status_t SurfaceTracing::disable() {
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070044 ATRACE_CALL();
45 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
46
Adrian Roos1e1a1282017-11-01 19:05:31 +010047 if (!mEnabled) {
48 return NO_ERROR;
49 }
Adrian Roos1e1a1282017-11-01 19:05:31 +010050 mEnabled = false;
51 status_t err(writeProtoFileLocked());
52 ALOGE_IF(err == PERMISSION_DENIED, "Could not save the proto file! Permission denied");
53 ALOGE_IF(err == NOT_ENOUGH_DATA, "Could not save the proto file! There are missing fields");
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070054 mTrace.reset();
Adrian Roos1e1a1282017-11-01 19:05:31 +010055 return err;
56}
57
Yichi Chenadc69612018-09-15 14:51:18 +080058bool SurfaceTracing::isEnabled() const {
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070059 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Adrian Roos1e1a1282017-11-01 19:05:31 +010060 return mEnabled;
61}
62
63void SurfaceTracing::traceLayers(const char* where, LayersProto layers) {
64 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
Vishnu Nairf770fcc2018-06-12 14:39:30 -070065 if (!mEnabled) {
66 return;
67 }
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070068 LayersTraceProto* entry = mTrace->add_entry();
Adrian Roos1e1a1282017-11-01 19:05:31 +010069 entry->set_elapsed_realtime_nanos(elapsedRealtimeNano());
70 entry->set_where(where);
71 entry->mutable_layers()->Swap(&layers);
Yichi Chenadc69612018-09-15 14:51:18 +080072
73 constexpr int maxBufferedEntryCount = 3600;
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070074 if (mTrace->entry_size() >= maxBufferedEntryCount) {
Yichi Chenadc69612018-09-15 14:51:18 +080075 // TODO: flush buffered entries without disabling tracing
76 ALOGE("too many buffered frames; force disable tracing");
77 mEnabled = false;
78 writeProtoFileLocked();
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070079 mTrace.reset();
Yichi Chenadc69612018-09-15 14:51:18 +080080 }
Adrian Roos1e1a1282017-11-01 19:05:31 +010081}
82
83status_t SurfaceTracing::writeProtoFileLocked() {
84 ATRACE_CALL();
85
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070086 if (!mTrace->IsInitialized()) {
Adrian Roos1e1a1282017-11-01 19:05:31 +010087 return NOT_ENOUGH_DATA;
88 }
89 std::string output;
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070090 if (!mTrace->SerializeToString(&output)) {
Adrian Roos1e1a1282017-11-01 19:05:31 +010091 return PERMISSION_DENIED;
92 }
93 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
94 return PERMISSION_DENIED;
95 }
96
97 return NO_ERROR;
98}
99
Yichi Chenadc69612018-09-15 14:51:18 +0800100void SurfaceTracing::dump(String8& result) const {
101 std::lock_guard<std::mutex> protoGuard(mTraceMutex);
102
103 result.appendFormat("Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700104 result.appendFormat(" number of entries: %d\n", mTrace ? mTrace->entry_size() : 0);
Yichi Chenadc69612018-09-15 14:51:18 +0800105}
106
Adrian Roos1e1a1282017-11-01 19:05:31 +0100107} // namespace android