forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_resource_updater_unittest.cc
834 lines (705 loc) · 35.3 KB
/
video_resource_updater_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/renderers/video_resource_updater.h"
#include <stddef.h>
#include <stdint.h>
#include "base/bind.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/test/task_environment.h"
#include "components/viz/client/client_resource_provider.h"
#include "components/viz/client/shared_bitmap_reporter.h"
#include "components/viz/test/fake_output_surface.h"
#include "components/viz/test/test_gles2_interface.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "media/base/video_frame.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace {
class FakeSharedBitmapReporter : public viz::SharedBitmapReporter {
public:
FakeSharedBitmapReporter() = default;
~FakeSharedBitmapReporter() override = default;
// viz::SharedBitmapReporter implementation.
void DidAllocateSharedBitmap(base::ReadOnlySharedMemoryRegion region,
const viz::SharedBitmapId& id) override {
DCHECK_EQ(shared_bitmaps_.count(id), 0u);
shared_bitmaps_.insert(id);
}
void DidDeleteSharedBitmap(const viz::SharedBitmapId& id) override {
DCHECK_EQ(shared_bitmaps_.count(id), 1u);
shared_bitmaps_.erase(id);
}
const base::flat_set<viz::SharedBitmapId> shared_bitmaps() const {
return shared_bitmaps_;
}
private:
base::flat_set<viz::SharedBitmapId> shared_bitmaps_;
};
class UploadCounterGLES2Interface : public viz::TestGLES2Interface {
public:
void TexSubImage2D(GLenum target,
GLint level,
GLint xoffset,
GLint yoffset,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
const void* pixels) override {
++upload_count_;
}
int UploadCount() { return upload_count_; }
void ResetUploadCount() { upload_count_ = 0; }
private:
int upload_count_;
};
class VideoResourceUpdaterTest : public testing::Test {
protected:
VideoResourceUpdaterTest() {
auto gl = std::make_unique<UploadCounterGLES2Interface>();
gl_ = gl.get();
context_provider_ = viz::TestContextProvider::Create(std::move(gl));
context_provider_->BindToCurrentThread();
}
// testing::Test implementation.
void SetUp() override {
testing::Test::SetUp();
resource_provider_ = std::make_unique<viz::ClientResourceProvider>();
}
std::unique_ptr<VideoResourceUpdater> CreateUpdaterForHardware(
bool use_stream_video_draw_quad = false) {
return std::make_unique<VideoResourceUpdater>(
context_provider_.get(), /*raster_context_provider=*/nullptr, nullptr,
resource_provider_.get(), use_stream_video_draw_quad,
/*use_gpu_memory_buffer_resources=*/false,
/*use_r16_texture=*/use_r16_texture_, /*max_resource_size=*/10000);
}
std::unique_ptr<VideoResourceUpdater> CreateUpdaterForSoftware() {
return std::make_unique<VideoResourceUpdater>(
/*context_provider=*/nullptr, /*raster_context_provider=*/nullptr,
&shared_bitmap_reporter_, resource_provider_.get(),
/*use_stream_video_draw_quad=*/false,
/*use_gpu_memory_buffer_resources=*/false,
/*use_r16_texture=*/false,
/*max_resource_size=*/10000);
}
// Note that the number of pixels needed for |size| must be less than or equal
// to the number of pixels needed for size of 100x100.
scoped_refptr<VideoFrame> CreateTestYUVVideoFrame(
const gfx::Size& size = gfx::Size(10, 10)) {
constexpr int kMaxDimension = 100;
static uint8_t y_data[kMaxDimension * kMaxDimension] = {0};
static uint8_t u_data[kMaxDimension * kMaxDimension / 2] = {0};
static uint8_t v_data[kMaxDimension * kMaxDimension / 2] = {0};
CHECK_LE(size.width() * size.height(), kMaxDimension * kMaxDimension);
scoped_refptr<VideoFrame> video_frame =
VideoFrame::WrapExternalYuvData(PIXEL_FORMAT_I422, // format
size, // coded_size
gfx::Rect(size), // visible_rect
size, // natural_size
size.width(), // y_stride
size.width() / 2, // u_stride
size.width() / 2, // v_stride
y_data, // y_data
u_data, // u_data
v_data, // v_data
base::TimeDelta()); // timestamp
EXPECT_TRUE(video_frame);
return video_frame;
}
scoped_refptr<VideoFrame> CreateWonkyTestYUVVideoFrame() {
const int kDimension = 10;
const int kYWidth = kDimension + 5;
const int kUWidth = (kYWidth + 1) / 2 + 200;
const int kVWidth = (kYWidth + 1) / 2 + 1;
static uint8_t y_data[kYWidth * kDimension] = {0};
static uint8_t u_data[kUWidth * kDimension] = {0};
static uint8_t v_data[kVWidth * kDimension] = {0};
scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapExternalYuvData(
PIXEL_FORMAT_I422, // format
gfx::Size(kYWidth, kDimension), // coded_size
gfx::Rect(2, 0, kDimension, kDimension), // visible_rect
gfx::Size(kDimension, kDimension), // natural_size
-kYWidth, // y_stride (negative)
kUWidth, // u_stride
kVWidth, // v_stride
y_data + kYWidth * (kDimension - 1), // y_data
u_data, // u_data
v_data, // v_data
base::TimeDelta()); // timestamp
EXPECT_TRUE(video_frame);
return video_frame;
}
scoped_refptr<VideoFrame> CreateTestRGBVideoFrame(VideoPixelFormat format) {
constexpr int kMaxDimension = 10;
constexpr gfx::Size kSize = gfx::Size(kMaxDimension, kMaxDimension);
static uint32_t rgb_data[kMaxDimension * kMaxDimension] = {0};
scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapExternalData(
format, // format
kSize, // coded_size
gfx::Rect(kSize), // visible_rect
kSize, // natural_size
reinterpret_cast<uint8_t*>(rgb_data), // data,
sizeof(rgb_data), // data_size
base::TimeDelta()); // timestamp
EXPECT_TRUE(video_frame);
return video_frame;
}
scoped_refptr<VideoFrame> CreateTestHighBitFrame() {
const int kDimension = 10;
gfx::Size size(kDimension, kDimension);
scoped_refptr<VideoFrame> video_frame(
VideoFrame::CreateFrame(PIXEL_FORMAT_YUV420P10, size, gfx::Rect(size),
size, base::TimeDelta()));
EXPECT_TRUE(video_frame);
return video_frame;
}
void SetReleaseSyncToken(const gpu::SyncToken& sync_token) {
release_sync_token_ = sync_token;
}
scoped_refptr<VideoFrame> CreateTestHardwareVideoFrame(
VideoPixelFormat format,
unsigned target) {
const int kDimension = 10;
gfx::Size size(kDimension, kDimension);
auto mailbox = gpu::Mailbox::GenerateForSharedImage();
gpu::MailboxHolder mailbox_holders[VideoFrame::kMaxPlanes] = {
gpu::MailboxHolder(mailbox, kMailboxSyncToken, target)};
scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapNativeTextures(
format, mailbox_holders,
base::BindOnce(&VideoResourceUpdaterTest::SetReleaseSyncToken,
base::Unretained(this)),
size, // coded_size
gfx::Rect(size), // visible_rect
size, // natural_size
base::TimeDelta()); // timestamp
EXPECT_TRUE(video_frame);
return video_frame;
}
scoped_refptr<VideoFrame> CreateTestRGBAHardwareVideoFrame() {
return CreateTestHardwareVideoFrame(PIXEL_FORMAT_ARGB, GL_TEXTURE_2D);
}
scoped_refptr<VideoFrame> CreateTestStreamTextureHardwareVideoFrame(
absl::optional<VideoFrameMetadata::CopyMode> copy_mode) {
scoped_refptr<VideoFrame> video_frame = CreateTestHardwareVideoFrame(
PIXEL_FORMAT_ARGB, GL_TEXTURE_EXTERNAL_OES);
video_frame->metadata().copy_mode = std::move(copy_mode);
return video_frame;
}
scoped_refptr<VideoFrame> CreateTestYuvHardwareVideoFrame(
VideoPixelFormat format,
size_t num_textures,
unsigned target) {
const int kDimension = 10;
gfx::Size size(kDimension, kDimension);
gpu::MailboxHolder mailbox_holders[VideoFrame::kMaxPlanes];
for (size_t i = 0; i < num_textures; ++i) {
gpu::Mailbox mailbox;
mailbox.name[0] = 50 + 1;
mailbox_holders[i] =
gpu::MailboxHolder(mailbox, kMailboxSyncToken, target);
}
scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapNativeTextures(
format, mailbox_holders,
base::BindOnce(&VideoResourceUpdaterTest::SetReleaseSyncToken,
base::Unretained(this)),
size, // coded_size
gfx::Rect(size), // visible_rect
size, // natural_size
base::TimeDelta()); // timestamp
EXPECT_TRUE(video_frame);
return video_frame;
}
size_t GetSharedImageCount() {
return context_provider_->SharedImageInterface()->shared_image_count();
}
static const gpu::SyncToken kMailboxSyncToken;
// VideoResourceUpdater registers as a MemoryDumpProvider, which requires
// a TaskRunner.
base::test::SingleThreadTaskEnvironment task_environment_;
UploadCounterGLES2Interface* gl_;
scoped_refptr<viz::TestContextProvider> context_provider_;
FakeSharedBitmapReporter shared_bitmap_reporter_;
std::unique_ptr<viz::ClientResourceProvider> resource_provider_;
gpu::SyncToken release_sync_token_;
bool use_r16_texture_ = false;
};
const gpu::SyncToken VideoResourceUpdaterTest::kMailboxSyncToken =
gpu::SyncToken(gpu::CommandBufferNamespace::GPU_IO,
gpu::CommandBufferId::FromUnsafeValue(0x123),
7);
TEST_F(VideoResourceUpdaterTest, SoftwareFrame) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestYUVVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
}
TEST_F(VideoResourceUpdaterTest, SoftwareFrameRGB) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
for (const auto& fmt : {PIXEL_FORMAT_XBGR, PIXEL_FORMAT_XRGB,
PIXEL_FORMAT_ABGR, PIXEL_FORMAT_ARGB}) {
scoped_refptr<VideoFrame> video_frame = CreateTestRGBVideoFrame(fmt);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA, resources.type);
}
}
TEST_F(VideoResourceUpdaterTest, HighBitFrameNoF16) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestHighBitFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
}
class VideoResourceUpdaterTestWithF16 : public VideoResourceUpdaterTest {
public:
VideoResourceUpdaterTestWithF16() : VideoResourceUpdaterTest() {
gl_->set_support_texture_half_float_linear(true);
}
};
TEST_F(VideoResourceUpdaterTestWithF16, HighBitFrame) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestHighBitFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_NEAR(resources.multiplier, 2.0, 0.1);
EXPECT_NEAR(resources.offset, 0.5, 0.1);
// Create the resource again, to test the path where the
// resources are cached.
VideoFrameExternalResources resources2 =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources2.type);
EXPECT_NEAR(resources2.multiplier, 2.0, 0.1);
EXPECT_NEAR(resources2.offset, 0.5, 0.1);
}
class VideoResourceUpdaterTestWithR16 : public VideoResourceUpdaterTest {
public:
VideoResourceUpdaterTestWithR16() : VideoResourceUpdaterTest() {
use_r16_texture_ = true;
gl_->set_support_texture_norm16(true);
}
};
TEST_F(VideoResourceUpdaterTestWithR16, HighBitFrame) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestHighBitFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
// Max 10-bit values as read by a sampler.
double max_10bit_value = ((1 << 10) - 1) / 65535.0;
EXPECT_NEAR(resources.multiplier * max_10bit_value, 1.0, 0.0001);
EXPECT_NEAR(resources.offset, 0.0, 0.1);
// Create the resource again, to test the path where the
// resources are cached.
VideoFrameExternalResources resources2 =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources2.type);
EXPECT_NEAR(resources2.multiplier * max_10bit_value, 1.0, 0.0001);
EXPECT_NEAR(resources2.offset, 0.0, 0.1);
}
TEST_F(VideoResourceUpdaterTest, HighBitFrameSoftwareCompositor) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
scoped_refptr<VideoFrame> video_frame = CreateTestHighBitFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
}
TEST_F(VideoResourceUpdaterTest, WonkySoftwareFrame) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateWonkyTestYUVVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
}
TEST_F(VideoResourceUpdaterTest, WonkySoftwareFrameSoftwareCompositor) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
scoped_refptr<VideoFrame> video_frame = CreateWonkyTestYUVVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
}
TEST_F(VideoResourceUpdaterTest, ReuseResource) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestYUVVideoFrame();
video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
// Allocate the resources for a YUV video frame.
gl_->ResetUploadCount();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(3u, resources.resources.size());
EXPECT_EQ(3u, resources.release_callbacks.size());
// Expect exactly three texture uploads, one for each plane.
EXPECT_EQ(3, gl_->UploadCount());
// Simulate the ResourceProvider releasing the resources back to the video
// updater.
for (auto& release_callback : resources.release_callbacks)
std::move(release_callback).Run(gpu::SyncToken(), false);
// Allocate resources for the same frame.
gl_->ResetUploadCount();
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(3u, resources.resources.size());
EXPECT_EQ(3u, resources.release_callbacks.size());
// The data should be reused so expect no texture uploads.
EXPECT_EQ(0, gl_->UploadCount());
}
TEST_F(VideoResourceUpdaterTest, ReuseResourceNoDelete) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestYUVVideoFrame();
video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
// Allocate the resources for a YUV video frame.
gl_->ResetUploadCount();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(3u, resources.resources.size());
EXPECT_EQ(3u, resources.release_callbacks.size());
// Expect exactly three texture uploads, one for each plane.
EXPECT_EQ(3, gl_->UploadCount());
// Allocate resources for the same frame.
gl_->ResetUploadCount();
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(3u, resources.resources.size());
EXPECT_EQ(3u, resources.release_callbacks.size());
// The data should be reused so expect no texture uploads.
EXPECT_EQ(0, gl_->UploadCount());
}
TEST_F(VideoResourceUpdaterTest, SoftwareFrameSoftwareCompositor) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
scoped_refptr<VideoFrame> video_frame = CreateTestYUVVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
}
TEST_F(VideoResourceUpdaterTest, SoftwareFrameRGBSoftwareCompositor) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
for (const auto& fmt : {PIXEL_FORMAT_XBGR, PIXEL_FORMAT_XRGB,
PIXEL_FORMAT_ABGR, PIXEL_FORMAT_ARGB}) {
scoped_refptr<VideoFrame> video_frame = CreateTestRGBVideoFrame(fmt);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
}
}
TEST_F(VideoResourceUpdaterTest, ReuseResourceSoftwareCompositor) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
scoped_refptr<VideoFrame> video_frame = CreateTestYUVVideoFrame();
video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
// Allocate the resources for a software video frame.
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(1u, resources.release_callbacks.size());
// Expect exactly one allocated shared bitmap.
EXPECT_EQ(1u, shared_bitmap_reporter_.shared_bitmaps().size());
auto shared_bitmaps_copy = shared_bitmap_reporter_.shared_bitmaps();
// Simulate the ResourceProvider releasing the resource back to the video
// updater.
std::move(resources.release_callbacks[0]).Run(gpu::SyncToken(), false);
// Allocate resources for the same frame.
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(1u, resources.release_callbacks.size());
// Ensure that the same shared bitmap was reused.
EXPECT_EQ(shared_bitmap_reporter_.shared_bitmaps(), shared_bitmaps_copy);
}
TEST_F(VideoResourceUpdaterTest, ReuseResourceNoDeleteSoftwareCompositor) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
scoped_refptr<VideoFrame> video_frame = CreateTestYUVVideoFrame();
video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234));
// Allocate the resources for a software video frame.
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(1u, resources.release_callbacks.size());
// Expect exactly one allocated shared bitmap.
EXPECT_EQ(1u, shared_bitmap_reporter_.shared_bitmaps().size());
auto shared_bitmaps_copy = shared_bitmap_reporter_.shared_bitmaps();
// Allocate resources for the same frame.
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(1u, resources.release_callbacks.size());
// Ensure that the same shared bitmap was reused.
EXPECT_EQ(shared_bitmap_reporter_.shared_bitmaps(), shared_bitmaps_copy);
}
TEST_F(VideoResourceUpdaterTest, ChangeResourceSizeSoftwareCompositor) {
constexpr gfx::Size kSize1(10, 10);
constexpr gfx::Size kSize2(20, 20);
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForSoftware();
// Allocate the resources for a software video frame.
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(
CreateTestYUVVideoFrame(kSize1));
// Expect exactly one allocated shared bitmap.
EXPECT_EQ(1u, shared_bitmap_reporter_.shared_bitmaps().size());
auto shared_bitmaps_copy = shared_bitmap_reporter_.shared_bitmaps();
// Simulate the ResourceProvider releasing the resource back to the video
// updater.
std::move(resources.release_callbacks[0]).Run(gpu::SyncToken(), false);
// Allocate resources for the next frame with a different size.
resources = updater->CreateExternalResourcesFromVideoFrame(
CreateTestYUVVideoFrame(kSize2));
// The first resource was released, so it can be reused but it's the wrong
// size. We should expect the first shared bitmap to be deleted and a new
// shared bitmap to be allocated.
EXPECT_EQ(1u, shared_bitmap_reporter_.shared_bitmaps().size());
EXPECT_NE(shared_bitmap_reporter_.shared_bitmaps(), shared_bitmaps_copy);
}
TEST_F(VideoResourceUpdaterTest, CreateForHardwarePlanes) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestRGBAHardwareVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(1u, resources.release_callbacks.size());
video_frame = CreateTestYuvHardwareVideoFrame(PIXEL_FORMAT_I420, 3,
GL_TEXTURE_RECTANGLE_ARB);
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(3u, resources.resources.size());
EXPECT_EQ(3u, resources.release_callbacks.size());
EXPECT_FALSE(resources.resources[0].read_lock_fences_enabled);
EXPECT_FALSE(resources.resources[1].read_lock_fences_enabled);
EXPECT_FALSE(resources.resources[2].read_lock_fences_enabled);
video_frame = CreateTestYuvHardwareVideoFrame(PIXEL_FORMAT_I420, 3,
GL_TEXTURE_RECTANGLE_ARB);
video_frame->metadata().read_lock_fences_enabled = true;
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_TRUE(resources.resources[0].read_lock_fences_enabled);
EXPECT_TRUE(resources.resources[1].read_lock_fences_enabled);
EXPECT_TRUE(resources.resources[2].read_lock_fences_enabled);
}
TEST_F(VideoResourceUpdaterTest,
CreateForHardwarePlanes_StreamTexture_CopyToNewTexture) {
// Note that |use_stream_video_draw_quad| is true for this test.
std::unique_ptr<VideoResourceUpdater> updater =
CreateUpdaterForHardware(true);
EXPECT_EQ(0u, GetSharedImageCount());
scoped_refptr<VideoFrame> video_frame =
CreateTestStreamTextureHardwareVideoFrame(absl::nullopt);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::STREAM_TEXTURE, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_EXTERNAL_OES,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(1u, resources.release_callbacks.size());
EXPECT_EQ(0u, GetSharedImageCount());
// A copied stream texture should return an RGBA resource in a new
// GL_TEXTURE_2D texture.
video_frame = CreateTestStreamTextureHardwareVideoFrame(
VideoFrameMetadata::CopyMode::kCopyToNewTexture);
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_2D,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(1u, resources.release_callbacks.size());
EXPECT_EQ(1u, GetSharedImageCount());
}
TEST_F(VideoResourceUpdaterTest,
CreateForHardwarePlanes_StreamTexture_CopyMailboxesOnly) {
// Note that |use_stream_video_draw_quad| is true for this test.
std::unique_ptr<VideoResourceUpdater> updater =
CreateUpdaterForHardware(true);
EXPECT_EQ(0u, GetSharedImageCount());
scoped_refptr<VideoFrame> video_frame =
CreateTestStreamTextureHardwareVideoFrame(absl::nullopt);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::STREAM_TEXTURE, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_EXTERNAL_OES,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(1u, resources.release_callbacks.size());
EXPECT_EQ(0u, GetSharedImageCount());
// If mailbox is copied, the texture target should still be
// GL_TEXTURE_EXTERNAL_OES and resource type should be STREAM_TEXTURE.
video_frame = CreateTestStreamTextureHardwareVideoFrame(
VideoFrameMetadata::CopyMode::kCopyMailboxesOnly);
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::STREAM_TEXTURE, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_EXTERNAL_OES,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(1u, resources.release_callbacks.size());
// This count will be 1 since a new mailbox will be created when mailbox is
// being copied.
EXPECT_EQ(1u, GetSharedImageCount());
}
TEST_F(VideoResourceUpdaterTest, CreateForHardwarePlanes_TextureQuad) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
EXPECT_EQ(0u, GetSharedImageCount());
scoped_refptr<VideoFrame> video_frame =
CreateTestStreamTextureHardwareVideoFrame(absl::nullopt);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGBA_PREMULTIPLIED, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_EXTERNAL_OES,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(1u, resources.release_callbacks.size());
EXPECT_EQ(0u, GetSharedImageCount());
}
// Passthrough the sync token returned by the compositor if we don't have an
// existing release sync token.
TEST_F(VideoResourceUpdaterTest, PassReleaseSyncToken) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
const gpu::SyncToken sync_token(gpu::CommandBufferNamespace::GPU_IO,
gpu::CommandBufferId::FromUnsafeValue(0x123),
123);
{
scoped_refptr<VideoFrame> video_frame = CreateTestRGBAHardwareVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
ASSERT_EQ(resources.release_callbacks.size(), 1u);
std::move(resources.release_callbacks[0]).Run(sync_token, false);
}
EXPECT_EQ(release_sync_token_, sync_token);
}
// Generate new sync token because video frame has an existing sync token.
TEST_F(VideoResourceUpdaterTest, GenerateReleaseSyncToken) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
const gpu::SyncToken sync_token1(gpu::CommandBufferNamespace::GPU_IO,
gpu::CommandBufferId::FromUnsafeValue(0x123),
123);
const gpu::SyncToken sync_token2(gpu::CommandBufferNamespace::GPU_IO,
gpu::CommandBufferId::FromUnsafeValue(0x234),
234);
{
scoped_refptr<VideoFrame> video_frame = CreateTestRGBAHardwareVideoFrame();
VideoFrameExternalResources resources1 =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
ASSERT_EQ(resources1.release_callbacks.size(), 1u);
std::move(resources1.release_callbacks[0]).Run(sync_token1, false);
VideoFrameExternalResources resources2 =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
ASSERT_EQ(resources2.release_callbacks.size(), 1u);
std::move(resources2.release_callbacks[0]).Run(sync_token2, false);
}
EXPECT_TRUE(release_sync_token_.HasData());
EXPECT_NE(release_sync_token_, sync_token1);
EXPECT_NE(release_sync_token_, sync_token2);
}
// Pass mailbox sync token as is if no GL operations are performed before frame
// resources are handed off to the compositor.
TEST_F(VideoResourceUpdaterTest, PassMailboxSyncToken) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame = CreateTestRGBAHardwareVideoFrame();
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
ASSERT_EQ(resources.resources.size(), 1u);
EXPECT_TRUE(resources.resources[0].mailbox_holder.sync_token.HasData());
EXPECT_EQ(resources.resources[0].mailbox_holder.sync_token,
kMailboxSyncToken);
}
// Generate new sync token for compositor when copying the texture.
TEST_F(VideoResourceUpdaterTest, GenerateSyncTokenOnTextureCopy) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
scoped_refptr<VideoFrame> video_frame =
CreateTestStreamTextureHardwareVideoFrame(
VideoFrameMetadata::CopyMode::kCopyToNewTexture);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
ASSERT_EQ(resources.resources.size(), 1u);
EXPECT_TRUE(resources.resources[0].mailbox_holder.sync_token.HasData());
EXPECT_NE(resources.resources[0].mailbox_holder.sync_token,
kMailboxSyncToken);
}
// NV12 VideoFrames backed by a single native texture can be sampled out
// by GL as RGB. To use them as HW overlays we need to know the format
// of the underlying buffer, that is YUV_420_BIPLANAR.
TEST_F(VideoResourceUpdaterTest, CreateForHardwarePlanes_SingleNV12) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
EXPECT_EQ(0u, GetSharedImageCount());
scoped_refptr<VideoFrame> video_frame =
CreateTestHardwareVideoFrame(PIXEL_FORMAT_NV12, GL_TEXTURE_EXTERNAL_OES);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGB, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_EXTERNAL_OES,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(viz::YUV_420_BIPLANAR, resources.resources[0].format);
video_frame = CreateTestYuvHardwareVideoFrame(PIXEL_FORMAT_NV12, 1,
GL_TEXTURE_RECTANGLE_ARB);
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGB, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_RECTANGLE_ARB,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(viz::YUV_420_BIPLANAR, resources.resources[0].format);
EXPECT_EQ(0u, GetSharedImageCount());
}
TEST_F(VideoResourceUpdaterTest, CreateForHardwarePlanes_DualNV12) {
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
EXPECT_EQ(0u, GetSharedImageCount());
scoped_refptr<VideoFrame> video_frame = CreateTestYuvHardwareVideoFrame(
PIXEL_FORMAT_NV12, 2, GL_TEXTURE_EXTERNAL_OES);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(2u, resources.resources.size());
EXPECT_EQ(2u, resources.release_callbacks.size());
EXPECT_EQ((GLenum)GL_TEXTURE_EXTERNAL_OES,
resources.resources[0].mailbox_holder.texture_target);
// |updater| doesn't set |buffer_format| in this case.
EXPECT_EQ(viz::RED_8, resources.resources[0].format);
EXPECT_EQ(viz::RG_88, resources.resources[1].format);
video_frame = CreateTestYuvHardwareVideoFrame(PIXEL_FORMAT_NV12, 2,
GL_TEXTURE_RECTANGLE_ARB);
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::YUV, resources.type);
EXPECT_EQ(2u, resources.resources.size());
EXPECT_EQ((GLenum)GL_TEXTURE_RECTANGLE_ARB,
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(viz::RED_8, resources.resources[0].format);
EXPECT_EQ(viz::RG_88, resources.resources[1].format);
EXPECT_EQ(0u, GetSharedImageCount());
}
TEST_F(VideoResourceUpdaterTest, CreateForHardwarePlanes_SingleP016HDR) {
constexpr auto kHDR10ColorSpace = gfx::ColorSpace::CreateHDR10();
gfx::HDRMetadata hdr_metadata{};
hdr_metadata.color_volume_metadata.luminance_max = 1000;
std::unique_ptr<VideoResourceUpdater> updater = CreateUpdaterForHardware();
EXPECT_EQ(0u, GetSharedImageCount());
scoped_refptr<VideoFrame> video_frame = CreateTestHardwareVideoFrame(
PIXEL_FORMAT_P016LE, GL_TEXTURE_EXTERNAL_OES);
video_frame->set_color_space(kHDR10ColorSpace);
video_frame->set_hdr_metadata(hdr_metadata);
VideoFrameExternalResources resources =
updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGB, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES),
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(viz::P010, resources.resources[0].format);
EXPECT_EQ(kHDR10ColorSpace, resources.resources[0].color_space);
EXPECT_EQ(hdr_metadata, resources.resources[0].hdr_metadata);
video_frame = CreateTestYuvHardwareVideoFrame(PIXEL_FORMAT_P016LE, 1,
GL_TEXTURE_RECTANGLE_ARB);
video_frame->set_color_space(kHDR10ColorSpace);
video_frame->set_hdr_metadata(hdr_metadata);
resources = updater->CreateExternalResourcesFromVideoFrame(video_frame);
EXPECT_EQ(VideoFrameResourceType::RGB, resources.type);
EXPECT_EQ(1u, resources.resources.size());
EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
resources.resources[0].mailbox_holder.texture_target);
EXPECT_EQ(viz::P010, resources.resources[0].format);
EXPECT_EQ(kHDR10ColorSpace, resources.resources[0].color_space);
EXPECT_EQ(hdr_metadata, resources.resources[0].hdr_metadata);
EXPECT_EQ(0u, GetSharedImageCount());
}
} // namespace
} // namespace media