begin_frame_tracker.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2014 The Chromium 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. #ifndef CC_SCHEDULER_BEGIN_FRAME_TRACKER_H_
  5. #define CC_SCHEDULER_BEGIN_FRAME_TRACKER_H_
  6. #include <set>
  7. #include <string>
  8. #include "base/time/time.h"
  9. #include "cc/cc_export.h"
  10. #include "components/viz/common/frame_sinks/begin_frame_args.h"
  11. namespace perfetto {
  12. class EventContext;
  13. namespace protos {
  14. namespace pbzero {
  15. class BeginImplFrameArgs;
  16. }
  17. } // namespace protos
  18. } // namespace perfetto
  19. namespace cc {
  20. // Microclass to trace and check properties for correct BeginFrameArgs (BFA)
  21. // usage and provide a few helper methods.
  22. //
  23. // With DCHECKs enable, this class checks the following "invariants";
  24. // * BFA are monotonically increasing.
  25. // * BFA is valid.
  26. // * The BFA is only used inside a given period.
  27. // * A new BFA isn't used before the last BFA is finished with.
  28. //
  29. // With the tracing category "cc.debug.scheduler.frames" enabled the tracker
  30. // will output the following trace information;
  31. // * Time period for which the BFA is in usage.
  32. // * The flow of BFA as they are passed between tracking objects.
  33. //
  34. // TODO(mithro): Record stats about the viz::BeginFrameArgs
  35. class CC_EXPORT BeginFrameTracker {
  36. public:
  37. explicit BeginFrameTracker(const base::Location& location);
  38. ~BeginFrameTracker();
  39. // The Start and Finish methods manage the period that a BFA should be
  40. // accessed for. This allows tight control over the BFA and prevents
  41. // accidental usage in the wrong period when code is split across multiple
  42. // locations.
  43. // Start using a new BFA value and check invariant properties.
  44. // **Must** only be called after finishing with any previous BFA.
  45. void Start(const viz::BeginFrameArgs& new_args);
  46. // Finish using the current BFA.
  47. // **Must** only be called while still using a BFA.
  48. void Finish();
  49. // The two accessors methods allow access to the BFA stored inside the
  50. // tracker. They are mutually exclusive, at any time it is only valid to call
  51. // one or the other. This makes sure you understand exactly which BFA you are
  52. // intending to use and verifies that is the case.
  53. // Get the current BFA object.
  54. // **Must** only be called between the start and finish methods calls.
  55. const viz::BeginFrameArgs& Current() const;
  56. // Get the last used BFA.
  57. // **Must** only be called when **not** between the start and finish method
  58. // calls.
  59. const viz::BeginFrameArgs& Last() const;
  60. // Helper method to try and return a valid interval property. Defaults to
  61. // BFA::DefaultInterval() is no other interval can be found. Can be called at
  62. // any time.
  63. base::TimeDelta Interval() const;
  64. void AsProtozeroInto(
  65. perfetto::EventContext& ctx,
  66. base::TimeTicks now,
  67. perfetto::protos::pbzero::BeginImplFrameArgs* dict) const;
  68. // The following methods violate principles of how viz::BeginFrameArgs should
  69. // be used. These methods should only be used when there is no other choice.
  70. bool DangerousMethodHasStarted() const {
  71. return !current_updated_at_.is_null();
  72. }
  73. bool DangerousMethodHasFinished() const { return HasFinished(); }
  74. const viz::BeginFrameArgs& DangerousMethodCurrentOrLast() const;
  75. private:
  76. // Return if currently not between the start/end period. This method should
  77. // be used extremely sparingly and normal indicates incorrect management of
  78. // the BFA object. Can be called at any time.
  79. bool HasFinished() const { return !current_finished_at_.is_null(); }
  80. const base::Location location_;
  81. const std::string location_string_;
  82. base::TimeTicks current_updated_at_;
  83. viz::BeginFrameArgs current_args_;
  84. base::TimeTicks current_finished_at_;
  85. };
  86. } // namespace cc
  87. #endif // CC_SCHEDULER_BEGIN_FRAME_TRACKER_H_