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

cc / base / devtools_instrumentation.h [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_BASE_DEVTOOLS_INSTRUMENTATION_H_
#define CC_BASE_DEVTOOLS_INSTRUMENTATION_H_

#include <stdint.h>

#include <memory>
#include <utility>

#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "base/trace_event/typed_macros.h"
#include "cc/base/base_export.h"

namespace cc {
namespace devtools_instrumentation {

namespace internal {
struct CC_BASE_EXPORT CategoryName {
  // Put these strings into a struct to allow external linkage.
  static constexpr const char kTimeline[] =
      TRACE_DISABLED_BY_DEFAULT("devtools.timeline");
  static constexpr const char kTimelineFrame[] =
      TRACE_DISABLED_BY_DEFAULT("devtools.timeline.frame");
};

CC_BASE_EXPORT extern const char kData[];
CC_BASE_EXPORT extern const char kFrameId[];
CC_BASE_EXPORT extern const char kLayerId[];
CC_BASE_EXPORT extern const char kLayerTreeId[];
CC_BASE_EXPORT extern const char kPixelRefId[];
CC_BASE_EXPORT extern const char kFrameSequenceNumber[];
CC_BASE_EXPORT extern const char kHasPartialUpdate[];

CC_BASE_EXPORT extern const char kImageDecodeTask[];
CC_BASE_EXPORT extern const char kBeginFrame[];
CC_BASE_EXPORT extern const char kNeedsBeginFrameChanged[];
CC_BASE_EXPORT extern const char kActivateLayerTree[];
CC_BASE_EXPORT extern const char kRequestMainThreadFrame[];
CC_BASE_EXPORT extern const char kDroppedFrame[];
CC_BASE_EXPORT extern const char kBeginMainThreadFrame[];
CC_BASE_EXPORT extern const char kDrawFrame[];
CC_BASE_EXPORT extern const char kCommit[];
}  // namespace internal

extern const char kPaintSetup[];
CC_BASE_EXPORT extern const char kUpdateLayer[];

class CC_BASE_EXPORT ScopedLayerTask {
 public:
  ScopedLayerTask(const char* event_name, int layer_id)
      : event_name_(event_name) {
    TRACE_EVENT_BEGIN1(internal::CategoryName::kTimeline, event_name_,
                       internal::kLayerId, layer_id);
  }
  ScopedLayerTask(const ScopedLayerTask&) = delete;
  ~ScopedLayerTask() {
    TRACE_EVENT_END0(internal::CategoryName::kTimeline, event_name_);
  }

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

 private:
  const char* event_name_;
};

class CC_BASE_EXPORT ScopedImageTask {
 public:
  enum class ImageType { kAvif, kBmp, kGif, kIco, kJpeg, kPng, kWebP, kOther };

  explicit ScopedImageTask(ImageType image_type)
      : image_type_(image_type), start_time_(base::TimeTicks::Now()) {}
  ScopedImageTask(const ScopedImageTask&) = delete;
  ~ScopedImageTask() = default;
  ScopedImageTask& operator=(const ScopedImageTask&) = delete;

  // Prevents logging duration metrics. Used in cases where a task performed
  // uninteresting work or was terminated early.
  void SuppressMetrics() { suppress_metrics_ = true; }

 protected:
  bool suppress_metrics_ = false;
  const ImageType image_type_;
  const base::TimeTicks start_time_;

  // UMA histogram parameters
  const uint32_t bucket_count_ = 50;
  base::TimeDelta hist_min_ = base::Microseconds(1);
  base::TimeDelta hist_max_ = base::Milliseconds(1000);
};

class CC_BASE_EXPORT ScopedImageUploadTask : public ScopedImageTask {
 public:
  ScopedImageUploadTask(const void* image_ptr, ImageType image_type);
  ScopedImageUploadTask(const ScopedImageUploadTask&) = delete;
  ~ScopedImageUploadTask();

  ScopedImageUploadTask& operator=(const ScopedImageUploadTask&) = delete;
};

class CC_BASE_EXPORT ScopedImageDecodeTask : public ScopedImageTask {
 public:
  enum class TaskType { kInRaster, kOutOfRaster };
  enum class DecodeType { kSoftware, kGpu };

  ScopedImageDecodeTask(const void* image_ptr,
                        DecodeType decode_type,
                        TaskType task_type,
                        ImageType image_type);
  ScopedImageDecodeTask(const ScopedImageDecodeTask&) = delete;
  ~ScopedImageDecodeTask();

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

 private:
  const DecodeType decode_type_;
  const TaskType task_type_;
};

class CC_BASE_EXPORT ScopedLayerTreeTask {
 public:
  ScopedLayerTreeTask(const char* event_name,
                      int layer_id,
                      int layer_tree_host_id)
      : event_name_(event_name) {
    TRACE_EVENT_BEGIN2(internal::CategoryName::kTimeline, event_name_,
                       internal::kLayerId, layer_id, internal::kLayerTreeId,
                       layer_tree_host_id);
  }
  ScopedLayerTreeTask(const ScopedLayerTreeTask&) = delete;
  ~ScopedLayerTreeTask() {
    TRACE_EVENT_END0(internal::CategoryName::kTimeline, event_name_);
  }

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

 private:
  const char* event_name_;
};

struct CC_BASE_EXPORT ScopedCommitTrace {
 public:
  explicit ScopedCommitTrace(int layer_tree_host_id, uint64_t sequence_number) {
    TRACE_EVENT_BEGIN2(internal::CategoryName::kTimeline, internal::kCommit,
                       internal::kLayerTreeId, layer_tree_host_id,
                       internal::kFrameSequenceNumber, sequence_number);
  }
  ScopedCommitTrace(const ScopedCommitTrace&) = delete;
  ~ScopedCommitTrace() {
    TRACE_EVENT_END0(internal::CategoryName::kTimeline, internal::kCommit);
  }

  ScopedCommitTrace& operator=(const ScopedCommitTrace&) = delete;
};

struct CC_BASE_EXPORT ScopedLayerObjectTracker
    : public base::trace_event::
          TraceScopedTrackableObject<int, internal::CategoryName::kTimeline> {
  explicit ScopedLayerObjectTracker(int layer_id)
      : base::trace_event::
            TraceScopedTrackableObject<int, internal::CategoryName::kTimeline>(
                internal::kLayerId,
                layer_id) {}
  ScopedLayerObjectTracker(const ScopedLayerObjectTracker&) = delete;
  ScopedLayerObjectTracker& operator=(const ScopedLayerObjectTracker&) = delete;
};

inline void CC_BASE_EXPORT DidActivateLayerTree(int layer_tree_host_id,
                                                int frame_id) {
  TRACE_EVENT_INSTANT2(internal::CategoryName::kTimelineFrame,
                       internal::kActivateLayerTree, TRACE_EVENT_SCOPE_THREAD,
                       internal::kLayerTreeId, layer_tree_host_id,
                       internal::kFrameId, frame_id);
}

inline void CC_BASE_EXPORT DidBeginFrame(int layer_tree_host_id,
                                         base::TimeTicks begin_frame_timestamp,
                                         uint64_t sequence_number) {
  TRACE_EVENT_INSTANT(internal::CategoryName::kTimelineFrame,
                      perfetto::StaticString(internal::kBeginFrame),
                      begin_frame_timestamp, internal::kLayerTreeId,
                      layer_tree_host_id, internal::kFrameSequenceNumber,
                      sequence_number);
}

inline void CC_BASE_EXPORT DidDrawFrame(int layer_tree_host_id,
                                        uint64_t sequence_number) {
  TRACE_EVENT_INSTANT2(internal::CategoryName::kTimelineFrame,
                       internal::kDrawFrame, TRACE_EVENT_SCOPE_THREAD,
                       internal::kLayerTreeId, layer_tree_host_id,
                       internal::kFrameSequenceNumber, sequence_number);
}

inline void CC_BASE_EXPORT DidRequestMainThreadFrame(int layer_tree_host_id) {
  TRACE_EVENT_INSTANT1(
      internal::CategoryName::kTimelineFrame, internal::kRequestMainThreadFrame,
      TRACE_EVENT_SCOPE_THREAD, internal::kLayerTreeId, layer_tree_host_id);
}

inline void CC_BASE_EXPORT
DidDropSmoothnessFrame(int layer_tree_host_id,
                       base::TimeTicks dropped_frame_timestamp,
                       uint64_t sequence_number,
                       bool has_partial_update) {
  TRACE_EVENT_INSTANT(internal::CategoryName::kTimelineFrame,
                      perfetto::StaticString(internal::kDroppedFrame),
                      dropped_frame_timestamp, internal::kLayerTreeId,
                      layer_tree_host_id, internal::kFrameSequenceNumber,
                      sequence_number, internal::kHasPartialUpdate,
                      has_partial_update);
}

inline std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
BeginMainThreadFrameData(int frame_id) {
  std::unique_ptr<base::trace_event::TracedValue> value(
      new base::trace_event::TracedValue());
  value->SetInteger("frameId", frame_id);
  return std::move(value);
}

inline void CC_BASE_EXPORT WillBeginMainThreadFrame(int layer_tree_host_id,
                                                    int frame_id) {
  TRACE_EVENT_INSTANT2(
      internal::CategoryName::kTimelineFrame, internal::kBeginMainThreadFrame,
      TRACE_EVENT_SCOPE_THREAD, internal::kLayerTreeId, layer_tree_host_id,
      internal::kData, BeginMainThreadFrameData(frame_id));
}

inline std::unique_ptr<base::trace_event::ConvertableToTraceFormat>
NeedsBeginFrameData(bool needs_begin_frame) {
  std::unique_ptr<base::trace_event::TracedValue> value(
      new base::trace_event::TracedValue());
  value->SetInteger("needsBeginFrame", needs_begin_frame);
  return std::move(value);
}

inline void CC_BASE_EXPORT NeedsBeginFrameChanged(int layer_tree_host_id,
                                                  bool new_value) {
  TRACE_EVENT_INSTANT2(
      internal::CategoryName::kTimelineFrame, internal::kNeedsBeginFrameChanged,
      TRACE_EVENT_SCOPE_THREAD, internal::kLayerTreeId, layer_tree_host_id,
      internal::kData, NeedsBeginFrameData(new_value));
}

}  // namespace devtools_instrumentation
}  // namespace cc

#endif  // CC_BASE_DEVTOOLS_INSTRUMENTATION_H_