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