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 updateEventRegion();
221
222 void updateAfterWidgetResize();
223 void positionOverflowControlsLayers();
224
225 bool isFrameLayerWithTiledBacking() const { return m_isFrameLayerWithTiledBacking; }
226
227 WEBCORE_EXPORT TiledBacking* tiledBacking() const;
228 void adjustTiledBackingCoverage();
229 void setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom);
230
231 void updateDebugIndicators(bool showBorder, bool showRepaintCounter);
232 bool paintsSubpixelAntialiasedText() const { return m_paintsSubpixelAntialiasedText; }
233
234 // GraphicsLayerClient interface
235 void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
236 void notifyAnimationStarted(const GraphicsLayer*, const String& animationKey, MonotonicTime startTime) override;
237 void notifyFlushRequired(const GraphicsLayer*) override;
238 void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
239
240 void paintContents(const GraphicsLayer*, GraphicsContext&, OptionSet<GraphicsLayerPaintingPhase>, const FloatRect& clip, GraphicsLayerPaintBehavior) override;
241
242 float deviceScaleFactor() const override;
243 float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
244
245 bool paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const override;
246
247 float pageScaleFactor() const override;
248 float zoomedOutPageScaleFactor() const override;
249
250 void didChangePlatformLayerForLayer(const GraphicsLayer*) override;
251 bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
252
253 bool isTrackingRepaints() const override;
254 bool shouldSkipLayerInDump(const GraphicsLayer*, LayerTreeAsTextBehavior) const override;
255 bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName, LayerTreeAsTextBehavior) const override;
256
257 bool shouldAggressivelyRetainTiles(const GraphicsLayer*) const override;
258 bool shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const override;
259 bool useGiantTiles() const override;
260 void logFilledVisibleFreshTile(unsigned) override;
261 bool needsPixelAligment() const override { return !m_isMainFrameRenderViewLayer; }
262
263 LayoutSize subpixelOffsetFromRenderer() const { return m_subpixelOffsetFromRenderer; }
264
265#if PLATFORM(IOS_FAMILY)
266 bool needsIOSDumpRenderTreeMainFrameRenderViewLayerIsAlwaysOpaqueHack(const GraphicsLayer&) const override;
267#endif
268
269#ifndef NDEBUG
270 void verifyNotPainting() override;
271#endif
272
273 WEBCORE_EXPORT LayoutRect contentsBox() const;
274
275 // For informative purposes only.
276 WEBCORE_EXPORT CompositingLayerType compositingLayerType() const;
277
278 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
279 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
280 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
281
282 bool canCompositeFilters() const { return m_canCompositeFilters; }
283#if ENABLE(FILTERS_LEVEL_2)
284 bool canCompositeBackdropFilters() const { return m_canCompositeBackdropFilters; }
285#endif
286
287 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
288 WEBCORE_EXPORT double backingStoreMemoryEstimate() const;
289
290 // For testing only.
291 WEBCORE_EXPORT void setUsesDisplayListDrawing(bool);
292 WEBCORE_EXPORT String displayListAsText(DisplayList::AsTextFlags) const;
293
294 WEBCORE_EXPORT void setIsTrackingDisplayListReplay(bool);
295 WEBCORE_EXPORT String replayDisplayListAsText(DisplayList::AsTextFlags) const;
296
297private:
298 friend class PaintedContentsInfo;
299
300 FloatRect backgroundBoxForSimpleContainerPainting() const;
301
302 void createPrimaryGraphicsLayer();
303 void destroyGraphicsLayers();
304
305 void willDestroyLayer(const GraphicsLayer*);
306
307 LayoutRect compositedBoundsIncludingMargin() const;
308
309 Ref<GraphicsLayer> createGraphicsLayer(const String&, GraphicsLayer::Type = GraphicsLayer::Type::Normal);
310
311 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
312 RenderBox* renderBox() const { return m_owningLayer.renderBox(); }
313 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
314
315 void updateInternalHierarchy();
316 bool updateAncestorClippingLayer(bool needsAncestorClip);
317 bool updateDescendantClippingLayer(bool needsDescendantClip);
318 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
319 bool updateForegroundLayer(bool needsForegroundLayer);
320 bool updateBackgroundLayer(bool needsBackgroundLayer);
321 bool updateMaskingLayer(bool hasMask, bool hasClipPath);
322 bool requiresHorizontalScrollbarLayer() const;
323 bool requiresVerticalScrollbarLayer() const;
324 bool requiresScrollCornerLayer() const;
325 bool updateScrollingLayers(bool scrollingLayers);
326
327 void updateScrollOffset(ScrollOffset);
328 void setLocationOfScrolledContents(ScrollOffset, ScrollingLayerPositionAction);
329
330 void updateChildClippingStrategy(bool needsDescendantsClippingLayer);
331 void updateMaskingLayerGeometry();
332 void updateRootLayerConfiguration();
333 void updatePaintingPhases();
334
335 void setBackgroundLayerPaintsFixedRootBackground(bool);
336
337 LayoutSize contentOffsetInCompositingLayer() const;
338 // Result is transform origin in device pixels.
339 FloatPoint3D computeTransformOriginForPainting(const LayoutRect& borderBox) const;
340
341 void updateOpacity(const RenderStyle&);
342 void updateTransform(const RenderStyle&);
343 void updateFilters(const RenderStyle&);
344#if ENABLE(FILTERS_LEVEL_2)
345 void updateBackdropFilters(const RenderStyle&);
346 void updateBackdropFiltersGeometry();
347#endif
348#if ENABLE(CSS_COMPOSITING)
349 void updateBlendMode(const RenderStyle&);
350#endif
351 void updateCustomAppearance(const RenderStyle&);
352
353 // Return the opacity value that this layer should use for compositing.
354 float compositingOpacity(float rendererOpacity) const;
355 Color rendererBackgroundColor() const;
356
357 bool isMainFrameRenderViewLayer() const;
358
359 bool paintsBoxDecorations() const;
360 bool paintsContent(RenderLayer::PaintedContentRequest&) const;
361
362 void updateDrawsContent(PaintedContentsInfo&);
363
364 // Returns true if this compositing layer has no visible content.
365 bool isSimpleContainerCompositingLayer(PaintedContentsInfo&) const;
366 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
367 bool containsPaintedContent(PaintedContentsInfo&) const;
368 // Returns true if the RenderLayer just contains an image that we can composite directly.
369 bool isDirectlyCompositedImage() const;
370 void updateImageContents(PaintedContentsInfo&);
371
372 void updateDirectlyCompositedBoxDecorations(PaintedContentsInfo&, bool& didUpdateContentsRect);
373 void updateDirectlyCompositedBackgroundColor(PaintedContentsInfo&, bool& didUpdateContentsRect);
374 void updateDirectlyCompositedBackgroundImage(PaintedContentsInfo&, bool& didUpdateContentsRect);
375
376 void resetContentsRect();
377
378 bool isPaintDestinationForDescendantLayers(RenderLayer::PaintedContentRequest&) const;
379 bool hasVisibleNonCompositedDescendants() const;
380
381 bool shouldClipCompositedBounds() const;
382
383 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_isFrameLayerWithTiledBacking); }
384 GraphicsLayer* tileCacheFlatteningLayer() const { return m_isFrameLayerWithTiledBacking ? m_childContainmentLayer.get() : nullptr; }
385
386 void paintIntoLayer(const GraphicsLayer*, GraphicsContext&, const IntRect& paintDirtyRect, OptionSet<PaintBehavior>, OptionSet<GraphicsLayerPaintingPhase>);
387
388 void paintDebugOverlays(const GraphicsLayer*, GraphicsContext&);
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