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/message_loop/message_loop.h"
6 #include "cc/output/compositor_frame.h"
7 #include "cc/test/fake_output_surface_client.h"
8 #include "content/browser/compositor/browser_compositor_output_surface_proxy.h"
9 #include "content/browser/compositor/software_browser_compositor_output_surface.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/compositor/compositor.h"
12 #include "ui/compositor/test/context_factories_for_test.h"
13 #include "ui/gfx/vsync_provider.h"
17 class FakeVSyncProvider
: public gfx::VSyncProvider
{
19 FakeVSyncProvider() : call_count_(0) {}
20 ~FakeVSyncProvider() override
{}
22 void GetVSyncParameters(const UpdateVSyncCallback
& callback
) override
{
23 callback
.Run(timebase_
, interval_
);
27 int call_count() const { return call_count_
; }
29 void set_timebase(base::TimeTicks timebase
) { timebase_
= timebase
; }
30 void set_interval(base::TimeDelta interval
) { interval_
= interval
; }
33 base::TimeTicks timebase_
;
34 base::TimeDelta interval_
;
38 DISALLOW_COPY_AND_ASSIGN(FakeVSyncProvider
);
41 class FakeSoftwareOutputDevice
: public cc::SoftwareOutputDevice
{
43 FakeSoftwareOutputDevice() : vsync_provider_(new FakeVSyncProvider()) {}
44 ~FakeSoftwareOutputDevice() override
{}
46 gfx::VSyncProvider
* GetVSyncProvider() override
{
47 return vsync_provider_
.get();
51 scoped_ptr
<gfx::VSyncProvider
> vsync_provider_
;
53 DISALLOW_COPY_AND_ASSIGN(FakeSoftwareOutputDevice
);
58 class SoftwareBrowserCompositorOutputSurfaceTest
: public testing::Test
{
60 SoftwareBrowserCompositorOutputSurfaceTest();
61 ~SoftwareBrowserCompositorOutputSurfaceTest() override
;
63 void SetUp() override
;
64 void TearDown() override
;
66 scoped_ptr
<content::BrowserCompositorOutputSurface
> CreateSurface(
67 scoped_ptr
<cc::SoftwareOutputDevice
> device
);
70 scoped_ptr
<content::BrowserCompositorOutputSurface
> output_surface_
;
72 scoped_ptr
<base::MessageLoop
> message_loop_
;
73 scoped_ptr
<ui::Compositor
> compositor_
;
75 IDMap
<content::BrowserCompositorOutputSurface
> surface_map_
;
76 scoped_refptr
<content::BrowserCompositorOutputSurfaceProxy
> surface_proxy_
;
78 DISALLOW_COPY_AND_ASSIGN(SoftwareBrowserCompositorOutputSurfaceTest
);
81 SoftwareBrowserCompositorOutputSurfaceTest::
82 SoftwareBrowserCompositorOutputSurfaceTest() {
83 // |message_loop_| is not used, but the main thread still has to exist for the
85 message_loop_
.reset(new base::MessageLoopForUI
);
88 SoftwareBrowserCompositorOutputSurfaceTest::
89 ~SoftwareBrowserCompositorOutputSurfaceTest() {}
91 void SoftwareBrowserCompositorOutputSurfaceTest::SetUp() {
92 bool enable_pixel_output
= false;
93 ui::ContextFactory
* context_factory
=
94 ui::InitializeContextFactoryForTests(enable_pixel_output
);
96 compositor_
.reset(new ui::Compositor(gfx::kNullAcceleratedWidget
,
98 base::MessageLoopProxy::current()));
100 new content::BrowserCompositorOutputSurfaceProxy(&surface_map_
);
103 void SoftwareBrowserCompositorOutputSurfaceTest::TearDown() {
104 output_surface_
.reset();
107 EXPECT_TRUE(surface_map_
.IsEmpty());
109 surface_map_
.Clear();
110 ui::TerminateContextFactoryForTests();
113 scoped_ptr
<content::BrowserCompositorOutputSurface
>
114 SoftwareBrowserCompositorOutputSurfaceTest::CreateSurface(
115 scoped_ptr
<cc::SoftwareOutputDevice
> device
) {
116 return scoped_ptr
<content::BrowserCompositorOutputSurface
>(
117 new content::SoftwareBrowserCompositorOutputSurface(
122 compositor_
->vsync_manager()));
125 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest
, NoVSyncProvider
) {
126 cc::FakeOutputSurfaceClient output_surface_client
;
127 scoped_ptr
<cc::SoftwareOutputDevice
> software_device(
128 new cc::SoftwareOutputDevice());
129 output_surface_
= CreateSurface(software_device
.Pass());
130 CHECK(output_surface_
->BindToClient(&output_surface_client
));
132 cc::CompositorFrame frame
;
133 output_surface_
->SwapBuffers(&frame
);
135 EXPECT_EQ(1, output_surface_client
.swap_count());
136 EXPECT_EQ(NULL
, output_surface_
->software_device()->GetVSyncProvider());
139 TEST_F(SoftwareBrowserCompositorOutputSurfaceTest
, VSyncProviderUpdates
) {
140 cc::FakeOutputSurfaceClient output_surface_client
;
141 scoped_ptr
<cc::SoftwareOutputDevice
> software_device(
142 new FakeSoftwareOutputDevice());
143 output_surface_
= CreateSurface(software_device
.Pass());
144 CHECK(output_surface_
->BindToClient(&output_surface_client
));
146 FakeVSyncProvider
* vsync_provider
= static_cast<FakeVSyncProvider
*>(
147 output_surface_
->software_device()->GetVSyncProvider());
148 EXPECT_EQ(0, vsync_provider
->call_count());
150 cc::CompositorFrame frame
;
151 output_surface_
->SwapBuffers(&frame
);
153 EXPECT_EQ(1, output_surface_client
.swap_count());
154 EXPECT_EQ(1, vsync_provider
->call_count());