123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766 |
- /*
- Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
- Copyright (C) 2014 Sony Computer Entertainment Inc.
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public
- License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Library General Public License for more details.
- You should have received a copy of the GNU Library General Public License
- along with this library; see the file COPYING.LIB. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
- */
- #include "config.h"
- #include "AcagiLayer.h"
- #include "FloatQuad.h"
- #include "Region.h"
- #include <wtf/MathExtras.h>
- #if USE(ACCELERATED_COMPOSITING) && USE(ACAGI)
- namespace WebCore {
- class AcagiPaintOptions {
- public:
- RefPtr<AcagiBitmapTexture> surface;
- float opacity;
- TransformationMatrix transform;
- IntSize offset;
- AcagiCompositor* compositor;
- AcagiPaintOptions()
- : opacity(1)
- , compositor(0)
- { }
- };
- const AcagiLayer* AcagiLayer::rootLayer() const
- {
- if (m_effectTarget)
- return m_effectTarget->rootLayer();
- if (m_parent)
- return m_parent->rootLayer();
- return this;
- }
- void AcagiLayer::computeTransformsRecursive()
- {
- if (m_state.size.isEmpty() && m_state.masksToBounds)
- return;
- // Compute transforms recursively on the way down to leafs.
- TransformationMatrix parentTransform;
- if (m_parent)
- parentTransform = m_parent->m_currentTransform.combinedForChildren();
- else if (m_effectTarget)
- parentTransform = m_effectTarget->m_currentTransform.combined();
- m_currentTransform.combineTransforms(parentTransform);
- m_state.visible = m_state.backfaceVisibility || !m_currentTransform.combined().isBackFaceVisible();
- if (m_parent && m_parent->m_state.preserves3D)
- m_centerZ = m_currentTransform.combined().mapPoint(FloatPoint3D(m_state.size.width() / 2, m_state.size.height() / 2, 0)).z();
- if (m_state.maskLayer)
- m_state.maskLayer->computeTransformsRecursive();
- if (m_state.replicaLayer)
- m_state.replicaLayer->computeTransformsRecursive();
- for (size_t i = 0; i < m_children.size(); ++i)
- m_children[i]->computeTransformsRecursive();
- // Reorder children if needed on the way back up.
- if (m_state.preserves3D)
- sortByZOrder(m_children);
- }
- void AcagiLayer::paint()
- {
- computeTransformsRecursive();
- AcagiPaintOptions options;
- options.compositor = m_compositor;
- options.compositor->bindSurface(0);
- paintRecursive(options);
- }
- static Color blendWithOpacity(const Color& color, float opacity)
- {
- RGBA32 rgba = color.rgb();
- // See Color::getRGBA() to know how to extract alpha from color.
- float alpha = alphaChannel(rgba) / 255.;
- float effectiveAlpha = alpha * opacity;
- return Color(colorWithOverrideAlpha(rgba, effectiveAlpha));
- }
- void AcagiLayer::computePatternTransformIfNeeded()
- {
- if (!m_patternTransformDirty)
- return;
- m_patternTransformDirty = false;
- m_patternTransform =
- TransformationMatrix::rectToRect(FloatRect(FloatPoint::zero(), m_state.contentsTileSize), m_state.contentsRect)
- .multiply(TransformationMatrix().translate(m_state.contentsTilePhase.x() / m_state.contentsRect.width(), m_state.contentsTilePhase.y() / m_state.contentsRect.height()));
- }
- void AcagiLayer::paintSelf(const AcagiPaintOptions& options)
- {
- if (!m_state.visible || !m_state.contentsVisible)
- return;
- // We apply the following transform to compensate for painting into a surface, and then apply the offset so that the painting fits in the target rect.
- TransformationMatrix transform;
- transform.translate(options.offset.width(), options.offset.height());
- transform.multiply(options.transform);
- transform.multiply(m_currentTransform.combined());
- if (m_state.solidColor.isValid() && !m_state.contentsRect.isEmpty() && m_state.solidColor.alpha()) {
- options.compositor->drawSolidColor(m_state.contentsRect, transform, blendWithOpacity(m_state.solidColor, options.opacity));
- if (m_state.showDebugBorders)
- options.compositor->drawBorder(m_state.debugBorderColor, m_state.debugBorderWidth, layerRect(), transform);
- return;
- }
- options.compositor->setWrapMode(AcagiCompositor::StretchWrap);
- options.compositor->setPatternTransform(TransformationMatrix());
- if (m_backingStore) {
- FloatRect targetRect = layerRect();
- ASSERT(!targetRect.isEmpty());
- m_backingStore->compositeContents(options.compositor, targetRect, transform, options.opacity);
- if (m_state.showDebugBorders)
- m_backingStore->drawBorder(options.compositor, m_state.debugBorderColor, m_state.debugBorderWidth, targetRect, transform);
- // Only draw repaint count for the main backing store.
- if (m_state.showRepaintCounter)
- m_backingStore->drawRepaintCounter(options.compositor, m_state.repaintCount, m_state.debugBorderColor, targetRect, transform);
- }
- if (!m_contentsLayer)
- return;
- if (!m_state.contentsTileSize.isEmpty()) {
- computePatternTransformIfNeeded();
- options.compositor->setWrapMode(AcagiCompositor::RepeatWrap);
- options.compositor->setPatternTransform(m_patternTransform);
- }
- ASSERT(!layerRect().isEmpty());
- m_contentsLayer->compositeContents(options.compositor, m_state.contentsRect, transform, options.opacity);
- if (m_state.showDebugBorders)
- m_contentsLayer->drawBorder(options.compositor, m_state.debugBorderColor, m_state.debugBorderWidth, m_state.contentsRect, transform);
- }
- int AcagiLayer::compareGraphicsLayersZValue(const void* a, const void* b)
- {
- AcagiLayer* const* layerA = static_cast<AcagiLayer* const*>(a);
- AcagiLayer* const* layerB = static_cast<AcagiLayer* const*>(b);
- return int(((*layerA)->m_centerZ - (*layerB)->m_centerZ) * 1000);
- }
- void AcagiLayer::sortByZOrder(Vector<AcagiLayer* >& array)
- {
- qsort(array.data(), array.size(), sizeof(AcagiLayer*), compareGraphicsLayersZValue);
- }
- void AcagiLayer::paintSelfAndChildren(const AcagiPaintOptions& options)
- {
- paintSelf(options);
- if (m_children.isEmpty())
- return;
- bool shouldClip = m_state.masksToBounds && !m_state.preserves3D;
- if (shouldClip) {
- TransformationMatrix clipTransform;
- clipTransform.translate(options.offset.width(), options.offset.height());
- clipTransform.multiply(options.transform);
- clipTransform.multiply(m_currentTransform.combined());
- options.compositor->beginClip(clipTransform, layerRect());
- }
- for (size_t i = 0; i < m_children.size(); ++i)
- m_children[i]->paintRecursive(options);
- if (shouldClip)
- options.compositor->endClip();
- }
- bool AcagiLayer::shouldBlend() const
- {
- if (m_state.preserves3D)
- return false;
- return m_currentOpacity < 1
- || hasFilters()
- || m_state.maskLayer
- || (m_state.replicaLayer && m_state.replicaLayer->m_state.maskLayer);
- }
- bool AcagiLayer::isVisible() const
- {
- if (m_state.size.isEmpty() && (m_state.masksToBounds || m_state.maskLayer || m_children.isEmpty()))
- return false;
- if (!m_state.visible && m_children.isEmpty())
- return false;
- if (!m_state.contentsVisible && m_children.isEmpty())
- return false;
- if (m_currentOpacity < 0.01)
- return false;
- return true;
- }
- void AcagiLayer::paintSelfAndChildrenWithReplica(const AcagiPaintOptions& options)
- {
- if (m_state.replicaLayer) {
- AcagiPaintOptions replicaOptions(options);
- replicaOptions.transform
- .multiply(m_state.replicaLayer->m_currentTransform.combined())
- .multiply(m_currentTransform.combined().inverse());
- paintSelfAndChildren(replicaOptions);
- }
- paintSelfAndChildren(options);
- }
- void AcagiLayer::setAnimatedTransform(const TransformationMatrix& matrix)
- {
- m_currentTransform.setLocalTransform(matrix);
- }
- void AcagiLayer::setAnimatedOpacity(float opacity)
- {
- m_currentOpacity = opacity;
- }
- TransformationMatrix AcagiLayer::replicaTransform()
- {
- return TransformationMatrix(m_state.replicaLayer->m_currentTransform.combined()).multiply(m_currentTransform.combined().inverse());
- }
- static void resolveOverlaps(Region newRegion, Region& overlapRegion, Region& nonOverlapRegion)
- {
- Region newOverlapRegion(newRegion);
- newOverlapRegion.intersect(nonOverlapRegion);
- nonOverlapRegion.subtract(newOverlapRegion);
- overlapRegion.unite(newOverlapRegion);
- newRegion.subtract(overlapRegion);
- nonOverlapRegion.unite(newRegion);
- }
- void AcagiLayer::computeOverlapRegions(Region& overlapRegion, Region& nonOverlapRegion, ResolveSelfOverlapMode mode)
- {
- if (!m_state.visible || !m_state.contentsVisible)
- return;
- FloatRect boundingRect;
- if (m_backingStore || m_state.masksToBounds || m_state.maskLayer || hasFilters())
- boundingRect = layerRect();
- else if (m_contentsLayer || m_state.solidColor.alpha())
- boundingRect = m_state.contentsRect;
- TransformationMatrix replicaMatrix;
- if (m_state.replicaLayer) {
- replicaMatrix = replicaTransform();
- boundingRect.unite(replicaMatrix.mapRect(boundingRect));
- }
- boundingRect = m_currentTransform.combined().mapRect(boundingRect);
- // Count all masks and filters as overlap layers.
- if (hasFilters() || m_state.maskLayer || (m_state.replicaLayer && m_state.replicaLayer->m_state.maskLayer)) {
- Region newOverlapRegion(enclosingIntRect(boundingRect));
- nonOverlapRegion.subtract(newOverlapRegion);
- overlapRegion.unite(newOverlapRegion);
- return;
- }
- Region newOverlapRegion;
- Region newNonOverlapRegion(enclosingIntRect(boundingRect));
- if (!m_state.masksToBounds) {
- for (size_t i = 0; i < m_children.size(); ++i) {
- AcagiLayer* child = m_children[i];
- child->computeOverlapRegions(newOverlapRegion, newNonOverlapRegion, ResolveSelfOverlapIfNeeded);
- }
- }
- if (m_state.replicaLayer) {
- newOverlapRegion.unite(replicaMatrix.mapRect(newOverlapRegion.bounds()));
- Region replicaRegion(replicaMatrix.mapRect(newNonOverlapRegion.bounds()));
- resolveOverlaps(replicaRegion, newOverlapRegion, newNonOverlapRegion);
- }
- if ((mode != ResolveSelfOverlapAlways) && shouldBlend()) {
- newNonOverlapRegion.unite(newOverlapRegion);
- newOverlapRegion = Region();
- }
- overlapRegion.unite(newOverlapRegion);
- resolveOverlaps(newNonOverlapRegion, overlapRegion, nonOverlapRegion);
- }
- void AcagiLayer::paintUsingOverlapRegions(const AcagiPaintOptions& options)
- {
- Region overlapRegion;
- Region nonOverlapRegion;
- computeOverlapRegions(overlapRegion, nonOverlapRegion, ResolveSelfOverlapAlways);
- if (overlapRegion.isEmpty()) {
- paintSelfAndChildrenWithReplica(options);
- return;
- }
- // Having both overlap and non-overlap regions carries some overhead. Avoid it if the overlap area
- // is big anyway.
- if (overlapRegion.bounds().size().area() > nonOverlapRegion.bounds().size().area()) {
- overlapRegion.unite(nonOverlapRegion);
- nonOverlapRegion = Region();
- }
- nonOverlapRegion.translate(options.offset);
- Vector<IntRect> rects = nonOverlapRegion.rects();
- for (size_t i = 0; i < rects.size(); ++i) {
- IntRect rect = rects[i];
- if (!rect.intersects(options.compositor->clipBounds()))
- continue;
- options.compositor->beginClip(TransformationMatrix(), rects[i]);
- paintSelfAndChildrenWithReplica(options);
- options.compositor->endClip();
- }
- rects = overlapRegion.rects();
- static const size_t OverlapRegionConsolidationThreshold = 4;
- if (nonOverlapRegion.isEmpty() && rects.size() > OverlapRegionConsolidationThreshold) {
- rects.clear();
- rects.append(overlapRegion.bounds());
- }
- IntSize maxTextureSize = options.compositor->maxTextureSize();
- IntRect adjustedClipBounds(options.compositor->clipBounds());
- adjustedClipBounds.move(-options.offset);
- for (size_t i = 0; i < rects.size(); ++i) {
- IntRect rect = rects[i];
- for (int x = rect.x(); x < rect.maxX(); x += maxTextureSize.width()) {
- for (int y = rect.y(); y < rect.maxY(); y += maxTextureSize.height()) {
- IntRect tileRect(IntPoint(x, y), maxTextureSize);
- tileRect.intersect(rect);
- if (!tileRect.intersects(adjustedClipBounds))
- continue;
- paintWithIntermediateSurface(options, tileRect);
- }
- }
- }
- }
- void AcagiLayer::applyMask(const AcagiPaintOptions& options)
- {
- options.compositor->setMaskMode(true);
- paintSelf(options);
- options.compositor->setMaskMode(false);
- }
- PassRefPtr<AcagiBitmapTexture> AcagiLayer::paintIntoSurface(const AcagiPaintOptions& options, const IntSize& size)
- {
- RefPtr<AcagiBitmapTexture> surface = options.compositor->acquireTextureFromPool(size);
- AcagiPaintOptions paintOptions(options);
- paintOptions.surface = surface;
- options.compositor->bindSurface(surface.get());
- paintSelfAndChildren(paintOptions);
- if (m_state.maskLayer)
- m_state.maskLayer->applyMask(options);
- options.compositor->bindSurface(surface.get());
- return surface;
- }
- static void commitSurface(const AcagiPaintOptions& options, PassRefPtr<AcagiBitmapTexture> surface, const IntRect& rect, float opacity)
- {
- options.compositor->bindSurface(options.surface.get());
- TransformationMatrix targetTransform;
- targetTransform.translate(options.offset.width(), options.offset.height());
- targetTransform.multiply(options.transform);
- options.compositor->drawTexture(*surface.get(), rect, targetTransform, opacity);
- }
- void AcagiLayer::paintWithIntermediateSurface(const AcagiPaintOptions& options, const IntRect& rect)
- {
- RefPtr<AcagiBitmapTexture> replicaSurface;
- RefPtr<AcagiBitmapTexture> mainSurface;
- AcagiPaintOptions paintOptions(options);
- paintOptions.offset = -IntSize(rect.x(), rect.y());
- paintOptions.opacity = 1;
- paintOptions.transform = TransformationMatrix();
- if (m_state.replicaLayer) {
- paintOptions.transform = replicaTransform();
- replicaSurface = paintIntoSurface(paintOptions, rect.size());
- paintOptions.transform = TransformationMatrix();
- if (m_state.replicaLayer->m_state.maskLayer)
- m_state.replicaLayer->m_state.maskLayer->applyMask(paintOptions);
- }
- if (replicaSurface && options.opacity == 1) {
- commitSurface(options, replicaSurface, rect, 1);
- replicaSurface.clear();
- }
- mainSurface = paintIntoSurface(paintOptions, rect.size());
- if (replicaSurface) {
- options.compositor->bindSurface(replicaSurface.get());
- options.compositor->drawTexture(*mainSurface.get(), FloatRect(FloatPoint::zero(), rect.size()));
- mainSurface = replicaSurface;
- }
- commitSurface(options, mainSurface, rect, options.opacity);
- }
- void AcagiLayer::paintRecursive(const AcagiPaintOptions& options)
- {
- if (!isVisible())
- return;
- AcagiPaintOptions paintOptions(options);
- paintOptions.opacity *= m_currentOpacity;
- if (!shouldBlend()) {
- paintSelfAndChildrenWithReplica(paintOptions);
- return;
- }
- paintUsingOverlapRegions(paintOptions);
- }
- AcagiLayer::~AcagiLayer()
- {
- for (int i = m_children.size() - 1; i >= 0; --i)
- m_children[i]->m_parent = 0;
- if (m_parent)
- m_parent->m_children.remove(m_parent->m_children.find(this));
- }
- AcagiCompositor* AcagiLayer::compositor() const
- {
- return rootLayer()->m_compositor;
- }
- void AcagiLayer::setChildren(const Vector<AcagiLayer*>& newChildren)
- {
- removeAllChildren();
- for (size_t i = 0; i < newChildren.size(); ++i)
- addChild(newChildren[i]);
- }
- void AcagiLayer::addChild(AcagiLayer* childLayer)
- {
- ASSERT(childLayer != this);
- if (childLayer->m_parent)
- childLayer->removeFromParent();
- childLayer->m_parent = this;
- m_children.append(childLayer);
- }
- void AcagiLayer::removeFromParent()
- {
- if (m_parent) {
- unsigned i;
- for (i = 0; i < m_parent->m_children.size(); i++) {
- if (this == m_parent->m_children[i]) {
- m_parent->m_children.remove(i);
- break;
- }
- }
- m_parent = 0;
- }
- }
- void AcagiLayer::removeAllChildren()
- {
- while (m_children.size()) {
- AcagiLayer* curLayer = m_children[0];
- ASSERT(curLayer->m_parent);
- curLayer->removeFromParent();
- }
- }
- void AcagiLayer::setMaskLayer(AcagiLayer* maskLayer)
- {
- if (maskLayer)
- maskLayer->m_effectTarget = this;
- m_state.maskLayer = maskLayer;
- }
- void AcagiLayer::setReplicaLayer(AcagiLayer* replicaLayer)
- {
- if (replicaLayer)
- replicaLayer->m_effectTarget = this;
- m_state.replicaLayer = replicaLayer;
- }
- void AcagiLayer::setPosition(const FloatPoint& position)
- {
- m_state.pos = position;
- m_currentTransform.setPosition(adjustedPosition());
- }
- void AcagiLayer::setSize(const FloatSize& size)
- {
- m_state.size = size;
- m_currentTransform.setSize(size);
- }
- void AcagiLayer::setAnchorPoint(const FloatPoint3D& anchorPoint)
- {
- m_state.anchorPoint = anchorPoint;
- m_currentTransform.setAnchorPoint(anchorPoint);
- }
- void AcagiLayer::setPreserves3D(bool preserves3D)
- {
- m_state.preserves3D = preserves3D;
- m_currentTransform.setFlattening(!preserves3D);
- }
- void AcagiLayer::setTransform(const TransformationMatrix& transform)
- {
- m_state.transform = transform;
- m_currentTransform.setLocalTransform(transform);
- }
- void AcagiLayer::setChildrenTransform(const TransformationMatrix& childrenTransform)
- {
- m_state.childrenTransform = childrenTransform;
- m_currentTransform.setChildrenTransform(childrenTransform);
- }
- void AcagiLayer::setContentsRect(const IntRect& contentsRect)
- {
- if (contentsRect == m_state.contentsRect)
- return;
- m_state.contentsRect = contentsRect;
- m_patternTransformDirty = true;
- }
- void AcagiLayer::setContentsTileSize(const IntSize& size)
- {
- if (size == m_state.contentsTileSize)
- return;
- m_state.contentsTileSize = size;
- m_patternTransformDirty = true;
- }
- void AcagiLayer::setContentsTilePhase(const IntPoint& phase)
- {
- if (phase == m_state.contentsTilePhase)
- return;
- m_state.contentsTilePhase = phase;
- m_patternTransformDirty = true;
- }
- void AcagiLayer::setMasksToBounds(bool masksToBounds)
- {
- m_state.masksToBounds = masksToBounds;
- }
- void AcagiLayer::setDrawsContent(bool drawsContent)
- {
- m_state.drawsContent = drawsContent;
- }
- void AcagiLayer::setContentsVisible(bool contentsVisible)
- {
- m_state.contentsVisible = contentsVisible;
- }
- void AcagiLayer::setContentsOpaque(bool contentsOpaque)
- {
- m_state.contentsOpaque = contentsOpaque;
- }
- void AcagiLayer::setBackfaceVisibility(bool backfaceVisibility)
- {
- m_state.backfaceVisibility = backfaceVisibility;
- }
- void AcagiLayer::setOpacity(float opacity)
- {
- m_state.opacity = opacity;
- }
- void AcagiLayer::setSolidColor(const Color& color)
- {
- m_state.solidColor = color;
- }
- void AcagiLayer::setDebugVisuals(bool showDebugBorders, const Color& debugBorderColor, float debugBorderWidth, bool showRepaintCounter)
- {
- m_state.showDebugBorders = showDebugBorders;
- m_state.debugBorderColor = debugBorderColor;
- m_state.debugBorderWidth = debugBorderWidth;
- m_state.showRepaintCounter = showRepaintCounter;
- }
- void AcagiLayer::setRepaintCount(int repaintCount)
- {
- m_state.repaintCount = repaintCount;
- }
- void AcagiLayer::setContentsLayer(PlatformLayerAcagi* platformLayer)
- {
- m_contentsLayer = platformLayer;
- }
- void AcagiLayer::setAnimations(const GraphicsLayerAnimations& animations)
- {
- m_animations = animations;
- }
- void AcagiLayer::setFixedToViewport(bool fixedToViewport)
- {
- m_fixedToViewport = fixedToViewport;
- }
- void AcagiLayer::setBackingStore(PassRefPtr<AcagiBackingStore> backingStore)
- {
- m_backingStore = backingStore;
- }
- bool AcagiLayer::descendantsOrSelfHaveRunningAnimations() const
- {
- if (m_animations.hasRunningAnimations())
- return true;
- for (size_t i = 0; i < m_children.size(); ++i) {
- if (m_children[i]->descendantsOrSelfHaveRunningAnimations())
- return true;
- }
- return false;
- }
- void AcagiLayer::applyAnimationsRecursively()
- {
- syncAnimations();
- for (size_t i = 0; i < m_children.size(); ++i)
- m_children[i]->applyAnimationsRecursively();
- }
- void AcagiLayer::syncAnimations()
- {
- m_animations.apply(this);
- if (!m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
- m_currentTransform.setLocalTransform(m_state.transform);
- if (!m_animations.hasActiveAnimationsOfType(AnimatedPropertyOpacity))
- m_currentOpacity = m_state.opacity;
- }
- bool AcagiLayer::isAncestorFixedToViewport() const
- {
- for (AcagiLayer* parent = m_parent; parent; parent = parent->m_parent) {
- if (parent->m_fixedToViewport)
- return true;
- }
- return false;
- }
- void AcagiLayer::setScrollPositionDeltaIfNeeded(const FloatSize& delta)
- {
- // delta is the difference between the scroll offset in the ui process and the scroll offset
- // in the web process. We add this delta to the position of fixed layers, to make
- // sure that they do not move while scrolling. We need to reset this delta to fixed layers
- // that have an ancestor which is also a fixed layer, because the delta will be added to the ancestor.
- if (isAncestorFixedToViewport())
- m_scrollPositionDelta = FloatSize();
- else
- m_scrollPositionDelta = delta;
- m_currentTransform.setPosition(adjustedPosition());
- }
- template<class HitTestCondition> AcagiLayer* AcagiLayer::hitTest(const FloatPoint& point, HitTestCondition condition)
- {
- if (!m_state.visible || !m_state.contentsVisible)
- return 0;
- AcagiLayer* result = 0;
- for (int i = m_children.size() - 1; !result && i >= 0; --i)
- result = m_children[i]->hitTest(point, condition);
- if (result)
- return result;
- return condition(this, point) ? this : 0;
- }
- bool AcagiLayer::scrollableLayerHitTestCondition(AcagiLayer* layer, const FloatPoint& point)
- {
- // scrolling layer's m_parent->m_parent, the parent of the scrolling layes, is the one that defines the
- // rectangle to be used for hit testing.
- if (!layer->isScrollable() || !layer->m_parent || !layer->m_parent->m_parent)
- return false;
- AcagiLayer* parentLayer = layer->m_parent->m_parent;
- FloatRect rect = parentLayer->layerRect();
- return parentLayer->m_currentTransform.combined().mapQuad(rect).containsPoint(point);
- }
- AcagiLayer* AcagiLayer::findScrollableContentsLayerAt(const FloatPoint& point)
- {
- return hitTest(point, &AcagiLayer::scrollableLayerHitTestCondition);
- }
- FloatSize AcagiLayer::mapScrollOffset(const FloatSize& offset)
- {
- double zeroX, zeroY, offsetX, offsetY;
- TransformationMatrix transform = m_currentTransform.combined().inverse();
- transform.map(0, 0, zeroX, zeroY);
- transform.map(offset.width(), offset.height(), offsetX, offsetY);
- return FloatSize(offsetX - zeroX, offsetY - zeroY);
- }
- void AcagiLayer::commitScrollOffset(const FloatSize& offset)
- {
- FloatSize fullOffset = m_accumulatedScrollOffsetFractionalPart + offset;
- int intWidth = round(fullOffset.width());
- int intHeight = round(fullOffset.height());
- // m_accumulatedScrollOffsetFractionalPart holds the fractional part of the user scroll offset that
- // has not yet been synced with the web process because the web process expects an IntSize.
- m_accumulatedScrollOffsetFractionalPart = FloatSize(fullOffset.width() - intWidth, fullOffset.height() - intHeight);
- m_scrollClient->commitScrollOffset(m_id, IntSize(intWidth, intHeight));
- }
- void AcagiLayer::scrollBy(const FloatSize& offset)
- {
- if (!isScrollable() || !m_scrollClient || offset.isZero())
- return;
- FloatSize scrollOffset = mapScrollOffset(offset);
- m_userScrollOffset += scrollOffset;
- m_currentTransform.setPosition(adjustedPosition());
- commitScrollOffset(scrollOffset);
- }
- void AcagiLayer::didCommitScrollOffset(const IntSize& offset)
- {
- m_userScrollOffset = FloatSize(m_userScrollOffset.width() - offset.width(), m_userScrollOffset.height() - offset.height());
- m_currentTransform.setPosition(adjustedPosition());
- }
- }
- #endif
|