Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/style/nsStyleTransformMatrix.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
/*
8
 * A class used for intermediate representations of the -moz-transform property.
9
 */
10
11
#include "nsStyleTransformMatrix.h"
12
#include "nsCSSValue.h"
13
#include "nsLayoutUtils.h"
14
#include "nsPresContext.h"
15
#include "nsSVGUtils.h"
16
#include "nsCSSKeywords.h"
17
#include "mozilla/ServoBindings.h"
18
#include "mozilla/StyleAnimationValue.h"
19
#include "gfxMatrix.h"
20
#include "gfxQuaternion.h"
21
22
using namespace mozilla;
23
using namespace mozilla::gfx;
24
25
namespace nsStyleTransformMatrix {
26
27
/* Note on floating point precision: The transform matrix is an array
28
 * of single precision 'float's, and so are most of the input values
29
 * we get from the style system, but intermediate calculations
30
 * involving angles need to be done in 'double'.
31
 */
32
33
34
// Define UNIFIED_CONTINUATIONS here and in nsDisplayList.cpp
35
// to have the transform property try
36
// to transform content with continuations as one unified block instead of
37
// several smaller ones.  This is currently disabled because it doesn't work
38
// correctly, since when the frames are initially being reflowed, their
39
// continuations all compute their bounding rects independently of each other
40
// and consequently get the wrong value.
41
//#define UNIFIED_CONTINUATIONS
42
43
void
44
TransformReferenceBox::EnsureDimensionsAreCached()
45
0
{
46
0
  if (mIsCached) {
47
0
    return;
48
0
  }
49
0
50
0
  MOZ_ASSERT(mFrame);
51
0
52
0
  mIsCached = true;
53
0
54
0
  if (mFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT) {
55
0
    if (!nsLayoutUtils::SVGTransformBoxEnabled()) {
56
0
      mX = -mFrame->GetPosition().x;
57
0
      mY = -mFrame->GetPosition().y;
58
0
      Size contextSize = nsSVGUtils::GetContextSize(mFrame);
59
0
      mWidth = nsPresContext::CSSPixelsToAppUnits(contextSize.width);
60
0
      mHeight = nsPresContext::CSSPixelsToAppUnits(contextSize.height);
61
0
    } else
62
0
    if (mFrame->StyleDisplay()->mTransformBox == StyleGeometryBox::FillBox) {
63
0
      // Percentages in transforms resolve against the SVG bbox, and the
64
0
      // transform is relative to the top-left of the SVG bbox.
65
0
      nsRect bboxInAppUnits =
66
0
        nsLayoutUtils::ComputeGeometryBox(const_cast<nsIFrame*>(mFrame),
67
0
                                          StyleGeometryBox::FillBox);
68
0
      // The mRect of an SVG nsIFrame is its user space bounds *including*
69
0
      // stroke and markers, whereas bboxInAppUnits is its user space bounds
70
0
      // including fill only.  We need to note the offset of the reference box
71
0
      // from the frame's mRect in mX/mY.
72
0
      mX = bboxInAppUnits.x - mFrame->GetPosition().x;
73
0
      mY = bboxInAppUnits.y - mFrame->GetPosition().y;
74
0
      mWidth = bboxInAppUnits.width;
75
0
      mHeight = bboxInAppUnits.height;
76
0
    } else {
77
0
      // The value 'border-box' is treated as 'view-box' for SVG content.
78
0
      MOZ_ASSERT(mFrame->StyleDisplay()->mTransformBox ==
79
0
                   StyleGeometryBox::ViewBox ||
80
0
                 mFrame->StyleDisplay()->mTransformBox ==
81
0
                   StyleGeometryBox::BorderBox,
82
0
                 "Unexpected value for 'transform-box'");
83
0
      // Percentages in transforms resolve against the width/height of the
84
0
      // nearest viewport (or its viewBox if one is applied), and the
85
0
      // transform is relative to {0,0} in current user space.
86
0
      mX = -mFrame->GetPosition().x;
87
0
      mY = -mFrame->GetPosition().y;
88
0
      Size contextSize = nsSVGUtils::GetContextSize(mFrame);
89
0
      mWidth = nsPresContext::CSSPixelsToAppUnits(contextSize.width);
90
0
      mHeight = nsPresContext::CSSPixelsToAppUnits(contextSize.height);
91
0
    }
92
0
    return;
93
0
  }
94
0
95
0
  // If UNIFIED_CONTINUATIONS is not defined, this is simply the frame's
96
0
  // bounding rectangle, translated to the origin.  Otherwise, it is the
97
0
  // smallest rectangle containing a frame and all of its continuations.  For
98
0
  // example, if there is a <span> element with several continuations split
99
0
  // over several lines, this function will return the rectangle containing all
100
0
  // of those continuations.
101
0
102
0
  nsRect rect;
103
0
104
0
#ifndef UNIFIED_CONTINUATIONS
105
0
  rect = mFrame->GetRect();
106
#else
107
  // Iterate the continuation list, unioning together the bounding rects:
108
  for (const nsIFrame *currFrame = mFrame->FirstContinuation();
109
       currFrame != nullptr;
110
       currFrame = currFrame->GetNextContinuation())
111
  {
112
    // Get the frame rect in local coordinates, then translate back to the
113
    // original coordinates:
114
    rect.UnionRect(result, nsRect(currFrame->GetOffsetTo(mFrame),
115
                                  currFrame->GetSize()));
116
  }
117
#endif
118
119
0
  mX = 0;
120
0
  mY = 0;
121
0
  mWidth = rect.Width();
122
0
  mHeight = rect.Height();
123
0
}
124
125
void
126
TransformReferenceBox::Init(const nsSize& aDimensions)
127
0
{
128
0
  MOZ_ASSERT(!mFrame && !mIsCached);
129
0
130
0
  mX = 0;
131
0
  mY = 0;
132
0
  mWidth = aDimensions.width;
133
0
  mHeight = aDimensions.height;
134
0
  mIsCached = true;
135
0
}
136
137
float
138
ProcessTranslatePart(const nsCSSValue& aValue,
139
                     TransformReferenceBox* aRefBox,
140
                     TransformReferenceBox::DimensionGetter aDimensionGetter)
141
0
{
142
0
  nscoord offset = 0;
143
0
  float percent = 0.0f;
144
0
145
0
  if (aValue.GetUnit() == eCSSUnit_Percent) {
146
0
    percent = aValue.GetPercentValue();
147
0
  } else if (aValue.GetUnit() == eCSSUnit_Pixel ||
148
0
             aValue.GetUnit() == eCSSUnit_Number) {
149
0
    // Raw numbers are treated as being pixels.
150
0
    return aValue.GetFloatValue();
151
0
  } else if (aValue.IsCalcUnit()) {
152
0
    // We can retrieve the Calc value directly because it has been computed
153
0
    // from the Servo side and set by nsCSSValue::SetCalcValue().
154
0
    nsStyleCoord::CalcValue calc = aValue.GetCalcValue();
155
0
    percent = calc.mPercent;
156
0
    offset = calc.mLength;
157
0
  } else {
158
0
    // Note: The unit of nsCSSValue passed from Servo side would be number,
159
0
    //       pixel, percent, or eCSSUnit_Calc, so it is impossible to go into
160
0
    //       this branch.
161
0
    MOZ_CRASH("unexpected unit in ProcessTranslatePart");
162
0
  }
163
0
164
0
  float translation =
165
0
    NSAppUnitsToFloatPixels(offset, AppUnitsPerCSSPixel());
166
0
  // We want to avoid calling aDimensionGetter if there's no percentage to be
167
0
  // resolved (for performance reasons - see TransformReferenceBox).
168
0
  if (percent != 0.0f && aRefBox && !aRefBox->IsEmpty()) {
169
0
    translation +=
170
0
      percent * NSAppUnitsToFloatPixels((aRefBox->*aDimensionGetter)(),
171
0
                                        AppUnitsPerCSSPixel());
172
0
  }
173
0
  return translation;
174
0
}
175
176
/**
177
 * Helper functions to process all the transformation function types.
178
 *
179
 * These take a matrix parameter to accumulate the current matrix.
180
 */
181
182
/* Helper function to process a matrix entry. */
183
static void
184
ProcessMatrix(Matrix4x4& aMatrix,
185
              const nsCSSValue::Array* aData,
186
              TransformReferenceBox& aRefBox)
187
0
{
188
0
  MOZ_ASSERT(aData->Count() == 7, "Invalid array!");
189
0
190
0
  gfxMatrix result;
191
0
192
0
  /* Take the first four elements out of the array as floats and store
193
0
   * them.
194
0
   */
195
0
  result._11 = aData->Item(1).GetFloatValue();
196
0
  result._12 = aData->Item(2).GetFloatValue();
197
0
  result._21 = aData->Item(3).GetFloatValue();
198
0
  result._22 = aData->Item(4).GetFloatValue();
199
0
200
0
  /* The last two elements have their length parts stored in aDelta
201
0
   * and their percent parts stored in aX[0] and aY[1].
202
0
   */
203
0
  result._31 = ProcessTranslatePart(aData->Item(5),
204
0
                                    &aRefBox, &TransformReferenceBox::Width);
205
0
  result._32 = ProcessTranslatePart(aData->Item(6),
206
0
                                    &aRefBox, &TransformReferenceBox::Height);
207
0
208
0
  aMatrix = result * aMatrix;
209
0
}
210
211
static void
212
ProcessMatrix3D(Matrix4x4& aMatrix,
213
                const nsCSSValue::Array* aData,
214
                TransformReferenceBox& aRefBox)
215
0
{
216
0
  MOZ_ASSERT(aData->Count() == 17, "Invalid array!");
217
0
218
0
  Matrix4x4 temp;
219
0
220
0
  temp._11 = aData->Item(1).GetFloatValue();
221
0
  temp._12 = aData->Item(2).GetFloatValue();
222
0
  temp._13 = aData->Item(3).GetFloatValue();
223
0
  temp._14 = aData->Item(4).GetFloatValue();
224
0
  temp._21 = aData->Item(5).GetFloatValue();
225
0
  temp._22 = aData->Item(6).GetFloatValue();
226
0
  temp._23 = aData->Item(7).GetFloatValue();
227
0
  temp._24 = aData->Item(8).GetFloatValue();
228
0
  temp._31 = aData->Item(9).GetFloatValue();
229
0
  temp._32 = aData->Item(10).GetFloatValue();
230
0
  temp._33 = aData->Item(11).GetFloatValue();
231
0
  temp._34 = aData->Item(12).GetFloatValue();
232
0
  temp._44 = aData->Item(16).GetFloatValue();
233
0
234
0
  temp._41 = ProcessTranslatePart(aData->Item(13),
235
0
                                  &aRefBox, &TransformReferenceBox::Width);
236
0
  temp._42 = ProcessTranslatePart(aData->Item(14),
237
0
                                  &aRefBox, &TransformReferenceBox::Height);
238
0
  temp._43 = ProcessTranslatePart(aData->Item(15), nullptr);
239
0
240
0
  aMatrix = temp * aMatrix;
241
0
}
242
243
// For accumulation for transform functions, |aOne| corresponds to |aB| and
244
// |aTwo| corresponds to |aA| for StyleAnimationValue::Accumulate().
245
class Accumulate {
246
public:
247
  template<typename T>
248
  static T operate(const T& aOne, const T& aTwo, double aCoeff)
249
0
  {
250
0
    return aOne + aTwo * aCoeff;
251
0
  }
Unexecuted instantiation: mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> nsStyleTransformMatrix::Accumulate::operate<mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> >(mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> const&, mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> const&, double)
Unexecuted instantiation: float nsStyleTransformMatrix::Accumulate::operate<float>(float const&, float const&, double)
252
253
  static Point4D operateForPerspective(const Point4D& aOne,
254
                                       const Point4D& aTwo,
255
                                       double aCoeff)
256
0
  {
257
0
    return (aOne - Point4D(0, 0, 0, 1)) +
258
0
           (aTwo - Point4D(0, 0, 0, 1)) * aCoeff +
259
0
           Point4D(0, 0, 0, 1);
260
0
  }
261
  static Point3D operateForScale(const Point3D& aOne,
262
                                 const Point3D& aTwo,
263
                                 double aCoeff)
264
0
  {
265
0
    // For scale, the identify element is 1, see AddTransformScale in
266
0
    // StyleAnimationValue.cpp.
267
0
    return (aOne - Point3D(1, 1, 1)) +
268
0
           (aTwo - Point3D(1, 1, 1)) * aCoeff +
269
0
           Point3D(1, 1, 1);
270
0
  }
271
272
  static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
273
                                    const gfxQuaternion& aTwo,
274
                                    double aCoeff)
275
0
  {
276
0
    if (aCoeff == 0.0) {
277
0
      return aOne.ToMatrix();
278
0
    }
279
0
280
0
    double theta = acos(mozilla::clamped(aTwo.w, -1.0, 1.0));
281
0
    double scale = (theta != 0.0) ? 1.0 / sin(theta) : 0.0;
282
0
    theta *= aCoeff;
283
0
    scale *= sin(theta);
284
0
285
0
    gfxQuaternion result = gfxQuaternion(scale * aTwo.x,
286
0
                                         scale * aTwo.y,
287
0
                                         scale * aTwo.z,
288
0
                                         cos(theta)) * aOne;
289
0
    return result.ToMatrix();
290
0
  }
291
292
  static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
293
                                      const Matrix4x4& aMatrix2,
294
                                      double aProgress)
295
0
  {
296
0
    return aMatrix1;
297
0
  }
298
299
  static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
300
                                  const Matrix4x4& aMatrix2,
301
                                  double aCount)
302
0
  {
303
0
    Matrix4x4 result;
304
0
    Servo_MatrixTransform_Operate(MatrixTransformOperator::Accumulate,
305
0
                                  &aMatrix1.components,
306
0
                                  &aMatrix2.components,
307
0
                                  aCount,
308
0
                                  &result.components);
309
0
    return result;
310
0
  }
311
};
312
313
class Interpolate {
314
public:
315
  template<typename T>
316
  static T operate(const T& aOne, const T& aTwo, double aCoeff)
317
0
  {
318
0
    return aOne + (aTwo - aOne) * aCoeff;
319
0
  }
Unexecuted instantiation: mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> nsStyleTransformMatrix::Interpolate::operate<mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> >(mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> const&, mozilla::gfx::Point3DTyped<mozilla::gfx::UnknownUnits, float> const&, double)
Unexecuted instantiation: float nsStyleTransformMatrix::Interpolate::operate<float>(float const&, float const&, double)
320
321
  static Point4D operateForPerspective(const Point4D& aOne,
322
                                       const Point4D& aTwo,
323
                                       double aCoeff)
324
0
  {
325
0
    return aOne + (aTwo - aOne) * aCoeff;
326
0
  }
327
328
  static Point3D operateForScale(const Point3D& aOne,
329
                                 const Point3D& aTwo,
330
                                 double aCoeff)
331
0
  {
332
0
    return aOne + (aTwo - aOne) * aCoeff;
333
0
  }
334
335
  static Matrix4x4 operateForRotate(const gfxQuaternion& aOne,
336
                                    const gfxQuaternion& aTwo,
337
                                    double aCoeff)
338
0
  {
339
0
    return aOne.Slerp(aTwo, aCoeff).ToMatrix();
340
0
  }
341
342
  static Matrix4x4 operateForFallback(const Matrix4x4& aMatrix1,
343
                                      const Matrix4x4& aMatrix2,
344
                                      double aProgress)
345
0
  {
346
0
    return aProgress < 0.5 ? aMatrix1 : aMatrix2;
347
0
  }
348
349
  static Matrix4x4 operateByServo(const Matrix4x4& aMatrix1,
350
                                  const Matrix4x4& aMatrix2,
351
                                  double aProgress)
352
0
  {
353
0
    Matrix4x4 result;
354
0
    Servo_MatrixTransform_Operate(MatrixTransformOperator::Interpolate,
355
0
                                  &aMatrix1.components,
356
0
                                  &aMatrix2.components,
357
0
                                  aProgress,
358
0
                                  &result.components);
359
0
    return result;
360
0
  }
361
};
362
363
/**
364
 * Calculate 2 matrices by decomposing them with Operator.
365
 *
366
 * @param aMatrix1   First matrix, using CSS pixel units.
367
 * @param aMatrix2   Second matrix, using CSS pixel units.
368
 * @param aProgress  Coefficient for the Operator.
369
 */
370
template <typename Operator>
371
static Matrix4x4
372
OperateTransformMatrix(const Matrix4x4 &aMatrix1,
373
                       const Matrix4x4 &aMatrix2,
374
                       double aProgress)
375
0
{
376
0
  // Decompose both matrices
377
0
378
0
  Point3D scale1(1, 1, 1), translate1;
379
0
  Point4D perspective1(0, 0, 0, 1);
380
0
  gfxQuaternion rotate1;
381
0
  nsStyleTransformMatrix::ShearArray shear1{0.0f, 0.0f, 0.0f};
382
0
383
0
  Point3D scale2(1, 1, 1), translate2;
384
0
  Point4D perspective2(0, 0, 0, 1);
385
0
  gfxQuaternion rotate2;
386
0
  nsStyleTransformMatrix::ShearArray shear2{0.0f, 0.0f, 0.0f};
387
0
388
0
  // Check if both matrices are decomposable.
389
0
  bool wasDecomposed;
390
0
  Matrix matrix2d1, matrix2d2;
391
0
  if (aMatrix1.Is2D(&matrix2d1) && aMatrix2.Is2D(&matrix2d2)) {
392
0
    wasDecomposed =
393
0
      Decompose2DMatrix(matrix2d1, scale1, shear1, rotate1, translate1) &&
394
0
      Decompose2DMatrix(matrix2d2, scale2, shear2, rotate2, translate2);
395
0
  } else {
396
0
    wasDecomposed =
397
0
      Decompose3DMatrix(aMatrix1, scale1, shear1,
398
0
                        rotate1, translate1, perspective1) &&
399
0
      Decompose3DMatrix(aMatrix2, scale2, shear2,
400
0
                        rotate2, translate2, perspective2);
401
0
  }
402
0
403
0
  // Fallback to discrete operation if one of the matrices is not decomposable.
404
0
  if (!wasDecomposed) {
405
0
    return Operator::operateForFallback(aMatrix1, aMatrix2, aProgress);
406
0
  }
407
0
408
0
  Matrix4x4 result;
409
0
410
0
  // Operate each of the pieces in response to |Operator|.
411
0
  Point4D perspective =
412
0
    Operator::operateForPerspective(perspective1, perspective2, aProgress);
413
0
  result.SetTransposedVector(3, perspective);
414
0
415
0
  Point3D translate =
416
0
    Operator::operate(translate1, translate2, aProgress);
417
0
  result.PreTranslate(translate.x, translate.y, translate.z);
418
0
419
0
  Matrix4x4 rotate = Operator::operateForRotate(rotate1, rotate2, aProgress);
420
0
  if (!rotate.IsIdentity()) {
421
0
    result = rotate * result;
422
0
  }
423
0
424
0
  // TODO: Would it be better to operate these as angles?
425
0
  //       How do we convert back to angles?
426
0
  float yzshear =
427
0
    Operator::operate(shear1[ShearType::YZSHEAR],
428
0
                      shear2[ShearType::YZSHEAR],
429
0
                      aProgress);
430
0
  if (yzshear != 0.0) {
431
0
    result.SkewYZ(yzshear);
432
0
  }
433
0
434
0
  float xzshear =
435
0
    Operator::operate(shear1[ShearType::XZSHEAR],
436
0
                      shear2[ShearType::XZSHEAR],
437
0
                      aProgress);
438
0
  if (xzshear != 0.0) {
439
0
    result.SkewXZ(xzshear);
440
0
  }
441
0
442
0
  float xyshear =
443
0
    Operator::operate(shear1[ShearType::XYSHEAR],
444
0
                      shear2[ShearType::XYSHEAR],
445
0
                      aProgress);
446
0
  if (xyshear != 0.0) {
447
0
    result.SkewXY(xyshear);
448
0
  }
449
0
450
0
  Point3D scale =
451
0
    Operator::operateForScale(scale1, scale2, aProgress);
452
0
  if (scale != Point3D(1.0, 1.0, 1.0)) {
453
0
    result.PreScale(scale.x, scale.y, scale.z);
454
0
  }
455
0
456
0
  return result;
457
0
}
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> nsStyleTransformMatrix::OperateTransformMatrix<nsStyleTransformMatrix::Interpolate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, double)
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> nsStyleTransformMatrix::OperateTransformMatrix<nsStyleTransformMatrix::Accumulate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, double)
458
459
template <typename Operator>
460
static Matrix4x4
461
OperateTransformMatrixByServo(const Matrix4x4 &aMatrix1,
462
                              const Matrix4x4 &aMatrix2,
463
                              double aProgress)
464
0
{
465
0
  return Operator::operateByServo(aMatrix1, aMatrix2, aProgress);
466
0
}
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> nsStyleTransformMatrix::OperateTransformMatrixByServo<nsStyleTransformMatrix::Interpolate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, double)
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> nsStyleTransformMatrix::OperateTransformMatrixByServo<nsStyleTransformMatrix::Accumulate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits> const&, double)
467
468
template <typename Operator>
469
static void
470
ProcessMatrixOperator(Matrix4x4& aMatrix,
471
                      const nsCSSValue::Array* aData,
472
                      TransformReferenceBox& aRefBox,
473
                      bool* aContains3dTransform)
474
0
{
475
0
  MOZ_ASSERT(aData->Count() == 4, "Invalid array!");
476
0
477
0
  auto readTransform = [&](const nsCSSValue& aValue) -> Matrix4x4 {
478
0
    const nsCSSValueList* list = nullptr;
479
0
    switch (aValue.GetUnit()) {
480
0
      case eCSSUnit_List:
481
0
        // For Gecko style backend.
482
0
        list = aValue.GetListValue();
483
0
        break;
484
0
      case eCSSUnit_SharedList:
485
0
        // For Servo style backend. The transform lists of interpolatematrix
486
0
        // are not created on the main thread (i.e. during parallel traversal),
487
0
        // and nsCSSValueList_heap is not thread safe. Therefore, we use
488
0
        // nsCSSValueSharedList as a workaround.
489
0
        list = aValue.GetSharedListValue()->mHead;
490
0
        break;
491
0
      default:
492
0
        list = nullptr;
493
0
    }
494
0
495
0
    Matrix4x4 matrix;
496
0
    if (!list) {
497
0
      return matrix;
498
0
    }
499
0
500
0
    float appUnitPerCSSPixel = AppUnitsPerCSSPixel();
501
0
    matrix = nsStyleTransformMatrix::ReadTransforms(list,
502
0
                                                    aRefBox,
503
0
                                                    appUnitPerCSSPixel,
504
0
                                                    aContains3dTransform);
505
0
    return matrix;
506
0
  };
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:void nsStyleTransformMatrix::ProcessMatrixOperator<nsStyleTransformMatrix::Interpolate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits>&, nsCSSValue::Array const*, nsStyleTransformMatrix::TransformReferenceBox&, bool*)::{lambda(nsCSSValue const&)#1}::operator()(nsCSSValue const&) const
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:void nsStyleTransformMatrix::ProcessMatrixOperator<nsStyleTransformMatrix::Accumulate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits>&, nsCSSValue::Array const*, nsStyleTransformMatrix::TransformReferenceBox&, bool*)::{lambda(nsCSSValue const&)#1}::operator()(nsCSSValue const&) const
507
0
508
0
  Matrix4x4 matrix1 = readTransform(aData->Item(1));
509
0
  Matrix4x4 matrix2 = readTransform(aData->Item(2));
510
0
  double progress = aData->Item(3).GetPercentValue();
511
0
512
0
  // We cannot use GeckoComputedStyle to check if we use Servo backend because
513
0
  // it could be null in Gecko. Instead, use the unit of the nsCSSValue because
514
0
  // we use eCSSUnit_SharedList for Servo backend.
515
0
  if (aData->Item(1).GetUnit() == eCSSUnit_SharedList) {
516
0
    aMatrix =
517
0
      OperateTransformMatrixByServo<Operator>(matrix1, matrix2, progress)
518
0
        * aMatrix;
519
0
    return;
520
0
  }
521
0
522
0
  aMatrix =
523
0
    OperateTransformMatrix<Operator>(matrix1, matrix2, progress) * aMatrix;
524
0
}
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:void nsStyleTransformMatrix::ProcessMatrixOperator<nsStyleTransformMatrix::Interpolate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits>&, nsCSSValue::Array const*, nsStyleTransformMatrix::TransformReferenceBox&, bool*)
Unexecuted instantiation: Unified_cpp_layout_style4.cpp:void nsStyleTransformMatrix::ProcessMatrixOperator<nsStyleTransformMatrix::Accumulate>(mozilla::gfx::Matrix4x4Typed<mozilla::gfx::UnknownUnits, mozilla::gfx::UnknownUnits>&, nsCSSValue::Array const*, nsStyleTransformMatrix::TransformReferenceBox&, bool*)
525
526
/* Helper function to process two matrices that we need to interpolate between */
527
void
528
ProcessInterpolateMatrix(Matrix4x4& aMatrix,
529
                         const nsCSSValue::Array* aData,
530
                         TransformReferenceBox& aRefBox,
531
                         bool* aContains3dTransform)
532
0
{
533
0
  ProcessMatrixOperator<Interpolate>(aMatrix, aData,
534
0
                                     aRefBox,
535
0
                                     aContains3dTransform);
536
0
}
537
538
void
539
ProcessAccumulateMatrix(Matrix4x4& aMatrix,
540
                        const nsCSSValue::Array* aData,
541
                        TransformReferenceBox& aRefBox,
542
                        bool* aContains3dTransform)
543
0
{
544
0
  ProcessMatrixOperator<Accumulate>(aMatrix, aData, aRefBox,
545
0
                                    aContains3dTransform);
546
0
}
547
548
/* Helper function to process a translatex function. */
549
static void
550
ProcessTranslateX(Matrix4x4& aMatrix,
551
                  const nsCSSValue::Array* aData,
552
                  TransformReferenceBox& aRefBox)
553
0
{
554
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
555
0
556
0
  Point3D temp;
557
0
558
0
  temp.x = ProcessTranslatePart(aData->Item(1),
559
0
                                &aRefBox, &TransformReferenceBox::Width);
560
0
  aMatrix.PreTranslate(temp);
561
0
}
562
563
/* Helper function to process a translatey function. */
564
static void
565
ProcessTranslateY(Matrix4x4& aMatrix,
566
                  const nsCSSValue::Array* aData,
567
                  TransformReferenceBox& aRefBox)
568
0
{
569
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
570
0
571
0
  Point3D temp;
572
0
573
0
  temp.y = ProcessTranslatePart(aData->Item(1),
574
0
                                &aRefBox, &TransformReferenceBox::Height);
575
0
  aMatrix.PreTranslate(temp);
576
0
}
577
578
static void
579
ProcessTranslateZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
580
0
{
581
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
582
0
583
0
  Point3D temp;
584
0
585
0
  temp.z = ProcessTranslatePart(aData->Item(1), nullptr);
586
0
  aMatrix.PreTranslate(temp);
587
0
}
588
589
/* Helper function to process a translate function. */
590
static void
591
ProcessTranslate(Matrix4x4& aMatrix,
592
                 const nsCSSValue::Array* aData,
593
                 TransformReferenceBox& aRefBox)
594
0
{
595
0
  MOZ_ASSERT(aData->Count() == 2 || aData->Count() == 3, "Invalid array!");
596
0
597
0
  Point3D temp;
598
0
599
0
  temp.x = ProcessTranslatePart(aData->Item(1),
600
0
                                &aRefBox, &TransformReferenceBox::Width);
601
0
602
0
  /* If we read in a Y component, set it appropriately */
603
0
  if (aData->Count() == 3) {
604
0
    temp.y = ProcessTranslatePart(aData->Item(2),
605
0
                                  &aRefBox, &TransformReferenceBox::Height);
606
0
  }
607
0
  aMatrix.PreTranslate(temp);
608
0
}
609
610
static void
611
ProcessTranslate3D(Matrix4x4& aMatrix,
612
                   const nsCSSValue::Array* aData,
613
                   TransformReferenceBox& aRefBox)
614
0
{
615
0
  MOZ_ASSERT(aData->Count() == 4, "Invalid array!");
616
0
617
0
  Point3D temp;
618
0
619
0
  temp.x = ProcessTranslatePart(aData->Item(1),
620
0
                                &aRefBox, &TransformReferenceBox::Width);
621
0
622
0
  temp.y = ProcessTranslatePart(aData->Item(2),
623
0
                                &aRefBox, &TransformReferenceBox::Height);
624
0
625
0
  temp.z = ProcessTranslatePart(aData->Item(3),
626
0
                                nullptr);
627
0
628
0
  aMatrix.PreTranslate(temp);
629
0
}
630
631
/* Helper function to set up a scale matrix. */
632
static void
633
ProcessScaleHelper(Matrix4x4& aMatrix,
634
                   float aXScale,
635
                   float aYScale,
636
                   float aZScale)
637
0
{
638
0
  aMatrix.PreScale(aXScale, aYScale, aZScale);
639
0
}
640
641
/* Process a scalex function. */
642
static void
643
ProcessScaleX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
644
0
{
645
0
  MOZ_ASSERT(aData->Count() == 2, "Bad array!");
646
0
  ProcessScaleHelper(aMatrix, aData->Item(1).GetFloatValue(), 1.0f, 1.0f);
647
0
}
648
649
/* Process a scaley function. */
650
static void
651
ProcessScaleY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
652
0
{
653
0
  MOZ_ASSERT(aData->Count() == 2, "Bad array!");
654
0
  ProcessScaleHelper(aMatrix, 1.0f, aData->Item(1).GetFloatValue(), 1.0f);
655
0
}
656
657
static void
658
ProcessScaleZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
659
0
{
660
0
  MOZ_ASSERT(aData->Count() == 2, "Bad array!");
661
0
  ProcessScaleHelper(aMatrix, 1.0f, 1.0f, aData->Item(1).GetFloatValue());
662
0
}
663
664
static void
665
ProcessScale3D(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
666
0
{
667
0
  MOZ_ASSERT(aData->Count() == 4, "Bad array!");
668
0
  ProcessScaleHelper(aMatrix,
669
0
                     aData->Item(1).GetFloatValue(),
670
0
                     aData->Item(2).GetFloatValue(),
671
0
                     aData->Item(3).GetFloatValue());
672
0
}
673
674
/* Process a scale function. */
675
static void
676
ProcessScale(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
677
0
{
678
0
  MOZ_ASSERT(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
679
0
  /* We either have one element or two.  If we have one, it's for both X and Y.
680
0
   * Otherwise it's one for each.
681
0
   */
682
0
  const nsCSSValue& scaleX = aData->Item(1);
683
0
  const nsCSSValue& scaleY = (aData->Count() == 2 ? scaleX :
684
0
                              aData->Item(2));
685
0
686
0
  ProcessScaleHelper(aMatrix,
687
0
                     scaleX.GetFloatValue(),
688
0
                     scaleY.GetFloatValue(),
689
0
                     1.0f);
690
0
}
691
692
/* Helper function that, given a set of angles, constructs the appropriate
693
 * skew matrix.
694
 */
695
static void
696
ProcessSkewHelper(Matrix4x4& aMatrix, double aXAngle, double aYAngle)
697
0
{
698
0
  aMatrix.SkewXY(aXAngle, aYAngle);
699
0
}
700
701
/* Function that converts a skewx transform into a matrix. */
702
static void
703
ProcessSkewX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
704
0
{
705
0
  NS_ASSERTION(aData->Count() == 2, "Bad array!");
706
0
  ProcessSkewHelper(aMatrix, aData->Item(1).GetAngleValueInRadians(), 0.0);
707
0
}
708
709
/* Function that converts a skewy transform into a matrix. */
710
static void
711
ProcessSkewY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
712
0
{
713
0
  NS_ASSERTION(aData->Count() == 2, "Bad array!");
714
0
  ProcessSkewHelper(aMatrix, 0.0, aData->Item(1).GetAngleValueInRadians());
715
0
}
716
717
/* Function that converts a skew transform into a matrix. */
718
static void
719
ProcessSkew(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
720
0
{
721
0
  NS_ASSERTION(aData->Count() == 2 || aData->Count() == 3, "Bad array!");
722
0
723
0
  double xSkew = aData->Item(1).GetAngleValueInRadians();
724
0
  double ySkew = (aData->Count() == 2
725
0
                  ? 0.0 : aData->Item(2).GetAngleValueInRadians());
726
0
727
0
  ProcessSkewHelper(aMatrix, xSkew, ySkew);
728
0
}
729
730
/* Function that converts a rotate transform into a matrix. */
731
static void
732
ProcessRotateZ(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
733
0
{
734
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
735
0
  double theta = aData->Item(1).GetAngleValueInRadians();
736
0
  aMatrix.RotateZ(theta);
737
0
}
738
739
static void
740
ProcessRotateX(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
741
0
{
742
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
743
0
  double theta = aData->Item(1).GetAngleValueInRadians();
744
0
  aMatrix.RotateX(theta);
745
0
}
746
747
static void
748
ProcessRotateY(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
749
0
{
750
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
751
0
  double theta = aData->Item(1).GetAngleValueInRadians();
752
0
  aMatrix.RotateY(theta);
753
0
}
754
755
static void
756
ProcessRotate3D(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
757
0
{
758
0
  MOZ_ASSERT(aData->Count() == 5, "Invalid array!");
759
0
760
0
  double theta = aData->Item(4).GetAngleValueInRadians();
761
0
  float x = aData->Item(1).GetFloatValue();
762
0
  float y = aData->Item(2).GetFloatValue();
763
0
  float z = aData->Item(3).GetFloatValue();
764
0
765
0
  Matrix4x4 temp;
766
0
  temp.SetRotateAxisAngle(x, y, z, theta);
767
0
768
0
  aMatrix = temp * aMatrix;
769
0
}
770
771
static void
772
ProcessPerspective(Matrix4x4& aMatrix, const nsCSSValue::Array* aData)
773
0
{
774
0
  MOZ_ASSERT(aData->Count() == 2, "Invalid array!");
775
0
776
0
  float depth = ProcessTranslatePart(aData->Item(1), nullptr);
777
0
  ApplyPerspectiveToMatrix(aMatrix, depth);
778
0
}
779
780
781
/**
782
 * SetToTransformFunction is essentially a giant switch statement that fans
783
 * out to many smaller helper functions.
784
 */
785
static void
786
MatrixForTransformFunction(Matrix4x4& aMatrix,
787
                           const nsCSSValue::Array * aData,
788
                           TransformReferenceBox& aRefBox,
789
                           bool* aContains3dTransform)
790
0
{
791
0
  MOZ_ASSERT(aContains3dTransform);
792
0
  MOZ_ASSERT(aData, "Why did you want to get data from a null array?");
793
0
794
0
  /* Get the keyword for the transform. */
795
0
  switch (TransformFunctionOf(aData)) {
796
0
  case eCSSKeyword_translatex:
797
0
    ProcessTranslateX(aMatrix, aData, aRefBox);
798
0
    break;
799
0
  case eCSSKeyword_translatey:
800
0
    ProcessTranslateY(aMatrix, aData, aRefBox);
801
0
    break;
802
0
  case eCSSKeyword_translatez:
803
0
    *aContains3dTransform = true;
804
0
    ProcessTranslateZ(aMatrix, aData);
805
0
    break;
806
0
  case eCSSKeyword_translate:
807
0
    ProcessTranslate(aMatrix, aData, aRefBox);
808
0
    break;
809
0
  case eCSSKeyword_translate3d:
810
0
    *aContains3dTransform = true;
811
0
    ProcessTranslate3D(aMatrix, aData, aRefBox);
812
0
    break;
813
0
  case eCSSKeyword_scalex:
814
0
    ProcessScaleX(aMatrix, aData);
815
0
    break;
816
0
  case eCSSKeyword_scaley:
817
0
    ProcessScaleY(aMatrix, aData);
818
0
    break;
819
0
  case eCSSKeyword_scalez:
820
0
    *aContains3dTransform = true;
821
0
    ProcessScaleZ(aMatrix, aData);
822
0
    break;
823
0
  case eCSSKeyword_scale:
824
0
    ProcessScale(aMatrix, aData);
825
0
    break;
826
0
  case eCSSKeyword_scale3d:
827
0
    *aContains3dTransform = true;
828
0
    ProcessScale3D(aMatrix, aData);
829
0
    break;
830
0
  case eCSSKeyword_skewx:
831
0
    ProcessSkewX(aMatrix, aData);
832
0
    break;
833
0
  case eCSSKeyword_skewy:
834
0
    ProcessSkewY(aMatrix, aData);
835
0
    break;
836
0
  case eCSSKeyword_skew:
837
0
    ProcessSkew(aMatrix, aData);
838
0
    break;
839
0
  case eCSSKeyword_rotatex:
840
0
    *aContains3dTransform = true;
841
0
    ProcessRotateX(aMatrix, aData);
842
0
    break;
843
0
  case eCSSKeyword_rotatey:
844
0
    *aContains3dTransform = true;
845
0
    ProcessRotateY(aMatrix, aData);
846
0
    break;
847
0
  case eCSSKeyword_rotatez:
848
0
    *aContains3dTransform = true;
849
0
    MOZ_FALLTHROUGH;
850
0
  case eCSSKeyword_rotate:
851
0
    ProcessRotateZ(aMatrix, aData);
852
0
    break;
853
0
  case eCSSKeyword_rotate3d:
854
0
    *aContains3dTransform = true;
855
0
    ProcessRotate3D(aMatrix, aData);
856
0
    break;
857
0
  case eCSSKeyword_matrix:
858
0
    ProcessMatrix(aMatrix, aData, aRefBox);
859
0
    break;
860
0
  case eCSSKeyword_matrix3d:
861
0
    *aContains3dTransform = true;
862
0
    ProcessMatrix3D(aMatrix, aData, aRefBox);
863
0
    break;
864
0
  case eCSSKeyword_interpolatematrix:
865
0
    ProcessMatrixOperator<Interpolate>(aMatrix, aData, aRefBox,
866
0
                                       aContains3dTransform);
867
0
    break;
868
0
  case eCSSKeyword_accumulatematrix:
869
0
    ProcessMatrixOperator<Accumulate>(aMatrix, aData, aRefBox,
870
0
                                      aContains3dTransform);
871
0
    break;
872
0
  case eCSSKeyword_perspective:
873
0
    *aContains3dTransform = true;
874
0
    ProcessPerspective(aMatrix, aData);
875
0
    break;
876
0
  default:
877
0
    MOZ_ASSERT_UNREACHABLE("Unknown transform function!");
878
0
  }
879
0
}
880
881
/**
882
 * Return the transform function, as an nsCSSKeyword, for the given
883
 * nsCSSValue::Array from a transform list.
884
 */
885
nsCSSKeyword
886
TransformFunctionOf(const nsCSSValue::Array* aData)
887
0
{
888
0
  MOZ_ASSERT(aData->Item(0).GetUnit() == eCSSUnit_Enumerated);
889
0
  return aData->Item(0).GetKeywordValue();
890
0
}
891
892
void
893
SetIdentityMatrix(nsCSSValue::Array* aMatrix)
894
0
{
895
0
  MOZ_ASSERT(aMatrix, "aMatrix should be non-null");
896
0
897
0
  nsCSSKeyword tfunc = TransformFunctionOf(aMatrix);
898
0
  MOZ_ASSERT(tfunc == eCSSKeyword_matrix ||
899
0
             tfunc == eCSSKeyword_matrix3d,
900
0
             "Only accept matrix and matrix3d");
901
0
902
0
  if (tfunc == eCSSKeyword_matrix) {
903
0
    MOZ_ASSERT(aMatrix->Count() == 7, "Invalid matrix");
904
0
    Matrix m;
905
0
    for (size_t i = 0; i < 6; ++i) {
906
0
      aMatrix->Item(i + 1).SetFloatValue(m.components[i], eCSSUnit_Number);
907
0
    }
908
0
    return;
909
0
  }
910
0
911
0
  MOZ_ASSERT(aMatrix->Count() == 17, "Invalid matrix3d");
912
0
  Matrix4x4 m;
913
0
  for (size_t i = 0; i < 16; ++i) {
914
0
    aMatrix->Item(i + 1).SetFloatValue(m.components[i], eCSSUnit_Number);
915
0
  }
916
0
}
917
918
static void
919
ReadTransformsImpl(Matrix4x4& aMatrix,
920
                   const nsCSSValueList* aList,
921
                   TransformReferenceBox& aRefBox,
922
                   bool* aContains3dTransform)
923
0
{
924
0
  for (const nsCSSValueList* curr = aList; curr != nullptr; curr = curr->mNext) {
925
0
    const nsCSSValue &currElem = curr->mValue;
926
0
    if (currElem.GetUnit() != eCSSUnit_Function) {
927
0
      NS_ASSERTION(currElem.GetUnit() == eCSSUnit_None &&
928
0
                   !aList->mNext,
929
0
                   "stream should either be a list of functions or a "
930
0
                   "lone None");
931
0
      continue;
932
0
    }
933
0
    NS_ASSERTION(currElem.GetArrayValue()->Count() >= 1,
934
0
                 "Incoming function is too short!");
935
0
936
0
    /* Read in a single transform matrix. */
937
0
    MatrixForTransformFunction(aMatrix, currElem.GetArrayValue(), aRefBox,
938
0
                               aContains3dTransform);
939
0
  }
940
0
}
941
942
Matrix4x4
943
ReadTransforms(const nsCSSValueList* aList,
944
               TransformReferenceBox& aRefBox,
945
               float aAppUnitsPerMatrixUnit,
946
               bool* aContains3dTransform)
947
0
{
948
0
  Matrix4x4 result;
949
0
  ReadTransformsImpl(result, aList, aRefBox, aContains3dTransform);
950
0
951
0
  float scale = float(AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
952
0
  result.PreScale(1/scale, 1/scale, 1/scale);
953
0
  result.PostScale(scale, scale, scale);
954
0
955
0
  return result;
956
0
}
957
958
Matrix4x4
959
ReadTransforms(const nsCSSValueList* aIndividualTransforms,
960
               const Maybe<MotionPathData>& aMotion,
961
               const nsCSSValueList* aTransform,
962
               TransformReferenceBox& aRefBox,
963
               float aAppUnitsPerMatrixUnit,
964
               bool* aContains3dTransform)
965
0
{
966
0
  Matrix4x4 result;
967
0
968
0
  if (aIndividualTransforms) {
969
0
    ReadTransformsImpl(result, aIndividualTransforms, aRefBox,
970
0
                       aContains3dTransform);
971
0
  }
972
0
973
0
  if (aMotion.isSome()) {
974
0
    // Create the equivalent translate and rotate function, according to the
975
0
    // order in spec. We combine the translate and then the rotate.
976
0
    // https://drafts.fxtf.org/motion-1/#calculating-path-transform
977
0
    result.PreTranslate(aMotion->mTranslate.x, aMotion->mTranslate.y, 0.0);
978
0
    if (aMotion->mRotate != 0.0) {
979
0
      result.RotateZ(aMotion->mRotate);
980
0
    }
981
0
  }
982
0
983
0
  if (aTransform) {
984
0
    ReadTransformsImpl(result, aTransform, aRefBox, aContains3dTransform);
985
0
  }
986
0
987
0
  float scale = float(AppUnitsPerCSSPixel()) / aAppUnitsPerMatrixUnit;
988
0
  result.PreScale(1/scale, 1/scale, 1/scale);
989
0
  result.PostScale(scale, scale, scale);
990
0
991
0
  return result;
992
0
}
993
994
Point
995
Convert2DPosition(nsStyleCoord const (&aValue)[2],
996
                  TransformReferenceBox& aRefBox,
997
                  int32_t aAppUnitsPerDevPixel)
998
0
{
999
0
  float position[2];
1000
0
  nsStyleTransformMatrix::TransformReferenceBox::DimensionGetter dimensionGetter[] =
1001
0
    { &nsStyleTransformMatrix::TransformReferenceBox::Width,
1002
0
      &nsStyleTransformMatrix::TransformReferenceBox::Height };
1003
0
  for (uint8_t index = 0; index < 2; ++index) {
1004
0
    const nsStyleCoord& value  = aValue[index];
1005
0
    if (value.GetUnit() == eStyleUnit_Calc) {
1006
0
      const nsStyleCoord::Calc *calc = value.GetCalcValue();
1007
0
      position[index] =
1008
0
        NSAppUnitsToFloatPixels((aRefBox.*dimensionGetter[index])(), aAppUnitsPerDevPixel) *
1009
0
          calc->mPercent +
1010
0
        NSAppUnitsToFloatPixels(calc->mLength, aAppUnitsPerDevPixel);
1011
0
    } else if (value.GetUnit() == eStyleUnit_Percent) {
1012
0
      position[index] =
1013
0
        NSAppUnitsToFloatPixels((aRefBox.*dimensionGetter[index])(), aAppUnitsPerDevPixel) *
1014
0
        value.GetPercentValue();
1015
0
    } else {
1016
0
      MOZ_ASSERT(value.GetUnit() == eStyleUnit_Coord,
1017
0
                "unexpected unit");
1018
0
      position[index] =
1019
0
        NSAppUnitsToFloatPixels(value.GetCoordValue(),
1020
0
                                aAppUnitsPerDevPixel);
1021
0
    }
1022
0
  }
1023
0
1024
0
  return Point(position[0], position[1]);
1025
0
}
1026
1027
/*
1028
 * The relevant section of the transitions specification:
1029
 * http://dev.w3.org/csswg/css3-transitions/#animation-of-property-types-
1030
 * defers all of the details to the 2-D and 3-D transforms specifications.
1031
 * For the 2-D transforms specification (all that's relevant for us, right
1032
 * now), the relevant section is:
1033
 * http://dev.w3.org/csswg/css3-2d-transforms/#animation
1034
 * This, in turn, refers to the unmatrix program in Graphics Gems,
1035
 * available from http://tog.acm.org/resources/GraphicsGems/ , and in
1036
 * particular as the file GraphicsGems/gemsii/unmatrix.c
1037
 * in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
1038
 *
1039
 * The unmatrix reference is for general 3-D transform matrices (any of the
1040
 * 16 components can have any value).
1041
 *
1042
 * For CSS 2-D transforms, we have a 2-D matrix with the bottom row constant:
1043
 *
1044
 * [ A C E ]
1045
 * [ B D F ]
1046
 * [ 0 0 1 ]
1047
 *
1048
 * For that case, I believe the algorithm in unmatrix reduces to:
1049
 *
1050
 *  (1) If A * D - B * C == 0, the matrix is singular.  Fail.
1051
 *
1052
 *  (2) Set translation components (Tx and Ty) to the translation parts of
1053
 *      the matrix (E and F) and then ignore them for the rest of the time.
1054
 *      (For us, E and F each actually consist of three constants:  a
1055
 *      length, a multiplier for the width, and a multiplier for the
1056
 *      height.  This actually requires its own decomposition, but I'll
1057
 *      keep that separate.)
1058
 *
1059
 *  (3) Let the X scale (Sx) be sqrt(A^2 + B^2).  Then divide both A and B
1060
 *      by it.
1061
 *
1062
 *  (4) Let the XY shear (K) be A * C + B * D.  From C, subtract A times
1063
 *      the XY shear.  From D, subtract B times the XY shear.
1064
 *
1065
 *  (5) Let the Y scale (Sy) be sqrt(C^2 + D^2).  Divide C, D, and the XY
1066
 *      shear (K) by it.
1067
 *
1068
 *  (6) At this point, A * D - B * C is either 1 or -1.  If it is -1,
1069
 *      negate the XY shear (K), the X scale (Sx), and A, B, C, and D.
1070
 *      (Alternatively, we could negate the XY shear (K) and the Y scale
1071
 *      (Sy).)
1072
 *
1073
 *  (7) Let the rotation be R = atan2(B, A).
1074
 *
1075
 * Then the resulting decomposed transformation is:
1076
 *
1077
 *   translate(Tx, Ty) rotate(R) skewX(atan(K)) scale(Sx, Sy)
1078
 *
1079
 * An interesting result of this is that all of the simple transform
1080
 * functions (i.e., all functions other than matrix()), in isolation,
1081
 * decompose back to themselves except for:
1082
 *   'skewY(φ)', which is 'matrix(1, tan(φ), 0, 1, 0, 0)', which decomposes
1083
 *   to 'rotate(φ) skewX(φ) scale(sec(φ), cos(φ))' since (ignoring the
1084
 *   alternate sign possibilities that would get fixed in step 6):
1085
 *     In step 3, the X scale factor is sqrt(1+tan²(φ)) = sqrt(sec²(φ)) = sec(φ).
1086
 *     Thus, after step 3, A = 1/sec(φ) = cos(φ) and B = tan(φ) / sec(φ) = sin(φ).
1087
 *     In step 4, the XY shear is sin(φ).
1088
 *     Thus, after step 4, C = -cos(φ)sin(φ) and D = 1 - sin²(φ) = cos²(φ).
1089
 *     Thus, in step 5, the Y scale is sqrt(cos²(φ)(sin²(φ) + cos²(φ)) = cos(φ).
1090
 *     Thus, after step 5, C = -sin(φ), D = cos(φ), and the XY shear is tan(φ).
1091
 *     Thus, in step 6, A * D - B * C = cos²(φ) + sin²(φ) = 1.
1092
 *     In step 7, the rotation is thus φ.
1093
 *
1094
 *   skew(θ, φ), which is matrix(1, tan(φ), tan(θ), 1, 0, 0), which decomposes
1095
 *   to 'rotate(φ) skewX(θ + φ) scale(sec(φ), cos(φ))' since (ignoring
1096
 *   the alternate sign possibilities that would get fixed in step 6):
1097
 *     In step 3, the X scale factor is sqrt(1+tan²(φ)) = sqrt(sec²(φ)) = sec(φ).
1098
 *     Thus, after step 3, A = 1/sec(φ) = cos(φ) and B = tan(φ) / sec(φ) = sin(φ).
1099
 *     In step 4, the XY shear is cos(φ)tan(θ) + sin(φ).
1100
 *     Thus, after step 4,
1101
 *     C = tan(θ) - cos(φ)(cos(φ)tan(θ) + sin(φ)) = tan(θ)sin²(φ) - cos(φ)sin(φ)
1102
 *     D = 1 - sin(φ)(cos(φ)tan(θ) + sin(φ)) = cos²(φ) - sin(φ)cos(φ)tan(θ)
1103
 *     Thus, in step 5, the Y scale is sqrt(C² + D²) =
1104
 *     sqrt(tan²(θ)(sin⁴(φ) + sin²(φ)cos²(φ)) -
1105
 *          2 tan(θ)(sin³(φ)cos(φ) + sin(φ)cos³(φ)) +
1106
 *          (sin²(φ)cos²(φ) + cos⁴(φ))) =
1107
 *     sqrt(tan²(θ)sin²(φ) - 2 tan(θ)sin(φ)cos(φ) + cos²(φ)) =
1108
 *     cos(φ) - tan(θ)sin(φ) (taking the negative of the obvious solution so
1109
 *     we avoid flipping in step 6).
1110
 *     After step 5, C = -sin(φ) and D = cos(φ), and the XY shear is
1111
 *     (cos(φ)tan(θ) + sin(φ)) / (cos(φ) - tan(θ)sin(φ)) =
1112
 *     (dividing both numerator and denominator by cos(φ))
1113
 *     (tan(θ) + tan(φ)) / (1 - tan(θ)tan(φ)) = tan(θ + φ).
1114
 *     (See http://en.wikipedia.org/wiki/List_of_trigonometric_identities .)
1115
 *     Thus, in step 6, A * D - B * C = cos²(φ) + sin²(φ) = 1.
1116
 *     In step 7, the rotation is thus φ.
1117
 *
1118
 *     To check this result, we can multiply things back together:
1119
 *
1120
 *     [ cos(φ) -sin(φ) ] [ 1 tan(θ + φ) ] [ sec(φ)    0   ]
1121
 *     [ sin(φ)  cos(φ) ] [ 0      1     ] [   0    cos(φ) ]
1122
 *
1123
 *     [ cos(φ)      cos(φ)tan(θ + φ) - sin(φ) ] [ sec(φ)    0   ]
1124
 *     [ sin(φ)      sin(φ)tan(θ + φ) + cos(φ) ] [   0    cos(φ) ]
1125
 *
1126
 *     but since tan(θ + φ) = (tan(θ) + tan(φ)) / (1 - tan(θ)tan(φ)),
1127
 *     cos(φ)tan(θ + φ) - sin(φ)
1128
 *      = cos(φ)(tan(θ) + tan(φ)) - sin(φ) + sin(φ)tan(θ)tan(φ)
1129
 *      = cos(φ)tan(θ) + sin(φ) - sin(φ) + sin(φ)tan(θ)tan(φ)
1130
 *      = cos(φ)tan(θ) + sin(φ)tan(θ)tan(φ)
1131
 *      = tan(θ) (cos(φ) + sin(φ)tan(φ))
1132
 *      = tan(θ) sec(φ) (cos²(φ) + sin²(φ))
1133
 *      = tan(θ) sec(φ)
1134
 *     and
1135
 *     sin(φ)tan(θ + φ) + cos(φ)
1136
 *      = sin(φ)(tan(θ) + tan(φ)) + cos(φ) - cos(φ)tan(θ)tan(φ)
1137
 *      = tan(θ) (sin(φ) - sin(φ)) + sin(φ)tan(φ) + cos(φ)
1138
 *      = sec(φ) (sin²(φ) + cos²(φ))
1139
 *      = sec(φ)
1140
 *     so the above is:
1141
 *     [ cos(φ)  tan(θ) sec(φ) ] [ sec(φ)    0   ]
1142
 *     [ sin(φ)     sec(φ)     ] [   0    cos(φ) ]
1143
 *
1144
 *     [    1   tan(θ) ]
1145
 *     [ tan(φ)    1   ]
1146
 */
1147
1148
/*
1149
 * Decompose2DMatrix implements the above decomposition algorithm.
1150
 */
1151
1152
bool
1153
Decompose2DMatrix(const Matrix& aMatrix,
1154
                  Point3D& aScale,
1155
                  ShearArray& aShear,
1156
                  gfxQuaternion& aRotate,
1157
                  Point3D& aTranslate)
1158
0
{
1159
0
  float A = aMatrix._11,
1160
0
        B = aMatrix._12,
1161
0
        C = aMatrix._21,
1162
0
        D = aMatrix._22;
1163
0
  if (A * D == B * C) {
1164
0
    // singular matrix
1165
0
    return false;
1166
0
  }
1167
0
1168
0
  float scaleX = sqrt(A * A + B * B);
1169
0
  A /= scaleX;
1170
0
  B /= scaleX;
1171
0
1172
0
  float XYshear = A * C + B * D;
1173
0
  C -= A * XYshear;
1174
0
  D -= B * XYshear;
1175
0
1176
0
  float scaleY = sqrt(C * C + D * D);
1177
0
  C /= scaleY;
1178
0
  D /= scaleY;
1179
0
  XYshear /= scaleY;
1180
0
1181
0
  float determinant = A * D - B * C;
1182
0
  // Determinant should now be 1 or -1.
1183
0
  if (0.99 > Abs(determinant) || Abs(determinant) > 1.01) {
1184
0
    return false;
1185
0
  }
1186
0
1187
0
  if (determinant < 0) {
1188
0
    A = -A;
1189
0
    B = -B;
1190
0
    C = -C;
1191
0
    D = -D;
1192
0
    XYshear = -XYshear;
1193
0
    scaleX = -scaleX;
1194
0
  }
1195
0
1196
0
  float rotate = atan2f(B, A);
1197
0
  aRotate = gfxQuaternion(0, 0, sin(rotate/2), cos(rotate/2));
1198
0
  aShear[ShearType::XYSHEAR] = XYshear;
1199
0
  aScale.x = scaleX;
1200
0
  aScale.y = scaleY;
1201
0
  aTranslate.x = aMatrix._31;
1202
0
  aTranslate.y = aMatrix._32;
1203
0
  return true;
1204
0
}
1205
1206
/**
1207
 * Implementation of the unmatrix algorithm, specified by:
1208
 *
1209
 * http://dev.w3.org/csswg/css3-2d-transforms/#unmatrix
1210
 *
1211
 * This, in turn, refers to the unmatrix program in Graphics Gems,
1212
 * available from http://tog.acm.org/resources/GraphicsGems/ , and in
1213
 * particular as the file GraphicsGems/gemsii/unmatrix.c
1214
 * in http://tog.acm.org/resources/GraphicsGems/AllGems.tar.gz
1215
 */
1216
bool
1217
Decompose3DMatrix(const Matrix4x4& aMatrix,
1218
                  Point3D& aScale,
1219
                  ShearArray& aShear,
1220
                  gfxQuaternion& aRotate,
1221
                  Point3D& aTranslate,
1222
                  Point4D& aPerspective)
1223
0
{
1224
0
  Matrix4x4 local = aMatrix;
1225
0
1226
0
  if (local[3][3] == 0) {
1227
0
    return false;
1228
0
  }
1229
0
  /* Normalize the matrix */
1230
0
  local.Normalize();
1231
0
1232
0
  /**
1233
0
   * perspective is used to solve for perspective, but it also provides
1234
0
   * an easy way to test for singularity of the upper 3x3 component.
1235
0
   */
1236
0
  Matrix4x4 perspective = local;
1237
0
  Point4D empty(0, 0, 0, 1);
1238
0
  perspective.SetTransposedVector(3, empty);
1239
0
1240
0
  if (perspective.Determinant() == 0.0) {
1241
0
    return false;
1242
0
  }
1243
0
1244
0
  /* First, isolate perspective. */
1245
0
  if (local[0][3] != 0 || local[1][3] != 0 ||
1246
0
      local[2][3] != 0) {
1247
0
    /* aPerspective is the right hand side of the equation. */
1248
0
    aPerspective = local.TransposedVector(3);
1249
0
1250
0
    /**
1251
0
     * Solve the equation by inverting perspective and multiplying
1252
0
     * aPerspective by the inverse.
1253
0
     */
1254
0
    perspective.Invert();
1255
0
    aPerspective = perspective.TransposeTransform4D(aPerspective);
1256
0
1257
0
    /* Clear the perspective partition */
1258
0
    local.SetTransposedVector(3, empty);
1259
0
  } else {
1260
0
    aPerspective = Point4D(0, 0, 0, 1);
1261
0
  }
1262
0
1263
0
  /* Next take care of translation */
1264
0
  for (int i = 0; i < 3; i++) {
1265
0
    aTranslate[i] = local[3][i];
1266
0
    local[3][i] = 0;
1267
0
  }
1268
0
1269
0
  /* Now get scale and shear. */
1270
0
1271
0
  /* Compute X scale factor and normalize first row. */
1272
0
  aScale.x = local[0].Length();
1273
0
  local[0] /= aScale.x;
1274
0
1275
0
  /* Compute XY shear factor and make 2nd local orthogonal to 1st. */
1276
0
  aShear[ShearType::XYSHEAR] = local[0].DotProduct(local[1]);
1277
0
  local[1] -= local[0] * aShear[ShearType::XYSHEAR];
1278
0
1279
0
  /* Now, compute Y scale and normalize 2nd local. */
1280
0
  aScale.y = local[1].Length();
1281
0
  local[1] /= aScale.y;
1282
0
  aShear[ShearType::XYSHEAR] /= aScale.y;
1283
0
1284
0
  /* Compute XZ and YZ shears, make 3rd local orthogonal */
1285
0
  aShear[ShearType::XZSHEAR] = local[0].DotProduct(local[2]);
1286
0
  local[2] -= local[0] * aShear[ShearType::XZSHEAR];
1287
0
  aShear[ShearType::YZSHEAR] = local[1].DotProduct(local[2]);
1288
0
  local[2] -= local[1] * aShear[ShearType::YZSHEAR];
1289
0
1290
0
  /* Next, get Z scale and normalize 3rd local. */
1291
0
  aScale.z = local[2].Length();
1292
0
  local[2] /= aScale.z;
1293
0
1294
0
  aShear[ShearType::XZSHEAR] /= aScale.z;
1295
0
  aShear[ShearType::YZSHEAR] /= aScale.z;
1296
0
1297
0
  /**
1298
0
   * At this point, the matrix (in locals) is orthonormal.
1299
0
   * Check for a coordinate system flip.  If the determinant
1300
0
   * is -1, then negate the matrix and the scaling factors.
1301
0
   */
1302
0
  if (local[0].DotProduct(local[1].CrossProduct(local[2])) < 0) {
1303
0
    aScale *= -1;
1304
0
    for (int i = 0; i < 3; i++) {
1305
0
      local[i] *= -1;
1306
0
    }
1307
0
  }
1308
0
1309
0
  /* Now, get the rotations out */
1310
0
  aRotate = gfxQuaternion(local);
1311
0
1312
0
  return true;
1313
0
}
1314
1315
Matrix
1316
CSSValueArrayTo2DMatrix(nsCSSValue::Array* aArray)
1317
0
{
1318
0
  MOZ_ASSERT(aArray &&
1319
0
             TransformFunctionOf(aArray) == eCSSKeyword_matrix &&
1320
0
             aArray->Count() == 7);
1321
0
  Matrix m(aArray->Item(1).GetFloatValue(),
1322
0
           aArray->Item(2).GetFloatValue(),
1323
0
           aArray->Item(3).GetFloatValue(),
1324
0
           aArray->Item(4).GetFloatValue(),
1325
0
           aArray->Item(5).GetFloatValue(),
1326
0
           aArray->Item(6).GetFloatValue());
1327
0
  return m;
1328
0
}
1329
1330
Matrix4x4
1331
CSSValueArrayTo3DMatrix(nsCSSValue::Array* aArray)
1332
0
{
1333
0
  MOZ_ASSERT(aArray &&
1334
0
             TransformFunctionOf(aArray) == eCSSKeyword_matrix3d &&
1335
0
             aArray->Count() == 17);
1336
0
  gfx::Float array[16];
1337
0
  for (size_t i = 0; i < 16; ++i) {
1338
0
    array[i] = aArray->Item(i+1).GetFloatValue();
1339
0
  }
1340
0
  Matrix4x4 m(array);
1341
0
  return m;
1342
0
}
1343
1344
Size
1345
GetScaleValue(const nsCSSValueSharedList* aList,
1346
              const nsIFrame* aForFrame)
1347
0
{
1348
0
  MOZ_ASSERT(aList && aList->mHead);
1349
0
  MOZ_ASSERT(aForFrame);
1350
0
1351
0
  bool dontCareBool;
1352
0
  TransformReferenceBox refBox(aForFrame);
1353
0
  Matrix4x4 transform = ReadTransforms(
1354
0
                          aList->mHead,
1355
0
                          refBox,
1356
0
                          aForFrame->PresContext()->AppUnitsPerDevPixel(),
1357
0
                          &dontCareBool);
1358
0
  Matrix transform2d;
1359
0
  bool canDraw2D = transform.CanDraw2D(&transform2d);
1360
0
  if (!canDraw2D) {
1361
0
    return Size();
1362
0
  }
1363
0
1364
0
  return transform2d.ScaleFactors(true);
1365
0
}
1366
1367
} // namespace nsStyleTransformMatrix