diff options
-rw-r--r-- | gm/localmatriximagefilter.cpp | 103 | ||||
-rw-r--r-- | gyp/core.gypi | 2 | ||||
-rw-r--r-- | include/core/SkImageFilter.h | 7 | ||||
-rw-r--r-- | src/core/SkImageFilter.cpp | 6 | ||||
-rw-r--r-- | src/core/SkLocalMatrixImageFilter.cpp | 57 | ||||
-rw-r--r-- | src/core/SkLocalMatrixImageFilter.h | 40 |
6 files changed, 215 insertions, 0 deletions
diff --git a/gm/localmatriximagefilter.cpp b/gm/localmatriximagefilter.cpp new file mode 100644 index 0000000000..1bdc940d92 --- /dev/null +++ b/gm/localmatriximagefilter.cpp @@ -0,0 +1,103 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "gm.h" +#include "SkCanvas.h" +#include "SkBlurImageFilter.h" +#include "SkColorFilterImageFilter.h" +#include "SkModeColorFilter.h" +#include "SkMorphologyImageFilter.h" +#include "SkOffsetImageFilter.h" +#include "SkSurface.h" + +static SkImage* make_image(SkCanvas* rootCanvas) { + SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); + SkAutoTUnref<SkSurface> surface(rootCanvas->newSurface(info)); + if (!surface) { + surface.reset(SkSurface::NewRaster(info)); + } + + SkPaint paint; + paint.setAntiAlias(true); + paint.setColor(SK_ColorRED); + surface->getCanvas()->drawCircle(50, 50, 50, paint); + return surface->newImageSnapshot(); +} + +typedef SkImageFilter* (*ImageFilterFactory)(); + +// +[]{...} did not work on windows (VS) +// (ImageFilterFactory)[]{...} did not work on linux (gcc) +// hence this cast function +template <typename T> ImageFilterFactory IFCCast(T arg) { return arg; } + +// Show the effect of localmatriximagefilter with various matrices, on various filters +class LocalMatrixImageFilterGM : public skiagm::GM { +public: + LocalMatrixImageFilterGM() {} + +protected: + SkString onShortName() override { + return SkString("localmatriximagefilter"); + } + + SkISize onISize() override { + return SkISize::Make(640, 640); + } + + static void show_image(SkCanvas* canvas, SkImage* image, SkImageFilter* filter) { + SkPaint paint; + paint.setStyle(SkPaint::kStroke_Style); + SkRect r = SkRect::MakeIWH(image->width(), image->height()).makeOutset(SK_ScalarHalf, + SK_ScalarHalf); + canvas->drawRect(r, paint); + + paint.setStyle(SkPaint::kFill_Style); + paint.setImageFilter(filter); + canvas->drawImage(image, 0, 0, &paint); + } + + void onDraw(SkCanvas* canvas) override { + SkAutoTUnref<SkImage> image0(make_image(canvas)); + + const ImageFilterFactory factories[] = { + IFCCast([]{ return SkBlurImageFilter::Create(8, 8); }), + IFCCast([]{ return SkDilateImageFilter::Create(8, 8); }), + IFCCast([]{ return SkErodeImageFilter::Create(8, 8); }), + IFCCast([]{ return SkOffsetImageFilter::Create(8, 8); }), + }; + + const SkMatrix matrices[] = { + SkMatrix::MakeScale(SK_ScalarHalf, SK_ScalarHalf), + SkMatrix::MakeScale(2, 2), + SkMatrix::MakeTrans(10, 10) + }; + + const SkScalar spacer = image0->width() * 3.0f / 2; + + canvas->translate(40, 40); + for (auto&& factory : factories) { + SkAutoTUnref<SkImageFilter> filter(factory()); + + canvas->save(); + show_image(canvas, image0, filter); + for (const auto& matrix : matrices) { + SkAutoTUnref<SkImageFilter> localFilter( + SkImageFilter::CreateLocalMatrixFilter(matrix, filter)); + canvas->translate(spacer, 0); + show_image(canvas, image0, localFilter); + } + canvas->restore(); + canvas->translate(0, spacer); + } + } + +private: + typedef GM INHERITED; +}; +DEF_GM( return new LocalMatrixImageFilterGM; ) + diff --git a/gyp/core.gypi b/gyp/core.gypi index 500065caca..dd00301c71 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -142,6 +142,8 @@ '<(skia_src_path)/core/SkLightingShader.h', '<(skia_src_path)/core/SkLightingShader.cpp', '<(skia_src_path)/core/SkLineClipper.cpp', + '<(skia_src_path)/core/SkLocalMatrixImageFilter.cpp', + '<(skia_src_path)/core/SkLocalMatrixImageFilter.h', '<(skia_src_path)/core/SkLocalMatrixShader.cpp', '<(skia_src_path)/core/SkMallocPixelRef.cpp', '<(skia_src_path)/core/SkMask.cpp', diff --git a/include/core/SkImageFilter.h b/include/core/SkImageFilter.h index 0595010c6f..0a3f7b8492 100644 --- a/include/core/SkImageFilter.h +++ b/include/core/SkImageFilter.h @@ -236,6 +236,13 @@ public: SkFilterQuality, SkImageFilter* input = NULL); + /** + * Create an SkLocalMatrixImageFilter, which transform the filter parameters + * of its inputs by the given matrix. + */ + static SkImageFilter* CreateLocalMatrixFilter(const SkMatrix& matrix, + SkImageFilter* input); + #if SK_SUPPORT_GPU /** * Wrap the given texture in a texture-backed SkBitmap. diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp index 08d46c7970..43adc2fad5 100644 --- a/src/core/SkImageFilter.cpp +++ b/src/core/SkImageFilter.cpp @@ -11,6 +11,7 @@ #include "SkBitmapDevice.h" #include "SkChecksum.h" #include "SkDevice.h" +#include "SkLocalMatrixImageFilter.h" #include "SkMatrixImageFilter.h" #include "SkMutex.h" #include "SkOncePtr.h" @@ -439,6 +440,11 @@ SkImageFilter* SkImageFilter::CreateMatrixFilter(const SkMatrix& matrix, return SkMatrixImageFilter::Create(matrix, filterQuality, input); } +SkImageFilter* SkImageFilter::CreateLocalMatrixFilter(const SkMatrix& matrix, + SkImageFilter* input) { + return SkLocalMatrixImageFilter::Create(matrix, input); +} + #if SK_SUPPORT_GPU void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) { diff --git a/src/core/SkLocalMatrixImageFilter.cpp b/src/core/SkLocalMatrixImageFilter.cpp new file mode 100644 index 0000000000..17ee1028a8 --- /dev/null +++ b/src/core/SkLocalMatrixImageFilter.cpp @@ -0,0 +1,57 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkLocalMatrixImageFilter.h" +#include "SkReadBuffer.h" +#include "SkString.h" + +SkImageFilter* SkLocalMatrixImageFilter::Create(const SkMatrix& localM, SkImageFilter* input) { + if (!input) { + return nullptr; + } + if (localM.getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { + return nullptr; + } + if (localM.isIdentity()) { + return SkRef(input); + } + return new SkLocalMatrixImageFilter(localM, input); +} + +SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM, SkImageFilter* input) + : INHERITED(1, &input), fLocalM(localM) +{} + +SkFlattenable* SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) { + SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); + SkMatrix lm; + buffer.readMatrix(&lm); + return SkLocalMatrixImageFilter::Create(lm, common.getInput(0)); +} + +void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { + this->INHERITED::flatten(buffer); + buffer.writeMatrix(fLocalM); +} + +bool SkLocalMatrixImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, const Context& ctx, + SkBitmap* result, SkIPoint* offset) const { + Context localCtx(SkMatrix::Concat(ctx.ctm(), fLocalM), ctx.clipBounds(), ctx.cache()); + return this->getInput(0)->filterImage(proxy, src, localCtx, result, offset); +} + +bool SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& matrix, + SkIRect* dst) const { + return this->getInput(0)->filterBounds(src, SkMatrix::Concat(matrix, fLocalM), dst); +} + +#ifndef SK_IGNORE_TO_STRING +void SkLocalMatrixImageFilter::toString(SkString* str) const { + str->append("SkLocalMatrixImageFilter: ("); + str->append(")"); +} +#endif diff --git a/src/core/SkLocalMatrixImageFilter.h b/src/core/SkLocalMatrixImageFilter.h new file mode 100644 index 0000000000..f908d025a5 --- /dev/null +++ b/src/core/SkLocalMatrixImageFilter.h @@ -0,0 +1,40 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkLocalMatrixImageFilter_DEFINED +#define SkLocalMatrixImageFilter_DEFINED + +#include "SkImageFilter.h" +#include "SkReadBuffer.h" +#include "SkString.h" + +/** + * Wraps another imagefilter + matrix, such that using this filter will give the same result + * as using the wrapped filter with the matrix applied to its context. + */ +class SkLocalMatrixImageFilter : public SkImageFilter { +public: + static SkImageFilter* Create(const SkMatrix& localM, SkImageFilter* input); + + SK_TO_STRING_OVERRIDE() + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLocalMatrixImageFilter) + +protected: + void flatten(SkWriteBuffer&) const override; + bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, + SkBitmap* result, SkIPoint* offset) const override; + bool onFilterBounds(const SkIRect& src, const SkMatrix&, SkIRect* dst) const override; + +private: + SkLocalMatrixImageFilter(const SkMatrix& localM, SkImageFilter* input); + + SkMatrix fLocalM; + + typedef SkImageFilter INHERITED; +}; + +#endif |