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