michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkSweepGradient.h" michael@0: michael@0: SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, michael@0: const Descriptor& desc) michael@0: : SkGradientShaderBase(desc) michael@0: , fCenter(SkPoint::Make(cx, cy)) michael@0: { michael@0: fPtsToUnit.setTranslate(-cx, -cy); michael@0: michael@0: // overwrite the tilemode to a canonical value (since sweep ignores it) michael@0: fTileMode = SkShader::kClamp_TileMode; michael@0: } michael@0: michael@0: SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap, michael@0: SkMatrix* matrix, SkShader::TileMode* xy) const { michael@0: if (bitmap) { michael@0: this->getGradientTableBitmap(bitmap); michael@0: } michael@0: if (matrix) { michael@0: *matrix = fPtsToUnit; michael@0: } michael@0: if (xy) { michael@0: xy[0] = fTileMode; michael@0: xy[1] = kClamp_TileMode; michael@0: } michael@0: return kSweep_BitmapType; michael@0: } michael@0: michael@0: SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const { michael@0: if (info) { michael@0: commonAsAGradient(info); michael@0: info->fPoint[0] = fCenter; michael@0: } michael@0: return kSweep_GradientType; michael@0: } michael@0: michael@0: SkSweepGradient::SkSweepGradient(SkReadBuffer& buffer) michael@0: : INHERITED(buffer), michael@0: fCenter(buffer.readPoint()) { michael@0: } michael@0: michael@0: void SkSweepGradient::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.writePoint(fCenter); michael@0: } michael@0: michael@0: // returns angle in a circle [0..2PI) -> [0..255] michael@0: static unsigned SkATan2_255(float y, float x) { michael@0: // static const float g255Over2PI = 255 / (2 * SK_ScalarPI); michael@0: static const float g255Over2PI = 40.584510488433314f; michael@0: michael@0: float result = sk_float_atan2(y, x); michael@0: if (result < 0) { michael@0: result += 2 * SK_ScalarPI; michael@0: } michael@0: SkASSERT(result >= 0); michael@0: // since our value is always >= 0, we can cast to int, which is faster than michael@0: // calling floorf() michael@0: int ir = (int)(result * g255Over2PI); michael@0: SkASSERT(ir >= 0 && ir <= 255); michael@0: return ir; michael@0: } michael@0: michael@0: void SkSweepGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, michael@0: int count) { michael@0: SkMatrix::MapXYProc proc = fDstToIndexProc; michael@0: const SkMatrix& matrix = fDstToIndex; michael@0: const SkPMColor* SK_RESTRICT cache = this->getCache32(); michael@0: int toggle = init_dither_toggle(x, y); michael@0: SkPoint srcPt; michael@0: michael@0: if (fDstToIndexClass != kPerspective_MatrixClass) { michael@0: proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: SkScalar dx, fx = srcPt.fX; michael@0: SkScalar dy, fy = srcPt.fY; michael@0: michael@0: if (fDstToIndexClass == kFixedStepInX_MatrixClass) { michael@0: SkFixed storage[2]; michael@0: (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, michael@0: &storage[0], &storage[1]); michael@0: dx = SkFixedToScalar(storage[0]); michael@0: dy = SkFixedToScalar(storage[1]); michael@0: } else { michael@0: SkASSERT(fDstToIndexClass == kLinear_MatrixClass); michael@0: dx = matrix.getScaleX(); michael@0: dy = matrix.getSkewY(); michael@0: } michael@0: michael@0: for (; count > 0; --count) { michael@0: *dstC++ = cache[toggle + SkATan2_255(fy, fx)]; michael@0: fx += dx; michael@0: fy += dy; michael@0: toggle = next_dither_toggle(toggle); michael@0: } michael@0: } else { // perspective case michael@0: for (int stop = x + count; x < stop; x++) { michael@0: proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; michael@0: toggle = next_dither_toggle(toggle); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, michael@0: int count) { michael@0: SkMatrix::MapXYProc proc = fDstToIndexProc; michael@0: const SkMatrix& matrix = fDstToIndex; michael@0: const uint16_t* SK_RESTRICT cache = this->getCache16(); michael@0: int toggle = init_dither_toggle16(x, y); michael@0: SkPoint srcPt; michael@0: michael@0: if (fDstToIndexClass != kPerspective_MatrixClass) { michael@0: proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: SkScalar dx, fx = srcPt.fX; michael@0: SkScalar dy, fy = srcPt.fY; michael@0: michael@0: if (fDstToIndexClass == kFixedStepInX_MatrixClass) { michael@0: SkFixed storage[2]; michael@0: (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, michael@0: &storage[0], &storage[1]); michael@0: dx = SkFixedToScalar(storage[0]); michael@0: dy = SkFixedToScalar(storage[1]); michael@0: } else { michael@0: SkASSERT(fDstToIndexClass == kLinear_MatrixClass); michael@0: dx = matrix.getScaleX(); michael@0: dy = matrix.getSkewY(); michael@0: } michael@0: michael@0: for (; count > 0; --count) { michael@0: int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits); michael@0: *dstC++ = cache[toggle + index]; michael@0: toggle = next_dither_toggle16(toggle); michael@0: fx += dx; michael@0: fy += dy; michael@0: } michael@0: } else { // perspective case michael@0: for (int stop = x + count; x < stop; x++) { michael@0: proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, michael@0: SkIntToScalar(y) + SK_ScalarHalf, &srcPt); michael@0: michael@0: int index = SkATan2_255(srcPt.fY, srcPt.fX); michael@0: index >>= (8 - kCache16Bits); michael@0: *dstC++ = cache[toggle + index]; michael@0: toggle = next_dither_toggle16(toggle); michael@0: } michael@0: } michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: #if SK_SUPPORT_GPU michael@0: michael@0: #include "GrTBackendEffectFactory.h" michael@0: michael@0: class GrGLSweepGradient : public GrGLGradientEffect { michael@0: public: michael@0: michael@0: GrGLSweepGradient(const GrBackendEffectFactory& factory, michael@0: const GrDrawEffect&) : INHERITED (factory) { } michael@0: virtual ~GrGLSweepGradient() { } michael@0: michael@0: virtual void emitCode(GrGLShaderBuilder*, michael@0: const GrDrawEffect&, michael@0: EffectKey, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray&) SK_OVERRIDE; michael@0: michael@0: static EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: return GenBaseGradientKey(drawEffect); michael@0: } michael@0: michael@0: private: michael@0: michael@0: typedef GrGLGradientEffect INHERITED; michael@0: michael@0: }; michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: class GrSweepGradient : public GrGradientEffect { michael@0: public: michael@0: static GrEffectRef* Create(GrContext* ctx, michael@0: const SkSweepGradient& shader, michael@0: const SkMatrix& matrix) { michael@0: AutoEffectUnref effect(SkNEW_ARGS(GrSweepGradient, (ctx, shader, matrix))); michael@0: return CreateEffectRef(effect); michael@0: } michael@0: virtual ~GrSweepGradient() { } michael@0: michael@0: static const char* Name() { return "Sweep Gradient"; } michael@0: virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: typedef GrGLSweepGradient GLEffect; michael@0: michael@0: private: michael@0: GrSweepGradient(GrContext* ctx, michael@0: const SkSweepGradient& shader, michael@0: const SkMatrix& matrix) michael@0: : INHERITED(ctx, shader, matrix, SkShader::kClamp_TileMode) { } michael@0: GR_DECLARE_EFFECT_TEST; michael@0: michael@0: typedef GrGradientEffect INHERITED; michael@0: }; michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: GR_DEFINE_EFFECT_TEST(GrSweepGradient); michael@0: michael@0: GrEffectRef* GrSweepGradient::TestCreate(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture**) { michael@0: SkPoint center = {random->nextUScalar1(), random->nextUScalar1()}; michael@0: michael@0: SkColor colors[kMaxRandomGradientColors]; michael@0: SkScalar stopsArray[kMaxRandomGradientColors]; michael@0: SkScalar* stops = stopsArray; michael@0: SkShader::TileMode tmIgnored; michael@0: int colorCount = RandomGradientParams(random, colors, &stops, &tmIgnored); michael@0: SkAutoTUnref shader(SkGradientShader::CreateSweep(center.fX, center.fY, michael@0: colors, stops, colorCount)); michael@0: SkPaint paint; michael@0: return shader->asNewEffect(context, paint); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: void GrGLSweepGradient::emitCode(GrGLShaderBuilder* builder, michael@0: const GrDrawEffect&, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray& coords, michael@0: const TextureSamplerArray& samplers) { michael@0: this->emitUniforms(builder, key); michael@0: SkString coords2D = builder->ensureFSCoords2D(coords, 0); michael@0: const GrGLContextInfo ctxInfo = builder->ctxInfo(); michael@0: SkString t; michael@0: // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi] michael@0: // On Intel GPU there is an issue where it reads the second arguement to atan "- %s.x" as an int michael@0: // thus must us -1.0 * %s.x to work correctly michael@0: if (kIntel_GrGLVendor != ctxInfo.vendor()){ michael@0: t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5", michael@0: coords2D.c_str(), coords2D.c_str()); michael@0: } else { michael@0: t.printf("atan(- %s.y, -1.0 * %s.x) * 0.1591549430918 + 0.5", michael@0: coords2D.c_str(), coords2D.c_str()); michael@0: } michael@0: this->emitColor(builder, t.c_str(), key, michael@0: outputColor, inputColor, samplers); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: GrEffectRef* SkSweepGradient::asNewEffect(GrContext* context, const SkPaint&) const { michael@0: SkMatrix matrix; michael@0: if (!this->getLocalMatrix().invert(&matrix)) { michael@0: return NULL; michael@0: } michael@0: matrix.postConcat(fPtsToUnit); michael@0: return GrSweepGradient::Create(context, *this, matrix); michael@0: } michael@0: michael@0: #else michael@0: michael@0: GrEffectRef* SkSweepGradient::asNewEffect(GrContext*, const SkPaint&) const { michael@0: SkDEBUGFAIL("Should not call in GPU-less build"); michael@0: return NULL; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkSweepGradient::toString(SkString* str) const { michael@0: str->append("SkSweepGradient: ("); michael@0: michael@0: str->append("center: ("); michael@0: str->appendScalar(fCenter.fX); michael@0: str->append(", "); michael@0: str->appendScalar(fCenter.fY); michael@0: str->append(") "); michael@0: michael@0: this->INHERITED::toString(str); michael@0: michael@0: str->append(")"); michael@0: } michael@0: #endif