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 "SkAtomics.h"
9 #include "SkEventTracer.h"
10 #include "SkLazyPtr.h"
11 
12 class SkDefaultEventTracer : public SkEventTracer {
13     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)14         addTraceEvent(char phase,
15                       const uint8_t* categoryEnabledFlag,
16                       const char* name,
17                       uint64_t id,
18                       int numArgs,
19                       const char** argNames,
20                       const uint8_t* argTypes,
21                       const uint64_t* argValues,
22                       uint8_t flags) override { return 0; }
23 
24     void
updateTraceEventDuration(const uint8_t * categoryEnabledFlag,const char * name,SkEventTracer::Handle handle)25         updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
26                                  const char* name,
27                                  SkEventTracer::Handle handle) override {}
28 
getCategoryGroupEnabled(const char * name)29     const uint8_t* getCategoryGroupEnabled(const char* name) override {
30         static uint8_t no = 0;
31         return &no;
32     }
getCategoryGroupName(const uint8_t * categoryEnabledFlag)33     const char* getCategoryGroupName(
34       const uint8_t* categoryEnabledFlag) override {
35         static const char* dummy = "dummy";
36         return dummy;
37     }
38 };
39 
40 // We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTracer.
41 static SkEventTracer* gUserTracer = nullptr;
42 SK_DECLARE_STATIC_LAZY_PTR(SkDefaultEventTracer, gDefaultTracer);
43 
SetInstance(SkEventTracer * tracer)44 void SkEventTracer::SetInstance(SkEventTracer* tracer) {
45     SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire));
46     sk_atomic_store(&gUserTracer, tracer, sk_memory_order_release);
47     // An atomic load during process shutdown is probably overkill, but safe overkill.
48     atexit([](){ SkDELETE(sk_atomic_load(&gUserTracer, sk_memory_order_acquire)); });
49 }
50 
GetInstance()51 SkEventTracer* SkEventTracer::GetInstance() {
52     if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acquire)) {
53         return tracer;
54     }
55     return gDefaultTracer.get();
56 }
57