RemoteLayerTree: Add support for more layer properties and transform layers
[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 "PlatformCALayerRemote.h"
33 #import "WebCoreArgumentCoders.h"
34 #import <WebCore/TextStream.h>
35 #import <wtf/text/CString.h>
36 #import <wtf/text/StringBuilder.h>
37
38 using namespace WebCore;
39
40 namespace WebKit {
41
42 RemoteLayerTreeTransaction::LayerCreationProperties::LayerCreationProperties()
43 {
44 }
45
46 void RemoteLayerTreeTransaction::LayerCreationProperties::encode(CoreIPC::ArgumentEncoder& encoder) const
47 {
48     encoder << layerID;
49     encoder.encodeEnum(type);
50 }
51
52 bool RemoteLayerTreeTransaction::LayerCreationProperties::decode(CoreIPC::ArgumentDecoder& decoder, LayerCreationProperties& result)
53 {
54     if (!decoder.decode(result.layerID))
55         return false;
56
57     if (!decoder.decodeEnum(result.type))
58         return false;
59
60     return true;
61 }
62
63 RemoteLayerTreeTransaction::LayerProperties::LayerProperties()
64     : changedProperties(NoChange)
65 {
66 }
67
68 void RemoteLayerTreeTransaction::LayerProperties::encode(CoreIPC::ArgumentEncoder& encoder) const
69 {
70     encoder.encodeEnum(changedProperties);
71
72     if (changedProperties & NameChanged)
73         encoder << name;
74
75     if (changedProperties & ChildrenChanged)
76         encoder << children;
77
78     if (changedProperties & PositionChanged)
79         encoder << position;
80
81     if (changedProperties & SizeChanged)
82         encoder << size;
83
84     if (changedProperties & BackgroundColorChanged)
85         encoder << backgroundColor;
86
87     if (changedProperties & AnchorPointChanged)
88         encoder << anchorPoint;
89
90     if (changedProperties & BorderWidthChanged)
91         encoder << borderWidth;
92
93     if (changedProperties & BorderColorChanged)
94         encoder << borderColor;
95
96     if (changedProperties & OpacityChanged)
97         encoder << opacity;
98
99     if (changedProperties & TransformChanged)
100         encoder << transform;
101
102     if (changedProperties & SublayerTransformChanged)
103         encoder << sublayerTransform;
104
105     if (changedProperties & HiddenChanged)
106         encoder << hidden;
107
108     if (changedProperties & GeometryFlippedChanged)
109         encoder << geometryFlipped;
110
111     if (changedProperties & DoubleSidedChanged)
112         encoder << doubleSided;
113
114     if (changedProperties & MasksToBoundsChanged)
115         encoder << masksToBounds;
116
117     if (changedProperties & OpaqueChanged)
118         encoder << opaque;
119 }
120
121 bool RemoteLayerTreeTransaction::LayerProperties::decode(CoreIPC::ArgumentDecoder& decoder, LayerProperties& result)
122 {
123     if (!decoder.decodeEnum(result.changedProperties))
124         return false;
125
126     if (result.changedProperties & NameChanged) {
127         if (!decoder.decode(result.name))
128             return false;
129     }
130
131     if (result.changedProperties & ChildrenChanged) {
132         if (!decoder.decode(result.children))
133             return false;
134
135         for (auto layerID : result.children) {
136             if (!layerID)
137                 return false;
138         }
139     }
140
141     if (result.changedProperties & PositionChanged) {
142         if (!decoder.decode(result.position))
143             return false;
144     }
145
146     if (result.changedProperties & SizeChanged) {
147         if (!decoder.decode(result.size))
148             return false;
149     }
150
151     if (result.changedProperties & BackgroundColorChanged) {
152         if (!decoder.decode(result.backgroundColor))
153             return false;
154     }
155
156     if (result.changedProperties & AnchorPointChanged) {
157         if (!decoder.decode(result.anchorPoint))
158             return false;
159     }
160
161     if (result.changedProperties & BorderWidthChanged) {
162         if (!decoder.decode(result.borderWidth))
163             return false;
164     }
165
166     if (result.changedProperties & BorderColorChanged) {
167         if (!decoder.decode(result.borderColor))
168             return false;
169     }
170
171     if (result.changedProperties & OpacityChanged) {
172         if (!decoder.decode(result.opacity))
173             return false;
174     }
175
176     if (result.changedProperties & TransformChanged) {
177         if (!decoder.decode(result.transform))
178             return false;
179     }
180
181     if (result.changedProperties & SublayerTransformChanged) {
182         if (!decoder.decode(result.sublayerTransform))
183             return false;
184     }
185
186     if (result.changedProperties & HiddenChanged) {
187         if (!decoder.decode(result.hidden))
188             return false;
189     }
190
191     if (result.changedProperties & GeometryFlippedChanged) {
192         if (!decoder.decode(result.geometryFlipped))
193             return false;
194     }
195
196     if (result.changedProperties & DoubleSidedChanged) {
197         if (!decoder.decode(result.doubleSided))
198             return false;
199     }
200
201     if (result.changedProperties & MasksToBoundsChanged) {
202         if (!decoder.decode(result.masksToBounds))
203             return false;
204     }
205
206     if (result.changedProperties & OpaqueChanged) {
207         if (!decoder.decode(result.opaque))
208             return false;
209     }
210
211     return true;
212 }
213
214 RemoteLayerTreeTransaction::RemoteLayerTreeTransaction()
215 {
216 }
217
218 RemoteLayerTreeTransaction::~RemoteLayerTreeTransaction()
219 {
220 }
221
222 void RemoteLayerTreeTransaction::encode(CoreIPC::ArgumentEncoder& encoder) const
223 {
224     encoder << m_rootLayerID;
225     encoder << m_createdLayers;
226     encoder << m_changedLayerProperties;
227     encoder << m_destroyedLayerIDs;
228 }
229
230 bool RemoteLayerTreeTransaction::decode(CoreIPC::ArgumentDecoder& decoder, RemoteLayerTreeTransaction& result)
231 {
232     if (!decoder.decode(result.m_rootLayerID))
233         return false;
234     if (!result.m_rootLayerID)
235         return false;
236
237     if (!decoder.decode(result.m_createdLayers))
238         return false;
239
240     if (!decoder.decode(result.m_changedLayerProperties))
241         return false;
242
243     if (!decoder.decode(result.m_destroyedLayerIDs))
244         return false;
245     for (LayerID layerID : result.m_destroyedLayerIDs) {
246         if (!layerID)
247             return false;
248     }
249
250     return true;
251 }
252
253 void RemoteLayerTreeTransaction::setRootLayerID(LayerID rootLayerID)
254 {
255     ASSERT_ARG(rootLayerID, rootLayerID);
256
257     m_rootLayerID = rootLayerID;
258 }
259
260 void RemoteLayerTreeTransaction::layerPropertiesChanged(PlatformCALayerRemote* remoteLayer, RemoteLayerTreeTransaction::LayerProperties& properties)
261 {
262     m_changedLayerProperties.set(remoteLayer->layerID(), properties);
263 }
264
265 void RemoteLayerTreeTransaction::setCreatedLayers(Vector<LayerCreationProperties> createdLayers)
266 {
267     m_createdLayers = std::move(createdLayers);
268 }
269
270 void RemoteLayerTreeTransaction::setDestroyedLayerIDs(Vector<LayerID> destroyedLayerIDs)
271 {
272     m_destroyedLayerIDs = std::move(destroyedLayerIDs);
273 }
274
275 #ifndef NDEBUG
276
277 static void writeIndent(StringBuilder& builder, int indent)
278 {
279     for (int i = 0; i < indent; ++i)
280         builder.append(' ');
281 }
282
283 static void dumpProperty(StringBuilder& builder, String name, const TransformationMatrix& transform)
284 {
285     if (transform.isIdentity())
286         return;
287
288     builder.append('\n');
289     writeIndent(builder, 3);
290     builder.append("(");
291     builder.append(name);
292     builder.append("\n");
293
294     TextStream ts;
295     ts << "    [" << transform.m11() << " " << transform.m12() << " " << transform.m13() << " " << transform.m14() << "]\n";
296     ts << "    [" << transform.m21() << " " << transform.m22() << " " << transform.m23() << " " << transform.m24() << "]\n";
297     ts << "    [" << transform.m31() << " " << transform.m32() << " " << transform.m33() << " " << transform.m34() << "]\n";
298     ts << "    [" << transform.m41() << " " << transform.m42() << " " << transform.m43() << " " << transform.m44() << "])";
299
300     builder.append(ts.release());
301 }
302
303 static void dumpChangedLayers(StringBuilder& builder, const HashMap<RemoteLayerTreeTransaction::LayerID, RemoteLayerTreeTransaction::LayerProperties>& changedLayerProperties)
304 {
305     if (changedLayerProperties.isEmpty())
306         return;
307
308     writeIndent(builder, 1);
309     builder.append("(changed-layers\n");
310
311     // Dump the layer properties sorted by layer ID.
312     Vector<RemoteLayerTreeTransaction::LayerID> layerIDs;
313     copyKeysToVector(changedLayerProperties, layerIDs);
314     std::sort(layerIDs.begin(), layerIDs.end());
315
316     for (auto layerID : layerIDs) {
317         const RemoteLayerTreeTransaction::LayerProperties& layerProperties = changedLayerProperties.get(layerID);
318
319         writeIndent(builder, 2);
320         builder.append("(layer ");
321         builder.appendNumber(layerID);
322
323         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::NameChanged) {
324             builder.append('\n');
325             writeIndent(builder, 3);
326             builder.append("(name \"");
327             builder.append(layerProperties.name);
328             builder.append("\")");
329         }
330
331         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::ChildrenChanged) {
332             builder.append('\n');
333             writeIndent(builder, 3);
334             builder.append("(children (");
335             for (size_t i = 0; i < layerProperties.children.size(); ++i) {
336                 if (i)
337                     builder.append(' ');
338                 builder.appendNumber(layerProperties.children[i]);
339             }
340
341             builder.append(")");
342         }
343
344         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::PositionChanged) {
345             builder.append('\n');
346             writeIndent(builder, 3);
347             builder.append("(position ");
348             builder.appendNumber(layerProperties.position.x());
349             builder.append(' ');
350             builder.appendNumber(layerProperties.position.y());
351             builder.append(' ');
352             builder.appendNumber(layerProperties.position.z());
353             builder.append(')');
354         }
355
356         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SizeChanged) {
357             builder.append('\n');
358             writeIndent(builder, 3);
359             builder.append("(size ");
360             builder.appendNumber(layerProperties.size.width());
361             builder.append(' ');
362             builder.appendNumber(layerProperties.size.height());
363             builder.append(')');
364         }
365
366         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::AnchorPointChanged) {
367             builder.append('\n');
368             writeIndent(builder, 3);
369             builder.append("(anchorPoint ");
370             builder.appendNumber(layerProperties.anchorPoint.x());
371             builder.append(' ');
372             builder.appendNumber(layerProperties.anchorPoint.y());
373             builder.append(' ');
374             builder.appendNumber(layerProperties.anchorPoint.z());
375             builder.append(')');
376         }
377
378         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BackgroundColorChanged) {
379             builder.append('\n');
380             writeIndent(builder, 3);
381             builder.append("(backgroundColor ");
382             builder.append(layerProperties.backgroundColor.serialized());
383             builder.append(')');
384         }
385
386         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BorderColorChanged) {
387             builder.append('\n');
388             writeIndent(builder, 3);
389             builder.append("(borderColor ");
390             builder.append(layerProperties.borderColor.serialized());
391             builder.append(')');
392         }
393
394         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::BorderWidthChanged) {
395             builder.append('\n');
396             writeIndent(builder, 3);
397             builder.append("(borderWidth ");
398             builder.appendNumber(layerProperties.borderWidth);
399             builder.append(')');
400         }
401
402         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::OpacityChanged) {
403             builder.append('\n');
404             writeIndent(builder, 3);
405             builder.append("(opacity ");
406             builder.appendNumber(layerProperties.opacity);
407             builder.append(')');
408         }
409
410         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::TransformChanged)
411             dumpProperty(builder, "transform", layerProperties.transform);
412
413         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::SublayerTransformChanged)
414             dumpProperty(builder, "sublayerTransform", layerProperties.sublayerTransform);
415
416         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::HiddenChanged) {
417             builder.append('\n');
418             writeIndent(builder, 3);
419             builder.append("(hidden ");
420             builder.append(layerProperties.hidden ? "true" : "false");
421             builder.append(')');
422         }
423
424         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::GeometryFlippedChanged) {
425             builder.append('\n');
426             writeIndent(builder, 3);
427             builder.append("(geometryFlipped ");
428             builder.append(layerProperties.geometryFlipped ? "true" : "false");
429             builder.append(')');
430         }
431
432         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::DoubleSidedChanged) {
433             builder.append('\n');
434             writeIndent(builder, 3);
435             builder.append("(doubleSided ");
436             builder.append(layerProperties.doubleSided ? "true" : "false");
437             builder.append(')');
438         }
439
440         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::MasksToBoundsChanged) {
441             builder.append('\n');
442             writeIndent(builder, 3);
443             builder.append("(masksToBounds ");
444             builder.append(layerProperties.masksToBounds ? "true" : "false");
445             builder.append(')');
446         }
447
448         if (layerProperties.changedProperties & RemoteLayerTreeTransaction::OpaqueChanged) {
449             builder.append('\n');
450             writeIndent(builder, 3);
451             builder.append("(opaque ");
452             builder.append(layerProperties.opaque ? "true" : "false");
453             builder.append(')');
454         }
455
456         builder.append(")\n");
457     }
458 }
459
460 void RemoteLayerTreeTransaction::dump() const
461 {
462     StringBuilder builder;
463
464     builder.append("(\n");
465
466     writeIndent(builder, 1);
467     builder.append("(root-layer ");
468     builder.appendNumber(m_rootLayerID);
469     builder.append(")\n");
470
471     if (!m_createdLayers.isEmpty()) {
472         writeIndent(builder, 1);
473         builder.append("(created-layers\n");
474         for (const auto& createdLayer : m_createdLayers) {
475             writeIndent(builder, 2);
476             builder.append("(");
477             switch (createdLayer.type) {
478             case PlatformCALayer::LayerTypeLayer:
479             case PlatformCALayer::LayerTypeWebLayer:
480                 builder.append("layer");
481                 break;
482             case PlatformCALayer::LayerTypeTransformLayer:
483                 builder.append("transform-layer");
484                 break;
485             case PlatformCALayer::LayerTypeWebTiledLayer:
486                 builder.append("tiled-layer");
487                 break;
488             case PlatformCALayer::LayerTypeTiledBackingLayer:
489                 builder.append("tiled-backing-layer");
490                 break;
491             case PlatformCALayer::LayerTypePageTiledBackingLayer:
492                 builder.append("page-tiled-backing-layer");
493                 break;
494             case PlatformCALayer::LayerTypeRootLayer:
495                 builder.append("root-layer");
496                 break;
497             case PlatformCALayer::LayerTypeAVPlayerLayer:
498                 builder.append("av-player-layer");
499                 break;
500             case PlatformCALayer::LayerTypeCustom:
501                 builder.append("custom-layer");
502                 break;
503             }
504             builder.append(' ');
505             builder.appendNumber(createdLayer.layerID);
506             builder.append(")\n");
507         }
508         builder.append(")\n");
509     }
510
511     dumpChangedLayers(builder, m_changedLayerProperties);
512
513     if (!m_destroyedLayerIDs.isEmpty()) {
514         writeIndent(builder, 1);
515         builder.append("(destroyed-layers ");
516         for (size_t i = 0; i < m_destroyedLayerIDs.size(); ++i) {
517             if (i)
518                 builder.append(' ');
519             builder.appendNumber(m_destroyedLayerIDs[i]);
520         }
521         builder.append(")\n");
522     }
523     builder.append(")\n");
524
525     fprintf(stderr, "%s", builder.toString().utf8().data());
526 }
527
528 #endif // NDEBUG
529
530 } // namespace WebKit