| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/gpu/ipc/client/gpu_jpeg_decode_accelerator_host.h" | 5 #include "media/gpu/ipc/client/gpu_jpeg_decode_accelerator_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/shared_memory_handle.h" | 12 #include "base/memory/shared_memory_handle.h" |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/synchronization/waitable_event.h" | |
| 15 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 16 #include "gpu/ipc/client/gpu_channel_host.h" | 14 #include "mojo/public/cpp/system/platform_handle.h" |
| 17 #include "ipc/ipc_listener.h" | |
| 18 #include "ipc/ipc_message_macros.h" | |
| 19 #include "ipc/ipc_message_utils.h" | |
| 20 #include "media/gpu/ipc/common/media_messages.h" | |
| 21 | 15 |
| 22 namespace media { | 16 namespace media { |
| 23 | 17 |
| 24 // Class to receive AcceleratedJpegDecoderHostMsg_DecodeAck IPC message on IO | |
| 25 // thread. This does very similar what MessageFilter usually does. It is not | |
| 26 // MessageFilter because GpuChannelHost doesn't support AddFilter. | |
| 27 class GpuJpegDecodeAcceleratorHost::Receiver : public IPC::Listener { | |
| 28 public: | |
| 29 Receiver(Client* client, | |
| 30 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
| 31 : client_(client), | |
| 32 io_task_runner_(io_task_runner), | |
| 33 weak_factory_for_io_( | |
| 34 base::MakeUnique<base::WeakPtrFactory<Receiver>>(this)), | |
| 35 weak_ptr_for_io_(weak_factory_for_io_->GetWeakPtr()) { | |
| 36 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 37 } | |
| 38 | |
| 39 ~Receiver() override { | |
| 40 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 41 // If |io_task_runner_| no longer accepts tasks, |weak_factory_for_io_| | |
| 42 // will leak. This is acceptable, because that should only happen on | |
| 43 // Browser shutdown. | |
| 44 io_task_runner_->DeleteSoon(FROM_HERE, weak_factory_for_io_.release()); | |
| 45 } | |
| 46 | |
| 47 void InvalidateWeakPtrOnIOThread(base::WaitableEvent* event) { | |
| 48 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 49 weak_factory_for_io_->InvalidateWeakPtrs(); | |
| 50 event->Signal(); | |
| 51 } | |
| 52 | |
| 53 // IPC::Listener implementation. | |
| 54 void OnChannelError() override { | |
| 55 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 56 | |
| 57 OnDecodeAck(kInvalidBitstreamBufferId, PLATFORM_FAILURE); | |
| 58 } | |
| 59 | |
| 60 bool OnMessageReceived(const IPC::Message& msg) override { | |
| 61 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 62 | |
| 63 bool handled = true; | |
| 64 IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAcceleratorHost::Receiver, msg) | |
| 65 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderHostMsg_DecodeAck, OnDecodeAck) | |
| 66 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 67 IPC_END_MESSAGE_MAP() | |
| 68 DCHECK(handled); | |
| 69 return handled; | |
| 70 } | |
| 71 | |
| 72 base::WeakPtr<IPC::Listener> AsWeakPtrForIO() { return weak_ptr_for_io_; } | |
| 73 | |
| 74 private: | |
| 75 void OnDecodeAck(int32_t bitstream_buffer_id, Error error) { | |
| 76 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 77 | |
| 78 if (!client_) | |
| 79 return; | |
| 80 | |
| 81 if (error == JpegDecodeAccelerator::NO_ERRORS) { | |
| 82 client_->VideoFrameReady(bitstream_buffer_id); | |
| 83 } else { | |
| 84 // Only NotifyError once. | |
| 85 // Client::NotifyError() may trigger deletion of |this| (on another | |
| 86 // thread), so calling it needs to be the last thing done on this stack! | |
| 87 JpegDecodeAccelerator::Client* client = nullptr; | |
| 88 std::swap(client, client_); | |
| 89 client->NotifyError(bitstream_buffer_id, error); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 Client* client_; | |
| 94 | |
| 95 // GPU IO task runner. | |
| 96 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 97 | |
| 98 SEQUENCE_CHECKER(sequence_checker_); | |
| 99 | |
| 100 // Weak pointers will be invalidated on IO thread. | |
| 101 std::unique_ptr<base::WeakPtrFactory<Receiver>> weak_factory_for_io_; | |
| 102 base::WeakPtr<Receiver> weak_ptr_for_io_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(Receiver); | |
| 105 }; | |
| 106 | |
| 107 GpuJpegDecodeAcceleratorHost::GpuJpegDecodeAcceleratorHost( | 18 GpuJpegDecodeAcceleratorHost::GpuJpegDecodeAcceleratorHost( |
| 108 scoped_refptr<gpu::GpuChannelHost> channel, | 19 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 109 int32_t route_id, | 20 mojom::GpuJpegDecodeAcceleratorPtrInfo jpeg_decoder_info) |
| 110 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 21 : io_task_runner_(io_task_runner) { |
| 111 : channel_(std::move(channel)), | 22 jpeg_decoder_.Bind(std::move(jpeg_decoder_info)); |
| 112 decoder_route_id_(route_id), | |
| 113 io_task_runner_(io_task_runner) { | |
| 114 DCHECK(channel_); | |
| 115 DCHECK_NE(decoder_route_id_, MSG_ROUTING_NONE); | |
| 116 } | 23 } |
| 117 | 24 |
| 118 GpuJpegDecodeAcceleratorHost::~GpuJpegDecodeAcceleratorHost() { | 25 GpuJpegDecodeAcceleratorHost::~GpuJpegDecodeAcceleratorHost() { |
| 119 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 26 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 120 Send(new AcceleratedJpegDecoderMsg_Destroy(decoder_route_id_)); | |
| 121 | |
| 122 if (receiver_) { | |
| 123 channel_->RemoveRoute(decoder_route_id_); | |
| 124 | |
| 125 // Invalidate weak ptr of |receiver_|. After that, no more messages will be | |
| 126 // routed to |receiver_| on IO thread. | |
| 127 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 128 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 129 // Use of Unretained() is safe, because if the task executes, we block | |
| 130 // until it is finished by waiting on |event| below. | |
| 131 bool task_expected_to_run = io_task_runner_->PostTask( | |
| 132 FROM_HERE, base::Bind(&Receiver::InvalidateWeakPtrOnIOThread, | |
| 133 base::Unretained(receiver_.get()), | |
| 134 base::Unretained(&event))); | |
| 135 // If the current call is happening during the browser shutdown, the | |
| 136 // |io_task_runner_| may no longer be accepting tasks. | |
| 137 if (task_expected_to_run) | |
| 138 event.Wait(); | |
| 139 } | |
| 140 } | 27 } |
| 141 | 28 |
| 142 bool GpuJpegDecodeAcceleratorHost::Initialize( | 29 bool GpuJpegDecodeAcceleratorHost::Initialize( |
| 143 JpegDecodeAccelerator::Client* client) { | 30 JpegDecodeAccelerator::Client* client) { |
| 144 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 31 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 145 | 32 |
| 146 bool succeeded = false; | 33 bool succeeded = false; |
| 147 // This cannot be on IO thread because the msg is synchronous. | 34 jpeg_decoder_->Initialize(&succeeded); |
| 148 Send(new GpuChannelMsg_CreateJpegDecoder(decoder_route_id_, &succeeded)); | |
| 149 | 35 |
| 150 if (!succeeded) { | 36 if (succeeded) |
| 151 DLOG(ERROR) << "Send(GpuChannelMsg_CreateJpegDecoder()) failed"; | 37 client_ = client; |
| 152 return false; | 38 |
| 39 return succeeded; |
| 40 } |
| 41 |
| 42 void GpuJpegDecodeAcceleratorHost::OnDecodeAckOnIOThread( |
| 43 int32_t bitstream_buffer_id, |
| 44 mojom::Error error) { |
| 45 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 46 |
| 47 if (!client_) |
| 48 return; |
| 49 |
| 50 if (error == mojom::Error::NO_ERRORS) { |
| 51 client_->VideoFrameReady(bitstream_buffer_id); |
| 52 } else { |
| 53 // Only NotifyError once. |
| 54 // Client::NotifyError() may trigger deletion of |this| (on another |
| 55 // thread), so calling it needs to be the last thing done on this stack! |
| 56 JpegDecodeAccelerator::Client* client = nullptr; |
| 57 std::swap(client, client_); |
| 58 client->NotifyError(bitstream_buffer_id, |
| 59 static_cast<JpegDecodeAccelerator::Error>(error)); |
| 153 } | 60 } |
| 61 } |
| 154 | 62 |
| 155 receiver_.reset(new Receiver(client, io_task_runner_)); | 63 void GpuJpegDecodeAcceleratorHost::OnDecodeAck(int32_t bitstream_buffer_id, |
| 64 mojom::Error error) { |
| 65 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 156 | 66 |
| 157 return true; | 67 io_task_runner_->PostTask( |
| 68 FROM_HERE, |
| 69 base::Bind(&GpuJpegDecodeAcceleratorHost::OnDecodeAckOnIOThread, |
| 70 base::Unretained(this), bitstream_buffer_id, error)); |
| 158 } | 71 } |
| 159 | 72 |
| 160 void GpuJpegDecodeAcceleratorHost::Decode( | 73 void GpuJpegDecodeAcceleratorHost::Decode( |
| 161 const BitstreamBuffer& bitstream_buffer, | 74 const BitstreamBuffer& bitstream_buffer, |
| 162 const scoped_refptr<VideoFrame>& video_frame) { | 75 const scoped_refptr<VideoFrame>& video_frame) { |
| 163 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 76 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 164 | 77 |
| 165 DCHECK( | 78 DCHECK( |
| 166 base::SharedMemory::IsHandleValid(video_frame->shared_memory_handle())); | 79 base::SharedMemory::IsHandleValid(video_frame->shared_memory_handle())); |
| 167 | 80 |
| 168 AcceleratedJpegDecoderMsg_Decode_Params decode_params; | |
| 169 decode_params.input_buffer = bitstream_buffer; | |
| 170 base::SharedMemoryHandle input_handle = | 81 base::SharedMemoryHandle input_handle = |
| 171 channel_->ShareToGpuProcess(bitstream_buffer.handle()); | 82 base::SharedMemory::DuplicateHandle(bitstream_buffer.handle()); |
| 172 if (!base::SharedMemory::IsHandleValid(input_handle)) { | 83 if (!base::SharedMemory::IsHandleValid(input_handle)) { |
| 173 DLOG(ERROR) << "Failed to duplicate handle of BitstreamBuffer"; | 84 DLOG(ERROR) << "Failed to duplicate handle of BitstreamBuffer"; |
| 174 return; | 85 return; |
| 175 } | 86 } |
| 176 decode_params.input_buffer.set_handle(input_handle); | 87 |
| 177 base::SharedMemoryHandle output_handle = | 88 base::SharedMemoryHandle output_handle = |
| 178 channel_->ShareToGpuProcess(video_frame->shared_memory_handle()); | 89 base::SharedMemory::DuplicateHandle(video_frame->shared_memory_handle()); |
| 179 if (!base::SharedMemory::IsHandleValid(output_handle)) { | 90 if (!base::SharedMemory::IsHandleValid(output_handle)) { |
| 180 DLOG(ERROR) << "Failed to duplicate handle of VideoFrame"; | 91 DLOG(ERROR) << "Failed to duplicate handle of VideoFrame"; |
| 181 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 92 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 182 if (input_handle.OwnershipPassesToIPC()) { | 93 if (input_handle.OwnershipPassesToIPC()) { |
| 183 input_handle.Close(); | 94 input_handle.Close(); |
| 184 } | 95 } |
| 185 #else | 96 #else |
| 186 // TODO(kcwu) fix the handle leak after crbug.com/493414 resolved. | 97 // TODO(kcwu) fix the handle leak after crbug.com/493414 resolved. |
| 187 #endif | 98 #endif |
| 188 return; | 99 return; |
| 189 } | 100 } |
| 190 | 101 |
| 102 mojo::ScopedSharedBufferHandle input_buffer_handle = |
| 103 mojo::WrapSharedMemoryHandle(input_handle, bitstream_buffer.size(), |
| 104 true /* read_only */); |
| 105 mojom::BitstreamBufferPtr buffer = mojom::BitstreamBuffer::New(); |
| 106 buffer->id = bitstream_buffer.id(); |
| 107 buffer->memory_handle = std::move(input_buffer_handle); |
| 108 buffer->size = bitstream_buffer.size(); |
| 109 buffer->offset = bitstream_buffer.offset(); |
| 110 buffer->timestamp = bitstream_buffer.presentation_timestamp(); |
| 111 buffer->key_id = bitstream_buffer.key_id(); |
| 112 buffer->iv = bitstream_buffer.iv(); |
| 113 buffer->subsamples = bitstream_buffer.subsamples(); |
| 114 |
| 191 size_t output_buffer_size = VideoFrame::AllocationSize( | 115 size_t output_buffer_size = VideoFrame::AllocationSize( |
| 192 video_frame->format(), video_frame->coded_size()); | 116 video_frame->format(), video_frame->coded_size()); |
| 117 mojo::ScopedSharedBufferHandle output_frame_handle = |
| 118 mojo::WrapSharedMemoryHandle(output_handle, output_buffer_size, |
| 119 false /* read_only */); |
| 193 | 120 |
| 194 decode_params.coded_size = video_frame->coded_size(); | 121 jpeg_decoder_->Decode(std::move(buffer), video_frame->coded_size(), |
| 195 decode_params.output_video_frame_handle = output_handle; | 122 std::move(output_frame_handle), |
| 196 decode_params.output_buffer_size = | 123 base::checked_cast<uint32_t>(output_buffer_size), |
| 197 base::checked_cast<uint32_t>(output_buffer_size); | 124 base::Bind(&GpuJpegDecodeAcceleratorHost::OnDecodeAck, |
| 198 Send(new AcceleratedJpegDecoderMsg_Decode(decoder_route_id_, decode_params)); | 125 base::Unretained(this))); |
| 199 } | 126 } |
| 200 | 127 |
| 201 bool GpuJpegDecodeAcceleratorHost::IsSupported() { | 128 bool GpuJpegDecodeAcceleratorHost::IsSupported() { |
| 202 return channel_->gpu_info().jpeg_decode_accelerator_supported; | 129 return true; |
| 203 } | |
| 204 | |
| 205 void GpuJpegDecodeAcceleratorHost::Send(IPC::Message* message) { | |
| 206 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 207 | |
| 208 if (!channel_->Send(message)) { | |
| 209 DLOG(ERROR) << "Send(" << message->type() << ") failed"; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 base::WeakPtr<IPC::Listener> GpuJpegDecodeAcceleratorHost::GetReceiver() { | |
| 214 return receiver_->AsWeakPtrForIO(); | |
| 215 } | 130 } |
| 216 | 131 |
| 217 } // namespace media | 132 } // namespace media |
| OLD | NEW |