OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/video_resource_updater.h" | 5 #include "cc/resources/video_resource_updater.h" |
6 | 6 |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 #include "cc/resources/resource_provider.h" | 8 #include "cc/resources/resource_provider.h" |
9 #include "cc/test/fake_output_surface.h" | 9 #include "cc/test/fake_output_surface.h" |
10 #include "cc/test/fake_output_surface_client.h" | 10 #include "cc/test/fake_output_surface_client.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 ++upload_count_; | 31 ++upload_count_; |
32 } | 32 } |
33 | 33 |
34 int UploadCount() { return upload_count_; } | 34 int UploadCount() { return upload_count_; } |
35 void ResetUploadCount() { upload_count_ = 0; } | 35 void ResetUploadCount() { upload_count_ = 0; } |
36 | 36 |
37 private: | 37 private: |
38 int upload_count_; | 38 int upload_count_; |
39 }; | 39 }; |
40 | 40 |
41 class SharedBitmapManagerAllocationCounter : public TestSharedBitmapManager { | |
42 public: | |
43 scoped_ptr<SharedBitmap> AllocateSharedBitmap( | |
44 const gfx::Size& size) override { | |
45 ++allocation_count_; | |
46 return TestSharedBitmapManager::AllocateSharedBitmap(size); | |
47 } | |
48 | |
49 int AllocationCount() { return allocation_count_; } | |
50 void ResetAllocationCount() { allocation_count_ = 0; } | |
51 | |
52 private: | |
53 int allocation_count_; | |
54 }; | |
55 | |
56 class VideoResourceUpdaterTest : public testing::Test { | 41 class VideoResourceUpdaterTest : public testing::Test { |
57 protected: | 42 protected: |
58 VideoResourceUpdaterTest() { | 43 VideoResourceUpdaterTest() { |
59 scoped_ptr<WebGraphicsContext3DUploadCounter> context3d( | 44 scoped_ptr<WebGraphicsContext3DUploadCounter> context3d( |
60 new WebGraphicsContext3DUploadCounter()); | 45 new WebGraphicsContext3DUploadCounter()); |
61 | 46 |
62 context3d_ = context3d.get(); | 47 context3d_ = context3d.get(); |
63 | 48 |
64 output_surface3d_ = | 49 output_surface3d_ = |
65 FakeOutputSurface::Create3d(context3d.Pass()); | 50 FakeOutputSurface::Create3d(context3d.Pass()); |
66 CHECK(output_surface3d_->BindToClient(&client_)); | 51 CHECK(output_surface3d_->BindToClient(&client_)); |
67 | 52 shared_bitmap_manager_.reset(new TestSharedBitmapManager()); |
68 output_surface_software_ = FakeOutputSurface::CreateSoftware( | |
69 make_scoped_ptr(new SoftwareOutputDevice)); | |
70 CHECK(output_surface_software_->BindToClient(&client_)); | |
71 | |
72 shared_bitmap_manager_.reset(new SharedBitmapManagerAllocationCounter()); | |
73 resource_provider3d_ = | 53 resource_provider3d_ = |
74 ResourceProvider::Create(output_surface3d_.get(), | 54 ResourceProvider::Create(output_surface3d_.get(), |
75 shared_bitmap_manager_.get(), | 55 shared_bitmap_manager_.get(), |
76 NULL, | 56 NULL, |
77 NULL, | 57 NULL, |
78 0, | 58 0, |
79 false, | 59 false, |
80 1); | 60 1); |
81 | |
82 resource_provider_software_ = ResourceProvider::Create( | |
83 output_surface_software_.get(), shared_bitmap_manager_.get(), NULL, | |
84 NULL, 0, false, 1); | |
85 } | 61 } |
86 | 62 |
87 scoped_refptr<media::VideoFrame> CreateTestYUVVideoFrame() { | 63 scoped_refptr<media::VideoFrame> CreateTestYUVVideoFrame() { |
88 const int kDimension = 10; | 64 const int kDimension = 10; |
89 gfx::Size size(kDimension, kDimension); | 65 gfx::Size size(kDimension, kDimension); |
90 static uint8 y_data[kDimension * kDimension] = { 0 }; | 66 static uint8 y_data[kDimension * kDimension] = { 0 }; |
91 static uint8 u_data[kDimension * kDimension / 2] = { 0 }; | 67 static uint8 u_data[kDimension * kDimension / 2] = { 0 }; |
92 static uint8 v_data[kDimension * kDimension / 2] = { 0 }; | 68 static uint8 v_data[kDimension * kDimension / 2] = { 0 }; |
93 | 69 |
94 return media::VideoFrame::WrapExternalYuvData( | 70 return media::VideoFrame::WrapExternalYuvData( |
95 media::VideoFrame::YV16, // format | 71 media::VideoFrame::YV16, // format |
96 size, // coded_size | 72 size, // coded_size |
97 gfx::Rect(size), // visible_rect | 73 gfx::Rect(size), // visible_rect |
98 size, // natural_size | 74 size, // natural_size |
99 size.width(), // y_stride | 75 size.width(), // y_stride |
100 size.width() / 2, // u_stride | 76 size.width() / 2, // u_stride |
101 size.width() / 2, // v_stride | 77 size.width() / 2, // v_stride |
102 y_data, // y_data | 78 y_data, // y_data |
103 u_data, // u_data | 79 u_data, // u_data |
104 v_data, // v_data | 80 v_data, // v_data |
105 base::TimeDelta(), // timestamp, | 81 base::TimeDelta(), // timestamp, |
106 base::Closure()); // no_longer_needed_cb | 82 base::Closure()); // no_longer_needed_cb |
107 } | 83 } |
108 | 84 |
109 WebGraphicsContext3DUploadCounter* context3d_; | 85 WebGraphicsContext3DUploadCounter* context3d_; |
110 FakeOutputSurfaceClient client_; | 86 FakeOutputSurfaceClient client_; |
111 scoped_ptr<FakeOutputSurface> output_surface3d_; | 87 scoped_ptr<FakeOutputSurface> output_surface3d_; |
112 scoped_ptr<FakeOutputSurface> output_surface_software_; | 88 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_; |
113 scoped_ptr<SharedBitmapManagerAllocationCounter> shared_bitmap_manager_; | |
114 scoped_ptr<ResourceProvider> resource_provider3d_; | 89 scoped_ptr<ResourceProvider> resource_provider3d_; |
115 scoped_ptr<ResourceProvider> resource_provider_software_; | |
116 }; | 90 }; |
117 | 91 |
118 TEST_F(VideoResourceUpdaterTest, SoftwareFrame) { | 92 TEST_F(VideoResourceUpdaterTest, SoftwareFrame) { |
119 VideoResourceUpdater updater(output_surface3d_->context_provider(), | 93 VideoResourceUpdater updater(output_surface3d_->context_provider(), |
120 resource_provider3d_.get()); | 94 resource_provider3d_.get()); |
121 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | 95 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
122 | 96 |
123 VideoFrameExternalResources resources = | 97 VideoFrameExternalResources resources = |
124 updater.CreateExternalResourcesFromVideoFrame(video_frame); | 98 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
125 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 99 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
126 } | 100 } |
127 | 101 |
128 TEST_F(VideoResourceUpdaterTest, ReuseResource) { | 102 TEST_F(VideoResourceUpdaterTest, ReuseResource) { |
129 VideoResourceUpdater updater(output_surface3d_->context_provider(), | 103 VideoResourceUpdater updater(output_surface3d_->context_provider(), |
130 resource_provider3d_.get()); | 104 resource_provider3d_.get()); |
131 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | 105 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
132 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); | 106 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); |
133 | 107 |
134 // Allocate the resources for a YUV video frame. | 108 // Allocate the resources for a YUV video frame. |
135 context3d_->ResetUploadCount(); | 109 context3d_->ResetUploadCount(); |
136 VideoFrameExternalResources resources = | 110 VideoFrameExternalResources resources = |
137 updater.CreateExternalResourcesFromVideoFrame(video_frame); | 111 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
138 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 112 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
139 EXPECT_EQ(size_t(3), resources.mailboxes.size()); | 113 EXPECT_EQ(size_t(3), resources.mailboxes.size()); |
140 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); | 114 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); |
141 EXPECT_EQ(size_t(0), resources.software_resources.size()); | |
142 // Expect exactly three texture uploads, one for each plane. | 115 // Expect exactly three texture uploads, one for each plane. |
143 EXPECT_EQ(3, context3d_->UploadCount()); | 116 EXPECT_EQ(3, context3d_->UploadCount()); |
144 | 117 |
145 // Simulate the ResourceProvider releasing the resources back to the video | 118 // Simulate the ResourceProvider releasing the resources back to the video |
146 // updater. | 119 // updater. |
147 for (ReleaseCallbackImpl& release_callback : resources.release_callbacks) | 120 for (ReleaseCallbackImpl& release_callback : resources.release_callbacks) |
148 release_callback.Run(0, false, nullptr); | 121 release_callback.Run(0, false, nullptr); |
149 | 122 |
150 // Allocate resources for the same frame. | 123 // Allocate resources for the same frame. |
151 context3d_->ResetUploadCount(); | 124 context3d_->ResetUploadCount(); |
(...skipping 11 matching lines...) Expand all Loading... |
163 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | 136 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
164 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); | 137 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); |
165 | 138 |
166 // Allocate the resources for a YUV video frame. | 139 // Allocate the resources for a YUV video frame. |
167 context3d_->ResetUploadCount(); | 140 context3d_->ResetUploadCount(); |
168 VideoFrameExternalResources resources = | 141 VideoFrameExternalResources resources = |
169 updater.CreateExternalResourcesFromVideoFrame(video_frame); | 142 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
170 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 143 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
171 EXPECT_EQ(size_t(3), resources.mailboxes.size()); | 144 EXPECT_EQ(size_t(3), resources.mailboxes.size()); |
172 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); | 145 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); |
173 EXPECT_EQ(size_t(0), resources.software_resources.size()); | |
174 // Expect exactly three texture uploads, one for each plane. | 146 // Expect exactly three texture uploads, one for each plane. |
175 EXPECT_EQ(3, context3d_->UploadCount()); | 147 EXPECT_EQ(3, context3d_->UploadCount()); |
176 | 148 |
177 // Allocate resources for the same frame. | 149 // Allocate resources for the same frame. |
178 context3d_->ResetUploadCount(); | 150 context3d_->ResetUploadCount(); |
179 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); | 151 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); |
180 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 152 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
181 EXPECT_EQ(size_t(3), resources.mailboxes.size()); | 153 EXPECT_EQ(size_t(3), resources.mailboxes.size()); |
182 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); | 154 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); |
183 // The data should be reused so expect no texture uploads. | 155 // The data should be reused so expect no texture uploads. |
184 EXPECT_EQ(0, context3d_->UploadCount()); | 156 EXPECT_EQ(0, context3d_->UploadCount()); |
185 } | 157 } |
186 | 158 |
187 TEST_F(VideoResourceUpdaterTest, SoftwareFrameSoftwareCompositor) { | |
188 VideoResourceUpdater updater(nullptr, resource_provider_software_.get()); | |
189 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | |
190 | |
191 VideoFrameExternalResources resources = | |
192 updater.CreateExternalResourcesFromVideoFrame(video_frame); | |
193 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); | |
194 } | |
195 | |
196 TEST_F(VideoResourceUpdaterTest, ReuseResourceSoftwareCompositor) { | |
197 VideoResourceUpdater updater(nullptr, resource_provider_software_.get()); | |
198 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | |
199 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); | |
200 | |
201 // Allocate the resources for a software video frame. | |
202 shared_bitmap_manager_->ResetAllocationCount(); | |
203 VideoFrameExternalResources resources = | |
204 updater.CreateExternalResourcesFromVideoFrame(video_frame); | |
205 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); | |
206 EXPECT_EQ(size_t(0), resources.mailboxes.size()); | |
207 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); | |
208 EXPECT_EQ(size_t(1), resources.software_resources.size()); | |
209 // Expect exactly one allocated shared bitmap. | |
210 EXPECT_EQ(1, shared_bitmap_manager_->AllocationCount()); | |
211 | |
212 // Simulate the ResourceProvider releasing the resource back to the video | |
213 // updater. | |
214 resources.software_release_callback.Run(0, false, nullptr); | |
215 | |
216 // Allocate resources for the same frame. | |
217 shared_bitmap_manager_->ResetAllocationCount(); | |
218 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); | |
219 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); | |
220 EXPECT_EQ(size_t(0), resources.mailboxes.size()); | |
221 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); | |
222 EXPECT_EQ(size_t(1), resources.software_resources.size()); | |
223 // The data should be reused so expect no new allocations. | |
224 EXPECT_EQ(0, shared_bitmap_manager_->AllocationCount()); | |
225 } | |
226 | |
227 TEST_F(VideoResourceUpdaterTest, ReuseResourceNoDeleteSoftwareCompositor) { | |
228 VideoResourceUpdater updater(nullptr, resource_provider_software_.get()); | |
229 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | |
230 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); | |
231 | |
232 // Allocate the resources for a software video frame. | |
233 shared_bitmap_manager_->ResetAllocationCount(); | |
234 VideoFrameExternalResources resources = | |
235 updater.CreateExternalResourcesFromVideoFrame(video_frame); | |
236 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); | |
237 EXPECT_EQ(size_t(0), resources.mailboxes.size()); | |
238 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); | |
239 EXPECT_EQ(size_t(1), resources.software_resources.size()); | |
240 // Expect exactly one allocated shared bitmap. | |
241 EXPECT_EQ(1, shared_bitmap_manager_->AllocationCount()); | |
242 | |
243 // Allocate resources for the same frame. | |
244 shared_bitmap_manager_->ResetAllocationCount(); | |
245 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); | |
246 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); | |
247 EXPECT_EQ(size_t(0), resources.mailboxes.size()); | |
248 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); | |
249 EXPECT_EQ(size_t(1), resources.software_resources.size()); | |
250 // The data should be reused so expect no new allocations. | |
251 EXPECT_EQ(0, shared_bitmap_manager_->AllocationCount()); | |
252 } | |
253 | |
254 } // namespace | 159 } // namespace |
255 } // namespace cc | 160 } // namespace cc |
OLD | NEW |