TextureHostWrapperD3D11.h (5693B)
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 MOZILLA_GFX_TextureHostWrapperD3D11_H 8 #define MOZILLA_GFX_TextureHostWrapperD3D11_H 9 10 #include <deque> 11 #include <unordered_map> 12 13 #include "mozilla/layers/LayersTypes.h" 14 #include "mozilla/layers/TextureHost.h" 15 16 class nsIThreadPool; 17 struct ID3D11Texture2D; 18 19 namespace mozilla { 20 namespace layers { 21 22 class DXGITextureHostD3D11; 23 24 /** 25 * A Class that allocates and recycles ID3D11Texture2D in TextureUpdate thread. 26 * And manages in use TextureHostWrapperD3D11s in compositor thread. 27 */ 28 class TextureWrapperD3D11Allocator { 29 public: 30 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureWrapperD3D11Allocator) 31 32 TextureWrapperD3D11Allocator(); 33 34 RefPtr<ID3D11Texture2D> CreateOrRecycle(gfx::SurfaceFormat aSurfaceFormat, 35 gfx::IntSize aSize); 36 37 void EnsureStagingTextureNV12(RefPtr<ID3D11Device> aDevice); 38 39 RefPtr<ID3D11Texture2D> GetStagingTextureNV12(); 40 41 RefPtr<ID3D11Device> GetDevice(); 42 43 void RecycleTexture(RefPtr<ID3D11Texture2D>& aTexture); 44 45 void RegisterTextureHostWrapper(const wr::ExternalImageId& aExternalImageId, 46 RefPtr<TextureHost> aTextureHost); 47 void UnregisterTextureHostWrapper( 48 const wr::ExternalImageId& aExternalImageId); 49 50 RefPtr<TextureHost> GetTextureHostWrapper( 51 const wr::ExternalImageId& aExternalImageId); 52 53 // Holds TextureUpdate thread. 54 // Use the SharedThreadPool to create an nsIThreadPool with a maximum of one 55 // thread, which is equivalent to an nsIThread for our purposes. 56 const nsCOMPtr<nsIThreadPool> mThread; 57 58 protected: 59 ~TextureWrapperD3D11Allocator(); 60 61 void ClearAllTextures(const MutexAutoLock& aProofOfLock); 62 63 // Holds TextureHostWrapperD3D11s that are in use. TextureHostWrapperD3D11 is 64 // wrapped by WebRenderTextureHost. Accessed only from compositor thread 65 std::unordered_map<uint64_t, RefPtr<TextureHost>> mTextureHostWrappers; 66 67 // Accessed only from TextureUpdate thread 68 RefPtr<ID3D11Texture2D> mStagingTexture; 69 70 Mutex mMutex; 71 72 // Protected by mMutex 73 74 RefPtr<ID3D11Device> mDevice; 75 gfx::IntSize mSize; 76 std::deque<RefPtr<ID3D11Texture2D>> mRecycledTextures; 77 }; 78 79 /** 80 * A TextureHost that exposes DXGITextureHostD3D11 as wrapping YUV 81 * BufferTextureHost. The DXGITextureHostD3D11 holds GpuProcessTextureId of 82 * ID3D11Texture2D. The ID3D11Texture2D is allocated and data updated in 83 * TextureUpdate thread. 84 */ 85 class TextureHostWrapperD3D11 : public TextureHost { 86 public: 87 static RefPtr<TextureHost> CreateFromBufferTexture( 88 const RefPtr<TextureWrapperD3D11Allocator>& aAllocator, 89 TextureHost* aTextureHost); 90 91 void DeallocateDeviceData() override {} 92 93 gfx::SurfaceFormat GetFormat() const override; 94 95 already_AddRefed<gfx::DataSourceSurface> GetAsSurface( 96 gfx::DataSourceSurface* aSurface) override { 97 return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING) 98 } 99 100 gfx::ColorRange GetColorRange() const override; 101 102 gfx::IntSize GetSize() const override; 103 104 bool IsValid() override; 105 106 #ifdef MOZ_LAYERS_HAVE_LOG 107 const char* Name() override { return "TextureHostWrapperD3D11"; } 108 #endif 109 110 void CreateRenderTexture( 111 const wr::ExternalImageId& aExternalImageId) override; 112 113 void MaybeDestroyRenderTexture() override; 114 115 uint32_t NumSubTextures() override; 116 117 void PushResourceUpdates(wr::TransactionBuilder& aResources, 118 ResourceUpdateOp aOp, 119 const Range<wr::ImageKey>& aImageKeys, 120 const wr::ExternalImageId& aExtID) override; 121 122 void PushDisplayItems(wr::DisplayListBuilder& aBuilder, 123 const wr::LayoutRect& aBounds, 124 const wr::LayoutRect& aClip, wr::ImageRendering aFilter, 125 const Range<wr::ImageKey>& aImageKeys, 126 PushDisplayItemFlagSet aFlags) override; 127 128 bool SupportsExternalCompositing(WebRenderBackend aBackend) override; 129 130 void UnbindTextureSource() override; 131 132 void NotifyNotUsed() override; 133 134 BufferTextureHost* AsBufferTextureHost() override; 135 136 bool IsWrappingSurfaceTextureHost() override; 137 138 TextureHostType GetTextureHostType() override; 139 140 bool NeedsDeferredDeletion() const override; 141 142 TextureHostWrapperD3D11* AsTextureHostWrapperD3D11() override { return this; } 143 144 DXGITextureHostD3D11* AsDXGITextureHostD3D11() override { 145 return mTextureHostD3D11; 146 } 147 148 void PostTask(); 149 150 bool UpdateTextureData(); 151 152 protected: 153 TextureHostWrapperD3D11( 154 TextureFlags aFlags, 155 const RefPtr<TextureWrapperD3D11Allocator>& aAllocator, 156 const GpuProcessTextureId aTextureId, 157 DXGITextureHostD3D11* aTextureHostD3D11, TextureHost* aWrappedTextureHost, 158 const wr::ExternalImageId aWrappedExternalImageId); 159 160 virtual ~TextureHostWrapperD3D11(); 161 162 TextureHost* EnsureWrappedTextureHost(); 163 164 const RefPtr<TextureWrapperD3D11Allocator> mAllocator; 165 const GpuProcessTextureId mTextureId; 166 // DXGITextureHostD3D11 that replaces the wrapping TextureHost. 167 const RefPtr<DXGITextureHostD3D11> mTextureHostD3D11; 168 // WebRenderTextureHost that wraps BufferTextureHost 169 // BufferTextureHost might be further wrapped by GPUVideoTextureHost. 170 CompositableTextureHostRef mWrappedTextureHost; 171 const wr::ExternalImageId mWrappedExternalImageId; 172 }; 173 174 } // namespace layers 175 } // namespace mozilla 176 177 #endif // MOZILLA_GFX_TextureHostWrapperD3D11_H