12a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org/*
22a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org * Copyright 2014 Google Inc.
32a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org *
42a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
52a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org * found in the LICENSE file.
62a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org */
72a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
82a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#ifndef GrTracing_DEFINED
92a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#define GrTracing_DEFINED
102a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
11bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel#include "GrGpu.h"
122a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#include "GrTraceMarker.h"
1372440a3785c13b8ec539d7e11bea1124eeddecbdbungeman#include "SkTLazy.h"
142a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#include "SkTraceEvent.h"
152a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
162a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org/**
172a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org * Marker generation class used for adding and removing markers around code blocks
182a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org */
192a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.orgclass GrGpuTraceMarkerGenerator : public ::SkNoncopyable {
202a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.orgpublic:
2173c4e64f4bb90059cf822dac9eaf30e1c38bec77robertphillips    GrGpuTraceMarkerGenerator() {}
222a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
232a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    ~GrGpuTraceMarkerGenerator() {
242a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org        if (fTraceMarker.isValid()) {
25dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt            // TODO remove trace marker
262a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org        }
272a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    }
282a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
292a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    void initialize(const char* marker_str, int* marker_counter) {
30dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt        // GrGpuTraceMarker* traceMarker = fTraceMarker.init(marker_str, *marker_counter);
31dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt        // TODO add trace marker
322a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    }
332a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
342a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.orgprivate:
352a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    SkTLazy<GrGpuTraceMarker> fTraceMarker;
362a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org};
372a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
38bbcb38df4f7477aa0a38da00a0757586a0125954egdanielclass GrGpuTraceMarkerGeneratorContext : public ::SkNoncopyable {
39bbcb38df4f7477aa0a38da00a0757586a0125954egdanielpublic:
40dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt    GrGpuTraceMarkerGeneratorContext(GrContext* context) {}
41bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel
42bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    ~GrGpuTraceMarkerGeneratorContext() {
43bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel        if (fTraceMarker.isValid()) {
44dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt            // TODO remove trace marker
45bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel        }
46bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    }
47bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel
48bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    void initialize(const char* marker_str, int* marker_counter) {
49dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt        // GrGpuTraceMarker* traceMarker = fTraceMarker.init(marker_str, *marker_counter);
50dac7005b877c7c1c6f36b6e298133dfc7b85b9c0joshualitt        // TODO add trace marker
51bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    }
52bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel
53bbcb38df4f7477aa0a38da00a0757586a0125954egdanielprivate:
54bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    SkTLazy<GrGpuTraceMarker> fTraceMarker;
55bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel};
56bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel
572a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org/**
582a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org * GR_CREATE_TRACE_MARKER will place begin and end trace markers for both
592a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org * cpu and gpu (if gpu tracing enabled) for the current scope.
60f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips * name is of type const char* and target is of type GrOpList*
612a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org */
624897ff20fc68242d8a3469721f381f03987a0723joshualitt#define GR_CREATE_TRACE_MARKER(name, target)                                                       \
634897ff20fc68242d8a3469721f381f03987a0723joshualitt    /* Chromium tracing */                                                                         \
644897ff20fc68242d8a3469721f381f03987a0723joshualitt    static int SK_MACRO_APPEND_LINE(name_counter) = 0;                                             \
654897ff20fc68242d8a3469721f381f03987a0723joshualitt    bool SK_MACRO_APPEND_LINE(gpuTracingEnabled);                                                  \
664897ff20fc68242d8a3469721f381f03987a0723joshualitt    TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"),                      \
674897ff20fc68242d8a3469721f381f03987a0723joshualitt                                       &SK_MACRO_APPEND_LINE(gpuTracingEnabled));                  \
684897ff20fc68242d8a3469721f381f03987a0723joshualitt    if (SK_MACRO_APPEND_LINE(gpuTracingEnabled)) {                                                 \
694897ff20fc68242d8a3469721f381f03987a0723joshualitt        INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED(name, SK_MACRO_APPEND_LINE(name_counter), target)   \
704897ff20fc68242d8a3469721f381f03987a0723joshualitt    }                                                                                              \
714897ff20fc68242d8a3469721f381f03987a0723joshualitt    INTERNAL_TRACE_EVENT_ADD_SCOPED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), name,                   \
724897ff20fc68242d8a3469721f381f03987a0723joshualitt                                    "id", SK_MACRO_APPEND_LINE(name_counter));
734897ff20fc68242d8a3469721f381f03987a0723joshualitt
744897ff20fc68242d8a3469721f381f03987a0723joshualitt#define INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED(name, name_counter, target)        \
7593a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel    static const char* SK_MACRO_APPEND_LINE(static_name) = name;                  \
7693a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel    INTERNAL_GR_CREATE_TRACE_MARKER(SK_MACRO_APPEND_LINE(static_name),            \
774897ff20fc68242d8a3469721f381f03987a0723joshualitt                                    name_counter,                                 \
784897ff20fc68242d8a3469721f381f03987a0723joshualitt                                    target)                                       \
794897ff20fc68242d8a3469721f381f03987a0723joshualitt    sk_atomic_inc(&name_counter);
8093a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel
8193a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel#define INTERNAL_GR_CREATE_TRACE_MARKER(name, name_counter, target, ...)          \
8293a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel    GR_CREATE_GPU_TRACE_MARKER(name, name_counter, target)                        \
8393a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel
844897ff20fc68242d8a3469721f381f03987a0723joshualitt#define GR_CREATE_GPU_TRACE_MARKER(name, name_counter, target)                    \
854897ff20fc68242d8a3469721f381f03987a0723joshualitt    GrGpuTraceMarkerGenerator SK_MACRO_APPEND_LINE(TMG)(target);                  \
864897ff20fc68242d8a3469721f381f03987a0723joshualitt    SK_MACRO_APPEND_LINE(TMG).initialize(name, &name_counter);                    \
874897ff20fc68242d8a3469721f381f03987a0723joshualitt
8887a721b2465c9ccfa191ce9f5012f92be7731fbcjoshualitt/**
8987a721b2465c9ccfa191ce9f5012f92be7731fbcjoshualitt * Context level GrTracing macros, classname and op are const char*, context is GrContext
9087a721b2465c9ccfa191ce9f5012f92be7731fbcjoshualitt * TODO can we just have one set of macros?  Probably.
9187a721b2465c9ccfa191ce9f5012f92be7731fbcjoshualitt */
925651ee6376dc3271ea8f4fc91b037c8f361c3cfajoshualitt#define GR_CREATE_TRACE_MARKER_CONTEXT(classname, op, context)                                     \
934897ff20fc68242d8a3469721f381f03987a0723joshualitt    /* Chromium tracing */                                                                         \
944897ff20fc68242d8a3469721f381f03987a0723joshualitt    static int SK_MACRO_APPEND_LINE(name_counter) = 0;                                             \
954897ff20fc68242d8a3469721f381f03987a0723joshualitt    bool SK_MACRO_APPEND_LINE(gpuTracingEnabled);                                                  \
964897ff20fc68242d8a3469721f381f03987a0723joshualitt    TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"),                      \
974897ff20fc68242d8a3469721f381f03987a0723joshualitt                                       &SK_MACRO_APPEND_LINE(gpuTracingEnabled));                  \
984897ff20fc68242d8a3469721f381f03987a0723joshualitt    if (SK_MACRO_APPEND_LINE(gpuTracingEnabled)) {                                                 \
995651ee6376dc3271ea8f4fc91b037c8f361c3cfajoshualitt        INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED_C(classname "::" op,                                \
1005651ee6376dc3271ea8f4fc91b037c8f361c3cfajoshualitt                                                 SK_MACRO_APPEND_LINE(name_counter), context)      \
1014897ff20fc68242d8a3469721f381f03987a0723joshualitt    }                                                                                              \
10287a721b2465c9ccfa191ce9f5012f92be7731fbcjoshualitt    GR_AUDIT_TRAIL_AUTO_FRAME(context->getAuditTrail(), classname "::" op);                        \
1035651ee6376dc3271ea8f4fc91b037c8f361c3cfajoshualitt    INTERNAL_TRACE_EVENT_ADD_SCOPED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), classname "::" op,      \
1044897ff20fc68242d8a3469721f381f03987a0723joshualitt                                    "id", SK_MACRO_APPEND_LINE(name_counter));
1054897ff20fc68242d8a3469721f381f03987a0723joshualitt
1064897ff20fc68242d8a3469721f381f03987a0723joshualitt#define INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED_C(name, name_counter, context)     \
10793a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel    static const char* SK_MACRO_APPEND_LINE(static_name) = name;                  \
10893a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel    INTERNAL_GR_CREATE_TRACE_MARKER_C(SK_MACRO_APPEND_LINE(static_name),          \
1094897ff20fc68242d8a3469721f381f03987a0723joshualitt                                      name_counter,                               \
1104897ff20fc68242d8a3469721f381f03987a0723joshualitt                                      context)                                    \
1114897ff20fc68242d8a3469721f381f03987a0723joshualitt    sk_atomic_inc(&name_counter);
11293a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel
11393a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel#define INTERNAL_GR_CREATE_TRACE_MARKER_C(name, name_counter, context, ...)       \
11493a37bc91a40a7cd14f9f8eca6097535ab1099e4egdaniel    GR_CREATE_GPU_TRACE_MARKER_C(name, name_counter, context)                     \
1154897ff20fc68242d8a3469721f381f03987a0723joshualitt
1164897ff20fc68242d8a3469721f381f03987a0723joshualitt#define GR_CREATE_GPU_TRACE_MARKER_C(name, name_counter, context)                 \
1174897ff20fc68242d8a3469721f381f03987a0723joshualitt    GrGpuTraceMarkerGeneratorContext SK_MACRO_APPEND_LINE(TMG)(context);          \
1184897ff20fc68242d8a3469721f381f03987a0723joshualitt    SK_MACRO_APPEND_LINE(TMG).initialize(name, &name_counter);                    \
1192a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
1202a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org#endif
121