[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / cc / resources / video_resource_updater_unittest.cc
blob3d4bf1778ba9504f583d0d8a4b2d126fdf4ee198
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
3 // found in the LICENSE file.
5 #include "cc/resources/video_resource_updater.h"
7 #include "cc/resources/resource_provider.h"
8 #include "cc/test/fake_output_surface.h"
9 #include "cc/test/fake_output_surface_client.h"
10 #include "cc/test/fake_resource_provider.h"
11 #include "cc/test/test_shared_bitmap_manager.h"
12 #include "cc/test/test_web_graphics_context_3d.h"
13 #include "cc/trees/blocking_task_runner.h"
14 #include "gpu/GLES2/gl2extchromium.h"
15 #include "media/base/video_frame.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace cc {
19 namespace {
21 class WebGraphicsContext3DUploadCounter : public TestWebGraphicsContext3D {
22 public:
23 void texSubImage2D(GLenum target,
24 GLint level,
25 GLint xoffset,
26 GLint yoffset,
27 GLsizei width,
28 GLsizei height,
29 GLenum format,
30 GLenum type,
31 const void* pixels) override {
32 ++upload_count_;
35 int UploadCount() { return upload_count_; }
36 void ResetUploadCount() { upload_count_ = 0; }
38 private:
39 int upload_count_;
42 class SharedBitmapManagerAllocationCounter : public TestSharedBitmapManager {
43 public:
44 scoped_ptr<SharedBitmap> AllocateSharedBitmap(
45 const gfx::Size& size) override {
46 ++allocation_count_;
47 return TestSharedBitmapManager::AllocateSharedBitmap(size);
50 int AllocationCount() { return allocation_count_; }
51 void ResetAllocationCount() { allocation_count_ = 0; }
53 private:
54 int allocation_count_;
57 class VideoResourceUpdaterTest : public testing::Test {
58 protected:
59 VideoResourceUpdaterTest() {
60 scoped_ptr<WebGraphicsContext3DUploadCounter> context3d(
61 new WebGraphicsContext3DUploadCounter());
63 context3d_ = context3d.get();
65 output_surface3d_ =
66 FakeOutputSurface::Create3d(context3d.Pass());
67 CHECK(output_surface3d_->BindToClient(&client_));
69 output_surface_software_ = FakeOutputSurface::CreateSoftware(
70 make_scoped_ptr(new SoftwareOutputDevice));
71 CHECK(output_surface_software_->BindToClient(&client_));
73 shared_bitmap_manager_.reset(new SharedBitmapManagerAllocationCounter());
74 resource_provider3d_ = FakeResourceProvider::Create(
75 output_surface3d_.get(), shared_bitmap_manager_.get());
77 resource_provider_software_ = FakeResourceProvider::Create(
78 output_surface_software_.get(), shared_bitmap_manager_.get());
81 scoped_refptr<media::VideoFrame> CreateTestYUVVideoFrame() {
82 const int kDimension = 10;
83 gfx::Size size(kDimension, kDimension);
84 static uint8 y_data[kDimension * kDimension] = { 0 };
85 static uint8 u_data[kDimension * kDimension / 2] = { 0 };
86 static uint8 v_data[kDimension * kDimension / 2] = { 0 };
88 return media::VideoFrame::WrapExternalYuvData(
89 media::PIXEL_FORMAT_YV16, // format
90 size, // coded_size
91 gfx::Rect(size), // visible_rect
92 size, // natural_size
93 size.width(), // y_stride
94 size.width() / 2, // u_stride
95 size.width() / 2, // v_stride
96 y_data, // y_data
97 u_data, // u_data
98 v_data, // v_data
99 base::TimeDelta()); // timestamp
102 static void ReleaseMailboxCB(unsigned sync_point) {}
104 scoped_refptr<media::VideoFrame> CreateTestRGBAHardwareVideoFrame() {
105 const int kDimension = 10;
106 gfx::Size size(kDimension, kDimension);
108 gpu::Mailbox mailbox;
109 mailbox.name[0] = 51;
111 const unsigned sync_point = 7;
112 const unsigned target = GL_TEXTURE_2D;
113 return media::VideoFrame::WrapNativeTexture(
114 media::PIXEL_FORMAT_ARGB,
115 gpu::MailboxHolder(mailbox, target, sync_point),
116 base::Bind(&ReleaseMailboxCB),
117 size, // coded_size
118 gfx::Rect(size), // visible_rect
119 size, // natural_size
120 base::TimeDelta()); // timestamp
123 scoped_refptr<media::VideoFrame> CreateTestYUVHardareVideoFrame() {
124 const int kDimension = 10;
125 gfx::Size size(kDimension, kDimension);
127 const int kPlanesNum = 3;
128 gpu::Mailbox mailbox[kPlanesNum];
129 for (int i = 0; i < kPlanesNum; ++i) {
130 mailbox[i].name[0] = 50 + 1;
132 const unsigned sync_point = 7;
133 const unsigned target = GL_TEXTURE_RECTANGLE_ARB;
134 return media::VideoFrame::WrapYUV420NativeTextures(
135 gpu::MailboxHolder(mailbox[media::VideoFrame::kYPlane], target,
136 sync_point),
137 gpu::MailboxHolder(mailbox[media::VideoFrame::kUPlane], target,
138 sync_point),
139 gpu::MailboxHolder(mailbox[media::VideoFrame::kVPlane], target,
140 sync_point),
141 base::Bind(&ReleaseMailboxCB),
142 size, // coded_size
143 gfx::Rect(size), // visible_rect
144 size, // natural_size
145 base::TimeDelta()); // timestamp
148 WebGraphicsContext3DUploadCounter* context3d_;
149 FakeOutputSurfaceClient client_;
150 scoped_ptr<FakeOutputSurface> output_surface3d_;
151 scoped_ptr<FakeOutputSurface> output_surface_software_;
152 scoped_ptr<SharedBitmapManagerAllocationCounter> shared_bitmap_manager_;
153 scoped_ptr<ResourceProvider> resource_provider3d_;
154 scoped_ptr<ResourceProvider> resource_provider_software_;
157 TEST_F(VideoResourceUpdaterTest, SoftwareFrame) {
158 VideoResourceUpdater updater(output_surface3d_->context_provider(),
159 resource_provider3d_.get());
160 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame();
162 VideoFrameExternalResources resources =
163 updater.CreateExternalResourcesFromVideoFrame(video_frame);
164 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type);
167 TEST_F(VideoResourceUpdaterTest, ReuseResource) {
168 VideoResourceUpdater updater(output_surface3d_->context_provider(),
169 resource_provider3d_.get());
170 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame();
171 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
173 // Allocate the resources for a YUV video frame.
174 context3d_->ResetUploadCount();
175 VideoFrameExternalResources resources =
176 updater.CreateExternalResourcesFromVideoFrame(video_frame);
177 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type);
178 EXPECT_EQ(size_t(3), resources.mailboxes.size());
179 EXPECT_EQ(size_t(3), resources.release_callbacks.size());
180 EXPECT_EQ(size_t(0), resources.software_resources.size());
181 // Expect exactly three texture uploads, one for each plane.
182 EXPECT_EQ(3, context3d_->UploadCount());
184 // Simulate the ResourceProvider releasing the resources back to the video
185 // updater.
186 for (ReleaseCallbackImpl& release_callback : resources.release_callbacks)
187 release_callback.Run(0, false, nullptr);
189 // Allocate resources for the same frame.
190 context3d_->ResetUploadCount();
191 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame);
192 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type);
193 EXPECT_EQ(size_t(3), resources.mailboxes.size());
194 EXPECT_EQ(size_t(3), resources.release_callbacks.size());
195 // The data should be reused so expect no texture uploads.
196 EXPECT_EQ(0, context3d_->UploadCount());
199 TEST_F(VideoResourceUpdaterTest, ReuseResourceNoDelete) {
200 VideoResourceUpdater updater(output_surface3d_->context_provider(),
201 resource_provider3d_.get());
202 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame();
203 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
205 // Allocate the resources for a YUV video frame.
206 context3d_->ResetUploadCount();
207 VideoFrameExternalResources resources =
208 updater.CreateExternalResourcesFromVideoFrame(video_frame);
209 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type);
210 EXPECT_EQ(size_t(3), resources.mailboxes.size());
211 EXPECT_EQ(size_t(3), resources.release_callbacks.size());
212 EXPECT_EQ(size_t(0), resources.software_resources.size());
213 // Expect exactly three texture uploads, one for each plane.
214 EXPECT_EQ(3, context3d_->UploadCount());
216 // Allocate resources for the same frame.
217 context3d_->ResetUploadCount();
218 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame);
219 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type);
220 EXPECT_EQ(size_t(3), resources.mailboxes.size());
221 EXPECT_EQ(size_t(3), resources.release_callbacks.size());
222 // The data should be reused so expect no texture uploads.
223 EXPECT_EQ(0, context3d_->UploadCount());
226 TEST_F(VideoResourceUpdaterTest, SoftwareFrameSoftwareCompositor) {
227 VideoResourceUpdater updater(nullptr, resource_provider_software_.get());
228 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame();
230 VideoFrameExternalResources resources =
231 updater.CreateExternalResourcesFromVideoFrame(video_frame);
232 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type);
235 TEST_F(VideoResourceUpdaterTest, ReuseResourceSoftwareCompositor) {
236 VideoResourceUpdater updater(nullptr, resource_provider_software_.get());
237 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame();
238 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
240 // Allocate the resources for a software video frame.
241 shared_bitmap_manager_->ResetAllocationCount();
242 VideoFrameExternalResources resources =
243 updater.CreateExternalResourcesFromVideoFrame(video_frame);
244 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type);
245 EXPECT_EQ(size_t(0), resources.mailboxes.size());
246 EXPECT_EQ(size_t(0), resources.release_callbacks.size());
247 EXPECT_EQ(size_t(1), resources.software_resources.size());
248 // Expect exactly one allocated shared bitmap.
249 EXPECT_EQ(1, shared_bitmap_manager_->AllocationCount());
251 // Simulate the ResourceProvider releasing the resource back to the video
252 // updater.
253 resources.software_release_callback.Run(0, false, nullptr);
255 // Allocate resources for the same frame.
256 shared_bitmap_manager_->ResetAllocationCount();
257 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame);
258 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type);
259 EXPECT_EQ(size_t(0), resources.mailboxes.size());
260 EXPECT_EQ(size_t(0), resources.release_callbacks.size());
261 EXPECT_EQ(size_t(1), resources.software_resources.size());
262 // The data should be reused so expect no new allocations.
263 EXPECT_EQ(0, shared_bitmap_manager_->AllocationCount());
266 TEST_F(VideoResourceUpdaterTest, ReuseResourceNoDeleteSoftwareCompositor) {
267 VideoResourceUpdater updater(nullptr, resource_provider_software_.get());
268 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame();
269 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
271 // Allocate the resources for a software video frame.
272 shared_bitmap_manager_->ResetAllocationCount();
273 VideoFrameExternalResources resources =
274 updater.CreateExternalResourcesFromVideoFrame(video_frame);
275 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type);
276 EXPECT_EQ(size_t(0), resources.mailboxes.size());
277 EXPECT_EQ(size_t(0), resources.release_callbacks.size());
278 EXPECT_EQ(size_t(1), resources.software_resources.size());
279 // Expect exactly one allocated shared bitmap.
280 EXPECT_EQ(1, shared_bitmap_manager_->AllocationCount());
282 // Allocate resources for the same frame.
283 shared_bitmap_manager_->ResetAllocationCount();
284 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame);
285 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type);
286 EXPECT_EQ(size_t(0), resources.mailboxes.size());
287 EXPECT_EQ(size_t(0), resources.release_callbacks.size());
288 EXPECT_EQ(size_t(1), resources.software_resources.size());
289 // The data should be reused so expect no new allocations.
290 EXPECT_EQ(0, shared_bitmap_manager_->AllocationCount());
293 TEST_F(VideoResourceUpdaterTest, CreateForHardwarePlanes) {
294 VideoResourceUpdater updater(output_surface3d_->context_provider(),
295 resource_provider3d_.get());
297 scoped_refptr<media::VideoFrame> video_frame =
298 CreateTestRGBAHardwareVideoFrame();
300 VideoFrameExternalResources resources =
301 updater.CreateExternalResourcesFromVideoFrame(video_frame);
302 EXPECT_EQ(VideoFrameExternalResources::RGBA_RESOURCE, resources.type);
303 EXPECT_EQ(1u, resources.mailboxes.size());
304 EXPECT_EQ(1u, resources.release_callbacks.size());
305 EXPECT_EQ(0u, resources.software_resources.size());
307 video_frame = CreateTestYUVHardareVideoFrame();
309 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame);
310 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type);
311 EXPECT_TRUE(resources.read_lock_fences_enabled);
312 EXPECT_EQ(3u, resources.mailboxes.size());
313 EXPECT_EQ(3u, resources.release_callbacks.size());
314 EXPECT_EQ(0u, resources.software_resources.size());
316 } // namespace
317 } // namespace cc