1 // Copyright 2015 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.
5 #include "base/run_loop.h"
6 #include "base/single_thread_task_runner.h"
7 #include "cc/test/fake_output_surface_client.h"
8 #include "cc/test/test_context_provider.h"
9 #include "cc/test/test_web_graphics_context_3d.h"
10 #include "content/browser/compositor/browser_compositor_output_surface.h"
11 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator.h"
12 #include "content/browser/compositor/reflector_impl.h"
13 #include "content/browser/compositor/reflector_texture.h"
14 #include "content/browser/compositor/test/no_transport_image_transport_factory.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/test/context_factories_for_test.h"
20 #if defined(USE_OZONE)
21 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator_ozone.h"
22 #include "ui/ozone/public/overlay_candidates_ozone.h"
23 #endif // defined(USE_OZONE)
27 class FakeTaskRunner
: public base::SingleThreadTaskRunner
{
31 bool PostNonNestableDelayedTask(const tracked_objects::Location
& from_here
,
32 const base::Closure
& task
,
33 base::TimeDelta delay
) override
{
36 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
37 const base::Closure
& task
,
38 base::TimeDelta delay
) override
{
41 bool RunsTasksOnCurrentThread() const override
{ return true; }
44 ~FakeTaskRunner() override
{}
47 #if defined(USE_OZONE)
48 class TestOverlayCandidatesOzone
: public ui::OverlayCandidatesOzone
{
50 TestOverlayCandidatesOzone() {}
51 ~TestOverlayCandidatesOzone() override
{}
53 void CheckOverlaySupport(OverlaySurfaceCandidateList
* surfaces
) override
{
54 (*surfaces
)[0].overlay_handled
= true;
57 #endif // defined(USE_OZONE)
59 scoped_ptr
<BrowserCompositorOverlayCandidateValidator
>
60 CreateTestValidatorOzone() {
61 #if defined(USE_OZONE)
62 return scoped_ptr
<BrowserCompositorOverlayCandidateValidator
>(
63 new BrowserCompositorOverlayCandidateValidatorOzone(
64 0, scoped_ptr
<ui::OverlayCandidatesOzone
>(
65 new TestOverlayCandidatesOzone())));
68 #endif // defined(USE_OZONE)
71 class TestOutputSurface
: public BrowserCompositorOutputSurface
{
74 const scoped_refptr
<cc::ContextProvider
>& context_provider
,
75 const scoped_refptr
<ui::CompositorVSyncManager
>& vsync_manager
)
76 : BrowserCompositorOutputSurface(context_provider
,
78 CreateTestValidatorOzone().Pass()) {}
80 void SetFlip(bool flip
) { capabilities_
.flipped_output_surface
= flip
; }
82 void SwapBuffers(cc::CompositorFrame
* frame
) override
{}
84 void OnReflectorChanged() override
{
86 reflector_texture_
.reset();
88 reflector_texture_
.reset(new ReflectorTexture(context_provider()));
89 reflector_
->OnSourceTextureMailboxUpdated(reflector_texture_
->mailbox());
93 #if defined(OS_MACOSX)
94 void OnSurfaceDisplayed() override
{}
95 void SetSurfaceSuspendedForRecycle(bool suspended
) override
{}
96 bool SurfaceShouldNotShowFramesAfterSuspendForRecycle() const override
{
101 gfx::Size
SurfaceSize() const override
{ return gfx::Size(256, 256); }
104 scoped_ptr
<ReflectorTexture
> reflector_texture_
;
107 const gfx::Rect
kSubRect(0, 0, 64, 64);
111 class ReflectorImplTest
: public testing::Test
{
113 void SetUp() override
{
114 bool enable_pixel_output
= false;
115 ui::ContextFactory
* context_factory
=
116 ui::InitializeContextFactoryForTests(enable_pixel_output
);
117 ImageTransportFactory::InitializeForUnitTests(
118 scoped_ptr
<ImageTransportFactory
>(
119 new NoTransportImageTransportFactory
));
120 message_loop_
.reset(new base::MessageLoop());
121 task_runner_
= message_loop_
->task_runner();
122 compositor_task_runner_
= new FakeTaskRunner();
123 compositor_
.reset(new ui::Compositor(gfx::kNullAcceleratedWidget
,
125 compositor_task_runner_
.get()));
126 context_provider_
= cc::TestContextProvider::Create(
127 cc::TestWebGraphicsContext3D::Create().Pass());
129 scoped_ptr
<TestOutputSurface
>(
130 new TestOutputSurface(context_provider_
,
131 compositor_
->vsync_manager())).Pass();
132 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
134 root_layer_
.reset(new ui::Layer(ui::LAYER_SOLID_COLOR
));
135 compositor_
->SetRootLayer(root_layer_
.get());
136 mirroring_layer_
.reset(new ui::Layer(ui::LAYER_SOLID_COLOR
));
137 compositor_
->root_layer()->Add(mirroring_layer_
.get());
138 gfx::Size size
= output_surface_
->SurfaceSize();
139 mirroring_layer_
->SetBounds(gfx::Rect(size
.width(), size
.height()));
142 void SetUpReflector() {
143 reflector_
= make_scoped_ptr(
144 new ReflectorImpl(compositor_
.get(), mirroring_layer_
.get()));
145 reflector_
->OnSourceSurfaceReady(output_surface_
.get());
148 void TearDown() override
{
150 reflector_
->RemoveMirroringLayer(mirroring_layer_
.get());
151 cc::TextureMailbox mailbox
;
152 scoped_ptr
<cc::SingleReleaseCallback
> release
;
153 if (mirroring_layer_
->PrepareTextureMailbox(&mailbox
, &release
, false)) {
154 release
->Run(0, false);
157 ui::TerminateContextFactoryForTests();
158 ImageTransportFactory::Terminate();
161 void UpdateTexture() { reflector_
->OnSourcePostSubBuffer(kSubRect
); }
164 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
165 scoped_refptr
<cc::ContextProvider
> context_provider_
;
166 cc::FakeOutputSurfaceClient output_surface_client_
;
167 scoped_ptr
<base::MessageLoop
> message_loop_
;
168 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
169 scoped_ptr
<ui::Compositor
> compositor_
;
170 scoped_ptr
<ui::Layer
> root_layer_
;
171 scoped_ptr
<ui::Layer
> mirroring_layer_
;
172 scoped_ptr
<ReflectorImpl
> reflector_
;
173 scoped_ptr
<TestOutputSurface
> output_surface_
;
177 TEST_F(ReflectorImplTest
, CheckNormalOutputSurface
) {
178 output_surface_
->SetFlip(false);
181 EXPECT_TRUE(mirroring_layer_
->TextureFlipped());
182 gfx::Rect expected_rect
=
183 kSubRect
+ gfx::Vector2d(0, output_surface_
->SurfaceSize().height()) -
184 gfx::Vector2d(0, kSubRect
.height());
185 EXPECT_EQ(expected_rect
, mirroring_layer_
->damaged_region());
188 TEST_F(ReflectorImplTest
, CheckInvertedOutputSurface
) {
189 output_surface_
->SetFlip(true);
192 EXPECT_FALSE(mirroring_layer_
->TextureFlipped());
193 EXPECT_EQ(kSubRect
, mirroring_layer_
->damaged_region());
196 #if defined(USE_OZONE)
197 TEST_F(ReflectorImplTest
, CheckOverlayNoReflector
) {
198 cc::OverlayCandidateList list
;
199 cc::OverlayCandidate plane_1
, plane_2
;
200 plane_1
.plane_z_order
= 0;
201 plane_2
.plane_z_order
= 1;
202 list
.push_back(plane_1
);
203 list
.push_back(plane_2
);
204 output_surface_
->GetOverlayCandidateValidator()->CheckOverlaySupport(&list
);
205 EXPECT_TRUE(list
[0].overlay_handled
);
208 TEST_F(ReflectorImplTest
, CheckOverlaySWMirroring
) {
210 cc::OverlayCandidateList list
;
211 cc::OverlayCandidate plane_1
, plane_2
;
212 plane_1
.plane_z_order
= 0;
213 plane_2
.plane_z_order
= 1;
214 list
.push_back(plane_1
);
215 list
.push_back(plane_2
);
216 output_surface_
->GetOverlayCandidateValidator()->CheckOverlaySupport(&list
);
217 EXPECT_FALSE(list
[0].overlay_handled
);
219 #endif // defined(USE_OZONE)
222 } // namespace content