- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / gpu_video_decode_accelerator.h
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 "content/common/gpu/gpu_command_buffer_stub.h"
15 #include "content/common/gpu/media/video_decode_accelerator_impl.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
22 namespace base {
23 class MessageLoopProxy;
24 }
25
26 namespace content {
27
28 class 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   virtual ~GpuVideoDecodeAccelerator();
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   // Handlers for IPC messages.
75   void OnDecode(base::SharedMemoryHandle handle, int32 id, uint32 size);
76   void OnAssignPictureBuffers(const std::vector<int32>& buffer_ids,
77                               const std::vector<uint32>& texture_ids);
78   void OnReusePictureBuffer(int32 picture_buffer_id);
79   void OnFlush();
80   void OnReset();
81   void OnDestroy();
82
83   // Called on IO thread when |filter_| has been removed.
84   void OnFilterRemoved();
85
86   // Sets the texture to cleared.
87   void SetTextureCleared(const media::Picture& picture);
88
89   // Message to Send() when initialization is done.  Is only non-NULL during
90   // initialization and is owned by the IPC channel underlying the
91   // GpuCommandBufferStub.
92   IPC::Message* init_done_msg_;
93
94   // Route ID to communicate with the host.
95   int32 host_route_id_;
96
97   // Unowned pointer to the underlying GpuCommandBufferStub.
98   GpuCommandBufferStub* stub_;
99
100   // The underlying VideoDecodeAccelerator.
101   scoped_ptr<VideoDecodeAcceleratorImpl> video_decode_accelerator_;
102
103   // Callback for making the relevant context current for GL calls.
104   // Returns false if failed.
105   base::Callback<bool(void)> make_context_current_;
106
107   // The texture dimensions as requested by ProvidePictureBuffers().
108   gfx::Size texture_dimensions_;
109
110   // The texture target as requested by ProvidePictureBuffers().
111   uint32 texture_target_;
112
113   // The message filter to run VDA::Decode on IO thread if VDA supports it.
114   scoped_refptr<MessageFilter> filter_;
115
116   // GPU child message loop.
117   scoped_refptr<base::MessageLoopProxy> child_message_loop_;
118
119   // GPU IO message loop.
120   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
121
122   // Weak pointers will be invalidated on IO thread.
123   base::WeakPtrFactory<Client> weak_factory_for_io_;
124
125   // Protects |uncleared_textures_| when DCHECK is on. This is for debugging
126   // only. We don't want to hold a lock on IO thread. When DCHECK is off,
127   // |uncleared_textures_| is only accessed from the child thread.
128   base::Lock debug_uncleared_textures_lock_;
129
130   // A map from picture buffer ID to TextureRef that have not been cleared.
131   std::map<int32, scoped_refptr<gpu::gles2::TextureRef> > uncleared_textures_;
132
133   DISALLOW_IMPLICIT_CONSTRUCTORS(GpuVideoDecodeAccelerator);
134 };
135
136 }  // namespace content
137
138 #endif  // CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_