This source file includes following definitions.
- compareOffsets
- properties
- sample
- normalizedKeyframes
- ensureKeyframeGroups
- ensureInterpolationEffect
- isReplaceOnly
- trace
- m_composite
- appendKeyframe
- removeRedundantKeyframes
- addSyntheticKeyframeIfRequired
- trace
#include "config.h"
#include "core/animation/KeyframeEffectModel.h"
#include "core/animation/TimedItem.h"
#include "wtf/text/StringHash.h"
namespace WebCore {
bool Keyframe::compareOffsets(const RefPtrWillBeMember<Keyframe>& a, const RefPtrWillBeMember<Keyframe>& b)
{
return a->offset() < b->offset();
}
PropertySet KeyframeEffectModelBase::properties() const
{
PropertySet result;
if (!m_keyframes.size()) {
return result;
}
result = m_keyframes[0]->properties();
for (size_t i = 1; i < m_keyframes.size(); i++) {
PropertySet extras = m_keyframes[i]->properties();
for (PropertySet::const_iterator it = extras.begin(); it != extras.end(); ++it) {
result.add(*it);
}
}
return result;
}
PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > KeyframeEffectModelBase::sample(int iteration, double fraction, double iterationDuration) const
{
ASSERT(iteration >= 0);
ASSERT(!isNull(fraction));
ensureKeyframeGroups();
ensureInterpolationEffect();
return m_interpolationEffect->getActiveInterpolations(fraction, iterationDuration);
}
KeyframeEffectModelBase::KeyframeVector KeyframeEffectModelBase::normalizedKeyframes(const KeyframeVector& keyframes)
{
size_t beginIndex = 0;
size_t endIndex = keyframes.size();
size_t lastIndex = endIndex;
double lastOffset = std::numeric_limits<double>::quiet_NaN();
for (size_t i = 0; i < keyframes.size(); ++i) {
double offset = keyframes[i]->offset();
if (!isNull(offset)) {
if (lastIndex < i && offset < lastOffset) {
endIndex = beginIndex;
break;
}
if (offset < 0) {
beginIndex = i + 1;
} else if (offset > 1) {
endIndex = std::min(i, endIndex);
}
lastIndex = i;
lastOffset = offset;
}
}
KeyframeVector result;
if (beginIndex != endIndex) {
result.reserveCapacity(endIndex - beginIndex);
for (size_t i = beginIndex; i < endIndex; ++i) {
result.append(keyframes[i]->clone());
}
if (isNull(result[result.size() - 1]->offset()))
result[result.size() - 1]->setOffset(1);
if (result.size() > 1 && isNull(result[0]->offset()))
result[0]->setOffset(0);
lastIndex = 0;
lastOffset = result[0]->offset();
for (size_t i = 1; i < result.size(); ++i) {
double offset = result[i]->offset();
if (!isNull(offset)) {
if (lastIndex + 1 < i) {
for (size_t j = 1; j < i - lastIndex; ++j)
result[lastIndex + j]->setOffset(lastOffset + (offset - lastOffset) * j / (i - lastIndex));
}
lastIndex = i;
lastOffset = offset;
}
}
}
return result;
}
void KeyframeEffectModelBase::ensureKeyframeGroups() const
{
if (m_keyframeGroups)
return;
m_keyframeGroups = adoptPtrWillBeNoop(new KeyframeGroupMap);
const KeyframeVector keyframes = normalizedKeyframes(getFrames());
for (KeyframeVector::const_iterator keyframeIter = keyframes.begin(); keyframeIter != keyframes.end(); ++keyframeIter) {
const Keyframe* keyframe = keyframeIter->get();
PropertySet keyframeProperties = keyframe->properties();
for (PropertySet::const_iterator propertyIter = keyframeProperties.begin(); propertyIter != keyframeProperties.end(); ++propertyIter) {
CSSPropertyID property = *propertyIter;
KeyframeGroupMap::iterator groupIter = m_keyframeGroups->find(property);
PropertySpecificKeyframeGroup* group;
if (groupIter == m_keyframeGroups->end())
group = m_keyframeGroups->add(property, adoptPtrWillBeNoop(new PropertySpecificKeyframeGroup)).storedValue->value.get();
else
group = groupIter->value.get();
ASSERT(keyframe->composite() == AnimationEffect::CompositeReplace);
group->appendKeyframe(keyframe->createPropertySpecificKeyframe(property));
}
}
for (KeyframeGroupMap::iterator iter = m_keyframeGroups->begin(); iter != m_keyframeGroups->end(); ++iter) {
iter->value->addSyntheticKeyframeIfRequired(this);
iter->value->removeRedundantKeyframes();
}
}
void KeyframeEffectModelBase::ensureInterpolationEffect() const
{
if (m_interpolationEffect)
return;
m_interpolationEffect = InterpolationEffect::create();
for (KeyframeGroupMap::const_iterator iter = m_keyframeGroups->begin(); iter != m_keyframeGroups->end(); ++iter) {
const PropertySpecificKeyframeVector& keyframes = iter->value->keyframes();
ASSERT(keyframes[0]->composite() == AnimationEffect::CompositeReplace);
for (size_t i = 0; i < keyframes.size() - 1; i++) {
ASSERT(keyframes[i + 1]->composite() == AnimationEffect::CompositeReplace);
double applyFrom = i ? keyframes[i]->offset() : (-std::numeric_limits<double>::infinity());
double applyTo = i == keyframes.size() - 2 ? std::numeric_limits<double>::infinity() : keyframes[i + 1]->offset();
if (applyTo == 1)
applyTo = std::numeric_limits<double>::infinity();
m_interpolationEffect->addInterpolation(keyframes[i]->createInterpolation(iter->key, keyframes[i + 1].get()),
keyframes[i]->easing(), keyframes[i]->offset(), keyframes[i + 1]->offset(), applyFrom, applyTo);
}
}
}
bool KeyframeEffectModelBase::isReplaceOnly()
{
ensureKeyframeGroups();
for (KeyframeGroupMap::iterator iter = m_keyframeGroups->begin(); iter != m_keyframeGroups->end(); ++iter) {
const PropertySpecificKeyframeVector& keyframeVector = iter->value->keyframes();
for (size_t i = 0; i < keyframeVector.size(); ++i) {
if (keyframeVector[i]->composite() != AnimationEffect::CompositeReplace)
return false;
}
}
return true;
}
void KeyframeEffectModelBase::trace(Visitor* visitor)
{
visitor->trace(m_keyframes);
visitor->trace(m_interpolationEffect);
#if ENABLE_OILPAN
visitor->trace(m_keyframeGroups);
#endif
}
Keyframe::PropertySpecificKeyframe::PropertySpecificKeyframe(double offset, PassRefPtr<TimingFunction> easing, AnimationEffect::CompositeOperation composite)
: m_offset(offset)
, m_easing(easing)
, m_composite(composite)
{
}
void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::appendKeyframe(PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe> keyframe)
{
ASSERT(m_keyframes.isEmpty() || m_keyframes.last()->offset() <= keyframe->offset());
m_keyframes.append(keyframe);
}
void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::removeRedundantKeyframes()
{
ASSERT(m_keyframes.size() >= 2);
for (int i = m_keyframes.size() - 1; i >= 0; --i) {
double offset = m_keyframes[i]->offset();
bool hasSameOffsetAsPreviousNeighbor = !i || m_keyframes[i - 1]->offset() == offset;
bool hasSameOffsetAsNextNeighbor = i == static_cast<int>(m_keyframes.size() - 1) || m_keyframes[i + 1]->offset() == offset;
if (hasSameOffsetAsPreviousNeighbor && hasSameOffsetAsNextNeighbor)
m_keyframes.remove(i);
}
ASSERT(m_keyframes.size() >= 2);
}
void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::addSyntheticKeyframeIfRequired(const KeyframeEffectModelBase* context)
{
ASSERT(!m_keyframes.isEmpty());
if (m_keyframes.first()->offset() != 0.0)
m_keyframes.insert(0, m_keyframes.first()->neutralKeyframe(0, nullptr));
if (m_keyframes.last()->offset() != 1.0)
appendKeyframe(m_keyframes.last()->neutralKeyframe(1, nullptr));
}
void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::trace(Visitor* visitor)
{
#if ENABLE(OILPAN)
visitor->trace(m_keyframes);
#endif
}
}