Home | History | Annotate | Download | only in media
      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 #ifndef CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_
      6 #define CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/shared_memory.h"
     14 #include "base/synchronization/waitable_event.h"
     15 #include "content/common/gpu/gpu_command_buffer_stub.h"
     16 #include "content/common/gpu/media/video_decode_accelerator_impl.h"
     17 #include "gpu/command_buffer/service/texture_manager.h"
     18 #include "ipc/ipc_listener.h"
     19 #include "ipc/ipc_sender.h"
     20 #include "media/video/video_decode_accelerator.h"
     21 #include "ui/gfx/size.h"
     22 
     23 namespace base {
     24 class MessageLoopProxy;
     25 }
     26 
     27 namespace content {
     28 
     29 class GpuVideoDecodeAccelerator
     30     : public IPC::Listener,
     31       public IPC::Sender,
     32       public media::VideoDecodeAccelerator::Client,
     33       public GpuCommandBufferStub::DestructionObserver {
     34  public:
     35   // Each of the arguments to the constructor must outlive this object.
     36   // |stub->decoder()| will be made current around any operation that touches
     37   // the underlying VDA so that it can make GL calls safely.
     38   GpuVideoDecodeAccelerator(
     39       int32 host_route_id,
     40       GpuCommandBufferStub* stub,
     41       const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
     42 
     43   // IPC::Listener implementation.
     44   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     45 
     46   // media::VideoDecodeAccelerator::Client implementation.
     47   virtual void ProvidePictureBuffers(uint32 requested_num_of_buffers,
     48                                      const gfx::Size& dimensions,
     49                                      uint32 texture_target) OVERRIDE;
     50   virtual void DismissPictureBuffer(int32 picture_buffer_id) OVERRIDE;
     51   virtual void PictureReady(const media::Picture& picture) OVERRIDE;
     52   virtual void NotifyInitializeDone() OVERRIDE;
     53   virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE;
     54   virtual void NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id) OVERRIDE;
     55   virtual void NotifyFlushDone() OVERRIDE;
     56   virtual void NotifyResetDone() OVERRIDE;
     57 
     58   // GpuCommandBufferStub::DestructionObserver implementation.
     59   virtual void OnWillDestroyStub() OVERRIDE;
     60 
     61   // Function to delegate sending to actual sender.
     62   virtual bool Send(IPC::Message* message) OVERRIDE;
     63 
     64   // Initialize the accelerator with the given profile and send the
     65   // |init_done_msg| when done.
     66   // The renderer process handle is valid as long as we have a channel between
     67   // GPU process and the renderer.
     68   void Initialize(const media::VideoCodecProfile profile,
     69                   IPC::Message* init_done_msg);
     70 
     71  private:
     72   class MessageFilter;
     73 
     74   // We only allow self-delete, from OnWillDestroyStub(), after cleanup there.
     75   virtual ~GpuVideoDecodeAccelerator();
     76 
     77   // Handlers for IPC messages.
     78   void OnDecode(base::SharedMemoryHandle handle, int32 id, uint32 size);
     79   void OnAssignPictureBuffers(const std::vector<int32>& buffer_ids,
     80                               const std::vector<uint32>& texture_ids);
     81   void OnReusePictureBuffer(int32 picture_buffer_id);
     82   void OnFlush();
     83   void OnReset();
     84   void OnDestroy();
     85 
     86   // Called on IO thread when |filter_| has been removed.
     87   void OnFilterRemoved();
     88 
     89   // Sets the texture to cleared.
     90   void SetTextureCleared(const media::Picture& picture);
     91 
     92   // Message to Send() when initialization is done.  Is only non-NULL during
     93   // initialization and is owned by the IPC channel underlying the
     94   // GpuCommandBufferStub.
     95   IPC::Message* init_done_msg_;
     96 
     97   // Route ID to communicate with the host.
     98   int32 host_route_id_;
     99 
    100   // Unowned pointer to the underlying GpuCommandBufferStub.
    101   GpuCommandBufferStub* stub_;
    102 
    103   // The underlying VideoDecodeAccelerator.
    104   scoped_ptr<VideoDecodeAcceleratorImpl> video_decode_accelerator_;
    105 
    106   // Callback for making the relevant context current for GL calls.
    107   // Returns false if failed.
    108   base::Callback<bool(void)> make_context_current_;
    109 
    110   // The texture dimensions as requested by ProvidePictureBuffers().
    111   gfx::Size texture_dimensions_;
    112 
    113   // The texture target as requested by ProvidePictureBuffers().
    114   uint32 texture_target_;
    115 
    116   // The message filter to run VDA::Decode on IO thread if VDA supports it.
    117   scoped_refptr<MessageFilter> filter_;
    118 
    119   // Used to wait on for |filter_| to be removed, before we can safely
    120   // destroy the VDA.
    121   base::WaitableEvent filter_removed_;
    122 
    123   // GPU child message loop.
    124   scoped_refptr<base::MessageLoopProxy> child_message_loop_;
    125 
    126   // GPU IO message loop.
    127   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
    128 
    129   // Weak pointers will be invalidated on IO thread.
    130   base::WeakPtrFactory<Client> weak_factory_for_io_;
    131 
    132   // Protects |uncleared_textures_| when DCHECK is on. This is for debugging
    133   // only. We don't want to hold a lock on IO thread. When DCHECK is off,
    134   // |uncleared_textures_| is only accessed from the child thread.
    135   base::Lock debug_uncleared_textures_lock_;
    136 
    137   // A map from picture buffer ID to TextureRef that have not been cleared.
    138   std::map<int32, scoped_refptr<gpu::gles2::TextureRef> > uncleared_textures_;
    139 
    140   DISALLOW_IMPLICIT_CONSTRUCTORS(GpuVideoDecodeAccelerator);
    141 };
    142 
    143 }  // namespace content
    144 
    145 #endif  // CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_
    146