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_CLIENTLAYERMANAGER_H
7 : #define GFX_CLIENTLAYERMANAGER_H
8 :
9 : #include <stdint.h> // for int32_t
10 : #include "Layers.h"
11 : #include "gfxContext.h" // for gfxContext
12 : #include "gfxPrefs.h"
13 : #include "mozilla/Attributes.h" // for override
14 : #include "mozilla/LinkedList.h" // For LinkedList
15 : #include "mozilla/WidgetUtils.h" // for ScreenRotation
16 : #include "mozilla/gfx/Rect.h" // for Rect
17 : #include "mozilla/layers/CompositorTypes.h"
18 : #include "mozilla/layers/FocusTarget.h" // for FocusTarget
19 : #include "mozilla/layers/LayersTypes.h" // for BufferMode, LayersBackend, etc
20 : #include "mozilla/layers/ShadowLayers.h" // for ShadowLayerForwarder, etc
21 : #include "mozilla/layers/APZTestData.h" // for APZTestData
22 : #include "nsCOMPtr.h" // for already_AddRefed
23 : #include "nsIObserver.h" // for nsIObserver
24 : #include "nsISupportsImpl.h" // for Layer::Release, etc
25 : #include "nsRect.h" // for mozilla::gfx::IntRect
26 : #include "nsTArray.h" // for nsTArray
27 : #include "nscore.h" // for nsAString
28 : #include "mozilla/layers/TransactionIdAllocator.h"
29 : #include "nsIWidget.h" // For plugin window configuration information structs
30 :
31 : class nsDisplayListBuilder;
32 :
33 : namespace mozilla {
34 :
35 : namespace dom {
36 : class TabGroup;
37 : }
38 : namespace layers {
39 :
40 : using dom::TabGroup;
41 :
42 : class ClientPaintedLayer;
43 : class CompositorBridgeChild;
44 : class ImageLayer;
45 : class FrameUniformityData;
46 :
47 : class ClientLayerManager final : public LayerManager
48 : {
49 : typedef nsTArray<RefPtr<Layer> > LayerRefArray;
50 :
51 : public:
52 : explicit ClientLayerManager(nsIWidget* aWidget);
53 :
54 : virtual void Destroy() override;
55 :
56 : protected:
57 : virtual ~ClientLayerManager();
58 :
59 : public:
60 316 : virtual ShadowLayerForwarder* AsShadowForwarder() override
61 : {
62 316 : return mForwarder;
63 : }
64 :
65 2 : virtual KnowsCompositor* AsKnowsCompositor() override
66 : {
67 2 : return mForwarder;
68 : }
69 :
70 2 : virtual ClientLayerManager* AsClientLayerManager() override
71 : {
72 2 : return this;
73 : }
74 :
75 : TabGroup* GetTabGroup();
76 :
77 : virtual int32_t GetMaxTextureSize() const override;
78 :
79 : virtual void SetDefaultTargetConfiguration(BufferMode aDoubleBuffering, ScreenRotation aRotation);
80 : virtual bool BeginTransactionWithTarget(gfxContext* aTarget) override;
81 : virtual bool BeginTransaction() override;
82 : virtual bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT) override;
83 : virtual void EndTransaction(DrawPaintedLayerCallback aCallback,
84 : void* aCallbackData,
85 : EndTransactionFlags aFlags = END_DEFAULT) override;
86 :
87 811 : virtual LayersBackend GetBackendType() override { return LayersBackend::LAYERS_CLIENT; }
88 1 : virtual LayersBackend GetCompositorBackendType() override
89 : {
90 1 : return AsShadowForwarder()->GetCompositorBackendType();
91 : }
92 : virtual void GetBackendName(nsAString& name) override;
93 0 : virtual const char* Name() const override { return "Client"; }
94 :
95 : virtual void SetRoot(Layer* aLayer) override;
96 :
97 : virtual void Mutated(Layer* aLayer) override;
98 : virtual void MutatedSimple(Layer* aLayer) override;
99 :
100 : virtual already_AddRefed<PaintedLayer> CreatePaintedLayer() override;
101 : virtual already_AddRefed<PaintedLayer> CreatePaintedLayerWithHint(PaintedLayerCreationHint aHint) override;
102 : virtual already_AddRefed<ContainerLayer> CreateContainerLayer() override;
103 : virtual already_AddRefed<ImageLayer> CreateImageLayer() override;
104 : virtual already_AddRefed<CanvasLayer> CreateCanvasLayer() override;
105 : virtual already_AddRefed<ReadbackLayer> CreateReadbackLayer() override;
106 : virtual already_AddRefed<ColorLayer> CreateColorLayer() override;
107 : virtual already_AddRefed<TextLayer> CreateTextLayer() override;
108 : virtual already_AddRefed<BorderLayer> CreateBorderLayer() override;
109 : virtual already_AddRefed<RefLayer> CreateRefLayer() override;
110 :
111 : virtual void UpdateTextureFactoryIdentifier(const TextureFactoryIdentifier& aNewIdentifier,
112 : uint64_t aDeviceResetSeqNo) override;
113 1 : virtual TextureFactoryIdentifier GetTextureFactoryIdentifier() override
114 : {
115 1 : return AsShadowForwarder()->GetTextureFactoryIdentifier();
116 : }
117 :
118 : virtual void FlushRendering() override;
119 : virtual void WaitOnTransactionProcessed() override;
120 : virtual void SendInvalidRegion(const nsIntRegion& aRegion) override;
121 :
122 : virtual uint32_t StartFrameTimeRecording(int32_t aBufferSize) override;
123 :
124 : virtual void StopFrameTimeRecording(uint32_t aStartIndex,
125 : nsTArray<float>& aFrameIntervals) override;
126 :
127 82 : virtual bool NeedsWidgetInvalidation() override { return false; }
128 :
129 : ShadowableLayer* Hold(Layer* aLayer);
130 :
131 490 : bool HasShadowManager() const { return mForwarder->HasShadowManager(); }
132 :
133 : virtual bool IsCompositingCheap() override;
134 0 : virtual bool HasShadowManagerInternal() const override { return HasShadowManager(); }
135 :
136 : virtual void SetIsFirstPaint() override;
137 :
138 : virtual void SetFocusTarget(const FocusTarget& aFocusTarget) override;
139 :
140 : /**
141 : * Pass through call to the forwarder for nsPresContext's
142 : * CollectPluginGeometryUpdates. Passes widget configuration information
143 : * to the compositor for transmission to the chrome process. This
144 : * configuration gets set when the window paints.
145 : */
146 : void StorePluginWidgetConfigurations(const nsTArray<nsIWidget::Configuration>&
147 : aConfigurations) override;
148 :
149 : // Drop cached resources and ask our shadow manager to do the same,
150 : // if we have one.
151 : virtual void ClearCachedResources(Layer* aSubtree = nullptr) override;
152 :
153 : void HandleMemoryPressure();
154 :
155 0 : void SetRepeatTransaction() { mRepeatTransaction = true; }
156 135 : bool GetRepeatTransaction() { return mRepeatTransaction; }
157 :
158 0 : bool IsRepeatTransaction() { return mIsRepeatTransaction; }
159 :
160 0 : void SetTransactionIncomplete() { mTransactionIncomplete = true; }
161 :
162 0 : bool HasShadowTarget() { return !!mShadowTarget; }
163 :
164 : void SetShadowTarget(gfxContext* aTarget) { mShadowTarget = aTarget; }
165 :
166 77 : bool CompositorMightResample() { return mCompositorMightResample; }
167 :
168 66 : DrawPaintedLayerCallback GetPaintedLayerCallback() const
169 66 : { return mPaintedLayerCallback; }
170 :
171 33 : void* GetPaintedLayerCallbackData() const
172 33 : { return mPaintedLayerCallbackData; }
173 :
174 : CompositorBridgeChild* GetRemoteRenderer();
175 :
176 : virtual CompositorBridgeChild* GetCompositorBridgeChild() override;
177 :
178 : // Disable component alpha layers with the software compositor.
179 54 : virtual bool ShouldAvoidComponentAlphaLayers() override { return !IsCompositingCheap(); }
180 :
181 734 : bool InConstruction() { return mPhase == PHASE_CONSTRUCTION; }
182 : #ifdef DEBUG
183 156 : bool InDrawing() { return mPhase == PHASE_DRAWING; }
184 : bool InForward() { return mPhase == PHASE_FORWARD; }
185 : #endif
186 29 : bool InTransaction() { return mPhase != PHASE_NONE; }
187 :
188 1 : virtual void SetNeedsComposite(bool aNeedsComposite) override
189 : {
190 1 : mNeedsComposite = aNeedsComposite;
191 1 : }
192 1 : virtual bool NeedsComposite() const override { return mNeedsComposite; }
193 :
194 : virtual void ScheduleComposite() override;
195 : virtual void GetFrameUniformity(FrameUniformityData* aFrameUniformityData) override;
196 :
197 : virtual void DidComposite(uint64_t aTransactionId,
198 : const mozilla::TimeStamp& aCompositeStart,
199 : const mozilla::TimeStamp& aCompositeEnd) override;
200 :
201 : virtual bool AreComponentAlphaLayersEnabled() override;
202 : virtual bool SupportsBackdropCopyForComponentAlpha() override;
203 :
204 : // Log APZ test data for the current paint. We supply the paint sequence
205 : // number ourselves, and take care of calling APZTestData::StartNewPaint()
206 : // when a new paint is started.
207 0 : void LogTestDataForCurrentPaint(FrameMetrics::ViewID aScrollId,
208 : const std::string& aKey,
209 : const std::string& aValue)
210 : {
211 0 : MOZ_ASSERT(gfxPrefs::APZTestLoggingEnabled(), "don't call me");
212 0 : mApzTestData.LogTestDataForPaint(mPaintSequenceNumber, aScrollId, aKey, aValue);
213 0 : }
214 :
215 : // Log APZ test data for a repaint request. The sequence number must be
216 : // passed in from outside, and APZTestData::StartNewRepaintRequest() needs
217 : // to be called from the outside as well when a new repaint request is started.
218 : void StartNewRepaintRequest(SequenceNumber aSequenceNumber);
219 :
220 : // TODO(botond): When we start using this and write a wrapper similar to
221 : // nsLayoutUtils::LogTestDataForPaint(), make sure that wrapper checks
222 : // gfxPrefs::APZTestLoggingEnabled().
223 : void LogTestDataForRepaintRequest(SequenceNumber aSequenceNumber,
224 : FrameMetrics::ViewID aScrollId,
225 : const std::string& aKey,
226 : const std::string& aValue)
227 : {
228 : MOZ_ASSERT(gfxPrefs::APZTestLoggingEnabled(), "don't call me");
229 : mApzTestData.LogTestDataForRepaintRequest(aSequenceNumber, aScrollId, aKey, aValue);
230 : }
231 :
232 : // Get the content-side APZ test data for reading. For writing, use the
233 : // LogTestData...() functions.
234 0 : const APZTestData& GetAPZTestData() const {
235 0 : return mApzTestData;
236 : }
237 :
238 : // Get a copy of the compositor-side APZ test data for our layers ID.
239 : void GetCompositorSideAPZTestData(APZTestData* aData) const;
240 :
241 : virtual void SetTransactionIdAllocator(TransactionIdAllocator* aAllocator) override;
242 :
243 25 : virtual uint64_t GetLastTransactionId() override { return mLatestTransactionId; }
244 :
245 : float RequestProperty(const nsAString& aProperty) override;
246 :
247 : bool AsyncPanZoomEnabled() const override;
248 :
249 : virtual void SetLayerObserverEpoch(uint64_t aLayerObserverEpoch) override;
250 :
251 : virtual void AddDidCompositeObserver(DidCompositeObserver* aObserver) override;
252 : virtual void RemoveDidCompositeObserver(DidCompositeObserver* aObserver) override;
253 :
254 : virtual already_AddRefed<PersistentBufferProvider>
255 : CreatePersistentBufferProvider(const gfx::IntSize& aSize, gfx::SurfaceFormat aFormat) override;
256 :
257 0 : static PaintTiming* MaybeGetPaintTiming(LayerManager* aManager) {
258 0 : if (!aManager) {
259 0 : return nullptr;
260 : }
261 0 : if (ClientLayerManager* lm = aManager->AsClientLayerManager()) {
262 0 : return &lm->AsShadowForwarder()->GetPaintTiming();
263 : }
264 0 : return nullptr;
265 : }
266 :
267 : protected:
268 : enum TransactionPhase {
269 : PHASE_NONE, PHASE_CONSTRUCTION, PHASE_DRAWING, PHASE_FORWARD
270 : };
271 : TransactionPhase mPhase;
272 :
273 : private:
274 : // Listen memory-pressure event for ClientLayerManager
275 : class MemoryPressureObserver final : public nsIObserver
276 : {
277 : public:
278 : NS_DECL_ISUPPORTS
279 : NS_DECL_NSIOBSERVER
280 :
281 2 : explicit MemoryPressureObserver(ClientLayerManager* aClientLayerManager)
282 2 : : mClientLayerManager(aClientLayerManager)
283 : {
284 2 : RegisterMemoryPressureEvent();
285 2 : }
286 :
287 : void Destroy();
288 :
289 : private:
290 0 : virtual ~MemoryPressureObserver() {}
291 : void RegisterMemoryPressureEvent();
292 : void UnregisterMemoryPressureEvent();
293 :
294 : ClientLayerManager* mClientLayerManager;
295 : };
296 :
297 : /**
298 : * Forward transaction results to the parent context.
299 : */
300 : void ForwardTransaction(bool aScheduleComposite);
301 :
302 : /**
303 : * Take a snapshot of the parent context, and copy
304 : * it into mShadowTarget.
305 : */
306 : void MakeSnapshotIfRequired();
307 :
308 : void ClearLayer(Layer* aLayer);
309 :
310 : void HandleMemoryPressureLayer(Layer* aLayer);
311 :
312 : bool EndTransactionInternal(DrawPaintedLayerCallback aCallback,
313 : void* aCallbackData,
314 : EndTransactionFlags);
315 :
316 : LayerRefArray mKeepAlive;
317 :
318 : nsIWidget* mWidget;
319 :
320 : /* PaintedLayer callbacks; valid at the end of a transaciton,
321 : * while rendering */
322 : DrawPaintedLayerCallback mPaintedLayerCallback;
323 : void *mPaintedLayerCallbackData;
324 :
325 : // When we're doing a transaction in order to draw to a non-default
326 : // target, the layers transaction is only performed in order to send
327 : // a PLayers:Update. We save the original non-default target to
328 : // mShadowTarget, and then perform the transaction using
329 : // mDummyTarget as the render target. After the transaction ends,
330 : // we send a message to our remote side to capture the actual pixels
331 : // being drawn to the default target, and then copy those pixels
332 : // back to mShadowTarget.
333 : RefPtr<gfxContext> mShadowTarget;
334 :
335 : RefPtr<TransactionIdAllocator> mTransactionIdAllocator;
336 : uint64_t mLatestTransactionId;
337 : TimeDuration mLastPaintTime;
338 :
339 : // Sometimes we draw to targets that don't natively support
340 : // landscape/portrait orientation. When we need to implement that
341 : // ourselves, |mTargetRotation| describes the induced transform we
342 : // need to apply when compositing content to our target.
343 : ScreenRotation mTargetRotation;
344 :
345 : // Used to repeat the transaction right away (to avoid rebuilding
346 : // a display list) to support progressive drawing.
347 : bool mRepeatTransaction;
348 : bool mIsRepeatTransaction;
349 : bool mTransactionIncomplete;
350 : bool mCompositorMightResample;
351 : bool mNeedsComposite;
352 :
353 : // An incrementing sequence number for paints.
354 : // Incremented in BeginTransaction(), but not for repeat transactions.
355 : uint32_t mPaintSequenceNumber;
356 :
357 : // A sequence number for checking whether we have not yet acknowledged
358 : // a device reset.
359 : uint64_t mDeviceResetSequenceNumber;
360 :
361 : APZTestData mApzTestData;
362 :
363 : RefPtr<ShadowLayerForwarder> mForwarder;
364 : mozilla::TimeStamp mTransactionStart;
365 :
366 : nsTArray<DidCompositeObserver*> mDidCompositeObservers;
367 :
368 : RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
369 : };
370 :
371 : class ClientLayer : public ShadowableLayer
372 : {
373 : public:
374 31 : ClientLayer()
375 31 : {
376 31 : MOZ_COUNT_CTOR(ClientLayer);
377 31 : }
378 :
379 : ~ClientLayer();
380 :
381 : // Shrink memory usage.
382 : // Called when "memory-pressure" is observed.
383 0 : virtual void HandleMemoryPressure() { }
384 :
385 : virtual void RenderLayer() = 0;
386 58 : virtual void RenderLayerWithReadback(ReadbackProcessor *aReadback) { RenderLayer(); }
387 :
388 0 : virtual ClientPaintedLayer* AsThebes() { return nullptr; }
389 :
390 : static inline ClientLayer *
391 654 : ToClientLayer(Layer* aLayer)
392 : {
393 654 : return static_cast<ClientLayer*>(aLayer->ImplData());
394 : }
395 :
396 : template <typename LayerType>
397 139 : static inline void RenderMaskLayers(LayerType* aLayer) {
398 139 : if (aLayer->GetMaskLayer()) {
399 0 : ToClientLayer(aLayer->GetMaskLayer())->RenderLayer();
400 : }
401 139 : for (size_t i = 0; i < aLayer->GetAncestorMaskLayerCount(); i++) {
402 0 : ToClientLayer(aLayer->GetAncestorMaskLayerAt(i))->RenderLayer();
403 : }
404 139 : }
405 : };
406 :
407 : // Create a LayerHandle for aLayer, if we're forwarding our layer tree
408 : // to a parent process. Record the new layer creation in the current
409 : // open transaction as a side effect.
410 : template<typename CreatedMethod> void
411 31 : CreateShadowFor(ClientLayer* aLayer,
412 : ClientLayerManager* aMgr,
413 : CreatedMethod aMethod)
414 : {
415 31 : LayerHandle shadow = aMgr->AsShadowForwarder()->ConstructShadowFor(aLayer);
416 31 : if (!shadow) {
417 0 : return;
418 : }
419 :
420 31 : aLayer->SetShadow(aMgr->AsShadowForwarder(), shadow);
421 31 : (aMgr->AsShadowForwarder()->*aMethod)(aLayer);
422 31 : aMgr->Hold(aLayer->AsLayer());
423 : }
424 :
425 : #define CREATE_SHADOW(_type) \
426 : CreateShadowFor(layer, this, \
427 : &ShadowLayerForwarder::Created ## _type ## Layer)
428 :
429 :
430 : } // namespace layers
431 : } // namespace mozilla
432 :
433 : #endif /* GFX_CLIENTLAYERMANAGER_H */
|