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 15 namespace flutter { 16 17 // DEPRECATED 18 // The frame per second FPS could be different than 60 (e.g., 120). 19 static const double kOneFrameMS = 1e3 / 60.0; 20 21 class Stopwatch { 22 public: 23 Stopwatch(); 24 25 ~Stopwatch(); 26 27 const fml::TimeDelta& LastLap() const; 28 CurrentLap()29 fml::TimeDelta CurrentLap() const { return fml::TimePoint::Now() - start_; } 30 31 fml::TimeDelta MaxDelta() const; 32 33 fml::TimeDelta AverageDelta() const; 34 35 void InitVisualizeSurface(const SkRect& rect) const; 36 37 void Visualize(SkCanvas& canvas, const SkRect& rect) const; 38 39 void Start(); 40 41 void Stop(); 42 43 void SetLapTime(const fml::TimeDelta& delta); 44 45 private: 46 fml::TimePoint start_; 47 std::vector<fml::TimeDelta> laps_; 48 size_t current_sample_; 49 // Mutable data cache for performance optimization of the graphs. Prevents 50 // expensive redrawing of old data. 51 mutable bool cache_dirty_; 52 mutable sk_sp<SkSurface> visualize_cache_surface_; 53 mutable size_t prev_drawn_sample_index_; 54 55 FML_DISALLOW_COPY_AND_ASSIGN(Stopwatch); 56 }; 57 58 class Counter { 59 public: Counter()60 Counter() : count_(0) {} 61 count()62 size_t count() const { return count_; } 63 64 void Reset(size_t count = 0) { count_ = count; } 65 66 void Increment(size_t count = 1) { count_ += count; } 67 68 private: 69 size_t count_; 70 71 FML_DISALLOW_COPY_AND_ASSIGN(Counter); 72 }; 73 74 class CounterValues { 75 public: 76 CounterValues(); 77 78 ~CounterValues(); 79 80 void Add(int64_t value); 81 82 void Visualize(SkCanvas& canvas, const SkRect& rect) const; 83 84 int64_t GetCurrentValue() const; 85 86 int64_t GetMaxValue() const; 87 88 int64_t GetMinValue() const; 89 90 private: 91 std::vector<int64_t> values_; 92 size_t current_sample_; 93 94 FML_DISALLOW_COPY_AND_ASSIGN(CounterValues); 95 }; 96 97 } // namespace flutter 98 99 #endif // FLUTTER_FLOW_INSTRUMENTATION_H_ 100