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