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 "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"
24 class MessageLoopProxy;
29 class GpuVideoDecodeAccelerator
30 : public IPC::Listener,
32 public media::VideoDecodeAccelerator::Client,
33 public GpuCommandBufferStub::DestructionObserver {
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(
40 GpuCommandBufferStub* stub,
41 const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
43 // IPC::Listener implementation.
44 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
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;
58 // GpuCommandBufferStub::DestructionObserver implementation.
59 virtual void OnWillDestroyStub() OVERRIDE;
61 // Function to delegate sending to actual sender.
62 virtual bool Send(IPC::Message* message) OVERRIDE;
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);
74 // We only allow self-delete, from OnWillDestroyStub(), after cleanup there.
75 virtual ~GpuVideoDecodeAccelerator();
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);
86 // Called on IO thread when |filter_| has been removed.
87 void OnFilterRemoved();
89 // Sets the texture to cleared.
90 void SetTextureCleared(const media::Picture& picture);
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_;
97 // Route ID to communicate with the host.
100 // Unowned pointer to the underlying GpuCommandBufferStub.
101 GpuCommandBufferStub* stub_;
103 // The underlying VideoDecodeAccelerator.
104 scoped_ptr<VideoDecodeAcceleratorImpl> video_decode_accelerator_;
106 // Callback for making the relevant context current for GL calls.
107 // Returns false if failed.
108 base::Callback<bool(void)> make_context_current_;
110 // The texture dimensions as requested by ProvidePictureBuffers().
111 gfx::Size texture_dimensions_;
113 // The texture target as requested by ProvidePictureBuffers().
114 uint32 texture_target_;
116 // The message filter to run VDA::Decode on IO thread if VDA supports it.
117 scoped_refptr<MessageFilter> filter_;
119 // Used to wait on for |filter_| to be removed, before we can safely
121 base::WaitableEvent filter_removed_;
123 // GPU child message loop.
124 scoped_refptr<base::MessageLoopProxy> child_message_loop_;
126 // GPU IO message loop.
127 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
129 // Weak pointers will be invalidated on IO thread.
130 base::WeakPtrFactory<Client> weak_factory_for_io_;
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_;
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_;
140 DISALLOW_IMPLICIT_CONSTRUCTORS(GpuVideoDecodeAccelerator);
143 } // namespace content
145 #endif // CONTENT_COMMON_GPU_MEDIA_GPU_VIDEO_DECODE_ACCELERATOR_H_