2013-05-13 14:10:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
2013-05-13 14:10:31 +00:00
|
|
|
#include "SkCanvas.h"
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "SkLayerDrawLooper.h"
|
2013-05-13 14:10:31 +00:00
|
|
|
#include "SkPaint.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2013-05-13 14:10:31 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
|
|
|
|
// This bench replicates a problematic use case of a draw looper used
|
|
|
|
// to create an inner blurred rect
|
2014-06-19 19:32:29 +00:00
|
|
|
class RectoriBench : public Benchmark {
|
2013-05-13 14:10:31 +00:00
|
|
|
public:
|
2013-09-13 19:52:27 +00:00
|
|
|
RectoriBench() {}
|
2013-05-13 14:10:31 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-05-13 14:10:31 +00:00
|
|
|
return "rectori";
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(const int loops, SkCanvas* canvas) override {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom Random;
|
2013-05-13 14:10:31 +00:00
|
|
|
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2013-09-06 14:16:12 +00:00
|
|
|
SkScalar blurSigma = Random.nextRangeScalar(1.5f, 25.0f);
|
|
|
|
SkScalar size = Random.nextRangeScalar(20*blurSigma, 50*blurSigma);
|
2013-05-13 14:10:31 +00:00
|
|
|
|
|
|
|
SkScalar x = Random.nextRangeScalar(0.0f, W - size);
|
|
|
|
SkScalar y = Random.nextRangeScalar(0.0f, H - size);
|
|
|
|
|
|
|
|
SkRect inner = { x, y, x + size, y + size };
|
|
|
|
|
|
|
|
SkRect outer(inner);
|
|
|
|
// outer is always outset either 2x or 4x the blur radius (we go with 2x)
|
2013-09-06 14:16:12 +00:00
|
|
|
outer.outset(2*blurSigma, 2*blurSigma);
|
2013-05-13 14:10:31 +00:00
|
|
|
|
|
|
|
SkPath p;
|
|
|
|
|
|
|
|
p.addRect(outer);
|
|
|
|
p.addRect(inner);
|
|
|
|
p.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
|
|
|
|
// This will be used to translate the normal draw outside the
|
|
|
|
// clip rect and translate the blurred version back inside
|
|
|
|
SkScalar translate = 2.0f * size;
|
|
|
|
|
|
|
|
SkPaint paint;
|
2013-09-06 14:16:12 +00:00
|
|
|
paint.setLooper(this->createLooper(-translate, blurSigma))->unref();
|
2013-05-13 14:10:31 +00:00
|
|
|
paint.setColor(0xff000000 | Random.nextU());
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
// clip always equals inner rect so we get the inside blur
|
|
|
|
canvas->clipRect(inner);
|
|
|
|
canvas->translate(translate, 0);
|
|
|
|
canvas->drawPath(p, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
W = 640,
|
|
|
|
H = 480,
|
|
|
|
};
|
|
|
|
|
2013-09-06 14:16:12 +00:00
|
|
|
SkLayerDrawLooper* createLooper(SkScalar xOff, SkScalar sigma) {
|
2014-04-15 15:48:36 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-05-13 14:10:31 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------
|
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
|
|
|
|
|
|
|
// TODO: add a color filter to better match what is seen in the wild
|
2013-05-14 07:01:11 +00:00
|
|
|
info.fPaintBits = /* SkLayerDrawLooper::kColorFilter_Bit |*/
|
2013-05-13 14:10:31 +00:00
|
|
|
SkLayerDrawLooper::kMaskFilter_Bit;
|
|
|
|
info.fColorMode = SkXfermode::kDst_Mode;
|
|
|
|
info.fOffset.set(xOff, 0);
|
|
|
|
info.fPostTranslate = false;
|
|
|
|
|
2014-04-15 15:48:36 +00:00
|
|
|
SkPaint* paint = looperBuilder.addLayer(info);
|
2013-05-13 14:10:31 +00:00
|
|
|
|
2014-04-28 16:25:35 +00:00
|
|
|
SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
|
2013-09-06 14:16:12 +00:00
|
|
|
sigma,
|
2013-05-13 14:10:31 +00:00
|
|
|
SkBlurMaskFilter::kHighQuality_BlurFlag);
|
|
|
|
paint->setMaskFilter(mf)->unref();
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
|
|
|
info.fPaintBits = 0;
|
|
|
|
info.fOffset.set(0, 0);
|
|
|
|
|
2014-04-15 15:48:36 +00:00
|
|
|
paint = looperBuilder.addLayer(info);
|
|
|
|
return looperBuilder.detachLooper();
|
2013-05-13 14:10:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-05-13 14:10:31 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new RectoriBench();)
|