| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkBlurTypes.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkColorFilter.h" |
| 14 |
#include "include/core/SkMaskFilter.h" |
| 15 |
#include "include/core/SkMatrix.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRRect.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkRefCnt.h" |
| 21 |
#include "include/core/SkScalar.h" |
| 22 |
#include "include/core/SkShader.h" |
| 23 |
#include "include/core/SkSize.h" |
| 24 |
#include "include/core/SkString.h" |
| 25 |
#include "include/core/SkTileMode.h" |
| 26 |
#include "include/core/SkTypes.h" |
| 27 |
#include "include/effects/SkGradient.h" |
| 28 |
#include "include/private/base/SkFloatingPoint.h" |
| 29 |
#include "src/core/SkBlurMask.h" |
| 30 |
|
| 31 |
/* |
| 32 |
* Spits out an arbitrary gradient to test blur with shader on paint |
| 33 |
*/ |
| 34 |
static sk_sp<SkShader> MakeRadial() { |
| 35 |
SkPoint pts[2] = { |
| 36 |
{ 0, 0 }, |
| 37 |
{ SkIntToScalar(100), SkIntToScalar(100) } |
| 38 |
}; |
| 39 |
SkTileMode tm = SkTileMode::kClamp; |
| 40 |
const SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen, }; |
| 41 |
const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 42 |
SkMatrix scale; |
| 43 |
scale.setScale(0.5f, 0.5f); |
| 44 |
scale.postTranslate(5.f, 5.f); |
| 45 |
SkPoint center0, center1; |
| 46 |
center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 47 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 48 |
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 49 |
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 50 |
return SkShaders::TwoPointConicalGradient(center1, (pts[1].fX - pts[0].fX) / 7, |
| 51 |
center0, (pts[1].fX - pts[0].fX) / 2, |
| 52 |
{{colors, pos, tm}, {}}, &scale); |
| 53 |
} |
| 54 |
|
| 55 |
// Simpler blurred RR test cases where all the radii are the same. |
| 56 |
class SimpleBlurRoundRectGM : public skiagm::GM { |
| 57 |
SkString getName() const override { return SkString("simpleblurroundrect"); } |
| 58 |
|
| 59 |
SkISize getISize() override { return {1000, 500}; } |
| 60 |
|
| 61 |
bool runAsBench() const override { return true; } |
| 62 |
|
| 63 |
void onDraw(SkCanvas* canvas) override { |
| 64 |
canvas->scale(1.5f, 1.5f); |
| 65 |
canvas->translate(50,50); |
| 66 |
|
| 67 |
const float blurRadii[] = {1.f, 5.f, 10.f, 20.f}; |
| 68 |
const float cornerRadii[] = {1.f, 5.f, 10.f, 20.f}; |
| 69 |
const SkRect r = SkRect::MakeWH(25.f, 25.f); |
| 70 |
for (size_t row = 0; row < std::size(blurRadii); ++row) { |
| 71 |
SkAutoCanvasRestore autoRestore(canvas, true); |
| 72 |
canvas->translate(0, (r.height() + 50.f) * row); |
| 73 |
for (size_t pair = 0; pair < std::size(cornerRadii); ++pair) { |
| 74 |
SkPaint paint; |
| 75 |
paint.setColor(SK_ColorBLACK); |
| 76 |
paint.setMaskFilter(SkMaskFilter::MakeBlur( |
| 77 |
kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(blurRadii[row]))); |
| 78 |
SkRRect rrect; |
| 79 |
rrect.setRectXY(r, cornerRadii[pair], cornerRadii[pair]); |
| 80 |
|
| 81 |
// Even-indexed columns are without a gradient |
| 82 |
canvas->drawRRect(rrect, paint); |
| 83 |
canvas->translate(r.width() + 50.f, 0); |
| 84 |
|
| 85 |
// Odd-indexed columns have a gradient |
| 86 |
paint.setShader(MakeRadial()); |
| 87 |
canvas->drawRRect(rrect, paint); |
| 88 |
canvas->translate(r.width() + 50.f, 0); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
DEF_GM(return new SimpleBlurRoundRectGM();) |
| 95 |
|
| 96 |
// From crbug.com/1138810 |
| 97 |
DEF_SIMPLE_GM(blur_large_rrects, canvas, 300, 300) { |
| 98 |
SkPaint paint; |
| 99 |
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 20.f)); |
| 100 |
|
| 101 |
auto rect = SkRect::MakeLTRB(5.f, -20000.f, 240.f, 25.f); |
| 102 |
SkRRect rrect = SkRRect::MakeRectXY(rect, 40.f, 40.f); |
| 103 |
for (int i = 0; i < 4; ++i) { |
| 104 |
SkColor4f color{(i & 1) ? 1.f : 0.f, |
| 105 |
(i & 2) ? 1.f : 0.f, |
| 106 |
(i < 2) ? 1.f : 0.f, |
| 107 |
1.f}; |
| 108 |
paint.setColor(color); |
| 109 |
canvas->drawRRect(rrect, paint); |
| 110 |
canvas->rotate(90.f, 150.f, 150.f); |
| 111 |
} |
| 112 |
} |