1 // Copyright 2019 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 
5 #ifndef COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_VSYNC_PARAMETER_LISTENER_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_VSYNC_PARAMETER_LISTENER_H_
7 
8 #include "base/macros.h"
9 #include "base/time/time.h"
10 #include "components/viz/service/viz_service_export.h"
11 #include "mojo/public/cpp/bindings/pending_remote.h"
12 #include "mojo/public/cpp/bindings/remote.h"
13 #include "services/viz/privileged/mojom/compositing/vsync_parameter_observer.mojom.h"
14 
15 namespace viz {
16 
17 // Sends updated vsync parameters over IPC when either the interval changes or
18 // the timebase offset skews more than kMaxTimebaseSkew. Timebase skew for last
19 // timebase sent to the observer T1 and a new timebase T2 is defined as
20 // difference between T1 + N * interval and T2 + M * interval for N and M that
21 // produce the smallest difference.
22 class VIZ_SERVICE_EXPORT VSyncParameterListener {
23  public:
24   explicit VSyncParameterListener(
25       mojo::PendingRemote<mojom::VSyncParameterObserver> observer);
26   ~VSyncParameterListener();
27 
28   void OnVSyncParametersUpdated(base::TimeTicks timebase,
29                                 base::TimeDelta interval);
30 
31  private:
32   friend class VSyncParameterListenerTestRunner;
33 
34   static constexpr base::TimeDelta kMaxTimebaseSkew =
35       base::TimeDelta::FromMicroseconds(25);
36 
37   bool ShouldSendUpdate(base::TimeTicks timebase, base::TimeDelta interval);
38 
39   mojo::Remote<mojom::VSyncParameterObserver> observer_;
40 
41   base::TimeDelta last_interval_;
42   base::TimeDelta last_offset_;
43 
44   DISALLOW_COPY_AND_ASSIGN(VSyncParameterListener);
45 };
46 
47 }  // namespace viz
48 
49 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_VSYNC_PARAMETER_LISTENER_H_
50