Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / gfx / layers / AnimationHelper.h
blob7afe9a7056a02d5c660bdaccd7f8c923b1fabead
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_layers_AnimationHelper_h
8 #define mozilla_layers_AnimationHelper_h
10 #include "mozilla/dom/Nullable.h"
11 #include "mozilla/layers/AnimationStorageData.h"
12 #include "mozilla/layers/LayersMessages.h" // for TransformData, etc
13 #include "mozilla/webrender/WebRenderTypes.h" // for RenderRoot
14 #include "mozilla/TimeStamp.h" // for TimeStamp
15 #include "mozilla/TimingParams.h"
16 #include "mozilla/Types.h" // for SideBits
17 #include "X11UndefineNone.h"
18 #include <unordered_map>
20 namespace mozilla::layers {
21 class Animation;
22 class APZSampler;
23 class CompositorAnimationStorage;
24 struct AnimatedValue;
26 typedef nsTArray<layers::Animation> AnimationArray;
28 /**
29 * This utility class allows reusing code between the webrender and
30 * non-webrender compositor-side implementations. It provides
31 * utility functions for sampling animations at particular timestamps.
33 class AnimationHelper {
34 public:
35 struct SampleResult {
36 enum class Type : uint8_t { None, Skipped, Sampled };
37 enum class Reason : uint8_t { None, ScrollToDelayPhase };
38 Type mType = Type::None;
39 Reason mReason = Reason::None;
41 SampleResult() = default;
42 SampleResult(Type aType, Reason aReason) : mType(aType), mReason(aReason) {}
44 static SampleResult Skipped() { return {Type::Skipped, Reason::None}; }
45 static SampleResult Sampled() { return {Type::Sampled, Reason::None}; }
47 bool IsNone() { return mType == Type::None; }
48 bool IsSkipped() { return mType == Type::Skipped; }
49 bool IsSampled() { return mType == Type::Sampled; }
52 /**
53 * Sample animations based on a given time stamp for a element(layer) with
54 * its animation data.
55 * Generally |aPreviousFrameTime| is used for the sampling if it's
56 * supplied to make the animation more in sync with other animations on the
57 * main-thread. But in the case where the animation just started at the time
58 * when the animation was sent to the compositor, |aCurrentFrameTime| is used
59 * for sampling instead to avoid flicker.
61 * Returns SampleResult::None if none of the animations are producing a result
62 * (e.g. they are in the delay phase with no backwards fill),
63 * SampleResult::Skipped if the animation output did not change since the last
64 * call of this function,
65 * SampleResult::Sampled if the animation output was updated.
67 * The only exception is the scroll-driven animation. When the user move the
68 * scrollbar to make the animations go from active phase to delay phase, this
69 * returns SampleResult::None but sets its |mReason| to
70 * Reason::ScrollToDelayPhase. The caller can check this flag so we can store
71 * the base style into the hash table to make sure we have the correct
72 * rendering result. The base style stays in the hash table until we sync with
73 * main thread.
75 * Using the same example from ExtractAnimations (below):
77 * Input |aPropertyAnimationGroups| (ignoring the base animation style):
79 * [
80 * Group A: [ { rotate, Animation A }, { rotate, Animation B } ],
81 * Group B: [ { scale, Animation B } ],
82 * Group C: [ { transform, Animation A }, { transform, Animation B } ],
83 * ]
85 * For each property group, this function interpolates each animation in turn,
86 * using the result of interpolating one animation as input for the next such
87 * that it reduces each property group to a single output value:
89 * [
90 * { rotate, StyleAnimationValue },
91 * { scale, StyleAnimationValue },
92 * { transform, StyleAnimationValue },
93 * ]
95 * For transform animations, the caller (SampleAnimations) will combine the
96 * result of the various transform properties into a final matrix.
98 static SampleResult SampleAnimationForEachNode(
99 const APZSampler* aAPZSampler, const LayersId& aLayersId,
100 const MutexAutoLock& aProofOfMapLock, TimeStamp aPreviousFrameTime,
101 TimeStamp aCurrentFrameTime, const AnimatedValue* aPreviousValue,
102 nsTArray<PropertyAnimationGroup>& aPropertyAnimationGroups,
103 nsTArray<RefPtr<StyleAnimationValue>>& aAnimationValues);
106 * Extract organized animation data by property into an array of
107 * PropertyAnimationGroup objects.
109 * For example, suppose we have the following animations:
111 * Animation A: [ transform, rotate ]
112 * Animation B: [ rotate, scale ]
113 * Animation C: [ transform ]
114 * Animation D: [ opacity ]
116 * When we go to send transform-like properties to the compositor, we
117 * sort them as follows:
120 * { rotate: Animation A (rotate segments only) },
121 * { rotate: Animation B ( " " ) },
122 * { scale: Animation B (scale segments only) },
123 * { transform: Animation A (transform segments only) },
124 * { transform: Animation C ( " " ) },
127 * In this function, we group these animations together by property producing
128 * output such as the following:
131 * [ { rotate, Animation A }, { rotate, Animation B } ],
132 * [ { scale, Animation B } ],
133 * [ { transform, Animation A }, { transform, Animation B } ],
136 * In the process of grouping these animations, we also convert their values
137 * from the rather compact representation we use for transferring across the
138 * IPC boundary into something we can readily use for sampling.
140 * Note: the animation groups:
141 * 1. transform-like properties: transfrom, translate, rotate, scale,
142 * offset-*.
143 * 2. opacity property: opacity.
144 * 3. background color property: background-color.
146 static AnimationStorageData ExtractAnimations(
147 const LayersId& aLayersId, const AnimationArray& aAnimations);
150 * Get a unique id to represent the compositor animation between child
151 * and parent side. This id will be used as a key to store animation
152 * data in the CompositorAnimationStorage per compositor.
153 * Each layer on the content side calls this when it gets new animation
154 * data.
156 static uint64_t GetNextCompositorAnimationsId();
159 * Convert an array of animation values into a matrix given the corresponding
160 * transform parameters. |aValue| must be a transform-like value
161 * (e.g. transform, translate etc.).
163 static gfx::Matrix4x4 ServoAnimationValueToMatrix4x4(
164 const nsTArray<RefPtr<StyleAnimationValue>>& aValue,
165 const TransformData& aTransformData, gfx::Path* aCachedMotionPath);
168 * Returns true if |aPrerenderedRect| transformed by |aTransform| were
169 * composited in |aClipRect| there appears area which wasn't pre-rendered
170 * on the main-thread. I.e. checkerboarding.
172 static bool ShouldBeJank(const LayoutDeviceRect& aPrerenderedRect,
173 SideBits aOverflowedSides,
174 const gfx::Matrix4x4& aTransform,
175 const ParentLayerRect& aClipRect);
178 } // namespace mozilla::layers
180 #endif // mozilla_layers_AnimationHelper_h