[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / platform / graphics / ca / GraphicsLayerCA.cpp
1 /*
2  * Copyright (C) 2010-2017 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 #include "config.h"
27 #include "GraphicsLayerCA.h"
28
29 #if USE(CA)
30
31 #include "Animation.h"
32 #include "DisplayListRecorder.h"
33 #include "DisplayListReplayer.h"
34 #include "FloatConversion.h"
35 #include "FloatRect.h"
36 #include "GraphicsLayerFactory.h"
37 #include "Image.h"
38 #include "Logging.h"
39 #include "PlatformCAFilters.h"
40 #include "PlatformCALayer.h"
41 #include "PlatformScreen.h"
42 #include "Region.h"
43 #include "RotateTransformOperation.h"
44 #include "ScaleTransformOperation.h"
45 #include "TiledBacking.h"
46 #include "TransformState.h"
47 #include "TranslateTransformOperation.h"
48 #include <QuartzCore/CATransform3D.h>
49 #include <limits.h>
50 #include <pal/spi/cf/CFUtilitiesSPI.h>
51 #include <wtf/CheckedArithmetic.h>
52 #include <wtf/HexNumber.h>
53 #include <wtf/MathExtras.h>
54 #include <wtf/NeverDestroyed.h>
55 #include <wtf/PointerComparison.h>
56 #include <wtf/SetForScope.h>
57 #include <wtf/SystemTracing.h>
58 #include <wtf/text/StringConcatenateNumbers.h>
59 #include <wtf/text/TextStream.h>
60
61 #if PLATFORM(IOS_FAMILY)
62 #include "SystemMemory.h"
63 #include "WebCoreThread.h"
64 #endif
65
66 #if PLATFORM(COCOA)
67 #include "PlatformCAAnimationCocoa.h"
68 #include "PlatformCALayerCocoa.h"
69 #endif
70
71 #if PLATFORM(WIN)
72 #include "PlatformCAAnimationWin.h"
73 #include "PlatformCALayerWin.h"
74 #endif
75
76 #if COMPILER(MSVC)
77 // See https://msdn.microsoft.com/en-us/library/1wea5zwe.aspx
78 #pragma warning(disable: 4701)
79 #endif
80
81 namespace WebCore {
82
83 // The threshold width or height above which a tiled layer will be used. This should be
84 // large enough to avoid tiled layers for most GraphicsLayers, but less than the OpenGL
85 // texture size limit on all supported hardware.
86 #if PLATFORM(IOS_FAMILY)
87 static const int cMaxPixelDimension = 1280;
88 static const int cMaxPixelDimensionLowMemory = 1024;
89 static const int cMemoryLevelToUseSmallerPixelDimension = 35;
90 #else
91 static const int cMaxPixelDimension = 2048;
92 #endif
93
94 // Derived empirically: <rdar://problem/13401861>
95 static const unsigned cMaxLayerTreeDepth = 128;
96
97 // About 10 screens of an iPhone 6 Plus. <rdar://problem/44532782>
98 static const unsigned cMaxTotalBackdropFilterArea = 1242 * 2208 * 10;
99
100 // If we send a duration of 0 to CA, then it will use the default duration
101 // of 250ms. So send a very small value instead.
102 static const float cAnimationAlmostZeroDuration = 1e-3f;
103
104 static bool isTransformTypeTransformationMatrix(TransformOperation::OperationType transformType)
105 {
106     switch (transformType) {
107     case TransformOperation::SKEW_X:
108     case TransformOperation::SKEW_Y:
109     case TransformOperation::SKEW:
110     case TransformOperation::MATRIX:
111     case TransformOperation::ROTATE_3D:
112     case TransformOperation::MATRIX_3D:
113     case TransformOperation::PERSPECTIVE:
114     case TransformOperation::IDENTITY:
115     case TransformOperation::NONE:
116         return true;
117     default:
118         return false;
119     }
120 }
121
122 static bool isTransformTypeFloatPoint3D(TransformOperation::OperationType transformType)
123 {
124     switch (transformType) {
125     case TransformOperation::SCALE:
126     case TransformOperation::SCALE_3D:
127     case TransformOperation::TRANSLATE:
128     case TransformOperation::TRANSLATE_3D:
129         return true;
130     default:
131         return false;
132     }
133 }
134
135 static bool isTransformTypeNumber(TransformOperation::OperationType transformType)
136 {
137     return !isTransformTypeTransformationMatrix(transformType) && !isTransformTypeFloatPoint3D(transformType);
138 }
139
140 static void getTransformFunctionValue(const TransformOperation* transformOp, TransformOperation::OperationType transformType, const FloatSize& size, float& value)
141 {
142     switch (transformType) {
143     case TransformOperation::ROTATE:
144     case TransformOperation::ROTATE_X:
145     case TransformOperation::ROTATE_Y:
146         value = transformOp ? narrowPrecisionToFloat(deg2rad(downcast<RotateTransformOperation>(*transformOp).angle())) : 0;
147         break;
148     case TransformOperation::SCALE_X:
149         value = transformOp ? narrowPrecisionToFloat(downcast<ScaleTransformOperation>(*transformOp).x()) : 1;
150         break;
151     case TransformOperation::SCALE_Y:
152         value = transformOp ? narrowPrecisionToFloat(downcast<ScaleTransformOperation>(*transformOp).y()) : 1;
153         break;
154     case TransformOperation::SCALE_Z:
155         value = transformOp ? narrowPrecisionToFloat(downcast<ScaleTransformOperation>(*transformOp).z()) : 1;
156         break;
157     case TransformOperation::TRANSLATE_X:
158         value = transformOp ? narrowPrecisionToFloat(downcast<TranslateTransformOperation>(*transformOp).x(size)) : 0;
159         break;
160     case TransformOperation::TRANSLATE_Y:
161         value = transformOp ? narrowPrecisionToFloat(downcast<TranslateTransformOperation>(*transformOp).y(size)) : 0;
162         break;
163     case TransformOperation::TRANSLATE_Z:
164         value = transformOp ? narrowPrecisionToFloat(downcast<TranslateTransformOperation>(*transformOp).z(size)) : 0;
165         break;
166     default:
167         break;
168     }
169 }
170
171 static void getTransformFunctionValue(const TransformOperation* transformOp, TransformOperation::OperationType transformType, const FloatSize& size, FloatPoint3D& value)
172 {
173     switch (transformType) {
174     case TransformOperation::SCALE:
175     case TransformOperation::SCALE_3D: {
176         const auto* scaleTransformOp = downcast<ScaleTransformOperation>(transformOp);
177         value.setX(scaleTransformOp ? narrowPrecisionToFloat(scaleTransformOp->x()) : 1);
178         value.setY(scaleTransformOp ? narrowPrecisionToFloat(scaleTransformOp->y()) : 1);
179         value.setZ(scaleTransformOp ? narrowPrecisionToFloat(scaleTransformOp->z()) : 1);
180         break;
181     }
182     case TransformOperation::TRANSLATE:
183     case TransformOperation::TRANSLATE_3D: {
184         const auto* translateTransformOp = downcast<TranslateTransformOperation>(transformOp);
185         value.setX(translateTransformOp ? narrowPrecisionToFloat(translateTransformOp->x(size)) : 0);
186         value.setY(translateTransformOp ? narrowPrecisionToFloat(translateTransformOp->y(size)) : 0);
187         value.setZ(translateTransformOp ? narrowPrecisionToFloat(translateTransformOp->z(size)) : 0);
188         break;
189     }
190     default:
191         break;
192     }
193 }
194
195 static void getTransformFunctionValue(const TransformOperation* transformOp, TransformOperation::OperationType transformType, const FloatSize& size, TransformationMatrix& value)
196 {
197     switch (transformType) {
198     case TransformOperation::SKEW_X:
199     case TransformOperation::SKEW_Y:
200     case TransformOperation::SKEW:
201     case TransformOperation::MATRIX:
202     case TransformOperation::ROTATE_3D:
203     case TransformOperation::MATRIX_3D:
204     case TransformOperation::PERSPECTIVE:
205     case TransformOperation::IDENTITY:
206     case TransformOperation::NONE:
207         if (transformOp)
208             transformOp->apply(value, size);
209         else
210             value.makeIdentity();
211         break;
212     default:
213         break;
214     }
215 }
216
217 static PlatformCAAnimation::ValueFunctionType getValueFunctionNameForTransformOperation(TransformOperation::OperationType transformType)
218 {
219     // Use literal strings to avoid link-time dependency on those symbols.
220     switch (transformType) {
221     case TransformOperation::ROTATE_X:
222         return PlatformCAAnimation::RotateX;
223     case TransformOperation::ROTATE_Y:
224         return PlatformCAAnimation::RotateY;
225     case TransformOperation::ROTATE:
226         return PlatformCAAnimation::RotateZ;
227     case TransformOperation::SCALE_X:
228         return PlatformCAAnimation::ScaleX;
229     case TransformOperation::SCALE_Y:
230         return PlatformCAAnimation::ScaleY;
231     case TransformOperation::SCALE_Z:
232         return PlatformCAAnimation::ScaleZ;
233     case TransformOperation::TRANSLATE_X:
234         return PlatformCAAnimation::TranslateX;
235     case TransformOperation::TRANSLATE_Y:
236         return PlatformCAAnimation::TranslateY;
237     case TransformOperation::TRANSLATE_Z:
238         return PlatformCAAnimation::TranslateZ;
239     case TransformOperation::SCALE:
240     case TransformOperation::SCALE_3D:
241         return PlatformCAAnimation::Scale;
242     case TransformOperation::TRANSLATE:
243     case TransformOperation::TRANSLATE_3D:
244         return PlatformCAAnimation::Translate;
245     default:
246         return PlatformCAAnimation::NoValueFunction;
247     }
248 }
249
250 static ASCIILiteral propertyIdToString(AnimatedPropertyID property)
251 {
252     switch (property) {
253     case AnimatedPropertyTransform:
254         return "transform"_s;
255     case AnimatedPropertyOpacity:
256         return "opacity"_s;
257     case AnimatedPropertyBackgroundColor:
258         return "backgroundColor"_s;
259     case AnimatedPropertyFilter:
260         return "filters"_s;
261 #if ENABLE(FILTERS_LEVEL_2)
262     case AnimatedPropertyWebkitBackdropFilter:
263         return "backdropFilters"_s;
264 #endif
265     case AnimatedPropertyInvalid:
266         ASSERT_NOT_REACHED();
267     }
268     ASSERT_NOT_REACHED();
269     return ASCIILiteral::null();
270 }
271
272 static String animationIdentifier(const String& animationName, AnimatedPropertyID property, int index, int subIndex)
273 {
274     return makeString(animationName, '_', static_cast<unsigned>(property), '_', index, '_', subIndex);
275 }
276
277 static bool animationHasStepsTimingFunction(const KeyframeValueList& valueList, const Animation* anim)
278 {
279     if (is<StepsTimingFunction>(anim->timingFunction()))
280         return true;
281     
282     for (unsigned i = 0; i < valueList.size(); ++i) {
283         if (const TimingFunction* timingFunction = valueList.at(i).timingFunction()) {
284             if (is<StepsTimingFunction>(timingFunction))
285                 return true;
286         }
287     }
288
289     return false;
290 }
291
292 static inline bool supportsAcceleratedFilterAnimations()
293 {
294 #if PLATFORM(COCOA)
295     return true;
296 #else
297     return false;
298 #endif
299 }
300
301 bool GraphicsLayer::supportsLayerType(Type type)
302 {
303     switch (type) {
304     case Type::Normal:
305     case Type::PageTiledBacking:
306     case Type::ScrollContainer:
307     case Type::ScrolledContents:
308         return true;
309     case Type::Shape:
310 #if PLATFORM(COCOA)
311         // FIXME: we can use shaper layers on Windows when PlatformCALayerCocoa::setShapePath() etc are implemented.
312         return true;
313 #else
314         return false;
315 #endif
316     }
317     ASSERT_NOT_REACHED();
318     return false;
319 }
320
321 bool GraphicsLayer::supportsBackgroundColorContent()
322 {
323     return true;
324 }
325
326 bool GraphicsLayer::supportsSubpixelAntialiasedLayerText()
327 {
328 #if PLATFORM(MAC)
329     return true;
330 #else
331     return false;
332 #endif
333 }
334
335 Ref<GraphicsLayer> GraphicsLayer::create(GraphicsLayerFactory* factory, GraphicsLayerClient& client, Type layerType)
336 {
337     if (factory) {
338         auto layer = factory->createGraphicsLayer(layerType, client);
339         layer->initialize(layerType);
340         return layer;
341     }
342     
343     auto layer = adoptRef(*new GraphicsLayerCA(layerType, client));
344     layer->initialize(layerType);
345     return WTFMove(layer);
346 }
347
348 bool GraphicsLayerCA::filtersCanBeComposited(const FilterOperations& filters)
349 {
350 #if PLATFORM(COCOA)
351     return PlatformCALayerCocoa::filtersCanBeComposited(filters);
352 #elif PLATFORM(WIN)
353     return PlatformCALayerWin::filtersCanBeComposited(filters);
354 #endif
355 }
356
357 Ref<PlatformCALayer> GraphicsLayerCA::createPlatformCALayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* owner)
358 {
359 #if PLATFORM(COCOA)
360     auto result = PlatformCALayerCocoa::create(layerType, owner);
361     
362     if (result->canHaveBackingStore())
363         result->setWantsDeepColorBackingStore(screenSupportsExtendedColor());
364     
365     return result;
366 #elif PLATFORM(WIN)
367     return PlatformCALayerWin::create(layerType, owner);
368 #endif
369 }
370     
371 Ref<PlatformCALayer> GraphicsLayerCA::createPlatformCALayer(PlatformLayer* platformLayer, PlatformCALayerClient* owner)
372 {
373 #if PLATFORM(COCOA)
374     return PlatformCALayerCocoa::create(platformLayer, owner);
375 #elif PLATFORM(WIN)
376     return PlatformCALayerWin::create(platformLayer, owner);
377 #endif
378 }
379
380 Ref<PlatformCALayer> GraphicsLayerCA::createPlatformCALayerForEmbeddedView(PlatformCALayer::LayerType layerType, GraphicsLayer::EmbeddedViewID, PlatformCALayerClient* owner)
381 {
382     return GraphicsLayerCA::createPlatformCALayer(layerType, owner);
383 }
384
385 Ref<PlatformCAAnimation> GraphicsLayerCA::createPlatformCAAnimation(PlatformCAAnimation::AnimationType type, const String& keyPath)
386 {
387 #if PLATFORM(COCOA)
388     return PlatformCAAnimationCocoa::create(type, keyPath);
389 #elif PLATFORM(WIN)
390     return PlatformCAAnimationWin::create(type, keyPath);
391 #endif
392 }
393
394 typedef HashMap<const GraphicsLayerCA*, std::pair<FloatRect, std::unique_ptr<DisplayList::DisplayList>>> LayerDisplayListHashMap;
395
396 static LayerDisplayListHashMap& layerDisplayListMap()
397 {
398     static NeverDestroyed<LayerDisplayListHashMap> sharedHashMap;
399     return sharedHashMap;
400 }
401
402 GraphicsLayerCA::GraphicsLayerCA(Type layerType, GraphicsLayerClient& client)
403     : GraphicsLayer(layerType, client)
404     , m_needsFullRepaint(false)
405     , m_usingBackdropLayerType(false)
406     , m_allowsBackingStoreDetaching(true)
407     , m_intersectsCoverageRect(false)
408     , m_hasEverPainted(false)
409     , m_hasDescendantsWithRunningTransformAnimations(false)
410     , m_hasDescendantsWithUncommittedChanges(false)
411 {
412 }
413
414 void GraphicsLayerCA::initialize(Type layerType)
415 {
416     PlatformCALayer::LayerType platformLayerType;
417     switch (layerType) {
418     case Type::Normal:
419     case Type::ScrolledContents:
420         platformLayerType = PlatformCALayer::LayerType::LayerTypeWebLayer;
421         break;
422     case Type::PageTiledBacking:
423         platformLayerType = PlatformCALayer::LayerType::LayerTypePageTiledBackingLayer;
424         break;
425     case Type::ScrollContainer:
426         platformLayerType = PlatformCALayer::LayerType::LayerTypeScrollContainerLayer;
427         break;
428     case Type::Shape:
429         platformLayerType = PlatformCALayer::LayerType::LayerTypeShapeLayer;
430         break;
431     }
432     m_layer = createPlatformCALayer(platformLayerType, this);
433     noteLayerPropertyChanged(ContentsScaleChanged);
434 }
435
436 GraphicsLayerCA::~GraphicsLayerCA()
437 {
438     if (UNLIKELY(isTrackingDisplayListReplay()))
439         layerDisplayListMap().remove(this);
440
441     // Do cleanup while we can still safely call methods on the derived class.
442     willBeDestroyed();
443 }
444
445 void GraphicsLayerCA::willBeDestroyed()
446 {
447     // We release our references to the PlatformCALayers here, but do not actively unparent them,
448     // since that will cause a commit and break our batched commit model. The layers will
449     // get released when the rootmost modified GraphicsLayerCA rebuilds its child layers.
450
451     // Clean up the layer.
452     if (m_layer)
453         m_layer->setOwner(nullptr);
454     
455     if (m_contentsLayer)
456         m_contentsLayer->setOwner(nullptr);
457
458     if (m_contentsClippingLayer)
459         m_contentsClippingLayer->setOwner(nullptr);
460
461     if (m_contentsShapeMaskLayer)
462         m_contentsShapeMaskLayer->setOwner(nullptr);
463
464     if (m_shapeMaskLayer)
465         m_shapeMaskLayer->setOwner(nullptr);
466     
467     if (m_structuralLayer)
468         m_structuralLayer->setOwner(nullptr);
469
470     if (m_backdropLayer)
471         m_backdropLayer->setOwner(nullptr);
472
473     if (m_backdropClippingLayer)
474         m_backdropClippingLayer->setOwner(nullptr);
475
476     removeCloneLayers();
477
478     GraphicsLayer::willBeDestroyed();
479 }
480
481 void GraphicsLayerCA::setName(const String& name)
482 {
483 #if ENABLE(TREE_DEBUGGING)
484     String caLayerDescription;
485     if (!m_layer->isPlatformCALayerRemote())
486         caLayerDescription = makeString("CALayer(0x", hex(reinterpret_cast<uintptr_t>(m_layer->platformLayer()), Lowercase), ") ");
487     GraphicsLayer::setName(makeString(caLayerDescription, "GraphicsLayer(0x", hex(reinterpret_cast<uintptr_t>(this), Lowercase), ", ", primaryLayerID(), ") ", name));
488 #else
489     GraphicsLayer::setName(name);
490 #endif
491
492     noteLayerPropertyChanged(NameChanged);
493 }
494
495 GraphicsLayer::PlatformLayerID GraphicsLayerCA::primaryLayerID() const
496 {
497     return primaryLayer()->layerID();
498 }
499
500 PlatformLayer* GraphicsLayerCA::platformLayer() const
501 {
502     return primaryLayer()->platformLayer();
503 }
504
505 bool GraphicsLayerCA::setChildren(Vector<Ref<GraphicsLayer>>&& children)
506 {
507     bool childrenChanged = GraphicsLayer::setChildren(WTFMove(children));
508     if (childrenChanged)
509         noteSublayersChanged();
510     
511     return childrenChanged;
512 }
513
514 void GraphicsLayerCA::addChild(Ref<GraphicsLayer>&& childLayer)
515 {
516     GraphicsLayer::addChild(WTFMove(childLayer));
517     noteSublayersChanged();
518 }
519
520 void GraphicsLayerCA::addChildAtIndex(Ref<GraphicsLayer>&& childLayer, int index)
521 {
522     GraphicsLayer::addChildAtIndex(WTFMove(childLayer), index);
523     noteSublayersChanged();
524 }
525
526 void GraphicsLayerCA::addChildBelow(Ref<GraphicsLayer>&& childLayer, GraphicsLayer* sibling)
527 {
528     GraphicsLayer::addChildBelow(WTFMove(childLayer), sibling);
529     noteSublayersChanged();
530 }
531
532 void GraphicsLayerCA::addChildAbove(Ref<GraphicsLayer>&& childLayer, GraphicsLayer* sibling)
533 {
534     GraphicsLayer::addChildAbove(WTFMove(childLayer), sibling);
535     noteSublayersChanged();
536 }
537
538 bool GraphicsLayerCA::replaceChild(GraphicsLayer* oldChild, Ref<GraphicsLayer>&& newChild)
539 {
540     if (GraphicsLayer::replaceChild(oldChild, WTFMove(newChild))) {
541         noteSublayersChanged();
542         return true;
543     }
544     return false;
545 }
546
547 void GraphicsLayerCA::removeFromParent()
548 {
549     if (m_parent)
550         downcast<GraphicsLayerCA>(*m_parent).noteSublayersChanged();
551     GraphicsLayer::removeFromParent();
552 }
553
554 void GraphicsLayerCA::setMaskLayer(RefPtr<GraphicsLayer>&& layer)
555 {
556     if (layer == m_maskLayer)
557         return;
558
559     GraphicsLayer::setMaskLayer(WTFMove(layer));
560     noteLayerPropertyChanged(MaskLayerChanged);
561
562     propagateLayerChangeToReplicas();
563     
564     if (m_replicatedLayer)
565         downcast<GraphicsLayerCA>(*m_replicatedLayer).propagateLayerChangeToReplicas();
566 }
567
568 void GraphicsLayerCA::setReplicatedLayer(GraphicsLayer* layer)
569 {
570     if (layer == m_replicatedLayer)
571         return;
572
573     GraphicsLayer::setReplicatedLayer(layer);
574     noteLayerPropertyChanged(ReplicatedLayerChanged);
575 }
576
577 void GraphicsLayerCA::setReplicatedByLayer(RefPtr<GraphicsLayer>&& layer)
578 {
579     if (layer == m_replicaLayer)
580         return;
581
582     GraphicsLayer::setReplicatedByLayer(WTFMove(layer));
583     noteSublayersChanged();
584     noteLayerPropertyChanged(ReplicatedLayerChanged);
585 }
586
587 void GraphicsLayerCA::setPosition(const FloatPoint& point)
588 {
589     if (point == m_position)
590         return;
591
592     GraphicsLayer::setPosition(point);
593     noteLayerPropertyChanged(GeometryChanged);
594 }
595
596 void GraphicsLayerCA::syncPosition(const FloatPoint& point)
597 {
598     if (point == m_position)
599         return;
600
601     GraphicsLayer::syncPosition(point);
602     // Ensure future flushes will recompute the coverage rect and update tiling.
603     noteLayerPropertyChanged(NeedsComputeVisibleAndCoverageRect, DontScheduleFlush);
604 }
605
606 void GraphicsLayerCA::setApproximatePosition(const FloatPoint& point)
607 {
608     if (point == m_approximatePosition)
609         return;
610
611     GraphicsLayer::setApproximatePosition(point);
612     // Ensure future flushes will recompute the coverage rect and update tiling.
613     noteLayerPropertyChanged(NeedsComputeVisibleAndCoverageRect, DontScheduleFlush);
614 }
615
616 void GraphicsLayerCA::setAnchorPoint(const FloatPoint3D& point)
617 {
618     if (point == m_anchorPoint)
619         return;
620
621     GraphicsLayer::setAnchorPoint(point);
622     noteLayerPropertyChanged(GeometryChanged);
623 }
624
625 void GraphicsLayerCA::setSize(const FloatSize& size)
626 {
627     if (size == m_size)
628         return;
629
630     GraphicsLayer::setSize(size);
631     noteLayerPropertyChanged(GeometryChanged);
632 }
633
634 void GraphicsLayerCA::setBoundsOrigin(const FloatPoint& origin)
635 {
636     if (origin == m_boundsOrigin)
637         return;
638
639     GraphicsLayer::setBoundsOrigin(origin);
640     noteLayerPropertyChanged(GeometryChanged);
641 }
642
643 void GraphicsLayerCA::syncBoundsOrigin(const FloatPoint& origin)
644 {
645     if (origin == m_boundsOrigin)
646         return;
647
648     GraphicsLayer::syncBoundsOrigin(origin);
649     noteLayerPropertyChanged(NeedsComputeVisibleAndCoverageRect, DontScheduleFlush);
650 }
651
652 void GraphicsLayerCA::setTransform(const TransformationMatrix& t)
653 {
654     if (t == transform())
655         return;
656
657     GraphicsLayer::setTransform(t);
658     noteLayerPropertyChanged(TransformChanged);
659 }
660
661 void GraphicsLayerCA::setChildrenTransform(const TransformationMatrix& t)
662 {
663     if (t == childrenTransform())
664         return;
665
666     GraphicsLayer::setChildrenTransform(t);
667     noteLayerPropertyChanged(ChildrenTransformChanged);
668 }
669
670 void GraphicsLayerCA::moveOrCopyLayerAnimation(MoveOrCopy operation, const String& animationIdentifier, PlatformCALayer *fromLayer, PlatformCALayer *toLayer)
671 {
672     RefPtr<PlatformCAAnimation> anim = fromLayer->animationForKey(animationIdentifier);
673     if (!anim)
674         return;
675
676     switch (operation) {
677     case Move:
678         fromLayer->removeAnimationForKey(animationIdentifier);
679         toLayer->addAnimationForKey(animationIdentifier, *anim);
680         break;
681
682     case Copy:
683         toLayer->addAnimationForKey(animationIdentifier, *anim);
684         break;
685     }
686 }
687
688 void GraphicsLayerCA::moveOrCopyAnimations(MoveOrCopy operation, PlatformCALayer *fromLayer, PlatformCALayer *toLayer)
689 {
690     if (!hasAnimations())
691         return;
692
693     // Look for running animations affecting this property.
694     for (const auto& it : m_animations->runningAnimations) {
695         const auto& propertyAnimations = it.value;
696         size_t numAnimations = propertyAnimations.size();
697         for (size_t i = 0; i < numAnimations; ++i) {
698             const LayerPropertyAnimation& currAnimation = propertyAnimations[i];
699
700             if (currAnimation.m_property == AnimatedPropertyTransform
701                 || currAnimation.m_property == AnimatedPropertyOpacity
702                 || currAnimation.m_property == AnimatedPropertyBackgroundColor
703                 || currAnimation.m_property == AnimatedPropertyFilter)
704                 moveOrCopyLayerAnimation(operation, animationIdentifier(currAnimation.m_name, currAnimation.m_property, currAnimation.m_index, currAnimation.m_subIndex), fromLayer, toLayer);
705         }
706     }
707 }
708
709 void GraphicsLayerCA::setPreserves3D(bool preserves3D)
710 {
711     if (preserves3D == m_preserves3D)
712         return;
713
714     GraphicsLayer::setPreserves3D(preserves3D);
715     noteLayerPropertyChanged(Preserves3DChanged);
716 }
717
718 void GraphicsLayerCA::setMasksToBounds(bool masksToBounds)
719 {
720     if (masksToBounds == m_masksToBounds)
721         return;
722
723     GraphicsLayer::setMasksToBounds(masksToBounds);
724     noteLayerPropertyChanged(MasksToBoundsChanged | DebugIndicatorsChanged);
725 }
726
727 void GraphicsLayerCA::setDrawsContent(bool drawsContent)
728 {
729     if (drawsContent == m_drawsContent)
730         return;
731
732     GraphicsLayer::setDrawsContent(drawsContent);
733     noteLayerPropertyChanged(DrawsContentChanged | DebugIndicatorsChanged);
734 }
735
736 void GraphicsLayerCA::setContentsVisible(bool contentsVisible)
737 {
738     if (contentsVisible == m_contentsVisible)
739         return;
740
741     GraphicsLayer::setContentsVisible(contentsVisible);
742     noteLayerPropertyChanged(ContentsVisibilityChanged);
743     // Visibility affects whether the contentsLayer is parented.
744     if (m_contentsLayer)
745         noteSublayersChanged();
746 }
747
748 void GraphicsLayerCA::setUserInteractionEnabled(bool userInteractionEnabled)
749 {
750     if (userInteractionEnabled == m_userInteractionEnabled)
751         return;
752     
753     GraphicsLayer::setUserInteractionEnabled(userInteractionEnabled);
754     noteLayerPropertyChanged(UserInteractionEnabledChanged);
755 }
756
757 void GraphicsLayerCA::setAcceleratesDrawing(bool acceleratesDrawing)
758 {
759     if (acceleratesDrawing == m_acceleratesDrawing)
760         return;
761
762     GraphicsLayer::setAcceleratesDrawing(acceleratesDrawing);
763     noteLayerPropertyChanged(AcceleratesDrawingChanged);
764 }
765
766 void GraphicsLayerCA::setUsesDisplayListDrawing(bool usesDisplayListDrawing)
767 {
768     if (usesDisplayListDrawing == m_usesDisplayListDrawing)
769         return;
770
771     setNeedsDisplay();
772     GraphicsLayer::setUsesDisplayListDrawing(usesDisplayListDrawing);
773 }
774
775 void GraphicsLayerCA::setBackgroundColor(const Color& color)
776 {
777     if (m_backgroundColor == color)
778         return;
779
780     GraphicsLayer::setBackgroundColor(color);
781     noteLayerPropertyChanged(BackgroundColorChanged);
782 }
783
784 void GraphicsLayerCA::setContentsOpaque(bool opaque)
785 {
786     if (m_contentsOpaque == opaque)
787         return;
788
789     GraphicsLayer::setContentsOpaque(opaque);
790     noteLayerPropertyChanged(ContentsOpaqueChanged);
791 }
792
793 void GraphicsLayerCA::setSupportsSubpixelAntialiasedText(bool supportsSubpixelAntialiasedText)
794 {
795     if (m_supportsSubpixelAntialiasedText == supportsSubpixelAntialiasedText)
796         return;
797
798     GraphicsLayer::setSupportsSubpixelAntialiasedText(supportsSubpixelAntialiasedText);
799     noteLayerPropertyChanged(SupportsSubpixelAntialiasedTextChanged);
800 }
801
802 void GraphicsLayerCA::setBackfaceVisibility(bool visible)
803 {
804     if (m_backfaceVisibility == visible)
805         return;
806     
807     GraphicsLayer::setBackfaceVisibility(visible);
808     noteLayerPropertyChanged(BackfaceVisibilityChanged);
809 }
810
811 void GraphicsLayerCA::setOpacity(float opacity)
812 {
813     float clampedOpacity = std::max(0.0f, std::min(opacity, 1.0f));
814
815     if (clampedOpacity == m_opacity)
816         return;
817
818     GraphicsLayer::setOpacity(clampedOpacity);
819     noteLayerPropertyChanged(OpacityChanged);
820 }
821
822 bool GraphicsLayerCA::setFilters(const FilterOperations& filterOperations)
823 {
824     bool canCompositeFilters = filtersCanBeComposited(filterOperations);
825
826     if (m_filters == filterOperations)
827         return canCompositeFilters;
828
829     // Filters cause flattening, so we should never have filters on a layer with preserves3D().
830     ASSERT(!filterOperations.size() || !preserves3D());
831
832     if (canCompositeFilters) {
833         GraphicsLayer::setFilters(filterOperations);
834         noteLayerPropertyChanged(FiltersChanged);
835     } else if (filters().size()) {
836         // In this case filters are rendered in software, so we need to remove any 
837         // previously attached hardware filters.
838         clearFilters();
839         noteLayerPropertyChanged(FiltersChanged);
840     }
841     return canCompositeFilters;
842 }
843
844 bool GraphicsLayerCA::setBackdropFilters(const FilterOperations& filterOperations)
845 {
846     bool canCompositeFilters = filtersCanBeComposited(filterOperations);
847
848     if (m_backdropFilters == filterOperations)
849         return canCompositeFilters;
850
851     // Filters cause flattening, so we should never have filters on a layer with preserves3D().
852     ASSERT(!filterOperations.size() || !preserves3D());
853
854     if (canCompositeFilters)
855         GraphicsLayer::setBackdropFilters(filterOperations);
856     else {
857         // FIXME: This would clear the backdrop filters if we had a software implementation.
858         clearBackdropFilters();
859     }
860
861     noteLayerPropertyChanged(BackdropFiltersChanged | DebugIndicatorsChanged);
862     return canCompositeFilters;
863 }
864
865 void GraphicsLayerCA::setBackdropFiltersRect(const FloatRoundedRect& backdropFiltersRect)
866 {
867     if (backdropFiltersRect == m_backdropFiltersRect)
868         return;
869
870     GraphicsLayer::setBackdropFiltersRect(backdropFiltersRect);
871     noteLayerPropertyChanged(BackdropFiltersRectChanged);
872 }
873
874 #if ENABLE(CSS_COMPOSITING)
875 void GraphicsLayerCA::setBlendMode(BlendMode blendMode)
876 {
877     if (GraphicsLayer::blendMode() == blendMode)
878         return;
879
880     GraphicsLayer::setBlendMode(blendMode);
881     noteLayerPropertyChanged(BlendModeChanged);
882 }
883 #endif
884
885 bool GraphicsLayerCA::backingStoreAttached() const
886 {
887     return m_layer->backingStoreAttached();
888 }
889
890 bool GraphicsLayerCA::backingStoreAttachedForTesting() const
891 {
892     return m_layer->backingStoreAttached() || m_layer->hasContents();
893 }
894
895 void GraphicsLayerCA::setNeedsDisplay()
896 {
897     if (!drawsContent())
898         return;
899
900     m_needsFullRepaint = true;
901     m_dirtyRects.clear();
902     noteLayerPropertyChanged(DirtyRectsChanged);
903     addRepaintRect(FloatRect(FloatPoint(), m_size));
904 }
905
906 void GraphicsLayerCA::setNeedsDisplayInRect(const FloatRect& r, ShouldClipToLayer shouldClip)
907 {
908     if (!drawsContent())
909         return;
910
911     if (m_needsFullRepaint)
912         return;
913
914     FloatRect rect(r);
915     if (shouldClip == ClipToLayer) {
916         FloatRect layerBounds(FloatPoint(), m_size);
917         rect.intersect(layerBounds);
918     }
919
920     if (rect.isEmpty())
921         return;
922
923     addRepaintRect(rect);
924
925     const size_t maxDirtyRects = 32;
926
927     for (size_t i = 0; i < m_dirtyRects.size(); ++i) {
928         if (m_dirtyRects[i].contains(rect))
929             return;
930     }
931
932     if (m_dirtyRects.size() < maxDirtyRects)
933         m_dirtyRects.append(rect);
934     else
935         m_dirtyRects[0].unite(rect);
936
937     noteLayerPropertyChanged(DirtyRectsChanged);
938 }
939
940 void GraphicsLayerCA::setContentsNeedsDisplay()
941 {
942     noteLayerPropertyChanged(ContentsNeedsDisplay);
943 }
944
945 void GraphicsLayerCA::setContentsRect(const FloatRect& rect)
946 {
947     if (rect == m_contentsRect)
948         return;
949
950     GraphicsLayer::setContentsRect(rect);
951     noteLayerPropertyChanged(ContentsRectsChanged);
952 }
953
954 void GraphicsLayerCA::setContentsClippingRect(const FloatRoundedRect& rect)
955 {
956     if (rect == m_contentsClippingRect)
957         return;
958
959     GraphicsLayer::setContentsClippingRect(rect);
960     noteLayerPropertyChanged(ContentsRectsChanged);
961 }
962
963 bool GraphicsLayerCA::setMasksToBoundsRect(const FloatRoundedRect& roundedRect)
964 {
965     if (roundedRect == m_masksToBoundsRect)
966         return true;
967
968     GraphicsLayer::setMasksToBoundsRect(roundedRect);
969     noteLayerPropertyChanged(MasksToBoundsRectChanged);
970     return true;
971 }
972
973 void GraphicsLayerCA::setShapeLayerPath(const Path& path)
974 {
975     // FIXME: need to check for path equality. No bool Path::operator==(const Path&)!.
976     GraphicsLayer::setShapeLayerPath(path);
977     noteLayerPropertyChanged(ShapeChanged);
978 }
979
980 void GraphicsLayerCA::setShapeLayerWindRule(WindRule windRule)
981 {
982     if (windRule == m_shapeLayerWindRule)
983         return;
984
985     GraphicsLayer::setShapeLayerWindRule(windRule);
986     noteLayerPropertyChanged(WindRuleChanged);
987 }
988
989 void GraphicsLayerCA::setEventRegion(EventRegion&& eventRegion)
990 {
991     if (eventRegion == m_eventRegion)
992         return;
993
994     GraphicsLayer::setEventRegion(WTFMove(eventRegion));
995     noteLayerPropertyChanged(EventRegionChanged, m_isCommittingChanges ? DontScheduleFlush : ScheduleFlush);
996 }
997
998 bool GraphicsLayerCA::shouldRepaintOnSizeChange() const
999 {
1000     return drawsContent() && !tiledBacking();
1001 }
1002
1003 bool GraphicsLayerCA::animationCanBeAccelerated(const KeyframeValueList& valueList, const Animation* anim) const
1004 {
1005     if (!anim || anim->isEmptyOrZeroDuration() || valueList.size() < 2)
1006         return false;
1007
1008     if (animationHasStepsTimingFunction(valueList, anim))
1009         return false;
1010
1011     return true;
1012 }
1013
1014 void GraphicsLayerCA::addProcessingActionForAnimation(const String& animationName, AnimationProcessingAction processingAction)
1015 {
1016     ensureLayerAnimations();
1017
1018     auto& processingActions = m_animations->animationsToProcess.ensure(animationName, [] {
1019         return Vector<AnimationProcessingAction> { };
1020     }).iterator->value;
1021
1022     if (!processingActions.isEmpty() && processingActions.last().action == Remove)
1023         return;
1024
1025     processingActions.append(processingAction);
1026 }
1027
1028 bool GraphicsLayerCA::addAnimation(const KeyframeValueList& valueList, const FloatSize& boxSize, const Animation* anim, const String& animationName, double timeOffset)
1029 {
1030     LOG(Animations, "GraphicsLayerCA %p %llu addAnimation %s (can be accelerated %d)", this, primaryLayerID(), animationName.utf8().data(), animationCanBeAccelerated(valueList, anim));
1031
1032     ASSERT(!animationName.isEmpty());
1033
1034     if (!animationCanBeAccelerated(valueList, anim))
1035         return false;
1036
1037     bool createdAnimations = false;
1038     if (valueList.property() == AnimatedPropertyTransform)
1039         createdAnimations = createTransformAnimationsFromKeyframes(valueList, anim, animationName, Seconds { timeOffset }, boxSize);
1040     else if (valueList.property() == AnimatedPropertyFilter) {
1041         if (supportsAcceleratedFilterAnimations())
1042             createdAnimations = createFilterAnimationsFromKeyframes(valueList, anim, animationName, Seconds { timeOffset });
1043     }
1044 #if ENABLE(FILTERS_LEVEL_2)
1045     else if (valueList.property() == AnimatedPropertyWebkitBackdropFilter) {
1046         if (supportsAcceleratedFilterAnimations())
1047             createdAnimations = createFilterAnimationsFromKeyframes(valueList, anim, animationName, Seconds { timeOffset });
1048     }
1049 #endif
1050     else
1051         createdAnimations = createAnimationFromKeyframes(valueList, anim, animationName, Seconds { timeOffset });
1052
1053     if (createdAnimations)
1054         noteLayerPropertyChanged(AnimationChanged | CoverageRectChanged);
1055
1056     return createdAnimations;
1057 }
1058
1059 void GraphicsLayerCA::pauseAnimation(const String& animationName, double timeOffset)
1060 {
1061     LOG(Animations, "GraphicsLayerCA %p pauseAnimation %s (running %d)", this, animationName.utf8().data(), animationIsRunning(animationName));
1062
1063     // Call add since if there is already a Remove in there, we don't want to overwrite it with a Pause.
1064     addProcessingActionForAnimation(animationName, AnimationProcessingAction { Pause, Seconds { timeOffset } });
1065
1066     noteLayerPropertyChanged(AnimationChanged);
1067 }
1068
1069 void GraphicsLayerCA::seekAnimation(const String& animationName, double timeOffset)
1070 {
1071     LOG(Animations, "GraphicsLayerCA %p seekAnimation %s (running %d)", this, animationName.utf8().data(), animationIsRunning(animationName));
1072
1073     // Call add since if there is already a Remove in there, we don't want to overwrite it with a Pause.
1074     addProcessingActionForAnimation(animationName, AnimationProcessingAction { Seek, Seconds { timeOffset } });
1075
1076     noteLayerPropertyChanged(AnimationChanged);
1077 }
1078
1079 void GraphicsLayerCA::removeAnimation(const String& animationName)
1080 {
1081     LOG(Animations, "GraphicsLayerCA %p removeAnimation %s (running %d)", this, animationName.utf8().data(), animationIsRunning(animationName));
1082
1083     if (!animationIsRunning(animationName))
1084         return;
1085
1086     addProcessingActionForAnimation(animationName, AnimationProcessingAction(Remove));
1087     noteLayerPropertyChanged(AnimationChanged | CoverageRectChanged);
1088 }
1089
1090 void GraphicsLayerCA::platformCALayerAnimationStarted(const String& animationKey, MonotonicTime startTime)
1091 {
1092     LOG(Animations, "GraphicsLayerCA %p platformCALayerAnimationStarted %s at %f", this, animationKey.utf8().data(), startTime.secondsSinceEpoch().seconds());
1093     client().notifyAnimationStarted(this, animationKey, startTime);
1094 }
1095
1096 void GraphicsLayerCA::platformCALayerAnimationEnded(const String& animationKey)
1097 {
1098     LOG(Animations, "GraphicsLayerCA %p platformCALayerAnimationEnded %s", this, animationKey.utf8().data());
1099     client().notifyAnimationEnded(this, animationKey);
1100 }
1101
1102 void GraphicsLayerCA::setContentsToSolidColor(const Color& color)
1103 {
1104     if (color == m_contentsSolidColor)
1105         return;
1106
1107     m_contentsSolidColor = color;
1108     
1109     bool contentsLayerChanged = false;
1110
1111     if (m_contentsSolidColor.isVisible()) {
1112         if (!m_contentsLayer || m_contentsLayerPurpose != ContentsLayerPurpose::BackgroundColor) {
1113             m_contentsLayerPurpose = ContentsLayerPurpose::BackgroundColor;
1114             m_contentsLayer = createPlatformCALayer(PlatformCALayer::LayerTypeLayer, this);
1115 #if ENABLE(TREE_DEBUGGING)
1116             m_contentsLayer->setName(makeString("contents color ", m_contentsLayer->layerID()));
1117 #else
1118             m_contentsLayer->setName("contents color");
1119 #endif
1120             contentsLayerChanged = true;
1121         }
1122     } else {
1123         contentsLayerChanged = m_contentsLayer;
1124         m_contentsLayerPurpose = ContentsLayerPurpose::None;
1125         m_contentsLayer = nullptr;
1126     }
1127
1128     if (contentsLayerChanged)
1129         noteSublayersChanged();
1130
1131     noteLayerPropertyChanged(ContentsColorLayerChanged);
1132 }
1133
1134 void GraphicsLayerCA::setContentsToImage(Image* image)
1135 {
1136     if (image) {
1137         auto newImage = image->nativeImageForCurrentFrame();
1138         if (!newImage)
1139             return;
1140
1141         // FIXME: probably don't need m_uncorrectedContentsImage at all now.
1142         if (m_uncorrectedContentsImage == newImage)
1143             return;
1144         
1145         m_uncorrectedContentsImage = WTFMove(newImage);
1146         m_pendingContentsImage = m_uncorrectedContentsImage;
1147
1148         m_contentsLayerPurpose = ContentsLayerPurpose::Image;
1149         if (!m_contentsLayer)
1150             noteSublayersChanged();
1151     } else {
1152         m_uncorrectedContentsImage = nullptr;
1153         m_pendingContentsImage = nullptr;
1154         m_contentsLayerPurpose = ContentsLayerPurpose::None;
1155         if (m_contentsLayer)
1156             noteSublayersChanged();
1157     }
1158
1159     noteLayerPropertyChanged(ContentsImageChanged);
1160 }
1161
1162 void GraphicsLayerCA::setContentsToEmbeddedView(GraphicsLayer::ContentsLayerEmbeddedViewType layerType, GraphicsLayer::EmbeddedViewID embeddedViewID)
1163 {
1164     bool contentsLayerChanged = false;
1165
1166     if (layerType != GraphicsLayer::ContentsLayerEmbeddedViewType::None) {
1167         m_contentsLayerPurpose = ContentsLayerPurpose::EmbeddedView;
1168
1169         PlatformCALayer::LayerType platformType;
1170         switch (layerType) {
1171         case GraphicsLayer::ContentsLayerEmbeddedViewType::EditableImage:
1172             platformType = PlatformCALayer::LayerTypeEditableImageLayer;
1173             break;
1174         default:
1175             ASSERT_NOT_REACHED();
1176             platformType = PlatformCALayer::LayerTypeLayer;
1177             break;
1178         }
1179
1180         if (!m_contentsLayer || m_contentsLayer->layerType() != platformType || m_contentsLayer->embeddedViewID() != embeddedViewID) {
1181             m_contentsLayer = createPlatformCALayerForEmbeddedView(platformType, embeddedViewID, this);
1182             m_contentsLayer->setAnchorPoint(FloatPoint3D());
1183             contentsLayerChanged = true;
1184         }
1185     } else {
1186         m_contentsLayerPurpose = ContentsLayerPurpose::None;
1187         contentsLayerChanged = m_contentsLayer;
1188         m_contentsLayer = nullptr;
1189     }
1190
1191     if (contentsLayerChanged)
1192         noteSublayersChanged();
1193 }
1194
1195 void GraphicsLayerCA::setContentsToPlatformLayer(PlatformLayer* platformLayer, ContentsLayerPurpose purpose)
1196 {
1197     if (m_contentsLayer && platformLayer == m_contentsLayer->platformLayer())
1198         return;
1199
1200     if (m_contentsClippingLayer && m_contentsLayer)
1201         m_contentsLayer->removeFromSuperlayer();
1202
1203     // FIXME: The passed in layer might be a raw layer or an externally created
1204     // PlatformCALayer. To determine this we attempt to get the
1205     // PlatformCALayer pointer. If this returns a null pointer we assume it's
1206     // raw. This test might be invalid if the raw layer is, for instance, the
1207     // PlatformCALayer is using a user data pointer in the raw layer, and
1208     // the creator of the raw layer is using it for some other purpose.
1209     // For now we don't support such a case.
1210     PlatformCALayer* platformCALayer = PlatformCALayer::platformCALayer(platformLayer);
1211     m_contentsLayer = platformLayer ? (platformCALayer ? platformCALayer : createPlatformCALayer(platformLayer, this).ptr()) : nullptr;
1212     m_contentsLayerPurpose = platformLayer ? purpose : ContentsLayerPurpose::None;
1213
1214     if (m_contentsClippingLayer && m_contentsLayer)
1215         m_contentsClippingLayer->appendSublayer(*m_contentsLayer);
1216
1217     noteSublayersChanged();
1218     noteLayerPropertyChanged(ContentsPlatformLayerChanged);
1219 }
1220
1221 #if PLATFORM(IOS_FAMILY)
1222 PlatformLayer* GraphicsLayerCA::contentsLayerForMedia() const
1223 {
1224     return m_contentsLayerPurpose == ContentsLayerPurpose::Media ? m_contentsLayer->platformLayer() : nullptr;
1225 }
1226 #endif
1227
1228 void GraphicsLayerCA::layerDidDisplay(PlatformCALayer* layer)
1229 {
1230     if (!m_layerClones)
1231         return;
1232
1233     LayerMap* layerCloneMap = nullptr;
1234
1235     if (layer == m_layer)
1236         layerCloneMap = &m_layerClones->primaryLayerClones;
1237     else if (layer == m_contentsLayer)
1238         layerCloneMap = &m_layerClones->contentsLayerClones;
1239
1240     if (layerCloneMap) {
1241         for (auto& platformLayerClone : layerCloneMap->values())
1242             platformLayerClone->copyContentsFromLayer(layer);
1243     }
1244 }
1245
1246 FloatPoint GraphicsLayerCA::computePositionRelativeToBase(float& pageScale) const
1247 {
1248     pageScale = 1;
1249
1250     FloatPoint offset;
1251     for (const GraphicsLayer* currLayer = this; currLayer; currLayer = currLayer->parent()) {
1252         if (currLayer->appliesPageScale()) {
1253             pageScale = currLayer->pageScaleFactor();
1254             return offset;
1255         }
1256
1257         offset += currLayer->position();
1258     }
1259
1260     return FloatPoint();
1261 }
1262
1263 void GraphicsLayerCA::flushCompositingState(const FloatRect& visibleRect)
1264 {
1265     TransformState state(TransformState::UnapplyInverseTransformDirection, FloatQuad(visibleRect));
1266     FloatQuad coverageQuad(visibleRect);
1267     state.setSecondaryQuad(&coverageQuad);
1268
1269     CommitState commitState;
1270     commitState.ancestorHadChanges = visibleRect != m_previousCommittedVisibleRect;
1271     m_previousCommittedVisibleRect = visibleRect;
1272
1273 #if PLATFORM(IOS_FAMILY)
1274     // In WK1, UIKit may be changing layer bounds behind our back in overflow-scroll layers, so disable the optimization.
1275     // See the similar test in computeVisibleAndCoverageRect().
1276     if (m_layer->isPlatformCALayerCocoa())
1277         commitState.ancestorHadChanges = true;
1278 #endif
1279
1280     recursiveCommitChanges(commitState, state);
1281 }
1282
1283 void GraphicsLayerCA::flushCompositingStateForThisLayerOnly()
1284 {
1285     float pageScaleFactor;
1286     bool layerTypeChanged = false;
1287     
1288     CommitState commitState;
1289
1290     FloatPoint offset = computePositionRelativeToBase(pageScaleFactor);
1291     commitLayerChangesBeforeSublayers(commitState, pageScaleFactor, offset, layerTypeChanged);
1292     commitLayerChangesAfterSublayers(commitState);
1293
1294     if (layerTypeChanged)
1295         client().didChangePlatformLayerForLayer(this);
1296 }
1297
1298 static inline bool accumulatesTransform(const GraphicsLayerCA& layer)
1299 {
1300     return !layer.masksToBounds() && (layer.preserves3D() || (layer.parent() && layer.parent()->preserves3D()));
1301 }
1302
1303 bool GraphicsLayerCA::recursiveVisibleRectChangeRequiresFlush(const CommitState& commitState, const TransformState& state) const
1304 {
1305     TransformState localState = state;
1306     CommitState childCommitState = commitState;
1307
1308     // This may be called at times when layout has not been updated, so we want to avoid calling out to the client
1309     // for animating transforms.
1310     VisibleAndCoverageRects rects = computeVisibleAndCoverageRect(localState, accumulatesTransform(*this), 0);
1311     adjustCoverageRect(rects, m_visibleRect);
1312
1313     auto bounds = FloatRect(m_boundsOrigin, size());
1314
1315     bool intersectsCoverageRect = rects.coverageRect.intersects(bounds);
1316     if (intersectsCoverageRect != m_intersectsCoverageRect)
1317         return true;
1318
1319     if (rects.coverageRect != m_coverageRect) {
1320         if (TiledBacking* tiledBacking = this->tiledBacking()) {
1321             if (tiledBacking->tilesWouldChangeForCoverageRect(rects.coverageRect))
1322                 return true;
1323         }
1324     }
1325
1326     if (m_maskLayer) {
1327         auto& maskLayerCA = downcast<GraphicsLayerCA>(*m_maskLayer);
1328         if (maskLayerCA.recursiveVisibleRectChangeRequiresFlush(childCommitState, localState))
1329             return true;
1330     }
1331
1332     for (const auto& layer : children()) {
1333         const auto& currentChild = downcast<GraphicsLayerCA>(layer.get());
1334         if (currentChild.recursiveVisibleRectChangeRequiresFlush(childCommitState, localState))
1335             return true;
1336     }
1337
1338     if (m_replicaLayer)
1339         if (downcast<GraphicsLayerCA>(*m_replicaLayer).recursiveVisibleRectChangeRequiresFlush(childCommitState, localState))
1340             return true;
1341     
1342     return false;
1343 }
1344
1345 bool GraphicsLayerCA::visibleRectChangeRequiresFlush(const FloatRect& clipRect) const
1346 {
1347     TransformState state(TransformState::UnapplyInverseTransformDirection, FloatQuad(clipRect));
1348     CommitState commitState;
1349     return recursiveVisibleRectChangeRequiresFlush(commitState, state);
1350 }
1351
1352 TiledBacking* GraphicsLayerCA::tiledBacking() const
1353 {
1354     return m_layer->tiledBacking();
1355 }
1356
1357 TransformationMatrix GraphicsLayerCA::layerTransform(const FloatPoint& position, const TransformationMatrix* customTransform) const
1358 {
1359     TransformationMatrix transform;
1360     transform.translate(position.x(), position.y());
1361
1362     TransformationMatrix currentTransform;
1363     if (customTransform)
1364         currentTransform = *customTransform;
1365     else if (m_transform)
1366         currentTransform = *m_transform;
1367     
1368     if (!currentTransform.isIdentity()) {
1369         FloatPoint3D absoluteAnchorPoint(anchorPoint());
1370         absoluteAnchorPoint.scale(size().width(), size().height(), 1);
1371         transform.translate3d(absoluteAnchorPoint.x(), absoluteAnchorPoint.y(), absoluteAnchorPoint.z());
1372         transform.multiply(currentTransform);
1373         transform.translate3d(-absoluteAnchorPoint.x(), -absoluteAnchorPoint.y(), -absoluteAnchorPoint.z());
1374     }
1375
1376     if (GraphicsLayer* parentLayer = parent()) {
1377         if (parentLayer->hasNonIdentityChildrenTransform()) {
1378             FloatPoint3D parentAnchorPoint(parentLayer->anchorPoint());
1379             parentAnchorPoint.scale(parentLayer->size().width(), parentLayer->size().height(), 1);
1380
1381             transform.translateRight3d(-parentAnchorPoint.x(), -parentAnchorPoint.y(), -parentAnchorPoint.z());
1382             transform = parentLayer->childrenTransform() * transform;
1383             transform.translateRight3d(parentAnchorPoint.x(), parentAnchorPoint.y(), parentAnchorPoint.z());
1384         }
1385     }
1386     
1387     return transform;
1388 }
1389
1390 GraphicsLayerCA::VisibleAndCoverageRects GraphicsLayerCA::computeVisibleAndCoverageRect(TransformState& state, bool preserves3D, ComputeVisibleRectFlags flags) const
1391 {
1392     FloatPoint position = approximatePosition();
1393     client().customPositionForVisibleRectComputation(this, position);
1394
1395     TransformationMatrix layerTransform;
1396     TransformationMatrix currentTransform;
1397     if ((flags & RespectAnimatingTransforms) && client().getCurrentTransform(this, currentTransform))
1398         layerTransform = this->layerTransform(position, &currentTransform);
1399     else
1400         layerTransform = this->layerTransform(position);
1401
1402     bool applyWasClamped;
1403     TransformState::TransformAccumulation accumulation = preserves3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform;
1404     state.applyTransform(layerTransform, accumulation, &applyWasClamped);
1405
1406     bool mapWasClamped;
1407     FloatRect clipRectForChildren = state.mappedQuad(&mapWasClamped).boundingBox();
1408     FloatPoint boundsOrigin = m_boundsOrigin;
1409 #if PLATFORM(IOS_FAMILY)
1410     // In WK1, UIKit may be changing layer bounds behind our back in overflow-scroll layers, so use the layer's origin.
1411     if (m_layer->isPlatformCALayerCocoa())
1412         boundsOrigin = m_layer->bounds().location();
1413 #endif
1414     clipRectForChildren.move(boundsOrigin.x(), boundsOrigin.y());
1415     
1416     FloatRect clipRectForSelf(boundsOrigin, m_size);
1417     if (!applyWasClamped && !mapWasClamped)
1418         clipRectForSelf.intersect(clipRectForChildren);
1419
1420     if (masksToBounds()) {
1421         ASSERT(accumulation == TransformState::FlattenTransform);
1422         // Flatten, and replace the quad in the TransformState with one that is clipped to this layer's bounds.
1423         state.flatten();
1424         state.setQuad(clipRectForSelf);
1425         if (state.isMappingSecondaryQuad()) {
1426             FloatQuad secondaryQuad(clipRectForSelf);
1427             state.setSecondaryQuad(&secondaryQuad);
1428         }
1429     }
1430
1431     FloatRect coverageRect = clipRectForSelf;
1432     Optional<FloatQuad> quad = state.mappedSecondaryQuad(&mapWasClamped);
1433     if (quad && !mapWasClamped && !applyWasClamped)
1434         coverageRect = (*quad).boundingBox();
1435
1436     return { clipRectForSelf, coverageRect, currentTransform };
1437 }
1438
1439 bool GraphicsLayerCA::adjustCoverageRect(VisibleAndCoverageRects& rects, const FloatRect& oldVisibleRect) const
1440 {
1441     FloatRect coverageRect = rects.coverageRect;
1442
1443     switch (type()) {
1444     case Type::PageTiledBacking:
1445         coverageRect = tiledBacking()->adjustTileCoverageRectForScrolling(coverageRect, size(), oldVisibleRect, rects.visibleRect, pageScaleFactor() * deviceScaleFactor());
1446         break;
1447     case Type::ScrolledContents:
1448         if (m_layer->usesTiledBackingLayer())
1449             coverageRect = tiledBacking()->adjustTileCoverageRectForScrolling(coverageRect, size(), oldVisibleRect, rects.visibleRect, pageScaleFactor() * deviceScaleFactor());
1450         else {
1451             // Even if we don't have tiled backing, we want to expand coverage so that contained layers get attached backing store.
1452             coverageRect = adjustCoverageRectForMovement(coverageRect, oldVisibleRect, rects.visibleRect);
1453         }
1454         break;
1455     case Type::Normal:
1456         if (m_layer->usesTiledBackingLayer())
1457             coverageRect = tiledBacking()->adjustTileCoverageRect(coverageRect, oldVisibleRect, rects.visibleRect, size() != m_sizeAtLastCoverageRectUpdate);
1458         break;
1459     default:
1460         break;
1461     }
1462     
1463     if (rects.coverageRect == coverageRect)
1464         return false;
1465
1466     rects.coverageRect = coverageRect;
1467     return true;
1468 }
1469
1470 void GraphicsLayerCA::setVisibleAndCoverageRects(const VisibleAndCoverageRects& rects)
1471 {
1472     bool visibleRectChanged = rects.visibleRect != m_visibleRect;
1473     bool coverageRectChanged = rects.coverageRect != m_coverageRect;
1474     if (!visibleRectChanged && !coverageRectChanged && !animationExtent())
1475         return;
1476
1477     auto bounds = FloatRect(m_boundsOrigin, size());
1478     if (auto extent = animationExtent()) {
1479         // Adjust the animation extent to match the current animation position.
1480         bounds = rects.animatingTransform.inverse().valueOr(TransformationMatrix()).mapRect(*extent);
1481     }
1482
1483     // FIXME: we need to take reflections into account when determining whether this layer intersects the coverage rect.
1484     bool intersectsCoverageRect = rects.coverageRect.intersects(bounds);
1485     if (intersectsCoverageRect != m_intersectsCoverageRect) {
1486         addUncommittedChanges(CoverageRectChanged);
1487         m_intersectsCoverageRect = intersectsCoverageRect;
1488     }
1489
1490     if (visibleRectChanged) {
1491         addUncommittedChanges(CoverageRectChanged);
1492         m_visibleRect = rects.visibleRect;
1493     }
1494
1495     if (coverageRectChanged) {
1496         addUncommittedChanges(CoverageRectChanged);
1497         m_coverageRect = rects.coverageRect;
1498     }
1499 }
1500
1501 bool GraphicsLayerCA::needsCommit(const CommitState& commitState)
1502 {
1503     if (commitState.ancestorHadChanges)
1504         return true;
1505     if (m_uncommittedChanges)
1506         return true;
1507     if (hasDescendantsWithUncommittedChanges())
1508         return true;
1509     // Accelerated transforms move the underlying layers without GraphicsLayers getting invalidated.
1510     if (isRunningTransformAnimation())
1511         return true;
1512     if (hasDescendantsWithRunningTransformAnimations())
1513         return true;
1514
1515     return false;
1516 }
1517
1518 // rootRelativeTransformForScaling is a transform from the root, but for layers with transform animations, it cherry-picked the state of the
1519 // animation that contributes maximally to the scale (on every layer with animations down the hierarchy).
1520 void GraphicsLayerCA::recursiveCommitChanges(CommitState& commitState, const TransformState& state, float pageScaleFactor, const FloatPoint& positionRelativeToBase, bool affectedByPageScale)
1521 {
1522     if (!needsCommit(commitState))
1523         return;
1524
1525     TransformState localState = state;
1526     CommitState childCommitState = commitState;
1527
1528     bool affectedByTransformAnimation = commitState.ancestorHasTransformAnimation;
1529
1530     bool accumulateTransform = accumulatesTransform(*this);
1531     VisibleAndCoverageRects rects = computeVisibleAndCoverageRect(localState, accumulateTransform);
1532     if (adjustCoverageRect(rects, m_visibleRect)) {
1533         if (state.isMappingSecondaryQuad()) {
1534             FloatQuad secondaryQuad(rects.coverageRect);
1535             localState.setLastPlanarSecondaryQuad(&secondaryQuad);
1536         }
1537     }
1538     setVisibleAndCoverageRects(rects);
1539
1540     if (commitState.ancestorStartedOrEndedTransformAnimation)
1541         addUncommittedChanges(CoverageRectChanged);
1542
1543 #ifdef VISIBLE_TILE_WASH
1544     // Use having a transform as a key to making the tile wash layer. If every layer gets a wash,
1545     // they start to obscure useful information.
1546     if ((!m_transform.isIdentity() || tiledBacking()) && !m_visibleTileWashLayer) {
1547         static NeverDestroyed<Color> washFillColor(255, 0, 0, 50);
1548         static NeverDestroyed<Color> washBorderColor(255, 0, 0, 100);
1549         
1550         m_visibleTileWashLayer = createPlatformCALayer(PlatformCALayer::LayerTypeLayer, this);
1551         String name = makeString("Visible Tile Wash Layer 0x", hex(reinterpret_cast<uintptr_t>(m_visibleTileWashLayer->platformLayer()), Lowercase));
1552         m_visibleTileWashLayer->setName(name);
1553         m_visibleTileWashLayer->setAnchorPoint(FloatPoint3D(0, 0, 0));
1554         m_visibleTileWashLayer->setBorderColor(washBorderColor);
1555         m_visibleTileWashLayer->setBorderWidth(8);
1556         m_visibleTileWashLayer->setBackgroundColor(washFillColor);
1557         noteSublayersChanged(DontScheduleFlush);
1558     }
1559
1560     if (m_visibleTileWashLayer) {
1561         m_visibleTileWashLayer->setPosition(m_visibleRect.location());
1562         m_visibleTileWashLayer->setBounds(FloatRect(FloatPoint(), m_visibleRect.size()));
1563     }
1564 #endif
1565
1566     bool hadChanges = m_uncommittedChanges;
1567
1568     // FIXME: This could be more fine-grained. Only some types of changes have impact on sublayers.
1569     if (!childCommitState.ancestorHadChanges)
1570         childCommitState.ancestorHadChanges = hadChanges;
1571
1572     if (appliesPageScale()) {
1573         pageScaleFactor = this->pageScaleFactor();
1574         affectedByPageScale = true;
1575     }
1576
1577     // Accumulate an offset from the ancestral pixel-aligned layer.
1578     FloatPoint baseRelativePosition = positionRelativeToBase;
1579     if (affectedByPageScale)
1580         baseRelativePosition += m_position;
1581
1582     bool wasRunningTransformAnimation = isRunningTransformAnimation();
1583     bool layerTypeChanged = false;
1584     
1585     commitLayerChangesBeforeSublayers(childCommitState, pageScaleFactor, baseRelativePosition, layerTypeChanged);
1586
1587     bool nowRunningTransformAnimation = wasRunningTransformAnimation;
1588     if (m_uncommittedChanges & AnimationChanged)
1589         nowRunningTransformAnimation = isRunningTransformAnimation();
1590
1591     if (wasRunningTransformAnimation != nowRunningTransformAnimation)
1592         childCommitState.ancestorStartedOrEndedTransformAnimation = true;
1593
1594     if (nowRunningTransformAnimation) {
1595         childCommitState.ancestorHasTransformAnimation = true;
1596         if (m_intersectsCoverageRect || !animationExtent())
1597             childCommitState.ancestorWithTransformAnimationIntersectsCoverageRect = true;
1598         affectedByTransformAnimation = true;
1599     }
1600     
1601     if (GraphicsLayerCA* maskLayer = downcast<GraphicsLayerCA>(m_maskLayer.get())) {
1602         maskLayer->setVisibleAndCoverageRects(rects);
1603         maskLayer->commitLayerChangesBeforeSublayers(childCommitState, pageScaleFactor, baseRelativePosition, layerTypeChanged);
1604     }
1605
1606     bool hasDescendantsWithRunningTransformAnimations = false;
1607     
1608     for (auto& layer : children()) {
1609         auto& currentChild = downcast<GraphicsLayerCA>(layer.get());
1610         currentChild.recursiveCommitChanges(childCommitState, localState, pageScaleFactor, baseRelativePosition, affectedByPageScale);
1611
1612         if (currentChild.isRunningTransformAnimation() || currentChild.hasDescendantsWithRunningTransformAnimations())
1613             hasDescendantsWithRunningTransformAnimations = true;
1614     }
1615
1616     commitState.totalBackdropFilterArea = childCommitState.totalBackdropFilterArea;
1617
1618     if (GraphicsLayerCA* replicaLayer = downcast<GraphicsLayerCA>(m_replicaLayer.get()))
1619         replicaLayer->recursiveCommitChanges(childCommitState, localState, pageScaleFactor, baseRelativePosition, affectedByPageScale);
1620
1621     if (GraphicsLayerCA* maskLayer = downcast<GraphicsLayerCA>(m_maskLayer.get()))
1622         maskLayer->commitLayerChangesAfterSublayers(childCommitState);
1623
1624     setHasDescendantsWithUncommittedChanges(false);
1625     setHasDescendantsWithRunningTransformAnimations(hasDescendantsWithRunningTransformAnimations);
1626
1627     bool hadDirtyRects = m_uncommittedChanges & DirtyRectsChanged;
1628     commitLayerChangesAfterSublayers(childCommitState);
1629
1630     if (affectedByTransformAnimation && m_layer->layerType() == PlatformCALayer::LayerTypeTiledBackingLayer)
1631         client().notifyFlushBeforeDisplayRefresh(this);
1632
1633     if (layerTypeChanged)
1634         client().didChangePlatformLayerForLayer(this);
1635
1636     if (usesDisplayListDrawing() && m_drawsContent && (!m_hasEverPainted || hadDirtyRects)) {
1637         TraceScope tracingScope(DisplayListRecordStart, DisplayListRecordEnd);
1638
1639         m_displayList = makeUnique<DisplayList::DisplayList>();
1640         
1641         FloatRect initialClip(boundsOrigin(), size());
1642
1643         GraphicsContext context([&](GraphicsContext& context) {
1644             return makeUnique<DisplayList::Recorder>(context, *m_displayList, GraphicsContextState(), initialClip, AffineTransform());
1645         });
1646         paintGraphicsLayerContents(context, FloatRect(FloatPoint(), size()));
1647     }
1648 }
1649
1650 void GraphicsLayerCA::platformCALayerCustomSublayersChanged(PlatformCALayer*)
1651 {
1652     noteLayerPropertyChanged(ChildrenChanged, m_isCommittingChanges ? DontScheduleFlush : ScheduleFlush);
1653 }
1654
1655 bool GraphicsLayerCA::platformCALayerShowRepaintCounter(PlatformCALayer* platformLayer) const
1656 {
1657     // The repaint counters are painted into the TileController tiles (which have no corresponding platform layer),
1658     // so we don't want to overpaint the repaint counter when called with the TileController's own layer.
1659     if (isPageTiledBackingLayer() && platformLayer)
1660         return false;
1661
1662     return isShowingRepaintCounter();
1663 }
1664
1665 void GraphicsLayerCA::platformCALayerPaintContents(PlatformCALayer*, GraphicsContext& context, const FloatRect& clip, GraphicsLayerPaintBehavior layerPaintBehavior)
1666 {
1667     m_hasEverPainted = true;
1668     if (m_displayList) {
1669         DisplayList::Replayer replayer(context, *m_displayList);
1670         
1671         if (UNLIKELY(isTrackingDisplayListReplay())) {
1672             auto replayList = replayer.replay(clip, isTrackingDisplayListReplay());
1673             layerDisplayListMap().add(this, std::pair<FloatRect, std::unique_ptr<DisplayList::DisplayList>>(clip, WTFMove(replayList)));
1674         } else
1675             replayer.replay(clip);
1676
1677         return;
1678     }
1679
1680     TraceScope tracingScope(PaintLayerStart, PaintLayerEnd);
1681     paintGraphicsLayerContents(context, clip, layerPaintBehavior);
1682 }
1683
1684 void GraphicsLayerCA::platformCALayerSetNeedsToRevalidateTiles()
1685 {
1686     noteLayerPropertyChanged(TilingAreaChanged, m_isCommittingChanges ? DontScheduleFlush : ScheduleFlush);
1687 }
1688
1689 float GraphicsLayerCA::platformCALayerDeviceScaleFactor() const
1690 {
1691     return deviceScaleFactor();
1692 }
1693
1694 float GraphicsLayerCA::platformCALayerContentsScaleMultiplierForNewTiles(PlatformCALayer*) const
1695 {
1696     return client().contentsScaleMultiplierForNewTiles(this);
1697 }
1698
1699 bool GraphicsLayerCA::platformCALayerShouldAggressivelyRetainTiles(PlatformCALayer*) const
1700 {
1701     return client().shouldAggressivelyRetainTiles(this);
1702 }
1703
1704 bool GraphicsLayerCA::platformCALayerShouldTemporarilyRetainTileCohorts(PlatformCALayer*) const
1705 {
1706     return client().shouldTemporarilyRetainTileCohorts(this);
1707 }
1708
1709 bool GraphicsLayerCA::platformCALayerUseGiantTiles() const
1710 {
1711     return client().useGiantTiles();
1712 }
1713
1714 void GraphicsLayerCA::platformCALayerLogFilledVisibleFreshTile(unsigned blankPixelCount)
1715 {
1716     client().logFilledVisibleFreshTile(blankPixelCount);
1717 }
1718
1719 static PlatformCALayer::LayerType layerTypeForCustomBackdropAppearance(GraphicsLayer::CustomAppearance appearance)
1720 {
1721     return appearance == GraphicsLayer::CustomAppearance::LightBackdrop ? PlatformCALayer::LayerTypeLightSystemBackdropLayer : PlatformCALayer::LayerTypeDarkSystemBackdropLayer;
1722 }
1723
1724 static bool isCustomBackdropLayerType(PlatformCALayer::LayerType layerType)
1725 {
1726     return layerType == PlatformCALayer::LayerTypeLightSystemBackdropLayer || layerType == PlatformCALayer::LayerTypeDarkSystemBackdropLayer;
1727 }
1728
1729 void GraphicsLayerCA::commitLayerChangesBeforeSublayers(CommitState& commitState, float pageScaleFactor, const FloatPoint& positionRelativeToBase, bool& layerChanged)
1730 {
1731     SetForScope<bool> committingChangesChange(m_isCommittingChanges, true);
1732
1733     ++commitState.treeDepth;
1734     if (m_structuralLayer)
1735         ++commitState.treeDepth;
1736
1737     if (!m_uncommittedChanges) {
1738         // Ensure that we cap layer depth in commitLayerChangesAfterSublayers().
1739         if (commitState.treeDepth > cMaxLayerTreeDepth)
1740             addUncommittedChanges(ChildrenChanged);
1741     }
1742
1743     bool needTiledLayer = requiresTiledLayer(pageScaleFactor);
1744     bool needBackdropLayerType = (customAppearance() == CustomAppearance::LightBackdrop || customAppearance() == CustomAppearance::DarkBackdrop);
1745
1746     PlatformCALayer::LayerType currentLayerType = m_layer->layerType();
1747     PlatformCALayer::LayerType neededLayerType = currentLayerType;
1748
1749     if (needBackdropLayerType)
1750         neededLayerType = layerTypeForCustomBackdropAppearance(customAppearance());
1751     else if (needTiledLayer)
1752         neededLayerType = PlatformCALayer::LayerTypeTiledBackingLayer;
1753     else if (currentLayerType == PlatformCALayer::LayerTypeTiledBackingLayer || isCustomBackdropLayerType(m_layer->layerType()))
1754         neededLayerType = PlatformCALayer::LayerTypeWebLayer;
1755
1756     if (neededLayerType != m_layer->layerType()) {
1757         changeLayerTypeTo(neededLayerType);
1758         layerChanged = true;
1759     }
1760
1761     // Need to handle Preserves3DChanged first, because it affects which layers subsequent properties are applied to
1762     if (m_uncommittedChanges & (Preserves3DChanged | ReplicatedLayerChanged | BackdropFiltersChanged)) {
1763         if (updateStructuralLayer())
1764             layerChanged = true;
1765     }
1766
1767     if (m_uncommittedChanges & GeometryChanged)
1768         updateGeometry(pageScaleFactor, positionRelativeToBase);
1769
1770     if (m_uncommittedChanges & DrawsContentChanged)
1771         updateDrawsContent();
1772
1773     if (m_uncommittedChanges & NameChanged)
1774         updateNames();
1775
1776     if (m_uncommittedChanges & ContentsImageChanged) // Needs to happen before ChildrenChanged
1777         updateContentsImage();
1778         
1779     if (m_uncommittedChanges & ContentsPlatformLayerChanged) // Needs to happen before ChildrenChanged
1780         updateContentsPlatformLayer();
1781
1782     if (m_uncommittedChanges & ContentsColorLayerChanged) // Needs to happen before ChildrenChanged
1783         updateContentsColorLayer();
1784     
1785     if (m_uncommittedChanges & BackgroundColorChanged)
1786         updateBackgroundColor();
1787
1788     if (m_uncommittedChanges & TransformChanged)
1789         updateTransform();
1790
1791     if (m_uncommittedChanges & ChildrenTransformChanged)
1792         updateChildrenTransform();
1793     
1794     if (m_uncommittedChanges & MasksToBoundsChanged)
1795         updateMasksToBounds();
1796     
1797     if (m_uncommittedChanges & ContentsVisibilityChanged)
1798         updateContentsVisibility();
1799
1800     if (m_uncommittedChanges & UserInteractionEnabledChanged)
1801         updateUserInteractionEnabled();
1802
1803     // Note that contentsScale can affect whether the layer can be opaque.
1804     if (m_uncommittedChanges & ContentsOpaqueChanged)
1805         updateContentsOpaque(pageScaleFactor);
1806
1807     if (m_uncommittedChanges & BackfaceVisibilityChanged)
1808         updateBackfaceVisibility();
1809
1810     if (m_uncommittedChanges & OpacityChanged)
1811         updateOpacityOnLayer();
1812
1813     if (m_uncommittedChanges & FiltersChanged)
1814         updateFilters();
1815
1816     // If there are backdrop filters, we need to always check the resource usage
1817     // because something up the tree may have changed its usage.
1818     if (m_uncommittedChanges & BackdropFiltersChanged || needsBackdrop())
1819         updateBackdropFilters(commitState);
1820
1821     if (m_uncommittedChanges & BackdropFiltersRectChanged)
1822         updateBackdropFiltersRect();
1823
1824 #if ENABLE(CSS_COMPOSITING)
1825     if (m_uncommittedChanges & BlendModeChanged)
1826         updateBlendMode();
1827 #endif
1828
1829     if (m_uncommittedChanges & ShapeChanged)
1830         updateShape();
1831
1832     if (m_uncommittedChanges & WindRuleChanged)
1833         updateWindRule();
1834
1835     if (m_uncommittedChanges & AnimationChanged)
1836         updateAnimations();
1837
1838     // Updating the contents scale can cause parts of the layer to be invalidated,
1839     // so make sure to update the contents scale before updating the dirty rects.
1840     if (m_uncommittedChanges & ContentsScaleChanged)
1841         updateContentsScale(pageScaleFactor);
1842
1843     if (m_uncommittedChanges & CoverageRectChanged)
1844         updateCoverage(commitState);
1845
1846     if (m_uncommittedChanges & AcceleratesDrawingChanged) // Needs to happen before TilingAreaChanged.
1847         updateAcceleratesDrawing();
1848
1849     if (m_uncommittedChanges & TilingAreaChanged) // Needs to happen after CoverageRectChanged, ContentsScaleChanged
1850         updateTiles();
1851
1852     if (m_uncommittedChanges & DirtyRectsChanged)
1853         repaintLayerDirtyRects();
1854     
1855     if (m_uncommittedChanges & ContentsRectsChanged) // Needs to happen before ChildrenChanged
1856         updateContentsRects();
1857
1858     if (m_uncommittedChanges & MasksToBoundsRectChanged) // Needs to happen before ChildrenChanged
1859         updateMasksToBoundsRect();
1860
1861     if (m_uncommittedChanges & EventRegionChanged)
1862         updateEventRegion();
1863
1864     if (m_uncommittedChanges & MaskLayerChanged) {
1865         updateMaskLayer();
1866         // If the mask layer becomes tiled it can set this flag again. Clear the flag so that
1867         // commitLayerChangesAfterSublayers doesn't update the mask again in the normal case.
1868         m_uncommittedChanges &= ~MaskLayerChanged;
1869     }
1870
1871     if (m_uncommittedChanges & ContentsNeedsDisplay)
1872         updateContentsNeedsDisplay();
1873
1874     if (m_uncommittedChanges & SupportsSubpixelAntialiasedTextChanged)
1875         updateSupportsSubpixelAntialiasedText();
1876
1877     if (m_uncommittedChanges & DebugIndicatorsChanged)
1878         updateDebugIndicators();
1879
1880     if (m_uncommittedChanges & CustomAppearanceChanged)
1881         updateCustomAppearance();
1882
1883     if (m_uncommittedChanges & ChildrenChanged) {
1884         updateSublayerList();
1885         // Sublayers may set this flag again, so clear it to avoid always updating sublayers in commitLayerChangesAfterSublayers().
1886         m_uncommittedChanges &= ~ChildrenChanged;
1887     }
1888
1889     // Ensure that we cap layer depth in commitLayerChangesAfterSublayers().
1890     if (commitState.treeDepth > cMaxLayerTreeDepth)
1891         addUncommittedChanges(ChildrenChanged);
1892 }
1893
1894 void GraphicsLayerCA::commitLayerChangesAfterSublayers(CommitState& commitState)
1895 {
1896     if (!m_uncommittedChanges)
1897         return;
1898
1899     SetForScope<bool> committingChangesChange(m_isCommittingChanges, true);
1900
1901     if (m_uncommittedChanges & MaskLayerChanged)
1902         updateMaskLayer();
1903
1904     if (m_uncommittedChanges & ChildrenChanged)
1905         updateSublayerList(commitState.treeDepth > cMaxLayerTreeDepth);
1906
1907     if (m_uncommittedChanges & ReplicatedLayerChanged)
1908         updateReplicatedLayers();
1909
1910     m_uncommittedChanges = NoChange;
1911 }
1912
1913 void GraphicsLayerCA::updateNames()
1914 {
1915     switch (structuralLayerPurpose()) {
1916     case StructuralLayerForPreserves3D:
1917         m_structuralLayer->setName("preserve-3d: " + name());
1918         break;
1919     case StructuralLayerForReplicaFlattening:
1920         m_structuralLayer->setName("replica flattening: " + name());
1921         break;
1922     case StructuralLayerForBackdrop:
1923         m_structuralLayer->setName("backdrop hosting: " + name());
1924         break;
1925     case NoStructuralLayer:
1926         break;
1927     }
1928     m_layer->setName(name());
1929 }
1930
1931 void GraphicsLayerCA::updateSublayerList(bool maxLayerDepthReached)
1932 {
1933     if (maxLayerDepthReached) {
1934         m_layer->setSublayers(PlatformCALayerList());
1935         return;
1936     }
1937     
1938     const PlatformCALayerList* customSublayers = m_layer->customSublayers();
1939
1940     PlatformCALayerList structuralLayerChildren;
1941     PlatformCALayerList primaryLayerChildren;
1942
1943     PlatformCALayerList& childListForSublayers = m_structuralLayer ? structuralLayerChildren : primaryLayerChildren;
1944
1945     if (customSublayers)
1946         primaryLayerChildren.appendVector(*customSublayers);
1947
1948     if (m_structuralLayer) {
1949         if (m_backdropLayer)
1950             structuralLayerChildren.append(m_backdropLayer);
1951
1952         if (m_replicaLayer)
1953             structuralLayerChildren.append(downcast<GraphicsLayerCA>(*m_replicaLayer).primaryLayer());
1954     
1955         structuralLayerChildren.append(m_layer);
1956     }
1957
1958     if (m_contentsLayer && m_contentsVisible) {
1959         // FIXME: add the contents layer in the correct order with negative z-order children.
1960         // This does not cause visible rendering issues because currently contents layers are only used
1961         // for replaced elements that don't have children.
1962         primaryLayerChildren.append(m_contentsClippingLayer ? m_contentsClippingLayer : m_contentsLayer);
1963     }
1964     
1965     for (const auto& layer : children()) {
1966         const auto& currentChild = downcast<GraphicsLayerCA>(layer.get());
1967         PlatformCALayer* childLayer = currentChild.layerForSuperlayer();
1968         childListForSublayers.append(childLayer);
1969     }
1970
1971 #ifdef VISIBLE_TILE_WASH
1972     if (m_visibleTileWashLayer)
1973         childListForSublayers.append(m_visibleTileWashLayer);
1974 #endif
1975
1976     if (m_structuralLayer)
1977         m_structuralLayer->setSublayers(structuralLayerChildren);
1978     
1979     m_layer->setSublayers(primaryLayerChildren);
1980 }
1981
1982 void GraphicsLayerCA::updateGeometry(float pageScaleFactor, const FloatPoint& positionRelativeToBase)
1983 {
1984     FloatPoint scaledPosition = m_position;
1985     FloatPoint3D scaledAnchorPoint = m_anchorPoint;
1986     FloatSize scaledSize = m_size;
1987     FloatSize pixelAlignmentOffset;
1988
1989     // FIXME: figure out if we really need to pixel align the graphics layer here.
1990     if (client().needsPixelAligment() && !WTF::isIntegral(pageScaleFactor) && m_drawsContent && !m_masksToBounds)
1991         computePixelAlignment(pageScaleFactor, positionRelativeToBase, scaledPosition, scaledAnchorPoint, pixelAlignmentOffset);
1992
1993     // Update position.
1994     // Position is offset on the layer by the layer anchor point.
1995     FloatPoint adjustedPosition(scaledPosition.x() + scaledAnchorPoint.x() * scaledSize.width(), scaledPosition.y() + scaledAnchorPoint.y() * scaledSize.height());
1996
1997     if (m_structuralLayer) {
1998         FloatPoint layerPosition(m_position.x() + m_anchorPoint.x() * m_size.width(), m_position.y() + m_anchorPoint.y() * m_size.height());
1999         FloatRect layerBounds(m_boundsOrigin, m_size);
2000
2001         m_structuralLayer->setPosition(layerPosition);
2002         m_structuralLayer->setBounds(layerBounds);
2003         m_structuralLayer->setAnchorPoint(m_anchorPoint);
2004
2005         if (m_layerClones) {
2006             for (auto& clone : m_layerClones->structuralLayerClones) {
2007                 PlatformCALayer* cloneLayer = clone.value.get();
2008                 FloatPoint clonePosition = layerPosition;
2009
2010                 if (m_replicaLayer && isReplicatedRootClone(clone.key)) {
2011                     // Maintain the special-case position for the root of a clone subtree,
2012                     // which we set up in replicatedLayerRoot().
2013                     clonePosition = positionForCloneRootLayer();
2014                 }
2015
2016                 cloneLayer->setPosition(clonePosition);
2017                 cloneLayer->setBounds(layerBounds);
2018                 cloneLayer->setAnchorPoint(m_anchorPoint);
2019             }
2020         }
2021
2022         // If we have a structural layer, we just use 0.5, 0.5 for the anchor point of the main layer.
2023         scaledAnchorPoint = FloatPoint(0.5f, 0.5f);
2024         adjustedPosition = FloatPoint(scaledAnchorPoint.x() * scaledSize.width() - pixelAlignmentOffset.width(), scaledAnchorPoint.y() * scaledSize.height() - pixelAlignmentOffset.height());
2025     }
2026
2027     m_pixelAlignmentOffset = pixelAlignmentOffset;
2028
2029     // Push the layer to device pixel boundary (setPosition()), but move the content back to its original position (setBounds())
2030     m_layer->setPosition(adjustedPosition);
2031     FloatRect adjustedBounds = FloatRect(FloatPoint(m_boundsOrigin - pixelAlignmentOffset), m_size);
2032     m_layer->setBounds(adjustedBounds);
2033     m_layer->setAnchorPoint(scaledAnchorPoint);
2034
2035     if (m_layerClones) {
2036         for (auto& clone : m_layerClones->primaryLayerClones) {
2037             PlatformCALayer* cloneLayer = clone.value.get();
2038             FloatPoint clonePosition = adjustedPosition;
2039
2040             if (!m_structuralLayer && m_replicaLayer && isReplicatedRootClone(clone.key)) {
2041                 // Maintain the special-case position for the root of a clone subtree,
2042                 // which we set up in replicatedLayerRoot().
2043                 clonePosition = positionForCloneRootLayer();
2044             }
2045
2046             cloneLayer->setPosition(clonePosition);
2047             cloneLayer->setBounds(adjustedBounds);
2048             cloneLayer->setAnchorPoint(scaledAnchorPoint);
2049         }
2050     }
2051 }
2052
2053 void GraphicsLayerCA::updateTransform()
2054 {
2055     primaryLayer()->setTransform(transform());
2056
2057     if (LayerMap* layerCloneMap = primaryLayerClones()) {
2058         for (auto& clone : *layerCloneMap) {
2059             PlatformCALayer* currLayer = clone.value.get();
2060             if (m_replicaLayer && isReplicatedRootClone(clone.key)) {
2061                 // Maintain the special-case transform for the root of a clone subtree,
2062                 // which we set up in replicatedLayerRoot().
2063                 currLayer->setTransform(TransformationMatrix());
2064             } else
2065                 currLayer->setTransform(transform());
2066         }
2067     }
2068 }
2069
2070 void GraphicsLayerCA::updateChildrenTransform()
2071 {
2072     primaryLayer()->setSublayerTransform(childrenTransform());
2073
2074     if (LayerMap* layerCloneMap = primaryLayerClones()) {
2075         for (auto& layer : layerCloneMap->values())
2076             layer->setSublayerTransform(childrenTransform());
2077     }
2078 }
2079
2080 void GraphicsLayerCA::updateMasksToBounds()
2081 {
2082     m_layer->setMasksToBounds(m_masksToBounds);
2083
2084     if (m_layerClones) {
2085         for (auto& layer : m_layerClones->primaryLayerClones.values())
2086             layer->setMasksToBounds(m_masksToBounds);
2087     }
2088 }
2089
2090 void GraphicsLayerCA::updateContentsVisibility()
2091 {
2092     // Note that m_contentsVisible also affects whether m_contentsLayer is parented.
2093     if (m_contentsVisible) {
2094         if (m_drawsContent)
2095             m_layer->setNeedsDisplay();
2096
2097         if (m_backdropLayer)
2098             m_backdropLayer->setHidden(false);
2099     } else {
2100         m_layer->setContents(nullptr);
2101
2102         if (m_layerClones) {
2103             for (auto& layer : m_layerClones->primaryLayerClones.values())
2104                 layer->setContents(nullptr);
2105         }
2106
2107         if (m_backdropLayer)
2108             m_backdropLayer->setHidden(true);
2109     }
2110
2111     m_layer->setContentsHidden(!m_contentsVisible);
2112 }
2113
2114 void GraphicsLayerCA::updateUserInteractionEnabled()
2115 {
2116     m_layer->setUserInteractionEnabled(m_userInteractionEnabled);
2117 }
2118
2119 void GraphicsLayerCA::updateContentsOpaque(float pageScaleFactor)
2120 {
2121     bool contentsOpaque = m_contentsOpaque;
2122     if (contentsOpaque) {
2123         float contentsScale = pageScaleFactor * deviceScaleFactor();
2124         if (!WTF::isIntegral(contentsScale) && !client().paintsOpaquelyAtNonIntegralScales(this))
2125             contentsOpaque = false;
2126     }
2127     
2128     m_layer->setOpaque(contentsOpaque);
2129
2130     if (m_layerClones) {
2131         for (auto& layer : m_layerClones->primaryLayerClones.values())
2132             layer->setOpaque(contentsOpaque);
2133     }
2134 }
2135
2136 void GraphicsLayerCA::updateBackfaceVisibility()
2137 {
2138     if (m_structuralLayer && structuralLayerPurpose() == StructuralLayerForReplicaFlattening) {
2139         m_structuralLayer->setDoubleSided(m_backfaceVisibility);
2140
2141         if (m_layerClones) {
2142             for (auto& layer : m_layerClones->structuralLayerClones.values())
2143                 layer->setDoubleSided(m_backfaceVisibility);
2144         }
2145     }
2146
2147     m_layer->setDoubleSided(m_backfaceVisibility);
2148
2149     if (m_layerClones) {
2150         for (auto& layer : m_layerClones->primaryLayerClones.values())
2151             layer->setDoubleSided(m_backfaceVisibility);
2152     }
2153 }
2154
2155 void GraphicsLayerCA::updateFilters()
2156 {
2157     m_layer->setFilters(m_filters);
2158
2159     if (m_layerClones) {
2160         for (auto& clone : m_layerClones->primaryLayerClones) {
2161             if (m_replicaLayer && isReplicatedRootClone(clone.key))
2162                 continue;
2163
2164             clone.value->setFilters(m_filters);
2165         }
2166     }
2167 }
2168
2169 void GraphicsLayerCA::updateBackdropFilters(CommitState& commitState)
2170 {
2171     using CheckedUnsigned = Checked<unsigned, RecordOverflow>;
2172
2173     bool canHaveBackdropFilters = needsBackdrop();
2174
2175     if (canHaveBackdropFilters) {
2176         canHaveBackdropFilters = false;
2177         IntRect backdropFilterRect = enclosingIntRect(m_backdropFiltersRect.rect());
2178         if (backdropFilterRect.width() > 0 && backdropFilterRect.height() > 0) {
2179             CheckedUnsigned backdropFilterArea = CheckedUnsigned(backdropFilterRect.width()) * CheckedUnsigned(backdropFilterRect.height());
2180             if (!backdropFilterArea.hasOverflowed()) {
2181                 CheckedUnsigned newTotalBackdropFilterArea = CheckedUnsigned(commitState.totalBackdropFilterArea) + backdropFilterArea;
2182                 if (!newTotalBackdropFilterArea.hasOverflowed() && newTotalBackdropFilterArea.unsafeGet() <= cMaxTotalBackdropFilterArea) {
2183                     commitState.totalBackdropFilterArea = newTotalBackdropFilterArea.unsafeGet();
2184                     canHaveBackdropFilters = true;
2185                 }
2186             }
2187         }
2188     }
2189
2190     if (!canHaveBackdropFilters) {
2191         if (m_backdropLayer) {
2192             m_backdropLayer->removeFromSuperlayer();
2193             m_backdropLayer->setOwner(nullptr);
2194             m_backdropLayer = nullptr;
2195         }
2196         return;
2197     }
2198
2199     // If nothing actually changed, no need to touch the layer properties.
2200     if (!(m_uncommittedChanges & BackdropFiltersChanged))
2201         return;
2202
2203     bool madeLayer = !m_backdropLayer;
2204     if (!m_backdropLayer) {
2205         m_backdropLayer = createPlatformCALayer(PlatformCALayer::LayerTypeBackdropLayer, this);
2206         m_backdropLayer->setAnchorPoint(FloatPoint3D());
2207         m_backdropLayer->setMasksToBounds(true);
2208         m_backdropLayer->setName("backdrop");
2209     }
2210
2211     m_backdropLayer->setHidden(!m_contentsVisible);
2212     m_backdropLayer->setFilters(m_backdropFilters);
2213
2214     if (m_layerClones) {
2215         for (auto& clone : m_layerClones->backdropLayerClones) {
2216             PlatformCALayer* cloneLayer = clone.value.get();
2217             cloneLayer->setHidden(!m_contentsVisible);
2218             cloneLayer->setFilters(m_backdropFilters);
2219         }
2220     }
2221
2222     if (madeLayer) {
2223         updateBackdropFiltersRect();
2224         noteSublayersChanged(DontScheduleFlush);
2225     }
2226 }
2227
2228 void GraphicsLayerCA::updateBackdropFiltersRect()
2229 {
2230     if (!m_backdropLayer)
2231         return;
2232
2233     FloatRect contentBounds(0, 0, m_backdropFiltersRect.rect().width(), m_backdropFiltersRect.rect().height());
2234     m_backdropLayer->setBounds(contentBounds);
2235     m_backdropLayer->setPosition(m_backdropFiltersRect.rect().location());
2236
2237     updateClippingStrategy(*m_backdropLayer, m_backdropClippingLayer, m_backdropFiltersRect);
2238
2239     if (m_layerClones) {
2240         for (auto& clone : m_layerClones->backdropLayerClones) {
2241             PlatformCALayer* backdropCloneLayer = clone.value.get();
2242             backdropCloneLayer->setBounds(contentBounds);
2243             backdropCloneLayer->setPosition(m_backdropFiltersRect.rect().location());
2244
2245             CloneID cloneID = clone.key;
2246             RefPtr<PlatformCALayer> backdropClippingLayerClone = m_layerClones->backdropClippingLayerClones.get(cloneID);
2247
2248             bool hadBackdropClippingLayer = backdropClippingLayerClone;
2249             updateClippingStrategy(*backdropCloneLayer, backdropClippingLayerClone, m_backdropFiltersRect);
2250
2251             if (!backdropClippingLayerClone)
2252                 m_layerClones->backdropClippingLayerClones.remove(cloneID);
2253             else if (backdropClippingLayerClone && !hadBackdropClippingLayer)
2254                 m_layerClones->backdropClippingLayerClones.add(cloneID, backdropClippingLayerClone);
2255         }
2256     }
2257 }
2258
2259 #if ENABLE(CSS_COMPOSITING)
2260 void GraphicsLayerCA::updateBlendMode()
2261 {
2262     primaryLayer()->setBlendMode(m_blendMode);
2263
2264     if (LayerMap* layerCloneMap = primaryLayerClones()) {
2265         for (auto& clone : *layerCloneMap) {
2266             if (m_replicaLayer && isReplicatedRootClone(clone.key))
2267                 continue;
2268             clone.value->setBlendMode(m_blendMode);
2269         }
2270     }
2271 }
2272 #endif
2273
2274 void GraphicsLayerCA::updateShape()
2275 {
2276     m_layer->setShapePath(m_shapeLayerPath);
2277
2278     if (LayerMap* layerCloneMap = primaryLayerClones()) {
2279         for (auto& layer : layerCloneMap->values())
2280             layer->setShapePath(m_shapeLayerPath);
2281     }
2282 }
2283
2284 void GraphicsLayerCA::updateWindRule()
2285 {
2286     m_layer->setShapeWindRule(m_shapeLayerWindRule);
2287 }
2288
2289 bool GraphicsLayerCA::updateStructuralLayer()
2290 {
2291     return ensureStructuralLayer(structuralLayerPurpose());
2292 }
2293
2294 bool GraphicsLayerCA::ensureStructuralLayer(StructuralLayerPurpose purpose)
2295 {
2296     const LayerChangeFlags structuralLayerChangeFlags = NameChanged
2297         | GeometryChanged
2298         | TransformChanged
2299         | ChildrenTransformChanged
2300         | ChildrenChanged
2301         | BackfaceVisibilityChanged
2302         | FiltersChanged
2303         | BackdropFiltersChanged
2304         | MaskLayerChanged
2305         | OpacityChanged;
2306
2307     bool structuralLayerChanged = false;
2308
2309     if (purpose == NoStructuralLayer) {
2310         if (m_structuralLayer) {
2311             // Replace the transformLayer in the parent with this layer.
2312             m_layer->removeFromSuperlayer();
2313  
2314             // If m_layer doesn't have a parent, it means it's the root layer and
2315             // is likely hosted by something that is not expecting to be changed
2316             ASSERT(m_structuralLayer->superlayer());
2317             m_structuralLayer->superlayer()->replaceSublayer(*m_structuralLayer, *m_layer);
2318
2319             moveAnimations(m_structuralLayer.get(), m_layer.get());
2320
2321             // Release the structural layer.
2322             m_structuralLayer = nullptr;
2323             structuralLayerChanged = true;
2324
2325             addUncommittedChanges(structuralLayerChangeFlags);
2326         }
2327         return structuralLayerChanged;
2328     }
2329
2330     if (purpose == StructuralLayerForPreserves3D) {
2331         if (m_structuralLayer && m_structuralLayer->layerType() != PlatformCALayer::LayerTypeTransformLayer)
2332             m_structuralLayer = nullptr;
2333         
2334         if (!m_structuralLayer) {
2335             m_structuralLayer = createPlatformCALayer(PlatformCALayer::LayerTypeTransformLayer, this);
2336             structuralLayerChanged = true;
2337         }
2338     } else {
2339         if (m_structuralLayer && m_structuralLayer->layerType() != PlatformCALayer::LayerTypeLayer)
2340             m_structuralLayer = nullptr;
2341
2342         if (!m_structuralLayer) {
2343             m_structuralLayer = createPlatformCALayer(PlatformCALayer::LayerTypeLayer, this);
2344             structuralLayerChanged = true;
2345         }
2346     }
2347     
2348     if (!structuralLayerChanged)
2349         return false;
2350     
2351     addUncommittedChanges(structuralLayerChangeFlags);
2352
2353     // We've changed the layer that our parent added to its sublayer list, so tell it to update
2354     // sublayers again in its commitLayerChangesAfterSublayers().
2355     downcast<GraphicsLayerCA>(*parent()).noteSublayersChanged(DontScheduleFlush);
2356
2357     // Set properties of m_layer to their default values, since these are expressed on on the structural layer.
2358     FloatPoint point(m_size.width() / 2.0f, m_size.height() / 2.0f);
2359     FloatPoint3D anchorPoint(0.5f, 0.5f, 0);
2360     m_layer->setPosition(point);
2361     m_layer->setAnchorPoint(anchorPoint);
2362     m_layer->setTransform(TransformationMatrix());
2363     m_layer->setOpacity(1);
2364     if (m_layerClones) {
2365         for (auto& layer : m_layerClones->primaryLayerClones.values()) {
2366             layer->setPosition(point);
2367             layer->setAnchorPoint(anchorPoint);
2368             layer->setTransform(TransformationMatrix());
2369             layer->setOpacity(1);
2370         }
2371     }
2372
2373     moveAnimations(m_layer.get(), m_structuralLayer.get());
2374     return true;
2375 }
2376
2377 GraphicsLayerCA::StructuralLayerPurpose GraphicsLayerCA::structuralLayerPurpose() const
2378 {
2379     if (preserves3D())
2380         return StructuralLayerForPreserves3D;
2381     
2382     if (isReplicated())
2383         return StructuralLayerForReplicaFlattening;
2384
2385     if (needsBackdrop())
2386         return StructuralLayerForBackdrop;
2387
2388     return NoStructuralLayer;
2389 }
2390
2391 void GraphicsLayerCA::updateDrawsContent()
2392 {
2393     if (m_drawsContent) {
2394         m_layer->setNeedsDisplay();
2395         m_hasEverPainted = false;
2396     } else {
2397         m_layer->setContents(nullptr);
2398         if (m_layerClones) {
2399             for (auto& layer : m_layerClones->primaryLayerClones.values())
2400                 layer->setContents(nullptr);
2401         }
2402     }
2403 }
2404
2405 void GraphicsLayerCA::updateCoverage(const CommitState& commitState)
2406 {
2407     // FIXME: Need to set coverage on clone layers too.
2408     if (TiledBacking* backing = tiledBacking()) {
2409         backing->setVisibleRect(m_visibleRect);
2410         backing->setCoverageRect(m_coverageRect);
2411     }
2412
2413     bool requiresBacking = m_intersectsCoverageRect
2414         || !allowsBackingStoreDetaching()
2415         || commitState.ancestorWithTransformAnimationIntersectsCoverageRect // FIXME: Compute backing exactly for descendants of animating layers.
2416         || (isRunningTransformAnimation() && !animationExtent()); // Create backing if we don't know the animation extent.
2417
2418 #if !LOG_DISABLED
2419     if (requiresBacking) {
2420         auto reasonForBacking = [&]() -> const char* {
2421             if (m_intersectsCoverageRect)
2422                 return "intersectsCoverageRect";
2423             
2424             if (!allowsBackingStoreDetaching())
2425                 return "backing detachment disallowed";
2426
2427             if (commitState.ancestorWithTransformAnimationIntersectsCoverageRect)
2428                 return "ancestor with transform";
2429             
2430             return "has transform animation with unknown extent";
2431         };
2432         LOG_WITH_STREAM(Layers, stream << "GraphicsLayerCA " << this << " id " << primaryLayerID() << " setBackingStoreAttached: " << requiresBacking << " (" << reasonForBacking() << ")");
2433     } else
2434         LOG_WITH_STREAM(Layers, stream << "GraphicsLayerCA " << this << " id " << primaryLayerID() << " setBackingStoreAttached: " << requiresBacking);
2435 #endif
2436
2437     m_layer->setBackingStoreAttached(requiresBacking);
2438     if (m_layerClones) {
2439         for (auto& layer : m_layerClones->primaryLayerClones.values())
2440             layer->setBackingStoreAttached(requiresBacking);
2441     }
2442
2443     m_sizeAtLastCoverageRectUpdate = m_size;
2444 }
2445
2446 void GraphicsLayerCA::updateAcceleratesDrawing()
2447 {
2448     m_layer->setAcceleratesDrawing(m_acceleratesDrawing);
2449 }
2450
2451 void GraphicsLayerCA::updateSupportsSubpixelAntialiasedText()
2452 {
2453     m_layer->setSupportsSubpixelAntialiasedText(m_supportsSubpixelAntialiasedText);
2454 }
2455
2456 static void setLayerDebugBorder(PlatformCALayer& layer, Color borderColor, float borderWidth)
2457 {
2458     layer.setBorderColor(borderColor);
2459     layer.setBorderWidth(borderColor.isValid() ? borderWidth : 0);
2460 }
2461
2462 static const float contentsLayerBorderWidth = 4;
2463 static Color contentsLayerDebugBorderColor(bool showingBorders)
2464 {
2465     return showingBorders ? Color(0, 0, 128, 180) : Color();
2466 }
2467
2468 static const float cloneLayerBorderWidth = 2;
2469 static Color cloneLayerDebugBorderColor(bool showingBorders)
2470 {
2471     return showingBorders ? Color(255, 122, 251) : Color();
2472 }
2473
2474 void GraphicsLayerCA::updateDebugIndicators()
2475 {
2476     Color borderColor;
2477     float width = 0;
2478
2479     bool showDebugBorders = isShowingDebugBorder();
2480     if (showDebugBorders)
2481         getDebugBorderInfo(borderColor, width);
2482
2483     // Paint repaint counter.
2484     m_layer->setNeedsDisplay();
2485
2486     setLayerDebugBorder(*m_layer, borderColor, width);
2487     if (m_contentsLayer)
2488         setLayerDebugBorder(*m_contentsLayer, contentsLayerDebugBorderColor(showDebugBorders), contentsLayerBorderWidth);
2489
2490     if (m_layerClones) {
2491         for (auto& layer : m_layerClones->primaryLayerClones.values())
2492             setLayerDebugBorder(*layer, borderColor, width);
2493
2494         Color cloneLayerBorderColor = cloneLayerDebugBorderColor(showDebugBorders);
2495         for (auto& layer : m_layerClones->structuralLayerClones.values())
2496             setLayerDebugBorder(*layer, cloneLayerBorderColor, cloneLayerBorderWidth);
2497
2498         Color contentsLayerBorderColor = contentsLayerDebugBorderColor(showDebugBorders);
2499         for (auto& layer : m_layerClones->contentsLayerClones.values())
2500             setLayerDebugBorder(*layer, contentsLayerBorderColor, contentsLayerBorderWidth);
2501     }
2502 }
2503
2504 void GraphicsLayerCA::updateTiles()
2505 {
2506     if (!m_layer->usesTiledBackingLayer())
2507         return;
2508
2509     tiledBacking()->revalidateTiles();
2510 }
2511
2512 void GraphicsLayerCA::updateBackgroundColor()
2513 {
2514     m_layer->setBackgroundColor(m_backgroundColor);
2515 }
2516
2517 void GraphicsLayerCA::updateContentsImage()
2518 {
2519     if (m_pendingContentsImage) {
2520         if (!m_contentsLayer.get()) {
2521             m_contentsLayer = createPlatformCALayer(PlatformCALayer::LayerTypeLayer, this);
2522 #if ENABLE(TREE_DEBUGGING)
2523             m_contentsLayer->setName(makeString("contents image ", m_contentsLayer->layerID()));
2524 #else
2525             m_contentsLayer->setName("contents image");
2526 #endif
2527             setupContentsLayer(m_contentsLayer.get());
2528             // m_contentsLayer will be parented by updateSublayerList
2529         }
2530
2531         // FIXME: maybe only do trilinear if the image is being scaled down,
2532         // but then what if the layer size changes?
2533         m_contentsLayer->setMinificationFilter(PlatformCALayer::Trilinear);
2534         m_contentsLayer->setContents(m_pendingContentsImage.get());
2535         m_pendingContentsImage = nullptr;
2536
2537         if (m_layerClones) {
2538             for (auto& layer : m_layerClones->contentsLayerClones.values())
2539                 layer->setContents(m_contentsLayer->contents());
2540         }
2541         
2542         updateContentsRects();
2543     } else {
2544         // No image.
2545         // m_contentsLayer will be removed via updateSublayerList.
2546         m_contentsLayer = nullptr;
2547     }
2548 }
2549
2550 void GraphicsLayerCA::updateContentsPlatformLayer()
2551 {
2552     if (!m_contentsLayer)
2553         return;
2554
2555     // Platform layer was set as m_contentsLayer, and will get parented in updateSublayerList().
2556     setupContentsLayer(m_contentsLayer.get());
2557
2558     if (m_contentsLayerPurpose == ContentsLayerPurpose::Canvas)
2559         m_contentsLayer->setNeedsDisplay();
2560
2561     updateContentsRects();
2562 }
2563
2564 void GraphicsLayerCA::updateContentsColorLayer()
2565 {
2566     // Color layer was set as m_contentsLayer, and will get parented in updateSublayerList().
2567     if (!m_contentsLayer || m_contentsLayerPurpose != ContentsLayerPurpose::BackgroundColor)
2568         return;
2569
2570     setupContentsLayer(m_contentsLayer.get());
2571     updateContentsRects();
2572     ASSERT(m_contentsSolidColor.isValid());
2573     m_contentsLayer->setBackgroundColor(m_contentsSolidColor);
2574
2575     if (m_layerClones) {
2576         for (auto& layer : m_layerClones->contentsLayerClones.values())
2577             layer->setBackgroundColor(m_contentsSolidColor);
2578     }
2579 }
2580
2581 // The clipping strategy depends on whether the rounded rect has equal corner radii.
2582 void GraphicsLayerCA::updateClippingStrategy(PlatformCALayer& clippingLayer, RefPtr<PlatformCALayer>& shapeMaskLayer, const FloatRoundedRect& roundedRect)
2583 {
2584     if (roundedRect.radii().isUniformCornerRadius()) {
2585         clippingLayer.setMask(nullptr);
2586         if (shapeMaskLayer) {
2587             shapeMaskLayer->setOwner(nullptr);
2588             shapeMaskLayer = nullptr;
2589         }
2590
2591         clippingLayer.setMasksToBounds(true);
2592         clippingLayer.setCornerRadius(roundedRect.radii().topLeft().width());
2593         return;
2594     }
2595
2596     if (!shapeMaskLayer) {
2597         shapeMaskLayer = createPlatformCALayer(PlatformCALayer::LayerTypeShapeLayer, this);
2598         shapeMaskLayer->setAnchorPoint({ });
2599         shapeMaskLayer->setName("shape mask");
2600     }
2601     
2602     shapeMaskLayer->setPosition(roundedRect.rect().location());
2603     FloatRect shapeBounds({ }, roundedRect.rect().size());
2604     shapeMaskLayer->setBounds(shapeBounds);
2605     FloatRoundedRect offsetRoundedRect(shapeBounds, roundedRect.radii());
2606     shapeMaskLayer->setShapeRoundedRect(offsetRoundedRect);
2607
2608     clippingLayer.setCornerRadius(0);
2609     clippingLayer.setMask(shapeMaskLayer.get());
2610 }
2611
2612 void GraphicsLayerCA::updateContentsRects()
2613 {
2614     if (!m_contentsLayer)
2615         return;
2616
2617     FloatPoint contentOrigin;
2618     const FloatRect contentBounds(0, 0, m_contentsRect.width(), m_contentsRect.height());
2619
2620     FloatPoint clippingOrigin(m_contentsClippingRect.rect().location());
2621     FloatRect clippingBounds(FloatPoint(), m_contentsClippingRect.rect().size());
2622     
2623     bool gainedOrLostClippingLayer = false;
2624     if (m_contentsClippingRect.isRounded() || !m_contentsClippingRect.rect().contains(m_contentsRect)) {
2625         if (!m_contentsClippingLayer) {
2626             m_contentsClippingLayer = createPlatformCALayer(PlatformCALayer::LayerTypeLayer, this);
2627             m_contentsClippingLayer->setAnchorPoint(FloatPoint());
2628 #if ENABLE(TREE_DEBUGGING)
2629             m_contentsClippingLayer->setName(makeString("contents clipping ", m_contentsClippingLayer->layerID()));
2630 #else
2631             m_contentsClippingLayer->setName("contents clipping");
2632 #endif
2633             gainedOrLostClippingLayer = true;
2634         }
2635
2636         m_contentsClippingLayer->setPosition(clippingOrigin);
2637         m_contentsClippingLayer->setBounds(clippingBounds);
2638
2639         updateClippingStrategy(*m_contentsClippingLayer, m_contentsShapeMaskLayer, m_contentsClippingRect);
2640
2641         if (gainedOrLostClippingLayer) {
2642             m_contentsLayer->removeFromSuperlayer();
2643             m_contentsClippingLayer->appendSublayer(*m_contentsLayer);
2644         }
2645     
2646         contentOrigin = FloatPoint(m_contentsRect.location() - m_contentsClippingRect.rect().location());
2647     } else {
2648         if (m_contentsClippingLayer) {
2649             m_contentsLayer->removeFromSuperlayer();
2650
2651             m_contentsClippingLayer->removeFromSuperlayer();
2652             m_contentsClippingLayer->setOwner(nullptr);
2653             m_contentsClippingLayer->setMask(nullptr);
2654             m_contentsClippingLayer = nullptr;
2655             gainedOrLostClippingLayer = true;
2656         }
2657
2658         if (m_contentsShapeMaskLayer) {
2659             m_contentsShapeMaskLayer->setOwner(nullptr);
2660             m_contentsShapeMaskLayer = nullptr;
2661         }
2662
2663         contentOrigin = m_contentsRect.location();
2664     }
2665     
2666     if (gainedOrLostClippingLayer)
2667         noteSublayersChanged(DontScheduleFlush);
2668
2669     m_contentsLayer->setPosition(contentOrigin);
2670     m_contentsLayer->setBounds(contentBounds);
2671
2672     if (m_layerClones) {
2673         for (auto& layer : m_layerClones->contentsLayerClones.values()) {
2674             layer->setPosition(contentOrigin);
2675             layer->setBounds(contentBounds);
2676         }
2677
2678         for (auto& clone : m_layerClones->contentsClippingLayerClones) {
2679             CloneID cloneID = clone.key;
2680             RefPtr<PlatformCALayer> shapeMaskLayerClone = m_layerClones->contentsShapeMaskLayerClones.get(cloneID);
2681
2682             bool hadShapeMask = shapeMaskLayerClone;
2683             updateClippingStrategy(*clone.value, shapeMaskLayerClone, m_contentsClippingRect);
2684
2685             if (!shapeMaskLayerClone)
2686                 m_layerClones->contentsShapeMaskLayerClones.remove(cloneID);
2687             else if (shapeMaskLayerClone && !hadShapeMask)
2688                 m_layerClones->contentsShapeMaskLayerClones.add(cloneID, shapeMaskLayerClone);
2689         }
2690     }
2691 }
2692
2693 void GraphicsLayerCA::updateMasksToBoundsRect()
2694 {
2695     updateClippingStrategy(*m_layer, m_shapeMaskLayer, m_masksToBoundsRect);
2696
2697     if (m_layerClones) {
2698         for (auto& clone : m_layerClones->primaryLayerClones) {
2699             CloneID cloneID = clone.key;
2700             RefPtr<PlatformCALayer> shapeMaskLayerClone = m_layerClones->shapeMaskLayerClones.get(cloneID);
2701
2702             bool hadShapeMask = shapeMaskLayerClone;
2703             updateClippingStrategy(*clone.value, shapeMaskLayerClone, m_masksToBoundsRect);
2704
2705             if (!shapeMaskLayerClone)
2706                 m_layerClones->shapeMaskLayerClones.remove(cloneID);
2707             else if (!hadShapeMask && shapeMaskLayerClone)
2708                 m_layerClones->shapeMaskLayerClones.add(cloneID, shapeMaskLayerClone);
2709         }
2710     }
2711 }
2712
2713 void GraphicsLayerCA::updateEventRegion()
2714 {
2715     m_layer->setEventRegion(eventRegion());
2716 }
2717
2718 void GraphicsLayerCA::updateMaskLayer()
2719 {
2720     PlatformCALayer* maskCALayer = m_maskLayer ? downcast<GraphicsLayerCA>(*m_maskLayer).primaryLayer() : nullptr;
2721     
2722     LayerMap* layerCloneMap;
2723     if (m_structuralLayer && structuralLayerPurpose() == StructuralLayerForBackdrop) {
2724         m_structuralLayer->setMask(maskCALayer);
2725         layerCloneMap = m_layerClones ? &m_layerClones->structuralLayerClones : nullptr;
2726     } else {
2727         m_layer->setMask(maskCALayer);
2728         layerCloneMap = m_layerClones ? &m_layerClones->primaryLayerClones : nullptr;
2729     }
2730
2731     LayerMap* maskLayerCloneMap = m_maskLayer ? downcast<GraphicsLayerCA>(*m_maskLayer).primaryLayerClones() : nullptr;
2732     if (layerCloneMap) {
2733         for (auto& clone : *layerCloneMap) {
2734             PlatformCALayer* maskClone = maskLayerCloneMap ? maskLayerCloneMap->get(clone.key) : nullptr;
2735             clone.value->setMask(maskClone);
2736         }
2737     }
2738 }
2739
2740 void GraphicsLayerCA::updateReplicatedLayers()
2741 {
2742     // Clone the descendants of the replicated layer, and parent under us.
2743     ReplicaState replicaState(ReplicaState::ReplicaBranch);
2744
2745     RefPtr<PlatformCALayer>replicaRoot = replicatedLayerRoot(replicaState);
2746     if (!replicaRoot)
2747         return;
2748
2749     if (m_structuralLayer)
2750         m_structuralLayer->insertSublayer(*replicaRoot, 0);
2751     else
2752         m_layer->insertSublayer(*replicaRoot, 0);
2753 }
2754
2755 // For now, this assumes that layers only ever have one replica, so replicaIndices contains only 0 and 1.
2756 GraphicsLayerCA::CloneID GraphicsLayerCA::ReplicaState::cloneID() const
2757 {
2758     size_t depth = m_replicaBranches.size();
2759
2760     const size_t bitsPerUChar = sizeof(UChar) * 8;
2761     size_t vectorSize = (depth + bitsPerUChar - 1) / bitsPerUChar;
2762     
2763     Vector<UChar> result(vectorSize);
2764     result.fill(0);
2765
2766     // Create a string from the bit sequence which we can use to identify the clone.
2767     // Note that the string may contain embedded nulls, but that's OK.
2768     for (size_t i = 0; i < depth; ++i) {
2769         UChar& currChar = result[i / bitsPerUChar];
2770         currChar = (currChar << 1) | m_replicaBranches[i];
2771     }
2772     
2773     return String::adopt(WTFMove(result));
2774 }
2775
2776 RefPtr<PlatformCALayer> GraphicsLayerCA::replicatedLayerRoot(ReplicaState& replicaState)
2777 {
2778     // Limit replica nesting, to avoid 2^N explosion of replica layers.
2779     if (!m_replicatedLayer || replicaState.replicaDepth() == ReplicaState::maxReplicaDepth)
2780         return nullptr;
2781
2782     GraphicsLayerCA& replicatedLayer = downcast<GraphicsLayerCA>(*m_replicatedLayer);
2783     
2784     RefPtr<PlatformCALayer> clonedLayerRoot = replicatedLayer.fetchCloneLayers(this, replicaState, RootCloneLevel);
2785     FloatPoint cloneRootPosition = replicatedLayer.positionForCloneRootLayer();
2786
2787     // Replica root has no offset or transform
2788     clonedLayerRoot->setPosition(cloneRootPosition);
2789     clonedLayerRoot->setTransform(TransformationMatrix());
2790
2791     return clonedLayerRoot;
2792 }
2793
2794 void GraphicsLayerCA::updateAnimations()
2795 {
2796     if (!hasAnimations())
2797         return;
2798
2799     size_t numAnimations;
2800     if ((numAnimations = m_animations->uncomittedAnimations.size())) {
2801         for (size_t i = 0; i < numAnimations; ++i) {
2802             const LayerPropertyAnimation& pendingAnimation = m_animations->uncomittedAnimations[i];
2803             setAnimationOnLayer(*pendingAnimation.m_animation, pendingAnimation.m_property, pendingAnimation.m_name, pendingAnimation.m_index, pendingAnimation.m_subIndex, pendingAnimation.m_timeOffset);
2804
2805             AnimationsMap::iterator it = m_animations->runningAnimations.find(pendingAnimation.m_name);
2806             if (it == m_animations->runningAnimations.end()) {
2807                 Vector<LayerPropertyAnimation> animations;
2808                 animations.append(pendingAnimation);
2809                 m_animations->runningAnimations.add(pendingAnimation.m_name, animations);
2810             } else {
2811                 Vector<LayerPropertyAnimation>& animations = it->value;
2812                 animations.append(pendingAnimation);
2813             }
2814         }
2815         m_animations->uncomittedAnimations.clear();
2816     }
2817
2818     if (m_animations->animationsToProcess.size()) {
2819         for (const auto& it : m_animations->animationsToProcess) {
2820             const String& currentAnimationName = it.key;
2821             auto animationIterator = m_animations->runningAnimations.find(currentAnimationName);
2822             if (animationIterator == m_animations->runningAnimations.end())
2823                 continue;
2824
2825             for (const auto& processingInfo : it.value) {
2826                 const Vector<LayerPropertyAnimation>& animations = animationIterator->value;
2827                 for (const auto& currentAnimation : animations) {
2828                     switch (processingInfo.action) {
2829                     case Remove:
2830                         removeCAAnimationFromLayer(currentAnimation.m_property, currentAnimationName, currentAnimation.m_index, currentAnimation.m_subIndex);
2831                         break;
2832                     case Pause:
2833                         pauseCAAnimationOnLayer(currentAnimation.m_property, currentAnimationName, currentAnimation.m_index, currentAnimation.m_subIndex, processingInfo.timeOffset);
2834                         break;
2835                     case Seek:
2836                         seekCAAnimationOnLayer(currentAnimation.m_property, currentAnimationName, currentAnimation.m_index, currentAnimation.m_subIndex, processingInfo.timeOffset);
2837                         break;
2838                     }
2839                 }
2840
2841                 if (processingInfo.action == Remove)
2842                     m_animations->runningAnimations.remove(currentAnimationName);
2843             }
2844         }
2845
2846         m_animations->animationsToProcess.clear();
2847     }
2848 }
2849
2850 bool GraphicsLayerCA::isRunningTransformAnimation() const
2851 {
2852     if (!hasAnimations())
2853         return false;
2854
2855     for (const auto& it : m_animations->runningAnimations) {
2856         const auto& propertyAnimations = it.value;
2857         size_t numAnimations = propertyAnimations.size();
2858         for (size_t i = 0; i < numAnimations; ++i) {
2859             const LayerPropertyAnimation& currAnimation = propertyAnimations[i];
2860             if (currAnimation.m_property == AnimatedPropertyTransform)
2861                 return true;
2862         }
2863     }
2864     return false;
2865 }
2866
2867 void GraphicsLayerCA::ensureLayerAnimations()
2868 {
2869     if (!m_animations)
2870         m_animations = makeUnique<LayerAnimations>();
2871 }
2872
2873 void GraphicsLayerCA::setAnimationOnLayer(PlatformCAAnimation& caAnim, AnimatedPropertyID property, const String& animationName, int index, int subIndex, Seconds timeOffset)
2874 {
2875     PlatformCALayer* layer = animatedLayer(property);
2876
2877     if (timeOffset)
2878         caAnim.setBeginTime(CACurrentMediaTime() - timeOffset.seconds());
2879
2880     String animationID = animationIdentifier(animationName, property, index, subIndex);
2881
2882     layer->removeAnimationForKey(animationID);
2883     layer->addAnimationForKey(animationID, caAnim);
2884
2885     if (LayerMap* layerCloneMap = animatedLayerClones(property)) {
2886         for (auto& clone : *layerCloneMap) {
2887             // Skip immediate replicas, since they move with the original.
2888             if (m_replicaLayer && isReplicatedRootClone(clone.key))
2889                 continue;
2890
2891             clone.value->removeAnimationForKey(animationID);
2892             clone.value->addAnimationForKey(animationID, caAnim);
2893         }
2894     }
2895 }
2896
2897 // Workaround for <rdar://problem/7311367>
2898 static void bug7311367Workaround(PlatformCALayer* transformLayer, const TransformationMatrix& transform)
2899 {
2900     if (!transformLayer)
2901         return;
2902
2903     TransformationMatrix caTransform = transform;
2904     caTransform.setM41(caTransform.m41() + 1);
2905     transformLayer->setTransform(caTransform);
2906
2907     caTransform.setM41(caTransform.m41() - 1);
2908     transformLayer->setTransform(caTransform);
2909 }
2910
2911 bool GraphicsLayerCA::removeCAAnimationFromLayer(AnimatedPropertyID property, const String& animationName, int index, int subIndex)
2912 {
2913     PlatformCALayer* layer = animatedLayer(property);
2914
2915     String animationID = animationIdentifier(animationName, property, index, subIndex);
2916
2917     if (!layer->animationForKey(animationID))
2918         return false;
2919     
2920     layer->removeAnimationForKey(animationID);
2921     bug7311367Workaround(m_structuralLayer.get(), transform());
2922
2923     if (LayerMap* layerCloneMap = animatedLayerClones(property)) {
2924         for (auto& clone : *layerCloneMap) {
2925             // Skip immediate replicas, since they move with the original.
2926             if (m_replicaLayer && isReplicatedRootClone(clone.key))
2927                 continue;
2928
2929             clone.value->removeAnimationForKey(animationID);
2930         }
2931     }
2932     return true;
2933 }
2934
2935 void GraphicsLayerCA::pauseCAAnimationOnLayer(AnimatedPropertyID property, const String& animationName, int index, int subIndex, Seconds timeOffset)
2936 {
2937     PlatformCALayer* layer = animatedLayer(property);
2938
2939     String animationID = animationIdentifier(animationName, property, index, subIndex);
2940
2941     RefPtr<PlatformCAAnimation> curAnim = layer->animationForKey(animationID);
2942     if (!curAnim)
2943         return;
2944
2945     // Animations on the layer are immutable, so we have to clone and modify.
2946     RefPtr<PlatformCAAnimation> newAnim = curAnim->copy();
2947
2948     newAnim->setSpeed(0);
2949     newAnim->setTimeOffset(timeOffset.seconds());
2950     
2951     layer->addAnimationForKey(animationID, *newAnim); // This will replace the running animation.
2952
2953     // Pause the animations on the clones too.
2954     if (LayerMap* layerCloneMap = animatedLayerClones(property)) {
2955         for (auto& clone : *layerCloneMap) {
2956             // Skip immediate replicas, since they move with the original.
2957             if (m_replicaLayer && isReplicatedRootClone(clone.key))
2958                 continue;
2959             clone.value->addAnimationForKey(animationID, *newAnim);
2960         }
2961     }
2962 }
2963
2964 void GraphicsLayerCA::seekCAAnimationOnLayer(AnimatedPropertyID property, const String& animationName, int index, int subIndex, Seconds timeOffset)
2965 {
2966     // FIXME: this can be refactored a fair bit or merged with pauseCAAnimationOnLayer() with an operation flag.
2967     PlatformCALayer* layer = animatedLayer(property);
2968
2969     String animationID = animationIdentifier(animationName, property, index, subIndex);
2970
2971     RefPtr<PlatformCAAnimation> currentAnimation = layer->animationForKey(animationID);
2972     if (!currentAnimation)
2973         return;
2974
2975     // Animations on the layer are immutable, so we have to clone and modify.
2976     RefPtr<PlatformCAAnimation> newAnim = currentAnimation->copy();
2977
2978     newAnim->setBeginTime(CACurrentMediaTime());
2979     newAnim->setTimeOffset(timeOffset.seconds());
2980
2981     layer->addAnimationForKey(animationID, *newAnim); // This will replace the running animation.
2982
2983     // Pause the animations on the clones too.
2984     if (LayerMap* layerCloneMap = animatedLayerClones(property)) {
2985         for (auto& clone : *layerCloneMap) {
2986             // Skip immediate replicas, since they move with the original.
2987             if (m_replicaLayer && isReplicatedRootClone(clone.key))
2988                 continue;
2989             clone.value->addAnimationForKey(animationID, *newAnim);
2990         }
2991     }
2992 }
2993
2994 void GraphicsLayerCA::repaintLayerDirtyRects()
2995 {
2996     if (m_needsFullRepaint) {
2997         ASSERT(!m_dirtyRects.size());
2998         m_layer->setNeedsDisplay();
2999         m_needsFullRepaint = false;
3000         return;
3001     }
3002
3003     for (auto& dirtyRect : m_dirtyRects)
3004         m_layer->setNeedsDisplayInRect(dirtyRect);
3005     
3006     m_dirtyRects.clear();
3007 }
3008
3009 void GraphicsLayerCA::updateContentsNeedsDisplay()
3010 {
3011     if (m_contentsLayer)
3012         m_contentsLayer->setNeedsDisplay();
3013 }
3014
3015 bool GraphicsLayerCA::createAnimationFromKeyframes(const KeyframeValueList& valueList, const Animation* animation, const String& animationName, Seconds timeOffset)
3016 {
3017     ASSERT(valueList.property() != AnimatedPropertyTransform && (!supportsAcceleratedFilterAnimations() || valueList.property() != AnimatedPropertyFilter));
3018
3019     bool isKeyframe = valueList.size() > 2;
3020     bool valuesOK;
3021     
3022     bool additive = false;
3023     int animationIndex = 0;
3024     
3025     RefPtr<PlatformCAAnimation> caAnimation;
3026
3027     if (isKeyframe) {
3028         caAnimation = createKeyframeAnimation(animation, propertyIdToString(valueList.property()), additive);
3029         valuesOK = setAnimationKeyframes(valueList, animation, caAnimation.get());
3030     } else {
3031         if (animation->timingFunction()->isSpringTimingFunction())
3032             caAnimation = createSpringAnimation(animation, propertyIdToString(valueList.property()), additive);
3033         else
3034             caAnimation = createBasicAnimation(animation, propertyIdToString(valueList.property()), additive);
3035         valuesOK = setAnimationEndpoints(valueList, animation, caAnimation.get());
3036     }
3037
3038     if (!valuesOK)
3039         return false;
3040
3041     appendToUncommittedAnimations(LayerPropertyAnimation(caAnimation.releaseNonNull(), animationName, valueList.property(), animationIndex, 0, timeOffset));
3042
3043     return true;
3044 }
3045
3046 bool GraphicsLayerCA::appendToUncommittedAnimations(const KeyframeValueList& valueList, const TransformOperations* operations, const Animation* animation, const String& animationName, const FloatSize& boxSize, int animationIndex, Seconds timeOffset, bool isMatrixAnimation)
3047 {
3048     TransformOperation::OperationType transformOp = isMatrixAnimation ? TransformOperation::MATRIX_3D : operations->operations().at(animationIndex)->type();
3049 #if !PLATFORM(WIN) && !HAVE(CA_WHERE_ADDITIVE_TRANSFORMS_ARE_REVERSED)
3050     bool additive = animationIndex > 0;
3051 #else
3052     int numAnimations = isMatrixAnimation ? 1 : operations->size();
3053     bool additive = animationIndex < numAnimations - 1;
3054 #endif
3055     bool isKeyframe = valueList.size() > 2;
3056
3057     RefPtr<PlatformCAAnimation> caAnimation;
3058     bool validMatrices = true;
3059     if (isKeyframe) {
3060         caAnimation = createKeyframeAnimation(animation, propertyIdToString(valueList.property()), additive);
3061         validMatrices = setTransformAnimationKeyframes(valueList, animation, caAnimation.get(), animationIndex, transformOp, isMatrixAnimation, boxSize);
3062     } else {
3063         if (animation->timingFunction()->isSpringTimingFunction())
3064             caAnimation = createSpringAnimation(animation, propertyIdToString(valueList.property()), additive);
3065         else
3066             caAnimation = createBasicAnimation(animation, propertyIdToString(valueList.property()), additive);
3067         validMatrices = setTransformAnimationEndpoints(valueList, animation, caAnimation.get(), animationIndex, transformOp, isMatrixAnimation, boxSize);
3068     }
3069     
3070     if (!validMatrices)
3071         return false;
3072
3073     appendToUncommittedAnimations(LayerPropertyAnimation(caAnimation.releaseNonNull(), animationName, valueList.property(), animationIndex, 0, timeOffset));
3074     return true;
3075 }
3076
3077 bool GraphicsLayerCA::createTransformAnimationsFromKeyframes(const KeyframeValueList& valueList, const Animation* animation, const String& animationName, Seconds timeOffset, const FloatSize& boxSize)
3078 {
3079     ASSERT(valueList.property() == AnimatedPropertyTransform);
3080
3081     bool hasBigRotation;
3082     int listIndex = validateTransformOperations(valueList, hasBigRotation);
3083     const TransformOperations* operations = (listIndex >= 0) ? &static_cast<const TransformAnimationValue&>(valueList.at(listIndex)).value() : 0;
3084
3085     bool validMatrices = true;
3086
3087     // If function lists don't match we do a matrix animation, otherwise we do a component hardware animation.
3088     bool isMatrixAnimation = listIndex < 0;
3089     int numAnimations = isMatrixAnimation ? 1 : operations->size();
3090
3091 #if !PLATFORM(WIN) && !HAVE(CA_WHERE_ADDITIVE_TRANSFORMS_ARE_REVERSED)
3092     for (int animationIndex = 0; animationIndex < numAnimations; ++animationIndex) {
3093 #else
3094     // Some versions of CA require animation lists to be applied in reverse order (<rdar://problem/43908047> and <rdar://problem/9112233>).
3095     for (int animationIndex = numAnimations - 1; animationIndex >= 0; --animationIndex) {
3096 #endif
3097         if (!appendToUncommittedAnimations(valueList, operations, animation, animationName, boxSize, animationIndex, timeOffset, isMatrixAnimation)) {
3098             validMatrices = false;
3099             break;
3100         }
3101     }
3102
3103     return validMatrices;
3104 }
3105
3106 bool GraphicsLayerCA::appendToUncommittedAnimations(const KeyframeValueList& valueList, const FilterOperation* operation, const Animation* animation, const String& animationName, int animationIndex, Seconds timeOffset)
3107 {
3108     bool isKeyframe = valueList.size() > 2;
3109     
3110     FilterOperation::OperationType filterOp = operation->type();
3111     int numAnimatedProperties = PlatformCAFilters::numAnimatedFilterProperties(filterOp);
3112     
3113     // Each filter might need to animate multiple properties, each with their own keyPath. The keyPath is always of the form:
3114     //
3115     //      filter.filter_<animationIndex>.<filterPropertyName>
3116     //
3117     // PlatformCAAnimation tells us how many properties each filter has and we iterate that many times and create an animation
3118     // for each. This internalFilterPropertyIndex gets passed to PlatformCAAnimation so it can properly create the property animation
3119     // values.
3120     for (int internalFilterPropertyIndex = 0; internalFilterPropertyIndex < numAnimatedProperties; ++internalFilterPropertyIndex) {
3121         bool valuesOK;
3122         RefPtr<PlatformCAAnimation> caAnimation;
3123         String keyPath = makeString("filters.filter_", animationIndex, '.', PlatformCAFilters::animatedFilterPropertyName(filterOp, internalFilterPropertyIndex));
3124         
3125         if (isKeyframe) {
3126             caAnimation = createKeyframeAnimation(animation, keyPath, false);
3127             valuesOK = setFilterAnimationKeyframes(valueList, animation, caAnimation.get(), animationIndex, internalFilterPropertyIndex, filterOp);
3128         } else {
3129             caAnimation = createBasicAnimation(animation, keyPath, false);
3130             valuesOK = setFilterAnimationEndpoints(valueList, animation, caAnimation.get(), animationIndex, internalFilterPropertyIndex);
3131         }
3132         
3133         ASSERT(valuesOK);
3134
3135         appendToUncommittedAnimations(LayerPropertyAnimation(caAnimation.releaseNonNull(), animationName, valueList.property(), animationIndex, internalFilterPropertyIndex, timeOffset));
3136     }
3137
3138     return true;
3139 }
3140
3141 void GraphicsLayerCA::appendToUncommittedAnimations(LayerPropertyAnimation&& animation)
3142 {
3143     ensureLayerAnimations();
3144
3145     // Since we're adding a new animation, make sure we clear any pending AnimationProcessingAction for this animation
3146     // as these are applied after we've committed new animations.
3147     m_animations->animationsToProcess.remove(animation.m_name);
3148
3149     m_animations->uncomittedAnimations.append(WTFMove(animation));
3150 }
3151
3152 bool GraphicsLayerCA::createFilterAnimationsFromKeyframes(const KeyframeValueList& valueList, const Animation* animation, const String& animationName, Seconds timeOffset)
3153 {
3154 #if ENABLE(FILTERS_LEVEL_2)
3155     ASSERT(valueList.property() == AnimatedPropertyFilter || valueList.property() == AnimatedPropertyWebkitBackdropFilter);
3156 #else
3157     ASSERT(valueList.property() == AnimatedPropertyFilter);
3158 #endif
3159
3160     int listIndex = validateFilterOperations(valueList);
3161     if (listIndex < 0)
3162         return false;
3163
3164     const FilterOperations& operations = static_cast<const FilterAnimationValue&>(valueList.at(listIndex)).value();
3165     // Make sure the platform layer didn't fallback to using software filter compositing instead.
3166     if (!filtersCanBeComposited(operations))
3167         return false;
3168
3169     int numAnimations = operations.size();
3170
3171     // FIXME: We can't currently hardware animate shadows.
3172     for (int i = 0; i < numAnimations; ++i) {
3173         if (operations.at(i)->type() == FilterOperation::DROP_SHADOW)
3174             return false;
3175     }
3176
3177     for (int animationIndex = 0; animationIndex < numAnimations; ++animationIndex) {
3178         if (!appendToUncommittedAnimations(valueList, operations.operations().at(animationIndex).get(), animation, animationName, animationIndex, timeOffset))
3179             return false;
3180     }
3181
3182     return true;
3183 }
3184
3185 Ref<PlatformCAAnimation> GraphicsLayerCA::createBasicAnimation(const Animation* anim, const String& keyPath, bool additive)
3186 {
3187     auto basicAnim = createPlatformCAAnimation(PlatformCAAnimation::Basic, keyPath);
3188     setupAnimation(basicAnim.ptr(), anim, additive);
3189     return basicAnim;
3190 }
3191
3192 Ref<PlatformCAAnimation> GraphicsLayerCA::createKeyframeAnimation(const Animation* anim, const String& keyPath, bool additive)
3193 {
3194     auto keyframeAnim = createPlatformCAAnimation(PlatformCAAnimation::Keyframe, keyPath);
3195     setupAnimation(keyframeAnim.ptr(), anim, additive);
3196     return keyframeAnim;
3197 }
3198
3199 Ref<PlatformCAAnimation> GraphicsLayerCA::createSpringAnimation(const Animation* anim, const String& keyPath, bool additive)
3200 {
3201     auto basicAnim = createPlatformCAAnimation(PlatformCAAnimation::Spring, keyPath);
3202     setupAnimation(basicAnim.ptr(), anim, additive);
3203     return basicAnim;
3204 }
3205
3206 void GraphicsLayerCA::setupAnimation(PlatformCAAnimation* propertyAnim, const Animation* anim, bool additive)
3207 {
3208     double duration = anim->duration();
3209     if (duration <= 0)
3210         duration = cAnimationAlmostZeroDuration;
3211
3212     float repeatCount = anim->iterationCount();
3213     if (repeatCount == Animation::IterationCountInfinite)
3214         repeatCount = std::numeric_limits<float>::max();
3215     else if (anim->direction() == Animation::AnimationDirectionAlternate || anim->direction() == Animation::AnimationDirectionAlternateReverse)
3216         repeatCount /= 2;
3217
3218     PlatformCAAnimation::FillModeType fillMode = PlatformCAAnimation::NoFillMode;
3219     switch (anim->fillMode()) {
3220     case AnimationFillMode::None:
3221         fillMode = PlatformCAAnimation::Forwards; // Use "forwards" rather than "removed" because the style system will remove the animation when it is finished. This avoids a flash.
3222         break;
3223     case AnimationFillMode::Backwards:
3224         fillMode = PlatformCAAnimation::Both; // Use "both" rather than "backwards" because the style system will remove the animation when it is finished. This avoids a flash.
3225         break;
3226     case AnimationFillMode::Forwards:
3227         fillMode = PlatformCAAnimation::Forwards;
3228         break;
3229     case AnimationFillMode::Both:
3230         fillMode = PlatformCAAnimation::Both;
3231         break;
3232     }
3233
3234     propertyAnim->setDuration(duration);
3235     propertyAnim->setRepeatCount(repeatCount);
3236     propertyAnim->setAutoreverses(anim->direction() == Animation::AnimationDirectionAlternate || anim->direction() == Animation::AnimationDirectionAlternateReverse);
3237     propertyAnim->setRemovedOnCompletion(false);
3238     propertyAnim->setAdditive(additive);
3239     propertyAnim->setFillMode(fillMode);
3240 }
3241
3242 const TimingFunction& GraphicsLayerCA::timingFunctionForAnimationValue(const AnimationValue& animValue, const Animation& anim)
3243 {
3244     if (animValue.timingFunction())
3245         return *animValue.timingFunction();
3246     if (anim.isTimingFunctionSet())
3247         return *anim.timingFunction();
3248     return CubicBezierTimingFunction::defaultTimingFunction();
3249 }
3250
3251 bool GraphicsLayerCA::setAnimationEndpoints(const KeyframeValueList& valueList, const Animation* animation, PlatformCAAnimation* basicAnim)
3252 {
3253     bool forwards = animation->directionIsForwards();
3254
3255     unsigned fromIndex = !forwards;
3256     unsigned toIndex = forwards;
3257     
3258     switch (valueList.property()) {
3259     case AnimatedPropertyOpacity: {
3260         basicAnim->setFromValue(static_cast<const FloatAnimationValue&>(valueList.at(fromIndex)).value());
3261         basicAnim->setToValue(static_cast<const FloatAnimationValue&>(valueList.at(toIndex)).value());
3262         break;
3263     }
3264     default:
3265         ASSERT_NOT_REACHED(); // we don't animate color yet
3266         break;
3267     }
3268
3269     // This codepath is used for 2-keyframe animations, so we still need to look in the start
3270     // for a timing function. Even in the reversing animation case, the first keyframe provides the timing function.
3271     basicAnim->setTimingFunction(&timingFunctionForAnimationValue(valueList.at(0), *animation), !forwards);
3272
3273     return true;
3274 }
3275
3276 bool GraphicsLayerCA::setAnimationKeyframes(const KeyframeValueList& valueList, const Animation* animation, PlatformCAAnimation* keyframeAnim)
3277 {
3278     Vector<float> keyTimes;
3279     Vector<float> values;
3280     Vector<const TimingFunction*> timingFunctions;
3281
3282     bool forwards = animation->directionIsForwards();
3283     
3284     for (unsigned i = 0; i < valueList.size(); ++i) {
3285         unsigned index = forwards ? i : (valueList.size() - i - 1);
3286         const AnimationValue& curValue = valueList.at(index);
3287         keyTimes.append(forwards ? curValue.keyTime() : (1 - curValue.keyTime()));
3288
3289         switch (valueList.property()) {
3290         case AnimatedPropertyOpacity: {
3291             const FloatAnimationValue& floatValue = static_cast<const FloatAnimationValue&>(curValue);
3292             values.append(floatValue.value());
3293             break;
3294         }
3295         default:
3296             ASSERT_NOT_REACHED(); // we don't animate color yet
3297             break;
3298         }
3299
3300         if (i < (valueList.size() - 1))
3301             timingFunctions.append(&timingFunctionForAnimationValue(forwards ? curValue : valueList.at(index - 1), *animation));
3302     }
3303
3304     keyframeAnim->setKeyTimes(keyTimes);
3305     keyframeAnim->setValues(values);
3306     keyframeAnim->setTimingFunctions(timingFunctions, !forwards);
3307
3308     return true;
3309 }
3310
3311 bool GraphicsLayerCA::setTransformAnimationEndpoints(const KeyframeValueList& valueList, const Animation* animation, PlatformCAAnimation* basicAnim, int functionIndex, TransformOperation::OperationType transformOpType, bool isMatrixAnimation, const FloatSize& boxSize)
3312 {
3313     ASSERT(valueList.size() == 2);
3314
3315     bool forwards = animation->directionIsForwards();
3316     
3317     unsigned fromIndex = !forwards;
3318     unsigned toIndex = forwards;
3319     
3320     auto& startValue = static_cast<const TransformAnimationValue&>(valueList.at(fromIndex));
3321     auto& endValue = static_cast<const TransformAnimationValue&>(valueList.at(toIndex));
3322
3323     if (isMatrixAnimation) {
3324         TransformationMatrix fromTransform, toTransform;
3325         startValue.value().apply(boxSize, fromTransform);
3326         endValue.value().apply(boxSize, toTransform);
3327
3328         // If any matrix is singular, CA won't animate it correctly. So fall back to software animation