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