blob: f465286c4f26738a5aafb874bcfb02f614a6e19c [file] [log] [blame]
Greg Daniel285db442016-10-14 09:12:53 -04001/*
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
16SkATrace::SkATrace() : fBeginSection(nullptr), fEndSection(nullptr), fIsEnabled(nullptr) {
Brian Osmane4f846a2017-08-10 15:45:30 -040017#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
18 fIsEnabled = []{ return static_cast<bool>(CC_UNLIKELY(ATRACE_ENABLED())); };
19 fBeginSection = [](const char* name){ ATRACE_BEGIN(name); };
20 fEndSection = []{ ATRACE_END(); };
21#elif defined(SK_BUILD_FOR_ANDROID)
Greg Daniel285db442016-10-14 09:12:53 -040022 if (void* lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL)) {
23 fBeginSection = (decltype(fBeginSection))dlsym(lib, "ATrace_beginSection");
24 fEndSection = (decltype(fEndSection))dlsym(lib, "ATrace_endSection");
25 fIsEnabled = (decltype(fIsEnabled))dlsym(lib, "ATrace_isEnabled");
26 }
27#endif
Brian Osmane4f846a2017-08-10 15:45:30 -040028
Greg Daniel285db442016-10-14 09:12:53 -040029 if (!fIsEnabled) {
30 fIsEnabled = []{ return false; };
31 }
32}
33
34SkEventTracer::Handle SkATrace::addTraceEvent(char phase,
35 const uint8_t* categoryEnabledFlag,
36 const char* name,
37 uint64_t id,
38 int numArgs,
39 const char** argNames,
40 const uint8_t* argTypes,
41 const uint64_t* argValues,
42 uint8_t flags) {
43 if (fIsEnabled()) {
44 if (TRACE_EVENT_PHASE_COMPLETE == phase ||
Greg Daniel285db442016-10-14 09:12:53 -040045 TRACE_EVENT_PHASE_INSTANT == phase) {
46 fBeginSection(name);
47 }
48
Brian Osman594838a2017-07-21 08:57:53 -040049 if (TRACE_EVENT_PHASE_INSTANT == phase) {
Greg Daniel285db442016-10-14 09:12:53 -040050 fEndSection();
51 }
52 }
53 return 0;
54}
55
56void SkATrace::updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
57 const char* name,
58 SkEventTracer::Handle handle) {
59 // This is only ever called from a scoped trace event so we will just end the ATrace section.
60 if (fIsEnabled()) {
61 fEndSection();
62 }
63}
64
65const uint8_t* SkATrace::getCategoryGroupEnabled(const char* name) {
66 // Chrome tracing is setup to not repeatly call this function once it has been initialized. So
67 // we can't use this to do a check for ATrace isEnabled(). Thus we will always return yes here
68 // and then check to see if ATrace is enabled when beginning and ending a section.
69 static uint8_t yes = SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags;
70 return &yes;
71}
72