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 
8 #ifndef GrTracing_DEFINED
9 #define GrTracing_DEFINED
10 
11 #include "GrDrawTarget.h"
12 #include "GrGpu.h"
13 #include "GrTraceMarker.h"
14 #include "SkTLazy.h"
15 #include "SkTraceEvent.h"
16 
17 /**
18  * Marker generation class used for adding and removing markers around code blocks
19  */
20 class GrGpuTraceMarkerGenerator : public ::SkNoncopyable {
21 public:
GrGpuTraceMarkerGenerator(GrDrawTarget * target)22     GrGpuTraceMarkerGenerator(GrDrawTarget* target) {}
23 
~GrGpuTraceMarkerGenerator()24     ~GrGpuTraceMarkerGenerator() {
25         if (fTraceMarker.isValid()) {
26             // TODO remove trace marker
27         }
28     }
29 
initialize(const char * marker_str,int * marker_counter)30     void initialize(const char* marker_str, int* marker_counter) {
31         // GrGpuTraceMarker* traceMarker = fTraceMarker.init(marker_str, *marker_counter);
32         // TODO add trace marker
33     }
34 
35 private:
36     SkTLazy<GrGpuTraceMarker> fTraceMarker;
37 };
38 
39 class GrGpuTraceMarkerGeneratorContext : public ::SkNoncopyable {
40 public:
GrGpuTraceMarkerGeneratorContext(GrContext * context)41     GrGpuTraceMarkerGeneratorContext(GrContext* context) {}
42 
~GrGpuTraceMarkerGeneratorContext()43     ~GrGpuTraceMarkerGeneratorContext() {
44         if (fTraceMarker.isValid()) {
45             // TODO remove trace marker
46         }
47     }
48 
initialize(const char * marker_str,int * marker_counter)49     void initialize(const char* marker_str, int* marker_counter) {
50         // GrGpuTraceMarker* traceMarker = fTraceMarker.init(marker_str, *marker_counter);
51         // TODO add trace marker
52     }
53 
54 private:
55     SkTLazy<GrGpuTraceMarker> fTraceMarker;
56 };
57 
58 /**
59  * GR_CREATE_TRACE_MARKER will place begin and end trace markers for both
60  * cpu and gpu (if gpu tracing enabled) for the current scope.
61  * name is of type const char* and target is of type GrDrawTarget*
62  */
63 #define GR_CREATE_TRACE_MARKER(name, target)                                                       \
64     /* Chromium tracing */                                                                         \
65     static int SK_MACRO_APPEND_LINE(name_counter) = 0;                                             \
66     bool SK_MACRO_APPEND_LINE(gpuTracingEnabled);                                                  \
67     TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"),                      \
68                                        &SK_MACRO_APPEND_LINE(gpuTracingEnabled));                  \
69     if (SK_MACRO_APPEND_LINE(gpuTracingEnabled)) {                                                 \
70         INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED(name, SK_MACRO_APPEND_LINE(name_counter), target)   \
71     }                                                                                              \
72     INTERNAL_TRACE_EVENT_ADD_SCOPED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), name,                   \
73                                     "id", SK_MACRO_APPEND_LINE(name_counter));
74 
75 #define INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED(name, name_counter, target)        \
76     static const char* SK_MACRO_APPEND_LINE(static_name) = name;                  \
77     INTERNAL_GR_CREATE_TRACE_MARKER(SK_MACRO_APPEND_LINE(static_name),            \
78                                     name_counter,                                 \
79                                     target)                                       \
80     sk_atomic_inc(&name_counter);
81 
82 #define INTERNAL_GR_CREATE_TRACE_MARKER(name, name_counter, target, ...)          \
83     GR_CREATE_GPU_TRACE_MARKER(name, name_counter, target)                        \
84 
85 #define GR_CREATE_GPU_TRACE_MARKER(name, name_counter, target)                    \
86     GrGpuTraceMarkerGenerator SK_MACRO_APPEND_LINE(TMG)(target);                  \
87     SK_MACRO_APPEND_LINE(TMG).initialize(name, &name_counter);                    \
88 
89 /**
90  * Context level GrTracing macros, classname and op are const char*, context is GrContext
91  * TODO can we just have one set of macros?  Probably.
92  */
93 #define GR_CREATE_TRACE_MARKER_CONTEXT(classname, op, context)                                     \
94     /* Chromium tracing */                                                                         \
95     static int SK_MACRO_APPEND_LINE(name_counter) = 0;                                             \
96     bool SK_MACRO_APPEND_LINE(gpuTracingEnabled);                                                  \
97     TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"),                      \
98                                        &SK_MACRO_APPEND_LINE(gpuTracingEnabled));                  \
99     if (SK_MACRO_APPEND_LINE(gpuTracingEnabled)) {                                                 \
100         INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED_C(classname "::" op,                                \
101                                                  SK_MACRO_APPEND_LINE(name_counter), context)      \
102     }                                                                                              \
103     GR_AUDIT_TRAIL_AUTO_FRAME(context->getAuditTrail(), classname "::" op);                        \
104     INTERNAL_TRACE_EVENT_ADD_SCOPED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), classname "::" op,      \
105                                     "id", SK_MACRO_APPEND_LINE(name_counter));
106 
107 #define INTERNAL_GR_CREATE_TRACE_MARKER_SCOPED_C(name, name_counter, context)     \
108     static const char* SK_MACRO_APPEND_LINE(static_name) = name;                  \
109     INTERNAL_GR_CREATE_TRACE_MARKER_C(SK_MACRO_APPEND_LINE(static_name),          \
110                                       name_counter,                               \
111                                       context)                                    \
112     sk_atomic_inc(&name_counter);
113 
114 #define INTERNAL_GR_CREATE_TRACE_MARKER_C(name, name_counter, context, ...)       \
115     GR_CREATE_GPU_TRACE_MARKER_C(name, name_counter, context)                     \
116 
117 #define GR_CREATE_GPU_TRACE_MARKER_C(name, name_counter, context)                 \
118     GrGpuTraceMarkerGeneratorContext SK_MACRO_APPEND_LINE(TMG)(context);          \
119     SK_MACRO_APPEND_LINE(TMG).initialize(name, &name_counter);                    \
120 
121 #endif
122