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/. */
7 #ifndef MOZILLA_GFX_TextureHostWrapperD3D11_H
8 #define MOZILLA_GFX_TextureHostWrapperD3D11_H
11 #include <unordered_map>
13 #include "mozilla/layers/LayersTypes.h"
14 #include "mozilla/layers/TextureHost.h"
15 #include "mozilla/UniquePtr.h"
18 struct ID3D11Texture2D
;
23 class DXGITextureHostD3D11
;
26 * A Class that allocates and recycles ID3D11Texture2D in TextureUpdate thread.
27 * And manages in use TextureHostWrapperD3D11s in compositor thread.
29 class TextureWrapperD3D11Allocator
{
31 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureWrapperD3D11Allocator
)
33 TextureWrapperD3D11Allocator();
35 RefPtr
<ID3D11Texture2D
> CreateOrRecycle(gfx::SurfaceFormat aSurfaceFormat
,
38 void EnsureStagingTextureNV12(RefPtr
<ID3D11Device
> aDevice
);
40 RefPtr
<ID3D11Texture2D
> GetStagingTextureNV12();
42 RefPtr
<ID3D11Device
> GetDevice();
44 void RecycleTexture(RefPtr
<ID3D11Texture2D
>& aTexture
);
46 void RegisterTextureHostWrapper(const wr::ExternalImageId
& aExternalImageId
,
47 RefPtr
<TextureHost
> aTextureHost
);
48 void UnregisterTextureHostWrapper(
49 const wr::ExternalImageId
& aExternalImageId
);
51 RefPtr
<TextureHost
> GetTextureHostWrapper(
52 const wr::ExternalImageId
& aExternalImageId
);
54 // Holds TextureUpdate thread.
55 // Use the SharedThreadPool to create an nsIThreadPool with a maximum of one
56 // thread, which is equivalent to an nsIThread for our purposes.
57 const nsCOMPtr
<nsIThreadPool
> mThread
;
60 ~TextureWrapperD3D11Allocator();
62 void ClearAllTextures(const MutexAutoLock
& aProofOfLock
);
64 // Holds TextureHostWrapperD3D11s that are in use. TextureHostWrapperD3D11 is
65 // wrapped by WebRenderTextureHost. Accessed only from compositor thread
66 std::unordered_map
<uint64_t, RefPtr
<TextureHost
>> mTextureHostWrappers
;
68 // Accessed only from TextureUpdate thread
69 RefPtr
<ID3D11Texture2D
> mStagingTexture
;
73 // Protected by mMutex
75 RefPtr
<ID3D11Device
> mDevice
;
77 std::deque
<RefPtr
<ID3D11Texture2D
>> mRecycledTextures
;
81 * A TextureHost that exposes DXGITextureHostD3D11 as wrapping YUV
82 * BufferTextureHost. The DXGITextureHostD3D11 holds GpuProcessTextureId of
83 * ID3D11Texture2D. The ID3D11Texture2D is allocated and data updated in
84 * TextureUpdate thread.
86 class TextureHostWrapperD3D11
: public TextureHost
{
88 static RefPtr
<TextureHost
> CreateFromBufferTexture(
89 const RefPtr
<TextureWrapperD3D11Allocator
>& aAllocator
,
90 TextureHost
* aTextureHost
);
92 void DeallocateDeviceData() override
{}
94 gfx::SurfaceFormat
GetFormat() const override
;
96 already_AddRefed
<gfx::DataSourceSurface
> GetAsSurface(
97 gfx::DataSourceSurface
* aSurface
) override
{
98 return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
101 gfx::ColorRange
GetColorRange() const override
;
103 gfx::IntSize
GetSize() const override
;
105 bool IsValid() override
;
107 #ifdef MOZ_LAYERS_HAVE_LOG
108 const char* Name() override
{ return "TextureHostWrapperD3D11"; }
111 void CreateRenderTexture(
112 const wr::ExternalImageId
& aExternalImageId
) override
;
114 void MaybeDestroyRenderTexture() override
;
116 uint32_t NumSubTextures() override
;
118 void PushResourceUpdates(wr::TransactionBuilder
& aResources
,
119 ResourceUpdateOp aOp
,
120 const Range
<wr::ImageKey
>& aImageKeys
,
121 const wr::ExternalImageId
& aExtID
) override
;
123 void PushDisplayItems(wr::DisplayListBuilder
& aBuilder
,
124 const wr::LayoutRect
& aBounds
,
125 const wr::LayoutRect
& aClip
, wr::ImageRendering aFilter
,
126 const Range
<wr::ImageKey
>& aImageKeys
,
127 PushDisplayItemFlagSet aFlags
) override
;
129 bool SupportsExternalCompositing(WebRenderBackend aBackend
) override
;
131 void UnbindTextureSource() override
;
133 void NotifyNotUsed() override
;
135 BufferTextureHost
* AsBufferTextureHost() override
;
137 bool IsWrappingSurfaceTextureHost() override
;
139 TextureHostType
GetTextureHostType() override
;
141 bool NeedsDeferredDeletion() const override
;
143 TextureHostWrapperD3D11
* AsTextureHostWrapperD3D11() override
{ return this; }
145 DXGITextureHostD3D11
* AsDXGITextureHostD3D11() override
{
146 return mTextureHostD3D11
;
151 bool UpdateTextureData();
154 TextureHostWrapperD3D11(
156 const RefPtr
<TextureWrapperD3D11Allocator
>& aAllocator
,
157 const GpuProcessTextureId aTextureId
,
158 DXGITextureHostD3D11
* aTextureHostD3D11
, TextureHost
* aWrappedTextureHost
,
159 const wr::ExternalImageId aWrappedExternalImageId
);
161 virtual ~TextureHostWrapperD3D11();
163 TextureHost
* EnsureWrappedTextureHost();
165 const RefPtr
<TextureWrapperD3D11Allocator
> mAllocator
;
166 const GpuProcessTextureId mTextureId
;
167 // DXGITextureHostD3D11 that replaces the wrapping TextureHost.
168 const RefPtr
<DXGITextureHostD3D11
> mTextureHostD3D11
;
169 // WebRenderTextureHost that wraps BufferTextureHost
170 // BufferTextureHost might be further wrapped by GPUVideoTextureHost.
171 CompositableTextureHostRef mWrappedTextureHost
;
172 const wr::ExternalImageId mWrappedExternalImageId
;
175 } // namespace layers
176 } // namespace mozilla
178 #endif // MOZILLA_GFX_TextureHostWrapperD3D11_H