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/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 : channel_(channel), | |
| 32 client_(NULL), | |
| 33 decoder_route_id_(MSG_ROUTING_NONE), | |
| 34 weak_this_factory_(this) { | |
| 35 DCHECK(channel_); | |
| 36 } | |
| 37 | |
| 38 GpuJpegDecodeAcceleratorHost::~GpuJpegDecodeAcceleratorHost() { | |
| 39 DCHECK(CalledOnValidThread()); | |
| 40 | |
| 41 if (channel_ && decoder_route_id_ != MSG_ROUTING_NONE) | |
| 42 channel_->RemoveRoute(decoder_route_id_); | |
| 43 } | |
| 44 | |
| 45 bool GpuJpegDecodeAcceleratorHost::OnMessageReceived(const IPC::Message& msg) { | |
| 46 LOG(ERROR) << __func__; | |
| 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 DCHECK(CalledOnValidThread()); | |
| 64 if (channel_) { | |
| 65 if (decoder_route_id_ != MSG_ROUTING_NONE) | |
| 66 channel_->RemoveRoute(decoder_route_id_); | |
| 67 channel_ = NULL; | |
| 68 } | |
| 69 DLOG(ERROR) << "OnChannelError()"; | |
| 70 PostNotifyError(kInvalidBitstreamBufferId, PLATFORM_FAILURE); | |
| 71 } | |
| 72 | |
| 73 bool GpuJpegDecodeAcceleratorHost::Initialize( | |
| 74 media::JpegDecodeAccelerator::Client* client) { | |
| 75 DCHECK(CalledOnValidThread()); | |
| 76 | |
| 77 int32 route_id = channel_->GenerateRouteID(); | |
| 78 | |
| 79 // XXX(kcwu): how to fix this hack? | |
| 80 BrowserThread::PostTask( | |
|
wuchengli
2015/03/23 06:30:14
Looks like GJDAH is accessed entirely on IO thread
kcwu
2015/03/30 18:12:14
I modified it to AddRoute from caller instead of h
| |
| 81 BrowserThread::IO, | |
| 82 FROM_HERE, | |
| 83 base::Bind( | |
| 84 &GpuChannelHost::AddRoute, | |
| 85 channel_, | |
| 86 route_id, | |
| 87 weak_this_factory_.GetWeakPtr())); | |
| 88 | |
| 89 bool succeeded = false; | |
| 90 Send(new GpuMsg_CreateJpegDecoder(route_id, &succeeded)); | |
| 91 | |
| 92 if (!succeeded) { | |
| 93 DLOG(ERROR) << "Send(GpuMsg_CreateJpegDecoder()) failed"; | |
| 94 channel_->RemoveRoute(route_id); | |
| 95 return false; | |
| 96 } | |
| 97 decoder_route_id_ = route_id; | |
| 98 client_ = client; | |
| 99 | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 void GpuJpegDecodeAcceleratorHost::Decode( | |
| 104 const media::BitstreamBuffer& bitstream_buffer, | |
| 105 const scoped_refptr<media::VideoFrame>& video_frame) { | |
| 106 DCHECK(CalledOnValidThread()); | |
| 107 if (!channel_) | |
| 108 return; | |
| 109 | |
| 110 base::SharedMemoryHandle input_handle = channel_->ShareToGpuProcess( | |
| 111 bitstream_buffer.handle()); | |
| 112 if (!base::SharedMemory::IsHandleValid(input_handle)) { | |
| 113 DLOG(ERROR) << "Failed to duplicate buffer handler of BitstreamBuffer"; | |
| 114 PostNotifyError(bitstream_buffer.id(), INVALID_ARGUMENT); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 if (!base::SharedMemory::IsHandleValid(video_frame->shared_memory_handle())) { | |
| 119 DLOG(ERROR) | |
| 120 << "Decode(): cannot output to frame not backed by shared memory"; | |
| 121 PostNotifyError(bitstream_buffer.id(), PLATFORM_FAILURE); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 base::SharedMemoryHandle output_handle = channel_->ShareToGpuProcess( | |
| 126 video_frame->shared_memory_handle()); | |
| 127 if (!base::SharedMemory::IsHandleValid(output_handle)) { | |
| 128 DLOG(ERROR) << "Decode(): failed to duplicate buffer handle of VideoFrame"; | |
| 129 PostNotifyError(bitstream_buffer.id(), PLATFORM_FAILURE); | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 // TODO(kcwu) not sure about the alignment | |
| 134 size_t output_buffer_size = media::VideoFrame::AllocationSize( | |
| 135 video_frame->format(), video_frame->coded_size()); | |
| 136 | |
| 137 AcceleratedJpegDecoderMsg_Decode_Params decode_params; | |
| 138 decode_params.coded_size = video_frame->coded_size(); | |
| 139 decode_params.input_buffer_id = bitstream_buffer.id(); | |
| 140 decode_params.input_buffer_handle = input_handle; | |
| 141 decode_params.input_buffer_size = bitstream_buffer.size(); | |
| 142 decode_params.output_video_frame_handle = output_handle; | |
| 143 decode_params.output_buffer_size = output_buffer_size; | |
| 144 Send(new AcceleratedJpegDecoderMsg_Decode(decoder_route_id_, decode_params)); | |
| 145 } | |
| 146 | |
| 147 void GpuJpegDecodeAcceleratorHost::Destroy() { | |
| 148 DCHECK(CalledOnValidThread()); | |
| 149 #if 1 | |
| 150 if (channel_) | |
| 151 Send(new AcceleratedJpegDecoderMsg_Destroy(decoder_route_id_)); | |
| 152 #endif | |
| 153 delete this; | |
| 154 } | |
| 155 | |
| 156 // TODO do we need this?? | |
| 157 #if 0 | |
| 158 void GpuJpegDecodeAcceleratorHost::OnWillDeleteImpl() { | |
| 159 DCHECK(CalledOnValidThread()); | |
| 160 | |
| 161 OnChannelError(); | |
| 162 } | |
| 163 #endif | |
| 164 | |
| 165 void GpuJpegDecodeAcceleratorHost::PostNotifyError( | |
| 166 int32_t bitstream_buffer_id, Error error) { | |
| 167 DCHECK(CalledOnValidThread()); | |
| 168 DVLOG(2) << "PostNotifyDecodeResult(): error=" << error; | |
| 169 // Post the error notification back to this thread, to avoid re-entrancy. | |
| 170 base::MessageLoopProxy::current()->PostTask( | |
| 171 FROM_HERE, | |
| 172 base::Bind(&GpuJpegDecodeAcceleratorHost::OnNotifyError, | |
| 173 weak_this_factory_.GetWeakPtr(), | |
| 174 bitstream_buffer_id, | |
| 175 error)); | |
| 176 } | |
| 177 | |
| 178 void GpuJpegDecodeAcceleratorHost::Send(IPC::Message* message) { | |
| 179 LOG(ERROR) << __func__; | |
| 180 DCHECK(CalledOnValidThread()); | |
| 181 uint32 message_type = message->type(); | |
| 182 if (!channel_->Send(message)) { | |
| 183 DLOG(ERROR) << "Send(" << message_type << ") failed"; | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void GpuJpegDecodeAcceleratorHost::OnVideoFrameReady( | |
| 188 int32_t bitstream_buffer_id) { | |
| 189 DCHECK(CalledOnValidThread()); | |
| 190 | |
| 191 DCHECK(client_); | |
| 192 client_->VideoFrameReady(bitstream_buffer_id); | |
| 193 } | |
| 194 | |
| 195 void GpuJpegDecodeAcceleratorHost::OnNotifyError(int32_t bitstream_buffer_id, | |
| 196 Error error) { | |
| 197 DCHECK(CalledOnValidThread()); | |
| 198 if (!client_) | |
| 199 return; | |
| 200 weak_this_factory_.InvalidateWeakPtrs(); | |
| 201 | |
| 202 // Client::NotifyError() may Destroy() |this|, so calling it needs to be the | |
| 203 // last thing done on this stack! | |
| 204 media::JpegDecodeAccelerator::Client* client = NULL; | |
| 205 std::swap(client, client_); | |
| 206 client->NotifyError(bitstream_buffer_id, | |
| 207 static_cast<media::JpegDecodeAccelerator::Error>(error)); | |
| 208 } | |
| 209 | |
| 210 } // namespace content | |
| OLD | NEW |