1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkTwoPointConicalGradient.h"
9 #include "SkTwoPointConicalGradient_gpu.h"
10
11 struct TwoPtRadialContext {
12 const TwoPtRadial& fRec;
13 float fRelX, fRelY;
14 const float fIncX, fIncY;
15 float fB;
16 const float fDB;
17
18 TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy,
19 SkScalar dfx, SkScalar dfy);
20 SkFixed nextT();
21 };
22
valid_divide(float numer,float denom,float * ratio)23 static int valid_divide(float numer, float denom, float* ratio) {
24 SkASSERT(ratio);
25 if (0 == denom) {
26 return 0;
27 }
28 *ratio = numer / denom;
29 return 1;
30 }
31
32 // Return the number of distinct real roots, and write them into roots[] in
33 // ascending order
find_quad_roots(float A,float B,float C,float roots[2],bool descendingOrder=false)34 static int find_quad_roots(float A, float B, float C, float roots[2], bool descendingOrder = false) {
35 SkASSERT(roots);
36
37 if (A == 0) {
38 return valid_divide(-C, B, roots);
39 }
40
41 float R = B*B - 4*A*C;
42 if (R < 0) {
43 return 0;
44 }
45 R = sk_float_sqrt(R);
46
47 #if 1
48 float Q = B;
49 if (Q < 0) {
50 Q -= R;
51 } else {
52 Q += R;
53 }
54 #else
55 // on 10.6 this was much slower than the above branch :(
56 float Q = B + copysignf(R, B);
57 #endif
58 Q *= -0.5f;
59 if (0 == Q) {
60 roots[0] = 0;
61 return 1;
62 }
63
64 float r0 = Q / A;
65 float r1 = C / Q;
66 roots[0] = r0 < r1 ? r0 : r1;
67 roots[1] = r0 > r1 ? r0 : r1;
68 if (descendingOrder) {
69 SkTSwap(roots[0], roots[1]);
70 }
71 return 2;
72 }
73
lerp(float x,float dx,float t)74 static float lerp(float x, float dx, float t) {
75 return x + t * dx;
76 }
77
sqr(float x)78 static float sqr(float x) { return x * x; }
79
init(const SkPoint & center0,SkScalar rad0,const SkPoint & center1,SkScalar rad1,bool flipped)80 void TwoPtRadial::init(const SkPoint& center0, SkScalar rad0,
81 const SkPoint& center1, SkScalar rad1,
82 bool flipped) {
83 fCenterX = SkScalarToFloat(center0.fX);
84 fCenterY = SkScalarToFloat(center0.fY);
85 fDCenterX = SkScalarToFloat(center1.fX) - fCenterX;
86 fDCenterY = SkScalarToFloat(center1.fY) - fCenterY;
87 fRadius = SkScalarToFloat(rad0);
88 fDRadius = SkScalarToFloat(rad1) - fRadius;
89
90 fA = sqr(fDCenterX) + sqr(fDCenterY) - sqr(fDRadius);
91 fRadius2 = sqr(fRadius);
92 fRDR = fRadius * fDRadius;
93
94 fFlipped = flipped;
95 }
96
TwoPtRadialContext(const TwoPtRadial & rec,SkScalar fx,SkScalar fy,SkScalar dfx,SkScalar dfy)97 TwoPtRadialContext::TwoPtRadialContext(const TwoPtRadial& rec, SkScalar fx, SkScalar fy,
98 SkScalar dfx, SkScalar dfy)
99 : fRec(rec)
100 , fRelX(SkScalarToFloat(fx) - rec.fCenterX)
101 , fRelY(SkScalarToFloat(fy) - rec.fCenterY)
102 , fIncX(SkScalarToFloat(dfx))
103 , fIncY(SkScalarToFloat(dfy))
104 , fB(-2 * (rec.fDCenterX * fRelX + rec.fDCenterY * fRelY + rec.fRDR))
105 , fDB(-2 * (rec.fDCenterX * fIncX + rec.fDCenterY * fIncY)) {}
106
nextT()107 SkFixed TwoPtRadialContext::nextT() {
108 float roots[2];
109
110 float C = sqr(fRelX) + sqr(fRelY) - fRec.fRadius2;
111 int countRoots = find_quad_roots(fRec.fA, fB, C, roots, fRec.fFlipped);
112
113 fRelX += fIncX;
114 fRelY += fIncY;
115 fB += fDB;
116
117 if (0 == countRoots) {
118 return TwoPtRadial::kDontDrawT;
119 }
120
121 // Prefer the bigger t value if both give a radius(t) > 0
122 // find_quad_roots returns the values sorted, so we start with the last
123 float t = roots[countRoots - 1];
124 float r = lerp(fRec.fRadius, fRec.fDRadius, t);
125 if (r <= 0) {
126 t = roots[0]; // might be the same as roots[countRoots-1]
127 r = lerp(fRec.fRadius, fRec.fDRadius, t);
128 if (r <= 0) {
129 return TwoPtRadial::kDontDrawT;
130 }
131 }
132 return SkFloatToFixed(t);
133 }
134
135 typedef void (*TwoPointConicalProc)(TwoPtRadialContext* rec, SkPMColor* dstC,
136 const SkPMColor* cache, int toggle, int count);
137
twopoint_clamp(TwoPtRadialContext * rec,SkPMColor * SK_RESTRICT dstC,const SkPMColor * SK_RESTRICT cache,int toggle,int count)138 static void twopoint_clamp(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC,
139 const SkPMColor* SK_RESTRICT cache, int toggle,
140 int count) {
141 for (; count > 0; --count) {
142 SkFixed t = rec->nextT();
143 if (TwoPtRadial::DontDrawT(t)) {
144 *dstC++ = 0;
145 } else {
146 SkFixed index = SkClampMax(t, 0xFFFF);
147 SkASSERT(index <= 0xFFFF);
148 *dstC++ = cache[toggle +
149 (index >> SkGradientShaderBase::kCache32Shift)];
150 }
151 toggle = next_dither_toggle(toggle);
152 }
153 }
154
twopoint_repeat(TwoPtRadialContext * rec,SkPMColor * SK_RESTRICT dstC,const SkPMColor * SK_RESTRICT cache,int toggle,int count)155 static void twopoint_repeat(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC,
156 const SkPMColor* SK_RESTRICT cache, int toggle,
157 int count) {
158 for (; count > 0; --count) {
159 SkFixed t = rec->nextT();
160 if (TwoPtRadial::DontDrawT(t)) {
161 *dstC++ = 0;
162 } else {
163 SkFixed index = repeat_tileproc(t);
164 SkASSERT(index <= 0xFFFF);
165 *dstC++ = cache[toggle +
166 (index >> SkGradientShaderBase::kCache32Shift)];
167 }
168 toggle = next_dither_toggle(toggle);
169 }
170 }
171
twopoint_mirror(TwoPtRadialContext * rec,SkPMColor * SK_RESTRICT dstC,const SkPMColor * SK_RESTRICT cache,int toggle,int count)172 static void twopoint_mirror(TwoPtRadialContext* rec, SkPMColor* SK_RESTRICT dstC,
173 const SkPMColor* SK_RESTRICT cache, int toggle,
174 int count) {
175 for (; count > 0; --count) {
176 SkFixed t = rec->nextT();
177 if (TwoPtRadial::DontDrawT(t)) {
178 *dstC++ = 0;
179 } else {
180 SkFixed index = mirror_tileproc(t);
181 SkASSERT(index <= 0xFFFF);
182 *dstC++ = cache[toggle +
183 (index >> SkGradientShaderBase::kCache32Shift)];
184 }
185 toggle = next_dither_toggle(toggle);
186 }
187 }
188
189 /////////////////////////////////////////////////////////////////////
190
SkTwoPointConicalGradient(const SkPoint & start,SkScalar startRadius,const SkPoint & end,SkScalar endRadius,bool flippedGrad,const Descriptor & desc)191 SkTwoPointConicalGradient::SkTwoPointConicalGradient(
192 const SkPoint& start, SkScalar startRadius,
193 const SkPoint& end, SkScalar endRadius,
194 bool flippedGrad, const Descriptor& desc)
195 : SkGradientShaderBase(desc, SkMatrix::I())
196 , fCenter1(start)
197 , fCenter2(end)
198 , fRadius1(startRadius)
199 , fRadius2(endRadius)
200 , fFlippedGrad(flippedGrad)
201 {
202 // this is degenerate, and should be caught by our caller
203 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
204 fRec.init(fCenter1, fRadius1, fCenter2, fRadius2, fFlippedGrad);
205 }
206
isOpaque() const207 bool SkTwoPointConicalGradient::isOpaque() const {
208 // Because areas outside the cone are left untouched, we cannot treat the
209 // shader as opaque even if the gradient itself is opaque.
210 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
211 return false;
212 }
213
contextSize(const ContextRec &) const214 size_t SkTwoPointConicalGradient::contextSize(const ContextRec&) const {
215 return sizeof(TwoPointConicalGradientContext);
216 }
217
onCreateContext(const ContextRec & rec,void * storage) const218 SkShader::Context* SkTwoPointConicalGradient::onCreateContext(const ContextRec& rec,
219 void* storage) const {
220 return new (storage) TwoPointConicalGradientContext(*this, rec);
221 }
222
TwoPointConicalGradientContext(const SkTwoPointConicalGradient & shader,const ContextRec & rec)223 SkTwoPointConicalGradient::TwoPointConicalGradientContext::TwoPointConicalGradientContext(
224 const SkTwoPointConicalGradient& shader, const ContextRec& rec)
225 : INHERITED(shader, rec)
226 {
227 // in general, we might discard based on computed-radius, so clear
228 // this flag (todo: sometimes we can detect that we never discard...)
229 fFlags &= ~kOpaqueAlpha_Flag;
230 }
231
shadeSpan(int x,int y,SkPMColor * dstCParam,int count)232 void SkTwoPointConicalGradient::TwoPointConicalGradientContext::shadeSpan(
233 int x, int y, SkPMColor* dstCParam, int count) {
234 const SkTwoPointConicalGradient& twoPointConicalGradient =
235 static_cast<const SkTwoPointConicalGradient&>(fShader);
236
237 int toggle = init_dither_toggle(x, y);
238
239 SkASSERT(count > 0);
240
241 SkPMColor* SK_RESTRICT dstC = dstCParam;
242
243 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
244
245 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
246
247 TwoPointConicalProc shadeProc = twopoint_repeat;
248 if (SkShader::kClamp_TileMode == twoPointConicalGradient.fTileMode) {
249 shadeProc = twopoint_clamp;
250 } else if (SkShader::kMirror_TileMode == twoPointConicalGradient.fTileMode) {
251 shadeProc = twopoint_mirror;
252 } else {
253 SkASSERT(SkShader::kRepeat_TileMode == twoPointConicalGradient.fTileMode);
254 }
255
256 if (fDstToIndexClass != kPerspective_MatrixClass) {
257 SkPoint srcPt;
258 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
259 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
260 SkScalar dx, fx = srcPt.fX;
261 SkScalar dy, fy = srcPt.fY;
262
263 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
264 const auto step = fDstToIndex.fixedStepInX(SkIntToScalar(y));
265 dx = step.fX;
266 dy = step.fY;
267 } else {
268 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
269 dx = fDstToIndex.getScaleX();
270 dy = fDstToIndex.getSkewY();
271 }
272
273 TwoPtRadialContext rec(twoPointConicalGradient.fRec, fx, fy, dx, dy);
274 (*shadeProc)(&rec, dstC, cache, toggle, count);
275 } else { // perspective case
276 SkScalar dstX = SkIntToScalar(x) + SK_ScalarHalf;
277 SkScalar dstY = SkIntToScalar(y) + SK_ScalarHalf;
278 for (; count > 0; --count) {
279 SkPoint srcPt;
280 dstProc(fDstToIndex, dstX, dstY, &srcPt);
281 TwoPtRadialContext rec(twoPointConicalGradient.fRec, srcPt.fX, srcPt.fY, 0, 0);
282 (*shadeProc)(&rec, dstC, cache, toggle, 1);
283
284 dstX += SK_Scalar1;
285 toggle = next_dither_toggle(toggle);
286 dstC += 1;
287 }
288 }
289 }
290
291 // Returns the original non-sorted version of the gradient
asAGradient(GradientInfo * info) const292 SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
293 GradientInfo* info) const {
294 if (info) {
295 commonAsAGradient(info, fFlippedGrad);
296 info->fPoint[0] = fCenter1;
297 info->fPoint[1] = fCenter2;
298 info->fRadius[0] = fRadius1;
299 info->fRadius[1] = fRadius2;
300 if (fFlippedGrad) {
301 SkTSwap(info->fPoint[0], info->fPoint[1]);
302 SkTSwap(info->fRadius[0], info->fRadius[1]);
303 }
304 }
305 return kConical_GradientType;
306 }
307
CreateProc(SkReadBuffer & buffer)308 SkFlattenable* SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) {
309 DescriptorScope desc;
310 if (!desc.unflatten(buffer)) {
311 return nullptr;
312 }
313 SkPoint c1 = buffer.readPoint();
314 SkPoint c2 = buffer.readPoint();
315 SkScalar r1 = buffer.readScalar();
316 SkScalar r2 = buffer.readScalar();
317
318 if (buffer.readBool()) { // flipped
319 SkTSwap(c1, c2);
320 SkTSwap(r1, r2);
321
322 SkColor* colors = desc.mutableColors();
323 SkScalar* pos = desc.mutablePos();
324 const int last = desc.fCount - 1;
325 const int half = desc.fCount >> 1;
326 for (int i = 0; i < half; ++i) {
327 SkTSwap(colors[i], colors[last - i]);
328 if (pos) {
329 SkScalar tmp = pos[i];
330 pos[i] = SK_Scalar1 - pos[last - i];
331 pos[last - i] = SK_Scalar1 - tmp;
332 }
333 }
334 if (pos) {
335 if (desc.fCount & 1) {
336 pos[half] = SK_Scalar1 - pos[half];
337 }
338 }
339 }
340
341 return SkGradientShader::CreateTwoPointConical(c1, r1, c2, r2, desc.fColors, desc.fPos,
342 desc.fCount, desc.fTileMode, desc.fGradFlags,
343 desc.fLocalMatrix);
344 }
345
flatten(SkWriteBuffer & buffer) const346 void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const {
347 this->INHERITED::flatten(buffer);
348 buffer.writePoint(fCenter1);
349 buffer.writePoint(fCenter2);
350 buffer.writeScalar(fRadius1);
351 buffer.writeScalar(fRadius2);
352 buffer.writeBool(fFlippedGrad);
353 }
354
355 #if SK_SUPPORT_GPU
356
357 #include "SkGr.h"
358
asFragmentProcessor(GrContext * context,const SkMatrix & viewM,const SkMatrix * localMatrix,SkFilterQuality) const359 const GrFragmentProcessor* SkTwoPointConicalGradient::asFragmentProcessor(
360 GrContext* context,
361 const SkMatrix& viewM,
362 const SkMatrix* localMatrix,
363 SkFilterQuality) const {
364 SkASSERT(context);
365 SkASSERT(fPtsToUnit.isIdentity());
366 SkAutoTUnref<const GrFragmentProcessor> inner(
367 Gr2PtConicalGradientEffect::Create(context, *this, fTileMode, localMatrix));
368 return GrFragmentProcessor::MulOutputByInputAlpha(inner);
369 }
370
371 #endif
372
373 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const374 void SkTwoPointConicalGradient::toString(SkString* str) const {
375 str->append("SkTwoPointConicalGradient: (");
376
377 str->append("center1: (");
378 str->appendScalar(fCenter1.fX);
379 str->append(", ");
380 str->appendScalar(fCenter1.fY);
381 str->append(") radius1: ");
382 str->appendScalar(fRadius1);
383 str->append(" ");
384
385 str->append("center2: (");
386 str->appendScalar(fCenter2.fX);
387 str->append(", ");
388 str->appendScalar(fCenter2.fY);
389 str->append(") radius2: ");
390 str->appendScalar(fRadius2);
391 str->append(" ");
392
393 this->INHERITED::toString(str);
394
395 str->append(")");
396 }
397 #endif
398