aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-05-23 19:03:05 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-05-23 19:03:05 +0000
commit437d6eb4e0d91710fdeb7ecedb694f658458ae00 (patch)
treec4adac1fcf5c4945578b2c23e346ef4a8898eea0 /src/effects
parentff26b7ea7cd57ab2c809c11986249b695d616d8c (diff)
use Descriptor struct to encapsulate all the common paramaeters between our various gradient types. If we like it, might promote it to the public API.
BUG= R=bsalomon@google.com Review URL: https://codereview.chromium.org/15733007 git-svn-id: http://skia.googlecode.com/svn/trunk@9260 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/effects')
-rw-r--r--src/effects/gradients/SkGradientShader.cpp87
-rw-r--r--src/effects/gradients/SkGradientShaderPriv.h17
-rw-r--r--src/effects/gradients/SkLinearGradient.cpp9
-rw-r--r--src/effects/gradients/SkLinearGradient.h4
-rw-r--r--src/effects/gradients/SkRadialGradient.cpp5
-rw-r--r--src/effects/gradients/SkRadialGradient.h4
-rw-r--r--src/effects/gradients/SkSweepGradient.cpp9
-rw-r--r--src/effects/gradients/SkSweepGradient.h3
-rw-r--r--src/effects/gradients/SkTwoPointConicalGradient.cpp6
-rw-r--r--src/effects/gradients/SkTwoPointConicalGradient.h4
-rw-r--r--src/effects/gradients/SkTwoPointRadialGradient.cpp6
-rw-r--r--src/effects/gradients/SkTwoPointRadialGradient.h4
12 files changed, 86 insertions, 72 deletions
diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index 684355d5df..d1ab539c07 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -12,19 +12,18 @@
#include "SkTwoPointConicalGradient.h"
#include "SkSweepGradient.h"
-SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScalar pos[],
- int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper) {
- SkASSERT(colorCount > 1);
+SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc) {
+ SkASSERT(desc.fCount > 1);
fCacheAlpha = 256; // init to a value that paint.getAlpha() can't return
- fMapper = mapper;
- SkSafeRef(mapper);
+ fMapper = desc.fMapper;
+ SkSafeRef(fMapper);
- SkASSERT((unsigned)mode < SkShader::kTileModeCount);
+ SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount);
SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs));
- fTileMode = mode;
- fTileProc = gTileProcs[mode];
+ fTileMode = desc.fTileMode;
+ fTileProc = gTileProcs[desc.fTileMode];
fCache16 = fCache16Storage = NULL;
fCache32 = NULL;
@@ -41,13 +40,13 @@ SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScala
colorCount = 2
fColorCount = 4
*/
- fColorCount = colorCount;
+ fColorCount = desc.fCount;
// check if we need to add in dummy start and/or end position/colors
bool dummyFirst = false;
bool dummyLast = false;
- if (pos) {
- dummyFirst = pos[0] != 0;
- dummyLast = pos[colorCount - 1] != SK_Scalar1;
+ if (desc.fPos) {
+ dummyFirst = desc.fPos[0] != 0;
+ dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1;
fColorCount += dummyFirst + dummyLast;
}
@@ -64,12 +63,12 @@ SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScala
{
SkColor* origColors = fOrigColors;
if (dummyFirst) {
- *origColors++ = colors[0];
+ *origColors++ = desc.fColors[0];
}
- memcpy(origColors, colors, colorCount * sizeof(SkColor));
+ memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor));
if (dummyLast) {
- origColors += colorCount;
- *origColors = colors[colorCount - 1];
+ origColors += desc.fCount;
+ *origColors = desc.fColors[desc.fCount - 1];
}
}
@@ -79,7 +78,7 @@ SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScala
recs->fPos = 0;
// recs->fScale = 0; // unused;
recs += 1;
- if (pos) {
+ if (desc.fPos) {
/* We need to convert the user's array of relative positions into
fixed-point positions and scale factors. We need these results
to be strictly monotonic (no two values equal or out of order).
@@ -89,14 +88,14 @@ SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScala
*/
SkFixed prev = 0;
int startIndex = dummyFirst ? 0 : 1;
- int count = colorCount + dummyLast;
+ int count = desc.fCount + dummyLast;
for (int i = startIndex; i < count; i++) {
// force the last value to be 1.0
SkFixed curr;
- if (i == colorCount) { // we're really at the dummyLast
+ if (i == desc.fCount) { // we're really at the dummyLast
curr = SK_Fixed1;
} else {
- curr = SkScalarToFixed(pos[i]);
+ curr = SkScalarToFixed(desc.fPos[i]);
}
// pin curr withing range
if (curr < 0) {
@@ -115,10 +114,10 @@ SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScala
recs += 1;
}
} else { // assume even distribution
- SkFixed dp = SK_Fixed1 / (colorCount - 1);
+ SkFixed dp = SK_Fixed1 / (desc.fCount - 1);
SkFixed p = dp;
- SkFixed scale = (colorCount - 1) << 8; // (1 << 24) / dp
- for (int i = 1; i < colorCount; i++) {
+ SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp
+ for (int i = 1; i < desc.fCount; i++) {
recs->fPos = p;
recs->fScale = scale;
recs += 1;
@@ -640,6 +639,18 @@ void SkGradientShaderBase::toString(SkString* str) const {
} \
} while (0)
+static void desc_init(SkGradientShaderBase::Descriptor* desc,
+ const SkColor colors[],
+ const SkScalar pos[], int colorCount,
+ SkShader::TileMode mode,
+ SkUnitMapper* mapper) {
+ desc->fColors = colors;
+ desc->fPos = pos;
+ desc->fCount = colorCount;
+ desc->fTileMode = mode;
+ desc->fMapper = mapper;
+}
+
SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
const SkColor colors[],
const SkScalar pos[], int colorCount,
@@ -650,8 +661,9 @@ SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
}
EXPAND_1_COLOR(colorCount);
- return SkNEW_ARGS(SkLinearGradient,
- (pts, colors, pos, colorCount, mode, mapper));
+ SkGradientShaderBase::Descriptor desc;
+ desc_init(&desc, colors, pos, colorCount, mode, mapper);
+ return SkNEW_ARGS(SkLinearGradient, (pts, desc));
}
SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
@@ -664,8 +676,9 @@ SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
}
EXPAND_1_COLOR(colorCount);
- return SkNEW_ARGS(SkRadialGradient,
- (center, radius, colors, pos, colorCount, mode, mapper));
+ SkGradientShaderBase::Descriptor desc;
+ desc_init(&desc, colors, pos, colorCount, mode, mapper);
+ return SkNEW_ARGS(SkRadialGradient, (center, radius, desc));
}
SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
@@ -682,9 +695,10 @@ SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
}
EXPAND_1_COLOR(colorCount);
+ SkGradientShaderBase::Descriptor desc;
+ desc_init(&desc, colors, pos, colorCount, mode, mapper);
return SkNEW_ARGS(SkTwoPointRadialGradient,
- (start, startRadius, end, endRadius, colors, pos,
- colorCount, mode, mapper));
+ (start, startRadius, end, endRadius, desc));
}
SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
@@ -704,21 +718,24 @@ SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
}
EXPAND_1_COLOR(colorCount);
+ SkGradientShaderBase::Descriptor desc;
+ desc_init(&desc, colors, pos, colorCount, mode, mapper);
return SkNEW_ARGS(SkTwoPointConicalGradient,
- (start, startRadius, end, endRadius, colors, pos,
- colorCount, mode, mapper));
+ (start, startRadius, end, endRadius, desc));
}
SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy,
const SkColor colors[],
const SkScalar pos[],
- int count, SkUnitMapper* mapper) {
- if (NULL == colors || count < 1) {
+ int colorCount, SkUnitMapper* mapper) {
+ if (NULL == colors || colorCount < 1) {
return NULL;
}
- EXPAND_1_COLOR(count);
+ EXPAND_1_COLOR(colorCount);
- return SkNEW_ARGS(SkSweepGradient, (cx, cy, colors, pos, count, mapper));
+ SkGradientShaderBase::Descriptor desc;
+ desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper);
+ return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc));
}
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
diff --git a/src/effects/gradients/SkGradientShaderPriv.h b/src/effects/gradients/SkGradientShaderPriv.h
index b9dbf1b87b..2f41bebbce 100644
--- a/src/effects/gradients/SkGradientShaderPriv.h
+++ b/src/effects/gradients/SkGradientShaderPriv.h
@@ -82,8 +82,21 @@ static const TileProc gTileProcs[] = {
class SkGradientShaderBase : public SkShader {
public:
- SkGradientShaderBase(const SkColor colors[], const SkScalar pos[],
- int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper);
+ struct Descriptor {
+ Descriptor() {
+ sk_bzero(this, sizeof(*this));
+ fTileMode = SkShader::kClamp_TileMode;
+ }
+
+ const SkColor* fColors;
+ const SkScalar* fPos;
+ int fCount;
+ SkShader::TileMode fTileMode;
+ SkUnitMapper* fMapper;
+ };
+
+public:
+ SkGradientShaderBase(const Descriptor& desc);
virtual ~SkGradientShaderBase();
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
diff --git a/src/effects/gradients/SkLinearGradient.cpp b/src/effects/gradients/SkLinearGradient.cpp
index e0f216c220..4bc697d904 100644
--- a/src/effects/gradients/SkLinearGradient.cpp
+++ b/src/effects/gradients/SkLinearGradient.cpp
@@ -62,13 +62,8 @@ static void pts_to_unit_matrix(const SkPoint pts[2], SkMatrix* matrix) {
///////////////////////////////////////////////////////////////////////////////
-SkLinearGradient::SkLinearGradient(const SkPoint pts[2],
- const SkColor colors[],
- const SkScalar pos[],
- int colorCount,
- SkShader::TileMode mode,
- SkUnitMapper* mapper)
- : SkGradientShaderBase(colors, pos, colorCount, mode, mapper)
+SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc)
+ : SkGradientShaderBase(desc)
, fStart(pts[0])
, fEnd(pts[1]) {
pts_to_unit_matrix(pts, &fPtsToUnit);
diff --git a/src/effects/gradients/SkLinearGradient.h b/src/effects/gradients/SkLinearGradient.h
index ff1796bd1f..24c6caba7b 100644
--- a/src/effects/gradients/SkLinearGradient.h
+++ b/src/effects/gradients/SkLinearGradient.h
@@ -13,9 +13,7 @@
class SkLinearGradient : public SkGradientShaderBase {
public:
- SkLinearGradient(const SkPoint pts[2],
- const SkColor colors[], const SkScalar pos[], int colorCount,
- SkShader::TileMode mode, SkUnitMapper* mapper);
+ SkLinearGradient(const SkPoint pts[2], const Descriptor&);
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
diff --git a/src/effects/gradients/SkRadialGradient.cpp b/src/effects/gradients/SkRadialGradient.cpp
index 3fce9c441c..ca65969995 100644
--- a/src/effects/gradients/SkRadialGradient.cpp
+++ b/src/effects/gradients/SkRadialGradient.cpp
@@ -153,9 +153,8 @@ void shadeSpan16_radial_repeat(SkScalar sfx, SkScalar sdx,
/////////////////////////////////////////////////////////////////////
SkRadialGradient::SkRadialGradient(const SkPoint& center, SkScalar radius,
- const SkColor colors[], const SkScalar pos[], int colorCount,
- SkShader::TileMode mode, SkUnitMapper* mapper)
- : SkGradientShaderBase(colors, pos, colorCount, mode, mapper),
+ const Descriptor& desc)
+ : SkGradientShaderBase(desc),
fCenter(center),
fRadius(radius)
{
diff --git a/src/effects/gradients/SkRadialGradient.h b/src/effects/gradients/SkRadialGradient.h
index 83f79aef94..fa0a969cf2 100644
--- a/src/effects/gradients/SkRadialGradient.h
+++ b/src/effects/gradients/SkRadialGradient.h
@@ -13,9 +13,7 @@
class SkRadialGradient : public SkGradientShaderBase {
public:
- SkRadialGradient(const SkPoint& center, SkScalar radius,
- const SkColor colors[], const SkScalar pos[], int colorCount,
- SkShader::TileMode mode, SkUnitMapper* mapper);
+ SkRadialGradient(const SkPoint& center, SkScalar radius, const Descriptor&);
virtual void shadeSpan(int x, int y, SkPMColor* dstC, int count)
SK_OVERRIDE;
virtual void shadeSpan16(int x, int y, uint16_t* dstCParam,
diff --git a/src/effects/gradients/SkSweepGradient.cpp b/src/effects/gradients/SkSweepGradient.cpp
index 1e6b642260..51433afbca 100644
--- a/src/effects/gradients/SkSweepGradient.cpp
+++ b/src/effects/gradients/SkSweepGradient.cpp
@@ -8,12 +8,15 @@
#include "SkSweepGradient.h"
-SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, const SkColor colors[],
- const SkScalar pos[], int count, SkUnitMapper* mapper)
-: SkGradientShaderBase(colors, pos, count, SkShader::kClamp_TileMode, mapper),
+SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy,
+ const Descriptor& desc)
+: SkGradientShaderBase(desc),
fCenter(SkPoint::Make(cx, cy))
{
fPtsToUnit.setTranslate(-cx, -cy);
+
+ // overwrite the tilemode to a canonical value (since sweep ignores it)
+ fTileMode = SkShader::kClamp_TileMode;
}
SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap,
diff --git a/src/effects/gradients/SkSweepGradient.h b/src/effects/gradients/SkSweepGradient.h
index d57242989d..8b685bc209 100644
--- a/src/effects/gradients/SkSweepGradient.h
+++ b/src/effects/gradients/SkSweepGradient.h
@@ -13,8 +13,7 @@
class SkSweepGradient : public SkGradientShaderBase {
public:
- SkSweepGradient(SkScalar cx, SkScalar cy, const SkColor colors[],
- const SkScalar pos[], int count, SkUnitMapper* mapper);
+ SkSweepGradient(SkScalar cx, SkScalar cy, const Descriptor&);
virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
diff --git a/src/effects/gradients/SkTwoPointConicalGradient.cpp b/src/effects/gradients/SkTwoPointConicalGradient.cpp
index abd974b6fa..0066942200 100644
--- a/src/effects/gradients/SkTwoPointConicalGradient.cpp
+++ b/src/effects/gradients/SkTwoPointConicalGradient.cpp
@@ -177,10 +177,8 @@ void SkTwoPointConicalGradient::init() {
SkTwoPointConicalGradient::SkTwoPointConicalGradient(
const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
- const SkColor colors[], const SkScalar pos[],
- int colorCount, SkShader::TileMode mode,
- SkUnitMapper* mapper)
- : SkGradientShaderBase(colors, pos, colorCount, mode, mapper),
+ const Descriptor& desc)
+ : SkGradientShaderBase(desc),
fCenter1(start),
fCenter2(end),
fRadius1(startRadius),
diff --git a/src/effects/gradients/SkTwoPointConicalGradient.h b/src/effects/gradients/SkTwoPointConicalGradient.h
index 0807ae3201..1358f0b2ac 100644
--- a/src/effects/gradients/SkTwoPointConicalGradient.h
+++ b/src/effects/gradients/SkTwoPointConicalGradient.h
@@ -47,9 +47,7 @@ class SkTwoPointConicalGradient : public SkGradientShaderBase {
public:
SkTwoPointConicalGradient(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
- const SkColor colors[], const SkScalar pos[],
- int colorCount, SkShader::TileMode mode,
- SkUnitMapper* mapper);
+ const Descriptor&);
virtual void shadeSpan(int x, int y, SkPMColor* dstCParam,
int count) SK_OVERRIDE;
diff --git a/src/effects/gradients/SkTwoPointRadialGradient.cpp b/src/effects/gradients/SkTwoPointRadialGradient.cpp
index f70b67d479..989c139556 100644
--- a/src/effects/gradients/SkTwoPointRadialGradient.cpp
+++ b/src/effects/gradients/SkTwoPointRadialGradient.cpp
@@ -170,10 +170,8 @@ void shadeSpan_twopoint_repeat(SkScalar fx, SkScalar dx,
SkTwoPointRadialGradient::SkTwoPointRadialGradient(
const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
- const SkColor colors[], const SkScalar pos[],
- int colorCount, SkShader::TileMode mode,
- SkUnitMapper* mapper)
- : SkGradientShaderBase(colors, pos, colorCount, mode, mapper),
+ const Descriptor& desc)
+ : SkGradientShaderBase(desc),
fCenter1(start),
fCenter2(end),
fRadius1(startRadius),
diff --git a/src/effects/gradients/SkTwoPointRadialGradient.h b/src/effects/gradients/SkTwoPointRadialGradient.h
index e82fc75c93..444c23dd70 100644
--- a/src/effects/gradients/SkTwoPointRadialGradient.h
+++ b/src/effects/gradients/SkTwoPointRadialGradient.h
@@ -15,9 +15,7 @@ class SkTwoPointRadialGradient : public SkGradientShaderBase {
public:
SkTwoPointRadialGradient(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
- const SkColor colors[], const SkScalar pos[],
- int colorCount, SkShader::TileMode mode,
- SkUnitMapper* mapper);
+ const Descriptor&);
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,