FenceD3D11.h (3161B)
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_FenceD3D11_H 8 #define MOZILLA_GFX_FenceD3D11_H 9 10 #include <unordered_map> 11 12 #include "mozilla/gfx/FileHandleWrapper.h" 13 #include "mozilla/layers/Fence.h" 14 15 struct ID3D11Device; 16 struct ID3D11Fence; 17 18 namespace mozilla { 19 namespace layers { 20 21 // 22 // A class for wrapping ID3D11Fence. 23 // 24 // The class can be used for singaling fence and waiting fence. When the class 25 // is created by Create(), it can be used for singaling fence and waiting 26 // fence. When the class is created by CreateFromHandle() it can be used only 27 // for waiting fence. 28 // 29 // There is a case that ID3D12Fence is used for fence signaling. In this case, 30 // the class can be used for waitng fence by using CreateFromHandle(). 31 // 32 // IncrementAndSignal() is used for signaling fence. The fence will be updated 33 // after all previous work has completed. 34 // 35 // For waiting fence, Update() is used to update the target value of the 36 // waiting. Wait() is then used to wait for the fence. 37 // 38 class FenceD3D11 final : public Fence { 39 public: 40 static RefPtr<FenceD3D11> Create(ID3D11Device* aDevice); 41 static RefPtr<FenceD3D11> CreateFromHandle( 42 RefPtr<gfx::FileHandleWrapper> aHandle, 43 const RefPtr<ID3D11Device> aDevice); 44 45 FenceD3D11* AsFenceD3D11() override { return this; } 46 47 // Check if ID3D11Device suppors ID3D11Fence creation. 48 static bool IsSupported(ID3D11Device* aDevice); 49 50 RefPtr<FenceD3D11> CloneFromHandle(); 51 52 // Updates mSignalFence to incremented value after all previous work has 53 // completed. Used only when FenceD3D11 is created by FenceD3D11::Create(). 54 bool IncrementAndSignal(); 55 56 // Update FenceValue to the specified value. 57 // Used only when FenceD3D11 is created by FenceD3D11::CreateFromHandle(). 58 void Update(uint64_t aFenceValue); 59 60 // Wait for fence until it reaches or exceeds mFenceValue. 61 bool Wait(ID3D11Device* aDevice); 62 63 uint64_t GetFenceValue() const { return mFenceValue; } 64 65 enum class OwnsFence : bool { No, Yes }; 66 67 const OwnsFence mOwnsFence; 68 // Device that is used for creating mSignalFence. 69 const RefPtr<ID3D11Device> mDevice; 70 // Fence that is used for updating fence value. 71 // Valid only when created with FenceD3D11::Create(). 72 const RefPtr<ID3D11Fence> mSignalFence; 73 const RefPtr<gfx::FileHandleWrapper> mHandle; 74 75 protected: 76 FenceD3D11(const OwnsFence aOwnsFence, const RefPtr<ID3D11Device> aDevice, 77 const RefPtr<ID3D11Fence> aSignalFence, 78 const RefPtr<gfx::FileHandleWrapper>& aHandle); 79 virtual ~FenceD3D11(); 80 81 uint64_t mFenceValue = 0; 82 // Fences that are used for waiting. 83 // They are opened for each D3D11 device that the fence is waited on. 84 // XXX change to LRU cache 85 std::unordered_map<const ID3D11Device*, RefPtr<ID3D11Fence>> mWaitFenceMap; 86 }; 87 88 } // namespace layers 89 } // namespace mozilla 90 91 #endif // MOZILLA_GFX_FenceD3D11_H