1/*
2 * Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "FloatPoint.h"
29#include "FloatPoint3D.h"
30#include "GraphicsLayer.h"
31#include "GraphicsLayerClient.h"
32#include "RenderLayer.h"
33#include "RenderLayerCompositor.h"
34#include "ScrollingCoordinator.h"
35
36namespace WebCore {
37
38class KeyframeList;
39class PaintedContentsInfo;
40class RenderLayerCompositor;
41class TiledBacking;
42class TransformationMatrix;
43
44enum CompositingLayerType {
45 NormalCompositingLayer, // non-tiled layer with backing store
46 TiledCompositingLayer, // tiled layer (always has backing store)
47 MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
48 ContainerCompositingLayer // layer with no backing store
49};
50
51// RenderLayerBacking controls the compositing behavior for a single RenderLayer.
52// It holds the various GraphicsLayers, and makes decisions about intra-layer rendering
53// optimizations.
54//
55// There is one RenderLayerBacking for each RenderLayer that is composited.
56
57class RenderLayerBacking final : public GraphicsLayerClient {
58 WTF_MAKE_NONCOPYABLE(RenderLayerBacking); WTF_MAKE_FAST_ALLOCATED;
59public:
60 explicit RenderLayerBacking(RenderLayer&);
61 ~RenderLayerBacking();
62
63#if PLATFORM(IOS_FAMILY)
64 void layerWillBeDestroyed();
65#endif
66
67 // Do cleanup while layer->backing() is still valid.
68 void willBeDestroyed();
69
70 RenderLayer& owningLayer() const { return m_owningLayer; }
71
72 // Included layers are non-z-order descendant layers that are painted into this backing.
73 const Vector<WeakPtr<RenderLayer>>& backingSharingLayers() const { return m_backingSharingLayers; }
74 void setBackingSharingLayers(Vector<WeakPtr<RenderLayer>>&&);
75
76 bool hasBackingSharingLayers() const { return !m_backingSharingLayers.isEmpty(); }
77
78 void removeBackingSharingLayer(RenderLayer&);
79 void clearBackingSharingLayers();
80
81 void updateConfigurationAfterStyleChange();
82
83 // Returns true if layer configuration changed.
84 bool updateConfiguration();
85
86 // Update graphics layer position and bounds.
87 void updateGeometry();
88
89 // Update state the requires that descendant layers have been updated.
90 void updateAfterDescendants();
91
92 // Update contents and clipping structure.
93 void updateDrawsContent();
94
95 void updateAfterLayout(bool needsClippingUpdate, bool needsFullRepaint);
96
97 GraphicsLayer* graphicsLayer() const { return m_graphicsLayer.get(); }
98
99 // Layer to clip children
100 bool hasClippingLayer() const { return (m_childContainmentLayer && !m_isFrameLayerWithTiledBacking); }
101 GraphicsLayer* clippingLayer() const { return !m_isFrameLayerWithTiledBacking ? m_childContainmentLayer.get() : nullptr; }
102
103 // Layer to get clipped by ancestor
104 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer != nullptr; }
105 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
106
107 GraphicsLayer* contentsContainmentLayer() const { return m_contentsContainmentLayer.get(); }
108
109 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
110 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
111 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
112
113 bool requiresBackgroundLayer() const { return m_requiresBackgroundLayer; }
114 void setRequiresBackgroundLayer(bool);
115
116 bool hasScrollingLayer() const { return m_scrollContainerLayer != nullptr; }
117 GraphicsLayer* scrollContainerLayer() const { return m_scrollContainerLayer.get(); }
118 GraphicsLayer* scrolledContentsLayer() const { return m_scrolledContentsLayer.get(); }
119
120 OptionSet<ScrollCoordinationRole> coordinatedScrollingRoles() const;
121
122 void detachFromScrollingCoordinator(OptionSet<ScrollCoordinationRole>);
123
124 ScrollingNodeID scrollingNodeIDForRole(ScrollCoordinationRole role) const
125 {
126 switch (role) {
127 case ScrollCoordinationRole::Scrolling:
128 return m_scrollingNodeID;
129 case ScrollCoordinationRole::FrameHosting:
130 return m_frameHostingNodeID;
131 case ScrollCoordinationRole::ViewportConstrained:
132 return m_viewportConstrainedNodeID;
133 case ScrollCoordinationRole::Positioning:
134 return m_positioningNodeID;
135 }
136 return 0;
137 }
138
139 void setScrollingNodeIDForRole(ScrollingNodeID nodeID, ScrollCoordinationRole role)
140 {
141 switch (role) {
142 case ScrollCoordinationRole::Scrolling:
143 m_scrollingNodeID = nodeID;
144 break;
145 case ScrollCoordinationRole::FrameHosting:
146 m_frameHostingNodeID = nodeID;
147 break;
148 case ScrollCoordinationRole::ViewportConstrained:
149 m_viewportConstrainedNodeID = nodeID;
150 setIsScrollCoordinatedWithViewportConstrainedRole(nodeID);
151 break;
152 case ScrollCoordinationRole::Positioning:
153 m_positioningNodeID = nodeID;
154 break;
155 }
156 }
157
158 ScrollingNodeID scrollingNodeIDForChildren() const
159 {
160 if (m_frameHostingNodeID)
161 return m_frameHostingNodeID;
162
163 if (m_scrollingNodeID)
164 return m_scrollingNodeID;
165
166 if (m_viewportConstrainedNodeID)
167 return m_viewportConstrainedNodeID;
168
169 return m_positioningNodeID;
170 }
171
172 void setIsScrollCoordinatedWithViewportConstrainedRole(bool);
173
174 bool hasMaskLayer() const { return m_maskLayer; }
175 bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer != nullptr; }
176
177 GraphicsLayer* parentForSublayers() const;
178 GraphicsLayer* childForSuperlayers() const;
179
180 // RenderLayers with backing normally short-circuit paintLayer() because
181 // their content is rendered via callbacks from GraphicsLayer. However, the document
182 // layer is special, because it has a GraphicsLayer to act as a container for the GraphicsLayers
183 // for descendants, but its contents usually render into the window (in which case this returns true).
184 // This returns false for other layers, and when the document layer actually needs to paint into its backing store
185 // for some reason.
186 bool paintsIntoWindow() const;
187
188 // Returns true for a composited layer that has no backing store of its own, so
189 // paints into some ancestor layer.
190 bool paintsIntoCompositedAncestor() const { return !m_requiresOwnBackingStore; }
191
192 void setRequiresOwnBackingStore(bool);
193
194 void setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
195 // r is in the coordinate space of the layer's render object
196 void setContentsNeedDisplayInRect(const LayoutRect&, GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
197
198 // Notification from the renderer that its content changed.
199 void contentChanged(ContentChangeType);
200
201 // Interface to start, finish, suspend and resume animations and transitions
202 bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle);
203 void transitionPaused(double timeOffset, CSSPropertyID);
204 void transitionFinished(CSSPropertyID);
205
206 bool startAnimation(double timeOffset, const Animation&, const KeyframeList&);
207 void animationPaused(double timeOffset, const String& name);
208 void animationSeeked(double timeOffset, const String& name);
209 void animationFinished(const String& name);
210
211 void suspendAnimations(MonotonicTime = MonotonicTime());
212 void resumeAnimations();
213
214 LayoutRect compositedBounds() const;
215 // Returns true if changed.
216 bool setCompositedBounds(const LayoutRect&);
217 // Returns true if changed.
218 bool updateCompositedBounds();
219
220 void updateAfterWidgetResize();
221 void positionOverflowControlsLayers();
222
223 bool isFrameLayerWithTiledBacking() const { return m_isFrameLayerWithTiledBacking; }
224
225 WEBCORE_EXPORT TiledBacking* tiledBacking() const;
226 void adjustTiledBackingCoverage();
227 void setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom);
228
229 void updateDebugIndicators(bool showBorder, bool showRepaintCounter);
230 bool paintsSubpixelAntialiasedText() const { return m_paintsSubpixelAntialiasedText; }
231
232 // GraphicsLayerClient interface
233 void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
234 void notifyAnimationStarted(const GraphicsLayer*, const String& animationKey, MonotonicTime startTime) override;
235 void notifyFlushRequired(const GraphicsLayer*) override;
236 void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
237
238 void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clip, GraphicsLayerPaintBehavior) override;
239
240 float deviceScaleFactor() const override;
241 float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
242
243 bool paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const override;
244
245 float pageScaleFactor() const override;
246 float zoomedOutPageScaleFactor() const override;
247
248 void didChangePlatformLayerForLayer(const GraphicsLayer*) override;
249 bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
250
251 bool isTrackingRepaints() const override;
252 bool shouldSkipLayerInDump(const GraphicsLayer*, LayerTreeAsTextBehavior) const override;
253 bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName, LayerTreeAsTextBehavior) const override;
254
255 bool shouldAggressivelyRetainTiles(const GraphicsLayer*) const override;
256 bool shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const override;
257 bool useGiantTiles() const override;
258 void logFilledVisibleFreshTile(unsigned) override;
259 bool needsPixelAligment() const override { return !m_isMainFrameRenderViewLayer; }
260
261 LayoutSize subpixelOffsetFromRenderer() const { return m_subpixelOffsetFromRenderer; }
262
263#if PLATFORM(IOS_FAMILY)
264 bool needsIOSDumpRenderTreeMainFrameRenderViewLayerIsAlwaysOpaqueHack(const GraphicsLayer&) const override;
265#endif
266
267#ifndef NDEBUG
268 void verifyNotPainting() override;
269#endif
270
271 WEBCORE_EXPORT LayoutRect contentsBox() const;
272
273 // For informative purposes only.
274 WEBCORE_EXPORT CompositingLayerType compositingLayerType() const;
275
276 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
277 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
278 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
279
280 bool canCompositeFilters() const { return m_canCompositeFilters; }
281#if ENABLE(FILTERS_LEVEL_2)
282 bool canCompositeBackdropFilters() const { return m_canCompositeBackdropFilters; }
283#endif
284
285 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
286 WEBCORE_EXPORT double backingStoreMemoryEstimate() const;
287
288 // For testing only.
289 WEBCORE_EXPORT void setUsesDisplayListDrawing(bool);
290 WEBCORE_EXPORT String displayListAsText(DisplayList::AsTextFlags) const;
291
292 WEBCORE_EXPORT void setIsTrackingDisplayListReplay(bool);
293 WEBCORE_EXPORT String replayDisplayListAsText(DisplayList::AsTextFlags) const;
294
295private:
296 friend class PaintedContentsInfo;
297
298 FloatRect backgroundBoxForSimpleContainerPainting() const;
299
300 void createPrimaryGraphicsLayer();
301 void destroyGraphicsLayers();
302
303 void willDestroyLayer(const GraphicsLayer*);
304
305 LayoutRect compositedBoundsIncludingMargin() const;
306
307 Ref<GraphicsLayer> createGraphicsLayer(const String&, GraphicsLayer::Type = GraphicsLayer::Type::Normal);
308
309 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
310 RenderBox* renderBox() const { return m_owningLayer.renderBox(); }
311 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
312
313 void updateInternalHierarchy();
314 bool updateAncestorClippingLayer(bool needsAncestorClip);
315 bool updateDescendantClippingLayer(bool needsDescendantClip);
316 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
317 bool updateForegroundLayer(bool needsForegroundLayer);
318 bool updateBackgroundLayer(bool needsBackgroundLayer);
319 void updateMaskingLayer(bool hasMask, bool hasClipPath);
320 bool requiresHorizontalScrollbarLayer() const;
321 bool requiresVerticalScrollbarLayer() const;
322 bool requiresScrollCornerLayer() const;
323 bool updateScrollingLayers(bool scrollingLayers);
324
325 void updateScrollOffset(ScrollOffset);
326 void setLocationOfScrolledContents(ScrollOffset, ScrollingLayerPositionAction);
327
328 void updateChildClippingStrategy(bool needsDescendantsClippingLayer);
329
330 void updateMaskingLayerGeometry();
331
332 void updateRootLayerConfiguration();
333
334 void setBackgroundLayerPaintsFixedRootBackground(bool);
335
336 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
337
338 LayoutSize contentOffsetInCompositingLayer() const;
339 // Result is transform origin in device pixels.
340 FloatPoint3D computeTransformOriginForPainting(const LayoutRect& borderBox) const;
341
342 void updateOpacity(const RenderStyle&);
343 void updateTransform(const RenderStyle&);
344 void updateFilters(const RenderStyle&);
345#if ENABLE(FILTERS_LEVEL_2)
346 void updateBackdropFilters(const RenderStyle&);
347 void updateBackdropFiltersGeometry();
348#endif
349#if ENABLE(CSS_COMPOSITING)
350 void updateBlendMode(const RenderStyle&);
351#endif
352 void updateCustomAppearance(const RenderStyle&);
353
354 // Return the opacity value that this layer should use for compositing.
355 float compositingOpacity(float rendererOpacity) const;
356 Color rendererBackgroundColor() const;
357
358 bool isMainFrameRenderViewLayer() const;
359
360 bool paintsBoxDecorations() const;
361 bool paintsContent(RenderLayer::PaintedContentRequest&) const;
362
363 void updateDrawsContent(PaintedContentsInfo&);
364 void updateEventRegion();
365
366 // Returns true if this compositing layer has no visible content.
367 bool isSimpleContainerCompositingLayer(PaintedContentsInfo&) const;
368 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
369 bool containsPaintedContent(PaintedContentsInfo&) const;
370 // Returns true if the RenderLayer just contains an image that we can composite directly.
371 bool isDirectlyCompositedImage() const;
372 void updateImageContents(PaintedContentsInfo&);
373
374 void updateDirectlyCompositedBoxDecorations(PaintedContentsInfo&, bool& didUpdateContentsRect);
375 void updateDirectlyCompositedBackgroundColor(PaintedContentsInfo&, bool& didUpdateContentsRect);
376 void updateDirectlyCompositedBackgroundImage(PaintedContentsInfo&, bool& didUpdateContentsRect);
377
378 void resetContentsRect();
379
380 bool isPaintDestinationForDescendantLayers(RenderLayer::PaintedContentRequest&) const;
381 bool hasVisibleNonCompositedDescendants() const;
382
383 bool shouldClipCompositedBounds() const;
384
385 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_isFrameLayerWithTiledBacking); }
386 GraphicsLayer* tileCacheFlatteningLayer() const { return m_isFrameLayerWithTiledBacking ? m_childContainmentLayer.get() : nullptr; }
387
388 void paintIntoLayer(const GraphicsLayer*, GraphicsContext&, const IntRect& paintDirtyRect, OptionSet<PaintBehavior>, GraphicsLayerPaintingPhase);
389
390 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID);
391 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID);
392
393 bool canIssueSetNeedsDisplay() const { return !paintsIntoWindow() && !paintsIntoCompositedAncestor(); }
394 LayoutRect computeParentGraphicsLayerRect(RenderLayer* compositedAncestor, LayoutSize& ancestorClippingLayerOffset) const;
395 LayoutRect computePrimaryGraphicsLayerRect(const LayoutRect& parentGraphicsLayerRect) const;
396
397 RenderLayer& m_owningLayer;
398
399 // A list other layers that paint into this backing store, later than m_owningLayer in paint order.
400 Vector<WeakPtr<RenderLayer>> m_backingSharingLayers;
401
402 RefPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
403 RefPtr<GraphicsLayer> m_contentsContainmentLayer; // Only used if we have a background layer; takes the transform.
404 RefPtr<GraphicsLayer> m_graphicsLayer;
405 RefPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
406 RefPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
407 RefPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children, or if the layer has a tile cache.
408 RefPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask and/or clip-path.
409 RefPtr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
410
411 RefPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
412 RefPtr<GraphicsLayer> m_layerForVerticalScrollbar;
413 RefPtr<GraphicsLayer> m_layerForScrollCorner;
414
415 RefPtr<GraphicsLayer> m_scrollContainerLayer; // Only used if the layer is using composited scrolling.
416 RefPtr<GraphicsLayer> m_scrolledContentsLayer; // Only used if the layer is using composited scrolling.
417
418 LayoutRect m_compositedBounds;
419 LayoutSize m_subpixelOffsetFromRenderer; // This is the subpixel distance between the primary graphics layer and the associated renderer's bounds.
420 LayoutSize m_compositedBoundsOffsetFromGraphicsLayer; // This is the subpixel distance between the primary graphics layer and the render layer bounds.
421
422 ScrollingNodeID m_viewportConstrainedNodeID { 0 };
423 ScrollingNodeID m_scrollingNodeID { 0 };
424 ScrollingNodeID m_frameHostingNodeID { 0 };
425 ScrollingNodeID m_positioningNodeID { 0 };
426
427 bool m_artificiallyInflatedBounds { false }; // bounds had to be made non-zero to make transform-origin work
428 bool m_isMainFrameRenderViewLayer { false };
429 bool m_isFrameLayerWithTiledBacking { false };
430 bool m_requiresOwnBackingStore { true };
431 bool m_canCompositeFilters { false };
432#if ENABLE(FILTERS_LEVEL_2)
433 bool m_canCompositeBackdropFilters { false };
434#endif
435 bool m_backgroundLayerPaintsFixedRootBackground { false };
436 bool m_requiresBackgroundLayer { false };
437 bool m_hasSubpixelRounding { false };
438 bool m_paintsSubpixelAntialiasedText { false }; // This is for logging only.
439};
440
441enum CanvasCompositingStrategy {
442 UnacceleratedCanvas,
443 CanvasPaintedToLayer,
444 CanvasAsLayerContents
445};
446CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject&);
447
448WTF::TextStream& operator<<(WTF::TextStream&, const RenderLayerBacking&);
449
450} // namespace WebCore
451