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
media / gpu / windows / d3d12_video_encode_accelerator_unittest.cc [blame]
// Copyright 2024 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/gpu/windows/d3d12_video_encode_accelerator.h"
#include "base/test/task_environment.h"
#include "media/base/media_util.h"
#include "media/base/win/d3d12_mocks.h"
#include "media/base/win/d3d12_video_mocks.h"
#include "media/gpu/windows/d3d12_video_encode_delegate.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using media::SetComPointeeAndReturnOk;
using testing::_;
using testing::Invoke;
using testing::Mock;
using testing::NiceMock;
using testing::Return;
namespace media {
namespace {
constexpr VideoCodecProfile kSupportedProfile = H264PROFILE_MAIN;
constexpr gfx::Size kSupportedSize{1920, 1080};
class MockVideoEncodeAcceleratorClient : public VideoEncodeAccelerator::Client {
public:
MockVideoEncodeAcceleratorClient() = default;
~MockVideoEncodeAcceleratorClient() override = default;
MOCK_METHOD3(RequireBitstreamBuffers,
void(unsigned int, const gfx::Size&, size_t));
MOCK_METHOD2(BitstreamBufferReady,
void(int32_t, const BitstreamBufferMetadata&));
MOCK_METHOD1(NotifyErrorStatus, void(const EncoderStatus&));
MOCK_METHOD1(NotifyEncoderInfoChange, void(const VideoEncoderInfo&));
};
class MockVideoEncoderDelegate : public D3D12VideoEncodeDelegate {
public:
MockVideoEncoderDelegate(ID3D12VideoDevice3* video_device,
VideoCodecProfile profile)
: D3D12VideoEncodeDelegate(video_device) {}
MOCK_METHOD1(Initialize, EncoderStatus(VideoEncodeAccelerator::Config));
MOCK_METHOD(size_t, GetMaxNumOfRefFrames, (), (const override));
MOCK_METHOD5(
Encode,
EncoderStatus::Or<EncodeResult>(Microsoft::WRL::ComPtr<ID3D12Resource>,
UINT,
const gfx::ColorSpace&,
const BitstreamBuffer&,
bool));
MOCK_METHOD(EncoderStatus::Or<BitstreamBufferMetadata>,
EncodeImpl,
(ID3D12Resource*, UINT, bool),
(override));
protected:
MOCK_METHOD(EncoderStatus,
InitializeVideoEncoder,
(const VideoEncodeAccelerator::Config&));
};
class MockVideoEncoderDelegateFactory
: public D3D12VideoEncodeAccelerator::VideoEncodeDelegateFactoryInterface {
public:
~MockVideoEncoderDelegateFactory() override = default;
std::unique_ptr<D3D12VideoEncodeDelegate> CreateVideoEncodeDelegate(
ID3D12VideoDevice3* video_device,
VideoCodecProfile profile) override {
auto encoder_delegate =
std::make_unique<NiceMock<MockVideoEncoderDelegate>>(video_device,
profile);
ON_CALL(*encoder_delegate, Initialize(_))
.WillByDefault(Return(EncoderStatus::Codes::kOk));
ON_CALL(*encoder_delegate, GetMaxNumOfRefFrames())
.WillByDefault(Return(16));
ON_CALL(*encoder_delegate, Encode(_, _, _, _, _))
.WillByDefault(Invoke(
[](Microsoft::WRL::ComPtr<ID3D12Resource>, UINT,
const gfx::ColorSpace&, const BitstreamBuffer& bitstream_buffer,
bool) -> D3D12VideoEncodeDelegate::EncodeResult {
return {bitstream_buffer.id()};
}));
return std::move(encoder_delegate);
}
VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles(
ID3D12VideoDevice3* video_device) override {
EXPECT_TRUE(video_device);
VideoEncodeAccelerator::SupportedProfile profile(kSupportedProfile,
kSupportedSize, 30, 1);
profile.gpu_supported_pixel_formats.push_back(PIXEL_FORMAT_NV12);
return {profile};
}
};
class MockGpuMemoryBuffer : public gfx::GpuMemoryBuffer {
public:
~MockGpuMemoryBuffer() override = default;
MOCK_METHOD(bool, Map, ());
MOCK_METHOD(void*, memory, (size_t));
MOCK_METHOD(void, Unmap, ());
MOCK_METHOD(gfx::Size, GetSize, (), (const));
MOCK_METHOD(gfx::BufferFormat, GetFormat, (), (const));
MOCK_METHOD(int, stride, (size_t plane), (const));
MOCK_METHOD(gfx::GpuMemoryBufferId, GetId, (), (const));
MOCK_METHOD(gfx::GpuMemoryBufferType, GetType, (), (const));
MOCK_METHOD(gfx::GpuMemoryBufferHandle, CloneHandle, (), (const));
MOCK_METHOD(void,
OnMemoryDump,
(base::trace_event::ProcessMemoryDump*,
const base::trace_event::MemoryAllocatorDumpGuid&,
uint64_t,
int),
(const));
};
} // namespace
class D3D12VideoEncodeAcceleratorTest : public testing::Test {
public:
~D3D12VideoEncodeAcceleratorTest() override = default;
protected:
void SetUp() override {
mock_device_ = MakeComPtr<NiceMock<D3D12DeviceMock>>();
mock_video_device3_ = MakeComPtr<NiceMock<D3D12VideoDevice3Mock>>();
mock_command_list_ = MakeComPtr<NiceMock<D3D12GraphicsCommandListMock>>();
mock_resource_ = MakeComPtr<NiceMock<D3D12ResourceMock>>();
COM_ON_CALL(mock_device_, QueryInterface(IID_ID3D12VideoDevice3, _))
.WillByDefault(SetComPointeeAndReturnOk<1>(mock_video_device3_.Get()));
COM_ON_CALL(mock_device_, CreateCommandList(_, _, _, _, _, _))
.WillByDefault(SetComPointeeAndReturnOk<5>(mock_command_list_.Get()));
COM_ON_CALL(mock_device_, OpenSharedHandle(_, IID_ID3D12Resource, _))
.WillByDefault(SetComPointeeAndReturnOk<2>(mock_resource_.Get()));
video_encode_accelerator_.reset(
new D3D12VideoEncodeAccelerator(mock_device_));
client_ = std::make_unique<NiceMock<MockVideoEncodeAcceleratorClient>>();
static_cast<D3D12VideoEncodeAccelerator*>(video_encode_accelerator_.get())
->encoder_factory_ =
std::make_unique<MockVideoEncoderDelegateFactory>();
}
VideoEncodeAccelerator::Config SupportedProfileToConfig(
const VideoEncodeAccelerator::SupportedProfile& profile) {
return VideoEncodeAccelerator::Config(
profile.gpu_supported_pixel_formats.front(), profile.max_resolution,
profile.profile, Bitrate::ConstantBitrate(300000u),
profile.max_framerate_numerator,
VideoEncodeAccelerator::Config::StorageType::kShmem,
VideoEncodeAccelerator::Config::ContentType::kCamera);
}
scoped_refptr<VideoFrame> CreateTestVideoFrame() {
auto mock_gpu_memory_buffer =
std::make_unique<NiceMock<MockGpuMemoryBuffer>>();
ON_CALL(*mock_gpu_memory_buffer, GetSize())
.WillByDefault(Return(kSupportedSize));
ON_CALL(*mock_gpu_memory_buffer, GetFormat())
.WillByDefault(Return(gfx::BufferFormat::YUV_420_BIPLANAR));
ON_CALL(*mock_gpu_memory_buffer, GetType())
.WillByDefault(Return(gfx::GpuMemoryBufferType::DXGI_SHARED_HANDLE));
return VideoFrame::WrapExternalGpuMemoryBuffer(
gfx::Rect(kSupportedSize), kSupportedSize,
std::move(mock_gpu_memory_buffer), base::TimeDelta{});
}
void WaitForEncoderTasksToComplete() {
base::RunLoop run_loop;
auto* d3d12_video_encode_accelerator =
static_cast<D3D12VideoEncodeAccelerator*>(
video_encode_accelerator_.get());
d3d12_video_encode_accelerator->encoder_task_runner_->PostTask(
FROM_HERE, run_loop.QuitClosure());
run_loop.Run();
}
void CheckInputFramesQueueAndBitstreamBuffersAreEitherEmpty() {
auto* d3d12_video_encode_accelerator =
static_cast<D3D12VideoEncodeAccelerator*>(
video_encode_accelerator_.get());
base::RunLoop run_loop;
d3d12_video_encode_accelerator->encoder_task_runner_->PostTask(
FROM_HERE, base::BindOnce(
[](const D3D12VideoEncodeAccelerator* encoder,
base::OnceClosure quit_closure) {
DCHECK_CALLED_ON_VALID_SEQUENCE(
encoder->encoder_sequence_checker_);
EXPECT_TRUE(encoder->input_frames_queue_.empty() ||
encoder->bitstream_buffers_.empty());
std::move(quit_closure).Run();
},
d3d12_video_encode_accelerator, run_loop.QuitClosure()));
run_loop.Run();
}
base::test::TaskEnvironment task_environment_;
std::unique_ptr<MediaLog> media_log_ = std::make_unique<NullMediaLog>();
Microsoft::WRL::ComPtr<D3D12DeviceMock> mock_device_;
Microsoft::WRL::ComPtr<D3D12VideoDevice3Mock> mock_video_device3_;
Microsoft::WRL::ComPtr<D3D12GraphicsCommandListMock> mock_command_list_;
Microsoft::WRL::ComPtr<D3D12ResourceMock> mock_resource_;
std::unique_ptr<VideoEncodeAccelerator> video_encode_accelerator_;
std::unique_ptr<MockVideoEncodeAcceleratorClient> client_;
};
TEST_F(D3D12VideoEncodeAcceleratorTest, SupportedProfilesCanBeInitialized) {
auto* d3d12_video_encode_accelerator =
static_cast<D3D12VideoEncodeAccelerator*>(
video_encode_accelerator_.get());
for (const auto& profile :
d3d12_video_encode_accelerator->GetSupportedProfiles()) {
auto config = SupportedProfileToConfig(profile);
EXPECT_TRUE(d3d12_video_encode_accelerator->Initialize(
config, client_.get(), media_log_->Clone()));
EXPECT_CALL(*client_, NotifyEncoderInfoChange(_)).Times(1);
EXPECT_CALL(*client_, NotifyErrorStatus(_)).Times(0);
WaitForEncoderTasksToComplete();
Mock::VerifyAndClearExpectations(&client_);
}
}
TEST_F(D3D12VideoEncodeAcceleratorTest, RejectsUnsupportedConfig) {
auto* d3d12_video_encode_accelerator =
static_cast<D3D12VideoEncodeAccelerator*>(
video_encode_accelerator_.get());
auto supported_profiles =
d3d12_video_encode_accelerator->GetSupportedProfiles();
EXPECT_FALSE(supported_profiles.empty());
auto profile = supported_profiles.front();
auto supported_config = SupportedProfileToConfig(profile);
constexpr std::pair<VideoCodecProfile, gfx::Size> kUnsupportedConfigs[]{
{VIDEO_CODEC_PROFILE_UNKNOWN, kSupportedSize},
{kSupportedProfile,
gfx::Size(kSupportedSize.width(), kSupportedSize.height() + 1)},
{kSupportedProfile,
gfx::Size(kSupportedSize.width() + 1, kSupportedSize.height())},
};
for (const auto& [video_codec_profile, size] : kUnsupportedConfigs) {
auto bad_config = supported_config;
bad_config.output_profile = video_codec_profile;
bad_config.input_visible_size = size;
EXPECT_TRUE(d3d12_video_encode_accelerator->Initialize(
bad_config, client_.get(), media_log_->Clone()));
EXPECT_CALL(*client_, NotifyEncoderInfoChange(_)).Times(0);
EXPECT_CALL(*client_, NotifyErrorStatus(_)).Times(1);
WaitForEncoderTasksToComplete();
Mock::VerifyAndClearExpectations(&client_);
}
}
TEST_F(D3D12VideoEncodeAcceleratorTest,
InputFramesQueueAndBitstreamBuffersAreEitherEmpty) {
auto* d3d12_video_encode_accelerator =
static_cast<D3D12VideoEncodeAccelerator*>(
video_encode_accelerator_.get());
auto supported_profiles =
d3d12_video_encode_accelerator->GetSupportedProfiles();
EXPECT_FALSE(supported_profiles.empty());
auto profile = supported_profiles.front();
auto supported_config = SupportedProfileToConfig(profile);
unsigned bitstream_buffer_count = 0;
size_t bitstream_buffer_size = 0;
EXPECT_CALL(*client_, RequireBitstreamBuffers(_, _, _))
.WillOnce(Invoke(
[&](unsigned int count, const gfx::Size& size, size_t size_in_bytes) {
bitstream_buffer_count = count;
bitstream_buffer_size = size_in_bytes;
}));
EXPECT_TRUE(d3d12_video_encode_accelerator->Initialize(
supported_config, client_.get(), media_log_->Clone()));
WaitForEncoderTasksToComplete();
Mock::VerifyAndClearExpectations(&client_);
std::vector<std::pair<int, base::UnsafeSharedMemoryRegion>> bitstream_buffers;
for (unsigned i = 0; i < bitstream_buffer_count; ++i) {
BitstreamBuffer bitstream_buffer(
i, base::UnsafeSharedMemoryRegion::Create(bitstream_buffer_size),
bitstream_buffer_size);
bitstream_buffers.emplace_back(i, bitstream_buffer.DuplicateRegion());
d3d12_video_encode_accelerator->UseOutputBitstreamBuffer(
std::move(bitstream_buffer));
}
WaitForEncoderTasksToComplete();
CheckInputFramesQueueAndBitstreamBuffersAreEitherEmpty();
unsigned frame_count = bitstream_buffer_count * 2;
for (unsigned i = 0; i < frame_count; ++i) {
d3d12_video_encode_accelerator->Encode(CreateTestVideoFrame(), false);
WaitForEncoderTasksToComplete();
CheckInputFramesQueueAndBitstreamBuffersAreEitherEmpty();
}
}
} // namespace media