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 #ifndef SkATrace_DEFINED
      9 #define SkATrace_DEFINED
     10 
     11 #include "SkEventTracer.h"
     12 
     13 /**
     14  * This class is used to support ATrace in android apps. It hooks into the SkEventTracer system. It
     15  * currently supports the macros TRACE_EVENT*, TRACE_EVENT_INSTANT*, and TRANCE_EVENT_BEGIN/END*.
     16  * For versions of these calls that take additoinal args and value pairs we currently just drop them
     17  * and report only the name. Since ATrace is a simple push and pop system (all traces are fully
     18  * nested), if using BEGIN and END you should also make sure your calls are properly nested (i.e. if
     19  * startA is before startB, then endB is before endA).
     20  */
     21 class SkATrace : public SkEventTracer {
     22 public:
     23     SkATrace();
     24 
     25     SkEventTracer::Handle addTraceEvent(char phase,
     26                                         const uint8_t* categoryEnabledFlag,
     27                                         const char* name,
     28                                         uint64_t id,
     29                                         int numArgs,
     30                                         const char** argNames,
     31                                         const uint8_t* argTypes,
     32                                         const uint64_t* argValues,
     33                                         uint8_t flags) override;
     34 
     35 
     36     void updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
     37                                   const char* name,
     38                                   SkEventTracer::Handle handle) override;
     39 
     40     const uint8_t* getCategoryGroupEnabled(const char* name) override;
     41 
     42     const char* getCategoryGroupName(const uint8_t* categoryEnabledFlag) override {
     43         static const char* category = "skiaATrace";
     44         return category;
     45     }
     46 
     47 private:
     48     void (*fBeginSection)(const char*);
     49     void (*fEndSection)(void);
     50     bool (*fIsEnabled)(void);
     51 };
     52 
     53 
     54 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
     55 
     56   #include <utils/Trace.h>
     57   #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) SkAndroidTraceUtil __trace = \
     58           (SkAndroidTraceUtil::atraceFormatBegin(fmt, ##__VA_ARGS__), SkAndroidTraceUtil())
     59 
     60   class SkAndroidTraceUtil {
     61   public:
     62       ~SkAndroidTraceUtil() { ATRACE_END(); }
     63 
     64       static void atraceFormatBegin(const char* fmt, ...) {
     65           if (CC_LIKELY(!ATRACE_ENABLED())) return;
     66 
     67           const int BUFFER_SIZE = 256;
     68           va_list ap;
     69           char buf[BUFFER_SIZE];
     70 
     71           va_start(ap, fmt);
     72           vsnprintf(buf, BUFFER_SIZE, fmt, ap);
     73           va_end(ap);
     74 
     75           ATRACE_BEGIN(buf);
     76       }
     77   };
     78 
     79 #else
     80   #define ATRACE_ANDROID_FRAMEWORK(fmt, ...)
     81 #endif // SK_BUILD_FOR_ANDROID_FRAMEWORK
     82 
     83 #endif
     84 
     85