| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 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 SkMatrixPriv_DEFINE |
| 9 |
#define SkMatrixPriv_DEFINE |
| 10 |
|
| 11 |
#include "include/core/SkM44.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkTypes.h" |
| 17 |
#include "src/base/SkVx.h" |
| 18 |
|
| 19 |
#include <cstdint> |
| 20 |
#include <cstring> |
| 21 |
struct SkPoint3; |
| 22 |
|
| 23 |
class SkMatrixPriv { |
| 24 |
public: |
| 25 |
enum { |
| 26 |
// writeTo/readFromMemory will never return a value larger than this |
| 27 |
kMaxFlattenSize = 9 * sizeof(SkScalar) + sizeof(uint32_t), |
| 28 |
}; |
| 29 |
|
| 30 |
static size_t WriteToMemory(const SkMatrix& matrix, void* buffer) { |
| 31 |
return matrix.writeToMemory(buffer); |
| 32 |
} |
| 33 |
|
| 34 |
static size_t ReadFromMemory(SkMatrix* matrix, const void* buffer, size_t length) { |
| 35 |
return matrix->readFromMemory(buffer, length); |
| 36 |
} |
| 37 |
|
| 38 |
typedef SkMatrix::MapPtsProc MapPtsProc; |
| 39 |
|
| 40 |
|
| 41 |
static MapPtsProc GetMapPtsProc(const SkMatrix& matrix) { |
| 42 |
return SkMatrix::GetMapPtsProc(matrix.getType()); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Attempt to map the rect through the inverse of the matrix. If it is not invertible, |
| 47 |
* then this returns false and dst is unchanged. |
| 48 |
*/ |
| 49 |
[[nodiscard]] static bool InverseMapRect(const SkMatrix& mx, SkRect* dst, const SkRect& src) { |
| 50 |
if (mx.isScaleTranslate()) { |
| 51 |
// A scale-translate matrix with a 0 scale factor is not invertible. |
| 52 |
if (mx.getScaleX() == 0.f || mx.getScaleY() == 0.f) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
const SkScalar tx = mx.getTranslateX(); |
| 57 |
const SkScalar ty = mx.getTranslateY(); |
| 58 |
// mx maps coordinates as ((sx*x + tx), (sy*y + ty)) so the inverse is |
| 59 |
// ((x - tx)/sx), (y - ty)/sy). If sx or sy are negative, we have to swap the edge |
| 60 |
// values to maintain a sorted rect. |
| 61 |
auto inverted = skvx::float4::Load(&src.fLeft); |
| 62 |
inverted -= skvx::float4(tx, ty, tx, ty); |
| 63 |
|
| 64 |
if (mx.getType() > SkMatrix::kTranslate_Mask) { |
| 65 |
const SkScalar sx = 1.f / mx.getScaleX(); |
| 66 |
const SkScalar sy = 1.f / mx.getScaleY(); |
| 67 |
inverted *= skvx::float4(sx, sy, sx, sy); |
| 68 |
if (sx < 0.f && sy < 0.f) { |
| 69 |
inverted = skvx::shuffle<2, 3, 0, 1>(inverted); // swap L|R and T|B |
| 70 |
} else if (sx < 0.f) { |
| 71 |
inverted = skvx::shuffle<2, 1, 0, 3>(inverted); // swap L|R |
| 72 |
} else if (sy < 0.f) { |
| 73 |
inverted = skvx::shuffle<0, 3, 2, 1>(inverted); // swap T|B |
| 74 |
} |
| 75 |
} |
| 76 |
inverted.store(&dst->fLeft); |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
// general case |
| 81 |
if (auto inverse = mx.invert()) { |
| 82 |
inverse->mapRect(dst, src); |
| 83 |
return true; |
| 84 |
} |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** Maps count pts, skipping stride bytes to advance from one SkPoint to the next. |
| 89 |
Points are mapped by multiplying each SkPoint by SkMatrix. Given: |
| 90 |
|
| 91 |
| A B C | | x | |
| 92 |
Matrix = | D E F |, pt = | y | |
| 93 |
| G H I | | 1 | |
| 94 |
|
| 95 |
each resulting pts SkPoint is computed as: |
| 96 |
|
| 97 |
|A B C| |x| Ax+By+C Dx+Ey+F |
| 98 |
Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , ------- |
| 99 |
|G H I| |1| Gx+Hy+I Gx+Hy+I |
| 100 |
|
| 101 |
@param mx matrix used to map the points |
| 102 |
@param pts storage for mapped points |
| 103 |
@param stride size of record starting with SkPoint, in bytes |
| 104 |
@param count number of points to transform |
| 105 |
*/ |
| 106 |
static void MapPointsWithStride(const SkMatrix& mx, SkPoint pts[], size_t stride, int count) { |
| 107 |
SkASSERT(stride >= sizeof(SkPoint)); |
| 108 |
SkASSERT(0 == stride % sizeof(SkScalar)); |
| 109 |
|
| 110 |
SkMatrix::TypeMask tm = mx.getType(); |
| 111 |
|
| 112 |
if (SkMatrix::kIdentity_Mask == tm) { |
| 113 |
return; |
| 114 |
} |
| 115 |
if (SkMatrix::kTranslate_Mask == tm) { |
| 116 |
const SkScalar tx = mx.getTranslateX(); |
| 117 |
const SkScalar ty = mx.getTranslateY(); |
| 118 |
skvx::float2 trans(tx, ty); |
| 119 |
for (int i = 0; i < count; ++i) { |
| 120 |
(skvx::float2::Load(&pts->fX) + trans).store(&pts->fX); |
| 121 |
pts = (SkPoint*)((intptr_t)pts + stride); |
| 122 |
} |
| 123 |
return; |
| 124 |
} |
| 125 |
// Insert other special-cases here (e.g. scale+translate) |
| 126 |
|
| 127 |
// general case |
| 128 |
if (mx.hasPerspective()) { |
| 129 |
for (int i = 0; i < count; ++i) { |
| 130 |
*pts = mx.mapPointPerspective(*pts); |
| 131 |
pts = (SkPoint*)((intptr_t)pts + stride); |
| 132 |
} |
| 133 |
} else { |
| 134 |
for (int i = 0; i < count; ++i) { |
| 135 |
*pts = mx.mapPointAffine(*pts); |
| 136 |
pts = (SkPoint*)((intptr_t)pts + stride); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** Maps src SkPoint array of length count to dst SkPoint array, skipping stride bytes |
| 142 |
to advance from one SkPoint to the next. |
| 143 |
Points are mapped by multiplying each SkPoint by SkMatrix. Given: |
| 144 |
|
| 145 |
| A B C | | x | |
| 146 |
Matrix = | D E F |, src = | y | |
| 147 |
| G H I | | 1 | |
| 148 |
|
| 149 |
each resulting dst SkPoint is computed as: |
| 150 |
|
| 151 |
|A B C| |x| Ax+By+C Dx+Ey+F |
| 152 |
Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , ------- |
| 153 |
|G H I| |1| Gx+Hy+I Gx+Hy+I |
| 154 |
|
| 155 |
@param mx matrix used to map the points |
| 156 |
@param dst storage for mapped points |
| 157 |
@param src points to transform |
| 158 |
@param stride size of record starting with SkPoint, in bytes |
| 159 |
@param count number of points to transform |
| 160 |
*/ |
| 161 |
static void MapPointsWithStride(const SkMatrix& mx, SkPoint dst[], size_t dstStride, |
| 162 |
const SkPoint src[], size_t srcStride, int count) { |
| 163 |
SkASSERT(srcStride >= sizeof(SkPoint)); |
| 164 |
SkASSERT(dstStride >= sizeof(SkPoint)); |
| 165 |
SkASSERT(0 == srcStride % sizeof(SkScalar)); |
| 166 |
SkASSERT(0 == dstStride % sizeof(SkScalar)); |
| 167 |
for (int i = 0; i < count; ++i) { |
| 168 |
*dst = mx.mapPoint(*src); |
| 169 |
src = (SkPoint*)((intptr_t)src + srcStride); |
| 170 |
dst = (SkPoint*)((intptr_t)dst + dstStride); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
static void MapHomogeneousPointsWithStride(const SkMatrix& mx, SkPoint3 dst[], size_t dstStride, |
| 175 |
const SkPoint3 src[], size_t srcStride, int count); |
| 176 |
|
| 177 |
static bool PostIDiv(SkMatrix* matrix, int divx, int divy) { |
| 178 |
return matrix->postIDiv(divx, divy); |
| 179 |
} |
| 180 |
|
| 181 |
static bool CheapEqual(const SkMatrix& a, const SkMatrix& b) { |
| 182 |
return &a == &b || 0 == memcmp(a.fMat, b.fMat, sizeof(a.fMat)); |
| 183 |
} |
| 184 |
|
| 185 |
static const SkScalar* M44ColMajor(const SkM44& m) { return m.fMat; } |
| 186 |
|
| 187 |
// This is legacy functionality that only checks the 3x3 portion. The matrix could have Z-based |
| 188 |
// shear, or other complex behavior. Only use this if you're planning to use the information |
| 189 |
// to accelerate some purely 2D operation. |
| 190 |
static bool IsScaleTranslateAsM33(const SkM44& m) { |
| 191 |
return m.rc(1,0) == 0 && m.rc(3,0) == 0 && |
| 192 |
m.rc(0,1) == 0 && m.rc(3,1) == 0 && |
| 193 |
m.rc(3,3) == 1; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
// Map the four corners of 'r' and return the bounding box of those points. The four corners of |
| 198 |
// 'r' are assumed to have z = 0 and w = 1. If the matrix has perspective, the returned |
| 199 |
// rectangle will be the bounding box of the projected points after being clipped to w > 0. |
| 200 |
static SkRect MapRect(const SkM44& m, const SkRect& r); |
| 201 |
|
| 202 |
// Returns the differential area scale factor for a local point 'p' that will be transformed |
| 203 |
// by 'm' (which may have perspective). If 'm' does not have perspective, this scale factor is |
| 204 |
// constant regardless of 'p'; when it does have perspective, it is specific to that point. |
| 205 |
// |
| 206 |
// This can be crudely thought of as "device pixel area" / "local pixel area" at 'p'. |
| 207 |
// |
| 208 |
// Returns positive infinity if the transformed homogeneous point has w <= 0. |
| 209 |
static SkScalar DifferentialAreaScale(const SkMatrix& m, const SkPoint& p); |
| 210 |
|
| 211 |
// Determines if the transformation m applied to the bounds can be approximated by |
| 212 |
// an affine transformation, i.e., the perspective part of the transformation has little |
| 213 |
// visible effect. |
| 214 |
static bool NearlyAffine(const SkMatrix& m, |
| 215 |
const SkRect& bounds, |
| 216 |
SkScalar tolerance = SK_ScalarNearlyZero); |
| 217 |
|
| 218 |
static SkScalar ComputeResScaleForStroking(const SkMatrix& matrix); |
| 219 |
}; |
| 220 |
|
| 221 |
#endif |