1 /*
2 * Copyright 2014 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 "include/utils/SkEventTracer.h"
9
10 #include "include/private/base/SkOnce.h"
11
12 #include <stdlib.h>
13 #include <atomic>
14
15 class SkDefaultEventTracer : public SkEventTracer {
16 SkEventTracer::Handle
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)17 addTraceEvent(char phase,
18 const uint8_t* categoryEnabledFlag,
19 const char* name,
20 uint64_t id,
21 int numArgs,
22 const char** argNames,
23 const uint8_t* argTypes,
24 const uint64_t* argValues,
25 uint8_t flags) override { return 0; }
26
27 void
updateTraceEventDuration(const uint8_t * categoryEnabledFlag,const char * name,SkEventTracer::Handle handle)28 updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
29 const char* name,
30 SkEventTracer::Handle handle) override {}
31
getCategoryGroupEnabled(const char * name)32 const uint8_t* getCategoryGroupEnabled(const char* name) override {
33 static uint8_t no = 0;
34 return &no;
35 }
getCategoryGroupName(const uint8_t * categoryEnabledFlag)36 const char* getCategoryGroupName(
37 const uint8_t* categoryEnabledFlag) override {
38 static const char* stub = "stub";
39 return stub;
40 }
41
42 // The default tracer does not yet support splitting up trace output into sections.
newTracingSection(const char * name)43 void newTracingSection(const char* name) override {}
44 };
45
46 // We prefer gUserTracer if it's been set, otherwise we fall back on a default tracer;
47 static std::atomic<SkEventTracer*> gUserTracer{nullptr};
48
SetInstance(SkEventTracer * tracer,bool leakTracer)49 bool SkEventTracer::SetInstance(SkEventTracer* tracer, bool leakTracer) {
50 SkEventTracer* expected = nullptr;
51 if (!gUserTracer.compare_exchange_strong(expected, tracer)) {
52 delete tracer;
53 return false;
54 }
55 // If leaking the tracer is accepted then there is no need to install
56 // the atexit.
57 if (!leakTracer) {
58 atexit([]() { delete gUserTracer.load(); });
59 }
60 return true;
61 }
62
GetInstance()63 SkEventTracer* SkEventTracer::GetInstance() {
64 if (auto tracer = gUserTracer.load(std::memory_order_acquire)) {
65 return tracer;
66 }
67 static SkOnce once;
68 static SkDefaultEventTracer* defaultTracer;
69 once([] { defaultTracer = new SkDefaultEventTracer; });
70 return defaultTracer;
71 }
72