OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/common/gpu/client/gpu_jpeg_decode_accelerator_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "content/common/gpu/client/gpu_channel_host.h" |
| 12 #include "content/common/gpu/gpu_messages.h" |
| 13 #include "ipc/ipc_message_macros.h" |
| 14 #include "ipc/ipc_message_utils.h" |
| 15 |
| 16 using media::JpegDecodeAccelerator; |
| 17 namespace content { |
| 18 |
| 19 GpuJpegDecodeAcceleratorHost::GpuJpegDecodeAcceleratorHost( |
| 20 GpuChannelHost* channel, |
| 21 int32 route_id, |
| 22 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| 23 : channel_(channel), |
| 24 client_(NULL), |
| 25 decoder_route_id_(route_id), |
| 26 io_message_loop_(io_message_loop), |
| 27 weak_this_factory_(this) { |
| 28 DCHECK(channel_); |
| 29 } |
| 30 |
| 31 GpuJpegDecodeAcceleratorHost::~GpuJpegDecodeAcceleratorHost() { |
| 32 DCHECK(CalledOnValidThread()); |
| 33 |
| 34 if (channel_ && decoder_route_id_ != MSG_ROUTING_NONE) |
| 35 channel_->RemoveRoute(decoder_route_id_); |
| 36 } |
| 37 |
| 38 bool GpuJpegDecodeAcceleratorHost::OnMessageReceived(const IPC::Message& msg) { |
| 39 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 40 bool handled = true; |
| 41 IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAcceleratorHost, msg) |
| 42 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderHostMsg_VideoFrameReady, |
| 43 OnVideoFrameReady) |
| 44 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderHostMsg_NotifyError, |
| 45 OnNotifyError) |
| 46 IPC_MESSAGE_UNHANDLED(handled = false) |
| 47 IPC_END_MESSAGE_MAP() |
| 48 DCHECK(handled); |
| 49 // See OnNotifyError for why |this| mustn't be used after OnNotifyError might |
| 50 // have been called above. |
| 51 return handled; |
| 52 } |
| 53 |
| 54 void GpuJpegDecodeAcceleratorHost::OnChannelError() { |
| 55 DVLOG(3) << __func__; |
| 56 DCHECK(CalledOnValidThread()); |
| 57 if (channel_) { |
| 58 if (decoder_route_id_ != MSG_ROUTING_NONE) |
| 59 channel_->RemoveRoute(decoder_route_id_); |
| 60 channel_ = NULL; |
| 61 } |
| 62 PostNotifyError(kInvalidBitstreamBufferId, PLATFORM_FAILURE); |
| 63 } |
| 64 |
| 65 bool GpuJpegDecodeAcceleratorHost::Initialize( |
| 66 media::JpegDecodeAccelerator::Client* client) { |
| 67 DCHECK(CalledOnValidThread()); |
| 68 |
| 69 bool succeeded = false; |
| 70 // This cannot be on IO thread because the msg is synchronous. |
| 71 Send(new GpuMsg_CreateJpegDecoder(decoder_route_id_, &succeeded)); |
| 72 |
| 73 if (!succeeded) { |
| 74 DLOG(ERROR) << "Send(GpuMsg_CreateJpegDecoder()) failed"; |
| 75 channel_->RemoveRoute(decoder_route_id_); |
| 76 return false; |
| 77 } |
| 78 client_ = client; |
| 79 |
| 80 return true; |
| 81 } |
| 82 |
| 83 void GpuJpegDecodeAcceleratorHost::Decode( |
| 84 const media::BitstreamBuffer& bitstream_buffer, |
| 85 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 if (!channel_) |
| 88 return; |
| 89 |
| 90 base::SharedMemoryHandle input_handle = |
| 91 channel_->ShareToGpuProcess(bitstream_buffer.handle()); |
| 92 if (!base::SharedMemory::IsHandleValid(input_handle)) { |
| 93 LOG(ERROR) << "Failed to duplicate buffer handle of BitstreamBuffer"; |
| 94 PostNotifyError(bitstream_buffer.id(), INVALID_ARGUMENT); |
| 95 return; |
| 96 } |
| 97 |
| 98 if (!base::SharedMemory::IsHandleValid(video_frame->shared_memory_handle())) { |
| 99 LOG(ERROR) |
| 100 << "Decode(): cannot output to frame not backed by shared memory"; |
| 101 PostNotifyError(bitstream_buffer.id(), INVALID_ARGUMENT); |
| 102 return; |
| 103 } |
| 104 |
| 105 base::SharedMemoryHandle output_handle = |
| 106 channel_->ShareToGpuProcess(video_frame->shared_memory_handle()); |
| 107 if (!base::SharedMemory::IsHandleValid(output_handle)) { |
| 108 LOG(ERROR) << "Decode(): failed to duplicate buffer handle of VideoFrame"; |
| 109 PostNotifyError(bitstream_buffer.id(), PLATFORM_FAILURE); |
| 110 return; |
| 111 } |
| 112 |
| 113 size_t output_buffer_size = media::VideoFrame::AllocationSize( |
| 114 video_frame->format(), video_frame->coded_size()); |
| 115 |
| 116 AcceleratedJpegDecoderMsg_Decode_Params decode_params; |
| 117 decode_params.coded_size = video_frame->coded_size(); |
| 118 decode_params.input_buffer_id = bitstream_buffer.id(); |
| 119 decode_params.input_buffer_handle = input_handle; |
| 120 decode_params.input_buffer_size = bitstream_buffer.size(); |
| 121 decode_params.output_video_frame_handle = output_handle; |
| 122 decode_params.output_buffer_size = output_buffer_size; |
| 123 Send(new AcceleratedJpegDecoderMsg_Decode(decoder_route_id_, decode_params)); |
| 124 } |
| 125 |
| 126 void GpuJpegDecodeAcceleratorHost::Destroy() { |
| 127 DCHECK(CalledOnValidThread()); |
| 128 if (channel_) |
| 129 Send(new AcceleratedJpegDecoderMsg_Destroy(decoder_route_id_)); |
| 130 client_ = NULL; |
| 131 delete this; |
| 132 } |
| 133 |
| 134 void GpuJpegDecodeAcceleratorHost::PostNotifyError(int32_t bitstream_buffer_id, |
| 135 Error error) { |
| 136 DVLOG(2) << __func__ << ": error=" << error |
| 137 << ", bitstream_buffer_id=" << bitstream_buffer_id; |
| 138 DCHECK(CalledOnValidThread()); |
| 139 // Post the error notification back to this thread, to avoid re-entrancy. |
| 140 base::MessageLoopProxy::current()->PostTask( |
| 141 FROM_HERE, |
| 142 base::Bind(&GpuJpegDecodeAcceleratorHost::OnNotifyError, |
| 143 weak_this_factory_.GetWeakPtr(), bitstream_buffer_id, error)); |
| 144 } |
| 145 |
| 146 void GpuJpegDecodeAcceleratorHost::Send(IPC::Message* message) { |
| 147 DVLOG(3) << __func__; |
| 148 DCHECK(CalledOnValidThread()); |
| 149 uint32 message_type = message->type(); |
| 150 if (!channel_->Send(message)) { |
| 151 DLOG(ERROR) << "Send(" << message_type << ") failed"; |
| 152 } |
| 153 } |
| 154 |
| 155 void GpuJpegDecodeAcceleratorHost::OnVideoFrameReady( |
| 156 int32_t bitstream_buffer_id) { |
| 157 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 158 |
| 159 DCHECK(client_); |
| 160 client_->VideoFrameReady(bitstream_buffer_id); |
| 161 } |
| 162 |
| 163 void GpuJpegDecodeAcceleratorHost::OnNotifyError(int32_t bitstream_buffer_id, |
| 164 Error error) { |
| 165 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 166 if (!client_) |
| 167 return; |
| 168 weak_this_factory_.InvalidateWeakPtrs(); |
| 169 |
| 170 // Client::NotifyError() may Destroy() |this|, so calling it needs to be the |
| 171 // last thing done on this stack! |
| 172 media::JpegDecodeAccelerator::Client* client = NULL; |
| 173 std::swap(client, client_); |
| 174 client->NotifyError(bitstream_buffer_id, |
| 175 static_cast<media::JpegDecodeAccelerator::Error>(error)); |
| 176 } |
| 177 |
| 178 } // namespace content |
OLD | NEW |