OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkTwoPointConicalGradient.h" | 8 #include "SkTwoPointConicalGradient.h" |
9 | 9 |
| 10 struct TwoPtRadialContext { |
| 11 const TwoPtRadial& fRec; |
| 12 float fRelX, fRelY; |
| 13 const float fIncX, fIncY; |
| 14 float fB; |
| 15 const float fDB; |
| 16 |
| 17 TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy, |
| 18 SkScalar dfx, SkScalar dfy); |
| 19 SkFixed nextT(); |
| 20 }; |
| 21 |
10 static int valid_divide(float numer, float denom, float* ratio) { | 22 static int valid_divide(float numer, float denom, float* ratio) { |
11 SkASSERT(ratio); | 23 SkASSERT(ratio); |
12 if (0 == denom) { | 24 if (0 == denom) { |
13 return 0; | 25 return 0; |
14 } | 26 } |
15 *ratio = numer / denom; | 27 *ratio = numer / denom; |
16 return 1; | 28 return 1; |
17 } | 29 } |
18 | 30 |
19 // Return the number of distinct real roots, and write them into roots[] in | 31 // Return the number of distinct real roots, and write them into roots[] in |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 fDCenterX = SkScalarToFloat(center1.fX) - fCenterX; | 80 fDCenterX = SkScalarToFloat(center1.fX) - fCenterX; |
69 fDCenterY = SkScalarToFloat(center1.fY) - fCenterY; | 81 fDCenterY = SkScalarToFloat(center1.fY) - fCenterY; |
70 fRadius = SkScalarToFloat(rad0); | 82 fRadius = SkScalarToFloat(rad0); |
71 fDRadius = SkScalarToFloat(rad1) - fRadius; | 83 fDRadius = SkScalarToFloat(rad1) - fRadius; |
72 | 84 |
73 fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius); | 85 fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius); |
74 fRadius2 = sqr(fRadius); | 86 fRadius2 = sqr(fRadius); |
75 fRDR = fRadius * fDRadius; | 87 fRDR = fRadius * fDRadius; |
76 } | 88 } |
77 | 89 |
78 void TwoPtRadial::setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy) { | 90 TwoPtRadialContext::TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkSc
alar fy, |
79 fRelX = SkScalarToFloat(fx) - fCenterX; | 91 SkScalar dfx, SkScalar dfy) |
80 fRelY = SkScalarToFloat(fy) - fCenterY; | 92 : fRec(rec) |
81 fIncX = SkScalarToFloat(dfx); | 93 , fRelX(SkScalarToFloat(fx) - rec.fCenterX) |
82 fIncY = SkScalarToFloat(dfy); | 94 , fRelY(SkScalarToFloat(fy) - rec.fCenterY) |
83 fB = -2 * (fDCenterX * fRelX + fDCenterY * fRelY + fRDR); | 95 , fIncX(SkScalarToFloat(dfx)) |
84 fDB = -2 * (fDCenterX * fIncX + fDCenterY * fIncY); | 96 , fIncY(SkScalarToFloat(dfy)) |
85 } | 97 , fB(-2 * (rec.fDCenterX * fRelX + rec.fDCenterY * fRelY + rec.fRDR)) |
| 98 , fDB(-2 * (rec.fDCenterX * fIncX + rec.fDCenterY * fIncY)) {} |
86 | 99 |
87 SkFixed TwoPtRadial::nextT() { | 100 SkFixed TwoPtRadialContext::nextT() { |
88 float roots[2]; | 101 float roots[2]; |
89 | 102 |
90 float C = sqr(fRelX) + sqr(fRelY) - fRadius2; | 103 float C = sqr(fRelX) + sqr(fRelY) - fRec.fRadius2; |
91 int countRoots = find_quad_roots(fA, fB, C, roots); | 104 int countRoots = find_quad_roots(fRec.fA, fB, C, roots); |
92 | 105 |
93 fRelX += fIncX; | 106 fRelX += fIncX; |
94 fRelY += fIncY; | 107 fRelY += fIncY; |
95 fB += fDB; | 108 fB += fDB; |
96 | 109 |
97 if (0 == countRoots) { | 110 if (0 == countRoots) { |
98 return kDontDrawT; | 111 return TwoPtRadial::kDontDrawT; |
99 } | 112 } |
100 | 113 |
101 // Prefer the bigger t value if both give a radius(t) > 0 | 114 // Prefer the bigger t value if both give a radius(t) > 0 |
102 // find_quad_roots returns the values sorted, so we start with the last | 115 // find_quad_roots returns the values sorted, so we start with the last |
103 float t = roots[countRoots - 1]; | 116 float t = roots[countRoots - 1]; |
104 float r = lerp(fRadius, fDRadius, t); | 117 float r = lerp(fRec.fRadius, fRec.fDRadius, t); |
105 if (r <= 0) { | 118 if (r <= 0) { |
106 t = roots[0]; // might be the same as roots[countRoots-1] | 119 t = roots[0]; // might be the same as roots[countRoots-1] |
107 r = lerp(fRadius, fDRadius, t); | 120 r = lerp(fRec.fRadius, fRec.fDRadius, t); |
108 if (r <= 0) { | 121 if (r <= 0) { |
109 return kDontDrawT; | 122 return TwoPtRadial::kDontDrawT; |
110 } | 123 } |
111 } | 124 } |
112 return SkFloatToFixed(t); | 125 return SkFloatToFixed(t); |
113 } | 126 } |
114 | 127 |
115 typedef void (*TwoPointConicalProc)(TwoPtRadial* rec, SkPMColor* dstC, | 128 typedef void (*TwoPointConicalProc)(TwoPtRadialContext* rec, SkPMColor* dstC, |
116 const SkPMColor* cache, int toggle, int coun
t); | 129 const SkPMColor* cache, int toggle, int coun
t); |
117 | 130 |
118 static void twopoint_clamp(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, | 131 static void twopoint_clamp(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC, |
119 const SkPMColor* SK_RESTRICT cache, int toggle, | 132 const SkPMColor* SK_RESTRICT cache, int toggle, |
120 int count) { | 133 int count) { |
121 for (; count > 0; --count) { | 134 for (; count > 0; --count) { |
122 SkFixed t = rec->nextT(); | 135 SkFixed t = rec->nextT(); |
123 if (TwoPtRadial::DontDrawT(t)) { | 136 if (TwoPtRadial::DontDrawT(t)) { |
124 *dstC++ = 0; | 137 *dstC++ = 0; |
125 } else { | 138 } else { |
126 SkFixed index = SkClampMax(t, 0xFFFF); | 139 SkFixed index = SkClampMax(t, 0xFFFF); |
127 SkASSERT(index <= 0xFFFF); | 140 SkASSERT(index <= 0xFFFF); |
128 *dstC++ = cache[toggle + | 141 *dstC++ = cache[toggle + |
129 (index >> SkGradientShaderBase::kCache32Shift)]; | 142 (index >> SkGradientShaderBase::kCache32Shift)]; |
130 } | 143 } |
131 toggle = next_dither_toggle(toggle); | 144 toggle = next_dither_toggle(toggle); |
132 } | 145 } |
133 } | 146 } |
134 | 147 |
135 static void twopoint_repeat(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, | 148 static void twopoint_repeat(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC
, |
136 const SkPMColor* SK_RESTRICT cache, int toggle, | 149 const SkPMColor* SK_RESTRICT cache, int toggle, |
137 int count) { | 150 int count) { |
138 for (; count > 0; --count) { | 151 for (; count > 0; --count) { |
139 SkFixed t = rec->nextT(); | 152 SkFixed t = rec->nextT(); |
140 if (TwoPtRadial::DontDrawT(t)) { | 153 if (TwoPtRadial::DontDrawT(t)) { |
141 *dstC++ = 0; | 154 *dstC++ = 0; |
142 } else { | 155 } else { |
143 SkFixed index = repeat_tileproc(t); | 156 SkFixed index = repeat_tileproc(t); |
144 SkASSERT(index <= 0xFFFF); | 157 SkASSERT(index <= 0xFFFF); |
145 *dstC++ = cache[toggle + | 158 *dstC++ = cache[toggle + |
146 (index >> SkGradientShaderBase::kCache32Shift)]; | 159 (index >> SkGradientShaderBase::kCache32Shift)]; |
147 } | 160 } |
148 toggle = next_dither_toggle(toggle); | 161 toggle = next_dither_toggle(toggle); |
149 } | 162 } |
150 } | 163 } |
151 | 164 |
152 static void twopoint_mirror(TwoPtRadial* rec, SkPMColor* SK_RESTRICT dstC, | 165 static void twopoint_mirror(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC
, |
153 const SkPMColor* SK_RESTRICT cache, int toggle, | 166 const SkPMColor* SK_RESTRICT cache, int toggle, |
154 int count) { | 167 int count) { |
155 for (; count > 0; --count) { | 168 for (; count > 0; --count) { |
156 SkFixed t = rec->nextT(); | 169 SkFixed t = rec->nextT(); |
157 if (TwoPtRadial::DontDrawT(t)) { | 170 if (TwoPtRadial::DontDrawT(t)) { |
158 *dstC++ = 0; | 171 *dstC++ = 0; |
159 } else { | 172 } else { |
160 SkFixed index = mirror_tileproc(t); | 173 SkFixed index = mirror_tileproc(t); |
161 SkASSERT(index <= 0xFFFF); | 174 SkASSERT(index <= 0xFFFF); |
162 *dstC++ = cache[toggle + | 175 *dstC++ = cache[toggle + |
(...skipping 24 matching lines...) Expand all Loading... |
187 this->init(); | 200 this->init(); |
188 } | 201 } |
189 | 202 |
190 bool SkTwoPointConicalGradient::isOpaque() const { | 203 bool SkTwoPointConicalGradient::isOpaque() const { |
191 // Because areas outside the cone are left untouched, we cannot treat the | 204 // Because areas outside the cone are left untouched, we cannot treat the |
192 // shader as opaque even if the gradient itself is opaque. | 205 // shader as opaque even if the gradient itself is opaque. |
193 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380 | 206 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380 |
194 return false; | 207 return false; |
195 } | 208 } |
196 | 209 |
197 void SkTwoPointConicalGradient::shadeSpan(int x, int y, SkPMColor* dstCParam, | 210 size_t SkTwoPointConicalGradient::contextSize() const { |
198 int count) { | 211 return sizeof(TwoPointConicalGradientContext); |
| 212 } |
| 213 |
| 214 SkShader::Context* SkTwoPointConicalGradient::createContext( |
| 215 const SkBitmap& device, const SkPaint& paint, |
| 216 const SkMatrix& matrix, void* storage) const { |
| 217 if (!this->validContext(device, paint, matrix)) { |
| 218 return NULL; |
| 219 } |
| 220 |
| 221 return SkNEW_PLACEMENT_ARGS(storage, TwoPointConicalGradientContext, |
| 222 (*this, device, paint, matrix)); |
| 223 } |
| 224 |
| 225 SkTwoPointConicalGradient::TwoPointConicalGradientContext::TwoPointConicalGradie
ntContext( |
| 226 const SkTwoPointConicalGradient& shader, const SkBitmap& device, |
| 227 const SkPaint& paint, const SkMatrix& matrix) |
| 228 : INHERITED(shader, device, paint, matrix) |
| 229 { |
| 230 // we don't have a span16 proc |
| 231 fFlags &= ~kHasSpan16_Flag; |
| 232 |
| 233 // in general, we might discard based on computed-radius, so clear |
| 234 // this flag (todo: sometimes we can detect that we never discard...) |
| 235 fFlags &= ~kOpaqueAlpha_Flag; |
| 236 } |
| 237 |
| 238 void SkTwoPointConicalGradient::TwoPointConicalGradientContext::shadeSpan( |
| 239 int x, int y, SkPMColor* dstCParam, int count) { |
| 240 const SkTwoPointConicalGradient& twoPointConicalGradient = |
| 241 static_cast<const SkTwoPointConicalGradient&>(fShader); |
| 242 |
199 int toggle = init_dither_toggle(x, y); | 243 int toggle = init_dither_toggle(x, y); |
200 | 244 |
201 SkASSERT(count > 0); | 245 SkASSERT(count > 0); |
202 | 246 |
203 SkPMColor* SK_RESTRICT dstC = dstCParam; | 247 SkPMColor* SK_RESTRICT dstC = dstCParam; |
204 | 248 |
205 SkMatrix::MapXYProc dstProc = fDstToIndexProc; | 249 SkMatrix::MapXYProc dstProc = fDstToIndexProc; |
206 | 250 |
207 const SkPMColor* SK_RESTRICT cache = this->getCache32(); | 251 const SkPMColor* SK_RESTRICT cache = fCache.getCache32(twoPointConicalGradie
nt); |
208 | 252 |
209 TwoPointConicalProc shadeProc = twopoint_repeat; | 253 TwoPointConicalProc shadeProc = twopoint_repeat; |
210 if (SkShader::kClamp_TileMode == fTileMode) { | 254 if (SkShader::kClamp_TileMode == twoPointConicalGradient.fTileMode) { |
211 shadeProc = twopoint_clamp; | 255 shadeProc = twopoint_clamp; |
212 } else if (SkShader::kMirror_TileMode == fTileMode) { | 256 } else if (SkShader::kMirror_TileMode == twoPointConicalGradient.fTileMode)
{ |
213 shadeProc = twopoint_mirror; | 257 shadeProc = twopoint_mirror; |
214 } else { | 258 } else { |
215 SkASSERT(SkShader::kRepeat_TileMode == fTileMode); | 259 SkASSERT(SkShader::kRepeat_TileMode == twoPointConicalGradient.fTileMode
); |
216 } | 260 } |
217 | 261 |
218 if (fDstToIndexClass != kPerspective_MatrixClass) { | 262 if (fDstToIndexClass != kPerspective_MatrixClass) { |
219 SkPoint srcPt; | 263 SkPoint srcPt; |
220 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, | 264 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, |
221 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 265 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
222 SkScalar dx, fx = srcPt.fX; | 266 SkScalar dx, fx = srcPt.fX; |
223 SkScalar dy, fy = srcPt.fY; | 267 SkScalar dy, fy = srcPt.fY; |
224 | 268 |
225 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { | 269 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
226 SkFixed fixedX, fixedY; | 270 SkFixed fixedX, fixedY; |
227 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); | 271 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); |
228 dx = SkFixedToScalar(fixedX); | 272 dx = SkFixedToScalar(fixedX); |
229 dy = SkFixedToScalar(fixedY); | 273 dy = SkFixedToScalar(fixedY); |
230 } else { | 274 } else { |
231 SkASSERT(fDstToIndexClass == kLinear_MatrixClass); | 275 SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
232 dx = fDstToIndex.getScaleX(); | 276 dx = fDstToIndex.getScaleX(); |
233 dy = fDstToIndex.getSkewY(); | 277 dy = fDstToIndex.getSkewY(); |
234 } | 278 } |
235 | 279 |
236 fRec.setup(fx, fy, dx, dy); | 280 TwoPtRadialContext rec(twoPointConicalGradient.fRec, fx, fy, dx, dy); |
237 (*shadeProc)(&fRec, dstC, cache, toggle, count); | 281 (*shadeProc)(&rec, dstC, cache, toggle, count); |
238 } else { // perspective case | 282 } else { // perspective case |
239 SkScalar dstX = SkIntToScalar(x) + SK_ScalarHalf; | 283 SkScalar dstX = SkIntToScalar(x) + SK_ScalarHalf; |
240 SkScalar dstY = SkIntToScalar(y) + SK_ScalarHalf; | 284 SkScalar dstY = SkIntToScalar(y) + SK_ScalarHalf; |
241 for (; count > 0; --count) { | 285 for (; count > 0; --count) { |
242 SkPoint srcPt; | 286 SkPoint srcPt; |
243 dstProc(fDstToIndex, dstX, dstY, &srcPt); | 287 dstProc(fDstToIndex, dstX, dstY, &srcPt); |
244 fRec.setup(srcPt.fX, srcPt.fY, 0, 0); | 288 TwoPtRadialContext rec(twoPointConicalGradient.fRec, srcPt.fX, srcPt
.fY, 0, 0); |
245 (*shadeProc)(&fRec, dstC, cache, toggle, 1); | 289 (*shadeProc)(&rec, dstC, cache, toggle, 1); |
246 | 290 |
247 dstX += SK_Scalar1; | 291 dstX += SK_Scalar1; |
248 toggle = next_dither_toggle(toggle); | 292 toggle = next_dither_toggle(toggle); |
249 dstC += 1; | 293 dstC += 1; |
250 } | 294 } |
251 } | 295 } |
252 } | 296 } |
253 | 297 |
254 bool SkTwoPointConicalGradient::setContext(const SkBitmap& device, | |
255 const SkPaint& paint, | |
256 const SkMatrix& matrix) { | |
257 if (!this->INHERITED::setContext(device, paint, matrix)) { | |
258 return false; | |
259 } | |
260 | |
261 // we don't have a span16 proc | |
262 fFlags &= ~kHasSpan16_Flag; | |
263 | |
264 // in general, we might discard based on computed-radius, so clear | |
265 // this flag (todo: sometimes we can detect that we never discard...) | |
266 fFlags &= ~kOpaqueAlpha_Flag; | |
267 | |
268 return true; | |
269 } | |
270 | |
271 SkShader::BitmapType SkTwoPointConicalGradient::asABitmap( | 298 SkShader::BitmapType SkTwoPointConicalGradient::asABitmap( |
272 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const { | 299 SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const { |
273 SkPoint diff = fCenter2 - fCenter1; | 300 SkPoint diff = fCenter2 - fCenter1; |
274 SkScalar diffLen = 0; | 301 SkScalar diffLen = 0; |
275 | 302 |
276 if (bitmap) { | 303 if (bitmap) { |
277 this->getGradientTableBitmap(bitmap); | 304 this->getGradientTableBitmap(bitmap); |
278 } | 305 } |
279 if (matrix) { | 306 if (matrix) { |
280 diffLen = diff.length(); | 307 diffLen = diff.length(); |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 str->appendScalar(fCenter2.fY); | 755 str->appendScalar(fCenter2.fY); |
729 str->append(") radius2: "); | 756 str->append(") radius2: "); |
730 str->appendScalar(fRadius2); | 757 str->appendScalar(fRadius2); |
731 str->append(" "); | 758 str->append(" "); |
732 | 759 |
733 this->INHERITED::toString(str); | 760 this->INHERITED::toString(str); |
734 | 761 |
735 str->append(")"); | 762 str->append(")"); |
736 } | 763 } |
737 #endif | 764 #endif |
OLD | NEW |