va_surface.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2013 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 the definition of VASurface class, used for decoding by
  6. // VaapiVideoDecodeAccelerator and VaapiH264Decoder.
  7. #ifndef MEDIA_GPU_VAAPI_VA_SURFACE_H_
  8. #define MEDIA_GPU_VAAPI_VA_SURFACE_H_
  9. #include <va/va.h>
  10. #include "base/callback.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "ui/gfx/geometry/size.h"
  13. namespace media {
  14. // A VA-API-specific decode surface used by VaapiH264Decoder to decode into
  15. // and use as reference for decoding other surfaces. It is also handed by the
  16. // decoder to VaapiVideoDecodeAccelerator when the contents of the surface are
  17. // ready and should be displayed. VAVDA converts the surface contents into an
  18. // X/Drm Pixmap bound to a texture for display and releases its reference to it.
  19. // Decoder releases its references to the surface when it's done decoding and
  20. // using it as reference. Note that a surface may still be used for reference
  21. // after it's been sent to output and also after it is no longer used by VAVDA.
  22. // Thus, the surface can be in use by both VAVDA and the Decoder at the same
  23. // time, or by either of them, with the restriction that VAVDA will never get
  24. // the surface until the contents are ready, and it is guaranteed that the
  25. // contents will not change after that.
  26. // When both the decoder and VAVDA release their references to the surface,
  27. // it is freed and the release callback is executed to put the surface back
  28. // into available surfaces pool, which is managed externally.
  29. //
  30. // VASurfaceID is allocated in VaapiWrapper.
  31. // |
  32. // +----->|
  33. // | v
  34. // | VASurfaceID is put onto VaapiVideoDecodeAccelerator::available_va_surfaces_
  35. // | | list.
  36. // | v
  37. // | VASurfaceID is taken off of the VVDA:available_va_surfaces_ when
  38. // | | VaapiH264Decoder requests more output surfaces, is wrapped into
  39. // | | a VASurface and passed to VaapiH264Decoder.
  40. // | v
  41. // | VASurface is put onto VaapiH264Decoder::available_va_surfaces_, keeping
  42. // | | the only reference to it until it's needed for decoding.
  43. // | v
  44. // | VaapiH264Decoder starts decoding a new frame. It takes a VASurface off of
  45. // | | VHD::available_va_surfaces_ and assigns it to a DecodeSurface,
  46. // | | which now keeps the only reference.
  47. // | v
  48. // | DecodeSurface is used for decoding, putting data into associated VASurface.
  49. // | |
  50. // | |--------------------------------------------------+
  51. // | | |
  52. // | v v
  53. // | DecodeSurface is to be output. VaapiH264Decoder uses the
  54. // | VaapiH264Decoder passes the associated DecodeSurface and associated
  55. // | VASurface to VaapiVideoDecodeAccelerator, VASurface as reference for
  56. // | which stores it (taking a ref) on decoding more frames.
  57. // | pending_output_cbs_ queue until an output |
  58. // | VaapiPicture becomes available. v
  59. // | | Once the DecodeSurface is not
  60. // | | needed as reference anymore,
  61. // | v it is released, releasing the
  62. // | A VaapiPicture becomes available after associated VASurface reference.
  63. // | the client of VVDA returns |
  64. // | a PictureBuffer associated with it. VVDA |
  65. // | puts the contents of the VASurface into |
  66. // | it and releases the reference to VASurface. |
  67. // | | |
  68. // | '---------------------------------------'
  69. // | |
  70. // | v
  71. // | Neither VVDA nor VHD hold a reference to VASurface. VASurface is released,
  72. // | ReleaseCB gets called in its destructor, which puts the associated
  73. // | VASurfaceID back onto VVDA::available_va_surfaces_.
  74. // | |
  75. // '-------------------------------------|
  76. // |
  77. // v
  78. // VaapiWrapper frees VASurfaceID.
  79. //
  80. class VASurface : public base::RefCountedThreadSafe<VASurface> {
  81. public:
  82. using ReleaseCB = base::OnceCallback<void(VASurfaceID)>;
  83. VASurface(VASurfaceID va_surface_id,
  84. const gfx::Size& size,
  85. unsigned int format,
  86. ReleaseCB release_cb);
  87. VASurface(const VASurface&) = delete;
  88. VASurface& operator=(const VASurface&) = delete;
  89. VASurfaceID id() const { return va_surface_id_; }
  90. const gfx::Size& size() const { return size_; }
  91. unsigned int format() const { return format_; }
  92. private:
  93. friend class base::RefCountedThreadSafe<VASurface>;
  94. ~VASurface();
  95. const VASurfaceID va_surface_id_;
  96. const gfx::Size size_;
  97. const unsigned int format_;
  98. ReleaseCB release_cb_;
  99. };
  100. } // namespace media
  101. #endif // MEDIA_GPU_VAAPI_VA_SURFACE_H_