1 // Copyright 2014 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/thread_task_runner_handle.h"
6 #include "cc/output/compositor_frame.h"
7 #include "cc/test/fake_output_surface_client.h"
8 #include "content/browser/compositor/software_browser_compositor_output_surface.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/compositor/compositor.h"
11 #include "ui/compositor/test/context_factories_for_test.h"
12 #include "ui/gfx/vsync_provider.h"
16 class FakeVSyncProvider
: public gfx::VSyncProvider
{
18 FakeVSyncProvider() : call_count_(0) {}
19 ~FakeVSyncProvider() override
{}
21 void GetVSyncParameters(const UpdateVSyncCallback
& callback
) override
{
22 callback
.Run(timebase_
, interval_
);
26 int call_count() const { return call_count_
; }
28 void set_timebase(base::TimeTicks timebase
) { timebase_
= timebase
; }
29 void set_interval(base::TimeDelta interval
) { interval_
= interval
; }
32 base::TimeTicks timebase_
;
33 base::TimeDelta interval_
;
37 DISALLOW_COPY_AND_ASSIGN(FakeVSyncProvider
);
40 class FakeSoftwareOutputDevice
: public cc::SoftwareOutputDevice
{
42 FakeSoftwareOutputDevice() : vsync_provider_(new FakeVSyncProvider()) {}
43 ~FakeSoftwareOutputDevice() override
{}
45 gfx::VSyncProvider
* GetVSyncProvider() override
{
46 return vsync_provider_
.get();
50 scoped_ptr
<gfx::VSyncProvider
> vsync_provider_
;
52 DISALLOW_COPY_AND_ASSIGN(FakeSoftwareOutputDevice
);
57 class SoftwareBrowserCompositorOutputSurfaceTest
: public testing::Test
{
59 SoftwareBrowserCompositorOutputSurfaceTest();
60 ~SoftwareBrowserCompositorOutputSurfaceTest() override
;
62 void SetUp() override
;
63 void TearDown() override
;
65 scoped_ptr
<content::BrowserCompositorOutputSurface
> CreateSurface(
66 scoped_ptr
<cc::SoftwareOutputDevice
> device
);
69 scoped_ptr
<content::BrowserCompositorOutputSurface
> output_surface_
;
71 scoped_ptr
<base::MessageLoop
> message_loop_
;
72 scoped_ptr
<ui::Compositor
> compositor_
;
74 DISALLOW_COPY_AND_ASSIGN(SoftwareBrowserCompositorOutputSurfaceTest
);
77 SoftwareBrowserCompositorOutputSurfaceTest::
78 SoftwareBrowserCompositorOutputSurfaceTest() {
79 // |message_loop_| is not used, but the main thread still has to exist for the
81 message_loop_
.reset(new base::MessageLoopForUI
);
84 SoftwareBrowserCompositorOutputSurfaceTest::
85 ~SoftwareBrowserCompositorOutputSurfaceTest() {}
87 void SoftwareBrowserCompositorOutputSurfaceTest::SetUp() {
88 bool enable_pixel_output
= false;
89 ui::ContextFactory
* context_factory
=
90 ui::InitializeContextFactoryForTests(enable_pixel_output
);
93 new ui::Compositor(context_factory
, base::ThreadTaskRunnerHandle::Get()));
94 compositor_
->SetAcceleratedWidgetAndStartCompositor(
95 gfx::kNullAcceleratedWidget
);
98 void SoftwareBrowserCompositorOutputSurfaceTest::TearDown() {
99 output_surface_
.reset();
101 ui::TerminateContextFactoryForTests();
104 scoped_ptr
<content::BrowserCompositorOutputSurface
>
105 SoftwareBrowserCompositorOutputSurfaceTest::CreateSurface(
106 scoped_ptr
<cc::SoftwareOutputDevice
> device
) {
107 return scoped_ptr
<content::BrowserCompositorOutputSurface
>(
108 new content::SoftwareBrowserCompositorOutputSurface(
110 compositor_
->vsync_manager()));
113 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest
, NoVSyncProvider
) {
114 cc::FakeOutputSurfaceClient output_surface_client
;
115 scoped_ptr
<cc::SoftwareOutputDevice
> software_device(
116 new cc::SoftwareOutputDevice());
117 output_surface_
= CreateSurface(software_device
.Pass());
118 CHECK(output_surface_
->BindToClient(&output_surface_client
));
120 cc::CompositorFrame frame
;
121 output_surface_
->SwapBuffers(&frame
);
123 EXPECT_EQ(1, output_surface_client
.swap_count());
124 EXPECT_EQ(NULL
, output_surface_
->software_device()->GetVSyncProvider());
127 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest
, VSyncProviderUpdates
) {
128 cc::FakeOutputSurfaceClient output_surface_client
;
129 scoped_ptr
<cc::SoftwareOutputDevice
> software_device(
130 new FakeSoftwareOutputDevice());
131 output_surface_
= CreateSurface(software_device
.Pass());
132 CHECK(output_surface_
->BindToClient(&output_surface_client
));
134 FakeVSyncProvider
* vsync_provider
= static_cast<FakeVSyncProvider
*>(
135 output_surface_
->software_device()->GetVSyncProvider());
136 EXPECT_EQ(0, vsync_provider
->call_count());
138 cc::CompositorFrame frame
;
139 output_surface_
->SwapBuffers(&frame
);
141 EXPECT_EQ(1, output_surface_client
.swap_count());
142 EXPECT_EQ(1, vsync_provider
->call_count());