Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef GFX_WEBRENDERLAYERMANAGER_H
7 : #define GFX_WEBRENDERLAYERMANAGER_H
8 :
9 : #include "gfxPrefs.h"
10 : #include "Layers.h"
11 : #include "mozilla/MozPromise.h"
12 : #include "mozilla/layers/APZTestData.h"
13 : #include "mozilla/layers/FocusTarget.h"
14 : #include "mozilla/layers/StackingContextHelper.h"
15 : #include "mozilla/layers/TransactionIdAllocator.h"
16 : #include "mozilla/layers/WebRenderUserData.h"
17 : #include "mozilla/webrender/WebRenderAPI.h"
18 : #include "mozilla/webrender/WebRenderTypes.h"
19 : #include "nsDisplayList.h"
20 :
21 : class nsIWidget;
22 :
23 : namespace mozilla {
24 : namespace layers {
25 :
26 : class CompositorBridgeChild;
27 : class KnowsCompositor;
28 : class PCompositorBridgeChild;
29 : class WebRenderBridgeChild;
30 : class WebRenderParentCommand;
31 :
32 : class WebRenderLayerManager final : public LayerManager
33 : {
34 : typedef nsTArray<RefPtr<Layer> > LayerRefArray;
35 :
36 : public:
37 : explicit WebRenderLayerManager(nsIWidget* aWidget);
38 : void Initialize(PCompositorBridgeChild* aCBChild, wr::PipelineId aLayersId, TextureFactoryIdentifier* aTextureFactoryIdentifier);
39 :
40 : virtual void Destroy() override;
41 :
42 : void DoDestroy(bool aIsSync);
43 :
44 : protected:
45 : virtual ~WebRenderLayerManager();
46 :
47 : public:
48 : virtual KnowsCompositor* AsKnowsCompositor() override;
49 0 : WebRenderLayerManager* AsWebRenderLayerManager() override { return this; }
50 : virtual CompositorBridgeChild* GetCompositorBridgeChild() override;
51 :
52 : virtual int32_t GetMaxTextureSize() const override;
53 :
54 : virtual bool BeginTransactionWithTarget(gfxContext* aTarget) override;
55 : virtual bool BeginTransaction() override;
56 : virtual bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT) override;
57 : Maybe<wr::ImageKey> CreateImageKey(nsDisplayItem* aItem,
58 : ImageContainer* aContainer,
59 : mozilla::wr::DisplayListBuilder& aBuilder,
60 : const StackingContextHelper& aSc,
61 : gfx::IntSize& aSize);
62 : bool PushImage(nsDisplayItem* aItem,
63 : ImageContainer* aContainer,
64 : mozilla::wr::DisplayListBuilder& aBuilder,
65 : const StackingContextHelper& aSc,
66 : const LayerRect& aRect);
67 : bool PushItemAsImage(nsDisplayItem* aItem,
68 : wr::DisplayListBuilder& aBuilder,
69 : const StackingContextHelper& aSc,
70 : nsDisplayListBuilder* aDisplayListBuilder);
71 : void CreateWebRenderCommandsFromDisplayList(nsDisplayList* aDisplayList,
72 : nsDisplayListBuilder* aDisplayListBuilder,
73 : StackingContextHelper& aSc,
74 : wr::DisplayListBuilder& aBuilder);
75 : void EndTransactionWithoutLayer(nsDisplayList* aDisplayList,
76 : nsDisplayListBuilder* aDisplayListBuilder);
77 0 : bool IsLayersFreeTransaction() { return mEndTransactionWithoutLayers; }
78 : virtual void EndTransaction(DrawPaintedLayerCallback aCallback,
79 : void* aCallbackData,
80 : EndTransactionFlags aFlags = END_DEFAULT) override;
81 :
82 0 : virtual LayersBackend GetBackendType() override { return LayersBackend::LAYERS_WR; }
83 0 : virtual void GetBackendName(nsAString& name) override { name.AssignLiteral("WebRender"); }
84 0 : virtual const char* Name() const override { return "WebRender"; }
85 :
86 : virtual void SetRoot(Layer* aLayer) override;
87 :
88 : virtual already_AddRefed<PaintedLayer> CreatePaintedLayer() override;
89 : virtual already_AddRefed<ContainerLayer> CreateContainerLayer() override;
90 : virtual already_AddRefed<ImageLayer> CreateImageLayer() override;
91 : virtual already_AddRefed<CanvasLayer> CreateCanvasLayer() override;
92 : virtual already_AddRefed<ReadbackLayer> CreateReadbackLayer() override;
93 : virtual already_AddRefed<ColorLayer> CreateColorLayer() override;
94 : virtual already_AddRefed<RefLayer> CreateRefLayer() override;
95 : virtual already_AddRefed<TextLayer> CreateTextLayer() override;
96 : virtual already_AddRefed<BorderLayer> CreateBorderLayer() override;
97 : virtual already_AddRefed<DisplayItemLayer> CreateDisplayItemLayer() override;
98 :
99 0 : virtual bool NeedsWidgetInvalidation() override { return false; }
100 :
101 : virtual void SetLayerObserverEpoch(uint64_t aLayerObserverEpoch) override;
102 :
103 : virtual void DidComposite(uint64_t aTransactionId,
104 : const mozilla::TimeStamp& aCompositeStart,
105 : const mozilla::TimeStamp& aCompositeEnd) override;
106 :
107 : virtual void ClearCachedResources(Layer* aSubtree = nullptr) override;
108 : virtual void UpdateTextureFactoryIdentifier(const TextureFactoryIdentifier& aNewIdentifier,
109 : uint64_t aDeviceResetSeqNo) override;
110 : virtual TextureFactoryIdentifier GetTextureFactoryIdentifier() override;
111 :
112 0 : virtual void SetTransactionIdAllocator(TransactionIdAllocator* aAllocator) override
113 0 : { mTransactionIdAllocator = aAllocator; }
114 :
115 : virtual void AddDidCompositeObserver(DidCompositeObserver* aObserver) override;
116 : virtual void RemoveDidCompositeObserver(DidCompositeObserver* aObserver) override;
117 :
118 : virtual void FlushRendering() override;
119 : virtual void WaitOnTransactionProcessed() override;
120 :
121 : virtual void SendInvalidRegion(const nsIntRegion& aRegion) override;
122 :
123 : virtual void ScheduleComposite() override;
124 :
125 0 : virtual void SetNeedsComposite(bool aNeedsComposite) override
126 : {
127 0 : mNeedsComposite = aNeedsComposite;
128 0 : }
129 0 : virtual bool NeedsComposite() const override { return mNeedsComposite; }
130 0 : virtual void SetIsFirstPaint() override { mIsFirstPaint = true; }
131 : virtual void SetFocusTarget(const FocusTarget& aFocusTarget) override;
132 :
133 : bool AsyncPanZoomEnabled() const override;
134 :
135 0 : DrawPaintedLayerCallback GetPaintedLayerCallback() const
136 0 : { return mPaintedLayerCallback; }
137 :
138 0 : void* GetPaintedLayerCallbackData() const
139 0 : { return mPaintedLayerCallbackData; }
140 :
141 : // adds an imagekey to a list of keys that will be discarded on the next
142 : // transaction or destruction
143 : void AddImageKeyForDiscard(wr::ImageKey);
144 : void DiscardImages();
145 : void DiscardLocalImages();
146 :
147 : // Before destroying a layer with animations, add its compositorAnimationsId
148 : // to a list of ids that will be discarded on the next transaction
149 : void AddCompositorAnimationsIdForDiscard(uint64_t aId);
150 : void DiscardCompositorAnimations();
151 :
152 0 : WebRenderBridgeChild* WrBridge() const { return mWrChild; }
153 :
154 : virtual void Mutated(Layer* aLayer) override;
155 : virtual void MutatedSimple(Layer* aLayer) override;
156 :
157 : void Hold(Layer* aLayer);
158 0 : void SetTransactionIncomplete() { mTransactionIncomplete = true; }
159 : bool IsMutatedLayer(Layer* aLayer);
160 :
161 : // See equivalent function in ClientLayerManager
162 0 : void LogTestDataForCurrentPaint(FrameMetrics::ViewID aScrollId,
163 : const std::string& aKey,
164 : const std::string& aValue) {
165 0 : MOZ_ASSERT(gfxPrefs::APZTestLoggingEnabled(), "don't call me");
166 0 : mApzTestData.LogTestDataForPaint(mPaintSequenceNumber, aScrollId, aKey, aValue);
167 0 : }
168 : // See equivalent function in ClientLayerManager
169 0 : const APZTestData& GetAPZTestData() const
170 0 : { return mApzTestData; }
171 :
172 : // Those are data that we kept between transactions. We used to cache some
173 : // data in the layer. But in layers free mode, we don't have layer which
174 : // means we need some other place to cached the data between transaction.
175 : // We store the data in frame's property.
176 : template<class T> already_AddRefed<T>
177 0 : CreateOrRecycleWebRenderUserData(nsDisplayItem* aItem)
178 : {
179 0 : MOZ_ASSERT(aItem);
180 0 : nsIFrame* frame = aItem->Frame();
181 :
182 0 : if (!frame->HasProperty(nsIFrame::WebRenderUserDataProperty())) {
183 0 : frame->AddProperty(nsIFrame::WebRenderUserDataProperty(),
184 0 : new nsIFrame::WebRenderUserDataTable());
185 : }
186 :
187 : nsIFrame::WebRenderUserDataTable* userDataTable =
188 0 : frame->GetProperty(nsIFrame::WebRenderUserDataProperty());
189 0 : RefPtr<WebRenderUserData>& data = userDataTable->GetOrInsert(aItem->GetPerFrameKey());
190 0 : if (!data || (data->GetType() != T::Type())) {
191 0 : data = new T(this);
192 : }
193 :
194 0 : MOZ_ASSERT(data);
195 0 : MOZ_ASSERT(data->GetType() == T::Type());
196 0 : RefPtr<T> res = static_cast<T*>(data.get());
197 0 : return res.forget();
198 : }
199 :
200 : private:
201 : /**
202 : * Take a snapshot of the parent context, and copy
203 : * it into mTarget.
204 : */
205 : void MakeSnapshotIfRequired(LayoutDeviceIntSize aSize);
206 :
207 : void ClearLayer(Layer* aLayer);
208 :
209 : bool EndTransactionInternal(DrawPaintedLayerCallback aCallback,
210 : void* aCallbackData,
211 : EndTransactionFlags aFlags,
212 : nsDisplayList* aDisplayList = nullptr,
213 : nsDisplayListBuilder* aDisplayListBuilder = nullptr);
214 :
215 : private:
216 : nsIWidget* MOZ_NON_OWNING_REF mWidget;
217 : std::vector<wr::ImageKey> mImageKeys;
218 : nsTArray<uint64_t> mDiscardedCompositorAnimationsIds;
219 :
220 : /* PaintedLayer callbacks; valid at the end of a transaciton,
221 : * while rendering */
222 : DrawPaintedLayerCallback mPaintedLayerCallback;
223 : void *mPaintedLayerCallbackData;
224 :
225 : RefPtr<WebRenderBridgeChild> mWrChild;
226 :
227 : RefPtr<TransactionIdAllocator> mTransactionIdAllocator;
228 : uint64_t mLatestTransactionId;
229 :
230 : nsTArray<DidCompositeObserver*> mDidCompositeObservers;
231 :
232 : LayerRefArray mKeepAlive;
233 :
234 : // These fields are used to save a copy of the display list for
235 : // empty transactions in layers-free mode.
236 : wr::BuiltDisplayList mBuiltDisplayList;
237 : nsTArray<WebRenderParentCommand> mParentCommands;
238 :
239 : // Layers that have been mutated. If we have an empty transaction
240 : // then a display item layer will no longer be valid
241 : // if it was a mutated layers.
242 : void AddMutatedLayer(Layer* aLayer);
243 : void ClearMutatedLayers();
244 : LayerRefArray mMutatedLayers;
245 : bool mTransactionIncomplete;
246 :
247 : bool mNeedsComposite;
248 : bool mIsFirstPaint;
249 : FocusTarget mFocusTarget;
250 : bool mEndTransactionWithoutLayers;
251 :
252 : // When we're doing a transaction in order to draw to a non-default
253 : // target, the layers transaction is only performed in order to send
254 : // a PLayers:Update. We save the original non-default target to
255 : // mTarget, and then perform the transaction. After the transaction ends,
256 : // we send a message to our remote side to capture the actual pixels
257 : // being drawn to the default target, and then copy those pixels
258 : // back to mTarget.
259 : RefPtr<gfxContext> mTarget;
260 :
261 : // See equivalent field in ClientLayerManager
262 : uint32_t mPaintSequenceNumber;
263 : // See equivalent field in ClientLayerManager
264 : APZTestData mApzTestData;
265 : };
266 :
267 : } // namespace layers
268 : } // namespace mozilla
269 :
270 : #endif /* GFX_WEBRENDERLAYERMANAGER_H */
|