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 const scoped_refptr<base::MessageLoopProxy>& io_message_loop); |
| 47 ~VaapiJpegDecodeAccelerator() override; |
| 48 |
| 49 // media::JpegDecodeAccelerator implementation. |
| 50 bool Initialize(media::JpegDecodeAccelerator::Client* client) override; |
| 51 void Decode(const media::BitstreamBuffer& bitstream_buffer, |
| 52 const scoped_refptr<media::VideoFrame>& video_frame) override; |
| 53 void Destroy() override; |
| 54 |
| 55 private: |
| 56 // Notify the client that an error has occurred and decoding cannot continue. |
| 57 void NotifyError(int32_t bitstream_buffer_id, Error error); |
| 58 |
| 59 // Queue decoding request. |
| 60 void QueueNewDecodeRequest( |
| 61 const media::BitstreamBuffer& bitstream_buffer, |
| 62 const scoped_refptr<media::VideoFrame>& video_frame); |
| 63 |
| 64 // Process one debuging request in the queue. |
| 65 void DecodeTask(); |
| 66 |
| 67 // Helper for Destroy(), doing all the actual work except for deleting self. |
| 68 void Cleanup(); |
| 69 |
| 70 // Puts contents of |va_surface| into given |video_frame|, releases the |
| 71 // surface and passes the resulting picture to client for output. |
| 72 bool OutputPicture(VASurfaceID va_surface_id, |
| 73 int32_t input_id, |
| 74 const scoped_refptr<media::VideoFrame>& video_frame); |
| 75 |
| 76 // VAVDA state. |
| 77 enum State { |
| 78 // Initialize() not called yet or failed. |
| 79 kUninitialized, |
| 80 // Initialize() succeeded. |
| 81 kInitialized, |
| 82 }; |
| 83 |
| 84 // Protects |decode_requests_| and |state_|. |
| 85 base::Lock lock_; |
| 86 State state_; |
| 87 |
| 88 // An input buffer awaiting consumption, provided by the client. |
| 89 struct DecodeRequest { |
| 90 DecodeRequest(media::BitstreamBuffer bitstream_buffer, |
| 91 scoped_refptr<media::VideoFrame> video_frame); |
| 92 ~DecodeRequest(); |
| 93 |
| 94 media::BitstreamBuffer bitstream_buffer; |
| 95 scoped_refptr<media::VideoFrame> video_frame; |
| 96 }; |
| 97 |
| 98 // Queue for incoming decode requests. |
| 99 typedef std::queue<linked_ptr<DecodeRequest>> DecodeRequests; |
| 100 DecodeRequests decode_requests_; |
| 101 |
| 102 // ChildThread's message loop |
| 103 base::MessageLoop* message_loop_; |
| 104 |
| 105 // GPU IO message loop. |
| 106 scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
| 107 |
| 108 // To expose client callbacks from JpegDecodeAccelerator. |
| 109 // NOTE: all calls to these objects *MUST* be executed on message_loop_. |
| 110 scoped_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; |
| 111 base::WeakPtr<Client> client_; |
| 112 |
| 113 // WeakPtr<> pointing to |this| for use in posting tasks from the decoder |
| 114 // thread back to the ChildThread. Because the decoder thread is a member of |
| 115 // this class, any task running on the decoder thread is guaranteed that this |
| 116 // object is still alive. As a result, tasks posted from ChildThread to |
| 117 // decoder thread should use base::Unretained(this), and tasks posted from the |
| 118 // decoder thread to the ChildThread should use |weak_this_|. |
| 119 base::WeakPtr<VaapiJpegDecodeAccelerator> weak_this_; |
| 120 |
| 121 scoped_ptr<VaapiWrapper> vaapi_wrapper_; |
| 122 |
| 123 // Comes after vaapi_wrapper_ to ensure its destructor is executed before |
| 124 // vaapi_wrapper_ is destroyed. |
| 125 scoped_ptr<VaapiJpegDecoder> decoder_; |
| 126 base::Thread decoder_thread_; |
| 127 // Use this to post tasks to |decoder_thread_| instead of |
| 128 // |decoder_thread_.message_loop()| because the latter will be NULL once |
| 129 // |decoder_thread_.Stop()| returns. |
| 130 scoped_refptr<base::MessageLoopProxy> decoder_thread_proxy_; |
| 131 |
| 132 // The WeakPtrFactory for |weak_this_|. |
| 133 base::WeakPtrFactory<VaapiJpegDecodeAccelerator> weak_this_factory_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(VaapiJpegDecodeAccelerator); |
| 136 }; |
| 137 |
| 138 } // namespace content |
| 139 |
| 140 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_JPEG_DECODE_ACCELERATOR_H_ |
OLD | NEW |