1/*
2 * Copyright (C) 2017 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#pragma once
27
28#include "AnimationTimeline.h"
29#include "DocumentTimelineOptions.h"
30#include "GenericTaskQueue.h"
31#include "Timer.h"
32#include <wtf/Markable.h>
33#include <wtf/Ref.h>
34
35namespace WebCore {
36
37class AnimationPlaybackEvent;
38class RenderElement;
39
40class DocumentTimeline final : public AnimationTimeline
41{
42public:
43 static Ref<DocumentTimeline> create(Document&);
44 static Ref<DocumentTimeline> create(Document&, DocumentTimelineOptions&&);
45 ~DocumentTimeline();
46
47 bool isDocumentTimeline() const final { return true; }
48
49 Vector<RefPtr<WebAnimation>> getAnimations() const;
50
51 Document* document() const { return m_document.get(); }
52
53 Optional<Seconds> currentTime() override;
54
55 void animationTimingDidChange(WebAnimation&) override;
56 void removeAnimation(WebAnimation&) override;
57 void animationWasAddedToElement(WebAnimation&, Element&) final;
58 void animationWasRemovedFromElement(WebAnimation&, Element&) final;
59
60 // If possible, compute the visual extent of any transform animation on the given renderer
61 // using the given rect, returning the result in the rect. Return false if there is some
62 // transform animation but we were unable to cheaply compute its effect on the extent.
63 bool computeExtentOfAnimation(RenderElement&, LayoutRect&) const;
64 std::unique_ptr<RenderStyle> animatedStyleForRenderer(RenderElement& renderer);
65 bool isRunningAnimationOnRenderer(RenderElement&, CSSPropertyID) const;
66 bool isRunningAcceleratedAnimationOnRenderer(RenderElement&, CSSPropertyID) const;
67 void animationAcceleratedRunningStateDidChange(WebAnimation&);
68 void applyPendingAcceleratedAnimations();
69 bool runningAnimationsForElementAreAllAccelerated(Element&) const;
70 bool resolveAnimationsForElement(Element&, RenderStyle&);
71 void detachFromDocument();
72
73 void enqueueAnimationPlaybackEvent(AnimationPlaybackEvent&);
74
75 void updateAnimationsAndSendEvents(DOMHighResTimeStamp timestamp);
76
77 void updateThrottlingState();
78 WEBCORE_EXPORT Seconds animationInterval() const;
79 WEBCORE_EXPORT void suspendAnimations();
80 WEBCORE_EXPORT void resumeAnimations();
81 WEBCORE_EXPORT bool animationsAreSuspended();
82 WEBCORE_EXPORT unsigned numberOfActiveAnimationsForTesting() const;
83 WEBCORE_EXPORT Vector<std::pair<String, double>> acceleratedAnimationsForElement(Element&) const;
84 WEBCORE_EXPORT unsigned numberOfAnimationTimelineInvalidationsForTesting() const;
85
86private:
87 DocumentTimeline(Document&, Seconds);
88
89 DOMHighResTimeStamp liveCurrentTime() const;
90 void cacheCurrentTime(DOMHighResTimeStamp);
91 void maybeClearCachedCurrentTime();
92 void scheduleInvalidationTaskIfNeeded();
93 void performInvalidationTask();
94 void scheduleAnimationResolution();
95 void unscheduleAnimationResolution();
96 void internalUpdateAnimationsAndSendEvents();
97 void performEventDispatchTask();
98 void updateListOfElementsWithRunningAcceleratedAnimationsForElement(Element&);
99 void transitionDidComplete(RefPtr<CSSTransition>);
100 void scheduleNextTick();
101
102 Timer m_tickScheduleTimer;
103 GenericTaskQueue<Timer> m_currentTimeClearingTaskQueue;
104 HashSet<RefPtr<WebAnimation>> m_acceleratedAnimationsPendingRunningStateChange;
105 HashSet<Element*> m_elementsWithRunningAcceleratedAnimations;
106 Vector<Ref<AnimationPlaybackEvent>> m_pendingAnimationEvents;
107 RefPtr<Document> m_document;
108 Markable<Seconds, Seconds::MarkableTraits> m_cachedCurrentTime;
109 Seconds m_originTime;
110 unsigned m_numberOfAnimationTimelineInvalidationsForTesting { 0 };
111 bool m_isSuspended { false };
112 bool m_waitingOnVMIdle { false };
113 bool m_animationResolutionScheduled { false };
114};
115
116} // namespace WebCore
117
118SPECIALIZE_TYPE_TRAITS_ANIMATION_TIMELINE(DocumentTimeline, isDocumentTimeline())
119