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