blob: 0e9b04eed119b6f91549dc67134bc873782ecaea [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
55bool SurfaceTracing::isEnabled() {
56 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);
68}
69
70status_t SurfaceTracing::writeProtoFileLocked() {
71 ATRACE_CALL();
72
73 if (!mTrace.IsInitialized()) {
74 return NOT_ENOUGH_DATA;
75 }
76 std::string output;
77 if (!mTrace.SerializeToString(&output)) {
78 return PERMISSION_DENIED;
79 }
80 if (!android::base::WriteStringToFile(output, mOutputFileName, true)) {
81 return PERMISSION_DENIED;
82 }
83
84 return NO_ERROR;
85}
86
87} // namespace android