rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | |
| 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.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 11 | SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, |
| 12 | const Descriptor& desc) |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 13 | : SkGradientShaderBase(desc) |
| 14 | , fCenter(SkPoint::Make(cx, cy)) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 15 | { |
| 16 | fPtsToUnit.setTranslate(-cx, -cy); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 17 | |
| 18 | // overwrite the tilemode to a canonical value (since sweep ignores it) |
| 19 | fTileMode = SkShader::kClamp_TileMode; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap, |
| 23 | SkMatrix* matrix, SkShader::TileMode* xy) const { |
| 24 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 25 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 26 | } |
| 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 | |
| 37 | SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const { |
| 38 | if (info) { |
| 39 | commonAsAGradient(info); |
| 40 | info->fPoint[0] = fCenter; |
| 41 | } |
| 42 | return kSweep_GradientType; |
| 43 | } |
| 44 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 45 | SkSweepGradient::SkSweepGradient(SkFlattenableReadBuffer& buffer) |
| 46 | : INHERITED(buffer), |
| 47 | fCenter(buffer.readPoint()) { |
| 48 | } |
| 49 | |
| 50 | void SkSweepGradient::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 51 | this->INHERITED::flatten(buffer); |
| 52 | buffer.writePoint(fCenter); |
| 53 | } |
| 54 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 55 | // returns angle in a circle [0..2PI) -> [0..255] |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 56 | static unsigned SkATan2_255(float y, float x) { |
| 57 | // static const float g255Over2PI = 255 / (2 * SK_ScalarPI); |
| 58 | static const float g255Over2PI = 40.584510488433314f; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 59 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 60 | float result = sk_float_atan2(y, x); |
| 61 | if (result < 0) { |
| 62 | result += 2 * SK_ScalarPI; |
| 63 | } |
| 64 | SkASSERT(result >= 0); |
| 65 | // since our value is always >= 0, we can cast to int, which is faster than |
| 66 | // calling floorf() |
| 67 | int ir = (int)(result * g255Over2PI); |
| 68 | SkASSERT(ir >= 0 && ir <= 255); |
| 69 | return ir; |
| 70 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 71 | |
| 72 | void SkSweepGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, |
| 73 | int count) { |
| 74 | SkMatrix::MapXYProc proc = fDstToIndexProc; |
| 75 | const SkMatrix& matrix = fDstToIndex; |
| 76 | const SkPMColor* SK_RESTRICT cache = this->getCache32(); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 77 | int toggle = init_dither_toggle(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 78 | SkPoint srcPt; |
| 79 | |
| 80 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 81 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 82 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 83 | SkScalar dx, fx = srcPt.fX; |
| 84 | SkScalar dy, fy = srcPt.fY; |
| 85 | |
| 86 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 87 | SkFixed storage[2]; |
| 88 | (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, |
| 89 | &storage[0], &storage[1]); |
| 90 | dx = SkFixedToScalar(storage[0]); |
| 91 | dy = SkFixedToScalar(storage[1]); |
| 92 | } else { |
| 93 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 94 | dx = matrix.getScaleX(); |
| 95 | dy = matrix.getSkewY(); |
| 96 | } |
| 97 | |
| 98 | for (; count > 0; --count) { |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 99 | *dstC++ = cache[toggle + SkATan2_255(fy, fx)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 100 | fx += dx; |
| 101 | fy += dy; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 102 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 103 | } |
| 104 | } else { // perspective case |
| 105 | for (int stop = x + count; x < stop; x++) { |
| 106 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 107 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 108 | *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 109 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, |
| 115 | int count) { |
| 116 | SkMatrix::MapXYProc proc = fDstToIndexProc; |
| 117 | const SkMatrix& matrix = fDstToIndex; |
| 118 | const uint16_t* SK_RESTRICT cache = this->getCache16(); |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 119 | int toggle = init_dither_toggle16(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 120 | SkPoint srcPt; |
| 121 | |
| 122 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 123 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 124 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 125 | SkScalar dx, fx = srcPt.fX; |
| 126 | SkScalar dy, fy = srcPt.fY; |
| 127 | |
| 128 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 129 | SkFixed storage[2]; |
| 130 | (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, |
| 131 | &storage[0], &storage[1]); |
| 132 | dx = SkFixedToScalar(storage[0]); |
| 133 | dy = SkFixedToScalar(storage[1]); |
| 134 | } else { |
| 135 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 136 | dx = matrix.getScaleX(); |
| 137 | dy = matrix.getSkewY(); |
| 138 | } |
| 139 | |
| 140 | for (; count > 0; --count) { |
| 141 | int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits); |
| 142 | *dstC++ = cache[toggle + index]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 143 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 144 | fx += dx; |
| 145 | fy += dy; |
| 146 | } |
| 147 | } else { // perspective case |
| 148 | for (int stop = x + count; x < stop; x++) { |
| 149 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 150 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 151 | |
| 152 | int index = SkATan2_255(srcPt.fY, srcPt.fX); |
| 153 | index >>= (8 - kCache16Bits); |
| 154 | *dstC++ = cache[toggle + index]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 155 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 160 | ///////////////////////////////////////////////////////////////////// |
| 161 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 162 | #if SK_SUPPORT_GPU |
| 163 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 164 | #include "GrTBackendEffectFactory.h" |
| 165 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 166 | class GrGLSweepGradient : public GrGLGradientEffect { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 167 | public: |
| 168 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 169 | GrGLSweepGradient(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 170 | const GrDrawEffect&) : INHERITED (factory) { } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 171 | virtual ~GrGLSweepGradient() { } |
| 172 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 173 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 174 | const GrDrawEffect&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 175 | EffectKey, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 176 | const char* outputColor, |
| 177 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 178 | const TransformedCoordsArray&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 179 | const TextureSamplerArray&) SK_OVERRIDE; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 180 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 181 | static EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 182 | return GenBaseGradientKey(drawEffect); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 183 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 187 | typedef GrGLGradientEffect INHERITED; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 188 | |
| 189 | }; |
| 190 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 191 | ///////////////////////////////////////////////////////////////////// |
| 192 | |
| 193 | class GrSweepGradient : public GrGradientEffect { |
| 194 | public: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 195 | static GrEffectRef* Create(GrContext* ctx, |
| 196 | const SkSweepGradient& shader, |
| 197 | const SkMatrix& matrix) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 198 | AutoEffectUnref effect(SkNEW_ARGS(GrSweepGradient, (ctx, shader, matrix))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 199 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 200 | } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 201 | virtual ~GrSweepGradient() { } |
| 202 | |
| 203 | static const char* Name() { return "Sweep Gradient"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 204 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 205 | return GrTBackendEffectFactory<GrSweepGradient>::getInstance(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 206 | } |
| 207 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 208 | typedef GrGLSweepGradient GLEffect; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 209 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 210 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 211 | GrSweepGradient(GrContext* ctx, |
| 212 | const SkSweepGradient& shader, |
| 213 | const SkMatrix& matrix) |
| 214 | : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { } |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 215 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 216 | |
| 217 | typedef GrGradientEffect INHERITED; |
| 218 | }; |
| 219 | |
| 220 | ///////////////////////////////////////////////////////////////////// |
| 221 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 222 | GR_DEFINE_EFFECT_TEST(GrSweepGradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 223 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 224 | GrEffectRef* GrSweepGradient::TestCreate(SkRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 225 | GrContext* context, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 226 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 227 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 228 | SkPoint center = {random->nextUScalar1(), random->nextUScalar1()}; |
| 229 | |
| 230 | SkColor colors[kMaxRandomGradientColors]; |
| 231 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 232 | SkScalar* stops = stopsArray; |
| 233 | SkShader::TileMode tmIgnored; |
| 234 | int colorCount = RandomGradientParams(random, colors, &stops, &tmIgnored); |
| 235 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateSweep(center.fX, center.fY, |
| 236 | colors, stops, colorCount)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 237 | SkPaint paint; |
| 238 | return shader->asNewEffect(context, paint); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | ///////////////////////////////////////////////////////////////////// |
| 242 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 243 | void GrGLSweepGradient::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 244 | const GrDrawEffect&, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 245 | EffectKey key, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 246 | const char* outputColor, |
| 247 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 248 | const TransformedCoordsArray& coords, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 249 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 250 | this->emitUniforms(builder, key); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 251 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 252 | SkString t; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 253 | t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5", coords2D.c_str(), coords2D.c_str()); |
skia.committer@gmail.com | 9a070f2 | 2013-09-10 07:01:44 +0000 | [diff] [blame] | 254 | this->emitColor(builder, t.c_str(), key, |
bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +0000 | [diff] [blame] | 255 | outputColor, inputColor, samplers); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | ///////////////////////////////////////////////////////////////////// |
| 259 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 260 | GrEffectRef* SkSweepGradient::asNewEffect(GrContext* context, const SkPaint&) const { |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 261 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 262 | if (!this->getLocalMatrix().invert(&matrix)) { |
humper@google.com | 84831ac | 2013-01-14 22:09:54 +0000 | [diff] [blame] | 263 | return NULL; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 264 | } |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 265 | matrix.postConcat(fPtsToUnit); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 266 | return GrSweepGradient::Create(context, *this, matrix); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 267 | } |
| 268 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 269 | #else |
| 270 | |
bsalomon@google.com | 5d2cd20 | 2013-01-16 15:31:06 +0000 | [diff] [blame] | 271 | GrEffectRef* SkSweepGradient::asNewEffect(GrContext*, const SkPaint&) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 272 | SkDEBUGFAIL("Should not call in GPU-less build"); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 273 | return NULL; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 277 | |
| 278 | #ifdef SK_DEVELOPER |
| 279 | void SkSweepGradient::toString(SkString* str) const { |
| 280 | str->append("SkSweepGradient: ("); |
| 281 | |
| 282 | str->append("center: ("); |
| 283 | str->appendScalar(fCenter.fX); |
| 284 | str->append(", "); |
| 285 | str->appendScalar(fCenter.fY); |
| 286 | str->append(") "); |
| 287 | |
| 288 | this->INHERITED::toString(str); |
| 289 | |
| 290 | str->append(")"); |
| 291 | } |
| 292 | #endif |