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_LAYERTRANSACTIONPARENT_H
8 #define MOZILLA_LAYERS_LAYERTRANSACTIONPARENT_H
9 
10 #include <stddef.h>  // for size_t
11 #include <stdint.h>  // for uint64_t, uint32_t
12 #include "CompositableTransactionParent.h"
13 #include "mozilla/Attributes.h"        // for override
14 #include "mozilla/ipc/SharedMemory.h"  // for SharedMemory, etc
15 #include "mozilla/layers/PLayerTransactionParent.h"
16 #include "nsRefPtrHashtable.h"
17 #include "nsTArrayForwardDeclare.h"  // for nsTArray
18 
19 namespace mozilla {
20 
21 namespace ipc {
22 class Shmem;
23 }  // namespace ipc
24 
25 namespace layers {
26 
27 class Layer;
28 class HostLayerManager;
29 class ShadowLayerParent;
30 class CompositableParent;
31 class CompositorAnimationStorage;
32 class CompositorBridgeParentBase;
33 
34 class LayerTransactionParent final : public PLayerTransactionParent,
35                                      public CompositableParentManager,
36                                      public mozilla::ipc::IShmemAllocator {
37   typedef nsTArray<Edit> EditArray;
38   typedef nsTArray<OpDestroy> OpDestroyArray;
39 
40   friend class PLayerTransactionParent;
41 
42  public:
43   LayerTransactionParent(HostLayerManager* aManager,
44                          CompositorBridgeParentBase* aBridge,
45                          CompositorAnimationStorage* aAnimStorage, LayersId aId,
46                          TimeDuration aVsyncRate);
47 
48  protected:
49   virtual ~LayerTransactionParent();
50 
51  public:
52   void Destroy();
53 
54   void SetLayerManager(HostLayerManager* aLayerManager,
55                        CompositorAnimationStorage* aAnimStorage);
56 
GetId()57   LayersId GetId() const { return mId; }
GetRoot()58   Layer* GetRoot() const { return mRoot; }
59 
GetChildEpoch()60   LayersObserverEpoch GetChildEpoch() const { return mChildEpoch; }
61   bool ShouldParentObserveEpoch();
62 
AsShmemAllocator()63   IShmemAllocator* AsShmemAllocator() override { return this; }
64 
65   bool AllocShmem(size_t aSize, ipc::SharedMemory::SharedMemoryType aType,
66                   ipc::Shmem* aShmem) override;
67 
68   bool AllocUnsafeShmem(size_t aSize, ipc::SharedMemory::SharedMemoryType aType,
69                         ipc::Shmem* aShmem) override;
70 
71   bool DeallocShmem(ipc::Shmem& aShmem) override;
72 
73   bool IsSameProcess() const override;
74 
75   void SetPendingTransactionId(TransactionId aId, const VsyncId& aVsyncId,
76                                const TimeStamp& aVsyncStartTime,
77                                const TimeStamp& aRefreshStartTime,
78                                const TimeStamp& aTxnStartTime,
79                                const TimeStamp& aTxnEndTime, bool aContainsSVG,
80                                const nsCString& aURL,
81                                const TimeStamp& aFwdTime);
82   void FlushPendingTransactions(const VsyncId& aId, TimeStamp& aCompositeEnd,
83                                 nsTArray<TransactionId>& aOutTransactions);
84 
85   // CompositableParentManager
86   void SendAsyncMessage(
87       const nsTArray<AsyncParentMessageData>& aMessage) override;
88 
89   void SendPendingAsyncMessages() override;
90 
91   void SetAboutToSendAsyncMessages() override;
92 
93   void NotifyNotUsed(PTextureParent* aTexture,
94                      uint64_t aTransactionId) override;
95 
GetChildProcessId()96   base::ProcessId GetChildProcessId() override { return OtherPid(); }
97 
98  protected:
99   mozilla::ipc::IPCResult RecvShutdown();
100   mozilla::ipc::IPCResult RecvShutdownSync();
101 
102   mozilla::ipc::IPCResult RecvPaintTime(const TransactionId& aTransactionId,
103                                         const TimeDuration& aPaintTime);
104 
105   mozilla::ipc::IPCResult RecvUpdate(const TransactionInfo& aInfo);
106 
107   mozilla::ipc::IPCResult RecvSetLayersObserverEpoch(
108       const LayersObserverEpoch& aChildEpoch);
109   mozilla::ipc::IPCResult RecvNewCompositable(const CompositableHandle& aHandle,
110                                               const TextureInfo& aInfo);
111   mozilla::ipc::IPCResult RecvReleaseLayer(const LayerHandle& aHandle);
112   mozilla::ipc::IPCResult RecvReleaseCompositable(
113       const CompositableHandle& aHandle);
114 
115   mozilla::ipc::IPCResult RecvClearCachedResources();
116   mozilla::ipc::IPCResult RecvScheduleComposite();
117   mozilla::ipc::IPCResult RecvSetTestSampleTime(const TimeStamp& aTime);
118   mozilla::ipc::IPCResult RecvLeaveTestMode();
119   mozilla::ipc::IPCResult RecvGetAnimationValue(
120       const uint64_t& aCompositorAnimationsId, OMTAValue* aValue);
121   mozilla::ipc::IPCResult RecvGetTransform(const LayerHandle& aHandle,
122                                            Maybe<Matrix4x4>* aTransform);
123   mozilla::ipc::IPCResult RecvSetAsyncScrollOffset(
124       const ScrollableLayerGuid::ViewID& aId, const float& aX, const float& aY);
125   mozilla::ipc::IPCResult RecvSetAsyncZoom(
126       const ScrollableLayerGuid::ViewID& aId, const float& aValue);
127   mozilla::ipc::IPCResult RecvFlushApzRepaints();
128   mozilla::ipc::IPCResult RecvGetAPZTestData(APZTestData* aOutData);
129   mozilla::ipc::IPCResult RecvGetFrameUniformity(FrameUniformityData* aOutData);
130   mozilla::ipc::IPCResult RecvRequestProperty(const nsString& aProperty,
131                                               float* aValue);
132   mozilla::ipc::IPCResult RecvSetConfirmedTargetAPZC(
133       const uint64_t& aBlockId, nsTArray<ScrollableLayerGuid>&& aTargets);
134   mozilla::ipc::IPCResult RecvRecordPaintTimes(const PaintTiming& aTiming);
135   mozilla::ipc::IPCResult RecvGetTextureFactoryIdentifier(
136       TextureFactoryIdentifier* aIdentifier);
137 
138   bool SetLayerAttributes(const OpSetLayerAttributes& aOp);
139 
140   void ActorDestroy(ActorDestroyReason why) override;
141 
142   template <typename T>
BindLayer(const RefPtr<Layer> & aLayer,const T & aCreateOp)143   bool BindLayer(const RefPtr<Layer>& aLayer, const T& aCreateOp) {
144     return BindLayerToHandle(aLayer, aCreateOp.layer());
145   }
146 
147   bool BindLayerToHandle(RefPtr<Layer> aLayer, const LayerHandle& aHandle);
148 
149   Layer* AsLayer(const LayerHandle& aLayer);
150 
151   bool Attach(Layer* aLayer, CompositableHost* aCompositable,
152               bool aIsAsyncVideo);
153 
AddIPDLReference()154   void AddIPDLReference() {
155     MOZ_ASSERT(mIPCOpen == false);
156     mIPCOpen = true;
157     AddRef();
158   }
ReleaseIPDLReference()159   void ReleaseIPDLReference() {
160     MOZ_ASSERT(mIPCOpen == true);
161     mIPCOpen = false;
162     Release();
163   }
164   friend class CompositorBridgeParent;
165   friend class ContentCompositorBridgeParent;
166 
167  private:
168   // This is a function so we can log or breakpoint on why hit
169   // testing tree changes are made.
UpdateHitTestingTree(Layer * aLayer,const char * aWhy)170   void UpdateHitTestingTree(Layer* aLayer, const char* aWhy) {
171     mUpdateHitTestingTree = true;
172   }
173 
174  private:
175   RefPtr<HostLayerManager> mLayerManager;
176   CompositorBridgeParentBase* mCompositorBridge;
177   RefPtr<CompositorAnimationStorage> mAnimStorage;
178 
179   // Hold the root because it might be grafted under various
180   // containers in the "real" layer tree
181   RefPtr<Layer> mRoot;
182 
183   // Mapping from LayerHandles to Layers.
184   nsRefPtrHashtable<nsUint64HashKey, Layer> mLayerMap;
185 
186   LayersId mId;
187 
188   // These fields keep track of the latest epoch values in the child and the
189   // parent. mChildEpoch is the latest epoch value received from the child.
190   // mParentEpoch is the latest epoch value that we have told BrowserParent
191   // about (via ObserveLayerUpdate).
192   LayersObserverEpoch mChildEpoch;
193   LayersObserverEpoch mParentEpoch;
194 
195   TimeDuration mVsyncRate;
196 
197   struct PendingTransaction {
198     TransactionId mId;
199     VsyncId mTxnVsyncId;
200     TimeStamp mVsyncStartTime;
201     TimeStamp mRefreshStartTime;
202     TimeStamp mTxnStartTime;
203     TimeStamp mTxnEndTime;
204     TimeStamp mFwdTime;
205     nsCString mTxnURL;
206     bool mContainsSVG;
207   };
208   AutoTArray<PendingTransaction, 2> mPendingTransactions;
209 
210   // When the widget/frame/browser stuff in this process begins its
211   // destruction process, we need to Disconnect() all the currently
212   // live shadow layers, because some of them might be orphaned from
213   // the layer tree.  This happens in Destroy() above.  After we
214   // Destroy() ourself, there's a window in which that information
215   // hasn't yet propagated back to the child side and it might still
216   // send us layer transactions.  We want to ignore those transactions
217   // because they refer to "zombie layers" on this side.  So, we track
218   // that state with |mDestroyed|.  This is similar to, but separate
219   // from, |mLayerManager->IsDestroyed()|; we might have had Destroy()
220   // called on us but the mLayerManager might not be destroyed, or
221   // vice versa.  In both cases though, we want to ignore shadow-layer
222   // transactions posted by the child.
223 
224   bool mDestroyed;
225   bool mIPCOpen;
226 
227   // This is set during RecvUpdate to track whether we'll need to update
228   // APZ's hit test regions.
229   bool mUpdateHitTestingTree;
230 };
231 
232 }  // namespace layers
233 }  // namespace mozilla
234 
235 #endif  // MOZILLA_LAYERS_LAYERTRANSACTIONPARENT_H
236