tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HelpersD3D11.h (2268B)


      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 #ifndef mozilla_gfx_layers_d3d11_HelpersD3D11_h
      8 #define mozilla_gfx_layers_d3d11_HelpersD3D11_h
      9 
     10 #include <d3d11.h>
     11 #include <array>
     12 #include "mozilla/TimeStamp.h"
     13 
     14 namespace mozilla {
     15 namespace layers {
     16 
     17 template <typename T>
     18 static inline bool WaitForGPUQuery(ID3D11Device* aDevice,
     19                                   ID3D11DeviceContext* aContext,
     20                                   ID3D11Query* aQuery, T* aOut) {
     21  TimeStamp start = TimeStamp::Now();
     22  while (aContext->GetData(aQuery, aOut, sizeof(*aOut), 0) != S_OK) {
     23    if (aDevice->GetDeviceRemovedReason() != S_OK) {
     24      return false;
     25    }
     26    if (TimeStamp::Now() - start > TimeDuration::FromSeconds(2)) {
     27      return false;
     28    }
     29    Sleep(0);
     30  }
     31  return true;
     32 }
     33 
     34 static inline bool WaitForFrameGPUQuery(ID3D11Device* aDevice,
     35                                        ID3D11DeviceContext* aContext,
     36                                        ID3D11Query* aQuery, BOOL* aOut) {
     37  TimeStamp start = TimeStamp::Now();
     38  bool success = true;
     39  while (aContext->GetData(aQuery, aOut, sizeof(*aOut), 0) != S_OK) {
     40    HRESULT hr = aDevice->GetDeviceRemovedReason();
     41    if (hr != S_OK) {
     42      gfxCriticalNoteOnce << "WaitForFrameGPUQuery device removed: "
     43                          << gfx::hexa(hr);
     44      return false;
     45    }
     46    if (TimeStamp::Now() - start > TimeDuration::FromSeconds(2)) {
     47      success = false;
     48      break;
     49    }
     50    Sleep(0);
     51  }
     52  return success;
     53 }
     54 
     55 inline void ClearResource(ID3D11Device* const device, ID3D11Resource* const res,
     56                          const std::array<float, 4>& vals) {
     57  RefPtr<ID3D11RenderTargetView> rtv;
     58  (void)device->CreateRenderTargetView(res, nullptr, getter_AddRefs(rtv));
     59 
     60  RefPtr<ID3D11DeviceContext> context;
     61  device->GetImmediateContext(getter_AddRefs(context));
     62  context->ClearRenderTargetView(rtv, vals.data());
     63 }
     64 
     65 }  // namespace layers
     66 }  // namespace mozilla
     67 
     68 #endif  // mozilla_gfx_layers_d3d11_HelpersD3D11_h