OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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" |
| 18 |
| 19 namespace content { |
| 20 namespace { |
| 21 class FakeTaskRunner : public base::SingleThreadTaskRunner { |
| 22 public: |
| 23 FakeTaskRunner() {} |
| 24 |
| 25 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 26 const base::Closure& task, |
| 27 base::TimeDelta delay) override { |
| 28 return true; |
| 29 } |
| 30 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 31 const base::Closure& task, |
| 32 base::TimeDelta delay) override { |
| 33 return true; |
| 34 } |
| 35 bool RunsTasksOnCurrentThread() const override { return true; } |
| 36 |
| 37 protected: |
| 38 ~FakeTaskRunner() override {} |
| 39 }; |
| 40 |
| 41 class TestOutputSurface : public BrowserCompositorOutputSurface { |
| 42 public: |
| 43 TestOutputSurface( |
| 44 const scoped_refptr<cc::ContextProvider>& context_provider, |
| 45 int surface_id, |
| 46 IDMap<BrowserCompositorOutputSurface>* output_surface_map, |
| 47 const scoped_refptr<ui::CompositorVSyncManager>& vsync_manager) |
| 48 : BrowserCompositorOutputSurface(context_provider, |
| 49 surface_id, |
| 50 output_surface_map, |
| 51 vsync_manager) {} |
| 52 |
| 53 void SetFlip(bool flip) { capabilities_.flipped_output_surface = flip; } |
| 54 |
| 55 void SwapBuffers(cc::CompositorFrame* frame) override {} |
| 56 |
| 57 #if defined(OS_MACOSX) |
| 58 void OnSurfaceDisplayed() override {} |
| 59 void OnSurfaceRecycled() override {} |
| 60 bool ShouldNotShowFramesAfterRecycle() const override { return false; } |
| 61 #endif |
| 62 |
| 63 gfx::Size SurfaceSize() const override { return gfx::Size(256, 256); } |
| 64 }; |
| 65 |
| 66 const gfx::Rect kSubRect = gfx::Rect(0, 0, 64, 64); |
| 67 const SkIRect kSkSubRect = SkIRect::MakeXYWH(0, 0, 64, 64); |
| 68 |
| 69 } // namespace |
| 70 |
| 71 class ReflectorImplTest : public testing::Test { |
| 72 public: |
| 73 void SetUp() override { |
| 74 bool enable_pixel_output = false; |
| 75 ui::ContextFactory* context_factory = |
| 76 ui::InitializeContextFactoryForTests(enable_pixel_output); |
| 77 ImageTransportFactory::InitializeForUnitTests( |
| 78 scoped_ptr<ImageTransportFactory>( |
| 79 new NoTransportImageTransportFactory)); |
| 80 message_loop_.reset(new base::MessageLoop()); |
| 81 proxy_ = message_loop_->message_loop_proxy(); |
| 82 compositor_task_runner_ = new FakeTaskRunner(); |
| 83 compositor_.reset(new ui::Compositor(gfx::kNullAcceleratedWidget, |
| 84 context_factory, |
| 85 compositor_task_runner_.get())); |
| 86 context_provider_ = cc::TestContextProvider::Create( |
| 87 cc::TestWebGraphicsContext3D::Create().Pass()); |
| 88 output_surface_ = |
| 89 scoped_ptr<TestOutputSurface>( |
| 90 new TestOutputSurface(context_provider_, 1, &surface_map_, |
| 91 compositor_->vsync_manager())).Pass(); |
| 92 CHECK(output_surface_->BindToClient(&output_surface_client_)); |
| 93 |
| 94 mirroring_layer_.reset(new ui::Layer()); |
| 95 gfx::Size size = output_surface_->SurfaceSize(); |
| 96 mirroring_layer_->SetBounds(gfx::Rect(size.width(), size.height())); |
| 97 |
| 98 int32 surface_id = 1; |
| 99 reflector_ = new ReflectorImpl(compositor_.get(), mirroring_layer_.get(), |
| 100 &surface_map_, proxy_.get(), surface_id); |
| 101 } |
| 102 |
| 103 void TearDown() override { |
| 104 cc::TextureMailbox mailbox; |
| 105 scoped_ptr<cc::SingleReleaseCallback> release; |
| 106 if (mirroring_layer_->PrepareTextureMailbox(&mailbox, &release, false)) { |
| 107 release->Run(0, false); |
| 108 } |
| 109 compositor_.reset(); |
| 110 ui::TerminateContextFactoryForTests(); |
| 111 ImageTransportFactory::Terminate(); |
| 112 } |
| 113 |
| 114 void Init() { base::RunLoop().RunUntilIdle(); } |
| 115 |
| 116 void UpdateTexture() { |
| 117 reflector_->UpdateSubBufferOnMainThread(output_surface_->SurfaceSize(), |
| 118 kSubRect); |
| 119 } |
| 120 |
| 121 protected: |
| 122 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; |
| 123 IDMap<BrowserCompositorOutputSurface> surface_map_; |
| 124 scoped_refptr<cc::ContextProvider> context_provider_; |
| 125 cc::FakeOutputSurfaceClient output_surface_client_; |
| 126 scoped_ptr<base::MessageLoop> message_loop_; |
| 127 scoped_refptr<base::MessageLoopProxy> proxy_; |
| 128 scoped_ptr<ui::Compositor> compositor_; |
| 129 scoped_ptr<ui::Layer> mirroring_layer_; |
| 130 scoped_refptr<ReflectorImpl> reflector_; |
| 131 scoped_ptr<TestOutputSurface> output_surface_; |
| 132 }; |
| 133 |
| 134 namespace { |
| 135 TEST_F(ReflectorImplTest, CheckNormalOutputSurface) { |
| 136 output_surface_->SetFlip(false); |
| 137 Init(); |
| 138 UpdateTexture(); |
| 139 EXPECT_TRUE(mirroring_layer_->TextureFlipped()); |
| 140 EXPECT_EQ(SkRegion(SkIRect::MakeXYWH( |
| 141 0, output_surface_->SurfaceSize().height() - kSubRect.height(), |
| 142 kSubRect.width(), kSubRect.height())), |
| 143 mirroring_layer_->damaged_region()); |
| 144 } |
| 145 |
| 146 TEST_F(ReflectorImplTest, CheckInvertedOutputSurface) { |
| 147 output_surface_->SetFlip(true); |
| 148 Init(); |
| 149 UpdateTexture(); |
| 150 EXPECT_FALSE(mirroring_layer_->TextureFlipped()); |
| 151 EXPECT_EQ(SkRegion(kSkSubRect), mirroring_layer_->damaged_region()); |
| 152 } |
| 153 |
| 154 } // namespace |
| 155 } // namespace content |
OLD | NEW |