[UI-side compositing] Assertion in PlatformCAFilters::setFiltersOnLayer with animated...
[WebKit-https.git] / Source / WebKit2 / Shared / mac / RemoteLayerTreeTransaction.mm
1 /*
2  * Copyright (C) 2012 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "RemoteLayerTreeTransaction.h"
28
29 #import "ArgumentCoders.h"
30 #import "MessageDecoder.h"
31 #import "MessageEncoder.h"
32 #import "PlatformCAAnimationRemote.h"
33 #import "PlatformCALayerRemote.h"
34 #import "WebCoreArgumentCoders.h"
35 #import <QuartzCore/QuartzCore.h>
36 #import <WebCore/LengthFunctions.h>
37 #import <WebCore/TextStream.h>
38 #import <WebCore/TimingFunction.h>
39 #import <wtf/text/CString.h>
40 #import <wtf/text/StringBuilder.h>
41
42 using namespace WebCore;
43
44 namespace WebKit {
45
46 RemoteLayerTreeTransaction::LayerCreationProperties::LayerCreationProperties()
47     : layerID(0)
48     , type(PlatformCALayer::LayerTypeLayer)
49     , hostingContextID(0)
50 {
51 }
52
53 void RemoteLayerTreeTransaction::LayerCreationProperties::encode(IPC::ArgumentEncoder& encoder) const
54 {
55     encoder << layerID;
56     encoder.encodeEnum(type);
57
58     if (type == PlatformCALayer::LayerTypeCustom)
59         encoder << hostingContextID;
60 }
61
62 bool RemoteLayerTreeTransaction::LayerCreationProperties::decode(IPC::ArgumentDecoder& decoder, LayerCreationProperties& result)
63 {
64     if (!decoder.decode(result.layerID))
65         return false;
66
67     if (!decoder.decodeEnum(result.type))
68         return false;
69
70     if (result.type == PlatformCALayer::LayerTypeCustom) {
71         if (!decoder.decode(result.hostingContextID))
72             return false;
73     }
74
75     return true;
76 }
77
78 RemoteLayerTreeTransaction::LayerProperties::LayerProperties()
79     : changedProperties(NoChange)
80     , everChangedProperties(NoChange)
81     , anchorPoint(0.5, 0.5, 0)
82     , contentsRect(FloatPoint(), FloatSize(1, 1))
83     , maskLayerID(0)
84     , timeOffset(0)
85     , speed(1)
86     , contentsScale(1)
87     , borderWidth(0)
88     , opacity(1)
89     , backgroundColor(Color::transparent)
90     , borderColor(Color::black)
91     , edgeAntialiasingMask(kCALayerLeftEdge | kCALayerRightEdge | kCALayerBottomEdge | kCALayerTopEdge)
92     , customAppearance(GraphicsLayer::NoCustomAppearance)
93     , customBehavior(GraphicsLayer::NoCustomBehavior)
94     , minificationFilter(PlatformCALayer::FilterType::Linear)
95     , magnificationFilter(PlatformCALayer::FilterType::Linear)
96     , blendMode(BlendModeNormal)
97     , hidden(false)
98     , geometryFlipped(false)
99     , doubleSided(true)
100     , masksToBounds(false)
101     , opaque(false)
102 {
103 }
104
105 RemoteLayerTreeTransaction::LayerProperties::LayerProperties(const LayerProperties& other)
106     : changedProperties(other.changedProperties)
107     , everChangedProperties(other.everChangedProperties)
108     , name(other.name)
109     , children(other.children)
110     , addedAnimations(other.addedAnimations)
111     , keyPathsOfAnimationsToRemove(other.keyPathsOfAnimationsToRemove)
112     , position(other.position)
113     , anchorPoint(other.anchorPoint)
114     , bounds(other.bounds)
115     , contentsRect(other.contentsRect)
116     , maskLayerID(other.maskLayerID)
117     , timeOffset(other.timeOffset)
118     , speed(other.speed)
119     , contentsScale(other.contentsScale)
120     , borderWidth(other.borderWidth)
121     , opacity(other.opacity)
122     , backgroundColor(other.backgroundColor)
123     , borderColor(other.borderColor)
124     , edgeAntialiasingMask(other.edgeAntialiasingMask)
125     , customAppearance(other.customAppearance)
126     , customBehavior(other.customBehavior)
127     , minificationFilter(other.minificationFilter)
128     , magnificationFilter(other.magnificationFilter)
129     , blendMode(other.blendMode)
130     , hidden(other.hidden)
131     , geometryFlipped(other.geometryFlipped)
132     , doubleSided(other.doubleSided)
133     , masksToBounds(other.masksToBounds)
134     , opaque(other.opaque)
135 {
136     // FIXME: LayerProperties should reference backing store by ID, so that two layers can have the same backing store (for clones).
137     // FIXME: LayerProperties shouldn't be copyable; PlatformCALayerRemote::clone should copy the relevant properties.
138
139     if (other.transform)
140         transform = std::make_unique<TransformationMatrix>(*other.transform);
141
142     if (other.sublayerTransform)
143         sublayerTransform = std::make_unique<TransformationMatrix>(*other.sublayerTransform);
144
145     if (other.filters)
146         filters = std::make_unique<FilterOperations>(*other.filters);
147 }
148
149 void RemoteLayerTreeTransaction::LayerProperties::encode(IPC::ArgumentEncoder& encoder) const
150 {
151     encoder.encodeEnum(changedProperties);
152
153     if (changedProperties & NameChanged)
154         encoder << name;
155
156     if (changedProperties & ChildrenChanged)
157         encoder << children;
158
159     if (changedProperties & AnimationsChanged) {
160         encoder << addedAnimations;
161         encoder << keyPathsOfAnimationsToRemove;
162     }
163
164     if (changedProperties & PositionChanged)
165         encoder << position;
166
167     if (changedProperties & BoundsChanged)
168         encoder << bounds;
169
170     if (changedProperties & BackgroundColorChanged)
171         encoder << backgroundColor;
172
173     if (changedProperties & AnchorPointChanged)
174         encoder << anchorPoint;
175
176     if (changedProperties & BorderWidthChanged)
177         encoder << borderWidth;
178
179     if (changedProperties & BorderColorChanged)
180         encoder << borderColor;
181
182     if (changedProperties & OpacityChanged)
183         encoder << opacity;
184
185     if (changedProperties & TransformChanged)
186         encoder << *transform;
187
188     if (changedProperties & SublayerTransformChanged)
189         encoder << *sublayerTransform;
190
191     if (changedProperties & HiddenChanged)
192         encoder << hidden;
193
194     if (changedProperties & GeometryFlippedChanged)
195         encoder << geometryFlipped;
196
197     if (changedProperties & DoubleSidedChanged)
198         encoder << doubleSided;
199
200     if (changedProperties & MasksToBoundsChanged)
201         encoder << masksToBounds;
202
203     if (changedProperties & OpaqueChanged)
204         encoder << opaque;
205
206     if (changedProperties & MaskLayerChanged)
207         encoder << maskLayerID;
208
209     if (changedProperties & ContentsRectChanged)
210         encoder << contentsRect;
211
212     if (changedProperties & ContentsScaleChanged)
213         encoder << contentsScale;
214
215     if (changedProperties & MinificationFilterChanged)
216         encoder.encodeEnum(minificationFilter);
217
218     if (changedProperties & MagnificationFilterChanged)
219         encoder.encodeEnum(magnificationFilter);
220
221     if (changedProperties & BlendModeChanged)
222         encoder.encodeEnum(blendMode);
223
224     if (changedProperties & SpeedChanged)
225         encoder << speed;
226
227     if (changedProperties & TimeOffsetChanged)
228         encoder << timeOffset;
229
230     if (changedProperties & BackingStoreChanged) {
231         bool hasFrontBuffer = backingStore && backingStore->hasFrontBuffer();
232         encoder << hasFrontBuffer;
233         if (hasFrontBuffer)
234             encoder << *backingStore;
235     }
236
237     if (changedProperties & FiltersChanged)
238         encoder << *filters;
239
240     if (changedProperties & EdgeAntialiasingMaskChanged)
241         encoder << edgeAntialiasingMask;
242
243     if (changedProperties & CustomAppearanceChanged)
244         encoder.encodeEnum(customAppearance);
245
246     if (changedProperties & CustomBehaviorChanged)
247         encoder.encodeEnum(customBehavior);
248 }
249
250 bool RemoteLayerTreeTransaction::LayerProperties::decode(IPC::ArgumentDecoder& decoder, LayerProperties& result)
251 {
252     if (!decoder.decodeEnum(result.changedProperties))
253         return false;
254
255     if (result.changedProperties & NameChanged) {
256         if (!decoder.decode(result.name))
257             return false;
258     }
259
260     if (result.changedProperties & ChildrenChanged) {
261         if (!decoder.decode(result.children))
262             return false;
263
264         for (auto& layerID : result.children) {
265             if (!layerID)
266                 return false;
267         }
268     }
269
270     if (result.changedProperties & AnimationsChanged) {
271         if (!decoder.decode(result.addedAnimations))
272             return false;
273
274         if (!decoder.decode(result.keyPathsOfAnimationsToRemove))
275             return false;
276     }
277
278     if (result.changedProperties & PositionChanged) {
279         if (!decoder.decode(result.position))
280             return false;
281     }
282
283     if (result.changedProperties & BoundsChanged) {
284         if (!decoder.decode(result.bounds))
285             return false;
286     }
287
288     if (result.changedProperties & BackgroundColorChanged) {
289         if (!decoder.decode(result.backgroundColor))
290             return false;
291     }
292
293     if (result.changedProperties & AnchorPointChanged) {
294         if (!decoder.decode(result.anchorPoint))
295             return false;
296     }
297
298     if (result.changedProperties & BorderWidthChanged) {
299         if (!decoder.decode(result.borderWidth))
300             return false;
301     }
302
303     if (result.changedProperties & BorderColorChanged) {
304         if (!decoder.decode(result.borderColor))
305             return false;
306     }
307
308     if (result.changedProperties & OpacityChanged) {
309         if (!decoder.decode(result.opacity))
310             return false;
311     }
312
313     if (result.changedProperties & TransformChanged) {
314         TransformationMatrix transform;
315         if (!decoder.decode(transform))
316             return false;
317         
318         result.transform = std::make_unique<TransformationMatrix>(transform);
319     }
320
321     if (result.changedProperties & SublayerTransformChanged) {
322         TransformationMatrix transform;
323         if (!decoder.decode(transform))
324             return false;
325
326         result.sublayerTransform = std::make_unique<TransformationMatrix>(transform);
327     }
328
329     if (result.changedProperties & HiddenChanged) {
330         if (!decoder.decode(result.hidden))
331             return false;
332     }
333
334     if (result.changedProperties & GeometryFlippedChanged) {
335         if (!decoder.decode(result.geometryFlipped))
336             return false;
337     }
338
339     if (result.changedProperties & DoubleSidedChanged) {
340         if (!decoder.decode(result.doubleSided))
341             return false;
342     }
343
344     if (result.changedProperties & MasksToBoundsChanged) {
345         if (!decoder.decode(result.masksToBounds))
346             return false;
347     }
348
349     if (result.changedProperties & OpaqueChanged) {
350         if (!decoder.decode(result.opaque))
351             return false;
352     }
353
354     if (result.changedProperties & MaskLayerChanged) {
355         if (!decoder.decode(result.maskLayerID))
356             return false;
357     }
358
359     if (result.changedProperties & ContentsRectChanged) {
360         if (!decoder.decode(result.contentsRect))
361             return false;
362     }
363
364     if (result.changedProperties & ContentsScaleChanged) {
365         if (!decoder.decode(result.contentsScale))
366             return false;
367     }
368
369     if (result.changedProperties & MinificationFilterChanged) {
370         if (!decoder.decodeEnum(result.minificationFilter))
371             return false;
372     }
373
374     if (result.changedProperties & MagnificationFilterChanged) {
375         if (!decoder.decodeEnum(result.magnificationFilter))
376             return false;
377     }
378
379     if (result.changedProperties & BlendModeChanged) {
380         if (!decoder.decodeEnum(result.blendMode))
381             return false;
382     }
383
384     if (result.changedProperties & SpeedChanged) {
385         if (!decoder.decode(result.speed))
386             return false;
387     }
388
389     if (result.changedProperties & TimeOffsetChanged) {
390         if (!decoder.decode(result.timeOffset))
391             return false;
392     }
393
394     if (result.changedProperties & BackingStoreChanged) {
395         bool hasFrontBuffer = false;
396         if (!decoder.decode(hasFrontBuffer))
397             return false;
398         if (hasFrontBuffer) {
399             std::unique_ptr<RemoteLayerBackingStore> backingStore = std::make_unique<RemoteLayerBackingStore>(nullptr);
400             if (!decoder.decode(*backingStore))
401                 return false;
402             
403             result.backingStore = std::move(backingStore);
404         } else
405             result.backingStore = nullptr;
406     }
407
408     if (result.changedProperties & FiltersChanged) {
409         std::unique_ptr<FilterOperations> filters = std::make_unique<FilterOperations>();
410         if (!decoder.decode(*filters))
411             return false;
412         result.filters = std::move(filters);
413     }
414
415     if (result.changedProperties & EdgeAntialiasingMaskChanged) {
416         if (!decoder.decode(result.edgeAntialiasingMask))
417             return false;
418     }
419
420     if (result.changedProperties & CustomAppearanceChanged) {
421         if (!decoder.decodeEnum(result.customAppearance))
422             return false;
423     }
424
425     if (result.changedProperties & CustomBehaviorChanged) {
426         if (!decoder.decodeEnum(result.customBehavior))
427             return false;
428     }
429
430     return true;
431 }
432
433 RemoteLayerTreeTransaction::RemoteLayerTreeTransaction()
434 {
435 }
436
437 RemoteLayerTreeTransaction::~RemoteLayerTreeTransaction()
438 {
439 }
440
441 void RemoteLayerTreeTransaction::encode(IPC::ArgumentEncoder& encoder) const
442 {
443     encoder << m_rootLayerID;
444     encoder << m_createdLayers;
445
446     encoder << static_cast<uint64_t>(m_changedLayers.size());
447
448     for (RefPtr<PlatformCALayerRemote> layer : m_changedLayers) {
449         encoder << layer->layerID();
450         encoder << layer->properties();
451     }
452     
453     encoder << m_destroyedLayerIDs;
454     encoder << m_videoLayerIDsPendingFullscreen;
455
456     encoder << m_contentsSize;
457     encoder << m_pageExtendedBackgroundColor;
458     encoder << m_pageScaleFactor;
459     encoder << m_minimumScaleFactor;
460     encoder << m_maximumScaleFactor;
461
462     encoder << m_lastVisibleContentRectUpdateID;
463     encoder << m_renderTreeSize;
464
465     encoder << m_scaleWasSetByUIProcess;
466     encoder << m_allowsUserScaling;
467 }
468
469 bool RemoteLayerTreeTransaction::decode(IPC::ArgumentDecoder& decoder, RemoteLayerTreeTransaction& result)
470 {
471     if (!decoder.decode(result.m_rootLayerID))
472         return false;
473     if (!result.m_rootLayerID)
474         return false;
475
476     if (!decoder.decode(result.m_createdLayers))
477         return false;
478
479     uint64_t numChangedLayerProperties;
480     if (!decoder.decode(numChangedLayerProperties))
481         return false;
482
483     for (uint64_t i = 0; i < numChangedLayerProperties; ++i) {
484         GraphicsLayer::PlatformLayerID layerID;
485         if (!decoder.decode(layerID))
486             return false;
487
488         std::unique_ptr<LayerProperties> layerProperties = std::make_unique<LayerProperties>();
489         if (!decoder.decode(*layerProperties))
490             return false;
491
492         result.changedLayerProperties().set(layerID, std::move(layerProperties));
493     }
494
495     if (!decoder.decode(result.m_destroyedLayerIDs))
496         return false;
497
498     for (auto& layerID : result.m_destroyedLayerIDs) {
499         if (!layerID)
500             return false;
501     }
502
503     if (!decoder.decode(result.m_videoLayerIDsPendingFullscreen))
504         return false;
505
506     if (!decoder.decode(result.m_contentsSize))
507         return false;
508     
509     if (!decoder.decode(result.m_pageExtendedBackgroundColor))
510         return false;
511
512     if (!decoder.decode(result.m_pageScaleFactor))
513         return false;
514
515     if (!decoder.decode(result.m_minimumScaleFactor))
516         return false;
517
518     if (!decoder.decode(result.m_maximumScaleFactor))
519         return false;
520
521     if (!decoder.decode(result.m_lastVisibleContentRectUpdateID))
522         return false;
523
524     if (!decoder.decode(result.m_renderTreeSize))
525         return false;
526
527     if (!decoder.decode(result.m_scaleWasSetByUIProcess))
528         return false;
529
530     if (!decoder.decode(result.m_allowsUserScaling))
531         return false;
532
533     return true;
534 }
535
536 void RemoteLayerTreeTransaction::setRootLayerID(GraphicsLayer::PlatformLayerID rootLayerID)
537 {
538     ASSERT_ARG(rootLayerID, rootLayerID);
539
540     m_rootLayerID = rootLayerID;
541 }
542
543 void RemoteLayerTreeTransaction::layerPropertiesChanged(PlatformCALayerRemote* remoteLayer, RemoteLayerTreeTransaction::LayerProperties& properties)
544 {
545     m_changedLayers.append(remoteLayer);
546 }
547
548 void RemoteLayerTreeTransaction::setCreatedLayers(Vector<LayerCreationProperties> createdLayers)
549 {
550     m_createdLayers = std::move(createdLayers);
551 }
552
553 void RemoteLayerTreeTransaction::setDestroyedLayerIDs(Vector<GraphicsLayer::PlatformLayerID> destroyedLayerIDs)
554 {
555     m_destroyedLayerIDs = std::move(destroyedLayerIDs);
556 }
557
558 #if !defined(NDEBUG) || !LOG_DISABLED
559
560 class RemoteLayerTreeTextStream : public TextStream
561 {
562 public:
563     using TextStream::operator<<;
564
565     RemoteLayerTreeTextStream()
566         : m_indent(0)
567     {
568     }
569
570     RemoteLayerTreeTextStream& operator<<(const TransformationMatrix&);
571     RemoteLayerTreeTextStream& operator<<(PlatformCALayer::FilterType);
572     RemoteLayerTreeTextStream& operator<<(FloatPoint3D);
573     RemoteLayerTreeTextStream& operator<<(Color);
574     RemoteLayerTreeTextStream& operator<<(FloatRect);
575     RemoteLayerTreeTextStream& operator<<(const Vector<WebCore::GraphicsLayer::PlatformLayerID>&);
576     RemoteLayerTreeTextStream& operator<<(const FilterOperation&);
577     RemoteLayerTreeTextStream& operator<<(const FilterOperations&);
578     RemoteLayerTreeTextStream& operator<<(const PlatformCAAnimationRemote::Properties&);
579     RemoteLayerTreeTextStream& operator<<(const RemoteLayerBackingStore&);
580     RemoteLayerTreeTextStream& operator<<(BlendMode);
581     RemoteLayerTreeTextStream& operator<<(PlatformCAAnimation::AnimationType);
582     RemoteLayerTreeTextStream& operator<<(PlatformCAAnimation::FillModeType);
583     RemoteLayerTreeTextStream& operator<<(PlatformCAAnimation::ValueFunctionType);
584     RemoteLayerTreeTextStream& operator<<(const TimingFunction&);
585     RemoteLayerTreeTextStream& operator<<(const PlatformCAAnimationRemote::KeyframeValue&);
586
587     void increaseIndent() { ++m_indent; }
588     void decreaseIndent() { --m_indent; ASSERT(m_indent >= 0); }
589
590     void writeIndent();
591
592 private:
593     int m_indent;
594 };
595
596 template <class T>
597 static void dumpProperty(RemoteLayerTreeTextStream& ts, String name, T value)
598 {
599     ts << "\n";
600     ts.increaseIndent();
601     ts.writeIndent();
602     ts << "(" << name << " ";
603     ts << value << ")";
604     ts.decreaseIndent();
605 }
606
607 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const TransformationMatrix& transform)
608 {
609     RemoteLayerTreeTextStream& ts = *this;
610     ts << "\n";
611     ts.increaseIndent();
612     ts.writeIndent();
613     ts << "[" << transform.m11() << " " << transform.m12() << " " << transform.m13() << " " << transform.m14() << "]\n";
614     ts.writeIndent();
615     ts << "[" << transform.m21() << " " << transform.m22() << " " << transform.m23() << " " << transform.m24() << "]\n";
616     ts.writeIndent();
617     ts << "[" << transform.m31() << " " << transform.m32() << " " << transform.m33() << " " << transform.m34() << "]\n";
618     ts.writeIndent();
619     ts << "[" << transform.m41() << " " << transform.m42() << " " << transform.m43() << " " << transform.m44() << "]";
620     ts.decreaseIndent();
621     return ts;
622 }
623
624 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCALayer::FilterType filterType)
625 {
626     RemoteLayerTreeTextStream& ts = *this;
627     switch (filterType) {
628     case PlatformCALayer::Linear:
629         ts << "linear";
630         break;
631     case PlatformCALayer::Nearest:
632         ts << "nearest";
633         break;
634     case PlatformCALayer::Trilinear:
635         ts << "trilinear";
636         break;
637     default:
638         ASSERT_NOT_REACHED();
639         break;
640     }
641     return ts;
642 }
643
644 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const FilterOperations& filters)
645 {
646     RemoteLayerTreeTextStream& ts = *this;
647     for (size_t i = 0; i < filters.size(); ++i) {
648         const auto filter = filters.at(i);
649         if (filter)
650             ts << *filter;
651         else
652             ts << "(null)";
653         if (i < filters.size() - 1)
654             ts << " ";
655     }
656     return ts;
657 }
658     
659 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const FilterOperation& filter)
660 {
661     RemoteLayerTreeTextStream& ts = *this;
662     switch (filter.type()) {
663     case FilterOperation::REFERENCE:
664         ts << "reference";
665         break;
666     case FilterOperation::GRAYSCALE: {
667         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
668         ts << "grayscale(" << colorMatrixFilter.amount() << ")";
669         break;
670     }
671     case FilterOperation::SEPIA: {
672         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
673         ts << "sepia(" << colorMatrixFilter.amount() << ")";
674         break;
675     }
676     case FilterOperation::SATURATE: {
677         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
678         ts << "saturate(" << colorMatrixFilter.amount() << ")";
679         break;
680     }
681     case FilterOperation::HUE_ROTATE: {
682         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
683         ts << "hue-rotate(" << colorMatrixFilter.amount() << ")";
684         break;
685     }
686     case FilterOperation::INVERT: {
687         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
688         ts << "invert(" << componentTransferFilter.amount() << ")";
689         break;
690     }
691     case FilterOperation::OPACITY: {
692         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
693         ts << "opacity(" << componentTransferFilter.amount() << ")";
694         break;
695     }
696     case FilterOperation::BRIGHTNESS: {
697         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
698         ts << "brightness(" << componentTransferFilter.amount() << ")";
699         break;
700     }
701     case FilterOperation::CONTRAST: {
702         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
703         ts << "contrast(" << componentTransferFilter.amount() << ")";
704         break;
705     }
706     case FilterOperation::BLUR: {
707         const BlurFilterOperation& blurFilter = toBlurFilterOperation(filter);
708         ts << "blur(" << floatValueForLength(blurFilter.stdDeviation(), 0) << ")";
709         break;
710     }
711     case FilterOperation::DROP_SHADOW: {
712         const DropShadowFilterOperation& dropShadowFilter = toDropShadowFilterOperation(filter);
713         ts << "drop-shadow(" << dropShadowFilter.x() << " " << dropShadowFilter.y() << " " << dropShadowFilter.location() << " ";
714         ts << dropShadowFilter.color() << ")";
715         break;
716     }
717     case FilterOperation::PASSTHROUGH:
718         ts << "passthrough";
719         break;
720     case FilterOperation::DEFAULT: {
721         const DefaultFilterOperation& defaultFilter = toDefaultFilterOperation(filter);
722         ts << "default type=" << (int)defaultFilter.representedType();
723         break;
724     }
725     case FilterOperation::NONE:
726         ts << "none";
727         break;
728     }
729     return ts;
730 }
731
732 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(BlendMode blendMode)
733 {
734     RemoteLayerTreeTextStream& ts = *this;
735     switch (blendMode) {
736     case BlendModeNormal: ts << "normal"; break;
737     case BlendModeMultiply: ts << "multiply"; break;
738     case BlendModeScreen: ts << "screen"; break;
739     case BlendModeOverlay: ts << "overlay"; break;
740     case BlendModeDarken: ts << "darken"; break;
741     case BlendModeLighten: ts << "lighten"; break;
742     case BlendModeColorDodge: ts << "color-dodge"; break;
743     case BlendModeColorBurn: ts << "color-burn"; break;
744     case BlendModeHardLight: ts << "hard-light"; break;
745     case BlendModeSoftLight: ts << "soft-light"; break;
746     case BlendModeDifference: ts << "difference"; break;
747     case BlendModeExclusion: ts << "exclusion"; break;
748     case BlendModeHue: ts << "hue"; break;
749     case BlendModeSaturation: ts << "saturation"; break;
750     case BlendModeColor: ts << "color"; break;
751     case BlendModeLuminosity: ts << "luminosity"; break;
752     }
753     return ts;
754 }
755
756 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCAAnimation::AnimationType type)
757 {
758     RemoteLayerTreeTextStream& ts = *this;
759     switch (type) {
760     case PlatformCAAnimation::Basic: ts << "basic"; break;
761     case PlatformCAAnimation::Keyframe: ts << "keyframe"; break;
762     }
763     return ts;
764 }
765
766 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCAAnimation::FillModeType type)
767 {
768     RemoteLayerTreeTextStream& ts = *this;
769     switch (type) {
770     case PlatformCAAnimation::NoFillMode: ts << "none"; break;
771     case PlatformCAAnimation::Forwards: ts << "forwards"; break;
772     case PlatformCAAnimation::Backwards: ts << "backwards"; break;
773     case PlatformCAAnimation::Both: ts << "both"; break;
774     }
775     return ts;
776 }
777
778 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCAAnimation::ValueFunctionType type)
779 {
780     RemoteLayerTreeTextStream& ts = *this;
781     switch (type) {
782     case PlatformCAAnimation::NoValueFunction: ts << "none"; break;
783     case PlatformCAAnimation::RotateX: ts << "rotateX"; break;
784     case PlatformCAAnimation::RotateY: ts << "rotateY"; break;
785     case PlatformCAAnimation::RotateZ: ts << "rotateX"; break;
786     case PlatformCAAnimation::ScaleX: ts << "scaleX"; break;
787     case PlatformCAAnimation::ScaleY: ts << "scaleY"; break;
788     case PlatformCAAnimation::ScaleZ: ts << "scaleX"; break;
789     case PlatformCAAnimation::Scale: ts << "scale"; break;
790     case PlatformCAAnimation::TranslateX: ts << "translateX"; break;
791     case PlatformCAAnimation::TranslateY: ts << "translateY"; break;
792     case PlatformCAAnimation::TranslateZ: ts << "translateZ"; break;
793     case PlatformCAAnimation::Translate: ts << "translate"; break;
794     }
795     return ts;
796 }
797
798 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const PlatformCAAnimationRemote::KeyframeValue& value)
799 {
800     RemoteLayerTreeTextStream& ts = *this;
801
802     switch (value.keyframeType()) {
803     case PlatformCAAnimationRemote::KeyframeValue::NumberKeyType:
804         ts << "number=" << value.numberValue();
805         break;
806     case PlatformCAAnimationRemote::KeyframeValue::ColorKeyType:
807         ts << "color=";
808         ts << value.colorValue();
809         break;
810     case PlatformCAAnimationRemote::KeyframeValue::PointKeyType:
811         ts << "point=";
812         ts << value.pointValue();
813         break;
814     case PlatformCAAnimationRemote::KeyframeValue::TransformKeyType:
815         ts << "transform=";
816         ts << value.transformValue();
817         break;
818     case PlatformCAAnimationRemote::KeyframeValue::FilterKeyType:
819         ts << "filter=";
820         if (value.filterValue())
821             ts << *value.filterValue();
822         else
823             ts << "null";
824         break;
825     }
826     return ts;
827 }
828
829 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const TimingFunction& timingFunction)
830 {
831     RemoteLayerTreeTextStream& ts = *this;
832     switch (timingFunction.type()) {
833     case TimingFunction::LinearFunction:
834         ts << "linear";
835         break;
836     case TimingFunction::CubicBezierFunction: {
837         const CubicBezierTimingFunction& cubicBezierFunction = static_cast<const CubicBezierTimingFunction&>(timingFunction);
838         ts << "cubic-bezier(" << cubicBezierFunction.x1() << ", " << cubicBezierFunction.y1() << ", " <<  cubicBezierFunction.x2() << ", " << cubicBezierFunction.y2() << ")";
839         break;
840     }
841     case TimingFunction::StepsFunction: {
842         const StepsTimingFunction& stepsFunction = static_cast<const StepsTimingFunction&>(timingFunction);
843         ts << "steps(" << stepsFunction.numberOfSteps() << ", " << (stepsFunction.stepAtStart() ? "start" : "end") << ")";
844         break;
845     }
846     }
847     return ts;
848 }
849
850 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const PlatformCAAnimationRemote::Properties& animation)
851 {
852     RemoteLayerTreeTextStream& ts = *this;
853
854     ts << "type=";
855     ts << animation.animationType;
856     ts << " keyPath=";
857     ts << animation.keyPath;
858
859     if (animation.beginTime)
860         dumpProperty(ts, "beginTime", animation.beginTime);
861
862     if (animation.duration)
863         dumpProperty(ts, "duration", animation.duration);
864
865     if (animation.timeOffset)
866         dumpProperty(ts, "timeOffset", animation.timeOffset);
867
868     dumpProperty(ts, "repeatCount", animation.repeatCount);
869
870     if (animation.speed != 1)
871         dumpProperty(ts, "speed", animation.speed);
872
873     dumpProperty(ts, "fillMode", animation.fillMode);
874     dumpProperty(ts, "valueFunction", animation.valueFunction);
875
876     if (animation.autoReverses)
877         dumpProperty(ts, "autoReverses", animation.autoReverses);
878
879     if (!animation.removedOnCompletion)
880         dumpProperty(ts, "removedOnCompletion", animation.removedOnCompletion);
881
882     if (animation.additive)
883         dumpProperty(ts, "additive", animation.additive);
884
885     if (animation.reverseTimingFunctions)
886         dumpProperty(ts, "reverseTimingFunctions", animation.reverseTimingFunctions);
887
888     if (animation.hasNonZeroBeginTime)
889         dumpProperty(ts, "hasNonZeroBeginTime", animation.hasNonZeroBeginTime);
890
891     ts << "\n";
892     ts.increaseIndent();
893     ts.writeIndent();
894     ts << "(" << "keyframes";
895     ts.increaseIndent();
896
897     size_t maxFrames = std::max(animation.keyValues.size(), animation.keyTimes.size());
898     maxFrames = std::max(maxFrames, animation.timingFunctions.size());
899
900     for (size_t i = 0; i < maxFrames; ++i) {
901         ts << "\n";
902         ts.writeIndent();
903         ts << "(keyframe " << unsigned(i);
904         if (i < animation.keyTimes.size())
905             dumpProperty(ts, "time", animation.keyTimes[i]);
906
907         if (i < animation.timingFunctions.size() && animation.timingFunctions[i])
908             dumpProperty<const TimingFunction&>(ts, "timing function", *animation.timingFunctions[i]);
909
910         if (i < animation.keyValues.size())
911             dumpProperty(ts, "value", animation.keyValues[i]);
912
913         ts << ")";
914     }
915
916     ts.decreaseIndent();
917     ts.decreaseIndent();
918
919     return ts;
920 }
921
922 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(FloatPoint3D point)
923 {
924     RemoteLayerTreeTextStream& ts = *this;
925     ts << point.x() << " " << point.y() << " " << point.z();
926     return ts;
927 }
928
929 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(Color color)
930 {
931     RemoteLayerTreeTextStream& ts = *this;
932     ts << color.serialized();
933     return ts;
934 }
935
936 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(FloatRect rect)
937 {
938     RemoteLayerTreeTextStream& ts = *this;
939     ts << rect.x() << " " << rect.y() << " " << rect.width() << " " << rect.height();
940     return ts;
941 }
942
943 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const Vector<GraphicsLayer::PlatformLayerID>& layers)
944 {
945     RemoteLayerTreeTextStream& ts = *this;
946
947     for (size_t i = 0; i < layers.size(); ++i) {
948         if (i)
949             ts << " ";
950         ts << layers[i];
951     }
952
953     return ts;
954 }
955
956 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const RemoteLayerBackingStore& backingStore)
957 {
958     RemoteLayerTreeTextStream& ts = *this;
959     ts << backingStore.size();
960     ts << " scale=" << backingStore.scale();
961     if (backingStore.isOpaque())
962         ts << " opaque";
963     if (backingStore.acceleratesDrawing())
964         ts << " accelerated";
965     return ts;
966 }
967
968 void RemoteLayerTreeTextStream::writeIndent()
969 {
970     for (int i = 0; i < m_indent; ++i)
971         *this << "  ";
972 }
973
974 static void dumpChangedLayers(RemoteLayerTreeTextStream& ts, const RemoteLayerTreeTransaction::LayerPropertiesMap& changedLayerProperties)
975 {
976     if (changedLayerProperties.isEmpty())
977         return;
978
979     ts << "\n";
980     ts.writeIndent();
981     ts << "(changed-layers";
982
983     // Dump the layer properties sorted by layer ID.
984     Vector<GraphicsLayer::PlatformLayerID> layerIDs;
985     copyKeysToVector(changedLayerProperties, layerIDs);
986     std::sort(layerIDs.begin(), layerIDs.end());
987
988     for (auto& layerID : layerIDs) {
989         const RemoteLayerTreeTransaction::LayerProperties& layerProperties = *changedLayerProperties.get(layerID);
990
991         ts << "\n";
992         ts.increaseIndent();
993         ts.writeIndent();
994         ts << "(layer " << layerID;
995
996         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::NameChanged)
997             dumpProperty(ts, "name", layerProperties.name);
998
999         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged)
1000             dumpProperty<Vector<GraphicsLayer::PlatformLayerID>>(ts, "children", layerProperties.children);
1001
1002         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::PositionChanged)
1003             dumpProperty(ts, "position", layerProperties.position);
1004
1005         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BoundsChanged)
1006             dumpProperty(ts, "bounds", layerProperties.bounds);
1007
1008         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::AnchorPointChanged)
1009             dumpProperty(ts, "anchorPoint", layerProperties.anchorPoint);
1010
1011         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BackgroundColorChanged)
1012             dumpProperty(ts, "backgroundColor", layerProperties.backgroundColor);
1013
1014         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BorderColorChanged)
1015             dumpProperty(ts, "borderColor", layerProperties.borderColor);
1016
1017         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BorderWidthChanged)
1018             dumpProperty(ts, "borderWidth", layerProperties.borderWidth);
1019
1020         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::OpacityChanged)
1021             dumpProperty(ts, "opacity", layerProperties.opacity);
1022
1023         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::TransformChanged)
1024             dumpProperty(ts, "transform", layerProperties.transform ? *layerProperties.transform : TransformationMatrix());
1025
1026         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SublayerTransformChanged)
1027             dumpProperty(ts, "sublayerTransform", layerProperties.sublayerTransform ? *layerProperties.sublayerTransform : TransformationMatrix());
1028
1029         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::HiddenChanged)
1030             dumpProperty(ts, "hidden", layerProperties.hidden);
1031
1032         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::GeometryFlippedChanged)
1033             dumpProperty(ts, "geometryFlipped", layerProperties.geometryFlipped);
1034
1035         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::DoubleSidedChanged)
1036             dumpProperty(ts, "doubleSided", layerProperties.doubleSided);
1037
1038         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MasksToBoundsChanged)
1039             dumpProperty(ts, "masksToBounds", layerProperties.masksToBounds);
1040
1041         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::OpaqueChanged)
1042             dumpProperty(ts, "opaque", layerProperties.opaque);
1043
1044         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MaskLayerChanged)
1045             dumpProperty(ts, "maskLayer", layerProperties.maskLayerID);
1046
1047         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ContentsRectChanged)
1048             dumpProperty(ts, "contentsRect", layerProperties.contentsRect);
1049
1050         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ContentsScaleChanged)
1051             dumpProperty(ts, "contentsScale", layerProperties.contentsScale);
1052
1053         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MinificationFilterChanged)
1054             dumpProperty(ts, "minificationFilter", layerProperties.minificationFilter);
1055
1056         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MagnificationFilterChanged)
1057             dumpProperty(ts, "magnificationFilter", layerProperties.magnificationFilter);
1058
1059         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BlendModeChanged)
1060             dumpProperty(ts, "blendMode", layerProperties.blendMode);
1061
1062         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SpeedChanged)
1063             dumpProperty(ts, "speed", layerProperties.speed);
1064
1065         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::TimeOffsetChanged)
1066             dumpProperty(ts, "timeOffset", layerProperties.timeOffset);
1067
1068         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BackingStoreChanged) {
1069             if (const RemoteLayerBackingStore* backingStore = layerProperties.backingStore.get())
1070                 dumpProperty<const RemoteLayerBackingStore&>(ts, "backingStore", *backingStore);
1071             else
1072                 dumpProperty(ts, "backingStore", "removed");
1073         }
1074
1075         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::FiltersChanged)
1076             dumpProperty(ts, "filters", layerProperties.filters ? *layerProperties.filters : FilterOperations());
1077
1078         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::AnimationsChanged) {
1079             for (const auto& keyValuePair : layerProperties.addedAnimations)
1080                 dumpProperty(ts, "animation " +  keyValuePair.key, keyValuePair.value);
1081
1082             for (const auto& name : layerProperties.keyPathsOfAnimationsToRemove)
1083                 dumpProperty(ts, "removed animation", name);
1084         }
1085
1086         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged)
1087             dumpProperty(ts, "edgeAntialiasingMask", layerProperties.edgeAntialiasingMask);
1088
1089         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::CustomAppearanceChanged)
1090             dumpProperty(ts, "customAppearance", layerProperties.customAppearance);
1091
1092         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::CustomBehaviorChanged)
1093             dumpProperty(ts, "customBehavior", layerProperties.customBehavior);
1094
1095         ts << ")";
1096
1097         ts.decreaseIndent();
1098     }
1099
1100     ts.decreaseIndent();
1101 }
1102
1103 void RemoteLayerTreeTransaction::dump() const
1104 {
1105     fprintf(stderr, "%s", description().data());
1106 }
1107
1108 CString RemoteLayerTreeTransaction::description() const
1109 {
1110     RemoteLayerTreeTextStream ts;
1111
1112     ts << "(\n";
1113     ts.increaseIndent();
1114     ts.writeIndent();
1115     ts << "(root-layer " << m_rootLayerID << ")";
1116
1117     if (!m_createdLayers.isEmpty()) {
1118         ts << "\n";
1119         ts.writeIndent();
1120         ts << "(created-layers";
1121         ts.increaseIndent();
1122         for (const auto& createdLayer : m_createdLayers) {
1123             ts << "\n";
1124             ts.writeIndent();
1125             ts << "(";
1126             switch (createdLayer.type) {
1127             case PlatformCALayer::LayerTypeLayer:
1128             case PlatformCALayer::LayerTypeWebLayer:
1129             case PlatformCALayer::LayerTypeSimpleLayer:
1130                 ts << "layer";
1131                 break;
1132             case PlatformCALayer::LayerTypeTransformLayer:
1133                 ts << "transform-layer";
1134                 break;
1135             case PlatformCALayer::LayerTypeWebTiledLayer:
1136                 ts << "tiled-layer";
1137                 break;
1138             case PlatformCALayer::LayerTypeTiledBackingLayer:
1139                 ts << "tiled-backing-layer";
1140                 break;
1141             case PlatformCALayer::LayerTypePageTiledBackingLayer:
1142                 ts << "page-tiled-backing-layer";
1143                 break;
1144             case PlatformCALayer::LayerTypeTiledBackingTileLayer:
1145                 ts << "tiled-backing-tile";
1146                 break;
1147             case PlatformCALayer::LayerTypeRootLayer:
1148                 ts << "root-layer";
1149                 break;
1150             case PlatformCALayer::LayerTypeAVPlayerLayer:
1151                 ts << "av-player-layer";
1152                 break;
1153             case PlatformCALayer::LayerTypeCustom:
1154                 ts << "custom-layer (context-id " << createdLayer.hostingContextID << ")";
1155                 break;
1156             }
1157             ts << " " << createdLayer.layerID << ")";
1158         }
1159         ts << ")";
1160         ts.decreaseIndent();
1161     }
1162
1163     dumpChangedLayers(ts, m_changedLayerProperties);
1164
1165     if (!m_destroyedLayerIDs.isEmpty())
1166         dumpProperty<Vector<GraphicsLayer::PlatformLayerID>>(ts, "destroyed-layers", m_destroyedLayerIDs);
1167
1168     ts << ")\n";
1169
1170     return ts.release().utf8();
1171 }
1172
1173 #endif // !defined(NDEBUG) || !LOG_DISABLED
1174
1175 } // namespace WebKit