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
android_webview / browser / gfx / test / rendering_test.cc [blame]
// Copyright 2014 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/test/rendering_test.h"
#include <memory>
#include <utility>
#include "android_webview/browser/gfx/browser_view_renderer.h"
#include "android_webview/browser/gfx/child_frame.h"
#include "android_webview/browser/gfx/gpu_service_webview.h"
#include "android_webview/browser/gfx/render_thread_manager.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/surfaces/frame_sink_id.h"
#include "components/viz/test/compositor_frame_helpers.h"
#include "content/public/browser/android/synchronous_compositor.h"
#include "content/public/test/test_synchronous_compositor_android.h"
namespace android_webview {
namespace {
// BrowserViewRenderer subclass used for enabling tests to observe
// OnParentDrawDataUpdated.
class TestBrowserViewRenderer : public BrowserViewRenderer {
public:
TestBrowserViewRenderer(
RenderingTest* rendering_test,
const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
: BrowserViewRenderer(rendering_test, ui_task_runner, io_task_runner),
rendering_test_(rendering_test) {}
~TestBrowserViewRenderer() override {}
void OnParentDrawDataUpdated(
CompositorFrameConsumer* compositor_frame_consumer) override {
BrowserViewRenderer::OnParentDrawDataUpdated(compositor_frame_consumer);
rendering_test_->OnParentDrawDataUpdated();
}
private:
const raw_ptr<RenderingTest> rendering_test_;
};
} // namespace
RenderingTest::RenderingTest()
: task_environment_(std::make_unique<base::test::TaskEnvironment>()) {
ui_task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
android_webview::GpuServiceWebView::GetInstance();
}
RenderingTest::~RenderingTest() {
DCHECK(ui_task_runner_->BelongsToCurrentThread());
if (window_.get())
window_->Detach();
}
ui::TouchHandleDrawable* RenderingTest::CreateDrawable() {
return nullptr;
}
void RenderingTest::SetUpTestHarness() {
DCHECK(!browser_view_renderer_.get());
DCHECK(!functor_.get());
browser_view_renderer_ = std::make_unique<TestBrowserViewRenderer>(
this, base::SingleThreadTaskRunner::GetCurrentDefault(),
base::SingleThreadTaskRunner::GetCurrentDefault());
browser_view_renderer_->SetActiveFrameSinkId(viz::FrameSinkId(1, 0));
browser_view_renderer_->SetDipScale(1.0f);
InitializeCompositor();
std::unique_ptr<FakeWindow> window(
new FakeWindow(browser_view_renderer_.get(), this, gfx::Rect(100, 100)));
functor_ = std::make_unique<FakeFunctor>();
functor_->Init(window.get(),
std::make_unique<RenderThreadManager>(
base::SingleThreadTaskRunner::GetCurrentDefault()));
browser_view_renderer_->SetCurrentCompositorFrameConsumer(
functor_->GetCompositorFrameConsumer());
window_ = std::move(window);
}
CompositorFrameConsumer* RenderingTest::GetCompositorFrameConsumer() {
return functor_->GetCompositorFrameConsumer();
}
CompositorFrameProducer* RenderingTest::GetCompositorFrameProducer() {
return browser_view_renderer_.get();
}
void RenderingTest::InitializeCompositor() {
DCHECK(!compositor_.get());
DCHECK(browser_view_renderer_.get());
compositor_ = std::make_unique<content::TestSynchronousCompositor>(
viz::FrameSinkId(1, 0));
compositor_->SetClient(browser_view_renderer_.get());
}
void RenderingTest::RunTest() {
SetUpTestHarness();
ui_task_runner_->PostTask(FROM_HERE, base::BindOnce(&RenderingTest::StartTest,
base::Unretained(this)));
run_loop_.Run();
}
void RenderingTest::StartTest() {
EndTest();
}
void RenderingTest::EndTest() {
run_loop_.QuitWhenIdle();
}
content::SynchronousCompositor* RenderingTest::ActiveCompositor() const {
return browser_view_renderer_->GetActiveCompositorForTesting();
}
std::unique_ptr<viz::CompositorFrame> RenderingTest::ConstructEmptyFrame() {
gfx::Rect viewport(browser_view_renderer_->size());
return std::make_unique<viz::CompositorFrame>(
viz::CompositorFrameBuilder()
.AddRenderPass(viewport, viewport)
.SetDeviceScaleFactor(1.0f)
.Build());
}
std::unique_ptr<viz::CompositorFrame> RenderingTest::ConstructFrame(
viz::ResourceId resource_id) {
std::unique_ptr<viz::CompositorFrame> compositor_frame(ConstructEmptyFrame());
viz::TransferableResource resource;
resource.id = resource_id;
compositor_frame->resource_list.push_back(resource);
return compositor_frame;
}
FakeFunctor* RenderingTest::GetFunctor() {
return functor_.get();
}
void RenderingTest::WillOnDraw() {
DCHECK(compositor_);
compositor_->SetHardwareFrame(0u, ConstructEmptyFrame());
}
bool RenderingTest::WillDrawOnRT(HardwareRendererDrawParams* params) {
params->width = window_->surface_size().width();
params->height = window_->surface_size().height();
gfx::Transform transform;
transform.GetColMajorF(params->transform);
return true;
}
void RenderingTest::OnNewPicture() {}
void RenderingTest::PostInvalidate(bool inside_vsync) {
if (window_)
window_->PostInvalidate();
}
gfx::Point RenderingTest::GetLocationOnScreen() {
return gfx::Point();
}
} // namespace android_webview