Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: content/browser/renderer_host/offscreen_canvas_surface_manager_unittest.cc

Issue 2479563005: Create manager to track OffscreenCanvasSurfaceImpl instances (Closed)
Patch Set: remove compositorframesink from manager Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 std::unique_ptr<OffscreenCanvasSurfaceImpl> surface_impl =
93 base::WrapUnique(new OffscreenCanvasSurfaceImpl());
94 surface_impl->GetSurfaceId(
95 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId,
96 base::Unretained(this)));
97
98 EXPECT_TRUE(this->getCurrentSurfaceId().is_valid());
99 EXPECT_EQ(1, this->getNumSurfaceImplInstances());
100 cc::FrameSinkId frame_sink_id = surface_impl.get()->frame_sink_id();
101 EXPECT_EQ(frame_sink_id, this->getCurrentSurfaceId().frame_sink_id());
102 EXPECT_EQ(surface_impl.get(),
103 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance(
104 frame_sink_id));
105
106 // Next, after the SurfaceId is passed onto OffscreenCanvas on worker, it will
107 // request browser side to create a OffscreenCanvasCompositorFrameSink with
108 // the given SurfaceId.
109 MockMojoCompositorFrameSinkClient client_service;
110 std::unique_ptr<OffscreenCanvasCompositorFrameSink> compositor_frame_sink =
111 base::WrapUnique(new OffscreenCanvasCompositorFrameSink(
dcheng 2016/11/15 07:28:29 auto compositor_frame_sink = base::MakeUnique<Offs
xlai (Olivia) 2016/11/15 17:16:18 Done.
112 this->getCurrentSurfaceId(), client_service.GetProxy()));
113 surface_impl = nullptr;
114 EXPECT_EQ(0, this->getNumSurfaceImplInstances());
115 }
116
117 TEST_F(OffscreenCanvasSurfaceManagerTest,
118 MultiHTMLCanvasElementTransferToOffscreen) {
119 // Same scenario as above test except that now we have two HTMLCanvasElement
120 // transferControlToOffscreen at the same time.
121 std::unique_ptr<OffscreenCanvasSurfaceImpl> surface_impl_a =
122 base::WrapUnique(new OffscreenCanvasSurfaceImpl());
123 surface_impl_a->GetSurfaceId(
124 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId,
125 base::Unretained(this)));
126 cc::SurfaceId surface_id_a = this->getCurrentSurfaceId();
127
128 EXPECT_TRUE(surface_id_a.is_valid());
129
130 std::unique_ptr<OffscreenCanvasSurfaceImpl> surface_impl_b =
131 base::WrapUnique(new OffscreenCanvasSurfaceImpl());
132 surface_impl_b->GetSurfaceId(
133 base::Bind(&OffscreenCanvasSurfaceManagerTest::setSurfaceId,
134 base::Unretained(this)));
135 cc::SurfaceId surface_id_b = this->getCurrentSurfaceId();
136
137 EXPECT_TRUE(surface_id_b.is_valid());
138 EXPECT_NE(surface_id_a, surface_id_b);
139
140 EXPECT_EQ(2, this->getNumSurfaceImplInstances());
141 EXPECT_EQ(surface_impl_a.get(),
142 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance(
143 surface_id_a.frame_sink_id()));
144 EXPECT_EQ(surface_impl_b.get(),
145 OffscreenCanvasSurfaceManager::GetInstance()->GetSurfaceInstance(
146 surface_id_b.frame_sink_id()));
147
148 // Next, one of the transferred OffscreenCanvas does commit().
149 MockMojoCompositorFrameSinkClient client_service_a;
150 std::unique_ptr<OffscreenCanvasCompositorFrameSink> compositor_frame_sink_a =
151 base::WrapUnique(new OffscreenCanvasCompositorFrameSink(
152 surface_id_a, client_service_a.GetProxy()));
153
154 // Another transferred OffscreenCanvas also does commit().
155 MockMojoCompositorFrameSinkClient client_service_b;
156 std::unique_ptr<OffscreenCanvasCompositorFrameSink> compositor_frame_sink_b =
157 base::WrapUnique(new OffscreenCanvasCompositorFrameSink(
158 surface_id_b, client_service_b.GetProxy()));
159
160 surface_impl_a = nullptr;
161 EXPECT_EQ(1, this->getNumSurfaceImplInstances());
162 surface_impl_b = nullptr;
163 EXPECT_EQ(0, this->getNumSurfaceImplInstances());
164 }
165
166 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698