Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: content/common/gpu/media/exynos_video_decode_accelerator.h

Issue 11198060: VDA implementation for Exynos, using V4L2 (Closed) Base URL: https://git.chromium.org/git/chromium/src@git-svn
Patch Set: And one more. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4 //
5 // This file contains an implementation of VideoDecoderAccelerator
Ami GONE FROM CHROMIUM 2012/10/31 01:06:50 OOC how does perf of this VDA compare to OVDA on e
sheu 2012/11/01 02:16:08 I haven't tuned it, but it's looking to be about 5
6 // that utilizes hardware video decoder present on the Exynos SoC.
7
8 #ifndef CONTENT_COMMON_GPU_MEDIA_EXYNOS_VIDEO_DECODE_ACCELERATOR_H_
9 #define CONTENT_COMMON_GPU_MEDIA_EXYNOS_VIDEO_DECODE_ACCELERATOR_H_
10
11 #include <deque>
12 #include <vector>
13
14 #include "base/callback_forward.h"
15 #include "base/file_util.h"
16 #include "base/memory/linked_ptr.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/threading/thread.h"
19 #include "content/common/content_export.h"
20 #include "media/base/video_decoder_config.h"
21 #include "media/video/video_decode_accelerator.h"
22 #include "third_party/angle/include/EGL/egl.h"
23 #include "third_party/angle/include/EGL/eglext.h"
24 #include "ui/gfx/size.h"
25
26 namespace base {
27 class MessageLoopProxy;
28 }
29
30 namespace gfx {
31 class GLContext;
32 }
33
34 namespace content {
35
36 // This class handls Exynos video acceleration directly through the V4L2 devices
37 // exported by the Multi Format Codec and GScaler hardware blocks.
38 //
39 // The threading model of this class is driven by the fact that it needs to
40 // interface two fundamentally different event queues -- the one Chromium
41 // provides through MessageLoop, and the one driven by the V4L2 devices which
42 // is waited on with epoll(). There are three threads involved in this class:
43 //
44 // * The child thread, which is the main GPU process thread which calls the
45 // media::VideoDecodeAccelerator entry points. Calls from this thread
46 // generally do not block (with the exception of Initialize() and Destroy()).
47 // They post tasks to the decoder_thread_, which actually services the task
48 // and calls back when complete through the
49 // media::VideoDecodeAccelerator::Client interface.
50 // * The decoder_thread_, owned by this class. It services API tasks as well as
51 // device events. Pretty much all state modification is done on this thread.
52 // * The device_thread_, owned by this class. All it does is epoll() on the
53 // V4L2 device and notify the decoder_thread_ when something interesting
54 // happens.
55 //
56 // Note that this class has no locks! Everything's serviced on the
57 // decoder_thread_, so there are no synchronization issues.
58 // ... well, there are, but it's a matter of getting messages posted in the
59 // right order, not fiddling with locks.
60 class CONTENT_EXPORT ExynosVideoDecodeAccelerator :
61 public media::VideoDecodeAccelerator {
62 public:
63 ExynosVideoDecodeAccelerator(
64 gfx::GLContext* gl_context,
65 Client* client,
66 const base::Callback<bool(void)>& make_context_current);
67 virtual ~ExynosVideoDecodeAccelerator();
68
69 // media::VideoDecodeAccelerator implementation.
70 // Note: Initialize() and Destroy() are synchronous.
71 virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE;
72 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE;
73 virtual void AssignPictureBuffers(
74 const std::vector<media::PictureBuffer>& buffers) OVERRIDE;
75 virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE;
76 virtual void Flush() OVERRIDE;
77 virtual void Reset() OVERRIDE;
78 virtual void Destroy() OVERRIDE;
79
80 // Do any necessary initialization before the sandbox is enabled.
81 static void PreSandboxInitialization();
82
83 // Lazily initialize static data after sandbox is enabled. Return false on
84 // init failure.
85 static bool PostSandboxInitialization();
86
87 private:
88 // These are rather subjectively tuned.
89 enum {
90 kMfcInputBufferCount = 4,
91 kMfcOutputBufferExtraCount = 2,
92 kMfcInputBufferMaxSize = 1024 * 1024,
93 kGscInputBufferCount = 4,
94 kGscOutputBufferCount = 6,
95 };
96
97 // Internal state of the decoder.
98 enum State {
99 kUninitialized, // Initialize() not yet called.
100 kInitialized, // Initialize() called. Ready to start decoding.
101 kDecoding, // DecodeBufferInitial() successful; decoding frames.
102 kResetting, // Presently resetting.
103 kAfterReset, // After Reset(), ready to start decoding again.
104 kError, // Error in kDecoding state.
105 };
106
107 // Record for decoder input buffers.
108 struct BitstreamBufferRecord {
109 BitstreamBufferRecord(base::SharedMemory* shm, size_t size, int32 input_id);
110 ~BitstreamBufferRecord() {}
Ami GONE FROM CHROMIUM 2012/10/31 01:06:50 here and below, dtors should be defined out-of-lin
111 const scoped_ptr<base::SharedMemory> shm;
112 const size_t size;
113 const int32 input_id;
114 };
115
116 // Record for MFC input buffers.
117 struct MfcInputRecord {
118 MfcInputRecord();
119 ~MfcInputRecord() {}
120 bool at_device; // held by device.
121 void* offset; // mmap() offset.
122 size_t length; // mmap() length.
123 off_t bytes_used; // bytes filled in the mmap() segment.
124 int32 input_id; // triggering input_id as given to Decode().
125 };
126
127 // Record for MFC output buffers.
128 struct MfcOutputRecord {
129 MfcOutputRecord();
130 ~MfcOutputRecord() {}
131 bool at_device; // held by device.
132 size_t bytes_used[2]; // bytes used in each dmabuf.
133 void* offset[2]; // mmap() offset for each plane.
134 size_t length[2]; // mmap() length for each plane.
135 int32 input_id; // triggering input_id as given to Decode().
136 };
137
138 // Record for GSC input buffers.
139 struct GscInputRecord {
140 GscInputRecord();
141 ~GscInputRecord() {}
142 bool at_device; // held by device.
143 int mfc_output; // MFC output buffer index to recycle when this input
144 // is complete
145 };
146
147 // Record for GSC output buffers.
148 struct GscOutputRecord {
149 GscOutputRecord();
150 ~GscOutputRecord() {}
151 bool at_device; // held by device.
152 bool at_client; // held by client.
153 int fd; // file descriptor from backing EGLImage.
154 EGLImageKHR egl_image; // backing EGLImage.
155 EGLSyncKHR egl_sync; // sync the compositor's use of the EGLImage.
156 int32 picture_id; // picture ID as returned to PictureReady().
157 };
158
159 // Auto-destruction reference for an array of EGLImage (for message-passing)
160 struct EGLImageKHRArrayRef {
161 EGLImageKHRArrayRef(EGLDisplay egl_display, EGLImageKHR egl_images[],
162 int egl_image_fds[], int egl_images_count);
163 ~EGLImageKHRArrayRef();
164 EGLDisplay const egl_display;
165 const scoped_array<EGLImageKHR> egl_images;
166 const scoped_array<int> egl_image_fds;
Ami GONE FROM CHROMIUM 2012/10/31 01:06:50 this and the previous are just arrays of PODs, so
sheu 2012/11/01 02:16:08 To keep things lightweight? I initially inclined
167 const int egl_images_count;
168 };
169
170 // Auto-destruction reference for EGLSync (for message-passing)
171 struct EGLSyncKHRRef {
172 EGLSyncKHRRef(EGLDisplay egl_display, EGLSyncKHR egl_sync);
173 ~EGLSyncKHRRef();
174 EGLDisplay const egl_display;
175 EGLSyncKHR egl_sync;
176 };
177
178 //
179 // Decoding tasks, to be run on decode_thread_.
180 //
181
182 // Enqueue a BitstreamBuffer to decode. This will enqueue a
183 // DecodeBufferTask() to actually decode the buffer.
184 void DecodeTask(scoped_ptr<BitstreamBufferRecord> bitstream_record);
185
186 // Decode from the queued BitstreamBuffers. Calls DecodeBufferInitial()
187 // or DecodeBufferContinue().
188 void DecodeBufferTask();
189 bool DecodeBufferInitial(const void* data, size_t size);
Ami GONE FROM CHROMIUM 2012/10/31 01:06:50 doco ret values of these two.
sheu 2012/11/01 02:16:08 Done.
190 bool DecodeBufferContinue(const void* data, size_t size);
191 // Helpers: accumulate and flush data for one decoded frame.
192 bool AppendToInputFrame(const void* data, size_t size);
193 bool FlushInputFrame();
194
195 // Process an AssignPictureBuffers() API call. After this, the device_thread_
196 // can be started safely, since we have all our buffers.
197 void AssignPictureBuffersTask(scoped_ptr<EGLImageKHRArrayRef> egl_images_ref);
198
199 // Service I/O on the V4L2 devices.
200 void ServiceDeviceTask();
201 // Handle the various device queues.
202 void EnqueueMfc();
203 void DequeueMfc();
204 void EnqueueGsc();
205 void DequeueGsc();
206
207 // Process a ReusePictureBuffer() API call. The API call create an EGLSync
208 // object on the main (GPU process) thread; we will record this object so we
209 // can wait on it before reusing the buffer.
210 void ReusePictureBufferTask(int32 picture_buffer_id,
211 scoped_ptr<EGLSyncKHRRef> egl_sync_ref);
212
213 // Flush() task. Child thread should not submit any more buffers until it
214 // receives the NotifyFlushDone callback.
215 void FlushTask();
216
217 // Reset() task. This task will schedule a ResetDoneTask() that will send
218 // the NotifyResetDone callback.
219 void ResetTask();
220 void ResetDoneTask();
221
222 // Device destruction task.
223 void DestroyTask();
224
225 // Cleanup() task, mostly used on the error path.
226 void CleanupTask();
Ami GONE FROM CHROMIUM 2012/10/31 01:06:50 This is named "Task" implying running on decoder_t
sheu 2012/11/01 02:16:08 Removed entirely. Wasn't doing much anyways, and
227
228 // Start/stop DeviceTask() running on device_thread_.
sheu 2012/11/01 02:16:08 Renamed to StartDevicePoll/StopDevicePoll.
229 bool StartDevice();
230 bool StopDevice();
231
232 //
233 // Device tasks, to be run on device_thread_.
234 //
235
236 // The device task.
237 void DeviceTask();
sheu 2012/11/01 02:16:08 Renamed to DevicePollLoop().
238
239 //
240 // Safe from any thread (uses PostTask() to child thread).
241 //
242
243 // Error notification.
244 void NotifyError(Error error);
245
246 //
247 // Other utility functions
248 //
249
250 // Create the buffers we need.
251 bool CreateMfcInputBuffers();
252 bool CreateMfcOutputBuffers();
253 bool CreateGscInputBuffers();
254 bool CreateGscOutputBuffers();
255
256 // Destroy these buffers.
257 void DestroyMfcInputBuffers();
258 void DestroyMfcOutputBuffers();
259 void DestroyGscInputBuffers();
260 void DestroyGscOutputBuffers();
261
262 // Our original calling message loop for the child thread.
263 scoped_refptr<base::MessageLoopProxy> child_message_loop_proxy_;
264
265 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder or
266 // device worker threads back to the child thread. Because the worker threads
267 // are members of this class, any task running on those threads is guaranteed
268 // that this object is still alive. As a result, tasks posted from the child
269 // thread to the decoder or device thread should use base::Unretained(this),
270 // and tasks posted the other way should use |weak_this_|.
271 base::WeakPtr<ExynosVideoDecodeAccelerator> weak_this_;
272
273 // To expose client callbacks from VideoDecodeAccelerator.
274 // NOTE: all calls to these objects *MUST* be executed on
275 // child_message_loop_proxy_.
276 base::WeakPtrFactory<Client> client_ptr_factory_;
277 base::WeakPtr<Client> client_;
278
279 //
280 // Decoder state, owned and operated by decoder_thread_.
281 // Before decoder_thread_ has started, the decoder state is managed by
282 // the child (main) thread. After decoder_thread_ has started, the decoder
283 // thread should be the only one managing these.
284 //
285
286 // This thread services tasks posted from the VDA API entry points by the
287 // child thread and device service callbacks posted from the device thread.
288 base::Thread decoder_thread_;
289 // Decoder state machine state.
290 State decoder_state_;
291 // Bitstream buffer we're presently reading.
292 scoped_ptr<BitstreamBufferRecord> decoder_current_bitstream_buffer_;
293 // MFC input buffer we're presently filling.
294 int decoder_current_input_buffer_;
295 // We track the number of buffer decode tasks we have scheduled, since each
296 // task execution should complete one buffer. If we fall behind (due to
297 // resource backpressure, etc.), we'll have to schedule more to catch up.
298 int decoder_decode_buffer_tasks_scheduled_;
299 // Buffers in the pipe, five-by-five.
300 int decoder_frames_inflight_;
301 // Buffers held by the client.
302 int decoder_frames_at_client_;
303 // Are we waiting for a flush notification?
304 bool decoder_flush_notify_requested_;
305 // Input queue for decoder_thread_: BitstreamBuffers in.
306 std::deque<linked_ptr<BitstreamBufferRecord> > decoder_input_queue_;
307
308 //
309 // Hardware state and associated queues. Since decoder_thread_ services
310 // the hardware, decoder_thread_ owns these too.
311 //
312
313 // Completed decode buffers, waiting for MFC.
314 std::deque<int> mfc_input_ready_queue_;
315
316 // MFC decode device.
317 int mfc_fd_;
318 file_util::ScopedFD mfc_fd_closer_;
319
320 // MFC input buffer state.
321 bool mfc_input_streamon_;
322 int mfc_input_buffer_count_;
323 // Input buffers ready to use, as a FIFO since we don't care about ordering.
324 std::vector<int> mfc_free_input_buffers_;
325 // Mapping of int index to MFC input buffer record.
326 std::vector<MfcInputRecord> mfc_input_buffer_map_;
327
328 // MFC output buffer state.
329 bool mfc_output_streamon_;
330 int mfc_output_buffer_count_;
331 // Output buffers ready to use, as a FIFO since we don't care about ordering.
332 std::vector<int> mfc_free_output_buffers_;
333 // Mapping of int index to MFC output buffer record.
334 std::vector<MfcOutputRecord> mfc_output_buffer_map_;
335 // Required size of MFC output buffers. Two sizes for two planes.
336 size_t mfc_output_buffer_size_[2];
337 uint32 mfc_output_buffer_pixelformat_;
338
339 // Completed MFC outputs, waiting for GSC.
340 std::deque<int> mfc_output_gsc_input_queue_;
341
342 // GSC decode device.
343 int gsc_fd_;
344 file_util::ScopedFD gsc_fd_closer_;
345
346 // GSC input buffer state.
347 bool gsc_input_streamon_;
348 int gsc_input_buffer_count_;
349 // Input buffers ready to use, as a FIFO since we don't care about ordering.
350 std::vector<int> gsc_free_input_buffers_;
351 // Mapping of int index to GSC input buffer record.
352 std::vector<GscInputRecord> gsc_input_buffer_map_;
353
354 // GSC output buffer state.
355 bool gsc_output_streamon_;
356 int gsc_output_buffer_count_;
357 // GSC output buffers enqueued to device and ready, but not yet in use.
358 int gsc_output_buffer_prepared_count_;
359 // GSC output buffers enqueued to device, total.
360 int gsc_output_buffer_queued_count_;
361 // Output buffers ready to use. We need a LIFO here.
362 std::deque<int> gsc_free_output_buffers_;
363 // Mapping of int index to GSC output buffer record.
364 std::vector<GscOutputRecord> gsc_output_buffer_map_;
365
366 // Output picture size.
367 gfx::Size frame_buffer_size_;
368
369 //
370 // The device thread handles notifications of V4L2 device changes.
371 //
372
373 // The thread.
374 base::Thread device_thread_;
375
376 //
377 // Other state, held by the child (main) thread.
378 //
379
380 // GL state
381 gfx::GLContext* gl_context_;
382 // Make our context current before running any EGL entry points.
383 base::Callback<bool(void)> make_context_current_;
384
385 // EGL state
386 EGLContext egl_context_;
387 EGLDisplay egl_display_;
388
389 // The codec we'll be decoding for.
390 media::VideoCodecProfile video_profile_;
391
392 DISALLOW_COPY_AND_ASSIGN(ExynosVideoDecodeAccelerator);
393 };
394
395 } // namespace content
396
397 #endif // CONTENT_COMMON_GPU_MEDIA_EXYNOS_VIDEO_DECODE_ACCELERATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698