File: viz_debugger.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (453 lines) | stat: -rw-r--r-- 16,787 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_VIZ_SERVICE_DEBUGGER_VIZ_DEBUGGER_H_
#define COMPONENTS_VIZ_SERVICE_DEBUGGER_VIZ_DEBUGGER_H_

#include <atomic>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "base/debug/debugging_buildflags.h"
#include "base/macros/concat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "base/values.h"
#include "components/viz/common/buildflags.h"
#include "components/viz/service/debugger/mojom/viz_debugger.mojom.h"
#include "components/viz/service/debugger/rwlock.h"
#include "components/viz/service/viz_service_export.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/vector2d_f.h"

// The visual debugger runtime can be completely disabled/enabled at compile
// time via the |USE_VIZ_DEBUGGER| build flag which corresponds to boolean gn
// arg 'use_viz_debugger'. Consult README.md for more information.

// To allow specific debug calls to become traces for end users you need to
// specify the DBG_USE_VIZ_DEBUGGER_TRACE() macro to the value 1. You can define
// this just prior to the callsite rather than needing to definite this prior to
// the header inclusion.

#if BUILDFLAG(USE_VIZ_DEBUGGER)

#define VIZ_DEBUGGER_IS_ON() true

namespace viz {

class VIZ_SERVICE_EXPORT VizDebugger {
 public:
  // These functions are called on a gpu thread that is not the
  // 'VizCompositorThread' and therefore have mulithreaded considerations.
  void FilterDebugStream(base::Value::Dict json);
  void StartDebugStream(
      mojo::PendingRemote<mojom::VizDebugOutput> pending_debug_output);
  void StopDebugStream();

  struct VIZ_SERVICE_EXPORT StaticSource {
    StaticSource(const char* anno_name,
                 const char* file_name,
                 int file_line,
                 const char* func_name);
    inline bool IsActive() const { return active; }
    inline bool IsEnabled() const { return enabled; }
    const char* anno = nullptr;
    const char* file = nullptr;
    const char* func = nullptr;
    const int line = 0;

    int reg_index = 0;
    bool active = false;
    bool enabled = false;
  };

  struct DrawOption {
    // TODO(petermcneeley): Consider moving this custom rgba color data over to
    // |SkColor| representation.
    uint8_t color_r;
    uint8_t color_g;
    uint8_t color_b;
    // Alpha is applied to rect fill only.
    uint8_t color_a;
  };

  ALWAYS_INLINE static bool IsEnabled() {
    return enabled_.load(std::memory_order_acquire);
  }

  static VizDebugger* GetInstance();

  VizDebugger();
  ~VizDebugger();

  struct VIZ_SERVICE_EXPORT BufferInfo {
    BufferInfo();
    ~BufferInfo();
    BufferInfo(const BufferInfo& a);
    SkBitmap bitmap;
  };

  struct Buffer {
    int id;
    BufferInfo buffer_info;
  };

  void SubmitBuffer(int buff_id, BufferInfo&& buffer);

  void CompleteFrame(const uint64_t counter,
                     const gfx::Size& window_pix,
                     base::TimeTicks time_ticks);
  void Draw(const gfx::SizeF& obj_size,
            const gfx::Vector2dF& pos,
            const StaticSource* dcs,
            DrawOption option,
            int* id,
            const gfx::RectF& uv,
            const std::string& text);

  void AddLogMessage(std::string log,
                     const StaticSource* dcs,
                     DrawOption option);

  VizDebugger(const VizDebugger&) = delete;
  VizDebugger& operator=(const VizDebugger&) = delete;

 private:
  friend class VizDebuggerInternal;
  static std::atomic<bool> enabled_;
  base::Value FrameAsJson(const uint64_t counter,
                          const gfx::Size& window_pix,
                          base::TimeTicks time_ticks);

  void AddFrame();
  void UpdateFilters();
  void RegisterSource(StaticSource* source);
  void DrawInternal(const gfx::SizeF& obj_size,
                    const gfx::Vector2dF& pos,
                    const StaticSource* dcs,
                    DrawOption option,
                    int* id,
                    const gfx::RectF& uv,
                    const std::string& text);
  void ApplyFilters(VizDebugger::StaticSource* source);
  mojo::Remote<mojom::VizDebugOutput> debug_output_;

  // This |task_runner_| is required to send json through mojo.
  scoped_refptr<base::SequencedTaskRunner> gpu_thread_task_runner_;

  struct CallSubmitCommon {
    CallSubmitCommon() = default;
    CallSubmitCommon(int index,
                     int source,
                     int64_t thread,
                     DrawOption draw_option)
        : draw_index(index),
          source_index(source),
          thread_id(thread),
          option(draw_option) {}
    base::Value::Dict GetDictionaryValue() const;
    int draw_index;
    int source_index;
    int64_t thread_id;
    VizDebugger::DrawOption option;
  };

  struct DrawCall : public CallSubmitCommon {
    DrawCall() = default;
    DrawCall(int index,
             int source,
             int64_t thread,
             DrawOption draw_option,
             gfx::SizeF size,
             gfx::Vector2dF position,
             int buffer_id,
             gfx::RectF uv_rect,
             std::string text_str)
        : CallSubmitCommon(index, source, thread, draw_option),
          obj_size(size),
          pos(position),
          buff_id(buffer_id),
          uv(uv_rect),
          text(text_str) {}
    gfx::SizeF obj_size;
    gfx::Vector2dF pos;
    int buff_id;
    gfx::RectF uv;
    std::string text;
  };

  struct LogCall : public CallSubmitCommon {
    LogCall() = default;
    LogCall(int index,
            int source,
            int64_t thread,
            DrawOption draw_option,
            std::string str)
        : CallSubmitCommon(index, source, thread, draw_option),
          value(std::move(str)) {}
    std::string value;
  };

  struct VIZ_SERVICE_EXPORT FilterBlock {
    FilterBlock(const std::string file_str,
                const std::string func_str,
                const std::string anno_str,
                bool is_active,
                bool is_enabled);
    ~FilterBlock();
    FilterBlock(const FilterBlock& other);
    std::string file;
    std::string func;
    std::string anno;
    bool active = false;
    bool enabled = false;
  };

  // Synchronize access to buffers and variables mutated by multiple threads.
  rwlock::RWLock read_write_lock_;

  // New filters to promoted to cached filters on next frame.
  std::vector<FilterBlock> new_filters_;
  bool apply_new_filters_next_frame_ = false;

  // Json is saved out every frame on the call to 'CompleteFrame' but may not be
  // uploaded immediately due to task runner sequencing.
  base::Value json_frame_output_;
  size_t last_sent_source_count_ = 0;

  // Cached filters to apply filtering to new sources not just on filter update.
  std::vector<FilterBlock> cached_filters_;
  int buffer_id = 0;

  // Common counter for all submissions. This variable can be accessed by
  // multiple threads atomically.
  std::atomic<int> submission_count_ = 0;

  // Default starting size for each buffer/vector.
  static constexpr int kDefaultBufferSize = 64;

  // Buffers/vectors for each type of debug calls. These vectors can be accessed
  // and mutated by multiple threads simultaneously or individually.
  std::vector<DrawCall> draw_rect_calls_{kDefaultBufferSize};
  std::vector<LogCall> logs_{kDefaultBufferSize};
  std::vector<StaticSource*> sources_;
  std::vector<Buffer> buffers_;

  // Individual tail indices tracker variables for next insertion index in
  // each buffer. These variables can be accessed by multiple threads
  // atomically.
  std::atomic<int> draw_calls_tail_idx_ = 0;
  std::atomic<int> logs_tail_idx_ = 0;
};

}  // namespace viz

#define DBG_OPT_RED viz::VizDebugger::DrawOption({255, 0, 0, 0})
#define DBG_OPT_GREEN viz::VizDebugger::DrawOption({0, 255, 0, 0})
#define DBG_OPT_BLUE viz::VizDebugger::DrawOption({0, 0, 255, 0})
#define DBG_OPT_BLACK viz::VizDebugger::DrawOption({0, 0, 0, 0})
#define DBG_DEFAULT_UV gfx::RectF(0, 0, 1, 1)

#define DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT(anno, option, pos, size, id, uv,   \
                                            text)                              \
  do {                                                                         \
    if (viz::VizDebugger::IsEnabled()) {                                       \
      static viz::VizDebugger::StaticSource dcs(anno, __FILE__, __LINE__,      \
                                                __func__);                     \
      if (dcs.IsActive()) {                                                    \
        viz::VizDebugger::GetInstance()->Draw(size, pos, &dcs, option, id, uv, \
                                              text);                           \
      }                                                                        \
    }                                                                          \
  } while (0)

#define DBG_COMPLETE_BUFFERS(buff_id, buffer)                           \
  do {                                                                  \
    if (viz::VizDebugger::IsEnabled()) {                                \
      viz::VizDebugger::GetInstance()->SubmitBuffer(buff_id,            \
                                                    std::move(buffer)); \
    }                                                                   \
  } while (0)

#define DBG_LOG_OPT(anno, option, format, ...)                            \
  do {                                                                    \
    if (viz::VizDebugger::IsEnabled()) {                                  \
      static viz::VizDebugger::StaticSource dcs(anno, __FILE__, __LINE__, \
                                                __func__);                \
      if (dcs.IsActive()) {                                               \
        viz::VizDebugger::GetInstance()->AddLogMessage(                   \
            base::StringPrintf(format, ##__VA_ARGS__), &dcs, option);     \
      }                                                                   \
    }                                                                     \
  } while (0)

#define DBG_CONNECTED_OR_TRACING(is_enabled) \
  is_enabled = VizDebugger::GetInstance()->IsEnabled();

#define DBG_FLAG_FBOOL(anno, fun_name)                                    \
  namespace {                                                             \
  bool fun_name() {                                                       \
    if (viz::VizDebugger::IsEnabled()) {                                  \
      static viz::VizDebugger::StaticSource dcs(anno, __FILE__, __LINE__, \
                                                __func__);                \
      if (dcs.IsEnabled()) {                                              \
        return true;                                                      \
      }                                                                   \
    }                                                                     \
    return false;                                                         \
  }                                                                       \
  }  // namespace

#else  //  !BUILDFLAG(USE_VIZ_DEBUGGER)

#define VIZ_DEBUGGER_IS_ON() false

// Viz Debugger is not enabled. The |VizDebugger| class is minimally defined to
// reduce the need for if/def checks in external code. All debugging macros
// compiled to empty statements but do eat some parameters to prevent used
// variable warnings.

namespace viz {
class VIZ_SERVICE_EXPORT VizDebugger {
 public:
  VizDebugger() = default;
  static inline VizDebugger* GetInstance() {
    static VizDebugger g_debugger;
    return &g_debugger;
  }

  // These structures are part of public API and must be included.
  struct VIZ_SERVICE_EXPORT BufferInfo {
    BufferInfo();
    ~BufferInfo();
    BufferInfo(const BufferInfo& a);
    SkBitmap bitmap;
  };

  struct DrawOption {
    uint8_t color_r;
    uint8_t color_g;
    uint8_t color_b;
    uint8_t color_a;
  };

  inline void CompleteFrame(uint64_t counter,
                            const gfx::Size& window_pix,
                            base::TimeTicks time_ticks) {}

  static inline bool IsEnabled() { return false; }
  VizDebugger(const VizDebugger&) = delete;
  VizDebugger& operator=(const VizDebugger&) = delete;
};

std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
DrawRectToTraceValue(const gfx::Vector2dF& pos,
                     const gfx::SizeF& size,
                     const std::string& text);

}  // namespace viz

#define VIZ_DEBUGGER_TRACING_CATEGORY "viz.visual_debugger"
#define DBG_OPT_RED 0

#define DBG_OPT_GREEN 0

#define DBG_OPT_BLUE 0

#define DBG_OPT_BLACK 0

#define DBG_DEFAULT_UV 0

#define DBG_VIZ_DEBUGGER_TRACE_IMPL(anno, pos, size, text)            \
  TRACE_EVENT_INSTANT1(                                               \
      TRACE_DISABLED_BY_DEFAULT(VIZ_DEBUGGER_TRACING_CATEGORY), anno, \
      TRACE_EVENT_FLAG_NONE, "args",                                  \
      viz::DrawRectToTraceValue(pos, size, text))

#define DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT(anno, option, pos, size, id, uv, \
                                            text)                            \
  std::ignore = option;                                                      \
  std::ignore = id;                                                          \
  std::ignore = uv;                                                          \
  DBG_VIZ_DEBUGGER_TRACE_IMPL(anno, pos, size, text)

#define DBG_COMPLETE_BUFFERS(buff_id, buffer) \
  std::ignore = buff_id;                      \
  std::ignore = buffer;

#define DBG_LOG_OPT(anno, option, format, ...) \
  std::ignore = anno;                          \
  std::ignore = option;                        \
  std::ignore = format;

#define DBG_CONNECTED_OR_TRACING(enabled)                             \
  TRACE_EVENT_CATEGORY_GROUP_ENABLED(      \
      TRACE_DISABLED_BY_DEFAULT(VIZ_DEBUGGER_TRACING_CATEGORY), (&enabled))

#define DBG_FLAG_FBOOL(anno, fun_name) \
  namespace {                          \
  constexpr bool fun_name() {          \
    return false;                      \
  }                                    \
  }

#endif  // BUILDFLAG(USE_VIZ_DEBUGGER)

// These are forwarding macro implementations common to the enabled/disabled
// compile paths
#define DBG_DRAW_RECTANGLE_OPT_BUFF_UV(anno, option, pos, size, id, uv) \
  DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT(anno, option, pos, size, id, uv, "")

#define DBG_DRAW_RECTANGLE_OPT(anno, option, pos, size) \
  DBG_DRAW_RECTANGLE_OPT_BUFF(anno, option, pos, size, nullptr)

#define DBG_DRAW_RECTANGLE(anno, pos, size) \
  DBG_DRAW_RECTANGLE_OPT_BUFF(anno, DBG_OPT_BLACK, pos, size, nullptr)

#define DBG_DRAW_RECTANGLE_OPT_BUFF(anno, option, pos, size, id) \
  DBG_DRAW_RECTANGLE_OPT_BUFF_UV(anno, option, pos, size, id, DBG_DEFAULT_UV)

#define DBG_DRAW_RECT_OPT_BUFF(anno, option, rect, id) \
  DBG_DRAW_RECT_OPT_BUFF_UV(anno, option, rect, id, DBG_DEFAULT_UV)

#define DBG_DRAW_RECT_OPT(anno, option, rect) \
  DBG_DRAW_RECT_OPT_BUFF(anno, option, rect, nullptr)

#define DBG_DRAW_RECT_BUFF(anno, rect, id) \
  DBG_DRAW_RECT_OPT_BUFF(anno, DBG_OPT_BLACK, rect, id)

#define DBG_DRAW_RECT_BUFF_UV(anno, rect, id, uv) \
  DBG_DRAW_RECT_OPT_BUFF_UV(anno, DBG_OPT_BLACK, rect, id, uv)

#define DBG_DRAW_RECT(anno, rect) DBG_DRAW_RECT_OPT(anno, DBG_OPT_BLACK, rect)

#define DBG_DRAW_TEXT_OPT(anno, option, pos, text)                        \
  DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT(                                    \
      anno, option, gfx::Vector2dF(pos.x(), pos.y()), gfx::SizeF(-1, -1), \
      nullptr, DBG_DEFAULT_UV, text)

#define DBG_DRAW_TEXT(anno, pos, text) \
  DBG_DRAW_TEXT_OPT(anno, DBG_OPT_BLACK, pos, text)

#define DBG_LOG(anno, format, ...) \
  DBG_LOG_OPT(anno, DBG_OPT_BLACK, format, ##__VA_ARGS__)

#define DBG_DRAW_RECT_OPT_BUFF_UV(anno, option, rect, id, uv)             \
  DBG_DRAW_RECTANGLE_OPT_BUFF_UV(                                         \
      anno, option, gfx::Vector2dF(rect.origin().x(), rect.origin().y()), \
      gfx::SizeF(rect.size()), id, uv)

#endif  // COMPONENTS_VIZ_SERVICE_DEBUGGER_VIZ_DEBUGGER_H_