#include "config.h"
#include "CCLayerTreeHostCommon.h"
#include "CCLayerImpl.h"
#include "CCLayerIterator.h"
#include "CCLayerSorter.h"
#include "CCMathUtil.h"
#include "CCRenderSurface.h"
#include "FloatQuad.h"
#include "IntRect.h"
#include "LayerChromium.h"
#include "RenderSurfaceChromium.h"
#include <public/WebTransformationMatrix.h>
using WebKit::WebTransformationMatrix;
namespace cc {
CCScrollAndScaleSet::CCScrollAndScaleSet()
{
}
CCScrollAndScaleSet::~CCScrollAndScaleSet()
{
}
IntRect CCLayerTreeHostCommon::calculateVisibleRect(const IntRect& targetSurfaceRect, const IntRect& layerBoundRect, const WebTransformationMatrix& transform)
{
IntRect layerInSurfaceSpace = CCMathUtil::mapClippedRect(transform, layerBoundRect);
if (targetSurfaceRect.contains(layerInSurfaceSpace))
return layerBoundRect;
IntRect minimalSurfaceRect = targetSurfaceRect;
minimalSurfaceRect.intersect(layerInSurfaceSpace);
const WebTransformationMatrix surfaceToLayer = transform.inverse();
IntRect layerRect = enclosingIntRect(CCMathUtil::projectClippedRect(surfaceToLayer, FloatRect(minimalSurfaceRect)));
layerRect.intersect(layerBoundRect);
return layerRect;
}
template<typename LayerType>
static inline bool layerIsInExisting3DRenderingContext(LayerType* layer)
{
return layer->parent() && layer->parent()->preserves3D();
}
template<typename LayerType>
static bool layerIsRootOfNewRenderingContext(LayerType* layer)
{
if (layer->parent())
return !layer->parent()->preserves3D() && layer->preserves3D();
return layer->preserves3D();
}
template<typename LayerType>
static bool isLayerBackFaceVisible(LayerType* layer)
{
if (layerIsInExisting3DRenderingContext(layer))
return layer->drawTransform().isBackFaceVisible();
return layer->transform().isBackFaceVisible();
}
template<typename LayerType>
static bool isSurfaceBackFaceVisible(LayerType* layer, const WebTransformationMatrix& drawTransform)
{
if (layerIsInExisting3DRenderingContext(layer))
return drawTransform.isBackFaceVisible();
if (layerIsRootOfNewRenderingContext(layer))
return layer->transform().isBackFaceVisible();
return false;
}
template<typename LayerType>
static inline bool layerClipsSubtree(LayerType* layer)
{
return layer->masksToBounds() || layer->maskLayer();
}
template<typename LayerType>
static IntRect calculateVisibleContentRect(LayerType* layer)
{
ASSERT(layer->renderTarget());
IntRect targetSurfaceRect = layer->renderTarget()->renderSurface()->contentRect();
targetSurfaceRect.intersect(layer->drawableContentRect());
if (targetSurfaceRect.isEmpty() || layer->contentBounds().isEmpty())
return IntRect();
const IntRect contentRect = IntRect(IntPoint(), layer->contentBounds());
IntRect visibleContentRect = CCLayerTreeHostCommon::calculateVisibleRect(targetSurfaceRect, contentRect, layer->drawTransform());
return visibleContentRect;
}
static bool isScaleOrTranslation(const WebTransformationMatrix& m)
{
return !m.m12() && !m.m13() && !m.m14()
&& !m.m21() && !m.m23() && !m.m24()
&& !m.m31() && !m.m32() && !m.m43()
&& m.m44();
}
static inline bool transformToParentIsKnown(CCLayerImpl*)
{
return true;
}
static inline bool transformToParentIsKnown(LayerChromium* layer)
{
return !layer->transformIsAnimating();
}
static inline bool transformToScreenIsKnown(CCLayerImpl*)
{
return true;
}
static inline bool transformToScreenIsKnown(LayerChromium* layer)
{
return !layer->screenSpaceTransformIsAnimating();
}
template<typename LayerType>
static bool layerShouldBeSkipped(LayerType* layer)
{
if (!layer->drawsContent() || layer->bounds().isEmpty())
return true;
LayerType* backfaceTestLayer = layer;
if (layer->useParentBackfaceVisibility()) {
ASSERT(layer->parent());
ASSERT(!layer->parent()->useParentBackfaceVisibility());
backfaceTestLayer = layer->parent();
}
if (!backfaceTestLayer->doubleSided() && transformToScreenIsKnown(backfaceTestLayer) && isLayerBackFaceVisible(backfaceTestLayer))
return true;
return false;
}
static inline bool subtreeShouldBeSkipped(CCLayerImpl* layer)
{
return !layer->opacity();
}
static inline bool subtreeShouldBeSkipped(LayerChromium* layer)
{
return !layer->opacity() && !layer->opacityIsAnimating();
}
template<typename LayerType>
static bool subtreeShouldRenderToSeparateSurface(LayerType* layer, bool axisAlignedWithRespectToParent)
{
if (!layer->parent())
return false;
bool descendantDrawsContent = layer->descendantDrawsContent();
if (layer->forceRenderSurface())
return true;
if (layer->maskLayer())
return true;
if (layer->replicaLayer())
return true;
if (!layer->filters().isEmpty() || !layer->backgroundFilters().isEmpty())
return true;
if (layerIsInExisting3DRenderingContext(layer) && !layer->preserves3D() && descendantDrawsContent)
return true;
if (layerClipsSubtree(layer) && !axisAlignedWithRespectToParent && descendantDrawsContent)
return true;
if (layer->opacity() != 1 && !layer->preserves3D() && descendantDrawsContent)
return true;
return false;
}
WebTransformationMatrix computeScrollCompensationForThisLayer(CCLayerImpl* scrollingLayer, const WebTransformationMatrix& parentMatrix)
{
WebTransformationMatrix partialLayerOriginTransform = parentMatrix;
partialLayerOriginTransform.scale(scrollingLayer->pageScaleDelta());
WebTransformationMatrix scrollCompensationForThisLayer = partialLayerOriginTransform;
scrollCompensationForThisLayer.translate(scrollingLayer->scrollDelta().width(), scrollingLayer->scrollDelta().height());
scrollCompensationForThisLayer.multiply(partialLayerOriginTransform.inverse());
return scrollCompensationForThisLayer;
}
WebTransformationMatrix computeScrollCompensationMatrixForChildren(LayerChromium* currentLayer, const WebTransformationMatrix& currentParentMatrix, const WebTransformationMatrix& currentScrollCompensation)
{
return WebTransformationMatrix();
}
WebTransformationMatrix computeScrollCompensationMatrixForChildren(CCLayerImpl* layer, const WebTransformationMatrix& parentMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix)
{
if (!layer->isContainerForFixedPositionLayers() && layer->scrollDelta().isZero() && !layer->renderSurface())
return currentScrollCompensationMatrix;
WebTransformationMatrix nextScrollCompensationMatrix;
if (!layer->isContainerForFixedPositionLayers())
nextScrollCompensationMatrix = currentScrollCompensationMatrix;
if (!layer->scrollDelta().isZero()) {
WebTransformationMatrix scrollCompensationForThisLayer = computeScrollCompensationForThisLayer(layer, parentMatrix);
nextScrollCompensationMatrix.multiply(scrollCompensationForThisLayer);
}
if (layer->renderSurface() && !nextScrollCompensationMatrix.isIdentity())
nextScrollCompensationMatrix = layer->renderSurface()->drawTransform().inverse() * nextScrollCompensationMatrix * layer->renderSurface()->drawTransform();
return nextScrollCompensationMatrix;
}
template<typename LayerType, typename LayerList>
void setupRootLayerAndSurfaceForRecursion(LayerType* rootLayer, LayerList& renderSurfaceLayerList, const IntSize& deviceViewportSize)
{
if (!rootLayer->renderSurface())
rootLayer->createRenderSurface();
rootLayer->renderSurface()->setContentRect(IntRect(IntPoint::zero(), deviceViewportSize));
rootLayer->renderSurface()->clearLayerLists();
ASSERT(renderSurfaceLayerList.isEmpty());
renderSurfaceLayerList.append(rootLayer);
}
template<typename LayerType, typename LayerList, typename RenderSurfaceType, typename LayerSorter>
static void calculateDrawTransformsInternal(LayerType* layer, LayerType* rootLayer, const WebTransformationMatrix& parentMatrix,
const WebTransformationMatrix& fullHierarchyMatrix, const WebTransformationMatrix& currentScrollCompensationMatrix,
const IntRect& clipRectFromAncestor, bool ancestorClipsSubtree,
RenderSurfaceType* nearestAncestorThatMovesPixels, LayerList& renderSurfaceLayerList, LayerList& layerList,
LayerSorter* layerSorter, int maxTextureSize, float deviceScaleFactor, IntRect& drawableContentRectOfSubtree)
{
drawableContentRectOfSubtree = IntRect();
if (subtreeShouldBeSkipped(layer))
return;
IntRect clipRectForSubtree;
bool subtreeShouldBeClipped = false;
float drawOpacity = layer->opacity();
bool drawOpacityIsAnimating = layer->opacityIsAnimating();
if (layer->parent() && layer->parent()->preserves3D()) {
drawOpacity *= layer->parent()->drawOpacity();
drawOpacityIsAnimating |= layer->parent()->drawOpacityIsAnimating();
}
IntSize bounds = layer->bounds();
FloatPoint anchorPoint = layer->anchorPoint();
FloatPoint position = layer->position() - layer->scrollDelta();
float centerOffsetX = (0.5 - anchorPoint.x()) * bounds.width();
float centerOffsetY = (0.5 - anchorPoint.y()) * bounds.height();
WebTransformationMatrix layerLocalTransform;
layerLocalTransform.scale(layer->pageScaleDelta());
layerLocalTransform.translate3d(position.x() + anchorPoint.x() * bounds.width(), position.y() + anchorPoint.y() * bounds.height(), layer->anchorPointZ());
layerLocalTransform.multiply(layer->transform());
layerLocalTransform.translate3d(centerOffsetX, centerOffsetY, -layer->anchorPointZ());
WebTransformationMatrix combinedTransform = parentMatrix;
combinedTransform.multiply(layerLocalTransform);
if (layer->fixedToContainerLayer()) {
combinedTransform = currentScrollCompensationMatrix * combinedTransform;
}
WebTransformationMatrix drawTransform = combinedTransform;
drawTransform.translate(-layer->bounds().width() / 2.0, -layer->bounds().height() / 2.0);
if (!layer->contentBounds().isEmpty() && !layer->bounds().isEmpty()) {
drawTransform.scaleNonUniform(layer->bounds().width() / static_cast<double>(layer->contentBounds().width()),
layer->bounds().height() / static_cast<double>(layer->contentBounds().height()));
}
WebTransformationMatrix layerScreenSpaceTransform = fullHierarchyMatrix;
if (!layer->preserves3D())
CCMathUtil::flattenTransformTo2d(layerScreenSpaceTransform);
layerScreenSpaceTransform.multiply(drawTransform);
layer->setScreenSpaceTransform(layerScreenSpaceTransform);
bool animatingTransformToTarget = layer->transformIsAnimating();
bool animatingTransformToScreen = animatingTransformToTarget;
if (layer->parent()) {
animatingTransformToTarget |= layer->parent()->drawTransformIsAnimating();
animatingTransformToScreen |= layer->parent()->screenSpaceTransformIsAnimating();
}
FloatRect contentRect(FloatPoint(), layer->contentBounds());
WebTransformationMatrix nextHierarchyMatrix = fullHierarchyMatrix;
WebTransformationMatrix sublayerMatrix;
if (subtreeShouldRenderToSeparateSurface(layer, isScaleOrTranslation(combinedTransform))) {
if (!layer->doubleSided() && transformToParentIsKnown(layer) && isSurfaceBackFaceVisible(layer, combinedTransform))
return;
if (!layer->renderSurface())
layer->createRenderSurface();
RenderSurfaceType* renderSurface = layer->renderSurface();
renderSurface->clearLayerLists();
renderSurface->setDrawTransform(drawTransform);
WebTransformationMatrix layerDrawTransform;
layerDrawTransform.scale(deviceScaleFactor);
if (!layer->contentBounds().isEmpty() && !layer->bounds().isEmpty()) {
layerDrawTransform.scaleNonUniform(layer->bounds().width() / static_cast<double>(layer->contentBounds().width()),
layer->bounds().height() / static_cast<double>(layer->contentBounds().height()));
}
layer->setDrawTransform(layerDrawTransform);
sublayerMatrix.makeIdentity();
sublayerMatrix.scale(deviceScaleFactor);
sublayerMatrix.translate(0.5 * bounds.width(), 0.5 * bounds.height());
renderSurface->setDrawOpacity(drawOpacity);
renderSurface->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
layer->setDrawOpacity(1);
layer->setDrawOpacityIsAnimating(false);
renderSurface->setTargetSurfaceTransformsAreAnimating(animatingTransformToTarget);
renderSurface->setScreenSpaceTransformsAreAnimating(animatingTransformToScreen);
animatingTransformToTarget = false;
layer->setDrawTransformIsAnimating(animatingTransformToTarget);
layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
nextHierarchyMatrix.multiply(renderSurface->drawTransform());
subtreeShouldBeClipped = false;
if (layer->maskLayer())
layer->maskLayer()->setRenderTarget(layer);
if (layer->replicaLayer() && layer->replicaLayer()->maskLayer())
layer->replicaLayer()->maskLayer()->setRenderTarget(layer);
if (layer->filters().hasFilterThatMovesPixels())
nearestAncestorThatMovesPixels = renderSurface;
renderSurface->setNearestAncestorThatMovesPixels(nearestAncestorThatMovesPixels);
renderSurfaceLayerList.append(layer);
} else {
layer->setDrawTransform(drawTransform);
layer->setDrawTransformIsAnimating(animatingTransformToTarget);
layer->setScreenSpaceTransformIsAnimating(animatingTransformToScreen);
sublayerMatrix = combinedTransform;
layer->setDrawOpacity(drawOpacity);
layer->setDrawOpacityIsAnimating(drawOpacityIsAnimating);
if (layer != rootLayer) {
ASSERT(layer->parent());
layer->clearRenderSurface();
subtreeShouldBeClipped = ancestorClipsSubtree;
if (ancestorClipsSubtree)
clipRectForSubtree = clipRectFromAncestor;
layer->setRenderTarget(layer->parent()->renderTarget());
} else {
ASSERT(!layer->parent());
ASSERT(layer->renderSurface());
ASSERT(ancestorClipsSubtree);
layer->renderSurface()->setClipRect(clipRectFromAncestor);
subtreeShouldBeClipped = false;
}
}
IntRect rectInTargetSpace = enclosingIntRect(CCMathUtil::mapClippedRect(layer->drawTransform(), contentRect));
if (layerClipsSubtree(layer)) {
subtreeShouldBeClipped = true;
if (ancestorClipsSubtree && !layer->renderSurface()) {
clipRectForSubtree = clipRectFromAncestor;
clipRectForSubtree.intersect(rectInTargetSpace);
} else
clipRectForSubtree = rectInTargetSpace;
}
if (!layer->preserves3D())
CCMathUtil::flattenTransformTo2d(sublayerMatrix);
sublayerMatrix.multiply(layer->sublayerTransform());
sublayerMatrix.translate3d(-bounds.width() * 0.5, -bounds.height() * 0.5, 0);
LayerList& descendants = (layer->renderSurface() ? layer->renderSurface()->layerList() : layerList);
unsigned sortingStartIndex = descendants.size();
if (!layerShouldBeSkipped(layer))
descendants.append(layer);
WebTransformationMatrix nextScrollCompensationMatrix = computeScrollCompensationMatrixForChildren(layer, parentMatrix, currentScrollCompensationMatrix);;
IntRect accumulatedDrawableContentRectOfChildren;
for (size_t i = 0; i < layer->children().size(); ++i) {
LayerType* child = CCLayerTreeHostCommon::getChildAsRawPtr(layer->children(), i);
IntRect drawableContentRectOfChildSubtree;
calculateDrawTransformsInternal<LayerType, LayerList, RenderSurfaceType, LayerSorter>(child, rootLayer, sublayerMatrix, nextHierarchyMatrix, nextScrollCompensationMatrix,
clipRectForSubtree, subtreeShouldBeClipped, nearestAncestorThatMovesPixels,
renderSurfaceLayerList, descendants, layerSorter, maxTextureSize, deviceScaleFactor, drawableContentRectOfChildSubtree);
if (!drawableContentRectOfChildSubtree.isEmpty()) {
accumulatedDrawableContentRectOfChildren.unite(drawableContentRectOfChildSubtree);
if (child->renderSurface())
descendants.append(child);
}
}
IntRect localDrawableContentRectOfSubtree = accumulatedDrawableContentRectOfChildren;
if (layer->drawsContent())
localDrawableContentRectOfSubtree.unite(rectInTargetSpace);
if (subtreeShouldBeClipped)
localDrawableContentRectOfSubtree.intersect(clipRectForSubtree);
IntRect drawableContentRectOfLayer = rectInTargetSpace;
if (subtreeShouldBeClipped)
drawableContentRectOfLayer.intersect(clipRectForSubtree);
layer->setDrawableContentRect(drawableContentRectOfLayer);
if (layer->renderSurface() && layer != rootLayer) {
RenderSurfaceType* renderSurface = layer->renderSurface();
IntRect clippedContentRect = localDrawableContentRectOfSubtree;
if (ancestorClipsSubtree)
renderSurface->setClipRect(clipRectFromAncestor);
else
renderSurface->setClipRect(IntRect());
if (!layer->replicaLayer() && transformToParentIsKnown(layer)) {
if (ancestorClipsSubtree && !clippedContentRect.isEmpty()) {
IntRect surfaceClipRect = CCLayerTreeHostCommon::calculateVisibleRect(renderSurface->clipRect(), clippedContentRect, renderSurface->drawTransform());
clippedContentRect.intersect(surfaceClipRect);
}
}
clippedContentRect.setWidth(std::min(clippedContentRect.width(), maxTextureSize));
clippedContentRect.setHeight(std::min(clippedContentRect.height(), maxTextureSize));
if (clippedContentRect.isEmpty())
renderSurface->clearLayerLists();
renderSurface->setContentRect(clippedContentRect);
renderSurface->setScreenSpaceTransform(layer->screenSpaceTransform());
if (layer->replicaLayer()) {
WebTransformationMatrix surfaceOriginToReplicaOriginTransform;
surfaceOriginToReplicaOriginTransform.scale(deviceScaleFactor);
surfaceOriginToReplicaOriginTransform.translate(layer->replicaLayer()->position().x() + layer->replicaLayer()->anchorPoint().x() * bounds.width(),
layer->replicaLayer()->position().y() + layer->replicaLayer()->anchorPoint().y() * bounds.height());
surfaceOriginToReplicaOriginTransform.multiply(layer->replicaLayer()->transform());
surfaceOriginToReplicaOriginTransform.translate(-layer->replicaLayer()->anchorPoint().x() * bounds.width(), -layer->replicaLayer()->anchorPoint().y() * bounds.height());
surfaceOriginToReplicaOriginTransform.scale(1 / deviceScaleFactor);
WebTransformationMatrix replicaOriginTransform = layer->renderSurface()->drawTransform() * surfaceOriginToReplicaOriginTransform;
renderSurface->setReplicaDrawTransform(replicaOriginTransform);
WebTransformationMatrix replicaScreenSpaceTransform = layer->renderSurface()->screenSpaceTransform() * surfaceOriginToReplicaOriginTransform;
renderSurface->setReplicaScreenSpaceTransform(replicaScreenSpaceTransform);
}
if (!layer->renderSurface()->layerList().size()) {
while (renderSurfaceLayerList.last() != layer) {
renderSurfaceLayerList.last()->clearRenderSurface();
renderSurfaceLayerList.removeLast();
}
ASSERT(renderSurfaceLayerList.last() == layer);
renderSurfaceLayerList.removeLast();
layer->clearRenderSurface();
return;
}
}
if (sortingStartIndex == descendants.size())
return;
if (descendants.size() && layer->preserves3D() && (!layer->parent() || !layer->parent()->preserves3D()))
sortLayers(&descendants.at(sortingStartIndex), descendants.end(), layerSorter);
if (layer->renderSurface())
drawableContentRectOfSubtree = enclosingIntRect(layer->renderSurface()->drawableContentRect());
else
drawableContentRectOfSubtree = localDrawableContentRectOfSubtree;
if (layer->hasContributingDelegatedRenderPasses())
layer->renderTarget()->renderSurface()->addContributingDelegatedRenderPassLayer(layer);
}
template<typename LayerType, typename LayerList, typename RenderSurfaceType>
static void calculateVisibleRectsInternal(const LayerList& renderSurfaceLayerList)
{
typedef CCLayerIterator<LayerType, LayerList, RenderSurfaceType, CCLayerIteratorActions::BackToFront> CCLayerIteratorType;
CCLayerIteratorType end = CCLayerIteratorType::end(&renderSurfaceLayerList);
for (CCLayerIteratorType it = CCLayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
if (it.representsTargetRenderSurface()) {
LayerType* maskLayer = it->maskLayer();
if (maskLayer)
maskLayer->setVisibleContentRect(IntRect(IntPoint(), it->contentBounds()));
LayerType* replicaMaskLayer = it->replicaLayer() ? it->replicaLayer()->maskLayer() : 0;
if (replicaMaskLayer)
replicaMaskLayer->setVisibleContentRect(IntRect(IntPoint(), it->contentBounds()));
} else if (it.representsItself()) {
IntRect visibleContentRect = calculateVisibleContentRect(*it);
it->setVisibleContentRect(visibleContentRect);
}
}
}
void CCLayerTreeHostCommon::calculateDrawTransforms(LayerChromium* rootLayer, const IntSize& deviceViewportSize, float deviceScaleFactor, int maxTextureSize, Vector<RefPtr<LayerChromium> >& renderSurfaceLayerList)
{
IntRect totalDrawableContentRect;
WebTransformationMatrix identityMatrix;
WebTransformationMatrix deviceScaleTransform;
deviceScaleTransform.scale(deviceScaleFactor);
setupRootLayerAndSurfaceForRecursion<LayerChromium, Vector<RefPtr<LayerChromium> > >(rootLayer, renderSurfaceLayerList, deviceViewportSize);
cc::calculateDrawTransformsInternal<LayerChromium, Vector<RefPtr<LayerChromium> >, RenderSurfaceChromium, void>(rootLayer, rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
rootLayer->renderSurface()->contentRect(), true, 0, renderSurfaceLayerList,
rootLayer->renderSurface()->layerList(), 0, maxTextureSize, deviceScaleFactor, totalDrawableContentRect);
}
void CCLayerTreeHostCommon::calculateDrawTransforms(CCLayerImpl* rootLayer, const IntSize& deviceViewportSize, float deviceScaleFactor, CCLayerSorter* layerSorter, int maxTextureSize, Vector<CCLayerImpl*>& renderSurfaceLayerList)
{
IntRect totalDrawableContentRect;
WebTransformationMatrix identityMatrix;
WebTransformationMatrix deviceScaleTransform;
deviceScaleTransform.scale(deviceScaleFactor);
setupRootLayerAndSurfaceForRecursion<CCLayerImpl, Vector<CCLayerImpl*> >(rootLayer, renderSurfaceLayerList, deviceViewportSize);
cc::calculateDrawTransformsInternal<CCLayerImpl, Vector<CCLayerImpl*>, CCRenderSurface, CCLayerSorter>(rootLayer, rootLayer, deviceScaleTransform, identityMatrix, identityMatrix,
rootLayer->renderSurface()->contentRect(), true, 0, renderSurfaceLayerList,
rootLayer->renderSurface()->layerList(), layerSorter, maxTextureSize, deviceScaleFactor, totalDrawableContentRect);
}
void CCLayerTreeHostCommon::calculateVisibleRects(Vector<RefPtr<LayerChromium> >& renderSurfaceLayerList)
{
calculateVisibleRectsInternal<LayerChromium, Vector<RefPtr<LayerChromium> >, RenderSurfaceChromium>(renderSurfaceLayerList);
}
void CCLayerTreeHostCommon::calculateVisibleRects(Vector<CCLayerImpl*>& renderSurfaceLayerList)
{
calculateVisibleRectsInternal<CCLayerImpl, Vector<CCLayerImpl*>, CCRenderSurface>(renderSurfaceLayerList);
}
static bool pointHitsRect(const IntPoint& viewportPoint, const WebTransformationMatrix& localSpaceToScreenSpaceTransform, FloatRect localSpaceRect)
{
if (!localSpaceToScreenSpaceTransform.isInvertible())
return false;
bool clipped = false;
FloatPoint hitTestPointInLocalSpace = CCMathUtil::projectPoint(localSpaceToScreenSpaceTransform.inverse(), FloatPoint(viewportPoint), clipped);
if (clipped)
return false;
return localSpaceRect.contains(hitTestPointInLocalSpace);
}
static bool pointIsClippedBySurfaceOrClipRect(const IntPoint& viewportPoint, CCLayerImpl* layer)
{
CCLayerImpl* currentLayer = layer;
while (currentLayer) {
if (currentLayer->renderSurface() && !pointHitsRect(viewportPoint, currentLayer->renderSurface()->screenSpaceTransform(), currentLayer->renderSurface()->contentRect()))
return true;
CCLayerImpl* renderTarget = currentLayer->renderTarget();
if (layerClipsSubtree(currentLayer) && !pointHitsRect(viewportPoint, renderTarget->renderSurface()->screenSpaceTransform(), currentLayer->drawableContentRect()))
return true;
currentLayer = currentLayer->parent();
}
return false;
}
CCLayerImpl* CCLayerTreeHostCommon::findLayerThatIsHitByPoint(const IntPoint& viewportPoint, Vector<CCLayerImpl*>& renderSurfaceLayerList)
{
CCLayerImpl* foundLayer = 0;
typedef CCLayerIterator<CCLayerImpl, Vector<CCLayerImpl*>, CCRenderSurface, CCLayerIteratorActions::FrontToBack> CCLayerIteratorType;
CCLayerIteratorType end = CCLayerIteratorType::end(&renderSurfaceLayerList);
for (CCLayerIteratorType it = CCLayerIteratorType::begin(&renderSurfaceLayerList); it != end; ++it) {
if (!it.representsItself())
continue;
CCLayerImpl* currentLayer = (*it);
FloatRect contentRect(FloatPoint::zero(), currentLayer->contentBounds());
if (!pointHitsRect(viewportPoint, currentLayer->screenSpaceTransform(), contentRect))
continue;
if (pointIsClippedBySurfaceOrClipRect(viewportPoint, currentLayer))
continue;
foundLayer = currentLayer;
break;
}
return foundLayer;
}
}