1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/csspaint/nativepaint/clip_path_paint_definition.h"
#include "cc/paint/paint_recorder.h"
#include "third_party/blink/renderer/core/animation/basic_shape_interpolation_functions.h"
#include "third_party/blink/renderer/core/animation/css/compositor_keyframe_double.h"
#include "third_party/blink/renderer/core/animation/css_shape_interpolation_type.h"
#include "third_party/blink/renderer/core/animation/element_animations.h"
#include "third_party/blink/renderer/core/animation/path_interpolation_functions.h"
#include "third_party/blink/renderer/core/css/basic_shape_functions.h"
#include "third_party/blink/renderer/core/css/clip_path_paint_image_generator.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_identifier_value_mappings.h"
#include "third_party/blink/renderer/core/css/css_inherited_value.h"
#include "third_party/blink/renderer/core/css/css_initial_value.h"
#include "third_party/blink/renderer/core/css/css_revert_layer_value.h"
#include "third_party/blink/renderer/core/css/css_revert_value.h"
#include "third_party/blink/renderer/core/css/css_to_length_conversion_data.h"
#include "third_party/blink/renderer/core/css/css_unset_value.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/cssom/paint_worklet_deferred_image.h"
#include "third_party/blink/renderer/core/css/cssom/paint_worklet_input.h"
#include "third_party/blink/renderer/core/css/properties/longhands.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/paint/clip_path_clipper.h"
#include "third_party/blink/renderer/core/style/computed_style_constants.h"
#include "third_party/blink/renderer/core/style/geometry_box_clip_path_operation.h"
#include "third_party/blink/renderer/core/style/shape_clip_path_operation.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "ui/gfx/geometry/size_f.h"
namespace blink {
namespace {
// produced an 'infinite' clip rect that will ensure no content is clipped. This
// is used for the case when clip-path is none.
SkPath InfiniteClipPath() {
return SkPath::Rect(gfx::RectFToSkRect(
ClipPathPaintImageGenerator::GetAnimationBoundingRect()));
}
// This struct contains the keyframe index and the intra-keyframe progress. It
// is calculated by GetAdjustedProgress.
struct AnimationProgress {
int idx;
float adjusted_progress;
AnimationProgress(int idx, float adjusted_progress)
: idx(idx), adjusted_progress(adjusted_progress) {}
bool operator==(const AnimationProgress& other) const {
return idx == other.idx && adjusted_progress == other.adjusted_progress;
}
};
// This class includes information that is required by the compositor thread
// when painting clip path.
class ClipPathPaintWorkletInput : public PaintWorkletInput {
public:
ClipPathPaintWorkletInput(
const gfx::RectF& reference_box,
const gfx::SizeF& clip_area_size,
const gfx::PointF& reference_origin,
int worklet_id,
float zoom,
Vector<SkPath> paths,
Vector<bool> shape_compatibilities,
Vector<double> offsets,
Vector<std::unique_ptr<gfx::TimingFunction>> timing_functions,
const std::optional<double>& progress,
const SkPath static_shape,
cc::PaintWorkletInput::PropertyKeys property_keys)
: PaintWorkletInput(clip_area_size, worklet_id, std::move(property_keys)),
paths_(std::move(paths)),
shape_compatibilities_(std::move(shape_compatibilities)),
offsets_(std::move(offsets)),
timing_functions_(std::move(timing_functions)),
progress_(progress),
static_shape_(static_shape),
dx_(reference_origin.x()),
dy_(reference_origin.y()) {}
~ClipPathPaintWorkletInput() override = default;
const std::optional<double>& MainThreadProgress() const { return progress_; }
const Vector<SkPath>& Paths() const { return paths_; }
const SkPath StaticPath() const { return static_shape_; }
// Returns TRUE if the BasicShape::ShapeType of the keyframe and its following
// keyframe are equal, FALSE otherwise. Not defined for the last keyframe.
bool CanAttemptInterpolation(int keyframe) const {
return shape_compatibilities_[keyframe];
}
PaintWorkletInputType GetType() const override {
return PaintWorkletInputType::kClipPath;
}
AnimationProgress GetAdjustedProgress(float progress) const {
// TODO(crbug.com/1374390): This function should be shared with composited
// bgcolor animations Get the start and end clip-path based on the progress
// and offsets.
unsigned result_index = offsets_.size() - 1;
if (progress <= 0) {
result_index = 0;
} else if (progress > 0 && progress < 1) {
for (unsigned i = 0; i < offsets_.size() - 1; i++) {
if (progress <= offsets_[i + 1]) {
result_index = i;
break;
}
}
}
if (result_index == offsets_.size() - 1) {
result_index = offsets_.size() - 2;
}
// Use offsets to calculate for intra-keyframe progress.
float local_progress =
(progress - offsets_[result_index]) /
(offsets_[result_index + 1] - offsets_[result_index]);
// Adjust for that keyframe's timing function
// TODO(crbug.com/347958668): Correct limit direction for phase and
// direction in order to make the correct evaluation at the boundary of a
// step-timing function.
return AnimationProgress(
result_index,
timing_functions_[result_index]->GetValue(
local_progress, TimingFunction::LimitDirection::RIGHT));
}
bool ValueChangeShouldCauseRepaint(const PropertyValue& val1,
const PropertyValue& val2) const override {
return !val1.float_value.has_value() || !val2.float_value.has_value() ||
GetAdjustedProgress(*val1.float_value) !=
GetAdjustedProgress(*val2.float_value);
}
void ApplyTranslation(cc::PaintCanvas* canvas) const {
canvas->translate(dx_, dy_);
}
private:
Vector<SkPath> paths_;
// Many shape types produce interpolable SkPaths, e.g. inset and a 4 point
// polygon are both 4 point paths. By spec, we only interpolate if the the
// BasicShape::ShapeType of each keyframe pair are equal. This tracks whether
// the input ShapeTypes were equal. If equal, we should attempt to interpolate
// between the resulting shapes.
Vector<bool> shape_compatibilities_;
Vector<double> offsets_;
// TODO(crbug.com/1374390): Refactor composited animations so that
// custom timing functions work for bgcolor animations as well
// animations. This class should be refactored so that the necessary
// properties exist in both this and Background Color paint worklet input
Vector<std::unique_ptr<gfx::TimingFunction>> timing_functions_;
std::optional<double> progress_;
SkPath static_shape_;
SkScalar dx_, dy_;
};
BasicShape* CreateBasicShape(
BasicShape::ShapeType type,
const InterpolableValue& interpolable_value,
const NonInterpolableValue& untyped_non_interpolable_value) {
if (type == BasicShape::kStylePathType) {
return PathInterpolationFunctions::AppliedValue(
interpolable_value, &untyped_non_interpolable_value);
}
CSSToLengthConversionData conversion_data(/*element=*/nullptr);
if (type == BasicShape::kStyleShapeType) {
return CSSShapeInterpolationType::CreateShape(
interpolable_value, &untyped_non_interpolable_value, conversion_data);
}
return basic_shape_interpolation_functions::CreateBasicShape(
interpolable_value, untyped_non_interpolable_value, conversion_data);
}
bool CanExtractShapeOrPath(const CSSValue* computed_value) {
// TODO(pdr): Support <geometry-box> (alone, or with a shape).
if (const auto* list = DynamicTo<CSSValueList>(computed_value)) {
return list->First().IsBasicShapeValue() || list->First().IsPathValue() ||
(list->First().IsShapeValue() &&
RuntimeEnabledFeatures::
CSSShapeFunctionCompositeAnimationEnabled());
}
return false;
}
bool IsClipPathNone(const CSSValue* computed_value) {
if (computed_value->IsIdentifierValue()) {
const CSSIdentifierValue* id_val = To<CSSIdentifierValue>(computed_value);
switch (id_val->GetValueID()) {
case CSSValueID::kNone:
case CSSValueID::kInitial:
case CSSValueID::kUnset:
return true;
default:
return false;
}
}
return false;
}
// Returns the basic shape of a keyframe, or null if the keyframe has no path
BasicShape* GetAnimatedShapeFromKeyframe(const PropertySpecificKeyframe* frame,
const KeyframeEffectModelBase* model,
const Element* element) {
BasicShape* basic_shape;
if (model->IsStringKeyframeEffectModel()) {
DCHECK(frame->IsCSSPropertySpecificKeyframe());
const CSSValue* value =
static_cast<const CSSPropertySpecificKeyframe*>(frame)->Value();
const CSSPropertyName property_name =
CSSPropertyName(CSSPropertyID::kClipPath);
const CSSValue* computed_value = StyleResolver::ComputeValue(
const_cast<Element*>(element), property_name, *value);
StyleResolverState state(element->GetDocument(),
*const_cast<Element*>(element));
// TODO(pdr): Support <geometry-box> (alone, or with a shape).
if (CanExtractShapeOrPath(computed_value)) {
basic_shape = BasicShapeForValue(
state, DynamicTo<CSSValueList>(computed_value)->First());
} else {
DCHECK(IsClipPathNone(computed_value));
return nullptr;
}
} else {
DCHECK(frame->IsTransitionPropertySpecificKeyframe());
const TransitionKeyframe::PropertySpecificKeyframe* keyframe =
To<TransitionKeyframe::PropertySpecificKeyframe>(frame);
const NonInterpolableValue* non_interpolable_value =
keyframe->GetValue()->Value().non_interpolable_value.Get();
BasicShape::ShapeType type =
PathInterpolationFunctions::IsPathNonInterpolableValue(
*non_interpolable_value)
? BasicShape::kStylePathType
// This can be any shape but kStylePathType. This is needed to
// distinguish between Path shape and other shapes in
// CreateBasicShape function.
: BasicShape::kBasicShapeCircleType;
basic_shape = CreateBasicShape(
type, *keyframe->GetValue()->Value().interpolable_value.Get(),
*non_interpolable_value);
}
CHECK(basic_shape);
return basic_shape;
}
bool ValidateClipPathValue(const Element* element,
const CSSValue* value,
const InterpolableValue* interpolable_value) {
if (value) {
const CSSPropertyName property_name =
CSSPropertyName(CSSPropertyID::kClipPath);
const CSSValue* computed_value = StyleResolver::ComputeValue(
const_cast<Element*>(element), property_name, *value);
// Don't try to composite animations where we can't extract a shape or path
if (computed_value && CanExtractShapeOrPath(computed_value)) {
const auto* list = DynamicTo<CSSValueList>(computed_value);
// TODO(crbug.com/379052285): We do not currently support anything other
// than kBorderBox. Any other value will result in bad interpolation. This
// should be resolved in future.
if (list->length() == 2 &&
(DynamicTo<CSSIdentifierValue>(list->Item(1))
->ConvertTo<GeometryBox>() != GeometryBox::kBorderBox)) {
return false;
}
return true;
}
// clip-path: none is a special case where we decline to clip a path.
if (IsClipPathNone(value)) {
return true;
}
return false;
} else if (interpolable_value) {
return true;
}
return false;
}
SkPath InterpolatePaths(const bool shapes_are_compatible,
const SkPath& from,
const SkPath& to,
const float progress) {
if (shapes_are_compatible && to.isInterpolatable(from)) {
SkPath out;
to.interpolate(from, progress, &out);
return out;
} else if (progress < 0.5) {
return from;
} else {
return to;
}
}
} // namespace
template <>
struct DowncastTraits<ClipPathPaintWorkletInput> {
static bool AllowFrom(const cc::PaintWorkletInput& worklet_input) {
auto* input = DynamicTo<PaintWorkletInput>(worklet_input);
return input && AllowFrom(*input);
}
static bool AllowFrom(const PaintWorkletInput& worklet_input) {
return worklet_input.GetType() ==
PaintWorkletInput::PaintWorkletInputType::kClipPath;
}
};
// TODO(crbug.com/1248605): Introduce helper functions commonly used by
// background-color and clip-path animations.
// static
Animation* ClipPathPaintDefinition::GetAnimationIfCompositable(
const Element* element) {
if (!element->GetElementAnimations()) {
return nullptr;
}
Animation* compositable_animation =
element->GetElementAnimations()->PaintWorkletClipPathAnimation();
if (!compositable_animation) {
return nullptr;
}
DCHECK(compositable_animation->Affects(*element, GetCSSPropertyClipPath()));
if (element->GetElementAnimations()->CompositedClipPathStatus() ==
ElementAnimations::CompositedPaintStatus::kComposited) {
DCHECK(AnimationIsValidForPaintWorklets(compositable_animation, element,
GetCSSPropertyClipPath(),
ValidateClipPathValue));
return compositable_animation;
}
return AnimationIsValidForPaintWorklets(compositable_animation, element,
GetCSSPropertyClipPath(),
ValidateClipPathValue)
? compositable_animation
: nullptr;
}
// static
ClipPathPaintDefinition* ClipPathPaintDefinition::Create(
LocalFrame& local_root) {
return MakeGarbageCollected<ClipPathPaintDefinition>(local_root);
}
ClipPathPaintDefinition::ClipPathPaintDefinition(LocalFrame& local_root)
: NativeCssPaintDefinition(
&local_root,
PaintWorkletInput::PaintWorkletInputType::kClipPath) {}
PaintRecord ClipPathPaintDefinition::Paint(
const CompositorPaintWorkletInput* compositor_input,
const CompositorPaintWorkletJob::AnimatedPropertyValues&
animated_property_values) {
const auto* input = To<ClipPathPaintWorkletInput>(compositor_input);
const Vector<SkPath>& paths = input->Paths();
SkPath cur_path = input->StaticPath();
if (input->MainThreadProgress().has_value() ||
!animated_property_values.empty()) {
float progress = 0;
if (!animated_property_values.empty()) {
DCHECK_EQ(animated_property_values.size(), 1u);
const auto& entry = animated_property_values.begin();
progress = entry->second.float_value.value();
} else {
progress = input->MainThreadProgress().value();
}
auto [result_index, adjusted_progress] =
input->GetAdjustedProgress(progress);
cur_path = InterpolatePaths(input->CanAttemptInterpolation(result_index),
paths[result_index], paths[result_index + 1],
adjusted_progress);
}
cc::InspectablePaintRecorder paint_recorder;
const gfx::Size clip_area_size(
gfx::ToRoundedSize(gfx::RectF(InfiniteIntRect()).size()));
cc::PaintCanvas* canvas = paint_recorder.beginRecording(clip_area_size);
cc::PaintFlags flags;
flags.setAntiAlias(true);
input->ApplyTranslation(canvas);
canvas->drawPath(cur_path, flags);
return paint_recorder.finishRecordingAsPicture();
}
// Creates a deferred image of size clip_area_size that will be painted via
// paint worklet. The clip paths will be scaled and translated according to
// reference_box.
// static
scoped_refptr<Image> ClipPathPaintDefinition::Paint(
float zoom,
const gfx::RectF& reference_box,
const gfx::SizeF& clip_area_size,
const Node& node,
int worklet_id) {
DCHECK(node.IsElementNode());
const Element* element = To<Element>(&node);
Vector<SkPath> paths;
Vector<bool> shape_compatibilities;
Vector<double> offsets;
std::optional<double> progress;
// The passed reference box is adjusted to be relative to a large enclosing
// rect. To prevent floating point errors, we defer the translation to the
// painting stage and allow path generation to proceed with the unadjusted
// rect.
gfx::RectF reference_size = gfx::RectF(reference_box.size());
Animation* animation = GetAnimationIfCompositable(element);
// If we are here the animation must be compositable.
CHECK(animation);
const AnimationEffect* effect = animation->effect();
DCHECK(effect->IsKeyframeEffect());
const KeyframeEffectModelBase* model =
static_cast<const KeyframeEffect*>(effect)->Model();
const PropertySpecificKeyframeVector* frames =
model->GetPropertySpecificKeyframes(
PropertyHandle(GetCSSPropertyClipPath()));
Vector<std::unique_ptr<gfx::TimingFunction>> timing_functions;
std::optional<BasicShape::ShapeType> prev_type = std::nullopt;
for (const auto& frame : *frames) {
BasicShape* basic_shape =
GetAnimatedShapeFromKeyframe(frame, model, element);
// No compatibility for the first shape.
if (!paths.empty()) {
shape_compatibilities.push_back(
prev_type && basic_shape ? (basic_shape->GetType() == *prev_type)
: false);
}
if (basic_shape) {
const Path path =
basic_shape->GetPath(reference_size, zoom, /*path_scale=*/1.f);
paths.push_back(path.GetSkPath());
prev_type = basic_shape->GetType();
} else {
paths.push_back(InfiniteClipPath());
prev_type = std::nullopt;
}
offsets.push_back(frame->Offset());
const TimingFunction& timing_function = frame->Easing();
// LinearTimingFunction::CloneToCC() returns nullptr as it is shared.
if (timing_function.GetType() == TimingFunction::Type::LINEAR) {
timing_functions.push_back(gfx::LinearTimingFunction::Create());
} else {
timing_functions.push_back(timing_function.CloneToCC());
}
}
progress = effect->Progress();
SkPath static_path;
switch (effect->SpecifiedTiming().fill_mode) {
case Timing::FillMode::AUTO:
case Timing::FillMode::NONE:
case Timing::FillMode::FORWARDS: {
// In the case where there is not currently a clip path, and the fill mode
// isn't backwards or both, we will need to ensure no items are clipped
// during the delay. Use an 'infinite' clip rect to do this.
if (element->GetLayoutObject()->StyleRef().HasClipPath()) {
ClipPathOperation* static_op =
element->GetLayoutObject()->StyleRef().ClipPath();
Path path;
switch (static_op->GetType()) {
case ClipPathOperation::kShape:
path = To<ShapeClipPathOperation>(static_op)->GetPath(
reference_size, zoom, /*path_scale=*/1.f);
break;
case ClipPathOperation::kGeometryBox:
path = ClipPathClipper::RoundedReferenceBox(
To<GeometryBoxClipPathOperation>(static_op)
->GetGeometryBox(),
*element->GetLayoutObject())
.GetPath();
break;
case ClipPathOperation::kReference:
// Reference clip paths are implemented with mask images, and are
// not reducible to single SkPaths.
NOTREACHED();
}
static_path = path.GetSkPath();
} else {
static_path = InfiniteClipPath();
}
break;
}
case Timing::FillMode::BOTH:
case Timing::FillMode::BACKWARDS: {
static_path = paths[0];
}
}
node.GetLayoutObject()->GetMutableForPainting().EnsureId();
CompositorElementId element_id = CompositorElementIdFromUniqueObjectId(
node.GetLayoutObject()->UniqueId(),
CompositorAnimations::CompositorElementNamespaceForProperty(
CSSPropertyID::kClipPath));
CompositorPaintWorkletInput::PropertyKeys input_property_keys;
input_property_keys.emplace_back(
CompositorPaintWorkletInput::NativePropertyType::kClipPath, element_id);
scoped_refptr<ClipPathPaintWorkletInput> input =
base::MakeRefCounted<ClipPathPaintWorkletInput>(
reference_size, clip_area_size, reference_box.origin(), worklet_id,
zoom, std::move(paths), std::move(shape_compatibilities),
std::move(offsets), std::move(timing_functions), progress,
static_path, std::move(input_property_keys));
return PaintWorkletDeferredImage::Create(std::move(input), clip_area_size);
}
void ClipPathPaintDefinition::Trace(Visitor* visitor) const {
NativePaintDefinition::Trace(visitor);
}
} // namespace blink
|