chromium/components/viz/service/debugger/viz_debugger.h

// 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/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()

namespace viz {

class VIZ_SERVICE_EXPORT VizDebugger {};

}  // namespace viz

#define DBG_OPT_RED
#define DBG_OPT_GREEN
#define DBG_OPT_BLUE
#define DBG_OPT_BLACK
#define DBG_DEFAULT_UV

#define DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT(anno, option, pos, size, id, uv,   \
                                            text)

#define DBG_COMPLETE_BUFFERS(buff_id, buffer)

#define DBG_LOG_OPT(anno, option, format, ...)

#define DBG_CONNECTED_OR_TRACING(is_enabled)

#define DBG_FLAG_FBOOL(anno, fun_name)

#else  //  !BUILDFLAG(USE_VIZ_DEBUGGER)

#define VIZ_DEBUGGER_IS_ON

// 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
#define DBG_OPT_RED

#define DBG_OPT_GREEN

#define DBG_OPT_BLUE

#define DBG_OPT_BLACK

#define DBG_DEFAULT_UV

#define DBG_VIZ_DEBUGGER_TRACE_IMPL

#define DBG_DRAW_RECTANGLE_OPT_BUFF_UV_TEXT

#define DBG_COMPLETE_BUFFERS

#define DBG_LOG_OPT

#define DBG_CONNECTED_OR_TRACING

#define DBG_FLAG_FBOOL

#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)

#define DBG_DRAW_RECTANGLE_OPT(anno, option, pos, size)

#define DBG_DRAW_RECTANGLE(anno, pos, size)

#define DBG_DRAW_RECTANGLE_OPT_BUFF(anno, option, pos, size, id)

#define DBG_DRAW_RECT_OPT_BUFF(anno, option, rect, id)

#define DBG_DRAW_RECT_OPT(anno, option, rect)

#define DBG_DRAW_RECT_BUFF(anno, rect, id)

#define DBG_DRAW_RECT_BUFF_UV(anno, rect, id, uv)

#define DBG_DRAW_RECT(anno, rect)

#define DBG_DRAW_TEXT_OPT(anno, option, pos, text)

#define DBG_DRAW_TEXT(anno, pos, text)

#define DBG_LOG(anno, format, ...)

#define DBG_DRAW_RECT_OPT_BUFF_UV(anno, option, rect, id, uv)

#endif  // COMPONENTS_VIZ_SERVICE_DEBUGGER_VIZ_DEBUGGER_H_