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
,
79 CreateTestValidatorOzone().Pass()) {}
81 void SetFlip(bool flip
) { capabilities_
.flipped_output_surface
= flip
; }
83 void SwapBuffers(cc::CompositorFrame
* frame
) override
{}
85 void OnReflectorChanged() override
{
87 reflector_texture_
.reset();
89 reflector_texture_
.reset(new ReflectorTexture(context_provider()));
90 reflector_
->OnSourceTextureMailboxUpdated(reflector_texture_
->mailbox());
94 #if defined(OS_MACOSX)
95 void OnSurfaceDisplayed() override
{}
96 void SetSurfaceSuspendedForRecycle(bool suspended
) override
{}
97 bool SurfaceShouldNotShowFramesAfterSuspendForRecycle() const override
{
102 gfx::Size
SurfaceSize() const override
{ return gfx::Size(256, 256); }
105 scoped_ptr
<ReflectorTexture
> reflector_texture_
;
108 const gfx::Rect
kSubRect(0, 0, 64, 64);
112 class ReflectorImplTest
: public testing::Test
{
114 void SetUp() override
{
115 bool enable_pixel_output
= false;
116 ui::ContextFactory
* context_factory
=
117 ui::InitializeContextFactoryForTests(enable_pixel_output
);
118 ImageTransportFactory::InitializeForUnitTests(
119 scoped_ptr
<ImageTransportFactory
>(
120 new NoTransportImageTransportFactory
));
121 message_loop_
.reset(new base::MessageLoop());
122 task_runner_
= message_loop_
->task_runner();
123 compositor_task_runner_
= new FakeTaskRunner();
124 compositor_
.reset(new ui::Compositor(gfx::kNullAcceleratedWidget
,
126 compositor_task_runner_
.get()));
127 context_provider_
= cc::TestContextProvider::Create(
128 cc::TestWebGraphicsContext3D::Create().Pass());
130 scoped_ptr
<TestOutputSurface
>(
131 new TestOutputSurface(context_provider_
,
132 compositor_
->vsync_manager())).Pass();
133 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
135 root_layer_
.reset(new ui::Layer(ui::LAYER_SOLID_COLOR
));
136 compositor_
->SetRootLayer(root_layer_
.get());
137 mirroring_layer_
.reset(new ui::Layer(ui::LAYER_SOLID_COLOR
));
138 compositor_
->root_layer()->Add(mirroring_layer_
.get());
139 gfx::Size size
= output_surface_
->SurfaceSize();
140 mirroring_layer_
->SetBounds(gfx::Rect(size
.width(), size
.height()));
143 void SetUpReflector() {
144 reflector_
= make_scoped_ptr(
145 new ReflectorImpl(compositor_
.get(), mirroring_layer_
.get()));
146 reflector_
->OnSourceSurfaceReady(output_surface_
.get());
149 void TearDown() override
{
151 reflector_
->RemoveMirroringLayer(mirroring_layer_
.get());
152 cc::TextureMailbox mailbox
;
153 scoped_ptr
<cc::SingleReleaseCallback
> release
;
154 if (mirroring_layer_
->PrepareTextureMailbox(&mailbox
, &release
, false)) {
155 release
->Run(0, false);
158 ui::TerminateContextFactoryForTests();
159 ImageTransportFactory::Terminate();
162 void UpdateTexture() { reflector_
->OnSourcePostSubBuffer(kSubRect
); }
165 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
166 scoped_refptr
<cc::ContextProvider
> context_provider_
;
167 cc::FakeOutputSurfaceClient output_surface_client_
;
168 scoped_ptr
<base::MessageLoop
> message_loop_
;
169 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
170 scoped_ptr
<ui::Compositor
> compositor_
;
171 scoped_ptr
<ui::Layer
> root_layer_
;
172 scoped_ptr
<ui::Layer
> mirroring_layer_
;
173 scoped_ptr
<ReflectorImpl
> reflector_
;
174 scoped_ptr
<TestOutputSurface
> output_surface_
;
178 TEST_F(ReflectorImplTest
, CheckNormalOutputSurface
) {
179 output_surface_
->SetFlip(false);
182 EXPECT_TRUE(mirroring_layer_
->TextureFlipped());
183 gfx::Rect expected_rect
=
184 kSubRect
+ gfx::Vector2d(0, output_surface_
->SurfaceSize().height()) -
185 gfx::Vector2d(0, kSubRect
.height());
186 EXPECT_EQ(expected_rect
, mirroring_layer_
->damaged_region());
189 TEST_F(ReflectorImplTest
, CheckInvertedOutputSurface
) {
190 output_surface_
->SetFlip(true);
193 EXPECT_FALSE(mirroring_layer_
->TextureFlipped());
194 EXPECT_EQ(kSubRect
, mirroring_layer_
->damaged_region());
197 #if defined(USE_OZONE)
198 TEST_F(ReflectorImplTest
, CheckOverlayNoReflector
) {
199 cc::OverlayCandidateList list
;
200 cc::OverlayCandidate plane_1
, plane_2
;
201 plane_1
.plane_z_order
= 0;
202 plane_2
.plane_z_order
= 1;
203 list
.push_back(plane_1
);
204 list
.push_back(plane_2
);
205 output_surface_
->GetOverlayCandidateValidator()->CheckOverlaySupport(&list
);
206 EXPECT_TRUE(list
[0].overlay_handled
);
209 TEST_F(ReflectorImplTest
, CheckOverlaySWMirroring
) {
211 cc::OverlayCandidateList list
;
212 cc::OverlayCandidate plane_1
, plane_2
;
213 plane_1
.plane_z_order
= 0;
214 plane_2
.plane_z_order
= 1;
215 list
.push_back(plane_1
);
216 list
.push_back(plane_2
);
217 output_surface_
->GetOverlayCandidateValidator()->CheckOverlaySupport(&list
);
218 EXPECT_FALSE(list
[0].overlay_handled
);
220 #endif // defined(USE_OZONE)
223 } // namespace content