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