15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "config.h"
3281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#include "core/animation/Animation.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/Dictionary.h"
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ExceptionState.h"
36f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#include "core/animation/ActiveAnimations.h"
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/animation/AnimationPlayer.h"
38d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "core/animation/AnimationTimeline.h"
3951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "core/animation/CompositorAnimations.h"
40c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "core/animation/Interpolation.h"
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/animation/KeyframeEffectModel.h"
4281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)#include "core/dom/Element.h"
4307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch#include "core/frame/UseCounter.h"
4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/rendering/RenderLayer.h"
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
46c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
48c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* target, PassRefPtrWillBeRawPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtrWillBeRawPtr<EventDelegate> eventDelegate)
4953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
50d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    return adoptRefWillBeNoop(new Animation(target, effect, timing, priority, eventDelegate));
5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
53d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* element, PassRefPtrWillBeRawPtr<AnimationEffect> effect, const Dictionary& timingInputDictionary)
5409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
5509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return create(element, effect, TimingInput::convert(timingInputDictionary));
5709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
58d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* element, PassRefPtrWillBeRawPtr<AnimationEffect> effect, double duration)
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
6009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return create(element, effect, TimingInput::convert(duration));
6209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
63d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* element, PassRefPtrWillBeRawPtr<AnimationEffect> effect)
6409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
6509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return create(element, effect, Timing());
6709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
68d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, const Dictionary& timingInputDictionary, ExceptionState& exceptionState)
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
7110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (element)
7210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        UseCounter::count(element->document(), UseCounter::AnimationConstructorKeyframeListEffectObjectTiming);
7307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return create(element, EffectInput::convert(element, keyframeDictionaryVector, exceptionState), TimingInput::convert(timingInputDictionary));
7409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
75d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, double duration, ExceptionState& exceptionState)
7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
7810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (element)
7910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        UseCounter::count(element->document(), UseCounter::AnimationConstructorKeyframeListEffectDoubleTiming);
8007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return create(element, EffectInput::convert(element, keyframeDictionaryVector, exceptionState), TimingInput::convert(duration));
8109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
82d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)PassRefPtrWillBeRawPtr<Animation> Animation::create(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, ExceptionState& exceptionState)
8309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
84d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
8510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (element)
8610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        UseCounter::count(element->document(), UseCounter::AnimationConstructorKeyframeListEffectNoTiming);
8707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return create(element, EffectInput::convert(element, keyframeDictionaryVector, exceptionState), Timing());
8809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
8909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
90c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)Animation::Animation(Element* target, PassRefPtrWillBeRawPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtrWillBeRawPtr<EventDelegate> eventDelegate)
91d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    : AnimationNode(timing, eventDelegate)
9293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    , m_target(target)
9381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    , m_effect(effect)
94d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    , m_sampledEffect(nullptr)
95f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    , m_priority(priority)
9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
97d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#if !ENABLE(OILPAN)
986f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (m_target)
996f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        m_target->ensureActiveAnimations().addAnimation(this);
100d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#endif
1016f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch}
1026f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
1036f543c786fc42989f552b4daa774ca5ff32fa697Ben MurdochAnimation::~Animation()
1046f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch{
105d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#if !ENABLE(OILPAN)
1066f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (m_target)
1076f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        m_target->activeAnimations()->notifyAnimationDestroyed(this);
108d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#endif
10953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
111d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void Animation::attach(AnimationPlayer* player)
11251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
113a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    if (m_target) {
114c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        m_target->ensureActiveAnimations().players().add(player);
115a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        m_target->setNeedsAnimationStyleRecalc();
116a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    }
117d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationNode::attach(player);
11851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
11951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
120d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void Animation::detach()
12153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
122f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    if (m_target)
123c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        m_target->activeAnimations()->players().remove(player());
1246f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (m_sampledEffect)
12583750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch        clearEffects();
126d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationNode::detach();
12753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
1285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
129a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdochvoid Animation::specifiedTimingChanged()
130a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch{
13110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (player()) {
13210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        // FIXME: Needs to consider groups when added.
13310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        ASSERT(player()->source() == this);
1347242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        player()->setCompositorPending(true);
13510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    }
136a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch}
137a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
138f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)static AnimationStack& ensureAnimationStack(Element* element)
13981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles){
140d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return element->ensureActiveAnimations().defaultStack();
14183750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch}
14283750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch
1436f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdochvoid Animation::applyEffects()
14483750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch{
145a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ASSERT(isInEffect());
146a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    ASSERT(player());
147bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if (!m_target || !m_effect)
148d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return;
149bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
150a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    double iteration = currentIteration();
151a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ASSERT(iteration >= 0);
152a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // FIXME: Handle iteration values which overflow int.
15310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > interpolations = m_effect->sample(static_cast<int>(iteration), timeFraction(), iterationDuration());
1546f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (m_sampledEffect) {
1556f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        m_sampledEffect->setInterpolations(interpolations.release());
1566f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    } else if (!interpolations->isEmpty()) {
157d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        OwnPtrWillBeRawPtr<SampledEffect> sampledEffect = SampledEffect::create(this, interpolations.release());
1586f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        m_sampledEffect = sampledEffect.get();
1596f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        ensureAnimationStack(m_target).add(sampledEffect.release());
1606f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    } else {
1616f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        return;
1626f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    }
1636f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
164a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    m_target->setNeedsAnimationStyleRecalc();
16593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
16681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
16793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)void Animation::clearEffects()
16893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
16983750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch    ASSERT(player());
1706f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    ASSERT(m_sampledEffect);
1716f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
1726f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    m_sampledEffect->clear();
173d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    m_sampledEffect = nullptr;
174a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    cancelAnimationOnCompositor();
175a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    m_target->setNeedsAnimationStyleRecalc();
17609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    invalidate();
17793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
17893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
179d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void Animation::updateChildrenAndEffects() const
18093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
181bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if (!m_effect)
182d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return;
18383750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch    if (isInEffect())
1846f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        const_cast<Animation*>(this)->applyEffects();
1856f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    else if (m_sampledEffect)
18693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        const_cast<Animation*>(this)->clearEffects();
18781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)}
18881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
18909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)double Animation::calculateTimeToEffectChange(bool forwards, double localTime, double timeToNextIteration) const
190bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles){
19110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    const double start = startTimeInternal() + specifiedTiming().startDelay;
19210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    const double end = start + activeDurationInternal();
19309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
19451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    switch (phase()) {
1957242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    case PhaseNone:
1967242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return std::numeric_limits<double>::infinity();
197bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    case PhaseBefore:
19809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        ASSERT(start >= localTime);
19909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return forwards
20009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            ? start - localTime
20109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            : std::numeric_limits<double>::infinity();
202bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    case PhaseActive:
20309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (forwards && hasActiveAnimationsOnCompositor()) {
20451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            // Need service to apply fill / fire events.
205a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            const double timeToEnd = end - localTime;
206a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            if (hasEvents()) {
207a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch                return std::min(timeToEnd, timeToNextIteration);
208a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            } else {
209a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch                return timeToEnd;
210a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            }
21151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        }
212bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return 0;
213bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    case PhaseAfter:
21409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        ASSERT(localTime >= end);
215bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        // If this Animation is still in effect then it will need to update
216bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        // when its parent goes out of effect. We have no way of knowing when
217bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        // that will be, however, so the parent will need to supply it.
21809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return forwards
21909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            ? std::numeric_limits<double>::infinity()
22009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            : localTime - end;
221bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    default:
222bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        ASSERT_NOT_REACHED();
22309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return std::numeric_limits<double>::infinity();
224bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    }
225bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)}
226bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
2276f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdochvoid Animation::notifySampledEffectRemovedFromAnimationStack()
2286f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch{
2296f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    ASSERT(m_sampledEffect);
230d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    m_sampledEffect = nullptr;
2316f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch}
2326f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
233d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#if !ENABLE(OILPAN)
2346f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdochvoid Animation::notifyElementDestroyed()
2356f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch{
2366f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    // If our player is kept alive just by the sampledEffect, we might get our
2376f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    // destructor called when we call SampledEffect::clear(), so we need to
2386f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    // clear m_sampledEffect first.
239d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    m_target = nullptr;
24010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    clearEventDelegate();
2416f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    SampledEffect* sampledEffect = m_sampledEffect;
242d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    m_sampledEffect = nullptr;
2436f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (sampledEffect)
2446f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        sampledEffect->clear();
2456f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch}
246d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#endif
2476f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
2487242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccibool Animation::isCandidateForAnimationOnCompositor(double playerPlaybackRate) const
24951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
25051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (!effect() || !m_target)
25151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return false;
2527242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    return CompositorAnimations::instance()->isCandidateForAnimationOnCompositor(specifiedTiming(), *effect(), playerPlaybackRate);
2537242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci}
2547242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
2557242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccibool Animation::maybeStartAnimationOnCompositor(double startTime, double currentTime)
2567242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci{
2577242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    return maybeStartAnimationOnCompositor(startTime, currentTime, 1);
25851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
25951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
2607242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccibool Animation::maybeStartAnimationOnCompositor(double startTime, double currentTime, double playerPlaybackRate)
26151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
26251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    ASSERT(!hasActiveAnimationsOnCompositor());
2637242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (!isCandidateForAnimationOnCompositor(playerPlaybackRate))
26451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return false;
2656f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (!CompositorAnimations::instance()->canStartAnimationOnCompositor(*m_target))
26651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return false;
2677242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (!CompositorAnimations::instance()->startAnimationOnCompositor(*m_target, startTime, currentTime, specifiedTiming(), *effect(), m_compositorAnimationIds, playerPlaybackRate))
26851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return false;
26951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    ASSERT(!m_compositorAnimationIds.isEmpty());
27051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return true;
27151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
27251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
27351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool Animation::hasActiveAnimationsOnCompositor() const
27451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
27551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return !m_compositorAnimationIds.isEmpty();
27651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
27751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
27851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool Animation::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
27951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
28051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return hasActiveAnimationsOnCompositor() && affects(property);
28151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
28251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
28351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)bool Animation::affects(CSSPropertyID property) const
28451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
28551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return m_effect && m_effect->affects(property);
28651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
28751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
28851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void Animation::cancelAnimationOnCompositor()
28951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
290a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    // FIXME: cancelAnimationOnCompositor is called from withins style recalc.
291a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    // This queries compositingState, which is not necessarily up to date.
292a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    // https://code.google.com/p/chromium/issues/detail?id=339847
293a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    DisableCompositingQueryAsserts disabler;
29451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (!hasActiveAnimationsOnCompositor())
29551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return;
29651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (!m_target || !m_target->renderer())
29751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return;
29851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
2996f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        CompositorAnimations::instance()->cancelAnimationOnCompositor(*m_target, m_compositorAnimationIds[i]);
30051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    m_compositorAnimationIds.clear();
3017242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    player()->setCompositorPending(true);
30251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
30351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
30451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void Animation::pauseAnimationForTestingOnCompositor(double pauseTime)
30551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
30651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    ASSERT(hasActiveAnimationsOnCompositor());
30751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (!m_target || !m_target->renderer())
30851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return;
30951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
3106f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        CompositorAnimations::instance()->pauseAnimationForTestingOnCompositor(*m_target, m_compositorAnimationIds[i], pauseTime);
31151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
31251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
313d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void Animation::trace(Visitor* visitor)
314d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles){
315d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    visitor->trace(m_target);
316d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    visitor->trace(m_effect);
317d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    visitor->trace(m_sampledEffect);
318d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AnimationNode::trace(visitor);
319d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)}
320d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
321c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
322