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" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
| 12 #include "SkBuffer.h" |
12 | 13 |
13 //////////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////////// |
14 | 15 |
15 /** | 16 /** |
16 * Evaluator to sample the values of a cubic bezier using forward differences. | 17 * Evaluator to sample the values of a cubic bezier using forward differences. |
17 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only | 18 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only |
18 * adding precalculated values. | 19 * adding precalculated values. |
19 * For a linear example we have the function f(t) = m*t+b, then the value of tha
t function at t+h | 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 * 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 * 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 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After | 22 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 return fPoints; | 111 return fPoints; |
111 } | 112 } |
112 | 113 |
113 private: | 114 private: |
114 int fMax, fCurrent, fDivisions; | 115 int fMax, fCurrent, fDivisions; |
115 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; | 116 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; |
116 }; | 117 }; |
117 | 118 |
118 //////////////////////////////////////////////////////////////////////////////// | 119 //////////////////////////////////////////////////////////////////////////////// |
119 | 120 |
120 SkPatch::SkPatch(SkPoint points[12], SkColor colors[4]) { | 121 SkPatch::SkPatch(const SkPoint points[12], const SkColor colors[4]) { |
121 | 122 this->reset(points, colors); |
122 for (int i = 0; i < 12; i++) { | |
123 fCtrlPoints[i] = points[i]; | |
124 } | |
125 for (int i = 0; i < 4; i++) { | |
126 fCornerColors[i] = colors[i]; | |
127 } | |
128 | |
129 } | 123 } |
130 | 124 |
131 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar
c01, SkScalar c11) { | 125 static uint8_t bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkSc
alar c01, |
| 126 SkScalar c11) { |
132 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 127 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
133 SkScalar b = c01 * (1.f - tx) + c11 * tx; | 128 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
134 return uint8_t(a * (1.f - ty) + b * ty); | 129 return uint8_t(a * (1.f - ty) + b * ty); |
135 } | 130 } |
136 | 131 |
137 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ | 132 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ |
138 | 133 |
139 if (lodX < 1 || lodY < 1) { | 134 if (lodX < 1 || lodY < 1) { |
140 return false; | 135 return false; |
141 } | 136 } |
142 | 137 |
143 // premultiply colors to avoid color bleeding. | 138 // premultiply colors to avoid color bleeding. |
144 SkPMColor colors[4]; | 139 SkPMColor colors[SkPatch::kNumColors]; |
145 for (int i = 0; i < 4; i++) { | 140 for (int i = 0; i < SkPatch::kNumColors; i++) { |
146 colors[i] = SkPreMultiplyColor(fCornerColors[i]); | 141 colors[i] = SkPreMultiplyColor(fCornerColors[i]); |
147 } | 142 } |
148 | 143 |
149 // number of indices is limited by size of uint16_t, so we clamp it to avoid
overflow | 144 // number of indices is limited by size of uint16_t, so we clamp it to avoid
overflow |
150 data->fVertexCount = SkMin32((lodX + 1) * (lodY + 1), 65536); | 145 data->fVertexCount = SkMin32((lodX + 1) * (lodY + 1), 65536); |
151 lodX = SkMin32(lodX, 255); | 146 lodX = SkMin32(lodX, 255); |
152 lodY = SkMin32(lodY, 255); | 147 lodY = SkMin32(lodY, 255); |
153 data->fIndexCount = lodX * lodY * 6; | 148 data->fIndexCount = lodX * lodY * 6; |
154 | 149 |
155 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 150 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
156 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); | 151 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); |
157 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 152 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
158 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); | 153 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); |
159 | 154 |
160 SkPoint pts[4]; | 155 SkPoint pts[SkPatch::kNumPtsCubic]; |
161 this->getBottomPoints(pts); | 156 this->getBottomPoints(pts); |
162 FwDCubicEvaluator fBottom(pts); | 157 FwDCubicEvaluator fBottom(pts); |
163 this->getTopPoints(pts); | 158 this->getTopPoints(pts); |
164 FwDCubicEvaluator fTop(pts); | 159 FwDCubicEvaluator fTop(pts); |
165 this->getLeftPoints(pts); | 160 this->getLeftPoints(pts); |
166 FwDCubicEvaluator fLeft(pts); | 161 FwDCubicEvaluator fLeft(pts); |
167 this->getRightPoints(pts); | 162 this->getRightPoints(pts); |
168 FwDCubicEvaluator fRight(pts); | 163 FwDCubicEvaluator fRight(pts); |
169 | 164 |
170 fBottom.restart(lodX); | 165 fBottom.restart(lodX); |
(...skipping 19 matching lines...) Expand all Loading... |
190 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() | 185 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() |
191 + u * fTop.getCtrlPoints()[3].x()) | 186 + u * fTop.getCtrlPoints()[3].x()) |
192 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].x() | 187 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].x() |
193 + u * fBottom.getCtrlPoints()[3].x()), | 188 + u * fBottom.getCtrlPoints()[3].x()), |
194 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() | 189 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() |
195 + u * fTop.getCtrlPoints()[3].y()) | 190 + u * fTop.getCtrlPoints()[3].y()) |
196 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].y() | 191 + v * ((1.0f - u) * fBottom.getCtrlPoint
s()[0].y() |
197 + u * fBottom.getCtrlPoints()[3].y())); | 192 + u * fBottom.getCtrlPoints()[3].y())); |
198 data->fPoints[dataIndex] = s0 + s1 - s2; | 193 data->fPoints[dataIndex] = s0 + s1 - s2; |
199 | 194 |
200 uint8_t a = bilinear(u, v, | 195 uint8_t a = bilerp(u, v, |
201 SkScalar(SkColorGetA(colors[kTopLeft_CornerColors]))
, | 196 SkScalar(SkColorGetA(colors[kTopLeft_CornerColors]))
, |
202 SkScalar(SkColorGetA(colors[kTopRight_CornerColors])
), | 197 SkScalar(SkColorGetA(colors[kTopRight_CornerColors])
), |
203 SkScalar(SkColorGetA(colors[kBottomLeft_CornerColors
])), | 198 SkScalar(SkColorGetA(colors[kBottomLeft_CornerColors
])), |
204 SkScalar(SkColorGetA(colors[kBottomRight_CornerColor
s]))); | 199 SkScalar(SkColorGetA(colors[kBottomRight_CornerColor
s]))); |
205 uint8_t r = bilinear(u, v, | 200 uint8_t r = bilerp(u, v, |
206 SkScalar(SkColorGetR(colors[kTopLeft_CornerColors]))
, | 201 SkScalar(SkColorGetR(colors[kTopLeft_CornerColors]))
, |
207 SkScalar(SkColorGetR(colors[kTopRight_CornerColors])
), | 202 SkScalar(SkColorGetR(colors[kTopRight_CornerColors])
), |
208 SkScalar(SkColorGetR(colors[kBottomLeft_CornerColors
])), | 203 SkScalar(SkColorGetR(colors[kBottomLeft_CornerColors
])), |
209 SkScalar(SkColorGetR(colors[kBottomRight_CornerColor
s]))); | 204 SkScalar(SkColorGetR(colors[kBottomRight_CornerColor
s]))); |
210 uint8_t g = bilinear(u, v, | 205 uint8_t g = bilerp(u, v, |
211 SkScalar(SkColorGetG(colors[kTopLeft_CornerColors]))
, | 206 SkScalar(SkColorGetG(colors[kTopLeft_CornerColors]))
, |
212 SkScalar(SkColorGetG(colors[kTopRight_CornerColors])
), | 207 SkScalar(SkColorGetG(colors[kTopRight_CornerColors])
), |
213 SkScalar(SkColorGetG(colors[kBottomLeft_CornerColors
])), | 208 SkScalar(SkColorGetG(colors[kBottomLeft_CornerColors
])), |
214 SkScalar(SkColorGetG(colors[kBottomRight_CornerColor
s]))); | 209 SkScalar(SkColorGetG(colors[kBottomRight_CornerColor
s]))); |
215 uint8_t b = bilinear(u, v, | 210 uint8_t b = bilerp(u, v, |
216 SkScalar(SkColorGetB(colors[kTopLeft_CornerColors]))
, | 211 SkScalar(SkColorGetB(colors[kTopLeft_CornerColors]))
, |
217 SkScalar(SkColorGetB(colors[kTopRight_CornerColors])
), | 212 SkScalar(SkColorGetB(colors[kTopRight_CornerColors])
), |
218 SkScalar(SkColorGetB(colors[kBottomLeft_CornerColors
])), | 213 SkScalar(SkColorGetB(colors[kBottomLeft_CornerColors
])), |
219 SkScalar(SkColorGetB(colors[kBottomRight_CornerColor
s]))); | 214 SkScalar(SkColorGetB(colors[kBottomRight_CornerColor
s]))); |
220 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); | 215 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); |
221 | 216 |
222 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); | 217 data->fTexCoords[dataIndex] = SkPoint::Make(u, v); |
223 | 218 |
224 if(x < lodX && y < lodY) { | 219 if(x < lodX && y < lodY) { |
225 int i = 6 * (x * lodY + y); | 220 int i = 6 * (x * lodY + y); |
226 data->fIndices[i] = x * stride + y; | 221 data->fIndices[i] = x * stride + y; |
227 data->fIndices[i + 1] = x * stride + 1 + y; | 222 data->fIndices[i + 1] = x * stride + 1 + y; |
228 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; | 223 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; |
229 data->fIndices[i + 3] = data->fIndices[i]; | 224 data->fIndices[i + 3] = data->fIndices[i]; |
230 data->fIndices[i + 4] = data->fIndices[i + 2]; | 225 data->fIndices[i + 4] = data->fIndices[i + 2]; |
231 data->fIndices[i + 5] = (x + 1) * stride + y; | 226 data->fIndices[i + 5] = (x + 1) * stride + y; |
232 } | 227 } |
233 v = SkScalarClampMax(v + 1.f / lodY, 1); | 228 v = SkScalarClampMax(v + 1.f / lodY, 1); |
234 } | 229 } |
235 u = SkScalarClampMax(u + 1.f / lodX, 1); | 230 u = SkScalarClampMax(u + 1.f / lodX, 1); |
236 } | 231 } |
237 return true; | 232 return true; |
238 } | 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 |