OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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 "content/browser/browser_thread_impl.h" |
| 6 #include "content/browser/compositor/test/no_transport_image_transport_factory.h
" |
| 7 #include "content/browser/renderer_host/offscreen_canvas_compositor_frame_sink.h
" |
| 8 #include "content/browser/renderer_host/offscreen_canvas_surface_impl.h" |
| 9 #include "content/browser/renderer_host/offscreen_canvas_surface_manager.h" |
| 10 #include "mojo/public/cpp/bindings/binding.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 #if defined(OS_ANDROID) |
| 14 #include "content/test/mock_gpu_channel_establish_factory.h" |
| 15 #else |
| 16 #include "content/browser/compositor/image_transport_factory.h" |
| 17 #endif |
| 18 |
| 19 namespace content { |
| 20 |
| 21 class MockMojoCompositorFrameSinkClient |
| 22 : public cc::mojom::MojoCompositorFrameSinkClient { |
| 23 public: |
| 24 MockMojoCompositorFrameSinkClient() : binding_(this) {} |
| 25 ~MockMojoCompositorFrameSinkClient() override {} |
| 26 |
| 27 cc::mojom::MojoCompositorFrameSinkClientPtr GetProxy() { |
| 28 return binding_.CreateInterfacePtrAndBind(); |
| 29 } |
| 30 |
| 31 void DidReceiveCompositorFrameAck() override {} |
| 32 void OnBeginFrame(const cc::BeginFrameArgs& args) override {} |
| 33 void ReclaimResources(const cc::ReturnedResourceArray& resources) override {} |
| 34 |
| 35 private: |
| 36 mojo::Binding<cc::mojom::MojoCompositorFrameSinkClient> binding_; |
| 37 }; |
| 38 |
| 39 class OffscreenCanvasSurfaceManagerTest : public testing::Test { |
| 40 public: |
| 41 int getNumSurfaceImplInstances() { |
| 42 return OffscreenCanvasSurfaceManager::GetInstance() |
| 43 ->registered_surface_instances_.size(); |
| 44 } |
| 45 const cc::SurfaceId getCurrentSurfaceId() { return current_surface_id_; } |
| 46 void setSurfaceId(const cc::SurfaceId& surface_id) { |
| 47 current_surface_id_ = surface_id; |
| 48 } |
| 49 |
| 50 protected: |
| 51 void SetUp() override; |
| 52 void TearDown() override; |
| 53 |
| 54 private: |
| 55 cc::SurfaceId current_surface_id_; |
| 56 std::unique_ptr<BrowserThreadImpl> ui_thread_; |
| 57 base::MessageLoopForUI message_loop_; |
| 58 #if defined(OS_ANDROID) |
| 59 MockGpuChannelEstablishFactory gpu_channel_factory_; |
| 60 #endif |
| 61 }; |
| 62 |
| 63 void OffscreenCanvasSurfaceManagerTest::SetUp() { |
| 64 #if defined(OS_ANDROID) |
| 65 ContextProviderFactoryImpl::Initialize(&gpu_channel_factory_); |
| 66 ui::ContextProviderFactory::SetInstance( |
| 67 ContextProviderFactoryImpl::GetInstance()); |
| 68 #else |
| 69 ImageTransportFactory::InitializeForUnitTests( |
| 70 std::unique_ptr<ImageTransportFactory>( |
| 71 new NoTransportImageTransportFactory)); |
| 72 #endif |
| 73 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, &message_loop_)); |
| 74 } |
| 75 |
| 76 void OffscreenCanvasSurfaceManagerTest::TearDown() { |
| 77 #if defined(OS_ANDROID) |
| 78 ui::ContextProviderFactory::SetInstance(nullptr); |
| 79 ContextProviderFactoryImpl::Terminate(); |
| 80 #else |
| 81 ImageTransportFactory::Terminate(); |
| 82 #endif |
| 83 } |
| 84 |
| 85 // This test mimics the workflow of OffscreenCanvas.commit() on renderer |
| 86 // process. |
| 87 TEST_F(OffscreenCanvasSurfaceManagerTest, |
| 88 SingleHTMLCanvasElementTransferToOffscreen) { |
| 89 // Assume that HTMLCanvasElement.transferControlToOffscreen() is triggered and |
| 90 // it will invoke GetSurfaceId function on OffscreenCanvasSurfaceImpl to |
| 91 // obtain a unique SurfaceId from browser. |
| 92 auto surface_impl = base::WrapUnique(new OffscreenCanvasSurfaceImpl()); |
| 93 surface_impl->GetSurfaceId( |
| 94 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId, |
| 95 base::Unretained(this))); |
| 96 |
| 97 EXPECT_TRUE(this->getCurrentSurfaceId().is_valid()); |
| 98 EXPECT_EQ(1, this->getNumSurfaceImplInstances()); |
| 99 cc::FrameSinkId frame_sink_id = surface_impl.get()->frame_sink_id(); |
| 100 EXPECT_EQ(frame_sink_id, this->getCurrentSurfaceId().frame_sink_id()); |
| 101 EXPECT_EQ(surface_impl.get(), |
| 102 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance( |
| 103 frame_sink_id)); |
| 104 |
| 105 // Next, after the SurfaceId is passed onto OffscreenCanvas on worker, it will |
| 106 // request browser side to create a OffscreenCanvasCompositorFrameSink with |
| 107 // the given SurfaceId. |
| 108 MockMojoCompositorFrameSinkClient client_service; |
| 109 auto compositor_frame_sink = |
| 110 base::WrapUnique(new OffscreenCanvasCompositorFrameSink( |
| 111 this->getCurrentSurfaceId(), client_service.GetProxy())); |
| 112 surface_impl = nullptr; |
| 113 EXPECT_EQ(0, this->getNumSurfaceImplInstances()); |
| 114 } |
| 115 |
| 116 TEST_F(OffscreenCanvasSurfaceManagerTest, |
| 117 MultiHTMLCanvasElementTransferToOffscreen) { |
| 118 // Same scenario as above test except that now we have two HTMLCanvasElement |
| 119 // transferControlToOffscreen at the same time. |
| 120 auto surface_impl_a = base::WrapUnique(new OffscreenCanvasSurfaceImpl()); |
| 121 surface_impl_a->GetSurfaceId( |
| 122 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId, |
| 123 base::Unretained(this))); |
| 124 cc::SurfaceId surface_id_a = this->getCurrentSurfaceId(); |
| 125 |
| 126 EXPECT_TRUE(surface_id_a.is_valid()); |
| 127 |
| 128 auto surface_impl_b = base::WrapUnique(new OffscreenCanvasSurfaceImpl()); |
| 129 surface_impl_b->GetSurfaceId( |
| 130 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId, |
| 131 base::Unretained(this))); |
| 132 cc::SurfaceId surface_id_b = this->getCurrentSurfaceId(); |
| 133 |
| 134 EXPECT_TRUE(surface_id_b.is_valid()); |
| 135 EXPECT_NE(surface_id_a, surface_id_b); |
| 136 |
| 137 EXPECT_EQ(2, this->getNumSurfaceImplInstances()); |
| 138 EXPECT_EQ(surface_impl_a.get(), |
| 139 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance( |
| 140 surface_id_a.frame_sink_id())); |
| 141 EXPECT_EQ(surface_impl_b.get(), |
| 142 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance( |
| 143 surface_id_b.frame_sink_id())); |
| 144 |
| 145 // Next, one of the transferred OffscreenCanvas does commit(). |
| 146 MockMojoCompositorFrameSinkClient client_service_a; |
| 147 auto compositor_frame_sink_a = |
| 148 base::WrapUnique(new OffscreenCanvasCompositorFrameSink( |
| 149 surface_id_a, client_service_a.GetProxy())); |
| 150 |
| 151 // Another transferred OffscreenCanvas also does commit(). |
| 152 MockMojoCompositorFrameSinkClient client_service_b; |
| 153 auto compositor_frame_sink_b = |
| 154 base::WrapUnique(new OffscreenCanvasCompositorFrameSink( |
| 155 surface_id_b, client_service_b.GetProxy())); |
| 156 |
| 157 surface_impl_a = nullptr; |
| 158 EXPECT_EQ(1, this->getNumSurfaceImplInstances()); |
| 159 surface_impl_b = nullptr; |
| 160 EXPECT_EQ(0, this->getNumSurfaceImplInstances()); |
| 161 } |
| 162 |
| 163 } // namespace content |
OLD | NEW |