13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Copyright 2014 The Chromium Authors. All rights reserved.
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Use of this source code is governed by a BSD-style license that can be
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// found in the LICENSE file.
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "base/message_loop/message_loop.h"
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "cc/output/compositor_frame.h"
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "content/browser/compositor/browser_compositor_output_surface_proxy.h"
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "content/browser/compositor/software_browser_compositor_output_surface.h"
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "testing/gtest/include/gtest/gtest.h"
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "ui/compositor/compositor.h"
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "ui/compositor/test/context_factories_for_test.h"
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "ui/gfx/vsync_provider.h"
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace {
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass FakeVSyncProvider : public gfx::VSyncProvider {
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry public:
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  FakeVSyncProvider() : call_count_(0) {}
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual ~FakeVSyncProvider() {}
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual void GetVSyncParameters(const UpdateVSyncCallback& callback)
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OVERRIDE {
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    callback.Run(timebase_, interval_);
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    call_count_++;
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int call_count() const { return call_count_; }
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void set_timebase(base::TimeTicks timebase) { timebase_ = timebase; }
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void set_interval(base::TimeDelta interval) { interval_ = interval; }
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry private:
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  base::TimeTicks timebase_;
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  base::TimeDelta interval_;
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int call_count_;
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DISALLOW_COPY_AND_ASSIGN(FakeVSyncProvider);
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass FakeSoftwareOutputDevice : public cc::SoftwareOutputDevice {
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry public:
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  FakeSoftwareOutputDevice() : vsync_provider_(new FakeVSyncProvider()) {}
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual ~FakeSoftwareOutputDevice() {}
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual gfx::VSyncProvider* GetVSyncProvider() OVERRIDE {
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return vsync_provider_.get();
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry private:
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<gfx::VSyncProvider> vsync_provider_;
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DISALLOW_COPY_AND_ASSIGN(FakeSoftwareOutputDevice);
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}  // namespace
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass SoftwareBrowserCompositorOutputSurfaceTest : public testing::Test {
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry public:
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  SoftwareBrowserCompositorOutputSurfaceTest();
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual ~SoftwareBrowserCompositorOutputSurfaceTest();
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual void SetUp() OVERRIDE;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual void TearDown() OVERRIDE;
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<content::BrowserCompositorOutputSurface> CreateSurface(
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      scoped_ptr<cc::SoftwareOutputDevice> device);
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry protected:
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<content::BrowserCompositorOutputSurface> output_surface_;
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<base::MessageLoop> message_loop_;
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<ui::Compositor> compositor_;
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  IDMap<content::BrowserCompositorOutputSurface> surface_map_;
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_refptr<content::BrowserCompositorOutputSurfaceProxy> surface_proxy_;
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DISALLOW_COPY_AND_ASSIGN(SoftwareBrowserCompositorOutputSurfaceTest);
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko PoyrySoftwareBrowserCompositorOutputSurfaceTest::
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    SoftwareBrowserCompositorOutputSurfaceTest() {
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // |message_loop_| is not used, but the main thread still has to exist for the
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // compositor to use.
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  message_loop_.reset(new base::MessageLoopForUI);
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
883c827367444ee418f129b2c238299f49d3264554Jarkko PoyrySoftwareBrowserCompositorOutputSurfaceTest::
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    ~SoftwareBrowserCompositorOutputSurfaceTest() {}
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid SoftwareBrowserCompositorOutputSurfaceTest::SetUp() {
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool enable_pixel_output = false;
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ui::ContextFactory* context_factory =
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      ui::InitializeContextFactoryForTests(enable_pixel_output);
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget,
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       context_factory,
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                       base::MessageLoopProxy::current()));
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  surface_proxy_ =
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      new content::BrowserCompositorOutputSurfaceProxy(&surface_map_);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid SoftwareBrowserCompositorOutputSurfaceTest::TearDown() {
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  output_surface_.reset();
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  compositor_.reset();
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  EXPECT_TRUE(surface_map_.IsEmpty());
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  surface_map_.Clear();
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ui::TerminateContextFactoryForTests();
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyryscoped_ptr<content::BrowserCompositorOutputSurface>
1143c827367444ee418f129b2c238299f49d3264554Jarkko PoyrySoftwareBrowserCompositorOutputSurfaceTest::CreateSurface(
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    scoped_ptr<cc::SoftwareOutputDevice> device) {
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return scoped_ptr<content::BrowserCompositorOutputSurface>(
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      new content::SoftwareBrowserCompositorOutputSurface(
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          surface_proxy_,
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          device.Pass(),
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          1,
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          &surface_map_,
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          compositor_->vsync_manager()));
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1253c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTEST_F(SoftwareBrowserCompositorOutputSurfaceTest, NoVSyncProvider) {
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<cc::SoftwareOutputDevice> software_device(
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      new cc::SoftwareOutputDevice());
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  output_surface_ = CreateSurface(software_device.Pass());
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  cc::CompositorFrame frame;
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  output_surface_->SwapBuffers(&frame);
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  EXPECT_EQ(NULL, output_surface_->software_device()->GetVSyncProvider());
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1363c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTEST_F(SoftwareBrowserCompositorOutputSurfaceTest, VSyncProviderUpdates) {
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<cc::SoftwareOutputDevice> software_device(
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      new FakeSoftwareOutputDevice());
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  output_surface_ = CreateSurface(software_device.Pass());
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  FakeVSyncProvider* vsync_provider = static_cast<FakeVSyncProvider*>(
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      output_surface_->software_device()->GetVSyncProvider());
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  EXPECT_EQ(0, vsync_provider->call_count());
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  cc::CompositorFrame frame;
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  output_surface_->SwapBuffers(&frame);
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  EXPECT_EQ(1, vsync_provider->call_count());
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry