Skip to content

Commit

Permalink
[motion-path] Refactor various parts of path operation and RenderStyle
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=260818
rdar://114584136

Reviewed by Simon Fraser.

This refactoring addresses a few issues with the current motion path design. First,
for paths that need additional data including its containing block rect or the element's
offset from its containing block, it is not correct to use the path operation to hold
these values, as the path operation is supposed to represent just the path specified by the
css. Instead, pass down these necessary values through a TransformOperationData struct.
Also added a test for this case to ensure that style sharing is not broken after this change.
Also, factor out some of the code related to computing the ray path into the new MotionPath
class, which now also includes applyMotionPathTransform.

* LayoutTests/TestExpectations:
* LayoutTests/imported/w3c/web-platform-tests/css/motion/offset-path-ray-019-expected.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/css/motion/offset-path-ray-019.html: Added.
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/animation/KeyframeEffect.cpp:
(WebCore::KeyframeEffect::computeTransformedExtentViaMatrix const):
* Source/WebCore/css/ComputedStyleExtractor.cpp:
(WebCore::computedTransform):
* Source/WebCore/platform/animation/AcceleratedEffectValues.cpp:
(WebCore::AcceleratedEffectValues::AcceleratedEffectValues):
* Source/WebCore/rendering/PathOperation.cpp:
(WebCore::RayPathOperation::create):
(WebCore::RayPathOperation::clone const):
(WebCore::RayPathOperation::getPath const):
(WebCore::RayPathOperation::lengthForPath const): Deleted.
(WebCore::RayPathOperation::lengthForContainPath const): Deleted.
* Source/WebCore/rendering/PathOperation.h:
(WebCore::PathOperation::getPath): Deleted.
* Source/WebCore/rendering/RenderBlockFlow.cpp:
* Source/WebCore/rendering/RenderBox.cpp:
(WebCore::RenderBox::applyTransform const):
* Source/WebCore/rendering/RenderLayer.cpp:
(WebCore::RenderLayer::updateTransform):
(WebCore::RenderLayer::setReferenceBoxForPathOperations): Deleted.
* Source/WebCore/rendering/RenderLayer.h:
* Source/WebCore/rendering/RenderLayerModelObject.cpp:
(WebCore::RenderLayerModelObject::applySVGTransform const):
* Source/WebCore/rendering/style/RenderStyle.cpp:
(WebCore::RenderStyle::applyTransform const):
(WebCore::RenderStyle::applyCSSTransform const):
(WebCore::getTraversalStateAtDistance): Deleted.
(WebCore::RenderStyle::applyMotionPathTransform const): Deleted.
* Source/WebCore/rendering/style/RenderStyle.h:
* Source/WebCore/svg/SVGGraphicsElement.cpp:
(WebCore::SVGGraphicsElement::animatedLocalTransform const):
* Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in:

Canonical link: https://commits.webkit.org/267416@main
  • Loading branch information
nmoucht committed Aug 29, 2023
1 parent cb4faa2 commit 3f5059d
Show file tree
Hide file tree
Showing 23 changed files with 379 additions and 191 deletions.
1 change: 1 addition & 0 deletions LayoutTests/TestExpectations
Original file line number Diff line number Diff line change
Expand Up @@ -5282,6 +5282,7 @@ webkit.org/b/233340 imported/w3c/web-platform-tests/css/motion/offset-anchor-tra

# CSS motion path ray test failing due to getting wrong size from containing block.
webkit.org/b/233344 imported/w3c/web-platform-tests/css/motion/offset-path-ray-007.html [ ImageOnlyFailure ]
webkit.org/b/233344 imported/w3c/web-platform-tests/css/motion/offset-path-ray-009.html [ ImageOnlyFailure ]

# CSS motion path URL support.
imported/w3c/web-platform-tests/css/motion/offset-path-url-002.html [ ImageOnlyFailure ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Motion Path: ray paths</title>
<style>
#container1 {
width: 100px;
height: 100px;
}
#container2 {
width: 200px;
height: 200px;
}
#target1 {
position: relative;
width: 50px;
height: 50px;
background-color: lime;
transform-origin: 0px 0px;
transform: translateX(100px);
}
#target2 {
position: relative;
width: 50px;
height: 50px;
background-color: lime;
transform-origin: 0px 0px;
transform: translateX(200px);
}
</style>
</head>
<body>
<div id="container1">
<div id="target1"></div>
</div>
<div id="container2">
<div id="target2"></div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<title>CSS Motion Path: ray paths</title>
<link rel="help" href="https://drafts.fxtf.org/motion-1/#offset-path-property">
<link rel="match" href="offset-path-ray-007-ref.html">
<meta name="assert" content="Tests ray() when sharing style with different sized containing blocks.">
<style>
#container1 {
width: 100px;
height: 100px;
}
#container2 {
width: 200px;
height: 200px;
}
#target {
position: relative;
width: 50px;
height: 50px;
background-color: lime;
transform-origin: 0px 0px;
offset-path: ray(90deg sides);
offset-distance: 100%;
offset-position: auto;
}
</style>
</head>
<body>
<div id="container1">
<div id="target"></div>
</div>
<div id="container2">
<div id="target"></div>
</div>

</body>
</html>
1 change: 1 addition & 0 deletions Source/WebCore/Headers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
rendering/LegacyLineLayout.h
rendering/LegacyRootInlineBox.h
rendering/MarkedText.h
rendering/MotionPath.h
rendering/OrderIterator.h
rendering/OverlapTestRequestClient.h
rendering/Pagination.h
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2541,6 +2541,7 @@ rendering/LegacyLineLayout.cpp
rendering/LegacyRootInlineBox.cpp
rendering/LegacyInlineTextBox.cpp
rendering/MarkedText.cpp
rendering/MotionPath.cpp
rendering/OrderIterator.cpp
rendering/PathOperation.cpp
rendering/PointerEventsHitRules.cpp
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@
1A85B2B70A1B2AC700D8C87C /* JSHTMLDivElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A85B2B50A1B2AC700D8C87C /* JSHTMLDivElement.h */; };
1A874ADF19085E9100B03171 /* WAKViewInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A874ADE19085E9100B03171 /* WAKViewInternal.h */; };
1A88A90517553CD7000C74F9 /* FileIconLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A88A90317553CD7000C74F9 /* FileIconLoader.h */; settings = {ATTRIBUTES = (Private, ); }; };
1A897BC32A903B1D00C12DAA /* MotionPath.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A897BC12A903B1C00C12DAA /* MotionPath.h */; settings = {ATTRIBUTES = (Private, ); }; };
1A8A643A1D19FC5300D0E00F /* Payment.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A8A64371D19FC5300D0E00F /* Payment.h */; settings = {ATTRIBUTES = (Private, ); }; };
1A8A643B1D19FC5300D0E00F /* PaymentContact.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A8A64381D19FC5300D0E00F /* PaymentContact.h */; settings = {ATTRIBUTES = (Private, ); }; };
1A8A643C1D19FC5300D0E00F /* PaymentMethod.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A8A64391D19FC5300D0E00F /* PaymentMethod.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -7521,6 +7522,8 @@
1A874ADE19085E9100B03171 /* WAKViewInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WAKViewInternal.h; sourceTree = "<group>"; };
1A88A90217553CD7000C74F9 /* FileIconLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FileIconLoader.cpp; sourceTree = "<group>"; };
1A88A90317553CD7000C74F9 /* FileIconLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileIconLoader.h; sourceTree = "<group>"; };
1A897BC12A903B1C00C12DAA /* MotionPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MotionPath.h; sourceTree = "<group>"; };
1A897BC22A903B1D00C12DAA /* MotionPath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MotionPath.cpp; sourceTree = "<group>"; };
1A8A64371D19FC5300D0E00F /* Payment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Payment.h; sourceTree = "<group>"; };
1A8A64381D19FC5300D0E00F /* PaymentContact.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PaymentContact.h; sourceTree = "<group>"; };
1A8A64391D19FC5300D0E00F /* PaymentMethod.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PaymentMethod.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -35328,6 +35331,8 @@
9BA273F3172206BB0097CE47 /* LogicalSelectionOffsetCaches.h */,
CE1866421F72E5B400A0CAB6 /* MarkedText.cpp */,
CE1866431F72E5B400A0CAB6 /* MarkedText.h */,
1A897BC22A903B1D00C12DAA /* MotionPath.cpp */,
1A897BC12A903B1C00C12DAA /* MotionPath.h */,
CDE7FC42181904B1002BBB77 /* OrderIterator.cpp */,
CDE7FC43181904B1002BBB77 /* OrderIterator.h */,
3774ABA30FA21EB400AD7DE9 /* OverlapTestRequestClient.h */,
Expand Down Expand Up @@ -40000,6 +40005,7 @@
E35188B425D387CF00A77AF3 /* ModuleScriptLoaderClient.h in Headers */,
F55B3DC61251F12D003EF269 /* MonthInputType.h in Headers */,
E39628C12395743100658ECD /* MotionManagerClient.h in Headers */,
1A897BC32A903B1D00C12DAA /* MotionPath.h in Headers */,
85031B460A44EFC700F992E0 /* MouseEvent.h in Headers */,
83765F951DAC522F00C06537 /* MouseEventInit.h in Headers */,
935C476309AC4CE600A6AAB4 /* MouseEventWithHitTestResults.h in Headers */,
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/animation/KeyframeEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "KeyframeEffectStack.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "MotionPath.h"
#include "MutableStyleProperties.h"
#include "PropertyAllowlist.h"
#include "RenderBox.h"
Expand Down Expand Up @@ -2314,7 +2315,7 @@ bool KeyframeEffect::computeTransformedExtentViaTransformList(const FloatRect& r
bool KeyframeEffect::computeTransformedExtentViaMatrix(const FloatRect& rendererBox, const RenderStyle& style, LayoutRect& bounds) const
{
TransformationMatrix transform;
style.applyTransform(transform, rendererBox);
style.applyTransform(transform, TransformOperationData(rendererBox, renderer()));
if (!transform.isAffine())
return false;

Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/css/ComputedStyleExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "FontCascade.h"
#include "FontSelectionValueInlines.h"
#include "GridPositionsResolver.h"
#include "MotionPath.h"
#include "NodeRenderStyle.h"
#include "PerspectiveTransformOperation.h"
#include "QuotesData.h"
Expand Down Expand Up @@ -846,7 +847,7 @@ static Ref<CSSValue> computedTransform(RenderElement* renderer, const RenderStyl

if (renderer) {
TransformationMatrix transform;
style.applyTransform(transform, renderer->transformReferenceBoxRect(style), { });
style.applyTransform(transform, TransformOperationData(renderer->transformReferenceBoxRect(style), renderer), { });
return CSSTransformListValue::create(matrixTransformValue(transform, style));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "IntSize.h"
#include "LengthFunctions.h"
#include "MotionPath.h"
#include "Path.h"
#include "RenderStyleInlines.h"

Expand Down Expand Up @@ -165,7 +166,7 @@ AcceleratedEffectValues::AcceleratedEffectValues(const RenderStyle& style, const
if (!offsetAnchor.x().isAuto())
anchor = floatPointForLengthPoint(offsetAnchor, borderBoxRect.size()) + borderBoxRect.location();

auto path = offsetPath->getPath(borderBoxRect);
auto path = offsetPath->getPath(FloatRect(borderBoxRect));
offsetDistance = { path ? path->length() : 0.0f, LengthType:: Fixed };
}

Expand Down
161 changes: 161 additions & 0 deletions Source/WebCore/rendering/MotionPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "MotionPath.h"

#include "GeometryUtilities.h"
#include "PathOperation.h"
#include "PathTraversalState.h"

namespace WebCore {

std::optional<MotionPathData> MotionPath::motionPathDataForRenderer(const RenderElement& renderer)
{
MotionPathData data;
auto pathOperation = renderer.style().offsetPath();
if (!pathOperation || !is<RenderLayerModelObject>(renderer))
return std::nullopt;
if (is<BoxPathOperation>(pathOperation)) {
auto& boxPathOperation = downcast<BoxPathOperation>(*pathOperation);
data.containingBlockBoundingRect = snapRectToDevicePixelsIfNeeded(renderer.referenceBoxRect(boxPathOperation.referenceBox()), downcast<RenderLayerModelObject>(renderer));
return data;
}
if (is<RayPathOperation>(pathOperation)) {
if (auto* parentBlock = renderer.containingBlock()) {
auto pathReferenceBoxRect = snapRectToDevicePixelsIfNeeded(parentBlock->transformReferenceBoxRect(parentBlock->style()), downcast<RenderLayerModelObject>(renderer));
data.containingBlockBoundingRect = pathReferenceBoxRect;
auto left = renderer.style().left();
auto top = renderer.style().top();
data.offsetFromContainingBlock = FloatPoint(left.isPercent() ? left.value() / 100 * pathReferenceBoxRect.width() : left.value(), top.isPercent() ? top.value() / 100 * pathReferenceBoxRect.height() : top.value());
return data;
}
}
return std::nullopt;
}

static PathTraversalState traversalStateAtDistance(const Path& path, const Length& distance)
{
auto pathLength = path.length();
auto distanceValue = floatValueForLength(distance, pathLength);

float resolvedLength = 0;
if (path.isClosed()) {
if (pathLength) {
resolvedLength = fmod(distanceValue, pathLength);
if (resolvedLength < 0)
resolvedLength += pathLength;
}
} else
resolvedLength = clampTo<float>(distanceValue, 0, pathLength);

ASSERT(resolvedLength >= 0);
return path.traversalStateAtLength(resolvedLength);
}

void MotionPath::applyMotionPathTransform(const RenderStyle& style, const TransformOperationData& transformData, TransformationMatrix& transform)
{
if (!style.offsetPath())
return;

auto& boundingBox = transformData.boundingBox();
auto transformOrigin = style.computeTransformOrigin(boundingBox).xy();
auto anchor = transformOrigin;
if (!style.offsetAnchor().x().isAuto())
anchor = floatPointForLengthPoint(style.offsetAnchor(), boundingBox.size()) + boundingBox.location();

// Shift element to the point on path specified by offset-path and offset-distance.
auto path = style.offsetPath()->getPath(transformData);
if (!path)
return;
auto traversalState = traversalStateAtDistance(*path, style.offsetDistance());
transform.translate(traversalState.current().x(), traversalState.current().y());

// Shift element to the anchor specified by offset-anchor.
transform.translate(-anchor.x(), -anchor.y());

auto shiftToOrigin = anchor - transformOrigin;
transform.translate(shiftToOrigin.width(), shiftToOrigin.height());

// Apply rotation.
auto rotation = style.offsetRotate();
if (rotation.hasAuto())
transform.rotate(traversalState.normalAngle() + rotation.angle());
else
transform.rotate(rotation.angle());

transform.translate(-shiftToOrigin.width(), -shiftToOrigin.height());
}

bool MotionPath::needsUpdateAfterContainingBlockLayout(const PathOperation& pathOperation)
{
return is<RayPathOperation>(pathOperation) || is<BoxPathOperation>(pathOperation);
}

double MotionPath::lengthForRayPath(const RayPathOperation& rayPathOperation, const MotionPathData& data)
{
auto& boundingBox = data.containingBlockBoundingRect;
auto distances = distanceOfPointToSidesOfRect(boundingBox, data.offsetFromContainingBlock);

switch (rayPathOperation.size()) {
case RayPathOperation::Size::ClosestSide:
return std::min( { distances.top(), distances.bottom(), distances.left(), distances.right() } );
case RayPathOperation::Size::FarthestSide:
return std::max( { distances.top(), distances.bottom(), distances.left(), distances.right() } );
case RayPathOperation::Size::FarthestCorner:
return std::hypot(std::max(distances.left(), distances.right()), std::max(distances.top(), distances.bottom()));
case RayPathOperation::Size::ClosestCorner:
return std::hypot(std::min(distances.left(), distances.right()), std::min(distances.top(), distances.bottom()));
case RayPathOperation::Size::Sides:
return lengthOfRayIntersectionWithBoundingBox(boundingBox, std::make_pair(data.offsetFromContainingBlock, rayPathOperation.angle()));
}
RELEASE_ASSERT_NOT_REACHED();
}

double MotionPath::lengthForRayContainPath(const FloatRect& elementRect, double computedPathLength)
{
return std::max(0.0, computedPathLength - (std::max(elementRect.width(), elementRect.height()) / 2));
}

std::optional<Path> MotionPath::computePathForRay(const RayPathOperation& rayPathOperation, const TransformOperationData& data)
{
auto motionPathData = data.motionPathData();
auto elementBoundingBox = data.boundingBox();
if (!motionPathData || motionPathData->containingBlockBoundingRect.isZero())
return std::nullopt;

double length = lengthForRayPath(rayPathOperation, *motionPathData);
if (rayPathOperation.isContaining())
length = lengthForRayContainPath(elementBoundingBox, length);

auto radians = deg2rad(toPositiveAngle(rayPathOperation.angle()) - 90.0);
auto point = FloatPoint(std::cos(radians) * length, std::sin(radians) * length);

Path path;
path.addLineTo(point);
return path;
}

} // namespace WebCore
Loading

0 comments on commit 3f5059d

Please sign in to comment.