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/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "base/single_thread_task_runner.h"
8 #include "cc/test/fake_output_surface_client.h"
9 #include "cc/test/test_context_provider.h"
10 #include "cc/test/test_web_graphics_context_3d.h"
11 #include "content/browser/compositor/browser_compositor_output_surface.h"
12 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator.h"
13 #include "content/browser/compositor/reflector_impl.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, new TestOverlayCandidatesOzone()));
67 #endif // defined(USE_OZONE)
70 class TestOutputSurface
: public BrowserCompositorOutputSurface
{
73 const scoped_refptr
<cc::ContextProvider
>& context_provider
,
74 const scoped_refptr
<ui::CompositorVSyncManager
>& vsync_manager
)
75 : BrowserCompositorOutputSurface(context_provider
,
77 CreateTestValidatorOzone().Pass()) {}
79 void SetFlip(bool flip
) { capabilities_
.flipped_output_surface
= flip
; }
81 void SwapBuffers(cc::CompositorFrame
* frame
) override
{}
83 #if defined(OS_MACOSX)
84 void OnSurfaceDisplayed() override
{}
85 void SetSurfaceSuspendedForRecycle(bool suspended
) override
{}
86 bool SurfaceShouldNotShowFramesAfterSuspendForRecycle() const override
{
91 gfx::Size
SurfaceSize() const override
{ return gfx::Size(256, 256); }
94 const gfx::Rect kSubRect
= gfx::Rect(0, 0, 64, 64);
95 const SkIRect kSkSubRect
= SkIRect::MakeXYWH(0, 0, 64, 64);
99 class ReflectorImplTest
: public testing::Test
{
101 void SetUp() override
{
102 bool enable_pixel_output
= false;
103 ui::ContextFactory
* context_factory
=
104 ui::InitializeContextFactoryForTests(enable_pixel_output
);
105 ImageTransportFactory::InitializeForUnitTests(
106 scoped_ptr
<ImageTransportFactory
>(
107 new NoTransportImageTransportFactory
));
108 message_loop_
.reset(new base::MessageLoop());
109 proxy_
= message_loop_
->message_loop_proxy();
110 compositor_task_runner_
= new FakeTaskRunner();
111 compositor_
.reset(new ui::Compositor(gfx::kNullAcceleratedWidget
,
113 compositor_task_runner_
.get()));
114 context_provider_
= cc::TestContextProvider::Create(
115 cc::TestWebGraphicsContext3D::Create().Pass());
117 scoped_ptr
<TestOutputSurface
>(
118 new TestOutputSurface(context_provider_
,
119 compositor_
->vsync_manager())).Pass();
120 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
122 mirroring_layer_
.reset(new ui::Layer(ui::LAYER_SOLID_COLOR
));
123 gfx::Size size
= output_surface_
->SurfaceSize();
124 mirroring_layer_
->SetBounds(gfx::Rect(size
.width(), size
.height()));
127 void SetUpReflector() {
128 reflector_
= make_scoped_ptr(
129 new ReflectorImpl(compositor_
.get(), mirroring_layer_
.get()));
130 reflector_
->OnSourceSurfaceReady(output_surface_
.get());
133 void TearDown() override
{
134 cc::TextureMailbox mailbox
;
135 scoped_ptr
<cc::SingleReleaseCallback
> release
;
136 if (mirroring_layer_
->PrepareTextureMailbox(&mailbox
, &release
, false)) {
137 release
->Run(0, false);
140 ui::TerminateContextFactoryForTests();
141 ImageTransportFactory::Terminate();
144 void UpdateTexture() { reflector_
->OnSourcePostSubBuffer(kSubRect
); }
147 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
148 scoped_refptr
<cc::ContextProvider
> context_provider_
;
149 cc::FakeOutputSurfaceClient output_surface_client_
;
150 scoped_ptr
<base::MessageLoop
> message_loop_
;
151 scoped_refptr
<base::MessageLoopProxy
> proxy_
;
152 scoped_ptr
<ui::Compositor
> compositor_
;
153 scoped_ptr
<ui::Layer
> mirroring_layer_
;
154 scoped_ptr
<ReflectorImpl
> reflector_
;
155 scoped_ptr
<TestOutputSurface
> output_surface_
;
159 TEST_F(ReflectorImplTest
, CheckNormalOutputSurface
) {
160 output_surface_
->SetFlip(false);
163 EXPECT_TRUE(mirroring_layer_
->TextureFlipped());
164 EXPECT_EQ(SkRegion(SkIRect::MakeXYWH(
165 0, output_surface_
->SurfaceSize().height() - kSubRect
.height(),
166 kSubRect
.width(), kSubRect
.height())),
167 mirroring_layer_
->damaged_region());
170 TEST_F(ReflectorImplTest
, CheckInvertedOutputSurface
) {
171 output_surface_
->SetFlip(true);
174 EXPECT_FALSE(mirroring_layer_
->TextureFlipped());
175 EXPECT_EQ(SkRegion(kSkSubRect
), mirroring_layer_
->damaged_region());
178 #if defined(USE_OZONE)
179 TEST_F(ReflectorImplTest
, CheckOverlayNoReflector
) {
180 cc::OverlayCandidateList list
;
181 cc::OverlayCandidate plane_1
, plane_2
;
182 plane_1
.plane_z_order
= 0;
183 plane_2
.plane_z_order
= 1;
184 list
.push_back(plane_1
);
185 list
.push_back(plane_2
);
186 output_surface_
->GetOverlayCandidateValidator()->CheckOverlaySupport(&list
);
187 EXPECT_TRUE(list
[0].overlay_handled
);
190 TEST_F(ReflectorImplTest
, CheckOverlaySWMirroring
) {
192 cc::OverlayCandidateList list
;
193 cc::OverlayCandidate plane_1
, plane_2
;
194 plane_1
.plane_z_order
= 0;
195 plane_2
.plane_z_order
= 1;
196 list
.push_back(plane_1
);
197 list
.push_back(plane_2
);
198 output_surface_
->GetOverlayCandidateValidator()->CheckOverlaySupport(&list
);
199 EXPECT_FALSE(list
[0].overlay_handled
);
201 #endif // defined(USE_OZONE)
204 } // namespace content