2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2009 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * This file borrows code heavily from platform/graphics/win/GraphicsLayerCACF.cpp
35 * (and hence it includes both copyrights)
36 * Ideally the common code (mostly the code that keeps track of the layer hierarchy)
37 * should be kept separate and shared between platforms. It would be a well worthwhile
38 * effort once the Windows implementation (binaries and headers) of CoreAnimation is
39 * checked in to the WebKit repository. Until then only Apple can make this happen.
44 #if USE(ACCELERATED_COMPOSITING)
46 #include "GraphicsLayerChromium.h"
48 #include "Canvas2DLayerChromium.h"
49 #include "ContentLayerChromium.h"
50 #include "DrawingBuffer.h"
51 #include "FloatConversion.h"
52 #include "FloatRect.h"
54 #include "ImageLayerChromium.h"
55 #include "LayerChromium.h"
56 #include "PlatformString.h"
57 #include "SystemTime.h"
59 #include <wtf/CurrentTime.h>
60 #include <wtf/StringExtras.h>
61 #include <wtf/text/CString.h>
67 static void setLayerBorderColor(LayerChromium& layer, const Color& color)
69 layer.setBorderColor(color);
72 static void clearBorderColor(LayerChromium& layer)
74 layer.setBorderColor(static_cast<RGBA32>(0));
77 static void setLayerBackgroundColor(LayerChromium& layer, const Color& color)
79 layer.setBackgroundColor(color);
82 static void clearLayerBackgroundColor(LayerChromium& layer)
84 layer.setBackgroundColor(static_cast<RGBA32>(0));
87 PassOwnPtr<GraphicsLayer> GraphicsLayer::create(GraphicsLayerClient* client)
89 return new GraphicsLayerChromium(client);
92 GraphicsLayerChromium::GraphicsLayerChromium(GraphicsLayerClient* client)
93 : GraphicsLayer(client)
94 , m_contentsLayerPurpose(NoContentsLayer)
95 , m_contentsLayerHasBackgroundColor(false)
97 m_layer = ContentLayerChromium::create(this);
99 updateDebugIndicators();
102 GraphicsLayerChromium::~GraphicsLayerChromium()
105 m_layer->setOwner(0);
107 m_contentsLayer->setOwner(0);
108 if (m_transformLayer)
109 m_transformLayer->setOwner(0);
112 void GraphicsLayerChromium::setName(const String& inName)
114 String name = String::format("GraphicsLayerChromium(%p) GraphicsLayer(%p) ", m_layer.get(), this) + inName;
115 GraphicsLayer::setName(name);
118 bool GraphicsLayerChromium::setChildren(const Vector<GraphicsLayer*>& children)
120 bool childrenChanged = GraphicsLayer::setChildren(children);
121 // FIXME: GraphicsLayer::setChildren calls addChild() for each sublayer, which
122 // will end up calling updateSublayerList() N times.
124 updateSublayerList();
126 return childrenChanged;
129 void GraphicsLayerChromium::addChild(GraphicsLayer* childLayer)
131 GraphicsLayer::addChild(childLayer);
132 updateSublayerList();
135 void GraphicsLayerChromium::addChildAtIndex(GraphicsLayer* childLayer, int index)
137 GraphicsLayer::addChildAtIndex(childLayer, index);
138 updateSublayerList();
141 void GraphicsLayerChromium::addChildBelow(GraphicsLayer* childLayer, GraphicsLayer* sibling)
143 GraphicsLayer::addChildBelow(childLayer, sibling);
144 updateSublayerList();
147 void GraphicsLayerChromium::addChildAbove(GraphicsLayer* childLayer, GraphicsLayer *sibling)
149 GraphicsLayer::addChildAbove(childLayer, sibling);
150 updateSublayerList();
153 bool GraphicsLayerChromium::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
155 if (GraphicsLayer::replaceChild(oldChild, newChild)) {
156 updateSublayerList();
162 void GraphicsLayerChromium::removeFromParent()
164 GraphicsLayer::removeFromParent();
165 layerForSuperlayer()->removeFromSuperlayer();
168 void GraphicsLayerChromium::setPosition(const FloatPoint& point)
170 GraphicsLayer::setPosition(point);
171 updateLayerPosition();
174 void GraphicsLayerChromium::setAnchorPoint(const FloatPoint3D& point)
176 if (point == m_anchorPoint)
179 GraphicsLayer::setAnchorPoint(point);
183 void GraphicsLayerChromium::setSize(const FloatSize& size)
188 GraphicsLayer::setSize(size);
192 void GraphicsLayerChromium::setTransform(const TransformationMatrix& transform)
194 if (transform == m_transform)
197 GraphicsLayer::setTransform(transform);
201 void GraphicsLayerChromium::setChildrenTransform(const TransformationMatrix& transform)
203 if (transform == m_childrenTransform)
206 GraphicsLayer::setChildrenTransform(transform);
207 updateChildrenTransform();
210 void GraphicsLayerChromium::setPreserves3D(bool preserves3D)
212 if (preserves3D == m_preserves3D)
215 GraphicsLayer::setPreserves3D(preserves3D);
216 updateLayerPreserves3D();
219 void GraphicsLayerChromium::setMasksToBounds(bool masksToBounds)
221 if (masksToBounds == m_masksToBounds)
224 GraphicsLayer::setMasksToBounds(masksToBounds);
225 updateMasksToBounds();
228 void GraphicsLayerChromium::setDrawsContent(bool drawsContent)
230 if (drawsContent == m_drawsContent)
233 GraphicsLayer::setDrawsContent(drawsContent);
234 updateLayerDrawsContent();
237 void GraphicsLayerChromium::setBackgroundColor(const Color& color)
239 if (m_backgroundColorSet && m_backgroundColor == color)
242 GraphicsLayer::setBackgroundColor(color);
244 m_contentsLayerHasBackgroundColor = true;
245 updateLayerBackgroundColor();
248 void GraphicsLayerChromium::clearBackgroundColor()
250 if (!m_backgroundColorSet)
253 GraphicsLayer::clearBackgroundColor();
254 clearLayerBackgroundColor(*m_contentsLayer);
257 void GraphicsLayerChromium::setContentsOpaque(bool opaque)
259 if (m_contentsOpaque == opaque)
262 GraphicsLayer::setContentsOpaque(opaque);
263 updateContentsOpaque();
266 void GraphicsLayerChromium::setBackfaceVisibility(bool visible)
268 if (m_backfaceVisibility == visible)
271 GraphicsLayer::setBackfaceVisibility(visible);
272 updateBackfaceVisibility();
275 void GraphicsLayerChromium::setOpacity(float opacity)
277 float clampedOpacity = max(min(opacity, 1.0f), 0.0f);
279 if (m_opacity == clampedOpacity)
282 GraphicsLayer::setOpacity(clampedOpacity);
283 primaryLayer()->setOpacity(opacity);
286 void GraphicsLayerChromium::setContentsNeedsDisplay()
289 m_contentsLayer->setNeedsDisplay();
292 void GraphicsLayerChromium::setNeedsDisplay()
295 m_layer->setNeedsDisplay();
298 void GraphicsLayerChromium::setNeedsDisplayInRect(const FloatRect& rect)
301 m_layer->setNeedsDisplay(rect);
304 void GraphicsLayerChromium::setContentsRect(const IntRect& rect)
306 if (rect == m_contentsRect)
309 GraphicsLayer::setContentsRect(rect);
310 updateContentsRect();
313 void GraphicsLayerChromium::setContentsToImage(Image* image)
315 bool childrenChanged = false;
317 if (!m_contentsLayer.get() || m_contentsLayerPurpose != ContentsLayerForImage) {
318 RefPtr<ImageLayerChromium> imageLayer = ImageLayerChromium::create(this);
319 setupContentsLayer(imageLayer.get());
320 m_contentsLayer = imageLayer;
321 m_contentsLayerPurpose = ContentsLayerForImage;
322 childrenChanged = true;
324 ImageLayerChromium* imageLayer = static_cast<ImageLayerChromium*>(m_contentsLayer.get());
325 imageLayer->setContents(image);
326 updateContentsRect();
328 if (m_contentsLayer) {
329 childrenChanged = true;
331 // The old contents layer will be removed via updateSublayerList.
337 updateSublayerList();
340 void GraphicsLayerChromium::setContentsToCanvas(PlatformLayer* platformLayer)
342 bool childrenChanged = false;
344 platformLayer->setOwner(this);
345 if (m_contentsLayer.get() != platformLayer) {
346 setupContentsLayer(platformLayer);
347 m_contentsLayer = platformLayer;
348 m_contentsLayerPurpose = ContentsLayerForCanvas;
349 childrenChanged = true;
351 m_contentsLayer->setNeedsDisplay();
352 updateContentsRect();
354 if (m_contentsLayer) {
355 childrenChanged = true;
357 // The old contents layer will be removed via updateSublayerList.
363 updateSublayerList();
366 void GraphicsLayerChromium::setContentsToMedia(PlatformLayer* layer)
368 bool childrenChanged = false;
370 if (!m_contentsLayer.get() || m_contentsLayerPurpose != ContentsLayerForVideo) {
371 setupContentsLayer(layer);
372 m_contentsLayer = layer;
373 m_contentsLayerPurpose = ContentsLayerForVideo;
374 childrenChanged = true;
376 layer->setOwner(this);
377 layer->setNeedsDisplay();
378 updateContentsRect();
380 if (m_contentsLayer) {
381 childrenChanged = true;
383 // The old contents layer will be removed via updateSublayerList.
389 updateSublayerList();
392 PlatformLayer* GraphicsLayerChromium::hostLayerForSublayers() const
394 return m_transformLayer ? m_transformLayer.get() : m_layer.get();
397 PlatformLayer* GraphicsLayerChromium::layerForSuperlayer() const
399 return m_transformLayer ? m_transformLayer.get() : m_layer.get();
402 PlatformLayer* GraphicsLayerChromium::platformLayer() const
404 return primaryLayer();
407 void GraphicsLayerChromium::setDebugBackgroundColor(const Color& color)
410 setLayerBackgroundColor(*m_layer, color);
412 clearLayerBackgroundColor(*m_layer);
415 void GraphicsLayerChromium::setDebugBorder(const Color& color, float borderWidth)
417 if (color.isValid()) {
418 setLayerBorderColor(*m_layer, color);
419 m_layer->setBorderWidth(borderWidth);
421 clearBorderColor(*m_layer);
422 m_layer->setBorderWidth(0);
426 void GraphicsLayerChromium::updateSublayerList()
428 Vector<RefPtr<LayerChromium> > newSublayers;
430 if (m_transformLayer) {
431 // Add the primary layer first. Even if we have negative z-order children, the primary layer always comes behind.
432 newSublayers.append(m_layer.get());
433 } else if (m_contentsLayer) {
434 // FIXME: add the contents layer in the correct order with negative z-order children.
435 // This does not cause visible rendering issues because currently contents layers are only used
436 // for replaced elements that don't have children.
437 newSublayers.append(m_contentsLayer.get());
440 const Vector<GraphicsLayer*>& childLayers = children();
441 size_t numChildren = childLayers.size();
442 for (size_t i = 0; i < numChildren; ++i) {
443 GraphicsLayerChromium* curChild = static_cast<GraphicsLayerChromium*>(childLayers[i]);
445 LayerChromium* childLayer = curChild->layerForSuperlayer();
446 newSublayers.append(childLayer);
449 for (size_t i = 0; i < newSublayers.size(); ++i)
450 newSublayers[i]->removeFromSuperlayer();
452 if (m_transformLayer) {
453 m_transformLayer->setSublayers(newSublayers);
455 if (m_contentsLayer) {
456 // If we have a transform layer, then the contents layer is parented in the
457 // primary layer (which is itself a child of the transform layer).
458 m_layer->removeAllSublayers();
459 m_layer->addSublayer(m_contentsLayer);
462 m_layer->setSublayers(newSublayers);
465 void GraphicsLayerChromium::updateLayerPosition()
467 // Position is offset on the layer by the layer anchor point.
468 FloatPoint layerPosition(m_position.x() + m_anchorPoint.x() * m_size.width(),
469 m_position.y() + m_anchorPoint.y() * m_size.height());
471 primaryLayer()->setPosition(layerPosition);
474 void GraphicsLayerChromium::updateLayerSize()
476 IntSize layerSize(m_size.width(), m_size.height());
477 if (m_transformLayer) {
478 m_transformLayer->setBounds(layerSize);
479 // The anchor of the contents layer is always at 0.5, 0.5, so the position is center-relative.
480 FloatPoint centerPoint(m_size.width() / 2, m_size.height() / 2);
481 m_layer->setPosition(centerPoint);
484 m_layer->setBounds(layerSize);
486 // Note that we don't resize m_contentsLayer. It's up the caller to do that.
488 // If we've changed the bounds, we need to recalculate the position
489 // of the layer, taking anchor point into account.
490 updateLayerPosition();
493 void GraphicsLayerChromium::updateAnchorPoint()
495 primaryLayer()->setAnchorPoint(FloatPoint(m_anchorPoint.x(), m_anchorPoint.y()));
496 primaryLayer()->setAnchorPointZ(m_anchorPoint.z());
497 updateLayerPosition();
500 void GraphicsLayerChromium::updateTransform()
502 primaryLayer()->setTransform(m_transform);
505 void GraphicsLayerChromium::updateChildrenTransform()
507 primaryLayer()->setSublayerTransform(m_childrenTransform);
510 void GraphicsLayerChromium::updateMasksToBounds()
512 m_layer->setMasksToBounds(m_masksToBounds);
513 updateDebugIndicators();
516 void GraphicsLayerChromium::updateContentsOpaque()
518 m_layer->setOpaque(m_contentsOpaque);
521 void GraphicsLayerChromium::updateBackfaceVisibility()
523 m_layer->setDoubleSided(m_backfaceVisibility);
526 void GraphicsLayerChromium::updateLayerPreserves3D()
528 if (m_preserves3D && !m_transformLayer) {
529 // Create the transform layer.
530 m_transformLayer = LayerChromium::create(this);
532 // Copy the position from this layer.
533 updateLayerPosition();
537 updateChildrenTransform();
539 m_layer->setPosition(FloatPoint(m_size.width() / 2.0f, m_size.height() / 2.0f));
541 m_layer->setAnchorPoint(FloatPoint(0.5f, 0.5f));
542 TransformationMatrix identity;
543 m_layer->setTransform(identity);
545 // Set the old layer to opacity of 1. Further down we will set the opacity on the transform layer.
546 m_layer->setOpacity(1);
548 // Move this layer to be a child of the transform layer.
549 if (m_layer->superlayer())
550 m_layer->superlayer()->replaceSublayer(m_layer.get(), m_transformLayer.get());
551 m_transformLayer->addSublayer(m_layer.get());
553 updateSublayerList();
554 } else if (!m_preserves3D && m_transformLayer) {
555 // Relace the transformLayer in the parent with this layer.
556 m_layer->removeFromSuperlayer();
557 m_transformLayer->superlayer()->replaceSublayer(m_transformLayer.get(), m_layer.get());
559 // Release the transform layer.
560 m_transformLayer = 0;
562 updateLayerPosition();
566 updateChildrenTransform();
568 updateSublayerList();
571 updateOpacityOnLayer();
574 void GraphicsLayerChromium::updateLayerDrawsContent()
577 m_layer->setNeedsDisplay();
579 updateDebugIndicators();
582 void GraphicsLayerChromium::updateLayerBackgroundColor()
584 if (!m_contentsLayer)
587 // We never create the contents layer just for background color yet.
588 if (m_backgroundColorSet)
589 setLayerBackgroundColor(*m_contentsLayer, m_backgroundColor);
591 clearLayerBackgroundColor(*m_contentsLayer);
594 void GraphicsLayerChromium::updateContentsVideo()
599 void GraphicsLayerChromium::updateContentsRect()
601 if (!m_contentsLayer)
604 m_contentsLayer->setPosition(FloatPoint(m_contentsRect.x(), m_contentsRect.y()));
605 m_contentsLayer->setBounds(IntSize(m_contentsRect.width(), m_contentsRect.height()));
608 void GraphicsLayerChromium::setupContentsLayer(LayerChromium* contentsLayer)
610 if (contentsLayer == m_contentsLayer)
613 if (m_contentsLayer) {
614 m_contentsLayer->removeFromSuperlayer();
619 m_contentsLayer = contentsLayer;
621 m_contentsLayer->setAnchorPoint(FloatPoint(0, 0));
623 // Insert the content layer first. Video elements require this, because they have
624 // shadow content that must display in front of the video.
625 m_layer->insertSublayer(m_contentsLayer.get(), 0);
627 updateContentsRect();
629 if (showDebugBorders()) {
630 setLayerBorderColor(*m_contentsLayer, Color(0, 0, 128, 180));
631 m_contentsLayer->setBorderWidth(1);
634 updateDebugIndicators();
637 // This function simply mimics the operation of GraphicsLayerCA
638 void GraphicsLayerChromium::updateOpacityOnLayer()
640 primaryLayer()->setOpacity(m_opacity);
643 } // namespace WebCore
645 #endif // USE(ACCELERATED_COMPOSITING)