2013-11-05 15:57:21 +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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkDrawLooper.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
|
|
|
#include "include/core/SkRect.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkString.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkLayerDrawLooper.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
2013-11-05 15:57:21 +00:00
|
|
|
|
2013-11-05 21:10:58 +00:00
|
|
|
// This GM mimics a blurred RR seen in the wild.
|
2013-11-05 15:57:21 +00:00
|
|
|
class BlurRoundRectGM : public skiagm::GM {
|
|
|
|
public:
|
2013-11-05 21:10:58 +00:00
|
|
|
BlurRoundRectGM(int width, int height)
|
2016-01-08 21:48:43 +00:00
|
|
|
: fName("blurroundrect"), fWidth(width), fHeight(height) {
|
2014-06-03 13:50:26 +00:00
|
|
|
fName.appendf("-WH-%ix%i-unevenCorners", width, height);
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-11-05 15:57:21 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2016-01-08 21:48:43 +00:00
|
|
|
return SkISize::Make(fWidth, fHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkVector radii[4];
|
|
|
|
radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
|
|
|
|
radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
|
|
|
|
radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight));
|
|
|
|
fRRect.setRectRadii(r, radii);
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-04-15 15:48:36 +00:00
|
|
|
SkLayerDrawLooper::Builder looperBuilder;
|
2013-11-05 15:57:21 +00:00
|
|
|
{
|
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
2013-11-05 21:10:58 +00:00
|
|
|
info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
|
|
|
|
| SkLayerDrawLooper::kColorFilter_Bit;
|
2016-11-03 18:45:31 +00:00
|
|
|
info.fColorMode = SkBlendMode::kSrc;
|
2013-11-05 15:57:21 +00:00
|
|
|
info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
|
|
|
|
info.fPostTranslate = false;
|
2014-04-15 15:48:36 +00:00
|
|
|
SkPaint* paint = looperBuilder.addLayerOnTop(info);
|
2018-03-14 17:01:17 +00:00
|
|
|
paint->setMaskFilter(SkMaskFilter::MakeBlur(
|
2014-04-28 16:25:35 +00:00
|
|
|
kNormal_SkBlurStyle,
|
2018-03-14 17:01:17 +00:00
|
|
|
SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf)));
|
2019-04-08 20:23:20 +00:00
|
|
|
paint->setColorFilter(SkColorFilters::Blend(SK_ColorLTGRAY, SkBlendMode::kSrcIn));
|
2018-08-16 14:17:03 +00:00
|
|
|
paint->setColor(SK_ColorGRAY);
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SkLayerDrawLooper::LayerInfo info;
|
2014-04-15 15:48:36 +00:00
|
|
|
looperBuilder.addLayerOnTop(info);
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
SkPaint paint;
|
2013-11-05 21:10:58 +00:00
|
|
|
canvas->drawRect(fRRect.rect(), paint);
|
2013-11-05 15:57:21 +00:00
|
|
|
|
2016-03-21 20:25:16 +00:00
|
|
|
paint.setLooper(looperBuilder.detach());
|
2013-11-05 16:28:38 +00:00
|
|
|
paint.setColor(SK_ColorCYAN);
|
2013-11-05 15:57:21 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2013-11-05 21:10:58 +00:00
|
|
|
canvas->drawRRect(fRRect, paint);
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-05 21:10:58 +00:00
|
|
|
SkString fName;
|
|
|
|
SkRRect fRRect;
|
2016-01-08 21:48:43 +00:00
|
|
|
int fWidth, fHeight;
|
2013-11-05 21:10:58 +00:00
|
|
|
|
|
|
|
typedef skiagm::GM INHERITED;
|
2013-11-05 15:57:21 +00:00
|
|
|
};
|
|
|
|
|
2014-12-18 19:54:13 +00:00
|
|
|
/*
|
|
|
|
* Spits out a dummy gradient to test blur with shader on paint
|
|
|
|
*/
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> MakeRadial() {
|
2014-12-18 19:54:13 +00:00
|
|
|
SkPoint pts[2] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(100), SkIntToScalar(100) }
|
|
|
|
};
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode tm = SkTileMode::kClamp;
|
2014-12-18 19:54:13 +00:00
|
|
|
const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
|
|
|
|
const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
|
|
|
|
SkMatrix scale;
|
|
|
|
scale.setScale(0.5f, 0.5f);
|
|
|
|
scale.postTranslate(5.f, 5.f);
|
|
|
|
SkPoint center0, center1;
|
|
|
|
center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
|
|
|
|
SkScalarAve(pts[0].fY, pts[1].fY));
|
|
|
|
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
|
|
|
|
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
|
2016-03-09 17:50:50 +00:00
|
|
|
return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
|
|
|
|
center0, (pts[1].fX - pts[0].fX) / 2,
|
|
|
|
colors, pos, SK_ARRAY_COUNT(colors), tm,
|
|
|
|
0, &scale);
|
2014-12-18 19:54:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 21:10:58 +00:00
|
|
|
// Simpler blurred RR test cases where all the radii are the same.
|
|
|
|
class SimpleBlurRoundRectGM : public skiagm::GM {
|
2013-11-05 15:57:21 +00:00
|
|
|
public:
|
2013-11-05 21:10:58 +00:00
|
|
|
SimpleBlurRoundRectGM()
|
|
|
|
: fName("simpleblurroundrect") {
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-12-18 19:54:13 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-11-05 21:10:58 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-12-18 19:54:13 +00:00
|
|
|
return SkISize::Make(1000, 500);
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas->scale(1.5f, 1.5f);
|
2014-04-28 19:49:24 +00:00
|
|
|
canvas->translate(50,50);
|
2013-11-05 21:10:58 +00:00
|
|
|
|
2014-04-28 19:49:24 +00:00
|
|
|
const float blurRadii[] = { 1,5,10,20 };
|
|
|
|
const int cornerRadii[] = { 1,5,10,20 };
|
2014-12-18 19:54:13 +00:00
|
|
|
const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
|
2013-11-05 21:10:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
|
|
|
|
SkAutoCanvasRestore autoRestore(canvas, true);
|
2014-04-28 19:49:24 +00:00
|
|
|
canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
|
2013-11-05 21:10:58 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
|
2014-12-18 19:54:13 +00:00
|
|
|
for (int k = 0; k <= 1; k++) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
2018-03-14 17:01:17 +00:00
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
|
2014-12-18 19:54:13 +00:00
|
|
|
|
|
|
|
bool useRadial = SkToBool(k);
|
|
|
|
if (useRadial) {
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(MakeRadial());
|
2014-12-18 19:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkRRect rrect;
|
|
|
|
rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
|
|
|
|
SkIntToScalar(cornerRadii[j]));
|
|
|
|
canvas->drawRRect(rrect, paint);
|
|
|
|
canvas->translate(r.width() + SkIntToScalar(50), 0);
|
|
|
|
}
|
2013-11-05 21:10:58 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 15:57:21 +00:00
|
|
|
}
|
|
|
|
private:
|
2013-11-05 21:10:58 +00:00
|
|
|
const SkString fName;
|
2013-11-05 15:57:21 +00:00
|
|
|
|
2013-11-05 21:10:58 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2013-11-05 15:57:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create one with dimensions/rounded corners based on the skp
|
2013-11-05 22:04:53 +00:00
|
|
|
//
|
|
|
|
// TODO(scroggo): Disabled in an attempt to rememdy
|
|
|
|
// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
|
|
|
|
// ran wrong number of tests')
|
|
|
|
//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
|
|
|
|
|
2013-11-05 15:57:21 +00:00
|
|
|
// Rounded rect with two opposite corners with large radii, the other two
|
|
|
|
// small.
|
2013-11-05 21:10:58 +00:00
|
|
|
DEF_GM(return new BlurRoundRectGM(100, 100);)
|
|
|
|
|
|
|
|
DEF_GM(return new SimpleBlurRoundRectGM();)
|