diff options
author | sugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-21 15:02:28 +0000 |
---|---|---|
committer | sugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-21 15:02:28 +0000 |
commit | a1c511b8704c6c266b90860a4c68f30ca7514f9b (patch) | |
tree | 18d8dad0623ae6852d5f7c20f2b477ad60d3f840 /src/effects | |
parent | ee3bc3b26771a58a78075f11cde8801e0e79f723 (diff) |
New SkRectShaderImageFilter image filter
This new changelist also introduces a new image filter called SkRectShaderImageFilter which is make to simply apply a shader on a region without using any inputs.
TEST=Added ShaderImageFilter test
Review URL: https://codereview.appspot.com/7300046
git-svn-id: http://skia.googlecode.com/svn/trunk@7808 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/effects')
-rwxr-xr-x | src/effects/SkColorFilterImageFilter.cpp | 27 | ||||
-rw-r--r-- | src/effects/SkRectShaderImageFilter.cpp | 58 |
2 files changed, 74 insertions, 11 deletions
diff --git a/src/effects/SkColorFilterImageFilter.cpp b/src/effects/SkColorFilterImageFilter.cpp index 549a6867df..b5ab255ce7 100755 --- a/src/effects/SkColorFilterImageFilter.cpp +++ b/src/effects/SkColorFilterImageFilter.cpp @@ -62,16 +62,17 @@ SkColorFilterImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf, SkScalar colorMatrix[20], inputMatrix[20]; SkColorFilter* inputColorFilter; if (input && cf->asColorMatrix(colorMatrix) - && (inputColorFilter = input->asColorFilter()) - && inputColorFilter->asColorMatrix(inputMatrix) - && !matrix_needs_clamping(inputMatrix)) { - SkScalar combinedMatrix[20]; - mult_color_matrix(inputMatrix, colorMatrix, combinedMatrix); - SkAutoTUnref<SkColorFilter> newCF(SkNEW_ARGS(SkColorMatrixFilter, (combinedMatrix))); - return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(0))); - } else { - return SkNEW_ARGS(SkColorFilterImageFilter, (cf, input)); + && input->asColorFilter(&inputColorFilter) + && (NULL != inputColorFilter)) { + SkAutoUnref autoUnref(inputColorFilter); + if (inputColorFilter->asColorMatrix(inputMatrix) && !matrix_needs_clamping(inputMatrix)) { + SkScalar combinedMatrix[20]; + mult_color_matrix(inputMatrix, colorMatrix, combinedMatrix); + SkAutoTUnref<SkColorFilter> newCF(SkNEW_ARGS(SkColorMatrixFilter, (combinedMatrix))); + return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(0))); + } } + return SkNEW_ARGS(SkColorFilterImageFilter, (cf, input)); } SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, SkImageFilter* input) : INHERITED(input), fColorFilter(cf) { @@ -110,6 +111,10 @@ bool SkColorFilterImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& sourc return true; } -SkColorFilter* SkColorFilterImageFilter::asColorFilter() const { - return fColorFilter; +bool SkColorFilterImageFilter::asColorFilter(SkColorFilter** filter) const { + if (filter) { + *filter = fColorFilter; + fColorFilter->ref(); + } + return true; } diff --git a/src/effects/SkRectShaderImageFilter.cpp b/src/effects/SkRectShaderImageFilter.cpp new file mode 100644 index 0000000000..b6c78efd0f --- /dev/null +++ b/src/effects/SkRectShaderImageFilter.cpp @@ -0,0 +1,58 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkRectShaderImageFilter.h" +#include "SkBitmap.h" +#include "SkCanvas.h" +#include "SkDevice.h" +#include "SkFlattenableBuffers.h" +#include "SkShader.h" + +SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, SkRect region) { + SkASSERT(s); + return SkNEW_ARGS(SkRectShaderImageFilter, (s, region)); +} + +SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, SkRect region) + : INHERITED(NULL) + , fShader(s) + , fRegion(region) { + SkASSERT(s); + SkSafeRef(s); +} + +SkRectShaderImageFilter::SkRectShaderImageFilter(SkFlattenableReadBuffer& buffer) + : INHERITED(buffer) { + fShader = buffer.readFlattenableT<SkShader>(); + buffer.readRect(&fRegion); +} + +void SkRectShaderImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { + this->INHERITED::flatten(buffer); + + buffer.writeFlattenable(fShader); + buffer.writeRect(fRegion); +} + +SkRectShaderImageFilter::~SkRectShaderImageFilter() { + SkSafeUnref(fShader); +} + +bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, + const SkBitmap& source, + const SkMatrix& matrix, + SkBitmap* result, + SkIPoint* loc) { + SkAutoTUnref<SkDevice> device(proxy->createDevice(fRegion.width(), fRegion.height())); + SkCanvas canvas(device.get()); + SkPaint paint; + paint.setShader(fShader); + canvas.drawRect(fRegion, paint); + *result = device.get()->accessBitmap(false); + return true; +} + |