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