Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1484)

Unified Diff: content/common/gpu/media/gpu_jpeg_decode_accelerator.cc

Issue 1016773002: MJPEG acceleration for video capture using VAAPI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix coded size, shm handle Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..6ffdc90d91eb9afb97677fda2f79f1fb2230282e
--- /dev/null
+++ b/content/common/gpu/media/gpu_jpeg_decode_accelerator.cc
@@ -0,0 +1,283 @@
+// 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"
+
+#if defined(OS_WIN)
+#elif defined(OS_MACOSX)
+#elif defined(OS_CHROMEOS)
+#if defined(ARCH_CPU_ARMEL)
+//#include "content/common/gpu/media/v4l2_jpeg_decode_accelerator.h"
+//#include "content/common/gpu/media/v4l2_jpeg_device.h"
+#endif // defined(ARCH_CPU_ARMEL)
+#if defined(ARCH_CPU_X86_FAMILY)
+#include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h"
+#endif // defined(ARCH_CPU_X86_FAMILY)
+#elif defined(USE_OZONE)
+#elif defined(OS_ANDROID)
+#endif
+
+#include "ui/gfx/geometry/size.h"
+
+// XXX
+#undef DLOG
+#define DLOG LOG
+namespace content {
+
+// DebugAutoLock works like AutoLock but only acquires the lock when
+// DCHECK is on.
+#if DCHECK_IS_ON
+typedef base::AutoLock DebugAutoLock;
+#else
+class DebugAutoLock {
+ public:
+ explicit DebugAutoLock(base::Lock&) {}
+};
+#endif
+
+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;
+ LOG(ERROR) << __func__;
+
+ 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)
+ /*,
+ weak_factory_for_io_(this)*/ {
+ child_message_loop_ = base::MessageLoopProxy::current();
+}
+
+GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() {
+ // This class can only be self-deleted from OnWillDestroyStub(), which means
+ // the VDA has already been destroyed in there.
+ DCHECK(!jpeg_decode_accelerator_);
+}
+
+bool GpuJpegDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAccelerator, msg)
+ IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Decode, OnDecode)
+ IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, OnDestroy)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+bool GpuJpegDecodeAccelerator::Initialize() {
wuchengli 2015/03/23 06:30:15 DCHECK this runs on child_message_loop_. Same for
kcwu 2015/04/16 14:38:26 Done.
+ LOG(ERROR) << __func__;
+ DCHECK(!jpeg_decode_accelerator_.get());
+
+ if (!channel_->AddRoute(host_route_id_, this)) {
+ DLOG(ERROR) << "GpuJpegDecodeAccelerator::Initialize(): "
+ "failed to add route";
+ return false;
+ }
+
+#if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY)
+ jpeg_decode_accelerator_.reset(
+ new VaapiJpegDecodeAccelerator());
+#else
+ NOTIMPLEMENTED() << "HW JPEG decode acceleration not available.";
wuchengli 2015/03/23 06:30:14 This shouldn't be an error. DLOG or DVLOG
kcwu 2015/04/14 20:02:34 Done.
+ return false;
+#endif
+
+ if (!jpeg_decode_accelerator_->Initialize(this)) {
+ return false;
+ }
+ return true;
+}
+
+void GpuJpegDecodeAccelerator::NotifyError(
+ int32_t buffer_id,
+ media::JpegDecodeAccelerator::Error error) {
+ Send(new AcceleratedJpegDecoderHostMsg_NotifyError(
+ host_route_id_, buffer_id, error));
+}
+
+void GpuJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) {
+ Send(new AcceleratedJpegDecoderHostMsg_VideoFrameReady(
+ host_route_id_, bitstream_buffer_id));
+}
+
+void DecodeFinished(scoped_ptr<base::SharedMemory> shm) {
+ // do nothing
+ LOG(ERROR) << "DecodeFinished";
+}
+
+// Runs on IO thread if jpeg_decode_accelerator_->CanDecodeOnIOThread() is
+// true, otherwise on the main thread.
+void GpuJpegDecodeAccelerator::OnDecode(
+ const AcceleratedJpegDecoderMsg_Decode_Params& params) {
+ LOG(ERROR) << __func__;
wuchengli 2015/03/23 06:30:15 Add DCHECK to make sure this runs on IO thread.
kcwu 2015/04/14 20:02:34 Done.
+#if 0
+ DCHECK(jpeg_decode_accelerator_.get());
+#endif
+
+ // TODO(kcwu) validate input values
+#if 0
+ if (id < 0) {
+ DLOG(ERROR) << "BitstreamBuffer id " << id << " out of range";
+ if (child_message_loop_->BelongsToCurrentThread()) {
wuchengli 2015/03/23 06:30:15 remove because this only runs on IO thread.
kcwu 2015/03/30 18:12:14 Done.
+ NotifyDecodeError(media::JpegDecodeAccelerator::INVALID_ARGUMENT);
+ } else {
+ child_message_loop_->PostTask(
+ FROM_HERE,
+ base::Bind(&GpuJpegDecodeAccelerator::NotifyError,
+ base::Unretained(this),
+ media::JpegDecodeAccelerator::INVALID_ARGUMENT));
+ }
+ return;
+ }
+#endif
+ 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)) {
wuchengli 2015/03/23 06:30:15 Can we do Map in VaapiJDA::DecodeTask so this won'
kcwu 2015/04/14 20:02:34 VaapiJDA::Decode expect VideoFrame, so we have to
+ DLOG(ERROR) << "GpuJpegDecodeAccelerator::OnDecode(): "
+ "could not map frame_id=" << 0; // TODO(kcwu)
+ 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,
+ 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) << "GpuJpegDecodeAccelerator::OnDecode(): "
+ << "could not create VideoFrame for buffer_id="
+ << params.input_buffer_id;
+ NotifyError(params.input_buffer_id,
+ media::JpegDecodeAccelerator::PLATFORM_FAILURE);
+ return;
+ }
+
+ jpeg_decode_accelerator_->Decode(input_buffer, frame);
+}
+
+void GpuJpegDecodeAccelerator::OnDestroy() {
+ LOG(ERROR) << __func__;
+ DCHECK(jpeg_decode_accelerator_.get());
+ //OnWillDestroyStub();
+}
+
+void GpuJpegDecodeAccelerator::OnFilterRemoved() {
+ // We're destroying; cancel all callbacks.
+// weak_factory_for_io_.InvalidateWeakPtrs();
+ filter_removed_.Signal();
+}
+
+#if 0
+void GpuJpegDecodeAccelerator::OnWillDestroyStub() {
+ // The stub is going away, so we have to stop and destroy VDA here, before
+ // returning, because the VDA may need the GL context to run and/or do its
+ // cleanup. We cannot destroy the VDA before the IO thread message filter is
+ // removed however, since we cannot service incoming messages with VDA gone.
+ // We cannot simply check for existence of VDA 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 VDA after it arrives and before returning.
+ if (filter_.get()) {
+ filter_removed_.Wait();
+ }
+
+
+ jpeg_decode_accelerator_.reset();
+ delete this;
+}
+#endif
+
+bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) {
+ if (filter_.get() && io_message_loop_->BelongsToCurrentThread())
+ return filter_->SendOnIOThread(message);
+ DCHECK(child_message_loop_->BelongsToCurrentThread());
+ return channel_->Send(message);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698