vaapi_utils.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2018 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. #ifndef MEDIA_GPU_VAAPI_VAAPI_UTILS_H_
  5. #define MEDIA_GPU_VAAPI_VAAPI_UTILS_H_
  6. #include <va/va.h>
  7. #include "base/callback_forward.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/thread_annotations.h"
  11. #include "ui/gfx/geometry/size.h"
  12. namespace base {
  13. class Lock;
  14. }
  15. namespace media {
  16. class VaapiWrapper;
  17. class Vp8ReferenceFrameVector;
  18. struct VAContextAndScopedVASurfaceDeleter;
  19. struct Vp8FrameHeader;
  20. // Class to map a given VABuffer, identified by |buffer_id|, for its lifetime.
  21. // The |lock_| might be null depending on the user of this class. If |lock_| is
  22. // not null, this class must operate under |lock_| acquired.
  23. class ScopedVABufferMapping {
  24. public:
  25. // |release_callback| will be called if the mapping of the buffer failed.
  26. ScopedVABufferMapping(const base::Lock* lock,
  27. VADisplay va_display,
  28. VABufferID buffer_id,
  29. base::OnceCallback<void(VABufferID)> release_callback =
  30. base::NullCallback());
  31. ScopedVABufferMapping(const ScopedVABufferMapping&) = delete;
  32. ScopedVABufferMapping& operator=(const ScopedVABufferMapping&) = delete;
  33. ~ScopedVABufferMapping();
  34. bool IsValid() const {
  35. CHECK(sequence_checker_.CalledOnValidSequence());
  36. return !!va_buffer_data_;
  37. }
  38. void* data() const {
  39. DCHECK(IsValid());
  40. CHECK(sequence_checker_.CalledOnValidSequence());
  41. return va_buffer_data_;
  42. }
  43. // Explicit destruction method, to retrieve the success/error result. It is
  44. // safe to call this method several times.
  45. VAStatus Unmap();
  46. private:
  47. raw_ptr<const base::Lock> lock_; // Only for AssertAcquired() calls.
  48. const VADisplay va_display_;
  49. const VABufferID buffer_id_;
  50. base::SequenceCheckerImpl sequence_checker_;
  51. void* va_buffer_data_ = nullptr;
  52. };
  53. // This class tracks the VABuffer life cycle from vaCreateBuffer() to
  54. // vaDestroyBuffer(). Users of this class are responsible for mapping and
  55. // unmapping the buffer as needed. The |lock_| might be null depending on the
  56. // user of this class. If |lock_| is not null, |lock_| is acquired for
  57. // destruction purposes.
  58. class ScopedVABuffer {
  59. public:
  60. // Creates ScopedVABuffer. Returns nullptr if creating the va buffer fails.
  61. static std::unique_ptr<ScopedVABuffer> Create(base::Lock* lock,
  62. VADisplay va_display,
  63. VAContextID va_context_id,
  64. VABufferType va_buffer_type,
  65. size_t size);
  66. static std::unique_ptr<ScopedVABuffer>
  67. CreateForTesting(VABufferID buffer_id, VABufferType buffer_type, size_t size);
  68. ScopedVABuffer(const ScopedVABuffer&) = delete;
  69. ScopedVABuffer& operator=(const ScopedVABuffer&) = delete;
  70. ~ScopedVABuffer();
  71. VABufferID id() const {
  72. CHECK(sequence_checker_.CalledOnValidSequence());
  73. return va_buffer_id_;
  74. }
  75. VABufferType type() const {
  76. CHECK(sequence_checker_.CalledOnValidSequence());
  77. return va_buffer_type_;
  78. }
  79. size_t size() const {
  80. CHECK(sequence_checker_.CalledOnValidSequence());
  81. return size_;
  82. }
  83. private:
  84. ScopedVABuffer(base::Lock* lock,
  85. VADisplay va_display,
  86. VABufferID va_buffer_id,
  87. VABufferType va_buffer_type,
  88. size_t size);
  89. const raw_ptr<base::Lock> lock_;
  90. const VADisplay va_display_ GUARDED_BY(lock_);
  91. base::SequenceCheckerImpl sequence_checker_;
  92. const VABufferID va_buffer_id_;
  93. const VABufferType va_buffer_type_;
  94. const size_t size_;
  95. };
  96. // This class tracks the VAImage life cycle from vaCreateImage() - vaGetImage()
  97. // to vaDestroyImage(). In between creation and destruction, image()->buf will
  98. // try to be be mapped on user space using a ScopedVABufferMapping. All
  99. // resources will be cleaned up appropriately. The |lock_| might be null
  100. // depending on the user of this class. If |lock_| is not null, |lock_| is
  101. // acquired for destruction purposes.
  102. class ScopedVAImage {
  103. public:
  104. ScopedVAImage(base::Lock* lock,
  105. VADisplay va_display,
  106. VASurfaceID va_surface_id,
  107. VAImageFormat* format /* Needs to be a pointer for libva */,
  108. const gfx::Size& size);
  109. ScopedVAImage(const ScopedVAImage&) = delete;
  110. ScopedVAImage& operator=(const ScopedVAImage&) = delete;
  111. ~ScopedVAImage();
  112. bool IsValid() const { return va_buffer_ && va_buffer_->IsValid(); }
  113. const VAImage* image() const {
  114. CHECK(sequence_checker_.CalledOnValidSequence());
  115. return image_.get();
  116. }
  117. const ScopedVABufferMapping* va_buffer() const {
  118. DCHECK(IsValid());
  119. CHECK(sequence_checker_.CalledOnValidSequence());
  120. return va_buffer_.get();
  121. }
  122. private:
  123. raw_ptr<base::Lock> lock_;
  124. const VADisplay va_display_ GUARDED_BY(lock_);
  125. std::unique_ptr<VAImage> image_;
  126. std::unique_ptr<ScopedVABufferMapping> va_buffer_;
  127. base::SequenceCheckerImpl sequence_checker_;
  128. };
  129. // A VA-API-specific surface used by video/image codec accelerators to work on.
  130. // As the name suggests, this class is self-cleaning.
  131. class ScopedVASurface {
  132. public:
  133. ScopedVASurface(scoped_refptr<VaapiWrapper> vaapi_wrapper,
  134. VASurfaceID va_surface_id,
  135. const gfx::Size& size,
  136. unsigned int va_rt_format);
  137. ScopedVASurface(const ScopedVASurface&) = delete;
  138. ScopedVASurface& operator=(const ScopedVASurface&) = delete;
  139. ~ScopedVASurface();
  140. bool IsValid() const;
  141. VASurfaceID id() const { return va_surface_id_; }
  142. const gfx::Size& size() const { return size_; }
  143. unsigned int format() const { return va_rt_format_; }
  144. private:
  145. friend struct VAContextAndScopedVASurfaceDeleter;
  146. const scoped_refptr<VaapiWrapper> vaapi_wrapper_;
  147. const VASurfaceID va_surface_id_;
  148. const gfx::Size size_;
  149. const unsigned int va_rt_format_;
  150. };
  151. // A combination of a numeric ID |id| and a callback to release it. This class
  152. // makes no assumptions on threading or lifetimes; |release_cb_| must provide
  153. // for this.
  154. // ScopedID allows for object-specific release callbacks, whereas
  155. // unique_ptr::deleter_type (or base::ScopedGeneric) only supports free
  156. // functions (or class-static methods) for freeing.
  157. template <typename T>
  158. class ScopedID {
  159. public:
  160. using ReleaseCB = base::OnceCallback<void(T)>;
  161. ScopedID(T id, ReleaseCB release_cb)
  162. : id_(id), release_cb_(std::move(release_cb)) {
  163. DCHECK(release_cb_);
  164. static_assert(std::is_integral<T>::value, "T must be a numeric type.");
  165. }
  166. ~ScopedID() { std::move(release_cb_).Run(id_); }
  167. ScopedID& operator=(const ScopedID&) = delete;
  168. ScopedID(const ScopedID&) = delete;
  169. T id() const { return id_; }
  170. private:
  171. const T id_;
  172. ReleaseCB release_cb_;
  173. };
  174. // Adapts |frame_header| to the Vaapi data types.
  175. void FillVP8DataStructures(const Vp8FrameHeader& frame_header,
  176. const Vp8ReferenceFrameVector& reference_frames,
  177. VAIQMatrixBufferVP8* iq_matrix_buf,
  178. VAProbabilityDataBufferVP8* prob_buf,
  179. VAPictureParameterBufferVP8* pic_param,
  180. VASliceParameterBufferVP8* slice_param);
  181. bool IsValidVABufferType(VABufferType type);
  182. } // namespace media
  183. #endif // MEDIA_GPU_VAAPI_VAAPI_UTILS_H_