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
android_webview / browser / gfx / begin_frame_source_webview.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/gfx/begin_frame_source_webview.h"
#include "base/auto_reset.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/trace_event/trace_event.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "android_webview/browser_jni_headers/RootBeginFrameSourceWebView_jni.h"
namespace android_webview {
class BeginFrameSourceWebView::BeginFrameObserver
: public viz::BeginFrameObserver {
public:
BeginFrameObserver(BeginFrameSourceWebView* owner) : owner_(owner) {}
void OnBeginFrame(const viz::BeginFrameArgs& args) override {
last_used_begin_frame_args_ = args;
owner_->SendBeginFrame(args);
}
const viz::BeginFrameArgs& LastUsedBeginFrameArgs() const override {
return last_used_begin_frame_args_;
}
void OnBeginFrameSourcePausedChanged(bool paused) override {
owner_->OnSetBeginFrameSourcePaused(paused);
}
bool WantsAnimateOnlyBeginFrames() const override { return true; }
private:
const raw_ptr<BeginFrameSourceWebView> owner_;
viz::BeginFrameArgs last_used_begin_frame_args_;
};
BeginFrameSourceWebView::BeginFrameSourceClient::BeginFrameSourceClient(
android_webview::BeginFrameSourceWebView* owner)
: owner_(owner) {}
BeginFrameSourceWebView::BeginFrameSourceClient::~BeginFrameSourceClient() =
default;
void BeginFrameSourceWebView::BeginFrameSourceClient::OnNeedsBeginFrames(
bool needs_begin_frames) {
owner_->OnNeedsBeginFrames(needs_begin_frames);
}
BeginFrameSourceWebView::BeginFrameSourceWebView()
: ExternalBeginFrameSource(&bfs_client_),
bfs_client_(this),
parent_observer_(std::make_unique<BeginFrameObserver>(this)) {
OnSetBeginFrameSourcePaused(true);
}
BeginFrameSourceWebView::~BeginFrameSourceWebView() {
if (observed_begin_frame_source_ && !observers_.empty())
observed_begin_frame_source_->RemoveObserver(parent_observer_.get());
}
void BeginFrameSourceWebView::SetParentSource(BeginFrameSourceWebView* parent) {
parent_ = parent;
ObserveBeginFrameSource(parent);
}
void BeginFrameSourceWebView::ObserveBeginFrameSource(
viz::BeginFrameSource* begin_frame_source) {
if (observed_begin_frame_source_ == begin_frame_source)
return;
if (observed_begin_frame_source_ && !observers_.empty())
observed_begin_frame_source_->RemoveObserver(parent_observer_.get());
observed_begin_frame_source_ = begin_frame_source;
if (observed_begin_frame_source_) {
if (!observers_.empty())
observed_begin_frame_source_->AddObserver(parent_observer_.get());
} else {
OnSetBeginFrameSourcePaused(true);
};
}
void BeginFrameSourceWebView::OnNeedsBeginFrames(bool needs_begin_frames) {
if (needs_begin_frames) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("cc,benchmark", "NeedsBeginFrames", this);
if (observed_begin_frame_source_)
observed_begin_frame_source_->AddObserver(parent_observer_.get());
} else {
TRACE_EVENT_NESTABLE_ASYNC_END0("cc,benchmark", "NeedsBeginFrames", this);
if (observed_begin_frame_source_)
observed_begin_frame_source_->RemoveObserver(parent_observer_.get());
}
}
void BeginFrameSourceWebView::SendBeginFrame(const viz::BeginFrameArgs& args) {
DCHECK(!inside_begin_frame_);
base::AutoReset<bool> inside_bf(&inside_begin_frame_, true);
OnBeginFrame(args);
AfterBeginFrame();
}
void BeginFrameSourceWebView::AddBeginFrameCompletionCallback(
base::OnceClosure callback) {
DCHECK(parent_);
DCHECK(inside_begin_frame_);
parent_->AddBeginFrameCompletionCallback(std::move(callback));
}
const viz::BeginFrameArgs&
BeginFrameSourceWebView::LastDispatchedBeginFrameArgs() {
return parent_observer_->LastUsedBeginFrameArgs();
}
// static
RootBeginFrameSourceWebView* RootBeginFrameSourceWebView::GetInstance() {
static base::NoDestructor<RootBeginFrameSourceWebView> instance;
return instance.get();
}
RootBeginFrameSourceWebView::RootBeginFrameSourceWebView()
: begin_frame_source_(kNotRestartableId,
60.0f,
/*requires_align_with_java=*/true),
j_object_(Java_RootBeginFrameSourceWebView_Constructor(
jni_zero::AttachCurrentThread(),
reinterpret_cast<jlong>(this))) {
ObserveBeginFrameSource(&begin_frame_source_);
}
RootBeginFrameSourceWebView::~RootBeginFrameSourceWebView() = default;
void RootBeginFrameSourceWebView::OnUpdateRefreshRate(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
float refresh_rate) {
begin_frame_source_.UpdateRefreshRate(refresh_rate);
}
void RootBeginFrameSourceWebView::AfterBeginFrame() {
// ScopedClosureRunner runs callback in the destructor unless it's cancelled.
// So this will run all callbacks scheduled to run after BeginFrame completed.
after_begin_frame_callbacks_.clear();
}
void RootBeginFrameSourceWebView::AddBeginFrameCompletionCallback(
base::OnceClosure callback) {
DCHECK(inside_begin_frame());
after_begin_frame_callbacks_.emplace_back(std::move(callback));
}
} // namespace android_webview