Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2008 Apple 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef SMILTimeContainer_h
     27 #define SMILTimeContainer_h
     28 
     29 #include "core/dom/QualifiedName.h"
     30 #include "core/svg/animation/SMILTime.h"
     31 #include "platform/Timer.h"
     32 #include "platform/heap/Handle.h"
     33 #include "wtf/HashMap.h"
     34 #include "wtf/HashSet.h"
     35 #include "wtf/PassRefPtr.h"
     36 #include "wtf/RefCounted.h"
     37 #include "wtf/text/StringHash.h"
     38 #include "wtf/text/WTFString.h"
     39 
     40 namespace blink {
     41 
     42 class Document;
     43 class SVGElement;
     44 class SVGSMILElement;
     45 class SVGSVGElement;
     46 
     47 class SMILTimeContainer : public RefCountedWillBeGarbageCollectedFinalized<SMILTimeContainer>  {
     48 public:
     49     static PassRefPtrWillBeRawPtr<SMILTimeContainer> create(SVGSVGElement& owner) { return adoptRefWillBeNoop(new SMILTimeContainer(owner)); }
     50     ~SMILTimeContainer();
     51 
     52     void schedule(SVGSMILElement*, SVGElement*, const QualifiedName&);
     53     void unschedule(SVGSMILElement*, SVGElement*, const QualifiedName&);
     54     void notifyIntervalsChanged();
     55 
     56     SMILTime elapsed() const;
     57 
     58     bool isPaused() const;
     59     bool isStarted() const;
     60 
     61     void begin();
     62     void pause();
     63     void resume();
     64     void setElapsed(SMILTime);
     65 
     66     void serviceAnimations(double monotonicAnimationStartTime);
     67     bool hasAnimations() const;
     68 
     69     void setDocumentOrderIndexesDirty() { m_documentOrderIndexesDirty = true; }
     70 
     71     void trace(Visitor*);
     72 
     73 private:
     74     explicit SMILTimeContainer(SVGSVGElement& owner);
     75 
     76     enum FrameSchedulingState {
     77         // No frame scheduled.
     78         Idle,
     79         // Scheduled a wakeup to update the animation values.
     80         SynchronizeAnimations,
     81         // Scheduled a wakeup to trigger an animation frame.
     82         FutureAnimationFrame,
     83         // Scheduled a animation frame for continuous update.
     84         AnimationFrame
     85     };
     86 
     87     bool isTimelineRunning() const;
     88     void scheduleAnimationFrame(SMILTime fireTime);
     89     void cancelAnimationFrame();
     90     void wakeupTimerFired(Timer<SMILTimeContainer>*);
     91     void updateAnimationsAndScheduleFrameIfNeeded(SMILTime elapsed, bool seekToTime = false);
     92     SMILTime updateAnimations(SMILTime elapsed, bool seekToTime = false);
     93     void serviceOnNextFrame();
     94     void scheduleWakeUp(double delayTime, FrameSchedulingState);
     95     bool hasPendingSynchronization() const;
     96 
     97     void updateDocumentOrderIndexes();
     98     double lastResumeTime() const { return m_resumeTime ? m_resumeTime : m_beginTime; }
     99 
    100     Document& document() const;
    101     double currentTime() const;
    102 
    103     double m_beginTime;
    104     double m_pauseTime;
    105     double m_resumeTime;
    106     double m_accumulatedActiveTime;
    107     double m_presetStartTime;
    108 
    109     FrameSchedulingState m_frameSchedulingState;
    110     bool m_documentOrderIndexesDirty;
    111 
    112     Timer<SMILTimeContainer> m_wakeupTimer;
    113 
    114     typedef pair<RawPtrWillBeWeakMember<SVGElement>, QualifiedName> ElementAttributePair;
    115     typedef WillBeHeapLinkedHashSet<RawPtrWillBeWeakMember<SVGSMILElement> > AnimationsLinkedHashSet;
    116     typedef WillBeHeapHashMap<ElementAttributePair, OwnPtrWillBeMember<AnimationsLinkedHashSet> > GroupedAnimationsMap;
    117     GroupedAnimationsMap m_scheduledAnimations;
    118 
    119     SVGSVGElement& m_ownerSVGElement;
    120 
    121 #if ENABLE(ASSERT)
    122     bool m_preventScheduledAnimationsChanges;
    123 #endif
    124 };
    125 }
    126 
    127 #endif // SMILTimeContainer_h
    128