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
|
// Copyright 2019 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/test/image_processor/image_processor_client.h"
#include <functional>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/bind_post_task.h"
#include "build/build_config.h"
#include "gpu/command_buffer/client/test_shared_image_interface.h"
#include "media/base/video_frame.h"
#include "media/base/video_frame_layout.h"
#include "media/gpu/chromeos/fourcc.h"
#include "media/gpu/chromeos/image_processor_factory.h"
#include "media/gpu/chromeos/platform_video_frame_utils.h"
#include "media/gpu/test/image.h"
#include "media/gpu/test/video_frame_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
namespace media::test {
namespace {
#define ASSERT_TRUE_OR_RETURN_NULLPTR(predicate) \
do { \
if (!(predicate)) { \
ADD_FAILURE(); \
return nullptr; \
} \
} while (0)
std::optional<VideoFrameLayout> CreateLayout(
const ImageProcessor::PortConfig& config) {
const VideoPixelFormat pixel_format = config.fourcc.ToVideoPixelFormat();
if (config.planes.empty())
return std::nullopt;
if (config.fourcc.IsMultiPlanar()) {
return VideoFrameLayout::CreateWithPlanes(pixel_format, config.size,
config.planes);
} else {
return VideoFrameLayout::CreateMultiPlanar(pixel_format, config.size,
config.planes);
}
}
} // anonymous namespace
// static
std::unique_ptr<ImageProcessorClient> ImageProcessorClient::Create(
std::optional<ImageProcessor::CreateBackendCB> create_backend_cb,
const ImageProcessor::PortConfig& input_config,
const ImageProcessor::PortConfig& output_config,
size_t num_buffers,
std::vector<std::unique_ptr<VideoFrameProcessor>> frame_processors) {
auto ip_client =
base::WrapUnique(new ImageProcessorClient(std::move(frame_processors)));
if (!ip_client->CreateImageProcessor(create_backend_cb, input_config,
output_config, num_buffers)) {
LOG(ERROR) << "Failed to create ImageProcessor";
return nullptr;
}
return ip_client;
}
ImageProcessorClient::ImageProcessorClient(
std::vector<std::unique_ptr<VideoFrameProcessor>> frame_processors)
: test_sii_(base::MakeRefCounted<gpu::TestSharedImageInterface>()),
frame_processors_(std::move(frame_processors)),
image_processor_client_thread_("ImageProcessorClientThread"),
output_cv_(&output_lock_),
num_processed_frames_(0),
image_processor_error_count_(0) {
CHECK(image_processor_client_thread_.Start());
DETACH_FROM_THREAD(image_processor_client_thread_checker_);
}
ImageProcessorClient::~ImageProcessorClient() {
DCHECK_CALLED_ON_VALID_THREAD(test_main_thread_checker_);
CHECK(image_processor_client_thread_.IsRunning());
// Destroys |image_processor_| on |image_processor_client_thread_|.
image_processor_client_thread_.task_runner()->DeleteSoon(
FROM_HERE, image_processor_.release());
image_processor_client_thread_.Stop();
}
bool ImageProcessorClient::CreateImageProcessor(
std::optional<ImageProcessor::CreateBackendCB> create_backend_cb,
const ImageProcessor::PortConfig& input_config,
const ImageProcessor::PortConfig& output_config,
size_t num_buffers) {
DCHECK_CALLED_ON_VALID_THREAD(test_main_thread_checker_);
base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
// base::Unretained(this) and std::cref() are safe here because |this|,
// |input_config| and |output_config| must outlive because this task is
// blocking.
image_processor_client_thread_.task_runner()->PostTask(
FROM_HERE, base::BindOnce(&ImageProcessorClient::CreateImageProcessorTask,
base::Unretained(this), create_backend_cb,
std::cref(input_config),
std::cref(output_config), num_buffers, &done));
done.Wait();
if (!image_processor_) {
LOG(ERROR) << "Failed to create ImageProcessor";
return false;
}
return true;
}
void ImageProcessorClient::CreateImageProcessorTask(
std::optional<ImageProcessor::CreateBackendCB> create_backend_cb,
const ImageProcessor::PortConfig& input_config,
const ImageProcessor::PortConfig& output_config,
size_t num_buffers,
base::WaitableEvent* done) {
DCHECK_CALLED_ON_VALID_THREAD(image_processor_client_thread_checker_);
// base::Unretained(this) for ErrorCB is safe here because the callback is
// executed on |image_processor_client_thread_| which is owned by this class.
ImageProcessor::ErrorCB error_cb = base::BindRepeating(
&ImageProcessorClient::NotifyError, base::Unretained(this));
if (create_backend_cb) {
image_processor_ =
ImageProcessor::Create(*create_backend_cb, input_config, output_config,
ImageProcessor::OutputMode::IMPORT, error_cb,
image_processor_client_thread_.task_runner());
} else {
image_processor_ = ImageProcessorFactory::Create(
input_config, output_config, num_buffers, error_cb,
image_processor_client_thread_.task_runner());
}
done->Signal();
}
scoped_refptr<VideoFrame> ImageProcessorClient::CreateInputFrame(
const Image& input_image) const {
DCHECK_CALLED_ON_VALID_THREAD(test_main_thread_checker_);
ASSERT_TRUE_OR_RETURN_NULLPTR(image_processor_);
ASSERT_TRUE_OR_RETURN_NULLPTR(input_image.IsLoaded());
const ImageProcessor::PortConfig& input_config =
image_processor_->input_config();
const VideoFrame::StorageType input_storage_type = input_config.storage_type;
std::optional<VideoFrameLayout> input_layout = CreateLayout(input_config);
ASSERT_TRUE_OR_RETURN_NULLPTR(input_layout);
if (VideoFrame::IsStorageTypeMappable(input_storage_type)) {
return CloneVideoFrame(CreateVideoFrameFromImage(input_image).get(),
*input_layout, test_sii_.get(),
VideoFrame::STORAGE_OWNED_MEMORY);
} else {
ASSERT_TRUE_OR_RETURN_NULLPTR(
input_storage_type == VideoFrame::STORAGE_DMABUFS ||
input_storage_type == VideoFrame::STORAGE_MAPPABLE_SHARED_IMAGE);
// NV12 is the only format that can be allocated with
// gfx::BufferUsage::VEA_READ_CAMERA_AND_CPU_READ_WRITE. So
// gfx::BufferUsage::GPU_READ_CPU_READ_WRITE is specified for other formats.
gfx::BufferUsage dst_buffer_usage =
(PIXEL_FORMAT_NV12 == input_image.PixelFormat())
? gfx::BufferUsage::VEA_READ_CAMERA_AND_CPU_READ_WRITE
: gfx::BufferUsage::GPU_READ_CPU_READ_WRITE;
return CloneVideoFrame(CreateVideoFrameFromImage(input_image).get(),
*input_layout, test_sii_.get(), input_storage_type,
dst_buffer_usage);
}
}
scoped_refptr<VideoFrame> ImageProcessorClient::CreateOutputFrame(
const Image& output_image) const {
DCHECK_CALLED_ON_VALID_THREAD(test_main_thread_checker_);
ASSERT_TRUE_OR_RETURN_NULLPTR(output_image.IsMetadataLoaded());
ASSERT_TRUE_OR_RETURN_NULLPTR(image_processor_);
const ImageProcessor::PortConfig& output_config =
image_processor_->output_config();
const VideoFrame::StorageType output_storage_type =
output_config.storage_type;
std::optional<VideoFrameLayout> output_layout = CreateLayout(output_config);
ASSERT_TRUE_OR_RETURN_NULLPTR(output_layout);
if (VideoFrame::IsStorageTypeMappable(output_storage_type)) {
return VideoFrame::CreateFrameWithLayout(
*output_layout, gfx::Rect(output_image.Size()), output_image.Size(),
base::TimeDelta(), false /* zero_initialize_memory*/);
}
ASSERT_TRUE_OR_RETURN_NULLPTR(
output_storage_type == VideoFrame::STORAGE_DMABUFS ||
output_storage_type == VideoFrame::STORAGE_MAPPABLE_SHARED_IMAGE);
scoped_refptr<VideoFrame> output_frame = CreatePlatformVideoFrame(
output_layout->format(), output_layout->coded_size(),
gfx::Rect(output_image.Size()), output_image.Size(), base::TimeDelta(),
gfx::BufferUsage::GPU_READ_CPU_READ_WRITE);
if (output_storage_type == VideoFrame::STORAGE_MAPPABLE_SHARED_IMAGE) {
output_frame = CreateGpuMemoryBufferVideoFrame(
output_frame.get(), gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
test_sii_.get());
}
return output_frame;
}
void ImageProcessorClient::FrameReady(size_t frame_index,
scoped_refptr<VideoFrame> frame) {
DCHECK_CALLED_ON_VALID_THREAD(image_processor_client_thread_checker_);
base::AutoLock auto_lock_(output_lock_);
// VideoFrame should be processed in FIFO order.
EXPECT_EQ(frame_index, num_processed_frames_);
for (auto& processor : frame_processors_)
processor->ProcessVideoFrame(frame, frame_index);
num_processed_frames_++;
output_cv_.Signal();
}
bool ImageProcessorClient::WaitUntilNumImageProcessed(
size_t num_processed,
base::TimeDelta max_wait) {
base::TimeDelta time_waiting;
// NOTE: Acquire lock here does not matter, because
// base::ConditionVariable::TimedWait() unlocks output_lock_ at the start and
// locks again at the end.
base::AutoLock auto_lock_(output_lock_);
while (time_waiting < max_wait) {
if (num_processed_frames_ >= num_processed)
return true;
const base::TimeTicks start_time = base::TimeTicks::Now();
output_cv_.TimedWait(max_wait);
time_waiting += base::TimeTicks::Now() - start_time;
}
return false;
}
bool ImageProcessorClient::WaitForFrameProcessors() {
bool success = true;
for (auto& processor : frame_processors_)
success &= processor->WaitUntilDone();
return success;
}
size_t ImageProcessorClient::GetNumOfProcessedImages() const {
base::AutoLock auto_lock_(output_lock_);
return num_processed_frames_;
}
size_t ImageProcessorClient::GetErrorCount() const {
base::AutoLock auto_lock_(output_lock_);
return image_processor_error_count_;
}
void ImageProcessorClient::NotifyError() {
DCHECK_CALLED_ON_VALID_THREAD(image_processor_client_thread_checker_);
base::AutoLock auto_lock_(output_lock_);
image_processor_error_count_++;
}
void ImageProcessorClient::Process(const Image& input_image,
const Image& output_image) {
DCHECK_CALLED_ON_VALID_THREAD(test_main_thread_checker_);
auto input_frame = CreateInputFrame(input_image);
ASSERT_TRUE(input_frame);
auto output_frame = CreateOutputFrame(output_image);
ASSERT_TRUE(output_frame);
image_processor_client_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&ImageProcessorClient::ProcessTask, base::Unretained(this),
std::move(input_frame), std::move(output_frame)));
}
void ImageProcessorClient::ProcessTask(scoped_refptr<VideoFrame> input_frame,
scoped_refptr<VideoFrame> output_frame) {
DCHECK_CALLED_ON_VALID_THREAD(image_processor_client_thread_checker_);
// base::Unretained(this) and std::cref() for FrameReadyCB is safe here
// because the callback is executed on |image_processor_client_thread_| which
// is owned by this class.
image_processor_->Process(std::move(input_frame), std::move(output_frame),
base::BindPostTaskToCurrentDefault(base::BindOnce(
&ImageProcessorClient::FrameReady,
base::Unretained(this), next_frame_index_)));
next_frame_index_++;
}
} // namespace media::test
|