tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

D3D11ZeroCopyTextureImage.h (4994B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef GFX_D311_TEXTURE_IMF_SAMPLE_IMAGE_H
      8 #define GFX_D311_TEXTURE_IMF_SAMPLE_IMAGE_H
      9 
     10 #include "ImageContainer.h"
     11 #include "mozilla/RefPtr.h"
     12 #include "mozilla/gfx/Types.h"
     13 #include "mozilla/layers/TextureClient.h"
     14 #include "mozilla/layers/TextureD3D11.h"
     15 #include "mozilla/ThreadSafeWeakPtr.h"
     16 
     17 struct ID3D11Texture2D;
     18 struct IMFSample;
     19 
     20 namespace mozilla {
     21 
     22 class D3D11TextureWrapper;
     23 
     24 namespace gl {
     25 class GLBlitHelper;
     26 }
     27 namespace layers {
     28 
     29 class FenceD3D11;
     30 
     31 class ZeroCopyUsageInfo final {
     32 public:
     33  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ZeroCopyUsageInfo)
     34 
     35  ZeroCopyUsageInfo() = default;
     36 
     37  bool SupportsZeroCopyNV12Texture() { return mSupportsZeroCopyNV12Texture; }
     38  void DisableZeroCopyNV12Texture() { mSupportsZeroCopyNV12Texture = false; }
     39 
     40 protected:
     41  ~ZeroCopyUsageInfo() = default;
     42 
     43  Atomic<bool> mSupportsZeroCopyNV12Texture{true};
     44 };
     45 
     46 // A shared ID3D11Texture2D created by the compositor device.
     47 // Expected to be used in GPU process.
     48 class D3D11ZeroCopyTextureImage : public Image {
     49 public:
     50  D3D11ZeroCopyTextureImage(
     51      ID3D11Texture2D* aTexture, const uint32_t aArrayIndex,
     52      const gfx::IntSize& aSize, const gfx::IntRect& aRect,
     53      const gfx::SurfaceFormat aFormat, const gfx::ColorSpace2 aColorSpace,
     54      const gfx::ColorRange aColorRange, const gfx::ColorDepth aColorDepth);
     55  virtual ~D3D11ZeroCopyTextureImage();
     56 
     57  void AllocateTextureClient(KnowsCompositor* aKnowsCompositor,
     58                             RefPtr<ZeroCopyUsageInfo> aUsageInfo,
     59                             const RefPtr<FenceD3D11> aWriteFence);
     60 
     61  gfx::IntSize GetSize() const override;
     62  already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override;
     63  nsresult BuildSurfaceDescriptorBuffer(
     64      SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags,
     65      const std::function<MemoryOrShmem(uint32_t)>& aAllocate) override;
     66  TextureClient* GetTextureClient(KnowsCompositor* aKnowsCompositor) override;
     67  gfx::IntRect GetPictureRect() const override { return mPictureRect; }
     68 
     69  ID3D11Texture2D* GetTexture() const;
     70 
     71  gfx::ColorRange GetColorRange() const { return mColorRange; }
     72 
     73  gfx::ColorDepth GetColorDepth() const override { return mColorDepth; }
     74 
     75 protected:
     76  friend class gl::GLBlitHelper;
     77  D3D11TextureData* GetData() const {
     78    if (!mTextureClient) {
     79      return nullptr;
     80    }
     81    return mTextureClient->GetInternalData()->AsD3D11TextureData();
     82  }
     83 
     84  RefPtr<ID3D11Texture2D> mTexture;
     85  RefPtr<TextureClient> mTextureClient;
     86 
     87 public:
     88  const uint32_t mArrayIndex;
     89  const gfx::IntSize mSize;
     90  const gfx::IntRect mPictureRect;
     91  const gfx::SurfaceFormat mFormat;
     92  const gfx::ColorSpace2 mColorSpace;
     93  const gfx::ColorRange mColorRange;
     94  const gfx::ColorDepth mColorDepth;
     95 };
     96 
     97 class IMFSampleWrapper : public SupportsThreadSafeWeakPtr<IMFSampleWrapper> {
     98 public:
     99  MOZ_DECLARE_REFCOUNTED_TYPENAME(IMFSampleWrapper)
    100 
    101  static RefPtr<IMFSampleWrapper> Create(IMFSample* aVideoSample);
    102  virtual ~IMFSampleWrapper();
    103  void ClearVideoSample();
    104 
    105 protected:
    106  explicit IMFSampleWrapper(IMFSample* aVideoSample);
    107 
    108  RefPtr<IMFSample> mVideoSample;
    109 };
    110 
    111 // Image class that wraps ID3D11Texture2D of IMFSample
    112 // Expected to be used in GPU process.
    113 class D3D11TextureIMFSampleImage final : public D3D11ZeroCopyTextureImage {
    114 public:
    115  D3D11TextureIMFSampleImage(IMFSample* aVideoSample, ID3D11Texture2D* aTexture,
    116                             const uint32_t aArrayIndex,
    117                             const gfx::IntSize& aSize,
    118                             const gfx::IntRect& aRect,
    119                             const gfx::SurfaceFormat aFormat,
    120                             const gfx::ColorSpace2 aColorSpace,
    121                             const gfx::ColorRange aColorRange,
    122                             const gfx::ColorDepth aColorDepth);
    123  virtual ~D3D11TextureIMFSampleImage() = default;
    124 
    125  RefPtr<IMFSampleWrapper> GetIMFSampleWrapper();
    126 
    127 private:
    128  RefPtr<IMFSampleWrapper> mVideoSample;
    129 };
    130 
    131 // Image class that wraps ID3D11Texture2D of AVFrame from ffvpx hw decoder
    132 // Expected to be used in GPU process.
    133 class D3D11TextureAVFrameImage final : public D3D11ZeroCopyTextureImage {
    134 public:
    135  D3D11TextureAVFrameImage(D3D11TextureWrapper* aWrapper,
    136                           const gfx::IntSize& aSize, const gfx::IntRect& aRect,
    137                           const gfx::ColorSpace2 aColorSpace,
    138                           const gfx::ColorRange aColorRange,
    139                           const gfx::ColorDepth aColorDepth);
    140  virtual ~D3D11TextureAVFrameImage() = default;
    141 
    142 private:
    143  UniquePtr<D3D11TextureWrapper> mWrapper;
    144 };
    145 
    146 }  // namespace layers
    147 }  // namespace mozilla
    148 
    149 #endif  // GFX_D311_TEXTURE_IMF_SAMPLE_IMAGE_H