Commit 422fbbe4 authored by Chih-Yu Huang's avatar Chih-Yu Huang Committed by Commit Bot

Reland "media/gpu: Add ImageProcessorWithPool class."

This CL adds ImageProcessorWithPool class, which is a simple client of
ImageProcessor. By injecting a DmabufVideoFramePool for allocating
output frames and a callback for receiving processed frames, the
caller could process input frames by Process() without managing output
buffer.

BUG=chromium:1004727
TEST=ninja image_processor_test

Change-Id: I472366e4bf9460a304fcc8c4d43277c21e16c2c0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1918917Reviewed-by: default avatarHirokazu Honda <hiroh@chromium.org>
Commit-Queue: Chih-Yu Huang <akahuang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#716086}
parent a2e3c1ce
......@@ -53,6 +53,8 @@ source_set("common") {
"image_processor.cc",
"image_processor.h",
"image_processor_factory.h",
"image_processor_with_pool.cc",
"image_processor_with_pool.h",
"libyuv_image_processor.cc",
"libyuv_image_processor.h",
"mailbox_video_frame_converter.cc",
......
// Copyright 2019 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/gpu/chromeos/image_processor_with_pool.h"
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "media/gpu/chromeos/gpu_buffer_layout.h"
#include "media/gpu/macros.h"
namespace media {
// static
std::unique_ptr<ImageProcessorWithPool> ImageProcessorWithPool::Create(
std::unique_ptr<ImageProcessor> image_processor,
DmabufVideoFramePool* const frame_pool,
size_t num_frames,
const scoped_refptr<base::SequencedTaskRunner> task_runner) {
const ImageProcessor::PortConfig& config = image_processor->output_config();
base::Optional<GpuBufferLayout> layout = frame_pool->RequestFrames(
config.fourcc, config.size, gfx::Rect(config.visible_size), config.size,
num_frames);
if (!layout || layout->size() != config.size) {
VLOGF(1) << "Failed to request frame with correct size. "
<< config.size.ToString() << " != "
<< (layout ? layout->size().ToString() : gfx::Size().ToString());
return nullptr;
}
return base::WrapUnique<ImageProcessorWithPool>(new ImageProcessorWithPool(
std::move(image_processor), frame_pool, std::move(task_runner)));
}
ImageProcessorWithPool::ImageProcessorWithPool(
std::unique_ptr<ImageProcessor> image_processor,
DmabufVideoFramePool* const frame_pool,
const scoped_refptr<base::SequencedTaskRunner> task_runner)
: image_processor_(std::move(image_processor)),
frame_pool_(frame_pool),
task_runner_(std::move(task_runner)) {
DVLOGF(3);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
weak_this_ = weak_this_factory_.GetWeakPtr();
}
ImageProcessorWithPool::~ImageProcessorWithPool() {
DVLOGF(3);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void ImageProcessorWithPool::Reset() {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
image_processor_->Reset();
pending_frames_ = {};
num_frames_in_ip_ = 0;
// Cancel pending tasks to avoid returning previous frames to the client.
weak_this_factory_.InvalidateWeakPtrs();
weak_this_ = weak_this_factory_.GetWeakPtr();
}
bool ImageProcessorWithPool::HasPendingFrames() const {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return !pending_frames_.empty() || num_frames_in_ip_ > 0;
}
void ImageProcessorWithPool::Process(scoped_refptr<VideoFrame> frame,
FrameReadyCB ready_cb) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pending_frames_.push(std::make_pair(std::move(frame), std::move(ready_cb)));
PumpProcessFrames();
}
void ImageProcessorWithPool::PumpProcessFrames() {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
while (!pending_frames_.empty()) {
scoped_refptr<VideoFrame> output_frame = frame_pool_->GetFrame();
if (!output_frame) {
// Notify when pool is available.
frame_pool_->NotifyWhenFrameAvailable(base::BindOnce(
base::IgnoreResult(&base::SequencedTaskRunner::PostTask),
task_runner_, FROM_HERE,
base::BindOnce(&ImageProcessorWithPool::PumpProcessFrames,
weak_this_)));
break;
}
scoped_refptr<VideoFrame> input_frame =
std::move(pending_frames_.front().first);
FrameReadyCB ready_cb = std::move(pending_frames_.front().second);
pending_frames_.pop();
++num_frames_in_ip_;
image_processor_->Process(
std::move(input_frame), std::move(output_frame),
base::BindOnce(&ImageProcessorWithPool::OnFrameProcessed, weak_this_,
std::move(ready_cb)));
}
}
void ImageProcessorWithPool::OnFrameProcessed(FrameReadyCB ready_cb,
scoped_refptr<VideoFrame> frame) {
DVLOGF(4);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_NE(num_frames_in_ip_, 0u);
--num_frames_in_ip_;
std::move(ready_cb).Run(std::move(frame));
}
} // namespace media
// Copyright 2019 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.
#ifndef MEDIA_GPU_CHROMEOS_IMAGE_PROCESSOR_WITH_POOL_H_
#define MEDIA_GPU_CHROMEOS_IMAGE_PROCESSOR_WITH_POOL_H_
#include <memory>
#include "base/containers/queue.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/sequenced_task_runner.h"
#include "media/base/video_frame.h"
#include "media/gpu/chromeos/dmabuf_video_frame_pool.h"
#include "media/gpu/chromeos/image_processor.h"
#include "media/gpu/media_gpu_export.h"
namespace media {
// A simple client implementation of ImageProcessor.
// By giving DmabufVideoFramePool, which provides VideoFrame for output, the
// caller can process input frames without managing VideoFrame for output.
class ImageProcessorWithPool {
public:
using FrameReadyCB = ImageProcessor::FrameReadyCB;
// Create ImageProcessorWithPool instance. |num_frames| is the number of
// frames requested from |frame_pool|.
static std::unique_ptr<ImageProcessorWithPool> Create(
std::unique_ptr<ImageProcessor> image_processor,
DmabufVideoFramePool* const frame_pool,
size_t num_frames,
const scoped_refptr<base::SequencedTaskRunner> task_runner);
~ImageProcessorWithPool();
// Processes |frame| by |image_processor_|. The processed output frame will be
// returned by |ready_cb|, which is called on |task_runner_|.
void Process(scoped_refptr<VideoFrame> frame, FrameReadyCB ready_cb);
// Returns true if there are pending frames.
bool HasPendingFrames() const;
// Abandons all pending process. After called, no previous frame will be
// returned by |ready_cb|.
void Reset();
private:
ImageProcessorWithPool(
std::unique_ptr<ImageProcessor> image_processor,
DmabufVideoFramePool* const frame_pool,
const scoped_refptr<base::SequencedTaskRunner> task_runner);
// Tries to pass pending frames to |image_processor_|.
void PumpProcessFrames();
// Called when |image_processor_| finishes processing frames.
void OnFrameProcessed(FrameReadyCB ready_cb, scoped_refptr<VideoFrame> frame);
// The image processor for processing frames.
std::unique_ptr<ImageProcessor> image_processor_;
// The frame pool to allocate output frames of the image processor.
// The caller should guarantee the pool alive during the lifetime of this
// ImageProcessorWithPool instance.
DmabufVideoFramePool* const frame_pool_;
// The pending input frames that wait for passing to |image_processor_|.
base::queue<std::pair<scoped_refptr<VideoFrame>, FrameReadyCB>>
pending_frames_;
// Number of frames that are processed in |image_processor_|.
size_t num_frames_in_ip_ = 0;
// The main task runner and its checker. All methods should be called on
// |task_runner_|.
const scoped_refptr<base::SequencedTaskRunner> task_runner_;
SEQUENCE_CHECKER(sequence_checker_);
// WeakPtr of this instance and its factory, bound to |task_runner_|.
base::WeakPtr<ImageProcessorWithPool> weak_this_;
base::WeakPtrFactory<ImageProcessorWithPool> weak_this_factory_{this};
};
} // namespace media
#endif // MEDIA_GPU_CHROMEOS_IMAGE_PROCESSOR_WITH_POOL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment