WebRenderBridgeChild.h (10053B)
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_layers_WebRenderBridgeChild_h 8 #define mozilla_layers_WebRenderBridgeChild_h 9 10 #include "mozilla/layers/CompositableForwarder.h" 11 #include "mozilla/layers/PWebRenderBridgeChild.h" 12 13 namespace mozilla { 14 15 namespace widget { 16 class CompositorWidget; 17 } 18 19 namespace wr { 20 class DisplayListBuilder; 21 class ResourceUpdateQueue; 22 class IpcResourceUpdateQueue; 23 } // namespace wr 24 25 namespace layers { 26 27 class CompositableClient; 28 class CompositorBridgeChild; 29 class StackingContextHelper; 30 class TextureForwarder; 31 class WebRenderLayerManager; 32 33 template <class T> 34 class ThreadSafeWeakPtrHashKey : public PLDHashEntryHdr { 35 public: 36 typedef RefPtr<T> KeyType; 37 typedef const T* KeyTypePointer; 38 39 explicit ThreadSafeWeakPtrHashKey(KeyTypePointer aKey) 40 : mKey(do_AddRef(const_cast<T*>(aKey))) {} 41 42 KeyType GetKey() const { return do_AddRef(mKey); } 43 bool KeyEquals(KeyTypePointer aKey) const { return mKey == aKey; } 44 45 static KeyTypePointer KeyToPointer(const KeyType& aKey) { return aKey.get(); } 46 static PLDHashNumber HashKey(KeyTypePointer aKey) { 47 return NS_PTR_TO_UINT32(aKey) >> 2; 48 } 49 enum { ALLOW_MEMMOVE = true }; 50 51 private: 52 ThreadSafeWeakPtr<T> mKey; 53 }; 54 55 typedef ThreadSafeWeakPtrHashKey<gfx::UnscaledFont> UnscaledFontHashKey; 56 typedef ThreadSafeWeakPtrHashKey<gfx::ScaledFont> ScaledFontHashKey; 57 58 class WebRenderBridgeChild final : public PWebRenderBridgeChild, 59 public CompositableForwarder { 60 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WebRenderBridgeChild, override) 61 62 friend class PWebRenderBridgeChild; 63 64 public: 65 explicit WebRenderBridgeChild(const wr::PipelineId& aPipelineId); 66 67 void AddWebRenderParentCommand(const WebRenderParentCommand& aCmd); 68 /// Similar to AddWebRenderParentCommand, with the exception that ops are not 69 /// picked up by off-screen transactions. Typically useful for operations that 70 /// remove resources and should not be applied before we have un-referenced 71 /// the resources from the main display list. 72 void AddWebRenderParentDestroyCommand(const WebRenderParentCommand& aCmd); 73 bool HasWebRenderParentCommands() { 74 return !mParentCommands.IsEmpty() || !mParentDestroyCommands.IsEmpty(); 75 } 76 77 void UpdateResources(wr::IpcResourceUpdateQueue& aResources); 78 void BeginTransaction(); 79 bool EndTransaction(DisplayListData&& aDisplayListData, 80 TransactionId aTransactionId, bool aContainsSVGroup, 81 const mozilla::VsyncId& aVsyncId, bool aRenderOffscreen, 82 const mozilla::TimeStamp& aVsyncStartTime, 83 const mozilla::TimeStamp& aRefreshStartTime, 84 const mozilla::TimeStamp& aTxnStartTime, 85 const nsCString& aTxtURL); 86 void EndEmptyTransaction(const FocusTarget& aFocusTarget, 87 Maybe<TransactionData>&& aTransactionData, 88 TransactionId aTransactionId, 89 const mozilla::VsyncId& aVsyncId, 90 const mozilla::TimeStamp& aVsyncStartTime, 91 const mozilla::TimeStamp& aRefreshStartTime, 92 const mozilla::TimeStamp& aTxnStartTime, 93 const nsCString& aTxtURL); 94 void ProcessWebRenderParentCommands(); 95 96 CompositorBridgeChild* GetCompositorBridgeChild(); 97 98 wr::PipelineId GetPipeline() { return mPipelineId; } 99 100 // KnowsCompositor 101 TextureForwarder* GetTextureForwarder() override; 102 LayersIPCActor* GetLayersIPCActor() override; 103 void SyncWithCompositor( 104 const Maybe<uint64_t>& aWindowID = Nothing()) override; 105 106 void AddPipelineIdForCompositable(const wr::PipelineId& aPipelineId, 107 const CompositableHandle& aHandle, 108 CompositableHandleOwner aOwner); 109 void RemovePipelineIdForCompositable(const wr::PipelineId& aPipelineId); 110 111 /// Release TextureClient that is bounded to ImageKey. 112 /// It is used for recycling TextureClient. 113 void ReleaseTextureOfImage(const wr::ImageKey& aKey); 114 115 /** 116 * Clean this up, finishing with SendShutDown() which will cause __delete__ 117 * to be sent from the parent side. 118 */ 119 void Destroy(bool aIsSync); 120 bool IPCOpen() const { return mIPCOpen && !mDestroyed; } 121 bool GetSentDisplayList() const { return mSentDisplayList; } 122 bool IsDestroyed() const { return mDestroyed; } 123 124 uint32_t GetNextResourceId() { return ++mResourceId; } 125 wr::IdNamespace GetNamespace() { return mIdNamespace; } 126 void SetNamespace(wr::IdNamespace aIdNamespace) { 127 mIdNamespace = aIdNamespace; 128 } 129 130 bool MatchesNamespace(const wr::ImageKey& aImageKey) const { 131 return aImageKey.mNamespace == mIdNamespace; 132 } 133 134 bool MatchesNamespace(const wr::BlobImageKey& aBlobKey) const { 135 return MatchesNamespace(aBlobKey._0); 136 } 137 138 wr::FontKey GetNextFontKey() { 139 return wr::FontKey{GetNamespace(), GetNextResourceId()}; 140 } 141 142 wr::FontInstanceKey GetNextFontInstanceKey() { 143 return wr::FontInstanceKey{GetNamespace(), GetNextResourceId()}; 144 } 145 146 wr::WrImageKey GetNextImageKey() { 147 return wr::WrImageKey{GetNamespace(), GetNextResourceId()}; 148 } 149 150 void PushGlyphs(wr::DisplayListBuilder& aBuilder, 151 wr::IpcResourceUpdateQueue& aResources, 152 Range<const wr::GlyphInstance> aGlyphs, 153 gfx::ScaledFont* aFont, const wr::ColorF& aColor, 154 const StackingContextHelper& aSc, 155 const wr::LayoutRect& aBounds, const wr::LayoutRect& aClip, 156 bool aBackfaceVisible, 157 const wr::GlyphOptions* aGlyphOptions = nullptr); 158 159 Maybe<wr::FontInstanceKey> GetFontKeyForScaledFont( 160 gfx::ScaledFont* aScaledFont, wr::IpcResourceUpdateQueue& aResources); 161 Maybe<wr::FontKey> GetFontKeyForUnscaledFont( 162 gfx::UnscaledFont* aUnscaledFont, wr::IpcResourceUpdateQueue& aResources); 163 void RemoveExpiredFontKeys(wr::IpcResourceUpdateQueue& aResources); 164 165 void BeginClearCachedResources(); 166 void EndClearCachedResources(); 167 168 void SetWebRenderLayerManager(WebRenderLayerManager* aManager); 169 170 mozilla::ipc::IShmemAllocator* GetShmemAllocator(); 171 172 bool IsThreadSafe() const override { return false; } 173 174 RefPtr<KnowsCompositor> GetForMedia() override; 175 176 /// Alloc a specific type of shmem that is intended for use in 177 /// IpcResourceUpdateQueue only, and cache at most one of them, 178 /// when called multiple times. 179 /// 180 /// Do not use this for anything else. 181 bool AllocResourceShmem(size_t aSize, RefCountedShmem& aShm); 182 /// Dealloc shared memory that was allocated with AllocResourceShmem. 183 /// 184 /// Do not use this for anything else. 185 void DeallocResourceShmem(RefCountedShmem& aShm); 186 187 void Capture(); 188 void StartCaptureSequence(const nsCString& path, uint32_t aFlags); 189 void StopCaptureSequence(); 190 191 bool SendEnsureConnected(TextureFactoryIdentifier* textureFactoryIdentifier, 192 MaybeIdNamespace* maybeIdNamespace, 193 nsCString* error); 194 195 private: 196 friend class CompositorBridgeChild; 197 198 ~WebRenderBridgeChild(); 199 200 void MergeWebRenderParentCommands(); 201 202 wr::ExternalImageId GetNextExternalImageId(); 203 204 // CompositableForwarder 205 void Connect(CompositableClient* aCompositable, 206 ImageContainer* aImageContainer = nullptr) override; 207 void ReleaseCompositable(const CompositableHandle& aHandle) override; 208 bool DestroyInTransaction(PTextureChild* aTexture) override; 209 bool DestroyInTransaction(const CompositableHandle& aHandle); 210 void RemoveTextureFromCompositable(CompositableClient* aCompositable, 211 TextureClient* aTexture) override; 212 void UseTextures(CompositableClient* aCompositable, 213 const nsTArray<TimedTextureClient>& aTextures) override; 214 void UseRemoteTexture(CompositableClient* aCompositable, 215 const RemoteTextureId aTextureId, 216 const RemoteTextureOwnerId aOwnerId, 217 const gfx::IntSize aSize, const TextureFlags aFlags, 218 const RefPtr<FwdTransactionTracker>& aTracker) override; 219 FwdTransactionCounter& GetFwdTransactionCounter() override; 220 221 bool InForwarderThread() override; 222 223 void ActorDestroy(ActorDestroyReason why) override; 224 225 void DoDestroy(); 226 227 mozilla::ipc::IPCResult RecvWrUpdated( 228 const wr::IdNamespace& aNewIdNamespace, 229 const TextureFactoryIdentifier& textureFactoryIdentifier); 230 mozilla::ipc::IPCResult RecvWrReleasedImages( 231 nsTArray<wr::ExternalImageKeyPair>&& aPairs); 232 233 void AddIPDLReference() { 234 MOZ_ASSERT(mIPCOpen == false); 235 mIPCOpen = true; 236 AddRef(); 237 } 238 void ReleaseIPDLReference() { 239 MOZ_ASSERT(mIPCOpen == true); 240 mIPCOpen = false; 241 Release(); 242 } 243 244 bool AddOpDestroy(const OpDestroy& aOp); 245 246 nsTArray<OpDestroy> mDestroyedActors; 247 nsTArray<WebRenderParentCommand> mParentCommands; 248 nsTArray<WebRenderParentCommand> mParentDestroyCommands; 249 nsTHashMap<nsUint64HashKey, CompositableClient*> mCompositables; 250 bool mIsInTransaction; 251 bool mIsInClearCachedResources; 252 wr::IdNamespace mIdNamespace; 253 uint32_t mResourceId; 254 wr::PipelineId mPipelineId; 255 WebRenderLayerManager* mManager; 256 257 bool mIPCOpen; 258 bool mDestroyed; 259 // True iff we have called SendSetDisplayList and haven't called 260 // SendClearCachedResources since that call. 261 bool mSentDisplayList; 262 263 uint32_t mFontKeysDeleted; 264 nsTHashMap<UnscaledFontHashKey, wr::FontKey> mFontKeys; 265 266 uint32_t mFontInstanceKeysDeleted; 267 nsTHashMap<ScaledFontHashKey, wr::FontInstanceKey> mFontInstanceKeys; 268 269 RefCountedShmem mResourceShm; 270 }; 271 272 } // namespace layers 273 } // namespace mozilla 274 275 #endif // mozilla_layers_WebRenderBridgeChild_h