summaryrefslogtreecommitdiffstats
path: root/gfx/layers/d3d11/GpuProcessD3D11TextureMap.cpp
blob: 36651b3279994581169c51b53294798b614509e5 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
/* -*- 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 "GpuProcessD3D11TextureMap.h"

#include "libyuv.h"
#include "mozilla/layers/CompositorThread.h"
#include "mozilla/layers/D3D11TextureIMFSampleImage.h"
#include "mozilla/layers/HelpersD3D11.h"
#include "mozilla/layers/TextureHostWrapperD3D11.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/webrender/RenderThread.h"

namespace mozilla {

namespace layers {

StaticAutoPtr<GpuProcessD3D11TextureMap> GpuProcessD3D11TextureMap::sInstance;

/* static */
void GpuProcessD3D11TextureMap::Init() {
  MOZ_ASSERT(XRE_IsGPUProcess());
  sInstance = new GpuProcessD3D11TextureMap();
}

/* static */
void GpuProcessD3D11TextureMap::Shutdown() {
  MOZ_ASSERT(XRE_IsGPUProcess());
  sInstance = nullptr;
}

/* static */
GpuProcessTextureId GpuProcessD3D11TextureMap::GetNextTextureId() {
  MOZ_ASSERT(XRE_IsGPUProcess());
  return GpuProcessTextureId::GetNext();
}

GpuProcessD3D11TextureMap::GpuProcessD3D11TextureMap()
    : mMonitor("GpuProcessD3D11TextureMap::mMonitor") {}

GpuProcessD3D11TextureMap::~GpuProcessD3D11TextureMap() {}

void GpuProcessD3D11TextureMap::Register(
    GpuProcessTextureId aTextureId, ID3D11Texture2D* aTexture,
    uint32_t aArrayIndex, const gfx::IntSize& aSize,
    RefPtr<IMFSampleUsageInfo> aUsageInfo) {
  MonitorAutoLock lock(mMonitor);
  Register(lock, aTextureId, aTexture, aArrayIndex, aSize, aUsageInfo);
}
void GpuProcessD3D11TextureMap::Register(
    const MonitorAutoLock& aProofOfLock, GpuProcessTextureId aTextureId,
    ID3D11Texture2D* aTexture, uint32_t aArrayIndex, const gfx::IntSize& aSize,
    RefPtr<IMFSampleUsageInfo> aUsageInfo) {
  MOZ_RELEASE_ASSERT(aTexture);

  auto it = mD3D11TexturesById.find(aTextureId);
  if (it != mD3D11TexturesById.end()) {
    MOZ_ASSERT_UNREACHABLE("unexpected to be called");
    return;
  }
  mD3D11TexturesById.emplace(
      aTextureId, TextureHolder(aTexture, aArrayIndex, aSize, aUsageInfo));
}

void GpuProcessD3D11TextureMap::Unregister(GpuProcessTextureId aTextureId) {
  MonitorAutoLock lock(mMonitor);

  auto it = mD3D11TexturesById.find(aTextureId);
  if (it == mD3D11TexturesById.end()) {
    return;
  }
  mD3D11TexturesById.erase(it);
}

RefPtr<ID3D11Texture2D> GpuProcessD3D11TextureMap::GetTexture(
    GpuProcessTextureId aTextureId) {
  MonitorAutoLock lock(mMonitor);

  auto it = mD3D11TexturesById.find(aTextureId);
  if (it == mD3D11TexturesById.end()) {
    return nullptr;
  }

  return it->second.mTexture;
}

Maybe<HANDLE> GpuProcessD3D11TextureMap::GetSharedHandleOfCopiedTexture(
    GpuProcessTextureId aTextureId) {
  TextureHolder holder;
  {
    MonitorAutoLock lock(mMonitor);

    auto it = mD3D11TexturesById.find(aTextureId);
    if (it == mD3D11TexturesById.end()) {
      return Nothing();
    }

    if (it->second.mCopiedTextureSharedHandle) {
      return Some(it->second.mCopiedTextureSharedHandle->GetHandle());
    }

    holder = it->second;
  }

  RefPtr<ID3D11Device> device;
  holder.mTexture->GetDevice(getter_AddRefs(device));
  if (!device) {
    return Nothing();
  }

  RefPtr<ID3D11DeviceContext> context;
  device->GetImmediateContext(getter_AddRefs(context));
  if (!context) {
    return Nothing();
  }

  CD3D11_TEXTURE2D_DESC newDesc(
      DXGI_FORMAT_NV12, holder.mSize.width, holder.mSize.height, 1, 1,
      D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
  newDesc.MiscFlags =
      D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;

  RefPtr<ID3D11Texture2D> copiedTexture;
  HRESULT hr =
      device->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(copiedTexture));
  if (FAILED(hr)) {
    return Nothing();
  }

  D3D11_TEXTURE2D_DESC inDesc;
  holder.mTexture->GetDesc(&inDesc);

  D3D11_TEXTURE2D_DESC outDesc;
  copiedTexture->GetDesc(&outDesc);

  UINT height = std::min(inDesc.Height, outDesc.Height);
  UINT width = std::min(inDesc.Width, outDesc.Width);
  D3D11_BOX srcBox = {0, 0, 0, width, height, 1};

  context->CopySubresourceRegion(copiedTexture, 0, 0, 0, 0, holder.mTexture,
                                 holder.mArrayIndex, &srcBox);

  RefPtr<IDXGIResource1> resource;
  copiedTexture->QueryInterface((IDXGIResource1**)getter_AddRefs(resource));
  if (!resource) {
    return Nothing();
  }

  HANDLE sharedHandle;
  hr = resource->CreateSharedHandle(
      nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
      &sharedHandle);
  if (FAILED(hr)) {
    return Nothing();
  }

  RefPtr<gfx::FileHandleWrapper> handle =
      new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));

  RefPtr<ID3D11Query> query;
  CD3D11_QUERY_DESC desc(D3D11_QUERY_EVENT);
  hr = device->CreateQuery(&desc, getter_AddRefs(query));
  if (FAILED(hr) || !query) {
    gfxWarning() << "Could not create D3D11_QUERY_EVENT: " << gfx::hexa(hr);
    return Nothing();
  }

  context->End(query);

  BOOL result;
  bool ret = WaitForFrameGPUQuery(device, context, query, &result);
  if (!ret) {
    gfxCriticalNoteOnce << "WaitForFrameGPUQuery() failed";
  }

  {
    MonitorAutoLock lock(mMonitor);

    auto it = mD3D11TexturesById.find(aTextureId);
    if (it == mD3D11TexturesById.end()) {
      MOZ_ASSERT_UNREACHABLE("unexpected to be called");
      return Nothing();
    }

    // Disable no video copy for future decoded video frames. Since
    // GetSharedHandleOfCopiedTexture() is slow.
    if (it->second.mIMFSampleUsageInfo) {
      it->second.mIMFSampleUsageInfo->DisableZeroCopyNV12Texture();
    }

    it->second.mCopiedTexture = copiedTexture;
    it->second.mCopiedTextureSharedHandle = handle;
  }

  return Some(handle->GetHandle());
}

size_t GpuProcessD3D11TextureMap::GetWaitingTextureCount() const {
  MonitorAutoLock lock(mMonitor);
  return mWaitingTextures.size();
}

bool GpuProcessD3D11TextureMap::WaitTextureReady(
    const GpuProcessTextureId aTextureId) {
  MOZ_ASSERT(wr::RenderThread::IsInRenderThread());

  MonitorAutoLock lock(mMonitor);
  auto it = mWaitingTextures.find(aTextureId);
  if (it == mWaitingTextures.end()) {
    return true;
  }

  auto start = TimeStamp::Now();
  const TimeDuration timeout = TimeDuration::FromMilliseconds(1000);
  while (1) {
    CVStatus status = mMonitor.Wait(timeout);
    if (status == CVStatus::Timeout) {
      MOZ_ASSERT_UNREACHABLE("unexpected to be called");
      gfxCriticalNoteOnce << "GpuProcessTextureI wait time out id:"
                          << uint64_t(aTextureId);
      return false;
    }

    auto it = mWaitingTextures.find(aTextureId);
    if (it == mWaitingTextures.end()) {
      break;
    }
  }

  auto end = TimeStamp::Now();
  const auto durationMs = static_cast<uint32_t>((end - start).ToMilliseconds());
  nsPrintfCString marker("TextureReadyWait %ums ", durationMs);
  PROFILER_MARKER_TEXT("PresentWait", GRAPHICS, {}, marker);

  return true;
}

void GpuProcessD3D11TextureMap::PostUpdateTextureDataTask(
    const GpuProcessTextureId aTextureId, TextureHost* aTextureHost,
    TextureHost* aWrappedTextureHost,
    TextureWrapperD3D11Allocator* aAllocator) {
  {
    MonitorAutoLock lock(mMonitor);

    auto it = mWaitingTextures.find(aTextureId);
    if (it != mWaitingTextures.end()) {
      MOZ_ASSERT_UNREACHABLE("unexpected to be called");
      return;
    }

    auto updatingTexture = MakeUnique<UpdatingTextureHolder>(
        aTextureId, aTextureHost, aWrappedTextureHost, aAllocator);

    mWaitingTextures.emplace(aTextureId);
    mWaitingTextureQueue.push_back(std::move(updatingTexture));
  }

  RefPtr<Runnable> runnable = NS_NewRunnableFunction(
      "GpuProcessD3D11TextureMap::PostUpdateTextureDataTaskRunnable", []() {
        GpuProcessD3D11TextureMap::Get()->HandleInTextureUpdateThread();
      });
  nsCOMPtr<nsIEventTarget> thread = aAllocator->mThread;
  thread->Dispatch(runnable.forget());
}

void GpuProcessD3D11TextureMap::HandleInTextureUpdateThread() {
  UniquePtr<UpdatingTextureHolder> textureHolder;
  {
    MonitorAutoLock lock(mMonitor);

    if (mWaitingTextureQueue.empty()) {
      return;
    }
    textureHolder = std::move(mWaitingTextureQueue.front());
    mWaitingTextureQueue.pop_front();
  }

  RefPtr<ID3D11Texture2D> texture = UpdateTextureData(textureHolder.get());

  {
    MonitorAutoLock lock(mMonitor);
    if (texture) {
      auto size = textureHolder->mWrappedTextureHost->GetSize();
      Register(lock, textureHolder->mTextureId, texture, /* aArrayIndex */ 0,
               size, /* aUsageInfo */ nullptr);
    }
    mWaitingTextures.erase(textureHolder->mTextureId);
    MOZ_ASSERT(mWaitingTextures.size() == mWaitingTextureQueue.size());
    mMonitor.Notify();
  }

  // Release UpdatingTextureHolder in CompositorThread
  RefPtr<Runnable> runnable = NS_NewRunnableFunction(
      "GpuProcessD3D11TextureMap::HandleInTextureUpdateThread::Runnable",
      [textureHolder = std::move(textureHolder)]() mutable {
        textureHolder = nullptr;
      });
  CompositorThread()->Dispatch(runnable.forget());
}

RefPtr<ID3D11Texture2D> GpuProcessD3D11TextureMap::UpdateTextureData(
    UpdatingTextureHolder* aHolder) {
  MOZ_ASSERT(aHolder);
  MOZ_ASSERT(aHolder->mAllocator->mThread->IsOnCurrentThread());

  auto* bufferTexture = aHolder->mWrappedTextureHost->AsBufferTextureHost();
  MOZ_ASSERT(bufferTexture);
  if (!bufferTexture) {
    MOZ_ASSERT_UNREACHABLE("unexpected to be called");
    return nullptr;
  }

  auto size = bufferTexture->GetSize();

  RefPtr<ID3D11Texture2D> texture2D =
      aHolder->mAllocator->CreateOrRecycle(gfx::SurfaceFormat::NV12, size);
  if (!texture2D) {
    return nullptr;
  }

  RefPtr<ID3D11Texture2D> stagingTexture =
      aHolder->mAllocator->GetStagingTextureNV12();
  if (!stagingTexture) {
    return nullptr;
  }

  RefPtr<ID3D11DeviceContext> context;
  RefPtr<ID3D11Device> device = aHolder->mAllocator->GetDevice();
  device->GetImmediateContext(getter_AddRefs(context));
  if (!context) {
    return nullptr;
  }

  RefPtr<ID3D10Multithread> mt;
  HRESULT hr = device->QueryInterface((ID3D10Multithread**)getter_AddRefs(mt));
  if (FAILED(hr) || !mt) {
    gfxCriticalError() << "Multithread safety interface not supported. " << hr;
    return nullptr;
  }

  if (!mt->GetMultithreadProtected()) {
    gfxCriticalError() << "Device used not marked as multithread-safe.";
    return nullptr;
  }

  D3D11MTAutoEnter mtAutoEnter(mt.forget());

  AutoLockD3D11Texture lockSt(stagingTexture);

  D3D11_MAP mapType = D3D11_MAP_WRITE;
  D3D11_MAPPED_SUBRESOURCE mappedResource;

  hr = context->Map(stagingTexture, 0, mapType, 0, &mappedResource);
  if (FAILED(hr)) {
    gfxCriticalNoteOnce << "Mapping D3D11 staging texture failed: "
                        << gfx::hexa(hr);
    return nullptr;
  }

  const size_t destStride = mappedResource.RowPitch;
  uint8_t* yDestPlaneStart = reinterpret_cast<uint8_t*>(mappedResource.pData);
  uint8_t* uvDestPlaneStart = reinterpret_cast<uint8_t*>(mappedResource.pData) +
                              destStride * size.height;
  // Convert I420 to NV12,
  const uint8_t* yChannel = bufferTexture->GetYChannel();
  const uint8_t* cbChannel = bufferTexture->GetCbChannel();
  const uint8_t* crChannel = bufferTexture->GetCrChannel();
  int32_t yStride = bufferTexture->GetYStride();
  int32_t cbCrStride = bufferTexture->GetCbCrStride();

  libyuv::I420ToNV12(yChannel, yStride, cbChannel, cbCrStride, crChannel,
                     cbCrStride, yDestPlaneStart, destStride, uvDestPlaneStart,
                     destStride, size.width, size.height);

  context->Unmap(stagingTexture, 0);

  context->CopyResource(texture2D, stagingTexture);

  return texture2D;
}

GpuProcessD3D11TextureMap::TextureHolder::TextureHolder(
    ID3D11Texture2D* aTexture, uint32_t aArrayIndex, const gfx::IntSize& aSize,
    RefPtr<IMFSampleUsageInfo> aUsageInfo)
    : mTexture(aTexture),
      mArrayIndex(aArrayIndex),
      mSize(aSize),
      mIMFSampleUsageInfo(aUsageInfo) {}

GpuProcessD3D11TextureMap::UpdatingTextureHolder::UpdatingTextureHolder(
    const GpuProcessTextureId aTextureId, TextureHost* aTextureHost,
    TextureHost* aWrappedTextureHost, TextureWrapperD3D11Allocator* aAllocator)
    : mTextureId(aTextureId),
      mTextureHost(aTextureHost),
      mWrappedTextureHost(aWrappedTextureHost),
      mAllocator(aAllocator) {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
}

GpuProcessD3D11TextureMap::UpdatingTextureHolder::~UpdatingTextureHolder() {
  MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread());
}

}  // namespace layers
}  // namespace mozilla