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 "SkPatch.h" |
9 | 9 |
10 #include "SkGeometry.h" | 10 #include "SkGeometry.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 SkScalar cx[4], cy[4]; | 46 SkScalar cx[4], cy[4]; |
47 SkGetCubicCoeff(fPoints, cx, cy); | 47 SkGetCubicCoeff(fPoints, cx, cy); |
48 fCoefs[0].set(cx[0], cy[0]); | 48 fCoefs[0].set(cx[0], cy[0]); |
49 fCoefs[1].set(cx[1], cy[1]); | 49 fCoefs[1].set(cx[1], cy[1]); |
50 fCoefs[2].set(cx[2], cy[2]); | 50 fCoefs[2].set(cx[2], cy[2]); |
51 fCoefs[3].set(cx[3], cy[3]); | 51 fCoefs[3].set(cx[3], cy[3]); |
52 | 52 |
53 this->restart(1); | 53 this->restart(1); |
54 } | 54 } |
55 | 55 |
56 explicit FwDCubicEvaluator(SkPoint points[4]) { | |
57 for (int i = 0; i< 4; i++) { | |
58 fPoints[i] = points[i]; | |
59 } | |
60 | |
61 SkScalar cx[4], cy[4]; | |
62 SkGetCubicCoeff(fPoints, cx, cy); | |
63 fCoefs[0].set(cx[0], cy[0]); | |
64 fCoefs[1].set(cx[1], cy[1]); | |
65 fCoefs[2].set(cx[2], cy[2]); | |
66 fCoefs[3].set(cx[3], cy[3]); | |
67 | |
68 this->restart(1); | |
69 } | |
70 | |
56 /** | 71 /** |
57 * Restarts the forward differences evaluator to the first value of t = 0. | 72 * Restarts the forward differences evaluator to the first value of t = 0. |
58 */ | 73 */ |
59 void restart(int divisions) { | 74 void restart(int divisions) { |
60 fDivisions = divisions; | 75 fDivisions = divisions; |
61 SkScalar h = 1.f / fDivisions; | 76 SkScalar h = 1.f / fDivisions; |
62 fCurrent = 0; | 77 fCurrent = 0; |
63 fMax = fDivisions + 1; | 78 fMax = fDivisions + 1; |
64 fFwDiff[0] = fCoefs[3]; | 79 fFwDiff[0] = fCoefs[3]; |
65 SkScalar h2 = h * h; | 80 SkScalar h2 = h * h; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 fCornerColors[2] = SkPreMultiplyColor(colors[2]); | 128 fCornerColors[2] = SkPreMultiplyColor(colors[2]); |
114 fCornerColors[3] = SkPreMultiplyColor(colors[3]); | 129 fCornerColors[3] = SkPreMultiplyColor(colors[3]); |
115 } | 130 } |
116 | 131 |
117 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11) { | 132 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11) { |
118 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 133 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
119 SkScalar b = c01 * (1.f - tx) + c11 * tx; | 134 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
120 return uint8_t(a * (1.f - ty) + b * ty); | 135 return uint8_t(a * (1.f - ty) + b * ty); |
121 } | 136 } |
122 | 137 |
123 bool SkPatch::getVertexData(SkPatch::VertexData* data, int divisions) { | 138 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const { |
124 | 139 |
125 if (divisions < 1) { | 140 if (lodX < 1 || lodY < 1) { |
126 return false; | 141 return false; |
127 } | 142 } |
128 | 143 |
129 int divX = divisions, divY = divisions; | 144 // number of indices is limited by size of uint16_t, so we clamp it to avoid overflow |
130 | 145 data->fVertexCount = (lodX + 1) * (lodY + 1); |
131 data->fVertexCount = (divX + 1) * (divY + 1); | 146 if (data->fVertexCount > 65536) { |
132 data->fIndexCount = divX * divY * 6; | 147 data->fVertexCount = 65536; |
148 lodX = lodY = 255; | |
bsalomon
2014/07/31 17:39:06
lodX = SkMin(lodX, 255) and same for lodY?
dandov
2014/07/31 21:35:40
Done.
| |
149 } | |
150 data->fIndexCount = lodX * lodY * 6; | |
133 | 151 |
134 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 152 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
135 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); | 153 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); |
136 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 154 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
137 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); | 155 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); |
138 | 156 |
139 FwDCubicEvaluator fBottom(fCtrlPoints[kBottomP0_CubicCtrlPts], | 157 SkPoint pts[4]; |
140 fCtrlPoints[kBottomP1_CubicCtrlPts], | 158 this->getBottomPoints(pts); |
141 fCtrlPoints[kBottomP2_CubicCtrlPts], | 159 FwDCubicEvaluator fBottom(pts); |
142 fCtrlPoints[kBottomP3_CubicCtrlPts]), | 160 this->getTopPoints(pts); |
143 fTop(fCtrlPoints[kTopP0_CubicCtrlPts], | 161 FwDCubicEvaluator fTop(pts); |
144 fCtrlPoints[kTopP1_CubicCtrlPts], | 162 this->getLeftPoints(pts); |
145 fCtrlPoints[kTopP2_CubicCtrlPts], | 163 FwDCubicEvaluator fLeft(pts); |
146 fCtrlPoints[kTopP2_CubicCtrlPts]), | 164 this->getRightPoints(pts); |
147 fLeft(fCtrlPoints[kLeftP0_CubicCtrlPts], | 165 FwDCubicEvaluator fRight(pts); |
148 fCtrlPoints[kLeftP1_CubicCtrlPts], | |
149 fCtrlPoints[kLeftP2_CubicCtrlPts], | |
150 fCtrlPoints[kLeftP3_CubicCtrlPts]), | |
151 fRight(fCtrlPoints[kRightP0_CubicCtrlPts], | |
152 fCtrlPoints[kRightP1_CubicCtrlPts], | |
153 fCtrlPoints[kRightP2_CubicCtrlPts], | |
154 fCtrlPoints[kRightP3_CubicCtrlPts]); | |
155 | 166 |
156 fBottom.restart(divX); | 167 fBottom.restart(lodX); |
157 fTop.restart(divX); | 168 fTop.restart(lodX); |
158 | 169 |
159 SkScalar u = 0.0f; | 170 SkScalar u = 0.0f; |
160 int stride = divY + 1; | 171 int stride = lodY + 1; |
161 for (int x = 0; x <= divX; x++) { | 172 for (int x = 0; x <= lodX; x++) { |
162 SkPoint bottom = fBottom.next(), top = fTop.next(); | 173 SkPoint bottom = fBottom.next(), top = fTop.next(); |
163 fLeft.restart(divY); | 174 fLeft.restart(lodY); |
164 fRight.restart(divY); | 175 fRight.restart(lodY); |
165 SkScalar v = 0.f; | 176 SkScalar v = 0.f; |
166 for (int y = 0; y <= divY; y++) { | 177 for (int y = 0; y <= lodY; y++) { |
167 int dataIndex = x * (divX + 1) + y; | 178 int dataIndex = x * (lodY + 1) + y; |
168 | 179 |
169 SkPoint left = fLeft.next(), right = fRight.next(); | 180 SkPoint left = fLeft.next(), right = fRight.next(); |
170 | 181 |
171 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), | 182 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), |
172 (1.0f - v) * top.y() + v * bottom.y()); | 183 (1.0f - v) * top.y() + v * bottom.y()); |
173 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), | 184 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), |
174 (1.0f - u) * left.y() + u * right.y()); | 185 (1.0f - u) * left.y() + u * right.y()); |
175 SkPoint s2 = SkPoint::Make( | 186 SkPoint s2 = SkPoint::Make( |
176 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo ints()[0].x() | 187 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo ints()[0].x() |
177 + u * fTop.getCtrlPoints()[3].x()) | 188 + u * fTop.getCtrlPoints()[3].x()) |
178 + v * ((1.0f - u) * fBottom.getCtrlPoint s()[0].x() | 189 + v * ((1.0f - u) * fBottom.getCtrlPoint s()[0].x() |
179 + u * fBottom.getCtrlPoints()[3].x()), | 190 + u * fBottom.getCtrlPoints()[3].x()), |
180 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo ints()[0].y() | 191 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo ints()[0].y() |
181 + u * fTop.getCtrlPoints()[3].y()) | 192 + u * fTop.getCtrlPoints()[3].y()) |
182 + v * ((1.0f - u) * fBottom.getCtrlPoint s()[0].y() | 193 + v * ((1.0f - u) * fBottom.getCtrlPoint s()[0].y() |
183 + u * fBottom.getCtrlPoints()[3].y())); | 194 + u * fBottom.getCtrlPoints()[3].y())); |
184 data->fPoints[dataIndex] = s0 + s1 - s2; | 195 data->fPoints[dataIndex] = s0 + s1 - s2; |
185 | 196 |
186 uint8_t a = bilinear(u, v, | 197 uint8_t a = bilinear(u, v, |
187 SkScalar(SkColorGetA(fCornerColors[0])), | 198 SkScalar(SkColorGetA(fCornerColors[0])), |
188 SkScalar(SkColorGetA(fCornerColors[1])), | 199 SkScalar(SkColorGetA(fCornerColors[1])), |
189 SkScalar(SkColorGetA(fCornerColors[2])), | 200 SkScalar(SkColorGetA(fCornerColors[2])), |
190 SkScalar(SkColorGetA(fCornerColors[3]))); | 201 SkScalar(SkColorGetA(fCornerColors[3]))); |
191 uint8_t r = bilinear(u, v, | 202 uint8_t r = bilinear(u, v, |
192 SkScalar(SkColorGetR(fCornerColors[0])), | 203 SkScalar(SkColorGetR(fCornerColors[0])), |
193 SkScalar(SkColorGetR(fCornerColors[1])), | 204 SkScalar(SkColorGetR(fCornerColors[1])), |
194 SkScalar(SkColorGetR(fCornerColors[2])), | 205 SkScalar(SkColorGetR(fCornerColors[2])), |
195 SkScalar(SkColorGetR(fCornerColors[3]))); | 206 SkScalar(SkColorGetR(fCornerColors[3]))); |
207 // check in case there are some precision errors. | |
208 r = r > a ? a : r; | |
196 uint8_t g = bilinear(u, v, | 209 uint8_t g = bilinear(u, v, |
197 SkScalar(SkColorGetG(fCornerColors[0])), | 210 SkScalar(SkColorGetG(fCornerColors[0])), |
198 SkScalar(SkColorGetG(fCornerColors[1])), | 211 SkScalar(SkColorGetG(fCornerColors[1])), |
199 SkScalar(SkColorGetG(fCornerColors[2])), | 212 SkScalar(SkColorGetG(fCornerColors[2])), |
200 SkScalar(SkColorGetG(fCornerColors[3]))); | 213 SkScalar(SkColorGetG(fCornerColors[3]))); |
214 g = g > a ? a : g; | |
201 uint8_t b = bilinear(u, v, | 215 uint8_t b = bilinear(u, v, |
202 SkScalar(SkColorGetB(fCornerColors[0])), | 216 SkScalar(SkColorGetB(fCornerColors[0])), |
203 SkScalar(SkColorGetB(fCornerColors[1])), | 217 SkScalar(SkColorGetB(fCornerColors[1])), |
204 SkScalar(SkColorGetB(fCornerColors[2])), | 218 SkScalar(SkColorGetB(fCornerColors[2])), |
205 SkScalar(SkColorGetB(fCornerColors[3]))); | 219 SkScalar(SkColorGetB(fCornerColors[3]))); |
220 b = b > a ? a : b; | |
206 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); | 221 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); |
207 | 222 |
208 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); | 223 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); |
209 | 224 |
210 if(x < divX && y < divY) { | 225 if(x < lodX && y < lodY) { |
211 int i = 6 * (x * divY + y); | 226 int i = 6 * (x * lodY + y); |
212 data->fIndices[i] = x * stride + y; | 227 data->fIndices[i] = x * stride + y; |
213 data->fIndices[i + 1] = x * stride + 1 + y; | 228 data->fIndices[i + 1] = x * stride + 1 + y; |
214 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; | 229 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; |
215 data->fIndices[i + 3] = data->fIndices[i]; | 230 data->fIndices[i + 3] = data->fIndices[i]; |
216 data->fIndices[i + 4] = data->fIndices[i + 2]; | 231 data->fIndices[i + 4] = data->fIndices[i + 2]; |
217 data->fIndices[i + 5] = (x + 1) * stride + y; | 232 data->fIndices[i + 5] = (x + 1) * stride + y; |
218 } | 233 } |
219 v += 1.f / divY; | 234 v += 1.f / lodY; |
220 } | 235 } |
221 u += 1.f / divX; | 236 u += 1.f / lodX; |
222 } | 237 } |
223 return true; | 238 return true; |
224 } | 239 } |
OLD | NEW |