From b7b9d02ac020f7f031c30aaea0c0467cf66e4569 Mon Sep 17 00:00:00 2001 From: Stan Iliev Date: Fri, 29 Jun 2018 18:19:12 -0400 Subject: [PATCH] Add a runtime flag to enable ATRACE Implement a runtime flag for android framework to enable systrace logging. To enable logging call SkATrace::setEnableTracing(true), instead of changing compile time flag SK_TRACE_EVENTS_IN_FRAMEWORK. Bug: skia: Change-Id: Ib4f607e001577a84e944042d799a98af8eb51626 Reviewed-on: https://skia-review.googlesource.com/138594 Commit-Queue: Stan Iliev Reviewed-by: Brian Osman --- src/core/SkATrace.cpp | 11 +++++++ src/core/SkTraceEventCommon.h | 62 +++++++++++++++++------------------ 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/core/SkATrace.cpp b/src/core/SkATrace.cpp index f465286c4f..bd80461796 100644 --- a/src/core/SkATrace.cpp +++ b/src/core/SkATrace.cpp @@ -9,6 +9,8 @@ #include "SkTraceEvent.h" +#include "SkTraceEventCommon.h" + #ifdef SK_BUILD_FOR_ANDROID #include #endif @@ -70,3 +72,12 @@ const uint8_t* SkATrace::getCategoryGroupEnabled(const char* name) { return &yes; } + +#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK + +bool SkAndroidFrameworkTraceUtil::gEnableAndroidTracing = false; + +#endif //SK_BUILD_FOR_ANDROID_FRAMEWORK + + + diff --git a/src/core/SkTraceEventCommon.h b/src/core/SkTraceEventCommon.h index 960e42910a..199b487796 100644 --- a/src/core/SkTraceEventCommon.h +++ b/src/core/SkTraceEventCommon.h @@ -5,6 +5,7 @@ #define SkTraceEventCommon_DEFINED #include "SkTraceEventPhase.h" +#include "SkTypes.h" // Trace events are for tracking application performance and resource usage. // Macros are provided to track: @@ -61,13 +62,15 @@ #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK #include -struct SkAndroidFrameworkTraceUtil { - +class SkAndroidFrameworkTraceUtil { +public: SkAndroidFrameworkTraceUtil(const char* name) { - ATRACE_BEGIN(name); + if (CC_UNLIKELY(gEnableAndroidTracing)) { + ATRACE_BEGIN(name); + } } SkAndroidFrameworkTraceUtil(bool, const char* fmt, ...) { - if (CC_LIKELY(!ATRACE_ENABLED())) return; + if (CC_LIKELY((!gEnableAndroidTracing) || (!ATRACE_ENABLED()))) return; const int BUFFER_SIZE = 256; va_list ap; @@ -79,19 +82,26 @@ struct SkAndroidFrameworkTraceUtil { ATRACE_BEGIN(buf); } - ~SkAndroidFrameworkTraceUtil() { ATRACE_END(); } + ~SkAndroidFrameworkTraceUtil() { + if (CC_UNLIKELY(gEnableAndroidTracing)) { + ATRACE_END(); + } + } + + static void setEnableTracing(bool enableAndroidTracing) { + gEnableAndroidTracing = enableAndroidTracing; + } + + static bool getEnableTracing() { + return gEnableAndroidTracing; + } + +private: + static bool gEnableAndroidTracing; }; #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) SkAndroidFrameworkTraceUtil __trace(true, fmt, ##__VA_ARGS__) -// If profiling Skia within the Android framework, setting this to 1 will route all Skia -// tracing events to ATrace. -#ifndef SK_TRACE_EVENTS_IN_FRAMEWORK -#define SK_TRACE_EVENTS_IN_FRAMEWORK 0 -#endif - -#if SK_TRACE_EVENTS_IN_FRAMEWORK - // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2 // associated arguments. In the framework, the arguments are ignored. #define TRACE_EVENT0(category_group, name) \ @@ -115,32 +125,22 @@ struct SkAndroidFrameworkTraceUtil { // Records the value of a counter called "name" immediately. Value // must be representable as a 32 bit integer. -#define TRACE_COUNTER1(category_group, name, value) ATRACE_INT(name, value) +#define TRACE_COUNTER1(category_group, name, value) \ + if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \ + ATRACE_INT(name, value); \ + } // Records the values of a multi-parted counter called "name" immediately. // In Chrome, this macro produces a stacked bar chart. ATrace doesn't support // that, so this just produces two separate counters. #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val) \ do { \ - ATRACE_INT(name "-" value1_name, value1_val); \ - ATRACE_INT(name "-" value2_name, value2_val); \ + if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \ + ATRACE_INT(name "-" value1_name, value1_val); \ + ATRACE_INT(name "-" value2_name, value2_val); \ + } \ } while (0) -#else - -#define TRACE_EVENT0(category_group, name) TRACE_EMPTY -#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) TRACE_EMPTY -#define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) TRACE_EMPTY - -#define TRACE_EVENT_INSTANT0(category_group, name, scope) TRACE_EMPTY -#define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) TRACE_EMPTY -#define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, arg2_name, arg2_val) TRACE_EMPTY - -#define TRACE_COUNTER1(category_group, name, value) TRACE_EMPTY -#define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val) TRACE_EMPTY - -#endif - // ATrace has no object tracking #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) TRACE_EMPTY #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) TRACE_EMPTY