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 GFX_WEBRENDERLAYERMANAGER_H
8 #define GFX_WEBRENDERLAYERMANAGER_H
9 
10 #include <cstddef>                    // for size_t
11 #include <cstdint>                    // for uint32_t, int32_t, INT32_MAX
12 #include <string>                     // for string
13 #include "Units.h"                    // for LayoutDeviceIntSize
14 #include "mozilla/AlreadyAddRefed.h"  // for already_AddRefed
15 #include "mozilla/Assertions.h"  // for AssertionConditionType, MOZ_ASSERT, MOZ_ASSERT_HELPER2
16 #include "mozilla/Attributes.h"               // for MOZ_NON_OWNING_REF
17 #include "mozilla/RefPtr.h"                   // for RefPtr
18 #include "mozilla/StaticPrefs_apz.h"          // for apz_test_logging_enabled
19 #include "mozilla/TimeStamp.h"                // for TimeStamp
20 #include "mozilla/gfx/Point.h"                // for IntSize
21 #include "mozilla/gfx/Types.h"                // for SurfaceFormat
22 #include "mozilla/layers/APZTestData.h"       // for APZTestData
23 #include "mozilla/layers/CompositorTypes.h"   // for TextureFactoryIdentifier
24 #include "mozilla/layers/DisplayItemCache.h"  // for DisplayItemCache
25 #include "mozilla/layers/FocusTarget.h"       // for FocusTarget
26 #include "mozilla/layers/LayerManager.h"  // for DidCompositeObserver (ptr only), LayerManager::END_DEFAULT, LayerManager::En...
27 #include "mozilla/layers/LayersTypes.h"  // for TransactionId, LayersBackend, CompositionPayload (ptr only), LayersBackend::...
28 #include "mozilla/layers/RenderRootStateManager.h"  // for RenderRootStateManager
29 #include "mozilla/layers/ScrollableLayerGuid.h"  // for ScrollableLayerGuid, ScrollableLayerGuid::ViewID
30 #include "mozilla/layers/WebRenderCommandBuilder.h"  // for WebRenderCommandBuilder
31 #include "mozilla/layers/WebRenderScrollData.h"      // for WebRenderScrollData
32 #include "nsHashKeys.h"                              // for nsRefPtrHashKey
33 #include "nsRegion.h"                                // for nsIntRegion
34 #include "nsStringFwd.h"                             // for nsCString, nsAString
35 #include "nsTArray.h"                                // for nsTArray
36 #include "nsTHashSet.h"
37 
38 class gfxContext;
39 class nsDisplayList;
40 class nsDisplayListBuilder;
41 class nsIWidget;
42 
43 namespace mozilla {
44 
45 struct ActiveScrolledRoot;
46 
47 namespace layers {
48 
49 class CompositorBridgeChild;
50 class KnowsCompositor;
51 class Layer;
52 class PCompositorBridgeChild;
53 class WebRenderBridgeChild;
54 class WebRenderParentCommand;
55 
56 class WebRenderLayerManager final : public LayerManager {
57   typedef nsTArray<RefPtr<Layer>> LayerRefArray;
58   typedef nsTHashSet<RefPtr<WebRenderUserData>> WebRenderUserDataRefTable;
59 
60  public:
61   explicit WebRenderLayerManager(nsIWidget* aWidget);
62   bool Initialize(PCompositorBridgeChild* aCBChild, wr::PipelineId aLayersId,
63                   TextureFactoryIdentifier* aTextureFactoryIdentifier,
64                   nsCString& aError);
65 
66   void Destroy() override;
67 
68   void DoDestroy(bool aIsSync);
69 
70  protected:
71   virtual ~WebRenderLayerManager();
72 
73  public:
74   KnowsCompositor* AsKnowsCompositor() override;
AsWebRenderLayerManager()75   WebRenderLayerManager* AsWebRenderLayerManager() override { return this; }
76   CompositorBridgeChild* GetCompositorBridgeChild() override;
77 
78   // WebRender can handle images larger than the max texture size via tiling.
GetMaxTextureSize()79   int32_t GetMaxTextureSize() const override { return INT32_MAX; }
80 
81   bool BeginTransactionWithTarget(gfxContext* aTarget,
82                                   const nsCString& aURL) override;
83   bool BeginTransaction(const nsCString& aURL) override;
84   bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT) override;
85   void EndTransactionWithoutLayer(nsDisplayList* aDisplayList,
86                                   nsDisplayListBuilder* aDisplayListBuilder,
87                                   WrFiltersHolder&& aFilters,
88                                   WebRenderBackgroundData* aBackground,
89                                   const double aGeckoDLBuildTime);
90   void EndTransaction(DrawPaintedLayerCallback aCallback, void* aCallbackData,
91                       EndTransactionFlags aFlags = END_DEFAULT) override;
92 
GetBackendType()93   LayersBackend GetBackendType() override { return LayersBackend::LAYERS_WR; }
94   void GetBackendName(nsAString& name) override;
Name()95   const char* Name() const override { return "WebRender"; }
96 
97   void SetRoot(Layer* aLayer) override;
98 
CreatePaintedLayer()99   already_AddRefed<PaintedLayer> CreatePaintedLayer() override {
100     return nullptr;
101   }
CreateContainerLayer()102   already_AddRefed<ContainerLayer> CreateContainerLayer() override {
103     return nullptr;
104   }
CreateImageLayer()105   already_AddRefed<ImageLayer> CreateImageLayer() override { return nullptr; }
CreateColorLayer()106   already_AddRefed<ColorLayer> CreateColorLayer() override { return nullptr; }
CreateCanvasLayer()107   already_AddRefed<CanvasLayer> CreateCanvasLayer() override { return nullptr; }
108 
NeedsWidgetInvalidation()109   bool NeedsWidgetInvalidation() override { return false; }
110 
111   void SetLayersObserverEpoch(LayersObserverEpoch aEpoch) override;
112 
113   void DidComposite(TransactionId aTransactionId,
114                     const mozilla::TimeStamp& aCompositeStart,
115                     const mozilla::TimeStamp& aCompositeEnd) override;
116 
117   void ClearCachedResources(Layer* aSubtree = nullptr) override;
118   void UpdateTextureFactoryIdentifier(
119       const TextureFactoryIdentifier& aNewIdentifier) override;
120   TextureFactoryIdentifier GetTextureFactoryIdentifier() override;
121 
122   void SetTransactionIdAllocator(TransactionIdAllocator* aAllocator) override;
123   TransactionId GetLastTransactionId() override;
124 
125   void AddDidCompositeObserver(DidCompositeObserver* aObserver) override;
126   void RemoveDidCompositeObserver(DidCompositeObserver* aObserver) override;
127 
128   void FlushRendering() override;
129   void WaitOnTransactionProcessed() override;
130 
131   void SendInvalidRegion(const nsIntRegion& aRegion) override;
132 
133   void ScheduleComposite() override;
134 
SetNeedsComposite(bool aNeedsComposite)135   void SetNeedsComposite(bool aNeedsComposite) override {
136     mNeedsComposite = aNeedsComposite;
137   }
NeedsComposite()138   bool NeedsComposite() const override { return mNeedsComposite; }
SetIsFirstPaint()139   void SetIsFirstPaint() override { mIsFirstPaint = true; }
GetIsFirstPaint()140   bool GetIsFirstPaint() const override { return mIsFirstPaint; }
141   void SetFocusTarget(const FocusTarget& aFocusTarget) override;
142 
143   already_AddRefed<PersistentBufferProvider> CreatePersistentBufferProvider(
144       const gfx::IntSize& aSize, gfx::SurfaceFormat aFormat) override;
145 
146   bool AsyncPanZoomEnabled() const override;
147 
148   // adds an imagekey to a list of keys that will be discarded on the next
149   // transaction or destruction
150   void DiscardImages();
151   void DiscardLocalImages();
152 
153   void ClearAsyncAnimations();
154   void WrReleasedImages(const nsTArray<wr::ExternalImageKeyPair>& aPairs);
155 
WrBridge()156   WebRenderBridgeChild* WrBridge() const { return mWrChild; }
157 
158   // See equivalent function in ClientLayerManager
LogTestDataForCurrentPaint(ScrollableLayerGuid::ViewID aScrollId,const std::string & aKey,const std::string & aValue)159   void LogTestDataForCurrentPaint(ScrollableLayerGuid::ViewID aScrollId,
160                                   const std::string& aKey,
161                                   const std::string& aValue) {
162     MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me");
163     mApzTestData.LogTestDataForPaint(mPaintSequenceNumber, aScrollId, aKey,
164                                      aValue);
165   }
LogAdditionalTestData(const std::string & aKey,const std::string & aValue)166   void LogAdditionalTestData(const std::string& aKey,
167                              const std::string& aValue) {
168     MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me");
169     mApzTestData.RecordAdditionalData(aKey, aValue);
170   }
171 
172   // See equivalent function in ClientLayerManager
GetAPZTestData()173   const APZTestData& GetAPZTestData() const { return mApzTestData; }
174 
CommandBuilder()175   WebRenderCommandBuilder& CommandBuilder() { return mWebRenderCommandBuilder; }
GetWebRenderUserDataTable()176   WebRenderUserDataRefTable* GetWebRenderUserDataTable() {
177     return mWebRenderCommandBuilder.GetWebRenderUserDataTable();
178   }
GetScrollData()179   WebRenderScrollData& GetScrollData() { return mScrollData; }
180 
181   void WrUpdated();
GetWidget()182   nsIWidget* GetWidget() { return mWidget; }
183 
184   uint32_t StartFrameTimeRecording(int32_t aBufferSize) override;
185   void StopFrameTimeRecording(uint32_t aStartIndex,
186                               nsTArray<float>& aFrameIntervals) override;
187 
GetRenderRootStateManager()188   RenderRootStateManager* GetRenderRootStateManager() { return &mStateManager; }
189 
190   virtual void PayloadPresented(const TimeStamp& aTimeStamp) override;
191 
192   void TakeCompositionPayloads(nsTArray<CompositionPayload>& aPayloads);
193 
194   void GetFrameUniformity(FrameUniformityData* aOutData) override;
195 
196  private:
197   /**
198    * Take a snapshot of the parent context, and copy
199    * it into mTarget.
200    */
201   void MakeSnapshotIfRequired(LayoutDeviceIntSize aSize);
202 
203  private:
204   nsIWidget* MOZ_NON_OWNING_REF mWidget;
205 
206   RefPtr<WebRenderBridgeChild> mWrChild;
207 
208   RefPtr<TransactionIdAllocator> mTransactionIdAllocator;
209   TransactionId mLatestTransactionId;
210 
211   nsTArray<DidCompositeObserver*> mDidCompositeObservers;
212 
213   // This holds the scroll data that we need to send to the compositor for
214   // APZ to do it's job
215   WebRenderScrollData mScrollData;
216 
217   bool mNeedsComposite;
218   bool mIsFirstPaint;
219   FocusTarget mFocusTarget;
220 
221   // When we're doing a transaction in order to draw to a non-default
222   // target, the layers transaction is only performed in order to send
223   // a PLayers:Update.  We save the original non-default target to
224   // mTarget, and then perform the transaction. After the transaction ends,
225   // we send a message to our remote side to capture the actual pixels
226   // being drawn to the default target, and then copy those pixels
227   // back to mTarget.
228   RefPtr<gfxContext> mTarget;
229 
230   // See equivalent field in ClientLayerManager
231   uint32_t mPaintSequenceNumber;
232   // See equivalent field in ClientLayerManager
233   APZTestData mApzTestData;
234 
235   TimeStamp mTransactionStart;
236   nsCString mURL;
237   WebRenderCommandBuilder mWebRenderCommandBuilder;
238 
239   size_t mLastDisplayListSize;
240   RenderRootStateManager mStateManager;
241   DisplayItemCache mDisplayItemCache;
242 };
243 
244 }  // namespace layers
245 }  // namespace mozilla
246 
247 #endif /* GFX_WEBRENDERLAYERMANAGER_H */
248