Implement animation for color-filter
[WebKit-https.git] / Source / WebCore / animation / KeyframeEffectReadOnly.h
1 /*
2  * Copyright (C) 2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #include "AnimationEffectReadOnly.h"
29 #include "AnimationEffectTimingProperties.h"
30 #include "CSSPropertyBlendingClient.h"
31 #include "CompositeOperation.h"
32 #include "Element.h"
33 #include "IterationCompositeOperation.h"
34 #include "KeyframeEffectOptions.h"
35 #include "KeyframeList.h"
36 #include "RenderStyle.h"
37 #include "StyleProperties.h"
38 #include <wtf/Ref.h>
39
40 namespace WebCore {
41
42 class Element;
43
44 class KeyframeEffectReadOnly : public AnimationEffectReadOnly
45     , public CSSPropertyBlendingClient {
46 public:
47     static ExceptionOr<Ref<KeyframeEffectReadOnly>> create(JSC::ExecState&, Element*, JSC::Strong<JSC::JSObject>&&, std::optional<Variant<double, KeyframeEffectOptions>>&&);
48     static ExceptionOr<Ref<KeyframeEffectReadOnly>> create(JSC::ExecState&, Ref<KeyframeEffectReadOnly>&&);
49     static Ref<KeyframeEffectReadOnly> create(const Element&);
50     ~KeyframeEffectReadOnly() { }
51
52     struct BasePropertyIndexedKeyframe {
53         Variant<std::nullptr_t, Vector<std::optional<double>>, double> offset = Vector<std::optional<double>>();
54         Variant<Vector<String>, String> easing = Vector<String>();
55         Variant<std::nullptr_t, Vector<std::optional<CompositeOperation>>, CompositeOperation> composite = Vector<std::optional<CompositeOperation>>();
56     };
57
58     struct PropertyAndValues {
59         CSSPropertyID property;
60         Vector<String> values;
61     };
62
63     struct KeyframeLikeObject {
64         BasePropertyIndexedKeyframe baseProperties;
65         Vector<PropertyAndValues> propertiesAndValues;
66     };
67
68     struct ParsedKeyframe {
69         std::optional<double> offset;
70         double computedOffset;
71         std::optional<CompositeOperation> composite;
72         String easing;
73         RefPtr<TimingFunction> timingFunction;
74         Ref<MutableStyleProperties> style;
75         HashMap<CSSPropertyID, String> unparsedStyle;
76
77         ParsedKeyframe()
78             : style(MutableStyleProperties::create())
79         {
80         }
81     };
82
83     struct BaseComputedKeyframe {
84         std::optional<double> offset;
85         double computedOffset;
86         String easing { "linear" };
87         std::optional<CompositeOperation> composite;
88     };
89
90     Element* target() const { return m_target.get(); }
91     void setTarget(RefPtr<Element>&&);
92
93     Vector<JSC::Strong<JSC::JSObject>> getKeyframes(JSC::ExecState&);
94
95     IterationCompositeOperation iterationComposite() const { return m_iterationCompositeOperation; }
96     CompositeOperation composite() const { return m_compositeOperation; }
97
98     void getAnimatedStyle(std::unique_ptr<RenderStyle>& animatedStyle);
99     void apply(RenderStyle&) override;
100     void invalidate() override;
101     void animationPlayStateDidChange(WebAnimation::PlayState) final;
102     void animationDidSeek() final;
103     void applyPendingAcceleratedActions();
104     bool isRunningAccelerated() const { return m_startedAccelerated; }
105
106     RenderElement* renderer() const override;
107     const RenderStyle& currentStyle() const override;
108     bool isAccelerated() const override { return m_startedAccelerated; }
109     bool filterFunctionListsMatch() const override { return m_filterFunctionListsMatch; }
110     bool transformFunctionListsMatch() const override { return m_transformFunctionListsMatch; }
111 #if ENABLE(FILTERS_LEVEL_2)
112     bool backdropFilterFunctionListsMatch() const override { return m_backdropFilterFunctionListsMatch; }
113 #endif
114     bool colorFilterFunctionListsMatch() const override { return m_colorFilterFunctionListsMatch; }
115
116     void computeDeclarativeAnimationBlendingKeyframes(const RenderStyle* oldStyle, const RenderStyle& newStyle);
117     bool stylesWouldYieldNewCSSTransitionsBlendingKeyframes(const RenderStyle& oldStyle, const RenderStyle& newStyle) const;
118     bool hasBlendingKeyframes() const { return m_blendingKeyframes.size(); }
119     const HashSet<CSSPropertyID>& animatedProperties() const { return m_blendingKeyframes.properties(); }
120
121     bool computeExtentOfTransformAnimation(LayoutRect&) const;
122     bool computeTransformedExtentViaTransformList(const FloatRect&, const RenderStyle&, LayoutRect&) const;
123     bool computeTransformedExtentViaMatrix(const FloatRect&, const RenderStyle&, LayoutRect&) const;
124     bool forceLayoutIfNeeded();
125
126 protected:
127     void copyPropertiesFromSource(Ref<KeyframeEffectReadOnly>&&);
128     ExceptionOr<void> processKeyframes(JSC::ExecState&, JSC::Strong<JSC::JSObject>&&);
129
130     IterationCompositeOperation m_iterationCompositeOperation { IterationCompositeOperation::Replace };
131     CompositeOperation m_compositeOperation { CompositeOperation::Replace };
132
133     KeyframeEffectReadOnly(ClassType, Ref<AnimationEffectTimingReadOnly>&&, Element*);
134
135 private:
136     enum class AcceleratedAction { Play, Pause, Seek };
137     void addPendingAcceleratedAction(AcceleratedAction);
138     void setAnimatedPropertiesInStyle(RenderStyle&, double);
139     TimingFunction* timingFunctionForKeyframeAtIndex(size_t);
140     Ref<const Animation> backingAnimationForCompositedRenderer() const;
141     void computedNeedsForcedLayout();
142     void computeStackingContextImpact();
143     void updateBlendingKeyframes();
144     void computeCSSAnimationBlendingKeyframes();
145     void computeCSSTransitionBlendingKeyframes(const RenderStyle* oldStyle, const RenderStyle& newStyle);
146     void computeShouldRunAccelerated();
147     void setBlendingKeyframes(KeyframeList&);
148     void checkForMatchingTransformFunctionLists();
149     void checkForMatchingFilterFunctionLists();
150 #if ENABLE(FILTERS_LEVEL_2)
151     void checkForMatchingBackdropFilterFunctionLists();
152 #endif
153     void checkForMatchingColorFilterFunctionLists();
154
155     bool checkForMatchingFilterFunctionLists(CSSPropertyID, const std::function<const FilterOperations& (const RenderStyle&)>&) const;
156
157     bool m_shouldRunAccelerated { false };
158     bool m_needsForcedLayout { false };
159     bool m_triggersStackingContext { false };
160     bool m_started { false };
161     bool m_startedAccelerated { false };
162     bool m_transformFunctionListsMatch { false };
163     bool m_filterFunctionListsMatch { false };
164 #if ENABLE(FILTERS_LEVEL_2)
165     bool m_backdropFilterFunctionListsMatch { false };
166 #endif
167     bool m_colorFilterFunctionListsMatch { false };
168
169     RefPtr<Element> m_target;
170     KeyframeList m_blendingKeyframes;
171     Vector<ParsedKeyframe> m_parsedKeyframes;
172
173     Vector<AcceleratedAction> m_pendingAcceleratedActions;
174 };
175
176 } // namespace WebCore
177
178 SPECIALIZE_TYPE_TRAITS_ANIMATION_EFFECT(KeyframeEffectReadOnly, isKeyframeEffectReadOnly());