blob: a8148d00862f6e8c03c2387185df251947ac7ed3 [file] [log] [blame]
kylechar5361171352019-04-26 18:23:121// 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#include "components/exo/vsync_timing_manager.h"
6
7#include "base/stl_util.h"
8#include "base/threading/thread_task_runner_handle.h"
9
10namespace exo {
11
12VSyncTimingManager::VSyncTimingManager(Delegate* delegate)
13 : delegate_(delegate) {}
14
15VSyncTimingManager::~VSyncTimingManager() = default;
16
17void VSyncTimingManager::AddObserver(Observer* obs) {
18 DCHECK(obs);
19
20 // This is adding the first observer so start receiving IPCs.
21 if (observers_.empty())
22 InitializeConnection();
23
24 observers_.push_back(obs);
25}
26
27void VSyncTimingManager::RemoveObserver(Observer* obs) {
28 DCHECK(obs);
29
30 base::Erase(observers_, obs);
31
32 // There are no more observers so stop receiving IPCs.
33 if (observers_.empty())
Mario Sanchez Prada90d2f462019-10-28 20:36:3534 receiver_.reset();
kylechar5361171352019-04-26 18:23:1235}
36
37void VSyncTimingManager::OnUpdateVSyncParameters(base::TimeTicks timebase,
38 base::TimeDelta interval) {
39 for (auto* observer : observers_)
40 observer->OnUpdateVSyncParameters(timebase, interval);
41}
42
43void VSyncTimingManager::InitializeConnection() {
Mario Sanchez Prada90d2f462019-10-28 20:36:3544 mojo::PendingRemote<viz::mojom::VSyncParameterObserver> remote =
45 receiver_.BindNewPipeAndPassRemote();
kylechar5361171352019-04-26 18:23:1246
Mario Sanchez Prada90d2f462019-10-28 20:36:3547 // Unretained is safe because |this| owns |receiver_| and will outlive it.
48 receiver_.set_disconnect_handler(base::BindOnce(
kylechar5361171352019-04-26 18:23:1249 &VSyncTimingManager::OnConnectionError, base::Unretained(this)));
50
Mario Sanchez Prada90d2f462019-10-28 20:36:3551 delegate_->AddVSyncParameterObserver(std::move(remote));
kylechar5361171352019-04-26 18:23:1252}
53
54void VSyncTimingManager::MaybeInitializeConnection() {
55 // The last observer might have been unregistered between when there was a
56 // connection error, in which case we don't need to reconnect. Alternatively,
57 // the last observer might have been unregistered and then a new observer
58 // registered, in which case we already reconnected.
Mario Sanchez Prada90d2f462019-10-28 20:36:3559 if (!observers_.empty() || receiver_.is_bound())
kylechar5361171352019-04-26 18:23:1260 InitializeConnection();
61}
62
63void VSyncTimingManager::OnConnectionError() {
Mario Sanchez Prada90d2f462019-10-28 20:36:3564 receiver_.reset();
kylechar5361171352019-04-26 18:23:1265
66 // Try to add a new observer after a short delay. If adding a new observer
67 // fails we'll retry again until successful. The delay avoids spamming
68 // retries.
69 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
70 FROM_HERE,
71 base::BindOnce(&VSyncTimingManager::MaybeInitializeConnection,
72 weak_ptr_factory_.GetWeakPtr()),
73 base::TimeDelta::FromMilliseconds(250));
74}
75
76} // namespace exo