Chromium Code Reviews| Index: content/common/gpu/client/gpu_jpeg_decode_accelerator_host.cc |
| diff --git a/content/common/gpu/client/gpu_jpeg_decode_accelerator_host.cc b/content/common/gpu/client/gpu_jpeg_decode_accelerator_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1dcf6695be6a2617da40101f35b175a96db1c05 |
| --- /dev/null |
| +++ b/content/common/gpu/client/gpu_jpeg_decode_accelerator_host.cc |
| @@ -0,0 +1,210 @@ |
| +// 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/client/gpu_jpeg_decode_accelerator_host.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "content/common/gpu/client/gpu_channel_host.h" |
| +#include "content/common/gpu/gpu_messages.h" |
| +#include "content/common/view_messages.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "ipc/ipc_message_macros.h" |
| +#include "ipc/ipc_message_utils.h" |
| + |
| +#if defined(OS_WIN) |
| +#include "content/public/common/sandbox_init.h" |
| +#endif // OS_WIN |
| + |
| +using media::JpegDecodeAccelerator; |
| +namespace content { |
| + |
| +#define NOTIFY_ERROR(error) \ |
| + PostNotifyError(error); \ |
| + DLOG(ERROR) |
| + |
| +GpuJpegDecodeAcceleratorHost::GpuJpegDecodeAcceleratorHost( |
| + GpuChannelHost* channel) |
| + : channel_(channel), |
| + client_(NULL), |
| + decoder_route_id_(MSG_ROUTING_NONE), |
| + weak_this_factory_(this) { |
| + DCHECK(channel_); |
| +} |
| + |
| +GpuJpegDecodeAcceleratorHost::~GpuJpegDecodeAcceleratorHost() { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + if (channel_ && decoder_route_id_ != MSG_ROUTING_NONE) |
| + channel_->RemoveRoute(decoder_route_id_); |
| +} |
| + |
| +bool GpuJpegDecodeAcceleratorHost::OnMessageReceived(const IPC::Message& msg) { |
| + LOG(ERROR) << __func__; |
| + DCHECK(CalledOnValidThread()); |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAcceleratorHost, msg) |
| + IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderHostMsg_VideoFrameReady, |
| + OnVideoFrameReady) |
| + IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderHostMsg_NotifyError, |
| + OnNotifyError) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + DCHECK(handled); |
| + // See OnNotifyError for why |this| mustn't be used after OnNotifyError might |
| + // have been called above. |
| + return handled; |
| +} |
| + |
| +void GpuJpegDecodeAcceleratorHost::OnChannelError() { |
| + DCHECK(CalledOnValidThread()); |
| + if (channel_) { |
| + if (decoder_route_id_ != MSG_ROUTING_NONE) |
| + channel_->RemoveRoute(decoder_route_id_); |
| + channel_ = NULL; |
| + } |
| + DLOG(ERROR) << "OnChannelError()"; |
| + PostNotifyError(kInvalidBitstreamBufferId, PLATFORM_FAILURE); |
| +} |
| + |
| +bool GpuJpegDecodeAcceleratorHost::Initialize( |
| + media::JpegDecodeAccelerator::Client* client) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + int32 route_id = channel_->GenerateRouteID(); |
| + |
| + // XXX(kcwu): how to fix this hack? |
| + BrowserThread::PostTask( |
|
wuchengli
2015/03/23 06:30:14
Looks like GJDAH is accessed entirely on IO thread
kcwu
2015/03/30 18:12:14
I modified it to AddRoute from caller instead of h
|
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind( |
| + &GpuChannelHost::AddRoute, |
| + channel_, |
| + route_id, |
| + weak_this_factory_.GetWeakPtr())); |
| + |
| + bool succeeded = false; |
| + Send(new GpuMsg_CreateJpegDecoder(route_id, &succeeded)); |
| + |
| + if (!succeeded) { |
| + DLOG(ERROR) << "Send(GpuMsg_CreateJpegDecoder()) failed"; |
| + channel_->RemoveRoute(route_id); |
| + return false; |
| + } |
| + decoder_route_id_ = route_id; |
| + client_ = client; |
| + |
| + return true; |
| +} |
| + |
| +void GpuJpegDecodeAcceleratorHost::Decode( |
| + const media::BitstreamBuffer& bitstream_buffer, |
| + const scoped_refptr<media::VideoFrame>& video_frame) { |
| + DCHECK(CalledOnValidThread()); |
| + if (!channel_) |
| + return; |
| + |
| + base::SharedMemoryHandle input_handle = channel_->ShareToGpuProcess( |
| + bitstream_buffer.handle()); |
| + if (!base::SharedMemory::IsHandleValid(input_handle)) { |
| + DLOG(ERROR) << "Failed to duplicate buffer handler of BitstreamBuffer"; |
| + PostNotifyError(bitstream_buffer.id(), INVALID_ARGUMENT); |
| + return; |
| + } |
| + |
| + if (!base::SharedMemory::IsHandleValid(video_frame->shared_memory_handle())) { |
| + DLOG(ERROR) |
| + << "Decode(): cannot output to frame not backed by shared memory"; |
| + PostNotifyError(bitstream_buffer.id(), PLATFORM_FAILURE); |
| + return; |
| + } |
| + |
| + base::SharedMemoryHandle output_handle = channel_->ShareToGpuProcess( |
| + video_frame->shared_memory_handle()); |
| + if (!base::SharedMemory::IsHandleValid(output_handle)) { |
| + DLOG(ERROR) << "Decode(): failed to duplicate buffer handle of VideoFrame"; |
| + PostNotifyError(bitstream_buffer.id(), PLATFORM_FAILURE); |
| + return; |
| + } |
| + |
| + // TODO(kcwu) not sure about the alignment |
| + size_t output_buffer_size = media::VideoFrame::AllocationSize( |
| + video_frame->format(), video_frame->coded_size()); |
| + |
| + AcceleratedJpegDecoderMsg_Decode_Params decode_params; |
| + decode_params.coded_size = video_frame->coded_size(); |
| + decode_params.input_buffer_id = bitstream_buffer.id(); |
| + decode_params.input_buffer_handle = input_handle; |
| + decode_params.input_buffer_size = bitstream_buffer.size(); |
| + decode_params.output_video_frame_handle = output_handle; |
| + decode_params.output_buffer_size = output_buffer_size; |
| + Send(new AcceleratedJpegDecoderMsg_Decode(decoder_route_id_, decode_params)); |
| +} |
| + |
| +void GpuJpegDecodeAcceleratorHost::Destroy() { |
| + DCHECK(CalledOnValidThread()); |
| +#if 1 |
| + if (channel_) |
| + Send(new AcceleratedJpegDecoderMsg_Destroy(decoder_route_id_)); |
| +#endif |
| + delete this; |
| +} |
| + |
| +// TODO do we need this?? |
| +#if 0 |
| +void GpuJpegDecodeAcceleratorHost::OnWillDeleteImpl() { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + OnChannelError(); |
| +} |
| +#endif |
| + |
| +void GpuJpegDecodeAcceleratorHost::PostNotifyError( |
| + int32_t bitstream_buffer_id, Error error) { |
| + DCHECK(CalledOnValidThread()); |
| + DVLOG(2) << "PostNotifyDecodeResult(): error=" << error; |
| + // Post the error notification back to this thread, to avoid re-entrancy. |
| + base::MessageLoopProxy::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&GpuJpegDecodeAcceleratorHost::OnNotifyError, |
| + weak_this_factory_.GetWeakPtr(), |
| + bitstream_buffer_id, |
| + error)); |
| +} |
| + |
| +void GpuJpegDecodeAcceleratorHost::Send(IPC::Message* message) { |
| + LOG(ERROR) << __func__; |
| + DCHECK(CalledOnValidThread()); |
| + uint32 message_type = message->type(); |
| + if (!channel_->Send(message)) { |
| + DLOG(ERROR) << "Send(" << message_type << ") failed"; |
| + } |
| +} |
| + |
| +void GpuJpegDecodeAcceleratorHost::OnVideoFrameReady( |
| + int32_t bitstream_buffer_id) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + DCHECK(client_); |
| + client_->VideoFrameReady(bitstream_buffer_id); |
| +} |
| + |
| +void GpuJpegDecodeAcceleratorHost::OnNotifyError(int32_t bitstream_buffer_id, |
| + Error error) { |
| + DCHECK(CalledOnValidThread()); |
| + if (!client_) |
| + return; |
| + weak_this_factory_.InvalidateWeakPtrs(); |
| + |
| + // Client::NotifyError() may Destroy() |this|, so calling it needs to be the |
| + // last thing done on this stack! |
| + media::JpegDecodeAccelerator::Client* client = NULL; |
| + std::swap(client, client_); |
| + client->NotifyError(bitstream_buffer_id, |
| + static_cast<media::JpegDecodeAccelerator::Error>(error)); |
| +} |
| + |
| +} // namespace content |