Source code

Revision control

Other Tools

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
#include "D3D11ShareHandleImage.h"
8
#include <memory>
9
#include "DXVA2Manager.h"
10
#include "WMF.h"
11
#include "d3d11.h"
12
#include "gfxImageSurface.h"
13
#include "gfxWindowsPlatform.h"
14
#include "mozilla/StaticPrefs_media.h"
15
#include "mozilla/gfx/DeviceManagerDx.h"
16
#include "mozilla/layers/CompositableClient.h"
17
#include "mozilla/layers/CompositableForwarder.h"
18
#include "mozilla/layers/TextureClient.h"
19
#include "mozilla/layers/TextureD3D11.h"
20
21
namespace mozilla {
22
namespace layers {
23
24
using namespace gfx;
25
26
D3D11ShareHandleImage::D3D11ShareHandleImage(const gfx::IntSize& aSize,
27
const gfx::IntRect& aRect,
28
gfx::YUVColorSpace aColorSpace,
29
gfx::ColorRange aColorRange)
30
: Image(nullptr, ImageFormat::D3D11_SHARE_HANDLE_TEXTURE),
31
mSize(aSize),
32
mPictureRect(aRect),
33
mYUVColorSpace(aColorSpace),
34
mColorRange(aColorRange) {}
35
36
bool D3D11ShareHandleImage::AllocateTexture(D3D11RecycleAllocator* aAllocator,
37
ID3D11Device* aDevice) {
38
if (aAllocator) {
39
mTextureClient =
40
aAllocator->CreateOrRecycleClient(mYUVColorSpace, mColorRange, mSize);
41
if (mTextureClient) {
42
D3D11TextureData* textureData = GetData();
43
MOZ_DIAGNOSTIC_ASSERT(textureData, "Wrong TextureDataType");
44
mTexture = textureData->GetD3D11Texture();
45
return true;
46
}
47
return false;
48
} else {
49
MOZ_ASSERT(aDevice);
50
CD3D11_TEXTURE2D_DESC newDesc(
51
DXGI_FORMAT_B8G8R8A8_UNORM, mSize.width, mSize.height, 1, 1,
52
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
53
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
54
55
HRESULT hr =
56
aDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(mTexture));
57
return SUCCEEDED(hr);
58
}
59
}
60
61
gfx::IntSize D3D11ShareHandleImage::GetSize() const { return mSize; }
62
63
TextureClient* D3D11ShareHandleImage::GetTextureClient(
64
KnowsCompositor* aKnowsCompositor) {
65
return mTextureClient;
66
}
67
68
already_AddRefed<gfx::SourceSurface>
69
D3D11ShareHandleImage::GetAsSourceSurface() {
70
RefPtr<ID3D11Texture2D> src = GetTexture();
71
if (!src) {
72
gfxWarning() << "Cannot readback from shared texture because no texture is "
73
"available.";
74
return nullptr;
75
}
76
77
return gfx::Factory::CreateBGRA8DataSourceSurfaceForD3D11Texture(src);
78
}
79
80
ID3D11Texture2D* D3D11ShareHandleImage::GetTexture() const { return mTexture; }
81
82
class MOZ_RAII D3D11TextureClientAllocationHelper
83
: public ITextureClientAllocationHelper {
84
public:
85
D3D11TextureClientAllocationHelper(gfx::SurfaceFormat aFormat,
86
gfx::YUVColorSpace aColorSpace,
87
gfx::ColorRange aColorRange,
88
const gfx::IntSize& aSize,
89
TextureAllocationFlags aAllocFlags,
90
ID3D11Device* aDevice,
91
TextureFlags aTextureFlags)
92
: ITextureClientAllocationHelper(aFormat, aSize, BackendSelector::Content,
93
aTextureFlags, aAllocFlags),
94
mYUVColorSpace(aColorSpace),
95
mColorRange(aColorRange),
96
mDevice(aDevice) {}
97
98
bool IsCompatible(TextureClient* aTextureClient) override {
99
D3D11TextureData* textureData =
100
aTextureClient->GetInternalData()->AsD3D11TextureData();
101
if (!textureData || aTextureClient->GetFormat() != mFormat ||
102
aTextureClient->GetSize() != mSize) {
103
return false;
104
}
105
// TODO: Should we also check for change in the allocation flags if RGBA?
106
return (aTextureClient->GetFormat() != gfx::SurfaceFormat::NV12 &&
107
aTextureClient->GetFormat() != gfx::SurfaceFormat::P010 &&
108
aTextureClient->GetFormat() != gfx::SurfaceFormat::P016) ||
109
(textureData->GetYUVColorSpace() == mYUVColorSpace &&
110
textureData->GetColorRange() == mColorRange &&
111
textureData->GetTextureAllocationFlags() == mAllocationFlags);
112
}
113
114
already_AddRefed<TextureClient> Allocate(
115
KnowsCompositor* aAllocator) override {
116
D3D11TextureData* data =
117
D3D11TextureData::Create(mSize, mFormat, mAllocationFlags, mDevice);
118
if (!data) {
119
return nullptr;
120
}
121
data->SetYUVColorSpace(mYUVColorSpace);
122
data->SetColorRange(mColorRange);
123
return MakeAndAddRef<TextureClient>(data, mTextureFlags,
124
aAllocator->GetTextureForwarder());
125
}
126
127
private:
128
const gfx::YUVColorSpace mYUVColorSpace;
129
const gfx::ColorRange mColorRange;
130
const RefPtr<ID3D11Device> mDevice;
131
};
132
133
D3D11RecycleAllocator::D3D11RecycleAllocator(
134
KnowsCompositor* aAllocator, ID3D11Device* aDevice,
135
gfx::SurfaceFormat aPreferredFormat)
136
: TextureClientRecycleAllocator(aAllocator),
137
mDevice(aDevice),
138
mCanUseNV12(StaticPrefs::media_wmf_use_nv12_format() &&
139
gfx::DeviceManagerDx::Get()->CanUseNV12()),
140
mCanUseP010(StaticPrefs::media_wmf_use_nv12_format() &&
141
gfx::DeviceManagerDx::Get()->CanUseP010()),
142
mCanUseP016(StaticPrefs::media_wmf_use_nv12_format() &&
143
gfx::DeviceManagerDx::Get()->CanUseP016()) {
144
SetPreferredSurfaceFormat(aPreferredFormat);
145
}
146
147
void D3D11RecycleAllocator::SetPreferredSurfaceFormat(
148
gfx::SurfaceFormat aPreferredFormat) {
149
if ((aPreferredFormat == gfx::SurfaceFormat::NV12 && mCanUseNV12) ||
150
(aPreferredFormat == gfx::SurfaceFormat::P010 && mCanUseP010) ||
151
(aPreferredFormat == gfx::SurfaceFormat::P016 && mCanUseP016)) {
152
mUsableSurfaceFormat = aPreferredFormat;
153
return;
154
}
155
// We can't handle the native source format, set it to BGRA which will
156
// force the caller to convert it later.
157
mUsableSurfaceFormat = gfx::SurfaceFormat::B8G8R8A8;
158
}
159
160
already_AddRefed<TextureClient> D3D11RecycleAllocator::CreateOrRecycleClient(
161
gfx::YUVColorSpace aColorSpace, gfx::ColorRange aColorRange,
162
const gfx::IntSize& aSize) {
163
// When CompositorDevice or ContentDevice is updated,
164
// we could not reuse old D3D11Textures. It could cause video flickering.
165
RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetImageDevice();
166
if (!!mImageDevice && mImageDevice != device) {
167
ShrinkToMinimumSize();
168
}
169
mImageDevice = device;
170
171
TextureAllocationFlags allocFlags = TextureAllocationFlags::ALLOC_DEFAULT;
172
if (StaticPrefs::media_wmf_use_sync_texture_AtStartup() ||
173
mDevice == DeviceManagerDx::Get()->GetCompositorDevice()) {
174
// If our device is the compositor device, we don't need any synchronization
175
// in practice.
176
allocFlags = TextureAllocationFlags::ALLOC_MANUAL_SYNCHRONIZATION;
177
}
178
179
D3D11TextureClientAllocationHelper helper(
180
mUsableSurfaceFormat, aColorSpace, aColorRange, aSize, allocFlags,
181
mDevice, layers::TextureFlags::DEFAULT);
182
183
RefPtr<TextureClient> textureClient = CreateOrRecycle(helper);
184
return textureClient.forget();
185
}
186
187
} // namespace layers
188
} // namespace mozilla