commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 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 | |
herb | b906daf | 2015-09-29 09:37:59 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 9 | #include "SkEventTracer.h" |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 10 | #include "SkOncePtr.h" |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 11 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | |
mtklein | 59bcfae | 2015-04-21 09:38:03 -0700 | [diff] [blame] | 14 | class SkDefaultEventTracer : public SkEventTracer { |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 15 | SkEventTracer::Handle |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 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, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | uint8_t flags) override { return 0; } |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 25 | |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 26 | void |
skia.committer@gmail.com | 4c18e9f | 2014-01-31 03:01:59 +0000 | [diff] [blame] | 27 | updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
| 28 | const char* name, |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 29 | SkEventTracer::Handle handle) override {} |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | const uint8_t* getCategoryGroupEnabled(const char* name) override { |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 32 | static uint8_t no = 0; |
| 33 | return &no; |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 34 | } |
| 35 | const char* getCategoryGroupName( |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | const uint8_t* categoryEnabledFlag) override { |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 37 | static const char* dummy = "dummy"; |
| 38 | return dummy; |
tfarina | 283b587 | 2015-04-25 12:59:44 -0700 | [diff] [blame] | 39 | } |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
mtklein | 59bcfae | 2015-04-21 09:38:03 -0700 | [diff] [blame] | 42 | // We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTracer. |
| 43 | static SkEventTracer* gUserTracer = nullptr; |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 44 | SK_DECLARE_STATIC_ONCE_PTR(SkDefaultEventTracer, gDefaultTracer); |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 45 | |
mtklein | 59bcfae | 2015-04-21 09:38:03 -0700 | [diff] [blame] | 46 | void SkEventTracer::SetInstance(SkEventTracer* tracer) { |
mtklein | 743be19 | 2015-04-22 20:30:12 -0700 | [diff] [blame] | 47 | SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire)); |
| 48 | sk_atomic_store(&gUserTracer, tracer, sk_memory_order_release); |
mtklein | 59bcfae | 2015-04-21 09:38:03 -0700 | [diff] [blame] | 49 | // An atomic load during process shutdown is probably overkill, but safe overkill. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 50 | atexit([]() { delete sk_atomic_load(&gUserTracer, sk_memory_order_acquire); }); |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | SkEventTracer* SkEventTracer::GetInstance() { |
mtklein | 743be19 | 2015-04-22 20:30:12 -0700 | [diff] [blame] | 54 | if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acquire)) { |
mtklein | 59bcfae | 2015-04-21 09:38:03 -0700 | [diff] [blame] | 55 | return tracer; |
| 56 | } |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 57 | return gDefaultTracer.get([]{ return new SkDefaultEventTracer; }); |
commit-bot@chromium.org | 6169f2b | 2014-01-31 00:04:25 +0000 | [diff] [blame] | 58 | } |