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/media/gpu_jpeg_decode_accelerator.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/stl_util.h" |
| 15 #include "content/common/gpu/gpu_channel.h" |
| 16 #include "content/common/gpu/gpu_messages.h" |
| 17 #include "content/public/common/content_switches.h" |
| 18 #include "gpu/command_buffer/common/command_buffer.h" |
| 19 #include "ipc/ipc_message_macros.h" |
| 20 #include "ipc/ipc_message_utils.h" |
| 21 #include "ipc/message_filter.h" |
| 22 #include "media/base/limits.h" |
| 23 #include "media/filters/jpeg_parser.h" |
| 24 #include "ui/gfx/geometry/size.h" |
| 25 |
| 26 #if defined(OS_CHROMEOS) |
| 27 #if defined(ARCH_CPU_X86_FAMILY) |
| 28 #include "content/common/gpu/media/vaapi_jpeg_decode_accelerator.h" |
| 29 #endif // defined(ARCH_CPU_X86_FAMILY) |
| 30 #endif |
| 31 |
| 32 namespace base { |
| 33 |
| 34 void DefaultDeleter<content::GpuJpegDecodeAccelerator>::operator()( |
| 35 void* jpeg_decode_accelerator) const { |
| 36 static_cast<content::GpuJpegDecodeAccelerator*>(jpeg_decode_accelerator) |
| 37 ->Destroy(); |
| 38 } |
| 39 |
| 40 } // namespace base |
| 41 |
| 42 namespace content { |
| 43 |
| 44 class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { |
| 45 public: |
| 46 MessageFilter(GpuJpegDecodeAccelerator* owner, int32 host_route_id) |
| 47 : owner_(owner), host_route_id_(host_route_id) {} |
| 48 |
| 49 void OnChannelError() override { sender_ = NULL; } |
| 50 |
| 51 void OnChannelClosing() override { sender_ = NULL; } |
| 52 |
| 53 void OnFilterAdded(IPC::Sender* sender) override { sender_ = sender; } |
| 54 |
| 55 void OnFilterRemoved() override { |
| 56 // This will delete |owner_| and |this|. |
| 57 owner_->OnFilterRemoved(); |
| 58 } |
| 59 |
| 60 bool OnMessageReceived(const IPC::Message& msg) override { |
| 61 if (msg.routing_id() != host_route_id_) |
| 62 return false; |
| 63 |
| 64 IPC_BEGIN_MESSAGE_MAP(MessageFilter, msg) |
| 65 IPC_MESSAGE_FORWARD(AcceleratedJpegDecoderMsg_Decode, owner_, |
| 66 GpuJpegDecodeAccelerator::OnDecode) |
| 67 IPC_MESSAGE_UNHANDLED(return false;) |
| 68 IPC_END_MESSAGE_MAP() |
| 69 return true; |
| 70 } |
| 71 |
| 72 bool SendOnIOThread(IPC::Message* message) { |
| 73 DCHECK(!message->is_sync()); |
| 74 if (!sender_) { |
| 75 delete message; |
| 76 return false; |
| 77 } |
| 78 return sender_->Send(message); |
| 79 } |
| 80 |
| 81 protected: |
| 82 virtual ~MessageFilter() {} |
| 83 |
| 84 private: |
| 85 GpuJpegDecodeAccelerator* owner_; |
| 86 int32 host_route_id_; |
| 87 // The sender to which this filter was added. |
| 88 IPC::Sender* sender_; |
| 89 }; |
| 90 |
| 91 GpuJpegDecodeAccelerator::GpuJpegDecodeAccelerator( |
| 92 GpuChannel* channel, |
| 93 int32 host_route_id, |
| 94 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| 95 : channel_(channel), |
| 96 host_route_id_(host_route_id), |
| 97 filter_removed_(true, false), |
| 98 io_message_loop_(io_message_loop) { |
| 99 child_message_loop_ = base::MessageLoopProxy::current(); |
| 100 } |
| 101 |
| 102 GpuJpegDecodeAccelerator::~GpuJpegDecodeAccelerator() { |
| 103 } |
| 104 |
| 105 bool GpuJpegDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { |
| 106 bool handled = true; |
| 107 IPC_BEGIN_MESSAGE_MAP(GpuJpegDecodeAccelerator, msg) |
| 108 IPC_MESSAGE_HANDLER(AcceleratedJpegDecoderMsg_Destroy, OnDestroy) |
| 109 IPC_MESSAGE_UNHANDLED(handled = false) |
| 110 IPC_END_MESSAGE_MAP() |
| 111 return handled; |
| 112 } |
| 113 |
| 114 bool GpuJpegDecodeAccelerator::Initialize() { |
| 115 DVLOG(3) << __func__; |
| 116 DCHECK(child_message_loop_->BelongsToCurrentThread()); |
| 117 DCHECK(!jpeg_decode_accelerator_.get()); |
| 118 |
| 119 if (!channel_->AddRoute(host_route_id_, this)) { |
| 120 DLOG(ERROR) << "GpuJpegDecodeAccelerator::Initialize(): " |
| 121 "failed to add route"; |
| 122 return false; |
| 123 } |
| 124 |
| 125 filter_ = new MessageFilter(this, host_route_id_); |
| 126 channel_->AddFilter(filter_.get()); |
| 127 |
| 128 // When add more platforms, GpuJpegDecodeAcceleratorAdapter::Supported need |
| 129 // update as well. |
| 130 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
| 131 jpeg_decode_accelerator_.reset( |
| 132 new VaapiJpegDecodeAccelerator(io_message_loop_)); |
| 133 #else |
| 134 DVLOG(1) << "HW JPEG decode acceleration not available."; |
| 135 return false; |
| 136 #endif |
| 137 |
| 138 if (!jpeg_decode_accelerator_->Initialize(this)) { |
| 139 return false; |
| 140 } |
| 141 return true; |
| 142 } |
| 143 |
| 144 void GpuJpegDecodeAccelerator::NotifyError( |
| 145 int32_t buffer_id, |
| 146 media::JpegDecodeAccelerator::Error error) { |
| 147 DVLOG(3) << __func__; |
| 148 Send(new AcceleratedJpegDecoderHostMsg_NotifyError(host_route_id_, buffer_id, |
| 149 error)); |
| 150 } |
| 151 |
| 152 void GpuJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { |
| 153 // This is called from JDA's decode thread. |
| 154 DVLOG(3) << __func__; |
| 155 Send(new AcceleratedJpegDecoderHostMsg_VideoFrameReady(host_route_id_, |
| 156 bitstream_buffer_id)); |
| 157 } |
| 158 |
| 159 void DecodeFinished(scoped_ptr<base::SharedMemory> shm) { |
| 160 // Do nothing. Because VideoFrame is backed by |shm|, the purpose of this |
| 161 // function is to just keep reference of |shm| to make sure it live util |
| 162 // decode finished. |
| 163 DVLOG(3) << __func__; |
| 164 } |
| 165 |
| 166 void GpuJpegDecodeAccelerator::OnDecode( |
| 167 const AcceleratedJpegDecoderMsg_Decode_Params& params) { |
| 168 DVLOG(3) << __func__; |
| 169 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 170 DCHECK(jpeg_decode_accelerator_.get()); |
| 171 |
| 172 if (params.input_buffer_id < 0) { |
| 173 LOG(ERROR) << "BitstreamBuffer id " << params.input_buffer_id |
| 174 << " out of range"; |
| 175 NotifyError(params.input_buffer_id, |
| 176 media::JpegDecodeAccelerator::INVALID_ARGUMENT); |
| 177 return; |
| 178 } |
| 179 |
| 180 media::BitstreamBuffer input_buffer(params.input_buffer_id, |
| 181 params.input_buffer_handle, |
| 182 params.input_buffer_size); |
| 183 |
| 184 scoped_ptr<base::SharedMemory> output_shm( |
| 185 new base::SharedMemory(params.output_video_frame_handle, false)); |
| 186 if (!output_shm->Map(params.output_buffer_size)) { |
| 187 LOG(ERROR) << "Could not map output shared memory for input buffer id " |
| 188 << params.input_buffer_id; |
| 189 NotifyError(params.input_buffer_id, |
| 190 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 191 return; |
| 192 } |
| 193 |
| 194 uint8* shm_memory = reinterpret_cast<uint8*>(output_shm->memory()); |
| 195 scoped_refptr<media::VideoFrame> frame = |
| 196 media::VideoFrame::WrapExternalPackedMemory( |
| 197 media::VideoFrame::I420, |
| 198 params.coded_size, |
| 199 gfx::Rect(params.coded_size), |
| 200 params.coded_size, |
| 201 shm_memory, |
| 202 params.output_buffer_size, |
| 203 params.output_video_frame_handle, |
| 204 0, |
| 205 base::TimeDelta(), |
| 206 base::Bind(DecodeFinished, base::Passed(&output_shm))); |
| 207 |
| 208 if (!frame.get()) { |
| 209 LOG(ERROR) << "Could not create VideoFrame for input buffer id " |
| 210 << params.input_buffer_id; |
| 211 NotifyError(params.input_buffer_id, |
| 212 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 213 return; |
| 214 } |
| 215 |
| 216 jpeg_decode_accelerator_->Decode(input_buffer, frame); |
| 217 } |
| 218 |
| 219 void GpuJpegDecodeAccelerator::OnDestroy() { |
| 220 DVLOG(3) << __func__; |
| 221 DCHECK(child_message_loop_->BelongsToCurrentThread()); |
| 222 DCHECK(jpeg_decode_accelerator_.get()); |
| 223 Destroy(); |
| 224 } |
| 225 |
| 226 void GpuJpegDecodeAccelerator::OnFilterRemoved() { |
| 227 // We're destroying; cancel all callbacks. |
| 228 filter_removed_.Signal(); |
| 229 } |
| 230 |
| 231 void GpuJpegDecodeAccelerator::Destroy() { |
| 232 // We cannot destroy the JDA before the IO thread message filter is |
| 233 // removed however, since we cannot service incoming messages with JDA gone. |
| 234 // We cannot simply check for existence of JDA on IO thread though, because |
| 235 // we don't want to synchronize the IO thread with the ChildThread. |
| 236 // So we have to wait for the RemoveFilter callback here instead and remove |
| 237 // the JDA after it arrives and before returning. |
| 238 if (filter_.get()) { |
| 239 channel_->RemoveFilter(filter_.get()); |
| 240 filter_removed_.Wait(); |
| 241 } |
| 242 |
| 243 channel_->RemoveRoute(host_route_id_); |
| 244 channel_->ReleaseJpegDecoder(); |
| 245 jpeg_decode_accelerator_.reset(); |
| 246 |
| 247 delete this; |
| 248 } |
| 249 |
| 250 bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { |
| 251 if (io_message_loop_->BelongsToCurrentThread()) |
| 252 return filter_->SendOnIOThread(message); |
| 253 return channel_->Send(message); |
| 254 } |
| 255 |
| 256 } // namespace content |
OLD | NEW |