Reland r246152 cros: Change how multi-profile is enabled.
[chromium-blink-merge.git] / media / filters / gpu_video_decoder.h
blob961e25ae3d9317205a1346f8baa69ecdce0a63a0
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 MEDIA_FILTERS_GPU_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_GPU_VIDEO_DECODER_H_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <utility>
12 #include <vector>
14 #include "base/memory/weak_ptr.h"
15 #include "media/base/pipeline_status.h"
16 #include "media/base/video_decoder.h"
17 #include "media/video/video_decode_accelerator.h"
19 template <class T> class scoped_refptr;
21 namespace base {
22 class SharedMemory;
23 class SingleThreadTaskRunner;
26 namespace media {
28 class DecoderBuffer;
29 class GpuVideoAcceleratorFactories;
30 class MediaLog;
32 // GPU-accelerated video decoder implementation. Relies on
33 // AcceleratedVideoDecoderMsg_Decode and friends. Can be created on any thread
34 // but must be accessed and destroyed on GpuVideoAcceleratorFactories's
35 // GetMessageLoop().
36 class MEDIA_EXPORT GpuVideoDecoder
37 : public VideoDecoder,
38 public VideoDecodeAccelerator::Client {
39 public:
40 explicit GpuVideoDecoder(
41 const scoped_refptr<GpuVideoAcceleratorFactories>& factories,
42 const scoped_refptr<MediaLog>& media_log);
44 // VideoDecoder implementation.
45 virtual void Initialize(const VideoDecoderConfig& config,
46 const PipelineStatusCB& status_cb) OVERRIDE;
47 virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
48 const DecodeCB& decode_cb) OVERRIDE;
49 virtual void Reset(const base::Closure& closure) OVERRIDE;
50 virtual void Stop(const base::Closure& closure) OVERRIDE;
51 virtual bool HasAlpha() const OVERRIDE;
52 virtual bool NeedsBitstreamConversion() const OVERRIDE;
53 virtual bool CanReadWithoutStalling() const OVERRIDE;
55 // VideoDecodeAccelerator::Client implementation.
56 virtual void NotifyInitializeDone() OVERRIDE;
57 virtual void ProvidePictureBuffers(uint32 count,
58 const gfx::Size& size,
59 uint32 texture_target) OVERRIDE;
60 virtual void DismissPictureBuffer(int32 id) OVERRIDE;
61 virtual void PictureReady(const media::Picture& picture) OVERRIDE;
62 virtual void NotifyEndOfBitstreamBuffer(int32 id) OVERRIDE;
63 virtual void NotifyFlushDone() OVERRIDE;
64 virtual void NotifyResetDone() OVERRIDE;
65 virtual void NotifyError(media::VideoDecodeAccelerator::Error error) OVERRIDE;
67 protected:
68 virtual ~GpuVideoDecoder();
70 private:
71 enum State {
72 kNormal,
73 kDrainingDecoder,
74 kDecoderDrained,
75 kError
78 // A shared memory segment and its allocated size.
79 struct SHMBuffer {
80 SHMBuffer(base::SharedMemory* m, size_t s);
81 ~SHMBuffer();
82 base::SharedMemory* shm;
83 size_t size;
86 // A SHMBuffer and the DecoderBuffer its data came from.
87 struct BufferPair {
88 BufferPair(SHMBuffer* s, const scoped_refptr<DecoderBuffer>& b);
89 ~BufferPair();
90 SHMBuffer* shm_buffer;
91 scoped_refptr<DecoderBuffer> buffer;
94 typedef std::map<int32, PictureBuffer> PictureBufferMap;
96 // Return true if more decode work can be piled on to the VDA.
97 bool CanMoreDecodeWorkBeDone();
99 // Enqueue a frame for later delivery (or drop it on the floor if a
100 // vda->Reset() is in progress) and trigger out-of-line delivery of the oldest
101 // ready frame to the client if there is a pending read. A NULL |frame|
102 // merely triggers delivery, and requires the ready_video_frames_ queue not be
103 // empty.
104 void EnqueueFrameAndTriggerFrameDelivery(
105 const scoped_refptr<VideoFrame>& frame);
107 // Indicate the picture buffer can be reused by the decoder.
108 void ReusePictureBuffer(int64 picture_buffer_id, uint32 sync_point);
110 void RecordBufferData(
111 const BitstreamBuffer& bitstream_buffer, const DecoderBuffer& buffer);
112 void GetBufferData(int32 id, base::TimeDelta* timetamp,
113 gfx::Rect* visible_rect, gfx::Size* natural_size);
115 void DestroyVDA();
117 // Request a shared-memory segment of at least |min_size| bytes. Will
118 // allocate as necessary. Caller does not own returned pointer.
119 SHMBuffer* GetSHM(size_t min_size);
121 // Return a shared-memory segment to the available pool.
122 void PutSHM(SHMBuffer* shm_buffer);
124 // Destroy all PictureBuffers in |buffers|, and delete their textures.
125 void DestroyPictureBuffers(PictureBufferMap* buffers);
127 // Assert the contract that this class is operated on the right thread.
128 void DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent() const;
130 bool needs_bitstream_conversion_;
132 // Bound to factories_->GetMessageLoop().
133 base::WeakPtrFactory<GpuVideoDecoder> weak_factory_;
134 base::WeakPtr<GpuVideoDecoder> weak_this_;
136 scoped_refptr<GpuVideoAcceleratorFactories> factories_;
138 // Populated during Initialize() (on success) and unchanged until an error
139 // occurs.
140 scoped_ptr<VideoDecodeAccelerator> vda_;
142 // Callbacks that are !is_null() only during their respective operation being
143 // asynchronously executed.
144 DecodeCB pending_decode_cb_;
145 base::Closure pending_reset_cb_;
147 State state_;
149 VideoDecoderConfig config_;
151 // Shared-memory buffer pool. Since allocating SHM segments requires a
152 // round-trip to the browser process, we keep allocation out of the
153 // steady-state of the decoder.
154 std::vector<SHMBuffer*> available_shm_segments_;
156 scoped_refptr<MediaLog> media_log_;
158 std::map<int32, BufferPair> bitstream_buffers_in_decoder_;
159 PictureBufferMap assigned_picture_buffers_;
160 PictureBufferMap dismissed_picture_buffers_;
161 // PictureBuffers given to us by VDA via PictureReady, which we sent forward
162 // as VideoFrames to be rendered via decode_cb_, and which will be returned
163 // to us via ReusePictureBuffer.
164 std::set<int32> picture_buffers_at_display_;
166 // The texture target used for decoded pictures.
167 uint32 decoder_texture_target_;
169 struct BufferData {
170 BufferData(int32 bbid, base::TimeDelta ts, const gfx::Rect& visible_rect,
171 const gfx::Size& natural_size);
172 ~BufferData();
173 int32 bitstream_buffer_id;
174 base::TimeDelta timestamp;
175 gfx::Rect visible_rect;
176 gfx::Size natural_size;
178 std::list<BufferData> input_buffer_data_;
180 // picture_buffer_id and the frame wrapping the corresponding Picture, for
181 // frames that have been decoded but haven't been requested by a Decode() yet.
182 std::list<scoped_refptr<VideoFrame> > ready_video_frames_;
183 int32 next_picture_buffer_id_;
184 int32 next_bitstream_buffer_id_;
186 // Set during ProvidePictureBuffers(), used for checking and implementing
187 // HasAvailableOutputFrames().
188 int available_pictures_;
190 DISALLOW_COPY_AND_ASSIGN(GpuVideoDecoder);
193 } // namespace media
195 #endif // MEDIA_FILTERS_GPU_VIDEO_DECODER_H_