| Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
|  | 27 | namespace android { | 
|  | 28 |  | 
|  | 29 | void 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 |  | 
|  | 41 | status_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 |  | 
|  | 55 | bool SurfaceTracing::isEnabled() { | 
|  | 56 | return mEnabled; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | void SurfaceTracing::traceLayers(const char* where, LayersProto layers) { | 
|  | 60 | std::lock_guard<std::mutex> protoGuard(mTraceMutex); | 
|  | 61 |  | 
|  | 62 | LayersTraceProto* entry = mTrace.add_entry(); | 
|  | 63 | entry->set_elapsed_realtime_nanos(elapsedRealtimeNano()); | 
|  | 64 | entry->set_where(where); | 
|  | 65 | entry->mutable_layers()->Swap(&layers); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | status_t SurfaceTracing::writeProtoFileLocked() { | 
|  | 69 | ATRACE_CALL(); | 
|  | 70 |  | 
|  | 71 | if (!mTrace.IsInitialized()) { | 
|  | 72 | return NOT_ENOUGH_DATA; | 
|  | 73 | } | 
|  | 74 | std::string output; | 
|  | 75 | if (!mTrace.SerializeToString(&output)) { | 
|  | 76 | return PERMISSION_DENIED; | 
|  | 77 | } | 
|  | 78 | if (!android::base::WriteStringToFile(output, mOutputFileName, true)) { | 
|  | 79 | return PERMISSION_DENIED; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | return NO_ERROR; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | } // namespace android |