GpuProcessD3D11TextureMap.h (4303B)
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_GpuProcessD3D11TextureMap_H 8 #define MOZILLA_GFX_GpuProcessD3D11TextureMap_H 9 10 #include <d3d11.h> 11 #include <unordered_map> 12 #include <unordered_set> 13 14 #include "mozilla/gfx/2D.h" 15 #include "mozilla/layers/LayersTypes.h" 16 #include "mozilla/layers/TextureHost.h" 17 #include "mozilla/Maybe.h" 18 #include "mozilla/StaticPtr.h" 19 20 namespace mozilla { 21 namespace layers { 22 23 class ZeroCopyUsageInfo; 24 class TextureWrapperD3D11Allocator; 25 26 /** 27 * A class to manage ID3D11Texture2Ds that is shared without using shared handle 28 * in GPU process. On some GPUs, ID3D11Texture2Ds of hardware decoded video 29 * frames with zero video frame copy could not use shared handle. 30 */ 31 class GpuProcessD3D11TextureMap { 32 struct UpdatingTextureHolder; 33 34 public: 35 static void Init(); 36 static void Shutdown(); 37 static GpuProcessD3D11TextureMap* Get() { return sInstance; } 38 static GpuProcessTextureId GetNextTextureId(); 39 40 GpuProcessD3D11TextureMap(); 41 ~GpuProcessD3D11TextureMap(); 42 43 void Register(GpuProcessTextureId aTextureId, ID3D11Texture2D* aTexture, 44 uint32_t aArrayIndex, const gfx::IntSize& aSize, 45 RefPtr<ZeroCopyUsageInfo> aUsageInfo, 46 RefPtr<gfx::FileHandleWrapper> aSharedHandle = nullptr); 47 void Register(const MonitorAutoLock& aProofOfLock, 48 GpuProcessTextureId aTextureId, ID3D11Texture2D* aTexture, 49 uint32_t aArrayIndex, const gfx::IntSize& aSize, 50 RefPtr<ZeroCopyUsageInfo> aUsageInfo, 51 RefPtr<gfx::FileHandleWrapper> aSharedHandle); 52 void Unregister(GpuProcessTextureId aTextureId); 53 54 RefPtr<ID3D11Texture2D> GetTexture(GpuProcessTextureId aTextureId); 55 Maybe<HANDLE> GetSharedHandle(GpuProcessTextureId aTextureId); 56 void DisableZeroCopyNV12Texture(GpuProcessTextureId aTextureId); 57 58 size_t GetWaitingTextureCount() const; 59 60 bool WaitTextureReady(const GpuProcessTextureId aTextureId); 61 62 void PostUpdateTextureDataTask(const GpuProcessTextureId aTextureId, 63 TextureHost* aTextureHost, 64 TextureHost* aWrappedTextureHost, 65 TextureWrapperD3D11Allocator* aAllocator); 66 67 void HandleInTextureUpdateThread(); 68 69 private: 70 struct TextureHolder { 71 TextureHolder(ID3D11Texture2D* aTexture, uint32_t aArrayIndex, 72 const gfx::IntSize& aSize, 73 RefPtr<ZeroCopyUsageInfo> aUsageInfo, 74 RefPtr<gfx::FileHandleWrapper> aSharedHandle); 75 TextureHolder() = default; 76 77 RefPtr<ID3D11Texture2D> mTexture; 78 uint32_t mArrayIndex = 0; 79 gfx::IntSize mSize; 80 RefPtr<ZeroCopyUsageInfo> mZeroCopyUsageInfo; 81 RefPtr<gfx::FileHandleWrapper> mSharedHandle; 82 RefPtr<ID3D11Texture2D> mCopiedTexture; 83 RefPtr<gfx::FileHandleWrapper> mCopiedTextureSharedHandle; 84 }; 85 86 struct UpdatingTextureHolder { 87 UpdatingTextureHolder(const GpuProcessTextureId aTextureId, 88 TextureHost* aTextureHost, 89 TextureHost* aWrappedTextureHost, 90 TextureWrapperD3D11Allocator* aAllocator); 91 92 ~UpdatingTextureHolder(); 93 94 const GpuProcessTextureId mTextureId; 95 RefPtr<TextureHost> mTextureHost; 96 CompositableTextureHostRef mWrappedTextureHost; 97 RefPtr<TextureWrapperD3D11Allocator> mAllocator; 98 }; 99 100 enum class UpdatingStatus { Waiting, Updating, Error }; 101 102 RefPtr<ID3D11Texture2D> UpdateTextureData(UpdatingTextureHolder* aHolder); 103 104 mutable Monitor mMonitor MOZ_UNANNOTATED; 105 106 std::unordered_map<GpuProcessTextureId, TextureHolder, 107 GpuProcessTextureId::HashFn> 108 mD3D11TexturesById; 109 110 std::deque<UniquePtr<UpdatingTextureHolder>> mWaitingTextureQueue; 111 112 std::unordered_set<GpuProcessTextureId, GpuProcessTextureId::HashFn> 113 mWaitingTextures; 114 115 static StaticAutoPtr<GpuProcessD3D11TextureMap> sInstance; 116 }; 117 118 } // namespace layers 119 } // namespace mozilla 120 121 #endif /* MOZILLA_GFX_GpuProcessD3D11TextureMap_H */