| 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 "SkColorPriv.h" | |
| 12 #include "SkBuffer.h" | |
| 13 | |
| 14 //////////////////////////////////////////////////////////////////////////////// | |
| 15 | |
| 16 /** | |
| 17 * Evaluator to sample the values of a cubic bezier using forward differences. | |
| 18 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only | |
| 19 * adding precalculated values. | |
| 20 * For a linear example we have the function f(t) = m*t+b, then the value of tha
t function at t+h | |
| 21 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must
add to the first | |
| 22 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After | |
| 23 * obtaining this value (mh) we could just add this constant step to our first s
ampled point | |
| 24 * to compute the next one. | |
| 25 * | |
| 26 * For the cubic case the first difference gives as a result a quadratic polynom
ial to which we can | |
| 27 * apply again forward differences and get linear function to which we can apply
again forward | |
| 28 * differences to get a constant difference. This is why we keep an array of siz
e 4, the 0th | |
| 29 * position keeps the sampled value while the next ones keep the quadratic, line
ar and constant | |
| 30 * difference values. | |
| 31 */ | |
| 32 | |
| 33 class FwDCubicEvaluator { | |
| 34 | |
| 35 public: | |
| 36 FwDCubicEvaluator() { } | |
| 37 | |
| 38 /** | |
| 39 * Receives the 4 control points of the cubic bezier. | |
| 40 */ | |
| 41 FwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoint d) { | |
| 42 fPoints[0] = a; | |
| 43 fPoints[1] = b; | |
| 44 fPoints[2] = c; | |
| 45 fPoints[3] = d; | |
| 46 | |
| 47 SkScalar cx[4], cy[4]; | |
| 48 SkGetCubicCoeff(fPoints, cx, cy); | |
| 49 fCoefs[0].set(cx[0], cy[0]); | |
| 50 fCoefs[1].set(cx[1], cy[1]); | |
| 51 fCoefs[2].set(cx[2], cy[2]); | |
| 52 fCoefs[3].set(cx[3], cy[3]); | |
| 53 | |
| 54 this->restart(1); | |
| 55 } | |
| 56 | |
| 57 explicit FwDCubicEvaluator(SkPoint points[4]) { | |
| 58 for (int i = 0; i< 4; i++) { | |
| 59 fPoints[i] = points[i]; | |
| 60 } | |
| 61 | |
| 62 SkScalar cx[4], cy[4]; | |
| 63 SkGetCubicCoeff(fPoints, cx, cy); | |
| 64 fCoefs[0].set(cx[0], cy[0]); | |
| 65 fCoefs[1].set(cx[1], cy[1]); | |
| 66 fCoefs[2].set(cx[2], cy[2]); | |
| 67 fCoefs[3].set(cx[3], cy[3]); | |
| 68 | |
| 69 this->restart(1); | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * Restarts the forward differences evaluator to the first value of t = 0. | |
| 74 */ | |
| 75 void restart(int divisions) { | |
| 76 fDivisions = divisions; | |
| 77 SkScalar h = 1.f / fDivisions; | |
| 78 fCurrent = 0; | |
| 79 fMax = fDivisions + 1; | |
| 80 fFwDiff[0] = fCoefs[3]; | |
| 81 SkScalar h2 = h * h; | |
| 82 SkScalar h3 = h2 * h; | |
| 83 | |
| 84 fFwDiff[3].set(6.f * fCoefs[0].x() * h3, 6.f * fCoefs[0].y() * h3); //6a
h^3 | |
| 85 fFwDiff[2].set(fFwDiff[3].x() + 2.f * fCoefs[1].x() * h2, //6ah^3 + 2bh^
2 | |
| 86 fFwDiff[3].y() + 2.f * fCoefs[1].y() * h2); | |
| 87 fFwDiff[1].set(fCoefs[0].x() * h3 + fCoefs[1].x() * h2 + fCoefs[2].x() *
h,//ah^3 + bh^2 +ch | |
| 88 fCoefs[0].y() * h3 + fCoefs[1].y() * h2 + fCoefs[2].y() *
h); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Check if the evaluator is still within the range of 0<=t<=1 | |
| 93 */ | |
| 94 bool done() const { | |
| 95 return fCurrent > fMax; | |
| 96 } | |
| 97 | |
| 98 /** | |
| 99 * Call next to obtain the SkPoint sampled and move to the next one. | |
| 100 */ | |
| 101 SkPoint next() { | |
| 102 SkPoint point = fFwDiff[0]; | |
| 103 fFwDiff[0] += fFwDiff[1]; | |
| 104 fFwDiff[1] += fFwDiff[2]; | |
| 105 fFwDiff[2] += fFwDiff[3]; | |
| 106 fCurrent++; | |
| 107 return point; | |
| 108 } | |
| 109 | |
| 110 const SkPoint* getCtrlPoints() const { | |
| 111 return fPoints; | |
| 112 } | |
| 113 | |
| 114 private: | |
| 115 int fMax, fCurrent, fDivisions; | |
| 116 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; | |
| 117 }; | |
| 118 | |
| 119 //////////////////////////////////////////////////////////////////////////////// | |
| 120 | |
| 121 SkPatch::SkPatch(const SkPoint points[12], const SkColor colors[4]) { | |
| 122 this->reset(points, colors); | |
| 123 } | |
| 124 | |
| 125 static uint8_t bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkSc
alar c01, | |
| 126 SkScalar c11) { | |
| 127 SkScalar a = c00 * (1.f - tx) + c10 * tx; | |
| 128 SkScalar b = c01 * (1.f - tx) + c11 * tx; | |
| 129 return uint8_t(a * (1.f - ty) + b * ty); | |
| 130 } | |
| 131 | |
| 132 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ | |
| 133 | |
| 134 if (lodX < 1 || lodY < 1) { | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 // premultiply colors to avoid color bleeding. | |
| 139 SkPMColor colors[SkPatch::kNumColors]; | |
| 140 for (int i = 0; i < SkPatch::kNumColors; i++) { | |
| 141 colors[i] = SkPreMultiplyColor(fCornerColors[i]); | |
| 142 } | |
| 143 | |
| 144 // number of indices is limited by size of uint16_t, so we clamp it to avoid
overflow | |
| 145 data->fVertexCount = SkMin32((lodX + 1) * (lodY + 1), 65536); | |
| 146 lodX = SkMin32(lodX, 255); | |
| 147 lodY = SkMin32(lodY, 255); | |
| 148 data->fIndexCount = lodX * lodY * 6; | |
| 149 | |
| 150 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); | |
| 151 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); | |
| 152 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); | |
| 153 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); | |
| 154 | |
| 155 SkPoint pts[SkPatch::kNumPtsCubic]; | |
| 156 this->getBottomPoints(pts); | |
| 157 FwDCubicEvaluator fBottom(pts); | |
| 158 this->getTopPoints(pts); | |
| 159 FwDCubicEvaluator fTop(pts); | |
| 160 this->getLeftPoints(pts); | |
| 161 FwDCubicEvaluator fLeft(pts); | |
| 162 this->getRightPoints(pts); | |
| 163 FwDCubicEvaluator fRight(pts); | |
| 164 | |
| 165 fBottom.restart(lodX); | |
| 166 fTop.restart(lodX); | |
| 167 | |
| 168 SkScalar u = 0.0f; | |
| 169 int stride = lodY + 1; | |
| 170 for (int x = 0; x <= lodX; x++) { | |
| 171 SkPoint bottom = fBottom.next(), top = fTop.next(); | |
| 172 fLeft.restart(lodY); | |
| 173 fRight.restart(lodY); | |
| 174 SkScalar v = 0.f; | |
| 175 for (int y = 0; y <= lodY; y++) { | |
| 176 int dataIndex = x * (lodY + 1) + y; | |
| 177 | |
| 178 SkPoint left = fLeft.next(), right = fRight.next(); | |
| 179 | |
| 180 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), | |
| 181 (1.0f - v) * top.y() + v * bottom.y()); | |
| 182 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), | |
| 183 (1.0f - u) * left.y() + u * right.y()); | |
| 184 SkPoint s2 = SkPoint::Make( | |
| 185 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() | |
| 186 + u * fTop.getCtrlPoints()[3].x()) | |
| 187 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].x() | |
| 188 + u * fBottom.getCtrlPoints()[3].x()), | |
| 189 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() | |
| 190 + u * fTop.getCtrlPoints()[3].y()) | |
| 191 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].y() | |
| 192 + u * fBottom.getCtrlPoints()[3].y())); | |
| 193 data->fPoints[dataIndex] = s0 + s1 - s2; | |
| 194 | |
| 195 uint8_t a = bilerp(u, v, | |
| 196 SkScalar(SkColorGetA(colors[kTopLeft_CornerColors]))
, | |
| 197 SkScalar(SkColorGetA(colors[kTopRight_CornerColors])
), | |
| 198 SkScalar(SkColorGetA(colors[kBottomLeft_CornerColors
])), | |
| 199 SkScalar(SkColorGetA(colors[kBottomRight_CornerColor
s]))); | |
| 200 uint8_t r = bilerp(u, v, | |
| 201 SkScalar(SkColorGetR(colors[kTopLeft_CornerColors]))
, | |
| 202 SkScalar(SkColorGetR(colors[kTopRight_CornerColors])
), | |
| 203 SkScalar(SkColorGetR(colors[kBottomLeft_CornerColors
])), | |
| 204 SkScalar(SkColorGetR(colors[kBottomRight_CornerColor
s]))); | |
| 205 uint8_t g = bilerp(u, v, | |
| 206 SkScalar(SkColorGetG(colors[kTopLeft_CornerColors]))
, | |
| 207 SkScalar(SkColorGetG(colors[kTopRight_CornerColors])
), | |
| 208 SkScalar(SkColorGetG(colors[kBottomLeft_CornerColors
])), | |
| 209 SkScalar(SkColorGetG(colors[kBottomRight_CornerColor
s]))); | |
| 210 uint8_t b = bilerp(u, v, | |
| 211 SkScalar(SkColorGetB(colors[kTopLeft_CornerColors]))
, | |
| 212 SkScalar(SkColorGetB(colors[kTopRight_CornerColors])
), | |
| 213 SkScalar(SkColorGetB(colors[kBottomLeft_CornerColors
])), | |
| 214 SkScalar(SkColorGetB(colors[kBottomRight_CornerColor
s]))); | |
| 215 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); | |
| 216 | |
| 217 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); | |
| 218 | |
| 219 if(x < lodX && y < lodY) { | |
| 220 int i = 6 * (x * lodY + y); | |
| 221 data->fIndices[i] = x * stride + y; | |
| 222 data->fIndices[i + 1] = x * stride + 1 + y; | |
| 223 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; | |
| 224 data->fIndices[i + 3] = data->fIndices[i]; | |
| 225 data->fIndices[i + 4] = data->fIndices[i + 2]; | |
| 226 data->fIndices[i + 5] = (x + 1) * stride + y; | |
| 227 } | |
| 228 v = SkScalarClampMax(v + 1.f / lodY, 1); | |
| 229 } | |
| 230 u = SkScalarClampMax(u + 1.f / lodX, 1); | |
| 231 } | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 235 size_t SkPatch::writeToMemory(void* storage) const { | |
| 236 int byteCount = kNumCtrlPts * sizeof(SkPoint) + kNumColors * sizeof(SkColor
); | |
| 237 | |
| 238 if (NULL == storage) { | |
| 239 return SkAlign4(byteCount); | |
| 240 } | |
| 241 | |
| 242 SkWBuffer buffer(storage); | |
| 243 | |
| 244 buffer.write(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint)); | |
| 245 buffer.write(fCornerColors, kNumColors * sizeof(SkColor)); | |
| 246 | |
| 247 buffer.padToAlign4(); | |
| 248 return buffer.pos(); | |
| 249 } | |
| 250 | |
| 251 size_t SkPatch::readFromMemory(const void* storage, size_t length) { | |
| 252 SkRBufferWithSizeCheck buffer(storage, length); | |
| 253 | |
| 254 if (!buffer.read(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint))) { | |
| 255 return 0; | |
| 256 } | |
| 257 | |
| 258 if (!buffer.read(fCornerColors, kNumColors * sizeof(SkColor))) { | |
| 259 return 0; | |
| 260 } | |
| 261 return kNumCtrlPts * sizeof(SkPoint) + kNumColors * sizeof(SkColor); | |
| 262 } | |
| OLD | NEW |