D3D11ShareHandleImage.h (3788B)
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_SHARE_HANDLE_IMAGE_H 8 #define GFX_D311_SHARE_HANDLE_IMAGE_H 9 10 #include "ImageContainer.h" 11 #include "d3d11.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/gfx/Types.h" 14 #include "mozilla/layers/TextureClient.h" 15 #include "mozilla/layers/TextureClientRecycleAllocator.h" 16 #include "mozilla/layers/TextureD3D11.h" 17 18 namespace mozilla { 19 namespace gl { 20 class GLBlitHelper; 21 } 22 namespace layers { 23 24 class D3D11RecycleAllocator final : public TextureClientRecycleAllocator { 25 public: 26 D3D11RecycleAllocator(KnowsCompositor* aAllocator, ID3D11Device* aDevice, 27 gfx::SurfaceFormat aPreferredFormat); 28 29 already_AddRefed<TextureClient> CreateOrRecycleClient( 30 gfx::ColorSpace2 aColorSpace, gfx::ColorRange aColorRange, 31 const gfx::IntSize& aSize); 32 33 void SetPreferredSurfaceFormat(gfx::SurfaceFormat aPreferredFormat); 34 gfx::SurfaceFormat GetUsableSurfaceFormat() const { 35 return mUsableSurfaceFormat; 36 } 37 38 RefPtr<ID3D11Texture2D> GetStagingTextureNV12(gfx::IntSize aSize); 39 40 void SetSyncObject(RefPtr<SyncObjectClient>& aSyncObject) { 41 mSyncObject = aSyncObject; 42 } 43 44 RefPtr<SyncObjectClient> GetSyncObject() { return mSyncObject; } 45 46 const RefPtr<ID3D11Device> mDevice; 47 const bool mCanUseNV12; 48 const bool mCanUseP010; 49 const bool mCanUseP016; 50 51 private: 52 /** 53 * Used for checking if CompositorDevice/ContentDevice is updated. 54 */ 55 RefPtr<ID3D11Device> mImageDevice; 56 gfx::SurfaceFormat mUsableSurfaceFormat; 57 58 RefPtr<ID3D11Texture2D> mStagingTexture; 59 gfx::IntSize mStagingTextureSize; 60 61 RefPtr<SyncObjectClient> mSyncObject; 62 }; 63 64 // Image class that wraps a ID3D11Texture2D. This class copies the image 65 // passed into SetData(), so that it can be accessed from other D3D devices. 66 // This class also manages the synchronization of the copy, to ensure the 67 // resource is ready to use. 68 class D3D11ShareHandleImage final : public Image { 69 public: 70 D3D11ShareHandleImage(const gfx::IntSize& aSize, const gfx::IntRect& aRect, 71 gfx::ColorSpace2 aColorSpace, 72 gfx::ColorRange aColorRange, 73 gfx::ColorDepth aColorDepth); 74 virtual ~D3D11ShareHandleImage() = default; 75 76 bool AllocateTexture(D3D11RecycleAllocator* aAllocator, 77 ID3D11Device* aDevice); 78 79 gfx::IntSize GetSize() const override; 80 already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override; 81 nsresult BuildSurfaceDescriptorBuffer( 82 SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags, 83 const std::function<MemoryOrShmem(uint32_t)>& aAllocate) override; 84 TextureClient* GetTextureClient(KnowsCompositor* aKnowsCompositor) override; 85 gfx::IntRect GetPictureRect() const override { return mPictureRect; } 86 87 ID3D11Texture2D* GetTexture() const; 88 89 gfx::ColorRange GetColorRange() const { return mColorRange; } 90 91 gfx::ColorDepth GetColorDepth() const override { return mColorDepth; } 92 93 private: 94 friend class gl::GLBlitHelper; 95 D3D11TextureData* GetData() const { 96 if (!mTextureClient) { 97 return nullptr; 98 } 99 return mTextureClient->GetInternalData()->AsD3D11TextureData(); 100 } 101 102 gfx::IntSize mSize; 103 gfx::IntRect mPictureRect; 104 105 public: 106 const gfx::ColorSpace2 mColorSpace; 107 108 private: 109 gfx::ColorRange mColorRange; 110 gfx::ColorDepth mColorDepth; 111 RefPtr<TextureClient> mTextureClient; 112 RefPtr<ID3D11Texture2D> mTexture; 113 }; 114 115 } // namespace layers 116 } // namespace mozilla 117 118 #endif // GFX_D3DSURFACEIMAGE_H