blob: 81ebb3441eb13159399ce771d05c0a5e3402953d [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkSweepGradient.h"
10
reed@google.com437d6eb2013-05-23 19:03:05 +000011SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000012 const Descriptor& desc, const SkMatrix* localMatrix)
13 : SkGradientShaderBase(desc, localMatrix)
reed@google.com3d3a8602013-05-24 14:58:44 +000014 , fCenter(SkPoint::Make(cx, cy))
rileya@google.com589708b2012-07-26 20:04:23 +000015{
16 fPtsToUnit.setTranslate(-cx, -cy);
reed@google.com437d6eb2013-05-23 19:03:05 +000017
18 // overwrite the tilemode to a canonical value (since sweep ignores it)
19 fTileMode = SkShader::kClamp_TileMode;
rileya@google.com589708b2012-07-26 20:04:23 +000020}
21
22SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap,
23 SkMatrix* matrix, SkShader::TileMode* xy) const {
24 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000025 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +000026 }
27 if (matrix) {
28 *matrix = fPtsToUnit;
29 }
30 if (xy) {
31 xy[0] = fTileMode;
32 xy[1] = kClamp_TileMode;
33 }
34 return kSweep_BitmapType;
35}
36
37SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const {
38 if (info) {
39 commonAsAGradient(info);
40 info->fPoint[0] = fCenter;
41 }
42 return kSweep_GradientType;
43}
44
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000045SkSweepGradient::SkSweepGradient(SkReadBuffer& buffer)
rileya@google.com589708b2012-07-26 20:04:23 +000046 : INHERITED(buffer),
47 fCenter(buffer.readPoint()) {
48}
49
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000050void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +000051 this->INHERITED::flatten(buffer);
52 buffer.writePoint(fCenter);
53}
54
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000055size_t SkSweepGradient::contextSize() const {
56 return sizeof(SweepGradientContext);
57}
58
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000059SkShader::Context* SkSweepGradient::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000060 return SkNEW_PLACEMENT_ARGS(storage, SweepGradientContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000061}
62
63SkSweepGradient::SweepGradientContext::SweepGradientContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000064 const SkSweepGradient& shader, const ContextRec& rec)
65 : INHERITED(shader, rec) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000066
rileya@google.com589708b2012-07-26 20:04:23 +000067// returns angle in a circle [0..2PI) -> [0..255]
rileya@google.com589708b2012-07-26 20:04:23 +000068static unsigned SkATan2_255(float y, float x) {
69 // static const float g255Over2PI = 255 / (2 * SK_ScalarPI);
70 static const float g255Over2PI = 40.584510488433314f;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
rileya@google.com589708b2012-07-26 20:04:23 +000072 float result = sk_float_atan2(y, x);
73 if (result < 0) {
74 result += 2 * SK_ScalarPI;
75 }
76 SkASSERT(result >= 0);
77 // since our value is always >= 0, we can cast to int, which is faster than
78 // calling floorf()
79 int ir = (int)(result * g255Over2PI);
80 SkASSERT(ir >= 0 && ir <= 255);
81 return ir;
82}
rileya@google.com589708b2012-07-26 20:04:23 +000083
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000084void SkSweepGradient::SweepGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
85 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +000086 SkMatrix::MapXYProc proc = fDstToIndexProc;
87 const SkMatrix& matrix = fDstToIndex;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000088 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
reed@google.com60040292013-02-04 18:21:23 +000089 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +000090 SkPoint srcPt;
91
92 if (fDstToIndexClass != kPerspective_MatrixClass) {
93 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
94 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
95 SkScalar dx, fx = srcPt.fX;
96 SkScalar dy, fy = srcPt.fY;
97
98 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
99 SkFixed storage[2];
100 (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf,
101 &storage[0], &storage[1]);
102 dx = SkFixedToScalar(storage[0]);
103 dy = SkFixedToScalar(storage[1]);
104 } else {
105 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
106 dx = matrix.getScaleX();
107 dy = matrix.getSkewY();
108 }
109
110 for (; count > 0; --count) {
reed@google.com60040292013-02-04 18:21:23 +0000111 *dstC++ = cache[toggle + SkATan2_255(fy, fx)];
rileya@google.com589708b2012-07-26 20:04:23 +0000112 fx += dx;
113 fy += dy;
reed@google.com60040292013-02-04 18:21:23 +0000114 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000115 }
116 } else { // perspective case
117 for (int stop = x + count; x < stop; x++) {
118 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
119 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
reed@google.com60040292013-02-04 18:21:23 +0000120 *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)];
reed@google.com60040292013-02-04 18:21:23 +0000121 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000122 }
123 }
124}
125
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000126void SkSweepGradient::SweepGradientContext::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC,
127 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000128 SkMatrix::MapXYProc proc = fDstToIndexProc;
129 const SkMatrix& matrix = fDstToIndex;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000130 const uint16_t* SK_RESTRICT cache = fCache->getCache16();
reed@google.com55853db2013-02-01 19:34:59 +0000131 int toggle = init_dither_toggle16(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000132 SkPoint srcPt;
133
134 if (fDstToIndexClass != kPerspective_MatrixClass) {
135 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
136 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
137 SkScalar dx, fx = srcPt.fX;
138 SkScalar dy, fy = srcPt.fY;
139
140 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
141 SkFixed storage[2];
142 (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf,
143 &storage[0], &storage[1]);
144 dx = SkFixedToScalar(storage[0]);
145 dy = SkFixedToScalar(storage[1]);
146 } else {
147 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
148 dx = matrix.getScaleX();
149 dy = matrix.getSkewY();
150 }
151
152 for (; count > 0; --count) {
153 int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits);
154 *dstC++ = cache[toggle + index];
reed@google.com55853db2013-02-01 19:34:59 +0000155 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000156 fx += dx;
157 fy += dy;
158 }
159 } else { // perspective case
160 for (int stop = x + count; x < stop; x++) {
161 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
162 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
163
164 int index = SkATan2_255(srcPt.fY, srcPt.fX);
165 index >>= (8 - kCache16Bits);
166 *dstC++ = cache[toggle + index];
reed@google.com55853db2013-02-01 19:34:59 +0000167 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000168 }
169 }
170}
171
rileya@google.comd7cc6512012-07-27 14:00:39 +0000172/////////////////////////////////////////////////////////////////////
173
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000174#if SK_SUPPORT_GPU
175
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000176#include "GrTBackendEffectFactory.h"
177
bsalomon@google.com0707c292012-10-25 21:45:42 +0000178class GrGLSweepGradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000179public:
180
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000181 GrGLSweepGradient(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000182 const GrDrawEffect&) : INHERITED (factory) { }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000183 virtual ~GrGLSweepGradient() { }
184
bsalomon@google.comf78df332012-10-29 12:43:38 +0000185 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000186 const GrDrawEffect&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000187 EffectKey,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000188 const char* outputColor,
189 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000190 const TransformedCoordsArray&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000191 const TextureSamplerArray&) SK_OVERRIDE;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000192
bsalomon@google.comc7818882013-03-20 19:19:53 +0000193 static EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
bsalomon@google.com82d12232013-09-09 15:36:26 +0000194 return GenBaseGradientKey(drawEffect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000195 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000196
197private:
198
bsalomon@google.com0707c292012-10-25 21:45:42 +0000199 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000200
201};
202
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000203/////////////////////////////////////////////////////////////////////
204
205class GrSweepGradient : public GrGradientEffect {
206public:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000207 static GrEffectRef* Create(GrContext* ctx,
208 const SkSweepGradient& shader,
209 const SkMatrix& matrix) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000210 AutoEffectUnref effect(SkNEW_ARGS(GrSweepGradient, (ctx, shader, matrix)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000211 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000212 }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000213 virtual ~GrSweepGradient() { }
214
215 static const char* Name() { return "Sweep Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000216 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
217 return GrTBackendEffectFactory<GrSweepGradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000218 }
219
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000220 typedef GrGLSweepGradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000221
bsalomon@google.comd4726202012-08-03 14:34:46 +0000222private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000223 GrSweepGradient(GrContext* ctx,
224 const SkSweepGradient& shader,
225 const SkMatrix& matrix)
226 : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { }
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000227 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000228
229 typedef GrGradientEffect INHERITED;
230};
231
232/////////////////////////////////////////////////////////////////////
233
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000234GR_DEFINE_EFFECT_TEST(GrSweepGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000235
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000236GrEffectRef* GrSweepGradient::TestCreate(SkRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000237 GrContext* context,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000238 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000239 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000240 SkPoint center = {random->nextUScalar1(), random->nextUScalar1()};
241
242 SkColor colors[kMaxRandomGradientColors];
243 SkScalar stopsArray[kMaxRandomGradientColors];
244 SkScalar* stops = stopsArray;
245 SkShader::TileMode tmIgnored;
246 int colorCount = RandomGradientParams(random, colors, &stops, &tmIgnored);
247 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateSweep(center.fX, center.fY,
248 colors, stops, colorCount));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000249 SkPaint paint;
250 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000251}
252
253/////////////////////////////////////////////////////////////////////
254
bsalomon@google.comf78df332012-10-29 12:43:38 +0000255void GrGLSweepGradient::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000256 const GrDrawEffect&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000257 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000258 const char* outputColor,
259 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000260 const TransformedCoordsArray& coords,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000261 const TextureSamplerArray& samplers) {
bsalomon@google.com82d12232013-09-09 15:36:26 +0000262 this->emitUniforms(builder, key);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000263 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
commit-bot@chromium.org54318d32014-02-14 17:27:04 +0000264 const GrGLContextInfo ctxInfo = builder->ctxInfo();
rileya@google.comd7cc6512012-07-27 14:00:39 +0000265 SkString t;
commit-bot@chromium.org54318d32014-02-14 17:27:04 +0000266 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
267 // On Intel GPU there is an issue where it reads the second arguement to atan "- %s.x" as an int
268 // thus must us -1.0 * %s.x to work correctly
269 if (kIntel_GrGLVendor != ctxInfo.vendor()){
270 t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5",
271 coords2D.c_str(), coords2D.c_str());
272 } else {
273 t.printf("atan(- %s.y, -1.0 * %s.x) * 0.1591549430918 + 0.5",
274 coords2D.c_str(), coords2D.c_str());
275 }
skia.committer@gmail.com9a070f22013-09-10 07:01:44 +0000276 this->emitColor(builder, t.c_str(), key,
bsalomon@google.com82d12232013-09-09 15:36:26 +0000277 outputColor, inputColor, samplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000278}
279
280/////////////////////////////////////////////////////////////////////
281
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000282GrEffectRef* SkSweepGradient::asNewEffect(GrContext* context, const SkPaint&) const {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000283 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000284 if (!this->getLocalMatrix().invert(&matrix)) {
humper@google.com84831ac2013-01-14 22:09:54 +0000285 return NULL;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000286 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000287 matrix.postConcat(fPtsToUnit);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000288 return GrSweepGradient::Create(context, *this, matrix);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000289}
290
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000291#else
292
bsalomon@google.com5d2cd202013-01-16 15:31:06 +0000293GrEffectRef* SkSweepGradient::asNewEffect(GrContext*, const SkPaint&) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000294 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000295 return NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000296}
297
298#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000299
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000300#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000301void SkSweepGradient::toString(SkString* str) const {
302 str->append("SkSweepGradient: (");
303
304 str->append("center: (");
305 str->appendScalar(fCenter.fX);
306 str->append(", ");
307 str->appendScalar(fCenter.fY);
308 str->append(") ");
309
310 this->INHERITED::toString(str);
311
312 str->append(")");
313}
314#endif