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/reflector_impl.h"
13 #include "content/browser/compositor/test/no_transport_image_transport_factory.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/compositor/compositor.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/test/context_factories_for_test.h"
21 class FakeTaskRunner
: public base::SingleThreadTaskRunner
{
25 bool PostNonNestableDelayedTask(const tracked_objects::Location
& from_here
,
26 const base::Closure
& task
,
27 base::TimeDelta delay
) override
{
30 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
31 const base::Closure
& task
,
32 base::TimeDelta delay
) override
{
35 bool RunsTasksOnCurrentThread() const override
{ return true; }
38 ~FakeTaskRunner() override
{}
41 class TestOutputSurface
: public BrowserCompositorOutputSurface
{
44 const scoped_refptr
<cc::ContextProvider
>& context_provider
,
45 const scoped_refptr
<ui::CompositorVSyncManager
>& vsync_manager
)
46 : BrowserCompositorOutputSurface(context_provider
,
49 void SetFlip(bool flip
) { capabilities_
.flipped_output_surface
= flip
; }
51 void SwapBuffers(cc::CompositorFrame
* frame
) override
{}
53 #if defined(OS_MACOSX)
54 void OnSurfaceDisplayed() override
{}
55 void OnSurfaceRecycled() override
{}
56 bool ShouldNotShowFramesAfterRecycle() const override
{ return false; }
59 gfx::Size
SurfaceSize() const override
{ return gfx::Size(256, 256); }
62 const gfx::Rect kSubRect
= gfx::Rect(0, 0, 64, 64);
63 const SkIRect kSkSubRect
= SkIRect::MakeXYWH(0, 0, 64, 64);
67 class ReflectorImplTest
: public testing::Test
{
69 void SetUp() override
{
70 bool enable_pixel_output
= false;
71 ui::ContextFactory
* context_factory
=
72 ui::InitializeContextFactoryForTests(enable_pixel_output
);
73 ImageTransportFactory::InitializeForUnitTests(
74 scoped_ptr
<ImageTransportFactory
>(
75 new NoTransportImageTransportFactory
));
76 message_loop_
.reset(new base::MessageLoop());
77 proxy_
= message_loop_
->message_loop_proxy();
78 compositor_task_runner_
= new FakeTaskRunner();
79 compositor_
.reset(new ui::Compositor(gfx::kNullAcceleratedWidget
,
81 compositor_task_runner_
.get()));
82 context_provider_
= cc::TestContextProvider::Create(
83 cc::TestWebGraphicsContext3D::Create().Pass());
85 scoped_ptr
<TestOutputSurface
>(
86 new TestOutputSurface(context_provider_
,
87 compositor_
->vsync_manager())).Pass();
88 CHECK(output_surface_
->BindToClient(&output_surface_client_
));
90 mirroring_layer_
.reset(new ui::Layer(ui::LAYER_SOLID_COLOR
));
91 gfx::Size size
= output_surface_
->SurfaceSize();
92 mirroring_layer_
->SetBounds(gfx::Rect(size
.width(), size
.height()));
95 void SetUpReflector() {
96 reflector_
= make_scoped_ptr(
97 new ReflectorImpl(compositor_
.get(), mirroring_layer_
.get()));
98 reflector_
->OnSourceSurfaceReady(output_surface_
.get());
101 void TearDown() override
{
102 cc::TextureMailbox mailbox
;
103 scoped_ptr
<cc::SingleReleaseCallback
> release
;
104 if (mirroring_layer_
->PrepareTextureMailbox(&mailbox
, &release
, false)) {
105 release
->Run(0, false);
108 ui::TerminateContextFactoryForTests();
109 ImageTransportFactory::Terminate();
112 void UpdateTexture() { reflector_
->OnSourcePostSubBuffer(kSubRect
); }
115 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
116 scoped_refptr
<cc::ContextProvider
> context_provider_
;
117 cc::FakeOutputSurfaceClient output_surface_client_
;
118 scoped_ptr
<base::MessageLoop
> message_loop_
;
119 scoped_refptr
<base::MessageLoopProxy
> proxy_
;
120 scoped_ptr
<ui::Compositor
> compositor_
;
121 scoped_ptr
<ui::Layer
> mirroring_layer_
;
122 scoped_ptr
<ReflectorImpl
> reflector_
;
123 scoped_ptr
<TestOutputSurface
> output_surface_
;
127 TEST_F(ReflectorImplTest
, CheckNormalOutputSurface
) {
128 output_surface_
->SetFlip(false);
131 EXPECT_TRUE(mirroring_layer_
->TextureFlipped());
132 EXPECT_EQ(SkRegion(SkIRect::MakeXYWH(
133 0, output_surface_
->SurfaceSize().height() - kSubRect
.height(),
134 kSubRect
.width(), kSubRect
.height())),
135 mirroring_layer_
->damaged_region());
138 TEST_F(ReflectorImplTest
, CheckInvertedOutputSurface
) {
139 output_surface_
->SetFlip(true);
142 EXPECT_FALSE(mirroring_layer_
->TextureFlipped());
143 EXPECT_EQ(SkRegion(kSkSubRect
), mirroring_layer_
->damaged_region());
147 } // namespace content