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
media / gpu / chromeos / image_processor.cc [blame]
// Copyright 2018 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/chromeos/image_processor.h"
#include <memory>
#include <ostream>
#include <sstream>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "media/base/video_types.h"
#include "media/gpu/macros.h"
namespace media {
namespace {
using PortConfig = ImageProcessorBackend::PortConfig;
// Verify if the format of |frame| matches |config|.
template <class FrameType>
bool CheckFrameFormat(const ImageProcessor::PortConfig& config,
const FrameType& frame) {
// Because propriatary format fourcc will map to other common VideoPixelFormat
// with same layout, we convert to VideoPixelFormat to check.
if (frame.format() != config.fourcc.ToVideoPixelFormat()) {
VLOGF(1) << "Invalid frame format="
<< VideoPixelFormatToString(frame.format())
<< ", expected=" << config.fourcc.ToString();
return false;
}
if (frame.layout().coded_size() != config.size) {
VLOGF(1) << "Invalid frame size=" << frame.layout().coded_size().ToString()
<< ", expected=" << config.size.ToString();
return false;
}
if (frame.visible_rect() != config.visible_rect) {
VLOGF(1) << "Invalid frame visible rectangle="
<< frame.visible_rect().ToString()
<< ", expected=" << config.visible_rect.ToString();
return false;
}
return true;
}
} // namespace
ImageProcessor::ClientCallback::ClientCallback(FrameReadyCB ready_cb)
: ready_cb(std::move(ready_cb)) {}
ImageProcessor::ClientCallback::ClientCallback(FrameResourceReadyCB ready_cb)
: frame_resource_ready_cb(std::move(ready_cb)) {}
ImageProcessor::ClientCallback::ClientCallback(
LegacyFrameResourceReadyCB legacy_frame_resource_ready_cb)
: legacy_frame_resource_ready_cb(
std::move(legacy_frame_resource_ready_cb)) {}
ImageProcessor::ClientCallback::ClientCallback(ClientCallback&&) = default;
ImageProcessor::ClientCallback::~ClientCallback() = default;
// static
std::unique_ptr<ImageProcessor> ImageProcessor::Create(
CreateBackendCB create_backend_cb,
const PortConfig& input_config,
const PortConfig& output_config,
OutputMode output_mode,
ErrorCB error_cb,
scoped_refptr<base::SequencedTaskRunner> client_task_runner) {
auto wrapped_error_cb = base::BindRepeating(
base::IgnoreResult(&base::SequencedTaskRunner::PostTask),
client_task_runner, FROM_HERE, std::move(error_cb));
std::unique_ptr<ImageProcessorBackend> backend = create_backend_cb.Run(
input_config, output_config, output_mode, std::move(wrapped_error_cb));
if (!backend)
return nullptr;
scoped_refptr<base::SequencedTaskRunner> backend_task_runner =
backend->task_runner();
return base::WrapUnique(new ImageProcessor(std::move(backend),
std::move(client_task_runner),
std::move(backend_task_runner)));
}
ImageProcessor::ImageProcessor(
std::unique_ptr<ImageProcessorBackend> backend,
scoped_refptr<base::SequencedTaskRunner> client_task_runner,
scoped_refptr<base::SequencedTaskRunner> backend_task_runner)
: backend_(std::move(backend)),
client_task_runner_(std::move(client_task_runner)),
backend_task_runner_(std::move(backend_task_runner)),
needs_linear_output_buffers_(backend_ &&
backend_->needs_linear_output_buffers()) {
DVLOGF(2);
DETACH_FROM_SEQUENCE(client_sequence_checker_);
weak_this_ = weak_this_factory_.GetWeakPtr();
}
ImageProcessor::~ImageProcessor() {
DVLOGF(3);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
weak_this_factory_.InvalidateWeakPtrs();
// Delete |backend_| on |backend_task_runner_|.
backend_task_runner_->DeleteSoon(FROM_HERE, std::move(backend_));
}
bool ImageProcessor::Process(scoped_refptr<VideoFrame> input_frame,
scoped_refptr<VideoFrame> output_frame,
FrameReadyCB cb) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
DCHECK_EQ(output_mode(), OutputMode::IMPORT);
DCHECK(input_frame);
DCHECK(output_frame);
if (!CheckFrameFormat(input_config(), *input_frame)) {
LOG(ERROR) << "Unexpected input VideoFrame format "
<< input_frame->AsHumanReadableString()
<< ", expected a compatible one with "
<< input_config().ToString();
return false;
}
if (!CheckFrameFormat(output_config(), *output_frame)) {
LOG(ERROR) << "Unexpected output VideoFrame format "
<< output_frame->AsHumanReadableString()
<< ", expected a compatible one with "
<< output_config().ToString();
return false;
}
int cb_index = StoreCallback(std::move(cb));
auto ready_cb = base::BindOnce(&ImageProcessor::OnProcessDoneThunk,
client_task_runner_, weak_this_, cb_index);
backend_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ImageProcessorBackend::Process,
base::Unretained(backend_.get()), std::move(input_frame),
std::move(output_frame), std::move(ready_cb)));
return true;
}
// static
void ImageProcessor::OnProcessDoneThunk(
scoped_refptr<base::SequencedTaskRunner> task_runner,
std::optional<base::WeakPtr<ImageProcessor>> weak_this,
int cb_index,
scoped_refptr<VideoFrame> frame) {
DVLOGF(4);
DCHECK(weak_this);
task_runner->PostTask(
FROM_HERE, base::BindOnce(&ImageProcessor::OnProcessDone, *weak_this,
cb_index, std::move(frame)));
}
void ImageProcessor::OnProcessDone(int cb_index,
scoped_refptr<VideoFrame> frame) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
auto it = pending_cbs_.find(cb_index);
// Skip if the callback is dropped by Reset().
if (it == pending_cbs_.end())
return;
DCHECK(it->second.ready_cb);
FrameReadyCB cb = std::move(it->second.ready_cb);
pending_cbs_.erase(it);
std::move(cb).Run(std::move(frame));
}
bool ImageProcessor::Process(scoped_refptr<FrameResource> input_frame,
scoped_refptr<FrameResource> output_frame,
FrameResourceReadyCB cb) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
DCHECK_EQ(output_mode(), OutputMode::IMPORT);
DCHECK(input_frame);
DCHECK(output_frame);
if (!CheckFrameFormat(input_config(), *input_frame)) {
LOG(ERROR) << "Unexpected input FrameResource format "
<< input_frame->AsHumanReadableString()
<< ", expected a compatible one with "
<< input_config().ToString();
return false;
}
if (!CheckFrameFormat(output_config(), *output_frame)) {
LOG(ERROR) << "Unexpected output FrameResource format "
<< output_frame->AsHumanReadableString()
<< ", expected a compatible one with "
<< output_config().ToString();
return false;
}
int cb_index = StoreCallback(std::move(cb));
auto ready_cb =
base::BindOnce(&ImageProcessor::OnProcessFrameResourceDoneThunk,
client_task_runner_, weak_this_, cb_index);
backend_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ImageProcessorBackend::ProcessFrame,
base::Unretained(backend_.get()), std::move(input_frame),
std::move(output_frame), std::move(ready_cb)));
return true;
}
// static
void ImageProcessor::OnProcessFrameResourceDoneThunk(
scoped_refptr<base::SequencedTaskRunner> task_runner,
std::optional<base::WeakPtr<ImageProcessor>> weak_this,
int cb_index,
scoped_refptr<FrameResource> frame) {
DVLOGF(4);
DCHECK(weak_this);
task_runner->PostTask(
FROM_HERE, base::BindOnce(&ImageProcessor::OnProcessFrameResourceDone,
*weak_this, cb_index, std::move(frame)));
}
void ImageProcessor::OnProcessFrameResourceDone(
int cb_index,
scoped_refptr<FrameResource> frame) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
auto it = pending_cbs_.find(cb_index);
// Skip if the callback is dropped by Reset().
if (it == pending_cbs_.end()) {
return;
}
DCHECK(it->second.frame_resource_ready_cb);
FrameResourceReadyCB cb = std::move(it->second.frame_resource_ready_cb);
pending_cbs_.erase(it);
std::move(cb).Run(std::move(frame));
}
bool ImageProcessor::Process(scoped_refptr<FrameResource> frame,
LegacyFrameResourceReadyCB cb) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
DCHECK_EQ(output_mode(), OutputMode::ALLOCATE);
int cb_index = StoreCallback(std::move(cb));
auto ready_cb =
base::BindOnce(&ImageProcessor::OnProcessFrameResourceLegacyDoneThunk,
client_task_runner_, weak_this_, cb_index);
backend_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&ImageProcessorBackend::ProcessLegacyFrame,
base::Unretained(backend_.get()),
std::move(frame), std::move(ready_cb)));
return true;
}
// static
void ImageProcessor::OnProcessFrameResourceLegacyDoneThunk(
scoped_refptr<base::SequencedTaskRunner> task_runner,
std::optional<base::WeakPtr<ImageProcessor>> weak_this,
int cb_index,
size_t buffer_id,
scoped_refptr<FrameResource> frame) {
DVLOGF(4);
DCHECK(weak_this);
task_runner->PostTask(
FROM_HERE,
base::BindOnce(&ImageProcessor::OnProcessFrameResourceLegacyDone,
*weak_this, cb_index, buffer_id, std::move(frame)));
}
void ImageProcessor::OnProcessFrameResourceLegacyDone(
int cb_index,
size_t buffer_id,
scoped_refptr<FrameResource> frame) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
auto it = pending_cbs_.find(cb_index);
// Skip if the callback is dropped by Reset().
if (it == pending_cbs_.end()) {
return;
}
DCHECK(it->second.legacy_frame_resource_ready_cb);
LegacyFrameResourceReadyCB cb =
std::move(it->second.legacy_frame_resource_ready_cb);
pending_cbs_.erase(it);
std::move(cb).Run(buffer_id, std::move(frame));
}
bool ImageProcessor::Reset() {
DVLOGF(3);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
backend_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&ImageProcessorBackend::Reset,
base::Unretained(backend_.get())));
// After clearing all pending callbacks, we can guarantee no frame are
// returned after that.
pending_cbs_.clear();
return true;
}
int ImageProcessor::StoreCallback(ClientCallback cb) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(client_sequence_checker_);
int cb_index = next_cb_index_++;
pending_cbs_.emplace(cb_index, std::move(cb));
return cb_index;
}
const PortConfig& ImageProcessor::input_config() const {
return backend_->input_config();
}
const PortConfig& ImageProcessor::output_config() const {
return backend_->output_config();
}
} // namespace media