Fix the Windows build after r200602, and do some minor tidyup.
[WebKit-https.git] / Source / WebCore / platform / graphics / ca / win / PlatformCALayerWin.cpp
1 /*
2  * Copyright (C) 2011, 2014-2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #include "PlatformCALayerWin.h"
29
30 #include "AbstractCACFLayerTreeHost.h"
31 #include "FontCascade.h"
32 #include "GDIUtilities.h"
33 #include "GraphicsContext.h"
34 #include "PlatformCAAnimationWin.h"
35 #include "PlatformCALayerWinInternal.h"
36 #include "TextRun.h"
37 #include "TileController.h"
38 #include "WebCoreHeaderDetection.h"
39 #include "WebTiledBackingLayerWin.h"
40 #include <QuartzCore/CoreAnimationCF.h>
41 #include <WebKitSystemInterface/WebKitSystemInterface.h>
42 #include <wtf/CurrentTime.h>
43 #include <wtf/text/CString.h>
44 #include <wtf/text/StringBuilder.h>
45
46 using namespace WebCore;
47
48 PassRefPtr<PlatformCALayer> PlatformCALayerWin::create(LayerType layerType, PlatformCALayerClient* owner)
49 {
50     return adoptRef(new PlatformCALayerWin(layerType, nullptr, owner));
51 }
52
53 PassRefPtr<PlatformCALayer> PlatformCALayerWin::create(PlatformLayer* platformLayer, PlatformCALayerClient* owner)
54 {
55     return adoptRef(new PlatformCALayerWin(LayerTypeCustom, platformLayer, owner));
56 }
57
58 static CFStringRef toCACFLayerType(PlatformCALayer::LayerType type)
59 {
60     return (type == PlatformCALayer::LayerTypeTransformLayer) ? kCACFTransformLayer : kCACFLayer;
61 }
62
63 static CFStringRef toCACFFilterType(PlatformCALayer::FilterType type)
64 {
65     switch (type) {
66     case PlatformCALayer::Linear: return kCACFFilterLinear;
67     case PlatformCALayer::Nearest: return kCACFFilterNearest;
68     case PlatformCALayer::Trilinear: return kCACFFilterTrilinear;
69     default: return nullptr;
70     }
71 }
72
73 static AbstractCACFLayerTreeHost* layerTreeHostForLayer(const PlatformCALayer* layer)
74 {
75     // We need the AbstractCACFLayerTreeHost associated with this layer, which is stored in the UserData of the CACFContext
76     void* userData = wkCACFLayerGetContextUserData(layer->platformLayer());
77     if (!userData)
78         return nullptr;
79
80     return static_cast<AbstractCACFLayerTreeHost*>(userData);
81 }
82
83 static PlatformCALayerWinInternal* intern(const PlatformCALayer* layer)
84 {
85     return static_cast<PlatformCALayerWinInternal*>(CACFLayerGetUserData(layer->platformLayer()));
86 }
87
88 static PlatformCALayerWinInternal* intern(void* layer)
89 {
90     return static_cast<PlatformCALayerWinInternal*>(CACFLayerGetUserData(static_cast<CACFLayerRef>(layer)));
91 }
92
93 PlatformCALayer* PlatformCALayer::platformCALayer(void* platformLayer)
94 {
95     if (!platformLayer)
96         return nullptr;
97     
98     PlatformCALayerWinInternal* layerIntern = intern(platformLayer);
99     return layerIntern ? layerIntern->owner() : nullptr;
100 }
101
102 PlatformCALayer::RepaintRectList PlatformCALayer::collectRectsToPaint(CGContextRef, PlatformCALayer*)
103 {
104     // FIXME: We should actually collect rects to use instead of defaulting to Windows'
105     // normal drawing path.
106     PlatformCALayer::RepaintRectList dirtyRects;
107     return dirtyRects;
108 }
109
110 void PlatformCALayer::drawLayerContents(CGContextRef context, WebCore::PlatformCALayer* platformCALayer, RepaintRectList&)
111 {
112     intern(platformCALayer)->displayCallback(platformCALayer->platformLayer(), context);
113 }
114
115 CGRect PlatformCALayer::frameForLayer(const PlatformLayer* tileLayer)
116 {
117     return CACFLayerGetFrame(static_cast<CACFLayerRef>(const_cast<PlatformLayer*>(tileLayer)));
118 }
119
120 static void displayCallback(CACFLayerRef caLayer, CGContextRef context)
121 {
122     ASSERT_ARG(caLayer, CACFLayerGetUserData(caLayer));
123     intern(caLayer)->displayCallback(caLayer, context);
124 }
125
126 static void layoutSublayersProc(CACFLayerRef caLayer) 
127 {
128     PlatformCALayer* layer = PlatformCALayer::platformCALayer(caLayer);
129     if (layer && layer->owner())
130         layer->owner()->platformCALayerLayoutSublayersOfLayer(layer);
131 }
132
133 PlatformCALayerWin::PlatformCALayerWin(LayerType layerType, PlatformLayer* layer, PlatformCALayerClient* owner)
134     : PlatformCALayer(layer ? LayerTypeCustom : layerType, owner)
135     , m_customAppearance(GraphicsLayer::NoCustomAppearance)
136 {
137     if (layer) {
138         m_layer = layer;
139         return;
140     }
141
142     m_layer = adoptCF(CACFLayerCreate(toCACFLayerType(layerType)));
143
144 #if HAVE(CACFLAYER_SETCONTENTSSCALE)
145     CACFLayerSetContentsScale(m_layer.get(), owner ? owner->platformCALayerDeviceScaleFactor() : deviceScaleFactorForWindow(nullptr));
146 #endif
147
148     // Create the PlatformCALayerWinInternal object and point to it in the userdata.
149     PlatformCALayerWinInternal* intern = nullptr;
150
151     if (usesTiledBackingLayer()) {
152         intern = new WebTiledBackingLayerWin(this);
153         TileController* tileController = reinterpret_cast<WebTiledBackingLayerWin*>(intern)->createTileController(this);
154         m_customSublayers = std::make_unique<PlatformCALayerList>(tileController->containerLayers());
155     } else
156         intern = new PlatformCALayerWinInternal(this);
157
158     CACFLayerSetUserData(m_layer.get(), intern);
159
160     // Set the display callback
161     CACFLayerSetDisplayCallback(m_layer.get(), displayCallback);
162     CACFLayerSetLayoutCallback(m_layer.get(), layoutSublayersProc);
163 }
164
165 PlatformCALayerWin::~PlatformCALayerWin()
166 {
167     // Toss all the kids
168     removeAllSublayers();
169
170     PlatformCALayerWinInternal* layerIntern = intern(this);
171     if (usesTiledBackingLayer())
172         reinterpret_cast<WebTiledBackingLayerWin*>(layerIntern)->invalidate();
173
174     // Get rid of the user data
175     CACFLayerSetUserData(m_layer.get(), nullptr);
176
177     CACFLayerRemoveFromSuperlayer(m_layer.get());
178
179     delete layerIntern;
180 }
181
182 PassRefPtr<PlatformCALayer> PlatformCALayerWin::clone(PlatformCALayerClient* owner) const
183 {
184     PlatformCALayer::LayerType type = (layerType() == PlatformCALayer::LayerTypeTransformLayer) ?
185         PlatformCALayer::LayerTypeTransformLayer : PlatformCALayer::LayerTypeLayer;
186     RefPtr<PlatformCALayer> newLayer = PlatformCALayerWin::create(type, owner);
187
188     newLayer->setPosition(position());
189     newLayer->setBounds(bounds());
190     newLayer->setAnchorPoint(anchorPoint());
191     newLayer->setTransform(transform());
192     newLayer->setSublayerTransform(sublayerTransform());
193     newLayer->setContents(contents());
194     newLayer->setMasksToBounds(masksToBounds());
195     newLayer->setDoubleSided(isDoubleSided());
196     newLayer->setOpaque(isOpaque());
197     newLayer->setBackgroundColor(backgroundColor());
198     newLayer->setContentsScale(contentsScale());
199     newLayer->copyFiltersFrom(*this);
200
201     return newLayer;
202 }
203
204 PlatformCALayer* PlatformCALayerWin::rootLayer() const
205 {
206     AbstractCACFLayerTreeHost* host = layerTreeHostForLayer(this);
207     return host ? host->rootLayer() : nullptr;
208 }
209
210 void PlatformCALayerWin::animationStarted(const String& animationKey, CFTimeInterval beginTime)
211 {
212     // Update start time for any animation not yet started
213     CFTimeInterval cacfBeginTime = currentTimeToMediaTime(beginTime);
214
215     HashMap<String, RefPtr<PlatformCAAnimation> >::const_iterator end = m_animations.end();
216     for (HashMap<String, RefPtr<PlatformCAAnimation> >::const_iterator it = m_animations.begin(); it != end; ++it)
217         it->value->setActualStartTimeIfNeeded(cacfBeginTime);
218
219     if (m_owner)
220         m_owner->platformCALayerAnimationStarted(animationKey, beginTime);
221 }
222
223 void PlatformCALayerWin::animationEnded(const String& animationKey)
224 {
225     if (m_owner)
226         m_owner->platformCALayerAnimationEnded(animationKey);
227 }
228
229 void PlatformCALayerWin::setNeedsDisplayInRect(const FloatRect& dirtyRect)
230 {
231     intern(this)->setNeedsDisplayInRect(dirtyRect);
232 }
233
234 void PlatformCALayerWin::setNeedsDisplay()
235 {
236     intern(this)->setNeedsDisplay();
237 }
238
239 void PlatformCALayerWin::setNeedsCommit()
240 {
241     AbstractCACFLayerTreeHost* host = layerTreeHostForLayer(this);
242     if (host)
243         host->layerTreeDidChange();
244 }
245
246 void PlatformCALayerWin::copyContentsFromLayer(PlatformCALayer* source)
247 {
248     if (source) {
249         RetainPtr<CFTypeRef> contents = CACFLayerGetContents(source->platformLayer());
250         CACFLayerSetContents(m_layer.get(), contents.get());
251     } else
252         CACFLayerSetContents(m_layer.get(), nullptr);
253
254     setNeedsCommit();
255 }
256
257 void PlatformCALayerWin::setNeedsLayout()
258 {
259     if (!m_owner || !m_owner->platformCALayerRespondsToLayoutChanges())
260         return;
261
262     CACFLayerSetNeedsLayout(m_layer.get());
263     setNeedsCommit();
264 }
265
266 PlatformCALayer* PlatformCALayerWin::superlayer() const
267 {
268     return platformCALayer(CACFLayerGetSuperlayer(m_layer.get()));
269 }
270
271 void PlatformCALayerWin::removeFromSuperlayer()
272 {
273     CACFLayerRemoveFromSuperlayer(m_layer.get());
274     setNeedsCommit();
275 }
276
277 void PlatformCALayerWin::setSublayers(const PlatformCALayerList& list)
278 {
279     intern(this)->setSublayers(list);
280 }
281
282 void PlatformCALayerWin::removeAllSublayers()
283 {
284     intern(this)->removeAllSublayers();
285 }
286
287 void PlatformCALayerWin::appendSublayer(PlatformCALayer& layer)
288 {
289     // This must be in terms of insertSublayer instead of a direct call so PlatformCALayerInternal can override.
290     insertSublayer(layer, intern(this)->sublayerCount());
291 }
292
293 void PlatformCALayerWin::insertSublayer(PlatformCALayer& layer, size_t index)
294 {
295     intern(this)->insertSublayer(layer, index);
296 }
297
298 void PlatformCALayerWin::replaceSublayer(PlatformCALayer& reference, PlatformCALayer& newLayer)
299 {
300     // This must not use direct calls to allow PlatformCALayerInternal to override.
301     ASSERT_ARG(reference, reference.superlayer() == this);
302
303     if (&reference == &newLayer)
304         return;
305
306     int referenceIndex = intern(this)->indexOfSublayer(&reference);
307     ASSERT(referenceIndex != -1);
308     if (referenceIndex == -1)
309         return;
310
311     reference.removeFromSuperlayer();
312
313     newLayer.removeFromSuperlayer();
314     insertSublayer(newLayer, referenceIndex);
315 }
316
317 void PlatformCALayerWin::adoptSublayers(PlatformCALayer& source)
318 {
319     PlatformCALayerList sublayers;
320     intern(&source)->getSublayers(sublayers);
321
322     // Use setSublayers() because it properly nulls out the superlayer pointers.
323     setSublayers(sublayers);
324 }
325
326 void PlatformCALayerWin::addAnimationForKey(const String& key, PlatformCAAnimation& animation)
327 {
328     // Add it to the animation list
329     m_animations.add(key, &animation);
330
331     CACFLayerAddAnimation(m_layer.get(), key.createCFString().get(), downcast<PlatformCAAnimationWin>(animation).platformAnimation());
332     setNeedsCommit();
333
334     // Tell the host about it so we can fire the start animation event
335     AbstractCACFLayerTreeHost* host = layerTreeHostForLayer(this);
336     if (host)
337         host->addPendingAnimatedLayer(this);
338 }
339
340 void PlatformCALayerWin::removeAnimationForKey(const String& key)
341 {
342     // Remove it from the animation list
343     m_animations.remove(key);
344
345     CACFLayerRemoveAnimation(m_layer.get(), key.createCFString().get());
346
347     // We don't "remove" a layer from AbstractCACFLayerTreeHost when it loses an animation.
348     // There may be other active animations on the layer and if an animation
349     // callback is fired on a layer without any animations no harm is done.
350
351     setNeedsCommit();
352 }
353
354 PassRefPtr<PlatformCAAnimation> PlatformCALayerWin::animationForKey(const String& key)
355 {
356     HashMap<String, RefPtr<PlatformCAAnimation> >::iterator it = m_animations.find(key);
357     if (it == m_animations.end())
358         return nullptr;
359
360     return it->value;
361 }
362
363 void PlatformCALayerWin::setMask(PlatformCALayer* layer)
364 {
365     CACFLayerSetMask(m_layer.get(), layer ? layer->platformLayer() : 0);
366     setNeedsCommit();
367 }
368
369 bool PlatformCALayerWin::isOpaque() const
370 {
371     return intern(this)->isOpaque();
372 }
373
374 void PlatformCALayerWin::setOpaque(bool value)
375 {
376     intern(this)->setOpaque(value);
377     setNeedsCommit();
378 }
379
380 FloatRect PlatformCALayerWin::bounds() const
381 {
382     return CACFLayerGetBounds(m_layer.get());
383 }
384
385 void PlatformCALayerWin::setBounds(const FloatRect& value)
386 {
387     intern(this)->setBounds(value);
388     setNeedsLayout();
389 }
390
391 FloatPoint3D PlatformCALayerWin::position() const
392 {
393     CGPoint point = CACFLayerGetPosition(m_layer.get());
394     return FloatPoint3D(point.x, point.y, CACFLayerGetZPosition(m_layer.get()));
395 }
396
397 void PlatformCALayerWin::setPosition(const FloatPoint3D& value)
398 {
399     CACFLayerSetPosition(m_layer.get(), CGPointMake(value.x(), value.y()));
400     CACFLayerSetZPosition(m_layer.get(), value.z());
401     setNeedsCommit();
402 }
403
404 FloatPoint3D PlatformCALayerWin::anchorPoint() const
405 {
406     CGPoint point = CACFLayerGetAnchorPoint(m_layer.get());
407     float z = CACFLayerGetAnchorPointZ(m_layer.get());
408     return FloatPoint3D(point.x, point.y, z);
409 }
410
411 void PlatformCALayerWin::setAnchorPoint(const FloatPoint3D& value)
412 {
413     CACFLayerSetAnchorPoint(m_layer.get(), CGPointMake(value.x(), value.y()));
414     CACFLayerSetAnchorPointZ(m_layer.get(), value.z());
415     setNeedsCommit();
416 }
417
418 TransformationMatrix PlatformCALayerWin::transform() const
419 {
420     return CACFLayerGetTransform(m_layer.get());
421 }
422
423 void PlatformCALayerWin::setTransform(const TransformationMatrix& value)
424 {
425     CACFLayerSetTransform(m_layer.get(), value);
426     setNeedsCommit();
427 }
428
429 TransformationMatrix PlatformCALayerWin::sublayerTransform() const
430 {
431     return CACFLayerGetSublayerTransform(m_layer.get());
432 }
433
434 void PlatformCALayerWin::setSublayerTransform(const TransformationMatrix& value)
435 {
436     CACFLayerSetSublayerTransform(m_layer.get(), value);
437     setNeedsCommit();
438 }
439
440 bool PlatformCALayerWin::isHidden() const
441 {
442     return CACFLayerIsHidden(m_layer.get());
443 }
444
445 void PlatformCALayerWin::setHidden(bool value)
446 {
447     CACFLayerSetHidden(m_layer.get(), value);
448     setNeedsCommit();
449 }
450
451 bool PlatformCALayerWin::contentsHidden() const
452 {
453     return false;
454 }
455
456 void PlatformCALayerWin::setContentsHidden(bool)
457 {
458 }
459
460 void PlatformCALayerWin::setBackingStoreAttached(bool)
461 {
462 }
463
464 bool PlatformCALayerWin::backingStoreAttached() const
465 {
466     return true;
467 }
468
469 bool PlatformCALayerWin::geometryFlipped() const
470 {
471     return CACFLayerIsGeometryFlipped(m_layer.get());
472 }
473
474 void PlatformCALayerWin::setGeometryFlipped(bool value)
475 {
476     CACFLayerSetGeometryFlipped(m_layer.get(), value);
477     setNeedsCommit();
478 }
479
480 bool PlatformCALayerWin::isDoubleSided() const
481 {
482     return CACFLayerIsDoubleSided(m_layer.get());
483 }
484
485 void PlatformCALayerWin::setDoubleSided(bool value)
486 {
487     CACFLayerSetDoubleSided(m_layer.get(), value);
488     setNeedsCommit();
489 }
490
491 bool PlatformCALayerWin::masksToBounds() const
492 {
493     return CACFLayerGetMasksToBounds(m_layer.get());
494 }
495
496 void PlatformCALayerWin::setMasksToBounds(bool value)
497 {
498     CACFLayerSetMasksToBounds(m_layer.get(), value);
499     setNeedsCommit();
500 }
501
502 bool PlatformCALayerWin::acceleratesDrawing() const
503 {
504     return false;
505 }
506
507 void PlatformCALayerWin::setAcceleratesDrawing(bool)
508 {
509 }
510
511 CFTypeRef PlatformCALayerWin::contents() const
512 {
513     return CACFLayerGetContents(m_layer.get());
514 }
515
516 void PlatformCALayerWin::setContents(CFTypeRef value)
517 {
518     CACFLayerSetContents(m_layer.get(), value);
519     setNeedsCommit();
520 }
521
522 void PlatformCALayerWin::setContentsRect(const FloatRect& value)
523 {
524     CACFLayerSetContentsRect(m_layer.get(), value);
525     setNeedsCommit();
526 }
527
528 void PlatformCALayerWin::setMinificationFilter(FilterType value)
529 {
530     CACFLayerSetMinificationFilter(m_layer.get(), toCACFFilterType(value));
531 }
532
533 void PlatformCALayerWin::setMagnificationFilter(FilterType value)
534 {
535     CACFLayerSetMagnificationFilter(m_layer.get(), toCACFFilterType(value));
536     setNeedsCommit();
537 }
538
539 Color PlatformCALayerWin::backgroundColor() const
540 {
541     return CACFLayerGetBackgroundColor(m_layer.get());
542 }
543
544 void PlatformCALayerWin::setBackgroundColor(const Color& value)
545 {
546     CGFloat components[4];
547     value.getRGBA(components[0], components[1], components[2], components[3]);
548
549     RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
550     RetainPtr<CGColorRef> color = adoptCF(CGColorCreate(colorSpace.get(), components));
551
552     CACFLayerSetBackgroundColor(m_layer.get(), color.get());
553     setNeedsCommit();
554 }
555
556 void PlatformCALayerWin::setBorderWidth(float value)
557 {
558     intern(this)->setBorderWidth(value);
559     setNeedsCommit();
560 }
561
562 void PlatformCALayerWin::setBorderColor(const Color& value)
563 {
564     intern(this)->setBorderColor(value);
565     setNeedsCommit();
566 }
567
568 float PlatformCALayerWin::opacity() const
569 {
570     return CACFLayerGetOpacity(m_layer.get());
571 }
572
573 void PlatformCALayerWin::setOpacity(float value)
574 {
575     CACFLayerSetOpacity(m_layer.get(), value);
576     setNeedsCommit();
577 }
578
579 void PlatformCALayerWin::setFilters(const FilterOperations&)
580 {
581 }
582
583 void PlatformCALayerWin::copyFiltersFrom(const PlatformCALayer&)
584 {
585 }
586
587 void PlatformCALayerWin::setName(const String& value)
588 {
589     CACFLayerSetName(m_layer.get(), value.createCFString().get());
590     setNeedsCommit();
591 }
592
593 void PlatformCALayerWin::setSpeed(float value)
594 {
595     CACFLayerSetSpeed(m_layer.get(), value);
596     setNeedsCommit();
597 }
598
599 void PlatformCALayerWin::setTimeOffset(CFTimeInterval value)
600 {
601     CACFLayerSetTimeOffset(m_layer.get(), value);
602     setNeedsCommit();
603 }
604
605 void PlatformCALayerWin::setEdgeAntialiasingMask(unsigned mask)
606 {
607     CACFLayerSetEdgeAntialiasingMask(m_layer.get(), mask);
608     setNeedsCommit();
609 }
610
611 float PlatformCALayerWin::contentsScale() const
612 {
613     return intern(this)->contentsScale();
614 }
615
616 void PlatformCALayerWin::setContentsScale(float scaleFactor)
617 {
618     intern(this)->setContentsScale(scaleFactor);
619     setNeedsCommit();
620 }
621
622 float PlatformCALayerWin::cornerRadius() const
623 {
624     return 0; // FIXME: implement.
625 }
626
627 void PlatformCALayerWin::setCornerRadius(float value)
628 {
629     // FIXME: implement.
630 }
631
632 FloatRoundedRect PlatformCALayerWin::shapeRoundedRect() const
633 {
634     // FIXME: implement.
635     return FloatRoundedRect();
636 }
637
638 void PlatformCALayerWin::setShapeRoundedRect(const FloatRoundedRect&)
639 {
640     // FIXME: implement.
641 }
642
643 WindRule PlatformCALayerWin::shapeWindRule() const
644 {
645     // FIXME: implement.
646     return RULE_NONZERO;
647 }
648
649 void PlatformCALayerWin::setShapeWindRule(WindRule)
650 {
651     // FIXME: implement.
652 }
653
654 Path PlatformCALayerWin::shapePath() const
655 {
656     // FIXME: implement.
657     return Path();
658 }
659
660 void PlatformCALayerWin::setShapePath(const Path&)
661 {
662     // FIXME: implement.
663 }
664
665 static void printIndent(StringBuilder& builder, int indent)
666 {
667     for ( ; indent > 0; --indent)
668         builder.append("  ");
669 }
670
671 static void printTransform(StringBuilder& builder, const CATransform3D& transform)
672 {
673     builder.append('[');
674     builder.appendNumber(transform.m11);
675     builder.append(' ');
676     builder.appendNumber(transform.m12);
677     builder.append(' ');
678     builder.appendNumber(transform.m13);
679     builder.append(' ');
680     builder.appendNumber(transform.m14);
681     builder.append("; ");
682     builder.appendNumber(transform.m21);
683     builder.append(' ');
684     builder.appendNumber(transform.m22);
685     builder.append(' ');
686     builder.appendNumber(transform.m23);
687     builder.append(' ');
688     builder.appendNumber(transform.m24);
689     builder.append("; ");
690     builder.appendNumber(transform.m31);
691     builder.append(' ');
692     builder.appendNumber(transform.m32);
693     builder.append(' ');
694     builder.appendNumber(transform.m33);
695     builder.append(' ');
696     builder.appendNumber(transform.m34);
697     builder.append("; ");
698     builder.appendNumber(transform.m41);
699     builder.append(' ');
700     builder.appendNumber(transform.m42);
701     builder.append(' ');
702     builder.appendNumber(transform.m43);
703     builder.append(' ');
704     builder.appendNumber(transform.m44);
705     builder.append(']');
706 }
707
708 static void printColor(StringBuilder& builder, int indent, const String& label, CGColorRef color)
709 {
710     Color layerColor(color);
711     if (!layerColor.isValid())
712         return;
713
714     builder.append('\n');
715     printIndent(builder, indent);
716     builder.append('(');
717     builder.append(label);
718     builder.append(' ');
719     builder.append(layerColor.nameForRenderTreeAsText());
720     builder.append(')');
721 }
722
723 static void printLayer(StringBuilder& builder, const PlatformCALayer* layer, int indent)
724 {
725     FloatPoint3D layerPosition = layer->position();
726     FloatPoint3D layerAnchorPoint = layer->anchorPoint();
727     FloatRect layerBounds = layer->bounds();
728     builder.append('\n');
729     printIndent(builder, indent);
730
731     char* layerTypeName = nullptr;
732     switch (layer->layerType()) {
733     case PlatformCALayer::LayerTypeLayer: layerTypeName = "layer"; break;
734     case PlatformCALayer::LayerTypeWebLayer: layerTypeName = "web-layer"; break;
735     case PlatformCALayer::LayerTypeSimpleLayer: layerTypeName = "simple-layer"; break;
736     case PlatformCALayer::LayerTypeTransformLayer: layerTypeName = "transform-layer"; break;
737     case PlatformCALayer::LayerTypeWebTiledLayer: layerTypeName = "web-tiled-layer"; break;
738     case PlatformCALayer::LayerTypeTiledBackingLayer: layerTypeName = "tiled-backing-layer"; break;
739     case PlatformCALayer::LayerTypePageTiledBackingLayer: layerTypeName = "page-tiled-backing-layer"; break;
740     case PlatformCALayer::LayerTypeTiledBackingTileLayer: layerTypeName = "tiled-backing-tile-layer"; break;
741     case PlatformCALayer::LayerTypeRootLayer: layerTypeName = "root-layer"; break;
742     case PlatformCALayer::LayerTypeAVPlayerLayer: layerTypeName = "avplayer-layer"; break;
743     case PlatformCALayer::LayerTypeWebGLLayer: layerTypeName = "webgl-layer"; break;
744     case PlatformCALayer::LayerTypeBackdropLayer: layerTypeName = "backdrop-layer"; break;
745     case PlatformCALayer::LayerTypeShapeLayer: layerTypeName = "shape-layer"; break;
746     case PlatformCALayer::LayerTypeLightSystemBackdropLayer: layerTypeName = "light-system-backdrop-layer"; break;
747     case PlatformCALayer::LayerTypeDarkSystemBackdropLayer: layerTypeName = "dark-system-backdrop-layer"; break;
748     case PlatformCALayer::LayerTypeScrollingLayer: layerTypeName = "scrolling-layer"; break;
749     case PlatformCALayer::LayerTypeCustom: layerTypeName = "custom-layer"; break;
750     }
751
752     builder.append("(");
753     builder.append(layerTypeName);
754     builder.append(" [");
755     builder.appendNumber(layerPosition.x());
756     builder.append(' ');
757     builder.appendNumber(layerPosition.y());
758     builder.append(' ');
759     builder.appendNumber(layerPosition.z());
760     builder.append("] [");
761     builder.appendNumber(layerBounds.x());
762     builder.append(' ');
763     builder.appendNumber(layerBounds.y());
764     builder.append(' ');
765     builder.appendNumber(layerBounds.width());
766     builder.append(' ');
767     builder.appendNumber(layerBounds.height());
768     builder.append("] [");
769     builder.appendNumber(layerAnchorPoint.x());
770     builder.append(' ');
771     builder.appendNumber(layerAnchorPoint.y());
772     builder.append(' ');
773     builder.appendNumber(layerAnchorPoint.z());
774     builder.append("] superlayer=");
775     builder.appendNumber(reinterpret_cast<unsigned long long>(layer->superlayer()));
776
777     // Print name if needed
778     String layerName = CACFLayerGetName(layer->platformLayer());
779     if (!layerName.isEmpty()) {
780         builder.append('\n');
781         printIndent(builder, indent + 1);
782         builder.append("(name \"");
783         builder.append(layerName);
784         builder.append("\")");
785     }
786
787     // Print borderWidth if needed
788     if (CGFloat borderWidth = CACFLayerGetBorderWidth(layer->platformLayer())) {
789         builder.append('\n');
790         printIndent(builder, indent + 1);
791         builder.append("(borderWidth ");
792         builder.appendNumber(borderWidth);
793         builder.append(')');
794     }
795
796     // Print backgroundColor if needed
797     printColor(builder, indent + 1, "backgroundColor", CACFLayerGetBackgroundColor(layer->platformLayer()));
798
799     // Print borderColor if needed
800     printColor(builder, indent + 1, "borderColor", CACFLayerGetBorderColor(layer->platformLayer()));
801
802     // Print masksToBounds if needed
803     if (bool layerMasksToBounds = layer->masksToBounds()) {
804         builder.append('\n');
805         printIndent(builder, indent + 1);
806         builder.append("(masksToBounds true)");
807     }
808
809     if (bool geometryFlipped = layer->geometryFlipped()) {
810         builder.append('\n');
811         printIndent(builder, indent + 1);
812         builder.append("(geometryFlipped true)");
813     }
814
815     // Print opacity if needed
816     float layerOpacity = layer->opacity();
817     if (layerOpacity != 1) {
818         builder.append('\n');
819         printIndent(builder, indent + 1);
820         builder.append("(opacity ");
821         builder.appendNumber(layerOpacity);
822         builder.append(')');
823     }
824
825     // Print sublayerTransform if needed
826     TransformationMatrix layerTransform = layer->sublayerTransform();
827     if (!layerTransform.isIdentity()) {
828         builder.append('\n');
829         printIndent(builder, indent + 1);
830         builder.append("(sublayerTransform ");
831         printTransform(builder, layerTransform);
832         builder.append(')');
833     }
834
835     // Print transform if needed
836     layerTransform = layer->transform();
837     if (!layerTransform.isIdentity()) {
838         builder.append('\n');
839         printIndent(builder, indent + 1);
840         builder.append("(transform ");
841         printTransform(builder, layerTransform);
842         builder.append(')');
843     }
844
845     // Print contents if needed
846     if (CFTypeRef layerContents = layer->contents()) {
847         if (CFGetTypeID(layerContents) == CGImageGetTypeID()) {
848             CGImageRef imageContents = static_cast<CGImageRef>(const_cast<void*>(layerContents));
849             builder.append('\n');
850             printIndent(builder, indent + 1);
851             builder.append("(contents (image [");
852             builder.appendNumber(CGImageGetWidth(imageContents));
853             builder.append(' ');
854             builder.appendNumber(CGImageGetHeight(imageContents));
855             builder.append("]))");
856         }
857
858         if (CFGetTypeID(layerContents) == CABackingStoreGetTypeID()) {
859             CABackingStoreRef backingStore = static_cast<CABackingStoreRef>(const_cast<void*>(layerContents));
860             CGImageRef imageContents = CABackingStoreGetCGImage(backingStore);
861             builder.append('\n');
862             printIndent(builder, indent + 1);
863             builder.append("(contents (backing-store [");
864             builder.appendNumber(CGImageGetWidth(imageContents));
865             builder.append(' ');
866             builder.appendNumber(CGImageGetHeight(imageContents));
867             builder.append("]))");
868         }
869     }
870
871     // Print sublayers if needed
872     int n = intern(layer)->sublayerCount();
873     if (n > 0) {
874         builder.append('\n');
875         printIndent(builder, indent + 1);
876         builder.append("(sublayers");
877
878         PlatformCALayerList sublayers;
879         intern(layer)->getSublayers(sublayers);
880         ASSERT(n == sublayers.size());
881         for (int i = 0; i < n; ++i)
882             printLayer(builder, sublayers[i].get(), indent + 2);
883
884         builder.append(')');
885     }
886
887     builder.append(')');
888 }
889
890 String PlatformCALayerWin::layerTreeAsString() const
891 {
892     // Print heading info
893     CGRect rootBounds = bounds();
894
895     StringBuilder builder;
896     builder.append("\n\n** Render tree at time ");
897     builder.appendNumber(monotonicallyIncreasingTime());
898     builder.append(" (bounds ");
899     builder.appendNumber(rootBounds.origin.x);
900     builder.append(", ");
901     builder.appendNumber(rootBounds.origin.y);
902     builder.append(' ');
903     builder.appendNumber(rootBounds.size.width);
904     builder.append('x');
905     builder.appendNumber(rootBounds.size.height);
906     builder.append(") **\n\n");
907
908     // Print layer tree from the root
909     printLayer(builder, this, 0);
910
911     return builder.toString();
912 }
913
914 PassRefPtr<PlatformCALayer> PlatformCALayerWin::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
915 {
916     return PlatformCALayerWin::create(layerType, client);
917 }
918
919 TiledBacking* PlatformCALayerWin::tiledBacking()
920 {
921     if (!usesTiledBackingLayer())
922         return nullptr;
923
924     return reinterpret_cast<WebTiledBackingLayerWin*>(intern(this))->tiledBacking();
925 }
926
927 void PlatformCALayerWin::drawTextAtPoint(CGContextRef context, CGFloat x, CGFloat y, CGSize scale, CGFloat fontSize, const char* message, size_t length) const
928 {
929     String text(message, length);
930
931     FontCascadeDescription desc;
932
933     NONCLIENTMETRICS metrics;
934     metrics.cbSize = sizeof(metrics);
935     SystemParametersInfo(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
936     desc.setOneFamily(metrics.lfSmCaptionFont.lfFaceName);
937
938     desc.setComputedSize(scale.width * fontSize);
939
940     FontCascade font = FontCascade(desc, 0, 0);
941     font.update(nullptr);
942
943     GraphicsContext cg(context);
944     cg.setFillColor(Color::black);
945     cg.drawText(font, TextRun(text), IntPoint(x, y));
946 }