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/. */
7 #ifndef GFX_WEBRENDERLAYERMANAGER_H
8 #define GFX_WEBRENDERLAYERMANAGER_H
10 #include <unordered_set>
11 #include <unordered_map>
15 #include "mozilla/Maybe.h"
16 #include "mozilla/MozPromise.h"
17 #include "mozilla/StaticPrefs_apz.h"
18 #include "mozilla/layers/APZTestData.h"
19 #include "mozilla/layers/FocusTarget.h"
20 #include "mozilla/layers/IpcResourceUpdateQueue.h"
21 #include "mozilla/layers/RenderRootStateManager.h"
22 #include "mozilla/layers/SharedSurfacesChild.h"
23 #include "mozilla/layers/StackingContextHelper.h"
24 #include "mozilla/layers/TransactionIdAllocator.h"
25 #include "mozilla/layers/WebRenderCommandBuilder.h"
26 #include "mozilla/layers/WebRenderScrollData.h"
27 #include "mozilla/layers/WebRenderUserData.h"
28 #include "mozilla/webrender/WebRenderAPI.h"
29 #include "mozilla/webrender/WebRenderTypes.h"
30 #include "nsDisplayList.h"
36 struct ActiveScrolledRoot
;
40 class CompositorBridgeChild
;
41 class KnowsCompositor
;
42 class PCompositorBridgeChild
;
43 class WebRenderBridgeChild
;
44 class WebRenderParentCommand
;
46 class WebRenderLayerManager final
: public LayerManager
{
47 typedef nsTArray
<RefPtr
<Layer
>> LayerRefArray
;
48 typedef nsTHashtable
<nsRefPtrHashKey
<WebRenderUserData
>>
49 WebRenderUserDataRefTable
;
52 explicit WebRenderLayerManager(nsIWidget
* aWidget
);
53 bool Initialize(PCompositorBridgeChild
* aCBChild
, wr::PipelineId aLayersId
,
54 TextureFactoryIdentifier
* aTextureFactoryIdentifier
);
56 void Destroy() override
;
58 void DoDestroy(bool aIsSync
);
61 virtual ~WebRenderLayerManager();
64 KnowsCompositor
* AsKnowsCompositor() override
;
65 WebRenderLayerManager
* AsWebRenderLayerManager() override
{ return this; }
66 CompositorBridgeChild
* GetCompositorBridgeChild() override
;
68 // WebRender can handle images larger than the max texture size via tiling.
69 int32_t GetMaxTextureSize() const override
{ return INT32_MAX
; }
71 bool BeginTransactionWithTarget(gfxContext
* aTarget
,
72 const nsCString
& aURL
) override
;
73 bool BeginTransaction(const nsCString
& aURL
) override
;
74 bool EndEmptyTransaction(EndTransactionFlags aFlags
= END_DEFAULT
) override
;
75 void EndTransactionWithoutLayer(
76 nsDisplayList
* aDisplayList
, nsDisplayListBuilder
* aDisplayListBuilder
,
77 WrFiltersHolder
&& aFilters
= WrFiltersHolder(),
78 WebRenderBackgroundData
* aBackground
= nullptr);
79 void EndTransaction(DrawPaintedLayerCallback aCallback
, void* aCallbackData
,
80 EndTransactionFlags aFlags
= END_DEFAULT
) override
;
82 LayersBackend
GetBackendType() override
{ return LayersBackend::LAYERS_WR
; }
83 void GetBackendName(nsAString
& name
) override
{
84 name
.AssignLiteral("WebRender");
86 const char* Name() const override
{ return "WebRender"; }
88 void SetRoot(Layer
* aLayer
) override
;
90 already_AddRefed
<PaintedLayer
> CreatePaintedLayer() override
{
93 already_AddRefed
<ContainerLayer
> CreateContainerLayer() override
{
96 already_AddRefed
<ImageLayer
> CreateImageLayer() override
{ return nullptr; }
97 already_AddRefed
<ColorLayer
> CreateColorLayer() override
{ return nullptr; }
98 already_AddRefed
<CanvasLayer
> CreateCanvasLayer() override
{ return nullptr; }
100 bool NeedsWidgetInvalidation() override
{ return false; }
102 void SetLayersObserverEpoch(LayersObserverEpoch aEpoch
) override
;
104 void DidComposite(TransactionId aTransactionId
,
105 const mozilla::TimeStamp
& aCompositeStart
,
106 const mozilla::TimeStamp
& aCompositeEnd
) override
;
108 void ClearCachedResources(Layer
* aSubtree
= nullptr) override
;
109 void UpdateTextureFactoryIdentifier(
110 const TextureFactoryIdentifier
& aNewIdentifier
) override
;
111 TextureFactoryIdentifier
GetTextureFactoryIdentifier() override
;
113 void SetTransactionIdAllocator(TransactionIdAllocator
* aAllocator
) override
;
114 TransactionId
GetLastTransactionId() override
;
116 void AddDidCompositeObserver(DidCompositeObserver
* aObserver
) override
;
117 void RemoveDidCompositeObserver(DidCompositeObserver
* aObserver
) override
;
119 void FlushRendering() override
;
120 void WaitOnTransactionProcessed() override
;
122 void SendInvalidRegion(const nsIntRegion
& aRegion
) override
;
124 void ScheduleComposite() override
;
126 void SetNeedsComposite(bool aNeedsComposite
) override
{
127 mNeedsComposite
= aNeedsComposite
;
129 bool NeedsComposite() const override
{ return mNeedsComposite
; }
130 void SetIsFirstPaint() override
{ mIsFirstPaint
= true; }
131 bool GetIsFirstPaint() const override
{ return mIsFirstPaint
; }
132 void SetFocusTarget(const FocusTarget
& aFocusTarget
) override
;
134 already_AddRefed
<PersistentBufferProvider
> CreatePersistentBufferProvider(
135 const gfx::IntSize
& aSize
, gfx::SurfaceFormat aFormat
) override
;
137 bool AsyncPanZoomEnabled() const override
;
139 // adds an imagekey to a list of keys that will be discarded on the next
140 // transaction or destruction
141 void DiscardImages();
142 void DiscardLocalImages();
144 void ClearAsyncAnimations();
145 void WrReleasedImages(const nsTArray
<wr::ExternalImageKeyPair
>& aPairs
);
147 WebRenderBridgeChild
* WrBridge() const { return mWrChild
; }
149 // See equivalent function in ClientLayerManager
150 void LogTestDataForCurrentPaint(ScrollableLayerGuid::ViewID aScrollId
,
151 const std::string
& aKey
,
152 const std::string
& aValue
) {
153 MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me");
154 mApzTestData
.LogTestDataForPaint(mPaintSequenceNumber
, aScrollId
, aKey
,
157 void LogAdditionalTestData(const std::string
& aKey
,
158 const std::string
& aValue
) {
159 MOZ_ASSERT(StaticPrefs::apz_test_logging_enabled(), "don't call me");
160 mApzTestData
.RecordAdditionalData(aKey
, aValue
);
163 // See equivalent function in ClientLayerManager
164 const APZTestData
& GetAPZTestData() const { return mApzTestData
; }
166 WebRenderCommandBuilder
& CommandBuilder() { return mWebRenderCommandBuilder
; }
167 WebRenderUserDataRefTable
* GetWebRenderUserDataTable() {
168 return mWebRenderCommandBuilder
.GetWebRenderUserDataTable();
170 WebRenderScrollData
& GetScrollData() { return mScrollData
; }
173 nsIWidget
* GetWidget() { return mWidget
; }
175 uint32_t StartFrameTimeRecording(int32_t aBufferSize
) override
;
176 void StopFrameTimeRecording(uint32_t aStartIndex
,
177 nsTArray
<float>& aFrameIntervals
) override
;
179 RenderRootStateManager
* GetRenderRootStateManager(
180 wr::RenderRoot aRenderRoot
) {
181 return &mStateManagers
[aRenderRoot
];
184 virtual void PayloadPresented() override
;
186 void TakeCompositionPayloads(nsTArray
<CompositionPayload
>& aPayloads
);
190 * Take a snapshot of the parent context, and copy
193 void MakeSnapshotIfRequired(LayoutDeviceIntSize aSize
);
196 nsIWidget
* MOZ_NON_OWNING_REF mWidget
;
198 RefPtr
<WebRenderBridgeChild
> mWrChild
;
200 RefPtr
<TransactionIdAllocator
> mTransactionIdAllocator
;
201 TransactionId mLatestTransactionId
;
203 nsTArray
<DidCompositeObserver
*> mDidCompositeObservers
;
205 // This holds the scroll data that we need to send to the compositor for
206 // APZ to do it's job
207 WebRenderScrollData mScrollData
;
209 bool mNeedsComposite
;
211 FocusTarget mFocusTarget
;
213 // When we're doing a transaction in order to draw to a non-default
214 // target, the layers transaction is only performed in order to send
215 // a PLayers:Update. We save the original non-default target to
216 // mTarget, and then perform the transaction. After the transaction ends,
217 // we send a message to our remote side to capture the actual pixels
218 // being drawn to the default target, and then copy those pixels
220 RefPtr
<gfxContext
> mTarget
;
222 // See equivalent field in ClientLayerManager
223 uint32_t mPaintSequenceNumber
;
224 // See equivalent field in ClientLayerManager
225 APZTestData mApzTestData
;
227 TimeStamp mTransactionStart
;
229 WebRenderCommandBuilder mWebRenderCommandBuilder
;
231 wr::RenderRootArray
<size_t> mLastDisplayListSizes
;
232 wr::RenderRootArray
<RenderRootStateManager
> mStateManagers
;
233 DisplayItemCache mDisplayItemCache
;
236 } // namespace layers
237 } // namespace mozilla
239 #endif /* GFX_WEBRENDERLAYERMANAGER_H */