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 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11) { |
132 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 126 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
133 SkScalar b = c01 * (1.f - tx) + c11 * tx; | 127 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
134 return uint8_t(a * (1.f - ty) + b * ty); | 128 return uint8_t(a * (1.f - ty) + b * ty); |
135 } | 129 } |
136 | 130 |
137 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const { | 131 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const { |
138 | 132 |
139 if (lodX < 1 || lodY < 1) { | 133 if (lodX < 1 || lodY < 1) { |
140 return false; | 134 return false; |
141 } | 135 } |
142 | 136 |
143 // premultiply colors to avoid color bleeding. | 137 // premultiply colors to avoid color bleeding. |
144 SkPMColor colors[4]; | 138 SkPMColor colors[SkPatch::kNumColors]; |
145 for (int i = 0; i < 4; i++) { | 139 for (int i = 0; i < SkPatch::kNumColors; i++) { |
146 colors[i] = SkPreMultiplyColor(fCornerColors[i]); | 140 colors[i] = SkPreMultiplyColor(fCornerColors[i]); |
147 } | 141 } |
148 | 142 |
149 // number of indices is limited by size of uint16_t, so we clamp it to avoid overflow | 143 // 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); | 144 data->fVertexCount = SkMin32((lodX + 1) * (lodY + 1), 65536); |
151 lodX = SkMin32(lodX, 255); | 145 lodX = SkMin32(lodX, 255); |
152 lodY = SkMin32(lodY, 255); | 146 lodY = SkMin32(lodY, 255); |
153 data->fIndexCount = lodX * lodY * 6; | 147 data->fIndexCount = lodX * lodY * 6; |
154 | 148 |
155 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 149 data->fPoints = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
156 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); | 150 data->fColors = SkNEW_ARRAY(uint32_t, data->fVertexCount); |
157 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); | 151 data->fTexCoords = SkNEW_ARRAY(SkPoint, data->fVertexCount); |
158 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); | 152 data->fIndices = SkNEW_ARRAY(uint16_t, data->fIndexCount); |
159 | 153 |
160 SkPoint pts[4]; | 154 SkPoint pts[SkPatch::kNumPtsCubic]; |
161 this->getBottomPoints(pts); | 155 this->getBottomPoints(pts); |
162 FwDCubicEvaluator fBottom(pts); | 156 FwDCubicEvaluator fBottom(pts); |
163 this->getTopPoints(pts); | 157 this->getTopPoints(pts); |
164 FwDCubicEvaluator fTop(pts); | 158 FwDCubicEvaluator fTop(pts); |
165 this->getLeftPoints(pts); | 159 this->getLeftPoints(pts); |
166 FwDCubicEvaluator fLeft(pts); | 160 FwDCubicEvaluator fLeft(pts); |
167 this->getRightPoints(pts); | 161 this->getRightPoints(pts); |
168 FwDCubicEvaluator fRight(pts); | 162 FwDCubicEvaluator fRight(pts); |
169 | 163 |
170 fBottom.restart(lodX); | 164 fBottom.restart(lodX); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 data->fIndices[i + 3] = data->fIndices[i]; | 223 data->fIndices[i + 3] = data->fIndices[i]; |
230 data->fIndices[i + 4] = data->fIndices[i + 2]; | 224 data->fIndices[i + 4] = data->fIndices[i + 2]; |
231 data->fIndices[i + 5] = (x + 1) * stride + y; | 225 data->fIndices[i + 5] = (x + 1) * stride + y; |
232 } | 226 } |
233 v = SkScalarClampMax(v + 1.f / lodY, 1); | 227 v = SkScalarClampMax(v + 1.f / lodY, 1); |
234 } | 228 } |
235 u = SkScalarClampMax(u + 1.f / lodX, 1); | 229 u = SkScalarClampMax(u + 1.f / lodX, 1); |
236 } | 230 } |
237 return true; | 231 return true; |
238 } | 232 } |
233 | |
234 size_t SkPatch::writeToMemory(void* storage) const { | |
235 int byteCount = kNumCtrlPts * sizeof(SkPoint) + kNumColors * sizeof(SkColor ); | |
236 | |
237 if (NULL == storage) { | |
238 return SkAlign4(byteCount); | |
239 } | |
240 | |
241 SkWBuffer buffer(storage); | |
242 | |
243 buffer.write(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint)); | |
244 buffer.write(fCornerColors, kNumColors * sizeof(SkColor)); | |
245 | |
246 buffer.padToAlign4(); | |
247 return buffer.pos(); | |
248 } | |
249 | |
250 size_t SkPatch::readFromMemory(const void* storage, size_t length) { | |
251 SkRBufferWithSizeCheck buffer(storage, length); | |
252 | |
253 int byteCount = 0; | |
254 | |
255 if (!buffer.read(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint))) { | |
robertphillips
2014/08/05 15:42:00
By the header comment - aren't we supposed to retu
dandov
2014/08/05 17:34:05
Done.
| |
256 return byteCount; | |
257 } | |
258 byteCount += kNumCtrlPts * sizeof(SkPoint); | |
259 | |
260 if (!buffer.read(fCornerColors, kNumColors * sizeof(SkColor))) { | |
robertphillips
2014/08/05 15:42:00
And here ?
dandov
2014/08/05 17:34:05
Done.
| |
261 return byteCount; | |
262 } | |
263 byteCount += kNumColors * sizeof(SkColor); | |
264 | |
265 return byteCount; | |
266 } | |
OLD | NEW |