diff options
Diffstat (limited to 'src/core/SkComposeShader.cpp')
-rw-r--r-- | src/core/SkComposeShader.cpp | 108 |
1 files changed, 50 insertions, 58 deletions
diff --git a/src/core/SkComposeShader.cpp b/src/core/SkComposeShader.cpp index 7e566a3..62bb2d6 100644 --- a/src/core/SkComposeShader.cpp +++ b/src/core/SkComposeShader.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. */ @@ -20,40 +20,36 @@ #include "SkColorPriv.h" #include "SkXfermode.h" -////////////////////////////////////////////////////////////////////////////////////// - -SkComposeShader::SkComposeShader(SkShader* sA, SkShader* sB, SkXfermode* mode) -{ +/////////////////////////////////////////////////////////////////////////////// + +SkComposeShader::SkComposeShader(SkShader* sA, SkShader* sB, SkXfermode* mode) { fShaderA = sA; sA->ref(); fShaderB = sB; sB->ref(); // mode may be null - fMode = mode; mode->safeRef(); + fMode = mode; + SkSafeRef(mode); } SkComposeShader::SkComposeShader(SkFlattenableReadBuffer& buffer) : - INHERITED(buffer) -{ + INHERITED(buffer) { fShaderA = static_cast<SkShader*>(buffer.readFlattenable()); fShaderB = static_cast<SkShader*>(buffer.readFlattenable()); fMode = static_cast<SkXfermode*>(buffer.readFlattenable()); } -SkComposeShader::~SkComposeShader() -{ - fMode->safeUnref(); // may be null +SkComposeShader::~SkComposeShader() { + SkSafeUnref(fMode); fShaderB->unref(); fShaderA->unref(); } -void SkComposeShader::beginSession() -{ +void SkComposeShader::beginSession() { this->INHERITED::beginSession(); fShaderA->beginSession(); fShaderB->beginSession(); } -void SkComposeShader::endSession() -{ +void SkComposeShader::endSession() { fShaderA->endSession(); fShaderB->endSession(); this->INHERITED::endSession(); @@ -61,14 +57,13 @@ void SkComposeShader::endSession() class SkAutoAlphaRestore { public: - SkAutoAlphaRestore(SkPaint* paint, uint8_t newAlpha) - { + SkAutoAlphaRestore(SkPaint* paint, uint8_t newAlpha) { fAlpha = paint->getAlpha(); fPaint = paint; paint->setAlpha(newAlpha); } - ~SkAutoAlphaRestore() - { + + ~SkAutoAlphaRestore() { fPaint->setAlpha(fAlpha); } private: @@ -76,8 +71,7 @@ private: uint8_t fAlpha; }; -void SkComposeShader::flatten(SkFlattenableWriteBuffer& buffer) -{ +void SkComposeShader::flatten(SkFlattenableWriteBuffer& buffer) { this->INHERITED::flatten(buffer); buffer.writeFlattenable(fShaderA); buffer.writeFlattenable(fShaderB); @@ -90,19 +84,19 @@ void SkComposeShader::flatten(SkFlattenableWriteBuffer& buffer) */ bool SkComposeShader::setContext(const SkBitmap& device, const SkPaint& paint, - const SkMatrix& matrix) -{ - if (!this->INHERITED::setContext(device, paint, matrix)) + const SkMatrix& matrix) { + if (!this->INHERITED::setContext(device, paint, matrix)) { return false; + } // we preconcat our localMatrix (if any) with the device matrix // before calling our sub-shaders SkMatrix tmpM; - + (void)this->getLocalMatrix(&tmpM); tmpM.setConcat(matrix, tmpM); - + SkAutoAlphaRestore restore(const_cast<SkPaint*>(&paint), 0xFF); return fShaderA->setContext(device, paint, tmpM) && @@ -113,58 +107,56 @@ bool SkComposeShader::setContext(const SkBitmap& device, // take up too much stack-space (each element is 4 bytes) #define TMP_COLOR_COUNT 64 -void SkComposeShader::shadeSpan(int x, int y, SkPMColor result[], int count) -{ +void SkComposeShader::shadeSpan(int x, int y, SkPMColor result[], int count) { SkShader* shaderA = fShaderA; SkShader* shaderB = fShaderB; SkXfermode* mode = fMode; unsigned scale = SkAlpha255To256(this->getPaintAlpha()); - + SkPMColor tmp[TMP_COLOR_COUNT]; - if (NULL == mode) // implied SRC_OVER - { + if (NULL == mode) { // implied SRC_OVER // TODO: when we have a good test-case, should use SkBlitRow::Proc32 // for these loops do { int n = count; - if (n > TMP_COLOR_COUNT) + if (n > TMP_COLOR_COUNT) { n = TMP_COLOR_COUNT; - + } + shaderA->shadeSpan(x, y, result, n); shaderB->shadeSpan(x, y, tmp, n); - if (256 == scale) - { - for (int i = 0; i < n; i++) + if (256 == scale) { + for (int i = 0; i < n; i++) { result[i] = SkPMSrcOver(tmp[i], result[i]); + } + } else { + for (int i = 0; i < n; i++) { + result[i] = SkAlphaMulQ(SkPMSrcOver(tmp[i], result[i]), + scale); + } } - else - { - for (int i = 0; i < n; i++) - result[i] = SkAlphaMulQ(SkPMSrcOver(tmp[i], result[i]), scale); - } - + result += n; x += n; count -= n; } while (count > 0); - } - else // use mode for the composition - { + } else { // use mode for the composition do { int n = count; - if (n > TMP_COLOR_COUNT) + if (n > TMP_COLOR_COUNT) { n = TMP_COLOR_COUNT; - + } + shaderA->shadeSpan(x, y, result, n); shaderB->shadeSpan(x, y, tmp, n); mode->xfer32(result, tmp, n, NULL); - if (256 == scale) - { - for (int i = 0; i < n; i++) + if (256 == scale) { + for (int i = 0; i < n; i++) { result[i] = SkAlphaMulQ(result[i], scale); + } } result += n; @@ -173,4 +165,4 @@ void SkComposeShader::shadeSpan(int x, int y, SkPMColor result[], int count) } while (count > 0); } } - + |