[wk2] RemoteLayerBackingStore front buffers should be purgeable when unparented
[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     encoder << m_layerIDsWithNewlyUnreachableBackingStore;
456
457     encoder << m_contentsSize;
458     encoder << m_pageExtendedBackgroundColor;
459     encoder << m_pageScaleFactor;
460     encoder << m_minimumScaleFactor;
461     encoder << m_maximumScaleFactor;
462
463     encoder << m_lastVisibleContentRectUpdateID;
464     encoder << m_renderTreeSize;
465
466     encoder << m_scaleWasSetByUIProcess;
467     encoder << m_allowsUserScaling;
468 }
469
470 bool RemoteLayerTreeTransaction::decode(IPC::ArgumentDecoder& decoder, RemoteLayerTreeTransaction& result)
471 {
472     if (!decoder.decode(result.m_rootLayerID))
473         return false;
474     if (!result.m_rootLayerID)
475         return false;
476
477     if (!decoder.decode(result.m_createdLayers))
478         return false;
479
480     uint64_t numChangedLayerProperties;
481     if (!decoder.decode(numChangedLayerProperties))
482         return false;
483
484     for (uint64_t i = 0; i < numChangedLayerProperties; ++i) {
485         GraphicsLayer::PlatformLayerID layerID;
486         if (!decoder.decode(layerID))
487             return false;
488
489         std::unique_ptr<LayerProperties> layerProperties = std::make_unique<LayerProperties>();
490         if (!decoder.decode(*layerProperties))
491             return false;
492
493         result.changedLayerProperties().set(layerID, std::move(layerProperties));
494     }
495
496     if (!decoder.decode(result.m_destroyedLayerIDs))
497         return false;
498
499     for (auto& layerID : result.m_destroyedLayerIDs) {
500         if (!layerID)
501             return false;
502     }
503
504     if (!decoder.decode(result.m_videoLayerIDsPendingFullscreen))
505         return false;
506
507     if (!decoder.decode(result.m_layerIDsWithNewlyUnreachableBackingStore))
508         return false;
509
510     for (auto& layerID : result.m_layerIDsWithNewlyUnreachableBackingStore) {
511         if (!layerID)
512             return false;
513     }
514
515     if (!decoder.decode(result.m_contentsSize))
516         return false;
517     
518     if (!decoder.decode(result.m_pageExtendedBackgroundColor))
519         return false;
520
521     if (!decoder.decode(result.m_pageScaleFactor))
522         return false;
523
524     if (!decoder.decode(result.m_minimumScaleFactor))
525         return false;
526
527     if (!decoder.decode(result.m_maximumScaleFactor))
528         return false;
529
530     if (!decoder.decode(result.m_lastVisibleContentRectUpdateID))
531         return false;
532
533     if (!decoder.decode(result.m_renderTreeSize))
534         return false;
535
536     if (!decoder.decode(result.m_scaleWasSetByUIProcess))
537         return false;
538
539     if (!decoder.decode(result.m_allowsUserScaling))
540         return false;
541
542     return true;
543 }
544
545 void RemoteLayerTreeTransaction::setRootLayerID(GraphicsLayer::PlatformLayerID rootLayerID)
546 {
547     ASSERT_ARG(rootLayerID, rootLayerID);
548
549     m_rootLayerID = rootLayerID;
550 }
551
552 void RemoteLayerTreeTransaction::layerPropertiesChanged(PlatformCALayerRemote* remoteLayer, RemoteLayerTreeTransaction::LayerProperties& properties)
553 {
554     m_changedLayers.append(remoteLayer);
555 }
556
557 void RemoteLayerTreeTransaction::setCreatedLayers(Vector<LayerCreationProperties> createdLayers)
558 {
559     m_createdLayers = std::move(createdLayers);
560 }
561
562 void RemoteLayerTreeTransaction::setDestroyedLayerIDs(Vector<GraphicsLayer::PlatformLayerID> destroyedLayerIDs)
563 {
564     m_destroyedLayerIDs = std::move(destroyedLayerIDs);
565 }
566
567 void RemoteLayerTreeTransaction::setLayerIDsWithNewlyUnreachableBackingStore(Vector<GraphicsLayer::PlatformLayerID> layerIDsWithNewlyUnreachableBackingStore)
568 {
569     m_layerIDsWithNewlyUnreachableBackingStore = std::move(layerIDsWithNewlyUnreachableBackingStore);
570 }
571
572 #if !defined(NDEBUG) || !LOG_DISABLED
573
574 class RemoteLayerTreeTextStream : public TextStream
575 {
576 public:
577     using TextStream::operator<<;
578
579     RemoteLayerTreeTextStream()
580         : m_indent(0)
581     {
582     }
583
584     RemoteLayerTreeTextStream& operator<<(const TransformationMatrix&);
585     RemoteLayerTreeTextStream& operator<<(PlatformCALayer::FilterType);
586     RemoteLayerTreeTextStream& operator<<(FloatPoint3D);
587     RemoteLayerTreeTextStream& operator<<(Color);
588     RemoteLayerTreeTextStream& operator<<(FloatRect);
589     RemoteLayerTreeTextStream& operator<<(const Vector<WebCore::GraphicsLayer::PlatformLayerID>&);
590     RemoteLayerTreeTextStream& operator<<(const FilterOperation&);
591     RemoteLayerTreeTextStream& operator<<(const FilterOperations&);
592     RemoteLayerTreeTextStream& operator<<(const PlatformCAAnimationRemote::Properties&);
593     RemoteLayerTreeTextStream& operator<<(const RemoteLayerBackingStore&);
594     RemoteLayerTreeTextStream& operator<<(BlendMode);
595     RemoteLayerTreeTextStream& operator<<(PlatformCAAnimation::AnimationType);
596     RemoteLayerTreeTextStream& operator<<(PlatformCAAnimation::FillModeType);
597     RemoteLayerTreeTextStream& operator<<(PlatformCAAnimation::ValueFunctionType);
598     RemoteLayerTreeTextStream& operator<<(const TimingFunction&);
599     RemoteLayerTreeTextStream& operator<<(const PlatformCAAnimationRemote::KeyframeValue&);
600
601     void increaseIndent() { ++m_indent; }
602     void decreaseIndent() { --m_indent; ASSERT(m_indent >= 0); }
603
604     void writeIndent();
605
606 private:
607     int m_indent;
608 };
609
610 template <class T>
611 static void dumpProperty(RemoteLayerTreeTextStream& ts, String name, T value)
612 {
613     ts << "\n";
614     ts.increaseIndent();
615     ts.writeIndent();
616     ts << "(" << name << " ";
617     ts << value << ")";
618     ts.decreaseIndent();
619 }
620
621 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const TransformationMatrix& transform)
622 {
623     RemoteLayerTreeTextStream& ts = *this;
624     ts << "\n";
625     ts.increaseIndent();
626     ts.writeIndent();
627     ts << "[" << transform.m11() << " " << transform.m12() << " " << transform.m13() << " " << transform.m14() << "]\n";
628     ts.writeIndent();
629     ts << "[" << transform.m21() << " " << transform.m22() << " " << transform.m23() << " " << transform.m24() << "]\n";
630     ts.writeIndent();
631     ts << "[" << transform.m31() << " " << transform.m32() << " " << transform.m33() << " " << transform.m34() << "]\n";
632     ts.writeIndent();
633     ts << "[" << transform.m41() << " " << transform.m42() << " " << transform.m43() << " " << transform.m44() << "]";
634     ts.decreaseIndent();
635     return ts;
636 }
637
638 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCALayer::FilterType filterType)
639 {
640     RemoteLayerTreeTextStream& ts = *this;
641     switch (filterType) {
642     case PlatformCALayer::Linear:
643         ts << "linear";
644         break;
645     case PlatformCALayer::Nearest:
646         ts << "nearest";
647         break;
648     case PlatformCALayer::Trilinear:
649         ts << "trilinear";
650         break;
651     default:
652         ASSERT_NOT_REACHED();
653         break;
654     }
655     return ts;
656 }
657
658 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const FilterOperations& filters)
659 {
660     RemoteLayerTreeTextStream& ts = *this;
661     for (size_t i = 0; i < filters.size(); ++i) {
662         const auto filter = filters.at(i);
663         if (filter)
664             ts << *filter;
665         else
666             ts << "(null)";
667         if (i < filters.size() - 1)
668             ts << " ";
669     }
670     return ts;
671 }
672     
673 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const FilterOperation& filter)
674 {
675     RemoteLayerTreeTextStream& ts = *this;
676     switch (filter.type()) {
677     case FilterOperation::REFERENCE:
678         ts << "reference";
679         break;
680     case FilterOperation::GRAYSCALE: {
681         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
682         ts << "grayscale(" << colorMatrixFilter.amount() << ")";
683         break;
684     }
685     case FilterOperation::SEPIA: {
686         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
687         ts << "sepia(" << colorMatrixFilter.amount() << ")";
688         break;
689     }
690     case FilterOperation::SATURATE: {
691         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
692         ts << "saturate(" << colorMatrixFilter.amount() << ")";
693         break;
694     }
695     case FilterOperation::HUE_ROTATE: {
696         const BasicColorMatrixFilterOperation& colorMatrixFilter = toBasicColorMatrixFilterOperation(filter);
697         ts << "hue-rotate(" << colorMatrixFilter.amount() << ")";
698         break;
699     }
700     case FilterOperation::INVERT: {
701         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
702         ts << "invert(" << componentTransferFilter.amount() << ")";
703         break;
704     }
705     case FilterOperation::OPACITY: {
706         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
707         ts << "opacity(" << componentTransferFilter.amount() << ")";
708         break;
709     }
710     case FilterOperation::BRIGHTNESS: {
711         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
712         ts << "brightness(" << componentTransferFilter.amount() << ")";
713         break;
714     }
715     case FilterOperation::CONTRAST: {
716         const BasicComponentTransferFilterOperation& componentTransferFilter = toBasicComponentTransferFilterOperation(filter);
717         ts << "contrast(" << componentTransferFilter.amount() << ")";
718         break;
719     }
720     case FilterOperation::BLUR: {
721         const BlurFilterOperation& blurFilter = toBlurFilterOperation(filter);
722         ts << "blur(" << floatValueForLength(blurFilter.stdDeviation(), 0) << ")";
723         break;
724     }
725     case FilterOperation::DROP_SHADOW: {
726         const DropShadowFilterOperation& dropShadowFilter = toDropShadowFilterOperation(filter);
727         ts << "drop-shadow(" << dropShadowFilter.x() << " " << dropShadowFilter.y() << " " << dropShadowFilter.location() << " ";
728         ts << dropShadowFilter.color() << ")";
729         break;
730     }
731     case FilterOperation::PASSTHROUGH:
732         ts << "passthrough";
733         break;
734     case FilterOperation::DEFAULT: {
735         const DefaultFilterOperation& defaultFilter = toDefaultFilterOperation(filter);
736         ts << "default type=" << (int)defaultFilter.representedType();
737         break;
738     }
739     case FilterOperation::NONE:
740         ts << "none";
741         break;
742     }
743     return ts;
744 }
745
746 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(BlendMode blendMode)
747 {
748     RemoteLayerTreeTextStream& ts = *this;
749     switch (blendMode) {
750     case BlendModeNormal: ts << "normal"; break;
751     case BlendModeMultiply: ts << "multiply"; break;
752     case BlendModeScreen: ts << "screen"; break;
753     case BlendModeOverlay: ts << "overlay"; break;
754     case BlendModeDarken: ts << "darken"; break;
755     case BlendModeLighten: ts << "lighten"; break;
756     case BlendModeColorDodge: ts << "color-dodge"; break;
757     case BlendModeColorBurn: ts << "color-burn"; break;
758     case BlendModeHardLight: ts << "hard-light"; break;
759     case BlendModeSoftLight: ts << "soft-light"; break;
760     case BlendModeDifference: ts << "difference"; break;
761     case BlendModeExclusion: ts << "exclusion"; break;
762     case BlendModeHue: ts << "hue"; break;
763     case BlendModeSaturation: ts << "saturation"; break;
764     case BlendModeColor: ts << "color"; break;
765     case BlendModeLuminosity: ts << "luminosity"; break;
766     }
767     return ts;
768 }
769
770 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCAAnimation::AnimationType type)
771 {
772     RemoteLayerTreeTextStream& ts = *this;
773     switch (type) {
774     case PlatformCAAnimation::Basic: ts << "basic"; break;
775     case PlatformCAAnimation::Keyframe: ts << "keyframe"; break;
776     }
777     return ts;
778 }
779
780 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCAAnimation::FillModeType type)
781 {
782     RemoteLayerTreeTextStream& ts = *this;
783     switch (type) {
784     case PlatformCAAnimation::NoFillMode: ts << "none"; break;
785     case PlatformCAAnimation::Forwards: ts << "forwards"; break;
786     case PlatformCAAnimation::Backwards: ts << "backwards"; break;
787     case PlatformCAAnimation::Both: ts << "both"; break;
788     }
789     return ts;
790 }
791
792 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(PlatformCAAnimation::ValueFunctionType type)
793 {
794     RemoteLayerTreeTextStream& ts = *this;
795     switch (type) {
796     case PlatformCAAnimation::NoValueFunction: ts << "none"; break;
797     case PlatformCAAnimation::RotateX: ts << "rotateX"; break;
798     case PlatformCAAnimation::RotateY: ts << "rotateY"; break;
799     case PlatformCAAnimation::RotateZ: ts << "rotateX"; break;
800     case PlatformCAAnimation::ScaleX: ts << "scaleX"; break;
801     case PlatformCAAnimation::ScaleY: ts << "scaleY"; break;
802     case PlatformCAAnimation::ScaleZ: ts << "scaleX"; break;
803     case PlatformCAAnimation::Scale: ts << "scale"; break;
804     case PlatformCAAnimation::TranslateX: ts << "translateX"; break;
805     case PlatformCAAnimation::TranslateY: ts << "translateY"; break;
806     case PlatformCAAnimation::TranslateZ: ts << "translateZ"; break;
807     case PlatformCAAnimation::Translate: ts << "translate"; break;
808     }
809     return ts;
810 }
811
812 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const PlatformCAAnimationRemote::KeyframeValue& value)
813 {
814     RemoteLayerTreeTextStream& ts = *this;
815
816     switch (value.keyframeType()) {
817     case PlatformCAAnimationRemote::KeyframeValue::NumberKeyType:
818         ts << "number=" << value.numberValue();
819         break;
820     case PlatformCAAnimationRemote::KeyframeValue::ColorKeyType:
821         ts << "color=";
822         ts << value.colorValue();
823         break;
824     case PlatformCAAnimationRemote::KeyframeValue::PointKeyType:
825         ts << "point=";
826         ts << value.pointValue();
827         break;
828     case PlatformCAAnimationRemote::KeyframeValue::TransformKeyType:
829         ts << "transform=";
830         ts << value.transformValue();
831         break;
832     case PlatformCAAnimationRemote::KeyframeValue::FilterKeyType:
833         ts << "filter=";
834         if (value.filterValue())
835             ts << *value.filterValue();
836         else
837             ts << "null";
838         break;
839     }
840     return ts;
841 }
842
843 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const TimingFunction& timingFunction)
844 {
845     RemoteLayerTreeTextStream& ts = *this;
846     switch (timingFunction.type()) {
847     case TimingFunction::LinearFunction:
848         ts << "linear";
849         break;
850     case TimingFunction::CubicBezierFunction: {
851         const CubicBezierTimingFunction& cubicBezierFunction = static_cast<const CubicBezierTimingFunction&>(timingFunction);
852         ts << "cubic-bezier(" << cubicBezierFunction.x1() << ", " << cubicBezierFunction.y1() << ", " <<  cubicBezierFunction.x2() << ", " << cubicBezierFunction.y2() << ")";
853         break;
854     }
855     case TimingFunction::StepsFunction: {
856         const StepsTimingFunction& stepsFunction = static_cast<const StepsTimingFunction&>(timingFunction);
857         ts << "steps(" << stepsFunction.numberOfSteps() << ", " << (stepsFunction.stepAtStart() ? "start" : "end") << ")";
858         break;
859     }
860     }
861     return ts;
862 }
863
864 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const PlatformCAAnimationRemote::Properties& animation)
865 {
866     RemoteLayerTreeTextStream& ts = *this;
867
868     ts << "type=";
869     ts << animation.animationType;
870     ts << " keyPath=";
871     ts << animation.keyPath;
872
873     if (animation.beginTime)
874         dumpProperty(ts, "beginTime", animation.beginTime);
875
876     if (animation.duration)
877         dumpProperty(ts, "duration", animation.duration);
878
879     if (animation.timeOffset)
880         dumpProperty(ts, "timeOffset", animation.timeOffset);
881
882     dumpProperty(ts, "repeatCount", animation.repeatCount);
883
884     if (animation.speed != 1)
885         dumpProperty(ts, "speed", animation.speed);
886
887     dumpProperty(ts, "fillMode", animation.fillMode);
888     dumpProperty(ts, "valueFunction", animation.valueFunction);
889
890     if (animation.autoReverses)
891         dumpProperty(ts, "autoReverses", animation.autoReverses);
892
893     if (!animation.removedOnCompletion)
894         dumpProperty(ts, "removedOnCompletion", animation.removedOnCompletion);
895
896     if (animation.additive)
897         dumpProperty(ts, "additive", animation.additive);
898
899     if (animation.reverseTimingFunctions)
900         dumpProperty(ts, "reverseTimingFunctions", animation.reverseTimingFunctions);
901
902     if (animation.hasExplicitBeginTime)
903         dumpProperty(ts, "hasExplicitBeginTime", animation.hasExplicitBeginTime);
904
905     ts << "\n";
906     ts.increaseIndent();
907     ts.writeIndent();
908     ts << "(" << "keyframes";
909     ts.increaseIndent();
910
911     size_t maxFrames = std::max(animation.keyValues.size(), animation.keyTimes.size());
912     maxFrames = std::max(maxFrames, animation.timingFunctions.size());
913
914     for (size_t i = 0; i < maxFrames; ++i) {
915         ts << "\n";
916         ts.writeIndent();
917         ts << "(keyframe " << unsigned(i);
918         if (i < animation.keyTimes.size())
919             dumpProperty(ts, "time", animation.keyTimes[i]);
920
921         if (i < animation.timingFunctions.size() && animation.timingFunctions[i])
922             dumpProperty<const TimingFunction&>(ts, "timing function", *animation.timingFunctions[i]);
923
924         if (i < animation.keyValues.size())
925             dumpProperty(ts, "value", animation.keyValues[i]);
926
927         ts << ")";
928     }
929
930     ts.decreaseIndent();
931     ts.decreaseIndent();
932
933     return ts;
934 }
935
936 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(FloatPoint3D point)
937 {
938     RemoteLayerTreeTextStream& ts = *this;
939     ts << point.x() << " " << point.y() << " " << point.z();
940     return ts;
941 }
942
943 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(Color color)
944 {
945     RemoteLayerTreeTextStream& ts = *this;
946     ts << color.serialized();
947     return ts;
948 }
949
950 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(FloatRect rect)
951 {
952     RemoteLayerTreeTextStream& ts = *this;
953     ts << rect.x() << " " << rect.y() << " " << rect.width() << " " << rect.height();
954     return ts;
955 }
956
957 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const Vector<GraphicsLayer::PlatformLayerID>& layers)
958 {
959     RemoteLayerTreeTextStream& ts = *this;
960
961     for (size_t i = 0; i < layers.size(); ++i) {
962         if (i)
963             ts << " ";
964         ts << layers[i];
965     }
966
967     return ts;
968 }
969
970 RemoteLayerTreeTextStream& RemoteLayerTreeTextStream::operator<<(const RemoteLayerBackingStore& backingStore)
971 {
972     RemoteLayerTreeTextStream& ts = *this;
973     ts << backingStore.size();
974     ts << " scale=" << backingStore.scale();
975     if (backingStore.isOpaque())
976         ts << " opaque";
977     if (backingStore.acceleratesDrawing())
978         ts << " accelerated";
979     return ts;
980 }
981
982 void RemoteLayerTreeTextStream::writeIndent()
983 {
984     for (int i = 0; i < m_indent; ++i)
985         *this << "  ";
986 }
987
988 static void dumpChangedLayers(RemoteLayerTreeTextStream& ts, const RemoteLayerTreeTransaction::LayerPropertiesMap& changedLayerProperties)
989 {
990     if (changedLayerProperties.isEmpty())
991         return;
992
993     ts << "\n";
994     ts.writeIndent();
995     ts << "(changed-layers";
996
997     // Dump the layer properties sorted by layer ID.
998     Vector<GraphicsLayer::PlatformLayerID> layerIDs;
999     copyKeysToVector(changedLayerProperties, layerIDs);
1000     std::sort(layerIDs.begin(), layerIDs.end());
1001
1002     for (auto& layerID : layerIDs) {
1003         const RemoteLayerTreeTransaction::LayerProperties& layerProperties = *changedLayerProperties.get(layerID);
1004
1005         ts << "\n";
1006         ts.increaseIndent();
1007         ts.writeIndent();
1008         ts << "(layer " << layerID;
1009
1010         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::NameChanged)
1011             dumpProperty(ts, "name", layerProperties.name);
1012
1013         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged)
1014             dumpProperty<Vector<GraphicsLayer::PlatformLayerID>>(ts, "children", layerProperties.children);
1015
1016         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::PositionChanged)
1017             dumpProperty(ts, "position", layerProperties.position);
1018
1019         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BoundsChanged)
1020             dumpProperty(ts, "bounds", layerProperties.bounds);
1021
1022         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::AnchorPointChanged)
1023             dumpProperty(ts, "anchorPoint", layerProperties.anchorPoint);
1024
1025         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BackgroundColorChanged)
1026             dumpProperty(ts, "backgroundColor", layerProperties.backgroundColor);
1027
1028         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BorderColorChanged)
1029             dumpProperty(ts, "borderColor", layerProperties.borderColor);
1030
1031         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BorderWidthChanged)
1032             dumpProperty(ts, "borderWidth", layerProperties.borderWidth);
1033
1034         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::OpacityChanged)
1035             dumpProperty(ts, "opacity", layerProperties.opacity);
1036
1037         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::TransformChanged)
1038             dumpProperty(ts, "transform", layerProperties.transform ? *layerProperties.transform : TransformationMatrix());
1039
1040         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SublayerTransformChanged)
1041             dumpProperty(ts, "sublayerTransform", layerProperties.sublayerTransform ? *layerProperties.sublayerTransform : TransformationMatrix());
1042
1043         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::HiddenChanged)
1044             dumpProperty(ts, "hidden", layerProperties.hidden);
1045
1046         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::GeometryFlippedChanged)
1047             dumpProperty(ts, "geometryFlipped", layerProperties.geometryFlipped);
1048
1049         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::DoubleSidedChanged)
1050             dumpProperty(ts, "doubleSided", layerProperties.doubleSided);
1051
1052         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MasksToBoundsChanged)
1053             dumpProperty(ts, "masksToBounds", layerProperties.masksToBounds);
1054
1055         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::OpaqueChanged)
1056             dumpProperty(ts, "opaque", layerProperties.opaque);
1057
1058         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MaskLayerChanged)
1059             dumpProperty(ts, "maskLayer", layerProperties.maskLayerID);
1060
1061         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ContentsRectChanged)
1062             dumpProperty(ts, "contentsRect", layerProperties.contentsRect);
1063
1064         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ContentsScaleChanged)
1065             dumpProperty(ts, "contentsScale", layerProperties.contentsScale);
1066
1067         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MinificationFilterChanged)
1068             dumpProperty(ts, "minificationFilter", layerProperties.minificationFilter);
1069
1070         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MagnificationFilterChanged)
1071             dumpProperty(ts, "magnificationFilter", layerProperties.magnificationFilter);
1072
1073         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BlendModeChanged)
1074             dumpProperty(ts, "blendMode", layerProperties.blendMode);
1075
1076         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SpeedChanged)
1077             dumpProperty(ts, "speed", layerProperties.speed);
1078
1079         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::TimeOffsetChanged)
1080             dumpProperty(ts, "timeOffset", layerProperties.timeOffset);
1081
1082         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BackingStoreChanged) {
1083             if (const RemoteLayerBackingStore* backingStore = layerProperties.backingStore.get())
1084                 dumpProperty<const RemoteLayerBackingStore&>(ts, "backingStore", *backingStore);
1085             else
1086                 dumpProperty(ts, "backingStore", "removed");
1087         }
1088
1089         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::FiltersChanged)
1090             dumpProperty(ts, "filters", layerProperties.filters ? *layerProperties.filters : FilterOperations());
1091
1092         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::AnimationsChanged) {
1093             for (const auto& keyValuePair : layerProperties.addedAnimations)
1094                 dumpProperty(ts, "animation " +  keyValuePair.key, keyValuePair.value);
1095
1096             for (const auto& name : layerProperties.keyPathsOfAnimationsToRemove)
1097                 dumpProperty(ts, "removed animation", name);
1098         }
1099
1100         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::EdgeAntialiasingMaskChanged)
1101             dumpProperty(ts, "edgeAntialiasingMask", layerProperties.edgeAntialiasingMask);
1102
1103         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::CustomAppearanceChanged)
1104             dumpProperty(ts, "customAppearance", layerProperties.customAppearance);
1105
1106         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::CustomBehaviorChanged)
1107             dumpProperty(ts, "customBehavior", layerProperties.customBehavior);
1108
1109         ts << ")";
1110
1111         ts.decreaseIndent();
1112     }
1113
1114     ts.decreaseIndent();
1115 }
1116
1117 void RemoteLayerTreeTransaction::dump() const
1118 {
1119     fprintf(stderr, "%s", description().data());
1120 }
1121
1122 CString RemoteLayerTreeTransaction::description() const
1123 {
1124     RemoteLayerTreeTextStream ts;
1125
1126     ts << "(\n";
1127     ts.increaseIndent();
1128     ts.writeIndent();
1129     ts << "(root-layer " << m_rootLayerID << ")";
1130
1131     if (!m_createdLayers.isEmpty()) {
1132         ts << "\n";
1133         ts.writeIndent();
1134         ts << "(created-layers";
1135         ts.increaseIndent();
1136         for (const auto& createdLayer : m_createdLayers) {
1137             ts << "\n";
1138             ts.writeIndent();
1139             ts << "(";
1140             switch (createdLayer.type) {
1141             case PlatformCALayer::LayerTypeLayer:
1142             case PlatformCALayer::LayerTypeWebLayer:
1143             case PlatformCALayer::LayerTypeSimpleLayer:
1144                 ts << "layer";
1145                 break;
1146             case PlatformCALayer::LayerTypeTransformLayer:
1147                 ts << "transform-layer";
1148                 break;
1149             case PlatformCALayer::LayerTypeWebTiledLayer:
1150                 ts << "tiled-layer";
1151                 break;
1152             case PlatformCALayer::LayerTypeTiledBackingLayer:
1153                 ts << "tiled-backing-layer";
1154                 break;
1155             case PlatformCALayer::LayerTypePageTiledBackingLayer:
1156                 ts << "page-tiled-backing-layer";
1157                 break;
1158             case PlatformCALayer::LayerTypeTiledBackingTileLayer:
1159                 ts << "tiled-backing-tile";
1160                 break;
1161             case PlatformCALayer::LayerTypeRootLayer:
1162                 ts << "root-layer";
1163                 break;
1164             case PlatformCALayer::LayerTypeAVPlayerLayer:
1165                 ts << "av-player-layer";
1166                 break;
1167             case PlatformCALayer::LayerTypeCustom:
1168                 ts << "custom-layer (context-id " << createdLayer.hostingContextID << ")";
1169                 break;
1170             }
1171             ts << " " << createdLayer.layerID << ")";
1172         }
1173         ts << ")";
1174         ts.decreaseIndent();
1175     }
1176
1177     dumpChangedLayers(ts, m_changedLayerProperties);
1178
1179     if (!m_destroyedLayerIDs.isEmpty())
1180         dumpProperty<Vector<GraphicsLayer::PlatformLayerID>>(ts, "destroyed-layers", m_destroyedLayerIDs);
1181
1182     ts << ")\n";
1183
1184     return ts.release().utf8();
1185 }
1186
1187 #endif // !defined(NDEBUG) || !LOG_DISABLED
1188
1189 } // namespace WebKit