Index: content/browser/renderer_host/gpu_jpeg_decoder.cc |
diff --git a/content/browser/renderer_host/gpu_jpeg_decoder.cc b/content/browser/renderer_host/gpu_jpeg_decoder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5c6d988b8d5d6ca90d9c6de3168836ca760c7ba3 |
--- /dev/null |
+++ b/content/browser/renderer_host/gpu_jpeg_decoder.cc |
@@ -0,0 +1,200 @@ |
+// 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/browser/renderer_host/gpu_jpeg_decoder.h" |
+ |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "base/trace_event/trace_event.h" |
+#include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
+#include "content/common/gpu/client/gpu_jpeg_decode_accelerator_host.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "media/base/video_frame.h" |
+ |
+namespace content { |
+ |
+// static |
+bool GpuJpegDecoder::Supported() { |
+// A lightweight check for caller to avoid IPC latency for known unsupported |
+// platform. Initialize() can do the real platform supporting check but it |
+// requires an IPC even for platforms that do not support HW decoder. |
+// TODO(kcwu): move this information to GpuInfo. |
+#if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
+ return true; |
+#endif |
+ return false; |
+} |
+ |
+GpuJpegDecoder::GpuJpegDecoder(media::VideoCaptureDevice::Client* device_client, |
+ const DecodeDoneCB& decode_done_cb) |
+ : device_client_(device_client), |
+ decode_done_cb_(decode_done_cb), |
+ capture_task_runner_(base::MessageLoopProxy::current()), |
+ initialized_(false), |
+ failed_(false), |
+ next_bitstream_buffer_id_(0), |
+ in_buffer_(media::JpegDecodeAccelerator::kInvalidBitstreamBufferId, |
+ base::SharedMemory::NULLHandle(), |
+ 0) { |
+} |
+ |
+GpuJpegDecoder::~GpuJpegDecoder() { |
+ DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
+} |
+ |
+bool GpuJpegDecoder::IsDecoding_Locked() { |
+ lock_.AssertAcquired(); |
+ return !decode_done_closure_.is_null(); |
+} |
+ |
+bool GpuJpegDecoder::Initialize() { |
+ DVLOG(3) << __func__; |
+ DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
+ base::AutoLock lock(lock_); |
+ if (!initialized_) { |
+ GpuChannelHost* const host = |
+ BrowserGpuChannelHostFactory::instance()->GetGpuChannel(); |
+ decoder_ = host->CreateJpegDecoder( |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
+ if (!decoder_->Initialize(this)) { |
+ decoder_.reset(); |
+ failed_ = true; |
+ } |
+ initialized_ = true; |
+ } |
+ return !failed_; |
+} |
+ |
+void GpuJpegDecoder::VideoFrameReady(int32_t bitstream_buffer_id) { |
+ DVLOG(3) << __func__; |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ TRACE_EVENT0("jpeg", "GpuJpegDecoder::VideoFrameReady"); |
+ base::AutoLock lock(lock_); |
+ |
+ if (bitstream_buffer_id == |
+ media::JpegDecodeAccelerator::kInvalidBitstreamBufferId) |
+ return; |
+ |
+ if (!IsDecoding_Locked()) { |
+ LOG(ERROR) << "Got decode response while not decoding"; |
+ return; |
+ } |
+ |
+ if (bitstream_buffer_id != in_buffer_.id()) { |
+ LOG(ERROR) << "Unexpected bitstream_buffer_id " << bitstream_buffer_id |
+ << ", expected " << in_buffer_.id(); |
+ return; |
+ } |
+ |
+ decode_done_closure_.Run(); |
+ decode_done_closure_.Reset(); |
+ |
+ TRACE_EVENT_ASYNC_END0("jpeg", "GpuJpegDecoder decoding", |
+ bitstream_buffer_id); |
+} |
+ |
+void GpuJpegDecoder::NotifyError(int32_t bitstream_buffer_id, |
+ media::JpegDecodeAccelerator::Error error) { |
+ DVLOG(3) << __func__; |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ LOG(ERROR) << "Decode error, bitstream_buffer_id=" << bitstream_buffer_id; |
+ |
+ base::AutoLock lock(lock_); |
+ failed_ = true; |
+ decode_done_closure_.Reset(); |
+} |
+ |
+bool GpuJpegDecoder::DecodeCapturedData( |
+ const uint8* data, |
+ size_t in_buffer_size, |
+ const media::VideoCaptureFormat& frame_format, |
+ int rotation, |
+ const base::TimeTicks& timestamp) { |
+ DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
+ DCHECK(initialized_); |
+ TRACE_EVENT_ASYNC_BEGIN0("jpeg", "GpuJpegDecoder decoding", |
+ next_bitstream_buffer_id_); |
+ TRACE_EVENT0("jpeg", "GpuJpegDecoder::DecodeCapturedData"); |
+ |
+ { |
+ base::AutoLock lock(lock_); |
+ // Conditions that lead to return true, which means dropping frame, should |
+ // be checked first. Otherwise the latter frames may be decoded (via caller |
+ // fallback to software decoding) before the former frame. |
mcasas
2015/05/07 00:59:03
Wait, is there still fallback to software decoding
wuchengli
2015/05/07 06:23:14
I discussed with Kuang-che. He'll remove the fallb
kcwu
2015/05/08 14:42:42
Done.
|
+ if (IsDecoding_Locked()) { |
+ DVLOG(1) << "Drop captured frame. Previous jpeg frame is still decoding"; |
+ return true; |
+ } |
+ if (failed_) |
+ return false; |
wuchengli
2015/05/06 07:59:18
Can we return a enum from this function? SUCCESS (
kcwu
2015/05/08 14:42:42
Acknowledged.
Revised to use jpeg thread.
|
+ if (rotation != 0) // unsupported |
+ return false; |
+ } |
+ |
+ // Since IsDecoding_Locked() is false here and we only decode a frame at a |
wuchengli
2015/05/04 14:14:41
This is not right. Suppose DecodeCapturedData is c
kcwu
2015/05/08 14:42:42
Acknowledged.
Revised to use jpeg thread.
|
+ // time, after this point, the only possible multithread access to this class |
+ // is NotifyError. This means |failed_| may be set to true when running |
+ // following code. |
+ |
+ const gfx::Size dimensions = frame_format.frame_size; |
+ scoped_ptr<media::VideoCaptureDevice::Client::Buffer> out_buffer( |
+ device_client_->ReserveOutputBuffer(media::PIXEL_FORMAT_I420, |
+ dimensions)); |
+ if (!out_buffer.get()) { |
+ DVLOG(1) << "Drop captured frame. No available buffers" |
+ << " (all buffers are still queued to display)"; |
+ return false; |
+ } |
+ |
+ // Enlarge input buffer if necessary. |
+ // Since IsDecoding_Locked() is false, |in_shared_memory_| will not be |
+ // accessed concurrently on another thread or even GPU process. It's safe to |
+ // change |in_shared_memory_| without lock. |
+ if (!in_shared_memory_.get() || |
+ in_buffer_size > in_shared_memory_->mapped_size()) { |
+ in_shared_memory_.reset(new base::SharedMemory); |
+ if (!in_shared_memory_->CreateAndMapAnonymous(in_buffer_size)) { |
+ DLOG(ERROR) << "CreateAndMapAnonymous failed, size=" << in_buffer_size; |
+ base::AutoLock lock(lock_); |
+ failed_ = true; |
+ return false; |
+ } |
+ } |
+ |
+ media::BitstreamBuffer in_buffer(next_bitstream_buffer_id_, |
+ in_shared_memory_->handle(), in_buffer_size); |
+ // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. |
+ next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF; |
+ |
+ scoped_refptr<media::VideoFrame> out_frame = |
+ media::VideoFrame::WrapExternalPackedMemory( |
+ media::VideoFrame::I420, |
+ dimensions, |
+ gfx::Rect(dimensions), |
+ dimensions, |
+ reinterpret_cast<uint8*>(out_buffer->data()), |
+ out_buffer->size(), |
+ out_buffer->AsPlatformHandle(), |
+ 0, |
+ base::TimeDelta(), |
+ base::Closure()); |
+ DCHECK(out_frame.get()); |
+ |
+ out_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
+ frame_format.frame_rate); |
+ |
+ memcpy(in_shared_memory_->memory(), data, in_buffer_size); |
+ |
+ base::AutoLock lock(lock_); |
+ if (failed_) |
+ return false; |
+ in_buffer_ = in_buffer; |
+ decode_done_closure_ = base::Bind(decode_done_cb_, base::Passed(&out_buffer), |
+ out_frame, timestamp); |
+ |
+ decoder_->Decode(in_buffer_, out_frame); |
+ return true; |
+} |
+ |
+} // namespace content |