2015-11-02 00:41:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ComputedTimingFunction.h"
|
2016-02-18 16:21:00 +03:00
|
|
|
#include "nsAlgorithm.h" // For clamped()
|
2015-11-02 00:41:00 +03:00
|
|
|
#include "nsStyleUtil.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
|
|
|
ComputedTimingFunction::Init(const nsTimingFunction &aFunction)
|
|
|
|
{
|
|
|
|
mType = aFunction.mType;
|
|
|
|
if (nsTimingFunction::IsSplineType(mType)) {
|
|
|
|
mTimingFunction.Init(aFunction.mFunc.mX1, aFunction.mFunc.mY1,
|
|
|
|
aFunction.mFunc.mX2, aFunction.mFunc.mY2);
|
|
|
|
} else {
|
2017-02-24 11:59:02 +03:00
|
|
|
mStepsOrFrames = aFunction.mStepsOrFrames;
|
2015-11-02 00:41:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double
|
2016-04-01 08:00:57 +03:00
|
|
|
StepTiming(uint32_t aSteps,
|
|
|
|
double aPortion,
|
|
|
|
ComputedTimingFunction::BeforeFlag aBeforeFlag,
|
|
|
|
nsTimingFunction::Type aType)
|
2015-11-02 00:41:00 +03:00
|
|
|
{
|
2016-05-23 07:02:12 +03:00
|
|
|
MOZ_ASSERT(aType == nsTimingFunction::Type::StepStart ||
|
|
|
|
aType == nsTimingFunction::Type::StepEnd, "invalid type");
|
2016-04-01 08:00:57 +03:00
|
|
|
|
|
|
|
// Calculate current step using step-end behavior
|
2017-02-08 03:25:31 +03:00
|
|
|
int32_t step = floor(aPortion * aSteps);
|
2016-04-01 08:00:57 +03:00
|
|
|
|
|
|
|
// step-start is one step ahead
|
|
|
|
if (aType == nsTimingFunction::Type::StepStart) {
|
|
|
|
step++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the "before flag" is set and we are at a transition point,
|
2017-02-08 03:25:31 +03:00
|
|
|
// drop back a step
|
|
|
|
if (aBeforeFlag == ComputedTimingFunction::BeforeFlag::Set &&
|
2016-04-01 08:00:57 +03:00
|
|
|
fmod(aPortion * aSteps, 1) == 0) {
|
|
|
|
step--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to a progress value
|
2017-02-08 03:25:31 +03:00
|
|
|
double result = double(step) / double(aSteps);
|
|
|
|
|
|
|
|
// We should not produce a result outside [0, 1] unless we have an
|
|
|
|
// input outside that range. This takes care of steps that would otherwise
|
|
|
|
// occur at boundaries.
|
|
|
|
if (result < 0.0 && aPortion >= 0.0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
if (result > 1.0 && aPortion <= 1.0) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
2017-02-24 11:59:02 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double
|
|
|
|
FramesTiming(uint32_t aFrames, double aPortion)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFrames > 1, "the number of frames must be greater than 1");
|
|
|
|
int32_t currentFrame = floor(aPortion * aFrames);
|
|
|
|
double result = double(currentFrame) / double(aFrames - 1);
|
2017-02-08 03:25:31 +03:00
|
|
|
|
2017-02-24 11:59:02 +03:00
|
|
|
// Don't overshoot the natural range of the animation (by producing an output
|
|
|
|
// progress greater than 1.0) when we are at the exact end of its interval
|
|
|
|
// (i.e. the input progress is 1.0).
|
|
|
|
if (result > 1.0 && aPortion <= 1.0) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
2017-02-08 03:25:31 +03:00
|
|
|
return result;
|
2015-11-02 00:41:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2016-04-01 08:00:57 +03:00
|
|
|
ComputedTimingFunction::GetValue(
|
|
|
|
double aPortion,
|
|
|
|
ComputedTimingFunction::BeforeFlag aBeforeFlag) const
|
2015-11-02 00:41:00 +03:00
|
|
|
{
|
|
|
|
if (HasSpline()) {
|
2016-02-18 16:22:00 +03:00
|
|
|
// Check for a linear curve.
|
|
|
|
// (GetSplineValue(), below, also checks this but doesn't work when
|
|
|
|
// aPortion is outside the range [0.0, 1.0]).
|
|
|
|
if (mTimingFunction.X1() == mTimingFunction.Y1() &&
|
|
|
|
mTimingFunction.X2() == mTimingFunction.Y2()) {
|
|
|
|
return aPortion;
|
|
|
|
}
|
|
|
|
|
2016-07-12 10:33:45 +03:00
|
|
|
// Ensure that we return 0 or 1 on both edges.
|
|
|
|
if (aPortion == 0.0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
if (aPortion == 1.0) {
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2016-02-18 16:22:00 +03:00
|
|
|
// For negative values, try to extrapolate with tangent (p1 - p0) or,
|
|
|
|
// if p1 is coincident with p0, with (p2 - p0).
|
|
|
|
if (aPortion < 0.0) {
|
|
|
|
if (mTimingFunction.X1() > 0.0) {
|
|
|
|
return aPortion * mTimingFunction.Y1() / mTimingFunction.X1();
|
|
|
|
} else if (mTimingFunction.Y1() == 0 && mTimingFunction.X2() > 0.0) {
|
|
|
|
return aPortion * mTimingFunction.Y2() / mTimingFunction.X2();
|
|
|
|
}
|
|
|
|
// If we can't calculate a sensible tangent, don't extrapolate at all.
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For values greater than 1, try to extrapolate with tangent (p2 - p3) or,
|
|
|
|
// if p2 is coincident with p3, with (p1 - p3).
|
|
|
|
if (aPortion > 1.0) {
|
|
|
|
if (mTimingFunction.X2() < 1.0) {
|
|
|
|
return 1.0 + (aPortion - 1.0) *
|
|
|
|
(mTimingFunction.Y2() - 1) / (mTimingFunction.X2() - 1);
|
|
|
|
} else if (mTimingFunction.Y2() == 1 && mTimingFunction.X1() < 1.0) {
|
|
|
|
return 1.0 + (aPortion - 1.0) *
|
|
|
|
(mTimingFunction.Y1() - 1) / (mTimingFunction.X1() - 1);
|
|
|
|
}
|
|
|
|
// If we can't calculate a sensible tangent, don't extrapolate at all.
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:41:00 +03:00
|
|
|
return mTimingFunction.GetSplineValue(aPortion);
|
|
|
|
}
|
2016-02-18 16:21:00 +03:00
|
|
|
|
2017-02-24 11:59:02 +03:00
|
|
|
return mType == nsTimingFunction::Type::Frames
|
|
|
|
? FramesTiming(mStepsOrFrames, aPortion)
|
|
|
|
: StepTiming(mStepsOrFrames, aPortion, aBeforeFlag, mType);
|
2015-11-02 00:41:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
ComputedTimingFunction::Compare(const ComputedTimingFunction& aRhs) const
|
|
|
|
{
|
|
|
|
if (mType != aRhs.mType) {
|
|
|
|
return int32_t(mType) - int32_t(aRhs.mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mType == nsTimingFunction::Type::CubicBezier) {
|
|
|
|
int32_t order = mTimingFunction.Compare(aRhs.mTimingFunction);
|
|
|
|
if (order != 0) {
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
} else if (mType == nsTimingFunction::Type::StepStart ||
|
2017-02-24 11:59:02 +03:00
|
|
|
mType == nsTimingFunction::Type::StepEnd ||
|
|
|
|
mType == nsTimingFunction::Type::Frames) {
|
|
|
|
if (mStepsOrFrames != aRhs.mStepsOrFrames) {
|
|
|
|
return int32_t(mStepsOrFrames) - int32_t(aRhs.mStepsOrFrames);
|
2015-11-02 00:41:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ComputedTimingFunction::AppendToString(nsAString& aResult) const
|
|
|
|
{
|
|
|
|
switch (mType) {
|
|
|
|
case nsTimingFunction::Type::CubicBezier:
|
|
|
|
nsStyleUtil::AppendCubicBezierTimingFunction(mTimingFunction.X1(),
|
|
|
|
mTimingFunction.Y1(),
|
|
|
|
mTimingFunction.X2(),
|
|
|
|
mTimingFunction.Y2(),
|
|
|
|
aResult);
|
|
|
|
break;
|
|
|
|
case nsTimingFunction::Type::StepStart:
|
|
|
|
case nsTimingFunction::Type::StepEnd:
|
2017-02-24 11:59:02 +03:00
|
|
|
nsStyleUtil::AppendStepsTimingFunction(mType, mStepsOrFrames, aResult);
|
|
|
|
break;
|
|
|
|
case nsTimingFunction::Type::Frames:
|
2017-02-24 09:50:08 +03:00
|
|
|
nsStyleUtil::AppendFramesTimingFunction(mStepsOrFrames, aResult);
|
2015-11-02 00:41:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nsStyleUtil::AppendCubicBezierKeywordTimingFunction(mType, aResult);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 16:47:00 +03:00
|
|
|
/* static */ int32_t
|
|
|
|
ComputedTimingFunction::Compare(const Maybe<ComputedTimingFunction>& aLhs,
|
|
|
|
const Maybe<ComputedTimingFunction>& aRhs)
|
|
|
|
{
|
|
|
|
// We can't use |operator<| for const Maybe<>& here because
|
|
|
|
// 'ease' is prior to 'linear' which is represented by Nothing().
|
|
|
|
// So we have to convert Nothing() as 'linear' and check it first.
|
|
|
|
nsTimingFunction::Type lhsType = aLhs.isNothing() ?
|
|
|
|
nsTimingFunction::Type::Linear : aLhs->GetType();
|
|
|
|
nsTimingFunction::Type rhsType = aRhs.isNothing() ?
|
|
|
|
nsTimingFunction::Type::Linear : aRhs->GetType();
|
|
|
|
|
|
|
|
if (lhsType != rhsType) {
|
|
|
|
return int32_t(lhsType) - int32_t(rhsType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both of them are Nothing().
|
|
|
|
if (lhsType == nsTimingFunction::Type::Linear) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other types.
|
|
|
|
return aLhs->Compare(aRhs.value());
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:41:00 +03:00
|
|
|
} // namespace mozilla
|