15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef UI_COMPOSITOR_COMPOSITOR_VSYNC_MANAGER_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UI_COMPOSITOR_COMPOSITOR_VSYNC_MANAGER_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/ref_counted.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/observer_list_threadsafe.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/synchronization/lock.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/time.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/compositor/compositor_export.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace ui {
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This class manages vsync parameters for a compositor. It merges updates of
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the parameters from different sources and sends the merged updates to
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// observers which register to it. This class is explicitly synchronized and is
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// safe to use and update from any thread. Observers of the manager will be
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// notified on the thread they have registered from, and should be removed from
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the same thread.
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class COMPOSITOR_EXPORT CompositorVSyncManager
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : public base::RefCountedThreadSafe<CompositorVSyncManager> {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class Observer {
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   public:
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual void OnUpdateVSyncParameters(base::TimeTicks timebase,
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                         base::TimeDelta interval) = 0;
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CompositorVSyncManager();
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The "authoritative" vsync interval, if provided, will override |interval|
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // as reported by UpdateVSyncParameters() whenever it is called.  This is
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // typically the value reported by a more reliable source, e.g. the platform
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // display configuration.  In the particular case of ChromeOS -- this is the
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // value queried through XRandR, which is more reliable than the value
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // queried through the 3D context.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetAuthoritativeVSyncInterval(base::TimeDelta interval);
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The vsync parameters consist of |timebase|, which is the platform timestamp
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // of the last vsync, and |interval|, which is the interval between vsyncs.
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |interval| may be overriden by SetAuthoritativeVSyncInterval() above.
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void UpdateVSyncParameters(base::TimeTicks timebase,
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             base::TimeDelta interval);
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void AddObserver(Observer* observer);
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RemoveObserver(Observer* observer);
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend class base::RefCountedThreadSafe<CompositorVSyncManager>;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~CompositorVSyncManager();
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void NotifyObservers(base::TimeTicks timebase, base::TimeDelta interval);
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // List of observers.
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Protects the cached vsync parameters below.
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Lock vsync_parameters_lock_;
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::TimeTicks last_timebase_;
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::TimeDelta last_interval_;
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::TimeDelta authoritative_vsync_interval_;
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CompositorVSyncManager);
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace ui
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // UI_COMPOSITOR_COMPOSITOR_VSYNC_MANAGER_H_
72