summaryrefslogtreecommitdiffstats
path: root/gfx/layers/d3d11/TextureHostWrapperD3D11.cpp
blob: 380307fcea8f41e846404f1499fed1d11261eaed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "TextureHostWrapperD3D11.h"

#include <d3d11.h>

#include "mozilla/gfx/DeviceManagerDx.h"
#include "mozilla/layers/AsyncImagePipelineManager.h"
#include "mozilla/layers/CompositorThread.h"
#include "mozilla/layers/GpuProcessD3D11TextureMap.h"
#include "mozilla/layers/TextureD3D11.h"
#include "mozilla/layers/WebRenderTextureHost.h"
#include "mozilla/SharedThreadPool.h"

namespace mozilla {
namespace layers {

TextureWrapperD3D11Allocator::TextureWrapperD3D11Allocator()
    : mThread(SharedThreadPool::Get("TextureUpdate"_ns, 1)),
      mMutex("TextureWrapperD3D11Allocator::mMutex") {}
TextureWrapperD3D11Allocator::~TextureWrapperD3D11Allocator() = default;

RefPtr<ID3D11Texture2D> TextureWrapperD3D11Allocator::CreateOrRecycle(
    gfx::SurfaceFormat aSurfaceFormat, gfx::IntSize aSize) {
  MOZ_ASSERT(mThread->IsOnCurrentThread());

  RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetImageDevice();
  {
    MutexAutoLock lock(mMutex);
    if (!!mDevice && mDevice != device) {
      // Device reset might happen
      ClearAllTextures(lock);
      mDevice = nullptr;
    }

    if (!mDevice) {
      mDevice = device;
      MOZ_ASSERT(mDevice == gfx::DeviceManagerDx::Get()->GetCompositorDevice());
    }

    if (!mDevice) {
      MOZ_ASSERT_UNREACHABLE("unexpected to be called");
      return nullptr;
    }

    if (aSurfaceFormat != gfx::SurfaceFormat::NV12) {
      MOZ_ASSERT_UNREACHABLE("unexpected to be called");
      return nullptr;
    }

    if (mSize != aSize) {
      ClearAllTextures(lock);
      mSize = aSize;
    }

    if (!mRecycledTextures.empty()) {
      RefPtr<ID3D11Texture2D> texture2D = mRecycledTextures.front();
      mRecycledTextures.pop_front();
      return texture2D;
    }
  }

  CD3D11_TEXTURE2D_DESC desc(
      DXGI_FORMAT_NV12, mSize.width, mSize.height, 1, 1,
      D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);

  RefPtr<ID3D11Texture2D> texture2D;
  HRESULT hr =
      device->CreateTexture2D(&desc, nullptr, getter_AddRefs(texture2D));
  if (FAILED(hr) || !texture2D) {
    return nullptr;
  }

  EnsureStagingTextureNV12(device);
  if (!mStagingTexture) {
    return nullptr;
  }

  return texture2D;
}

void TextureWrapperD3D11Allocator::EnsureStagingTextureNV12(
    RefPtr<ID3D11Device> aDevice) {
  MOZ_ASSERT(mThread->IsOnCurrentThread());
  MOZ_ASSERT(aDevice);

  if (mStagingTexture) {
    return;
  }

  D3D11_TEXTURE2D_DESC desc = {};
  desc.Width = mSize.width;
  desc.Height = mSize.height;
  desc.Format = DXGI_FORMAT_NV12;
  desc.MipLevels = 1;
  desc.ArraySize = 1;
  desc.Usage = D3D11_USAGE_STAGING;
  desc.BindFlags = 0;
  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  desc.MiscFlags = 0;
  desc.SampleDesc.Count = 1;

  RefPtr<ID3D11Texture2D> stagingTexture;
  HRESULT hr =
      aDevice->CreateTexture2D(&desc, nullptr, getter_AddRefs(stagingTexture));
  if (FAILED(hr) || !stagingTexture) {
    return;
  }
  mStagingTexture = stagingTexture;
}

RefPtr<ID3D11Texture2D> TextureWrapperD3D11Allocator::GetStagingTextureNV12() {
  MOZ_ASSERT(mThread->IsOnCurrentThread());

  return mStagingTexture;
}

RefPtr<ID3D11Device> TextureWrapperD3D11Allocator::GetDevice() {
  MOZ_ASSERT(mThread->IsOnCurrentThread());

  MutexAutoLock lock(mMutex);
  return mDevice;
};

void TextureWrapperD3D11Allocator::ClearAllTextures(
    const MutexAutoLock& aProofOfLock) {
  MOZ_ASSERT(mThread->IsOnCurrentThread());

  mStagingTexture = nullptr;
  mRecycledTextures.clear();
}

void TextureWrapperD3D11Allocator::RecycleTexture(
    RefPtr<ID3D11Texture2D>& aTexture) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  MOZ_ASSERT(aTexture);

  RefPtr<ID3D11Device> device;
  aTexture->GetDevice(getter_AddRefs(device));

  D3D11_TEXTURE2D_DESC desc;
  aTexture->GetDesc(&desc);

  {
    MutexAutoLock lock(mMutex);
    if (device != mDevice || desc.Format != DXGI_FORMAT_NV12 ||
        desc.Width != static_cast<UINT>(mSize.width) ||
        desc.Height != static_cast<UINT>(mSize.height)) {
      return;
    }

    const auto kMaxPooledSized = 5;
    if (mRecycledTextures.size() > kMaxPooledSized) {
      return;
    }
    mRecycledTextures.emplace_back(aTexture);
  }
}

void TextureWrapperD3D11Allocator::RegisterTextureHostWrapper(
    const wr::ExternalImageId& aExternalImageId,
    RefPtr<TextureHost> aTextureHost) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());

  auto it = mTextureHostWrappers.find(wr::AsUint64(aExternalImageId));
  if (it != mTextureHostWrappers.end()) {
    MOZ_ASSERT_UNREACHABLE("unexpected to be called");
    return;
  }
  mTextureHostWrappers.emplace(wr::AsUint64(aExternalImageId), aTextureHost);
}

void TextureWrapperD3D11Allocator::UnregisterTextureHostWrapper(
    const wr::ExternalImageId& aExternalImageId) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());

  auto it = mTextureHostWrappers.find(wr::AsUint64(aExternalImageId));
  if (it == mTextureHostWrappers.end()) {
    MOZ_ASSERT_UNREACHABLE("unexpected to be called");
    return;
  }
  mTextureHostWrappers.erase(it);
}

RefPtr<TextureHost> TextureWrapperD3D11Allocator::GetTextureHostWrapper(
    const wr::ExternalImageId& aExternalImageId) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());

  auto it = mTextureHostWrappers.find(wr::AsUint64(aExternalImageId));
  if (it == mTextureHostWrappers.end()) {
    return nullptr;
  }
  return it->second;
}

// static
RefPtr<TextureHost> TextureHostWrapperD3D11::CreateFromBufferTexture(
    const RefPtr<TextureWrapperD3D11Allocator>& aAllocator,
    TextureHost* aTextureHost) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  MOZ_ASSERT(aAllocator);
  MOZ_ASSERT(aTextureHost);

  if (!XRE_IsGPUProcess()) {
    return nullptr;
  }

  auto* bufferTexture = aTextureHost->AsBufferTextureHost();
  if (!bufferTexture || bufferTexture->GetFormat() != gfx::SurfaceFormat::YUV) {
    MOZ_ASSERT_UNREACHABLE("unexpected to be called");
    return nullptr;
  }

  auto extId = aTextureHost->GetMaybeExternalImageId();
  MOZ_RELEASE_ASSERT(extId.isSome());

  // Reuse TextureHostWrapperD3D11 if it is still valid.
  RefPtr<TextureHost> textureHost =
      aAllocator->GetTextureHostWrapper(extId.ref());
  if (textureHost) {
    return textureHost;
  }

  auto size = bufferTexture->GetSize();
  auto colorDepth = bufferTexture->GetColorDepth();
  auto colorRange = bufferTexture->GetColorRange();
  auto chromaSubsampling = bufferTexture->GetChromaSubsampling();

  // Check if data could be used with NV12
  // XXX support gfx::ColorRange::FULL
  if (size.width % 2 != 0 || size.height % 2 != 0 ||
      colorDepth != gfx::ColorDepth::COLOR_8 ||
      colorRange != gfx::ColorRange::LIMITED ||
      chromaSubsampling != gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT) {
    return nullptr;
  }

  auto id = GpuProcessD3D11TextureMap::GetNextTextureId();
  auto flags = aTextureHost->GetFlags() | TextureFlags::SOFTWARE_DECODED_VIDEO;

  auto colorSpace = ToColorSpace2(bufferTexture->GetYUVColorSpace());

  auto descD3D10 = SurfaceDescriptorD3D10(
      nullptr, Some(id),
      /* arrayIndex */ 0, gfx::SurfaceFormat::NV12, size, colorSpace,
      colorRange, /* hasKeyedMutex */ false, /* fenceInfo */ Nothing(),
      /* gpuProcessQueryId */ Nothing());

  RefPtr<DXGITextureHostD3D11> textureHostD3D11 =
      new DXGITextureHostD3D11(flags, descD3D10);

  RefPtr<TextureHostWrapperD3D11> textureHostWrapper =
      new TextureHostWrapperD3D11(flags, aAllocator, id, textureHostD3D11,
                                  aTextureHost, extId.ref());
  textureHostWrapper->PostTask();

  auto externalImageId = AsyncImagePipelineManager::GetNextExternalImageId();

  textureHost =
      new WebRenderTextureHost(flags, textureHostWrapper, externalImageId);
  aAllocator->RegisterTextureHostWrapper(extId.ref(), textureHost);

  return textureHost;
}

TextureHostWrapperD3D11::TextureHostWrapperD3D11(
    TextureFlags aFlags, const RefPtr<TextureWrapperD3D11Allocator>& aAllocator,
    const GpuProcessTextureId aTextureId,
    DXGITextureHostD3D11* aTextureHostD3D11, TextureHost* aWrappedTextureHost,
    const wr::ExternalImageId aWrappedExternalImageId)
    : TextureHost(TextureHostType::DXGI, aFlags),
      mAllocator(aAllocator),
      mTextureId(aTextureId),
      mTextureHostD3D11(aTextureHostD3D11),
      mWrappedTextureHost(aWrappedTextureHost),
      mWrappedExternalImageId(aWrappedExternalImageId) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  MOZ_ASSERT(mAllocator);
  MOZ_ASSERT(mTextureHostD3D11);
  MOZ_ASSERT(mWrappedTextureHost);

  MOZ_COUNT_CTOR(TextureHostWrapperD3D11);
}

TextureHostWrapperD3D11::~TextureHostWrapperD3D11() {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
  MOZ_COUNT_DTOR(TextureHostWrapperD3D11);

  auto* textureMap = GpuProcessD3D11TextureMap::Get();
  if (textureMap) {
    RefPtr<ID3D11Texture2D> texture = textureMap->GetTexture(mTextureId);
    if (texture) {
      mAllocator->RecycleTexture(texture);
      textureMap->Unregister(mTextureId);
    }
  } else {
    gfxCriticalNoteOnce << "GpuProcessD3D11TextureMap does not exist";
  }
}

void TextureHostWrapperD3D11::PostTask() {
  GpuProcessD3D11TextureMap::Get()->PostUpdateTextureDataTask(
      mTextureId, this, mWrappedTextureHost, mAllocator);
}

bool TextureHostWrapperD3D11::IsValid() { return true; }

gfx::ColorRange TextureHostWrapperD3D11::GetColorRange() const {
  return mTextureHostD3D11->GetColorRange();
}

gfx::IntSize TextureHostWrapperD3D11::GetSize() const {
  return mTextureHostD3D11->GetSize();
}

gfx::SurfaceFormat TextureHostWrapperD3D11::GetFormat() const {
  return mTextureHostD3D11->GetFormat();
}

void TextureHostWrapperD3D11::CreateRenderTexture(
    const wr::ExternalImageId& aExternalImageId) {
  MOZ_ASSERT(mExternalImageId.isSome());

  mTextureHostD3D11->EnsureRenderTexture(mExternalImageId);
}

void TextureHostWrapperD3D11::MaybeDestroyRenderTexture() {
  // TextureHostWrapperD3D11 does not create RenderTexture, then
  // TextureHostWrapperD3D11 does not need to destroy RenderTexture.
  mExternalImageId = Nothing();
}

uint32_t TextureHostWrapperD3D11::NumSubTextures() {
  return mTextureHostD3D11->NumSubTextures();
}

void TextureHostWrapperD3D11::PushResourceUpdates(
    wr::TransactionBuilder& aResources, ResourceUpdateOp aOp,
    const Range<wr::ImageKey>& aImageKeys, const wr::ExternalImageId& aExtID) {
  mTextureHostD3D11->PushResourceUpdates(aResources, aOp, aImageKeys, aExtID);
}

void TextureHostWrapperD3D11::PushDisplayItems(
    wr::DisplayListBuilder& aBuilder, const wr::LayoutRect& aBounds,
    const wr::LayoutRect& aClip, wr::ImageRendering aFilter,
    const Range<wr::ImageKey>& aImageKeys, PushDisplayItemFlagSet aFlags) {
  MOZ_ASSERT(aImageKeys.length() > 0);

  mTextureHostD3D11->PushDisplayItems(aBuilder, aBounds, aClip, aFilter,
                                      aImageKeys, aFlags);
}

bool TextureHostWrapperD3D11::SupportsExternalCompositing(
    WebRenderBackend aBackend) {
  return mTextureHostD3D11->SupportsExternalCompositing(aBackend);
}

void TextureHostWrapperD3D11::UnbindTextureSource() {
  mTextureHostD3D11->UnbindTextureSource();
  // Handle read unlock
  TextureHost::UnbindTextureSource();
}

void TextureHostWrapperD3D11::NotifyNotUsed() {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());

  mAllocator->UnregisterTextureHostWrapper(mWrappedExternalImageId);

  MOZ_ASSERT(mWrappedTextureHost);
  if (mWrappedTextureHost) {
    mWrappedTextureHost = nullptr;
  }
  mTextureHostD3D11->NotifyNotUsed();
  TextureHost::NotifyNotUsed();
}

BufferTextureHost* TextureHostWrapperD3D11::AsBufferTextureHost() {
  return nullptr;
}

bool TextureHostWrapperD3D11::IsWrappingSurfaceTextureHost() { return false; }

TextureHostType TextureHostWrapperD3D11::GetTextureHostType() {
  return mTextureHostD3D11->GetTextureHostType();
}

bool TextureHostWrapperD3D11::NeedsDeferredDeletion() const {
  return mTextureHostD3D11->NeedsDeferredDeletion();
}

}  // namespace layers
}  // namespace mozilla