2012-08-20 14:53:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <utility>
|
2012-08-20 14:53:21 +00:00
|
|
|
|
|
|
|
class ImageFiltersGraphGM : public skiagm::GM {
|
|
|
|
public:
|
2014-05-27 16:28:43 +00:00
|
|
|
ImageFiltersGraphGM() {}
|
2012-08-20 14:53:21 +00:00
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkString onShortName() override {
|
2012-08-20 14:53:21 +00:00
|
|
|
return SkString("imagefiltersgraph");
|
|
|
|
}
|
|
|
|
|
2016-01-26 16:41:02 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(600, 150); }
|
2012-08-20 14:53:21 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = SkImage::MakeFromBitmap(
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::create_string_bitmap(100, 100, SK_ColorWHITE, 20, 70, 96, "e"));
|
2014-05-27 16:28:43 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-03-27 19:33:46 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
2012-10-24 15:14:26 +00:00
|
|
|
{
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> bitmapSource(SkImageFilters::Image(fImage));
|
2019-04-08 20:23:20 +00:00
|
|
|
sk_sp<SkColorFilter> cf(SkColorFilters::Blend(SK_ColorRED,
|
2016-10-28 19:42:34 +00:00
|
|
|
SkBlendMode::kSrcIn));
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> blur(SkImageFilters::Blur(4.0f, 4.0f, std::move(bitmapSource)));
|
|
|
|
sk_sp<SkImageFilter> erode(SkImageFilters::Erode(4, 4, blur));
|
|
|
|
sk_sp<SkImageFilter> color(SkImageFilters::ColorFilter(std::move(cf),
|
2016-04-05 18:50:42 +00:00
|
|
|
std::move(erode)));
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> merge(SkImageFilters::Merge(blur, color));
|
2012-10-24 15:14:26 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2016-03-30 20:34:16 +00:00
|
|
|
paint.setImageFilter(std::move(merge));
|
2012-10-24 15:14:26 +00:00
|
|
|
canvas->drawPaint(paint);
|
2013-10-24 15:59:31 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2012-10-24 15:14:26 +00:00
|
|
|
}
|
|
|
|
{
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> morph(SkImageFilters::Dilate(5, 5, nullptr));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2019-04-30 16:18:54 +00:00
|
|
|
float matrix[20] = { 1, 0, 0, 0, 0,
|
|
|
|
0, 1, 0, 0, 0,
|
|
|
|
0, 0, 1, 0, 0,
|
|
|
|
0, 0, 0, 0.5f, 0 };
|
2012-08-20 14:53:21 +00:00
|
|
|
|
2019-04-30 16:18:54 +00:00
|
|
|
sk_sp<SkColorFilter> matrixFilter(SkColorFilters::Matrix(matrix));
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> colorMorph(SkImageFilters::ColorFilter(std::move(matrixFilter),
|
2016-04-05 18:50:42 +00:00
|
|
|
std::move(morph)));
|
2012-10-24 15:14:26 +00:00
|
|
|
SkPaint paint;
|
2020-10-09 14:45:07 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blend(SkBlendMode::kSrcOver,
|
|
|
|
std::move(colorMorph)));
|
2016-03-29 18:32:50 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
DrawClippedImage(canvas, fImage.get(), paint);
|
2013-10-24 15:59:31 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2012-10-24 15:14:26 +00:00
|
|
|
}
|
2013-10-24 01:46:11 +00:00
|
|
|
{
|
2019-04-30 16:18:54 +00:00
|
|
|
float matrix[20] = { 1, 0, 0, 0, 0,
|
|
|
|
0, 1, 0, 0, 0,
|
|
|
|
0, 0, 1, 0, 0,
|
|
|
|
0, 0, 0, 0.5f, 0 };
|
|
|
|
sk_sp<SkColorFilter> matrixCF(SkColorFilters::Matrix(matrix));
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> matrixFilter(SkImageFilters::ColorFilter(std::move(matrixCF),
|
2016-04-05 18:50:42 +00:00
|
|
|
nullptr));
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> offsetFilter(SkImageFilters::Offset(10.0f, 10.f, matrixFilter));
|
2013-10-24 01:46:11 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Arithmetic(
|
2017-01-09 15:48:23 +00:00
|
|
|
0, 1, 1, 0, true, std::move(matrixFilter), std::move(offsetFilter), nullptr));
|
2016-03-29 18:32:50 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
DrawClippedImage(canvas, fImage.get(), paint);
|
2013-10-24 15:59:31 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
}
|
|
|
|
{
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> blur(SkImageFilters::Blur(10, 10, nullptr));
|
2013-10-24 15:59:31 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRect = SkIRect::MakeWH(95, 100);
|
2013-10-24 15:59:31 +00:00
|
|
|
SkPaint paint;
|
2016-03-29 18:32:50 +00:00
|
|
|
paint.setImageFilter(
|
2020-10-09 14:45:07 +00:00
|
|
|
SkImageFilters::Blend(SkBlendMode::kSrcIn, std::move(blur), nullptr, &cropRect));
|
2016-03-17 17:51:11 +00:00
|
|
|
DrawClippedImage(canvas, fImage.get(), paint);
|
2013-10-24 15:59:31 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2013-10-24 01:46:11 +00:00
|
|
|
}
|
2016-01-26 16:41:02 +00:00
|
|
|
{
|
|
|
|
// Dilate -> matrix convolution.
|
|
|
|
// This tests that a filter using asFragmentProcessor (matrix
|
|
|
|
// convolution) correctly handles a non-zero source offset
|
|
|
|
// (supplied by the dilate).
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> dilate(SkImageFilters::Dilate(5, 5, nullptr));
|
2016-01-26 16:41:02 +00:00
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
SkScalar kernel[9] = { -1, -1, -1,
|
|
|
|
-1, 7, -1,
|
|
|
|
-1, -1, -1 };
|
2016-01-26 16:41:02 +00:00
|
|
|
SkISize kernelSize = SkISize::Make(3, 3);
|
2019-08-02 19:21:23 +00:00
|
|
|
SkScalar gain = 1.0f, bias = 0;
|
2016-01-26 16:41:02 +00:00
|
|
|
SkIPoint kernelOffset = SkIPoint::Make(1, 1);
|
|
|
|
bool convolveAlpha = false;
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> convolve(SkImageFilters::MatrixConvolution(
|
|
|
|
kernelSize, kernel, gain, bias, kernelOffset, SkTileMode::kClamp, convolveAlpha,
|
|
|
|
std::move(dilate)));
|
2016-01-26 16:41:02 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(convolve));
|
2016-03-17 17:51:11 +00:00
|
|
|
DrawClippedImage(canvas, fImage.get(), paint);
|
2016-01-26 16:41:02 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
}
|
Make SkImageFilter crop rects relative to the primitive origin, instead of relative to their parent's crop rect. This is required by SVG semantics, and is more sane anyway.
To do this, this patch changes the "offset/loc" parameter in filterImage() / onFilterImage() from an inout-param to an out-param only, so that the calling filter can know how much the input filter wants its result offset (and doesn't include the original primitive position). This offset can then be applied to the current filter's crop rect. (I've renamed the parameter "offset" in all cases to make this clear.) This makes the call sites in SkCanvas/SkGpuDevice responsible for applying the resulting offset to the primitive's position, which is actually a fairly small change.
This change also fixes SkTileImageFilter and SkOffsetImageFilter to correctly handle an input offset, which they weren't before. This required modifying the GM's, since they assumed the broken behaviour.
NOTE: this will require rebaselining the imagefiltersgraph test, since it has a new test case.
NOTE: this will "break" the Blink layout tests css3/filters/effect-reference-subregion-chained-hw.html and css3/filters/effect-reference-subregion-hw.html, but it actually makes them give correct results. It should be suppressed on the skia roll, and I'll rebaseline it.
R=reed@google.com
Review URL: https://codereview.chromium.org/112803004
git-svn-id: http://skia.googlecode.com/svn/trunk@12895 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-03 21:48:22 +00:00
|
|
|
{
|
|
|
|
// Test that crop offsets are absolute, not relative to the parent's crop rect.
|
2019-04-08 20:23:20 +00:00
|
|
|
sk_sp<SkColorFilter> cf1(SkColorFilters::Blend(SK_ColorBLUE, SkBlendMode::kSrcIn));
|
|
|
|
sk_sp<SkColorFilter> cf2(SkColorFilters::Blend(SK_ColorGREEN, SkBlendMode::kSrcIn));
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect outerRect = SkIRect::MakeXYWH(10, 10, 80, 80);
|
|
|
|
SkIRect innerRect= SkIRect::MakeXYWH(20, 20, 60, 60);
|
|
|
|
sk_sp<SkImageFilter> color1(SkImageFilters::ColorFilter(
|
|
|
|
std::move(cf1), nullptr, &outerRect));
|
|
|
|
sk_sp<SkImageFilter> color2(SkImageFilters::ColorFilter(
|
|
|
|
std::move(cf2), std::move(color1), &innerRect));
|
Make SkImageFilter crop rects relative to the primitive origin, instead of relative to their parent's crop rect. This is required by SVG semantics, and is more sane anyway.
To do this, this patch changes the "offset/loc" parameter in filterImage() / onFilterImage() from an inout-param to an out-param only, so that the calling filter can know how much the input filter wants its result offset (and doesn't include the original primitive position). This offset can then be applied to the current filter's crop rect. (I've renamed the parameter "offset" in all cases to make this clear.) This makes the call sites in SkCanvas/SkGpuDevice responsible for applying the resulting offset to the primitive's position, which is actually a fairly small change.
This change also fixes SkTileImageFilter and SkOffsetImageFilter to correctly handle an input offset, which they weren't before. This required modifying the GM's, since they assumed the broken behaviour.
NOTE: this will require rebaselining the imagefiltersgraph test, since it has a new test case.
NOTE: this will "break" the Blink layout tests css3/filters/effect-reference-subregion-chained-hw.html and css3/filters/effect-reference-subregion-hw.html, but it actually makes them give correct results. It should be suppressed on the skia roll, and I'll rebaseline it.
R=reed@google.com
Review URL: https://codereview.chromium.org/112803004
git-svn-id: http://skia.googlecode.com/svn/trunk@12895 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-03 21:48:22 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2016-04-05 16:09:36 +00:00
|
|
|
paint.setImageFilter(std::move(color2));
|
2015-03-27 19:33:46 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
Make SkImageFilter crop rects relative to the primitive origin, instead of relative to their parent's crop rect. This is required by SVG semantics, and is more sane anyway.
To do this, this patch changes the "offset/loc" parameter in filterImage() / onFilterImage() from an inout-param to an out-param only, so that the calling filter can know how much the input filter wants its result offset (and doesn't include the original primitive position). This offset can then be applied to the current filter's crop rect. (I've renamed the parameter "offset" in all cases to make this clear.) This makes the call sites in SkCanvas/SkGpuDevice responsible for applying the resulting offset to the primitive's position, which is actually a fairly small change.
This change also fixes SkTileImageFilter and SkOffsetImageFilter to correctly handle an input offset, which they weren't before. This required modifying the GM's, since they assumed the broken behaviour.
NOTE: this will require rebaselining the imagefiltersgraph test, since it has a new test case.
NOTE: this will "break" the Blink layout tests css3/filters/effect-reference-subregion-chained-hw.html and css3/filters/effect-reference-subregion-hw.html, but it actually makes them give correct results. It should be suppressed on the skia roll, and I'll rebaseline it.
R=reed@google.com
Review URL: https://codereview.chromium.org/112803004
git-svn-id: http://skia.googlecode.com/svn/trunk@12895 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-01-03 21:48:22 +00:00
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 100), paint);
|
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
}
|
2012-08-20 14:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-15 18:26:13 +00:00
|
|
|
static void DrawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(SkRect::MakeIWH(image->width(), image->height()));
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2015-09-15 18:26:13 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2012-08-20 14:53:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
DEF_GM(return new ImageFiltersGraphGM;)
|