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/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 | |
|
wuchengli
2015/04/15 07:11:58
add a blank line
kcwu
2015/04/16 14:38:28
Done.
| |
| 41 namespace content { | |
| 42 | |
| 43 class GpuJpegDecodeAccelerator::MessageFilter : public IPC::MessageFilter { | |
| 44 public: | |
| 45 MessageFilter(GpuJpegDecodeAccelerator* owner, int32 host_route_id) | |
| 46 : owner_(owner), host_route_id_(host_route_id) {} | |
| 47 | |
| 48 void OnChannelError() override { sender_ = NULL; } | |
| 49 | |
| 50 void OnChannelClosing() override { sender_ = NULL; } | |
| 51 | |
| 52 void OnFilterAdded(IPC::Sender* sender) override { sender_ = sender; } | |
| 53 | |
| 54 void OnFilterRemoved() override { | |
| 55 // This will delete |owner_| and |this|. | |
| 56 owner_->OnFilterRemoved(); | |
| 57 } | |
| 58 | |
| 59 bool OnMessageReceived(const IPC::Message& msg) override { | |
| 60 if (msg.routing_id() != host_route_id_) | |
| 61 return false; | |
| 62 DVLOG(3) << __func__; | |
|
wuchengli
2015/04/15 07:11:58
The log in OnDecode should be enough. This can be
kcwu
2015/04/16 14:38:28
Done.
| |
| 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 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) | |
| 129 jpeg_decode_accelerator_.reset(new VaapiJpegDecodeAccelerator()); | |
| 130 #else | |
| 131 DVLOG(1) << "HW JPEG decode acceleration not available."; | |
| 132 return false; | |
| 133 #endif | |
| 134 | |
| 135 if (!jpeg_decode_accelerator_->Initialize(this)) { | |
| 136 return false; | |
| 137 } | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 void GpuJpegDecodeAccelerator::NotifyError( | |
| 142 int32_t buffer_id, | |
| 143 media::JpegDecodeAccelerator::Error error) { | |
| 144 DVLOG(3) << __func__; | |
| 145 Send(new AcceleratedJpegDecoderHostMsg_NotifyError(host_route_id_, buffer_id, | |
| 146 error)); | |
| 147 } | |
| 148 | |
| 149 void GpuJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { | |
| 150 // This is called from JDA's decode thread. | |
|
wuchengli
2015/04/15 07:11:58
This should be called from IO thread to reduce the
kcwu
2015/04/20 17:47:58
I don't think this will reduce latency. The flow i
| |
| 151 DVLOG(3) << __func__; | |
| 152 Send(new AcceleratedJpegDecoderHostMsg_VideoFrameReady(host_route_id_, | |
| 153 bitstream_buffer_id)); | |
| 154 } | |
| 155 | |
| 156 void DecodeFinished(scoped_ptr<base::SharedMemory> shm) { | |
| 157 // Do nothing. The purpose of this function is just to keep life-cycle of | |
| 158 // SharedMemory as VideoFrame | |
|
wuchengli
2015/04/15 07:11:58
I don't understand. What do you mean?
kcwu
2015/04/20 17:47:58
Done.
Comment updated.
| |
| 159 DVLOG(3) << "DecodeFinished"; | |
|
wuchengli
2015/04/15 07:11:58
__func__
kcwu
2015/04/16 14:38:28
Done.
| |
| 160 } | |
| 161 | |
| 162 void GpuJpegDecodeAccelerator::OnDecode( | |
| 163 const AcceleratedJpegDecoderMsg_Decode_Params& params) { | |
| 164 DVLOG(3) << __func__; | |
| 165 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
| 166 DCHECK(jpeg_decode_accelerator_.get()); | |
| 167 | |
| 168 if (params.input_buffer_id < 0) { | |
| 169 DLOG(ERROR) << "BitstreamBuffer id " << params.input_buffer_id | |
|
wuchengli
2015/04/15 07:11:58
s/DLOG/LOG/
kcwu
2015/04/16 14:38:28
Done.
| |
| 170 << " out of range"; | |
| 171 NotifyError(params.input_buffer_id, | |
| 172 media::JpegDecodeAccelerator::INVALID_ARGUMENT); | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 media::BitstreamBuffer input_buffer(params.input_buffer_id, | |
| 177 params.input_buffer_handle, | |
| 178 params.input_buffer_size); | |
| 179 | |
| 180 scoped_ptr<base::SharedMemory> output_shm( | |
| 181 new base::SharedMemory(params.output_video_frame_handle, false)); | |
| 182 if (!output_shm->Map(params.output_buffer_size)) { | |
| 183 DLOG(ERROR) << "Could not map output shared memory for input buffer id " | |
|
wuchengli
2015/04/15 07:11:58
s/DLOG/LOG/
kcwu
2015/04/16 14:38:28
Done.
| |
| 184 << params.input_buffer_id; | |
| 185 NotifyError(params.input_buffer_id, | |
| 186 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 uint8* shm_memory = reinterpret_cast<uint8*>(output_shm->memory()); | |
| 191 scoped_refptr<media::VideoFrame> frame = | |
| 192 media::VideoFrame::WrapExternalPackedMemory( | |
| 193 media::VideoFrame::I420, | |
| 194 params.coded_size, | |
| 195 gfx::Rect(params.coded_size), | |
| 196 params.coded_size, | |
|
wuchengli
2015/04/15 07:11:58
We don't really care about the visible size here.
kcwu
2015/04/16 14:38:28
Yes
| |
| 197 shm_memory, | |
| 198 params.output_buffer_size, | |
| 199 params.output_video_frame_handle, | |
| 200 0, | |
| 201 base::TimeDelta(), | |
| 202 base::Bind(DecodeFinished, base::Passed(&output_shm))); | |
| 203 | |
| 204 if (!frame.get()) { | |
| 205 DLOG(ERROR) << "Could not create VideoFrame for input buffer id " | |
|
wuchengli
2015/04/15 07:11:58
s/DLOG/LOG/
kcwu
2015/04/16 14:38:28
Done.
| |
| 206 << params.input_buffer_id; | |
| 207 NotifyError(params.input_buffer_id, | |
| 208 media::JpegDecodeAccelerator::PLATFORM_FAILURE); | |
| 209 return; | |
| 210 } | |
| 211 | |
| 212 jpeg_decode_accelerator_->Decode(input_buffer, frame); | |
| 213 } | |
| 214 | |
| 215 void GpuJpegDecodeAccelerator::OnDestroy() { | |
|
wuchengli
2015/04/15 07:11:58
DCHECK main thread
kcwu
2015/04/20 17:47:58
Done.
| |
| 216 DVLOG(3) << __func__; | |
| 217 DCHECK(jpeg_decode_accelerator_.get()); | |
| 218 Destroy(); | |
| 219 } | |
| 220 | |
| 221 void GpuJpegDecodeAccelerator::OnFilterRemoved() { | |
| 222 // We're destroying; cancel all callbacks. | |
| 223 filter_removed_.Signal(); | |
| 224 } | |
| 225 | |
| 226 void GpuJpegDecodeAccelerator::Destroy() { | |
|
wuchengli
2015/04/15 07:11:57
Do we need this method? Can this be combined with
kcwu
2015/04/20 17:47:58
There are two flows to destroy GJDA:
1. msg -> OnD
| |
| 227 // We cannot destroy the JDA before the IO thread message filter is | |
| 228 // removed however, since we cannot service incoming messages with JDA gone. | |
| 229 // We cannot simply check for existence of JDA on IO thread though, because | |
| 230 // we don't want to synchronize the IO thread with the ChildThread. | |
| 231 // So we have to wait for the RemoveFilter callback here instead and remove | |
| 232 // the JDA after it arrives and before returning. | |
| 233 if (filter_.get()) { | |
|
wuchengli
2015/04/15 07:11:58
|filter_| should always exist after Initialize. Th
kcwu
2015/04/20 17:47:58
This is necessary for safety if GpuJpegDecodeAccel
| |
| 234 channel_->RemoveFilter(filter_.get()); | |
| 235 filter_removed_.Wait(); | |
| 236 } | |
| 237 | |
| 238 channel_->RemoveRoute(host_route_id_); | |
|
wuchengli
2015/04/15 07:11:58
This can be called in ReleaseJpegDecoder.
kcwu
2015/04/20 17:47:58
AddRoute is in Initialize(). I'd like RemoveRoute
| |
| 239 channel_->ReleaseJpegDecoder(); | |
| 240 jpeg_decode_accelerator_.reset(); | |
| 241 | |
| 242 delete this; | |
| 243 } | |
| 244 | |
| 245 bool GpuJpegDecodeAccelerator::Send(IPC::Message* message) { | |
| 246 if (filter_.get() && io_message_loop_->BelongsToCurrentThread()) | |
|
wuchengli
2015/04/15 07:11:58
Remove filter_.get() check.
kcwu
2015/04/20 17:47:58
Done.
| |
| 247 return filter_->SendOnIOThread(message); | |
| 248 return channel_->Send(message); | |
| 249 } | |
| 250 | |
| 251 } // namespace content | |
| OLD | NEW |