OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/common/gpu/media/gpu_video_decode_accelerator.h" | 5 #include "content/common/gpu/media/gpu_video_decode_accelerator.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 | 14 |
14 #include "content/common/gpu/gpu_channel.h" | 15 #include "content/common/gpu/gpu_channel.h" |
15 #include "content/common/gpu/gpu_messages.h" | 16 #include "content/common/gpu/gpu_messages.h" |
16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
17 #include "gpu/command_buffer/common/command_buffer.h" | 18 #include "gpu/command_buffer/common/command_buffer.h" |
18 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
19 #include "ipc/ipc_message_utils.h" | 20 #include "ipc/ipc_message_utils.h" |
20 #include "ui/gl/gl_context.h" | 21 #include "ui/gl/gl_context.h" |
21 #include "ui/gl/gl_surface_egl.h" | 22 #include "ui/gl/gl_surface_egl.h" |
(...skipping 25 matching lines...) Expand all Loading... |
47 } | 48 } |
48 | 49 |
49 if (!stub->decoder()->MakeCurrent()) { | 50 if (!stub->decoder()->MakeCurrent()) { |
50 DLOG(ERROR) << "Failed to MakeCurrent()"; | 51 DLOG(ERROR) << "Failed to MakeCurrent()"; |
51 return false; | 52 return false; |
52 } | 53 } |
53 | 54 |
54 return true; | 55 return true; |
55 } | 56 } |
56 | 57 |
| 58 class GpuVideoDecodeAccelerator::MessageFilter |
| 59 : public IPC::ChannelProxy::MessageFilter { |
| 60 public: |
| 61 MessageFilter(GpuVideoDecodeAccelerator* owner, int32 host_route_id) |
| 62 : owner_(owner), host_route_id_(host_route_id) {} |
| 63 |
| 64 virtual void OnFilterRemoved() OVERRIDE { |
| 65 // This will delete |owner_| and |this|. |
| 66 owner_->OnFilterRemoved(); |
| 67 } |
| 68 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE { |
| 69 if (msg.routing_id() != host_route_id_) |
| 70 return false; |
| 71 |
| 72 IPC_BEGIN_MESSAGE_MAP(MessageFilter, msg) |
| 73 IPC_MESSAGE_FORWARD(AcceleratedVideoDecoderMsg_Decode, owner_, |
| 74 GpuVideoDecodeAccelerator::OnDecode) |
| 75 IPC_MESSAGE_UNHANDLED(return false;) |
| 76 IPC_END_MESSAGE_MAP() |
| 77 return true; |
| 78 } |
| 79 |
| 80 protected: |
| 81 virtual ~MessageFilter() {} |
| 82 |
| 83 private: |
| 84 GpuVideoDecodeAccelerator* owner_; |
| 85 int32 host_route_id_; |
| 86 }; |
| 87 |
57 GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator(int32 host_route_id, | 88 GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator(int32 host_route_id, |
58 GpuCommandBufferStub* stub) | 89 GpuCommandBufferStub* stub) |
59 : init_done_msg_(NULL), | 90 : init_done_msg_(NULL), |
60 host_route_id_(host_route_id), | 91 host_route_id_(host_route_id), |
61 stub_(stub), | 92 stub_(stub), |
62 texture_target_(0) { | 93 texture_target_(0) { |
63 DCHECK(stub_); | 94 DCHECK(stub_); |
64 stub_->AddDestructionObserver(this); | 95 stub_->AddDestructionObserver(this); |
65 stub_->channel()->AddRoute(host_route_id_, this); | 96 stub_->channel()->AddRoute(host_route_id_, this); |
| 97 child_message_loop_ = base::MessageLoopProxy::current(); |
66 make_context_current_ = | 98 make_context_current_ = |
67 base::Bind(&MakeDecoderContextCurrent, stub_->AsWeakPtr()); | 99 base::Bind(&MakeDecoderContextCurrent, stub_->AsWeakPtr()); |
68 } | 100 } |
69 | 101 |
70 GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() { | 102 GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() { |
71 DCHECK(stub_); | |
72 if (video_decode_accelerator_) | 103 if (video_decode_accelerator_) |
73 video_decode_accelerator_.release()->Destroy(); | 104 video_decode_accelerator_.release()->Destroy(); |
74 | |
75 stub_->channel()->RemoveRoute(host_route_id_); | |
76 stub_->RemoveDestructionObserver(this); | |
77 } | 105 } |
78 | 106 |
79 bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { | 107 bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) { |
80 DCHECK(stub_); | 108 DCHECK(stub_); |
81 if (!video_decode_accelerator_) | 109 if (!video_decode_accelerator_) |
82 return false; | 110 return false; |
83 bool handled = true; | 111 bool handled = true; |
84 IPC_BEGIN_MESSAGE_MAP(GpuVideoDecodeAccelerator, msg) | 112 IPC_BEGIN_MESSAGE_MAP(GpuVideoDecodeAccelerator, msg) |
85 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Decode, OnDecode) | 113 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Decode, OnDecode) |
86 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignPictureBuffers, | 114 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignPictureBuffers, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 170 } |
143 if (!Send(new AcceleratedVideoDecoderHostMsg_ErrorNotification( | 171 if (!Send(new AcceleratedVideoDecoderHostMsg_ErrorNotification( |
144 host_route_id_, error))) { | 172 host_route_id_, error))) { |
145 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ErrorNotification) " | 173 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ErrorNotification) " |
146 << "failed"; | 174 << "failed"; |
147 } | 175 } |
148 } | 176 } |
149 | 177 |
150 void GpuVideoDecodeAccelerator::Initialize( | 178 void GpuVideoDecodeAccelerator::Initialize( |
151 const media::VideoCodecProfile profile, | 179 const media::VideoCodecProfile profile, |
152 IPC::Message* init_done_msg) { | 180 IPC::Message* init_done_msg, |
| 181 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) { |
153 DCHECK(stub_); | 182 DCHECK(stub_); |
154 DCHECK(!video_decode_accelerator_.get()); | 183 DCHECK(!video_decode_accelerator_.get()); |
155 DCHECK(!init_done_msg_); | 184 DCHECK(!init_done_msg_); |
156 DCHECK(init_done_msg); | 185 DCHECK(init_done_msg); |
157 init_done_msg_ = init_done_msg; | 186 init_done_msg_ = init_done_msg; |
158 | 187 |
159 #if !defined(OS_WIN) | 188 #if !defined(OS_WIN) |
160 // Ensure we will be able to get a GL context at all before initializing | 189 // Ensure we will be able to get a GL context at all before initializing |
161 // non-Windows VDAs. | 190 // non-Windows VDAs. |
162 if (!make_context_current_.Run()) { | 191 if (!make_context_current_.Run()) { |
163 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); | 192 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
164 return; | 193 return; |
165 } | 194 } |
166 #endif | 195 #endif |
167 | 196 |
168 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
169 if (base::win::GetVersion() < base::win::VERSION_WIN7) { | 198 if (base::win::GetVersion() < base::win::VERSION_WIN7) { |
170 NOTIMPLEMENTED() << "HW video decode acceleration not available."; | 199 NOTIMPLEMENTED() << "HW video decode acceleration not available."; |
171 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); | 200 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
172 return; | 201 return; |
173 } | 202 } |
174 DLOG(INFO) << "Initializing DXVA HW decoder for windows."; | 203 DLOG(INFO) << "Initializing DXVA HW decoder for windows."; |
175 video_decode_accelerator_.reset(new DXVAVideoDecodeAccelerator( | 204 video_decode_accelerator_.reset(new DXVAVideoDecodeAccelerator( |
176 this, make_context_current_)); | 205 this, make_context_current_)); |
177 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) | 206 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11) |
178 video_decode_accelerator_.reset(new ExynosVideoDecodeAccelerator( | 207 video_decode_accelerator_.reset(new ExynosVideoDecodeAccelerator( |
179 gfx::GLSurfaceEGL::GetHardwareDisplay(), | 208 gfx::GLSurfaceEGL::GetHardwareDisplay(), |
180 stub_->decoder()->GetGLContext()->GetHandle(), | 209 stub_->decoder()->GetGLContext()->GetHandle(), |
181 this, | 210 this, |
182 make_context_current_)); | 211 make_context_current_, |
| 212 io_message_loop)); |
183 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) | 213 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11) |
184 gfx::GLContextGLX* glx_context = | 214 gfx::GLContextGLX* glx_context = |
185 static_cast<gfx::GLContextGLX*>(stub_->decoder()->GetGLContext()); | 215 static_cast<gfx::GLContextGLX*>(stub_->decoder()->GetGLContext()); |
186 GLXContext glx_context_handle = | 216 GLXContext glx_context_handle = |
187 static_cast<GLXContext>(glx_context->GetHandle()); | 217 static_cast<GLXContext>(glx_context->GetHandle()); |
188 video_decode_accelerator_.reset(new VaapiVideoDecodeAccelerator( | 218 video_decode_accelerator_.reset(new VaapiVideoDecodeAccelerator( |
189 glx_context->display(), glx_context_handle, this, | 219 glx_context->display(), glx_context_handle, this, |
190 make_context_current_)); | 220 make_context_current_)); |
191 #elif defined(OS_ANDROID) | 221 #elif defined(OS_ANDROID) |
192 video_decode_accelerator_.reset(new AndroidVideoDecodeAccelerator( | 222 video_decode_accelerator_.reset(new AndroidVideoDecodeAccelerator( |
193 this, | 223 this, |
194 stub_->decoder()->AsWeakPtr(), | 224 stub_->decoder()->AsWeakPtr(), |
195 make_context_current_)); | 225 make_context_current_)); |
196 #else | 226 #else |
197 NOTIMPLEMENTED() << "HW video decode acceleration not available."; | 227 NOTIMPLEMENTED() << "HW video decode acceleration not available."; |
198 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); | 228 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
199 return; | 229 return; |
200 #endif | 230 #endif |
201 | 231 |
| 232 if (video_decode_accelerator_->CanDecodeOnIOThread()) { |
| 233 filter_ = new MessageFilter(this, host_route_id_); |
| 234 stub_->channel()->AddFilter(filter_.get()); |
| 235 } |
| 236 |
202 if (!video_decode_accelerator_->Initialize(profile)) | 237 if (!video_decode_accelerator_->Initialize(profile)) |
203 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); | 238 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE); |
204 } | 239 } |
205 | 240 |
| 241 // Runs on IO thread if video_decode_accelerator_->CanDecodeOnIOThread() is |
| 242 // true, otherwise on the main thread. |
206 void GpuVideoDecodeAccelerator::OnDecode( | 243 void GpuVideoDecodeAccelerator::OnDecode( |
207 base::SharedMemoryHandle handle, int32 id, uint32 size) { | 244 base::SharedMemoryHandle handle, int32 id, uint32 size) { |
208 DCHECK(video_decode_accelerator_.get()); | 245 DCHECK(video_decode_accelerator_.get()); |
209 if (id < 0) { | 246 if (id < 0) { |
210 DLOG(FATAL) << "BitstreamBuffer id " << id << " out of range"; | 247 DLOG(FATAL) << "BitstreamBuffer id " << id << " out of range"; |
211 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT); | 248 if (child_message_loop_->BelongsToCurrentThread()) { |
| 249 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT); |
| 250 } else { |
| 251 child_message_loop_->PostTask( |
| 252 FROM_HERE, |
| 253 base::Bind(&GpuVideoDecodeAccelerator::NotifyError, |
| 254 base::Unretained(this), |
| 255 media::VideoDecodeAccelerator::INVALID_ARGUMENT)); |
| 256 } |
212 return; | 257 return; |
213 } | 258 } |
214 video_decode_accelerator_->Decode(media::BitstreamBuffer(id, handle, size)); | 259 video_decode_accelerator_->Decode(media::BitstreamBuffer(id, handle, size)); |
215 } | 260 } |
216 | 261 |
217 void GpuVideoDecodeAccelerator::OnAssignPictureBuffers( | 262 void GpuVideoDecodeAccelerator::OnAssignPictureBuffers( |
218 const std::vector<int32>& buffer_ids, | 263 const std::vector<int32>& buffer_ids, |
219 const std::vector<uint32>& texture_ids, | 264 const std::vector<uint32>& texture_ids, |
220 const std::vector<gfx::Size>& sizes) { | 265 const std::vector<gfx::Size>& sizes) { |
221 DCHECK(stub_); | 266 DCHECK(stub_); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 video_decode_accelerator_->Flush(); | 335 video_decode_accelerator_->Flush(); |
291 } | 336 } |
292 | 337 |
293 void GpuVideoDecodeAccelerator::OnReset() { | 338 void GpuVideoDecodeAccelerator::OnReset() { |
294 DCHECK(video_decode_accelerator_.get()); | 339 DCHECK(video_decode_accelerator_.get()); |
295 video_decode_accelerator_->Reset(); | 340 video_decode_accelerator_->Reset(); |
296 } | 341 } |
297 | 342 |
298 void GpuVideoDecodeAccelerator::OnDestroy() { | 343 void GpuVideoDecodeAccelerator::OnDestroy() { |
299 DCHECK(video_decode_accelerator_.get()); | 344 DCHECK(video_decode_accelerator_.get()); |
300 delete this; | 345 DCHECK(stub_); |
| 346 stub_->channel()->RemoveRoute(host_route_id_); |
| 347 stub_->RemoveDestructionObserver(this); |
| 348 if (filter_.get()) { |
| 349 // Remove the filter first because the member variables can be accessed on |
| 350 // IO thread. When filter is removed, OnFilterRemoved will delete |this|. |
| 351 stub_->channel()->RemoveFilter(filter_.get()); |
| 352 } else { |
| 353 delete this; |
| 354 } |
| 355 } |
| 356 |
| 357 void GpuVideoDecodeAccelerator::OnFilterRemoved() { |
| 358 child_message_loop_->DeleteSoon(FROM_HERE, this); |
301 } | 359 } |
302 | 360 |
303 void GpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( | 361 void GpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer( |
304 int32 bitstream_buffer_id) { | 362 int32 bitstream_buffer_id) { |
305 if (!Send(new AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed( | 363 if (!Send(new AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed( |
306 host_route_id_, bitstream_buffer_id))) { | 364 host_route_id_, bitstream_buffer_id))) { |
307 DLOG(ERROR) | 365 DLOG(ERROR) |
308 << "Send(AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed) " | 366 << "Send(AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed) " |
309 << "failed"; | 367 << "failed"; |
310 } | 368 } |
(...skipping 10 matching lines...) Expand all Loading... |
321 void GpuVideoDecodeAccelerator::NotifyFlushDone() { | 379 void GpuVideoDecodeAccelerator::NotifyFlushDone() { |
322 if (!Send(new AcceleratedVideoDecoderHostMsg_FlushDone(host_route_id_))) | 380 if (!Send(new AcceleratedVideoDecoderHostMsg_FlushDone(host_route_id_))) |
323 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_FlushDone) failed"; | 381 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_FlushDone) failed"; |
324 } | 382 } |
325 | 383 |
326 void GpuVideoDecodeAccelerator::NotifyResetDone() { | 384 void GpuVideoDecodeAccelerator::NotifyResetDone() { |
327 if (!Send(new AcceleratedVideoDecoderHostMsg_ResetDone(host_route_id_))) | 385 if (!Send(new AcceleratedVideoDecoderHostMsg_ResetDone(host_route_id_))) |
328 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ResetDone) failed"; | 386 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ResetDone) failed"; |
329 } | 387 } |
330 | 388 |
331 void GpuVideoDecodeAccelerator::OnWillDestroyStub() { | 389 void GpuVideoDecodeAccelerator::OnWillDestroyStub() { OnDestroy(); } |
332 delete this; | |
333 } | |
334 | 390 |
335 bool GpuVideoDecodeAccelerator::Send(IPC::Message* message) { | 391 bool GpuVideoDecodeAccelerator::Send(IPC::Message* message) { |
336 DCHECK(stub_); | 392 DCHECK(stub_); |
337 return stub_->channel()->Send(message); | 393 return stub_->channel()->Send(message); |
338 } | 394 } |
339 | 395 |
340 } // namespace content | 396 } // namespace content |
OLD | NEW |