2015-04-20 14:27:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkFilterQuality.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
2020-03-02 21:27:03 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/effects/SkColorMatrix.h"
|
2020-03-02 21:27:03 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/effects/SkHighContrastFilter.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2020-03-02 21:27:03 +00:00
|
|
|
#include "include/effects/SkShaderMaskFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2015-04-20 14:27:15 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
/**
|
|
|
|
* Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see
|
|
|
|
* that we apply the xfermode *after* the image has been created and filtered, and not during
|
|
|
|
* the creation step (i.e. before it is filtered).
|
|
|
|
*
|
2015-11-07 13:29:00 +00:00
|
|
|
* see https://bug.skia.org/3741
|
2015-04-20 14:27:15 +00:00
|
|
|
*/
|
2016-10-06 00:33:02 +00:00
|
|
|
static void do_draw(SkCanvas* canvas, SkBlendMode mode, sk_sp<SkImageFilter> imf) {
|
2015-04-20 16:10:31 +00:00
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->clipRect(SkRect::MakeWH(220, 220));
|
2015-12-10 20:08:44 +00:00
|
|
|
|
2015-04-20 16:10:31 +00:00
|
|
|
// want to force a layer, so modes like DstIn can combine meaningfully, but the final
|
|
|
|
// image can still be shown against our default (opaque) background. non-opaque GMs
|
|
|
|
// are a lot more trouble to compare/triage.
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->drawColor(SK_ColorGREEN);
|
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-12-10 20:08:44 +00:00
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
|
|
|
|
SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
|
2015-12-10 20:08:44 +00:00
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawOval(r0, paint);
|
|
|
|
|
2015-04-20 16:10:31 +00:00
|
|
|
paint.setColor(0x660000FF);
|
2016-04-05 22:09:00 +00:00
|
|
|
paint.setImageFilter(std::move(imf));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(mode);
|
2015-04-20 14:27:15 +00:00
|
|
|
canvas->drawOval(r1, paint);
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2015-04-20 14:27:15 +00:00
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) {
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->translate(10, 10);
|
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
// just need an imagefilter to trigger the code-path (which creates a tmp layer)
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> imf(SkImageFilters::MatrixTransform(SkMatrix::I(),
|
2016-04-05 22:09:00 +00:00
|
|
|
kNone_SkFilterQuality,
|
|
|
|
nullptr));
|
2015-04-20 14:27:15 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
const SkBlendMode modes[] = {
|
|
|
|
SkBlendMode::kSrcATop, SkBlendMode::kDstIn
|
2015-04-20 14:27:15 +00:00
|
|
|
};
|
2015-12-10 20:08:44 +00:00
|
|
|
|
2015-04-20 14:27:15 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
|
|
|
|
canvas->save();
|
2015-09-09 15:16:41 +00:00
|
|
|
do_draw(canvas, modes[i], nullptr);
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->translate(240, 0);
|
2015-09-09 15:16:41 +00:00
|
|
|
do_draw(canvas, modes[i], imf);
|
2015-04-20 14:27:15 +00:00
|
|
|
canvas->restore();
|
2015-12-10 20:08:44 +00:00
|
|
|
|
2015-04-20 16:10:31 +00:00
|
|
|
canvas->translate(0, 240);
|
2015-04-20 14:27:15 +00:00
|
|
|
}
|
2015-09-09 15:16:41 +00:00
|
|
|
}
|
2015-12-07 17:28:34 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* canvas) {
|
2016-12-29 16:50:48 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::MakeS32(100, 100, kPremul_SkAlphaType);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface(ToolUtils::makeSurface(canvas, info));
|
2015-12-07 17:28:34 +00:00
|
|
|
surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-12-07 17:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare blurs when we're tightly clipped (fast) and not as tightly (slower)
|
|
|
|
//
|
|
|
|
// Expect the two to draw the same (modulo the extra border of pixels when the clip is larger)
|
|
|
|
//
|
|
|
|
DEF_SIMPLE_GM(fast_slow_blurimagefilter, canvas, 620, 260) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(make_image(canvas));
|
2015-12-07 17:28:34 +00:00
|
|
|
const SkRect r = SkRect::MakeIWH(image->width(), image->height());
|
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
|
|
|
|
SkPaint paint;
|
2019-08-02 19:21:23 +00:00
|
|
|
paint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
|
2015-12-07 17:28:34 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
// we outset the clip by 1, to fall out of the fast-case in drawImage
|
|
|
|
// i.e. the clip is larger than the image
|
|
|
|
for (SkScalar outset = 0; outset <= 1; ++outset) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r.makeOutset(outset, outset));
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + 20);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(r.width() + 20, 0);
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 19:28:08 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-08 15:01:20 +00:00
|
|
|
static void draw_set(SkCanvas* canvas, sk_sp<SkImageFilter> filters[], int count) {
|
2016-01-07 19:28:08 +00:00
|
|
|
const SkRect r = SkRect::MakeXYWH(30, 30, 200, 200);
|
|
|
|
const SkScalar offset = 250;
|
|
|
|
SkScalar dx = 0, dy = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
canvas->save();
|
|
|
|
SkRRect rr = SkRRect::MakeRectXY(r.makeOffset(dx, dy), 20, 20);
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipRRect(rr, true);
|
2017-04-28 17:48:37 +00:00
|
|
|
canvas->saveLayer({ &rr.getBounds(), nullptr, filters[i].get(), nullptr, nullptr, 0 });
|
2016-01-07 19:28:08 +00:00
|
|
|
canvas->drawColor(0x40FFFFFF);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
if (0 == dx) {
|
|
|
|
dx = offset;
|
|
|
|
} else {
|
|
|
|
dx = 0;
|
|
|
|
dy = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 00:34:19 +00:00
|
|
|
class SaveLayerWithBackdropGM : public skiagm::GM {
|
|
|
|
protected:
|
|
|
|
bool runAsBench() const override { return true; }
|
|
|
|
SkString onShortName() override { return SkString("savelayer_with_backdrop"); }
|
|
|
|
SkISize onISize() override { return SkISize::Make(830, 550); }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkColorMatrix cm;
|
|
|
|
cm.setSaturation(10);
|
|
|
|
sk_sp<SkColorFilter> cf(SkColorFilters::Matrix(cm));
|
|
|
|
const SkScalar kernel[] = { 4, 0, 4, 0, -15, 0, 4, 0, 4 };
|
|
|
|
sk_sp<SkImageFilter> filters[] = {
|
|
|
|
SkImageFilters::Blur(10, 10, nullptr),
|
|
|
|
SkImageFilters::Dilate(8, 8, nullptr),
|
|
|
|
SkImageFilters::MatrixConvolution({ 3, 3 }, kernel, 1, 0, { 0, 0 },
|
|
|
|
SkTileMode::kDecal, true, nullptr),
|
|
|
|
SkImageFilters::ColorFilter(std::move(cf), nullptr),
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
SkScalar fSx, fSy, fTx, fTy;
|
|
|
|
} xforms[] = {
|
|
|
|
{ 1, 1, 0, 0 },
|
|
|
|
{ 0.5f, 0.5f, 530, 0 },
|
|
|
|
{ 0.25f, 0.25f, 530, 275 },
|
|
|
|
{ 0.125f, 0.125f, 530, 420 },
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(kMedium_SkFilterQuality);
|
|
|
|
sk_sp<SkImage> image(GetResourceAsImage("images/mandrill_512.png"));
|
|
|
|
|
|
|
|
canvas->translate(20, 20);
|
|
|
|
for (const auto& xform : xforms) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(xform.fTx, xform.fTy);
|
|
|
|
canvas->scale(xform.fSx, xform.fSy);
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
draw_set(canvas, filters, SK_ARRAY_COUNT(filters));
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2016-01-07 19:28:08 +00:00
|
|
|
}
|
2019-08-14 00:34:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new SaveLayerWithBackdropGM();)
|
2020-03-02 21:27:03 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Test that color filters and mask filters are applied before the image filter, even if it would
|
|
|
|
// normally be a sprite draw that could avoid an auto-saveLayer.
|
|
|
|
DEF_SIMPLE_GM(imagefilters_effect_order, canvas, 512, 512) {
|
|
|
|
sk_sp<SkImage> image(GetResourceAsImage("images/mandrill_256.png"));
|
|
|
|
if (canvas->getGrContext()) {
|
2020-03-03 14:49:35 +00:00
|
|
|
sk_sp<SkImage> gpuImage = image->makeTextureImage(canvas->getGrContext());
|
|
|
|
if (gpuImage) {
|
|
|
|
image = std::move(gpuImage);
|
|
|
|
}
|
2020-03-02 21:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize kernelSize = SkISize::Make(3, 3);
|
|
|
|
SkIPoint kernelOffset = SkIPoint::Make(1, 1);
|
|
|
|
// A Laplacian edge detector, ie https://en.wikipedia.org/wiki/Kernel_(image_processing)
|
|
|
|
SkScalar kernel[9] = {-1.f, -1.f, -1.f,
|
|
|
|
-1.f, 8.f, -1.f,
|
|
|
|
-1.f, -1.f, -1.f};
|
|
|
|
auto edgeDetector = SkImageFilters::MatrixConvolution(
|
|
|
|
kernelSize, kernel, 1.f, 0.f, kernelOffset, SkTileMode::kClamp, false, nullptr);
|
|
|
|
// This uses the high contrast filter because it resembles a pre-processing step you may perform
|
|
|
|
// prior to edge detection. The specifics of the high contrast algorithm don't matter for the GM
|
|
|
|
auto edgeAmplify = SkHighContrastFilter::Make(
|
|
|
|
{false, SkHighContrastConfig::InvertStyle::kNoInvert, 0.5f});
|
|
|
|
|
|
|
|
SkPaint testCFPaint;
|
|
|
|
testCFPaint.setColorFilter(edgeAmplify);
|
|
|
|
testCFPaint.setImageFilter(edgeDetector);
|
|
|
|
|
|
|
|
// The expected result is color filter then image filter, so represent this explicitly in the
|
|
|
|
// image filter graph.
|
|
|
|
SkPaint expectedCFPaint;
|
|
|
|
expectedCFPaint.setImageFilter(SkImageFilters::Compose(edgeDetector,
|
|
|
|
SkImageFilters::ColorFilter(edgeAmplify, nullptr)));
|
|
|
|
|
|
|
|
// Draw the image twice (expected on the left, test on the right that should match)
|
|
|
|
SkRect crop = SkRect::Make(image->bounds());
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(crop);
|
|
|
|
canvas->drawImage(image, 0, 0, &expectedCFPaint); // Filter applied by draw's SkPaint
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(image->width(), 0);
|
|
|
|
canvas->clipRect(crop);
|
|
|
|
canvas->drawImage(image, 0, 0, &testCFPaint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// Now test mask filters. These should be run before the image filter, and thus have the same
|
|
|
|
// effect as multiplying by an alpha mask.
|
|
|
|
|
|
|
|
// This mask filter pokes a hole in the center of the image
|
|
|
|
static constexpr SkColor kAlphas[] = { SK_ColorBLACK, SK_ColorTRANSPARENT };
|
|
|
|
static constexpr SkScalar kPos[] = { 0.4f, 0.9f };
|
|
|
|
sk_sp<SkShader> alphaMaskShader = SkGradientShader::MakeRadial(
|
|
|
|
{128.f, 128.f}, 128.f, kAlphas, kPos, 2, SkTileMode::kClamp);
|
|
|
|
sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(alphaMaskShader);
|
|
|
|
|
|
|
|
// If edge detector sees the mask filter, it'll have alpha and then blend with the original
|
|
|
|
// image; otherwise the mask filter will apply late (incorrectly) and none of the original
|
|
|
|
// image will be visible.
|
|
|
|
sk_sp<SkImageFilter> edgeBlend = SkImageFilters::Xfermode(SkBlendMode::kSrcOver,
|
|
|
|
SkImageFilters::Image(image), edgeDetector);
|
|
|
|
|
|
|
|
SkPaint testMaskPaint;
|
|
|
|
testMaskPaint.setMaskFilter(maskFilter);
|
|
|
|
testMaskPaint.setImageFilter(edgeBlend);
|
|
|
|
|
|
|
|
SkPaint alphaPaint;
|
|
|
|
alphaPaint.setShader(alphaMaskShader);
|
|
|
|
SkPaint expectedMaskPaint;
|
|
|
|
expectedMaskPaint.setImageFilter(SkImageFilters::Compose(edgeBlend,
|
|
|
|
SkImageFilters::Xfermode(SkBlendMode::kSrcIn,
|
|
|
|
SkImageFilters::Paint(alphaPaint))));
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(0, image->height());
|
|
|
|
canvas->clipRect(crop);
|
|
|
|
canvas->drawImage(image, 0, 0, &expectedMaskPaint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(image->width(), image->height());
|
|
|
|
canvas->clipRect(crop);
|
|
|
|
canvas->drawImage(image, 0, 0, &testMaskPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|