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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 112 |
98 private: | 113 private: |
99 int fMax, fCurrent, fDivisions; | 114 int fMax, fCurrent, fDivisions; |
100 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; | 115 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; |
101 }; | 116 }; |
102 | 117 |
103 //////////////////////////////////////////////////////////////////////////////// | 118 //////////////////////////////////////////////////////////////////////////////// |
104 | 119 |
105 SkPatch::SkPatch(SkPoint points[12], SkColor colors[4]) { | 120 SkPatch::SkPatch(SkPoint points[12], SkColor colors[4]) { |
106 | 121 |
107 for (int i = 0; i<12; i++) { | 122 for (int i = 0; i < 12; i++) { |
108 fCtrlPoints[i] = points[i]; | 123 fCtrlPoints[i] = points[i]; |
109 } | 124 } |
| 125 for (int i = 0; i < 4; i++) { |
| 126 fCornerColors[i] = colors[i]; |
| 127 } |
110 | 128 |
111 fCornerColors[0] = SkPreMultiplyColor(colors[0]); | |
112 fCornerColors[1] = SkPreMultiplyColor(colors[1]); | |
113 fCornerColors[2] = SkPreMultiplyColor(colors[2]); | |
114 fCornerColors[3] = SkPreMultiplyColor(colors[3]); | |
115 } | 129 } |
116 | 130 |
117 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar
c01, SkScalar c11) { | 131 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar
c01, SkScalar c11) { |
118 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 132 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
119 SkScalar b = c01 * (1.f - tx) + c11 * tx; | 133 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
120 return uint8_t(a * (1.f - ty) + b * ty); | 134 return uint8_t(a * (1.f - ty) + b * ty); |
121 } | 135 } |
122 | 136 |
123 bool SkPatch::getVertexData(SkPatch::VertexData* data, int divisions) { | 137 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ |
124 | 138 |
125 if (divisions < 1) { | 139 if (lodX < 1 || lodY < 1) { |
126 return false; | 140 return false; |
127 } | 141 } |
128 | 142 |
129 int divX = divisions, divY = divisions; | 143 // premultiply colors to avoid color bleeding. |
| 144 SkPMColor colors[4]; |
| 145 for (int i = 0; i < 4; i++) { |
| 146 colors[i] = SkPreMultiplyColor(fCornerColors[i]); |
| 147 } |
130 | 148 |
131 data->fVertexCount = (divX + 1) * (divY + 1); | 149 // number of indices is limited by size of uint16_t, so we clamp it to avoid
overflow |
132 data->fIndexCount = divX * divY * 6; | 150 data->fVertexCount = SkMin32((lodX + 1) * (lodY + 1), 65536); |
| 151 lodX = SkMin32(lodX, 255); |
| 152 lodY = SkMin32(lodY, 255); |
| 153 data->fIndexCount = lodX * lodY * 6; |
133 | 154 |
134 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 155 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
135 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); | 156 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); |
136 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 157 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
137 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); | 158 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); |
138 | 159 |
139 FwDCubicEvaluator fBottom(fCtrlPoints[kBottomP0_CubicCtrlPts], | 160 SkPoint pts[4]; |
140 fCtrlPoints[kBottomP1_CubicCtrlPts], | 161 this->getBottomPoints(pts); |
141 fCtrlPoints[kBottomP2_CubicCtrlPts], | 162 FwDCubicEvaluator fBottom(pts); |
142 fCtrlPoints[kBottomP3_CubicCtrlPts]), | 163 this->getTopPoints(pts); |
143 fTop(fCtrlPoints[kTopP0_CubicCtrlPts], | 164 FwDCubicEvaluator fTop(pts); |
144 fCtrlPoints[kTopP1_CubicCtrlPts], | 165 this->getLeftPoints(pts); |
145 fCtrlPoints[kTopP2_CubicCtrlPts], | 166 FwDCubicEvaluator fLeft(pts); |
146 fCtrlPoints[kTopP2_CubicCtrlPts]), | 167 this->getRightPoints(pts); |
147 fLeft(fCtrlPoints[kLeftP0_CubicCtrlPts], | 168 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 | 169 |
156 fBottom.restart(divX); | 170 fBottom.restart(lodX); |
157 fTop.restart(divX); | 171 fTop.restart(lodX); |
158 | 172 |
159 SkScalar u = 0.0f; | 173 SkScalar u = 0.0f; |
160 int stride = divY + 1; | 174 int stride = lodY + 1; |
161 for (int x = 0; x <= divX; x++) { | 175 for (int x = 0; x <= lodX; x++) { |
162 SkPoint bottom = fBottom.next(), top = fTop.next(); | 176 SkPoint bottom = fBottom.next(), top = fTop.next(); |
163 fLeft.restart(divY); | 177 fLeft.restart(lodY); |
164 fRight.restart(divY); | 178 fRight.restart(lodY); |
165 SkScalar v = 0.f; | 179 SkScalar v = 0.f; |
166 for (int y = 0; y <= divY; y++) { | 180 for (int y = 0; y <= lodY; y++) { |
167 int dataIndex = x * (divX + 1) + y; | 181 int dataIndex = x * (lodY + 1) + y; |
168 | 182 |
169 SkPoint left = fLeft.next(), right = fRight.next(); | 183 SkPoint left = fLeft.next(), right = fRight.next(); |
170 | 184 |
171 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), | 185 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), |
172 (1.0f - v) * top.y() + v * bottom.y()); | 186 (1.0f - v) * top.y() + v * bottom.y()); |
173 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), | 187 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), |
174 (1.0f - u) * left.y() + u * right.y()); | 188 (1.0f - u) * left.y() + u * right.y()); |
175 SkPoint s2 = SkPoint::Make( | 189 SkPoint s2 = SkPoint::Make( |
176 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() | 190 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() |
177 + u * fTop.getCtrlPoints()[3].x()) | 191 + u * fTop.getCtrlPoints()[3].x()) |
178 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].x() | 192 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].x() |
179 + u * fBottom.getCtrlPoints()[3].x()), | 193 + u * fBottom.getCtrlPoints()[3].x()), |
180 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() | 194 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() |
181 + u * fTop.getCtrlPoints()[3].y()) | 195 + u * fTop.getCtrlPoints()[3].y()) |
182 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].y() | 196 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].y() |
183 + u * fBottom.getCtrlPoints()[3].y())); | 197 + u * fBottom.getCtrlPoints()[3].y())); |
184 data->fPoints[dataIndex] = s0 + s1 - s2; | 198 data->fPoints[dataIndex] = s0 + s1 - s2; |
185 | 199 |
186 uint8_t a = bilinear(u, v, | 200 uint8_t a = bilinear(u, v, |
187 SkScalar(SkColorGetA(fCornerColors[0])), | 201 SkScalar(SkColorGetA(colors[kTopLeft_CornerColors]))
, |
188 SkScalar(SkColorGetA(fCornerColors[1])), | 202 SkScalar(SkColorGetA(colors[kTopRight_CornerColors])
), |
189 SkScalar(SkColorGetA(fCornerColors[2])), | 203 SkScalar(SkColorGetA(colors[kBottomLeft_CornerColors
])), |
190 SkScalar(SkColorGetA(fCornerColors[3]))); | 204 SkScalar(SkColorGetA(colors[kBottomRight_CornerColor
s]))); |
191 uint8_t r = bilinear(u, v, | 205 uint8_t r = bilinear(u, v, |
192 SkScalar(SkColorGetR(fCornerColors[0])), | 206 SkScalar(SkColorGetR(colors[kTopLeft_CornerColors]))
, |
193 SkScalar(SkColorGetR(fCornerColors[1])), | 207 SkScalar(SkColorGetR(colors[kTopRight_CornerColors])
), |
194 SkScalar(SkColorGetR(fCornerColors[2])), | 208 SkScalar(SkColorGetR(colors[kBottomLeft_CornerColors
])), |
195 SkScalar(SkColorGetR(fCornerColors[3]))); | 209 SkScalar(SkColorGetR(colors[kBottomRight_CornerColor
s]))); |
196 uint8_t g = bilinear(u, v, | 210 uint8_t g = bilinear(u, v, |
197 SkScalar(SkColorGetG(fCornerColors[0])), | 211 SkScalar(SkColorGetG(colors[kTopLeft_CornerColors]))
, |
198 SkScalar(SkColorGetG(fCornerColors[1])), | 212 SkScalar(SkColorGetG(colors[kTopRight_CornerColors])
), |
199 SkScalar(SkColorGetG(fCornerColors[2])), | 213 SkScalar(SkColorGetG(colors[kBottomLeft_CornerColors
])), |
200 SkScalar(SkColorGetG(fCornerColors[3]))); | 214 SkScalar(SkColorGetG(colors[kBottomRight_CornerColor
s]))); |
201 uint8_t b = bilinear(u, v, | 215 uint8_t b = bilinear(u, v, |
202 SkScalar(SkColorGetB(fCornerColors[0])), | 216 SkScalar(SkColorGetB(colors[kTopLeft_CornerColors]))
, |
203 SkScalar(SkColorGetB(fCornerColors[1])), | 217 SkScalar(SkColorGetB(colors[kTopRight_CornerColors])
), |
204 SkScalar(SkColorGetB(fCornerColors[2])), | 218 SkScalar(SkColorGetB(colors[kBottomLeft_CornerColors
])), |
205 SkScalar(SkColorGetB(fCornerColors[3]))); | 219 SkScalar(SkColorGetB(colors[kBottomRight_CornerColor
s]))); |
206 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); | 220 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); |
207 | 221 |
208 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); | 222 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); |
209 | 223 |
210 if(x < divX && y < divY) { | 224 if(x < lodX && y < lodY) { |
211 int i = 6 * (x * divY + y); | 225 int i = 6 * (x * lodY + y); |
212 data->fIndices[i] = x * stride + y; | 226 data->fIndices[i] = x * stride + y; |
213 data->fIndices[i + 1] = x * stride + 1 + y; | 227 data->fIndices[i + 1] = x * stride + 1 + y; |
214 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; | 228 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; |
215 data->fIndices[i + 3] = data->fIndices[i]; | 229 data->fIndices[i + 3] = data->fIndices[i]; |
216 data->fIndices[i + 4] = data->fIndices[i + 2]; | 230 data->fIndices[i + 4] = data->fIndices[i + 2]; |
217 data->fIndices[i + 5] = (x + 1) * stride + y; | 231 data->fIndices[i + 5] = (x + 1) * stride + y; |
218 } | 232 } |
219 v += 1.f / divY; | 233 v = SkScalarClampMax(v + 1.f / lodY, 1); |
220 } | 234 } |
221 u += 1.f / divX; | 235 u = SkScalarClampMax(u + 1.f / lodX, 1); |
222 } | 236 } |
223 return true; | 237 return true; |
224 } | 238 } |
OLD | NEW |