• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SkATrace()16 SkATrace::SkATrace() : fBeginSection(nullptr), fEndSection(nullptr), fIsEnabled(nullptr) {
17 #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)
22     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
28 
29     if (!fIsEnabled) {
30         fIsEnabled = []{ return false; };
31     }
32 }
33 
addTraceEvent(char phase,const uint8_t * categoryEnabledFlag,const char * name,uint64_t id,int numArgs,const char ** argNames,const uint8_t * argTypes,const uint64_t * argValues,uint8_t flags)34 SkEventTracer::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 ||
45             TRACE_EVENT_PHASE_INSTANT == phase) {
46             fBeginSection(name);
47         }
48 
49         if (TRACE_EVENT_PHASE_INSTANT == phase) {
50             fEndSection();
51         }
52     }
53     return 0;
54 }
55 
updateTraceEventDuration(const uint8_t * categoryEnabledFlag,const char * name,SkEventTracer::Handle handle)56 void 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 
getCategoryGroupEnabled(const char * name)65 const 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 
73