vaapi_image_decoder.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 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_IMAGE_DECODER_H_
  5. #define MEDIA_GPU_VAAPI_VAAPI_IMAGE_DECODER_H_
  6. #include <stdint.h>
  7. #include <va/va.h>
  8. #include <memory>
  9. #include "base/callback_forward.h"
  10. #include "base/containers/span.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "gpu/config/gpu_info.h"
  13. #include "third_party/skia/include/core/SkImageInfo.h"
  14. namespace gfx {
  15. class NativePixmapDmaBuf;
  16. } // namespace gfx
  17. namespace media {
  18. struct NativePixmapAndSizeInfo;
  19. class ScopedVASurface;
  20. class VaapiWrapper;
  21. enum class VaapiFunctions;
  22. using ReportErrorToUMACB = base::RepeatingCallback<void(VaapiFunctions)>;
  23. struct VAContextAndScopedVASurfaceDeleter {
  24. void operator()(ScopedVASurface* scoped_va_surface) const;
  25. };
  26. using ScopedVAContextAndSurface =
  27. std::unique_ptr<ScopedVASurface, VAContextAndScopedVASurfaceDeleter>;
  28. enum class VaapiImageDecodeStatus : uint32_t {
  29. kSuccess,
  30. kParseFailed,
  31. kUnsupportedImage,
  32. kUnsupportedSubsampling,
  33. kSurfaceCreationFailed,
  34. kSubmitVABuffersFailed,
  35. kExecuteDecodeFailed,
  36. kUnsupportedSurfaceFormat,
  37. kCannotGetImage,
  38. kCannotExportSurface,
  39. kInvalidState,
  40. };
  41. // This class abstracts the idea of VA-API format-specific decoders. It is the
  42. // responsibility of each subclass to initialize |vaapi_wrapper_| appropriately
  43. // for the purpose of performing hardware-accelerated image decodes of a
  44. // particular format (e.g. JPEG or WebP). Objects of this class are not
  45. // thread-safe, but they are also not thread-affine, i.e., the caller is free to
  46. // call the methods on any thread, but calls must be synchronized externally.
  47. class VaapiImageDecoder {
  48. public:
  49. VaapiImageDecoder(const VaapiImageDecoder&) = delete;
  50. VaapiImageDecoder& operator=(const VaapiImageDecoder&) = delete;
  51. virtual ~VaapiImageDecoder();
  52. // Initializes |vaapi_wrapper_| in kDecode mode with the
  53. // appropriate VAAPI profile and |error_uma_cb| for error reporting.
  54. virtual bool Initialize(const ReportErrorToUMACB& error_uma_cb);
  55. // Decodes a picture. It will fill VA-API parameters and call the
  56. // corresponding VA-API methods according to the image in |encoded_image|.
  57. // The image will be decoded into an internally allocated ScopedVASurface.
  58. // This VA surface will remain valid until the next Decode() call or
  59. // destruction of this class. Returns a VaapiImageDecodeStatus that will
  60. // indicate whether the decode succeeded or the reason it failed. Note that
  61. // the internal ScopedVASurface is destroyed on failure.
  62. virtual VaapiImageDecodeStatus Decode(
  63. base::span<const uint8_t> encoded_image);
  64. // Returns a pointer to the internally managed ScopedVASurface.
  65. virtual const ScopedVASurface* GetScopedVASurface() const;
  66. // Returns the type of image supported by this decoder.
  67. virtual gpu::ImageDecodeAcceleratorType GetType() const = 0;
  68. // Returns the type of mapping needed to convert the NativePixmapDmaBuf
  69. // returned by ExportAsNativePixmapDmaBuf() from YUV to RGB.
  70. virtual SkYUVColorSpace GetYUVColorSpace() const = 0;
  71. // Returns the image profile supported by this decoder.
  72. virtual gpu::ImageDecodeAcceleratorSupportedProfile GetSupportedProfile()
  73. const;
  74. // Exports the decoded data from the last Decode() call as a
  75. // gfx::NativePixmapDmaBuf. Returns nullptr on failure and sets *|status| to
  76. // the reason for failure. On success, the image decoder gives up ownership of
  77. // the buffer underlying the NativePixmapDmaBuf.
  78. virtual std::unique_ptr<NativePixmapAndSizeInfo> ExportAsNativePixmapDmaBuf(
  79. VaapiImageDecodeStatus* status);
  80. protected:
  81. explicit VaapiImageDecoder(VAProfile va_profile);
  82. ScopedVAContextAndSurface scoped_va_context_and_surface_;
  83. scoped_refptr<VaapiWrapper> vaapi_wrapper_;
  84. private:
  85. // Submits an image to the VA-API by filling its parameters and calling on the
  86. // corresponding methods according to the image in |encoded_image|. Returns a
  87. // VaapiImageDecodeStatus that will indicate whether the operation succeeded
  88. // or the reason it failed.
  89. virtual VaapiImageDecodeStatus AllocateVASurfaceAndSubmitVABuffers(
  90. base::span<const uint8_t> encoded_image) = 0;
  91. // The VA profile used for the current image decoder.
  92. const VAProfile va_profile_;
  93. };
  94. } // namespace media
  95. #endif // MEDIA_GPU_VAAPI_VAAPI_IMAGE_DECODER_H_