xref: /aosp_15_r20/external/skia/src/gpu/tessellate/AffineMatrix.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef skgpu_tessellate_AffineMatrix_DEFINED
9 #define skgpu_tessellate_AffineMatrix_DEFINED
10 
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkTypes.h"
14 #include "src/base/SkUtils.h"
15 #include "src/base/SkVx.h"
16 
17 namespace skgpu::tess {
18 
19 // Applies an affine 2d transformation to points. Uses SIMD, but takes care to map points
20 // identically, regardless of which method is called.
21 //
22 // This class stores redundant data, so it is best used only as a stack-allocated object at the
23 // point of use.
24 class AffineMatrix {
25 public:
26     AffineMatrix() = default;
AffineMatrix(const SkMatrix & m)27     AffineMatrix(const SkMatrix& m) { *this = m; }
28 
29     AffineMatrix& operator=(const SkMatrix& m) {
30         SkASSERT(!m.hasPerspective());
31         // Duplicate the matrix in float4.lo and float4.hi so we can map two points at once.
32         fScale = skvx::float2(m.getScaleX(), m.getScaleY()).xyxy();
33         fSkew = skvx::float2(m.getSkewX(), m.getSkewY()).xyxy();
34         fTrans = skvx::float2(m.getTranslateX(), m.getTranslateY()).xyxy();
35         return *this;
36     }
37 
map2Points(skvx::float4 p0p1)38     SK_ALWAYS_INLINE skvx::float4 map2Points(skvx::float4 p0p1) const {
39         return fScale * p0p1 + (fSkew * p0p1.yxwz() + fTrans);
40     }
41 
map2Points(const SkPoint pts[2])42     SK_ALWAYS_INLINE skvx::float4 map2Points(const SkPoint pts[2]) const {
43         return this->map2Points(skvx::float4::Load(pts));
44     }
45 
map2Points(SkPoint p0,SkPoint p1)46     SK_ALWAYS_INLINE skvx::float4 map2Points(SkPoint p0, SkPoint p1) const {
47         return this->map2Points(skvx::float4(sk_bit_cast<skvx::float2>(p0),
48                                              sk_bit_cast<skvx::float2>(p1)));
49     }
50 
mapPoint(skvx::float2 p)51     SK_ALWAYS_INLINE skvx::float2 mapPoint(skvx::float2 p) const {
52         return fScale.lo * p + (fSkew.lo * p.yx() + fTrans.lo);
53     }
54 
map1Point(const SkPoint pt[1])55     SK_ALWAYS_INLINE skvx::float2 map1Point(const SkPoint pt[1]) const {
56         return this->mapPoint(skvx::float2::Load(pt));
57     }
58 
mapPoint(SkPoint p)59     SK_ALWAYS_INLINE SkPoint mapPoint(SkPoint p) const {
60         return sk_bit_cast<SkPoint>(this->mapPoint(sk_bit_cast<skvx::float2>(p)));
61     }
62 
63 private:
64     skvx::float4 fScale;
65     skvx::float4 fSkew;
66     skvx::float4 fTrans;
67 };
68 
69 }  // namespace skgpu::tess
70 
71 #endif  // skgpu_tessellate_AffineMatrix_DEFINED
72