Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / GraphicsLayer.cpp
1 /*
2  * Copyright (C) 2009 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 COMPUTER, 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 "platform/graphics/GraphicsLayer.h"
29
30 #include "SkImageFilter.h"
31 #include "SkMatrix44.h"
32 #include "platform/geometry/FloatRect.h"
33 #include "platform/geometry/LayoutRect.h"
34 #include "platform/graphics/GraphicsLayerFactory.h"
35 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
36 #include "platform/graphics/skia/NativeImageSkia.h"
37 #include "platform/scroll/ScrollableArea.h"
38 #include "platform/text/TextStream.h"
39 #include "public/platform/Platform.h"
40 #include "public/platform/WebAnimation.h"
41 #include "public/platform/WebCompositorSupport.h"
42 #include "public/platform/WebFilterOperations.h"
43 #include "public/platform/WebFloatPoint.h"
44 #include "public/platform/WebFloatRect.h"
45 #include "public/platform/WebGraphicsLayerDebugInfo.h"
46 #include "public/platform/WebLayer.h"
47 #include "public/platform/WebPoint.h"
48 #include "public/platform/WebSize.h"
49 #include "wtf/CurrentTime.h"
50 #include "wtf/HashMap.h"
51 #include "wtf/HashSet.h"
52 #include "wtf/text/WTFString.h"
53
54 #ifndef NDEBUG
55 #include <stdio.h>
56 #endif
57
58 using blink::Platform;
59 using blink::WebAnimation;
60 using blink::WebFilterOperations;
61 using blink::WebLayer;
62 using blink::WebPoint;
63
64 namespace WebCore {
65
66 typedef HashMap<const GraphicsLayer*, Vector<FloatRect> > RepaintMap;
67 static RepaintMap& repaintRectMap()
68 {
69     DEFINE_STATIC_LOCAL(RepaintMap, map, ());
70     return map;
71 }
72
73 PassOwnPtr<GraphicsLayer> GraphicsLayer::create(GraphicsLayerFactory* factory, GraphicsLayerClient* client)
74 {
75     return factory->createGraphicsLayer(client);
76 }
77
78 GraphicsLayer::GraphicsLayer(GraphicsLayerClient* client)
79     : m_client(client)
80     , m_anchorPoint(0.5f, 0.5f, 0)
81     , m_backgroundColor(Color::transparent)
82     , m_opacity(1)
83     , m_zPosition(0)
84     , m_blendMode(blink::WebBlendModeNormal)
85     , m_contentsOpaque(false)
86     , m_shouldFlattenTransform(true)
87     , m_backfaceVisibility(true)
88     , m_masksToBounds(false)
89     , m_drawsContent(false)
90     , m_contentsVisible(true)
91     , m_isRootForIsolatedGroup(false)
92     , m_hasScrollParent(false)
93     , m_hasClipParent(false)
94     , m_paintingPhase(GraphicsLayerPaintAllWithOverflowClip)
95     , m_contentsOrientation(CompositingCoordinatesTopDown)
96     , m_parent(0)
97     , m_maskLayer(0)
98     , m_contentsClippingMaskLayer(0)
99     , m_replicaLayer(0)
100     , m_replicatedLayer(0)
101     , m_paintCount(0)
102     , m_contentsSolidColor(Color::transparent)
103     , m_contentsLayer(0)
104     , m_contentsLayerId(0)
105     , m_scrollableArea(0)
106     , m_3dRenderingContext(0)
107 {
108 #ifndef NDEBUG
109     if (m_client)
110         m_client->verifyNotPainting();
111 #endif
112
113     m_opaqueRectTrackingContentLayerDelegate = adoptPtr(new OpaqueRectTrackingContentLayerDelegate(this));
114     m_layer = adoptPtr(Platform::current()->compositorSupport()->createContentLayer(m_opaqueRectTrackingContentLayerDelegate.get()));
115     m_layer->layer()->setDrawsContent(m_drawsContent && m_contentsVisible);
116     m_layer->layer()->setWebLayerClient(this);
117     m_layer->setAutomaticallyComputeRasterScale(true);
118 }
119
120 GraphicsLayer::~GraphicsLayer()
121 {
122     for (size_t i = 0; i < m_linkHighlights.size(); ++i)
123         m_linkHighlights[i]->clearCurrentGraphicsLayer();
124     m_linkHighlights.clear();
125
126 #ifndef NDEBUG
127     if (m_client)
128         m_client->verifyNotPainting();
129 #endif
130
131     if (m_replicaLayer)
132         m_replicaLayer->setReplicatedLayer(0);
133
134     if (m_replicatedLayer)
135         m_replicatedLayer->setReplicatedByLayer(0);
136
137     removeAllChildren();
138     removeFromParent();
139
140     resetTrackedRepaints();
141     ASSERT(!m_parent);
142 }
143
144 void GraphicsLayer::setParent(GraphicsLayer* layer)
145 {
146     ASSERT(!layer || !layer->hasAncestor(this));
147     m_parent = layer;
148 }
149
150 bool GraphicsLayer::hasAncestor(GraphicsLayer* ancestor) const
151 {
152     for (GraphicsLayer* curr = parent(); curr; curr = curr->parent()) {
153         if (curr == ancestor)
154             return true;
155     }
156
157     return false;
158 }
159
160 bool GraphicsLayer::setChildren(const Vector<GraphicsLayer*>& newChildren)
161 {
162     // If the contents of the arrays are the same, nothing to do.
163     if (newChildren == m_children)
164         return false;
165
166     removeAllChildren();
167
168     size_t listSize = newChildren.size();
169     for (size_t i = 0; i < listSize; ++i)
170         addChildInternal(newChildren[i]);
171
172     updateChildList();
173
174     return true;
175 }
176
177 void GraphicsLayer::addChildInternal(GraphicsLayer* childLayer)
178 {
179     ASSERT(childLayer != this);
180
181     if (childLayer->parent())
182         childLayer->removeFromParent();
183
184     childLayer->setParent(this);
185     m_children.append(childLayer);
186
187     // Don't call updateChildList here, this function is used in cases where it
188     // should not be called until all children are processed.
189 }
190
191 void GraphicsLayer::addChild(GraphicsLayer* childLayer)
192 {
193     addChildInternal(childLayer);
194     updateChildList();
195 }
196
197 void GraphicsLayer::addChildAtIndex(GraphicsLayer* childLayer, int index)
198 {
199     ASSERT(childLayer != this);
200
201     if (childLayer->parent())
202         childLayer->removeFromParent();
203
204     childLayer->setParent(this);
205     m_children.insert(index, childLayer);
206
207     updateChildList();
208 }
209
210 void GraphicsLayer::addChildBelow(GraphicsLayer* childLayer, GraphicsLayer* sibling)
211 {
212     ASSERT(childLayer != this);
213     childLayer->removeFromParent();
214
215     bool found = false;
216     for (unsigned i = 0; i < m_children.size(); i++) {
217         if (sibling == m_children[i]) {
218             m_children.insert(i, childLayer);
219             found = true;
220             break;
221         }
222     }
223
224     childLayer->setParent(this);
225
226     if (!found)
227         m_children.append(childLayer);
228
229     updateChildList();
230 }
231
232 void GraphicsLayer::addChildAbove(GraphicsLayer* childLayer, GraphicsLayer* sibling)
233 {
234     childLayer->removeFromParent();
235     ASSERT(childLayer != this);
236
237     bool found = false;
238     for (unsigned i = 0; i < m_children.size(); i++) {
239         if (sibling == m_children[i]) {
240             m_children.insert(i+1, childLayer);
241             found = true;
242             break;
243         }
244     }
245
246     childLayer->setParent(this);
247
248     if (!found)
249         m_children.append(childLayer);
250
251     updateChildList();
252 }
253
254 bool GraphicsLayer::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
255 {
256     ASSERT(!newChild->parent());
257     bool found = false;
258     for (unsigned i = 0; i < m_children.size(); i++) {
259         if (oldChild == m_children[i]) {
260             m_children[i] = newChild;
261             found = true;
262             break;
263         }
264     }
265
266     if (found) {
267         oldChild->setParent(0);
268
269         newChild->removeFromParent();
270         newChild->setParent(this);
271
272         updateChildList();
273         return true;
274     }
275
276     return false;
277 }
278
279 void GraphicsLayer::removeAllChildren()
280 {
281     while (m_children.size()) {
282         GraphicsLayer* curLayer = m_children[0];
283         ASSERT(curLayer->parent());
284         curLayer->removeFromParent();
285     }
286 }
287
288 void GraphicsLayer::removeFromParent()
289 {
290     if (m_parent) {
291         unsigned i;
292         for (i = 0; i < m_parent->m_children.size(); i++) {
293             if (this == m_parent->m_children[i]) {
294                 m_parent->m_children.remove(i);
295                 break;
296             }
297         }
298
299         setParent(0);
300     }
301
302     platformLayer()->removeFromParent();
303 }
304
305 void GraphicsLayer::setReplicatedByLayer(GraphicsLayer* layer)
306 {
307     // FIXME: this could probably be a full early exit.
308     if (m_replicaLayer != layer) {
309         if (m_replicaLayer)
310             m_replicaLayer->setReplicatedLayer(0);
311
312         if (layer)
313             layer->setReplicatedLayer(this);
314
315         m_replicaLayer = layer;
316     }
317
318     WebLayer* webReplicaLayer = layer ? layer->platformLayer() : 0;
319     platformLayer()->setReplicaLayer(webReplicaLayer);
320 }
321
322 void GraphicsLayer::setOffsetFromRenderer(const IntSize& offset, ShouldSetNeedsDisplay shouldSetNeedsDisplay)
323 {
324     if (offset == m_offsetFromRenderer)
325         return;
326
327     m_offsetFromRenderer = offset;
328
329     // If the compositing layer offset changes, we need to repaint.
330     if (shouldSetNeedsDisplay == SetNeedsDisplay)
331         setNeedsDisplay();
332 }
333
334 void GraphicsLayer::paintGraphicsLayerContents(GraphicsContext& context, const IntRect& clip)
335 {
336     if (!m_client)
337         return;
338     incrementPaintCount();
339     m_client->paintContents(this, context, m_paintingPhase, clip);
340 }
341
342 void GraphicsLayer::setZPosition(float position)
343 {
344     m_zPosition = position;
345 }
346
347 void GraphicsLayer::updateChildList()
348 {
349     WebLayer* childHost = m_layer->layer();
350     childHost->removeAllChildren();
351
352     clearContentsLayerIfUnregistered();
353
354     if (m_contentsLayer) {
355         // FIXME: add the contents layer in the correct order with negative z-order children.
356         // This does not cause visible rendering issues because currently contents layers are only used
357         // for replaced elements that don't have children.
358         childHost->addChild(m_contentsLayer);
359     }
360
361     const Vector<GraphicsLayer*>& childLayers = children();
362     size_t numChildren = childLayers.size();
363     for (size_t i = 0; i < numChildren; ++i) {
364         GraphicsLayer* curChild = childLayers[i];
365
366         childHost->addChild(curChild->platformLayer());
367     }
368
369     for (size_t i = 0; i < m_linkHighlights.size(); ++i)
370         childHost->addChild(m_linkHighlights[i]->layer());
371 }
372
373 void GraphicsLayer::updateLayerIsDrawable()
374 {
375     // For the rest of the accelerated compositor code, there is no reason to make a
376     // distinction between drawsContent and contentsVisible. So, for m_layer->layer(), these two
377     // flags are combined here. m_contentsLayer shouldn't receive the drawsContent flag
378     // so it is only given contentsVisible.
379
380     m_layer->layer()->setDrawsContent(m_drawsContent && m_contentsVisible);
381     if (WebLayer* contentsLayer = contentsLayerIfRegistered())
382         contentsLayer->setDrawsContent(m_contentsVisible);
383
384     if (m_drawsContent) {
385         m_layer->layer()->invalidate();
386         for (size_t i = 0; i < m_linkHighlights.size(); ++i)
387             m_linkHighlights[i]->invalidate();
388     }
389 }
390
391 void GraphicsLayer::updateContentsRect()
392 {
393     WebLayer* contentsLayer = contentsLayerIfRegistered();
394     if (!contentsLayer)
395         return;
396
397     contentsLayer->setPosition(FloatPoint(m_contentsRect.x(), m_contentsRect.y()));
398     contentsLayer->setBounds(IntSize(m_contentsRect.width(), m_contentsRect.height()));
399
400     if (m_contentsClippingMaskLayer) {
401         if (m_contentsClippingMaskLayer->size() != m_contentsRect.size()) {
402             m_contentsClippingMaskLayer->setSize(m_contentsRect.size());
403             m_contentsClippingMaskLayer->setNeedsDisplay();
404         }
405         m_contentsClippingMaskLayer->setPosition(FloatPoint());
406         m_contentsClippingMaskLayer->setOffsetFromRenderer(offsetFromRenderer() + IntSize(m_contentsRect.location().x(), m_contentsRect.location().y()));
407     }
408 }
409
410 static HashSet<int>* s_registeredLayerSet;
411
412 void GraphicsLayer::registerContentsLayer(WebLayer* layer)
413 {
414     if (!s_registeredLayerSet)
415         s_registeredLayerSet = new HashSet<int>;
416     if (s_registeredLayerSet->contains(layer->id()))
417         CRASH();
418     s_registeredLayerSet->add(layer->id());
419 }
420
421 void GraphicsLayer::unregisterContentsLayer(WebLayer* layer)
422 {
423     ASSERT(s_registeredLayerSet);
424     if (!s_registeredLayerSet->contains(layer->id()))
425         CRASH();
426     s_registeredLayerSet->remove(layer->id());
427 }
428
429 void GraphicsLayer::setContentsTo(WebLayer* layer)
430 {
431     bool childrenChanged = false;
432     if (layer) {
433         ASSERT(s_registeredLayerSet);
434         if (!s_registeredLayerSet->contains(layer->id()))
435             CRASH();
436         if (m_contentsLayerId != layer->id()) {
437             setupContentsLayer(layer);
438             childrenChanged = true;
439         }
440         updateContentsRect();
441     } else {
442         if (m_contentsLayer) {
443             childrenChanged = true;
444
445             // The old contents layer will be removed via updateChildList.
446             m_contentsLayer = 0;
447             m_contentsLayerId = 0;
448         }
449     }
450
451     if (childrenChanged)
452         updateChildList();
453 }
454
455 void GraphicsLayer::setupContentsLayer(WebLayer* contentsLayer)
456 {
457     ASSERT(contentsLayer);
458     m_contentsLayer = contentsLayer;
459     m_contentsLayerId = m_contentsLayer->id();
460
461     m_contentsLayer->setWebLayerClient(this);
462     m_contentsLayer->setAnchorPoint(FloatPoint(0, 0));
463     m_contentsLayer->setUseParentBackfaceVisibility(true);
464
465     // It is necessary to call setDrawsContent as soon as we receive the new contentsLayer, for
466     // the correctness of early exit conditions in setDrawsContent() and setContentsVisible().
467     m_contentsLayer->setDrawsContent(m_contentsVisible);
468
469     // Insert the content layer first. Video elements require this, because they have
470     // shadow content that must display in front of the video.
471     m_layer->layer()->insertChild(m_contentsLayer, 0);
472     WebLayer* borderWebLayer = m_contentsClippingMaskLayer ? m_contentsClippingMaskLayer->platformLayer() : 0;
473     m_contentsLayer->setMaskLayer(borderWebLayer);
474
475     m_contentsLayer->setRenderingContext(m_3dRenderingContext);
476 }
477
478 void GraphicsLayer::clearContentsLayerIfUnregistered()
479 {
480     if (!m_contentsLayerId || s_registeredLayerSet->contains(m_contentsLayerId))
481         return;
482
483     m_contentsLayer = 0;
484     m_contentsLayerId = 0;
485 }
486
487 GraphicsLayerDebugInfo& GraphicsLayer::debugInfo()
488 {
489     return m_debugInfo;
490 }
491
492 blink::WebGraphicsLayerDebugInfo* GraphicsLayer::takeDebugInfoFor(WebLayer* layer)
493 {
494     GraphicsLayerDebugInfo* clone = m_debugInfo.clone();
495     clone->setDebugName(debugName(layer));
496     return clone;
497 }
498
499 WebLayer* GraphicsLayer::contentsLayerIfRegistered()
500 {
501     clearContentsLayerIfUnregistered();
502     return m_contentsLayer;
503 }
504
505 void GraphicsLayer::resetTrackedRepaints()
506 {
507     repaintRectMap().remove(this);
508 }
509
510 void GraphicsLayer::addRepaintRect(const FloatRect& repaintRect)
511 {
512     if (m_client->isTrackingRepaints()) {
513         FloatRect largestRepaintRect(FloatPoint(), m_size);
514         largestRepaintRect.intersect(repaintRect);
515         RepaintMap::iterator repaintIt = repaintRectMap().find(this);
516         if (repaintIt == repaintRectMap().end()) {
517             Vector<FloatRect> repaintRects;
518             repaintRects.append(largestRepaintRect);
519             repaintRectMap().set(this, repaintRects);
520         } else {
521             Vector<FloatRect>& repaintRects = repaintIt->value;
522             repaintRects.append(largestRepaintRect);
523         }
524     }
525 }
526
527 void GraphicsLayer::collectTrackedRepaintRects(Vector<FloatRect>& rects) const
528 {
529     if (!m_client->isTrackingRepaints())
530         return;
531
532     RepaintMap::iterator repaintIt = repaintRectMap().find(this);
533     if (repaintIt != repaintRectMap().end())
534         rects.append(repaintIt->value);
535 }
536
537 void GraphicsLayer::dumpLayer(TextStream& ts, int indent, LayerTreeFlags flags, RenderingContextMap& renderingContextMap) const
538 {
539     writeIndent(ts, indent);
540     ts << "(" << "GraphicsLayer";
541
542     if (flags & LayerTreeIncludesDebugInfo) {
543         ts << " " << static_cast<void*>(const_cast<GraphicsLayer*>(this));
544         ts << " \"" << m_client->debugName(this) << "\"";
545     }
546
547     ts << "\n";
548     dumpProperties(ts, indent, flags, renderingContextMap);
549     writeIndent(ts, indent);
550     ts << ")\n";
551 }
552
553 void GraphicsLayer::dumpProperties(TextStream& ts, int indent, LayerTreeFlags flags, RenderingContextMap& renderingContextMap) const
554 {
555     if (m_position != FloatPoint()) {
556         writeIndent(ts, indent + 1);
557         ts << "(position " << m_position.x() << " " << m_position.y() << ")\n";
558     }
559
560     if (m_boundsOrigin != FloatPoint()) {
561         writeIndent(ts, indent + 1);
562         ts << "(bounds origin " << m_boundsOrigin.x() << " " << m_boundsOrigin.y() << ")\n";
563     }
564
565     if (m_anchorPoint != FloatPoint3D(0.5f, 0.5f, 0)) {
566         writeIndent(ts, indent + 1);
567         ts << "(anchor " << m_anchorPoint.x() << " " << m_anchorPoint.y() << ")\n";
568     }
569
570     if (m_size != IntSize()) {
571         writeIndent(ts, indent + 1);
572         ts << "(bounds " << m_size.width() << " " << m_size.height() << ")\n";
573     }
574
575     if (m_opacity != 1) {
576         writeIndent(ts, indent + 1);
577         ts << "(opacity " << m_opacity << ")\n";
578     }
579
580     if (m_blendMode != blink::WebBlendModeNormal) {
581         writeIndent(ts, indent + 1);
582         ts << "(blendMode " << compositeOperatorName(CompositeSourceOver, m_blendMode) << ")\n";
583     }
584
585     if (m_isRootForIsolatedGroup) {
586         writeIndent(ts, indent + 1);
587         ts << "(isolate " << m_isRootForIsolatedGroup << ")\n";
588     }
589
590     if (m_contentsOpaque) {
591         writeIndent(ts, indent + 1);
592         ts << "(contentsOpaque " << m_contentsOpaque << ")\n";
593     }
594
595     if (!m_shouldFlattenTransform) {
596         writeIndent(ts, indent + 1);
597         ts << "(shouldFlattenTransform " << m_shouldFlattenTransform << ")\n";
598     }
599
600     if (m_3dRenderingContext) {
601         RenderingContextMap::const_iterator it = renderingContextMap.find(m_3dRenderingContext);
602         int contextId = renderingContextMap.size() + 1;
603         if (it == renderingContextMap.end())
604             renderingContextMap.set(m_3dRenderingContext, contextId);
605         else
606             contextId = it->value;
607
608         writeIndent(ts, indent + 1);
609         ts << "(3dRenderingContext " << contextId << ")\n";
610     }
611
612     if (m_drawsContent) {
613         writeIndent(ts, indent + 1);
614         ts << "(drawsContent " << m_drawsContent << ")\n";
615     }
616
617     if (!m_contentsVisible) {
618         writeIndent(ts, indent + 1);
619         ts << "(contentsVisible " << m_contentsVisible << ")\n";
620     }
621
622     if (!m_backfaceVisibility) {
623         writeIndent(ts, indent + 1);
624         ts << "(backfaceVisibility " << (m_backfaceVisibility ? "visible" : "hidden") << ")\n";
625     }
626
627     if (flags & LayerTreeIncludesDebugInfo) {
628         writeIndent(ts, indent + 1);
629         ts << "(";
630         if (m_client)
631             ts << "client " << static_cast<void*>(m_client);
632         else
633             ts << "no client";
634         ts << ")\n";
635     }
636
637     if (m_backgroundColor.alpha()) {
638         writeIndent(ts, indent + 1);
639         ts << "(backgroundColor " << m_backgroundColor.nameForRenderTreeAsText() << ")\n";
640     }
641
642     if (!m_transform.isIdentity()) {
643         writeIndent(ts, indent + 1);
644         ts << "(transform ";
645         ts << "[" << m_transform.m11() << " " << m_transform.m12() << " " << m_transform.m13() << " " << m_transform.m14() << "] ";
646         ts << "[" << m_transform.m21() << " " << m_transform.m22() << " " << m_transform.m23() << " " << m_transform.m24() << "] ";
647         ts << "[" << m_transform.m31() << " " << m_transform.m32() << " " << m_transform.m33() << " " << m_transform.m34() << "] ";
648         ts << "[" << m_transform.m41() << " " << m_transform.m42() << " " << m_transform.m43() << " " << m_transform.m44() << "])\n";
649     }
650
651     if (m_replicaLayer) {
652         writeIndent(ts, indent + 1);
653         ts << "(replica layer";
654         if (flags & LayerTreeIncludesDebugInfo)
655             ts << " " << m_replicaLayer;
656         ts << ")\n";
657         m_replicaLayer->dumpLayer(ts, indent + 2, flags, renderingContextMap);
658     }
659
660     if (m_replicatedLayer) {
661         writeIndent(ts, indent + 1);
662         ts << "(replicated layer";
663         if (flags & LayerTreeIncludesDebugInfo)
664             ts << " " << m_replicatedLayer;
665         ts << ")\n";
666     }
667
668     if ((flags & LayerTreeIncludesRepaintRects) && repaintRectMap().contains(this) && !repaintRectMap().get(this).isEmpty()) {
669         writeIndent(ts, indent + 1);
670         ts << "(repaint rects\n";
671         for (size_t i = 0; i < repaintRectMap().get(this).size(); ++i) {
672             if (repaintRectMap().get(this)[i].isEmpty())
673                 continue;
674             writeIndent(ts, indent + 2);
675             ts << "(rect ";
676             ts << repaintRectMap().get(this)[i].x() << " ";
677             ts << repaintRectMap().get(this)[i].y() << " ";
678             ts << repaintRectMap().get(this)[i].width() << " ";
679             ts << repaintRectMap().get(this)[i].height();
680             ts << ")\n";
681         }
682         writeIndent(ts, indent + 1);
683         ts << ")\n";
684     }
685
686     if ((flags & LayerTreeIncludesPaintingPhases) && paintingPhase()) {
687         writeIndent(ts, indent + 1);
688         ts << "(paintingPhases\n";
689         if (paintingPhase() & GraphicsLayerPaintBackground) {
690             writeIndent(ts, indent + 2);
691             ts << "GraphicsLayerPaintBackground\n";
692         }
693         if (paintingPhase() & GraphicsLayerPaintForeground) {
694             writeIndent(ts, indent + 2);
695             ts << "GraphicsLayerPaintForeground\n";
696         }
697         if (paintingPhase() & GraphicsLayerPaintMask) {
698             writeIndent(ts, indent + 2);
699             ts << "GraphicsLayerPaintMask\n";
700         }
701         if (paintingPhase() & GraphicsLayerPaintChildClippingMask) {
702             writeIndent(ts, indent + 2);
703             ts << "GraphicsLayerPaintChildClippingMask\n";
704         }
705         if (paintingPhase() & GraphicsLayerPaintOverflowContents) {
706             writeIndent(ts, indent + 2);
707             ts << "GraphicsLayerPaintOverflowContents\n";
708         }
709         if (paintingPhase() & GraphicsLayerPaintCompositedScroll) {
710             writeIndent(ts, indent + 2);
711             ts << "GraphicsLayerPaintCompositedScroll\n";
712         }
713         writeIndent(ts, indent + 1);
714         ts << ")\n";
715     }
716
717     if (flags & LayerTreeIncludesClipAndScrollParents) {
718         if (m_hasScrollParent) {
719             writeIndent(ts, indent + 1);
720             ts << "(hasScrollParent 1)\n";
721         }
722         if (m_hasClipParent) {
723             writeIndent(ts, indent + 1);
724             ts << "(hasClipParent 1)\n";
725         }
726     }
727
728     if (m_children.size()) {
729         writeIndent(ts, indent + 1);
730         ts << "(children " << m_children.size() << "\n";
731
732         unsigned i;
733         for (i = 0; i < m_children.size(); i++)
734             m_children[i]->dumpLayer(ts, indent + 2, flags, renderingContextMap);
735         writeIndent(ts, indent + 1);
736         ts << ")\n";
737     }
738 }
739
740 String GraphicsLayer::layerTreeAsText(LayerTreeFlags flags) const
741 {
742     TextStream ts;
743
744     RenderingContextMap renderingContextMap;
745     dumpLayer(ts, 0, flags, renderingContextMap);
746     return ts.release();
747 }
748
749 String GraphicsLayer::debugName(blink::WebLayer* webLayer) const
750 {
751     String name;
752     if (!m_client)
753         return name;
754
755     String highlightDebugName;
756     for (size_t i = 0; i < m_linkHighlights.size(); ++i) {
757         if (webLayer == m_linkHighlights[i]->layer()) {
758             highlightDebugName = "LinkHighlight[" + String::number(i) + "] for " + m_client->debugName(this);
759             break;
760         }
761     }
762
763     if (webLayer == m_contentsLayer) {
764         name = "ContentsLayer for " + m_client->debugName(this);
765     } else if (!highlightDebugName.isEmpty()) {
766         name = highlightDebugName;
767     } else if (webLayer == m_layer->layer()) {
768         name = m_client->debugName(this);
769     } else {
770         ASSERT_NOT_REACHED();
771     }
772     return name;
773 }
774
775 void GraphicsLayer::setCompositingReasons(CompositingReasons reasons)
776 {
777     m_debugInfo.setCompositingReasons(reasons);
778 }
779
780 void GraphicsLayer::setPosition(const FloatPoint& point)
781 {
782     m_position = point;
783     platformLayer()->setPosition(m_position);
784 }
785
786 void GraphicsLayer::setAnchorPoint(const FloatPoint3D& point)
787 {
788     m_anchorPoint = point;
789     platformLayer()->setAnchorPoint(FloatPoint(m_anchorPoint.x(), m_anchorPoint.y()));
790     platformLayer()->setAnchorPointZ(m_anchorPoint.z());
791 }
792
793 void GraphicsLayer::setSize(const FloatSize& size)
794 {
795     // We are receiving negative sizes here that cause assertions to fail in the compositor. Clamp them to 0 to
796     // avoid those assertions.
797     // FIXME: This should be an ASSERT instead, as negative sizes should not exist in WebCore.
798     FloatSize clampedSize = size;
799     if (clampedSize.width() < 0 || clampedSize.height() < 0)
800         clampedSize = FloatSize();
801
802     if (clampedSize == m_size)
803         return;
804
805     m_size = clampedSize;
806
807     m_layer->layer()->setBounds(flooredIntSize(m_size));
808     // Note that we don't resize m_contentsLayer. It's up the caller to do that.
809 }
810
811 void GraphicsLayer::setTransform(const TransformationMatrix& transform)
812 {
813     m_transform = transform;
814     platformLayer()->setTransform(TransformationMatrix::toSkMatrix44(m_transform));
815 }
816
817 void GraphicsLayer::setShouldFlattenTransform(bool shouldFlatten)
818 {
819     if (shouldFlatten == m_shouldFlattenTransform)
820         return;
821
822     m_shouldFlattenTransform = shouldFlatten;
823
824     m_layer->layer()->setShouldFlattenTransform(shouldFlatten);
825 }
826
827 void GraphicsLayer::setRenderingContext(int context)
828 {
829     if (m_3dRenderingContext == context)
830         return;
831
832     m_3dRenderingContext = context;
833     m_layer->layer()->setRenderingContext(context);
834
835     if (m_contentsLayer)
836         m_contentsLayer->setRenderingContext(m_3dRenderingContext);
837 }
838
839 void GraphicsLayer::setMasksToBounds(bool masksToBounds)
840 {
841     m_masksToBounds = masksToBounds;
842     m_layer->layer()->setMasksToBounds(m_masksToBounds);
843 }
844
845 void GraphicsLayer::setDrawsContent(bool drawsContent)
846 {
847     // Note carefully this early-exit is only correct because we also properly call
848     // WebLayer::setDrawsContent whenever m_contentsLayer is set to a new layer in setupContentsLayer().
849     if (drawsContent == m_drawsContent)
850         return;
851
852     m_drawsContent = drawsContent;
853     updateLayerIsDrawable();
854 }
855
856 void GraphicsLayer::setContentsVisible(bool contentsVisible)
857 {
858     // Note carefully this early-exit is only correct because we also properly call
859     // WebLayer::setDrawsContent whenever m_contentsLayer is set to a new layer in setupContentsLayer().
860     if (contentsVisible == m_contentsVisible)
861         return;
862
863     m_contentsVisible = contentsVisible;
864     updateLayerIsDrawable();
865 }
866
867 void GraphicsLayer::setClipParent(blink::WebLayer* parent)
868 {
869     m_hasClipParent = !!parent;
870     m_layer->layer()->setClipParent(parent);
871 }
872
873 void GraphicsLayer::setScrollParent(blink::WebLayer* parent)
874 {
875     m_hasScrollParent = !!parent;
876     m_layer->layer()->setScrollParent(parent);
877 }
878
879 void GraphicsLayer::setBackgroundColor(const Color& color)
880 {
881     if (color == m_backgroundColor)
882         return;
883
884     m_backgroundColor = color;
885     m_layer->layer()->setBackgroundColor(m_backgroundColor.rgb());
886 }
887
888 void GraphicsLayer::setContentsOpaque(bool opaque)
889 {
890     m_contentsOpaque = opaque;
891     m_layer->layer()->setOpaque(m_contentsOpaque);
892     m_opaqueRectTrackingContentLayerDelegate->setOpaque(m_contentsOpaque);
893 }
894
895 void GraphicsLayer::setMaskLayer(GraphicsLayer* maskLayer)
896 {
897     if (maskLayer == m_maskLayer)
898         return;
899
900     m_maskLayer = maskLayer;
901     WebLayer* maskWebLayer = m_maskLayer ? m_maskLayer->platformLayer() : 0;
902     m_layer->layer()->setMaskLayer(maskWebLayer);
903 }
904
905 void GraphicsLayer::setContentsClippingMaskLayer(GraphicsLayer* contentsClippingMaskLayer)
906 {
907     if (contentsClippingMaskLayer == m_contentsClippingMaskLayer)
908         return;
909
910     m_contentsClippingMaskLayer = contentsClippingMaskLayer;
911     WebLayer* contentsLayer = contentsLayerIfRegistered();
912     if (!contentsLayer)
913         return;
914     WebLayer* contentsClippingMaskWebLayer = m_contentsClippingMaskLayer ? m_contentsClippingMaskLayer->platformLayer() : 0;
915     contentsLayer->setMaskLayer(contentsClippingMaskWebLayer);
916     updateContentsRect();
917 }
918
919 void GraphicsLayer::setBackfaceVisibility(bool visible)
920 {
921     m_backfaceVisibility = visible;
922     m_layer->setDoubleSided(m_backfaceVisibility);
923 }
924
925 void GraphicsLayer::setOpacity(float opacity)
926 {
927     float clampedOpacity = std::max(std::min(opacity, 1.0f), 0.0f);
928     m_opacity = clampedOpacity;
929     platformLayer()->setOpacity(opacity);
930 }
931
932 void GraphicsLayer::setBlendMode(blink::WebBlendMode blendMode)
933 {
934     if (m_blendMode == blendMode)
935         return;
936     m_blendMode = blendMode;
937     platformLayer()->setBlendMode(blink::WebBlendMode(blendMode));
938 }
939
940 void GraphicsLayer::setIsRootForIsolatedGroup(bool isolated)
941 {
942     if (m_isRootForIsolatedGroup == isolated)
943         return;
944     m_isRootForIsolatedGroup = isolated;
945     platformLayer()->setIsRootForIsolatedGroup(isolated);
946 }
947
948 void GraphicsLayer::setContentsNeedsDisplay()
949 {
950     if (WebLayer* contentsLayer = contentsLayerIfRegistered()) {
951         contentsLayer->invalidate();
952         addRepaintRect(contentsRect());
953     }
954 }
955
956 void GraphicsLayer::setNeedsDisplay()
957 {
958     if (drawsContent()) {
959         m_layer->layer()->invalidate();
960         addRepaintRect(FloatRect(FloatPoint(), m_size));
961         for (size_t i = 0; i < m_linkHighlights.size(); ++i)
962             m_linkHighlights[i]->invalidate();
963     }
964 }
965
966 void GraphicsLayer::setNeedsDisplayInRect(const FloatRect& rect)
967 {
968     if (drawsContent()) {
969         m_layer->layer()->invalidateRect(rect);
970         addRepaintRect(rect);
971         for (size_t i = 0; i < m_linkHighlights.size(); ++i)
972             m_linkHighlights[i]->invalidate();
973     }
974 }
975
976 void GraphicsLayer::setContentsRect(const IntRect& rect)
977 {
978     if (rect == m_contentsRect)
979         return;
980
981     m_contentsRect = rect;
982     updateContentsRect();
983 }
984
985 void GraphicsLayer::setContentsToImage(Image* image)
986 {
987     RefPtr<NativeImageSkia> nativeImage = image ? image->nativeImageForCurrentFrame() : 0;
988     if (nativeImage) {
989         if (!m_imageLayer) {
990             m_imageLayer = adoptPtr(Platform::current()->compositorSupport()->createImageLayer());
991             registerContentsLayer(m_imageLayer->layer());
992         }
993         m_imageLayer->setBitmap(nativeImage->bitmap());
994         m_imageLayer->layer()->setOpaque(image->currentFrameKnownToBeOpaque());
995         updateContentsRect();
996     } else {
997         if (m_imageLayer) {
998             unregisterContentsLayer(m_imageLayer->layer());
999             m_imageLayer.clear();
1000         }
1001     }
1002
1003     setContentsTo(m_imageLayer ? m_imageLayer->layer() : 0);
1004 }
1005
1006 void GraphicsLayer::setContentsToNinePatch(Image* image, const IntRect& aperture)
1007 {
1008     if (m_ninePatchLayer) {
1009         unregisterContentsLayer(m_ninePatchLayer->layer());
1010         m_ninePatchLayer.clear();
1011     }
1012     RefPtr<NativeImageSkia> nativeImage = image ? image->nativeImageForCurrentFrame() : 0;
1013     if (nativeImage) {
1014         m_ninePatchLayer = adoptPtr(Platform::current()->compositorSupport()->createNinePatchLayer());
1015         m_ninePatchLayer->setBitmap(nativeImage->bitmap(), aperture);
1016         m_ninePatchLayer->layer()->setOpaque(image->currentFrameKnownToBeOpaque());
1017         registerContentsLayer(m_ninePatchLayer->layer());
1018     }
1019     setContentsTo(m_ninePatchLayer ? m_ninePatchLayer->layer() : 0);
1020 }
1021
1022 void GraphicsLayer::setContentsToSolidColor(const Color& color)
1023 {
1024     if (color == m_contentsSolidColor)
1025         return;
1026
1027     m_contentsSolidColor = color;
1028     if (color.alpha()) {
1029         if (!m_solidColorLayer) {
1030             m_solidColorLayer = adoptPtr(Platform::current()->compositorSupport()->createSolidColorLayer());
1031             registerContentsLayer(m_solidColorLayer->layer());
1032         }
1033         m_solidColorLayer->setBackgroundColor(color.rgb());
1034     } else {
1035         if (!m_solidColorLayer)
1036             return;
1037         unregisterContentsLayer(m_solidColorLayer->layer());
1038         m_solidColorLayer.clear();
1039     }
1040     setContentsTo(m_solidColorLayer ? m_solidColorLayer->layer() : 0);
1041 }
1042
1043 bool GraphicsLayer::addAnimation(PassOwnPtr<WebAnimation> popAnimation)
1044 {
1045     OwnPtr<WebAnimation> animation(popAnimation);
1046     ASSERT(animation);
1047     platformLayer()->setAnimationDelegate(this);
1048
1049     // Remove any existing animations with the same animation id and target property.
1050     platformLayer()->removeAnimation(animation->id(), animation->targetProperty());
1051     return platformLayer()->addAnimation(animation.leakPtr());
1052 }
1053
1054 void GraphicsLayer::pauseAnimation(int animationId, double timeOffset)
1055 {
1056     platformLayer()->pauseAnimation(animationId, timeOffset);
1057 }
1058
1059 void GraphicsLayer::removeAnimation(int animationId)
1060 {
1061     platformLayer()->removeAnimation(animationId);
1062 }
1063
1064 WebLayer* GraphicsLayer::platformLayer() const
1065 {
1066     return m_layer->layer();
1067 }
1068
1069 static bool copyWebCoreFilterOperationsToWebFilterOperations(const FilterOperations& filters, WebFilterOperations& webFilters)
1070 {
1071     for (size_t i = 0; i < filters.size(); ++i) {
1072         const FilterOperation& op = *filters.at(i);
1073         switch (op.type()) {
1074         case FilterOperation::REFERENCE:
1075             return false; // Not supported.
1076         case FilterOperation::GRAYSCALE:
1077         case FilterOperation::SEPIA:
1078         case FilterOperation::SATURATE:
1079         case FilterOperation::HUE_ROTATE: {
1080             float amount = toBasicColorMatrixFilterOperation(op).amount();
1081             switch (op.type()) {
1082             case FilterOperation::GRAYSCALE:
1083                 webFilters.appendGrayscaleFilter(amount);
1084                 break;
1085             case FilterOperation::SEPIA:
1086                 webFilters.appendSepiaFilter(amount);
1087                 break;
1088             case FilterOperation::SATURATE:
1089                 webFilters.appendSaturateFilter(amount);
1090                 break;
1091             case FilterOperation::HUE_ROTATE:
1092                 webFilters.appendHueRotateFilter(amount);
1093                 break;
1094             default:
1095                 ASSERT_NOT_REACHED();
1096             }
1097             break;
1098         }
1099         case FilterOperation::INVERT:
1100         case FilterOperation::OPACITY:
1101         case FilterOperation::BRIGHTNESS:
1102         case FilterOperation::CONTRAST: {
1103             float amount = toBasicComponentTransferFilterOperation(op).amount();
1104             switch (op.type()) {
1105             case FilterOperation::INVERT:
1106                 webFilters.appendInvertFilter(amount);
1107                 break;
1108             case FilterOperation::OPACITY:
1109                 webFilters.appendOpacityFilter(amount);
1110                 break;
1111             case FilterOperation::BRIGHTNESS:
1112                 webFilters.appendBrightnessFilter(amount);
1113                 break;
1114             case FilterOperation::CONTRAST:
1115                 webFilters.appendContrastFilter(amount);
1116                 break;
1117             default:
1118                 ASSERT_NOT_REACHED();
1119             }
1120             break;
1121         }
1122         case FilterOperation::BLUR: {
1123             float pixelRadius = toBlurFilterOperation(op).stdDeviation().getFloatValue();
1124             webFilters.appendBlurFilter(pixelRadius);
1125             break;
1126         }
1127         case FilterOperation::DROP_SHADOW: {
1128             const DropShadowFilterOperation& dropShadowOp = toDropShadowFilterOperation(op);
1129             webFilters.appendDropShadowFilter(WebPoint(dropShadowOp.x(), dropShadowOp.y()), dropShadowOp.stdDeviation(), dropShadowOp.color().rgb());
1130             break;
1131         }
1132         case FilterOperation::CUSTOM:
1133         case FilterOperation::VALIDATED_CUSTOM:
1134             return false; // Not supported.
1135         case FilterOperation::NONE:
1136             break;
1137         }
1138     }
1139     return true;
1140 }
1141
1142 bool GraphicsLayer::setFilters(const FilterOperations& filters)
1143 {
1144     SkiaImageFilterBuilder builder;
1145     OwnPtr<WebFilterOperations> webFilters = adoptPtr(Platform::current()->compositorSupport()->createFilterOperations());
1146     FilterOutsets outsets = filters.outsets();
1147     builder.setCropOffset(FloatSize(outsets.left(), outsets.top()));
1148     if (!builder.buildFilterOperations(filters, webFilters.get())) {
1149         // Make sure the filters are removed from the platform layer, as they are
1150         // going to fallback to software mode.
1151         webFilters->clear();
1152         m_layer->layer()->setFilters(*webFilters);
1153         m_filters = FilterOperations();
1154         return false;
1155     }
1156
1157     m_layer->layer()->setFilters(*webFilters);
1158     m_filters = filters;
1159     return true;
1160 }
1161
1162 void GraphicsLayer::setBackgroundFilters(const FilterOperations& filters)
1163 {
1164     OwnPtr<WebFilterOperations> webFilters = adoptPtr(Platform::current()->compositorSupport()->createFilterOperations());
1165     if (!copyWebCoreFilterOperationsToWebFilterOperations(filters, *webFilters))
1166         return;
1167     m_layer->layer()->setBackgroundFilters(*webFilters);
1168 }
1169
1170 void GraphicsLayer::addLinkHighlight(LinkHighlightClient* linkHighlight)
1171 {
1172     ASSERT(linkHighlight && !m_linkHighlights.contains(linkHighlight));
1173     m_linkHighlights.append(linkHighlight);
1174     linkHighlight->layer()->setWebLayerClient(this);
1175     updateChildList();
1176 }
1177
1178 void GraphicsLayer::removeLinkHighlight(LinkHighlightClient* linkHighlight)
1179 {
1180     m_linkHighlights.remove(m_linkHighlights.find(linkHighlight));
1181     updateChildList();
1182 }
1183
1184 void GraphicsLayer::setScrollableArea(ScrollableArea* scrollableArea, bool isMainFrame)
1185 {
1186     if (m_scrollableArea == scrollableArea)
1187         return;
1188
1189     m_scrollableArea = scrollableArea;
1190
1191     // Main frame scrolling may involve pinch zoom and gets routed through
1192     // WebViewImpl explicitly rather than via GraphicsLayer::didScroll.
1193     if (isMainFrame)
1194         m_layer->layer()->setScrollClient(0);
1195     else
1196         m_layer->layer()->setScrollClient(this);
1197 }
1198
1199 void GraphicsLayer::paint(GraphicsContext& context, const IntRect& clip)
1200 {
1201     paintGraphicsLayerContents(context, clip);
1202 }
1203
1204
1205 void GraphicsLayer::notifyAnimationStarted(double wallClockTime, double monotonicTime, WebAnimation::TargetProperty)
1206 {
1207     if (m_client)
1208         m_client->notifyAnimationStarted(this, wallClockTime, monotonicTime);
1209 }
1210
1211 void GraphicsLayer::notifyAnimationFinished(double, double, WebAnimation::TargetProperty)
1212 {
1213     // Do nothing.
1214 }
1215
1216 void GraphicsLayer::didScroll()
1217 {
1218     if (m_scrollableArea)
1219         m_scrollableArea->scrollToOffsetWithoutAnimation(m_scrollableArea->minimumScrollPosition() + toIntSize(m_layer->layer()->scrollPosition()));
1220 }
1221
1222 } // namespace WebCore
1223
1224 #ifndef NDEBUG
1225 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer)
1226 {
1227     if (!layer)
1228         return;
1229
1230     String output = layer->layerTreeAsText(WebCore::LayerTreeIncludesDebugInfo);
1231     fprintf(stderr, "%s\n", output.utf8().data());
1232 }
1233 #endif