- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / gpu_video_decode_accelerator.cc
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.
4
5 #include "content/common/gpu/media/gpu_video_decode_accelerator.h"
6
7 #include <vector>
8
9 #include "base/bind.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"
14
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"
24
25 #if defined(OS_WIN)
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"
37 #endif
38
39 #include "ui/gfx/size.h"
40
41 namespace content {
42
43 static bool MakeDecoderContextCurrent(
44     const base::WeakPtr<GpuCommandBufferStub> stub) {
45   if (!stub.get()) {
46     DLOG(ERROR) << "Stub is gone; won't MakeCurrent().";
47     return false;
48   }
49
50   if (!stub->decoder()->MakeCurrent()) {
51     DLOG(ERROR) << "Failed to MakeCurrent()";
52     return false;
53   }
54
55   return true;
56 }
57
58 // A helper class that works like AutoLock but only acquires the lock when
59 // DCHECK is on.
60 class DebugAutoLock {
61  public:
62   explicit DebugAutoLock(base::Lock& lock) : lock_(lock) {
63     if (DCHECK_IS_ON())
64       lock_.Acquire();
65   }
66
67   ~DebugAutoLock() {
68     if (DCHECK_IS_ON()) {
69       lock_.AssertAcquired();
70       lock_.Release();
71     }
72   }
73
74  private:
75   base::Lock& lock_;
76   DISALLOW_COPY_AND_ASSIGN(DebugAutoLock);
77 };
78
79 class GpuVideoDecodeAccelerator::MessageFilter
80     : public IPC::ChannelProxy::MessageFilter {
81  public:
82   MessageFilter(GpuVideoDecodeAccelerator* owner, int32 host_route_id)
83       : owner_(owner), host_route_id_(host_route_id) {}
84
85   virtual void OnChannelError() OVERRIDE { channel_ = NULL; }
86
87   virtual void OnChannelClosing() OVERRIDE { channel_ = NULL; }
88
89   virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE {
90     channel_ = channel;
91   }
92
93   virtual void OnFilterRemoved() OVERRIDE {
94     // This will delete |owner_| and |this|.
95     owner_->OnFilterRemoved();
96   }
97
98   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE {
99     if (msg.routing_id() != host_route_id_)
100       return false;
101
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()
107     return true;
108   }
109
110   bool SendOnIOThread(IPC::Message* message) {
111     DCHECK(!message->is_sync());
112     if (!channel_) {
113       delete message;
114       return false;
115     }
116     return channel_->Send(message);
117   }
118
119  protected:
120   virtual ~MessageFilter() {}
121
122  private:
123   GpuVideoDecodeAccelerator* owner_;
124   int32 host_route_id_;
125   // The channel to which this filter was added.
126   IPC::Channel* channel_;
127 };
128
129 GpuVideoDecodeAccelerator::GpuVideoDecodeAccelerator(
130     int32 host_route_id,
131     GpuCommandBufferStub* stub,
132     const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
133     : init_done_msg_(NULL),
134       host_route_id_(host_route_id),
135       stub_(stub),
136       texture_target_(0),
137       io_message_loop_(io_message_loop),
138       weak_factory_for_io_(this) {
139   DCHECK(stub_);
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());
145 }
146
147 GpuVideoDecodeAccelerator::~GpuVideoDecodeAccelerator() {
148   if (video_decode_accelerator_)
149     video_decode_accelerator_.release()->Destroy();
150 }
151
152 bool GpuVideoDecodeAccelerator::OnMessageReceived(const IPC::Message& msg) {
153   DCHECK(stub_);
154   if (!video_decode_accelerator_)
155     return false;
156   bool handled = true;
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()
168   return handled;
169 }
170
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);
179     return;
180   }
181   if (!Send(new AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers(
182            host_route_id_,
183            requested_num_of_buffers,
184            dimensions,
185            texture_target))) {
186     DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ProvidePictureBuffers) "
187                 << "failed";
188   }
189   texture_dimensions_ = dimensions;
190   texture_target_ = texture_target;
191 }
192
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) "
199                 << "failed";
200   }
201   DebugAutoLock auto_lock(debug_uncleared_textures_lock_);
202   uncleared_textures_.erase(picture_buffer_id);
203 }
204
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);
212   } else {
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()));
217     }
218   }
219
220   if (!Send(new AcceleratedVideoDecoderHostMsg_PictureReady(
221           host_route_id_,
222           picture.picture_buffer_id(),
223           picture.bitstream_buffer_id()))) {
224     DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_PictureReady) failed";
225   }
226 }
227
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(
234         init_done_msg_, -1);
235     if (!Send(init_done_msg_))
236       DLOG(ERROR) << "Send(init_done_msg_) failed";
237     init_done_msg_ = NULL;
238     return;
239   }
240   if (!Send(new AcceleratedVideoDecoderHostMsg_ErrorNotification(
241           host_route_id_, error))) {
242     DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ErrorNotification) "
243                 << "failed";
244   }
245 }
246
247 void GpuVideoDecodeAccelerator::Initialize(
248     const media::VideoCodecProfile profile,
249     IPC::Message* init_done_msg) {
250   DCHECK(stub_);
251   DCHECK(!video_decode_accelerator_.get());
252   DCHECK(!init_done_msg_);
253   DCHECK(init_done_msg);
254   init_done_msg_ = init_done_msg;
255
256 #if !defined(OS_WIN)
257   // Ensure we will be able to get a GL context at all before initializing
258   // non-Windows VDAs.
259   if (!make_context_current_.Run()) {
260     NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
261     return;
262   }
263 #endif
264
265 #if defined(OS_WIN)
266   if (base::win::GetVersion() < base::win::VERSION_WIN7) {
267     NOTIMPLEMENTED() << "HW video decode acceleration not available.";
268     NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
269     return;
270   }
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(),
278       this,
279       weak_factory_for_io_.GetWeakPtr(),
280       make_context_current_,
281       io_message_loop_));
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(),
286       this,
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(
298       this,
299       stub_->decoder()->AsWeakPtr(),
300       make_context_current_));
301 #else
302   NOTIMPLEMENTED() << "HW video decode acceleration not available.";
303   NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
304   return;
305 #endif
306
307   if (video_decode_accelerator_->CanDecodeOnIOThread()) {
308     filter_ = new MessageFilter(this, host_route_id_);
309     stub_->channel()->AddFilter(filter_.get());
310   }
311
312   if (!video_decode_accelerator_->Initialize(profile))
313     NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
314 }
315
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());
321   if (id < 0) {
322     DLOG(ERROR) << "BitstreamBuffer id " << id << " out of range";
323     if (child_message_loop_->BelongsToCurrentThread()) {
324       NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
325     } else {
326       child_message_loop_->PostTask(
327           FROM_HERE,
328           base::Bind(&GpuVideoDecodeAccelerator::NotifyError,
329                      base::Unretained(this),
330                      media::VideoDecodeAccelerator::INVALID_ARGUMENT));
331     }
332     return;
333   }
334   video_decode_accelerator_->Decode(media::BitstreamBuffer(id, handle, size));
335 }
336
337 void GpuVideoDecodeAccelerator::OnAssignPictureBuffers(
338     const std::vector<int32>& buffer_ids,
339     const std::vector<uint32>& texture_ids) {
340   DCHECK(stub_);
341   if (buffer_ids.size() != texture_ids.size()) {
342     NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
343     return;
344   }
345
346   gpu::gles2::GLES2Decoder* command_decoder = stub_->decoder();
347   gpu::gles2::TextureManager* texture_manager =
348       command_decoder->GetContextGroup()->texture_manager();
349
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);
356       return;
357     }
358     gpu::gles2::TextureRef* texture_ref = texture_manager->GetTexture(
359         texture_ids[i]);
360     if (!texture_ref) {
361       DLOG(ERROR) << "Failed to find texture id " << texture_ids[i];
362       NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
363       return;
364     }
365     gpu::gles2::Texture* info = texture_ref->texture();
366     if (info->target() != texture_target_) {
367       DLOG(ERROR) << "Texture target mismatch for texture id "
368                   << texture_ids[i];
369       NotifyError(media::VideoDecodeAccelerator::INVALID_ARGUMENT);
370       return;
371     }
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,
377                                     0,
378                                     0,
379                                     texture_dimensions_.width(),
380                                     texture_dimensions_.height(),
381                                     1,
382                                     0,
383                                     0,
384                                     0,
385                                     false);
386     } else {
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);
394         return;
395       }
396     }
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);
402       return;
403     }
404     buffers.push_back(media::PictureBuffer(
405         buffer_ids[i], texture_dimensions_, service_texture_id));
406     textures.push_back(texture_ref);
407   }
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];
412 }
413
414 void GpuVideoDecodeAccelerator::OnReusePictureBuffer(
415     int32 picture_buffer_id) {
416   DCHECK(video_decode_accelerator_.get());
417   video_decode_accelerator_->ReusePictureBuffer(picture_buffer_id);
418 }
419
420 void GpuVideoDecodeAccelerator::OnFlush() {
421   DCHECK(video_decode_accelerator_.get());
422   video_decode_accelerator_->Flush();
423 }
424
425 void GpuVideoDecodeAccelerator::OnReset() {
426   DCHECK(video_decode_accelerator_.get());
427   video_decode_accelerator_->Reset();
428 }
429
430 void GpuVideoDecodeAccelerator::OnDestroy() {
431   DCHECK(video_decode_accelerator_.get());
432   OnWillDestroyStub();
433 }
434
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);
439 }
440
441 void GpuVideoDecodeAccelerator::NotifyEndOfBitstreamBuffer(
442     int32 bitstream_buffer_id) {
443   if (!Send(new AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed(
444           host_route_id_, bitstream_buffer_id))) {
445     DLOG(ERROR)
446         << "Send(AcceleratedVideoDecoderHostMsg_BitstreamBufferProcessed) "
447         << "failed";
448   }
449 }
450
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;
457 }
458
459 void GpuVideoDecodeAccelerator::NotifyFlushDone() {
460   if (!Send(new AcceleratedVideoDecoderHostMsg_FlushDone(host_route_id_)))
461     DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_FlushDone) failed";
462 }
463
464 void GpuVideoDecodeAccelerator::NotifyResetDone() {
465   if (!Send(new AcceleratedVideoDecoderHostMsg_ResetDone(host_route_id_)))
466     DLOG(ERROR) << "Send(AcceleratedVideoDecoderHostMsg_ResetDone) failed";
467 }
468
469 void GpuVideoDecodeAccelerator::OnWillDestroyStub() {
470   DCHECK(stub_);
471   stub_->channel()->RemoveRoute(host_route_id_);
472   stub_->RemoveDestructionObserver(this);
473   if (filter_.get()) {
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());
477   } else {
478     delete this;
479   }
480 }
481
482 bool GpuVideoDecodeAccelerator::Send(IPC::Message* message) {
483   DCHECK(stub_);
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);
488 }
489
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
498
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);
506 }
507
508 }  // namespace content