Commit 9b55e054 authored by fs's avatar fs Committed by Commit bot

Fix clip-path reference box/coordinate space setup for hit-testing

Synchronize hit-testing code with rendering code with regards to how the
coordinate space is handled (for userSpaceOnUse <clipPath>s), and how
the reference boxes is computed.

Also "straighten" out code-flow a bit while here.

BUG=418484
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:linux_layout_tests_slimming_paint_v2

Review-Url: https://codereview.chromium.org/2280963002
Cr-Commit-Position: refs/heads/master@{#414730}
parent 4c8cbb22
<!DOCTYPE html>
<title>Hit-test of clip-path userSpaceOnUse &lt;clipPath> on &lt;div></title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<style>
body {
margin: 0;
}
.box {
width: 100px;
height: 100px;
background-color: blue;
margin: 100px;
-webkit-clip-path: url(#clip);
clip-path: url(#clip);
}
</style>
<div class=box></div>
<svg height="0">
<clipPath id="clip" clipPathUnits="userSpaceOnUse">
<polygon points="50,0 100,50 50,100 0,50"/>
</clipPath>
</svg>
<script>
function assert_element_at(element, pointlist) {
for (var point of pointlist) {
var result = document.elementFromPoint(point[0], point[1]);
assert_equals(result, element, point.join(','));
}
}
test(function() {
var div = document.querySelector('.box');
// Points inside clip-path.
assert_element_at(div, [[150, 150], [150, 125], [150, 175], [125, 150], [175, 150]]);
// Points outside clip-path.
assert_element_at(document.body, [[120, 120], [180, 120], [120, 180], [180, 180]]);
});
</script>
<!DOCTYPE html>
<title>Hit-test of clip-path polygon on &lt;div> with box-shadow</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<style>
body {
margin: 0;
}
.box {
width: 100px;
height: 100px;
background-color: blue;
box-shadow: -100px 0px red;
-webkit-clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}
</style>
<div class=box></div>
<script>
function assert_element_at(element, pointlist) {
for (var point of pointlist) {
var result = document.elementFromPoint(point[0], point[1]);
assert_equals(result, element, point.join(','));
}
}
test(function() {
var div = document.querySelector('.box');
// Points inside clip-path.
assert_element_at(div, [[50, 50], [50, 25], [50, 75], [25, 50], [75, 50]]);
// Points outside clip-path.
assert_element_at(document.body, [[20, 20], [80, 20], [20, 80], [80, 80]]);
});
</script>
......@@ -2057,35 +2057,48 @@ PaintLayer* PaintLayer::hitTestChildren(ChildrenIteration childrentoVisit, Paint
return resultLayer;
}
LayoutRect PaintLayer::boxForClipPath() const
{
if (!layoutObject()->isBox()) {
SECURITY_DCHECK(layoutObject()->isLayoutInline());
return toLayoutInline(layoutObject())->linesBoundingBox();
}
return toLayoutBox(layoutObject())->borderBoxRect();
}
bool PaintLayer::hitTestClippedOutByClipPath(PaintLayer* rootLayer, const HitTestLocation& hitTestLocation) const
{
if (!layoutObject()->hasClipPath())
return false;
ASSERT(isSelfPaintingLayer());
DCHECK(isSelfPaintingLayer());
DCHECK(rootLayer);
LayoutPoint offsetToRootLayer;
if (rootLayer)
convertToLayerCoords(rootLayer, offsetToRootLayer);
LayoutRect rootRelativeBounds = physicalBoundingBoxIncludingReflectionAndStackingChildren(offsetToRootLayer);
convertToLayerCoords(rootLayer, offsetToRootLayer);
LayoutRect referenceBox(boxForClipPath());
referenceBox.moveBy(offsetToRootLayer);
FloatPoint point(hitTestLocation.point());
ClipPathOperation* clipPathOperation = layoutObject()->style()->clipPath();
ASSERT(clipPathOperation);
DCHECK(clipPathOperation);
if (clipPathOperation->type() == ClipPathOperation::SHAPE) {
ShapeClipPathOperation* clipPath = toShapeClipPathOperation(clipPathOperation);
if (!clipPath->path(FloatRect(rootRelativeBounds)).contains(FloatPoint(hitTestLocation.point())))
return true;
} else {
ASSERT(clipPathOperation->type() == ClipPathOperation::REFERENCE);
ReferenceClipPathOperation* referenceClipPathOperation = toReferenceClipPathOperation(clipPathOperation);
Element* element = layoutObject()->document().getElementById(referenceClipPathOperation->fragment());
if (isSVGClipPathElement(element) && element->layoutObject()) {
LayoutSVGResourceClipper* clipper = toLayoutSVGResourceClipper(toLayoutSVGResourceContainer(element->layoutObject()));
if (!clipper->hitTestClipContent(FloatRect(rootRelativeBounds), FloatPoint(hitTestLocation.point())))
return true;
}
return !clipPath->path(FloatRect(referenceBox)).contains(point);
}
return false;
DCHECK_EQ(clipPathOperation->type(), ClipPathOperation::REFERENCE);
ReferenceClipPathOperation* referenceClipPathOperation = toReferenceClipPathOperation(clipPathOperation);
Element* element = layoutObject()->document().getElementById(referenceClipPathOperation->fragment());
if (!isSVGClipPathElement(element) || !element->layoutObject())
return false;
LayoutSVGResourceClipper* clipper =
toLayoutSVGResourceClipper(toLayoutSVGResourceContainer(element->layoutObject()));
// If the clipPath is using "userspace on use" units, then the origin of
// the coordinate system is the top-left of the reference box, so adjust
// the point accordingly.
if (clipper->clipPathUnits() == SVGUnitTypes::kSvgUnitTypeUserspaceonuse)
point.moveBy(-offsetToRootLayer);
return !clipper->hitTestClipContent(FloatRect(referenceBox), point);
}
bool PaintLayer::intersectsDamageRect(const LayoutRect& layerBounds, const LayoutRect& damageRect, const LayoutPoint& offsetFromRoot) const
......
......@@ -373,6 +373,8 @@ public:
LayoutRect physicalBoundingBoxIncludingReflectionAndStackingChildren(const LayoutPoint& offsetFromRoot, CalculateBoundsOptions = MaybeIncludeTransformForAncestorLayer) const;
LayoutRect fragmentsBoundingBox(const PaintLayer* ancestorLayer) const;
LayoutRect boxForClipPath() const;
LayoutRect boundingBoxForCompositingOverlapTest() const;
// If true, this layer's children are included in its bounds for overlap testing.
......
......@@ -181,14 +181,6 @@ static bool shouldRepaintSubsequence(PaintLayer& paintLayer, const PaintLayerPai
return needsRepaint;
}
static LayoutRect computeReferenceBox(const LayoutBoxModelObject& boxModelObject)
{
if (boxModelObject.isLayoutInline())
return toLayoutInline(boxModelObject).linesBoundingBox();
SECURITY_DCHECK(boxModelObject.isBox());
return toLayoutBox(boxModelObject).borderBoxRect();
}
PaintLayerPainter::PaintResult PaintLayerPainter::paintLayerContents(GraphicsContext& context, const PaintLayerPaintingInfo& paintingInfoArg, PaintLayerFlags paintFlags, FragmentPolicy fragmentPolicy)
{
ASSERT(m_paintLayer.isSelfPaintingLayer() || m_paintLayer.hasSelfPaintingLayerDescendant());
......@@ -262,7 +254,8 @@ PaintLayerPainter::PaintResult PaintLayerPainter::paintLayerContents(GraphicsCon
rootRelativeBoundsComputed = true;
}
paintingInfo.ancestorHasClipPathClipping = true;
LayoutRect referenceBox(computeReferenceBox(*m_paintLayer.layoutObject()));
LayoutRect referenceBox(m_paintLayer.boxForClipPath());
referenceBox.moveBy(offsetFromRoot);
clipPathClipper.emplace(
context, *m_paintLayer.layoutObject(), FloatRect(referenceBox), FloatRect(rootRelativeBounds), FloatPoint(offsetFromRoot));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment