CompositorBridgeChild.h (8876B)
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_CompositorBridgeChild_h 8 #define mozilla_layers_CompositorBridgeChild_h 9 10 #include "base/basictypes.h" // for DISALLOW_EVIL_CONSTRUCTORS 11 #include "mozilla/Monitor.h" 12 #include "mozilla/ipc/ProtocolUtils.h" 13 #include "mozilla/layers/PCompositorBridgeChild.h" 14 #include "mozilla/layers/TextureForwarder.h" // for TextureForwarder 15 #include "mozilla/webrender/WebRenderTypes.h" 16 #include "nsClassHashtable.h" // for nsClassHashtable 17 #include "nsCOMPtr.h" // for nsCOMPtr 18 #include "nsHashKeys.h" // for nsUint64HashKey 19 #include "nsISupportsImpl.h" // for NS_INLINE_DECL_REFCOUNTING 20 #include "nsIWeakReferenceUtils.h" 21 22 #include <unordered_map> 23 24 namespace mozilla { 25 26 namespace dom { 27 class BrowserChild; 28 } // namespace dom 29 30 namespace widget { 31 class CompositorWidget; 32 } // namespace widget 33 34 namespace layers { 35 36 using mozilla::dom::BrowserChild; 37 38 class IAPZCTreeManager; 39 class APZCTreeManagerChild; 40 class CanvasChild; 41 class CompositorBridgeParent; 42 class CompositorManagerChild; 43 class CompositorOptions; 44 class WebRenderLayerManager; 45 class TextureClient; 46 struct FrameMetrics; 47 struct FwdTransactionCounter; 48 49 class CompositorBridgeChild final : public PCompositorBridgeChild, 50 public TextureForwarder { 51 typedef nsTArray<AsyncParentMessageData> AsyncParentMessageArray; 52 53 friend class PCompositorBridgeChild; 54 55 public: 56 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorBridgeChild, override); 57 58 explicit CompositorBridgeChild(CompositorManagerChild* aManager); 59 60 /** 61 * Initialize the singleton compositor bridge for a content process. 62 */ 63 void InitForContent(uint32_t aNamespace); 64 65 void InitForWidget(uint64_t aProcessToken, 66 WebRenderLayerManager* aLayerManager, uint32_t aNamespace); 67 68 void Destroy(); 69 70 static CompositorBridgeChild* Get(); 71 72 // Returns whether the compositor is in the GPU process (false if in the UI 73 // process). This may only be called on the main thread. 74 static bool CompositorIsInGPUProcess(); 75 76 mozilla::ipc::IPCResult RecvDidComposite( 77 const LayersId& aId, const nsTArray<TransactionId>& aTransactionIds, 78 const TimeStamp& aCompositeStart, const TimeStamp& aCompositeEnd); 79 80 mozilla::ipc::IPCResult RecvNotifyFrameStats( 81 nsTArray<FrameStats>&& aFrameStats); 82 83 mozilla::ipc::IPCResult RecvNotifyJankedAnimations( 84 const LayersId& aLayersId, nsTArray<uint64_t>&& aJankedAnimations); 85 86 PTextureChild* AllocPTextureChild( 87 const SurfaceDescriptor& aSharedData, ReadLockDescriptor& aReadLock, 88 const LayersBackend& aLayersBackend, const TextureFlags& aFlags, 89 const LayersId& aId, const uint64_t& aSerial, 90 const wr::MaybeExternalImageId& aExternalImageId); 91 92 bool DeallocPTextureChild(PTextureChild* actor); 93 94 mozilla::ipc::IPCResult RecvParentAsyncMessages( 95 nsTArray<AsyncParentMessageData>&& aMessages); 96 PTextureChild* CreateTexture( 97 const SurfaceDescriptor& aSharedData, ReadLockDescriptor&& aReadLock, 98 LayersBackend aLayersBackend, TextureFlags aFlags, 99 const dom::ContentParentId& aContentId, uint64_t aSerial, 100 wr::MaybeExternalImageId& aExternalImageId) override; 101 102 already_AddRefed<CanvasChild> GetCanvasChild() final; 103 104 void EndCanvasTransaction(); 105 106 /** 107 * Release resources until they are next required. 108 */ 109 void ClearCachedResources(); 110 111 // Beware that these methods don't override their super-class equivalent 112 // (which are not virtual), they just overload them. All of these Send* 113 // methods just add a sanity check (that it is not too late send a message) 114 // and forward the call to the super-class's equivalent method. This means 115 // that it is correct to call directly the super-class methods, but you won't 116 // get the extra safety provided here. 117 bool SendWillClose(); 118 bool SendPause(); 119 bool SendResume(); 120 bool SendResumeAsync(); 121 bool SendAdoptChild(const LayersId& id); 122 bool SendFlushRendering(const wr::RenderReasons& aReasons); 123 bool SendFlushRenderingAsync(const wr::RenderReasons& aReasons); 124 125 /** 126 * This can be used, sparingly, to force all flush rendering to be 127 * synchronous. This should only be done temporarily, as we want almost 128 * all flushes to be async. It is intended to be used for animations 129 * that rely on repeated small changes to scene rebuilds. These look 130 * better with a consistent frame rate and sync flushes will help 131 * generate a stable frame rate. 132 */ 133 void SetForceSyncFlushRendering(bool aForceSyncFlushRendering); 134 135 bool SendStartFrameTimeRecording(const int32_t& bufferSize, 136 uint32_t* startIndex); 137 bool SendStopFrameTimeRecording(const uint32_t& startIndex, 138 nsTArray<float>* intervals); 139 bool IsSameProcess() const override; 140 141 bool IPCOpen() const override { return mCanSend; } 142 143 bool IsPaused() const { return mPaused; } 144 145 static void ShutDown(); 146 147 FwdTransactionCounter& GetFwdTransactionCounter(); 148 149 /** 150 * Hold TextureClient ref until end of usage on host side if 151 * TextureFlags::RECYCLE is set. Host side's usage is checked via 152 * CompositableRef. 153 */ 154 void HoldUntilCompositableRefReleasedIfNecessary(TextureClient* aClient); 155 156 /** 157 * Notify id of Texture When host side end its use. Transaction id is used to 158 * make sure if there is no newer usage. 159 */ 160 void NotifyNotUsed(uint64_t aTextureId, uint64_t aFwdTransactionId); 161 162 void CancelWaitForNotifyNotUsed(uint64_t aTextureId) override; 163 164 FixedSizeSmallShmemSectionAllocator* GetTileLockAllocator() override; 165 166 nsISerialEventTarget* GetThread() const override { return mThread; } 167 168 base::ProcessId GetParentPid() const override { return OtherPid(); } 169 170 bool AllocUnsafeShmem(size_t aSize, mozilla::ipc::Shmem* aShmem) override; 171 bool AllocShmem(size_t aSize, mozilla::ipc::Shmem* aShmem) override; 172 bool DeallocShmem(mozilla::ipc::Shmem& aShmem) override; 173 174 PAPZCTreeManagerChild* AllocPAPZCTreeManagerChild(const LayersId& aLayersId); 175 bool DeallocPAPZCTreeManagerChild(PAPZCTreeManagerChild* aActor); 176 177 PAPZChild* AllocPAPZChild(const LayersId& aLayersId); 178 bool DeallocPAPZChild(PAPZChild* aActor); 179 180 PWebRenderBridgeChild* AllocPWebRenderBridgeChild( 181 const wr::PipelineId& aPipelineId, const LayoutDeviceIntSize&, 182 const WindowKind&); 183 bool DeallocPWebRenderBridgeChild(PWebRenderBridgeChild* aActor); 184 185 wr::MaybeExternalImageId GetNextExternalImageId() override; 186 187 wr::PipelineId GetNextPipelineId(); 188 189 private: 190 // Private destructor, to discourage deletion outside of Release(): 191 virtual ~CompositorBridgeChild(); 192 193 // Must only be called from the paint thread. If the main thread is delaying 194 // IPC messages, this forwards all such delayed IPC messages to the I/O thread 195 // and resumes IPC. 196 void ResumeIPCAfterAsyncPaint(); 197 198 void PrepareFinalDestroy(); 199 void AfterDestroy(); 200 201 void ActorDestroy(ActorDestroyReason aWhy) override; 202 203 mozilla::ipc::IPCResult RecvObserveLayersUpdate(const LayersId& aLayersId, 204 const bool& aActive); 205 206 mozilla::ipc::IPCResult RecvCompositorOptionsChanged( 207 const LayersId& aLayersId, const CompositorOptions& aNewOptions); 208 209 uint64_t GetNextResourceId(); 210 211 RefPtr<CompositorManagerChild> mCompositorManager; 212 213 RefPtr<WebRenderLayerManager> mLayerManager; 214 215 uint32_t mIdNamespace; 216 uint32_t mResourceId; 217 218 // When not multi-process, hold a reference to the CompositorBridgeParent to 219 // keep it alive. This reference should be null in multi-process. 220 RefPtr<CompositorBridgeParent> mCompositorBridgeParent; 221 222 DISALLOW_EVIL_CONSTRUCTORS(CompositorBridgeChild); 223 224 // True until the beginning of the two-step shutdown sequence of this actor. 225 bool mCanSend; 226 227 // False until the actor is destroyed. 228 bool mActorDestroyed; 229 230 bool mPaused; 231 232 bool mForceSyncFlushRendering; 233 234 /** 235 * Hold TextureClients refs until end of their usages on host side. 236 * It defer calling of TextureClient recycle callback. 237 */ 238 std::unordered_map<uint64_t, RefPtr<TextureClient>> 239 mTexturesWaitingNotifyNotUsed; 240 241 nsCOMPtr<nsISerialEventTarget> mThread; 242 243 uint64_t mProcessToken; 244 245 FixedSizeSmallShmemSectionAllocator* mSectionAllocator; 246 247 // TextureClients that must be kept alive during async painting. This 248 // is only accessed on the main thread. 249 nsTArray<RefPtr<TextureClient>> mTextureClientsForAsyncPaint; 250 251 RefPtr<CanvasChild> mCanvasChild; 252 }; 253 254 } // namespace layers 255 } // namespace mozilla 256 257 #endif // mozilla_layers_CompositorBrigedChild_h