diff options
author | Derek Sollenberger <djsollen@google.com> | 2011-05-16 13:07:43 -0400 |
---|---|---|
committer | Derek Sollenberger <djsollen@google.com> | 2011-05-17 07:52:26 -0400 |
commit | 35e2e62b55598210f6999fc2ea26ff8f41446ffe (patch) | |
tree | 4fca2ca7d02d58e56d9b146b770ecb1e8da8d8af /src/effects | |
parent | f173507281c41ccde5f0ef849cd720639d7e25d0 (diff) | |
download | external_skia-35e2e62b55598210f6999fc2ea26ff8f41446ffe.zip external_skia-35e2e62b55598210f6999fc2ea26ff8f41446ffe.tar.gz external_skia-35e2e62b55598210f6999fc2ea26ff8f41446ffe.tar.bz2 |
Skia Merge (revision 1327)
Change-Id: I46f41274d07a3d7bac4728f8841c7f5e89dc9181
Diffstat (limited to 'src/effects')
-rw-r--r-- | src/effects/SkAvoidXfermode.cpp | 48 | ||||
-rw-r--r-- | src/effects/SkColorFilters.cpp | 224 | ||||
-rw-r--r-- | src/effects/SkColorMatrixFilter.cpp | 53 | ||||
-rw-r--r-- | src/effects/SkCornerPathEffect.cpp | 130 | ||||
-rw-r--r-- | src/effects/SkDashPathEffect.cpp | 79 | ||||
-rw-r--r-- | src/effects/SkDiscretePathEffect.cpp | 36 | ||||
-rw-r--r-- | src/effects/SkEmbossMask.cpp | 48 | ||||
-rw-r--r-- | src/effects/SkEmbossMaskFilter.cpp | 67 | ||||
-rw-r--r-- | src/effects/SkGradientShader.cpp | 471 | ||||
-rw-r--r-- | src/effects/SkKernel33MaskFilter.cpp | 60 | ||||
-rw-r--r-- | src/effects/SkLayerDrawLooper.cpp | 26 | ||||
-rw-r--r-- | src/effects/SkLayerRasterizer.cpp | 73 | ||||
-rw-r--r-- | src/effects/SkPaintFlagsDrawFilter.cpp | 16 | ||||
-rw-r--r-- | src/effects/SkPixelXorXfermode.cpp | 36 | ||||
-rw-r--r-- | src/effects/SkPorterDuff.cpp | 16 | ||||
-rw-r--r-- | src/effects/SkRectShape.cpp | 16 | ||||
-rw-r--r-- | src/effects/SkTableMaskFilter.cpp | 16 | ||||
-rw-r--r-- | src/effects/SkTransparentShader.cpp | 124 |
18 files changed, 923 insertions, 616 deletions
diff --git a/src/effects/SkAvoidXfermode.cpp b/src/effects/SkAvoidXfermode.cpp index 97bfeae..de3fe28 100644 --- a/src/effects/SkAvoidXfermode.cpp +++ b/src/effects/SkAvoidXfermode.cpp @@ -2,16 +2,16 @@ ** ** Copyright 2006, The Android Open Source Project ** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at ** -** http://www.apache.org/licenses/LICENSE-2.0 +** http://www.apache.org/licenses/LICENSE-2.0 ** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and ** limitations under the License. */ @@ -19,7 +19,7 @@ #include "SkColorPriv.h" SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) -{ +{ if (tolerance > 255) { tolerance = 255; } @@ -66,7 +66,7 @@ static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) unsigned dr = SkAbs32(SkGetPackedR16(c) - r); unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS); unsigned db = SkAbs32(SkGetPackedB16(c) - b); - + return SkMax32(dr, SkMax32(dg, db)); } @@ -76,11 +76,11 @@ static unsigned color_dist4444(uint16_t c, unsigned r, unsigned g, unsigned b) SkASSERT(r <= 0xF); SkASSERT(g <= 0xF); SkASSERT(b <= 0xF); - + unsigned dr = SkAbs32(SkGetPackedR4444(c) - r); unsigned dg = SkAbs32(SkGetPackedG4444(c) - g); unsigned db = SkAbs32(SkGetPackedB4444(c) - b); - + return SkMax32(dr, SkMax32(dg, db)); } @@ -94,7 +94,7 @@ static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) unsigned dr = SkAbs32(SkGetPackedR32(c) - r); unsigned dg = SkAbs32(SkGetPackedG32(c) - g); unsigned db = SkAbs32(SkGetPackedB32(c) - b); - + return SkMax32(dr, SkMax32(dg, db)); } @@ -128,9 +128,9 @@ void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count, unsigned opB = SkColorGetB(fOpColor); uint32_t mul = fDistMul; uint32_t sub = (fDistMul - (1 << 14)) << 8; - + int MAX, mask; - + if (kTargetColor_Mode == fMode) { mask = -1; MAX = 255; @@ -138,17 +138,17 @@ void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count, mask = 0; MAX = 0; } - + for (int i = 0; i < count; i++) { int d = color_dist32(dst[i], opR, opG, opB); // now reverse d if we need to d = MAX + (d ^ mask) - mask; SkASSERT((unsigned)d <= 255); d = Accurate255To256(d); - + d = scale_dist_14(d, mul, sub); SkASSERT(d <= 256); - + if (d > 0) { if (NULL != aa) { d = SkAlphaMul(d, Accurate255To256(*aa++)); @@ -181,7 +181,7 @@ void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count, uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS; int MAX, mask; - + if (kTargetColor_Mode == fMode) { mask = -1; MAX = 31; @@ -220,9 +220,9 @@ void SkAvoidXfermode::xfer4444(uint16_t dst[], const SkPMColor src[], int count, unsigned opB = SkColorGetB(fOpColor) >> 4; uint32_t mul = fDistMul; uint32_t sub = (fDistMul - (1 << 14)) << 4; - + int MAX, mask; - + if (kTargetColor_Mode == fMode) { mask = -1; MAX = 15; @@ -230,7 +230,7 @@ void SkAvoidXfermode::xfer4444(uint16_t dst[], const SkPMColor src[], int count, mask = 0; MAX = 0; } - + for (int i = 0; i < count; i++) { int d = color_dist4444(dst[i], opR, opG, opB); // now reverse d if we need to @@ -240,7 +240,7 @@ void SkAvoidXfermode::xfer4444(uint16_t dst[], const SkPMColor src[], int count, d += d >> 3; d = scale_dist_14(d, mul, sub); SkASSERT(d <= 16); - + if (d > 0) { if (NULL != aa) { d = SkAlphaMul(d, Accurate255To256(*aa++)); @@ -258,3 +258,5 @@ void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count, co // override in subclass } +static SkFlattenable::Registrar + gSkAvoidXfermodeReg("SkAvoidXfermode", SkAvoidXfermode::CreateProc); diff --git a/src/effects/SkColorFilters.cpp b/src/effects/SkColorFilters.cpp index a396d35..0f8e227 100644 --- a/src/effects/SkColorFilters.cpp +++ b/src/effects/SkColorFilters.cpp @@ -19,26 +19,70 @@ #include "SkColorPriv.h" #include "SkUtils.h" -// common baseclass -class Sk_XfermodeColorFilter : public SkColorFilter { -protected: - Sk_XfermodeColorFilter(SkColor color) - : fPMColor(SkPreMultiplyColor(color)) {} +#define ILLEGAL_XFERMODE_MODE ((SkXfermode::Mode)-1) + +// baseclass for filters that store a color and mode +class SkModeColorFilter : public SkColorFilter { +public: + SkModeColorFilter(SkColor color) { + fColor = color; + fMode = ILLEGAL_XFERMODE_MODE; + + fPMColor = SkPreMultiplyColor(fColor); + } + + SkModeColorFilter(SkColor color, SkXfermode::Mode mode) { + fColor = color; + fMode = mode; + + fPMColor = SkPreMultiplyColor(fColor); + }; + + virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode) { + if (ILLEGAL_XFERMODE_MODE == fMode) { + return false; + } + + if (color) { + *color = fColor; + } + if (mode) { + *mode = fMode; + } + return true; + } + SkColor getColor() const { return fColor; } + SkXfermode::Mode getMode() const { return fMode; } + bool isModeValid() const { return ILLEGAL_XFERMODE_MODE != fMode; } + +protected: virtual void flatten(SkFlattenableWriteBuffer& buffer) { - buffer.write32(fPMColor); + this->INHERITED::flatten(buffer); + buffer.write32(fColor); + buffer.write32(fMode); } - Sk_XfermodeColorFilter(SkFlattenableReadBuffer& buffer) { - fPMColor = buffer.readU32(); + SkModeColorFilter(SkFlattenableReadBuffer& buffer) { + fColor = buffer.readU32(); + fMode = (SkXfermode::Mode)buffer.readU32(); + + fPMColor = SkPreMultiplyColor(fColor); } + // cache of fColor in premultiply space SkPMColor fPMColor; + +private: + SkColor fColor; + SkXfermode::Mode fMode; + + typedef SkColorFilter INHERITED; }; -class SkSrc_XfermodeColorFilter : public Sk_XfermodeColorFilter { +class Src_SkModeColorFilter : public SkModeColorFilter { public: - SkSrc_XfermodeColorFilter(SkColor color) : INHERITED(color) {} + Src_SkModeColorFilter(SkColor color) : INHERITED(color, SkXfermode::kSrc_Mode) {} virtual uint32_t getFlags() { if (SkGetPackedA32(fPMColor) == 0xFF) { @@ -59,24 +103,26 @@ public: sk_memset16(result, SkPixel32ToPixel16(fPMColor), count); } + static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { + return SkNEW_ARGS(Src_SkModeColorFilter, (buffer)); + } + protected: virtual Factory getFactory() { return CreateProc; } - SkSrc_XfermodeColorFilter(SkFlattenableReadBuffer& buffer) + Src_SkModeColorFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} - -private: - static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { - return SkNEW_ARGS(SkSrc_XfermodeColorFilter, (buffer)); - } - typedef Sk_XfermodeColorFilter INHERITED; +private: + typedef SkModeColorFilter INHERITED; }; -class SkSrcOver_XfermodeColorFilter : public Sk_XfermodeColorFilter { +class SrcOver_SkModeColorFilter : public SkModeColorFilter { public: - SkSrcOver_XfermodeColorFilter(SkColor color) - : INHERITED(color), fColor32Proc(SkBlitRow::ColorProcFactory()) {} + SrcOver_SkModeColorFilter(SkColor color) + : INHERITED(color, SkXfermode::kSrcOver_Mode) { + fColor32Proc = NULL; + } virtual uint32_t getFlags() { if (SkGetPackedA32(fPMColor) == 0xFF) { @@ -85,9 +131,12 @@ public: return 0; } } - + virtual void filterSpan(const SkPMColor shader[], int count, SkPMColor result[]) { + if (NULL == fColor32Proc) { + fColor32Proc = SkBlitRow::ColorProcFactory(); + } fColor32Proc(result, shader, count, fPMColor); } @@ -96,32 +145,40 @@ public: SkASSERT(this->getFlags() & kHasFilter16_Flag); sk_memset16(result, SkPixel32ToPixel16(fPMColor), count); } - + + static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { + return SkNEW_ARGS(SrcOver_SkModeColorFilter, (buffer)); + } + protected: virtual Factory getFactory() { return CreateProc; } - - SkSrcOver_XfermodeColorFilter(SkFlattenableReadBuffer& buffer) - : INHERITED(buffer), fColor32Proc(SkBlitRow::ColorProcFactory()) {} - + + SrcOver_SkModeColorFilter(SkFlattenableReadBuffer& buffer) + : INHERITED(buffer), fColor32Proc(NULL) {} + private: - static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { - return SkNEW_ARGS(SkSrcOver_XfermodeColorFilter, (buffer)); - } - - typedef Sk_XfermodeColorFilter INHERITED; + SkBlitRow::ColorProc fColor32Proc; + + typedef SkModeColorFilter INHERITED; }; -////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// -class SkXfermodeColorFilter : public Sk_XfermodeColorFilter { +class Proc_SkModeColorFilter : public SkModeColorFilter { public: - SkXfermodeColorFilter(SkColor color, SkXfermodeProc proc, - SkXfermodeProc16 proc16) : INHERITED(color) { + Proc_SkModeColorFilter(SkColor color, SkXfermode::Mode mode) : INHERITED(color, mode) { + fProc = SkXfermode::GetProc(mode); + fProc16 = SkXfermode::GetProc16(mode, color); + } + + Proc_SkModeColorFilter(SkColor color, + SkXfermodeProc proc, SkXfermodeProc16 proc16) + : INHERITED(color, ILLEGAL_XFERMODE_MODE) { fProc = proc; fProc16 = proc16; } - + virtual uint32_t getFlags() { return fProc16 ? (kAlphaUnchanged_Flag | kHasFilter16_Flag) : 0; } @@ -135,50 +192,51 @@ public: result[i] = proc(color, shader[i]); } } - + virtual void filterSpan16(const uint16_t shader[], int count, uint16_t result[]) { SkASSERT(this->getFlags() & kHasFilter16_Flag); - + SkPMColor color = fPMColor; SkXfermodeProc16 proc16 = fProc16; - + for (int i = 0; i < count; i++) { result[i] = proc16(color, shader[i]); } } - + + static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { + return SkNEW_ARGS(Proc_SkModeColorFilter, (buffer)); + } + protected: virtual void flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); buffer.writeFunctionPtr((void*)fProc); buffer.writeFunctionPtr((void*)fProc16); } - - virtual Factory getFactory() { + + virtual Factory getFactory() { return CreateProc; } - SkXfermodeColorFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { + Proc_SkModeColorFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { fProc = (SkXfermodeProc) buffer.readFunctionPtr(); fProc16 = (SkXfermodeProc16) buffer.readFunctionPtr(); } -private: - static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { - return SkNEW_ARGS(SkXfermodeColorFilter, (buffer)); - } +private: SkXfermodeProc fProc; SkXfermodeProc16 fProc16; - - typedef Sk_XfermodeColorFilter INHERITED; + + typedef SkModeColorFilter INHERITED; }; SkColorFilter* SkColorFilter::CreateProcFilter(SkColor color, SkXfermodeProc proc, SkXfermodeProc16 proc16) { return proc ? - SkNEW_ARGS(SkXfermodeColorFilter, (color, proc, proc16)) : + SkNEW_ARGS(Proc_SkModeColorFilter, (color, proc, proc16)) : NULL; } @@ -213,15 +271,14 @@ SkColorFilter* SkColorFilter::CreateModeFilter(SkColor color, (0xFF == alpha && SkXfermode::kDstIn_Mode == mode)) { return NULL; } - + switch (mode) { - case SkXfermode::kSrc_Mode: - return SkNEW_ARGS(SkSrc_XfermodeColorFilter, (color)); - case SkXfermode::kSrcOver_Mode: - return SkNEW_ARGS(SkSrcOver_XfermodeColorFilter, (color)); - default: - return SkColorFilter::CreateProcFilter(color, SkXfermode::GetProc(mode), - SkXfermode::GetProc16(mode, color)); + case SkXfermode::kSrc_Mode: + return SkNEW_ARGS(Src_SkModeColorFilter, (color)); + case SkXfermode::kSrcOver_Mode: + return SkNEW_ARGS(SrcOver_SkModeColorFilter, (color)); + default: + return SkNEW_ARGS(Proc_SkModeColorFilter, (color, mode)); } } @@ -254,7 +311,7 @@ public: unsigned scaleR = SkAlpha255To256(SkColorGetR(fMul)); unsigned scaleG = SkAlpha255To256(SkColorGetG(fMul)); unsigned scaleB = SkAlpha255To256(SkColorGetB(fMul)); - + unsigned addR = SkColorGetR(fAdd); unsigned addG = SkColorGetG(fAdd); unsigned addB = SkColorGetB(fAdd); @@ -263,7 +320,7 @@ public: SkPMColor c = shader[i]; if (c) { unsigned a = SkGetPackedA32(c); - unsigned scaleA = SkAlpha255To256(a); + unsigned scaleA = SkAlpha255To256(a); unsigned r = pin(SkAlphaMul(SkGetPackedR32(c), scaleR) + SkAlphaMul(addR, scaleA), a); unsigned g = pin(SkAlphaMul(SkGetPackedG32(c), scaleG) + SkAlphaMul(addG, scaleA), a); unsigned b = pin(SkAlphaMul(SkGetPackedB32(c), scaleB) + SkAlphaMul(addB, scaleA), a); @@ -275,10 +332,11 @@ public: protected: virtual void flatten(SkFlattenableWriteBuffer& buffer) { + this->INHERITED::flatten(buffer); buffer.write32(fMul); buffer.write32(fAdd); } - + virtual Factory getFactory() { return CreateProc; } @@ -287,13 +345,15 @@ protected: fMul = buffer.readU32(); fAdd = buffer.readU32(); } - + SkColor fMul, fAdd; private: static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkLightingColorFilter, (buffer)); } + + typedef SkColorFilter INHERITED; }; class SkLightingColorFilter_JustAdd : public SkLightingColorFilter { @@ -311,7 +371,7 @@ public: SkPMColor c = shader[i]; if (c) { unsigned a = SkGetPackedA32(c); - unsigned scaleA = SkAlpha255To256(a); + unsigned scaleA = SkAlpha255To256(a); unsigned r = pin(SkGetPackedR32(c) + SkAlphaMul(addR, scaleA), a); unsigned g = pin(SkGetPackedG32(c) + SkAlphaMul(addG, scaleA), a); unsigned b = pin(SkGetPackedB32(c) + SkAlphaMul(addB, scaleA), a); @@ -331,6 +391,7 @@ private: static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkLightingColorFilter_JustAdd, (buffer)); } + typedef SkLightingColorFilter INHERITED; }; @@ -344,7 +405,7 @@ public: unsigned scaleR = SkAlpha255To256(SkColorGetR(fMul)); unsigned scaleG = SkAlpha255To256(SkColorGetG(fMul)); unsigned scaleB = SkAlpha255To256(SkColorGetB(fMul)); - + for (int i = 0; i < count; i++) { SkPMColor c = shader[i]; if (c) { @@ -363,7 +424,7 @@ protected: SkLightingColorFilter_JustMul(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} - + private: static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkLightingColorFilter_JustMul, (buffer)); @@ -382,7 +443,7 @@ public: SkASSERT(SkColorGetR(mul) == SkColorGetG(mul)); SkASSERT(SkColorGetR(mul) == SkColorGetB(mul)); } - + virtual uint32_t getFlags() { return this->INHERITED::getFlags() | (kAlphaUnchanged_Flag | kHasFilter16_Flag); } @@ -409,7 +470,7 @@ private: static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkLightingColorFilter_SingleMul, (buffer)); } - + typedef SkLightingColorFilter INHERITED; }; @@ -417,22 +478,22 @@ class SkLightingColorFilter_NoPin : public SkLightingColorFilter { public: SkLightingColorFilter_NoPin(SkColor mul, SkColor add) : INHERITED(mul, add) {} - + virtual void filterSpan(const SkPMColor shader[], int count, SkPMColor result[]) { unsigned scaleR = SkAlpha255To256(SkColorGetR(fMul)); unsigned scaleG = SkAlpha255To256(SkColorGetG(fMul)); unsigned scaleB = SkAlpha255To256(SkColorGetB(fMul)); - + unsigned addR = SkColorGetR(fAdd); unsigned addG = SkColorGetG(fAdd); unsigned addB = SkColorGetB(fAdd); - + for (int i = 0; i < count; i++) { SkPMColor c = shader[i]; if (c) { unsigned a = SkGetPackedA32(c); - unsigned scaleA = SkAlpha255To256(a); + unsigned scaleA = SkAlpha255To256(a); unsigned r = SkAlphaMul(SkGetPackedR32(c), scaleR) + SkAlphaMul(addR, scaleA); unsigned g = SkAlphaMul(SkGetPackedG32(c), scaleG) + SkAlphaMul(addG, scaleA); unsigned b = SkAlphaMul(SkGetPackedB32(c), scaleB) + SkAlphaMul(addB, scaleA); @@ -441,18 +502,18 @@ public: result[i] = c; } } - + protected: virtual Factory getFactory() { return CreateProc; } - + SkLightingColorFilter_NoPin(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} - + private: static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkLightingColorFilter_NoPin, (buffer)); } - + typedef SkLightingColorFilter INHERITED; }; @@ -467,7 +528,7 @@ protected: } virtual void flatten(SkFlattenableWriteBuffer& buffer) {} - + virtual Factory getFactory() { return CreateProc; } @@ -507,3 +568,14 @@ SkColorFilter* SkColorFilter::CreateLightingFilter(SkColor mul, SkColor add) { return SkNEW_ARGS(SkLightingColorFilter, (mul, add)); } +static SkFlattenable::Registrar + gSrcColorFilterReg("Src_SkModeColorFilterReg", + Src_SkModeColorFilter::CreateProc); + +static SkFlattenable::Registrar + gSrcOverColorFilterReg("SrcOver_SkModeColorFilterReg", + SrcOver_SkModeColorFilter::CreateProc); + +static SkFlattenable::Registrar + gProcColorFilterReg("Proc_SkModeColorFilterReg", + Proc_SkModeColorFilter::CreateProc); diff --git a/src/effects/SkColorMatrixFilter.cpp b/src/effects/SkColorMatrixFilter.cpp index 07c8d2f..9270052 100644 --- a/src/effects/SkColorMatrixFilter.cpp +++ b/src/effects/SkColorMatrixFilter.cpp @@ -18,7 +18,7 @@ static void General(SkColorMatrixFilter::State* state, const int32_t* SK_RESTRICT array = state->fArray; const int shift = state->fShift; int32_t* SK_RESTRICT result = state->fResult; - + result[0] = rowmul4(&array[0], r, g, b, a) >> shift; result[1] = rowmul4(&array[5], r, g, b, a) >> shift; result[2] = rowmul4(&array[10], r, g, b, a) >> shift; @@ -29,7 +29,7 @@ static void General16(SkColorMatrixFilter::State* state, unsigned r, unsigned g, unsigned b, unsigned a) { const int32_t* SK_RESTRICT array = state->fArray; int32_t* SK_RESTRICT result = state->fResult; - + result[0] = rowmul4(&array[0], r, g, b, a) >> 16; result[1] = rowmul4(&array[5], r, g, b, a) >> 16; result[2] = rowmul4(&array[10], r, g, b, a) >> 16; @@ -41,7 +41,7 @@ static void AffineAdd(SkColorMatrixFilter::State* state, const int32_t* SK_RESTRICT array = state->fArray; const int shift = state->fShift; int32_t* SK_RESTRICT result = state->fResult; - + result[0] = rowmul3(&array[0], r, g, b) >> shift; result[1] = rowmul3(&array[5], r, g, b) >> shift; result[2] = rowmul3(&array[10], r, g, b) >> shift; @@ -52,7 +52,7 @@ static void AffineAdd16(SkColorMatrixFilter::State* state, unsigned r, unsigned g, unsigned b, unsigned a) { const int32_t* SK_RESTRICT array = state->fArray; int32_t* SK_RESTRICT result = state->fResult; - + result[0] = rowmul3(&array[0], r, g, b) >> 16; result[1] = rowmul3(&array[5], r, g, b) >> 16; result[2] = rowmul3(&array[10], r, g, b) >> 16; @@ -64,7 +64,7 @@ static void ScaleAdd(SkColorMatrixFilter::State* state, const int32_t* SK_RESTRICT array = state->fArray; const int shift = state->fShift; int32_t* SK_RESTRICT result = state->fResult; - + // cast to (int) to keep the expression signed for the shift result[0] = (array[0] * (int)r + array[4]) >> shift; result[1] = (array[6] * (int)g + array[9]) >> shift; @@ -76,7 +76,7 @@ static void ScaleAdd16(SkColorMatrixFilter::State* state, unsigned r, unsigned g, unsigned b, unsigned a) { const int32_t* SK_RESTRICT array = state->fArray; int32_t* SK_RESTRICT result = state->fResult; - + // cast to (int) to keep the expression signed for the shift result[0] = (array[0] * (int)r + array[4]) >> 16; result[1] = (array[6] * (int)g + array[9]) >> 16; @@ -89,7 +89,7 @@ static void Add(SkColorMatrixFilter::State* state, const int32_t* SK_RESTRICT array = state->fArray; const int shift = state->fShift; int32_t* SK_RESTRICT result = state->fResult; - + result[0] = r + (array[4] >> shift); result[1] = g + (array[9] >> shift); result[2] = b + (array[14] >> shift); @@ -100,7 +100,7 @@ static void Add16(SkColorMatrixFilter::State* state, unsigned r, unsigned g, unsigned b, unsigned a) { const int32_t* SK_RESTRICT array = state->fArray; int32_t* SK_RESTRICT result = state->fResult; - + result[0] = r + (array[4] >> 16); result[1] = g + (array[9] >> 16); result[2] = b + (array[14] >> 16); @@ -117,7 +117,7 @@ void SkColorMatrixFilter::setup(const SkScalar SK_RESTRICT src[20]) { // fState is undefined, but that is OK, since we shouldn't look at it return; } - + int32_t* SK_RESTRICT array = fState.fArray; int i; @@ -129,7 +129,7 @@ void SkColorMatrixFilter::setup(const SkScalar SK_RESTRICT src[20]) { value = SkAbs32(value); max = SkMax32(max, value); } - + /* All of fArray[] values must fit in 23 bits, to safely allow me to multiply them by 8bit unsigned values, and get a signed answer without overflow. This means clz needs to be 9 or bigger @@ -146,7 +146,7 @@ void SkColorMatrixFilter::setup(const SkScalar SK_RESTRICT src[20]) { } one >>= bits; } - + // check if we have to munge Alpha int32_t changesAlpha = (array[15] | array[16] | array[17] | (array[18] - one) | array[19]); @@ -224,36 +224,36 @@ void SkColorMatrixFilter::filterSpan(const SkPMColor src[], int count, Proc proc = fProc; State* state = &fState; int32_t* SK_RESTRICT result = state->fResult; - + if (NULL == proc) { if (src != dst) { memcpy(dst, src, count * sizeof(SkPMColor)); } return; } - + const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); for (int i = 0; i < count; i++) { SkPMColor c = src[i]; - + unsigned r = SkGetPackedR32(c); unsigned g = SkGetPackedG32(c); unsigned b = SkGetPackedB32(c); unsigned a = SkGetPackedA32(c); - + // need our components to be un-premultiplied if (255 != a) { SkUnPreMultiply::Scale scale = table[a]; r = SkUnPreMultiply::ApplyScale(scale, r); g = SkUnPreMultiply::ApplyScale(scale, g); b = SkUnPreMultiply::ApplyScale(scale, b); - + SkASSERT(r <= 255); SkASSERT(g <= 255); SkASSERT(b <= 255); } - + proc(state, r, g, b, a); r = pin(result[0], SK_R32_MASK); @@ -278,7 +278,7 @@ void SkColorMatrixFilter::filterSpan16(const uint16_t src[], int count, Proc proc = fProc; State* state = &fState; int32_t* SK_RESTRICT result = state->fResult; - + if (NULL == proc) { if (src != dst) { memcpy(dst, src, count * sizeof(uint16_t)); @@ -288,18 +288,18 @@ void SkColorMatrixFilter::filterSpan16(const uint16_t src[], int count, for (int i = 0; i < count; i++) { uint16_t c = src[i]; - + // expand to 8bit components (since our matrix translate is 8bit biased unsigned r = SkPacked16ToR32(c); unsigned g = SkPacked16ToG32(c); unsigned b = SkPacked16ToB32(c); - + proc(state, r, g, b, 0); - + r = pin(result[0], SK_R32_MASK); g = pin(result[1], SK_G32_MASK); b = pin(result[2], SK_B32_MASK); - + // now packed it back down to 16bits (hmmm, could dither...) dst[i] = SkPack888ToRGB16(r, g, b); } @@ -314,17 +314,20 @@ void SkColorMatrixFilter::flatten(SkFlattenableWriteBuffer& buffer) { buffer.writeMul4(&fState, sizeof(fState)); buffer.write32(fFlags); } - + SkFlattenable::Factory SkColorMatrixFilter::getFactory() { return CreateProc; } - + SkColorMatrixFilter::SkColorMatrixFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { fProc = (Proc)buffer.readFunctionPtr(); buffer.read(&fState, sizeof(fState)); fFlags = buffer.readU32(); } - + SkFlattenable* SkColorMatrixFilter::CreateProc(SkFlattenableReadBuffer& buf) { return SkNEW_ARGS(SkColorMatrixFilter, (buf)); } +static SkFlattenable::Registrar + gSkColorMatrixFilterReg("SkColorMatrixFilter", + SkColorMatrixFilter::CreateProc); diff --git a/src/effects/SkCornerPathEffect.cpp b/src/effects/SkCornerPathEffect.cpp index 27d765f..158cefa 100644 --- a/src/effects/SkCornerPathEffect.cpp +++ b/src/effects/SkCornerPathEffect.cpp @@ -28,8 +28,8 @@ SkCornerPathEffect::~SkCornerPathEffect() { } -static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius, SkPoint* step) -{ +static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius, + SkPoint* step) { SkScalar dist = SkPoint::Distance(a, b); step->set(b.fX - a.fX, b.fY - a.fY); @@ -37,17 +37,17 @@ static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius, SkP if (dist <= radius * 2) { step->scale(SK_ScalarHalf); return false; - } - else { + } else { step->scale(SkScalarDiv(radius, dist)); return true; } } -bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) -{ - if (fRadius == 0) +bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src, + SkScalar* width) { + if (fRadius == 0) { return false; + } SkPath::Iter iter(src, false); SkPath::Verb verb, prevVerb = (SkPath::Verb)-1; @@ -65,97 +65,91 @@ bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* wi for (;;) { switch (verb = iter.next(pts)) { - case SkPath::kMove_Verb: - // close out the previous (open) contour - if (SkPath::kLine_Verb == prevVerb) { - dst->lineTo(lastCorner); - } - closed = iter.isClosedContour(); - if (closed) { - moveTo = pts[0]; - prevIsValid = false; - } - else { - dst->moveTo(pts[0]); - prevIsValid = true; - } - break; - case SkPath::kLine_Verb: - { + case SkPath::kMove_Verb: + // close out the previous (open) contour + if (SkPath::kLine_Verb == prevVerb) { + dst->lineTo(lastCorner); + } + closed = iter.isClosedContour(); + if (closed) { + moveTo = pts[0]; + prevIsValid = false; + } else { + dst->moveTo(pts[0]); + prevIsValid = true; + } + break; + case SkPath::kLine_Verb: { bool drawSegment = ComputeStep(pts[0], pts[1], fRadius, &step); // prev corner if (!prevIsValid) { dst->moveTo(moveTo + step); prevIsValid = true; - } - else { - dst->quadTo(pts[0].fX, pts[0].fY, pts[0].fX + step.fX, pts[0].fY + step.fY); + } else { + dst->quadTo(pts[0].fX, pts[0].fY, pts[0].fX + step.fX, + pts[0].fY + step.fY); } if (drawSegment) { dst->lineTo(pts[1].fX - step.fX, pts[1].fY - step.fY); } lastCorner = pts[1]; prevIsValid = true; + break; } - break; - case SkPath::kQuad_Verb: - // TBD - just replicate the curve for now - if (!prevIsValid) - { - dst->moveTo(pts[0]); - prevIsValid = true; - } - dst->quadTo(pts[1], pts[2]); - lastCorner = pts[2]; - firstStep.set(0, 0); - break; - case SkPath::kCubic_Verb: - if (!prevIsValid) - { - dst->moveTo(pts[0]); - prevIsValid = true; - } - // TBD - just replicate the curve for now - dst->cubicTo(pts[1], pts[2], pts[3]); - lastCorner = pts[3]; - firstStep.set(0, 0); - break; - case SkPath::kClose_Verb: - if (firstStep.fX || firstStep.fY) - dst->quadTo(lastCorner.fX, lastCorner.fY, - lastCorner.fX + firstStep.fX, - lastCorner.fY + firstStep.fY); - dst->close(); - break; - case SkPath::kDone_Verb: - goto DONE; + case SkPath::kQuad_Verb: + // TBD - just replicate the curve for now + if (!prevIsValid) { + dst->moveTo(pts[0]); + prevIsValid = true; + } + dst->quadTo(pts[1], pts[2]); + lastCorner = pts[2]; + firstStep.set(0, 0); + break; + case SkPath::kCubic_Verb: + if (!prevIsValid) { + dst->moveTo(pts[0]); + prevIsValid = true; + } + // TBD - just replicate the curve for now + dst->cubicTo(pts[1], pts[2], pts[3]); + lastCorner = pts[3]; + firstStep.set(0, 0); + break; + case SkPath::kClose_Verb: + if (firstStep.fX || firstStep.fY) { + dst->quadTo(lastCorner.fX, lastCorner.fY, + lastCorner.fX + firstStep.fX, + lastCorner.fY + firstStep.fY); + } + dst->close(); + break; + case SkPath::kDone_Verb: + goto DONE; } - if (SkPath::kMove_Verb == prevVerb) + if (SkPath::kMove_Verb == prevVerb) { firstStep = step; + } prevVerb = verb; } DONE: return true; } -SkFlattenable::Factory SkCornerPathEffect::getFactory() -{ +SkFlattenable::Factory SkCornerPathEffect::getFactory() { return CreateProc; } -void SkCornerPathEffect::flatten(SkFlattenableWriteBuffer& buffer) -{ +void SkCornerPathEffect::flatten(SkFlattenableWriteBuffer& buffer) { buffer.writeScalar(fRadius); } -SkFlattenable* SkCornerPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) -{ +SkFlattenable* SkCornerPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkCornerPathEffect, (buffer)); } -SkCornerPathEffect::SkCornerPathEffect(SkFlattenableReadBuffer& buffer) -{ +SkCornerPathEffect::SkCornerPathEffect(SkFlattenableReadBuffer& buffer) { fRadius = buffer.readScalar(); } diff --git a/src/effects/SkDashPathEffect.cpp b/src/effects/SkDashPathEffect.cpp index 48581b5..82f357c 100644 --- a/src/effects/SkDashPathEffect.cpp +++ b/src/effects/SkDashPathEffect.cpp @@ -19,24 +19,24 @@ #include "SkBuffer.h" #include "SkPathMeasure.h" -static inline int is_even(int x) -{ +static inline int is_even(int x) { return (~x) << 31; } -static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, int32_t* index) -{ +static SkScalar FindFirstInterval(const SkScalar intervals[], SkScalar phase, + int32_t* index) { int i; - for (i = 0; phase > intervals[i]; i++) + for (i = 0; phase > intervals[i]; i++) { phase -= intervals[i]; + } *index = i; return intervals[i] - phase; } -SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, SkScalar phase, bool scaleToFit) - : fScaleToFit(scaleToFit) -{ +SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, + SkScalar phase, bool scaleToFit) + : fScaleToFit(scaleToFit) { SkASSERT(intervals); SkASSERT(count > 1 && SkAlign2(count) == count); @@ -44,46 +44,44 @@ SkDashPathEffect::SkDashPathEffect(const SkScalar intervals[], int count, SkScal fCount = count; SkScalar len = 0; - for (int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { SkASSERT(intervals[i] >= 0); fIntervals[i] = intervals[i]; len += intervals[i]; } fIntervalLength = len; - if (len > 0) // we don't handle 0 length dash arrays - { - if (phase < 0) - { + if (len > 0) { // we don't handle 0 length dash arrays + if (phase < 0) { phase = -phase; - if (phase > len) + if (phase > len) { phase = SkScalarMod(phase, len); + } phase = len - phase; - } - else if (phase >= len) + } else if (phase >= len) { phase = SkScalarMod(phase, len); + } SkASSERT(phase >= 0 && phase < len); fInitialDashLength = FindFirstInterval(intervals, phase, &fInitialDashIndex); SkASSERT(fInitialDashLength >= 0); SkASSERT(fInitialDashIndex >= 0 && fInitialDashIndex < fCount); - } - else + } else { fInitialDashLength = -1; // signal bad dash intervals + } } -SkDashPathEffect::~SkDashPathEffect() -{ +SkDashPathEffect::~SkDashPathEffect() { sk_free(fIntervals); } -bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) -{ +bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, + SkScalar* width) { // we do nothing if the src wants to be filled, or if our dashlength is 0 - if (*width < 0 || fInitialDashLength < 0) + if (*width < 0 || fInitialDashLength < 0) { return false; + } SkPathMeasure meas(src, false); const SkScalar* intervals = fIntervals; @@ -95,12 +93,10 @@ bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* widt int index = fInitialDashIndex; SkScalar scale = SK_Scalar1; - if (fScaleToFit) - { - if (fIntervalLength >= length) + if (fScaleToFit) { + if (fIntervalLength >= length) { scale = SkScalarDiv(length, fIntervalLength); - else - { + } else { SkScalar div = SkScalarDiv(length, fIntervalLength); int n = SkScalarFloor(div); scale = SkScalarDiv(length, n * fIntervalLength); @@ -110,12 +106,10 @@ bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* widt SkScalar distance = 0; SkScalar dlen = SkScalarMul(fInitialDashLength, scale); - while (distance < length) - { + while (distance < length) { SkASSERT(dlen >= 0); addedSegment = false; - if (is_even(index) && dlen > 0 && !skipFirstSegment) - { + if (is_even(index) && dlen > 0 && !skipFirstSegment) { addedSegment = true; meas.getSegment(distance, distance + dlen, dst, true); } @@ -127,27 +121,28 @@ bool SkDashPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* widt // wrap around our intervals array if necessary index += 1; SkASSERT(index <= fCount); - if (index == fCount) + if (index == fCount) { index = 0; + } // fetch our next dlen dlen = SkScalarMul(intervals[index], scale); } // extend if we ended on a segment and we need to join up with the (skipped) initial segment - if (meas.isClosed() && is_even(fInitialDashIndex) && fInitialDashLength > 0) + if (meas.isClosed() && is_even(fInitialDashIndex) && + fInitialDashLength > 0) { meas.getSegment(0, SkScalarMul(fInitialDashLength, scale), dst, !addedSegment); + } } while (meas.nextContour()); return true; } -SkFlattenable::Factory SkDashPathEffect::getFactory() -{ +SkFlattenable::Factory SkDashPathEffect::getFactory() { return fInitialDashLength < 0 ? NULL : CreateProc; } -void SkDashPathEffect::flatten(SkFlattenableWriteBuffer& buffer) -{ +void SkDashPathEffect::flatten(SkFlattenableWriteBuffer& buffer) { SkASSERT(fInitialDashLength >= 0); buffer.write32(fCount); @@ -158,13 +153,11 @@ void SkDashPathEffect::flatten(SkFlattenableWriteBuffer& buffer) buffer.writeMul4(fIntervals, fCount * sizeof(fIntervals[0])); } -SkFlattenable* SkDashPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) -{ +SkFlattenable* SkDashPathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkDashPathEffect, (buffer)); } -SkDashPathEffect::SkDashPathEffect(SkFlattenableReadBuffer& buffer) -{ +SkDashPathEffect::SkDashPathEffect(SkFlattenableReadBuffer& buffer) { fCount = buffer.readS32(); fInitialDashIndex = buffer.readS32(); fInitialDashLength = buffer.readScalar(); diff --git a/src/effects/SkDiscretePathEffect.cpp b/src/effects/SkDiscretePathEffect.cpp index 6286045..3b8d48e 100644 --- a/src/effects/SkDiscretePathEffect.cpp +++ b/src/effects/SkDiscretePathEffect.cpp @@ -20,8 +20,7 @@ #include "SkPathMeasure.h" #include "SkRandom.h" -static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) -{ +static void Perterb(SkPoint* p, const SkVector& tangent, SkScalar scale) { SkVector normal = tangent; normal.rotateCCW(); normal.setLength(scale); @@ -34,8 +33,8 @@ SkDiscretePathEffect::SkDiscretePathEffect(SkScalar segLength, SkScalar deviatio { } -bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width) -{ +bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, + SkScalar* width) { bool doFill = *width < 0; SkPathMeasure meas(src, doFill); @@ -48,58 +47,49 @@ bool SkDiscretePathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* do { SkScalar length = meas.getLength(); - if (fSegLength * (2 + doFill) > length) - { + if (fSegLength * (2 + doFill) > length) { meas.getSegment(0, length, dst, true); // to short for us to mangle - } - else - { + } else { int n = SkScalarRound(SkScalarDiv(length, fSegLength)); SkScalar delta = length / n; SkScalar distance = 0; - if (meas.isClosed()) - { + if (meas.isClosed()) { n -= 1; distance += delta/2; } meas.getPosTan(distance, &p, &v); Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale)); dst->moveTo(p); - while (--n >= 0) - { + while (--n >= 0) { distance += delta; meas.getPosTan(distance, &p, &v); Perterb(&p, v, SkScalarMul(rand.nextSScalar1(), scale)); dst->lineTo(p); } - if (meas.isClosed()) + if (meas.isClosed()) { dst->close(); + } } } while (meas.nextContour()); return true; } -SkFlattenable::Factory SkDiscretePathEffect::getFactory() -{ +SkFlattenable::Factory SkDiscretePathEffect::getFactory() { return CreateProc; } -SkFlattenable* SkDiscretePathEffect::CreateProc(SkFlattenableReadBuffer& buffer) -{ +SkFlattenable* SkDiscretePathEffect::CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkDiscretePathEffect, (buffer)); } -void SkDiscretePathEffect::flatten(SkFlattenableWriteBuffer& buffer) -{ +void SkDiscretePathEffect::flatten(SkFlattenableWriteBuffer& buffer) { buffer.writeScalar(fSegLength); buffer.writeScalar(fPerterb); } -SkDiscretePathEffect::SkDiscretePathEffect(SkFlattenableReadBuffer& buffer) -{ +SkDiscretePathEffect::SkDiscretePathEffect(SkFlattenableReadBuffer& buffer) { fSegLength = buffer.readScalar(); fPerterb = buffer.readScalar(); } - diff --git a/src/effects/SkEmbossMask.cpp b/src/effects/SkEmbossMask.cpp index fd11f54..742ccd2 100644 --- a/src/effects/SkEmbossMask.cpp +++ b/src/effects/SkEmbossMask.cpp @@ -17,8 +17,7 @@ #include "SkEmbossMask.h" -static inline int nonzero_to_one(int x) -{ +static inline int nonzero_to_one(int x) { #if 0 return x != 0; #else @@ -26,8 +25,7 @@ static inline int nonzero_to_one(int x) #endif } -static inline int neq_to_one(int x, int max) -{ +static inline int neq_to_one(int x, int max) { #if 0 return x != max; #else @@ -36,8 +34,7 @@ static inline int neq_to_one(int x, int max) #endif } -static inline int neq_to_mask(int x, int max) -{ +static inline int neq_to_mask(int x, int max) { #if 0 return -(x != max); #else @@ -46,8 +43,7 @@ static inline int neq_to_mask(int x, int max) #endif } -static inline unsigned div255(unsigned x) -{ +static inline unsigned div255(unsigned x) { SkASSERT(x <= (255*255)); return x * ((1 << 24) / 255) >> 24; } @@ -60,28 +56,27 @@ static inline unsigned div255(unsigned x) #include <stdio.h> -void SkEmbossMask_BuildTable() -{ +void SkEmbossMask_BuildTable() { // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table FILE* file = ::fopen("SkEmbossMask_Table.h", "w"); SkASSERT(file); ::fprintf(file, "#include \"SkTypes.h\"\n\n"); ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n"); - for (int dx = 0; dx <= 255/2; dx++) - { - for (int dy = 0; dy <= 255/2; dy++) - { + for (int dx = 0; dx <= 255/2; dx++) { + for (int dy = 0; dy <= 255/2; dy++) { if ((dy & 15) == 0) ::fprintf(file, "\t"); uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4)); ::fprintf(file, "0x%04X", value); - if (dx * 128 + dy < 128*128-1) + if (dx * 128 + dy < 128*128-1) { ::fprintf(file, ", "); - if ((dy & 15) == 15) + } + if ((dy & 15) == 15) { ::fprintf(file, "\n"); + } } } ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta); @@ -90,8 +85,7 @@ void SkEmbossMask_BuildTable() #endif -void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) -{ +void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) { SkASSERT(kDelta == kDeltaUsedToBuildTable); SkASSERT(mask->fFormat == SkMask::k3D_Format); @@ -114,14 +108,11 @@ void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) int maxx = mask->fBounds.width() - 1; int prev_row = 0; - for (int y = 0; y <= maxy; y++) - { + for (int y = 0; y <= maxy; y++) { int next_row = neq_to_mask(y, maxy) & rowBytes; - for (int x = 0; x <= maxx; x++) - { - if (alpha[x]) - { + for (int x = 0; x <= maxx; x++) { + if (alpha[x]) { int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)]; int ny = alpha[x + next_row] - alpha[x - prev_row]; @@ -129,8 +120,7 @@ void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) int mul = ambient; int add = 0; - if (numer > 0) // preflight when numer/denom will be <= 0 - { + if (numer > 0) { // preflight when numer/denom will be <= 0 #if 0 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta); SkFixed dot = numer / denom; @@ -150,8 +140,7 @@ void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) // hilite = R * Eye(0, 0, 1) int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8; - if (hilite > 0) - { + if (hilite > 0) { // pin hilite to 255, since our fast math is also a little sloppy hilite = SkClampMax(hilite, 255); @@ -160,8 +149,9 @@ void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) // and then possibly cache a 256 table for a given specular // value in the light, and just pass that in to this function. add = hilite; - for (int i = specular >> 4; i > 0; --i) + for (int i = specular >> 4; i > 0; --i) { add = div255(add * hilite); + } } } multiply[x] = SkToU8(mul); diff --git a/src/effects/SkEmbossMaskFilter.cpp b/src/effects/SkEmbossMaskFilter.cpp index 9d585ff..51c19b2 100644 --- a/src/effects/SkEmbossMaskFilter.cpp +++ b/src/effects/SkEmbossMaskFilter.cpp @@ -21,22 +21,27 @@ #include "SkEmbossMask.h" #include "SkBuffer.h" +static inline int pin2byte(int n) { + if (n < 0) { + n = 0; + } else if (n > 0xFF) { + n = 0xFF; + } + return n; +} + SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], SkScalar ambient, SkScalar specular, - SkScalar blurRadius) -{ - if (direction == NULL) + SkScalar blurRadius) { + if (direction == NULL) { return NULL; + } // ambient should be 0...1 as a scalar - int am = SkScalarToFixed(ambient) >> 8; - if (am < 0) am = 0; - else if (am > 0xFF) am = 0xFF; + int am = pin2byte(SkScalarToFixed(ambient) >> 8); // specular should be 0..15.99 as a scalar - int sp = SkScalarToFixed(specular) >> 12; - if (sp < 0) sp = 0; - else if (sp > 0xFF) sp = 0xFF; + int sp = pin2byte(SkScalarToFixed(specular) >> 12); SkEmbossMaskFilter::Light light; @@ -47,41 +52,43 @@ SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], return SkNEW_ARGS(SkEmbossMaskFilter, (light, blurRadius)); } -///////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// -static void normalize(SkScalar v[3]) -{ +static void normalize(SkScalar v[3]) { SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]); mag = SkScalarSqrt(mag); - for (int i = 0; i < 3; i++) + for (int i = 0; i < 3; i++) { v[i] = SkScalarDiv(v[i], mag); + } } SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius) - : fLight(light), fBlurRadius(blurRadius) -{ + : fLight(light), fBlurRadius(blurRadius) { normalize(fLight.fDirection); } -SkMask::Format SkEmbossMaskFilter::getFormat() -{ +SkMask::Format SkEmbossMaskFilter::getFormat() { return SkMask::k3D_Format; } -bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, const SkMatrix& matrix, SkIPoint* margin) -{ +bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, + const SkMatrix& matrix, SkIPoint* margin) { SkScalar radius = matrix.mapRadius(fBlurRadius); - if (!SkBlurMask::Blur(dst, src, radius, SkBlurMask::kInner_Style, SkBlurMask::kLow_Quality)) + if (!SkBlurMask::Blur(dst, src, radius, SkBlurMask::kInner_Style, + SkBlurMask::kLow_Quality)) { return false; + } dst->fFormat = SkMask::k3D_Format; - if (margin) + if (margin) { margin->set(SkScalarCeil(radius), SkScalarCeil(radius)); + } - if (src.fImage == NULL) + if (src.fImage == NULL) { return true; + } // create a larger buffer for the other two channels (should force fBlur to do this for us) @@ -98,7 +105,8 @@ bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, const SkMatr // run the light direction through the matrix... Light light = fLight; - matrix.mapVectors((SkVector*)(void*)light.fDirection, (SkVector*)(void*)fLight.fDirection, 1); + matrix.mapVectors((SkVector*)(void*)light.fDirection, + (SkVector*)(void*)fLight.fDirection, 1); // now restore the length of the XY component // cast to SkVector so we can call setLength (this double cast silences alias warnings) @@ -115,25 +123,22 @@ bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, const SkMatr return true; } -SkFlattenable* SkEmbossMaskFilter::CreateProc(SkFlattenableReadBuffer& buffer) -{ +SkFlattenable* SkEmbossMaskFilter::CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkEmbossMaskFilter, (buffer)); } -SkFlattenable::Factory SkEmbossMaskFilter::getFactory() -{ +SkFlattenable::Factory SkEmbossMaskFilter::getFactory() { return CreateProc; } -SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer) : SkMaskFilter(buffer) -{ +SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer) + : SkMaskFilter(buffer) { buffer.read(&fLight, sizeof(fLight)); SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean fBlurRadius = buffer.readScalar(); } -void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) -{ +void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); fLight.fPad = 0; // for the font-cache lookup to be clean diff --git a/src/effects/SkGradientShader.cpp b/src/effects/SkGradientShader.cpp index dca87b0..cfe444e 100644 --- a/src/effects/SkGradientShader.cpp +++ b/src/effects/SkGradientShader.cpp @@ -27,7 +27,40 @@ #define USE_DITHER_32BIT_GRADIENT #endif -/////////////////////////////////////////////////////////////////////////// +#define SK_ENABLE_FAST_LINEAR_GRADIENTS + +#ifdef SK_ENABLE_FAST_LINEAR_GRADIENTS +static void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, + int count) { + if (count > 0) { + if (v0 == v1) { + sk_memset32(dst, v0, count); + } else { + int pairs = count >> 1; + for (int i = 0; i < pairs; i++) { + *dst++ = v0; + *dst++ = v1; + } + if (count & 1) { + *dst = v0; + } + } + } +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Can't use a two-argument function with side effects like this in a +// constructor's initializer's argument list because the order of +// evaluations in that context is undefined (and backwards on linux/gcc). +static SkPoint unflatten_point(SkReader32& buffer) { + SkPoint retval; + retval.fX = buffer.readScalar(); + retval.fY = buffer.readScalar(); + return retval; +} + +/////////////////////////////////////////////////////////////////////////////// typedef SkFixed (*TileProc)(SkFixed); @@ -50,7 +83,7 @@ static const TileProc gTileProcs[] = { mirror_tileproc }; -////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// static inline int repeat_bits(int x, const int bits) { return x & ((1 << bits) - 1); @@ -83,7 +116,7 @@ static inline int mirror_8bits(int x) { #endif } -////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// class Gradient_Shader : public SkShader { public: @@ -309,7 +342,7 @@ Gradient_Shader::Gradient_Shader(SkFlattenableReadBuffer& buffer) : recs[i].fScale = buffer.readU32(); } } - buffer.read(&fPtsToUnit, sizeof(SkMatrix)); + SkReadMatrix(&buffer, &fPtsToUnit); fFlags = 0; } @@ -337,7 +370,7 @@ void Gradient_Shader::flatten(SkFlattenableWriteBuffer& buffer) { buffer.write32(recs[i].fScale); } } - buffer.writeMul4(&fPtsToUnit, sizeof(SkMatrix)); + SkWriteMatrix(&buffer, fPtsToUnit); } bool Gradient_Shader::setContext(const SkBitmap& device, @@ -709,7 +742,7 @@ void Gradient_Shader::commonAsAGradient(GradientInfo* info) const { } } -/////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// static void pts_to_unit_matrix(const SkPoint pts[2], SkMatrix* matrix) { SkVector vec = pts[1] - pts[0]; @@ -758,8 +791,8 @@ public: protected: Linear_Gradient(SkFlattenableReadBuffer& buffer) : Gradient_Shader(buffer), - fStart(SkPoint::Make(buffer.readScalar(), buffer.readScalar())), - fEnd(SkPoint::Make(buffer.readScalar(), buffer.readScalar())) { + fStart(unflatten_point(buffer)), + fEnd(unflatten_point(buffer)) { } virtual Factory getFactory() { return CreateProc; } @@ -789,14 +822,24 @@ bool Linear_Gradient::setContext(const SkBitmap& device, const SkPaint& paint, } // Return true if fx, fx+dx, fx+2*dx, ... is always in range -static inline bool no_need_for_clamp(int fx, int dx, int count) -{ +static inline bool no_need_for_clamp(int fx, int dx, int count) { SkASSERT(count > 0); return (unsigned)((fx | (fx + (count - 1) * dx)) >> 8) <= 0xFF; } -void Linear_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) -{ +#include "SkClampRange.h" + +#define NO_CHECK_ITER \ + do { \ + unsigned fi = fx >> 8; \ + SkASSERT(fi <= 0xFF); \ + fx += dx; \ + *dstC++ = cache[toggle + fi]; \ + toggle ^= TOGGLE_MASK; \ + } while (0) + + +void Linear_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) { SkASSERT(count > 0); SkPoint srcPt; @@ -832,6 +875,39 @@ void Linear_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) // TODO: dither version sk_memset32(dstC, cache[fi >> (16 - kCache32Bits)], count); } else if (proc == clamp_tileproc) { +#ifdef SK_ENABLE_FAST_LINEAR_GRADIENTS + SkClampRange range; + range.init(fx, dx, count, 0, 0xFF); + + if ((count = range.fCount0) > 0) { + sk_memset32_dither(dstC, + cache[toggle + range.fV0], + cache[(toggle ^ TOGGLE_MASK) + range.fV0], + count); + dstC += count; + } + if ((count = range.fCount1) > 0) { + int unroll = count >> 3; + fx = range.fFx1; + for (int i = 0; i < unroll; i++) { + NO_CHECK_ITER; NO_CHECK_ITER; + NO_CHECK_ITER; NO_CHECK_ITER; + NO_CHECK_ITER; NO_CHECK_ITER; + NO_CHECK_ITER; NO_CHECK_ITER; + } + if ((count &= 7) > 0) { + do { + NO_CHECK_ITER; + } while (--count != 0); + } + } + if ((count = range.fCount2) > 0) { + sk_memset32_dither(dstC, + cache[toggle + range.fV1], + cache[(toggle ^ TOGGLE_MASK) + range.fV1], + count); + } +#else do { unsigned fi = SkClampMax(fx >> 8, 0xFF); SkASSERT(fi <= 0xFF); @@ -839,6 +915,7 @@ void Linear_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) *dstC++ = cache[toggle + fi]; toggle ^= TOGGLE_MASK; } while (--count != 0); +#endif } else if (proc == mirror_tileproc) { do { unsigned fi = mirror_8bits(fx >> 8); @@ -913,8 +990,17 @@ static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other, } } -void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) -{ +#define NO_CHECK_ITER_16 \ + do { \ + unsigned fi = fx >> kCache16Shift; \ + SkASSERT(fi <= kCache16Mask); \ + fx += dx; \ + *dstC++ = cache[toggle + fi]; \ + toggle ^= TOGGLE_MASK; \ + } while (0) + + +void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) { SkASSERT(count > 0); SkPoint srcPt; @@ -922,6 +1008,7 @@ void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) TileProc proc = fTileProc; const uint16_t* cache = this->getCache16(); int toggle = ((x ^ y) & 1) << kCache16Bits; + const int TOGGLE_MASK = (1 << kCache32Bits); if (fDstToIndexClass != kPerspective_MatrixClass) { dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, @@ -941,22 +1028,57 @@ void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) // we're a vertical gradient, so no change in a span unsigned fi = proc(fx) >> kCache16Shift; SkASSERT(fi <= kCache16Mask); - dither_memset16(dstC, cache[toggle + fi], cache[(toggle ^ (1 << kCache16Bits)) + fi], count); + dither_memset16(dstC, cache[toggle + fi], + cache[(toggle ^ TOGGLE_MASK) + fi], count); } else if (proc == clamp_tileproc) { +#ifdef SK_ENABLE_FAST_LINEAR_GRADIENTS + SkClampRange range; + range.init(fx, dx, count, 0, kCache16Mask); + + if ((count = range.fCount0) > 0) { + dither_memset16(dstC, + cache[toggle + range.fV0], + cache[(toggle ^ TOGGLE_MASK) + range.fV0], + count); + dstC += count; + } + if ((count = range.fCount1) > 0) { + int unroll = count >> 3; + fx = range.fFx1; + for (int i = 0; i < unroll; i++) { + NO_CHECK_ITER_16; NO_CHECK_ITER_16; + NO_CHECK_ITER_16; NO_CHECK_ITER_16; + NO_CHECK_ITER_16; NO_CHECK_ITER_16; + NO_CHECK_ITER_16; NO_CHECK_ITER_16; + } + if ((count &= 7) > 0) { + do { + NO_CHECK_ITER_16; + } while (--count != 0); + } + } + if ((count = range.fCount2) > 0) { + dither_memset16(dstC, + cache[toggle + range.fV1], + cache[(toggle ^ TOGGLE_MASK) + range.fV1], + count); + } +#else do { unsigned fi = SkClampMax(fx >> kCache16Shift, kCache16Mask); SkASSERT(fi <= kCache16Mask); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= (1 << kCache16Bits); + toggle ^= TOGGLE_MASK; } while (--count != 0); +#endif } else if (proc == mirror_tileproc) { do { unsigned fi = mirror_bits(fx >> kCache16Shift, kCache16Bits); SkASSERT(fi <= kCache16Mask); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= (1 << kCache16Bits); + toggle ^= TOGGLE_MASK; } while (--count != 0); } else { SkASSERT(proc == repeat_tileproc); @@ -965,7 +1087,7 @@ void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) SkASSERT(fi <= kCache16Mask); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= (1 << kCache16Bits); + toggle ^= TOGGLE_MASK; } while (--count != 0); } } else { @@ -978,7 +1100,7 @@ void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) int index = fi >> kCache16Shift; *dstC++ = cache[toggle + index]; - toggle ^= (1 << kCache16Bits); + toggle ^= TOGGLE_MASK; dstX += SK_Scalar1; } while (--count != 0); @@ -996,26 +1118,27 @@ void Linear_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) #include <stdio.h> -void SkRadialGradient_BuildTable() -{ +void SkRadialGradient_BuildTable() { // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table FILE* file = ::fopen("SkRadialGradient_Table.h", "w"); SkASSERT(file); ::fprintf(file, "static const uint8_t gSqrt8Table[] = {\n"); - for (int i = 0; i < kSQRT_TABLE_SIZE; i++) - { - if ((i & 15) == 0) + for (int i = 0; i < kSQRT_TABLE_SIZE; i++) { + if ((i & 15) == 0) { ::fprintf(file, "\t"); + } uint8_t value = SkToU8(SkFixedSqrt(i * SK_Fixed1 / kSQRT_TABLE_SIZE) >> 8); ::fprintf(file, "0x%02X", value); - if (i < kSQRT_TABLE_SIZE-1) + if (i < kSQRT_TABLE_SIZE-1) { ::fprintf(file, ", "); - if ((i & 15) == 15) + } + if ((i & 15) == 15) { ::fprintf(file, "\n"); + } } ::fprintf(file, "};\n"); ::fclose(file); @@ -1024,8 +1147,8 @@ void SkRadialGradient_BuildTable() #endif -static void rad_to_unit_matrix(const SkPoint& center, SkScalar radius, SkMatrix* matrix) -{ +static void rad_to_unit_matrix(const SkPoint& center, SkScalar radius, + SkMatrix* matrix) { SkScalar inv = SkScalarInvert(radius); matrix->setTranslate(-center.fX, -center.fY); @@ -1046,8 +1169,8 @@ public: rad_to_unit_matrix(center, radius, &fPtsToUnit); } - virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) - { + + virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) { SkASSERT(count > 0); SkPoint srcPt; @@ -1055,29 +1178,24 @@ public: TileProc proc = fTileProc; const SkPMColor* cache = this->getCache32(); - if (fDstToIndexClass != kPerspective_MatrixClass) - { + if (fDstToIndexClass != kPerspective_MatrixClass) { dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf, &srcPt); SkFixed dx, fx = SkScalarToFixed(srcPt.fX); SkFixed dy, fy = SkScalarToFixed(srcPt.fY); - if (fDstToIndexClass == kFixedStepInX_MatrixClass) - { + if (fDstToIndexClass == kFixedStepInX_MatrixClass) { SkFixed storage[2]; (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &storage[0], &storage[1]); dx = storage[0]; dy = storage[1]; - } - else - { + } else { SkASSERT(fDstToIndexClass == kLinear_MatrixClass); dx = SkScalarToFixed(fDstToIndex.getScaleX()); dy = SkScalarToFixed(fDstToIndex.getSkewY()); } - if (proc == clamp_tileproc) - { + if (proc == clamp_tileproc) { const uint8_t* sqrt_table = gSqrt8Table; fx >>= 1; dx >>= 1; @@ -1092,23 +1210,25 @@ public: fx += dx; fy += dy; } while (--count != 0); - } - else if (proc == mirror_tileproc) - { + } else if (proc == mirror_tileproc) { do { - SkFixed dist = SkFixedSqrt(SkFixedSquare(fx) + SkFixedSquare(fy)); + SkFixed magnitudeSquared = SkFixedSquare(fx) + SkFixedSquare(fy); + if (magnitudeSquared < 0) // Overflow. + magnitudeSquared = SK_FixedMax; + SkFixed dist = SkFixedSqrt(magnitudeSquared); unsigned fi = mirror_tileproc(dist); SkASSERT(fi <= 0xFFFF); *dstC++ = cache[fi >> (16 - kCache32Bits)]; fx += dx; fy += dy; } while (--count != 0); - } - else - { + } else { SkASSERT(proc == repeat_tileproc); do { - SkFixed dist = SkFixedSqrt(SkFixedSquare(fx) + SkFixedSquare(fy)); + SkFixed magnitudeSquared = SkFixedSquare(fx) + SkFixedSquare(fy); + if (magnitudeSquared < 0) // Overflow. + magnitudeSquared = SK_FixedMax; + SkFixed dist = SkFixedSqrt(magnitudeSquared); unsigned fi = repeat_tileproc(dist); SkASSERT(fi <= 0xFFFF); *dstC++ = cache[fi >> (16 - kCache32Bits)]; @@ -1116,9 +1236,7 @@ public: fy += dy; } while (--count != 0); } - } - else // perspective case - { + } else { // perspective case SkScalar dstX = SkIntToScalar(x); SkScalar dstY = SkIntToScalar(y); do { @@ -1272,7 +1390,7 @@ public: protected: Radial_Gradient(SkFlattenableReadBuffer& buffer) : Gradient_Shader(buffer), - fCenter(SkPoint::Make(buffer.readScalar(), buffer.readScalar())), + fCenter(unflatten_point(buffer)), fRadius(buffer.readScalar()) { } virtual Factory getFactory() { return CreateProc; } @@ -1355,6 +1473,7 @@ private: */ +#ifdef SK_USE_SLOW_2POINT_RADIAL_GRADIENT static inline SkFixed two_point_radial(SkFixed b, SkFixed fx, SkFixed fy, SkFixed sr2d2, SkFixed foura, SkFixed oneOverTwoA, bool posRoot) { SkFixed c = SkFixedSquare(fx) + SkFixedSquare(fy) - sr2d2; SkFixed discrim = SkFixedSquare(b) - SkFixedMul(foura, c); @@ -1368,6 +1487,25 @@ static inline SkFixed two_point_radial(SkFixed b, SkFixed fx, SkFixed fy, SkFixe return SkFixedMul(-b - rootDiscrim, oneOverTwoA); } } +#else +static inline SkFixed two_point_radial(SkScalar b, SkScalar fx, SkScalar fy, + SkScalar sr2d2, SkScalar foura, + SkScalar oneOverTwoA, bool posRoot) { + SkScalar c = SkScalarSquare(fx) + SkScalarSquare(fy) - sr2d2; + SkScalar discrim = SkScalarSquare(b) - SkScalarMul(foura, c); + if (discrim < 0) { + discrim = -discrim; + } + SkScalar rootDiscrim = SkScalarSqrt(discrim); + SkScalar result; + if (posRoot) { + result = SkScalarMul(-b + rootDiscrim, oneOverTwoA); + } else { + result = SkScalarMul(-b - rootDiscrim, oneOverTwoA); + } + return SkScalarToFixed(result); +} +#endif class Two_Point_Radial_Gradient : public Gradient_Shader { public: @@ -1429,14 +1567,15 @@ public: return kRadial2_GradientType; } +#ifdef SK_USE_SLOW_2POINT_RADIAL_GRADIENT virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) { SkASSERT(count > 0); - + // Zero difference between radii: fill with transparent black. if (fDiffRadius == 0) { - sk_bzero(dstC, count * sizeof(*dstC)); - return; + sk_bzero(dstC, count * sizeof(*dstC)); + return; } SkMatrix::MapXYProc dstProc = fDstToIndexProc; TileProc proc = fTileProc; @@ -1452,10 +1591,10 @@ public: { SkPoint srcPt; dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, - SkIntToScalar(y) + SK_ScalarHalf, &srcPt); + SkIntToScalar(y) + SK_ScalarHalf, &srcPt); SkFixed dx, fx = SkScalarToFixed(srcPt.fX); SkFixed dy, fy = SkScalarToFixed(srcPt.fY); - + if (fDstToIndexClass == kFixedStepInX_MatrixClass) { (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &dx, &dy); @@ -1527,6 +1666,93 @@ public: } } } +#else + virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) { + SkASSERT(count > 0); + + // Zero difference between radii: fill with transparent black. + if (fDiffRadius == 0) { + sk_bzero(dstC, count * sizeof(*dstC)); + return; + } + SkMatrix::MapXYProc dstProc = fDstToIndexProc; + TileProc proc = fTileProc; + const SkPMColor* cache = this->getCache32(); + + SkScalar foura = fA * 4; + bool posRoot = fDiffRadius < 0; + if (fDstToIndexClass != kPerspective_MatrixClass) { + SkPoint srcPt; + dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, + SkIntToScalar(y) + SK_ScalarHalf, &srcPt); + SkScalar dx, fx = srcPt.fX; + SkScalar dy, fy = srcPt.fY; + + if (fDstToIndexClass == kFixedStepInX_MatrixClass) { + SkFixed fixedX, fixedY; + (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), &fixedX, &fixedY); + dx = SkFixedToScalar(fixedX); + dy = SkFixedToScalar(fixedY); + } else { + SkASSERT(fDstToIndexClass == kLinear_MatrixClass); + dx = fDstToIndex.getScaleX(); + dy = fDstToIndex.getSkewY(); + } + SkScalar b = (SkScalarMul(fDiff.fX, fx) + + SkScalarMul(fDiff.fY, fy) - fStartRadius) * 2; + SkScalar db = (SkScalarMul(fDiff.fX, dx) + + SkScalarMul(fDiff.fY, dy)) * 2; + if (proc == clamp_tileproc) { + for (; count > 0; --count) { + SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot); + SkFixed index = SkClampMax(t, 0xFFFF); + SkASSERT(index <= 0xFFFF); + *dstC++ = cache[index >> (16 - kCache32Bits)]; + fx += dx; + fy += dy; + b += db; + } + } else if (proc == mirror_tileproc) { + for (; count > 0; --count) { + SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot); + SkFixed index = mirror_tileproc(t); + SkASSERT(index <= 0xFFFF); + *dstC++ = cache[index >> (16 - kCache32Bits)]; + fx += dx; + fy += dy; + b += db; + } + } else { + SkASSERT(proc == repeat_tileproc); + for (; count > 0; --count) { + SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot); + SkFixed index = repeat_tileproc(t); + SkASSERT(index <= 0xFFFF); + *dstC++ = cache[index >> (16 - kCache32Bits)]; + fx += dx; + fy += dy; + b += db; + } + } + } else { // perspective case + SkScalar dstX = SkIntToScalar(x); + SkScalar dstY = SkIntToScalar(y); + for (; count > 0; --count) { + SkPoint srcPt; + dstProc(fDstToIndex, dstX, dstY, &srcPt); + SkScalar fx = srcPt.fX; + SkScalar fy = srcPt.fY; + SkScalar b = (SkScalarMul(fDiff.fX, fx) + + SkScalarMul(fDiff.fY, fy) - fStartRadius) * 2; + SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot); + SkFixed index = proc(t); + SkASSERT(index <= 0xFFFF); + *dstC++ = cache[index >> (16 - kCache32Bits)]; + dstX += SK_Scalar1; + } + } + } +#endif virtual bool setContext(const SkBitmap& device, const SkPaint& paint, @@ -1557,8 +1783,8 @@ public: protected: Two_Point_Radial_Gradient(SkFlattenableReadBuffer& buffer) : Gradient_Shader(buffer), - fCenter1(SkPoint::Make(buffer.readScalar(), buffer.readScalar())), - fCenter2(SkPoint::Make(buffer.readScalar(), buffer.readScalar())), + fCenter1(unflatten_point(buffer)), + fCenter2(unflatten_point(buffer)), fRadius1(buffer.readScalar()), fRadius2(buffer.readScalar()) { init(); @@ -1642,7 +1868,7 @@ public: protected: Sweep_Gradient(SkFlattenableReadBuffer& buffer) : Gradient_Shader(buffer), - fCenter(SkPoint::Make(buffer.readScalar(), buffer.readScalar())) { + fCenter(unflatten_point(buffer)) { } virtual Factory getFactory() { return CreateProc; } @@ -1660,10 +1886,8 @@ static uint8_t gSweepTable[65]; /* Our table stores precomputed values for atan: [0...1] -> [0..PI/4] We scale the results to [0..32] */ -static const uint8_t* build_sweep_table() -{ - if (!gSweepTableReady) - { +static const uint8_t* build_sweep_table() { + if (!gSweepTableReady) { const int N = 65; const double DENOM = N - 1; @@ -1696,8 +1920,7 @@ static const uint8_t* build_sweep_table() { return gSweepTable; } // Same as (but faster than) SkFixedDiv(numer, denom) >> 10 //unsigned div_64(int numer, int denom); -static unsigned div_64(int numer, int denom) -{ +static unsigned div_64(int numer, int denom) { SkASSERT(numer <= denom); SkASSERT(numer > 0); SkASSERT(denom > 0); @@ -1707,8 +1930,9 @@ static unsigned div_64(int numer, int denom) int bits = 6 - nbits + dbits; SkASSERT(bits <= 6); - if (bits < 0) // detect underflow + if (bits < 0) { // detect underflow return 0; + } denom <<= dbits - 1; numer <<= nbits - 1; @@ -1716,14 +1940,14 @@ static unsigned div_64(int numer, int denom) unsigned result = 0; // do the first one - if ((numer -= denom) >= 0) + if ((numer -= denom) >= 0) { result = 1; - else + } else { numer += denom; + } // Now fall into our switch statement if there are more bits to compute - if (bits > 0) - { + if (bits > 0) { // make room for the rest of the answer bits result <<= bits; switch (bits) { @@ -1764,13 +1988,11 @@ static unsigned div_64(int numer, int denom) } // Given x,y in the first quadrant, return 0..63 for the angle [0..90] -static unsigned atan_0_90(SkFixed y, SkFixed x) -{ +static unsigned atan_0_90(SkFixed y, SkFixed x) { #ifdef SK_DEBUG { static bool gOnce; - if (!gOnce) - { + if (!gOnce) { gOnce = true; SkASSERT(div_64(55, 55) == 64); SkASSERT(div_64(128, 256) == 32); @@ -1790,8 +2012,7 @@ static unsigned atan_0_90(SkFixed y, SkFixed x) unsigned result; bool swap = (x < y); - if (swap) - { + if (swap) { // first part of the atan(v) = PI/2 - atan(1/v) identity // since our div_64 and table want v <= 1, where v = y/x SkTSwap<SkFixed>(x, y); @@ -1810,8 +2031,7 @@ static unsigned atan_0_90(SkFixed y, SkFixed x) SkASSERT(result < SK_ARRAY_COUNT(gSweepTable)); result = table[result]; - if (swap) - { + if (swap) { // complete the atan(v) = PI/2 - atan(1/v) identity result = 64 - result; // pin to 63 @@ -1823,16 +2043,16 @@ static unsigned atan_0_90(SkFixed y, SkFixed x) } // returns angle in a circle [0..2PI) -> [0..255] -static unsigned SkATan2_255(SkFixed y, SkFixed x) -{ - if (x == 0) - { - if (y == 0) +static unsigned SkATan2_255(SkFixed y, SkFixed x) { + if (x == 0) { + if (y == 0) { return 0; + } return y < 0 ? 192 : 64; } - if (y == 0) + if (y == 0) { return x < 0 ? 128 : 0; + } /* Find the right quadrant for x,y Since atan_0_90 only handles the first quadrant, we rotate x,y @@ -1869,54 +2089,46 @@ static unsigned SkATan2_255(SkFixed y, SkFixed x) */ x = (x ^ xsign) - xsign; y = (y ^ ysign) - ysign; - if (add & 64) // quads 1 or 3 need to swap x,y + if (add & 64) { // quads 1 or 3 need to swap x,y SkTSwap<SkFixed>(x, y); + } unsigned result = add + atan_0_90(y, x); SkASSERT(result < 256); return result; } -void Sweep_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) -{ +void Sweep_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) { SkMatrix::MapXYProc proc = fDstToIndexProc; const SkMatrix& matrix = fDstToIndex; const SkPMColor* cache = this->getCache32(); SkPoint srcPt; - if (fDstToIndexClass != kPerspective_MatrixClass) - { + if (fDstToIndexClass != kPerspective_MatrixClass) { proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf, &srcPt); SkFixed dx, fx = SkScalarToFixed(srcPt.fX); SkFixed dy, fy = SkScalarToFixed(srcPt.fY); - if (fDstToIndexClass == kFixedStepInX_MatrixClass) - { + if (fDstToIndexClass == kFixedStepInX_MatrixClass) { SkFixed storage[2]; (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, &storage[0], &storage[1]); dx = storage[0]; dy = storage[1]; - } - else - { + } else { SkASSERT(fDstToIndexClass == kLinear_MatrixClass); dx = SkScalarToFixed(matrix.getScaleX()); dy = SkScalarToFixed(matrix.getSkewY()); } - for (; count > 0; --count) - { + for (; count > 0; --count) { *dstC++ = cache[SkATan2_255(fy, fx)]; fx += dx; fy += dy; } - } - else // perspective case - { - for (int stop = x + count; x < stop; x++) - { + } else { // perspective case + for (int stop = x + count; x < stop; x++) { proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf, &srcPt); @@ -1927,49 +2139,40 @@ void Sweep_Gradient::shadeSpan(int x, int y, SkPMColor dstC[], int count) } } -void Sweep_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) -{ +void Sweep_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) { SkMatrix::MapXYProc proc = fDstToIndexProc; const SkMatrix& matrix = fDstToIndex; const uint16_t* cache = this->getCache16(); int toggle = ((x ^ y) & 1) << kCache16Bits; SkPoint srcPt; - if (fDstToIndexClass != kPerspective_MatrixClass) - { + if (fDstToIndexClass != kPerspective_MatrixClass) { proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf, &srcPt); SkFixed dx, fx = SkScalarToFixed(srcPt.fX); SkFixed dy, fy = SkScalarToFixed(srcPt.fY); - if (fDstToIndexClass == kFixedStepInX_MatrixClass) - { + if (fDstToIndexClass == kFixedStepInX_MatrixClass) { SkFixed storage[2]; (void)matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf, &storage[0], &storage[1]); dx = storage[0]; dy = storage[1]; - } - else - { + } else { SkASSERT(fDstToIndexClass == kLinear_MatrixClass); dx = SkScalarToFixed(matrix.getScaleX()); dy = SkScalarToFixed(matrix.getSkewY()); } - for (; count > 0; --count) - { + for (; count > 0; --count) { int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits); *dstC++ = cache[toggle + index]; toggle ^= (1 << kCache16Bits); fx += dx; fy += dy; } - } - else // perspective case - { - for (int stop = x + count; x < stop; x++) - { + } else { // perspective case + for (int stop = x + count; x < stop; x++) { proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, SkIntToScalar(y) + SK_ScalarHalf, &srcPt); @@ -1982,8 +2185,8 @@ void Sweep_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) } } -/////////////////////////////////////////////////////////////////////////// -/////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// // assumes colors is SkColor* and pos is SkScalar* #define EXPAND_1_COLOR(count) \ @@ -1997,10 +2200,11 @@ void Sweep_Gradient::shadeSpan16(int x, int y, uint16_t dstC[], int count) } \ } while (0) -SkShader* SkGradientShader::CreateLinear( const SkPoint pts[2], - const SkColor colors[], const SkScalar pos[], int colorCount, - SkShader::TileMode mode, SkUnitMapper* mapper) -{ +SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], + const SkColor colors[], + const SkScalar pos[], int colorCount, + SkShader::TileMode mode, + SkUnitMapper* mapper) { if (NULL == pts || NULL == colors || colorCount < 1) { return NULL; } @@ -2010,10 +2214,11 @@ SkShader* SkGradientShader::CreateLinear( const SkPoint pts[2], (pts, colors, pos, colorCount, mode, mapper)); } -SkShader* SkGradientShader::CreateRadial( const SkPoint& center, SkScalar radius, - const SkColor colors[], const SkScalar pos[], int colorCount, - SkShader::TileMode mode, SkUnitMapper* mapper) -{ +SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, + const SkColor colors[], + const SkScalar pos[], int colorCount, + SkShader::TileMode mode, + SkUnitMapper* mapper) { if (radius <= 0 || NULL == colors || colorCount < 1) { return NULL; } @@ -2031,22 +2236,21 @@ SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, const SkScalar pos[], int colorCount, SkShader::TileMode mode, - SkUnitMapper* mapper) -{ + SkUnitMapper* mapper) { if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { return NULL; } EXPAND_1_COLOR(colorCount); return SkNEW_ARGS(Two_Point_Radial_Gradient, - (start, startRadius, end, endRadius, colors, pos, colorCount, mode, mapper)); + (start, startRadius, end, endRadius, colors, pos, + colorCount, mode, mapper)); } SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], - int count, SkUnitMapper* mapper) -{ + int count, SkUnitMapper* mapper) { if (NULL == colors || count < 1) { return NULL; } @@ -2064,3 +2268,6 @@ static SkFlattenable::Registrar gRadialGradientReg("Radial_Gradient", static SkFlattenable::Registrar gSweepGradientReg("Sweep_Gradient", Sweep_Gradient::CreateProc); +static SkFlattenable::Registrar + gTwoPointRadialGradientReg("Two_Point_Radial_Gradient", + Two_Point_Radial_Gradient::CreateProc); diff --git a/src/effects/SkKernel33MaskFilter.cpp b/src/effects/SkKernel33MaskFilter.cpp index 429e120..5051f71 100644 --- a/src/effects/SkKernel33MaskFilter.cpp +++ b/src/effects/SkKernel33MaskFilter.cpp @@ -1,21 +1,21 @@ #include "SkKernel33MaskFilter.h" #include "SkColorPriv.h" -SkMask::Format SkKernel33ProcMaskFilter::getFormat() -{ +SkMask::Format SkKernel33ProcMaskFilter::getFormat() { return SkMask::kA8_Format; } -bool SkKernel33ProcMaskFilter::filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, SkIPoint* margin) -{ +bool SkKernel33ProcMaskFilter::filterMask(SkMask* dst, const SkMask& src, + const SkMatrix&, SkIPoint* margin) { // margin??? dst->fImage = NULL; dst->fBounds = src.fBounds; dst->fBounds.inset(-1, -1); dst->fFormat = SkMask::kA8_Format; - if (NULL == src.fImage) + if (NULL == src.fImage) { return true; + } dst->fRowBytes = dst->fBounds.width(); size_t size = dst->computeImageSize(); @@ -39,28 +39,26 @@ bool SkKernel33ProcMaskFilter::filterMask(SkMask* dst, const SkMask& src, const unsigned scale = fPercent256; - for (int y = -1; y <= h; y++) - { + for (int y = -1; y <= h; y++) { uint8_t* dstRow = dstImage; - for (int x = -1; x <= w; x++) - { + for (int x = -1; x <= w; x++) { memset(storage, 0, sizeof(storage)); uint8_t* storagePtr = &storage[0][0]; - for (int ky = y - 1; ky <= y + 1; ky++) - { + for (int ky = y - 1; ky <= y + 1; ky++) { const uint8_t* srcRow = srcImage + ky * srcRB; // may be out-of-range - for (int kx = x - 1; kx <= x + 1; kx++) - { - if ((unsigned)ky < (unsigned)h && (unsigned)kx < (unsigned)w) + for (int kx = x - 1; kx <= x + 1; kx++) { + if ((unsigned)ky < (unsigned)h && (unsigned)kx < (unsigned)w) { *storagePtr = srcRow[kx]; + } storagePtr++; } } int value = this->computeValue(srcRows); - if (scale < 256) + if (scale < 256) { value = SkAlphaBlend(value, srcRows[1][1], scale); + } *dstRow++ = SkToU8(value); } dstImage += dst->fRowBytes; @@ -68,57 +66,53 @@ bool SkKernel33ProcMaskFilter::filterMask(SkMask* dst, const SkMask& src, const return true; } -void SkKernel33ProcMaskFilter::flatten(SkFlattenableWriteBuffer& wb) -{ +void SkKernel33ProcMaskFilter::flatten(SkFlattenableWriteBuffer& wb) { this->INHERITED::flatten(wb); wb.write32(fPercent256); } SkKernel33ProcMaskFilter::SkKernel33ProcMaskFilter(SkFlattenableReadBuffer& rb) - : SkMaskFilter(rb) -{ + : SkMaskFilter(rb) { fPercent256 = rb.readS32(); } /////////////////////////////////////////////////////////////////////////////// -uint8_t SkKernel33MaskFilter::computeValue(uint8_t* const* srcRows) -{ +uint8_t SkKernel33MaskFilter::computeValue(uint8_t* const* srcRows) { int value = 0; - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { value += fKernel[i][j] * srcRows[i][j]; + } + } value >>= fShift; - if (value < 0) + if (value < 0) { value = 0; - else if (value > 255) + } else if (value > 255) { value = 255; + } return (uint8_t)value; } -void SkKernel33MaskFilter::flatten(SkFlattenableWriteBuffer& wb) -{ +void SkKernel33MaskFilter::flatten(SkFlattenableWriteBuffer& wb) { this->INHERITED::flatten(wb); wb.writeMul4(fKernel, 9 * sizeof(int)); wb.write32(fShift); } -SkFlattenable::Factory SkKernel33MaskFilter::getFactory() -{ +SkFlattenable::Factory SkKernel33MaskFilter::getFactory() { return Create; } -SkFlattenable* SkKernel33MaskFilter::Create(SkFlattenableReadBuffer& rb) -{ +SkFlattenable* SkKernel33MaskFilter::Create(SkFlattenableReadBuffer& rb) { return new SkKernel33MaskFilter(rb); } SkKernel33MaskFilter::SkKernel33MaskFilter(SkFlattenableReadBuffer& rb) - : SkKernel33ProcMaskFilter(rb) -{ + : SkKernel33ProcMaskFilter(rb) { rb.read(fKernel, 9 * sizeof(int)); fShift = rb.readS32(); } diff --git a/src/effects/SkLayerDrawLooper.cpp b/src/effects/SkLayerDrawLooper.cpp index b3f3886..3b21d49 100644 --- a/src/effects/SkLayerDrawLooper.cpp +++ b/src/effects/SkLayerDrawLooper.cpp @@ -5,7 +5,8 @@ #include "SkUnPreMultiply.h" SkLayerDrawLooper::LayerInfo::LayerInfo() { - fPaintBits = 0; // ignore out paint + fFlagsMask = 0; // ignore our paint flags + fPaintBits = 0; // ignore our paint fields fColorMode = SkXfermode::kDst_Mode; // ignore our color fOffset.set(0, 0); fPostTranslate = false; @@ -36,11 +37,11 @@ SkPaint* SkLayerDrawLooper::addLayer(const LayerInfo& info) { return &rec->fPaint; } -SkPaint* SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) { +void SkLayerDrawLooper::addLayer(SkScalar dx, SkScalar dy) { LayerInfo info; info.fOffset.set(dx, dy); - return this->addLayer(info); + (void)this->addLayer(info); } void SkLayerDrawLooper::init(SkCanvas* canvas) { @@ -63,17 +64,25 @@ static SkColor xferColor(SkColor src, SkColor dst, SkXfermode::Mode mode) { } } -void SkLayerDrawLooper::ApplyBits(SkPaint* dst, const SkPaint& src, - BitFlags bits, SkXfermode::Mode colorMode) { - dst->setColor(xferColor(src.getColor(), dst->getColor(), colorMode)); +void SkLayerDrawLooper::ApplyInfo(SkPaint* dst, const SkPaint& src, + const LayerInfo& info) { + + uint32_t mask = info.fFlagsMask; + dst->setFlags((dst->getFlags() & ~mask) | (src.getFlags() & mask)); + + dst->setColor(xferColor(src.getColor(), dst->getColor(), info.fColorMode)); + + BitFlags bits = info.fPaintBits; if (0 == bits) { return; } if (kEntirePaint_Bits == bits) { - // we've already compute the color, so save it from the assignment + // we've already computed these, so save it from the assignment + uint32_t f = dst->getFlags(); SkColor c = dst->getColor(); *dst = src; + dst->setFlags(f); dst->setColor(c); return; } @@ -133,8 +142,7 @@ bool SkLayerDrawLooper::next(SkCanvas* canvas, SkPaint* paint) { return false; } - ApplyBits(paint, fCurrRec->fPaint, fCurrRec->fInfo.fPaintBits, - fCurrRec->fInfo.fColorMode); + ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo); canvas->save(SkCanvas::kMatrix_SaveFlag); if (fCurrRec->fInfo.fPostTranslate) { diff --git a/src/effects/SkLayerRasterizer.cpp b/src/effects/SkLayerRasterizer.cpp index 851f418..27683ef 100644 --- a/src/effects/SkLayerRasterizer.cpp +++ b/src/effects/SkLayerRasterizer.cpp @@ -35,8 +35,7 @@ SkLayerRasterizer::SkLayerRasterizer() : fLayers(sizeof(SkLayerRasterizer_Rec)) { } -SkLayerRasterizer::~SkLayerRasterizer() -{ +SkLayerRasterizer::~SkLayerRasterizer() { SkDeque::F2BIter iter(fLayers); SkLayerRasterizer_Rec* rec; @@ -44,35 +43,34 @@ SkLayerRasterizer::~SkLayerRasterizer() rec->fPaint.~SkPaint(); } -void SkLayerRasterizer::addLayer(const SkPaint& paint, SkScalar dx, SkScalar dy) -{ +void SkLayerRasterizer::addLayer(const SkPaint& paint, SkScalar dx, + SkScalar dy) { SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers.push_back(); new (&rec->fPaint) SkPaint(paint); rec->fOffset.set(dx, dy); } -static bool compute_bounds(const SkDeque& layers, const SkPath& path, const SkMatrix& matrix, - const SkIRect* clipBounds, SkIRect* bounds) -{ +static bool compute_bounds(const SkDeque& layers, const SkPath& path, + const SkMatrix& matrix, + const SkIRect* clipBounds, SkIRect* bounds) { SkDeque::F2BIter iter(layers); SkLayerRasterizer_Rec* rec; bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32); - while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) - { + while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) { const SkPaint& paint = rec->fPaint; SkPath fillPath, devPath; const SkPath* p = &path; - if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) - { + if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { paint.getFillPath(path, &fillPath); p = &fillPath; } - if (p->isEmpty()) + if (p->isEmpty()) { continue; + } // apply the matrix and offset { @@ -82,9 +80,11 @@ static bool compute_bounds(const SkDeque& layers, const SkPath& path, const SkMa } SkMask mask; - if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(), &matrix, - &mask, SkMask::kJustComputeBounds_CreateMode)) + if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(), + &matrix, &mask, + SkMask::kJustComputeBounds_CreateMode)) { return false; + } bounds->join(mask.fBounds); } @@ -93,19 +93,17 @@ static bool compute_bounds(const SkDeque& layers, const SkPath& path, const SkMa bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix, const SkIRect* clipBounds, - SkMask* mask, SkMask::CreateMode mode) -{ - if (fLayers.empty()) + SkMask* mask, SkMask::CreateMode mode) { + if (fLayers.empty()) { return false; + } - if (SkMask::kJustRenderImage_CreateMode != mode) - { + if (SkMask::kJustRenderImage_CreateMode != mode) { if (!compute_bounds(fLayers, path, matrix, clipBounds, &mask->fBounds)) return false; } - if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) - { + if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { mask->fFormat = SkMask::kA8_Format; mask->fRowBytes = mask->fBounds.width(); size_t size = mask->computeImageSize(); @@ -116,8 +114,7 @@ bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix, memset(mask->fImage, 0, size); } - if (SkMask::kJustComputeBounds_CreateMode != mode) - { + if (SkMask::kJustComputeBounds_CreateMode != mode) { SkBitmap device; SkDraw draw; SkMatrix translatedMatrix; // this translates us to our local pixels @@ -153,14 +150,12 @@ bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix, /////////// Routines for flattening ///////////////// -static void paint_read(SkPaint* paint, SkFlattenableReadBuffer& buffer) -{ +static void paint_read(SkPaint* paint, SkFlattenableReadBuffer& buffer) { paint->setAntiAlias(buffer.readBool()); paint->setStyle((SkPaint::Style)buffer.readU8()); paint->setAlpha(buffer.readU8()); - if (paint->getStyle() != SkPaint::kFill_Style) - { + if (paint->getStyle() != SkPaint::kFill_Style) { paint->setStrokeWidth(buffer.readScalar()); paint->setStrokeMiter(buffer.readScalar()); paint->setStrokeCap((SkPaint::Cap)buffer.readU8()); @@ -173,14 +168,12 @@ static void paint_read(SkPaint* paint, SkFlattenableReadBuffer& buffer) SkSafeUnref(paint->setXfermode((SkXfermode*)buffer.readFlattenable())); } -static void paint_write(const SkPaint& paint, SkFlattenableWriteBuffer& buffer) -{ +static void paint_write(const SkPaint& paint, SkFlattenableWriteBuffer& buffer) { buffer.writeBool(paint.isAntiAlias()); buffer.write8(paint.getStyle()); buffer.write8(paint.getAlpha()); - if (paint.getStyle() != SkPaint::kFill_Style) - { + if (paint.getStyle() != SkPaint::kFill_Style) { buffer.writeScalar(paint.getStrokeWidth()); buffer.writeScalar(paint.getStrokeMiter()); buffer.write8(paint.getStrokeCap()); @@ -194,12 +187,10 @@ static void paint_write(const SkPaint& paint, SkFlattenableWriteBuffer& buffer) } SkLayerRasterizer::SkLayerRasterizer(SkFlattenableReadBuffer& buffer) - : SkRasterizer(buffer), fLayers(sizeof(SkLayerRasterizer_Rec)) -{ + : SkRasterizer(buffer), fLayers(sizeof(SkLayerRasterizer_Rec)) { int count = buffer.readS32(); - for (int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers.push_back(); #if 0 @@ -213,8 +204,7 @@ SkLayerRasterizer::SkLayerRasterizer(SkFlattenableReadBuffer& buffer) } } -void SkLayerRasterizer::flatten(SkFlattenableWriteBuffer& buffer) -{ +void SkLayerRasterizer::flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); buffer.write32(fLayers.count()); @@ -222,8 +212,7 @@ void SkLayerRasterizer::flatten(SkFlattenableWriteBuffer& buffer) SkDeque::F2BIter iter(fLayers); const SkLayerRasterizer_Rec* rec; - while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != NULL) - { + while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != NULL) { #if 0 rec->fPaint.flatten(buffer); #else @@ -234,13 +223,11 @@ void SkLayerRasterizer::flatten(SkFlattenableWriteBuffer& buffer) } } -SkFlattenable* SkLayerRasterizer::CreateProc(SkFlattenableReadBuffer& buffer) -{ +SkFlattenable* SkLayerRasterizer::CreateProc(SkFlattenableReadBuffer& buffer) { return SkNEW_ARGS(SkLayerRasterizer, (buffer)); } -SkFlattenable::Factory SkLayerRasterizer::getFactory() -{ +SkFlattenable::Factory SkLayerRasterizer::getFactory() { return CreateProc; } diff --git a/src/effects/SkPaintFlagsDrawFilter.cpp b/src/effects/SkPaintFlagsDrawFilter.cpp index 62eb53a..9b7cff1 100644 --- a/src/effects/SkPaintFlagsDrawFilter.cpp +++ b/src/effects/SkPaintFlagsDrawFilter.cpp @@ -1,3 +1,19 @@ +/* + Copyright 2011 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + #include "SkPaintFlagsDrawFilter.h" #include "SkPaint.h" diff --git a/src/effects/SkPixelXorXfermode.cpp b/src/effects/SkPixelXorXfermode.cpp index a5599e2..f4a74ee 100644 --- a/src/effects/SkPixelXorXfermode.cpp +++ b/src/effects/SkPixelXorXfermode.cpp @@ -1,36 +1,48 @@ +/* + Copyright 2011 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + #include "SkPixelXorXfermode.h" #include "SkColorPriv.h" // we always return an opaque color, 'cause I don't know what to do with // the alpha-component and still return a valid premultiplied color. -SkPMColor SkPixelXorXfermode::xferColor(SkPMColor src, SkPMColor dst) -{ +SkPMColor SkPixelXorXfermode::xferColor(SkPMColor src, SkPMColor dst) { SkPMColor res = src ^ dst ^ fOpColor; res |= (SK_A32_MASK << SK_A32_SHIFT); // force it to be opaque return res; } -void SkPixelXorXfermode::flatten(SkFlattenableWriteBuffer& wb) -{ +void SkPixelXorXfermode::flatten(SkFlattenableWriteBuffer& wb) { this->INHERITED::flatten(wb); wb.write32(fOpColor); } SkPixelXorXfermode::SkPixelXorXfermode(SkFlattenableReadBuffer& rb) - : SkXfermode(rb) -{ + : SkXfermode(rb) { fOpColor = rb.readU32(); } -SkFlattenable::Factory SkPixelXorXfermode::getFactory() -{ +SkFlattenable::Factory SkPixelXorXfermode::getFactory() { return Create; } -SkFlattenable* SkPixelXorXfermode::Create(SkFlattenableReadBuffer& rb) -{ +SkFlattenable* SkPixelXorXfermode::Create(SkFlattenableReadBuffer& rb) { return SkNEW_ARGS(SkPixelXorXfermode, (rb)); } - - +static SkFlattenable::Registrar + gSkPixelXorXfermodeReg("SkPixelXorXfermode", + SkPixelXorXfermode::CreateProc); diff --git a/src/effects/SkPorterDuff.cpp b/src/effects/SkPorterDuff.cpp index 980ce29..fd3e1c0 100644 --- a/src/effects/SkPorterDuff.cpp +++ b/src/effects/SkPorterDuff.cpp @@ -1,3 +1,19 @@ +/* + Copyright 2011 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + #include "SkPorterDuff.h" #include "SkXfermode.h" diff --git a/src/effects/SkRectShape.cpp b/src/effects/SkRectShape.cpp index 8a38a1e..47520f0 100644 --- a/src/effects/SkRectShape.cpp +++ b/src/effects/SkRectShape.cpp @@ -1,3 +1,19 @@ +/* + Copyright 2011 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + #include "SkRectShape.h" #include "SkCanvas.h" diff --git a/src/effects/SkTableMaskFilter.cpp b/src/effects/SkTableMaskFilter.cpp index aea939f..8edbf7e 100644 --- a/src/effects/SkTableMaskFilter.cpp +++ b/src/effects/SkTableMaskFilter.cpp @@ -1,3 +1,19 @@ +/* + Copyright 2011 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + #include "SkTableMaskFilter.h" SkTableMaskFilter::SkTableMaskFilter() { diff --git a/src/effects/SkTransparentShader.cpp b/src/effects/SkTransparentShader.cpp index 6b79839..c6caba3 100644 --- a/src/effects/SkTransparentShader.cpp +++ b/src/effects/SkTransparentShader.cpp @@ -20,63 +20,56 @@ bool SkTransparentShader::setContext(const SkBitmap& device, const SkPaint& paint, - const SkMatrix& matrix) -{ + const SkMatrix& matrix) { fDevice = &device; fAlpha = paint.getAlpha(); return this->INHERITED::setContext(device, paint, matrix); } -uint32_t SkTransparentShader::getFlags() -{ +uint32_t SkTransparentShader::getFlags() { uint32_t flags = this->INHERITED::getFlags(); switch (fDevice->getConfig()) { - case SkBitmap::kRGB_565_Config: - flags |= kHasSpan16_Flag; - if (fAlpha == 255) - flags |= kOpaqueAlpha_Flag; - break; - case SkBitmap::kARGB_8888_Config: - case SkBitmap::kARGB_4444_Config: - if (fAlpha == 255 && fDevice->isOpaque()) - flags |= kOpaqueAlpha_Flag; - break; - default: - break; + case SkBitmap::kRGB_565_Config: + flags |= kHasSpan16_Flag; + if (fAlpha == 255) + flags |= kOpaqueAlpha_Flag; + break; + case SkBitmap::kARGB_8888_Config: + case SkBitmap::kARGB_4444_Config: + if (fAlpha == 255 && fDevice->isOpaque()) + flags |= kOpaqueAlpha_Flag; + break; + default: + break; } return flags; } -void SkTransparentShader::shadeSpan(int x, int y, SkPMColor span[], int count) -{ +void SkTransparentShader::shadeSpan(int x, int y, SkPMColor span[], int count) { unsigned scale = SkAlpha255To256(fAlpha); switch (fDevice->getConfig()) { - case SkBitmap::kARGB_8888_Config: - if (scale == 256) - memcpy(span, fDevice->getAddr32(x, y), count * sizeof(SkPMColor)); - else - { - const SkPMColor* src = fDevice->getAddr32(x, y); - for (int i = count - 1; i >= 0; --i) - span[i] = SkAlphaMulQ(src[i], scale); - } - break; - case SkBitmap::kRGB_565_Config: - { + case SkBitmap::kARGB_8888_Config: + if (scale == 256) { + memcpy(span, fDevice->getAddr32(x, y), count * sizeof(SkPMColor)); + } else { + const SkPMColor* src = fDevice->getAddr32(x, y); + for (int i = count - 1; i >= 0; --i) { + span[i] = SkAlphaMulQ(src[i], scale); + } + } + break; + case SkBitmap::kRGB_565_Config: { const uint16_t* src = fDevice->getAddr16(x, y); - if (scale == 256) - { - for (int i = count - 1; i >= 0; --i) + if (scale == 256) { + for (int i = count - 1; i >= 0; --i) { span[i] = SkPixel16ToPixel32(src[i]); - } - else - { + } + } else { unsigned alpha = fAlpha; - for (int i = count - 1; i >= 0; --i) - { + for (int i = count - 1; i >= 0; --i) { uint16_t c = src[i]; unsigned r = SkPacked16ToR32(c); unsigned g = SkPacked16ToG32(c); @@ -88,55 +81,48 @@ void SkTransparentShader::shadeSpan(int x, int y, SkPMColor span[], int count) SkAlphaMul(b, scale)); } } + break; } - break; - case SkBitmap::kARGB_4444_Config: - { + case SkBitmap::kARGB_4444_Config: { const uint16_t* src = fDevice->getAddr16(x, y); - if (scale == 256) - { - for (int i = count - 1; i >= 0; --i) + if (scale == 256) { + for (int i = count - 1; i >= 0; --i) { span[i] = SkPixel4444ToPixel32(src[i]); - } - else - { + } + } else { unsigned scale16 = scale >> 4; - for (int i = count - 1; i >= 0; --i) - { + for (int i = count - 1; i >= 0; --i) { uint32_t c = SkExpand_4444(src[i]) * scale16; span[i] = SkCompact_8888(c); } } + break; } + case SkBitmap::kIndex8_Config: + SkASSERT(!"index8 not supported as a destination device"); break; - case SkBitmap::kIndex8_Config: - SkASSERT(!"index8 not supported as a destination device"); - break; - case SkBitmap::kA8_Config: - { + case SkBitmap::kA8_Config: { const uint8_t* src = fDevice->getAddr8(x, y); - if (scale == 256) - { - for (int i = count - 1; i >= 0; --i) + if (scale == 256) { + for (int i = count - 1; i >= 0; --i) { span[i] = SkPackARGB32(src[i], 0, 0, 0); - } - else - { - for (int i = count - 1; i >= 0; --i) + } + } else { + for (int i = count - 1; i >= 0; --i) { span[i] = SkPackARGB32(SkAlphaMul(src[i], scale), 0, 0, 0); + } } + break; } - break; - case SkBitmap::kA1_Config: - SkASSERT(!"kA1_Config umimplemented at this time"); - break; - default: // to avoid warnings - break; + case SkBitmap::kA1_Config: + SkASSERT(!"kA1_Config umimplemented at this time"); + break; + default: // to avoid warnings + break; } } -void SkTransparentShader::shadeSpan16(int x, int y, uint16_t span[], int count) -{ +void SkTransparentShader::shadeSpan16(int x, int y, uint16_t span[], int count) { SkASSERT(fDevice->getConfig() == SkBitmap::kRGB_565_Config); memcpy(span, fDevice->getAddr16(x, y), count << 1); |