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