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 #ifndef CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
| 6 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
| 7 |
| 8 #include <queue> |
| 9 |
| 10 #include "base/memory/linked_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "content/common/content_export.h" |
| 17 #include "content/common/gpu/media/vaapi_jpeg_decoder.h" |
| 18 #include "content/common/gpu/media/vaapi_wrapper.h" |
| 19 #include "media/base/bitstream_buffer.h" |
| 20 #include "media/video/jpeg_decode_accelerator.h" |
| 21 |
| 22 namespace content { |
| 23 |
| 24 // Class to provide JPEG decode acceleration for Intel systems with hardware |
| 25 // support for it, and on which libva is available. |
| 26 // Decoding tasks are performed in a separate decoding thread. |
| 27 // |
| 28 // Threading/life-cycle: this object is created & destroyed on the GPU |
| 29 // ChildThread. A few methods on it are called on the decoder thread which is |
| 30 // stopped during |this->Destroy()|, so any tasks posted to the decoder thread |
| 31 // can assume |*this| is still alive. See |weak_this_| below for more details. |
| 32 class CONTENT_EXPORT VaapiJpegDecodeAccelerator |
| 33 : public media::JpegDecodeAccelerator { |
| 34 public: |
| 35 VaapiJpegDecodeAccelerator( |
| 36 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); |
| 37 ~VaapiJpegDecodeAccelerator() override; |
| 38 |
| 39 // media::JpegDecodeAccelerator implementation. |
| 40 bool Initialize(media::JpegDecodeAccelerator::Client* client) override; |
| 41 void Decode(const media::BitstreamBuffer& bitstream_buffer, |
| 42 const scoped_refptr<media::VideoFrame>& video_frame) override; |
| 43 void Destroy() override; |
| 44 |
| 45 private: |
| 46 // Notifies the client that an error has occurred and decoding cannot |
| 47 // continue. |
| 48 void NotifyError(int32_t bitstream_buffer_id, Error error); |
| 49 |
| 50 // Processes one decode request in the queue. |
| 51 void DecodeTask(); |
| 52 |
| 53 // Helper for Destroy(), doing all the actual work except for deleting self. |
| 54 void Cleanup(); |
| 55 |
| 56 // Puts contents of |va_surface| into given |video_frame|, releases the |
| 57 // surface and passes the buffer id of the resulting picture to client for |
| 58 // output. |
| 59 bool OutputPicture(VASurfaceID va_surface_id, |
| 60 int32_t input_buffer_id, |
| 61 const scoped_refptr<media::VideoFrame>& video_frame); |
| 62 |
| 63 // VAVDA state. |
| 64 enum State { |
| 65 // Initialize() not called yet or failed. |
| 66 kUninitialized, |
| 67 // Initialize() succeeded. |
| 68 kInitialized, |
| 69 }; |
| 70 |
| 71 // Protects |decode_requests_| and |initialized_|. |
| 72 base::Lock lock_; |
| 73 bool initialized_; |
| 74 |
| 75 // An input buffer and the corresponding output video frame awaiting |
| 76 // consumption, provided by the client. |
| 77 struct DecodeRequest { |
| 78 DecodeRequest(const media::BitstreamBuffer& bitstream_buffer, |
| 79 scoped_refptr<media::VideoFrame> video_frame); |
| 80 ~DecodeRequest(); |
| 81 |
| 82 media::BitstreamBuffer bitstream_buffer; |
| 83 scoped_refptr<media::VideoFrame> video_frame; |
| 84 }; |
| 85 |
| 86 // Queue for incoming decode requests. |
| 87 typedef std::queue<linked_ptr<DecodeRequest>> DecodeRequests; |
| 88 DecodeRequests decode_requests_; |
| 89 |
| 90 // ChildThread's task runner. |
| 91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 92 |
| 93 // GPU IO task runner. |
| 94 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 95 |
| 96 // To expose client callbacks from JpegDecodeAccelerator. |
| 97 // NOTE: all calls to these objects *MUST* be executed on task_runner_. |
| 98 scoped_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; |
| 99 base::WeakPtr<Client> client_; |
| 100 |
| 101 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder |
| 102 // thread back to the ChildThread. Because the decoder thread is a member of |
| 103 // this class, any task running on the decoder thread is guaranteed that this |
| 104 // object is still alive. As a result, tasks posted from ChildThread to |
| 105 // decoder thread should use base::Unretained(this), and tasks posted from the |
| 106 // decoder thread to the ChildThread should use |weak_this_|. |
| 107 base::WeakPtr<VaapiJpegDecodeAccelerator> weak_this_; |
| 108 |
| 109 scoped_ptr<VaapiWrapper> vaapi_wrapper_; |
| 110 |
| 111 // Comes after vaapi_wrapper_ to ensure its destructor is executed before |
| 112 // vaapi_wrapper_ is destroyed. |
| 113 scoped_ptr<VaapiJpegDecoder> decoder_; |
| 114 base::Thread decoder_thread_; |
| 115 // Use this to post tasks to |decoder_thread_| instead of |
| 116 // |decoder_thread_.task_runner()| because the latter will be NULL once |
| 117 // |decoder_thread_.Stop()| returns. |
| 118 scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_; |
| 119 |
| 120 VASurfaceID va_surface_; |
| 121 gfx::Size last_coded_size_; |
| 122 |
| 123 // The WeakPtrFactory for |weak_this_|. |
| 124 base::WeakPtrFactory<VaapiJpegDecodeAccelerator> weak_this_factory_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(VaapiJpegDecodeAccelerator); |
| 127 }; |
| 128 |
| 129 } // namespace content |
| 130 |
| 131 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
OLD | NEW |