Bug 1946787 - Avoid creating redundant GradientCache::OnMaxEntriesBreached tasks...
[gecko.git] / dom / animation / AnimationUtils.h
blobfdca177de8ae8fe723f68a120137676d65a8f8e2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_AnimationUtils_h
8 #define mozilla_dom_AnimationUtils_h
10 #include "mozilla/PseudoStyleType.h"
11 #include "mozilla/TimeStamp.h"
12 #include "mozilla/dom/Nullable.h"
13 #include "nsRFPService.h"
14 #include "nsStringFwd.h"
16 class nsIContent;
17 class nsIFrame;
18 struct JSContext;
20 namespace mozilla {
22 class EffectSet;
24 namespace dom {
25 class Document;
26 class Element;
27 } // namespace dom
29 class AnimationUtils {
30 public:
31 using Document = dom::Document;
33 static dom::Nullable<double> TimeDurationToDouble(
34 const dom::Nullable<TimeDuration>& aTime, RTPCallerType aRTPCallerType) {
35 dom::Nullable<double> result;
37 if (!aTime.IsNull()) {
38 // 0 is an inappropriate mixin for this this area; however CSS Animations
39 // needs to have it's Time Reduction Logic refactored, so it's currently
40 // only clamping for RFP mode. RFP mode gives a much lower time precision,
41 // so we accept the security leak here for now
42 result.SetValue(nsRFPService::ReduceTimePrecisionAsMSecsRFPOnly(
43 aTime.Value().ToMilliseconds(), 0, aRTPCallerType));
46 return result;
49 static dom::Nullable<TimeDuration> DoubleToTimeDuration(
50 const dom::Nullable<double>& aTime) {
51 dom::Nullable<TimeDuration> result;
53 if (!aTime.IsNull()) {
54 result.SetValue(TimeDuration::FromMilliseconds(aTime.Value()));
57 return result;
60 static void LogAsyncAnimationFailure(nsCString& aMessage,
61 const nsIContent* aContent = nullptr);
63 /**
64 * Get the document from the JS context to use when parsing CSS properties.
66 static Document* GetCurrentRealmDocument(JSContext* aCx);
68 /**
69 * Get the document from the global object, or nullptr if the document has
70 * no window, to use when constructing DOM object without entering the
71 * target window's compartment (see KeyframeEffect constructor).
73 static Document* GetDocumentFromGlobal(JSObject* aGlobalObject);
75 /**
76 * Returns true if the given frame has an animated scale.
78 static bool FrameHasAnimatedScale(const nsIFrame* aFrame);
80 /**
81 * Returns true if the given (pseudo-)element has any transitions that are
82 * current (playing or waiting to play) or in effect (e.g. filling forwards).
84 static bool HasCurrentTransitions(const dom::Element* aElement,
85 const PseudoStyleRequest& aPseudoRequest =
86 PseudoStyleRequest::NotPseudo());
88 /**
89 * Returns true if this pseudo style type is supported by animations.
90 * Note: This doesn't include PseudoStyleType::NotPseudo.
92 static bool IsSupportedPseudoForAnimations(PseudoStyleType aType) {
93 // FIXME: Bug 1615469: Support first-line and first-letter for Animation.
94 return PseudoStyle::IsViewTransitionPseudoElement(aType) ||
95 aType == PseudoStyleType::before ||
96 aType == PseudoStyleType::after || aType == PseudoStyleType::marker;
98 static bool IsSupportedPseudoForAnimations(
99 const PseudoStyleRequest& aRequest) {
100 return IsSupportedPseudoForAnimations(aRequest.mType);
104 * Returns true if the difference between |aFirst| and |aSecond| is within
105 * the animation time tolerance (i.e. 1 microsecond).
107 static bool IsWithinAnimationTimeTolerance(const TimeDuration& aFirst,
108 const TimeDuration& aSecond) {
109 if (aFirst == TimeDuration::Forever() ||
110 aSecond == TimeDuration::Forever()) {
111 return aFirst == aSecond;
114 TimeDuration diff = aFirst >= aSecond ? aFirst - aSecond : aSecond - aFirst;
115 return diff <= TimeDuration::FromMicroseconds(1);
118 // Returns the pair of |Element, PseudoStyleRequest| from an element which
119 // could be an element or a pseudo element (i.e. an element used for restyling
120 // and DOM tree.).
122 // Animation module usually uses a pair of (Element*, PseudoStyleRequest) to
123 // represent the animation target.
124 // Note that we sepatate the originating element and PseudoStyleRequest in
125 // Animation code, but store the animations on "::before", "::after", and
126 // "::marker" in the originating element. For view-transition pseudo-elements
127 // and others, we store their KeyframeEffect, timelines, animations, and
128 // transitions in the pseudo-element themself. So use this function carefully.
129 static std::pair<const dom::Element*, PseudoStyleRequest>
130 GetElementPseudoPair(const dom::Element* aElementOrPseudo);
133 } // namespace mozilla
135 #endif