16169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org/*
26169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org * Copyright 2014 Google Inc.
36169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org *
46169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
56169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org * found in the LICENSE file.
66169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org */
76169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org
8b906daf112416c1698a79cf51aa70fe11aa19d0aherb#include "SkAtomics.h"
96169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org#include "SkEventTracer.h"
102880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include "SkOncePtr.h"
112880df2609eba09b555ca37be04b6ad89290c765Tom Hudson
122880df2609eba09b555ca37be04b6ad89290c765Tom Hudson#include <stdlib.h>
1360e0fee6d4acff638ccc9670c4055aced529a7a0bungeman
1459bcfaed660369da2a08fc3ad1ec2799b646f3e1mtkleinclass SkDefaultEventTracer : public SkEventTracer {
15283b5878e7225a49511cbd3f0fa12603a51319dftfarina    SkEventTracer::Handle
166169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org        addTraceEvent(char phase,
176169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      const uint8_t* categoryEnabledFlag,
186169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      const char* name,
196169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      uint64_t id,
206169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      int numArgs,
216169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      const char** argNames,
226169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      const uint8_t* argTypes,
236169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org                      const uint64_t* argValues,
2436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein                      uint8_t flags) override { return 0; }
254c18e9fbb685cccf23342757e786027a032197daskia.committer@gmail.com
26283b5878e7225a49511cbd3f0fa12603a51319dftfarina    void
274c18e9fbb685cccf23342757e786027a032197daskia.committer@gmail.com        updateTraceEventDuration(const uint8_t* categoryEnabledFlag,
284c18e9fbb685cccf23342757e786027a032197daskia.committer@gmail.com                                 const char* name,
29283b5878e7225a49511cbd3f0fa12603a51319dftfarina                                 SkEventTracer::Handle handle) override {}
306169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org
3136352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    const uint8_t* getCategoryGroupEnabled(const char* name) override {
326169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org        static uint8_t no = 0;
336169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org        return &no;
34283b5878e7225a49511cbd3f0fa12603a51319dftfarina    }
35283b5878e7225a49511cbd3f0fa12603a51319dftfarina    const char* getCategoryGroupName(
3636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein      const uint8_t* categoryEnabledFlag) override {
376169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org        static const char* dummy = "dummy";
386169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org        return dummy;
39283b5878e7225a49511cbd3f0fa12603a51319dftfarina    }
406169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org};
416169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org
4259bcfaed660369da2a08fc3ad1ec2799b646f3e1mtklein// We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTracer.
4359bcfaed660369da2a08fc3ad1ec2799b646f3e1mtkleinstatic SkEventTracer* gUserTracer = nullptr;
442880df2609eba09b555ca37be04b6ad89290c765Tom HudsonSK_DECLARE_STATIC_ONCE_PTR(SkDefaultEventTracer, gDefaultTracer);
456169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org
4659bcfaed660369da2a08fc3ad1ec2799b646f3e1mtkleinvoid SkEventTracer::SetInstance(SkEventTracer* tracer) {
47743be194eda4c1f37c4a5f62f38ef88f09f30649mtklein    SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire));
48743be194eda4c1f37c4a5f62f38ef88f09f30649mtklein    sk_atomic_store(&gUserTracer, tracer, sk_memory_order_release);
4959bcfaed660369da2a08fc3ad1ec2799b646f3e1mtklein    // An atomic load during process shutdown is probably overkill, but safe overkill.
502880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    atexit([]() { delete sk_atomic_load(&gUserTracer, sk_memory_order_acquire); });
516169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org}
526169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org
536169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.orgSkEventTracer* SkEventTracer::GetInstance() {
54743be194eda4c1f37c4a5f62f38ef88f09f30649mtklein    if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acquire)) {
5559bcfaed660369da2a08fc3ad1ec2799b646f3e1mtklein        return tracer;
5659bcfaed660369da2a08fc3ad1ec2799b646f3e1mtklein    }
572880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    return gDefaultTracer.get([]{ return new SkDefaultEventTracer; });
586169f2b4da39099b4e593f5ff85538dfe2f0249ecommit-bot@chromium.org}
59