Index: media/gpu/fake_jpeg_decode_accelerator.cc |
diff --git a/media/gpu/fake_jpeg_decode_accelerator.cc b/media/gpu/fake_jpeg_decode_accelerator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3bb623ed8895aa849944a26b6be14dcdafe135e7 |
--- /dev/null |
+++ b/media/gpu/fake_jpeg_decode_accelerator.cc |
@@ -0,0 +1,105 @@ |
+// Copyright 2017 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/fake_jpeg_decode_accelerator.h" |
+ |
+#include "base/bind.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "media/filters/jpeg_parser.h" |
+#include "media/gpu/shared_memory_region.h" |
+#include "third_party/libyuv/include/libyuv.h" |
+ |
+namespace media { |
+ |
+FakeJpegDecodeAccelerator::FakeJpegDecodeAccelerator( |
+ const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
+ : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
+ io_task_runner_(std::move(io_task_runner)), |
+ weak_factory_(this) {} |
+ |
+FakeJpegDecodeAccelerator::~FakeJpegDecodeAccelerator() = default; |
+ |
+bool FakeJpegDecodeAccelerator::Initialize( |
+ JpegDecodeAccelerator::Client* client) { |
+ client_ = client; |
mcasas
2017/03/08 01:33:17
I guess here is
DCHECK(io_task_runner_->BelongsTo
chfremer
2017/03/08 18:58:08
In this case it is |task_runner_|, not |io_task_ru
|
+ return true; |
+} |
+ |
+void FakeJpegDecodeAccelerator::Decode( |
+ const BitstreamBuffer& bitstream_buffer, |
+ const scoped_refptr<VideoFrame>& video_frame) { |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
+ // SharedMemoryRegion will take over the |bitstream_buffer.handle()|. |
+ std::unique_ptr<SharedMemoryRegion> src_shm_( |
+ new SharedMemoryRegion(bitstream_buffer, true)); |
+ if (!src_shm_->Map()) { |
+ DLOG(ERROR) << "Unable to map shared memory in FakeJpegDecodeAccelerator"; |
+ NotifyError(bitstream_buffer.id(), JpegDecodeAccelerator::UNREADABLE_INPUT); |
+ return; |
+ } |
+ |
+ JpegParseResult parse_result; |
+ if (!ParseJpegPicture(static_cast<uint8_t*>(src_shm_->memory()), |
+ src_shm_->size(), &parse_result)) { |
+ DLOG(ERROR) << "ParseJpegPicture failed in FakeJpegDecodeAccelerator"; |
+ NotifyError(bitstream_buffer.id(), |
+ JpegDecodeAccelerator::PARSE_JPEG_FAILED); |
+ return; |
+ } |
+ const gfx::Size src_coded_size(parse_result.frame_header.coded_width, |
+ parse_result.frame_header.coded_height); |
+ DCHECK_GE(src_coded_size.width(), video_frame->coded_size().width()); |
+ DCHECK_GE(src_coded_size.height(), video_frame->coded_size().height()); |
+ |
+ uint8_t* dst_y = video_frame->data(VideoFrame::kYPlane); |
+ uint8_t* dst_u = video_frame->data(VideoFrame::kUPlane); |
+ uint8_t* dst_v = video_frame->data(VideoFrame::kVPlane); |
+ size_t dst_y_stride = video_frame->stride(VideoFrame::kYPlane); |
+ size_t dst_u_stride = video_frame->stride(VideoFrame::kUPlane); |
+ size_t dst_v_stride = video_frame->stride(VideoFrame::kVPlane); |
+ |
+ if (libyuv::ConvertToI420( |
+ static_cast<uint8_t*>(src_shm_->memory()), src_shm_->size(), dst_y, |
+ dst_y_stride, dst_u, dst_u_stride, dst_v, dst_v_stride, 0, 0, |
+ src_coded_size.width(), src_coded_size.height(), |
+ video_frame->coded_size().width(), video_frame->coded_size().height(), |
+ libyuv::kRotate0, libyuv::FOURCC_MJPG) != 0) { |
+ DLOG(ERROR) << "Software decode failed."; |
+ NotifyError(bitstream_buffer.id(), JpegDecodeAccelerator::PLATFORM_FAILURE); |
+ return; |
+ } |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread, |
+ weak_factory_.GetWeakPtr(), bitstream_buffer.id())); |
+} |
+ |
+bool FakeJpegDecodeAccelerator::IsSupported() { |
+ return true; |
+} |
+ |
+void FakeJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, |
+ Error error) { |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&FakeJpegDecodeAccelerator::NotifyErrorOnClientThread, |
+ weak_factory_.GetWeakPtr(), bitstream_buffer_id, error)); |
+} |
+ |
+void FakeJpegDecodeAccelerator::NotifyErrorOnClientThread( |
+ int32_t bitstream_buffer_id, |
+ Error error) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ client_->NotifyError(bitstream_buffer_id, error); |
+} |
+ |
+void FakeJpegDecodeAccelerator::OnDecodeDoneOnClientThread( |
+ int32_t input_buffer_id) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ client_->VideoFrameReady(input_buffer_id); |
+} |
+ |
+} // namespace media |