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

gpu / ipc / common / dxgi_helpers.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "gpu/ipc/common/dxgi_helpers.h"

#include "base/check.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"

namespace {

constexpr char kStagingTextureLabel[] = "DxgiGmb_Map_StagingTexture";

Microsoft::WRL::ComPtr<ID3D11Texture2D> CreateStagingTexture(
    ID3D11Device* d3d11_device,
    D3D11_TEXTURE2D_DESC input_desc) {
  D3D11_TEXTURE2D_DESC staging_desc = {};
  staging_desc.Width = input_desc.Width;
  staging_desc.Height = input_desc.Height;
  staging_desc.Format = input_desc.Format;
  staging_desc.MipLevels = 1;
  staging_desc.ArraySize = 1;
  staging_desc.SampleDesc.Count = 1;
  staging_desc.Usage = D3D11_USAGE_STAGING;
  staging_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;

  Microsoft::WRL::ComPtr<ID3D11Texture2D> staging_texture;
  HRESULT hr =
      d3d11_device->CreateTexture2D(&staging_desc, nullptr, &staging_texture);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Failed to create staging texture. hr=" << std::hex << hr;
    return nullptr;
  }
  // Add debug label to the long lived texture.
  staging_texture->SetPrivateData(WKPDID_D3DDebugObjectName,
                                  strlen(kStagingTextureLabel),
                                  kStagingTextureLabel);

  return staging_texture;
}

}  // namespace

namespace gpu {

D3D11ScopedTextureUnmap::D3D11ScopedTextureUnmap(
    Microsoft::WRL::ComPtr<ID3D11DeviceContext> context,
    Microsoft::WRL::ComPtr<ID3D11Texture2D> texture)
    : context_(std::move(context)), texture_(std::move(texture)) {}

D3D11ScopedTextureUnmap::~D3D11ScopedTextureUnmap() {
  context_->Unmap(texture_.Get(), 0);
}

DXGIScopedReleaseKeyedMutex::DXGIScopedReleaseKeyedMutex(
    Microsoft::WRL::ComPtr<IDXGIKeyedMutex> keyed_mutex,
    UINT64 key)
    : keyed_mutex_(std::move(keyed_mutex)), key_(key) {
  DCHECK(keyed_mutex_);
}

DXGIScopedReleaseKeyedMutex::~DXGIScopedReleaseKeyedMutex() {
  HRESULT hr = keyed_mutex_->ReleaseSync(key_);
  DCHECK(SUCCEEDED(hr));
}

bool CopyDXGIBufferToShMem(
    HANDLE dxgi_handle,
    base::span<uint8_t> shared_memory,
    ID3D11Device* d3d11_device,
    Microsoft::WRL::ComPtr<ID3D11Texture2D>* staging_texture) {
  DCHECK(d3d11_device);

  uint8_t* dest_buffer = shared_memory.data();
  size_t dst_buffer_size = shared_memory.size_bytes();

  Microsoft::WRL::ComPtr<ID3D11Device1> device1;
  HRESULT hr = d3d11_device->QueryInterface(IID_PPV_ARGS(&device1));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Failed to open D3D11_1 device. hr=" << std::hex << hr;
    return false;
  }

  Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;

  // Open texture on device using shared handle
  hr = device1->OpenSharedResource1(dxgi_handle, IID_PPV_ARGS(&texture));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Failed to open shared texture. hr=" << std::hex << hr;
    return false;
  }

  return CopyD3D11TexToMem(texture.Get(), dest_buffer, dst_buffer_size,
                           d3d11_device, staging_texture);
}

bool CopyD3D11TexToMem(
    ID3D11Texture2D* src_texture,
    uint8_t* dst_buffer,
    size_t buffer_size,
    ID3D11Device* d3d11_device,
    Microsoft::WRL::ComPtr<ID3D11Texture2D>* staging_texture) {
  DCHECK(d3d11_device);
  DCHECK(staging_texture);
  DCHECK(dst_buffer);
  DCHECK(src_texture);

  D3D11_TEXTURE2D_DESC texture_desc = {};
  src_texture->GetDesc(&texture_desc);

  if (texture_desc.Format != DXGI_FORMAT_NV12) {
    DLOG(ERROR) << "Can't copy non-NV12 texture. format="
                << static_cast<int>(texture_desc.Format);
    return false;
  }
  size_t copy_size = texture_desc.Height * texture_desc.Width * 3 / 2;
  if (buffer_size < copy_size) {
    DLOG(ERROR) << "Invalid buffer size for copy.";
    return false;
  }

  // The texture isn't accessible for CPU reads, thus a staging texture is used.
  bool create_staging_texture = !*staging_texture;
  if (*staging_texture) {
    D3D11_TEXTURE2D_DESC staging_texture_desc;
    (*staging_texture)->GetDesc(&staging_texture_desc);
    create_staging_texture =
        (staging_texture_desc.Width != texture_desc.Width ||
         staging_texture_desc.Height != texture_desc.Height ||
         staging_texture_desc.Format != texture_desc.Format);
  }
  if (create_staging_texture) {
    *staging_texture = CreateStagingTexture(d3d11_device, texture_desc);
    if (!*staging_texture)
      return false;
  }

  Microsoft::WRL::ComPtr<ID3D11DeviceContext> device_context;
  d3d11_device->GetImmediateContext(&device_context);
  HRESULT hr = S_OK;

  if (texture_desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {
    Microsoft::WRL::ComPtr<IDXGIKeyedMutex> keyed_mutex;

    hr = src_texture->QueryInterface(IID_PPV_ARGS(&keyed_mutex));

    if (FAILED(hr)) {
      DLOG(ERROR) << "Failed to get keyed mutex. Error msg: "
                  << logging::SystemErrorCodeToString(hr);
      return false;
    }

    // Key equal to 0 is also used by the producer. Therefore, this keyed
    // mutex acts purely as a regular mutex.
    // 300ms is long enough to get the mutex in 99.999% of cases. Yet we
    // don't want to stall the callee indefinitely if the mutex is held by
    // e.g. GpuMain thread while it's blocked on driver waiting for shader
    // compilation.
    // It's better to drop a frame in this case.
    hr = keyed_mutex->AcquireSync(0, 300);

    // Can't check FAILED(hr), because AcquireSync may return e.g. WAIT_TIMEOUT
    // value.
    if (hr != S_OK) {
      DLOG(ERROR) << "Failed to acquire keyed mutex. Error msg: "
                  << logging::SystemErrorCodeToString(hr);
      return false;
    }
    DXGIScopedReleaseKeyedMutex release_keyed_mutex(keyed_mutex, 0);

    device_context->CopySubresourceRegion(staging_texture->Get(), 0, 0, 0, 0,
                                          src_texture, 0, nullptr);
  } else {
    device_context->CopySubresourceRegion(staging_texture->Get(), 0, 0, 0, 0,
                                          src_texture, 0, nullptr);
  }

  D3D11_MAPPED_SUBRESOURCE mapped_resource = {};
  hr = device_context->Map(staging_texture->Get(), 0, D3D11_MAP_READ, 0,
                           &mapped_resource);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Failed to map texture for read. Error msg: "
                << logging::SystemErrorCodeToString(hr);
    return false;
  }
  D3D11ScopedTextureUnmap scoped_unmap(device_context, *staging_texture);

  const uint8_t* source_buffer = static_cast<uint8_t*>(mapped_resource.pData);
  const uint32_t source_stride = mapped_resource.RowPitch;
  const uint32_t dest_stride = texture_desc.Width;

  return libyuv::NV12Copy(source_buffer, source_stride,
                          source_buffer + texture_desc.Height * source_stride,
                          source_stride, dst_buffer, dest_stride,
                          dst_buffer + texture_desc.Height * dest_stride,
                          dest_stride, texture_desc.Width,
                          texture_desc.Height) == 0;
}

GPU_EXPORT bool CopyShMemToDXGIBuffer(base::span<uint8_t> shared_memory,
                                      HANDLE dxgi_handle,
                                      ID3D11Device* d3d11_device) {
  CHECK(d3d11_device);

  uint8_t* src_buffer = shared_memory.data();
  size_t src_buffer_size = shared_memory.size_bytes();

  Microsoft::WRL::ComPtr<ID3D11Device1> device1;
  HRESULT hr = d3d11_device->QueryInterface(IID_PPV_ARGS(&device1));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Failed to open D3D11_1 device. hr=" << std::hex << hr;
    return false;
  }

  Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;

  // Open texture on device using shared handle
  hr = device1->OpenSharedResource1(dxgi_handle, IID_PPV_ARGS(&texture));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Failed to open shared texture. hr=" << std::hex << hr;
    return false;
  }

  return CopyMemToD3D11Tex(src_buffer, src_buffer_size, texture.Get(),
                           d3d11_device);
}

GPU_EXPORT bool CopyMemToD3D11Tex(uint8_t* src_buffer,
                                  size_t buffer_size,
                                  ID3D11Texture2D* output_texture,
                                  ID3D11Device* d3d11_device) {
  CHECK(d3d11_device);
  CHECK(src_buffer);
  CHECK(output_texture);

  D3D11_TEXTURE2D_DESC texture_desc = {};
  output_texture->GetDesc(&texture_desc);

  if (texture_desc.Format != DXGI_FORMAT_NV12) {
    DLOG(ERROR) << "Can't copy non-NV12 texture. format="
                << static_cast<int>(texture_desc.Format);
    return false;
  }
  size_t copy_size = texture_desc.Height * texture_desc.Width * 3 / 2;
  if (buffer_size < copy_size) {
    DLOG(ERROR) << "Invalid buffer size for copy.";
    return false;
  }

  Microsoft::WRL::ComPtr<ID3D11DeviceContext> device_context;
  d3d11_device->GetImmediateContext(&device_context);
  HRESULT hr = S_OK;

  if (texture_desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {
    Microsoft::WRL::ComPtr<IDXGIKeyedMutex> keyed_mutex;

    hr = output_texture->QueryInterface(IID_PPV_ARGS(&keyed_mutex));

    if (FAILED(hr)) {
      DLOG(ERROR) << "Failed to get keyed mutex. Error msg: "
                  << logging::SystemErrorCodeToString(hr);
      return false;
    }

    // Key equal to 0 is also used by the producer. Therefore, this keyed
    // mutex acts purely as a regular mutex.
    hr = keyed_mutex->AcquireSync(0, INFINITE);
    // Can't check FAILED(hr), because AcquireSync may return e.g. WAIT_TIMEOUT
    // value.
    if (hr != S_OK) {
      DLOG(ERROR) << "Failed to acquire keyed mutex. Error msg: "
                  << logging::SystemErrorCodeToString(hr);
      return false;
    }
    DXGIScopedReleaseKeyedMutex release_keyed_mutex(keyed_mutex, 0);

    device_context->UpdateSubresource(output_texture, 0, nullptr, src_buffer,
                                      texture_desc.Width, copy_size);
  } else {
    device_context->UpdateSubresource(output_texture, 0, nullptr, src_buffer,
                                      texture_desc.Width, copy_size);
  }

  return true;
}

}  // namespace gpu