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 "gpu/config/gpu_info.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/geometry/size.h"
25 class GpuVideoDecodeAccelerator
26 : public IPC::Listener
,
28 public media::VideoDecodeAccelerator::Client
,
29 public GpuCommandBufferStub::DestructionObserver
{
31 // Each of the arguments to the constructor must outlive this object.
32 // |stub->decoder()| will be made current around any operation that touches
33 // the underlying VDA so that it can make GL calls safely.
34 GpuVideoDecodeAccelerator(
36 GpuCommandBufferStub
* stub
,
37 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_task_runner
);
39 // IPC::Listener implementation.
40 bool OnMessageReceived(const IPC::Message
& message
) override
;
42 // media::VideoDecodeAccelerator::Client implementation.
43 void ProvidePictureBuffers(uint32 requested_num_of_buffers
,
44 const gfx::Size
& dimensions
,
45 uint32 texture_target
) override
;
46 void DismissPictureBuffer(int32 picture_buffer_id
) override
;
47 void PictureReady(const media::Picture
& picture
) override
;
48 void NotifyError(media::VideoDecodeAccelerator::Error error
) override
;
49 void NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id
) override
;
50 void NotifyFlushDone() override
;
51 void NotifyResetDone() override
;
53 // GpuCommandBufferStub::DestructionObserver implementation.
54 void OnWillDestroyStub() override
;
56 // Function to delegate sending to actual sender.
57 bool Send(IPC::Message
* message
) override
;
59 // Initialize VDAs from the set of VDAs supported for current platform until
60 // one of them succeeds for given |profile|. Send the |init_done_msg| when
61 // done. filter_ is passed to GpuCommandBufferStub channel only if the chosen
62 // VDA can decode on IO thread.
63 void Initialize(const media::VideoCodecProfile profile
,
64 IPC::Message
* init_done_msg
);
66 // Static query for supported profiles. This query calls the appropriate
67 // platform-specific version. The returned supported profiles vector will
68 // not contain duplicates.
69 static gpu::VideoDecodeAcceleratorSupportedProfiles
GetSupportedProfiles();
72 typedef scoped_ptr
<media::VideoDecodeAccelerator
>(
73 GpuVideoDecodeAccelerator::*CreateVDAFp
)();
77 scoped_ptr
<media::VideoDecodeAccelerator
> CreateDXVAVDA();
78 scoped_ptr
<media::VideoDecodeAccelerator
> CreateV4L2VDA();
79 scoped_ptr
<media::VideoDecodeAccelerator
> CreateV4L2SliceVDA();
80 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVaapiVDA();
81 scoped_ptr
<media::VideoDecodeAccelerator
> CreateVTVDA();
82 scoped_ptr
<media::VideoDecodeAccelerator
> CreateOzoneVDA();
83 scoped_ptr
<media::VideoDecodeAccelerator
> CreateAndroidVDA();
85 // We only allow self-delete, from OnWillDestroyStub(), after cleanup there.
86 ~GpuVideoDecodeAccelerator() override
;
88 // Handlers for IPC messages.
89 void OnDecode(base::SharedMemoryHandle handle
,
92 base::TimeDelta presentation_timestamp
);
93 void OnAssignPictureBuffers(const std::vector
<int32
>& buffer_ids
,
94 const std::vector
<uint32
>& texture_ids
);
95 void OnReusePictureBuffer(int32 picture_buffer_id
);
100 // Called on IO thread when |filter_| has been removed.
101 void OnFilterRemoved();
103 // Sets the texture to cleared.
104 void SetTextureCleared(const media::Picture
& picture
);
106 // Helper for replying to the creation request.
107 void SendCreateDecoderReply(IPC::Message
* message
, bool succeeded
);
109 // Helper to bind |image| to the texture specified by |client_texture_id|.
110 void BindImage(uint32 client_texture_id
,
111 uint32 texture_target
,
112 scoped_refptr
<gfx::GLImage
> image
);
114 // Route ID to communicate with the host.
115 const int32 host_route_id_
;
117 // Unowned pointer to the underlying GpuCommandBufferStub. |this| is
118 // registered as a DestuctionObserver of |stub_| and will self-delete when
119 // |stub_| is destroyed.
120 GpuCommandBufferStub
* const stub_
;
122 // The underlying VideoDecodeAccelerator.
123 scoped_ptr
<media::VideoDecodeAccelerator
> video_decode_accelerator_
;
125 // Callback for making the relevant context current for GL calls.
126 // Returns false if failed.
127 base::Callback
<bool(void)> make_context_current_
;
129 // The texture dimensions as requested by ProvidePictureBuffers().
130 gfx::Size texture_dimensions_
;
132 // The texture target as requested by ProvidePictureBuffers().
133 uint32 texture_target_
;
135 // The message filter to run VDA::Decode on IO thread if VDA supports it.
136 scoped_refptr
<MessageFilter
> filter_
;
138 // Used to wait on for |filter_| to be removed, before we can safely
140 base::WaitableEvent filter_removed_
;
142 // GPU child thread task runner.
143 const scoped_refptr
<base::SingleThreadTaskRunner
> child_task_runner_
;
145 // GPU IO thread task runner.
146 const scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
148 // Weak pointers will be invalidated on IO thread.
149 base::WeakPtrFactory
<Client
> weak_factory_for_io_
;
151 // Protects |uncleared_textures_| when DCHECK is on. This is for debugging
152 // only. We don't want to hold a lock on IO thread. When DCHECK is off,
153 // |uncleared_textures_| is only accessed from the child thread.
154 base::Lock debug_uncleared_textures_lock_
;
156 // A map from picture buffer ID to TextureRef that have not been cleared.
157 std::map
<int32
, scoped_refptr
<gpu::gles2::TextureRef
> > uncleared_textures_
;
159 DISALLOW_IMPLICIT_CONSTRUCTORS(GpuVideoDecodeAccelerator
);
162 } // namespace content
164 #endif // CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_