1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/animation/css/compositor_keyframe_value_factory.h"
6 
7 #include "third_party/blink/renderer/core/animation/compositor_animations.h"
8 #include "third_party/blink/renderer/core/animation/css/compositor_keyframe_color.h"
9 #include "third_party/blink/renderer/core/animation/css/compositor_keyframe_double.h"
10 #include "third_party/blink/renderer/core/animation/css/compositor_keyframe_filter_operations.h"
11 #include "third_party/blink/renderer/core/animation/css/compositor_keyframe_transform.h"
12 #include "third_party/blink/renderer/core/animation/property_handle.h"
13 #include "third_party/blink/renderer/core/css/css_color_value.h"
14 #include "third_party/blink/renderer/core/css/properties/css_property.h"
15 #include "third_party/blink/renderer/core/style/computed_style.h"
16 #include "third_party/blink/renderer/platform/heap/heap.h"
17 #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
18 
19 namespace blink {
20 
CreateFromTransformProperties(scoped_refptr<TransformOperation> transform,double zoom,scoped_refptr<TransformOperation> initial_transform)21 static CompositorKeyframeValue* CreateFromTransformProperties(
22     scoped_refptr<TransformOperation> transform,
23     double zoom,
24     scoped_refptr<TransformOperation> initial_transform) {
25   TransformOperations operation;
26   bool has_transform = static_cast<bool>(transform);
27   if (has_transform || initial_transform) {
28     operation.Operations().push_back(
29         std::move(has_transform ? transform : initial_transform));
30   }
31   return MakeGarbageCollected<CompositorKeyframeTransform>(
32       operation, has_transform ? zoom : 1);
33 }
34 
Create(const PropertyHandle & property,const ComputedStyle & style)35 CompositorKeyframeValue* CompositorKeyframeValueFactory::Create(
36     const PropertyHandle& property,
37     const ComputedStyle& style) {
38   const CSSProperty& css_property = property.GetCSSProperty();
39 #if DCHECK_IS_ON()
40   // Variables are conditionally interpolable and compositable.
41   if (css_property.PropertyID() != CSSPropertyID::kVariable) {
42     DCHECK(css_property.IsInterpolable());
43     DCHECK(css_property.IsCompositableProperty());
44   }
45 #endif
46   switch (css_property.PropertyID()) {
47     case CSSPropertyID::kOpacity:
48       return MakeGarbageCollected<CompositorKeyframeDouble>(style.Opacity());
49     case CSSPropertyID::kFilter:
50       return MakeGarbageCollected<CompositorKeyframeFilterOperations>(
51           style.Filter());
52     case CSSPropertyID::kBackdropFilter:
53       return MakeGarbageCollected<CompositorKeyframeFilterOperations>(
54           style.BackdropFilter());
55     case CSSPropertyID::kTransform:
56       return MakeGarbageCollected<CompositorKeyframeTransform>(
57           style.Transform(), style.EffectiveZoom());
58     case CSSPropertyID::kTranslate: {
59       return CreateFromTransformProperties(style.Translate(),
60                                            style.EffectiveZoom(), nullptr);
61     }
62     case CSSPropertyID::kRotate: {
63       return CreateFromTransformProperties(style.Rotate(),
64                                            style.EffectiveZoom(), nullptr);
65     }
66     case CSSPropertyID::kScale: {
67       return CreateFromTransformProperties(style.Scale(), style.EffectiveZoom(),
68                                            nullptr);
69     }
70     case CSSPropertyID::kVariable: {
71       if (!RuntimeEnabledFeatures::OffMainThreadCSSPaintEnabled()) {
72         return nullptr;
73       }
74       const AtomicString& property_name = property.CustomPropertyName();
75       const CSSValue* value = style.GetVariableValue(property_name);
76 
77       const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value);
78       if (primitive_value && primitive_value->IsNumber()) {
79         return MakeGarbageCollected<CompositorKeyframeDouble>(
80             primitive_value->GetFloatValue());
81       }
82 
83       // TODO: Add supported for interpolable color values from
84       // CSSIdentifierValue when given a value of currentcolor
85       if (const auto* color_value = DynamicTo<cssvalue::CSSColorValue>(value)) {
86         Color color = color_value->Value();
87         return MakeGarbageCollected<CompositorKeyframeColor>(SkColorSetARGB(
88             color.Alpha(), color.Red(), color.Green(), color.Blue()));
89       }
90 
91       return nullptr;
92     }
93     default:
94       NOTREACHED();
95       return nullptr;
96   }
97 }
98 
99 }  // namespace blink
100