| 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 "src/core/SkATrace.h" |
| 9 |
|
| 10 |
#include "include/utils/SkTraceEventPhase.h" |
| 11 |
|
| 12 |
#ifdef SK_BUILD_FOR_ANDROID |
| 13 |
#include <dlfcn.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 17 |
#include <cutils/trace.h> |
| 18 |
#include "src/core/SkTraceEventCommon.h" // for SkAndroidFrameworkTraceUtil |
| 19 |
#endif |
| 20 |
|
| 21 |
SkATrace::SkATrace() : fBeginSection(nullptr), fEndSection(nullptr), fIsEnabled(nullptr) { |
| 22 |
#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 23 |
fIsEnabled = []{ return static_cast<bool>(CC_UNLIKELY(ATRACE_ENABLED())); }; |
| 24 |
fBeginSection = [](const char* name){ ATRACE_BEGIN(name); }; |
| 25 |
fEndSection = []{ ATRACE_END(); }; |
| 26 |
#elif defined(SK_BUILD_FOR_ANDROID) |
| 27 |
if (void* lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL)) { |
| 28 |
fBeginSection = (decltype(fBeginSection))dlsym(lib, "ATrace_beginSection"); |
| 29 |
fEndSection = (decltype(fEndSection))dlsym(lib, "ATrace_endSection"); |
| 30 |
fIsEnabled = (decltype(fIsEnabled))dlsym(lib, "ATrace_isEnabled"); |
| 31 |
} |
| 32 |
#endif |
| 33 |
|
| 34 |
if (!fIsEnabled) { |
| 35 |
fIsEnabled = []{ return false; }; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
SkEventTracer::Handle SkATrace::addTraceEvent(char phase, |
| 40 |
const uint8_t* categoryEnabledFlag, |
| 41 |
const char* name, |
| 42 |
uint64_t id, |
| 43 |
int numArgs, |
| 44 |
const char** argNames, |
| 45 |
const uint8_t* argTypes, |
| 46 |
const uint64_t* argValues, |
| 47 |
uint8_t flags) { |
| 48 |
if (fIsEnabled()) { |
| 49 |
if (TRACE_EVENT_PHASE_COMPLETE == phase || |
| 50 |
TRACE_EVENT_PHASE_INSTANT == phase) { |
| 51 |
fBeginSection(name); |
| 52 |
} |
| 53 |
|
| 54 |
if (TRACE_EVENT_PHASE_INSTANT == phase) { |
| 55 |
fEndSection(); |
| 56 |
} |
| 57 |
} |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
void SkATrace::updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| 62 |
const char* name, |
| 63 |
SkEventTracer::Handle handle) { |
| 64 |
// This is only ever called from a scoped trace event so we will just end the ATrace section. |
| 65 |
if (fIsEnabled()) { |
| 66 |
fEndSection(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
const uint8_t* SkATrace::getCategoryGroupEnabled(const char* name) { |
| 71 |
// Chrome tracing is setup to not repeatly call this function once it has been initialized. So |
| 72 |
// we can't use this to do a check for ATrace isEnabled(). Thus we will always return yes here |
| 73 |
// and then check to see if ATrace is enabled when beginning and ending a section. |
| 74 |
static uint8_t yes = SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags; |
| 75 |
return &yes; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 80 |
|
| 81 |
bool SkAndroidFrameworkTraceUtil::gEnableAndroidTracing = false; |
| 82 |
bool SkAndroidFrameworkTraceUtil::gUsePerfettoTrackEvents = false; |
| 83 |
|
| 84 |
#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 85 |
|
| 86 |
|
| 87 |
|