blob: 32a0207c23ebaa0f3d0ac9edd7a29688a67c0e23 [file] [log] [blame]
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +00001/*
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
herbb906daf2015-09-29 09:37:59 -07008#include "SkAtomics.h"
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +00009#include "SkEventTracer.h"
mtklein6c59d802015-09-09 09:09:53 -070010#include "SkOncePtr.h"
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000011
bungeman60e0fee2015-08-26 05:15:46 -070012#include <stdlib.h>
13
mtklein59bcfae2015-04-21 09:38:03 -070014class SkDefaultEventTracer : public SkEventTracer {
tfarina283b5872015-04-25 12:59:44 -070015 SkEventTracer::Handle
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000016 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,
mtklein36352bf2015-03-25 18:17:31 -070024 uint8_t flags) override { return 0; }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000025
tfarina283b5872015-04-25 12:59:44 -070026 void
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000027 updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
28 const char* name,
tfarina283b5872015-04-25 12:59:44 -070029 SkEventTracer::Handle handle) override {}
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000030
mtklein36352bf2015-03-25 18:17:31 -070031 const uint8_t* getCategoryGroupEnabled(const char* name) override {
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000032 static uint8_t no = 0;
33 return &no;
tfarina283b5872015-04-25 12:59:44 -070034 }
35 const char* getCategoryGroupName(
mtklein36352bf2015-03-25 18:17:31 -070036 const uint8_t* categoryEnabledFlag) override {
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000037 static const char* dummy = "dummy";
38 return dummy;
tfarina283b5872015-04-25 12:59:44 -070039 }
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000040};
41
mtklein59bcfae2015-04-21 09:38:03 -070042// We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTracer.
43static SkEventTracer* gUserTracer = nullptr;
mtklein6c59d802015-09-09 09:09:53 -070044SK_DECLARE_STATIC_ONCE_PTR(SkDefaultEventTracer, gDefaultTracer);
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000045
mtklein59bcfae2015-04-21 09:38:03 -070046void SkEventTracer::SetInstance(SkEventTracer* tracer) {
mtklein743be192015-04-22 20:30:12 -070047 SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire));
48 sk_atomic_store(&gUserTracer, tracer, sk_memory_order_release);
mtklein59bcfae2015-04-21 09:38:03 -070049 // An atomic load during process shutdown is probably overkill, but safe overkill.
halcanary385fe4d2015-08-26 13:07:48 -070050 atexit([]() { delete sk_atomic_load(&gUserTracer, sk_memory_order_acquire); });
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000051}
52
53SkEventTracer* SkEventTracer::GetInstance() {
mtklein743be192015-04-22 20:30:12 -070054 if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acquire)) {
mtklein59bcfae2015-04-21 09:38:03 -070055 return tracer;
56 }
mtklein6c59d802015-09-09 09:09:53 -070057 return gDefaultTracer.get([]{ return new SkDefaultEventTracer; });
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000058}