summaryrefslogtreecommitdiffstats
path: root/layout/base/MotionPathUtils.h
blob: d1e143b37a4a46af349d4917754a1664aeac55f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_MotionPathUtils_h
#define mozilla_MotionPathUtils_h

#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Point.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/Maybe.h"
#include "mozilla/ServoStyleConsts.h"
#include "Units.h"

class nsIFrame;

namespace nsStyleTransformMatrix {
class TransformReferenceBox;
}

namespace mozilla {

using RayFunction = StyleRayFunction<StyleAngle>;

namespace layers {
class MotionPathData;
class PathCommand;
}  // namespace layers

struct ResolvedMotionPathData {
  gfx::Point mTranslate;
  float mRotate;
  // The delta value between transform-origin and offset-anchor.
  gfx::Point mShift;
};

struct RayReferenceData {
  // The initial position related to the containing block.
  CSSPoint mInitialPosition;
  // The rect of the containing block.
  CSSRect mContainingBlockRect;

  RayReferenceData() = default;
  explicit RayReferenceData(const nsIFrame* aFrame);

  bool operator==(const RayReferenceData& aOther) const {
    return mInitialPosition == aOther.mInitialPosition &&
           mContainingBlockRect == aOther.mContainingBlockRect;
  }
};

// The collected information for offset-path. We preprocess the value of
// offset-path and use this data for resolving motion path.
struct OffsetPathData {
  enum class Type : uint8_t {
    None,
    Path,
    Ray,
  };

  struct PathData {
    RefPtr<gfx::Path> mGfxPath;
    bool mIsClosedIntervals;
  };

  struct RayData {
    const RayFunction* mRay;
    RayReferenceData mData;
  };

  Type mType;
  union {
    PathData mPath;
    RayData mRay;
  };

  static OffsetPathData None() { return OffsetPathData(); }
  static OffsetPathData Path(const StyleSVGPathData& aPath,
                             already_AddRefed<gfx::Path>&& aGfxPath) {
    const auto& path = aPath._0.AsSpan();
    return OffsetPathData(std::move(aGfxPath),
                          !path.empty() && path.rbegin()->IsClosePath());
  }
  static OffsetPathData Ray(const RayFunction& aRay,
                            const RayReferenceData& aData) {
    return OffsetPathData(&aRay, aData);
  }
  static OffsetPathData Ray(const RayFunction& aRay, RayReferenceData&& aData) {
    return OffsetPathData(&aRay, std::move(aData));
  }

  bool IsNone() const { return mType == Type::None; }
  bool IsPath() const { return mType == Type::Path; }
  bool IsRay() const { return mType == Type::Ray; }

  const PathData& AsPath() const {
    MOZ_ASSERT(IsPath());
    return mPath;
  }

  const RayData& AsRay() const {
    MOZ_ASSERT(IsRay());
    return mRay;
  }

  ~OffsetPathData() {
    switch (mType) {
      case Type::Path:
        mPath.~PathData();
        break;
      case Type::Ray:
        mRay.~RayData();
        break;
      default:
        break;
    }
  }

  OffsetPathData(const OffsetPathData& aOther) : mType(aOther.mType) {
    switch (mType) {
      case Type::Path:
        mPath = aOther.mPath;
        break;
      case Type::Ray:
        mRay = aOther.mRay;
        break;
      default:
        break;
    }
  }

  OffsetPathData(OffsetPathData&& aOther) : mType(aOther.mType) {
    switch (mType) {
      case Type::Path:
        mPath = std::move(aOther.mPath);
        break;
      case Type::Ray:
        mRay = std::move(aOther.mRay);
        break;
      default:
        break;
    }
  }

 private:
  OffsetPathData() : mType(Type::None) {}
  OffsetPathData(already_AddRefed<gfx::Path>&& aPath, bool aIsClosed)
      : mType(Type::Path), mPath{std::move(aPath), aIsClosed} {}
  OffsetPathData(const RayFunction* aRay, RayReferenceData&& aRef)
      : mType(Type::Ray), mRay{aRay, std::move(aRef)} {}
  OffsetPathData(const RayFunction* aRay, const RayReferenceData& aRef)
      : mType(Type::Ray), mRay{aRay, aRef} {}
  OffsetPathData& operator=(const OffsetPathData&) = delete;
  OffsetPathData& operator=(OffsetPathData&&) = delete;
};

// MotionPathUtils is a namespace class containing utility functions related to
// processing motion path in the [motion-1].
// https://drafts.fxtf.org/motion-1/
class MotionPathUtils final {
  using TransformReferenceBox = nsStyleTransformMatrix::TransformReferenceBox;

 public:
  // SVG frames (unlike other frames) have a reference box that can be (and
  // typically is) offset from the TopLeft() of the frame.
  //
  // In motion path, we have to make sure the object is aligned with offset-path
  // when using content area, so we should tweak the anchor point by a given
  // offset.
  static CSSPoint ComputeAnchorPointAdjustment(const nsIFrame& aFrame);

  /**
   * Generate the motion path transform result. This function may be called on
   * the compositor thread.
   */
  static Maybe<ResolvedMotionPathData> ResolveMotionPath(
      const OffsetPathData& aPath, const LengthPercentage& aDistance,
      const StyleOffsetRotate& aRotate, const StylePositionOrAuto& aAnchor,
      const CSSPoint& aTransformOrigin, TransformReferenceBox&,
      const CSSPoint& aAnchorPointAdjustment);

  /**
   * Generate the motion path transform result with |nsIFrame|. This is only
   * called in the main thread.
   */
  static Maybe<ResolvedMotionPathData> ResolveMotionPath(
      const nsIFrame* aFrame, TransformReferenceBox&);

  /**
   * Generate the motion path transfrom result with styles and
   * layers::MotionPathData.
   * This is only called by the compositor.
   */
  static Maybe<ResolvedMotionPathData> ResolveMotionPath(
      const StyleOffsetPath* aPath, const StyleLengthPercentage* aDistance,
      const StyleOffsetRotate* aRotate, const StylePositionOrAuto* aAnchor,
      const Maybe<layers::MotionPathData>& aMotionPathData,
      TransformReferenceBox&, gfx::Path* aCachedMotionPath);

  /**
   * Normalize StyleSVGPathData.
   *
   * The algorithm of normalization is the same as normalize() in
   * servo/components/style/values/specified/svg_path.rs
   * FIXME: Bug 1489392: We don't have to normalize the path here if we accept
   * the spec issue which would like to normalize svg paths at computed time.
   * https://github.com/w3c/svgwg/issues/321
   */
  static StyleSVGPathData NormalizeSVGPathData(const StyleSVGPathData& aPath);

  /**
   * Build a gfx::Path from the computed svg path. We should give it a path
   * builder. If |aPathBuilder| is nullptr, we return null path.
   * */
  static already_AddRefed<gfx::Path> BuildPath(const StyleSVGPathData& aPath,
                                               gfx::PathBuilder* aPathBuilder);

  /**
   * Get a path builder for compositor.
   */
  static already_AddRefed<gfx::PathBuilder> GetCompositorPathBuilder();
};

}  // namespace mozilla

#endif  // mozilla_MotionPathUtils_h