blob: 1819ac2edd57e4bf35632849de77ceaf60c64c39 [file] [log] [blame]
scroggo@google.com7b056592013-11-05 15:57:21 +00001/*
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.h"
9#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
12#include "SkColorFilter.h"
13#include "SkLayerDrawLooper.h"
14#include "SkPaint.h"
15#include "SkPath.h"
16#include "SkPoint.h"
17#include "SkRect.h"
18#include "SkRRect.h"
19#include "SkString.h"
20#include "SkXfermode.h"
21
scroggo@google.com8610d2c2013-11-05 21:10:58 +000022// This GM mimics a blurred RR seen in the wild.
scroggo@google.com7b056592013-11-05 15:57:21 +000023class BlurRoundRectGM : public skiagm::GM {
24public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000025 BlurRoundRectGM(int width, int height, int radius)
scroggo@google.com7b056592013-11-05 15:57:21 +000026 : fName("blurroundrect")
scroggo@google.com8610d2c2013-11-05 21:10:58 +000027 {
28 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
29 fRRect.setRectXY(r, SkIntToScalar(radius), SkIntToScalar(radius));
scroggoef470f12014-06-03 06:50:26 -070030 fName.appendf("-WH-%ix%i-corner-%i", width, height, radius);
scroggo@google.com8610d2c2013-11-05 21:10:58 +000031 }
32
33 BlurRoundRectGM(int width, int height)
34 : fName("blurroundrect") {
scroggoef470f12014-06-03 06:50:26 -070035 fName.appendf("-WH-%ix%i-unevenCorners", width, height);
scroggo@google.com7b056592013-11-05 15:57:21 +000036 SkVector radii[4];
scroggo@google.com8610d2c2013-11-05 21:10:58 +000037 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
38 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
39 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
40 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
41 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
scroggo@google.com7b056592013-11-05 15:57:21 +000042 fRRect.setRectRadii(r, radii);
43 }
44
45 virtual SkString onShortName() SK_OVERRIDE {
46 return fName;
47 }
48
49 virtual SkISize onISize() SK_OVERRIDE {
scroggo@google.com8610d2c2013-11-05 21:10:58 +000050 return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()),
51 SkScalarCeilToInt(fRRect.rect().height()));
scroggo@google.com7b056592013-11-05 15:57:21 +000052 }
53
54 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000055 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com7b056592013-11-05 15:57:21 +000056 {
57 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000058 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
59 | SkLayerDrawLooper::kColorFilter_Bit;
scroggo@google.com7b056592013-11-05 15:57:21 +000060 info.fColorMode = SkXfermode::kSrc_Mode;
61 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
62 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000063 SkPaint* paint = looperBuilder.addLayerOnTop(info);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000064 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000065 kNormal_SkBlurStyle,
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000066 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
scroggo@google.com7b056592013-11-05 15:57:21 +000067 SkBlurMaskFilter::kHighQuality_BlurFlag);
68 paint->setMaskFilter(maskFilter)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000069 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY,
scroggo@google.com7b056592013-11-05 15:57:21 +000070 SkXfermode::kSrcIn_Mode);
71 paint->setColorFilter(colorFilter)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000072 paint->setColor(SK_ColorGRAY);
scroggo@google.com7b056592013-11-05 15:57:21 +000073 }
74 {
75 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000076 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000077 }
78 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000079 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000080
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000081 paint.setLooper(looperBuilder.detachLooper())->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000082 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000083 paint.setAntiAlias(true);
84
scroggo@google.com8610d2c2013-11-05 21:10:58 +000085 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000086 }
87
88private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000089 SkString fName;
90 SkRRect fRRect;
91
92 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000093};
94
joshualitt341400e2014-12-18 11:54:13 -080095#include "SkGradientShader.h"
96/*
97 * Spits out a dummy gradient to test blur with shader on paint
98 */
99static SkShader* MakeRadial() {
100 SkPoint pts[2] = {
101 { 0, 0 },
102 { SkIntToScalar(100), SkIntToScalar(100) }
103 };
104 SkShader::TileMode tm = SkShader::kClamp_TileMode;
105 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
106 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
107 SkMatrix scale;
108 scale.setScale(0.5f, 0.5f);
109 scale.postTranslate(5.f, 5.f);
110 SkPoint center0, center1;
111 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
112 SkScalarAve(pts[0].fY, pts[1].fY));
113 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
114 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
115 return SkGradientShader::CreateTwoPointRadial(center1, (pts[1].fX - pts[0].fX) / 7,
116 center0, (pts[1].fX - pts[0].fX) / 2,
117 colors, pos, SK_ARRAY_COUNT(colors), tm,
118 0, &scale);
119}
120
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000121// Simpler blurred RR test cases where all the radii are the same.
122class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000123public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000124 SimpleBlurRoundRectGM()
125 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000126 }
127
128protected:
joshualitt341400e2014-12-18 11:54:13 -0800129 virtual uint32_t onGetFlags() const SK_OVERRIDE {
130 return kSkipTiled_Flag;
131 }
132
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000133 virtual SkString onShortName() SK_OVERRIDE {
134 return fName;
135 }
136
137 virtual SkISize onISize() SK_OVERRIDE {
joshualitt341400e2014-12-18 11:54:13 -0800138 return SkISize::Make(1000, 500);
scroggo@google.com7b056592013-11-05 15:57:21 +0000139 }
140
141 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000142 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000143 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000144
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000145 const float blurRadii[] = { 1,5,10,20 };
146 const int cornerRadii[] = { 1,5,10,20 };
joshualitt341400e2014-12-18 11:54:13 -0800147 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000148 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
149 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000150 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000151 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
joshualitt341400e2014-12-18 11:54:13 -0800152 for (int k = 0; k <= 1; k++) {
153 SkMaskFilter* filter = SkBlurMaskFilter::Create(
154 kNormal_SkBlurStyle,
155 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])),
156 SkBlurMaskFilter::kHighQuality_BlurFlag);
157 SkPaint paint;
158 paint.setColor(SK_ColorBLACK);
159 paint.setMaskFilter(filter)->unref();
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000160
joshualitt341400e2014-12-18 11:54:13 -0800161 bool useRadial = SkToBool(k);
162 if (useRadial) {
163 paint.setShader(MakeRadial())->unref();
164 }
165
166 SkRRect rrect;
167 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
168 SkIntToScalar(cornerRadii[j]));
169 canvas->drawRRect(rrect, paint);
170 canvas->translate(r.width() + SkIntToScalar(50), 0);
171 }
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000172 }
173 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000174 }
175private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000176 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000177
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000178 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000179};
180
181// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000182//
183// TODO(scroggo): Disabled in an attempt to rememdy
184// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
185// ran wrong number of tests')
186//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
187
scroggo@google.com7b056592013-11-05 15:57:21 +0000188// Rounded rect with two opposite corners with large radii, the other two
189// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000190DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000191
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000192DEF_GM(return new SimpleBlurRoundRectGM();)