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/. */
7 #ifndef MOZILLA_GFX_FenceD3D11_H
8 #define MOZILLA_GFX_FenceD3D11_H
10 #include <unordered_map>
12 #include "mozilla/gfx/FileHandleWrapper.h"
13 #include "nsISupportsImpl.h"
23 // A class for wrapping ID3D11Fence.
25 // The class can be used for singaling fence and waiting fence. When the class
26 // is created by Create(), it can be used for singaling fence and waiting
27 // fence. When the class is created by CreateFromHandle() it can be used only
30 // There is a case that ID3D12Fence is used for fence signaling. In this case,
31 // the class can be used for waitng fence by using CreateFromHandle().
33 // IncrementAndSignal() is used for signaling fence. The fence will be updated
34 // after all previous work has completed.
36 // For waiting fence, Update() is used to update the target value of the
37 // waiting. Wait() is then used to wait for the fence.
39 class FenceD3D11 final
{
41 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FenceD3D11
);
43 static RefPtr
<FenceD3D11
> Create(ID3D11Device
* aDevice
);
44 static RefPtr
<FenceD3D11
> CreateFromHandle(
45 RefPtr
<gfx::FileHandleWrapper
> aHandle
);
47 // Check if ID3D11Device suppors ID3D11Fence creation.
48 static bool IsSupported(ID3D11Device
* aDevice
);
50 // Updates mSignalFence to incremented value after all previous work has
51 // completed. Used only when FenceD3D11 is created by FenceD3D11::Create().
52 bool IncrementAndSignal();
54 // Update FenceValue to the specified value.
55 // Used only when FenceD3D11 is created by FenceD3D11::CreateFromHandle().
56 void Update(uint64_t aFenceValue
);
58 // Wait for fence until it reaches or exceeds mFenceValue.
59 bool Wait(ID3D11Device
* aDevice
);
61 uint64_t GetFenceValue() const { return mFenceValue
; }
63 gfx::FenceInfo
GetFenceInfo() const;
65 const RefPtr
<gfx::FileHandleWrapper
> mHandle
;
68 explicit FenceD3D11(RefPtr
<gfx::FileHandleWrapper
>& aHandle
);
71 // Device that is used for creating mSignalFence.
72 RefPtr
<ID3D11Device
> mDevice
;
73 // Fence that is used for updating fence value.
74 // Valid only when created with FenceD3D11::Create().
75 RefPtr
<ID3D11Fence
> mSignalFence
;
76 uint64_t mFenceValue
= 0;
77 // Fences that are used for waiting.
78 // They are opened for each D3D11 device that the fence is waited on.
79 // XXX change to LRU cache
80 std::unordered_map
<const ID3D11Device
*, RefPtr
<ID3D11Fence
>> mWaitFenceMap
;
84 } // namespace mozilla
86 #endif // MOZILLA_GFX_FenceD3D11_H