Greg Daniel | 285db44 | 2016-10-14 09:12:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkATrace.h" |
| 9 | |
| 10 | #include "SkTraceEvent.h" |
| 11 | |
| 12 | #ifdef SK_BUILD_FOR_ANDROID |
| 13 | #include <dlfcn.h> |
| 14 | #endif |
| 15 | |
| 16 | SkATrace::SkATrace() : fBeginSection(nullptr), fEndSection(nullptr), fIsEnabled(nullptr) { |
| 17 | #ifdef SK_BUILD_FOR_ANDROID |
| 18 | if (void* lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL)) { |
| 19 | fBeginSection = (decltype(fBeginSection))dlsym(lib, "ATrace_beginSection"); |
| 20 | fEndSection = (decltype(fEndSection))dlsym(lib, "ATrace_endSection"); |
| 21 | fIsEnabled = (decltype(fIsEnabled))dlsym(lib, "ATrace_isEnabled"); |
| 22 | } |
| 23 | #endif |
| 24 | if (!fIsEnabled) { |
| 25 | fIsEnabled = []{ return false; }; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | SkEventTracer::Handle SkATrace::addTraceEvent(char phase, |
| 30 | const uint8_t* categoryEnabledFlag, |
| 31 | const char* name, |
| 32 | uint64_t id, |
| 33 | int numArgs, |
| 34 | const char** argNames, |
| 35 | const uint8_t* argTypes, |
| 36 | const uint64_t* argValues, |
| 37 | uint8_t flags) { |
| 38 | if (fIsEnabled()) { |
| 39 | if (TRACE_EVENT_PHASE_COMPLETE == phase || |
| 40 | TRACE_EVENT_PHASE_BEGIN == phase || |
| 41 | TRACE_EVENT_PHASE_INSTANT == phase) { |
| 42 | fBeginSection(name); |
| 43 | } |
| 44 | |
| 45 | if (TRACE_EVENT_PHASE_END == phase || |
| 46 | TRACE_EVENT_PHASE_INSTANT == phase) { |
| 47 | fEndSection(); |
| 48 | } |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | void SkATrace::updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| 54 | const char* name, |
| 55 | SkEventTracer::Handle handle) { |
| 56 | // This is only ever called from a scoped trace event so we will just end the ATrace section. |
| 57 | if (fIsEnabled()) { |
| 58 | fEndSection(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const uint8_t* SkATrace::getCategoryGroupEnabled(const char* name) { |
| 63 | // Chrome tracing is setup to not repeatly call this function once it has been initialized. So |
| 64 | // we can't use this to do a check for ATrace isEnabled(). Thus we will always return yes here |
| 65 | // and then check to see if ATrace is enabled when beginning and ending a section. |
| 66 | static uint8_t yes = SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags; |
| 67 | return &yes; |
| 68 | } |
| 69 | |