OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 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 #include "SkPatch.h" |
| 9 |
| 10 #include "SkGeometry.h" |
| 11 #include "SkGr.h" |
| 12 |
| 13 |
| 14 //////////////////////////////////////////////////////////////////////////////// |
| 15 |
| 16 SkFwDCubicEvaluator::SkFwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoin
t d){ |
| 17 fPoints[0] = a; |
| 18 fPoints[1] = b; |
| 19 fPoints[2] = c; |
| 20 fPoints[3] = d; |
| 21 |
| 22 SkScalar cx[4], cy[4]; |
| 23 SkGetCubicCoeff(fPoints, cx, cy); |
| 24 fCoefs[0].set(cx[0], cy[0]); |
| 25 fCoefs[1].set(cx[1], cy[1]); |
| 26 fCoefs[2].set(cx[2], cy[2]); |
| 27 fCoefs[3].set(cx[3], cy[3]); |
| 28 |
| 29 this->reset(1); |
| 30 } |
| 31 |
| 32 void SkFwDCubicEvaluator::reset(int res){ |
| 33 fRes = res; |
| 34 SkScalar h = 1.f/res; |
| 35 fCurrent = 0; |
| 36 fMax = res + 1; |
| 37 fFwDiff[0] = fCoefs[3]; |
| 38 SkScalar h2 = h*h; |
| 39 SkScalar h3 = h2*h; |
| 40 |
| 41 fFwDiff[3].set(6.f*fCoefs[0].x()*h3, 6.f*fCoefs[0].y()*h3); //6ah^3 |
| 42 fFwDiff[2].set(fFwDiff[3].x() + 2.f*fCoefs[1].x()*h2, //6ah^3 + 2bh^2 |
| 43 fFwDiff[3].y() + 2.f*fCoefs[1].y()*h2); |
| 44 fFwDiff[1].set(fCoefs[0].x()*h3 + fCoefs[1].x()*h2 + fCoefs[2].x()*h, //ah^3
+ bh^2 + ch |
| 45 fCoefs[0].y()*h3 + fCoefs[1].y()*h2 + fCoefs[2].y()*h); |
| 46 } |
| 47 |
| 48 const SkPoint* SkFwDCubicEvaluator::getPoints() { |
| 49 return fPoints; |
| 50 } |
| 51 |
| 52 const SkPoint* SkFwDCubicEvaluator::getCoefs() { |
| 53 return fCoefs; |
| 54 } |
| 55 |
| 56 int SkFwDCubicEvaluator::getResolution() { |
| 57 return fRes; |
| 58 } |
| 59 |
| 60 //////////////////////////////////////////////////////////////////////////////// |
| 61 |
| 62 SkPatch::SkPatch(SkPoint points[12], SkColor (*colors)[4], int res) |
| 63 : fCtrlPoints(points) |
| 64 , fBottom(points[0], points[1], points[2], points[3]) |
| 65 , fTop(points[4], points[5], points[6], points[7]) |
| 66 , fLeft(points[0], points[8], points[9], points[4]) |
| 67 , fRight(points[3], points[10], points[11], points[7]) |
| 68 , fResX(res) |
| 69 , fResY(res) |
| 70 , fVertCount(0) |
| 71 , fIndexCount(0) |
| 72 , fPoints(NULL) |
| 73 , fTexCoords(NULL) |
| 74 , fColors(NULL) |
| 75 , fIndices(NULL) |
| 76 , fIsSet(false) { |
| 77 fCornerColors[0] = SkPreMultiplyColor((*colors)[0]); |
| 78 fCornerColors[1] = SkPreMultiplyColor((*colors)[1]); |
| 79 fCornerColors[2] = SkPreMultiplyColor((*colors)[2]); |
| 80 fCornerColors[3] = SkPreMultiplyColor((*colors)[3]); |
| 81 } |
| 82 |
| 83 void SkPatch::clean() { |
| 84 SkDELETE_ARRAY (fPoints); |
| 85 SkDELETE_ARRAY (fTexCoords); |
| 86 SkDELETE_ARRAY (fColors); |
| 87 SkDELETE_ARRAY (fIndices); |
| 88 fVertCount = 0; |
| 89 fIndexCount = 0; |
| 90 fPoints = NULL; |
| 91 fTexCoords = NULL; |
| 92 fColors = NULL; |
| 93 fIndices = NULL; |
| 94 fIsSet = false; |
| 95 } |
| 96 |
| 97 SkPatch::~SkPatch() { |
| 98 this->clean(); |
| 99 } |
| 100 |
| 101 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar
c01, SkScalar c11) { |
| 102 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
| 103 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
| 104 return uint8_t(a * (1.f - ty) + b * ty); |
| 105 } |
| 106 |
| 107 void SkPatch::setData() { |
| 108 |
| 109 if (fIsSet) { |
| 110 return; |
| 111 } |
| 112 |
| 113 fIsSet = true; |
| 114 fVertCount = (fResX + 1) * (fResY + 1); |
| 115 fIndexCount = fResX * fResY * 6; |
| 116 |
| 117 fPoints = SkNEW_ARRAY(SkPoint, fVertCount); |
| 118 fColors = SkNEW_ARRAY(uint32_t, fVertCount); |
| 119 fTexCoords = SkNEW_ARRAY(SkPoint, fVertCount); |
| 120 fIndices = SkNEW_ARRAY(uint16_t, fIndexCount); |
| 121 |
| 122 fBottom.reset(fResX); |
| 123 fTop.reset(fResX); |
| 124 |
| 125 SkScalar u = 0.0f; |
| 126 int stride = fResY+1; |
| 127 for (int x = 0; x <= fResX; x++) { |
| 128 SkPoint bottom = fBottom++, top = fTop++; |
| 129 fLeft.reset(fResY); |
| 130 fRight.reset(fResY); |
| 131 SkScalar v = 0.f; |
| 132 for (int y = 0; y <= fResY; y++) { |
| 133 int dataIndex = x*(fResX + 1) + y; |
| 134 |
| 135 SkPoint left = fLeft++, right = fRight++; |
| 136 |
| 137 SkPoint s0 = SkPoint::Make((1.0f - v)*bottom.x() + v*top.x(), |
| 138 (1.0f - v)*bottom.y() + v*top.y()); |
| 139 SkPoint s1 = SkPoint::Make((1.0f - u)*left.x() + u*right.x(), |
| 140 (1.0f - u)*left.y() + u*right.y()); |
| 141 SkPoint s2 = SkPoint::Make( |
| 142 (1.0f - v)*((1.0f - u)*fBottom.getPoints()[0].x() |
| 143 + u*fBottom.getPoints()[3].x()) |
| 144 + v*((1.0f - u)*fTop.getPoints()[0].x() + u*fTop.get
Points()[3].x()), |
| 145 (1.0f - v)*((1.0f - u)*fBottom.getPoints()[0].y() |
| 146 + u*fBottom.getPoints()[3].y()) |
| 147 + v*((1.0f - u)*fTop.getPoints()[0].y() + u*fTop.get
Points()[3].y())); |
| 148 fPoints[dataIndex] = s0 + s1 - s2; |
| 149 |
| 150 uint8_t a = bilinear(u, v, |
| 151 SkScalar(SkColorGetA(fCornerColors[0])), |
| 152 SkScalar(SkColorGetA(fCornerColors[1])), |
| 153 SkScalar(SkColorGetA(fCornerColors[2])), |
| 154 SkScalar(SkColorGetA(fCornerColors[3]))); |
| 155 uint8_t r = bilinear(u, v, |
| 156 SkScalar(SkColorGetR(fCornerColors[0])), |
| 157 SkScalar(SkColorGetR(fCornerColors[1])), |
| 158 SkScalar(SkColorGetR(fCornerColors[2])), |
| 159 SkScalar(SkColorGetR(fCornerColors[3]))); |
| 160 uint8_t g = bilinear(u, v, |
| 161 SkScalar(SkColorGetG(fCornerColors[0])), |
| 162 SkScalar(SkColorGetG(fCornerColors[1])), |
| 163 SkScalar(SkColorGetG(fCornerColors[2])), |
| 164 SkScalar(SkColorGetG(fCornerColors[3]))); |
| 165 uint8_t b = bilinear(u, v, |
| 166 SkScalar(SkColorGetB(fCornerColors[0])), |
| 167 SkScalar(SkColorGetB(fCornerColors[1])), |
| 168 SkScalar(SkColorGetB(fCornerColors[2])), |
| 169 SkScalar(SkColorGetB(fCornerColors[3]))); |
| 170 fColors[dataIndex] = SkPackARGB32(a,r,g,b); |
| 171 |
| 172 |
| 173 fTexCoords[dataIndex] = SkPoint::Make(u, v); |
| 174 |
| 175 if(x < fResX && y < fResY) { |
| 176 int i = 6*(x*fResY + y); |
| 177 fIndices[i] = x*stride+y; |
| 178 fIndices[i+1] = x*stride+1+y; |
| 179 fIndices[i+2] = (x+1)*stride+1+y; |
| 180 fIndices[i+3] = fIndices[i]; |
| 181 fIndices[i+4] = fIndices[i+2]; |
| 182 fIndices[i+5] = (x+1)*stride+y; |
| 183 } |
| 184 v+=1.f/fResY; |
| 185 } |
| 186 u+=1.f/fResX; |
| 187 } |
| 188 } |
OLD | NEW |