| 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_graphite_geom_Transform_DEFINED |
| 9 |
#define skgpu_graphite_geom_Transform_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkM44.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/private/base/SkAssert.h" |
| 14 |
#include "include/private/base/SkFloatingPoint.h" |
| 15 |
|
| 16 |
#include <utility> |
| 17 |
|
| 18 |
namespace skgpu::graphite { |
| 19 |
|
| 20 |
class Rect; |
| 21 |
|
| 22 |
// Transform encapsulates an SkM44 matrix, its inverse, and other properties dependent on the |
| 23 |
// original matrix value that are useful when rendering. |
| 24 |
class Transform { |
| 25 |
public: |
| 26 |
// Type classifies the transform into coarse categories so that certain optimizations or |
| 27 |
// properties can be queried efficiently |
| 28 |
enum class Type : unsigned { |
| 29 |
// Applying the matrix to a vector or point is a no-op, so could be skipped entirely. |
| 30 |
kIdentity, |
| 31 |
// The matrix transforms a rect to another rect, without mirrors or rotations, so both |
| 32 |
// pre-and-post transform coordinates can be exactly represented as rects. |
| 33 |
kSimpleRectStaysRect, |
| 34 |
// The matrix transforms a rect to another rect, but may mirror or rotate the corners |
| 35 |
// relative to each other. This means that the post-transformed rect completely fills |
| 36 |
// that space. |
| 37 |
kRectStaysRect, |
| 38 |
// The matrix transform may have skew or rotation, so a mapped rect does not fill space, |
| 39 |
// but there is no need to perform perspective division or w-plane clipping. This also |
| 40 |
// includes orthographic projections. |
| 41 |
kAffine, |
| 42 |
// The matrix includes perspective and requires further projection to 2D, so care must be |
| 43 |
// taken when w is less than or near 0, and homogeneous division and perspective-correct |
| 44 |
// interpolation are needed when rendering. |
| 45 |
kPerspective, |
| 46 |
// The matrix is not invertible or not finite, so should not be used to draw. |
| 47 |
kInvalid, |
| 48 |
}; |
| 49 |
|
| 50 |
explicit Transform(SkM44::Uninitialized_Constructor) |
| 51 |
: fM(SkM44::kUninitialized_Constructor) |
| 52 |
, fInvM(SkM44::kUninitialized_Constructor) |
| 53 |
, fType(Type::kInvalid) {} |
| 54 |
|
| 55 |
explicit Transform(const SkM44& m); |
| 56 |
Transform(const Transform& t) = default; |
| 57 |
|
| 58 |
static constexpr Transform Identity() { |
| 59 |
return Transform(SkM44(), SkM44(), Type::kIdentity, 1.f, 1.f); |
| 60 |
} |
| 61 |
static constexpr Transform Invalid() { |
| 62 |
return Transform(SkM44(SkM44::kNaN_Constructor), SkM44(SkM44::kNaN_Constructor), |
| 63 |
Type::kInvalid, 1.f, 1.f); |
| 64 |
} |
| 65 |
|
| 66 |
static inline Transform Translate(float x, float y) { |
| 67 |
if (x == 0.f && y == 0.f) { |
| 68 |
return Identity(); |
| 69 |
} else if (SkIsFinite(x, y)) { |
| 70 |
return Transform(SkM44::Translate(x, y), SkM44::Translate(-x, -y), |
| 71 |
Type::kSimpleRectStaysRect, 1.f, 1.f); |
| 72 |
} else { |
| 73 |
return Invalid(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static inline Transform Inverse(const Transform& t) { |
| 78 |
return Transform(t.fInvM, t.fM, t.fType, 1.f / t.fMaxScaleFactor, 1.f / t.fMinScaleFactor); |
| 79 |
} |
| 80 |
|
| 81 |
Transform& operator=(const Transform& t) = default; |
| 82 |
|
| 83 |
operator const SkM44&() const { return fM; } |
| 84 |
operator SkMatrix() const { return fM.asM33(); } |
| 85 |
|
| 86 |
bool operator!=(const Transform& t) const { return !(*this == t); } |
| 87 |
bool operator==(const Transform& t) const { |
| 88 |
return this->valid() == t.valid() && (!this->valid() || fM == t.fM); |
| 89 |
} |
| 90 |
|
| 91 |
const SkM44& matrix() const { return fM; } |
| 92 |
const SkM44& inverse() const { return fInvM; } |
| 93 |
|
| 94 |
Type type() const { return fType; } |
| 95 |
bool valid() const { return fType != Type::kInvalid; } |
| 96 |
|
| 97 |
// Return the {min,max} scale factor at the pre-transformed location 'p'. A unit circle about |
| 98 |
// 'p' transformed by this Transform will be contained in an ellipse with radii equal to 'min' |
| 99 |
// and 'max', e.g. moving 1 local unit will move at least 'min' pixels and at most 'max' pixels |
| 100 |
std::pair<float, float> scaleFactors(const SkV2& p) const; |
| 101 |
|
| 102 |
// This is valid for non-projection types and 1.0 for projection matrices. |
| 103 |
float maxScaleFactor() const { |
| 104 |
SkASSERT(this->valid()); |
| 105 |
return fMaxScaleFactor; |
| 106 |
} |
| 107 |
|
| 108 |
// Return the minimum distance needed to move in local (pre-transform) space to ensure that the |
| 109 |
// transformed coordinates are at least 1px away from the original mapped point. This minimum |
| 110 |
// distance is specific to the given local 'bounds' since the scale factors change with |
| 111 |
// perspective. |
| 112 |
// |
| 113 |
// If the bounds would be clipped by the w=0 plane or otherwise is ill-conditioned, this will |
| 114 |
// return positive infinity. |
| 115 |
float localAARadius(const Rect& bounds) const; |
| 116 |
|
| 117 |
Rect mapRect(const Rect& rect) const; |
| 118 |
Rect inverseMapRect(const Rect& rect) const; |
| 119 |
|
| 120 |
void mapPoints(const Rect& localRect, SkV4 deviceOut[4]) const; |
| 121 |
void mapPoints(const SkV2* localIn, SkV4* deviceOut, int count) const; |
| 122 |
|
| 123 |
void mapPoints(const SkV4* localIn, SkV4* deviceOut, int count) const; |
| 124 |
void inverseMapPoints(const SkV4* deviceIn, SkV4* localOut, int count) const; |
| 125 |
|
| 126 |
// Returns a transform equal to the pre- or post-translation of this matrix |
| 127 |
Transform preTranslate(float x, float y) const { |
| 128 |
return this->concat(SkM44::Translate(x, y)); |
| 129 |
} |
| 130 |
Transform postTranslate(float x, float y) const { |
| 131 |
return Translate(x, y).concat(*this); |
| 132 |
} |
| 133 |
|
| 134 |
// Returns a transform equal to (this * t) |
| 135 |
Transform concat(const Transform& t) const { |
| 136 |
SkASSERT(this->valid()); |
| 137 |
return Transform(fM * t.fM); |
| 138 |
} |
| 139 |
Transform concat(const SkM44& t) const { |
| 140 |
SkASSERT(this->valid()); |
| 141 |
return Transform(fM * t); |
| 142 |
} |
| 143 |
|
| 144 |
// Returns a transform equal to (this * t^-1) |
| 145 |
Transform concatInverse(const Transform& t) const { |
| 146 |
SkASSERT(this->valid()); |
| 147 |
return Transform(fM * t.fInvM); |
| 148 |
} |
| 149 |
Transform concatInverse(const SkM44& t) const { |
| 150 |
SkASSERT(this->valid()); |
| 151 |
// Saves a multiply compared to inverting just 't' and calculating both fM*t^-1 and t*fInvM |
| 152 |
// (t * this^-1)^-1 = this * t^-1 |
| 153 |
return Inverse(Transform(t * fInvM)); |
| 154 |
} |
| 155 |
|
| 156 |
private: |
| 157 |
// Used for static factories that have known properties |
| 158 |
constexpr Transform(const SkM44& m, const SkM44& invM, Type type, |
| 159 |
float minScale, float maxScale) |
| 160 |
: fM(m) |
| 161 |
, fInvM(invM) |
| 162 |
, fType(type) |
| 163 |
, fMinScaleFactor(minScale) |
| 164 |
, fMaxScaleFactor(maxScale) {} |
| 165 |
|
| 166 |
SkM44 fM; |
| 167 |
SkM44 fInvM; // M^-1 |
| 168 |
Type fType; |
| 169 |
|
| 170 |
// These are cached for non-projection transforms since they are constant; projection matrices |
| 171 |
// must be computed per point, and these values are ignored. |
| 172 |
float fMinScaleFactor = 1.f; |
| 173 |
float fMaxScaleFactor = 1.f; |
| 174 |
}; |
| 175 |
|
| 176 |
} // namespace skgpu::graphite |
| 177 |
|
| 178 |
#endif // skgpu_graphite_geom_Transform_DEFINED |