diff options
-rw-r--r-- | gm/imagefiltersclipped.cpp | 6 | ||||
-rw-r--r-- | gm/imagefiltersscaled.cpp | 6 | ||||
-rw-r--r-- | gyp/effects.gypi | 2 | ||||
-rw-r--r-- | include/effects/SkPaintImageFilter.h | 45 | ||||
-rw-r--r-- | samplecode/SampleFilterFuzz.cpp | 313 | ||||
-rw-r--r-- | src/core/SkPathEffect.cpp | 12 | ||||
-rw-r--r-- | src/effects/SkPaintImageFilter.cpp | 81 | ||||
-rw-r--r-- | src/ports/SkGlobalInitialization_chromium.cpp | 2 | ||||
-rw-r--r-- | src/ports/SkGlobalInitialization_default.cpp | 2 | ||||
-rw-r--r-- | tests/ImageFilterTest.cpp | 15 | ||||
-rw-r--r-- | tests/PaintImageFilterTest.cpp | 113 |
11 files changed, 577 insertions, 20 deletions
diff --git a/gm/imagefiltersclipped.cpp b/gm/imagefiltersclipped.cpp index a0fedfdeee..f5c9cefbb2 100644 --- a/gm/imagefiltersclipped.cpp +++ b/gm/imagefiltersclipped.cpp @@ -16,9 +16,9 @@ #include "SkLightingImageFilter.h" #include "SkMorphologyImageFilter.h" #include "SkOffsetImageFilter.h" +#include "SkPaintImageFilter.h" #include "SkPerlinNoiseShader.h" #include "SkPoint3.h" -#include "SkRectShaderImageFilter.h" #include "SkScalar.h" #include "SkSurface.h" #include "gm.h" @@ -139,7 +139,9 @@ protected: SkSafeUnref(filters[i]); } - SkAutoTUnref<SkImageFilter> rectFilter(SkRectShaderImageFilter::Create(noise)); + SkPaint noisePaint; + noisePaint.setShader(noise); + SkAutoTUnref<SkImageFilter> rectFilter(SkPaintImageFilter::Create(noisePaint)); canvas->translate(SK_ARRAY_COUNT(filters)*(r.width() + margin), 0); for (int xOffset = 0; xOffset < 80; xOffset += 16) { bounds.fLeft = SkIntToScalar(xOffset); diff --git a/gm/imagefiltersscaled.cpp b/gm/imagefiltersscaled.cpp index 3221c88cae..7576ee1375 100644 --- a/gm/imagefiltersscaled.cpp +++ b/gm/imagefiltersscaled.cpp @@ -16,9 +16,9 @@ #include "SkLightingImageFilter.h" #include "SkMorphologyImageFilter.h" #include "SkOffsetImageFilter.h" +#include "SkPaintImageFilter.h" #include "SkPerlinNoiseShader.h" #include "SkPoint3.h" -#include "SkRectShaderImageFilter.h" #include "SkScalar.h" #include "SkSurface.h" #include "gm.h" @@ -56,6 +56,8 @@ protected: SkAutoTUnref<SkImageFilter> checkerboard(SkImageSource::Create(fCheckerboard)); SkAutoTUnref<SkShader> noise(SkPerlinNoiseShader::CreateFractalNoise( SkDoubleToScalar(0.1), SkDoubleToScalar(0.05), 1, 0)); + SkPaint noisePaint; + noisePaint.setShader(noise); SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10)); SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10), @@ -84,7 +86,7 @@ protected: SkErodeImageFilter::Create(1, 1, checkerboard.get()), SkOffsetImageFilter::Create(SkIntToScalar(32), 0), SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQuality), - SkRectShaderImageFilter::Create(noise), + SkPaintImageFilter::Create(noisePaint), SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd), SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd), diff --git a/gyp/effects.gypi b/gyp/effects.gypi index 3320f4f556..7321ff48d7 100644 --- a/gyp/effects.gypi +++ b/gyp/effects.gypi @@ -55,6 +55,7 @@ '<(skia_src_path)/effects/SkMorphologyImageFilter.cpp', '<(skia_src_path)/effects/SkOffsetImageFilter.cpp', '<(skia_src_path)/effects/SkPaintFlagsDrawFilter.cpp', + '<(skia_src_path)/effects/SkPaintImageFilter.cpp', '<(skia_src_path)/effects/SkPerlinNoiseShader.cpp', '<(skia_src_path)/effects/SkPictureImageFilter.cpp', '<(skia_src_path)/effects/SkPixelXorXfermode.cpp', @@ -111,6 +112,7 @@ '<(skia_include_path)/effects/SkMorphologyImageFilter.h', '<(skia_include_path)/effects/SkOffsetImageFilter.h', '<(skia_include_path)/effects/SkPaintFlagsDrawFilter.h', + '<(skia_include_path)/effects/SkPaintImageFilter.h', '<(skia_include_path)/effects/SkPerlinNoiseShader.h', '<(skia_include_path)/effects/SkPixelXorXfermode.h', '<(skia_include_path)/effects/SkRectShaderImageFilter.h', diff --git a/include/effects/SkPaintImageFilter.h b/include/effects/SkPaintImageFilter.h new file mode 100644 index 0000000000..eee3630f31 --- /dev/null +++ b/include/effects/SkPaintImageFilter.h @@ -0,0 +1,45 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkPaintImageFilter_DEFINED +#define SkPaintImageFilter_DEFINED + +#include "SkImageFilter.h" +#include "SkPaint.h" + +class SK_API SkPaintImageFilter : public SkImageFilter { +public: + /** Create a new image filter which fills the given rectangle using the + * given paint. If no rectangle is specified, an output is produced with + * the same bounds as the input primitive (even though the input + * primitive's pixels are not used for processing). + * @param paint Paint to use when filling the rect. + * @param rect Rectangle of output pixels. If NULL or a given crop edge is + * not specified, the source primitive's bounds are used + * instead. + */ + static SkImageFilter* Create(const SkPaint& paint, const CropRect* rect = NULL); + + bool canComputeFastBounds() const override; + + SK_TO_STRING_OVERRIDE() + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPaintImageFilter) + +protected: + void flatten(SkWriteBuffer&) const override; + bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, SkBitmap* result, + SkIPoint* loc) const override; + +private: + SkPaintImageFilter(const SkPaint& paint, const CropRect* rect); + + SkPaint fPaint; + + typedef SkImageFilter INHERITED; +}; + +#endif diff --git a/samplecode/SampleFilterFuzz.cpp b/samplecode/SampleFilterFuzz.cpp index a0f7aff5ef..9d45fa6e07 100644 --- a/samplecode/SampleFilterFuzz.cpp +++ b/samplecode/SampleFilterFuzz.cpp @@ -5,32 +5,46 @@ * found in the LICENSE file. */ #include "SampleCode.h" +#include "Sk1DPathEffect.h" +#include "Sk2DPathEffect.h" #include "SkAlphaThresholdFilter.h" +#include "SkArcToPathEffect.h" +#include "SkAnnotation.h" #include "SkBlurImageFilter.h" +#include "SkBlurMaskFilter.h" #include "SkCanvas.h" #include "SkColorCubeFilter.h" #include "SkColorFilter.h" #include "SkColorFilterImageFilter.h" +#include "SkColorMatrixFilter.h" #include "SkComposeImageFilter.h" +#include "SkCornerPathEffect.h" +#include "SkDashPathEffect.h" #include "SkData.h" +#include "SkDiscretePathEffect.h" #include "SkDisplacementMapEffect.h" #include "SkDropShadowImageFilter.h" +#include "SkEmbossMaskFilter.h" #include "SkFlattenableSerialization.h" #include "SkImageSource.h" +#include "SkLayerRasterizer.h" #include "SkLightingImageFilter.h" +#include "SkLumaColorFilter.h" #include "SkMagnifierImageFilter.h" #include "SkMatrixConvolutionImageFilter.h" #include "SkMergeImageFilter.h" #include "SkMorphologyImageFilter.h" #include "SkOffsetImageFilter.h" +#include "SkPaintImageFilter.h" #include "SkPerlinNoiseShader.h" #include "SkPictureImageFilter.h" #include "SkPictureRecorder.h" #include "SkPoint3.h" #include "SkRandom.h" -#include "SkRectShaderImageFilter.h" +#include "SkTableColorFilter.h" #include "SkTestImageFilters.h" #include "SkTileImageFilter.h" +#include "SkTypeface.h" #include "SkView.h" #include "SkXfermodeImageFilter.h" #include <stdio.h> @@ -94,6 +108,36 @@ static SkScalar make_scalar(bool positiveOnly = false) { return make_number(positiveOnly); } +static SkString make_string() { + int length = R(1000); + SkString str(length); + for (int i = 0; i < length; ++i) { + str[i] = static_cast<char>(R(256)); + } + return str; +} + +static SkString make_font_name() { + int sel = R(8); + + switch(sel) { + case 0: return SkString("Courier New"); + case 1: return SkString("Helvetica"); + case 2: return SkString("monospace"); + case 3: return SkString("sans-serif"); + case 4: return SkString("serif"); + case 5: return SkString("Times"); + case 6: return SkString("Times New Roman"); + case 7: + default: + return make_string(); + } +} + +static bool make_bool() { + return R(2) == 1; +} + static SkRect make_rect() { return SkRect::MakeWH(SkIntToScalar(R(static_cast<float>(kBitmapSize))), SkIntToScalar(R(static_cast<float>(kBitmapSize)))); @@ -119,6 +163,50 @@ static SkXfermode::Mode make_xfermode() { return static_cast<SkXfermode::Mode>(R(SkXfermode::kLastMode+1)); } +static SkPaint::Align make_paint_align() { + return static_cast<SkPaint::Align>(R(SkPaint::kRight_Align+1)); +} + +static SkPaint::Hinting make_paint_hinting() { + return static_cast<SkPaint::Hinting>(R(SkPaint::kFull_Hinting+1)); +} + +static SkPaint::Style make_paint_style() { + return static_cast<SkPaint::Style>(R(SkPaint::kStrokeAndFill_Style+1)); +} + +static SkPaint::Cap make_paint_cap() { + return static_cast<SkPaint::Cap>(R(SkPaint::kDefault_Cap+1)); +} + +static SkPaint::Join make_paint_join() { + return static_cast<SkPaint::Join>(R(SkPaint::kDefault_Join+1)); +} + +static SkPaint::TextEncoding make_paint_text_encoding() { + return static_cast<SkPaint::TextEncoding>(R(SkPaint::kGlyphID_TextEncoding+1)); +} + +static SkBlurStyle make_blur_style() { + return static_cast<SkBlurStyle>(R(kLastEnum_SkBlurStyle+1)); +} + +static SkBlurMaskFilter::BlurFlags make_blur_mask_filter_flag() { + return static_cast<SkBlurMaskFilter::BlurFlags>(R(SkBlurMaskFilter::kAll_BlurFlag+1)); +} + +static SkFilterQuality make_filter_quality() { + return static_cast<SkFilterQuality>(R(kHigh_SkFilterQuality+1)); +} + +static SkTypeface::Style make_typeface_style() { + return static_cast<SkTypeface::Style>(R(SkTypeface::kBoldItalic+1)); +} + +static SkPath1DPathEffect::Style make_path_1d_path_effect_style() { + return static_cast<SkPath1DPathEffect::Style>(R(SkPath1DPathEffect::kStyleCount)); +} + static SkColor make_color() { return (R(2) == 1) ? 0xFFC0F0A0 : 0xFF000090; } @@ -257,7 +345,213 @@ static void drawSomething(SkCanvas* canvas) { canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint); } -static SkImageFilter* make_image_filter(bool canBeNull = true) { +static void rand_color_table(uint8_t* table) { + for (int i = 0; i < 256; ++i) { + table[i] = R(256); + } +} + +static SkColorFilter* make_color_filter() { + SkColorFilter* colorFilter; + switch (R(6)) { + case 0: { + SkScalar array[20]; + for (int i = 0; i < 20; ++i) { + array[i] = make_scalar(); + } + colorFilter = SkColorMatrixFilter::Create(array); + break; + } + case 1: + colorFilter = SkLumaColorFilter::Create(); + break; + case 2: { + uint8_t tableA[256]; + uint8_t tableR[256]; + uint8_t tableG[256]; + uint8_t tableB[256]; + rand_color_table(tableA); + rand_color_table(tableR); + rand_color_table(tableG); + rand_color_table(tableB); + colorFilter = SkTableColorFilter::CreateARGB(tableA, tableR, tableG, tableB); + break; + } + case 3: + colorFilter = SkColorFilter::CreateModeFilter(make_color(), make_xfermode()); + break; + case 4: + colorFilter = SkColorFilter::CreateLightingFilter(make_color(), make_color()); + break; + case 5: + default: + colorFilter = nullptr; + break; + } + return colorFilter; +} + +static SkPath make_path() { + SkPath path; + int numOps = R(30); + for (int i = 0; i < numOps; ++i) { + switch (R(6)) { + case 0: + path.moveTo(make_scalar(), make_scalar()); + break; + case 1: + path.lineTo(make_scalar(), make_scalar()); + break; + case 2: + path.quadTo(make_scalar(), make_scalar(), make_scalar(), make_scalar()); + break; + case 3: + path.conicTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar()); + break; + case 4: + path.cubicTo(make_scalar(), make_scalar(), make_scalar(), + make_scalar(), make_scalar(), make_scalar()); + break; + case 5: + default: + path.arcTo(make_scalar(), make_scalar(), make_scalar(), make_scalar(), make_scalar()); + break; + + } + } + path.close(); + return path; +} + +static SkPathEffect* make_path_effect(bool canBeNull = true) { + SkPathEffect* pathEffect = nullptr; + if (canBeNull && (R(3) == 1)) { return pathEffect; } + + switch (R(9)) { + case 0: + pathEffect = SkArcToPathEffect::Create(make_scalar(true)); + break; + case 1: { + SkAutoTUnref<SkPathEffect> outer(make_path_effect(false)); + SkAutoTUnref<SkPathEffect> inner(make_path_effect(false)); + pathEffect = SkComposePathEffect::Create(outer, inner); + break; + } + case 2: + pathEffect = SkCornerPathEffect::Create(make_scalar()); + break; + case 3: { + int count = R(10); + SkScalar intervals[10]; + for (int i = 0; i < count; ++i) { + intervals[i] = make_scalar(); + } + pathEffect = SkDashPathEffect::Create(intervals, count, make_scalar()); + break; + } + case 4: + pathEffect = SkDiscretePathEffect::Create(make_scalar(), make_scalar()); + break; + case 5: + pathEffect = SkPath1DPathEffect::Create(make_path(), + make_scalar(), + make_scalar(), + make_path_1d_path_effect_style()); + break; + case 6: + pathEffect = SkLine2DPathEffect::Create(make_scalar(), make_matrix()); + break; + case 7: + pathEffect = SkPath2DPathEffect::Create(make_matrix(), make_path()); + break; + case 8: + default: + pathEffect = SkSumPathEffect::Create(make_path_effect(false), + make_path_effect(false)); + break; + } + return pathEffect; +} + +static SkMaskFilter* make_mask_filter() { + SkMaskFilter* maskFilter; + switch (R(3)) { + case 0: + maskFilter = SkBlurMaskFilter::Create(make_blur_style(), + make_scalar(), + make_blur_mask_filter_flag()); + case 1: { + SkEmbossMaskFilter::Light light; + for (int i = 0; i < 3; ++i) { + light.fDirection[i] = make_scalar(); + } + light.fPad = R(65536); + light.fAmbient = R(256); + light.fSpecular = R(256); + maskFilter = SkEmbossMaskFilter::Create(make_scalar(), + light); + } + case 2: + default: + maskFilter = nullptr; + break; + } + return maskFilter; +} + +static SkImageFilter* make_image_filter(bool canBeNull = true); + +static SkPaint make_paint() { + SkPaint paint; + paint.setHinting(make_paint_hinting()); + paint.setAntiAlias(make_bool()); + paint.setDither(make_bool()); + paint.setLinearText(make_bool()); + paint.setSubpixelText(make_bool()); + paint.setLCDRenderText(make_bool()); + paint.setEmbeddedBitmapText(make_bool()); + paint.setAutohinted(make_bool()); + paint.setVerticalText(make_bool()); + paint.setUnderlineText(make_bool()); + paint.setStrikeThruText(make_bool()); + paint.setFakeBoldText(make_bool()); + paint.setDevKernText(make_bool()); + paint.setFilterQuality(make_filter_quality()); + paint.setStyle(make_paint_style()); + paint.setColor(make_color()); + paint.setStrokeWidth(make_scalar()); + paint.setStrokeMiter(make_scalar()); + paint.setStrokeCap(make_paint_cap()); + paint.setStrokeJoin(make_paint_join()); + paint.setColorFilter(make_color_filter()); + paint.setXfermodeMode(make_xfermode()); + paint.setPathEffect(make_path_effect()); + paint.setMaskFilter(make_mask_filter()); + SkAutoTUnref<SkTypeface> typeface( + SkTypeface::CreateFromName(make_font_name().c_str(), make_typeface_style())); + paint.setTypeface(typeface); + SkLayerRasterizer::Builder rasterizerBuilder; + SkPaint paintForRasterizer; + if (R(2) == 1) { + paintForRasterizer = make_paint(); + } + rasterizerBuilder.addLayer(paintForRasterizer); + SkAutoTUnref<SkRasterizer> rasterizer(rasterizerBuilder.detachRasterizer()); + paint.setRasterizer(rasterizer); + paint.setImageFilter(make_image_filter()); + SkAutoDataUnref data(make_3Dlut(nullptr, make_bool(), make_bool(), make_bool())); + SkAutoTUnref<SkAnnotation> annotation( + SkAnnotation::Create(make_string().c_str(), data)); + paint.setAnnotation(annotation); + paint.setTextAlign(make_paint_align()); + paint.setTextSize(make_scalar()); + paint.setTextScaleX(make_scalar()); + paint.setTextSkewX(make_scalar()); + paint.setTextEncoding(make_paint_text_encoding()); + return paint; +} + +static SkImageFilter* make_image_filter(bool canBeNull) { SkImageFilter* filter = 0; // Add a 1 in 3 chance to get a nullptr input @@ -266,7 +560,7 @@ static SkImageFilter* make_image_filter(bool canBeNull = true) { enum { ALPHA_THRESHOLD, MERGE, COLOR, LUT3D, BLUR, MAGNIFIER, DOWN_SAMPLE, XFERMODE, OFFSET, MATRIX, MATRIX_CONVOLUTION, COMPOSE, DISTANT_LIGHT, POINT_LIGHT, SPOT_LIGHT, NOISE, DROP_SHADOW, - MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, NUM_FILTERS }; + MORPHOLOGY, BITMAP, DISPLACE, TILE, PICTURE, PAINT, NUM_FILTERS }; switch (R(NUM_FILTERS)) { case ALPHA_THRESHOLD: @@ -277,9 +571,7 @@ static SkImageFilter* make_image_filter(bool canBeNull = true) { break; case COLOR: { - SkAutoTUnref<SkColorFilter> cf((R(2) == 1) ? - SkColorFilter::CreateModeFilter(make_color(), make_xfermode()) : - SkColorFilter::CreateLightingFilter(make_color(), make_color())); + SkAutoTUnref<SkColorFilter> cf(make_color_filter()); filter = cf.get() ? SkColorFilterImageFilter::Create(cf, make_image_filter()) : 0; } break; @@ -372,9 +664,11 @@ static SkImageFilter* make_image_filter(bool canBeNull = true) { make_scalar(true), make_scalar(true), R(10.0f), make_scalar()) : SkPerlinNoiseShader::CreateTurbulence( make_scalar(true), make_scalar(true), R(10.0f), make_scalar())); + SkPaint paint; + paint.setShader(shader); SkImageFilter::CropRect cropR(SkRect::MakeWH(SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize))); - filter = SkRectShaderImageFilter::Create(shader, &cropR); + filter = SkPaintImageFilter::Create(paint, &cropR); } break; case DROP_SHADOW: @@ -421,6 +715,11 @@ static SkImageFilter* make_image_filter(bool canBeNull = true) { filter = SkPictureImageFilter::Create(pict.get(), make_rect()); } break; + case PAINT: + { + SkImageFilter::CropRect cropR(make_rect()); + filter = SkPaintImageFilter::Create(make_paint(), &cropR); + } default: break; } diff --git a/src/core/SkPathEffect.cpp b/src/core/SkPathEffect.cpp index 2403ffcb30..293bb53b2c 100644 --- a/src/core/SkPathEffect.cpp +++ b/src/core/SkPathEffect.cpp @@ -67,7 +67,11 @@ void SkPairPathEffect::toString(SkString* str) const { SkFlattenable* SkComposePathEffect::CreateProc(SkReadBuffer& buffer) { SkAutoTUnref<SkPathEffect> pe0(buffer.readPathEffect()); SkAutoTUnref<SkPathEffect> pe1(buffer.readPathEffect()); - return SkComposePathEffect::Create(pe0, pe1); + if (pe0 && pe1) { + return SkComposePathEffect::Create(pe0, pe1); + } else { + return nullptr; + } } bool SkComposePathEffect::filterPath(SkPath* dst, const SkPath& src, @@ -100,7 +104,11 @@ void SkComposePathEffect::toString(SkString* str) const { SkFlattenable* SkSumPathEffect::CreateProc(SkReadBuffer& buffer) { SkAutoTUnref<SkPathEffect> pe0(buffer.readPathEffect()); SkAutoTUnref<SkPathEffect> pe1(buffer.readPathEffect()); - return SkSumPathEffect::Create(pe0, pe1); + if (pe0 && pe1) { + return SkSumPathEffect::Create(pe0, pe1); + } else { + return nullptr; + } } bool SkSumPathEffect::filterPath(SkPath* dst, const SkPath& src, diff --git a/src/effects/SkPaintImageFilter.cpp b/src/effects/SkPaintImageFilter.cpp new file mode 100644 index 0000000000..d141f34dfb --- /dev/null +++ b/src/effects/SkPaintImageFilter.cpp @@ -0,0 +1,81 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkPaintImageFilter.h" +#include "SkBitmap.h" +#include "SkCanvas.h" +#include "SkDevice.h" +#include "SkReadBuffer.h" +#include "SkWriteBuffer.h" + +SkImageFilter* SkPaintImageFilter::Create(const SkPaint& paint, const CropRect* cropRect) { + return new SkPaintImageFilter(paint, cropRect); +} + +SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cropRect) + : INHERITED(0, nullptr, cropRect) + , fPaint(paint) { +} + +SkFlattenable* SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) { + SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); + SkPaint paint; + buffer.readPaint(&paint); + return Create(paint, &common.cropRect()); +} + +void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const { + this->INHERITED::flatten(buffer); + buffer.writePaint(fPaint); +} + +bool SkPaintImageFilter::onFilterImage(Proxy* proxy, + const SkBitmap& source, + const Context& ctx, + SkBitmap* result, + SkIPoint* offset) const { + SkIRect bounds; + if (!this->applyCropRect(ctx, source, SkIPoint::Make(0, 0), &bounds)) { + return false; + } + + SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), + bounds.height())); + if (nullptr == device.get()) { + return false; + } + SkCanvas canvas(device.get()); + + SkMatrix matrix(ctx.ctm()); + matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); + SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bounds.height())); + SkMatrix inverse; + if (matrix.invert(&inverse)) { + inverse.mapRect(&rect); + } + canvas.setMatrix(matrix); + canvas.drawRect(rect, fPaint); + + *result = device.get()->accessBitmap(false); + offset->fX = bounds.fLeft; + offset->fY = bounds.fTop; + return true; +} + +bool SkPaintImageFilter::canComputeFastBounds() const { + // http:skbug.com/4627: "make computeFastBounds and onFilterBounds() CropRect-aware" + // computeFastBounds() doesn't currently take the crop rect into account, + // so we can't compute it. If a full crop rect is set, we should return true here. + return false; +} + +#ifndef SK_IGNORE_TO_STRING +void SkPaintImageFilter::toString(SkString* str) const { + str->appendf("SkPaintImageFilter: ("); + str->append(")"); +} +#endif diff --git a/src/ports/SkGlobalInitialization_chromium.cpp b/src/ports/SkGlobalInitialization_chromium.cpp index f86fdedf43..a756743d1f 100644 --- a/src/ports/SkGlobalInitialization_chromium.cpp +++ b/src/ports/SkGlobalInitialization_chromium.cpp @@ -55,6 +55,7 @@ #include "SkMorphologyImageFilter.h" #include "SkOffsetImageFilter.h" #include "SkOnce.h" +#include "SkPaintImageFilter.h" #include "SkPerlinNoiseShader.h" #include "SkPictureImageFilter.h" #include "SkPictureShader.h" @@ -116,6 +117,7 @@ public: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath1DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLine2DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkModeColorFilter) + SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPaintImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath2DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPerlinNoiseShader) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPictureImageFilter) diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp index e710806698..2b7fda021c 100644 --- a/src/ports/SkGlobalInitialization_default.cpp +++ b/src/ports/SkGlobalInitialization_default.cpp @@ -51,6 +51,7 @@ #include "SkMorphologyImageFilter.h" #include "SkOffsetImageFilter.h" #include "SkOnce.h" +#include "SkPaintImageFilter.h" #include "SkPerlinNoiseShader.h" #include "SkPictureImageFilter.h" #include "SkPictureShader.h" @@ -95,6 +96,7 @@ public: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath1DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLine2DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkModeColorFilter) + SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPaintImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath2DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPerlinNoiseShader) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPictureImageFilter) diff --git a/tests/ImageFilterTest.cpp b/tests/ImageFilterTest.cpp index fa1569d89a..c6991cd052 100644 --- a/tests/ImageFilterTest.cpp +++ b/tests/ImageFilterTest.cpp @@ -23,6 +23,7 @@ #include "SkMergeImageFilter.h" #include "SkMorphologyImageFilter.h" #include "SkOffsetImageFilter.h" +#include "SkPaintImageFilter.h" #include "SkPerlinNoiseShader.h" #include "SkPicture.h" #include "SkPictureImageFilter.h" @@ -30,7 +31,6 @@ #include "SkPoint3.h" #include "SkReadBuffer.h" #include "SkRect.h" -#include "SkRectShaderImageFilter.h" #include "SkSurface.h" #include "SkTableColorFilter.h" #include "SkTileImageFilter.h" @@ -439,14 +439,15 @@ DEF_TEST(ImageFilterDrawTiled, reporter) { SkAutoTUnref<SkPicture> picture(recorder.endRecording()); SkAutoTUnref<SkImageFilter> pictureFilter(SkPictureImageFilter::Create(picture.get())); SkAutoTUnref<SkShader> shader(SkPerlinNoiseShader::CreateTurbulence(SK_Scalar1, SK_Scalar1, 1, 0)); + SkPaint noisePaint; + noisePaint.setShader(shader); - SkAutoTUnref<SkImageFilter> rectShaderFilter(SkRectShaderImageFilter::Create(shader.get())); + SkAutoTUnref<SkImageFilter> paintFilter(SkPaintImageFilter::Create(noisePaint)); - SkAutoTUnref<SkShader> greenColorShader(SkShader::CreateColorShader(SK_ColorGREEN)); SkImageFilter::CropRect leftSideCropRect(SkRect::MakeXYWH(0, 0, 32, 64)); - SkAutoTUnref<SkImageFilter> rectShaderFilterLeft(SkRectShaderImageFilter::Create(greenColorShader.get(), &leftSideCropRect)); + SkAutoTUnref<SkImageFilter> paintFilterLeft(SkPaintImageFilter::Create(greenPaint, &leftSideCropRect)); SkImageFilter::CropRect rightSideCropRect(SkRect::MakeXYWH(32, 0, 32, 64)); - SkAutoTUnref<SkImageFilter> rectShaderFilterRight(SkRectShaderImageFilter::Create(greenColorShader.get(), &rightSideCropRect)); + SkAutoTUnref<SkImageFilter> paintFilterRight(SkPaintImageFilter::Create(greenPaint, &rightSideCropRect)); struct { const char* fName; @@ -471,7 +472,7 @@ DEF_TEST(ImageFilterDrawTiled, reporter) { SkMatrixConvolutionImageFilter::kRepeat_TileMode, false) }, { "merge", SkMergeImageFilter::Create(nullptr, nullptr, SkXfermode::kSrcOver_Mode) }, { "merge with disjoint inputs", SkMergeImageFilter::Create( - rectShaderFilterLeft, rectShaderFilterRight, SkXfermode::kSrcOver_Mode) }, + paintFilterLeft, paintFilterRight, SkXfermode::kSrcOver_Mode) }, { "offset", SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1) }, { "dilate", SkDilateImageFilter::Create(3, 2) }, { "erode", SkErodeImageFilter::Create(2, 3) }, @@ -480,7 +481,7 @@ DEF_TEST(ImageFilterDrawTiled, reporter) { { "matrix", SkImageFilter::CreateMatrixFilter(matrix, kLow_SkFilterQuality) }, { "blur and offset", SkOffsetImageFilter::Create(five, five, blur.get()) }, { "picture and blur", SkBlurImageFilter::Create(five, five, pictureFilter.get()) }, - { "rect shader and blur", SkBlurImageFilter::Create(five, five, rectShaderFilter.get()) }, + { "paint and blur", SkBlurImageFilter::Create(five, five, paintFilter.get()) }, }; SkBitmap untiledResult, tiledResult; diff --git a/tests/PaintImageFilterTest.cpp b/tests/PaintImageFilterTest.cpp new file mode 100644 index 0000000000..b71fe47119 --- /dev/null +++ b/tests/PaintImageFilterTest.cpp @@ -0,0 +1,113 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkCanvas.h" +#include "SkGradientShader.h" +#include "SkPaintImageFilter.h" +#include "SkShader.h" +#include "Test.h" + +static void test_unscaled(skiatest::Reporter* reporter) { + int w = 10, h = 10; + SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); + + SkBitmap filterResult, paintResult; + + filterResult.allocN32Pixels(w, h); + SkCanvas canvasFilter(filterResult); + canvasFilter.clear(0x00000000); + + paintResult.allocN32Pixels(w, h); + SkCanvas canvasPaint(paintResult); + canvasPaint.clear(0x00000000); + + SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5)); + SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN}; + SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1}; + SkScalar radius = SkIntToScalar(5); + + SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial( + center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode)); + SkPaint gradientPaint; + gradientPaint.setShader(s); + + // Test using the image filter + { + SkPaint paint; + SkImageFilter::CropRect cr(r); + paint.setImageFilter(SkPaintImageFilter::Create(gradientPaint, &cr))->unref(); + canvasFilter.drawRect(r, paint); + } + + // Test using the paint directly + { + canvasPaint.drawRect(r, gradientPaint); + } + + // Assert that both paths yielded the same result + for (int y = 0; y < r.height(); ++y) { + const SkPMColor* filterPtr = filterResult.getAddr32(0, y); + const SkPMColor* paintPtr = paintResult.getAddr32(0, y); + for (int x = 0; x < r.width(); ++x, ++filterPtr, ++paintPtr) { + REPORTER_ASSERT(reporter, *filterPtr == *paintPtr); + } + } +} + +static void test_scaled(skiatest::Reporter* reporter) { + int w = 10, h = 10; + SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); + + SkBitmap filterResult, paintResult; + + filterResult.allocN32Pixels(w, h); + SkCanvas canvasFilter(filterResult); + canvasFilter.clear(0x00000000); + + paintResult.allocN32Pixels(w, h); + SkCanvas canvasPaint(paintResult); + canvasPaint.clear(0x00000000); + + SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5)); + SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN}; + SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1}; + SkScalar radius = SkIntToScalar(5); + + SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial( + center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode)); + SkPaint gradientPaint; + gradientPaint.setShader(s); + + // Test using the image filter + { + SkPaint paint; + SkImageFilter::CropRect cr(r); + paint.setImageFilter(SkPaintImageFilter::Create(gradientPaint, &cr))->unref(); + canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2)); + canvasFilter.drawRect(r, paint); + } + + // Test using the paint directly + { + canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2)); + canvasPaint.drawRect(r, gradientPaint); + } + + // Assert that both paths yielded the same result + for (int y = 0; y < r.height(); ++y) { + const SkPMColor* filterPtr = filterResult.getAddr32(0, y); + const SkPMColor* paintPtr = paintResult.getAddr32(0, y); + for (int x = 0; x < r.width(); ++x, ++filterPtr, ++paintPtr) { + REPORTER_ASSERT(reporter, *filterPtr == *paintPtr); + } + } +} + +DEF_TEST(PaintImageFilter, reporter) { + test_unscaled(reporter); + test_scaled(reporter); +} |