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 USING_GARBAGE_COLLECTED_MIXIN(ScriptedAnimationController); 52 53 public: 54 explicit ScriptedAnimationController(LocalDOMWindow*); 55 ~ScriptedAnimationController() override = default; 56 57 void Trace(Visitor*) override; NameInHeapSnapshot()58 const char* NameInHeapSnapshot() const override { 59 return "ScriptedAnimationController"; 60 } 61 62 // Runs all the video.requestVideoFrameCallback() callbacks associated with 63 // one HTMLVideoElement. |double| is the current frame time in milliseconds 64 // (e.g. |current_frame_time_ms_|), to be passed as the "now" parameter 65 // when running the callbacks. 66 using ExecuteVfcCallback = base::OnceCallback<void(double)>; 67 68 // Animation frame callbacks are used for requestAnimationFrame(). 69 typedef int CallbackId; 70 CallbackId RegisterFrameCallback( 71 FrameRequestCallbackCollection::FrameCallback*); 72 void CancelFrameCallback(CallbackId); 73 // Returns true if any callback is currently registered. 74 bool HasFrameCallback() const; 75 76 CallbackId RegisterPostFrameCallback( 77 FrameRequestCallbackCollection::FrameCallback*); 78 void CancelPostFrameCallback(CallbackId); 79 80 // Queues up the execution of video.requestVideoFrameCallback() callbacks for 81 // a specific HTMLVideoELement, as part of the next rendering steps. 82 void ScheduleVideoFrameCallbacksExecution(ExecuteVfcCallback); 83 84 // Animation frame events are used for resize events, scroll events, etc. 85 void EnqueueEvent(Event*); 86 void EnqueuePerFrameEvent(Event*); 87 88 // Animation frame tasks are used for Fullscreen. 89 void EnqueueTask(base::OnceClosure); 90 91 // Used for the MediaQueryList change event. 92 void EnqueueMediaQueryChangeListeners( 93 HeapVector<Member<MediaQueryListListener>>&); 94 95 // Invokes callbacks, dispatches events, etc. The order is defined by HTML: 96 // https://html.spec.whatwg.org/C/#event-loop-processing-model 97 void ServiceScriptedAnimations(base::TimeTicks monotonic_time_now); 98 void RunPostFrameCallbacks(); 99 100 void ContextLifecycleStateChanged(mojom::FrameLifecycleState) final; ContextDestroyed()101 void ContextDestroyed() final {} 102 103 void DispatchEventsAndCallbacksForPrinting(); 104 CurrentFrameHadRAF()105 bool CurrentFrameHadRAF() const { return current_frame_had_raf_; } NextFrameHasPendingRAF()106 bool NextFrameHasPendingRAF() const { return next_frame_has_pending_raf_; } 107 108 private: 109 void ScheduleAnimationIfNeeded(); 110 111 void RunTasks(); 112 void DispatchEvents( 113 const AtomicString& event_interface_filter = AtomicString()); 114 void ExecuteFrameCallbacks(); 115 void ExecuteVideoFrameCallbacks(); 116 void CallMediaQueryListListeners(); 117 118 bool HasScheduledFrameTasks() const; 119 120 LocalDOMWindow* GetWindow() const; 121 122 ALWAYS_INLINE bool InsertToPerFrameEventsMap(const Event* event); 123 ALWAYS_INLINE void EraseFromPerFrameEventsMap(const Event* event); 124 125 FrameRequestCallbackCollection callback_collection_; 126 Vector<base::OnceClosure> task_queue_; 127 Vector<ExecuteVfcCallback> vfc_execution_queue_; 128 HeapVector<Member<Event>> event_queue_; 129 using PerFrameEventsMap = 130 HeapHashMap<Member<const EventTarget>, HashSet<const StringImpl*>>; 131 PerFrameEventsMap per_frame_events_; 132 using MediaQueryListListeners = 133 HeapListHashSet<Member<MediaQueryListListener>>; 134 MediaQueryListListeners media_query_list_listeners_; 135 double current_frame_time_ms_ = 0.0; 136 double current_frame_legacy_time_ms_ = 0.0; 137 138 // Used for animation metrics; see cc::CompositorTimingHistory::DidDraw. 139 bool current_frame_had_raf_; 140 bool next_frame_has_pending_raf_; 141 }; 142 143 } // namespace blink 144 145 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_SCRIPTED_ANIMATION_CONTROLLER_H_ 146