Chromium Code Reviews| Index: content/common/gpu/media/gpu_jpeg_decode_accelerator.cc |
| diff --git a/content/common/gpu/media/gpu_jpeg_decode_accelerator.cc b/content/common/gpu/media/gpu_jpeg_decode_accelerator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5354a21a21ee5e3106622a9ddd5a791bb20f4e73 |
| --- /dev/null |
| +++ b/content/common/gpu/media/gpu_jpeg_decode_accelerator.cc |
| @@ -0,0 +1,251 @@ |
| +// Copyright 2015 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 "content/common/gpu/media/gpu_jpeg_decode_accelerator.h" |
| + |
| +#include <stdint.h> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "base/stl_util.h" |
| +#include "content/common/gpu/gpu_channel.h" |
| +#include "content/common/gpu/gpu_messages.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "gpu/command_buffer/common/command_buffer.h" |
| +#include "ipc/ipc_message_macros.h" |
| +#include "ipc/ipc_message_utils.h" |
| +#include "ipc/message_filter.h" |
| +#include "media/base/limits.h" |
| +#include "media/filters/jpeg_parser.h" |
| +#include "ui/gfx/geometry/size.h" |
| + |
| +#if defined(OS_CHROMEOS) |
| +#if defined(ARCH_CPU_X86_FAMILY) |
| +#include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h" |
| +#endif // defined(ARCH_CPU_X86_FAMILY) |
| +#endif |
| + |
| +namespace base { |
| + |
| +void DefaultDeleter<content::GpuJpegDecodeAccelerator>::operator()( |
| + void* jpeg_decode_accelerator) const { |
| + static_cast<content::GpuJpegDecodeAccelerator*>(jpeg_decode_accelerator) |
| + ->Destroy(); |
| +} |
| + |
| +} // namespace base |
|
wuchengli
2015/04/15 07:11:58
add a blank line
kcwu
2015/04/16 14:38:28
Done.
|
| +namespace content { |
| + |
| +class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { |
| + public: |
| + MessageFilter(GpuJpegDecodeAccelerator* owner, int32 host_route_id) |
| + : owner_(owner), host_route_id_(host_route_id) {} |
| + |
| + void OnChannelError() override { sender_ = NULL; } |
| + |
| + void OnChannelClosing() override { sender_ = NULL; } |
| + |
| + void OnFilterAdded(IPC::Sender* sender) override { sender_ = sender; } |
| + |
| + void OnFilterRemoved() override { |
| + // This will delete |owner_| and |this|. |
| + owner_->OnFilterRemoved(); |
| + } |
| + |
| + bool OnMessageReceived(const IPC::Message& msg) override { |
| + if (msg.routing_id() != host_route_id_) |
| + return false; |
| + DVLOG(3) << __func__; |
|
wuchengli
2015/04/15 07:11:58
The log in OnDecode should be enough. This can be
kcwu
2015/04/16 14:38:28
Done.
|
| + |
| + IPC_BEGIN_MESSAGE_MAP(MessageFilter, msg) |
| + IPC_MESSAGE_FORWARD(AcceleratedJpegDecoderMsg_Decode, owner_, |
| + GpuJpegDecodeAccelerator::OnDecode) |
| + IPC_MESSAGE_UNHANDLED(return false;) |
| + IPC_END_MESSAGE_MAP() |
| + return true; |
| + } |
| + |
| + bool SendOnIOThread(IPC::Message* message) { |
| + DCHECK(!message->is_sync()); |
| + if (!sender_) { |
| + delete message; |
| + return false; |
| + } |
| + return sender_->Send(message); |
| + } |
| + |
| + protected: |
| + virtual ~MessageFilter() {} |
| + |
| + private: |
| + GpuJpegDecodeAccelerator* owner_; |
| + int32 host_route_id_; |
| + // The sender to which this filter was added. |
| + IPC::Sender* sender_; |
| +}; |
| + |
| +GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( |
| + GpuChannel* channel, |
| + int32 host_route_id, |
| + const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| + : channel_(channel), |
| + host_route_id_(host_route_id), |
| + filter_removed_(true, false), |
| + io_message_loop_(io_message_loop) { |
| + child_message_loop_ = base::MessageLoopProxy::current(); |
| +} |
| + |
| +GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { |
| +} |
| + |
| +bool GpuJpegDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAccelerator, msg) |
| + IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, OnDestroy) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +bool GpuJpegDecodeAccelerator::Initialize() { |
| + DVLOG(3) << __func__; |
| + DCHECK(child_message_loop_->BelongsToCurrentThread()); |
| + DCHECK(!jpeg_decode_accelerator_.get()); |
| + |
| + if (!channel_->AddRoute(host_route_id_, this)) { |
| + DLOG(ERROR) << "GpuJpegDecodeAccelerator::Initialize(): " |
| + "failed to add route"; |
| + return false; |
| + } |
| + |
| + filter_ = new MessageFilter(this, host_route_id_); |
| + channel_->AddFilter(filter_.get()); |
| + |
| +#if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
| + jpeg_decode_accelerator_.reset(new VaapiJpegDecodeAccelerator()); |
| +#else |
| + DVLOG(1) << "HW JPEG decode acceleration not available."; |
| + return false; |
| +#endif |
| + |
| + if (!jpeg_decode_accelerator_->Initialize(this)) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +void GpuJpegDecodeAccelerator::NotifyError( |
| + int32_t buffer_id, |
| + media::JpegDecodeAccelerator::Error error) { |
| + DVLOG(3) << __func__; |
| + Send(new AcceleratedJpegDecoderHostMsg_NotifyError(host_route_id_, buffer_id, |
| + error)); |
| +} |
| + |
| +void GpuJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { |
| + // This is called from JDA's decode thread. |
|
wuchengli
2015/04/15 07:11:58
This should be called from IO thread to reduce the
kcwu
2015/04/20 17:47:58
I don't think this will reduce latency. The flow i
|
| + DVLOG(3) << __func__; |
| + Send(new AcceleratedJpegDecoderHostMsg_VideoFrameReady(host_route_id_, |
| + bitstream_buffer_id)); |
| +} |
| + |
| +void DecodeFinished(scoped_ptr<base::SharedMemory> shm) { |
| + // Do nothing. The purpose of this function is just to keep life-cycle of |
| + // SharedMemory as VideoFrame |
|
wuchengli
2015/04/15 07:11:58
I don't understand. What do you mean?
kcwu
2015/04/20 17:47:58
Done.
Comment updated.
|
| + DVLOG(3) << "DecodeFinished"; |
|
wuchengli
2015/04/15 07:11:58
__func__
kcwu
2015/04/16 14:38:28
Done.
|
| +} |
| + |
| +void GpuJpegDecodeAccelerator::OnDecode( |
| + const AcceleratedJpegDecoderMsg_Decode_Params& params) { |
| + DVLOG(3) << __func__; |
| + DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| + DCHECK(jpeg_decode_accelerator_.get()); |
| + |
| + if (params.input_buffer_id < 0) { |
| + DLOG(ERROR) << "BitstreamBuffer id " << params.input_buffer_id |
|
wuchengli
2015/04/15 07:11:58
s/DLOG/LOG/
kcwu
2015/04/16 14:38:28
Done.
|
| + << " out of range"; |
| + NotifyError(params.input_buffer_id, |
| + media::JpegDecodeAccelerator::INVALID_ARGUMENT); |
| + return; |
| + } |
| + |
| + media::BitstreamBuffer input_buffer(params.input_buffer_id, |
| + params.input_buffer_handle, |
| + params.input_buffer_size); |
| + |
| + scoped_ptr<base::SharedMemory> output_shm( |
| + new base::SharedMemory(params.output_video_frame_handle, false)); |
| + if (!output_shm->Map(params.output_buffer_size)) { |
| + DLOG(ERROR) << "Could not map output shared memory for input buffer id " |
|
wuchengli
2015/04/15 07:11:58
s/DLOG/LOG/
kcwu
2015/04/16 14:38:28
Done.
|
| + << params.input_buffer_id; |
| + NotifyError(params.input_buffer_id, |
| + media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| + return; |
| + } |
| + |
| + uint8* shm_memory = reinterpret_cast<uint8*>(output_shm->memory()); |
| + scoped_refptr<media::VideoFrame> frame = |
| + media::VideoFrame::WrapExternalPackedMemory( |
| + media::VideoFrame::I420, |
| + params.coded_size, |
| + gfx::Rect(params.coded_size), |
| + params.coded_size, |
|
wuchengli
2015/04/15 07:11:58
We don't really care about the visible size here.
kcwu
2015/04/16 14:38:28
Yes
|
| + shm_memory, |
| + params.output_buffer_size, |
| + params.output_video_frame_handle, |
| + 0, |
| + base::TimeDelta(), |
| + base::Bind(DecodeFinished, base::Passed(&output_shm))); |
| + |
| + if (!frame.get()) { |
| + DLOG(ERROR) << "Could not create VideoFrame for input buffer id " |
|
wuchengli
2015/04/15 07:11:58
s/DLOG/LOG/
kcwu
2015/04/16 14:38:28
Done.
|
| + << params.input_buffer_id; |
| + NotifyError(params.input_buffer_id, |
| + media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| + return; |
| + } |
| + |
| + jpeg_decode_accelerator_->Decode(input_buffer, frame); |
| +} |
| + |
| +void GpuJpegDecodeAccelerator::OnDestroy() { |
|
wuchengli
2015/04/15 07:11:58
DCHECK main thread
kcwu
2015/04/20 17:47:58
Done.
|
| + DVLOG(3) << __func__; |
| + DCHECK(jpeg_decode_accelerator_.get()); |
| + Destroy(); |
| +} |
| + |
| +void GpuJpegDecodeAccelerator::OnFilterRemoved() { |
| + // We're destroying; cancel all callbacks. |
| + filter_removed_.Signal(); |
| +} |
| + |
| +void GpuJpegDecodeAccelerator::Destroy() { |
|
wuchengli
2015/04/15 07:11:57
Do we need this method? Can this be combined with
kcwu
2015/04/20 17:47:58
There are two flows to destroy GJDA:
1. msg -> OnD
|
| + // We cannot destroy the JDA before the IO thread message filter is |
| + // removed however, since we cannot service incoming messages with JDA gone. |
| + // We cannot simply check for existence of JDA on IO thread though, because |
| + // we don't want to synchronize the IO thread with the ChildThread. |
| + // So we have to wait for the RemoveFilter callback here instead and remove |
| + // the JDA after it arrives and before returning. |
| + if (filter_.get()) { |
|
wuchengli
2015/04/15 07:11:58
|filter_| should always exist after Initialize. Th
kcwu
2015/04/20 17:47:58
This is necessary for safety if GpuJpegDecodeAccel
|
| + channel_->RemoveFilter(filter_.get()); |
| + filter_removed_.Wait(); |
| + } |
| + |
| + channel_->RemoveRoute(host_route_id_); |
|
wuchengli
2015/04/15 07:11:58
This can be called in ReleaseJpegDecoder.
kcwu
2015/04/20 17:47:58
AddRoute is in Initialize(). I'd like RemoveRoute
|
| + channel_->ReleaseJpegDecoder(); |
| + jpeg_decode_accelerator_.reset(); |
| + |
| + delete this; |
| +} |
| + |
| +bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { |
| + if (filter_.get() && io_message_loop_->BelongsToCurrentThread()) |
|
wuchengli
2015/04/15 07:11:58
Remove filter_.get() check.
kcwu
2015/04/20 17:47:58
Done.
|
| + return filter_->SendOnIOThread(message); |
| + return channel_->Send(message); |
| +} |
| + |
| +} // namespace content |