1/*
2 * Copyright (C) 2011 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
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. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 */
25
26#pragma once
27
28#include "DOMHighResTimeStamp.h"
29#include "Timer.h"
30#include <wtf/OptionSet.h>
31#include <wtf/RefCounted.h>
32#include <wtf/RefPtr.h>
33#include <wtf/Vector.h>
34
35namespace WebCore {
36
37class Document;
38class Page;
39class RequestAnimationFrameCallback;
40
41class ScriptedAnimationController : public RefCounted<ScriptedAnimationController>
42{
43public:
44 static Ref<ScriptedAnimationController> create(Document& document)
45 {
46 return adoptRef(*new ScriptedAnimationController(document));
47 }
48 ~ScriptedAnimationController();
49 void clearDocumentPointer() { m_document = nullptr; }
50 bool requestAnimationFrameEnabled() const;
51
52 typedef int CallbackId;
53
54 CallbackId registerCallback(Ref<RequestAnimationFrameCallback>&&);
55 void cancelCallback(CallbackId);
56 void serviceRequestAnimationFrameCallbacks(DOMHighResTimeStamp timestamp);
57
58 void suspend();
59 void resume();
60
61 enum class ThrottlingReason {
62 VisuallyIdle = 1 << 0,
63 OutsideViewport = 1 << 1,
64 LowPowerMode = 1 << 2,
65 NonInteractedCrossOriginFrame = 1 << 3,
66 };
67 void addThrottlingReason(ThrottlingReason);
68 void removeThrottlingReason(ThrottlingReason);
69
70 WEBCORE_EXPORT bool isThrottled() const;
71 WEBCORE_EXPORT Seconds interval() const;
72
73private:
74 ScriptedAnimationController(Document&);
75
76 void scheduleAnimation();
77 void animationTimerFired();
78
79 Page* page() const;
80
81 typedef Vector<RefPtr<RequestAnimationFrameCallback>> CallbackList;
82 CallbackList m_callbacks;
83
84 WeakPtr<Document> m_document;
85 CallbackId m_nextCallbackId { 0 };
86 int m_suspendCount { 0 };
87
88 Timer m_animationTimer;
89 double m_lastAnimationFrameTimestamp { 0 };
90
91#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
92 OptionSet<ThrottlingReason> m_throttlingReasons;
93 bool m_isUsingTimer { false };
94#endif
95};
96
97} // namespace WebCore
98