OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkAtomics.h" | 8 #include "SkAtomics.h" |
9 #include "SkEventTracer.h" | 9 #include "SkEventTracer.h" |
10 #include "SkOnce.h" | 10 #include "SkOncePtr.h" |
11 | 11 |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 | 13 |
14 class SkDefaultEventTracer : public SkEventTracer { | 14 class SkDefaultEventTracer : public SkEventTracer { |
15 SkEventTracer::Handle | 15 SkEventTracer::Handle |
16 addTraceEvent(char phase, | 16 addTraceEvent(char phase, |
17 const uint8_t* categoryEnabledFlag, | 17 const uint8_t* categoryEnabledFlag, |
18 const char* name, | 18 const char* name, |
19 uint64_t id, | 19 uint64_t id, |
20 int numArgs, | 20 int numArgs, |
(...skipping 11 matching lines...) Expand all Loading... |
32 static uint8_t no = 0; | 32 static uint8_t no = 0; |
33 return &no; | 33 return &no; |
34 } | 34 } |
35 const char* getCategoryGroupName( | 35 const char* getCategoryGroupName( |
36 const uint8_t* categoryEnabledFlag) override { | 36 const uint8_t* categoryEnabledFlag) override { |
37 static const char* dummy = "dummy"; | 37 static const char* dummy = "dummy"; |
38 return dummy; | 38 return dummy; |
39 } | 39 } |
40 }; | 40 }; |
41 | 41 |
42 // We prefer gUserTracer if it's been set, otherwise we fall back on a default t
racer; | 42 // We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTra
cer. |
43 static SkEventTracer* gUserTracer = nullptr; | 43 static SkEventTracer* gUserTracer = nullptr; |
| 44 SK_DECLARE_STATIC_ONCE_PTR(SkDefaultEventTracer, gDefaultTracer); |
44 | 45 |
45 void SkEventTracer::SetInstance(SkEventTracer* tracer) { | 46 void SkEventTracer::SetInstance(SkEventTracer* tracer) { |
46 SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire)); | 47 SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire)); |
47 sk_atomic_store(&gUserTracer, tracer, sk_memory_order_release); | 48 sk_atomic_store(&gUserTracer, tracer, sk_memory_order_release); |
48 // An atomic load during process shutdown is probably overkill, but safe ove
rkill. | 49 // An atomic load during process shutdown is probably overkill, but safe ove
rkill. |
49 atexit([]() { delete sk_atomic_load(&gUserTracer, sk_memory_order_acquire);
}); | 50 atexit([]() { delete sk_atomic_load(&gUserTracer, sk_memory_order_acquire);
}); |
50 } | 51 } |
51 | 52 |
52 SkEventTracer* SkEventTracer::GetInstance() { | 53 SkEventTracer* SkEventTracer::GetInstance() { |
53 if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acq
uire)) { | 54 if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acq
uire)) { |
54 return tracer; | 55 return tracer; |
55 } | 56 } |
56 static SkOnce once; | 57 return gDefaultTracer.get([]{ return new SkDefaultEventTracer; }); |
57 static SkDefaultEventTracer* defaultTracer; | |
58 once([] { defaultTracer = new SkDefaultEventTracer; }); | |
59 return defaultTracer; | |
60 } | 58 } |
OLD | NEW |