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/SkFilterQuality.h" 12 #include "include/core/SkMatrix.h" 13 #include "include/private/SkNx.h" 14 #include "src/core/SkPointPriv.h" 15 16 class SkMatrixPriv { 17 public: 18 enum { 19 // writeTo/readFromMemory will never return a value larger than this 20 kMaxFlattenSize = 9 * sizeof(SkScalar) + sizeof(uint32_t), 21 }; 22 WriteToMemory(const SkMatrix & matrix,void * buffer)23 static size_t WriteToMemory(const SkMatrix& matrix, void* buffer) { 24 return matrix.writeToMemory(buffer); 25 } 26 ReadFromMemory(SkMatrix * matrix,const void * buffer,size_t length)27 static size_t ReadFromMemory(SkMatrix* matrix, const void* buffer, size_t length) { 28 return matrix->readFromMemory(buffer, length); 29 } 30 31 typedef SkMatrix::MapXYProc MapXYProc; 32 typedef SkMatrix::MapPtsProc MapPtsProc; 33 34 GetMapPtsProc(const SkMatrix & matrix)35 static MapPtsProc GetMapPtsProc(const SkMatrix& matrix) { 36 return SkMatrix::GetMapPtsProc(matrix.getType()); 37 } 38 GetMapXYProc(const SkMatrix & matrix)39 static MapXYProc GetMapXYProc(const SkMatrix& matrix) { 40 return SkMatrix::GetMapXYProc(matrix.getType()); 41 } 42 43 /** 44 * Attempt to map the rect through the inverse of the matrix. If it is not invertible, 45 * then this returns false and dst is unchanged. 46 */ InverseMapRect(const SkMatrix & mx,SkRect * dst,const SkRect & src)47 static bool SK_WARN_UNUSED_RESULT InverseMapRect(const SkMatrix& mx, 48 SkRect* dst, const SkRect& src) { 49 if (mx.getType() <= SkMatrix::kTranslate_Mask) { 50 SkScalar tx = mx.getTranslateX(); 51 SkScalar ty = mx.getTranslateY(); 52 Sk4f trans(tx, ty, tx, ty); 53 (Sk4f::Load(&src.fLeft) - trans).store(&dst->fLeft); 54 return true; 55 } 56 // Insert other special-cases here (e.g. scale+translate) 57 58 // general case 59 SkMatrix inverse; 60 if (mx.invert(&inverse)) { 61 inverse.mapRect(dst, src); 62 return true; 63 } 64 return false; 65 } 66 67 /** Maps count pts, skipping stride bytes to advance from one SkPoint to the next. 68 Points are mapped by multiplying each SkPoint by SkMatrix. Given: 69 70 | A B C | | x | 71 Matrix = | D E F |, pt = | y | 72 | G H I | | 1 | 73 74 each resulting pts SkPoint is computed as: 75 76 |A B C| |x| Ax+By+C Dx+Ey+F 77 Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , ------- 78 |G H I| |1| Gx+Hy+I Gx+Hy+I 79 80 @param mx matrix used to map the points 81 @param pts storage for mapped points 82 @param stride size of record starting with SkPoint, in bytes 83 @param count number of points to transform 84 */ MapPointsWithStride(const SkMatrix & mx,SkPoint pts[],size_t stride,int count)85 static void MapPointsWithStride(const SkMatrix& mx, SkPoint pts[], size_t stride, int count) { 86 SkASSERT(stride >= sizeof(SkPoint)); 87 SkASSERT(0 == stride % sizeof(SkScalar)); 88 89 SkMatrix::TypeMask tm = mx.getType(); 90 91 if (SkMatrix::kIdentity_Mask == tm) { 92 return; 93 } 94 if (SkMatrix::kTranslate_Mask == tm) { 95 const SkScalar tx = mx.getTranslateX(); 96 const SkScalar ty = mx.getTranslateY(); 97 Sk2s trans(tx, ty); 98 for (int i = 0; i < count; ++i) { 99 (Sk2s::Load(&pts->fX) + trans).store(&pts->fX); 100 pts = (SkPoint*)((intptr_t)pts + stride); 101 } 102 return; 103 } 104 // Insert other special-cases here (e.g. scale+translate) 105 106 // general case 107 SkMatrix::MapXYProc proc = mx.getMapXYProc(); 108 for (int i = 0; i < count; ++i) { 109 proc(mx, pts->fX, pts->fY, pts); 110 pts = (SkPoint*)((intptr_t)pts + stride); 111 } 112 } 113 114 /** Maps src SkPoint array of length count to dst SkPoint array, skipping stride bytes 115 to advance from one SkPoint to the next. 116 Points are mapped by multiplying each SkPoint by SkMatrix. Given: 117 118 | A B C | | x | 119 Matrix = | D E F |, src = | y | 120 | G H I | | 1 | 121 122 each resulting dst SkPoint is computed as: 123 124 |A B C| |x| Ax+By+C Dx+Ey+F 125 Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , ------- 126 |G H I| |1| Gx+Hy+I Gx+Hy+I 127 128 @param mx matrix used to map the points 129 @param dst storage for mapped points 130 @param src points to transform 131 @param stride size of record starting with SkPoint, in bytes 132 @param count number of points to transform 133 */ MapPointsWithStride(const SkMatrix & mx,SkPoint dst[],size_t dstStride,const SkPoint src[],size_t srcStride,int count)134 static void MapPointsWithStride(const SkMatrix& mx, SkPoint dst[], size_t dstStride, 135 const SkPoint src[], size_t srcStride, int count) { 136 SkASSERT(srcStride >= sizeof(SkPoint)); 137 SkASSERT(dstStride >= sizeof(SkPoint)); 138 SkASSERT(0 == srcStride % sizeof(SkScalar)); 139 SkASSERT(0 == dstStride % sizeof(SkScalar)); 140 for (int i = 0; i < count; ++i) { 141 mx.mapPoints(dst, src, 1); 142 src = (SkPoint*)((intptr_t)src + srcStride); 143 dst = (SkPoint*)((intptr_t)dst + dstStride); 144 } 145 } 146 147 static void MapHomogeneousPointsWithStride(const SkMatrix& mx, SkPoint3 dst[], size_t dstStride, 148 const SkPoint3 src[], size_t srcStride, int count); 149 150 // Returns the recommended filterquality, assuming the caller originally wanted kHigh (bicubic) 151 static SkFilterQuality AdjustHighQualityFilterLevel(const SkMatrix&, 152 bool matrixIsInverse = false); 153 }; 154 155 #endif 156