skia2/gm/imagefilters.cpp
Yuqian Li 52a6a40535 Add savelayer_with_backdrop GM to benchmark
The performance regression only happened on iOS devices. Benchmarking
on Desktops or Android devices doesn't seem to show any significant
regressions.

Tested this on iPhone XS with nanobench and saw a 10ms->30ms regression:

regression:
  61/63  MB 1   10.5ms  10.7ms  10.8ms  12.2ms  5%  █▂▁▁▂▁▂▂▂▁  8888    GM_savelayer_with_backdrop
 101/101 MB 1   36ms    36.5ms  36.4ms  36.8ms  1%  ▁▅▃▆▆▄▆▃▃█  gles    GM_savelayer_with_backdrop

before regression:
  61/63  MB 1   10.3ms  10.4ms  10.6ms  12.4ms  6%  █▂▁▂▁▁▁▁▁▁  8888    GM_savelayer_with_backdrop
  96/96  MB 1   10.6ms  10.7ms  10.7ms  10.8ms  1%  ▄▁▄▂▆█▅▃▂▅  gles    GM_savelayer_with_backdrop

It's interesting to see that 8888 is always fast. Is the regressing CL changing blur from software to GPU?

Bug: https://github.com/flutter/flutter/issues/36064
Change-Id: Iea2305e41a31bd5b4267f45bfd835231d49d639a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/234413
Commit-Queue: Yuqian Li <liyuqian@google.com>
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
2019-08-19 23:19:37 +00:00

192 lines
6.7 KiB
C++

/*
* 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/gm.h"
#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"
#include "include/core/SkImage.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#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"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkColorMatrix.h"
#include "include/effects/SkImageFilters.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include <utility>
/**
* 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).
*
* see https://bug.skia.org/3741
*/
static void do_draw(SkCanvas* canvas, SkBlendMode mode, sk_sp<SkImageFilter> imf) {
SkAutoCanvasRestore acr(canvas, true);
canvas->clipRect(SkRect::MakeWH(220, 220));
// 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.
canvas->saveLayer(nullptr, nullptr);
canvas->drawColor(SK_ColorGREEN);
SkPaint paint;
paint.setAntiAlias(true);
SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
paint.setColor(SK_ColorRED);
canvas->drawOval(r0, paint);
paint.setColor(0x660000FF);
paint.setImageFilter(std::move(imf));
paint.setBlendMode(mode);
canvas->drawOval(r1, paint);
}
DEF_SIMPLE_GM(imagefilters_xfermodes, canvas, 480, 480) {
canvas->translate(10, 10);
// just need an imagefilter to trigger the code-path (which creates a tmp layer)
sk_sp<SkImageFilter> imf(SkImageFilters::MatrixTransform(SkMatrix::I(),
kNone_SkFilterQuality,
nullptr));
const SkBlendMode modes[] = {
SkBlendMode::kSrcATop, SkBlendMode::kDstIn
};
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
canvas->save();
do_draw(canvas, modes[i], nullptr);
canvas->translate(240, 0);
do_draw(canvas, modes[i], imf);
canvas->restore();
canvas->translate(0, 240);
}
}
static sk_sp<SkImage> make_image(SkCanvas* canvas) {
const SkImageInfo info = SkImageInfo::MakeS32(100, 100, kPremul_SkAlphaType);
auto surface(ToolUtils::makeSurface(canvas, info));
surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
return surface->makeImageSnapshot();
}
// 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) {
sk_sp<SkImage> image(make_image(canvas));
const SkRect r = SkRect::MakeIWH(image->width(), image->height());
canvas->translate(10, 10);
for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
SkPaint paint;
paint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
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);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
static void draw_set(SkCanvas* canvas, sk_sp<SkImageFilter> filters[], int count) {
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);
canvas->clipRRect(rr, true);
canvas->saveLayer({ &rr.getBounds(), nullptr, filters[i].get(), nullptr, nullptr, 0 });
canvas->drawColor(0x40FFFFFF);
canvas->restore();
canvas->restore();
if (0 == dx) {
dx = offset;
} else {
dx = 0;
dy = offset;
}
}
}
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();
}
}
};
DEF_GM(return new SaveLayerWithBackdropGM();)