OLD | NEW |
(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 |
| 6 // that utilizes the 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 <list> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/callback_forward.h" |
| 15 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/threading/thread.h" |
| 18 #include "content/common/content_export.h" |
| 19 #include "media/base/video_decoder_config.h" |
| 20 #include "media/video/video_decode_accelerator.h" |
| 21 #include "third_party/angle/include/EGL/egl.h" |
| 22 #include "third_party/angle/include/EGL/eglext.h" |
| 23 #include "ui/gfx/size.h" |
| 24 |
| 25 namespace base { |
| 26 class MessageLoopProxy; |
| 27 } |
| 28 |
| 29 namespace content { |
| 30 class H264Parser; |
| 31 |
| 32 // This class handles Exynos video acceleration directly through the V4L2 |
| 33 // devices exported by the Multi Format Codec and GScaler hardware blocks. |
| 34 // |
| 35 // The threading model of this class is driven by the fact that it needs to |
| 36 // interface two fundamentally different event queues -- the one Chromium |
| 37 // provides through MessageLoop, and the one driven by the V4L2 devices which |
| 38 // is waited on with epoll(). There are three threads involved in this class: |
| 39 // |
| 40 // * The child thread, which is the main GPU process thread which calls the |
| 41 // media::VideoDecodeAccelerator entry points. Calls from this thread |
| 42 // generally do not block (with the exception of Initialize() and Destroy()). |
| 43 // They post tasks to the decoder_thread_, which actually services the task |
| 44 // and calls back when complete through the |
| 45 // media::VideoDecodeAccelerator::Client interface. |
| 46 // * The decoder_thread_, owned by this class. It services API tasks, through |
| 47 // the *Task() routines, as well as V4L2 device events, through |
| 48 // ServiceDeviceTask(). Almost all state modification is done on this thread. |
| 49 // * The device_poll_thread_, owned by this class. All it does is epoll() on |
| 50 // the V4L2 in DevicePollTask() and schedule a ServiceDeviceTask() on the |
| 51 // decoder_thread_ when something interesting happens. |
| 52 // TODO(sheu): replace this thread with an TYPE_IO decoder_thread_. |
| 53 // |
| 54 // Note that this class has no locks! Everything's serviced on the |
| 55 // decoder_thread_, so there are no synchronization issues. |
| 56 // ... well, there are, but it's a matter of getting messages posted in the |
| 57 // right order, not fiddling with locks. |
| 58 class CONTENT_EXPORT ExynosVideoDecodeAccelerator : |
| 59 public media::VideoDecodeAccelerator { |
| 60 public: |
| 61 ExynosVideoDecodeAccelerator( |
| 62 EGLDisplay egl_display, |
| 63 EGLContext egl_context, |
| 64 Client* client, |
| 65 const base::Callback<bool(void)>& make_context_current); |
| 66 virtual ~ExynosVideoDecodeAccelerator(); |
| 67 |
| 68 // media::VideoDecodeAccelerator implementation. |
| 69 // Note: Initialize() and Destroy() are synchronous. |
| 70 virtual bool Initialize(media::VideoCodecProfile profile) OVERRIDE; |
| 71 virtual void Decode(const media::BitstreamBuffer& bitstream_buffer) OVERRIDE; |
| 72 virtual void AssignPictureBuffers( |
| 73 const std::vector<media::PictureBuffer>& buffers) OVERRIDE; |
| 74 virtual void ReusePictureBuffer(int32 picture_buffer_id) OVERRIDE; |
| 75 virtual void Flush() OVERRIDE; |
| 76 virtual void Reset() OVERRIDE; |
| 77 virtual void Destroy() OVERRIDE; |
| 78 |
| 79 // Do any necessary initialization before the sandbox is enabled. |
| 80 static void PreSandboxInitialization(); |
| 81 |
| 82 // Lazily initialize static data after sandbox is enabled. Return false on |
| 83 // init failure. |
| 84 static bool PostSandboxInitialization(); |
| 85 |
| 86 private: |
| 87 // These are rather subjectively tuned. |
| 88 enum { |
| 89 kMfcInputBufferCount = 8, |
| 90 kMfcOutputBufferExtraCount = 5, // number of buffers above request by V4L2. |
| 91 kMfcInputBufferMaxSize = 512 * 1024, |
| 92 kGscInputBufferCount = 6, |
| 93 kGscOutputBufferCount = 6, |
| 94 }; |
| 95 |
| 96 // Internal state of the decoder. |
| 97 enum State { |
| 98 kUninitialized, // Initialize() not yet called. |
| 99 kInitialized, // Initialize() returned true; ready to start decoding. |
| 100 kDecoding, // DecodeBufferInitial() successful; decoding frames. |
| 101 kResetting, // Presently resetting. |
| 102 kAfterReset, // After Reset(), ready to start decoding again. |
| 103 kError, // Error in kDecoding state. |
| 104 }; |
| 105 |
| 106 enum BufferId { |
| 107 kFlushBufferId = -2 // Buffer id for flush buffer, queued by FlushTask(). |
| 108 }; |
| 109 |
| 110 // File descriptors we need to poll. |
| 111 enum PollFds { |
| 112 kPollMfc = (1 << 0), |
| 113 kPollGsc = (1 << 1), |
| 114 }; |
| 115 |
| 116 // Auto-destruction reference for BitstreamBuffer, for message-passing from |
| 117 // Decode() to DecodeTask(). |
| 118 struct BitstreamBufferRef; |
| 119 |
| 120 // Auto-destruction reference for an array of PictureBuffer, for |
| 121 // message-passing from AssignPictureBuffers() to AssignPictureBuffersTask(). |
| 122 struct PictureBufferArrayRef; |
| 123 |
| 124 // Auto-destruction reference for EGLSync (for message-passing). |
| 125 struct EGLSyncKHRRef; |
| 126 |
| 127 // Record for MFC input buffers. |
| 128 struct MfcInputRecord { |
| 129 MfcInputRecord(); |
| 130 ~MfcInputRecord(); |
| 131 bool at_device; // held by device. |
| 132 void* address; // mmap() address. |
| 133 size_t length; // mmap() length. |
| 134 off_t bytes_used; // bytes filled in the mmap() segment. |
| 135 int32 input_id; // triggering input_id as given to Decode(). |
| 136 }; |
| 137 |
| 138 // Record for MFC output buffers. |
| 139 struct MfcOutputRecord { |
| 140 MfcOutputRecord(); |
| 141 ~MfcOutputRecord(); |
| 142 bool at_device; // held by device. |
| 143 size_t bytes_used[2]; // bytes used in each dmabuf. |
| 144 void* address[2]; // mmap() address for each plane. |
| 145 size_t length[2]; // mmap() length for each plane. |
| 146 int32 input_id; // triggering input_id as given to Decode(). |
| 147 }; |
| 148 |
| 149 // Record for GSC input buffers. |
| 150 struct GscInputRecord { |
| 151 GscInputRecord(); |
| 152 ~GscInputRecord(); |
| 153 bool at_device; // held by device. |
| 154 int mfc_output; // MFC output buffer index to recycle when this input |
| 155 // is complete |
| 156 }; |
| 157 |
| 158 // Record for GSC output buffers. |
| 159 struct GscOutputRecord { |
| 160 GscOutputRecord(); |
| 161 ~GscOutputRecord(); |
| 162 bool at_device; // held by device. |
| 163 bool at_client; // held by client. |
| 164 int fd; // file descriptor from backing EGLImage. |
| 165 EGLImageKHR egl_image; // backing EGLImage. |
| 166 EGLSyncKHR egl_sync; // sync the compositor's use of the EGLImage. |
| 167 int32 picture_id; // picture buffer id as returned to PictureReady(). |
| 168 }; |
| 169 |
| 170 // |
| 171 // Decoding tasks, to be run on decode_thread_. |
| 172 // |
| 173 |
| 174 // Enqueue a BitstreamBuffer to decode. This will enqueue a |
| 175 // DecodeBufferTask() to actually decode the buffer. |
| 176 void DecodeTask(scoped_ptr<BitstreamBufferRef> bitstream_record); |
| 177 |
| 178 // Decode from the queued BitstreamBuffers. Calls DecodeBufferInitial() |
| 179 // or DecodeBufferContinue(). |
| 180 void DecodeBufferTask(); |
| 181 // Find the extents of one frame fragment to push to HW. |
| 182 bool FindFrameFragment(const uint8* data, size_t size, size_t* endpos); |
| 183 // Schedule another DecodeBufferTask() if we're behind. |
| 184 void ScheduleDecodeBufferTaskIfNeeded(); |
| 185 |
| 186 // Return true if we should continue to schedule DecodeBufferTask()s after |
| 187 // completion. Store the amount of input actually consumed in |endpos|. |
| 188 bool DecodeBufferInitial(const void* data, size_t size, size_t* endpos); |
| 189 bool DecodeBufferContinue(const void* data, size_t size); |
| 190 |
| 191 // Accumulate data for the next frame to decode. May return false in |
| 192 // non-error conditions; for example when pipeline is full and should be |
| 193 // retried later. |
| 194 bool AppendToInputFrame(const void* data, size_t size); |
| 195 // Flush data for one decoded frame. |
| 196 bool FlushInputFrame(); |
| 197 |
| 198 // Process an AssignPictureBuffers() API call. After this, the |
| 199 // device_poll_thread_ can be started safely, since we have all our |
| 200 // buffers. |
| 201 void AssignPictureBuffersTask(scoped_ptr<PictureBufferArrayRef> pic_buffers); |
| 202 |
| 203 // Service I/O on the V4L2 devices. This task should only be scheduled from |
| 204 // DevicePollTask(). |
| 205 void ServiceDeviceTask(); |
| 206 // Handle the various device queues. |
| 207 void EnqueueMfc(); |
| 208 void DequeueMfc(); |
| 209 void EnqueueGsc(); |
| 210 void DequeueGsc(); |
| 211 // Enqueue a buffer on the corresponding queue. |
| 212 bool EnqueueMfcInputRecord(); |
| 213 bool EnqueueMfcOutputRecord(); |
| 214 bool EnqueueGscInputRecord(); |
| 215 bool EnqueueGscOutputRecord(); |
| 216 |
| 217 // Process a ReusePictureBuffer() API call. The API call create an EGLSync |
| 218 // object on the main (GPU process) thread; we will record this object so we |
| 219 // can wait on it before reusing the buffer. |
| 220 void ReusePictureBufferTask(int32 picture_buffer_id, |
| 221 scoped_ptr<EGLSyncKHRRef> egl_sync_ref); |
| 222 |
| 223 // Flush() task. Child thread should not submit any more buffers until it |
| 224 // receives the NotifyFlushDone callback. This task will schedule an empty |
| 225 // BitstreamBufferRef (with input_id == kFlushBufferId) to perform the flush. |
| 226 void FlushTask(); |
| 227 // Notify the client of a flush completion, if required. This should be |
| 228 // called any time a relevant queue could potentially be emptied: see |
| 229 // function definition. |
| 230 void NotifyFlushDoneIfNeeded(); |
| 231 |
| 232 // Reset() task. This task will schedule a ResetDoneTask() that will send |
| 233 // the NotifyResetDone callback, then set the decoder state to kResetting so |
| 234 // that all intervening tasks will drain. |
| 235 void ResetTask(); |
| 236 // ResetDoneTask() will set the decoder state back to kAfterReset, so |
| 237 // subsequent decoding can continue. |
| 238 void ResetDoneTask(); |
| 239 |
| 240 // Device destruction task. |
| 241 void DestroyTask(); |
| 242 |
| 243 // Attempt to start/stop device_poll_thread_. |
| 244 bool StartDevicePoll(); |
| 245 bool StopDevicePoll(); |
| 246 // Set/clear the device poll interrupt (using device_poll_interrupt_fd_). |
| 247 bool SetDevicePollInterrupt(); |
| 248 bool ClearDevicePollInterrupt(); |
| 249 |
| 250 // |
| 251 // Device tasks, to be run on device_poll_thread_. |
| 252 // |
| 253 |
| 254 // The device task. |
| 255 void DevicePollTask(unsigned int poll_fds); |
| 256 |
| 257 // |
| 258 // Safe from any thread. |
| 259 // |
| 260 |
| 261 // Error notification (using PostTask() to child thread, if necessary). |
| 262 void NotifyError(Error error); |
| 263 |
| 264 // Set the decoder_thread_ state (using PostTask to decoder thread, if |
| 265 // necessary). |
| 266 void SetDecoderState(State state); |
| 267 |
| 268 // |
| 269 // Other utility functions. Called on decoder_thread_, unless |
| 270 // decoder_thread_ is not yet started, in which case the child thread can call |
| 271 // these (e.g. in Initialize() or Destroy()). |
| 272 // |
| 273 |
| 274 // Create the buffers we need. |
| 275 bool CreateMfcInputBuffers(); |
| 276 bool CreateMfcOutputBuffers(); |
| 277 bool CreateGscInputBuffers(); |
| 278 bool CreateGscOutputBuffers(); |
| 279 |
| 280 // Destroy these buffers. |
| 281 void DestroyMfcInputBuffers(); |
| 282 void DestroyMfcOutputBuffers(); |
| 283 void DestroyGscInputBuffers(); |
| 284 void DestroyGscOutputBuffers(); |
| 285 |
| 286 // Our original calling message loop for the child thread. |
| 287 scoped_refptr<base::MessageLoopProxy> child_message_loop_proxy_; |
| 288 |
| 289 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder or |
| 290 // device worker threads back to the child thread. Because the worker threads |
| 291 // are members of this class, any task running on those threads is guaranteed |
| 292 // that this object is still alive. As a result, tasks posted from the child |
| 293 // thread to the decoder or device thread should use base::Unretained(this), |
| 294 // and tasks posted the other way should use |weak_this_|. |
| 295 base::WeakPtr<ExynosVideoDecodeAccelerator> weak_this_; |
| 296 |
| 297 // To expose client callbacks from VideoDecodeAccelerator. |
| 298 // NOTE: all calls to these objects *MUST* be executed on |
| 299 // child_message_loop_proxy_. |
| 300 base::WeakPtrFactory<Client> client_ptr_factory_; |
| 301 base::WeakPtr<Client> client_; |
| 302 |
| 303 // |
| 304 // Decoder state, owned and operated by decoder_thread_. |
| 305 // Before decoder_thread_ has started, the decoder state is managed by |
| 306 // the child (main) thread. After decoder_thread_ has started, the decoder |
| 307 // thread should be the only one managing these. |
| 308 // |
| 309 |
| 310 // This thread services tasks posted from the VDA API entry points by the |
| 311 // child thread and device service callbacks posted from the device thread. |
| 312 base::Thread decoder_thread_; |
| 313 // Decoder state machine state. |
| 314 State decoder_state_; |
| 315 // BitstreamBuffer we're presently reading. |
| 316 scoped_ptr<BitstreamBufferRef> decoder_current_bitstream_buffer_; |
| 317 // BitstreamBuffer that we should delay decoding, during Reset() or Flush(). |
| 318 int decoder_delay_bitstream_buffer_id_; |
| 319 // MFC input buffer we're presently filling. |
| 320 int decoder_current_input_buffer_; |
| 321 // We track the number of buffer decode tasks we have scheduled, since each |
| 322 // task execution should complete one buffer. If we fall behind (due to |
| 323 // resource backpressure, etc.), we'll have to schedule more to catch up. |
| 324 int decoder_decode_buffer_tasks_scheduled_; |
| 325 // Picture buffers held by the client. |
| 326 int decoder_frames_at_client_; |
| 327 // Are we flushing? |
| 328 bool decoder_flushing_; |
| 329 // Input queue for decoder_thread_: BitstreamBuffers in. |
| 330 std::list<linked_ptr<BitstreamBufferRef> > decoder_input_queue_; |
| 331 // For H264 decode, hardware requires that we send it frame-sized chunks. |
| 332 // We'll need to parse the stream. |
| 333 scoped_ptr<content::H264Parser> decoder_h264_parser_; |
| 334 |
| 335 // |
| 336 // Hardware state and associated queues. Since decoder_thread_ services |
| 337 // the hardware, decoder_thread_ owns these too. |
| 338 // |
| 339 |
| 340 // Completed decode buffers, waiting for MFC. |
| 341 std::list<int> mfc_input_ready_queue_; |
| 342 |
| 343 // MFC decode device. |
| 344 int mfc_fd_; |
| 345 |
| 346 // MFC input buffer state. |
| 347 bool mfc_input_streamon_; |
| 348 // MFC input buffers, total. |
| 349 int mfc_input_buffer_count_; |
| 350 // MFC input buffers enqueued to device. |
| 351 int mfc_input_buffer_queued_count_; |
| 352 // Input buffers ready to use, as a LIFO since we don't care about ordering. |
| 353 std::vector<int> mfc_free_input_buffers_; |
| 354 // Mapping of int index to MFC input buffer record. |
| 355 std::vector<MfcInputRecord> mfc_input_buffer_map_; |
| 356 |
| 357 // MFC output buffer state. |
| 358 bool mfc_output_streamon_; |
| 359 // MFC output buffers, total. |
| 360 int mfc_output_buffer_count_; |
| 361 // MFC output buffers enqueued to device. |
| 362 int mfc_output_buffer_queued_count_; |
| 363 // Output buffers ready to use, as a LIFO since we don't care about ordering. |
| 364 std::vector<int> mfc_free_output_buffers_; |
| 365 // Mapping of int index to MFC output buffer record. |
| 366 std::vector<MfcOutputRecord> mfc_output_buffer_map_; |
| 367 // Required size of MFC output buffers. Two sizes for two planes. |
| 368 size_t mfc_output_buffer_size_[2]; |
| 369 uint32 mfc_output_buffer_pixelformat_; |
| 370 |
| 371 // Completed MFC outputs, waiting for GSC. |
| 372 std::list<int> mfc_output_gsc_input_queue_; |
| 373 |
| 374 // GSC decode device. |
| 375 int gsc_fd_; |
| 376 |
| 377 // GSC input buffer state. |
| 378 bool gsc_input_streamon_; |
| 379 // GSC input buffers, total. |
| 380 int gsc_input_buffer_count_; |
| 381 // GSC input buffers enqueued to device. |
| 382 int gsc_input_buffer_queued_count_; |
| 383 // Input buffers ready to use, as a LIFO since we don't care about ordering. |
| 384 std::vector<int> gsc_free_input_buffers_; |
| 385 // Mapping of int index to GSC input buffer record. |
| 386 std::vector<GscInputRecord> gsc_input_buffer_map_; |
| 387 |
| 388 // GSC output buffer state. |
| 389 bool gsc_output_streamon_; |
| 390 // GSC output buffers, total. |
| 391 int gsc_output_buffer_count_; |
| 392 // GSC output buffers enqueued to device. |
| 393 int gsc_output_buffer_queued_count_; |
| 394 // Output buffers ready to use. We need a FIFO here. |
| 395 std::list<int> gsc_free_output_buffers_; |
| 396 // Mapping of int index to GSC output buffer record. |
| 397 std::vector<GscOutputRecord> gsc_output_buffer_map_; |
| 398 |
| 399 // Output picture size. |
| 400 gfx::Size frame_buffer_size_; |
| 401 |
| 402 // |
| 403 // The device polling thread handles notifications of V4L2 device changes. |
| 404 // |
| 405 |
| 406 // The thread. |
| 407 base::Thread device_poll_thread_; |
| 408 // eventfd fd to signal device poll thread when its poll() should be |
| 409 // interrupted. |
| 410 int device_poll_interrupt_fd_; |
| 411 |
| 412 // |
| 413 // Other state, held by the child (main) thread. |
| 414 // |
| 415 |
| 416 // Make our context current before running any EGL entry points. |
| 417 base::Callback<bool(void)> make_context_current_; |
| 418 |
| 419 // EGL state |
| 420 EGLDisplay egl_display_; |
| 421 EGLContext egl_context_; |
| 422 |
| 423 // The codec we'll be decoding for. |
| 424 media::VideoCodecProfile video_profile_; |
| 425 |
| 426 DISALLOW_COPY_AND_ASSIGN(ExynosVideoDecodeAccelerator); |
| 427 }; |
| 428 |
| 429 } // namespace content |
| 430 |
| 431 #endif // CONTENT_COMMON_GPU_MEDIA_EXYNOS_VIDEO_DECODE_ACCELERATOR_H_ |
OLD | NEW |