2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "platform/graphics/GraphicsLayer.h"
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"
58 using blink::Platform;
59 using blink::WebAnimation;
60 using blink::WebFilterOperations;
61 using blink::WebLayer;
62 using blink::WebPoint;
66 typedef HashMap<const GraphicsLayer*, Vector<FloatRect> > RepaintMap;
67 static RepaintMap& repaintRectMap()
69 DEFINE_STATIC_LOCAL(RepaintMap, map, ());
73 PassOwnPtr<GraphicsLayer> GraphicsLayer::create(GraphicsLayerFactory* factory, GraphicsLayerClient* client)
75 return factory->createGraphicsLayer(client);
78 GraphicsLayer::GraphicsLayer(GraphicsLayerClient* client)
80 , m_anchorPoint(0.5f, 0.5f, 0)
81 , m_backgroundColor(Color::transparent)
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)
98 , m_contentsClippingMaskLayer(0)
100 , m_replicatedLayer(0)
102 , m_contentsSolidColor(Color::transparent)
104 , m_contentsLayerId(0)
105 , m_scrollableArea(0)
106 , m_3dRenderingContext(0)
110 m_client->verifyNotPainting();
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);
120 GraphicsLayer::~GraphicsLayer()
122 for (size_t i = 0; i < m_linkHighlights.size(); ++i)
123 m_linkHighlights[i]->clearCurrentGraphicsLayer();
124 m_linkHighlights.clear();
128 m_client->verifyNotPainting();
132 m_replicaLayer->setReplicatedLayer(0);
134 if (m_replicatedLayer)
135 m_replicatedLayer->setReplicatedByLayer(0);
140 resetTrackedRepaints();
144 void GraphicsLayer::setParent(GraphicsLayer* layer)
146 ASSERT(!layer || !layer->hasAncestor(this));
150 bool GraphicsLayer::hasAncestor(GraphicsLayer* ancestor) const
152 for (GraphicsLayer* curr = parent(); curr; curr = curr->parent()) {
153 if (curr == ancestor)
160 bool GraphicsLayer::setChildren(const Vector<GraphicsLayer*>& newChildren)
162 // If the contents of the arrays are the same, nothing to do.
163 if (newChildren == m_children)
168 size_t listSize = newChildren.size();
169 for (size_t i = 0; i < listSize; ++i)
170 addChildInternal(newChildren[i]);
177 void GraphicsLayer::addChildInternal(GraphicsLayer* childLayer)
179 ASSERT(childLayer != this);
181 if (childLayer->parent())
182 childLayer->removeFromParent();
184 childLayer->setParent(this);
185 m_children.append(childLayer);
187 // Don't call updateChildList here, this function is used in cases where it
188 // should not be called until all children are processed.
191 void GraphicsLayer::addChild(GraphicsLayer* childLayer)
193 addChildInternal(childLayer);
197 void GraphicsLayer::addChildAtIndex(GraphicsLayer* childLayer, int index)
199 ASSERT(childLayer != this);
201 if (childLayer->parent())
202 childLayer->removeFromParent();
204 childLayer->setParent(this);
205 m_children.insert(index, childLayer);
210 void GraphicsLayer::addChildBelow(GraphicsLayer* childLayer, GraphicsLayer* sibling)
212 ASSERT(childLayer != this);
213 childLayer->removeFromParent();
216 for (unsigned i = 0; i < m_children.size(); i++) {
217 if (sibling == m_children[i]) {
218 m_children.insert(i, childLayer);
224 childLayer->setParent(this);
227 m_children.append(childLayer);
232 void GraphicsLayer::addChildAbove(GraphicsLayer* childLayer, GraphicsLayer* sibling)
234 childLayer->removeFromParent();
235 ASSERT(childLayer != this);
238 for (unsigned i = 0; i < m_children.size(); i++) {
239 if (sibling == m_children[i]) {
240 m_children.insert(i+1, childLayer);
246 childLayer->setParent(this);
249 m_children.append(childLayer);
254 bool GraphicsLayer::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
256 ASSERT(!newChild->parent());
258 for (unsigned i = 0; i < m_children.size(); i++) {
259 if (oldChild == m_children[i]) {
260 m_children[i] = newChild;
267 oldChild->setParent(0);
269 newChild->removeFromParent();
270 newChild->setParent(this);
279 void GraphicsLayer::removeAllChildren()
281 while (m_children.size()) {
282 GraphicsLayer* curLayer = m_children[0];
283 ASSERT(curLayer->parent());
284 curLayer->removeFromParent();
288 void GraphicsLayer::removeFromParent()
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);
302 platformLayer()->removeFromParent();
305 void GraphicsLayer::setReplicatedByLayer(GraphicsLayer* layer)
307 // FIXME: this could probably be a full early exit.
308 if (m_replicaLayer != layer) {
310 m_replicaLayer->setReplicatedLayer(0);
313 layer->setReplicatedLayer(this);
315 m_replicaLayer = layer;
318 WebLayer* webReplicaLayer = layer ? layer->platformLayer() : 0;
319 platformLayer()->setReplicaLayer(webReplicaLayer);
322 void GraphicsLayer::setOffsetFromRenderer(const IntSize& offset, ShouldSetNeedsDisplay shouldSetNeedsDisplay)
324 if (offset == m_offsetFromRenderer)
327 m_offsetFromRenderer = offset;
329 // If the compositing layer offset changes, we need to repaint.
330 if (shouldSetNeedsDisplay == SetNeedsDisplay)
334 void GraphicsLayer::paintGraphicsLayerContents(GraphicsContext& context, const IntRect& clip)
338 incrementPaintCount();
339 m_client->paintContents(this, context, m_paintingPhase, clip);
342 void GraphicsLayer::setZPosition(float position)
344 m_zPosition = position;
347 void GraphicsLayer::updateChildList()
349 WebLayer* childHost = m_layer->layer();
350 childHost->removeAllChildren();
352 clearContentsLayerIfUnregistered();
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);
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];
366 childHost->addChild(curChild->platformLayer());
369 for (size_t i = 0; i < m_linkHighlights.size(); ++i)
370 childHost->addChild(m_linkHighlights[i]->layer());
373 void GraphicsLayer::updateLayerIsDrawable()
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.
380 m_layer->layer()->setDrawsContent(m_drawsContent && m_contentsVisible);
381 if (WebLayer* contentsLayer = contentsLayerIfRegistered())
382 contentsLayer->setDrawsContent(m_contentsVisible);
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();
391 void GraphicsLayer::updateContentsRect()
393 WebLayer* contentsLayer = contentsLayerIfRegistered();
397 contentsLayer->setPosition(FloatPoint(m_contentsRect.x(), m_contentsRect.y()));
398 contentsLayer->setBounds(IntSize(m_contentsRect.width(), m_contentsRect.height()));
400 if (m_contentsClippingMaskLayer) {
401 if (m_contentsClippingMaskLayer->size() != m_contentsRect.size()) {
402 m_contentsClippingMaskLayer->setSize(m_contentsRect.size());
403 m_contentsClippingMaskLayer->setNeedsDisplay();
405 m_contentsClippingMaskLayer->setPosition(FloatPoint());
406 m_contentsClippingMaskLayer->setOffsetFromRenderer(offsetFromRenderer() + IntSize(m_contentsRect.location().x(), m_contentsRect.location().y()));
410 static HashSet<int>* s_registeredLayerSet;
412 void GraphicsLayer::registerContentsLayer(WebLayer* layer)
414 if (!s_registeredLayerSet)
415 s_registeredLayerSet = new HashSet<int>;
416 if (s_registeredLayerSet->contains(layer->id()))
418 s_registeredLayerSet->add(layer->id());
421 void GraphicsLayer::unregisterContentsLayer(WebLayer* layer)
423 ASSERT(s_registeredLayerSet);
424 if (!s_registeredLayerSet->contains(layer->id()))
426 s_registeredLayerSet->remove(layer->id());
429 void GraphicsLayer::setContentsTo(WebLayer* layer)
431 bool childrenChanged = false;
433 ASSERT(s_registeredLayerSet);
434 if (!s_registeredLayerSet->contains(layer->id()))
436 if (m_contentsLayerId != layer->id()) {
437 setupContentsLayer(layer);
438 childrenChanged = true;
440 updateContentsRect();
442 if (m_contentsLayer) {
443 childrenChanged = true;
445 // The old contents layer will be removed via updateChildList.
447 m_contentsLayerId = 0;
455 void GraphicsLayer::setupContentsLayer(WebLayer* contentsLayer)
457 ASSERT(contentsLayer);
458 m_contentsLayer = contentsLayer;
459 m_contentsLayerId = m_contentsLayer->id();
461 m_contentsLayer->setWebLayerClient(this);
462 m_contentsLayer->setAnchorPoint(FloatPoint(0, 0));
463 m_contentsLayer->setUseParentBackfaceVisibility(true);
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);
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);
475 m_contentsLayer->setRenderingContext(m_3dRenderingContext);
478 void GraphicsLayer::clearContentsLayerIfUnregistered()
480 if (!m_contentsLayerId || s_registeredLayerSet->contains(m_contentsLayerId))
484 m_contentsLayerId = 0;
487 GraphicsLayerDebugInfo& GraphicsLayer::debugInfo()
492 blink::WebGraphicsLayerDebugInfo* GraphicsLayer::takeDebugInfoFor(WebLayer* layer)
494 GraphicsLayerDebugInfo* clone = m_debugInfo.clone();
495 clone->setDebugName(debugName(layer));
499 WebLayer* GraphicsLayer::contentsLayerIfRegistered()
501 clearContentsLayerIfUnregistered();
502 return m_contentsLayer;
505 void GraphicsLayer::resetTrackedRepaints()
507 repaintRectMap().remove(this);
510 void GraphicsLayer::addRepaintRect(const FloatRect& repaintRect)
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);
521 Vector<FloatRect>& repaintRects = repaintIt->value;
522 repaintRects.append(largestRepaintRect);
527 void GraphicsLayer::collectTrackedRepaintRects(Vector<FloatRect>& rects) const
529 if (!m_client->isTrackingRepaints())
532 RepaintMap::iterator repaintIt = repaintRectMap().find(this);
533 if (repaintIt != repaintRectMap().end())
534 rects.append(repaintIt->value);
537 void GraphicsLayer::dumpLayer(TextStream& ts, int indent, LayerTreeFlags flags, RenderingContextMap& renderingContextMap) const
539 writeIndent(ts, indent);
540 ts << "(" << "GraphicsLayer";
542 if (flags & LayerTreeIncludesDebugInfo) {
543 ts << " " << static_cast<void*>(const_cast<GraphicsLayer*>(this));
544 ts << " \"" << m_client->debugName(this) << "\"";
548 dumpProperties(ts, indent, flags, renderingContextMap);
549 writeIndent(ts, indent);
553 void GraphicsLayer::dumpProperties(TextStream& ts, int indent, LayerTreeFlags flags, RenderingContextMap& renderingContextMap) const
555 if (m_position != FloatPoint()) {
556 writeIndent(ts, indent + 1);
557 ts << "(position " << m_position.x() << " " << m_position.y() << ")\n";
560 if (m_boundsOrigin != FloatPoint()) {
561 writeIndent(ts, indent + 1);
562 ts << "(bounds origin " << m_boundsOrigin.x() << " " << m_boundsOrigin.y() << ")\n";
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";
570 if (m_size != IntSize()) {
571 writeIndent(ts, indent + 1);
572 ts << "(bounds " << m_size.width() << " " << m_size.height() << ")\n";
575 if (m_opacity != 1) {
576 writeIndent(ts, indent + 1);
577 ts << "(opacity " << m_opacity << ")\n";
580 if (m_blendMode != blink::WebBlendModeNormal) {
581 writeIndent(ts, indent + 1);
582 ts << "(blendMode " << compositeOperatorName(CompositeSourceOver, m_blendMode) << ")\n";
585 if (m_isRootForIsolatedGroup) {
586 writeIndent(ts, indent + 1);
587 ts << "(isolate " << m_isRootForIsolatedGroup << ")\n";
590 if (m_contentsOpaque) {
591 writeIndent(ts, indent + 1);
592 ts << "(contentsOpaque " << m_contentsOpaque << ")\n";
595 if (!m_shouldFlattenTransform) {
596 writeIndent(ts, indent + 1);
597 ts << "(shouldFlattenTransform " << m_shouldFlattenTransform << ")\n";
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);
606 contextId = it->value;
608 writeIndent(ts, indent + 1);
609 ts << "(3dRenderingContext " << contextId << ")\n";
612 if (m_drawsContent) {
613 writeIndent(ts, indent + 1);
614 ts << "(drawsContent " << m_drawsContent << ")\n";
617 if (!m_contentsVisible) {
618 writeIndent(ts, indent + 1);
619 ts << "(contentsVisible " << m_contentsVisible << ")\n";
622 if (!m_backfaceVisibility) {
623 writeIndent(ts, indent + 1);
624 ts << "(backfaceVisibility " << (m_backfaceVisibility ? "visible" : "hidden") << ")\n";
627 if (flags & LayerTreeIncludesDebugInfo) {
628 writeIndent(ts, indent + 1);
631 ts << "client " << static_cast<void*>(m_client);
637 if (m_backgroundColor.alpha()) {
638 writeIndent(ts, indent + 1);
639 ts << "(backgroundColor " << m_backgroundColor.nameForRenderTreeAsText() << ")\n";
642 if (!m_transform.isIdentity()) {
643 writeIndent(ts, indent + 1);
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";
651 if (m_replicaLayer) {
652 writeIndent(ts, indent + 1);
653 ts << "(replica layer";
654 if (flags & LayerTreeIncludesDebugInfo)
655 ts << " " << m_replicaLayer;
657 m_replicaLayer->dumpLayer(ts, indent + 2, flags, renderingContextMap);
660 if (m_replicatedLayer) {
661 writeIndent(ts, indent + 1);
662 ts << "(replicated layer";
663 if (flags & LayerTreeIncludesDebugInfo)
664 ts << " " << m_replicatedLayer;
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())
674 writeIndent(ts, indent + 2);
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();
682 writeIndent(ts, indent + 1);
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";
693 if (paintingPhase() & GraphicsLayerPaintForeground) {
694 writeIndent(ts, indent + 2);
695 ts << "GraphicsLayerPaintForeground\n";
697 if (paintingPhase() & GraphicsLayerPaintMask) {
698 writeIndent(ts, indent + 2);
699 ts << "GraphicsLayerPaintMask\n";
701 if (paintingPhase() & GraphicsLayerPaintChildClippingMask) {
702 writeIndent(ts, indent + 2);
703 ts << "GraphicsLayerPaintChildClippingMask\n";
705 if (paintingPhase() & GraphicsLayerPaintOverflowContents) {
706 writeIndent(ts, indent + 2);
707 ts << "GraphicsLayerPaintOverflowContents\n";
709 if (paintingPhase() & GraphicsLayerPaintCompositedScroll) {
710 writeIndent(ts, indent + 2);
711 ts << "GraphicsLayerPaintCompositedScroll\n";
713 writeIndent(ts, indent + 1);
717 if (flags & LayerTreeIncludesClipAndScrollParents) {
718 if (m_hasScrollParent) {
719 writeIndent(ts, indent + 1);
720 ts << "(hasScrollParent 1)\n";
722 if (m_hasClipParent) {
723 writeIndent(ts, indent + 1);
724 ts << "(hasClipParent 1)\n";
728 if (m_children.size()) {
729 writeIndent(ts, indent + 1);
730 ts << "(children " << m_children.size() << "\n";
733 for (i = 0; i < m_children.size(); i++)
734 m_children[i]->dumpLayer(ts, indent + 2, flags, renderingContextMap);
735 writeIndent(ts, indent + 1);
740 String GraphicsLayer::layerTreeAsText(LayerTreeFlags flags) const
744 RenderingContextMap renderingContextMap;
745 dumpLayer(ts, 0, flags, renderingContextMap);
749 String GraphicsLayer::debugName(blink::WebLayer* webLayer) const
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);
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);
770 ASSERT_NOT_REACHED();
775 void GraphicsLayer::setCompositingReasons(CompositingReasons reasons)
777 m_debugInfo.setCompositingReasons(reasons);
780 void GraphicsLayer::setPosition(const FloatPoint& point)
783 platformLayer()->setPosition(m_position);
786 void GraphicsLayer::setAnchorPoint(const FloatPoint3D& point)
788 m_anchorPoint = point;
789 platformLayer()->setAnchorPoint(FloatPoint(m_anchorPoint.x(), m_anchorPoint.y()));
790 platformLayer()->setAnchorPointZ(m_anchorPoint.z());
793 void GraphicsLayer::setSize(const FloatSize& size)
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();
802 if (clampedSize == m_size)
805 m_size = clampedSize;
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.
811 void GraphicsLayer::setTransform(const TransformationMatrix& transform)
813 m_transform = transform;
814 platformLayer()->setTransform(TransformationMatrix::toSkMatrix44(m_transform));
817 void GraphicsLayer::setShouldFlattenTransform(bool shouldFlatten)
819 if (shouldFlatten == m_shouldFlattenTransform)
822 m_shouldFlattenTransform = shouldFlatten;
824 m_layer->layer()->setShouldFlattenTransform(shouldFlatten);
827 void GraphicsLayer::setRenderingContext(int context)
829 if (m_3dRenderingContext == context)
832 m_3dRenderingContext = context;
833 m_layer->layer()->setRenderingContext(context);
836 m_contentsLayer->setRenderingContext(m_3dRenderingContext);
839 void GraphicsLayer::setMasksToBounds(bool masksToBounds)
841 m_masksToBounds = masksToBounds;
842 m_layer->layer()->setMasksToBounds(m_masksToBounds);
845 void GraphicsLayer::setDrawsContent(bool drawsContent)
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)
852 m_drawsContent = drawsContent;
853 updateLayerIsDrawable();
856 void GraphicsLayer::setContentsVisible(bool contentsVisible)
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)
863 m_contentsVisible = contentsVisible;
864 updateLayerIsDrawable();
867 void GraphicsLayer::setClipParent(blink::WebLayer* parent)
869 m_hasClipParent = !!parent;
870 m_layer->layer()->setClipParent(parent);
873 void GraphicsLayer::setScrollParent(blink::WebLayer* parent)
875 m_hasScrollParent = !!parent;
876 m_layer->layer()->setScrollParent(parent);
879 void GraphicsLayer::setBackgroundColor(const Color& color)
881 if (color == m_backgroundColor)
884 m_backgroundColor = color;
885 m_layer->layer()->setBackgroundColor(m_backgroundColor.rgb());
888 void GraphicsLayer::setContentsOpaque(bool opaque)
890 m_contentsOpaque = opaque;
891 m_layer->layer()->setOpaque(m_contentsOpaque);
892 m_opaqueRectTrackingContentLayerDelegate->setOpaque(m_contentsOpaque);
895 void GraphicsLayer::setMaskLayer(GraphicsLayer* maskLayer)
897 if (maskLayer == m_maskLayer)
900 m_maskLayer = maskLayer;
901 WebLayer* maskWebLayer = m_maskLayer ? m_maskLayer->platformLayer() : 0;
902 m_layer->layer()->setMaskLayer(maskWebLayer);
905 void GraphicsLayer::setContentsClippingMaskLayer(GraphicsLayer* contentsClippingMaskLayer)
907 if (contentsClippingMaskLayer == m_contentsClippingMaskLayer)
910 m_contentsClippingMaskLayer = contentsClippingMaskLayer;
911 WebLayer* contentsLayer = contentsLayerIfRegistered();
914 WebLayer* contentsClippingMaskWebLayer = m_contentsClippingMaskLayer ? m_contentsClippingMaskLayer->platformLayer() : 0;
915 contentsLayer->setMaskLayer(contentsClippingMaskWebLayer);
916 updateContentsRect();
919 void GraphicsLayer::setBackfaceVisibility(bool visible)
921 m_backfaceVisibility = visible;
922 m_layer->setDoubleSided(m_backfaceVisibility);
925 void GraphicsLayer::setOpacity(float opacity)
927 float clampedOpacity = std::max(std::min(opacity, 1.0f), 0.0f);
928 m_opacity = clampedOpacity;
929 platformLayer()->setOpacity(opacity);
932 void GraphicsLayer::setBlendMode(blink::WebBlendMode blendMode)
934 if (m_blendMode == blendMode)
936 m_blendMode = blendMode;
937 platformLayer()->setBlendMode(blink::WebBlendMode(blendMode));
940 void GraphicsLayer::setIsRootForIsolatedGroup(bool isolated)
942 if (m_isRootForIsolatedGroup == isolated)
944 m_isRootForIsolatedGroup = isolated;
945 platformLayer()->setIsRootForIsolatedGroup(isolated);
948 void GraphicsLayer::setContentsNeedsDisplay()
950 if (WebLayer* contentsLayer = contentsLayerIfRegistered()) {
951 contentsLayer->invalidate();
952 addRepaintRect(contentsRect());
956 void GraphicsLayer::setNeedsDisplay()
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();
966 void GraphicsLayer::setNeedsDisplayInRect(const FloatRect& rect)
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();
976 void GraphicsLayer::setContentsRect(const IntRect& rect)
978 if (rect == m_contentsRect)
981 m_contentsRect = rect;
982 updateContentsRect();
985 void GraphicsLayer::setContentsToImage(Image* image)
987 RefPtr<NativeImageSkia> nativeImage = image ? image->nativeImageForCurrentFrame() : 0;
990 m_imageLayer = adoptPtr(Platform::current()->compositorSupport()->createImageLayer());
991 registerContentsLayer(m_imageLayer->layer());
993 m_imageLayer->setBitmap(nativeImage->bitmap());
994 m_imageLayer->layer()->setOpaque(image->currentFrameKnownToBeOpaque());
995 updateContentsRect();
998 unregisterContentsLayer(m_imageLayer->layer());
999 m_imageLayer.clear();
1003 setContentsTo(m_imageLayer ? m_imageLayer->layer() : 0);
1006 void GraphicsLayer::setContentsToNinePatch(Image* image, const IntRect& aperture)
1008 if (m_ninePatchLayer) {
1009 unregisterContentsLayer(m_ninePatchLayer->layer());
1010 m_ninePatchLayer.clear();
1012 RefPtr<NativeImageSkia> nativeImage = image ? image->nativeImageForCurrentFrame() : 0;
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());
1019 setContentsTo(m_ninePatchLayer ? m_ninePatchLayer->layer() : 0);
1022 void GraphicsLayer::setContentsToSolidColor(const Color& color)
1024 if (color == m_contentsSolidColor)
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());
1033 m_solidColorLayer->setBackgroundColor(color.rgb());
1035 if (!m_solidColorLayer)
1037 unregisterContentsLayer(m_solidColorLayer->layer());
1038 m_solidColorLayer.clear();
1040 setContentsTo(m_solidColorLayer ? m_solidColorLayer->layer() : 0);
1043 bool GraphicsLayer::addAnimation(PassOwnPtr<WebAnimation> popAnimation)
1045 OwnPtr<WebAnimation> animation(popAnimation);
1047 platformLayer()->setAnimationDelegate(this);
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());
1054 void GraphicsLayer::pauseAnimation(int animationId, double timeOffset)
1056 platformLayer()->pauseAnimation(animationId, timeOffset);
1059 void GraphicsLayer::removeAnimation(int animationId)
1061 platformLayer()->removeAnimation(animationId);
1064 WebLayer* GraphicsLayer::platformLayer() const
1066 return m_layer->layer();
1069 static bool copyWebCoreFilterOperationsToWebFilterOperations(const FilterOperations& filters, WebFilterOperations& webFilters)
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);
1085 case FilterOperation::SEPIA:
1086 webFilters.appendSepiaFilter(amount);
1088 case FilterOperation::SATURATE:
1089 webFilters.appendSaturateFilter(amount);
1091 case FilterOperation::HUE_ROTATE:
1092 webFilters.appendHueRotateFilter(amount);
1095 ASSERT_NOT_REACHED();
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);
1108 case FilterOperation::OPACITY:
1109 webFilters.appendOpacityFilter(amount);
1111 case FilterOperation::BRIGHTNESS:
1112 webFilters.appendBrightnessFilter(amount);
1114 case FilterOperation::CONTRAST:
1115 webFilters.appendContrastFilter(amount);
1118 ASSERT_NOT_REACHED();
1122 case FilterOperation::BLUR: {
1123 float pixelRadius = toBlurFilterOperation(op).stdDeviation().getFloatValue();
1124 webFilters.appendBlurFilter(pixelRadius);
1127 case FilterOperation::DROP_SHADOW: {
1128 const DropShadowFilterOperation& dropShadowOp = toDropShadowFilterOperation(op);
1129 webFilters.appendDropShadowFilter(WebPoint(dropShadowOp.x(), dropShadowOp.y()), dropShadowOp.stdDeviation(), dropShadowOp.color().rgb());
1132 case FilterOperation::CUSTOM:
1133 case FilterOperation::VALIDATED_CUSTOM:
1134 return false; // Not supported.
1135 case FilterOperation::NONE:
1142 bool GraphicsLayer::setFilters(const FilterOperations& filters)
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();
1157 m_layer->layer()->setFilters(*webFilters);
1158 m_filters = filters;
1162 void GraphicsLayer::setBackgroundFilters(const FilterOperations& filters)
1164 OwnPtr<WebFilterOperations> webFilters = adoptPtr(Platform::current()->compositorSupport()->createFilterOperations());
1165 if (!copyWebCoreFilterOperationsToWebFilterOperations(filters, *webFilters))
1167 m_layer->layer()->setBackgroundFilters(*webFilters);
1170 void GraphicsLayer::addLinkHighlight(LinkHighlightClient* linkHighlight)
1172 ASSERT(linkHighlight && !m_linkHighlights.contains(linkHighlight));
1173 m_linkHighlights.append(linkHighlight);
1174 linkHighlight->layer()->setWebLayerClient(this);
1178 void GraphicsLayer::removeLinkHighlight(LinkHighlightClient* linkHighlight)
1180 m_linkHighlights.remove(m_linkHighlights.find(linkHighlight));
1184 void GraphicsLayer::setScrollableArea(ScrollableArea* scrollableArea, bool isMainFrame)
1186 if (m_scrollableArea == scrollableArea)
1189 m_scrollableArea = scrollableArea;
1191 // Main frame scrolling may involve pinch zoom and gets routed through
1192 // WebViewImpl explicitly rather than via GraphicsLayer::didScroll.
1194 m_layer->layer()->setScrollClient(0);
1196 m_layer->layer()->setScrollClient(this);
1199 void GraphicsLayer::paint(GraphicsContext& context, const IntRect& clip)
1201 paintGraphicsLayerContents(context, clip);
1205 void GraphicsLayer::notifyAnimationStarted(double wallClockTime, double monotonicTime, WebAnimation::TargetProperty)
1208 m_client->notifyAnimationStarted(this, wallClockTime, monotonicTime);
1211 void GraphicsLayer::notifyAnimationFinished(double, double, WebAnimation::TargetProperty)
1216 void GraphicsLayer::didScroll()
1218 if (m_scrollableArea)
1219 m_scrollableArea->scrollToOffsetWithoutAnimation(m_scrollableArea->minimumScrollPosition() + toIntSize(m_layer->layer()->scrollPosition()));
1222 } // namespace WebCore
1225 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer)
1230 String output = layer->layerTreeAsText(WebCore::LayerTreeIncludesDebugInfo);
1231 fprintf(stderr, "%s\n", output.utf8().data());