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