1/*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/animation/AnimationStack.h"
33
34#include "core/animation/CompositorAnimations.h"
35#include "core/animation/StyleInterpolation.h"
36#include "core/animation/css/CSSAnimations.h"
37#include "wtf/BitArray.h"
38#include "wtf/NonCopyingSort.h"
39#include <algorithm>
40
41namespace blink {
42
43namespace {
44
45void copyToActiveInterpolationMap(const WillBeHeapVector<RefPtrWillBeMember<blink::Interpolation> >& source, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<blink::Interpolation> >& target)
46{
47    for (size_t i = 0; i < source.size(); ++i) {
48        Interpolation* interpolation = source[i].get();
49        target.set(toStyleInterpolation(interpolation)->id(), interpolation);
50    }
51}
52
53bool compareEffects(const OwnPtrWillBeMember<SampledEffect>& effect1, const OwnPtrWillBeMember<SampledEffect>& effect2)
54{
55    ASSERT(effect1 && effect2);
56    return effect1->sequenceNumber() < effect2->sequenceNumber();
57}
58
59void copyNewAnimationsToActiveInterpolationMap(const WillBeHeapVector<RawPtrWillBeMember<InertAnimation> >& newAnimations, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& result)
60{
61    for (size_t i = 0; i < newAnimations.size(); ++i) {
62        OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > sample = newAnimations[i]->sample(0);
63        if (sample) {
64            copyToActiveInterpolationMap(*sample, result);
65        }
66    }
67}
68
69} // namespace
70
71AnimationStack::AnimationStack()
72{
73}
74
75bool AnimationStack::affects(CSSPropertyID property) const
76{
77    for (size_t i = 0; i < m_effects.size(); ++i) {
78        if (m_effects[i]->animation() && m_effects[i]->animation()->affects(property))
79            return true;
80    }
81    return false;
82}
83
84bool AnimationStack::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
85{
86    for (size_t i = 0; i < m_effects.size(); ++i) {
87        if (m_effects[i]->animation() && m_effects[i]->animation()->hasActiveAnimationsOnCompositor(property))
88            return true;
89    }
90    return false;
91}
92
93WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > AnimationStack::activeInterpolations(AnimationStack* animationStack, const WillBeHeapVector<RawPtrWillBeMember<InertAnimation> >* newAnimations, const WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> >* cancelledAnimationPlayers, Animation::Priority priority, double timelineCurrentTime)
94{
95    // We don't exactly know when new animations will start, but timelineCurrentTime is a good estimate.
96
97    WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result;
98
99    if (animationStack) {
100        WillBeHeapVector<OwnPtrWillBeMember<SampledEffect> >& effects = animationStack->m_effects;
101        // std::sort doesn't work with OwnPtrs
102        nonCopyingSort(effects.begin(), effects.end(), compareEffects);
103        animationStack->simplifyEffects();
104        for (size_t i = 0; i < effects.size(); ++i) {
105            const SampledEffect& effect = *effects[i];
106            if (effect.priority() != priority || (cancelledAnimationPlayers && effect.animation() && cancelledAnimationPlayers->contains(effect.animation()->player())))
107                continue;
108            copyToActiveInterpolationMap(effect.interpolations(), result);
109        }
110    }
111
112    if (newAnimations)
113        copyNewAnimationsToActiveInterpolationMap(*newAnimations, result);
114
115    return result;
116}
117
118void AnimationStack::simplifyEffects()
119{
120    // FIXME: This will need to be updated when we have 'add' keyframes.
121
122    BitArray<numCSSProperties> replacedProperties;
123    for (size_t i = m_effects.size(); i--; ) {
124        SampledEffect& effect = *m_effects[i];
125        effect.removeReplacedInterpolationsIfNeeded(replacedProperties);
126        if (!effect.canChange()) {
127            for (size_t i = 0; i < effect.interpolations().size(); ++i)
128                replacedProperties.set(toStyleInterpolation(effect.interpolations()[i].get())->id());
129        }
130    }
131
132    size_t dest = 0;
133    for (size_t i = 0; i < m_effects.size(); ++i) {
134        if (!m_effects[i]->interpolations().isEmpty()) {
135            m_effects[dest++].swap(m_effects[i]);
136            continue;
137        }
138        if (m_effects[i]->animation())
139            m_effects[i]->animation()->notifySampledEffectRemovedFromAnimationStack();
140    }
141    m_effects.shrink(dest);
142}
143
144void AnimationStack::trace(Visitor* visitor)
145{
146    visitor->trace(m_effects);
147}
148
149bool AnimationStack::getAnimatedBoundingBox(FloatBox& box, CSSPropertyID property) const
150{
151    FloatBox originalBox(box);
152    for (size_t i = 0; i < m_effects.size(); ++i) {
153        if (m_effects[i]->animation() && m_effects[i]->animation()->affects(property)) {
154            Animation* anim = m_effects[i]->animation();
155            if (!anim)
156                continue;
157            const Timing& timing = anim->specifiedTiming();
158            double startRange = 0;
159            double endRange = 1;
160            timing.timingFunction->range(&startRange, &endRange);
161            FloatBox expandingBox(originalBox);
162            if (!CompositorAnimations::instance()->getAnimatedBoundingBox(expandingBox, *anim->effect(), startRange, endRange))
163                return false;
164            box.expandTo(expandingBox);
165        }
166    }
167    return true;
168}
169
170} // namespace blink
171