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
media / video / renderable_gpu_memory_buffer_video_frame_pool_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/video/renderable_gpu_memory_buffer_video_frame_pool.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/notimplemented.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "components/viz/test/test_context_provider.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/config/gpu_finch_features.h"
#include "media/base/format_utils.h"
#include "media/base/media_switches.h"
#include "media/base/video_frame.h"
#include "media/video/fake_gpu_memory_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
namespace media {
namespace {
gfx::ColorSpace GetColorSpaceForPixelFormat(media::VideoPixelFormat format) {
switch (format) {
case media::PIXEL_FORMAT_NV12:
return gfx::ColorSpace::CreateREC709();
case media::PIXEL_FORMAT_ARGB:
case media::PIXEL_FORMAT_ABGR:
return gfx::ColorSpace::CreateSRGB();
default:
NOTREACHED();
}
}
class FakeContext : public RenderableGpuMemoryBufferVideoFramePool::Context {
public:
FakeContext()
: context_provider_(viz::TestContextProvider::Create()),
weak_factory_(this) {}
~FakeContext() override = default;
scoped_refptr<gpu::ClientSharedImage> CreateSharedImage(
gfx::GpuMemoryBuffer* gpu_memory_buffer,
const viz::SharedImageFormat& si_format,
const gfx::ColorSpace& color_space,
gpu::SharedImageUsageSet usage,
gpu::SyncToken& sync_token) override {
DoCreateSharedImage(si_format, gpu_memory_buffer->GetSize(), color_space,
usage, gpu_memory_buffer->CloneHandle());
return context_provider_->SharedImageInterface()->CreateSharedImage(
{si_format, gpu_memory_buffer->GetSize(), color_space, usage,
"RenderableGpuMemoryBufferVideoFramePoolTest"},
gpu_memory_buffer->CloneHandle());
}
scoped_refptr<gpu::ClientSharedImage> CreateSharedImage(
const gfx::Size& size,
gfx::BufferUsage buffer_usage,
const viz::SharedImageFormat& si_format,
const gfx::ColorSpace& color_space,
gpu::SharedImageUsageSet usage,
gpu::SyncToken& sync_token) override {
DoCreateMappableSharedImage(size, buffer_usage, si_format, color_space,
usage, sync_token);
context_provider_->SharedImageInterface()
->UseTestGMBInSharedImageCreationWithBufferUsage();
return context_provider_->SharedImageInterface()->CreateSharedImage(
{si_format, size, color_space, usage,
"RenderableGpuMemoryBufferVideoFramePoolTest"},
gpu::kNullSurfaceHandle, buffer_usage);
}
MOCK_METHOD2(DoCreateGpuMemoryBuffer,
void(const gfx::Size& size, gfx::BufferFormat format));
MOCK_METHOD5(DoCreateSharedImage,
void(viz::SharedImageFormat format,
const gfx::Size& size,
const gfx::ColorSpace& color_space,
gpu::SharedImageUsageSet usage,
gfx::GpuMemoryBufferHandle buffer_handle));
MOCK_METHOD6(DoCreateMappableSharedImage,
void(const gfx::Size& size,
gfx::BufferUsage buffer_usage,
const viz::SharedImageFormat& si_format,
const gfx::ColorSpace& color_space,
gpu::SharedImageUsageSet usage,
gpu::SyncToken& sync_token));
MOCK_METHOD2(DestroySharedImage,
void(const gpu::SyncToken& sync_token,
scoped_refptr<gpu::ClientSharedImage> shared_image));
base::WeakPtr<FakeContext> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
private:
scoped_refptr<viz::TestContextProvider> context_provider_;
base::WeakPtrFactory<FakeContext> weak_factory_;
};
class RenderableGpuMemoryBufferVideoFramePoolTest
: public testing::TestWithParam<VideoPixelFormat> {
public:
RenderableGpuMemoryBufferVideoFramePoolTest() : format_(GetParam()) {}
protected:
void VerifySharedImageCreation(FakeContext* context) {
viz::SharedImageFormat si_format;
switch (format_) {
case PIXEL_FORMAT_NV12:
si_format = viz::MultiPlaneFormat::kNV12;
break;
case PIXEL_FORMAT_ARGB:
si_format = viz::SinglePlaneFormat::kBGRA_8888;
break;
case PIXEL_FORMAT_ABGR:
si_format = viz::SinglePlaneFormat::kRGBA_8888;
break;
default:
NOTREACHED();
}
EXPECT_CALL(*context,
DoCreateMappableSharedImage(_, _, si_format, _, _, _));
}
VideoPixelFormat format_;
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, SimpleLifetimes) {
base::test::SingleThreadTaskEnvironment task_environment;
const gfx::Size size0(128, 256);
const gfx::ColorSpace color_space0 = GetColorSpaceForPixelFormat(format_);
base::WeakPtr<FakeContext> context;
std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool;
{
auto context_strong = std::make_unique<FakeContext>();
context = context_strong->GetWeakPtr();
pool = RenderableGpuMemoryBufferVideoFramePool::Create(
std::move(context_strong), format_);
}
// Create a new frame.
VerifySharedImageCreation(context.get());
auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
// Expect the frame to be reused.
EXPECT_CALL(*context, DoCreateMappableSharedImage(_, _, _, _, _, _)).Times(0);
auto video_frame1 = pool->MaybeCreateVideoFrame(size0, color_space0);
// Expect a new frame to be created.
VerifySharedImageCreation(context.get());
auto video_frame2 = pool->MaybeCreateVideoFrame(size0, color_space0);
// Expect a new frame to be created.
VerifySharedImageCreation(context.get());
auto video_frame3 = pool->MaybeCreateVideoFrame(size0, color_space0);
// Freeing two frames will not result in any frames being destroyed, because
// we allow unused 2 frames to exist.
video_frame1 = nullptr;
video_frame2 = nullptr;
task_environment.RunUntilIdle();
// Freeing the third frame will result in one of the frames being destroyed.
EXPECT_CALL(*context, DestroySharedImage(_, _));
video_frame3 = nullptr;
task_environment.RunUntilIdle();
// Destroying the pool will result in the remaining two frames being
// destroyed.
EXPECT_TRUE(!!context);
EXPECT_CALL(*context, DestroySharedImage(_, _)).Times(2);
pool.reset();
task_environment.RunUntilIdle();
EXPECT_FALSE(!!context);
}
TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, FrameFreedAfterPool) {
base::test::SingleThreadTaskEnvironment task_environment;
const gfx::Size size0(128, 256);
const gfx::ColorSpace color_space0 = GetColorSpaceForPixelFormat(format_);
base::WeakPtr<FakeContext> context;
std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool;
{
auto context_strong = std::make_unique<FakeContext>();
context = context_strong->GetWeakPtr();
pool = RenderableGpuMemoryBufferVideoFramePool::Create(
std::move(context_strong), format_);
}
// Create a new frame.
VerifySharedImageCreation(context.get());
auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0);
task_environment.RunUntilIdle();
// If the pool is destroyed, but a frame still exists, the context will not
// be destroyed.
pool.reset();
task_environment.RunUntilIdle();
EXPECT_TRUE(context);
// Destroy the frame. Still nothing will happen, because its destruction will
// happen after a posted task is run.
video_frame0 = nullptr;
// The shared images will be destroyed once the posted task is run.
EXPECT_CALL(*context, DestroySharedImage(_, _));
task_environment.RunUntilIdle();
EXPECT_FALSE(!!context);
}
TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, CrossThread) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
const gfx::Size size0(128, 256);
const gfx::ColorSpace color_space0 = GetColorSpaceForPixelFormat(format_);
// Create a pool on the main thread.
auto pool = RenderableGpuMemoryBufferVideoFramePool::Create(
std::make_unique<FakeContext>(), format_);
base::ThreadPool::CreateSequencedTaskRunner({})->PostTaskAndReplyWithResult(
FROM_HERE,
// Create a frame on another thread.
base::BindLambdaForTesting(
[&]() { return pool->MaybeCreateVideoFrame(size0, color_space0); }),
// Destroy the video frame on the main thread.
base::BindLambdaForTesting(
[&](scoped_refptr<VideoFrame> video_frame0) {}));
task_environment.RunUntilIdle();
// Destroy the pool.
pool = nullptr;
task_environment.RunUntilIdle();
}
TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest,
VideoFramesDestroyedConcurrently) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
const gfx::Size size0(128, 256);
const gfx::ColorSpace color_space0 = GetColorSpaceForPixelFormat(format_);
// Create a pool and several frames on the main thread.
base::WeakPtr<FakeContext> context;
std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool;
{
auto context_strong = std::make_unique<FakeContext>();
context = context_strong->GetWeakPtr();
pool = RenderableGpuMemoryBufferVideoFramePool::Create(
std::move(context_strong), format_);
}
std::vector<scoped_refptr<VideoFrame>> frames;
static constexpr int kNumFrames = 3;
for (int i = 0; i < kNumFrames; i++) {
VerifySharedImageCreation(context.get());
frames.emplace_back(pool->MaybeCreateVideoFrame(size0, color_space0));
}
task_environment.RunUntilIdle();
// Expect all frames to be destroyed eventually.
EXPECT_CALL(*context, DestroySharedImage(_, _)).Times(kNumFrames);
// Destroy frames on separate threads. TSAN will tell us if there's a problem.
for (int i = 0; i < kNumFrames; i++) {
base::ThreadPool::CreateSequencedTaskRunner({})->PostTask(
FROM_HERE, base::DoNothingWithBoundArgs(std::move(frames[i])));
}
pool.reset();
task_environment.RunUntilIdle();
EXPECT_FALSE(!!context);
}
TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, ConcurrentCreateDestroy) {
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
const gfx::Size size0(128, 256);
const gfx::ColorSpace color_space0 = GetColorSpaceForPixelFormat(format_);
// Create a pool on the main thread.
auto pool = RenderableGpuMemoryBufferVideoFramePool::Create(
std::make_unique<FakeContext>(), format_);
// Create a frame on the main thread.
auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0);
task_environment.RunUntilIdle();
// Destroy the frame on another thread. TSAN will tell us if there's a
// problem.
base::ThreadPool::CreateSequencedTaskRunner({})->PostTask(
FROM_HERE, base::DoNothingWithBoundArgs(std::move(video_frame0)));
// Create another frame on the main thread.
auto video_frame1 = pool->MaybeCreateVideoFrame(size0, color_space0);
task_environment.RunUntilIdle();
video_frame1 = nullptr;
pool.reset();
task_environment.RunUntilIdle();
}
TEST_P(RenderableGpuMemoryBufferVideoFramePoolTest, RespectSizeAndColorSpace) {
base::test::SingleThreadTaskEnvironment task_environment;
const gfx::Size size0(128, 256);
const gfx::ColorSpace color_space0 = GetColorSpaceForPixelFormat(format_);
const gfx::Size size1(256, 256);
const gfx::ColorSpace color_space1 = gfx::ColorSpace::CreateREC601();
base::WeakPtr<FakeContext> context;
std::unique_ptr<RenderableGpuMemoryBufferVideoFramePool> pool;
{
auto context_strong = std::make_unique<FakeContext>();
context = context_strong->GetWeakPtr();
pool = RenderableGpuMemoryBufferVideoFramePool::Create(
std::move(context_strong), format_);
}
// Create a new frame.
VerifySharedImageCreation(context.get());
auto video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
// Expect the frame to be reused.
EXPECT_CALL(*context, DoCreateMappableSharedImage(_, _, _, _, _, _)).Times(0);
video_frame0 = pool->MaybeCreateVideoFrame(size0, color_space0);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
// Change the size, expect a new frame to be created (and the previous frame
// to be destroyed).
EXPECT_CALL(*context, DestroySharedImage(_, _));
VerifySharedImageCreation(context.get());
video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space0);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
// Expect that frame to be reused.
EXPECT_CALL(*context, DoCreateMappableSharedImage(_, _, _, _, _, _)).Times(0);
video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space0);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
// Change the color space, expect a new frame to be created (and the previous
// frame to be destroyed).
EXPECT_CALL(*context, DestroySharedImage(_, _));
VerifySharedImageCreation(context.get());
video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space1);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
// Expect that frame to be reused.
EXPECT_CALL(*context, DoCreateMappableSharedImage(_, _, _, _, _, _)).Times(0);
video_frame0 = pool->MaybeCreateVideoFrame(size1, color_space1);
video_frame0 = nullptr;
task_environment.RunUntilIdle();
EXPECT_CALL(*context, DestroySharedImage(_, _));
pool.reset();
task_environment.RunUntilIdle();
EXPECT_FALSE(!!context);
}
INSTANTIATE_TEST_SUITE_P(
All,
RenderableGpuMemoryBufferVideoFramePoolTest,
testing::Values(media::VideoPixelFormat::PIXEL_FORMAT_NV12,
media::VideoPixelFormat::PIXEL_FORMAT_ARGB,
media::VideoPixelFormat::PIXEL_FORMAT_ABGR));
} // namespace
} // namespace media