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
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176

content / browser / renderer_host / input / fling_scheduler_android.cc [blame]

// Copyright 2018 The Chromium Authors
// 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_android.h"

#include "base/feature_list.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_android.h"
#include "content/public/common/content_features.h"
#include "ui/android/view_android.h"

namespace content {

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

FlingSchedulerAndroid::~FlingSchedulerAndroid() {
  RemoveCompositorTick();
}

void FlingSchedulerAndroid::ScheduleFlingProgress(
    base::WeakPtr<input::FlingController> fling_controller) {
  DCHECK(fling_controller);
  fling_controller_ = fling_controller;
  if (observed_compositor_)
    return;

  ui::WindowAndroid* window = GetRootWindow();
  if (!window)
    return;

  // If the root window does not have a Compositor (happens on Android
  // WebView), we'll never receive an OnAnimate call. In this case fall back
  // to BeginFrames coming from the host.
  if (!window->GetCompositor()) {
    auto* view = host_->GetView();
    if (view && !view->IsRenderWidgetHostViewChildFrame()) {
      static_cast<RenderWidgetHostViewAndroid*>(view)
          ->SetNeedsBeginFrameForFlingProgress();
    }
    return;
  }

  RequestCompositorTick();
}

void FlingSchedulerAndroid::DidStopFlingingOnBrowser(
    base::WeakPtr<input::FlingController> fling_controller) {
  DCHECK(fling_controller);
  RemoveCompositorTick();
  fling_controller_ = nullptr;
  host_->GetRenderInputRouter()->DidStopFlinging();
}

bool FlingSchedulerAndroid::NeedsBeginFrameForFlingProgress() {
  ui::WindowAndroid* window = GetRootWindow();
  // If the root window does not have a Compositor (happens on Android
  // WebView), we'll never receive an OnAnimate call. In this case fall back
  // to BeginFrames coming from the host.
  return !window || !window->GetCompositor();
}

bool FlingSchedulerAndroid::ShouldUseMobileFlingCurve() {
  return true;
}
gfx::Vector2dF FlingSchedulerAndroid::GetPixelsPerInch(
    const gfx::PointF& position_in_screen) {
  return gfx::Vector2dF(input::kDefaultPixelsPerInch,
                        input::kDefaultPixelsPerInch);
}

void FlingSchedulerAndroid::ProgressFlingOnBeginFrameIfneeded(
    base::TimeTicks current_time) {
  // If a WindowAndroid is being observed, there is no need for BeginFrames
  // coming from the host.
  if (observed_compositor_)
    return;
  if (!fling_controller_)
    return;
  fling_controller_->ProgressFling(current_time);
}

ui::WindowAndroid* FlingSchedulerAndroid::GetRootWindow() {
  if (!host_->GetView() || !host_->GetView()->GetNativeView())
    return nullptr;
  return host_->GetView()->GetNativeView()->GetWindowAndroid();
}

void FlingSchedulerAndroid::RequestCompositorTick() {
  if (!fling_controller_)
    return;

  if (observed_compositor_)
    return;

  if (!observed_view_ && host_->GetView()) {
    if (ui::ViewAndroid* native_view = host_->GetView()->GetNativeView()) {
      native_view->AddObserver(this);
      observed_view_ = native_view;
    }
  }

  ui::WindowAndroid* window = GetRootWindow();
  if (!window)
    return;

  if (!observed_window_) {
    window->AddObserver(this);
    observed_window_ = window;
  }

  CompositorImpl* compositor =
      static_cast<CompositorImpl*>(window->GetCompositor());
  if (!compositor)
    return;

  compositor->AddSimpleBeginFrameObserver(this);
  observed_compositor_ = compositor;
}

void FlingSchedulerAndroid::RemoveCompositorTick() {
  if (observed_view_) {
    observed_view_->RemoveObserver(this);
    observed_view_ = nullptr;
  }

  if (observed_window_) {
    observed_window_->RemoveObserver(this);
    observed_window_ = nullptr;
  }

  if (!observed_compositor_)
    return;
  observed_compositor_->RemoveSimpleBeginFrameObserver(this);
  observed_compositor_ = nullptr;
}

void FlingSchedulerAndroid::OnAttachCompositor() {
  RequestCompositorTick();
}

void FlingSchedulerAndroid::OnDetachCompositor() {
  RemoveCompositorTick();
}

void FlingSchedulerAndroid::OnAttachedToWindow() {
  RequestCompositorTick();
}

void FlingSchedulerAndroid::OnDetachedFromWindow() {
  RemoveCompositorTick();
}

void FlingSchedulerAndroid::OnViewAndroidDestroyed() {
  RemoveCompositorTick();
}

void FlingSchedulerAndroid::OnBeginFrame(base::TimeTicks frame_begin_time,
                                         base::TimeDelta frame_interval) {
  DCHECK(observed_compositor_);
  if (fling_controller_)
    fling_controller_->ProgressFling(frame_begin_time);
}

void FlingSchedulerAndroid::OnBeginFrameSourceShuttingDown() {
  if (observed_compositor_) {
    observed_compositor_->RemoveSimpleBeginFrameObserver(this);
    observed_compositor_ = nullptr;
  }
}

}  // namespace content