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

Unified Diff: Source/core/animation/InvalidatableStyleInterpolation.cpp

Issue 1153943003: Add foundation for removing AnimatableValues from StyleInterpolation (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add missing header file Created 5 years, 7 months 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/animation/InvalidatableStyleInterpolation.cpp
diff --git a/Source/core/animation/InvalidatableStyleInterpolation.cpp b/Source/core/animation/InvalidatableStyleInterpolation.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..9315b36bf3cd75f5c9158f0dbe99979bd627cb5a
--- /dev/null
+++ b/Source/core/animation/InvalidatableStyleInterpolation.cpp
@@ -0,0 +1,188 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/animation/InvalidatableStyleInterpolation.h"
+
+#include "core/animation/StringKeyframe.h"
+
+namespace blink {
+
+class PairwiseConversionCache : public InvalidatableStyleInterpolation::CachedConversion {
+public:
+ static PassOwnPtrWillBeRawPtr<PairwiseConversionCache> create(
+ PassRefPtrWillBeRawPtr<InterpolationInvalidators> invalidators,
+ const AnimationType& type,
+ PassOwnPtrWillBeRawPtr<InterpolableValue> startInterpolableValue,
+ PassOwnPtrWillBeRawPtr<InterpolableValue> endInterpolableValue,
+ PassRefPtrWillBeRawPtr<NonInterpolableValue> nonInterpolableValue)
+ {
+ return adoptPtrWillBeNoop(new PairwiseConversionCache(invalidators, type, startInterpolableValue, endInterpolableValue, nonInterpolableValue));
+ }
+
+ virtual void interpolate(double fraction, AnimationValue& result) const
+ {
+ ASSERT_UNUSED(m_type, result.type == &m_type);
shans 2015/05/28 05:48:14 This doesn't need to be ASSERT_UNUSED.
alancutter (OOO until 2018) 2015/06/01 07:35:29 This used to be ASSERT but it failed to compile in
+ ASSERT(result.nonInterpolableValue == m_nonInterpolableValue);
shans 2015/05/28 05:48:14 These probably aren't needed here. We can assert t
alancutter (OOO until 2018) 2015/06/01 07:35:29 This assertion is to check that nothing's messed w
+ m_startInterpolableValue->interpolate(*m_endInterpolableValue, fraction, *result.interpolableValue);
+ }
+
+ DEFINE_INLINE_VIRTUAL_TRACE()
+ {
+ CachedConversion::trace(visitor);
+ visitor->trace(m_startInterpolableValue);
+ visitor->trace(m_endInterpolableValue);
+ visitor->trace(m_nonInterpolableValue);
+ }
+
+private:
+ PairwiseConversionCache(
+ PassRefPtrWillBeRawPtr<InterpolationInvalidators> invalidators,
+ const AnimationType& type,
+ PassOwnPtrWillBeRawPtr<InterpolableValue> startInterpolableValue,
+ PassOwnPtrWillBeRawPtr<InterpolableValue> endInterpolableValue,
+ PassRefPtrWillBeRawPtr<NonInterpolableValue> nonInterpolableValue)
+ : CachedConversion(invalidators)
+ , m_type(type)
+ , m_startInterpolableValue(startInterpolableValue)
+ , m_endInterpolableValue(endInterpolableValue)
+ , m_nonInterpolableValue(nonInterpolableValue)
+ { }
+
+ const AnimationType& m_type;
+ OwnPtrWillBeMember<InterpolableValue> m_startInterpolableValue;
+ OwnPtrWillBeMember<InterpolableValue> m_endInterpolableValue;
+ RefPtrWillBeMember<NonInterpolableValue> m_nonInterpolableValue;
+};
+
+class DefaultConversionCache : public InvalidatableStyleInterpolation::CachedConversion {
+public:
+ static PassOwnPtrWillBeRawPtr<DefaultConversionCache> create(
+ PassRefPtrWillBeRawPtr<InterpolationInvalidators> invalidators,
+ AnimationValue& start,
+ AnimationValue& end)
+ {
+ return adoptPtrWillBeNoop(new DefaultConversionCache(invalidators, start, end));
+ }
+
+ static bool isBeforeFlip(double fraction) { return fraction < 0.5; }
+
+ virtual void interpolate(double fraction, AnimationValue& result) const
+ {
+ if (!std::isnan(m_lastFraction) && isBeforeFlip(fraction) == isBeforeFlip(m_lastFraction))
shans 2015/05/28 05:48:14 isBeforeFlip is a more obscure name than < 0.5...
alancutter (OOO until 2018) 2015/06/01 07:35:29 Inlined isBeforeFlip().
+ return;
+ result.copy(isBeforeFlip(fraction) ? m_start : m_end);
+ m_lastFraction = fraction;
+ }
+
+ DEFINE_INLINE_VIRTUAL_TRACE()
+ {
+ CachedConversion::trace(visitor);
+ visitor->trace(m_start);
+ visitor->trace(m_end);
+ }
+
+private:
+ DefaultConversionCache(
+ PassRefPtrWillBeRawPtr<InterpolationInvalidators> invalidators,
+ AnimationValue& start,
+ AnimationValue& end)
+ : CachedConversion(invalidators)
+ , m_lastFraction(std::numeric_limits<double>::quiet_NaN())
+ {
+ m_start.consume(start);
+ m_end.consume(end);
+ }
+
+ AnimationValue m_start;
+ AnimationValue m_end;
+ mutable double m_lastFraction;
+};
+
+
+InvalidatableStyleInterpolation::InvalidatableStyleInterpolation(
+ const Vector<const AnimationType*>& applicableTypes,
+ PassRefPtrWillBeRawPtr<CSSPropertySpecificKeyframe> startKeyframe,
+ PassRefPtrWillBeRawPtr<CSSPropertySpecificKeyframe> endKeyframe)
+ : StyleInterpolation(nullptr, nullptr, applicableTypes.first()->property())
+ , m_applicableTypes(applicableTypes)
+ , m_startKeyframe(startKeyframe)
+ , m_endKeyframe(endKeyframe)
+ , m_cachedConversion(nullptr)
+{
+ maybeCachePairwiseConversion(nullptr);
+ interpolate(0, 0);
+}
+
+bool InvalidatableStyleInterpolation::maybeCachePairwiseConversion(const StyleResolverState* state) const
+{
+ for (const auto& animationType : m_applicableTypes) {
+ RefPtrWillBeRawPtr<InterpolationInvalidators> invalidators;
+ OwnPtrWillBeRawPtr<InterpolableValue> startInterpolableValue;
+ OwnPtrWillBeRawPtr<InterpolableValue> endInterpolableValue;
+ RefPtrWillBeRawPtr<NonInterpolableValue> nonInterpolableValue;
+ if (animationType->maybeConvertPairwise(*m_startKeyframe, *m_endKeyframe, state, invalidators, startInterpolableValue, endInterpolableValue, nonInterpolableValue)) {
shans 2015/05/28 05:48:13 This is quite messy, and strongly suggests that ma
alancutter (OOO until 2018) 2015/06/01 07:35:29 Something I decided when making the AnimationType
alancutter (OOO until 2018) 2015/06/01 23:47:54 On second thought I can just define a return struc
+ m_cachedConversion = PairwiseConversionCache::create(
+ invalidators.release(),
+ *animationType,
+ startInterpolableValue.release(),
+ endInterpolableValue.release(),
+ nonInterpolableValue.release());
+ return true;
+ }
+ }
+ return false;
+}
+
+void InvalidatableStyleInterpolation::interpolate(int, double fraction)
+{
+ m_currentFraction = fraction;
+ if (m_cachedConversion)
+ m_cachedConversion->interpolate(fraction, m_cachedValue);
+ else
+ m_cachedValue.clear();
+}
+
+void InvalidatableStyleInterpolation::validateCache(const StyleResolverState& state) const
+{
+ if (m_cachedConversion && !m_cachedConversion->isInvalid(state))
+ return;
+ m_cachedConversion = nullptr;
+ if (!maybeCachePairwiseConversion(&state))
+ cacheDefaultConversion(state);
+ m_cachedConversion->interpolate(m_currentFraction, m_cachedValue);
+}
+
+void InvalidatableStyleInterpolation::cacheDefaultConversion(const StyleResolverState& state) const
+{
+ RefPtrWillBeRawPtr<InterpolationInvalidators> startInvalidators;
+ AnimationValue startValue = convertSingleKeyframe(state, *m_startKeyframe, startInvalidators);
+
+ RefPtrWillBeRawPtr<InterpolationInvalidators> endInvalidators;
+ AnimationValue endValue = convertSingleKeyframe(state, *m_endKeyframe, endInvalidators);
+
+ InterpolationInvalidators::chain(startInvalidators, endInvalidators.release());
+ m_cachedConversion = DefaultConversionCache::create(startInvalidators.release(), startValue, endValue);
+}
+
+AnimationValue InvalidatableStyleInterpolation::convertSingleKeyframe(const StyleResolverState& state, const CSSPropertySpecificKeyframe& keyframe, RefPtrWillBeRawPtr<InterpolationInvalidators>& resultInvalidators) const
+{
+ for (const auto& animationType : m_applicableTypes) {
+ OwnPtrWillBeRawPtr<InterpolableValue> interpolableValue;
+ RefPtrWillBeRawPtr<NonInterpolableValue> nonInterpolableValue;
+ if (animationType->maybeConvertSingle(keyframe, &state, resultInvalidators, interpolableValue, nonInterpolableValue))
+ return AnimationValue(animationType, interpolableValue.release(), nonInterpolableValue.release());
+ }
+ ASSERT_NOT_REACHED();
+ return AnimationValue();
+}
+
+void InvalidatableStyleInterpolation::apply(StyleResolverState& state) const
+{
+ validateCache(state);
+ if (m_cachedValue)
+ m_cachedValue.type->apply(*m_cachedValue.interpolableValue, m_cachedValue.nonInterpolableValue.get(), state);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698