summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/input/fling_scheduler.cc
blob: 0b6ab39313d835b3e3525c99484eb94ab558a52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/input/fling_scheduler.h"

#include "build/build_config.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "ui/compositor/compositor.h"

#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#endif

namespace content {

FlingScheduler::FlingScheduler(RenderWidgetHostImpl* host) : host_(host) {
  DCHECK(host);
}

FlingScheduler::~FlingScheduler() {
  if (observed_compositor_)
    observed_compositor_->RemoveAnimationObserver(this);
}

void FlingScheduler::ScheduleFlingProgress(
    base::WeakPtr<FlingController> fling_controller) {
  DCHECK(fling_controller);
  fling_controller_ = fling_controller;
  // Don't do anything if a ui::Compositor is already being observed.
  if (observed_compositor_)
    return;
  ui::Compositor* compositor = GetCompositor();
  // If a ui::Compositor can't be obtained, ask the host for BeginFrames.
  if (!compositor) {
    host_->SetNeedsBeginFrameForFlingProgress();
    return;
  }
  compositor->AddAnimationObserver(this);
  observed_compositor_ = compositor;
}

void FlingScheduler::DidStopFlingingOnBrowser(
    base::WeakPtr<FlingController> fling_controller) {
  DCHECK(fling_controller);
  if (observed_compositor_) {
    observed_compositor_->RemoveAnimationObserver(this);
    observed_compositor_ = nullptr;
  }
  fling_controller_ = nullptr;
  host_->DidStopFlinging();
}

void FlingScheduler::ProgressFlingOnBeginFrameIfneeded(
    base::TimeTicks current_time) {
  // No fling is active.
  if (!fling_controller_)
    return;

  // FlingProgress will be called within FlingController::OnAnimationStep.
  if (observed_compositor_)
    return;

  fling_controller_->ProgressFling(current_time);
}

ui::Compositor* FlingScheduler::GetCompositor() {
#if defined(USE_AURA)
  if (host_->GetView() && host_->GetView()->GetNativeView() &&
      host_->GetView()->GetNativeView()->GetHost() &&
      host_->GetView()->GetNativeView()->GetHost()->compositor()) {
    return host_->GetView()->GetNativeView()->GetHost()->compositor();
  }
#endif

  return nullptr;
}

void FlingScheduler::OnAnimationStep(base::TimeTicks timestamp) {
  if (fling_controller_)
    fling_controller_->ProgressFling(timestamp);
}

void FlingScheduler::OnCompositingShuttingDown(ui::Compositor* compositor) {
  DCHECK_EQ(observed_compositor_, compositor);
  observed_compositor_->RemoveAnimationObserver(this);
  observed_compositor_ = nullptr;
}

}  // namespace content