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 #if USE(ACCELERATED_COMPOSITING)
30 #include "GraphicsLayer.h"
32 #include "FloatPoint.h"
33 #include "GraphicsContext.h"
34 #include "LayoutTypes.h"
35 #include "RotateTransformOperation.h"
36 #include "TextStream.h"
37 #include <wtf/text/CString.h>
38 #include <wtf/text/WTFString.h>
46 void KeyframeValueList::insert(const AnimationValue* value)
48 for (size_t i = 0; i < m_values.size(); ++i) {
49 const AnimationValue* curValue = m_values[i];
50 if (curValue->keyTime() == value->keyTime()) {
53 m_values.insert(i + 1, value);
56 if (curValue->keyTime() > value->keyTime()) {
58 m_values.insert(i, value);
63 m_values.append(value);
66 GraphicsLayer::GraphicsLayer(GraphicsLayerClient* client)
68 , m_anchorPoint(0.5f, 0.5f, 0)
71 , m_backgroundColorSet(false)
72 , m_contentsOpaque(false)
73 , m_preserves3D(false)
74 , m_backfaceVisibility(true)
75 , m_usingTiledLayer(false)
76 , m_masksToBounds(false)
77 , m_drawsContent(false)
78 , m_contentsVisible(true)
79 , m_acceleratesDrawing(false)
80 , m_maintainsPixelAlignment(false)
81 , m_appliesPageScale(false)
82 , m_usingTileCache(false)
83 , m_paintingPhase(GraphicsLayerPaintAll)
84 , m_contentsOrientation(CompositingCoordinatesTopDown)
88 , m_replicatedLayer(0)
90 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
91 , m_isScrollbar(false)
96 m_client->verifyNotPainting();
100 GraphicsLayer::~GraphicsLayer()
102 ASSERT(!m_parent); // willBeDestroyed should have been called already.
105 void GraphicsLayer::willBeDestroyed()
109 m_client->verifyNotPainting();
113 m_replicaLayer->setReplicatedLayer(0);
115 if (m_replicatedLayer)
116 m_replicatedLayer->setReplicatedByLayer(0);
122 void GraphicsLayer::setParent(GraphicsLayer* layer)
124 ASSERT(!layer || !layer->hasAncestor(this));
128 bool GraphicsLayer::hasAncestor(GraphicsLayer* ancestor) const
130 for (GraphicsLayer* curr = parent(); curr; curr = curr->parent()) {
131 if (curr == ancestor)
138 bool GraphicsLayer::setChildren(const Vector<GraphicsLayer*>& newChildren)
140 // If the contents of the arrays are the same, nothing to do.
141 if (newChildren == m_children)
146 size_t listSize = newChildren.size();
147 for (size_t i = 0; i < listSize; ++i)
148 addChild(newChildren[i]);
153 void GraphicsLayer::addChild(GraphicsLayer* childLayer)
155 ASSERT(childLayer != this);
157 if (childLayer->parent())
158 childLayer->removeFromParent();
160 childLayer->setParent(this);
161 m_children.append(childLayer);
164 void GraphicsLayer::addChildAtIndex(GraphicsLayer* childLayer, int index)
166 ASSERT(childLayer != this);
168 if (childLayer->parent())
169 childLayer->removeFromParent();
171 childLayer->setParent(this);
172 m_children.insert(index, childLayer);
175 void GraphicsLayer::addChildBelow(GraphicsLayer* childLayer, GraphicsLayer* sibling)
177 ASSERT(childLayer != this);
178 childLayer->removeFromParent();
181 for (unsigned i = 0; i < m_children.size(); i++) {
182 if (sibling == m_children[i]) {
183 m_children.insert(i, childLayer);
189 childLayer->setParent(this);
192 m_children.append(childLayer);
195 void GraphicsLayer::addChildAbove(GraphicsLayer* childLayer, GraphicsLayer* sibling)
197 childLayer->removeFromParent();
198 ASSERT(childLayer != this);
201 for (unsigned i = 0; i < m_children.size(); i++) {
202 if (sibling == m_children[i]) {
203 m_children.insert(i+1, childLayer);
209 childLayer->setParent(this);
212 m_children.append(childLayer);
215 bool GraphicsLayer::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
217 ASSERT(!newChild->parent());
219 for (unsigned i = 0; i < m_children.size(); i++) {
220 if (oldChild == m_children[i]) {
221 m_children[i] = newChild;
227 oldChild->setParent(0);
229 newChild->removeFromParent();
230 newChild->setParent(this);
236 void GraphicsLayer::removeAllChildren()
238 while (m_children.size()) {
239 GraphicsLayer* curLayer = m_children[0];
240 ASSERT(curLayer->parent());
241 curLayer->removeFromParent();
245 void GraphicsLayer::removeFromParent()
249 for (i = 0; i < m_parent->m_children.size(); i++) {
250 if (this == m_parent->m_children[i]) {
251 m_parent->m_children.remove(i);
260 void GraphicsLayer::noteDeviceOrPageScaleFactorChangedIncludingDescendants()
262 deviceOrPageScaleFactorChanged();
265 m_maskLayer->deviceOrPageScaleFactorChanged();
268 m_replicaLayer->noteDeviceOrPageScaleFactorChangedIncludingDescendants();
270 const Vector<GraphicsLayer*>& childLayers = children();
271 size_t numChildren = childLayers.size();
272 for (size_t i = 0; i < numChildren; ++i)
273 childLayers[i]->noteDeviceOrPageScaleFactorChangedIncludingDescendants();
276 void GraphicsLayer::setReplicatedByLayer(GraphicsLayer* layer)
278 if (m_replicaLayer == layer)
282 m_replicaLayer->setReplicatedLayer(0);
285 layer->setReplicatedLayer(this);
287 m_replicaLayer = layer;
290 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
291 void GraphicsLayer::setOffsetFromRenderer(const IntSize& offset, const bool needToDisplay)
293 void GraphicsLayer::setOffsetFromRenderer(const IntSize& offset)
296 if (offset == m_offsetFromRenderer)
299 m_offsetFromRenderer = offset;
301 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
304 // If the compositing layer offset changes, we need to repaint.
308 void GraphicsLayer::setBackgroundColor(const Color& color)
310 m_backgroundColor = color;
311 m_backgroundColorSet = true;
314 void GraphicsLayer::clearBackgroundColor()
316 m_backgroundColor = Color();
317 m_backgroundColorSet = false;
320 void GraphicsLayer::paintGraphicsLayerContents(GraphicsContext& context, const IntRect& clip)
323 LayoutSize offset = offsetFromRenderer();
324 context.translate(-offset);
326 LayoutRect clipRect(clip);
327 clipRect.move(offset);
329 m_client->paintContents(this, context, m_paintingPhase, pixelSnappedIntRect(clipRect));
333 String GraphicsLayer::animationNameForTransition(AnimatedPropertyID property)
335 // | is not a valid identifier character in CSS, so this can never conflict with a keyframe identifier.
336 String id = "-|transition";
337 id.append(static_cast<char>(property));
342 void GraphicsLayer::suspendAnimations(double)
346 void GraphicsLayer::resumeAnimations()
350 void GraphicsLayer::updateDebugIndicators()
352 if (GraphicsLayer::showDebugBorders()) {
353 if (drawsContent()) {
354 if (m_usingTileCache) // tile cache layer: dark blue
355 setDebugBorder(Color(0, 0, 128, 128), 0.5);
356 else if (m_usingTiledLayer)
357 setDebugBorder(Color(255, 128, 0, 128), 2); // tiled layer: orange
359 setDebugBorder(Color(0, 128, 32, 128), 2); // normal layer: green
360 } else if (masksToBounds()) {
361 setDebugBorder(Color(128, 255, 255, 48), 20); // masking layer: pale blue
363 setDebugBorder(Color(255, 255, 0, 192), 2); // container: yellow
367 void GraphicsLayer::setZPosition(float position)
369 m_zPosition = position;
372 float GraphicsLayer::accumulatedOpacity() const
377 return m_opacity * (parent() ? parent()->accumulatedOpacity() : 1);
380 void GraphicsLayer::distributeOpacity(float accumulatedOpacity)
382 // If this is a transform layer we need to distribute our opacity to all our children
384 // Incoming accumulatedOpacity is the contribution from our parent(s). We mutiply this by our own
385 // opacity to get the total contribution
386 accumulatedOpacity *= m_opacity;
388 setOpacityInternal(accumulatedOpacity);
391 size_t numChildren = children().size();
392 for (size_t i = 0; i < numChildren; ++i)
393 children()[i]->distributeOpacity(accumulatedOpacity);
397 #if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
398 GraphicsLayer::GraphicsLayerFactory* GraphicsLayer::s_graphicsLayerFactory = 0;
400 void GraphicsLayer::setGraphicsLayerFactory(GraphicsLayer::GraphicsLayerFactory factory)
402 s_graphicsLayerFactory = factory;
406 #if ENABLE(CSS_FILTERS)
407 static inline const FilterOperations* filterOperationsAt(const KeyframeValueList& valueList, size_t index)
409 return static_cast<const FilterAnimationValue*>(valueList.at(index))->value();
412 int GraphicsLayer::validateFilterOperations(const KeyframeValueList& valueList)
414 ASSERT(valueList.property() == AnimatedPropertyWebkitFilter);
416 if (valueList.size() < 2)
419 // Empty filters match anything, so find the first non-empty entry as the reference
420 size_t firstIndex = 0;
421 for ( ; firstIndex < valueList.size(); ++firstIndex) {
422 if (filterOperationsAt(valueList, firstIndex)->operations().size() > 0)
426 if (firstIndex >= valueList.size())
429 const FilterOperations* firstVal = filterOperationsAt(valueList, firstIndex);
431 for (size_t i = firstIndex + 1; i < valueList.size(); ++i) {
432 const FilterOperations* val = filterOperationsAt(valueList, i);
434 // An emtpy filter list matches anything.
435 if (val->operations().isEmpty())
438 if (!firstVal->operationsMatch(*val))
446 // An "invalid" list is one whose functions don't match, and therefore has to be animated as a Matrix
447 // The hasBigRotation flag will always return false if isValid is false. Otherwise hasBigRotation is
448 // true if the rotation between any two keyframes is >= 180 degrees.
450 static inline const TransformOperations* operationsAt(const KeyframeValueList& valueList, size_t index)
452 return static_cast<const TransformAnimationValue*>(valueList.at(index))->value();
455 int GraphicsLayer::validateTransformOperations(const KeyframeValueList& valueList, bool& hasBigRotation)
457 ASSERT(valueList.property() == AnimatedPropertyWebkitTransform);
459 hasBigRotation = false;
461 if (valueList.size() < 2)
464 // Empty transforms match anything, so find the first non-empty entry as the reference.
465 size_t firstIndex = 0;
466 for ( ; firstIndex < valueList.size(); ++firstIndex) {
467 if (operationsAt(valueList, firstIndex)->operations().size() > 0)
471 if (firstIndex >= valueList.size())
474 const TransformOperations* firstVal = operationsAt(valueList, firstIndex);
476 // See if the keyframes are valid.
477 for (size_t i = firstIndex + 1; i < valueList.size(); ++i) {
478 const TransformOperations* val = operationsAt(valueList, i);
480 // An emtpy transform list matches anything.
481 if (val->operations().isEmpty())
484 if (!firstVal->operationsMatch(*val))
488 // Keyframes are valid, check for big rotations.
489 double lastRotAngle = 0.0;
490 double maxRotAngle = -1.0;
492 for (size_t j = 0; j < firstVal->operations().size(); ++j) {
493 TransformOperation::OperationType type = firstVal->operations().at(j)->getOperationType();
495 // if this is a rotation entry, we need to see if any angle differences are >= 180 deg
496 if (type == TransformOperation::ROTATE_X ||
497 type == TransformOperation::ROTATE_Y ||
498 type == TransformOperation::ROTATE_Z ||
499 type == TransformOperation::ROTATE_3D) {
500 lastRotAngle = static_cast<RotateTransformOperation*>(firstVal->operations().at(j).get())->angle();
503 maxRotAngle = fabs(lastRotAngle);
505 for (size_t i = firstIndex + 1; i < valueList.size(); ++i) {
506 const TransformOperations* val = operationsAt(valueList, i);
507 double rotAngle = val->operations().isEmpty() ? 0 : (static_cast<RotateTransformOperation*>(val->operations().at(j).get())->angle());
508 double diffAngle = fabs(rotAngle - lastRotAngle);
509 if (diffAngle > maxRotAngle)
510 maxRotAngle = diffAngle;
511 lastRotAngle = rotAngle;
516 hasBigRotation = maxRotAngle >= 180.0;
521 double GraphicsLayer::backingStoreMemoryEstimate() const
526 // Effects of page and device scale are ignored; subclasses should override to take these into account.
527 return static_cast<double>(4 * size().width()) * size().height();
530 static void writeIndent(TextStream& ts, int indent)
532 for (int i = 0; i != indent; ++i)
536 void GraphicsLayer::dumpLayer(TextStream& ts, int indent, LayerTreeAsTextBehavior behavior) const
538 writeIndent(ts, indent);
539 ts << "(" << "GraphicsLayer";
541 if (behavior & LayerTreeAsTextDebug) {
542 ts << " " << static_cast<void*>(const_cast<GraphicsLayer*>(this));
543 ts << " \"" << m_name << "\"";
547 dumpProperties(ts, indent, behavior);
548 writeIndent(ts, indent);
552 void GraphicsLayer::dumpProperties(TextStream& ts, int indent, LayerTreeAsTextBehavior behavior) const
554 if (m_position != FloatPoint()) {
555 writeIndent(ts, indent + 1);
556 ts << "(position " << m_position.x() << " " << m_position.y() << ")\n";
559 if (m_boundsOrigin != FloatPoint()) {
560 writeIndent(ts, indent + 1);
561 ts << "(bounds origin " << m_boundsOrigin.x() << " " << m_boundsOrigin.y() << ")\n";
564 if (m_anchorPoint != FloatPoint3D(0.5f, 0.5f, 0)) {
565 writeIndent(ts, indent + 1);
566 ts << "(anchor " << m_anchorPoint.x() << " " << m_anchorPoint.y() << ")\n";
569 if (m_size != IntSize()) {
570 writeIndent(ts, indent + 1);
571 ts << "(bounds " << m_size.width() << " " << m_size.height() << ")\n";
574 if (m_opacity != 1) {
575 writeIndent(ts, indent + 1);
576 ts << "(opacity " << m_opacity << ")\n";
579 if (m_usingTiledLayer) {
580 writeIndent(ts, indent + 1);
581 ts << "(usingTiledLayer " << m_usingTiledLayer << ")\n";
585 writeIndent(ts, indent + 1);
586 ts << "(preserves3D " << m_preserves3D << ")\n";
589 if (m_drawsContent) {
590 writeIndent(ts, indent + 1);
591 ts << "(drawsContent " << m_drawsContent << ")\n";
594 if (!m_contentsVisible) {
595 writeIndent(ts, indent + 1);
596 ts << "(contentsVisible " << m_contentsVisible << ")\n";
599 if (!m_backfaceVisibility) {
600 writeIndent(ts, indent + 1);
601 ts << "(backfaceVisibility " << (m_backfaceVisibility ? "visible" : "hidden") << ")\n";
604 if (behavior & LayerTreeAsTextDebug) {
605 writeIndent(ts, indent + 1);
608 ts << "client " << static_cast<void*>(m_client);
614 if (m_backgroundColorSet) {
615 writeIndent(ts, indent + 1);
616 ts << "(backgroundColor " << m_backgroundColor.nameForRenderTreeAsText() << ")\n";
619 if (!m_transform.isIdentity()) {
620 writeIndent(ts, indent + 1);
622 ts << "[" << m_transform.m11() << " " << m_transform.m12() << " " << m_transform.m13() << " " << m_transform.m14() << "] ";
623 ts << "[" << m_transform.m21() << " " << m_transform.m22() << " " << m_transform.m23() << " " << m_transform.m24() << "] ";
624 ts << "[" << m_transform.m31() << " " << m_transform.m32() << " " << m_transform.m33() << " " << m_transform.m34() << "] ";
625 ts << "[" << m_transform.m41() << " " << m_transform.m42() << " " << m_transform.m43() << " " << m_transform.m44() << "])\n";
628 // Avoid dumping the sublayer transform on the root layer, because it's used for geometry flipping, whose behavior
629 // differs between platforms.
630 if (parent() && !m_childrenTransform.isIdentity()) {
631 writeIndent(ts, indent + 1);
632 ts << "(childrenTransform ";
633 ts << "[" << m_childrenTransform.m11() << " " << m_childrenTransform.m12() << " " << m_childrenTransform.m13() << " " << m_childrenTransform.m14() << "] ";
634 ts << "[" << m_childrenTransform.m21() << " " << m_childrenTransform.m22() << " " << m_childrenTransform.m23() << " " << m_childrenTransform.m24() << "] ";
635 ts << "[" << m_childrenTransform.m31() << " " << m_childrenTransform.m32() << " " << m_childrenTransform.m33() << " " << m_childrenTransform.m34() << "] ";
636 ts << "[" << m_childrenTransform.m41() << " " << m_childrenTransform.m42() << " " << m_childrenTransform.m43() << " " << m_childrenTransform.m44() << "])\n";
639 if (m_replicaLayer) {
640 writeIndent(ts, indent + 1);
641 ts << "(replica layer";
642 if (behavior & LayerTreeAsTextDebug)
643 ts << " " << m_replicaLayer;
645 m_replicaLayer->dumpLayer(ts, indent + 2, behavior);
648 if (m_replicatedLayer) {
649 writeIndent(ts, indent + 1);
650 ts << "(replicated layer";
651 if (behavior & LayerTreeAsTextDebug)
652 ts << " " << m_replicatedLayer;;
656 if (m_children.size()) {
657 writeIndent(ts, indent + 1);
658 ts << "(children " << m_children.size() << "\n";
661 for (i = 0; i < m_children.size(); i++)
662 m_children[i]->dumpLayer(ts, indent + 2, behavior);
663 writeIndent(ts, indent + 1);
668 String GraphicsLayer::layerTreeAsText(LayerTreeAsTextBehavior behavior) const
672 dumpLayer(ts, 0, behavior);
676 } // namespace WebCore
679 void showGraphicsLayerTree(const WebCore::GraphicsLayer* layer)
684 WTF::String output = layer->layerTreeAsText(LayerTreeAsTextDebug);
685 fprintf(stderr, "%s\n", output.utf8().data());
689 #endif // USE(ACCELERATED_COMPOSITING)