Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: third_party/WebKit/Source/core/animation/InvalidatableStyleInterpolation.cpp

Issue 1412313005: Rename InvalidatableStyleInterpolation to InvalidatableInterpolation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@_renameCSSInterpolationTypes
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "config.h"
6 #include "core/animation/InvalidatableStyleInterpolation.h"
7
8 #include "core/animation/InterpolationEnvironment.h"
9 #include "core/animation/StringKeyframe.h"
10 #include "core/css/resolver/StyleResolverState.h"
11
12 namespace blink {
13
14 void InvalidatableStyleInterpolation::interpolate(int, double fraction)
15 {
16 if (fraction == m_currentFraction)
17 return;
18
19 if (m_currentFraction == 0 || m_currentFraction == 1 || fraction == 0 || fra ction == 1)
20 clearCache();
21
22 m_currentFraction = fraction;
23 if (m_isCached && m_cachedPairConversion)
24 m_cachedPairConversion->interpolateValue(fraction, m_cachedValue);
25 // We defer the interpolation to ensureValidInterpolation() if m_cachedPairC onversion is null.
26 }
27
28 PassOwnPtr<PairwisePrimitiveInterpolation> InvalidatableStyleInterpolation::mayb eConvertPairwise(const InterpolationEnvironment* environment, const UnderlyingVa lue& underlyingValue) const
29 {
30 ASSERT(m_currentFraction != 0 && m_currentFraction != 1);
31 for (const auto& interpolationType : m_interpolationTypes) {
32 if ((m_startKeyframe->isNeutral() || m_endKeyframe->isNeutral()) && (!un derlyingValue || underlyingValue->type() != *interpolationType))
33 continue;
34 OwnPtr<PairwisePrimitiveInterpolation> pairwiseConversion = interpolatio nType->maybeConvertPairwise(*m_startKeyframe, *m_endKeyframe, environment, under lyingValue, m_conversionCheckers);
35 if (pairwiseConversion) {
36 ASSERT(pairwiseConversion->type() == *interpolationType);
37 return pairwiseConversion.release();
38 }
39 }
40 return nullptr;
41 }
42
43 PassOwnPtr<InterpolationValue> InvalidatableStyleInterpolation::convertSingleKey frame(const PropertySpecificKeyframe& keyframe, const InterpolationEnvironment& environment, const UnderlyingValue& underlyingValue) const
44 {
45 if (keyframe.isNeutral() && !underlyingValue)
46 return nullptr;
47 for (const auto& interpolationType : m_interpolationTypes) {
48 if (keyframe.isNeutral() && underlyingValue->type() != *interpolationTyp e)
49 continue;
50 OwnPtr<InterpolationValue> result = interpolationType->maybeConvertSingl e(keyframe, &environment, underlyingValue, m_conversionCheckers);
51 if (result) {
52 ASSERT(result->type() == *interpolationType);
53 return result.release();
54 }
55 }
56 ASSERT(keyframe.isNeutral());
57 return nullptr;
58 }
59
60 PassOwnPtr<InterpolationValue> InvalidatableStyleInterpolation::maybeConvertUnde rlyingValue(const InterpolationEnvironment& environment) const
61 {
62 for (const auto& interpolationType : m_interpolationTypes) {
63 OwnPtr<InterpolationValue> result = interpolationType->maybeConvertUnder lyingValue(environment);
64 if (result)
65 return result.release();
66 }
67 return nullptr;
68 }
69
70 bool InvalidatableStyleInterpolation::dependsOnUnderlyingValue() const
71 {
72 return (m_startKeyframe->underlyingFraction() != 0 && m_currentFraction != 1 ) || (m_endKeyframe->underlyingFraction() != 0 && m_currentFraction != 0);
73 }
74
75 bool InvalidatableStyleInterpolation::isNeutralKeyframeActive() const
76 {
77 return (m_startKeyframe->isNeutral() && m_currentFraction != 1) || (m_endKey frame->isNeutral() && m_currentFraction != 0);
78 }
79
80 void InvalidatableStyleInterpolation::clearCache() const
81 {
82 m_isCached = false;
83 m_cachedPairConversion.clear();
84 m_conversionCheckers.clear();
85 m_cachedValue.clear();
86 }
87
88 bool InvalidatableStyleInterpolation::isCacheValid(const InterpolationEnvironmen t& environment, const UnderlyingValue& underlyingValue) const
89 {
90 if (!m_isCached)
91 return false;
92 if (isNeutralKeyframeActive()) {
93 if (m_cachedPairConversion && m_cachedPairConversion->isFlip())
94 return false;
95 // Pairwise interpolation can never happen between different Interpolati onTypes, neutral values always represent the underlying value.
96 if (!underlyingValue || !m_cachedValue || m_cachedValue->type() != under lyingValue->type())
97 return false;
98 }
99 for (const auto& checker : m_conversionCheckers) {
100 if (!checker->isValid(environment, underlyingValue))
101 return false;
102 }
103 return true;
104 }
105
106 const InterpolationValue* InvalidatableStyleInterpolation::ensureValidInterpolat ion(const InterpolationEnvironment& environment, const UnderlyingValue& underlyi ngValue) const
107 {
108 ASSERT(!std::isnan(m_currentFraction));
109 if (isCacheValid(environment, underlyingValue))
110 return m_cachedValue.get();
111 clearCache();
112 if (m_currentFraction == 0) {
113 m_cachedValue = convertSingleKeyframe(*m_startKeyframe, environment, und erlyingValue);
114 } else if (m_currentFraction == 1) {
115 m_cachedValue = convertSingleKeyframe(*m_endKeyframe, environment, under lyingValue);
116 } else {
117 OwnPtr<PairwisePrimitiveInterpolation> pairwiseConversion = maybeConvert Pairwise(&environment, underlyingValue);
118 if (pairwiseConversion) {
119 m_cachedValue = pairwiseConversion->initialValue();
120 m_cachedPairConversion = pairwiseConversion.release();
121 } else {
122 m_cachedPairConversion = FlipPrimitiveInterpolation::create(
123 convertSingleKeyframe(*m_startKeyframe, environment, underlyingV alue),
124 convertSingleKeyframe(*m_endKeyframe, environment, underlyingVal ue));
125 }
126 m_cachedPairConversion->interpolateValue(m_currentFraction, m_cachedValu e);
127 }
128 m_isCached = true;
129 return m_cachedValue.get();
130 }
131
132 void InvalidatableStyleInterpolation::setFlagIfInheritUsed(InterpolationEnvironm ent& environment) const
133 {
134 if (!property().isCSSProperty())
135 return;
136 if (!environment.state().parentStyle())
137 return;
138 const CSSValue* startValue = toCSSPropertySpecificKeyframe(m_startKeyframe)- >value();
139 const CSSValue* endValue = toCSSPropertySpecificKeyframe(m_endKeyframe)->val ue();
140 if ((startValue && startValue->isInheritedValue())
141 || (endValue && endValue->isInheritedValue())) {
142 environment.state().parentStyle()->setHasExplicitlyInheritedProperties() ;
143 }
144 }
145
146 double InvalidatableStyleInterpolation::underlyingFraction() const
147 {
148 if (m_currentFraction == 0)
149 return m_startKeyframe->underlyingFraction();
150 if (m_currentFraction == 1)
151 return m_endKeyframe->underlyingFraction();
152 return m_cachedPairConversion->interpolateUnderlyingFraction(m_startKeyframe ->underlyingFraction(), m_endKeyframe->underlyingFraction(), m_currentFraction);
153 }
154
155 void InvalidatableStyleInterpolation::applyStack(const ActiveInterpolations& int erpolations, InterpolationEnvironment& environment)
156 {
157 ASSERT(!interpolations.isEmpty());
158 size_t startingIndex = 0;
159
160 // Compute the underlying value to composite onto.
161 UnderlyingValue underlyingValue;
162 const InvalidatableStyleInterpolation& firstInterpolation = toInvalidatableS tyleInterpolation(*interpolations.at(startingIndex));
163 if (firstInterpolation.dependsOnUnderlyingValue()) {
164 underlyingValue.set(firstInterpolation.maybeConvertUnderlyingValue(envir onment));
165 } else {
166 const InterpolationValue* firstValue = firstInterpolation.ensureValidInt erpolation(environment, UnderlyingValue());
167 // Fast path for replace interpolations that are the only one to apply.
168 if (interpolations.size() == 1) {
169 if (firstValue) {
170 firstInterpolation.setFlagIfInheritUsed(environment);
171 firstValue->type().apply(firstValue->interpolableValue(), firstV alue->nonInterpolableValue(), environment);
172 }
173 return;
174 }
175 underlyingValue.set(firstValue);
176 startingIndex++;
177 }
178
179 // Composite interpolations onto the underlying value.
180 bool shouldApply = false;
181 for (size_t i = startingIndex; i < interpolations.size(); i++) {
182 const InvalidatableStyleInterpolation& currentInterpolation = toInvalida tableStyleInterpolation(*interpolations.at(i));
183 ASSERT(currentInterpolation.dependsOnUnderlyingValue());
184 const InterpolationValue* currentValue = currentInterpolation.ensureVali dInterpolation(environment, underlyingValue);
185 if (!currentValue)
186 continue;
187 shouldApply = true;
188 currentInterpolation.setFlagIfInheritUsed(environment);
189 double underlyingFraction = currentInterpolation.underlyingFraction();
190 if (underlyingFraction == 0 || !underlyingValue || underlyingValue->type () != currentValue->type())
191 underlyingValue.set(currentValue);
192 else
193 currentValue->type().composite(underlyingValue, underlyingFraction, *currentValue);
194 }
195
196 if (shouldApply && underlyingValue)
197 underlyingValue->type().apply(underlyingValue->interpolableValue(), unde rlyingValue->nonInterpolableValue(), environment);
198 }
199
200 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698