OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 hardware video decoder present on Intel CPUs. |
| 7 |
| 8 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
| 9 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
| 10 |
| 11 #include <map> |
| 12 #include <queue> |
| 13 #include <utility> |
| 14 #include <vector> |
| 15 |
| 16 #include "base/logging.h" |
| 17 #include "base/memory/linked_ptr.h" |
| 18 #include "base/memory/shared_memory.h" |
| 19 #include "base/memory/weak_ptr.h" |
| 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/synchronization/condition_variable.h" |
| 22 #include "base/synchronization/lock.h" |
| 23 #include "base/threading/non_thread_safe.h" |
| 24 #include "base/threading/thread.h" |
| 25 #include "content/common/content_export.h" |
| 26 #include "content/common/gpu/media/vaapi_jpeg_decoder.h" |
| 27 #include "content/common/gpu/media/vaapi_wrapper.h" |
| 28 #include "media/base/bitstream_buffer.h" |
| 29 #include "media/video/jpeg_decode_accelerator.h" |
| 30 #include "media/video/picture.h" |
| 31 |
| 32 namespace content { |
| 33 |
| 34 // Class to provide JPEG decode acceleration for Intel systems with hardware |
| 35 // support for it, and on which libva is available. |
| 36 // Decoding tasks are performed in a separate decoding thread. |
| 37 // |
| 38 // Threading/life-cycle: this object is created & destroyed on the GPU |
| 39 // ChildThread. A few methods on it are called on the decoder thread which is |
| 40 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread |
| 41 // can assume |*this| is still alive. See |weak_this_| below for more details. |
| 42 class CONTENT_EXPORT VaapiJpegDecodeAccelerator |
| 43 : public media::JpegDecodeAccelerator { |
| 44 public: |
| 45 VaapiJpegDecodeAccelerator(); |
| 46 ~VaapiJpegDecodeAccelerator() override; |
| 47 |
| 48 // media::JpegDecodeAccelerator implementation. |
| 49 bool Initialize(media::JpegDecodeAccelerator::Client* client) override; |
| 50 void Decode(const media::BitstreamBuffer& bitstream_buffer, |
| 51 const scoped_refptr<media::VideoFrame>& video_frame) override; |
| 52 void Destroy() override; |
| 53 |
| 54 private: |
| 55 // Notify the client that an error has occurred and decoding cannot continue. |
| 56 void NotifyError(int32_t bitstream_buffer_id, Error error); |
| 57 |
| 58 // Map the received input buffer into this process' address space and |
| 59 // queue it for decode. |
| 60 void MapAndQueueNewInputBuffer( |
| 61 const media::BitstreamBuffer& bitstream_buffer, |
| 62 const scoped_refptr<media::VideoFrame>& video_frame); |
| 63 |
| 64 // Get a new input buffer from the queue and set it up in decoder. This will |
| 65 // sleep if no input buffers are available. Return true if a new buffer has |
| 66 // been set up, false if an early exit has been requested (due to initiated |
| 67 // reset/flush/destroy). |
| 68 bool GetInputBuffer_Locked(); |
| 69 |
| 70 // Continue decoding given input buffers and sleep waiting for input/output |
| 71 // as needed. Will exit if a new set of surfaces or reset/flush/destroy |
| 72 // is requested. |
| 73 void DecodeTask(); |
| 74 |
| 75 // Helper for Destroy(), doing all the actual work except for deleting self. |
| 76 void Cleanup(); |
| 77 |
| 78 // TODO(kcwu) fix comment |
| 79 // Callback to be executed once we have a |va_surface| to be output and |
| 80 // an available |tfp_picture| to use for output. |
| 81 // Puts contents of |va_surface| into given |tfp_picture|, releases the |
| 82 // surface and passes the resulting picture to client for output. |
| 83 bool OutputPicture(VASurfaceID va_surface_id, |
| 84 int32_t input_id, |
| 85 const scoped_refptr<media::VideoFrame>& video_frame); |
| 86 |
| 87 // VAVDA state. |
| 88 enum State { |
| 89 // Initialize() not called yet or failed. |
| 90 kUninitialized, |
| 91 // DecodeTask running. |
| 92 kDecoding, |
| 93 // Idle, decoder in state ready to start/resume decoding. |
| 94 kIdle, |
| 95 // Destroying, waiting for the decoder to finish current task. |
| 96 kDestroying, |
| 97 }; |
| 98 |
| 99 // Protects input buffer and surface queues and state_. |
| 100 base::Lock lock_; |
| 101 State state_; |
| 102 |
| 103 // An input buffer awaiting consumption, provided by the client. |
| 104 struct InputBuffer { |
| 105 InputBuffer(); |
| 106 ~InputBuffer(); |
| 107 |
| 108 int32 id; |
| 109 size_t size; |
| 110 scoped_ptr<base::SharedMemory> shm; |
| 111 |
| 112 scoped_refptr<media::VideoFrame> video_frame; |
| 113 }; |
| 114 |
| 115 // Queue for incoming input buffers. |
| 116 typedef std::queue<linked_ptr<InputBuffer> > InputBuffers; |
| 117 InputBuffers input_buffers_; |
| 118 // Signalled when input buffers are queued onto the input_buffers_ queue. |
| 119 base::ConditionVariable input_ready_; |
| 120 |
| 121 // Current input buffer at decoder. |
| 122 linked_ptr<InputBuffer> curr_input_buffer_; |
| 123 |
| 124 // ChildThread's message loop |
| 125 base::MessageLoop* message_loop_; |
| 126 |
| 127 // To expose client callbacks from VideoDecodeAccelerator. |
| 128 // NOTE: all calls to these objects *MUST* be executed on message_loop_. |
| 129 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; |
| 130 base::WeakPtr<Client> client_; |
| 131 |
| 132 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder |
| 133 // thread back to the ChildThread. Because the decoder thread is a member of |
| 134 // this class, any task running on the decoder thread is guaranteed that this |
| 135 // object is still alive. As a result, tasks posted from ChildThread to |
| 136 // decoder thread should use base::Unretained(this), and tasks posted from the |
| 137 // decoder thread to the ChildThread should use |weak_this_|. |
| 138 base::WeakPtr<VaapiJpegDecodeAccelerator> weak_this_; |
| 139 |
| 140 // Callback used when creating VASurface objects. |
| 141 VASurface::ReleaseCB va_surface_release_cb_; |
| 142 |
| 143 scoped_ptr<VaapiWrapper> vaapi_wrapper_; |
| 144 |
| 145 // Comes after vaapi_wrapper_ to ensure its destructor is executed before |
| 146 // vaapi_wrapper_ is destroyed. |
| 147 scoped_ptr<VaapiJpegDecoder> decoder_; |
| 148 base::Thread decoder_thread_; |
| 149 // Use this to post tasks to |decoder_thread_| instead of |
| 150 // |decoder_thread_.message_loop()| because the latter will be NULL once |
| 151 // |decoder_thread_.Stop()| returns. |
| 152 scoped_refptr<base::MessageLoopProxy> decoder_thread_proxy_; |
| 153 |
| 154 // The WeakPtrFactory for |weak_this_|. |
| 155 base::WeakPtrFactory<VaapiJpegDecodeAccelerator> weak_this_factory_; |
| 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(VaapiJpegDecodeAccelerator); |
| 158 }; |
| 159 |
| 160 } // namespace content |
| 161 |
| 162 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
OLD | NEW |