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 | addf2ed | 2014-08-11 08:28:24 -0700 | [diff] [blame] | 11 | SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, const Descriptor& desc) |
| 12 | : SkGradientShaderBase(desc) |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 13 | , fCenter(SkPoint::Make(cx, cy)) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 14 | { |
| 15 | fPtsToUnit.setTranslate(-cx, -cy); |
reed@google.com | 437d6eb | 2013-05-23 19:03:05 +0000 | [diff] [blame] | 16 | |
| 17 | // overwrite the tilemode to a canonical value (since sweep ignores it) |
| 18 | fTileMode = SkShader::kClamp_TileMode; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap, |
| 22 | SkMatrix* matrix, SkShader::TileMode* xy) const { |
| 23 | if (bitmap) { |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 24 | this->getGradientTableBitmap(bitmap); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 25 | } |
| 26 | if (matrix) { |
| 27 | *matrix = fPtsToUnit; |
| 28 | } |
| 29 | if (xy) { |
| 30 | xy[0] = fTileMode; |
| 31 | xy[1] = kClamp_TileMode; |
| 32 | } |
| 33 | return kSweep_BitmapType; |
| 34 | } |
| 35 | |
| 36 | SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const { |
| 37 | if (info) { |
| 38 | commonAsAGradient(info); |
| 39 | info->fPoint[0] = fCenter; |
| 40 | } |
| 41 | return kSweep_GradientType; |
| 42 | } |
| 43 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 44 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 45 | SkSweepGradient::SkSweepGradient(SkReadBuffer& buffer) |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 46 | : INHERITED(buffer), |
| 47 | fCenter(buffer.readPoint()) { |
| 48 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | SkFlattenable* SkSweepGradient::CreateProc(SkReadBuffer& buffer) { |
| 52 | DescriptorScope desc; |
| 53 | if (!desc.unflatten(buffer)) { |
| 54 | return NULL; |
| 55 | } |
| 56 | const SkPoint center = buffer.readPoint(); |
| 57 | return SkGradientShader::CreateSweep(center.x(), center.y(), desc.fColors, desc.fPos, |
| 58 | desc.fCount, desc.fGradFlags, desc.fLocalMatrix); |
| 59 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 61 | void SkSweepGradient::flatten(SkWriteBuffer& buffer) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 62 | this->INHERITED::flatten(buffer); |
| 63 | buffer.writePoint(fCenter); |
| 64 | } |
| 65 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 66 | size_t SkSweepGradient::contextSize() const { |
| 67 | return sizeof(SweepGradientContext); |
| 68 | } |
| 69 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 70 | SkShader::Context* SkSweepGradient::onCreateContext(const ContextRec& rec, void* storage) const { |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 71 | return SkNEW_PLACEMENT_ARGS(storage, SweepGradientContext, (*this, rec)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | SkSweepGradient::SweepGradientContext::SweepGradientContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 75 | const SkSweepGradient& shader, const ContextRec& rec) |
| 76 | : INHERITED(shader, rec) {} |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 77 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 78 | // returns angle in a circle [0..2PI) -> [0..255] |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 79 | static unsigned SkATan2_255(float y, float x) { |
| 80 | // static const float g255Over2PI = 255 / (2 * SK_ScalarPI); |
| 81 | static const float g255Over2PI = 40.584510488433314f; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 82 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 83 | float result = sk_float_atan2(y, x); |
| 84 | if (result < 0) { |
| 85 | result += 2 * SK_ScalarPI; |
| 86 | } |
| 87 | SkASSERT(result >= 0); |
| 88 | // since our value is always >= 0, we can cast to int, which is faster than |
| 89 | // calling floorf() |
| 90 | int ir = (int)(result * g255Over2PI); |
| 91 | SkASSERT(ir >= 0 && ir <= 255); |
| 92 | return ir; |
| 93 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 95 | void SkSweepGradient::SweepGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, |
| 96 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 97 | SkMatrix::MapXYProc proc = fDstToIndexProc; |
| 98 | const SkMatrix& matrix = fDstToIndex; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 99 | const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 100 | int toggle = init_dither_toggle(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 101 | SkPoint srcPt; |
| 102 | |
| 103 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 104 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 105 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 106 | SkScalar dx, fx = srcPt.fX; |
| 107 | SkScalar dy, fy = srcPt.fY; |
| 108 | |
| 109 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 110 | SkFixed storage[2]; |
| 111 | (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, |
| 112 | &storage[0], &storage[1]); |
| 113 | dx = SkFixedToScalar(storage[0]); |
| 114 | dy = SkFixedToScalar(storage[1]); |
| 115 | } else { |
| 116 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 117 | dx = matrix.getScaleX(); |
| 118 | dy = matrix.getSkewY(); |
| 119 | } |
| 120 | |
| 121 | for (; count > 0; --count) { |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 122 | *dstC++ = cache[toggle + SkATan2_255(fy, fx)]; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 123 | fx += dx; |
| 124 | fy += dy; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 125 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 126 | } |
| 127 | } else { // perspective case |
| 128 | for (int stop = x + count; x < stop; x++) { |
| 129 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 130 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 131 | *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 132 | toggle = next_dither_toggle(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 137 | void SkSweepGradient::SweepGradientContext::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, |
| 138 | int count) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 139 | SkMatrix::MapXYProc proc = fDstToIndexProc; |
| 140 | const SkMatrix& matrix = fDstToIndex; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 141 | const uint16_t* SK_RESTRICT cache = fCache->getCache16(); |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 142 | int toggle = init_dither_toggle16(x, y); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 143 | SkPoint srcPt; |
| 144 | |
| 145 | if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 146 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 147 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 148 | SkScalar dx, fx = srcPt.fX; |
| 149 | SkScalar dy, fy = srcPt.fY; |
| 150 | |
| 151 | if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| 152 | SkFixed storage[2]; |
| 153 | (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, |
| 154 | &storage[0], &storage[1]); |
| 155 | dx = SkFixedToScalar(storage[0]); |
| 156 | dy = SkFixedToScalar(storage[1]); |
| 157 | } else { |
| 158 | SkASSERT(fDstToIndexClass == kLinear_MatrixClass); |
| 159 | dx = matrix.getScaleX(); |
| 160 | dy = matrix.getSkewY(); |
| 161 | } |
| 162 | |
| 163 | for (; count > 0; --count) { |
| 164 | int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits); |
| 165 | *dstC++ = cache[toggle + index]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 166 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 167 | fx += dx; |
| 168 | fy += dy; |
| 169 | } |
| 170 | } else { // perspective case |
| 171 | for (int stop = x + count; x < stop; x++) { |
| 172 | proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 173 | SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 174 | |
| 175 | int index = SkATan2_255(srcPt.fY, srcPt.fX); |
| 176 | index >>= (8 - kCache16Bits); |
| 177 | *dstC++ = cache[toggle + index]; |
reed@google.com | 55853db | 2013-02-01 19:34:59 +0000 | [diff] [blame] | 178 | toggle = next_dither_toggle16(toggle); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 183 | ///////////////////////////////////////////////////////////////////// |
| 184 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 185 | #if SK_SUPPORT_GPU |
| 186 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 187 | #include "GrTBackendEffectFactory.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame^] | 188 | #include "gl/builders/GrGLProgramBuilder.h" |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 189 | #include "SkGr.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 190 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 191 | class GrGLSweepGradient : public GrGLGradientEffect { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 192 | public: |
| 193 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 194 | GrGLSweepGradient(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 195 | const GrDrawEffect&) : INHERITED (factory) { } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 196 | virtual ~GrGLSweepGradient() { } |
| 197 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame^] | 198 | virtual void emitCode(GrGLProgramBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 199 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 200 | const GrEffectKey&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 201 | const char* outputColor, |
| 202 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 203 | const TransformedCoordsArray&, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 204 | const TextureSamplerArray&) SK_OVERRIDE; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 205 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 206 | static void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, GrEffectKeyBuilder* b) { |
| 207 | b->add32(GenBaseGradientKey(drawEffect)); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 208 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 212 | typedef GrGLGradientEffect INHERITED; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 213 | |
| 214 | }; |
| 215 | |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 216 | ///////////////////////////////////////////////////////////////////// |
| 217 | |
| 218 | class GrSweepGradient : public GrGradientEffect { |
| 219 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 220 | static GrEffect* Create(GrContext* ctx, const SkSweepGradient& shader, const SkMatrix& m) { |
| 221 | return SkNEW_ARGS(GrSweepGradient, (ctx, shader, m)); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 222 | } |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 223 | virtual ~GrSweepGradient() { } |
| 224 | |
| 225 | static const char* Name() { return "Sweep Gradient"; } |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 226 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 227 | return GrTBackendEffectFactory<GrSweepGradient>::getInstance(); |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 228 | } |
| 229 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 230 | typedef GrGLSweepGradient GLEffect; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 231 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 232 | private: |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 233 | GrSweepGradient(GrContext* ctx, |
| 234 | const SkSweepGradient& shader, |
| 235 | const SkMatrix& matrix) |
| 236 | : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { } |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 237 | GR_DECLARE_EFFECT_TEST; |
rileya@google.com | 98e8b6d | 2012-07-31 20:38:06 +0000 | [diff] [blame] | 238 | |
| 239 | typedef GrGradientEffect INHERITED; |
| 240 | }; |
| 241 | |
| 242 | ///////////////////////////////////////////////////////////////////// |
| 243 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 244 | GR_DEFINE_EFFECT_TEST(GrSweepGradient); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 245 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 246 | GrEffect* GrSweepGradient::TestCreate(SkRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 247 | GrContext* context, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 248 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 249 | GrTexture**) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 250 | SkPoint center = {random->nextUScalar1(), random->nextUScalar1()}; |
| 251 | |
| 252 | SkColor colors[kMaxRandomGradientColors]; |
| 253 | SkScalar stopsArray[kMaxRandomGradientColors]; |
| 254 | SkScalar* stops = stopsArray; |
| 255 | SkShader::TileMode tmIgnored; |
| 256 | int colorCount = RandomGradientParams(random, colors, &stops, &tmIgnored); |
| 257 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateSweep(center.fX, center.fY, |
| 258 | colors, stops, colorCount)); |
bsalomon@google.com | e197cbf | 2013-01-14 16:46:26 +0000 | [diff] [blame] | 259 | SkPaint paint; |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 260 | GrEffect* effect; |
| 261 | GrColor paintColor; |
| 262 | SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effect)); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 263 | return effect; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | ///////////////////////////////////////////////////////////////////// |
| 267 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame^] | 268 | void GrGLSweepGradient::emitCode(GrGLProgramBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 269 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 270 | const GrEffectKey& key, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 271 | const char* outputColor, |
| 272 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 273 | const TransformedCoordsArray& coords, |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 274 | const TextureSamplerArray& samplers) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 275 | uint32_t baseKey = key.get32(0); |
| 276 | this->emitUniforms(builder, baseKey); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame^] | 277 | SkString coords2D = builder->getFragmentShaderBuilder()->ensureFSCoords2D(coords, 0); |
commit-bot@chromium.org | 54318d3 | 2014-02-14 17:27:04 +0000 | [diff] [blame] | 278 | const GrGLContextInfo ctxInfo = builder->ctxInfo(); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 279 | SkString t; |
commit-bot@chromium.org | 54318d3 | 2014-02-14 17:27:04 +0000 | [diff] [blame] | 280 | // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi] |
| 281 | // On Intel GPU there is an issue where it reads the second arguement to atan "- %s.x" as an int |
| 282 | // thus must us -1.0 * %s.x to work correctly |
| 283 | if (kIntel_GrGLVendor != ctxInfo.vendor()){ |
| 284 | t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5", |
| 285 | coords2D.c_str(), coords2D.c_str()); |
| 286 | } else { |
| 287 | t.printf("atan(- %s.y, -1.0 * %s.x) * 0.1591549430918 + 0.5", |
| 288 | coords2D.c_str(), coords2D.c_str()); |
| 289 | } |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 290 | this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, samplers); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | ///////////////////////////////////////////////////////////////////// |
| 294 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 295 | bool SkSweepGradient::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 296 | const SkMatrix* localMatrix, GrColor* paintColor, |
| 297 | GrEffect** effect) const { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 298 | |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 299 | SkMatrix matrix; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 300 | if (!this->getLocalMatrix().invert(&matrix)) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 301 | return false; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 302 | } |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 303 | if (localMatrix) { |
| 304 | SkMatrix inv; |
| 305 | if (!localMatrix->invert(&inv)) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 306 | return false; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 307 | } |
| 308 | matrix.postConcat(inv); |
| 309 | } |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame] | 310 | matrix.postConcat(fPtsToUnit); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 311 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 312 | *effect = GrSweepGradient::Create(context, *this, matrix); |
| 313 | *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 314 | |
| 315 | return true; |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 316 | } |
| 317 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 318 | #else |
| 319 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 320 | bool SkSweepGradient::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 321 | const SkMatrix* localMatrix, GrColor* paintColor, |
| 322 | GrEffect** effect) const { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 323 | SkDEBUGFAIL("Should not call in GPU-less build"); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 324 | return false; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | #endif |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 328 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 329 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 330 | void SkSweepGradient::toString(SkString* str) const { |
| 331 | str->append("SkSweepGradient: ("); |
| 332 | |
| 333 | str->append("center: ("); |
| 334 | str->appendScalar(fCenter.fX); |
| 335 | str->append(", "); |
| 336 | str->appendScalar(fCenter.fY); |
| 337 | str->append(") "); |
| 338 | |
| 339 | this->INHERITED::toString(str); |
| 340 | |
| 341 | str->append(")"); |
| 342 | } |
| 343 | #endif |