Chromium Code Reviews| 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/synchronization/waitable_event.h" | |
| 10 #include "content/common/gpu/client/gpu_channel_host.h" | |
| 11 #include "content/common/gpu/gpu_messages.h" | |
| 12 #include "ipc/ipc_message_macros.h" | |
| 13 #include "ipc/ipc_message_utils.h" | |
| 14 | |
| 15 using media::JpegDecodeAccelerator; | |
| 16 namespace content { | |
| 17 | |
| 18 GpuJpegDecodeAcceleratorHost::GpuJpegDecodeAcceleratorHost( | |
| 19 GpuChannelHost* channel, | |
| 20 int32 route_id, | |
| 21 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
| 22 : channel_(channel), | |
| 23 client_(nullptr), | |
| 24 decoder_route_id_(route_id), | |
| 25 io_task_runner_(io_task_runner) { | |
| 26 DCHECK(channel_); | |
| 27 DCHECK_NE(decoder_route_id_, MSG_ROUTING_NONE); | |
| 28 } | |
| 29 | |
| 30 GpuJpegDecodeAcceleratorHost::~GpuJpegDecodeAcceleratorHost() { | |
| 31 DCHECK(CalledOnValidThread()); | |
| 32 Send(new AcceleratedJpegDecoderMsg_Destroy(decoder_route_id_)); | |
| 33 | |
| 34 channel_->RemoveRoute(decoder_route_id_); | |
| 35 | |
| 36 // No callback from RemoveRoute, so we just post a task to IO thread to make | |
| 37 // sure IO thread is done. | |
| 38 base::WaitableEvent event(false, false); | |
| 39 io_task_runner_->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal, | |
| 40 base::Unretained(&event))); | |
| 41 event.Wait(); | |
|
piman
2015/06/05 21:49:40
I don't think this is enough, you could still have
kcwu
2015/06/08 19:28:48
Done.
| |
| 42 } | |
| 43 | |
| 44 bool GpuJpegDecodeAcceleratorHost::OnMessageReceived(const IPC::Message& msg) { | |
| 45 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 46 | |
| 47 bool handled = true; | |
| 48 IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAcceleratorHost, msg) | |
| 49 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderHostMsg_DecodeAck, OnDecodeAck) | |
| 50 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 51 IPC_END_MESSAGE_MAP() | |
| 52 DCHECK(handled); | |
| 53 return handled; | |
| 54 } | |
| 55 | |
| 56 void GpuJpegDecodeAcceleratorHost::OnChannelError() { | |
| 57 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 58 | |
| 59 OnDecodeAck(kInvalidBitstreamBufferId, PLATFORM_FAILURE); | |
| 60 } | |
| 61 | |
| 62 bool GpuJpegDecodeAcceleratorHost::Initialize( | |
| 63 media::JpegDecodeAccelerator::Client* client) { | |
| 64 DCHECK(CalledOnValidThread()); | |
| 65 | |
| 66 bool succeeded = false; | |
| 67 // This cannot be on IO thread because the msg is synchronous. | |
| 68 Send(new GpuMsg_CreateJpegDecoder(decoder_route_id_, &succeeded)); | |
| 69 | |
| 70 if (!succeeded) { | |
| 71 DLOG(ERROR) << "Send(GpuMsg_CreateJpegDecoder()) failed"; | |
| 72 return false; | |
| 73 } | |
| 74 client_ = client; | |
| 75 | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 void GpuJpegDecodeAcceleratorHost::Decode( | |
| 80 const media::BitstreamBuffer& bitstream_buffer, | |
| 81 const scoped_refptr<media::VideoFrame>& video_frame) { | |
| 82 DCHECK(CalledOnValidThread()); | |
| 83 | |
| 84 DCHECK( | |
| 85 base::SharedMemory::IsHandleValid(video_frame->shared_memory_handle())); | |
| 86 | |
| 87 base::SharedMemoryHandle input_handle = | |
| 88 channel_->ShareToGpuProcess(bitstream_buffer.handle()); | |
| 89 if (!base::SharedMemory::IsHandleValid(input_handle)) { | |
| 90 DLOG(ERROR) << "Failed to duplicate handle of BitstreamBuffer"; | |
| 91 return; | |
| 92 } | |
| 93 base::SharedMemoryHandle output_handle = | |
| 94 channel_->ShareToGpuProcess(video_frame->shared_memory_handle()); | |
| 95 if (!base::SharedMemory::IsHandleValid(output_handle)) { | |
| 96 DLOG(ERROR) << "Failed to duplicate handle of VideoFrame"; | |
| 97 if (input_handle.auto_close) { | |
| 98 // Defer closing task to the ScopedFD. | |
| 99 base::ScopedFD(input_handle.fd); | |
| 100 } | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 size_t output_buffer_size = media::VideoFrame::AllocationSize( | |
| 105 video_frame->format(), video_frame->coded_size()); | |
| 106 | |
| 107 AcceleratedJpegDecoderMsg_Decode_Params decode_params; | |
| 108 decode_params.coded_size = video_frame->coded_size(); | |
| 109 decode_params.input_buffer_id = bitstream_buffer.id(); | |
| 110 decode_params.input_buffer_handle = input_handle; | |
| 111 decode_params.input_buffer_size = bitstream_buffer.size(); | |
| 112 decode_params.output_video_frame_handle = output_handle; | |
| 113 decode_params.output_buffer_size = output_buffer_size; | |
| 114 Send(new AcceleratedJpegDecoderMsg_Decode(decoder_route_id_, decode_params)); | |
| 115 } | |
| 116 | |
| 117 void GpuJpegDecodeAcceleratorHost::Send(IPC::Message* message) { | |
| 118 DCHECK(CalledOnValidThread()); | |
| 119 | |
| 120 if (!channel_->Send(message)) { | |
| 121 DLOG(ERROR) << "Send(" << message->type() << ") failed"; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void GpuJpegDecodeAcceleratorHost::OnDecodeAck(int32_t bitstream_buffer_id, | |
| 126 Error error) { | |
| 127 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 128 | |
| 129 if (!client_) | |
| 130 return; | |
| 131 | |
| 132 if (error == media::JpegDecodeAccelerator::NO_ERROR) { | |
| 133 client_->VideoFrameReady(bitstream_buffer_id); | |
| 134 } else { | |
| 135 // Only NotifyError once. | |
| 136 // Client::NotifyError() may delete |this|, so calling it needs to be the | |
| 137 // last thing done on this stack! | |
| 138 media::JpegDecodeAccelerator::Client* client = nullptr; | |
| 139 std::swap(client, client_); | |
| 140 client->NotifyError(bitstream_buffer_id, error); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 } // namespace content | |
| OLD | NEW |