blob: 2574306f6cbc6312e0e85a38678ed13ecc7cdd9f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkOnce.h"
9#include "include/utils/SkEventTracer.h"
Mike Kleinb24f7f42018-12-04 09:05:26 -050010#include <atomic>
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
mtkleinffa4a922016-05-05 16:05:56 -070042// We prefer gUserTracer if it's been set, otherwise we fall back on a default tracer;
Mike Kleinb24f7f42018-12-04 09:05:26 -050043static std::atomic<SkEventTracer*> gUserTracer{nullptr};
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000044
Brian Salomon175f5882017-05-12 12:02:50 -040045bool SkEventTracer::SetInstance(SkEventTracer* tracer) {
46 SkEventTracer* expected = nullptr;
Mike Kleinb24f7f42018-12-04 09:05:26 -050047 if (!gUserTracer.compare_exchange_strong(expected, tracer)) {
Brian Salomon175f5882017-05-12 12:02:50 -040048 delete tracer;
49 return false;
50 }
Mike Kleinb24f7f42018-12-04 09:05:26 -050051 atexit([]() { delete gUserTracer.load(); });
Brian Salomon175f5882017-05-12 12:02:50 -040052 return true;
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000053}
54
55SkEventTracer* SkEventTracer::GetInstance() {
Mike Kleinb24f7f42018-12-04 09:05:26 -050056 if (auto tracer = gUserTracer.load(std::memory_order_acquire)) {
mtklein59bcfae2015-04-21 09:38:03 -070057 return tracer;
58 }
mtkleinffa4a922016-05-05 16:05:56 -070059 static SkOnce once;
60 static SkDefaultEventTracer* defaultTracer;
61 once([] { defaultTracer = new SkDefaultEventTracer; });
62 return defaultTracer;
commit-bot@chromium.org6169f2b2014-01-31 00:04:25 +000063}