1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_FLOW_INSTRUMENTATION_H_
6#define FLUTTER_FLOW_INSTRUMENTATION_H_
7
8#include <vector>
9
10#include "flutter/fml/macros.h"
11#include "flutter/fml/time/time_delta.h"
12#include "flutter/fml/time/time_point.h"
13#include "third_party/skia/include/core/SkCanvas.h"
14
15namespace flutter {
16
17class Stopwatch {
18 public:
19 Stopwatch(fml::Milliseconds frame_budget = fml::kDefaultFrameBudget);
20
21 ~Stopwatch();
22
23 const fml::TimeDelta& LastLap() const;
24
25 fml::TimeDelta CurrentLap() const { return fml::TimePoint::Now() - start_; }
26
27 fml::TimeDelta MaxDelta() const;
28
29 fml::TimeDelta AverageDelta() const;
30
31 void InitVisualizeSurface(const SkRect& rect) const;
32
33 void Visualize(SkCanvas* canvas, const SkRect& rect) const;
34
35 void Start();
36
37 void Stop();
38
39 void SetLapTime(const fml::TimeDelta& delta);
40
41 private:
42 inline double UnitFrameInterval(double time_ms) const;
43 inline double UnitHeight(double time_ms, double max_height) const;
44
45 fml::TimePoint start_;
46 std::vector<fml::TimeDelta> laps_;
47 size_t current_sample_;
48
49 fml::Milliseconds frame_budget_;
50
51 // Mutable data cache for performance optimization of the graphs. Prevents
52 // expensive redrawing of old data.
53 mutable bool cache_dirty_;
54 mutable sk_sp<SkSurface> visualize_cache_surface_;
55 mutable size_t prev_drawn_sample_index_;
56
57 FML_DISALLOW_COPY_AND_ASSIGN(Stopwatch);
58};
59
60class Counter {
61 public:
62 Counter() : count_(0) {}
63
64 size_t count() const { return count_; }
65
66 void Reset(size_t count = 0) { count_ = count; }
67
68 void Increment(size_t count = 1) { count_ += count; }
69
70 private:
71 size_t count_;
72
73 FML_DISALLOW_COPY_AND_ASSIGN(Counter);
74};
75
76class CounterValues {
77 public:
78 CounterValues();
79
80 ~CounterValues();
81
82 void Add(int64_t value);
83
84 void Visualize(SkCanvas* canvas, const SkRect& rect) const;
85
86 int64_t GetCurrentValue() const;
87
88 int64_t GetMaxValue() const;
89
90 int64_t GetMinValue() const;
91
92 private:
93 std::vector<int64_t> values_;
94 size_t current_sample_;
95
96 FML_DISALLOW_COPY_AND_ASSIGN(CounterValues);
97};
98
99} // namespace flutter
100
101#endif // FLUTTER_FLOW_INSTRUMENTATION_H_
102