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 "D3D11YCbCrImage.h"
8
9
#include "gfx2DGlue.h"
10
#include "YCbCrUtils.h"
11
#include "mozilla/gfx/gfxVars.h"
12
#include "mozilla/gfx/DeviceManagerDx.h"
13
#include "mozilla/layers/CompositableClient.h"
14
#include "mozilla/layers/CompositableForwarder.h"
15
#include "mozilla/layers/TextureD3D11.h"
16
17
using namespace mozilla::gfx;
18
19
namespace mozilla {
20
namespace layers {
21
22
D3D11YCbCrImage::D3D11YCbCrImage()
23
: Image(NULL, ImageFormat::D3D11_YCBCR_IMAGE) {}
24
25
D3D11YCbCrImage::~D3D11YCbCrImage() {}
26
27
bool D3D11YCbCrImage::SetData(KnowsCompositor* aAllocator,
28
ImageContainer* aContainer,
29
const PlanarYCbCrData& aData) {
30
mPictureRect = IntRect(aData.mPicX, aData.mPicY, aData.mPicSize.width,
31
aData.mPicSize.height);
32
mYSize = aData.mYSize;
33
mCbCrSize = aData.mCbCrSize;
34
mColorDepth = aData.mColorDepth;
35
mColorSpace = aData.mYUVColorSpace;
36
mColorRange = aData.mColorRange;
37
38
D3D11YCbCrRecycleAllocator* allocator =
39
aContainer->GetD3D11YCbCrRecycleAllocator(aAllocator);
40
if (!allocator) {
41
return false;
42
}
43
44
RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetImageDevice();
45
if (!device) {
46
return false;
47
}
48
49
{
50
DXGIYCbCrTextureAllocationHelper helper(aData, TextureFlags::DEFAULT,
51
device);
52
mTextureClient = allocator->CreateOrRecycle(helper);
53
}
54
55
if (!mTextureClient) {
56
return false;
57
}
58
59
DXGIYCbCrTextureData* data =
60
mTextureClient->GetInternalData()->AsDXGIYCbCrTextureData();
61
62
ID3D11Texture2D* textureY = data->GetD3D11Texture(0);
63
ID3D11Texture2D* textureCb = data->GetD3D11Texture(1);
64
ID3D11Texture2D* textureCr = data->GetD3D11Texture(2);
65
66
RefPtr<ID3D10Multithread> mt;
67
HRESULT hr = device->QueryInterface((ID3D10Multithread**)getter_AddRefs(mt));
68
69
if (FAILED(hr) || !mt) {
70
gfxCriticalError() << "Multithread safety interface not supported. " << hr;
71
return false;
72
}
73
74
if (!mt->GetMultithreadProtected()) {
75
gfxCriticalError() << "Device used not marked as multithread-safe.";
76
return false;
77
}
78
79
D3D11MTAutoEnter mtAutoEnter(mt.forget());
80
81
RefPtr<ID3D11DeviceContext> ctx;
82
device->GetImmediateContext(getter_AddRefs(ctx));
83
if (!ctx) {
84
gfxCriticalError() << "Failed to get immediate context.";
85
return false;
86
}
87
88
AutoLockD3D11Texture lockY(textureY);
89
AutoLockD3D11Texture lockCb(textureCb);
90
AutoLockD3D11Texture lockCr(textureCr);
91
92
ctx->UpdateSubresource(textureY, 0, nullptr, aData.mYChannel, aData.mYStride,
93
aData.mYStride * aData.mYSize.height);
94
ctx->UpdateSubresource(textureCb, 0, nullptr, aData.mCbChannel,
95
aData.mCbCrStride,
96
aData.mCbCrStride * aData.mCbCrSize.height);
97
ctx->UpdateSubresource(textureCr, 0, nullptr, aData.mCrChannel,
98
aData.mCbCrStride,
99
aData.mCbCrStride * aData.mCbCrSize.height);
100
101
return true;
102
}
103
104
IntSize D3D11YCbCrImage::GetSize() const { return mPictureRect.Size(); }
105
106
TextureClient* D3D11YCbCrImage::GetTextureClient(
107
KnowsCompositor* aKnowsCompositor) {
108
return mTextureClient;
109
}
110
111
const DXGIYCbCrTextureData* D3D11YCbCrImage::GetData() const {
112
if (!mTextureClient) return nullptr;
113
114
return mTextureClient->GetInternalData()->AsDXGIYCbCrTextureData();
115
}
116
117
already_AddRefed<SourceSurface> D3D11YCbCrImage::GetAsSourceSurface() {
118
if (!mTextureClient) {
119
gfxWarning()
120
<< "GetAsSourceSurface() called on uninitialized D3D11YCbCrImage.";
121
return nullptr;
122
}
123
124
gfx::IntSize size(mPictureRect.Size());
125
gfx::SurfaceFormat format =
126
gfx::ImageFormatToSurfaceFormat(gfxVars::OffscreenFormat());
127
HRESULT hr;
128
129
PlanarYCbCrData data;
130
131
DXGIYCbCrTextureData* dxgiData =
132
mTextureClient->GetInternalData()->AsDXGIYCbCrTextureData();
133
134
if (!dxgiData) {
135
gfxCriticalError() << "Failed to get texture client internal data.";
136
return nullptr;
137
}
138
139
RefPtr<ID3D11Texture2D> texY = dxgiData->GetD3D11Texture(0);
140
RefPtr<ID3D11Texture2D> texCb = dxgiData->GetD3D11Texture(1);
141
RefPtr<ID3D11Texture2D> texCr = dxgiData->GetD3D11Texture(2);
142
RefPtr<ID3D11Texture2D> softTexY, softTexCb, softTexCr;
143
D3D11_TEXTURE2D_DESC desc;
144
145
RefPtr<ID3D11Device> dev;
146
texY->GetDevice(getter_AddRefs(dev));
147
148
if (!dev || dev != gfx::DeviceManagerDx::Get()->GetImageDevice()) {
149
gfxCriticalError() << "D3D11Device is obsoleted";
150
return nullptr;
151
}
152
153
RefPtr<ID3D10Multithread> mt;
154
hr = dev->QueryInterface((ID3D10Multithread**)getter_AddRefs(mt));
155
156
if (FAILED(hr) || !mt) {
157
gfxCriticalError() << "Multithread safety interface not supported.";
158
return nullptr;
159
}
160
161
if (!mt->GetMultithreadProtected()) {
162
gfxCriticalError() << "Device used not marked as multithread-safe.";
163
return nullptr;
164
}
165
166
D3D11MTAutoEnter mtAutoEnter(mt.forget());
167
168
texY->GetDesc(&desc);
169
desc.BindFlags = 0;
170
desc.MiscFlags = 0;
171
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
172
desc.Usage = D3D11_USAGE_STAGING;
173
174
dev->CreateTexture2D(&desc, nullptr, getter_AddRefs(softTexY));
175
176
texCb->GetDesc(&desc);
177
desc.BindFlags = 0;
178
desc.MiscFlags = 0;
179
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
180
desc.Usage = D3D11_USAGE_STAGING;
181
182
dev->CreateTexture2D(&desc, nullptr, getter_AddRefs(softTexCb));
183
184
texCr->GetDesc(&desc);
185
desc.BindFlags = 0;
186
desc.MiscFlags = 0;
187
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
188
desc.Usage = D3D11_USAGE_STAGING;
189
190
dev->CreateTexture2D(&desc, nullptr, getter_AddRefs(softTexCr));
191
192
RefPtr<ID3D11DeviceContext> ctx;
193
dev->GetImmediateContext(getter_AddRefs(ctx));
194
if (!ctx) {
195
gfxCriticalError() << "Failed to get immediate context.";
196
return nullptr;
197
}
198
199
{
200
AutoLockD3D11Texture lockY(texY);
201
AutoLockD3D11Texture lockCb(texCb);
202
AutoLockD3D11Texture lockCr(texCr);
203
ctx->CopyResource(softTexY, texY);
204
ctx->CopyResource(softTexCb, texCb);
205
ctx->CopyResource(softTexCr, texCr);
206
}
207
208
D3D11_MAPPED_SUBRESOURCE mapY, mapCb, mapCr;
209
RefPtr<gfx::DataSourceSurface> surface;
210
mapY.pData = mapCb.pData = mapCr.pData = nullptr;
211
212
hr = ctx->Map(softTexY, 0, D3D11_MAP_READ, 0, &mapY);
213
if (FAILED(hr)) {
214
gfxCriticalError() << "Failed to map Y plane (" << hr << ")";
215
return nullptr;
216
}
217
hr = ctx->Map(softTexCb, 0, D3D11_MAP_READ, 0, &mapCb);
218
if (FAILED(hr)) {
219
gfxCriticalError() << "Failed to map Y plane (" << hr << ")";
220
return nullptr;
221
}
222
hr = ctx->Map(softTexCr, 0, D3D11_MAP_READ, 0, &mapCr);
223
if (FAILED(hr)) {
224
gfxCriticalError() << "Failed to map Y plane (" << hr << ")";
225
return nullptr;
226
}
227
228
MOZ_ASSERT(mapCb.RowPitch == mapCr.RowPitch);
229
230
data.mPicX = mPictureRect.X();
231
data.mPicY = mPictureRect.Y();
232
data.mPicSize = mPictureRect.Size();
233
data.mStereoMode = StereoMode::MONO;
234
data.mColorDepth = mColorDepth;
235
data.mYUVColorSpace = mColorSpace;
236
data.mColorRange = mColorRange;
237
data.mYSkip = data.mCbSkip = data.mCrSkip = 0;
238
data.mYSize = mYSize;
239
data.mCbCrSize = mCbCrSize;
240
data.mYChannel = static_cast<uint8_t*>(mapY.pData);
241
data.mYStride = mapY.RowPitch;
242
data.mCbChannel = static_cast<uint8_t*>(mapCb.pData);
243
data.mCrChannel = static_cast<uint8_t*>(mapCr.pData);
244
data.mCbCrStride = mapCb.RowPitch;
245
246
gfx::GetYCbCrToRGBDestFormatAndSize(data, format, size);
247
if (size.width > PlanarYCbCrImage::MAX_DIMENSION ||
248
size.height > PlanarYCbCrImage::MAX_DIMENSION) {
249
gfxCriticalError() << "Illegal image dest width or height";
250
return nullptr;
251
}
252
253
surface = gfx::Factory::CreateDataSourceSurface(size, format);
254
if (!surface) {
255
gfxCriticalError() << "Failed to create DataSourceSurface for image: "
256
<< size << " " << format;
257
return nullptr;
258
}
259
260
DataSourceSurface::ScopedMap mapping(surface, DataSourceSurface::WRITE);
261
if (!mapping.IsMapped()) {
262
gfxCriticalError() << "Failed to map DataSourceSurface for D3D11YCbCrImage";
263
return nullptr;
264
}
265
266
gfx::ConvertYCbCrToRGB(data, format, size, mapping.GetData(),
267
mapping.GetStride());
268
269
ctx->Unmap(softTexY, 0);
270
ctx->Unmap(softTexCb, 0);
271
ctx->Unmap(softTexCr, 0);
272
273
return surface.forget();
274
}
275
276
class AutoCheckLockD3D11Texture final {
277
public:
278
explicit AutoCheckLockD3D11Texture(ID3D11Texture2D* aTexture)
279
: mIsLocked(false) {
280
aTexture->QueryInterface((IDXGIKeyedMutex**)getter_AddRefs(mMutex));
281
if (!mMutex) {
282
// If D3D11Texture does not have keyed mutex, we think that the
283
// D3D11Texture could be locked.
284
mIsLocked = true;
285
return;
286
}
287
288
// Test to see if the keyed mutex has been released
289
HRESULT hr = mMutex->AcquireSync(0, 0);
290
if (SUCCEEDED(hr)) {
291
mIsLocked = true;
292
}
293
}
294
295
~AutoCheckLockD3D11Texture() {
296
if (!mMutex) {
297
return;
298
}
299
HRESULT hr = mMutex->ReleaseSync(0);
300
if (FAILED(hr)) {
301
NS_WARNING("Failed to unlock the texture");
302
}
303
}
304
305
bool IsLocked() const { return mIsLocked; }
306
307
private:
308
bool mIsLocked;
309
RefPtr<IDXGIKeyedMutex> mMutex;
310
};
311
312
DXGIYCbCrTextureAllocationHelper::DXGIYCbCrTextureAllocationHelper(
313
const PlanarYCbCrData& aData, TextureFlags aTextureFlags,
314
ID3D11Device* aDevice)
315
: ITextureClientAllocationHelper(gfx::SurfaceFormat::YUV, aData.mYSize,
316
BackendSelector::Content, aTextureFlags,
317
ALLOC_DEFAULT),
318
mData(aData),
319
mDevice(aDevice) {}
320
321
bool DXGIYCbCrTextureAllocationHelper::IsCompatible(
322
TextureClient* aTextureClient) {
323
MOZ_ASSERT(aTextureClient->GetFormat() == gfx::SurfaceFormat::YUV);
324
325
DXGIYCbCrTextureData* dxgiData =
326
aTextureClient->GetInternalData()->AsDXGIYCbCrTextureData();
327
if (!dxgiData || aTextureClient->GetSize() != mData.mYSize ||
328
dxgiData->GetYSize() != mData.mYSize ||
329
dxgiData->GetCbCrSize() != mData.mCbCrSize ||
330
dxgiData->GetColorDepth() != mData.mColorDepth ||
331
dxgiData->GetYUVColorSpace() != mData.mYUVColorSpace) {
332
return false;
333
}
334
335
ID3D11Texture2D* textureY = dxgiData->GetD3D11Texture(0);
336
ID3D11Texture2D* textureCb = dxgiData->GetD3D11Texture(1);
337
ID3D11Texture2D* textureCr = dxgiData->GetD3D11Texture(2);
338
339
RefPtr<ID3D11Device> device;
340
textureY->GetDevice(getter_AddRefs(device));
341
if (!device || device != gfx::DeviceManagerDx::Get()->GetImageDevice()) {
342
return false;
343
}
344
345
// Test to see if the keyed mutex has been released.
346
// If D3D11Texture failed to lock, do not recycle the DXGIYCbCrTextureData.
347
348
AutoCheckLockD3D11Texture lockY(textureY);
349
AutoCheckLockD3D11Texture lockCr(textureCr);
350
AutoCheckLockD3D11Texture lockCb(textureCb);
351
352
if (!lockY.IsLocked() || !lockCr.IsLocked() || !lockCb.IsLocked()) {
353
return false;
354
}
355
356
return true;
357
}
358
359
already_AddRefed<TextureClient> DXGIYCbCrTextureAllocationHelper::Allocate(
360
KnowsCompositor* aAllocator) {
361
CD3D11_TEXTURE2D_DESC newDesc(mData.mColorDepth == gfx::ColorDepth::COLOR_8
362
? DXGI_FORMAT_R8_UNORM
363
: DXGI_FORMAT_R16_UNORM,
364
mData.mYSize.width, mData.mYSize.height, 1, 1);
365
// WebRender requests keyed mutex
366
if (mDevice == gfx::DeviceManagerDx::Get()->GetCompositorDevice() &&
367
!gfxVars::UseWebRender()) {
368
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
369
} else {
370
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
371
}
372
373
RefPtr<ID3D10Multithread> mt;
374
HRESULT hr = mDevice->QueryInterface((ID3D10Multithread**)getter_AddRefs(mt));
375
376
if (FAILED(hr) || !mt) {
377
gfxCriticalError() << "Multithread safety interface not supported. " << hr;
378
return nullptr;
379
}
380
381
if (!mt->GetMultithreadProtected()) {
382
gfxCriticalError() << "Device used not marked as multithread-safe.";
383
return nullptr;
384
}
385
386
D3D11MTAutoEnter mtAutoEnter(mt.forget());
387
388
RefPtr<ID3D11Texture2D> textureY;
389
hr = mDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureY));
390
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
391
392
newDesc.Width = mData.mCbCrSize.width;
393
newDesc.Height = mData.mCbCrSize.height;
394
395
RefPtr<ID3D11Texture2D> textureCb;
396
hr = mDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureCb));
397
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
398
399
RefPtr<ID3D11Texture2D> textureCr;
400
hr = mDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureCr));
401
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
402
403
TextureForwarder* forwarder =
404
aAllocator ? aAllocator->GetTextureForwarder() : nullptr;
405
406
return TextureClient::CreateWithData(
407
DXGIYCbCrTextureData::Create(textureY, textureCb, textureCr, mData.mYSize,
408
mData.mYSize, mData.mCbCrSize,
409
mData.mColorDepth, mData.mYUVColorSpace,
410
mData.mColorRange),
411
mTextureFlags, forwarder);
412
}
413
414
already_AddRefed<TextureClient> D3D11YCbCrRecycleAllocator::Allocate(
415
SurfaceFormat aFormat, IntSize aSize, BackendSelector aSelector,
416
TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
417
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
418
return nullptr;
419
}
420
421
} // namespace layers
422
} // namespace mozilla