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
3 // found in the LICENSE file.
5 #include "content/common/gpu/media/gpu_video_decode_accelerator.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #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 "media/base/limits.h"
22 #include "ui/gl/gl_context.h"
23 #include "ui/gl/gl_surface_egl.h"
26 #include "base/win/windows_version.h"
27 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
28 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11)
29 #include "content/common/gpu/media/exynos_video_decode_accelerator.h"
30 #elif defined(OS_TIZEN_MOBILE) && defined(ARCH_CPU_X86_FAMILY)
31 #include "content/common/gpu/media/vaapi_video_decode_accelerator_tizen.h"
32 #elif defined(OS_LINUX) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
33 #include "ui/gl/gl_context_glx.h"
34 #include "content/common/gpu/media/vaapi_video_decode_accelerator.h"
35 #elif defined(OS_ANDROID)
36 #include "content/common/gpu/media/android_video_decode_accelerator.h"
39 #include "ui/gfx/size.h"
43 static bool MakeDecoderContextCurrent(
44 const base::WeakPtr<GpuCommandBufferStub> stub) {
46 DLOG(ERROR) << "Stub is gone; won't MakeCurrent().";
50 if (!stub->decoder()->MakeCurrent()) {
51 DLOG(ERROR) << "Failed to MakeCurrent()";
58 // A helper class that works like AutoLock but only acquires the lock when
62 explicit DebugAutoLock(base::Lock& lock) : lock_(lock) {
69 lock_.AssertAcquired();
76 DISALLOW_COPY_AND_ASSIGN(DebugAutoLock);
79 class GpuVideoDecodeAccelerator::MessageFilter
80 : public IPC::ChannelProxy::MessageFilter {
82 MessageFilter(GpuVideoDecodeAccelerator* owner, int32 host_route_id)
83 : owner_(owner), host_route_id_(host_route_id) {}
85 virtual void OnChannelError() OVERRIDE { channel_ = NULL; }
87 virtual void OnChannelClosing() OVERRIDE { channel_ = NULL; }
89 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE {
93 virtual void OnFilterRemoved() OVERRIDE {
94 // This will delete |owner_| and |this|.
95 owner_->OnFilterRemoved();
98 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE {
99 if (msg.routing_id() != host_route_id_)
102 IPC_BEGIN_MESSAGE_MAP(MessageFilter, msg)
103 IPC_MESSAGE_FORWARD(AcceleratedVideoDecoderMsg_Decode, owner_,
104 GpuVideoDecodeAccelerator::OnDecode)
105 IPC_MESSAGE_UNHANDLED(return false;)
106 IPC_END_MESSAGE_MAP()
110 bool SendOnIOThread(IPC::Message* message) {
111 DCHECK(!message->is_sync());
116 return channel_->Send(message);
120 virtual ~MessageFilter() {}
123 GpuVideoDecodeAccelerator* owner_;
124 int32 host_route_id_;
125 // The channel to which this filter was added.
126 IPC::Channel* channel_;
129 GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator(
131 GpuCommandBufferStub* stub,
132 const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
133 : init_done_msg_(NULL),
134 host_route_id_(host_route_id),
137 io_message_loop_(io_message_loop),
138 weak_factory_for_io_(this) {
140 stub_->AddDestructionObserver(this);
141 stub_->channel()->AddRoute(host_route_id_, this);
142 child_message_loop_ = base::MessageLoopProxy::current();
143 make_context_current_ =
144 base::Bind(&MakeDecoderContextCurrent, stub_->AsWeakPtr());
147 GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() {
148 if (video_decode_accelerator_)
149 video_decode_accelerator_.release()->Destroy();
152 bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) {
154 if (!video_decode_accelerator_)
157 IPC_BEGIN_MESSAGE_MAP(GpuVideoDecodeAccelerator, msg)
158 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Decode, OnDecode)
159 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_AssignPictureBuffers,
160 OnAssignPictureBuffers)
161 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_ReusePictureBuffer,
162 OnReusePictureBuffer)
163 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Flush, OnFlush)
164 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Reset, OnReset)
165 IPC_MESSAGE_HANDLER(AcceleratedVideoDecoderMsg_Destroy, OnDestroy)
166 IPC_MESSAGE_UNHANDLED(handled = false)
167 IPC_END_MESSAGE_MAP()
171 void GpuVideoDecodeAccelerator::ProvidePictureBuffers(
172 uint32 requested_num_of_buffers,
173 const gfx::Size& dimensions,
174 uint32 texture_target) {
175 if (dimensions.width() > media::limits::kMaxDimension ||
176 dimensions.height() > media::limits::kMaxDimension ||
177 dimensions.GetArea() > media::limits::kMaxCanvas) {
178 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
181 if (!Send(new AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers(
183 requested_num_of_buffers,
186 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers) "
189 texture_dimensions_ = dimensions;
190 texture_target_ = texture_target;
193 void GpuVideoDecodeAccelerator::DismissPictureBuffer(
194 int32 picture_buffer_id) {
195 // Notify client that picture buffer is now unused.
196 if (!Send(new AcceleratedVideoDecoderHostMsg_DismissPictureBuffer(
197 host_route_id_, picture_buffer_id))) {
198 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_DismissPictureBuffer) "
201 DebugAutoLock auto_lock(debug_uncleared_textures_lock_);
202 uncleared_textures_.erase(picture_buffer_id);
205 void GpuVideoDecodeAccelerator::PictureReady(
206 const media::Picture& picture) {
207 // VDA may call PictureReady on IO thread. SetTextureCleared should run on
208 // the child thread. VDA is responsible to call PictureReady on the child
209 // thread when a picture buffer is delivered the first time.
210 if (child_message_loop_->BelongsToCurrentThread()) {
211 SetTextureCleared(picture);
213 DCHECK(io_message_loop_->BelongsToCurrentThread());
214 if (DCHECK_IS_ON()) {
215 DebugAutoLock auto_lock(debug_uncleared_textures_lock_);
216 DCHECK_EQ(0u, uncleared_textures_.count(picture.picture_buffer_id()));
220 if (!Send(new AcceleratedVideoDecoderHostMsg_PictureReady(
222 picture.picture_buffer_id(),
223 picture.bitstream_buffer_id()))) {
224 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_PictureReady) failed";
228 void GpuVideoDecodeAccelerator::NotifyError(
229 media::VideoDecodeAccelerator::Error error) {
230 if (init_done_msg_) {
231 // If we get an error while we're initializing, NotifyInitializeDone won't
232 // be called, so we need to send the reply (with an error) here.
233 GpuCommandBufferMsg_CreateVideoDecoder::WriteReplyParams(
235 if (!Send(init_done_msg_))
236 DLOG(ERROR) << "Send(init_done_msg_) failed";
237 init_done_msg_ = NULL;
240 if (!Send(new AcceleratedVideoDecoderHostMsg_ErrorNotification(
241 host_route_id_, error))) {
242 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ErrorNotification) "
247 void GpuVideoDecodeAccelerator::Initialize(
248 const media::VideoCodecProfile profile,
249 IPC::Message* init_done_msg) {
251 DCHECK(!video_decode_accelerator_.get());
252 DCHECK(!init_done_msg_);
253 DCHECK(init_done_msg);
254 init_done_msg_ = init_done_msg;
257 // Ensure we will be able to get a GL context at all before initializing
259 if (!make_context_current_.Run()) {
260 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
266 if (base::win::GetVersion() < base::win::VERSION_WIN7) {
267 NOTIMPLEMENTED() << "HW video decode acceleration not available.";
268 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
271 DLOG(INFO) << "Initializing DXVA HW decoder for windows.";
272 video_decode_accelerator_.reset(new DXVAVideoDecodeAccelerator(
273 this, make_context_current_));
274 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) && defined(USE_X11)
275 video_decode_accelerator_.reset(new ExynosVideoDecodeAccelerator(
276 gfx::GLSurfaceEGL::GetHardwareDisplay(),
277 stub_->decoder()->GetGLContext()->GetHandle(),
279 weak_factory_for_io_.GetWeakPtr(),
280 make_context_current_,
282 #elif defined(OS_TIZEN_MOBILE) && defined(ARCH_CPU_X86_FAMILY)
283 video_decode_accelerator_.reset(new VaapiVideoDecodeAccelerator(
284 gfx::GLSurfaceEGL::GetHardwareDisplay(),
285 stub_->decoder()->GetGLContext()->GetHandle(),
287 make_context_current_));
288 #elif defined(OS_LINUX) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
289 gfx::GLContextGLX* glx_context =
290 static_cast<gfx::GLContextGLX*>(stub_->decoder()->GetGLContext());
291 GLXContext glx_context_handle =
292 static_cast<GLXContext>(glx_context->GetHandle());
293 video_decode_accelerator_.reset(new VaapiVideoDecodeAccelerator(
294 glx_context->display(), glx_context_handle, this,
295 make_context_current_));
296 #elif defined(OS_ANDROID)
297 video_decode_accelerator_.reset(new AndroidVideoDecodeAccelerator(
299 stub_->decoder()->AsWeakPtr(),
300 make_context_current_));
302 NOTIMPLEMENTED() << "HW video decode acceleration not available.";
303 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
307 if (video_decode_accelerator_->CanDecodeOnIOThread()) {
308 filter_ = new MessageFilter(this, host_route_id_);
309 stub_->channel()->AddFilter(filter_.get());
312 if (!video_decode_accelerator_->Initialize(profile))
313 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
316 // Runs on IO thread if video_decode_accelerator_->CanDecodeOnIOThread() is
317 // true, otherwise on the main thread.
318 void GpuVideoDecodeAccelerator::OnDecode(
319 base::SharedMemoryHandle handle, int32 id, uint32 size) {
320 DCHECK(video_decode_accelerator_.get());
322 DLOG(ERROR) << "BitstreamBuffer id " << id << " out of range";
323 if (child_message_loop_->BelongsToCurrentThread()) {
324 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
326 child_message_loop_->PostTask(
328 base::Bind(&GpuVideoDecodeAccelerator::NotifyError,
329 base::Unretained(this),
330 media::VideoDecodeAccelerator::INVALID_ARGUMENT));
334 video_decode_accelerator_->Decode(media::BitstreamBuffer(id, handle, size));
337 void GpuVideoDecodeAccelerator::OnAssignPictureBuffers(
338 const std::vector<int32>& buffer_ids,
339 const std::vector<uint32>& texture_ids) {
341 if (buffer_ids.size() != texture_ids.size()) {
342 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
346 gpu::gles2::GLES2Decoder* command_decoder = stub_->decoder();
347 gpu::gles2::TextureManager* texture_manager =
348 command_decoder->GetContextGroup()->texture_manager();
350 std::vector<media::PictureBuffer> buffers;
351 std::vector<scoped_refptr<gpu::gles2::TextureRef> > textures;
352 for (uint32 i = 0; i < buffer_ids.size(); ++i) {
353 if (buffer_ids[i] < 0) {
354 DLOG(ERROR) << "Buffer id " << buffer_ids[i] << " out of range";
355 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
358 gpu::gles2::TextureRef* texture_ref = texture_manager->GetTexture(
361 DLOG(ERROR) << "Failed to find texture id " << texture_ids[i];
362 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
365 gpu::gles2::Texture* info = texture_ref->texture();
366 if (info->target() != texture_target_) {
367 DLOG(ERROR) << "Texture target mismatch for texture id "
369 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
372 if (texture_target_ == GL_TEXTURE_EXTERNAL_OES) {
373 // GL_TEXTURE_EXTERNAL_OES textures have their dimensions defined by the
374 // underlying EGLImage. Use |texture_dimensions_| for this size.
375 texture_manager->SetLevelInfo(texture_ref,
376 GL_TEXTURE_EXTERNAL_OES,
379 texture_dimensions_.width(),
380 texture_dimensions_.height(),
387 // For other targets, texture dimensions should already be defined.
388 GLsizei width = 0, height = 0;
389 info->GetLevelSize(texture_target_, 0, &width, &height);
390 if (width != texture_dimensions_.width() ||
391 height != texture_dimensions_.height()) {
392 DLOG(ERROR) << "Size mismatch for texture id " << texture_ids[i];
393 NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
397 uint32 service_texture_id;
398 if (!command_decoder->GetServiceTextureId(
399 texture_ids[i], &service_texture_id)) {
400 DLOG(ERROR) << "Failed to translate texture!";
401 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
404 buffers.push_back(media::PictureBuffer(
405 buffer_ids[i], texture_dimensions_, service_texture_id));
406 textures.push_back(texture_ref);
408 video_decode_accelerator_->AssignPictureBuffers(buffers);
409 DebugAutoLock auto_lock(debug_uncleared_textures_lock_);
410 for (uint32 i = 0; i < buffer_ids.size(); ++i)
411 uncleared_textures_[buffer_ids[i]] = textures[i];
414 void GpuVideoDecodeAccelerator::OnReusePictureBuffer(
415 int32 picture_buffer_id) {
416 DCHECK(video_decode_accelerator_.get());
417 video_decode_accelerator_->ReusePictureBuffer(picture_buffer_id);
420 void GpuVideoDecodeAccelerator::OnFlush() {
421 DCHECK(video_decode_accelerator_.get());
422 video_decode_accelerator_->Flush();
425 void GpuVideoDecodeAccelerator::OnReset() {
426 DCHECK(video_decode_accelerator_.get());
427 video_decode_accelerator_->Reset();
430 void GpuVideoDecodeAccelerator::OnDestroy() {
431 DCHECK(video_decode_accelerator_.get());
435 void GpuVideoDecodeAccelerator::OnFilterRemoved() {
436 // We're destroying; cancel all callbacks.
437 weak_factory_for_io_.InvalidateWeakPtrs();
438 child_message_loop_->DeleteSoon(FROM_HERE, this);
441 void GpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer(
442 int32 bitstream_buffer_id) {
443 if (!Send(new AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed(
444 host_route_id_, bitstream_buffer_id))) {
446 << "Send(AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed) "
451 void GpuVideoDecodeAccelerator::NotifyInitializeDone() {
452 GpuCommandBufferMsg_CreateVideoDecoder::WriteReplyParams(
453 init_done_msg_, host_route_id_);
454 if (!Send(init_done_msg_))
455 DLOG(ERROR) << "Send(init_done_msg_) failed";
456 init_done_msg_ = NULL;
459 void GpuVideoDecodeAccelerator::NotifyFlushDone() {
460 if (!Send(new AcceleratedVideoDecoderHostMsg_FlushDone(host_route_id_)))
461 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_FlushDone) failed";
464 void GpuVideoDecodeAccelerator::NotifyResetDone() {
465 if (!Send(new AcceleratedVideoDecoderHostMsg_ResetDone(host_route_id_)))
466 DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ResetDone) failed";
469 void GpuVideoDecodeAccelerator::OnWillDestroyStub() {
471 stub_->channel()->RemoveRoute(host_route_id_);
472 stub_->RemoveDestructionObserver(this);
474 // Remove the filter first because the member variables can be accessed on
475 // IO thread. When filter is removed, OnFilterRemoved will delete |this|.
476 stub_->channel()->RemoveFilter(filter_.get());
482 bool GpuVideoDecodeAccelerator::Send(IPC::Message* message) {
484 if (filter_.get() && io_message_loop_->BelongsToCurrentThread())
485 return filter_->SendOnIOThread(message);
486 DCHECK(child_message_loop_->BelongsToCurrentThread());
487 return stub_->channel()->Send(message);
490 void GpuVideoDecodeAccelerator::SetTextureCleared(
491 const media::Picture& picture) {
492 DCHECK(child_message_loop_->BelongsToCurrentThread());
493 DebugAutoLock auto_lock(debug_uncleared_textures_lock_);
494 std::map<int32, scoped_refptr<gpu::gles2::TextureRef> >::iterator it;
495 it = uncleared_textures_.find(picture.picture_buffer_id());
496 if (it == uncleared_textures_.end())
497 return; // the texture has been cleared
499 scoped_refptr<gpu::gles2::TextureRef> texture_ref = it->second;
500 GLenum target = texture_ref->texture()->target();
501 gpu::gles2::TextureManager* texture_manager =
502 stub_->decoder()->GetContextGroup()->texture_manager();
503 DCHECK(!texture_ref->texture()->IsLevelCleared(target, 0));
504 texture_manager->SetLevelCleared(texture_ref, target, 0, true);
505 uncleared_textures_.erase(it);
508 } // namespace content