d680ec908d776349b6e7c4622bee325f64270c7a
[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 <WebKitSystemInterface/WebKitSystemInterface.h>
43 #include <wtf/CurrentTime.h>
44 #include <wtf/text/CString.h>
45 #include <wtf/text/StringBuilder.h>
46
47 using namespace WebCore;
48
49 Ref<PlatformCALayer> PlatformCALayerWin::create(LayerType layerType, PlatformCALayerClient* owner)
50 {
51     return adoptRef(*new PlatformCALayerWin(layerType, nullptr, owner));
52 }
53
54 Ref<PlatformCALayer> PlatformCALayerWin::create(PlatformLayer* platformLayer, PlatformCALayerClient* owner)
55 {
56     return adoptRef(*new PlatformCALayerWin(LayerTypeCustom, platformLayer, owner));
57 }
58
59 static CFStringRef toCACFLayerType(PlatformCALayer::LayerType type)
60 {
61     return (type == PlatformCALayer::LayerTypeTransformLayer) ? kCACFTransformLayer : kCACFLayer;
62 }
63
64 static CFStringRef toCACFFilterType(PlatformCALayer::FilterType type)
65 {
66     switch (type) {
67     case PlatformCALayer::Linear: return kCACFFilterLinear;
68     case PlatformCALayer::Nearest: return kCACFFilterNearest;
69     case PlatformCALayer::Trilinear: return kCACFFilterTrilinear;
70     default: return nullptr;
71     }
72 }
73
74 static AbstractCACFLayerTreeHost* layerTreeHostForLayer(const PlatformCALayer* layer)
75 {
76     // We need the AbstractCACFLayerTreeHost associated with this layer, which is stored in the UserData of the CACFContext
77     void* userData = wkCACFLayerGetContextUserData(layer->platformLayer());
78     if (!userData)
79         return nullptr;
80
81     return static_cast<AbstractCACFLayerTreeHost*>(userData);
82 }
83
84 static PlatformCALayerWinInternal* intern(const PlatformCALayer* layer)
85 {
86     return static_cast<PlatformCALayerWinInternal*>(CACFLayerGetUserData(layer->platformLayer()));
87 }
88
89 static PlatformCALayerWinInternal* intern(void* layer)
90 {
91     return static_cast<PlatformCALayerWinInternal*>(CACFLayerGetUserData(static_cast<CACFLayerRef>(layer)));
92 }
93
94 PlatformCALayer* PlatformCALayer::platformCALayer(void* platformLayer)
95 {
96     if (!platformLayer)
97         return nullptr;
98     
99     PlatformCALayerWinInternal* layerIntern = intern(platformLayer);
100     return layerIntern ? layerIntern->owner() : nullptr;
101 }
102
103 PlatformCALayer::RepaintRectList PlatformCALayer::collectRectsToPaint(CGContextRef, PlatformCALayer*)
104 {
105     // FIXME: We should actually collect rects to use instead of defaulting to Windows'
106     // normal drawing path.
107     PlatformCALayer::RepaintRectList dirtyRects;
108     return dirtyRects;
109 }
110
111 void PlatformCALayer::drawLayerContents(CGContextRef context, WebCore::PlatformCALayer* platformCALayer, RepaintRectList&, GraphicsLayerPaintBehavior)
112 {
113     intern(platformCALayer)->displayCallback(platformCALayer->platformLayer(), context);
114 }
115
116 CGRect PlatformCALayer::frameForLayer(const PlatformLayer* tileLayer)
117 {
118     return CACFLayerGetFrame(static_cast<CACFLayerRef>(const_cast<PlatformLayer*>(tileLayer)));
119 }
120
121 static void displayCallback(CACFLayerRef caLayer, CGContextRef context)
122 {
123     ASSERT_ARG(caLayer, CACFLayerGetUserData(caLayer));
124     intern(caLayer)->displayCallback(caLayer, context);
125 }
126
127 static void layoutSublayersProc(CACFLayerRef caLayer) 
128 {
129     PlatformCALayer* layer = PlatformCALayer::platformCALayer(caLayer);
130     if (layer && layer->owner())
131         layer->owner()->platformCALayerLayoutSublayersOfLayer(layer);
132 }
133
134 PlatformCALayerWin::PlatformCALayerWin(LayerType layerType, PlatformLayer* layer, PlatformCALayerClient* owner)
135     : PlatformCALayer(layer ? LayerTypeCustom : layerType, owner)
136     , m_customAppearance(GraphicsLayer::NoCustomAppearance)
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 = std::make_unique<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 CFTypeRef PlatformCALayerWin::contents() const
540 {
541     return CACFLayerGetContents(m_layer.get());
542 }
543
544 void PlatformCALayerWin::setContents(CFTypeRef value)
545 {
546     CACFLayerSetContents(m_layer.get(), value);
547     setNeedsCommit();
548 }
549
550 void PlatformCALayerWin::setContentsRect(const FloatRect& value)
551 {
552     CACFLayerSetContentsRect(m_layer.get(), value);
553     setNeedsCommit();
554 }
555
556 void PlatformCALayerWin::setMinificationFilter(FilterType value)
557 {
558     CACFLayerSetMinificationFilter(m_layer.get(), toCACFFilterType(value));
559 }
560
561 void PlatformCALayerWin::setMagnificationFilter(FilterType value)
562 {
563     CACFLayerSetMagnificationFilter(m_layer.get(), toCACFFilterType(value));
564     setNeedsCommit();
565 }
566
567 Color PlatformCALayerWin::backgroundColor() const
568 {
569     return CACFLayerGetBackgroundColor(m_layer.get());
570 }
571
572 void PlatformCALayerWin::setBackgroundColor(const Color& value)
573 {
574     CGFloat components[4];
575     value.getRGBA(components[0], components[1], components[2], components[3]);
576
577     RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
578     RetainPtr<CGColorRef> color = adoptCF(CGColorCreate(colorSpace.get(), components));
579
580     CACFLayerSetBackgroundColor(m_layer.get(), color.get());
581     setNeedsCommit();
582 }
583
584 void PlatformCALayerWin::setBorderWidth(float value)
585 {
586     intern(this)->setBorderWidth(value);
587     setNeedsCommit();
588 }
589
590 void PlatformCALayerWin::setBorderColor(const Color& value)
591 {
592     intern(this)->setBorderColor(value);
593     setNeedsCommit();
594 }
595
596 float PlatformCALayerWin::opacity() const
597 {
598     return CACFLayerGetOpacity(m_layer.get());
599 }
600
601 void PlatformCALayerWin::setOpacity(float value)
602 {
603     CACFLayerSetOpacity(m_layer.get(), value);
604     setNeedsCommit();
605 }
606
607 void PlatformCALayerWin::setFilters(const FilterOperations&)
608 {
609 }
610
611 void PlatformCALayerWin::copyFiltersFrom(const PlatformCALayer&)
612 {
613 }
614
615 void PlatformCALayerWin::setName(const String& value)
616 {
617     CACFLayerSetName(m_layer.get(), value.createCFString().get());
618     setNeedsCommit();
619 }
620
621 void PlatformCALayerWin::setSpeed(float value)
622 {
623     CACFLayerSetSpeed(m_layer.get(), value);
624     setNeedsCommit();
625 }
626
627 void PlatformCALayerWin::setTimeOffset(CFTimeInterval value)
628 {
629     CACFLayerSetTimeOffset(m_layer.get(), value);
630     setNeedsCommit();
631 }
632
633 void PlatformCALayerWin::setEdgeAntialiasingMask(unsigned mask)
634 {
635     CACFLayerSetEdgeAntialiasingMask(m_layer.get(), mask);
636     setNeedsCommit();
637 }
638
639 float PlatformCALayerWin::contentsScale() const
640 {
641     return intern(this)->contentsScale();
642 }
643
644 void PlatformCALayerWin::setContentsScale(float scaleFactor)
645 {
646     intern(this)->setContentsScale(scaleFactor);
647     setNeedsCommit();
648 }
649
650 float PlatformCALayerWin::cornerRadius() const
651 {
652     return 0; // FIXME: implement.
653 }
654
655 void PlatformCALayerWin::setCornerRadius(float value)
656 {
657     // FIXME: implement.
658 }
659
660 FloatRoundedRect PlatformCALayerWin::shapeRoundedRect() const
661 {
662     // FIXME: implement.
663     return FloatRoundedRect();
664 }
665
666 void PlatformCALayerWin::setShapeRoundedRect(const FloatRoundedRect&)
667 {
668     // FIXME: implement.
669 }
670
671 WindRule PlatformCALayerWin::shapeWindRule() const
672 {
673     // FIXME: implement.
674     return RULE_NONZERO;
675 }
676
677 void PlatformCALayerWin::setShapeWindRule(WindRule)
678 {
679     // FIXME: implement.
680 }
681
682 Path PlatformCALayerWin::shapePath() const
683 {
684     // FIXME: implement.
685     return Path();
686 }
687
688 void PlatformCALayerWin::setShapePath(const Path&)
689 {
690     // FIXME: implement.
691 }
692
693 static void printIndent(StringBuilder& builder, int indent)
694 {
695     for ( ; indent > 0; --indent)
696         builder.append("  ");
697 }
698
699 static void printTransform(StringBuilder& builder, const CATransform3D& transform)
700 {
701     builder.append('[');
702     builder.appendNumber(transform.m11);
703     builder.append(' ');
704     builder.appendNumber(transform.m12);
705     builder.append(' ');
706     builder.appendNumber(transform.m13);
707     builder.append(' ');
708     builder.appendNumber(transform.m14);
709     builder.append("; ");
710     builder.appendNumber(transform.m21);
711     builder.append(' ');
712     builder.appendNumber(transform.m22);
713     builder.append(' ');
714     builder.appendNumber(transform.m23);
715     builder.append(' ');
716     builder.appendNumber(transform.m24);
717     builder.append("; ");
718     builder.appendNumber(transform.m31);
719     builder.append(' ');
720     builder.appendNumber(transform.m32);
721     builder.append(' ');
722     builder.appendNumber(transform.m33);
723     builder.append(' ');
724     builder.appendNumber(transform.m34);
725     builder.append("; ");
726     builder.appendNumber(transform.m41);
727     builder.append(' ');
728     builder.appendNumber(transform.m42);
729     builder.append(' ');
730     builder.appendNumber(transform.m43);
731     builder.append(' ');
732     builder.appendNumber(transform.m44);
733     builder.append(']');
734 }
735
736 static void printColor(StringBuilder& builder, int indent, const String& label, CGColorRef color)
737 {
738     Color layerColor(color);
739     if (!layerColor.isValid())
740         return;
741
742     builder.append('\n');
743     printIndent(builder, indent);
744     builder.append('(');
745     builder.append(label);
746     builder.append(' ');
747     builder.append(layerColor.nameForRenderTreeAsText());
748     builder.append(')');
749 }
750
751 static void printLayer(StringBuilder& builder, const PlatformCALayer* layer, int indent)
752 {
753     FloatPoint3D layerPosition = layer->position();
754     FloatPoint3D layerAnchorPoint = layer->anchorPoint();
755     FloatRect layerBounds = layer->bounds();
756     builder.append('\n');
757     printIndent(builder, indent);
758
759     char* layerTypeName = nullptr;
760     switch (layer->layerType()) {
761     case PlatformCALayer::LayerTypeLayer: layerTypeName = "layer"; break;
762     case PlatformCALayer::LayerTypeWebLayer: layerTypeName = "web-layer"; break;
763     case PlatformCALayer::LayerTypeSimpleLayer: layerTypeName = "simple-layer"; break;
764     case PlatformCALayer::LayerTypeTransformLayer: layerTypeName = "transform-layer"; break;
765     case PlatformCALayer::LayerTypeTiledBackingLayer: layerTypeName = "tiled-backing-layer"; break;
766     case PlatformCALayer::LayerTypePageTiledBackingLayer: layerTypeName = "page-tiled-backing-layer"; break;
767     case PlatformCALayer::LayerTypeTiledBackingTileLayer: layerTypeName = "tiled-backing-tile-layer"; break;
768     case PlatformCALayer::LayerTypeRootLayer: layerTypeName = "root-layer"; break;
769     case PlatformCALayer::LayerTypeAVPlayerLayer: layerTypeName = "avplayer-layer"; break;
770     case PlatformCALayer::LayerTypeContentsProvidedLayer: layerTypeName = "contents-provided-layer"; break;
771     case PlatformCALayer::LayerTypeBackdropLayer: layerTypeName = "backdrop-layer"; break;
772     case PlatformCALayer::LayerTypeShapeLayer: layerTypeName = "shape-layer"; break;
773     case PlatformCALayer::LayerTypeLightSystemBackdropLayer: layerTypeName = "light-system-backdrop-layer"; break;
774     case PlatformCALayer::LayerTypeDarkSystemBackdropLayer: layerTypeName = "dark-system-backdrop-layer"; break;
775     case PlatformCALayer::LayerTypeScrollingLayer: layerTypeName = "scrolling-layer"; break;
776     case PlatformCALayer::LayerTypeCustom: layerTypeName = "custom-layer"; break;
777     }
778
779     builder.append("(");
780     builder.append(layerTypeName);
781     builder.append(" [");
782     builder.appendNumber(layerPosition.x());
783     builder.append(' ');
784     builder.appendNumber(layerPosition.y());
785     builder.append(' ');
786     builder.appendNumber(layerPosition.z());
787     builder.append("] [");
788     builder.appendNumber(layerBounds.x());
789     builder.append(' ');
790     builder.appendNumber(layerBounds.y());
791     builder.append(' ');
792     builder.appendNumber(layerBounds.width());
793     builder.append(' ');
794     builder.appendNumber(layerBounds.height());
795     builder.append("] [");
796     builder.appendNumber(layerAnchorPoint.x());
797     builder.append(' ');
798     builder.appendNumber(layerAnchorPoint.y());
799     builder.append(' ');
800     builder.appendNumber(layerAnchorPoint.z());
801     builder.append("] superlayer=");
802     builder.appendNumber(reinterpret_cast<unsigned long long>(layer->superlayer()));
803
804     // Print name if needed
805     String layerName = CACFLayerGetName(layer->platformLayer());
806     if (!layerName.isEmpty()) {
807         builder.append('\n');
808         printIndent(builder, indent + 1);
809         builder.append("(name \"");
810         builder.append(layerName);
811         builder.append("\")");
812     }
813
814     // Print borderWidth if needed
815     if (CGFloat borderWidth = CACFLayerGetBorderWidth(layer->platformLayer())) {
816         builder.append('\n');
817         printIndent(builder, indent + 1);
818         builder.append("(borderWidth ");
819         builder.appendNumber(borderWidth);
820         builder.append(')');
821     }
822
823     // Print backgroundColor if needed
824     printColor(builder, indent + 1, "backgroundColor", CACFLayerGetBackgroundColor(layer->platformLayer()));
825
826     // Print borderColor if needed
827     printColor(builder, indent + 1, "borderColor", CACFLayerGetBorderColor(layer->platformLayer()));
828
829     // Print masksToBounds if needed
830     if (bool layerMasksToBounds = layer->masksToBounds()) {
831         builder.append('\n');
832         printIndent(builder, indent + 1);
833         builder.append("(masksToBounds true)");
834     }
835
836     if (bool geometryFlipped = layer->geometryFlipped()) {
837         builder.append('\n');
838         printIndent(builder, indent + 1);
839         builder.append("(geometryFlipped true)");
840     }
841
842     // Print opacity if needed
843     float layerOpacity = layer->opacity();
844     if (layerOpacity != 1) {
845         builder.append('\n');
846         printIndent(builder, indent + 1);
847         builder.append("(opacity ");
848         builder.appendNumber(layerOpacity);
849         builder.append(')');
850     }
851
852     // Print sublayerTransform if needed
853     TransformationMatrix layerTransform = layer->sublayerTransform();
854     if (!layerTransform.isIdentity()) {
855         builder.append('\n');
856         printIndent(builder, indent + 1);
857         builder.append("(sublayerTransform ");
858         printTransform(builder, layerTransform);
859         builder.append(')');
860     }
861
862     // Print transform if needed
863     layerTransform = layer->transform();
864     if (!layerTransform.isIdentity()) {
865         builder.append('\n');
866         printIndent(builder, indent + 1);
867         builder.append("(transform ");
868         printTransform(builder, layerTransform);
869         builder.append(')');
870     }
871
872     // Print contents if needed
873     if (CFTypeRef layerContents = layer->contents()) {
874         if (CFGetTypeID(layerContents) == CGImageGetTypeID()) {
875             CGImageRef imageContents = static_cast<CGImageRef>(const_cast<void*>(layerContents));
876             builder.append('\n');
877             printIndent(builder, indent + 1);
878             builder.append("(contents (image [");
879             builder.appendNumber(CGImageGetWidth(imageContents));
880             builder.append(' ');
881             builder.appendNumber(CGImageGetHeight(imageContents));
882             builder.append("]))");
883         }
884
885         if (CFGetTypeID(layerContents) == CABackingStoreGetTypeID()) {
886             CABackingStoreRef backingStore = static_cast<CABackingStoreRef>(const_cast<void*>(layerContents));
887             CGImageRef imageContents = CABackingStoreGetCGImage(backingStore);
888             builder.append('\n');
889             printIndent(builder, indent + 1);
890             builder.append("(contents (backing-store [");
891             builder.appendNumber(CGImageGetWidth(imageContents));
892             builder.append(' ');
893             builder.appendNumber(CGImageGetHeight(imageContents));
894             builder.append("]))");
895         }
896     }
897
898     // Print sublayers if needed
899     int n = intern(layer)->sublayerCount();
900     if (n > 0) {
901         builder.append('\n');
902         printIndent(builder, indent + 1);
903         builder.append("(sublayers");
904
905         PlatformCALayerList sublayers;
906         intern(layer)->getSublayers(sublayers);
907         ASSERT(n == sublayers.size());
908         for (int i = 0; i < n; ++i)
909             printLayer(builder, sublayers[i].get(), indent + 2);
910
911         builder.append(')');
912     }
913
914     builder.append(')');
915 }
916
917 String PlatformCALayerWin::layerTreeAsString() const
918 {
919     // Print heading info
920     CGRect rootBounds = bounds();
921
922     StringBuilder builder;
923     builder.append("\n\n** Render tree at time ");
924     builder.appendNumber(MonotonicTime::now().secondsSinceEpoch().seconds());
925     builder.append(" (bounds ");
926     builder.appendNumber(rootBounds.origin.x);
927     builder.append(", ");
928     builder.appendNumber(rootBounds.origin.y);
929     builder.append(' ');
930     builder.appendNumber(rootBounds.size.width);
931     builder.append('x');
932     builder.appendNumber(rootBounds.size.height);
933     builder.append(") **\n\n");
934
935     // Print layer tree from the root
936     printLayer(builder, this, 0);
937
938     return builder.toString();
939 }
940
941 Ref<PlatformCALayer> PlatformCALayerWin::createCompatibleLayer(PlatformCALayer::LayerType layerType, PlatformCALayerClient* client) const
942 {
943     return PlatformCALayerWin::create(layerType, client);
944 }
945
946 TiledBacking* PlatformCALayerWin::tiledBacking()
947 {
948     if (!usesTiledBackingLayer())
949         return nullptr;
950
951     return reinterpret_cast<WebTiledBackingLayerWin*>(intern(this))->tiledBacking();
952 }
953
954 void PlatformCALayerWin::drawTextAtPoint(CGContextRef context, CGFloat x, CGFloat y, CGSize scale, CGFloat fontSize, const char* message, size_t length, CGFloat, Color) const
955 {
956     String text(message, length);
957
958     FontCascadeDescription desc;
959
960     NONCLIENTMETRICS metrics;
961     metrics.cbSize = sizeof(metrics);
962     SystemParametersInfo(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
963     desc.setOneFamily(metrics.lfSmCaptionFont.lfFaceName);
964
965     desc.setComputedSize(scale.width * fontSize);
966
967     FontCascade font = FontCascade(desc, 0, 0);
968     font.update(nullptr);
969
970     GraphicsContext cg(context);
971     cg.setFillColor(Color::black);
972     cg.drawText(font, TextRun(text), IntPoint(x, y));
973 }
974
975 #endif