CompositableForwarder.h (7172B)
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_COMPOSITABLEFORWARDER 8 #define MOZILLA_LAYERS_COMPOSITABLEFORWARDER 9 10 #include <stdint.h> // for int32_t, uint32_t, uint64_t 11 12 #include "ImageContainer.h" 13 #include "mozilla/Assertions.h" // for AssertionConditionType, MOZ_ASSERT, MOZ_ASSERT_HELPER1 14 #include "mozilla/Atomics.h" 15 #include "mozilla/RefPtr.h" // for RefPtr 16 #include "mozilla/TimeStamp.h" // for TimeStamp 17 #include "mozilla/ipc/ProtocolUtils.h" // for IToplevelProtocol, ProtocolID 18 #include "mozilla/layers/KnowsCompositor.h" // for KnowsCompositor 19 #include "nsRect.h" // for nsIntRect 20 #include "nsRegion.h" // for nsIntRegion 21 #include "nsTArray.h" // for nsTArray 22 23 namespace mozilla { 24 namespace layers { 25 class CompositableClient; 26 class CompositableHandle; 27 class PTextureChild; 28 class SurfaceDescriptorTiles; 29 class TextureClient; 30 31 /** 32 * FwdTransactionCounter issues forwarder transaction numbers that represent a 33 * sequential stream of transactions to be transported over IPDL. Since every 34 * top-level protocol represents its own independently-sequenced IPDL queue, 35 * transaction numbers most naturally align with top-level protocols, rather 36 * than have different sub-protocols with their own independent transaction 37 * numbers that can't be usefully sequenced. FwdTransactionCounter expects 38 * users of it to provide themselves as proof they are a top-level protocol 39 * to avoid issues. 40 */ 41 struct FwdTransactionCounter { 42 explicit FwdTransactionCounter(mozilla::ipc::IToplevelProtocol* aToplevel) 43 : mFwdTransactionType(aToplevel->GetProtocolId()) {} 44 45 /** 46 * The ID of the top-level protocol. This is useful to tag the source of 47 * the transaction numbers in case different sources must be disambiguated. 48 */ 49 mozilla::ipc::ProtocolId mFwdTransactionType; 50 51 /** 52 * Transaction id of ShadowLayerForwarder. 53 * It is incremented by UpdateFwdTransactionId() in each BeginTransaction() 54 * call. 55 */ 56 uint64_t mFwdTransactionId = 0; 57 }; 58 59 /** 60 * FwdTransactionTracker is to be used by CompositableForwarder consumers that 61 * must remember the last transaction in which they were used. 62 */ 63 class FwdTransactionTracker { 64 public: 65 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FwdTransactionTracker) 66 67 static already_AddRefed<FwdTransactionTracker> GetOrCreate( 68 RefPtr<FwdTransactionTracker>& aTracker) { 69 if (!aTracker) { 70 aTracker = new FwdTransactionTracker; 71 } 72 return do_AddRef(aTracker); 73 } 74 75 bool IsUsed() const { return mFwdTransactionType && mFwdTransactionId; } 76 77 void Use(const FwdTransactionCounter& aCounter) { 78 mFwdTransactionType = aCounter.mFwdTransactionType; 79 mFwdTransactionId = aCounter.mFwdTransactionId; 80 } 81 82 Atomic<mozilla::ipc::ProtocolId> mFwdTransactionType{ 83 (mozilla::ipc::ProtocolId)0}; 84 Atomic<uint64_t> mFwdTransactionId{0}; 85 86 private: 87 FwdTransactionTracker() = default; 88 ~FwdTransactionTracker() = default; 89 }; 90 91 inline RemoteTextureTxnType ToRemoteTextureTxnType( 92 const RefPtr<FwdTransactionTracker>& aTracker) { 93 return aTracker ? (RemoteTextureTxnType)aTracker->mFwdTransactionType : 0; 94 } 95 96 inline RemoteTextureTxnId ToRemoteTextureTxnId( 97 const RefPtr<FwdTransactionTracker>& aTracker) { 98 return aTracker ? (RemoteTextureTxnId)aTracker->mFwdTransactionId : 0; 99 } 100 101 /** 102 * A transaction is a set of changes that happenned on the content side, that 103 * should be sent to the compositor side. 104 * CompositableForwarder is an interface to manage a transaction of 105 * compositable objetcs. 106 * 107 * ShadowLayerForwarder is an example of a CompositableForwarder (that can 108 * additionally forward modifications of the Layer tree). 109 * ImageBridgeChild is another CompositableForwarder. 110 * 111 * CompositableForwarder implements KnowsCompositor for simplicity as all 112 * implementations of CompositableForwarder currently also implement 113 * KnowsCompositor. This dependency could be split if we add new use cases. 114 */ 115 class CompositableForwarder : public KnowsCompositor { 116 public: 117 CompositableForwarder(); 118 ~CompositableForwarder(); 119 120 /** 121 * Setup the IPDL actor for aCompositable to be part of layers 122 * transactions. 123 */ 124 virtual void Connect(CompositableClient* aCompositable, 125 ImageContainer* aImageContainer = nullptr) = 0; 126 127 virtual void ReleaseCompositable(const CompositableHandle& aHandle) = 0; 128 virtual bool DestroyInTransaction(PTextureChild* aTexture) = 0; 129 130 /** 131 * Tell the CompositableHost on the compositor side to remove the texture 132 * from the CompositableHost. 133 * This function does not delete the TextureHost corresponding to the 134 * TextureClient passed in parameter. 135 * When the TextureClient has TEXTURE_DEALLOCATE_CLIENT flag, 136 * the transaction becomes synchronous. 137 */ 138 virtual void RemoveTextureFromCompositable(CompositableClient* aCompositable, 139 TextureClient* aTexture) = 0; 140 141 /** 142 * Tell the CompositableHost on the compositor side to clear Images 143 * from the CompositableHost. 144 */ 145 virtual void ClearImagesFromCompositable(CompositableClient* aCompositable, 146 ClearImagesType aType) {} 147 148 struct TimedTextureClient { 149 TimedTextureClient() 150 : mTextureClient(nullptr), mFrameID(0), mProducerID(0) {} 151 152 TextureClient* mTextureClient; 153 TimeStamp mTimeStamp; 154 nsIntRect mPictureRect; 155 int32_t mFrameID; 156 int32_t mProducerID; 157 }; 158 /** 159 * Tell the CompositableHost on the compositor side what textures to use for 160 * the next composition. 161 */ 162 virtual void UseTextures(CompositableClient* aCompositable, 163 const nsTArray<TimedTextureClient>& aTextures) = 0; 164 165 virtual void UseRemoteTexture( 166 CompositableClient* aCompositable, const RemoteTextureId aTextureId, 167 const RemoteTextureOwnerId aOwnerId, const gfx::IntSize aSize, 168 const TextureFlags aFlags, 169 const RefPtr<layers::FwdTransactionTracker>& aTracker) = 0; 170 171 void UpdateFwdTransactionId() { 172 ++GetFwdTransactionCounter().mFwdTransactionId; 173 } 174 uint64_t GetFwdTransactionId() { 175 return GetFwdTransactionCounter().mFwdTransactionId; 176 } 177 mozilla::ipc::ProtocolId GetFwdTransactionType() { 178 return GetFwdTransactionCounter().mFwdTransactionType; 179 } 180 181 virtual bool InForwarderThread() = 0; 182 183 void AssertInForwarderThread() { MOZ_ASSERT(InForwarderThread()); } 184 185 void TrackFwdTransaction(const RefPtr<FwdTransactionTracker>& aTracker) { 186 if (aTracker) { 187 aTracker->Use(GetFwdTransactionCounter()); 188 } 189 } 190 191 protected: 192 virtual FwdTransactionCounter& GetFwdTransactionCounter() = 0; 193 194 nsTArray<RefPtr<TextureClient>> mTexturesToRemove; 195 nsTArray<RefPtr<CompositableClient>> mCompositableClientsToRemove; 196 }; 197 198 } // namespace layers 199 } // namespace mozilla 200 201 #endif