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