OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkPatch.h" | 8 #include "SkGeometry.h" |
| 9 #include "SkBuffer.h" |
| 10 #include "SkColorPriv.h" |
| 11 #include "SkPatchUtils.h" |
9 | 12 |
10 #include "SkGeometry.h" | |
11 #include "SkColorPriv.h" | |
12 #include "SkBuffer.h" | |
13 | 13 |
14 //////////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////////// |
15 | 15 |
16 /** | 16 FwDCubicEvaluator::FwDCubicEvaluator() |
17 * Evaluator to sample the values of a cubic bezier using forward differences. | 17 : fMax(0) |
18 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only | 18 , fCurrent(0) |
19 * adding precalculated values. | 19 , fDivisions(0) { |
20 * For a linear example we have the function f(t) = m*t+b, then the value of tha
t function at t+h | 20 memset(fPoints, 0, 4 * sizeof(SkPoint)); |
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 | 21 memset(fPoints, 0, 4 * sizeof(SkPoint)); |
22 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After | 22 memset(fPoints, 0, 4 * sizeof(SkPoint)); |
23 * obtaining this value (mh) we could just add this constant step to our first s
ampled point | 23 } |
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 | 24 |
33 class FwDCubicEvaluator { | 25 FwDCubicEvaluator::FwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoint d)
{ |
| 26 fPoints[0] = a; |
| 27 fPoints[1] = b; |
| 28 fPoints[2] = c; |
| 29 fPoints[3] = d; |
34 | 30 |
35 public: | 31 SkScalar cx[4], cy[4]; |
36 FwDCubicEvaluator() { } | 32 SkGetCubicCoeff(fPoints, cx, cy); |
| 33 fCoefs[0].set(cx[0], cy[0]); |
| 34 fCoefs[1].set(cx[1], cy[1]); |
| 35 fCoefs[2].set(cx[2], cy[2]); |
| 36 fCoefs[3].set(cx[3], cy[3]); |
37 | 37 |
38 /** | 38 this->restart(1); |
39 * Receives the 4 control points of the cubic bezier. | 39 } |
40 */ | 40 |
41 FwDCubicEvaluator(SkPoint a, SkPoint b, SkPoint c, SkPoint d) { | 41 FwDCubicEvaluator::FwDCubicEvaluator(const SkPoint points[4]) { |
42 fPoints[0] = a; | 42 memcpy(fPoints, points, 4 * sizeof(SkPoint)); |
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 | 43 |
57 explicit FwDCubicEvaluator(SkPoint points[4]) { | 44 SkScalar cx[4], cy[4]; |
58 for (int i = 0; i< 4; i++) { | 45 SkGetCubicCoeff(fPoints, cx, cy); |
59 fPoints[i] = points[i]; | 46 fCoefs[0].set(cx[0], cy[0]); |
60 } | 47 fCoefs[1].set(cx[1], cy[1]); |
61 | 48 fCoefs[2].set(cx[2], cy[2]); |
62 SkScalar cx[4], cy[4]; | 49 fCoefs[3].set(cx[3], cy[3]); |
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 | 50 |
72 /** | 51 this->restart(1); |
73 * Restarts the forward differences evaluator to the first value of t = 0. | 52 } |
74 */ | 53 |
75 void restart(int divisions) { | 54 void FwDCubicEvaluator::restart(int divisions) { |
76 fDivisions = divisions; | 55 fDivisions = divisions; |
77 SkScalar h = 1.f / fDivisions; | 56 SkScalar h = 1.f / fDivisions; |
78 fCurrent = 0; | 57 fCurrent = 0; |
79 fMax = fDivisions + 1; | 58 fMax = fDivisions + 1; |
80 fFwDiff[0] = fCoefs[3]; | 59 fFwDiff[0] = fCoefs[3]; |
81 SkScalar h2 = h * h; | 60 SkScalar h2 = h * h; |
82 SkScalar h3 = h2 * h; | 61 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 | 62 |
91 /** | 63 fFwDiff[3].set(6.f * fCoefs[0].x() * h3, 6.f * fCoefs[0].y() * h3); //6ah^3 |
92 * Check if the evaluator is still within the range of 0<=t<=1 | 64 fFwDiff[2].set(fFwDiff[3].x() + 2.f * fCoefs[1].x() * h2, //6ah^3 + 2bh^2 |
93 */ | 65 fFwDiff[3].y() + 2.f * fCoefs[1].y() * h2); |
94 bool done() const { | 66 fFwDiff[1].set(fCoefs[0].x() * h3 + fCoefs[1].x() * h2 + fCoefs[2].x() * h,/
/ah^3 + bh^2 +ch |
95 return fCurrent > fMax; | 67 fCoefs[0].y() * h3 + fCoefs[1].y() * h2 + fCoefs[2].y() * h); |
96 } | 68 } |
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 | 69 |
119 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
120 | 71 |
121 SkPatch::SkPatch(const SkPoint points[12], const SkColor colors[4]) { | 72 SkPatch::SkPatch(const SkPoint points[12]) { |
122 this->reset(points, colors); | 73 this->reset(points); |
123 } | 74 } |
124 | 75 |
125 static uint8_t bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkSc
alar c01, | 76 bool SkPatch::getVertexData(SkPatch::VertexData* data, const SkColor colors[4], |
126 SkScalar c11) { | 77 const SkPoint texCoords[4], int lodX, int lodY) cons
t { |
127 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 78 return SkPatchUtils::getVertexData(data, fCtrlPoints, colors, texCoords, lod
X, lodY); |
128 SkScalar b = c01 * (1.f - tx) + c11 * tx; | |
129 return uint8_t(a * (1.f - ty) + b * ty); | |
130 } | 79 } |
131 | 80 |
132 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ | 81 const SkRect SkPatch::getBounds() const { |
133 | 82 SkRect bounds; |
134 if (lodX < 1 || lodY < 1) { | 83 bounds.set(fCtrlPoints, kNumCtrlPts); |
135 return false; | 84 return bounds; |
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 } | 85 } |
234 | 86 |
235 size_t SkPatch::writeToMemory(void* storage) const { | 87 size_t SkPatch::writeToMemory(void* storage) const { |
236 int byteCount = kNumCtrlPts * sizeof(SkPoint) + kNumColors * sizeof(SkColor
); | 88 int byteCount = kNumCtrlPts * sizeof(SkPoint); |
237 | 89 |
238 if (NULL == storage) { | 90 if (NULL == storage) { |
239 return SkAlign4(byteCount); | 91 return SkAlign4(byteCount); |
240 } | 92 } |
241 | 93 |
242 SkWBuffer buffer(storage); | 94 SkWBuffer buffer(storage); |
243 | 95 |
244 buffer.write(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint)); | 96 buffer.write(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint)); |
245 buffer.write(fCornerColors, kNumColors * sizeof(SkColor)); | |
246 | 97 |
247 buffer.padToAlign4(); | 98 buffer.padToAlign4(); |
248 return buffer.pos(); | 99 return buffer.pos(); |
249 } | 100 } |
250 | 101 |
251 size_t SkPatch::readFromMemory(const void* storage, size_t length) { | 102 size_t SkPatch::readFromMemory(const void* storage, size_t length) { |
252 SkRBufferWithSizeCheck buffer(storage, length); | 103 SkRBufferWithSizeCheck buffer(storage, length); |
253 | 104 |
254 if (!buffer.read(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint))) { | 105 if (!buffer.read(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint))) { |
255 return 0; | 106 return 0; |
256 } | 107 } |
257 | 108 |
258 if (!buffer.read(fCornerColors, kNumColors * sizeof(SkColor))) { | 109 return kNumCtrlPts * sizeof(SkPoint); |
259 return 0; | |
260 } | |
261 return kNumCtrlPts * sizeof(SkPoint) + kNumColors * sizeof(SkColor); | |
262 } | 110 } |
OLD | NEW |