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 14 * ANY 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 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * 24 */ 25 26 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_DOM_SCRIPTED_ANIMATION_CONTROLLER_H_ 27 #define THIRD_PARTY_BLINK_RENDERER_CORE_DOM_SCRIPTED_ANIMATION_CONTROLLER_H_ 28 29 #include "third_party/blink/renderer/core/core_export.h" 30 #include "third_party/blink/renderer/core/dom/frame_request_callback_collection.h" 31 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_state_observer.h" 32 #include "third_party/blink/renderer/platform/bindings/name_client.h" 33 #include "third_party/blink/renderer/platform/heap/handle.h" 34 #include "third_party/blink/renderer/platform/wtf/casting.h" 35 #include "third_party/blink/renderer/platform/wtf/functional.h" 36 #include "third_party/blink/renderer/platform/wtf/text/atomic_string.h" 37 #include "third_party/blink/renderer/platform/wtf/text/string_impl.h" 38 #include "third_party/blink/renderer/platform/wtf/vector.h" 39 40 namespace blink { 41 42 class Event; 43 class EventTarget; 44 class LocalDOMWindow; 45 class MediaQueryListListener; 46 47 class CORE_EXPORT ScriptedAnimationController 48 : public GarbageCollected<ScriptedAnimationController>, 49 public ExecutionContextLifecycleStateObserver, 50 public NameClient { 51 public: 52 explicit ScriptedAnimationController(LocalDOMWindow*); 53 ~ScriptedAnimationController() override = default; 54 55 void Trace(Visitor*) const override; NameInHeapSnapshot()56 const char* NameInHeapSnapshot() const override { 57 return "ScriptedAnimationController"; 58 } 59 60 // Runs all the video.requestVideoFrameCallback() callbacks associated with 61 // one HTMLVideoElement. |double| is the current frame time in milliseconds 62 // (e.g. |current_frame_time_ms_|), to be passed as the "now" parameter 63 // when running the callbacks. 64 using ExecuteVfcCallback = base::OnceCallback<void(double)>; 65 66 // Animation frame callbacks are used for requestAnimationFrame(). 67 typedef int CallbackId; 68 CallbackId RegisterFrameCallback(FrameCallback*); 69 void CancelFrameCallback(CallbackId); 70 // Returns true if any callback is currently registered. 71 bool HasFrameCallback() const; 72 73 // Queues up the execution of video.requestVideoFrameCallback() callbacks for 74 // a specific HTMLVideoELement, as part of the next rendering steps. 75 void ScheduleVideoFrameCallbacksExecution(ExecuteVfcCallback); 76 77 // Animation frame events are used for resize events, scroll events, etc. 78 void EnqueueEvent(Event*); 79 void EnqueuePerFrameEvent(Event*); 80 81 // Animation frame tasks are used for Fullscreen. 82 void EnqueueTask(base::OnceClosure); 83 84 // Used for the MediaQueryList change event. 85 void EnqueueMediaQueryChangeListeners( 86 HeapVector<Member<MediaQueryListListener>>&); 87 88 // Invokes callbacks, dispatches events, etc. The order is defined by HTML: 89 // https://html.spec.whatwg.org/C/#event-loop-processing-model 90 void ServiceScriptedAnimations(base::TimeTicks monotonic_time_now); 91 92 void ContextLifecycleStateChanged(mojom::FrameLifecycleState) final; ContextDestroyed()93 void ContextDestroyed() final {} 94 95 void DispatchEventsAndCallbacksForPrinting(); 96 CurrentFrameHadRAF()97 bool CurrentFrameHadRAF() const { return current_frame_had_raf_; } NextFrameHasPendingRAF()98 bool NextFrameHasPendingRAF() const { return next_frame_has_pending_raf_; } 99 100 private: 101 void ScheduleAnimationIfNeeded(); 102 103 void RunTasks(); 104 void DispatchEvents( 105 const AtomicString& event_interface_filter = AtomicString()); 106 void ExecuteFrameCallbacks(); 107 void ExecuteVideoFrameCallbacks(); 108 void CallMediaQueryListListeners(); 109 110 bool HasScheduledFrameTasks() const; 111 112 LocalDOMWindow* GetWindow() const; 113 114 ALWAYS_INLINE bool InsertToPerFrameEventsMap(const Event* event); 115 ALWAYS_INLINE void EraseFromPerFrameEventsMap(const Event* event); 116 117 FrameRequestCallbackCollection callback_collection_; 118 Vector<base::OnceClosure> task_queue_; 119 Vector<ExecuteVfcCallback> vfc_execution_queue_; 120 HeapVector<Member<Event>> event_queue_; 121 using PerFrameEventsMap = 122 HeapHashMap<Member<const EventTarget>, HashSet<const StringImpl*>>; 123 PerFrameEventsMap per_frame_events_; 124 using MediaQueryListListeners = 125 HeapListHashSet<Member<MediaQueryListListener>>; 126 MediaQueryListListeners media_query_list_listeners_; 127 double current_frame_time_ms_ = 0.0; 128 double current_frame_legacy_time_ms_ = 0.0; 129 130 // Used for animation metrics; see cc::CompositorTimingHistory::DidDraw. 131 bool current_frame_had_raf_; 132 bool next_frame_has_pending_raf_; 133 }; 134 135 } // namespace blink 136 137 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_SCRIPTED_ANIMATION_CONTROLLER_H_ 138