GraphicsLayerQt.cpp [plain text]
#include "config.h"
#include "GraphicsLayerQt.h"
#include "CurrentTime.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "Image.h"
#include "RefCounted.h"
#include "TranslateTransformOperation.h"
#include "UnitBezier.h"
#include <QtCore/qabstractanimation.h>
#include <QtCore/qdebug.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qset.h>
#include <QtCore/qtimer.h>
#include <QtGui/qcolor.h>
#include <QtGui/qgraphicseffect.h>
#include <QtGui/qgraphicsitem.h>
#include <QtGui/qgraphicsscene.h>
#include <QtGui/qpainter.h>
#include <QtGui/qpixmap.h>
#include <QtGui/qpixmapcache.h>
#include <QtGui/qstyleoption.h>
namespace WebCore {
class MaskEffectQt : public QGraphicsEffect {
public:
MaskEffectQt(QObject* parent, QGraphicsItem* maskLayer)
: QGraphicsEffect(parent)
, m_maskLayer(maskLayer)
{
}
void draw(QPainter* painter)
{
QPixmap maskPixmap(sourceBoundingRect().toAlignedRect().size());
maskPixmap.fill(Qt::transparent);
QPainter maskPainter(&maskPixmap);
QStyleOptionGraphicsItem option;
option.exposedRect = option.rect = maskPixmap.rect();
maskPainter.setRenderHints(painter->renderHints(), true);
m_maskLayer->paint(&maskPainter, &option, 0);
maskPainter.end();
QPoint offset;
QPixmap srcPixmap = sourcePixmap(Qt::LogicalCoordinates, &offset, QGraphicsEffect::NoPad);
QPixmap pixmap(srcPixmap.size());
pixmap.fill(Qt::transparent);
if (pixmap.isNull())
return;
QPainter pixmapPainter(&pixmap);
pixmapPainter.setRenderHints(painter->renderHints());
pixmapPainter.setCompositionMode(QPainter::CompositionMode_Source);
pixmapPainter.drawPixmap(0, 0, srcPixmap);
pixmapPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
pixmapPainter.drawPixmap(0, 0, maskPixmap);
pixmapPainter.end();
painter->drawPixmap(offset, pixmap);
}
QGraphicsItem* m_maskLayer;
};
class GraphicsLayerQtImpl : public QGraphicsObject {
Q_OBJECT
public:
enum ChangeMask {
NoChanges = 0,
ParentChange = (1L << 0),
ChildrenChange = (1L << 1),
MaskLayerChange = (1L << 2),
PositionChange = (1L << 3),
AnchorPointChange = (1L << 4),
SizeChange = (1L << 5),
TransformChange = (1L << 6),
ContentChange = (1L << 7),
GeometryOrientationChange = (1L << 8),
ContentsOrientationChange = (1L << 9),
OpacityChange = (1L << 10),
ContentsRectChange = (1L << 11),
Preserves3DChange = (1L << 12),
MasksToBoundsChange = (1L << 13),
DrawsContentChange = (1L << 14),
ContentsOpaqueChange = (1L << 15),
BackfaceVisibilityChange = (1L << 16),
ChildrenTransformChange = (1L << 17),
DisplayChange = (1L << 18),
BackgroundColorChange = (1L << 19),
DistributesOpacityChange = (1L << 20)
};
enum StaticContentType { HTMLContentType, PixmapContentType, ColorContentType, MediaContentType, Canvas3DContentType};
const GraphicsLayerQtImpl* rootLayer() const;
GraphicsLayerQtImpl(GraphicsLayerQt* newLayer);
virtual ~GraphicsLayerQtImpl();
virtual QPainterPath opaqueArea() const;
virtual QRectF boundingRect() const;
virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);
void setBaseTransform(const TransformationMatrix&);
void updateTransform();
void notifyChange(ChangeMask);
QPixmap recache(const QRegion&);
void flushChanges(bool recursive = true, bool forceTransformUpdate = false);
public slots:
void notifyAnimationStarted();
void notifySyncRequired();
signals:
void notifyAnimationStartedAsync();
public:
GraphicsLayerQt* m_layer;
TransformationMatrix m_baseTransform;
TransformationMatrix m_transformRelativeToRootLayer;
bool m_transformAnimationRunning;
bool m_opacityAnimationRunning;
QWeakPointer<MaskEffectQt> m_maskEffect;
struct ContentData {
QPixmap pixmap;
QRegion regionToUpdate;
bool updateAll;
QColor contentsBackgroundColor;
QColor backgroundColor;
QWeakPointer<QGraphicsObject> mediaLayer;
StaticContentType contentType;
float opacity;
ContentData()
: updateAll(false)
, contentType(HTMLContentType)
, opacity(1.f)
{
}
};
ContentData m_pendingContent;
ContentData m_currentContent;
int m_changeMask;
QSizeF m_size;
QPixmapCache::Key m_backingStoreKey;
#ifndef QT_NO_ANIMATION
QList<QWeakPointer<QAbstractAnimation> > m_animations;
#endif
QTimer m_suspendTimer;
struct State {
GraphicsLayer* maskLayer;
FloatPoint pos;
FloatPoint3D anchorPoint;
FloatSize size;
TransformationMatrix transform;
TransformationMatrix childrenTransform;
Color backgroundColor;
Color currentColor;
GraphicsLayer::CompositingCoordinatesOrientation geoOrientation;
GraphicsLayer::CompositingCoordinatesOrientation contentsOrientation;
float opacity;
QRect contentsRect;
bool preserves3D: 1;
bool masksToBounds: 1;
bool drawsContent: 1;
bool contentsOpaque: 1;
bool backfaceVisibility: 1;
bool distributeOpacity: 1;
bool align: 2;
State(): maskLayer(0), opacity(1.f), preserves3D(false), masksToBounds(false),
drawsContent(false), contentsOpaque(false), backfaceVisibility(false),
distributeOpacity(false)
{
}
} m_state;
#if ENABLE(3D_CANVAS)
const GraphicsContext3D* m_gc3D;
#endif
#ifndef QT_NO_ANIMATION
friend class AnimationQtBase;
#endif
};
GraphicsLayerQtImpl::GraphicsLayerQtImpl(GraphicsLayerQt* newLayer)
: QGraphicsObject(0)
, m_layer(newLayer)
, m_transformAnimationRunning(false)
, m_opacityAnimationRunning(false)
, m_changeMask(NoChanges)
#if ENABLE(3D_CANVAS)
, m_gc3D(0)
#endif
{
setAcceptedMouseButtons(Qt::NoButton);
setEnabled(true);
connect(this, SIGNAL(notifyAnimationStartedAsync()), this, SLOT(notifyAnimationStarted()), Qt::QueuedConnection);
}
GraphicsLayerQtImpl::~GraphicsLayerQtImpl()
{
const QList<QGraphicsItem*> children = childItems();
for (QList<QGraphicsItem*>::const_iterator it = children.begin(); it != children.end(); ++it) {
if (QGraphicsItem* item = *it) {
if (scene())
scene()->removeItem(item);
item->setParentItem(0);
}
}
#ifndef QT_NO_ANIMATION
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_animations.begin(); it != m_animations.end(); ++it)
if (QAbstractAnimation* anim = it->data())
delete anim;
#endif
}
const GraphicsLayerQtImpl* GraphicsLayerQtImpl::rootLayer() const
{
if (const GraphicsLayerQtImpl* parent = qobject_cast<const GraphicsLayerQtImpl*>(parentObject()))
return parent->rootLayer();
return this;
}
QPixmap GraphicsLayerQtImpl::recache(const QRegion& regionToUpdate)
{
if (!m_layer->drawsContent())
return QPixmap();
QRegion region = regionToUpdate;
QPixmap pixmap;
if (!QPixmapCache::find(m_backingStoreKey, &pixmap))
region = QRegion(QRect(0, 0, m_size.width(), m_size.height()));
if (m_size != pixmap.size()) {
pixmap = QPixmap(m_size.toSize());
if (!m_layer->contentsOpaque())
pixmap.fill(Qt::transparent);
m_pendingContent.regionToUpdate = QRegion(QRect(QPoint(0, 0), m_size.toSize()));
}
QPainter painter(&pixmap);
GraphicsContext gc(&painter);
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.fillRect(region.boundingRect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
m_layer->paintGraphicsLayerContents(gc, region.boundingRect());
m_backingStoreKey = QPixmapCache::insert(pixmap);
return pixmap;
}
void GraphicsLayerQtImpl::updateTransform()
{
if (!m_transformAnimationRunning)
m_baseTransform = m_layer->transform();
TransformationMatrix localTransform;
GraphicsLayerQtImpl* parent = qobject_cast<GraphicsLayerQtImpl*>(parentObject());
const qreal originX = m_state.anchorPoint.x() * m_size.width();
const qreal originY = m_state.anchorPoint.y() * m_size.height();
localTransform
.translate3d(originX + m_state.pos.x(), originY + m_state.pos.y(), m_state.anchorPoint.z())
.multLeft(m_baseTransform)
.translate3d(-originX, -originY, -m_state.anchorPoint.z());
m_transformRelativeToRootLayer = TransformationMatrix(parent ? parent->m_transformRelativeToRootLayer : TransformationMatrix())
.multLeft(localTransform);
if (!m_state.backfaceVisibility && m_transformRelativeToRootLayer.inverse().m33() < 0) {
setVisible(false);
return;
}
if (parent) {
const QPointF centerPointMappedToRoot = rootLayer()->mapFromItem(this, m_size.width() / 2, m_size.height() / 2);
setFlag(ItemStacksBehindParent,
m_transformRelativeToRootLayer.mapPoint(FloatPoint3D(centerPointMappedToRoot.x(), centerPointMappedToRoot.y(), 0)).z() <
parent->m_transformRelativeToRootLayer.mapPoint(FloatPoint3D(centerPointMappedToRoot.x(), centerPointMappedToRoot.y(), 0)).z());
}
setVisible(true);
if (!m_state.preserves3D) {
m_transformRelativeToRootLayer.setM13(0);
m_transformRelativeToRootLayer.setM23(0);
m_transformRelativeToRootLayer.setM31(0);
m_transformRelativeToRootLayer.setM32(0);
m_transformRelativeToRootLayer.setM33(1);
m_transformRelativeToRootLayer.setM34(0);
m_transformRelativeToRootLayer.setM43(0);
}
if (!m_state.childrenTransform.isIdentity())
m_transformRelativeToRootLayer
.translate(m_size.width() / 2, m_size.height() /2)
.multLeft(m_state.childrenTransform)
.translate(-m_size.width() / 2, -m_size.height() /2);
bool inverseOk = true;
const QTransform parentTransform = parent ? parent->itemTransform(rootLayer()) : QTransform();
const QTransform transform2D = QTransform(m_transformRelativeToRootLayer) * parentTransform.inverted(&inverseOk);
if (!inverseOk)
return;
setTransform(transform2D);
const QList<QGraphicsItem*> children = childItems();
for (QList<QGraphicsItem*>::const_iterator it = children.begin(); it != children.end(); ++it)
if (GraphicsLayerQtImpl* layer= qobject_cast<GraphicsLayerQtImpl*>((*it)->toGraphicsObject()))
layer->updateTransform();
}
void GraphicsLayerQtImpl::setBaseTransform(const TransformationMatrix& baseTransform)
{
m_baseTransform = baseTransform;
updateTransform();
}
QPainterPath GraphicsLayerQtImpl::opaqueArea() const
{
QPainterPath painterPath;
if (m_currentContent.backgroundColor.isValid() && m_currentContent.backgroundColor.alpha() == 0xff)
painterPath.addRect(boundingRect());
else {
if (m_state.contentsOpaque
|| (m_currentContent.contentType == ColorContentType && m_currentContent.contentsBackgroundColor.alpha() == 0xff)
|| (m_currentContent.contentType == MediaContentType)
|| (m_currentContent.contentType == PixmapContentType && !m_currentContent.pixmap.hasAlpha())) {
painterPath.addRect(m_state.contentsRect);
}
}
return painterPath;
}
QRectF GraphicsLayerQtImpl::boundingRect() const
{
return QRectF(QPointF(0, 0), QSizeF(m_size));
}
void GraphicsLayerQtImpl::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
if (m_currentContent.backgroundColor.isValid())
painter->fillRect(option->rect, QColor(m_currentContent.backgroundColor));
switch (m_currentContent.contentType) {
case HTMLContentType:
if (m_state.drawsContent) {
QPixmap backingStore;
if (!QPixmapCache::find(m_backingStoreKey, &backingStore) || backingStore.size() != m_size.toSize())
backingStore = recache(QRegion(m_state.contentsRect));
painter->drawPixmap(0, 0, backingStore);
}
break;
case PixmapContentType:
painter->drawPixmap(m_state.contentsRect, m_currentContent.pixmap);
break;
case ColorContentType:
painter->fillRect(m_state.contentsRect, m_currentContent.contentsBackgroundColor);
break;
case MediaContentType:
break;
#if ENABLE(3D_CANVAS)
case Canvas3DContentType:
m_gc3D->paint(painter, option->rect);
break;
#endif
}
}
void GraphicsLayerQtImpl::notifySyncRequired()
{
if (m_layer->client())
m_layer->client()->notifySyncRequired(m_layer);
}
void GraphicsLayerQtImpl::notifyChange(ChangeMask changeMask)
{
m_changeMask |= changeMask;
static QMetaMethod syncMethod = staticMetaObject.method(staticMetaObject.indexOfMethod("notifySyncRequired()"));
syncMethod.invoke(this, Qt::QueuedConnection);
}
void GraphicsLayerQtImpl::flushChanges(bool recursive, bool forceUpdateTransform)
{
if (!m_layer || m_changeMask == NoChanges)
goto afterLayerChanges;
if (m_currentContent.contentType == HTMLContentType && (m_changeMask & ParentChange)) {
if (!m_layer->parent() && !parentItem())
setParentItem(0);
else if (m_layer && m_layer->parent() && m_layer->parent()->nativeLayer() != parentItem())
setParentItem(m_layer->parent()->nativeLayer());
}
if (m_changeMask & ChildrenChange) {
QSet<QGraphicsItem*> newChildren;
const Vector<GraphicsLayer*> newChildrenVector = (m_layer->children());
newChildren.reserve(newChildrenVector.size());
for (size_t i = 0; i < newChildrenVector.size(); ++i)
newChildren.insert(newChildrenVector[i]->platformLayer());
const QSet<QGraphicsItem*> currentChildren = childItems().toSet();
const QSet<QGraphicsItem*> childrenToAdd = newChildren - currentChildren;
const QSet<QGraphicsItem*> childrenToRemove = currentChildren - newChildren;
for (QSet<QGraphicsItem*>::const_iterator it = childrenToAdd.begin(); it != childrenToAdd.end(); ++it)
if (QGraphicsItem* w = *it)
w->setParentItem(this);
for (QSet<QGraphicsItem*>::const_iterator it = childrenToRemove.begin(); it != childrenToRemove.end(); ++it)
if (GraphicsLayerQtImpl* w = qobject_cast<GraphicsLayerQtImpl*>((*it)->toGraphicsObject()))
w->setParentItem(0);
for (size_t i = 0; i < newChildrenVector.size(); ++i)
if (newChildrenVector[i]->platformLayer())
newChildrenVector[i]->platformLayer()->setZValue(i);
}
if (m_changeMask & MaskLayerChange) {
setFlag(ItemClipsChildrenToShape, m_layer->maskLayer() || m_layer->masksToBounds());
setGraphicsEffect(0);
if (m_layer->maskLayer()) {
if (GraphicsLayerQtImpl* mask = qobject_cast<GraphicsLayerQtImpl*>(m_layer->maskLayer()->platformLayer()->toGraphicsObject())) {
mask->m_maskEffect = new MaskEffectQt(this, mask);
setGraphicsEffect(mask->m_maskEffect.data());
}
}
}
if (m_changeMask & SizeChange) {
if (m_layer->size() != m_state.size) {
prepareGeometryChange();
m_size = QSizeF(m_layer->size().width(), m_layer->size().height());
}
}
if ((m_changeMask & ChildrenTransformChange) && m_state.childrenTransform != m_layer->childrenTransform())
if (scene())
scene()->update();
if (m_changeMask & (ChildrenTransformChange | Preserves3DChange | TransformChange | AnchorPointChange | SizeChange | BackfaceVisibilityChange | PositionChange)) {
forceUpdateTransform = true;
}
if (m_changeMask & (ContentChange | DrawsContentChange | MaskLayerChange)) {
switch (m_pendingContent.contentType) {
case PixmapContentType:
update();
setFlag(ItemHasNoContents, false);
break;
case MediaContentType:
setFlag(ItemHasNoContents, true);
m_pendingContent.mediaLayer.data()->setParentItem(this);
break;
case ColorContentType:
if (m_pendingContent.contentType != m_currentContent.contentType || m_pendingContent.contentsBackgroundColor != m_currentContent.contentsBackgroundColor)
update();
m_state.drawsContent = false;
setFlag(ItemHasNoContents, false);
setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, false);
break;
case HTMLContentType:
if (m_pendingContent.contentType != m_currentContent.contentType)
update();
if (!m_state.drawsContent && m_layer->drawsContent())
update();
setFlag(ItemHasNoContents, !m_layer->drawsContent());
break;
#if ENABLE(3D_CANVAS)
case Canvas3DContentType:
if (m_pendingContent.contentType != m_currentContent.contentType)
update();
setCacheMode(NoCache);
setFlag(ItemHasNoContents, false);
break;
#endif
}
}
if ((m_changeMask & OpacityChange) && m_state.opacity != m_layer->opacity() && !m_opacityAnimationRunning)
setOpacity(m_layer->opacity());
if (m_changeMask & ContentsRectChange) {
const QRect rect(m_layer->contentsRect());
if (m_state.contentsRect != rect) {
m_state.contentsRect = rect;
update();
}
}
if ((m_changeMask & MasksToBoundsChange)
&& m_state.masksToBounds != m_layer->masksToBounds()) {
setFlag(QGraphicsItem::ItemClipsToShape, m_layer->masksToBounds());
setFlag(QGraphicsItem::ItemClipsChildrenToShape, m_layer->masksToBounds());
}
if ((m_changeMask & ContentsOpaqueChange) && m_state.contentsOpaque != m_layer->contentsOpaque())
prepareGeometryChange();
if (m_maskEffect)
m_maskEffect.data()->update();
else if (m_changeMask & DisplayChange) {
recache(m_pendingContent.regionToUpdate);
update(m_pendingContent.regionToUpdate.boundingRect());
m_pendingContent.regionToUpdate = QRegion();
}
if ((m_changeMask & BackgroundColorChange) && (m_pendingContent.backgroundColor != m_currentContent.backgroundColor))
update();
m_state.maskLayer = m_layer->maskLayer();
m_state.pos = m_layer->position();
m_state.anchorPoint = m_layer->anchorPoint();
m_state.size = m_layer->size();
m_state.transform = m_layer->transform();
m_state.geoOrientation = m_layer->geometryOrientation();
m_state.contentsOrientation =m_layer->contentsOrientation();
m_state.opacity = m_layer->opacity();
m_state.contentsRect = m_layer->contentsRect();
m_state.preserves3D = m_layer->preserves3D();
m_state.masksToBounds = m_layer->masksToBounds();
m_state.drawsContent = m_layer->drawsContent();
m_state.contentsOpaque = m_layer->contentsOpaque();
m_state.backfaceVisibility = m_layer->backfaceVisibility();
m_state.childrenTransform = m_layer->childrenTransform();
m_currentContent.pixmap = m_pendingContent.pixmap;
m_currentContent.contentType = m_pendingContent.contentType;
m_currentContent.mediaLayer = m_pendingContent.mediaLayer;
m_currentContent.backgroundColor = m_pendingContent.backgroundColor;
m_currentContent.contentsBackgroundColor = m_pendingContent.contentsBackgroundColor;
m_pendingContent.regionToUpdate = QRegion();
m_changeMask = NoChanges;
afterLayerChanges:
if (forceUpdateTransform)
updateTransform();
if (!recursive)
return;
QList<QGraphicsItem*> children = childItems();
if (m_state.maskLayer)
children.append(m_state.maskLayer->platformLayer());
for (QList<QGraphicsItem*>::const_iterator it = children.begin(); it != children.end(); ++it) {
if (QGraphicsItem* item = *it)
if (GraphicsLayerQtImpl* layer = qobject_cast<GraphicsLayerQtImpl*>(item->toGraphicsObject()))
layer->flushChanges(true, forceUpdateTransform);
}
}
void GraphicsLayerQtImpl::notifyAnimationStarted()
{
m_layer->client()->notifyAnimationStarted(m_layer, WTF::currentTime());
}
GraphicsLayerQt::GraphicsLayerQt(GraphicsLayerClient* client)
: GraphicsLayer(client)
, m_impl(PassOwnPtr<GraphicsLayerQtImpl>(new GraphicsLayerQtImpl(this)))
{
}
GraphicsLayerQt::~GraphicsLayerQt()
{
}
PassOwnPtr<GraphicsLayer> GraphicsLayer::create(GraphicsLayerClient* client)
{
return new GraphicsLayerQt(client);
}
GraphicsLayer::CompositingCoordinatesOrientation GraphicsLayer::compositingCoordinatesOrientation()
{
return CompositingCoordinatesTopDown;
}
void GraphicsLayerQt::setNeedsDisplay()
{
m_impl->m_pendingContent.regionToUpdate = QRegion(QRect(QPoint(0, 0), QSize(size().width(), size().height())));
m_impl->notifyChange(GraphicsLayerQtImpl::DisplayChange);
}
void GraphicsLayerQt::setNeedsDisplayInRect(const FloatRect& rect)
{
m_impl->m_pendingContent.regionToUpdate|= QRectF(rect).toAlignedRect();
m_impl->notifyChange(GraphicsLayerQtImpl::DisplayChange);
}
void GraphicsLayerQt::setName(const String& name)
{
m_impl->setObjectName(name);
GraphicsLayer::setName(name);
}
void GraphicsLayerQt::setParent(GraphicsLayer* layer)
{
m_impl->notifyChange(GraphicsLayerQtImpl::ParentChange);
GraphicsLayer::setParent(layer);
}
bool GraphicsLayerQt::setChildren(const Vector<GraphicsLayer*>& children)
{
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenChange);
return GraphicsLayer::setChildren(children);
}
void GraphicsLayerQt::addChild(GraphicsLayer* layer)
{
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenChange);
GraphicsLayer::addChild(layer);
}
void GraphicsLayerQt::addChildAtIndex(GraphicsLayer* layer, int index)
{
GraphicsLayer::addChildAtIndex(layer, index);
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenChange);
}
void GraphicsLayerQt::addChildAbove(GraphicsLayer* layer, GraphicsLayer* sibling)
{
GraphicsLayer::addChildAbove(layer, sibling);
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenChange);
}
void GraphicsLayerQt::addChildBelow(GraphicsLayer* layer, GraphicsLayer* sibling)
{
GraphicsLayer::addChildBelow(layer, sibling);
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenChange);
}
bool GraphicsLayerQt::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
{
if (GraphicsLayer::replaceChild(oldChild, newChild)) {
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenChange);
return true;
}
return false;
}
void GraphicsLayerQt::removeFromParent()
{
if (parent())
m_impl->notifyChange(GraphicsLayerQtImpl::ParentChange);
GraphicsLayer::removeFromParent();
}
void GraphicsLayerQt::setMaskLayer(GraphicsLayer* value)
{
if (value == maskLayer())
return;
GraphicsLayer::setMaskLayer(value);
m_impl->notifyChange(GraphicsLayerQtImpl::MaskLayerChange);
}
void GraphicsLayerQt::setPosition(const FloatPoint& value)
{
if (value == position())
return;
GraphicsLayer::setPosition(value);
m_impl->notifyChange(GraphicsLayerQtImpl::PositionChange);
}
void GraphicsLayerQt::setAnchorPoint(const FloatPoint3D& value)
{
if (value == anchorPoint())
return;
GraphicsLayer::setAnchorPoint(value);
m_impl->notifyChange(GraphicsLayerQtImpl::AnchorPointChange);
}
void GraphicsLayerQt::setSize(const FloatSize& value)
{
if (value == size())
return;
GraphicsLayer::setSize(value);
m_impl->notifyChange(GraphicsLayerQtImpl::SizeChange);
}
void GraphicsLayerQt::setTransform(const TransformationMatrix& value)
{
if (value == transform())
return;
GraphicsLayer::setTransform(value);
m_impl->notifyChange(GraphicsLayerQtImpl::TransformChange);
}
void GraphicsLayerQt::setChildrenTransform(const TransformationMatrix& value)
{
if (value == childrenTransform())
return;
GraphicsLayer::setChildrenTransform(value);
m_impl->notifyChange(GraphicsLayerQtImpl::ChildrenTransformChange);
}
void GraphicsLayerQt::setPreserves3D(bool value)
{
if (value == preserves3D())
return;
GraphicsLayer::setPreserves3D(value);
m_impl->notifyChange(GraphicsLayerQtImpl::Preserves3DChange);
}
void GraphicsLayerQt::setMasksToBounds(bool value)
{
if (value == masksToBounds())
return;
GraphicsLayer::setMasksToBounds(value);
m_impl->notifyChange(GraphicsLayerQtImpl::MasksToBoundsChange);
}
void GraphicsLayerQt::setDrawsContent(bool value)
{
if (value == drawsContent())
return;
m_impl->notifyChange(GraphicsLayerQtImpl::DrawsContentChange);
GraphicsLayer::setDrawsContent(value);
}
void GraphicsLayerQt::setBackgroundColor(const Color& value)
{
if (value == m_impl->m_pendingContent.backgroundColor)
return;
m_impl->m_pendingContent.backgroundColor = value;
GraphicsLayer::setBackgroundColor(value);
m_impl->notifyChange(GraphicsLayerQtImpl::BackgroundColorChange);
}
void GraphicsLayerQt::clearBackgroundColor()
{
if (!m_impl->m_pendingContent.backgroundColor.isValid())
return;
m_impl->m_pendingContent.backgroundColor = QColor();
GraphicsLayer::clearBackgroundColor();
m_impl->notifyChange(GraphicsLayerQtImpl::BackgroundColorChange);
}
void GraphicsLayerQt::setContentsOpaque(bool value)
{
if (value == contentsOpaque())
return;
m_impl->notifyChange(GraphicsLayerQtImpl::ContentsOpaqueChange);
GraphicsLayer::setContentsOpaque(value);
}
void GraphicsLayerQt::setBackfaceVisibility(bool value)
{
if (value == backfaceVisibility())
return;
GraphicsLayer::setBackfaceVisibility(value);
m_impl->notifyChange(GraphicsLayerQtImpl::BackfaceVisibilityChange);
}
void GraphicsLayerQt::setOpacity(float value)
{
if (value == opacity())
return;
GraphicsLayer::setOpacity(value);
m_impl->notifyChange(GraphicsLayerQtImpl::OpacityChange);
}
void GraphicsLayerQt::setContentsRect(const IntRect& value)
{
if (value == contentsRect())
return;
GraphicsLayer::setContentsRect(value);
m_impl->notifyChange(GraphicsLayerQtImpl::ContentsRectChange);
}
void GraphicsLayerQt::setContentsToImage(Image* image)
{
m_impl->notifyChange(GraphicsLayerQtImpl::ContentChange);
m_impl->m_pendingContent.contentType = GraphicsLayerQtImpl::HTMLContentType;
GraphicsLayer::setContentsToImage(image);
if (image) {
QPixmap* pxm = image->nativeImageForCurrentFrame();
if (pxm) {
m_impl->m_pendingContent.pixmap = *pxm;
m_impl->m_pendingContent.contentType = GraphicsLayerQtImpl::PixmapContentType;
return;
}
}
m_impl->m_pendingContent.pixmap = QPixmap();
}
void GraphicsLayerQt::setContentsBackgroundColor(const Color& color)
{
m_impl->notifyChange(GraphicsLayerQtImpl::ContentChange);
m_impl->m_pendingContent.contentType = GraphicsLayerQtImpl::ColorContentType;
m_impl->m_pendingContent.contentsBackgroundColor = QColor(color);
GraphicsLayer::setContentsBackgroundColor(color);
}
#if ENABLE(3D_CANVAS)
void GraphicsLayerQt::setContentsToGraphicsContext3D(const GraphicsContext3D* ctx)
{
if (ctx == m_impl->m_gc3D)
return;
m_impl->m_pendingContent.contentType = GraphicsLayerQtImpl::Canvas3DContentType;
m_impl->m_gc3D = ctx;
m_impl->notifyChange(GraphicsLayerQtImpl::ContentChange);
}
void GraphicsLayerQt::setGraphicsContext3DNeedsDisplay()
{
setNeedsDisplay();
}
#endif
void GraphicsLayerQt::setContentsToMedia(PlatformLayer* media)
{
if (media) {
m_impl->m_pendingContent.contentType = GraphicsLayerQtImpl::MediaContentType;
m_impl->m_pendingContent.mediaLayer = media->toGraphicsObject();
} else
m_impl->m_pendingContent.contentType = GraphicsLayerQtImpl::HTMLContentType;
m_impl->notifyChange(GraphicsLayerQtImpl::ContentChange);
GraphicsLayer::setContentsToMedia(media);
}
void GraphicsLayerQt::setGeometryOrientation(CompositingCoordinatesOrientation orientation)
{
m_impl->notifyChange(GraphicsLayerQtImpl::GeometryOrientationChange);
GraphicsLayer::setGeometryOrientation(orientation);
}
void GraphicsLayerQt::setContentsOrientation(CompositingCoordinatesOrientation orientation)
{
m_impl->notifyChange(GraphicsLayerQtImpl::ContentsOrientationChange);
GraphicsLayer::setContentsOrientation(orientation);
}
void GraphicsLayerQt::distributeOpacity(float o)
{
m_impl->notifyChange(GraphicsLayerQtImpl::OpacityChange);
m_impl->m_state.distributeOpacity = true;
}
float GraphicsLayerQt::accumulatedOpacity() const
{
return m_impl->effectiveOpacity();
}
void GraphicsLayerQt::syncCompositingState()
{
m_impl->flushChanges();
GraphicsLayer::syncCompositingState();
}
NativeLayer GraphicsLayerQt::nativeLayer() const
{
return m_impl.get();
}
PlatformLayer* GraphicsLayerQt::platformLayer() const
{
return m_impl.get();
}
template <typename T>
struct KeyframeValueQt {
TimingFunction timingFunction;
T value;
};
static inline double solveEpsilon(double duration)
{
return 1.0 / (200.0 * duration);
}
static inline double solveCubicBezierFunction(qreal p1x, qreal p1y, qreal p2x, qreal p2y, double t, double duration)
{
UnitBezier bezier(p1x, p1y, p2x, p2y);
return bezier.solve(t, solveEpsilon(duration));
}
static inline qreal applyTimingFunction(const TimingFunction& timingFunction, qreal progress, double duration)
{
if (timingFunction.type() == LinearTimingFunction)
return progress;
if (timingFunction.type() == CubicBezierTimingFunction) {
return solveCubicBezierFunction(timingFunction.x1(),
timingFunction.y1(),
timingFunction.x2(),
timingFunction.y2(),
double(progress), double(duration) / 1000);
}
return progress;
}
static void webkitAnimationToQtAnimationValue(const AnimationValue* animationValue, TransformOperations& transformOperations)
{
transformOperations = TransformOperations();
if (!animationValue)
return;
if (const TransformOperations* ops = static_cast<const TransformAnimationValue*>(animationValue)->value())
transformOperations = *ops;
}
static void webkitAnimationToQtAnimationValue(const AnimationValue* animationValue, qreal& realValue)
{
realValue = animationValue ? static_cast<const FloatAnimationValue*>(animationValue)->value() : 0;
}
#ifndef QT_NO_ANIMATION
class AnimationQtBase : public QAbstractAnimation {
public:
AnimationQtBase(GraphicsLayerQtImpl* layer, const KeyframeValueList& values, const IntSize& boxSize, const Animation* anim, const QString & name)
: QAbstractAnimation(0)
, m_layer(layer)
, m_boxSize(boxSize)
, m_duration(anim->duration() * 1000)
, m_isAlternate(anim->direction() == Animation::AnimationDirectionAlternate)
, m_webkitPropertyID(values.property())
, m_webkitAnimation(anim)
, m_keyframesName(name)
, m_fillsForwards(false)
{
}
virtual void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
QAbstractAnimation::updateState(newState, oldState);
if (newState == Running && oldState == Stopped && m_layer.data())
m_layer.data()->notifyAnimationStartedAsync();
}
virtual int duration() const { return m_duration; }
QWeakPointer<GraphicsLayerQtImpl> m_layer;
IntSize m_boxSize;
int m_duration;
bool m_isAlternate;
AnimatedPropertyID m_webkitPropertyID;
const Animation* m_webkitAnimation;
QString m_keyframesName;
bool m_fillsForwards;
};
template <typename T>
class AnimationQt : public AnimationQtBase {
public:
AnimationQt(GraphicsLayerQtImpl* layer, const KeyframeValueList& values, const IntSize& boxSize, const Animation* anim, const QString & name)
:AnimationQtBase(layer, values, boxSize, anim, name)
{
for (size_t i = 0; i < values.size(); ++i) {
const AnimationValue* animationValue = values.at(i);
KeyframeValueQt<T> keyframeValue;
if (animationValue->timingFunction())
keyframeValue.timingFunction = *animationValue->timingFunction();
else
keyframeValue.timingFunction = anim->timingFunction();
webkitAnimationToQtAnimationValue(animationValue, keyframeValue.value);
m_keyframeValues[animationValue->keyTime()] = keyframeValue;
}
}
protected:
virtual void applyFrame(const T& fromValue, const T& toValue, qreal progress) = 0;
virtual void updateCurrentTime(int currentTime)
{
if (!m_layer)
return;
qreal progress = qreal(currentLoopTime()) / duration();
if (m_isAlternate && currentLoop()%2)
progress = 1-progress;
if (m_keyframeValues.isEmpty())
return;
typename QMap<qreal, KeyframeValueQt<T> >::iterator it = m_keyframeValues.find(progress);
if (it == m_keyframeValues.end())
it = m_keyframeValues.lowerBound(progress)-1;
if (it == m_keyframeValues.end())
it = m_keyframeValues.begin();
typename QMap<qreal, KeyframeValueQt<T> >::iterator it2 = it+1;
if (it2 == m_keyframeValues.end())
it2 = it;
const KeyframeValueQt<T>& fromKeyframe = it.value();
const KeyframeValueQt<T>& toKeyframe = it2.value();
const TimingFunction& timingFunc = fromKeyframe.timingFunction;
const T& fromValue = fromKeyframe.value;
const T& toValue = toKeyframe.value;
progress = (!progress || progress == 1 || it.key() == it2.key())
? progress
: applyTimingFunction(timingFunc, (progress - it.key()) / (it2.key() - it.key()), duration());
applyFrame(fromValue, toValue, progress);
}
QMap<qreal, KeyframeValueQt<T> > m_keyframeValues;
};
class TransformAnimationQt : public AnimationQt<TransformOperations> {
public:
TransformAnimationQt(GraphicsLayerQtImpl* layer, const KeyframeValueList& values, const IntSize& boxSize, const Animation* anim, const QString & name)
: AnimationQt<TransformOperations>(layer, values, boxSize, anim, name)
{
}
~TransformAnimationQt()
{
if (m_fillsForwards)
setCurrentTime(1);
}
virtual void applyFrame(const TransformOperations& sourceOperations, const TransformOperations& targetOperations, qreal progress)
{
TransformationMatrix transformMatrix;
bool validTransformLists = true;
const int sourceOperationCount = sourceOperations.size();
if (sourceOperationCount) {
if (targetOperations.size() != sourceOperationCount)
validTransformLists = false;
else
for (size_t j = 0; j < sourceOperationCount && validTransformLists; ++j)
if (!sourceOperations.operations()[j]->isSameType(*targetOperations.operations()[j]))
validTransformLists = false;
}
if (validTransformLists) {
for (size_t i = 0; i < targetOperations.size(); ++i)
targetOperations.operations()[i]->blend(sourceOperations.at(i), progress)->apply(transformMatrix, m_boxSize);
} else {
targetOperations.apply(m_boxSize, transformMatrix);
transformMatrix.blend(m_sourceMatrix, progress);
}
m_layer.data()->setBaseTransform(transformMatrix);
if (m_fillsForwards)
m_layer.data()->m_layer->setTransform(m_layer.data()->m_baseTransform);
}
virtual void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
AnimationQtBase::updateState(newState, oldState);
if (!m_layer)
return;
m_layer.data()->flushChanges(true);
if (newState == QAbstractAnimation::Running) {
m_sourceMatrix = m_layer.data()->m_layer->transform();
m_layer.data()->m_transformAnimationRunning = true;
} else if (newState == QAbstractAnimation::Stopped) {
m_layer.data()->m_transformAnimationRunning = false;
if (m_layer && m_layer.data()->m_layer)
m_layer.data()->setBaseTransform(m_layer.data()->m_layer->transform());
}
}
TransformationMatrix m_sourceMatrix;
};
class OpacityAnimationQt : public AnimationQt<qreal> {
public:
OpacityAnimationQt(GraphicsLayerQtImpl* layer, const KeyframeValueList& values, const IntSize& boxSize, const Animation* anim, const QString & name)
: AnimationQt<qreal>(layer, values, boxSize, anim, name)
{
}
~OpacityAnimationQt()
{
if (m_fillsForwards)
setCurrentTime(1);
}
virtual void applyFrame(const qreal& fromValue, const qreal& toValue, qreal progress)
{
qreal opacity = qBound(qreal(0), fromValue + (toValue-fromValue)*progress, qreal(1));
if (!m_layer.data()->opacity() && opacity)
m_layer.data()->scene()->update();
m_layer.data()->setOpacity(opacity);
if (m_fillsForwards)
m_layer.data()->m_layer->setOpacity(opacity);
}
virtual void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
{
QAbstractAnimation::updateState(newState, oldState);
if (m_layer)
m_layer.data()->m_opacityAnimationRunning = (newState == QAbstractAnimation::Running);
if (newState == Stopped)
if (m_layer && m_layer.data()->m_layer)
m_layer.data()->setOpacity(m_layer.data()->m_layer->opacity());
}
};
bool GraphicsLayerQt::addAnimation(const KeyframeValueList& values, const IntSize& boxSize, const Animation* anim, const String& keyframesName, double timeOffset)
{
if (!anim->duration() || !anim->iterationCount())
return false;
AnimationQtBase* newAnim = 0;
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_impl->m_animations.begin(); it != m_impl->m_animations.end(); ++it) {
if (*it) {
AnimationQtBase* curAnimation = static_cast<AnimationQtBase*>(it->data());
if (curAnimation && curAnimation->m_webkitAnimation == anim)
newAnim = curAnimation;
}
}
if (!newAnim) {
switch (values.property()) {
case AnimatedPropertyOpacity:
newAnim = new OpacityAnimationQt(m_impl.get(), values, boxSize, anim, keyframesName);
break;
case AnimatedPropertyWebkitTransform:
newAnim = new TransformAnimationQt(m_impl.get(), values, boxSize, anim, keyframesName);
break;
default:
return false;
}
newAnim->setLoopCount(anim->iterationCount());
newAnim->m_fillsForwards = anim->fillsForwards();
m_impl->m_animations.append(QWeakPointer<QAbstractAnimation>(newAnim));
QObject::connect(&m_impl->m_suspendTimer, SIGNAL(timeout()), newAnim, SLOT(resume()));
}
m_impl->flushChanges(false);
if (anim->fillsBackwards())
newAnim->setCurrentTime(0);
if (anim->delay())
QTimer::singleShot(anim->delay() * 1000, newAnim, SLOT(start()));
else
newAnim->start();
newAnim->setCurrentTime(timeOffset * 1000);
return true;
}
void GraphicsLayerQt::removeAnimationsForProperty(AnimatedPropertyID id)
{
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_impl->m_animations.begin(); it != m_impl->m_animations.end(); ++it) {
if (*it) {
AnimationQtBase* anim = static_cast<AnimationQtBase*>(it->data());
if (anim && anim->m_webkitPropertyID == id) {
anim->stop();
anim->deleteLater();
it = m_impl->m_animations.erase(it);
--it;
}
}
}
}
void GraphicsLayerQt::removeAnimationsForKeyframes(const String& name)
{
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_impl->m_animations.begin(); it != m_impl->m_animations.end(); ++it) {
if (*it) {
AnimationQtBase* anim = static_cast<AnimationQtBase*>((*it).data());
if (anim && anim->m_keyframesName == QString(name)) {
anim->stop();
anim->deleteLater();
it = m_impl->m_animations.erase(it);
--it;
}
}
}
}
void GraphicsLayerQt::pauseAnimation(const String& name, double timeOffset)
{
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_impl->m_animations.begin(); it != m_impl->m_animations.end(); ++it) {
if (!(*it))
continue;
AnimationQtBase* anim = static_cast<AnimationQtBase*>((*it).data());
if (anim && anim->m_keyframesName == QString(name)) {
anim->setCurrentTime(timeOffset * 1000);
anim->pause();
}
}
}
void GraphicsLayerQt::suspendAnimations(double time)
{
if (m_impl->m_suspendTimer.isActive()) {
m_impl->m_suspendTimer.stop();
m_impl->m_suspendTimer.start(time * 1000);
} else {
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_impl->m_animations.begin(); it != m_impl->m_animations.end(); ++it) {
QAbstractAnimation* anim = it->data();
if (anim)
anim->pause();
}
}
}
void GraphicsLayerQt::resumeAnimations()
{
if (m_impl->m_suspendTimer.isActive()) {
m_impl->m_suspendTimer.stop();
for (QList<QWeakPointer<QAbstractAnimation> >::iterator it = m_impl->m_animations.begin(); it != m_impl->m_animations.end(); ++it) {
QAbstractAnimation* anim = (*it).data();
if (anim)
anim->resume();
}
}
}
#endif // QT_NO_ANIMATION
}
#include <GraphicsLayerQt.moc>