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 #ifndef CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_
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/geometry/size.h"
23 class MessageLoopProxy
;
28 class GpuVideoDecodeAccelerator
29 : public IPC::Listener
,
31 public media::VideoDecodeAccelerator::Client
,
32 public GpuCommandBufferStub::DestructionObserver
{
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(
39 GpuCommandBufferStub
* stub
,
40 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
);
42 // IPC::Listener implementation.
43 bool OnMessageReceived(const IPC::Message
& message
) override
;
45 // media::VideoDecodeAccelerator::Client implementation.
46 void ProvidePictureBuffers(uint32 requested_num_of_buffers
,
47 const gfx::Size
& dimensions
,
48 uint32 texture_target
) override
;
49 void DismissPictureBuffer(int32 picture_buffer_id
) override
;
50 void PictureReady(const media::Picture
& picture
) override
;
51 void NotifyError(media::VideoDecodeAccelerator::Error error
) override
;
52 void NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id
) override
;
53 void NotifyFlushDone() override
;
54 void NotifyResetDone() override
;
56 // GpuCommandBufferStub::DestructionObserver implementation.
57 void OnWillDestroyStub() override
;
59 // Function to delegate sending to actual sender.
60 bool Send(IPC::Message
* message
) override
;
62 // Initialize VDAs from the set of VDAs supported for current platform until
63 // one of them succeeds for given |profile|. Send the |init_done_msg| when
64 // done. filter_ is passed to GpuCommandBufferStub channel only if the chosen
65 // VDA can decode on IO thread.
66 void Initialize(const media::VideoCodecProfile profile
,
67 IPC::Message
* init_done_msg
);
70 typedef scoped_ptr
<media::VideoDecodeAccelerator
>(
71 GpuVideoDecodeAccelerator::*CreateVDAFp
)();
75 // Return a set of VDA Create function pointers applicable to the current
77 std::vector
<CreateVDAFp
> CreateVDAFps();
78 scoped_ptr
<media::VideoDecodeAccelerator
> CreateDXVAVDA();
79 scoped_ptr
<media::VideoDecodeAccelerator
> CreateV4L2VDA();
80 scoped_ptr
<media::VideoDecodeAccelerator
> CreateV4L2SliceVDA();
81 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVaapiVDA();
82 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVTVDA();
83 scoped_ptr
<media::VideoDecodeAccelerator
> CreateOzoneVDA();
84 scoped_ptr
<media::VideoDecodeAccelerator
> CreateAndroidVDA();
86 // We only allow self-delete, from OnWillDestroyStub(), after cleanup there.
87 ~GpuVideoDecodeAccelerator() override
;
89 // Handlers for IPC messages.
90 void OnDecode(base::SharedMemoryHandle handle
, int32 id
, uint32 size
);
91 void OnAssignPictureBuffers(const std::vector
<int32
>& buffer_ids
,
92 const std::vector
<uint32
>& texture_ids
);
93 void OnReusePictureBuffer(int32 picture_buffer_id
);
98 // Called on IO thread when |filter_| has been removed.
99 void OnFilterRemoved();
101 // Sets the texture to cleared.
102 void SetTextureCleared(const media::Picture
& picture
);
104 // Helper for replying to the creation request.
105 void SendCreateDecoderReply(IPC::Message
* message
, bool succeeded
);
107 // Helper to bind |image| to the texture specified by |client_texture_id|.
108 void BindImage(uint32 client_texture_id
,
109 uint32 texture_target
,
110 scoped_refptr
<gfx::GLImage
> image
);
112 // Route ID to communicate with the host.
113 int32 host_route_id_
;
115 // Unowned pointer to the underlying GpuCommandBufferStub. |this| is
116 // registered as a DestuctionObserver of |stub_| and will self-delete when
117 // |stub_| is destroyed.
118 GpuCommandBufferStub
* stub_
;
120 // The underlying VideoDecodeAccelerator.
121 scoped_ptr
<media::VideoDecodeAccelerator
> video_decode_accelerator_
;
123 // Callback for making the relevant context current for GL calls.
124 // Returns false if failed.
125 base::Callback
<bool(void)> make_context_current_
;
127 // The texture dimensions as requested by ProvidePictureBuffers().
128 gfx::Size texture_dimensions_
;
130 // The texture target as requested by ProvidePictureBuffers().
131 uint32 texture_target_
;
133 // The message filter to run VDA::Decode on IO thread if VDA supports it.
134 scoped_refptr
<MessageFilter
> filter_
;
136 // Used to wait on for |filter_| to be removed, before we can safely
138 base::WaitableEvent filter_removed_
;
140 // GPU child message loop.
141 scoped_refptr
<base::MessageLoopProxy
> child_message_loop_
;
143 // GPU IO message loop.
144 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
146 // Weak pointers will be invalidated on IO thread.
147 base::WeakPtrFactory
<Client
> weak_factory_for_io_
;
149 // Protects |uncleared_textures_| when DCHECK is on. This is for debugging
150 // only. We don't want to hold a lock on IO thread. When DCHECK is off,
151 // |uncleared_textures_| is only accessed from the child thread.
152 base::Lock debug_uncleared_textures_lock_
;
154 // A map from picture buffer ID to TextureRef that have not been cleared.
155 std::map
<int32
, scoped_refptr
<gpu::gles2::TextureRef
> > uncleared_textures_
;
157 DISALLOW_IMPLICIT_CONSTRUCTORS(GpuVideoDecodeAccelerator
);
160 } // namespace content
162 #endif // CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_