ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / common / gpu / media / vt_video_decode_accelerator.h
blob69a8c4737db73c236389875f2cc93870f0ec167d
1 // Copyright 2014 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_VT_VIDEO_DECODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_
8 #include <stdint.h>
10 #include <map>
11 #include <queue>
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/threading/thread.h"
18 #include "base/threading/thread_checker.h"
19 #include "content/common/gpu/media/vt.h"
20 #include "media/filters/h264_parser.h"
21 #include "media/video/h264_poc.h"
22 #include "media/video/video_decode_accelerator.h"
23 #include "ui/gfx/geometry/size.h"
24 #include "ui/gl/gl_context_cgl.h"
26 namespace base {
27 class SingleThreadTaskRunner;
28 } // namespace base
30 namespace content {
32 // Preload VideoToolbox libraries, needed for sandbox warmup.
33 bool InitializeVideoToolbox();
35 // VideoToolbox.framework implementation of the VideoDecodeAccelerator
36 // interface for Mac OS X (currently limited to 10.9+).
37 class VTVideoDecodeAccelerator : public media::VideoDecodeAccelerator {
38 public:
39 explicit VTVideoDecodeAccelerator(
40 CGLContextObj cgl_context,
41 const base::Callback<bool(void)>& make_context_current);
42 ~VTVideoDecodeAccelerator() override;
44 // VideoDecodeAccelerator implementation.
45 bool Initialize(media::VideoCodecProfile profile, Client* client) override;
46 void Decode(const media::BitstreamBuffer& bitstream) override;
47 void AssignPictureBuffers(
48 const std::vector<media::PictureBuffer>& pictures) override;
49 void ReusePictureBuffer(int32_t picture_id) override;
50 void Flush() override;
51 void Reset() override;
52 void Destroy() override;
53 bool CanDecodeOnIOThread() override;
55 // Called by OutputThunk() when VideoToolbox finishes decoding a frame.
56 void Output(
57 void* source_frame_refcon,
58 OSStatus status,
59 CVImageBufferRef image_buffer);
61 private:
62 // Logged to UMA, so never reuse values. Make sure to update
63 // VTVDASessionFailureType in histograms.xml to match.
64 enum VTVDASessionFailureType {
65 SFT_SUCCESSFULLY_INITIALIZED = 0,
66 SFT_PLATFORM_ERROR = 1,
67 SFT_INVALID_STREAM = 2,
68 SFT_UNSUPPORTED_STREAM_PARAMETERS = 3,
69 SFT_DECODE_ERROR = 4,
70 SFT_UNSUPPORTED_STREAM = 5,
71 // Must always be equal to largest entry logged.
72 SFT_MAX = SFT_UNSUPPORTED_STREAM
75 enum State {
76 STATE_DECODING,
77 STATE_ERROR,
78 STATE_DESTROYING,
81 enum TaskType {
82 TASK_FRAME,
83 TASK_FLUSH,
84 TASK_RESET,
85 TASK_DESTROY,
88 struct Frame {
89 Frame(int32_t bitstream_id);
90 ~Frame();
92 // ID of the bitstream buffer this Frame will be decoded from.
93 int32_t bitstream_id;
95 // Relative presentation order of this frame (see AVC spec).
96 int32_t pic_order_cnt;
98 // Nnumber of frames after this one in decode order that can appear before
99 // before it in presentation order.
100 int32_t reorder_window;
102 // Size of the decoded frame.
103 // TODO(sandersd): visible_rect.
104 gfx::Size coded_size;
106 // VideoToolbox decoded image, if decoding was successful.
107 base::ScopedCFTypeRef<CVImageBufferRef> image;
110 struct Task {
111 Task(TaskType type);
112 ~Task();
114 TaskType type;
115 linked_ptr<Frame> frame;
119 // Methods for interacting with VideoToolbox. Run on |decoder_thread_|.
122 // Compute the |pic_order_cnt| for a frame. Returns true or calls
123 // NotifyError() before returning false.
124 bool ComputePicOrderCnt(
125 const media::H264SPS* sps,
126 const media::H264SliceHeader& slice_hdr,
127 Frame* frame);
129 // Set up VideoToolbox using the current SPS and PPS. Returns true or calls
130 // NotifyError() before returning false.
131 bool ConfigureDecoder();
133 // Wait for VideoToolbox to output all pending frames. Returns true or calls
134 // NotifyError() before returning false.
135 bool FinishDelayedFrames();
137 // |frame| is owned by |pending_frames_|.
138 void DecodeTask(const media::BitstreamBuffer&, Frame* frame);
139 void DecodeDone(Frame* frame);
142 // Methods for interacting with |client_|. Run on |gpu_task_runner_|.
144 void NotifyError(
145 Error vda_error_type,
146 VTVDASessionFailureType session_failure_type);
148 // |type| is the type of task that the flush will complete, one of TASK_FLUSH,
149 // TASK_RESET, or TASK_DESTROY.
150 void QueueFlush(TaskType type);
151 void FlushTask(TaskType type);
152 void FlushDone(TaskType type);
154 // Try to make progress on tasks in the |task_queue_| or sending frames in the
155 // |reorder_queue_|.
156 void ProcessWorkQueues();
158 // These methods returns true if a task was completed, false otherwise.
159 bool ProcessTaskQueue();
160 bool ProcessReorderQueue();
161 bool ProcessFrame(const Frame& frame);
162 bool SendFrame(const Frame& frame);
165 // GPU thread state.
167 CGLContextObj cgl_context_;
168 base::Callback<bool(void)> make_context_current_;
169 media::VideoDecodeAccelerator::Client* client_;
170 State state_;
172 // Queue of pending flush tasks. This is used to drop frames when a reset
173 // is pending.
174 std::queue<TaskType> pending_flush_tasks_;
176 // Queue of tasks to complete in the GPU thread.
177 std::queue<Task> task_queue_;
179 // Utility class to define the order of frames in the reorder queue.
180 struct FrameOrder {
181 bool operator()(
182 const linked_ptr<Frame>& lhs,
183 const linked_ptr<Frame>& rhs) const;
186 // Queue of decoded frames in presentation order.
187 std::priority_queue<linked_ptr<Frame>,
188 std::vector<linked_ptr<Frame>>,
189 FrameOrder> reorder_queue_;
191 // Size of assigned picture buffers.
192 gfx::Size picture_size_;
194 // Frames that have not yet been decoded, keyed by bitstream ID; maintains
195 // ownership of Frame objects while they flow through VideoToolbox.
196 std::map<int32_t, linked_ptr<Frame>> pending_frames_;
198 // Set of assigned bitstream IDs, so that Destroy() can release them all.
199 std::set<int32_t> assigned_bitstream_ids_;
201 // All picture buffers assigned to us. Used to check if reused picture buffers
202 // should be added back to the available list or released. (They are not
203 // released immediately because we need the reuse event to free the binding.)
204 std::set<int32_t> assigned_picture_ids_;
206 // Texture IDs of assigned pictures.
207 std::map<int32_t, uint32_t> texture_ids_;
209 // Pictures ready to be rendered to.
210 std::vector<int32_t> available_picture_ids_;
212 // Image buffers kept alive while they are bound to pictures.
213 std::map<int32_t, base::ScopedCFTypeRef<CVImageBufferRef>> picture_bindings_;
216 // Decoder thread state.
218 VTDecompressionOutputCallbackRecord callback_;
219 base::ScopedCFTypeRef<CMFormatDescriptionRef> format_;
220 base::ScopedCFTypeRef<VTDecompressionSessionRef> session_;
221 media::H264Parser parser_;
222 gfx::Size coded_size_;
224 int last_sps_id_;
225 std::vector<uint8_t> last_sps_;
226 std::vector<uint8_t> last_spsext_;
227 int last_pps_id_;
228 std::vector<uint8_t> last_pps_;
229 media::H264POC poc_;
232 // Shared state (set up and torn down on GPU thread).
234 scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner_;
235 base::ThreadChecker gpu_thread_checker_;
236 base::WeakPtr<VTVideoDecodeAccelerator> weak_this_;
237 base::Thread decoder_thread_;
239 // Declared last to ensure that all weak pointers are invalidated before
240 // other destructors run.
241 base::WeakPtrFactory<VTVideoDecodeAccelerator> weak_this_factory_;
243 DISALLOW_COPY_AND_ASSIGN(VTVideoDecodeAccelerator);
246 } // namespace content
248 #endif // CONTENT_COMMON_GPU_MEDIA_VT_VIDEO_DECODE_ACCELERATOR_H_