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