blob: 3e6f1bdd985596d239a91dd0ca749dd69783dcc1 [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
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000045 virtual uint32_t onGetFlags() const SK_OVERRIDE {
46 return kSkipTiled_Flag;
47 }
48
scroggo@google.com7b056592013-11-05 15:57:21 +000049 virtual SkString onShortName() SK_OVERRIDE {
50 return fName;
51 }
52
53 virtual SkISize onISize() SK_OVERRIDE {
scroggo@google.com8610d2c2013-11-05 21:10:58 +000054 return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()),
55 SkScalarCeilToInt(fRRect.rect().height()));
scroggo@google.com7b056592013-11-05 15:57:21 +000056 }
57
58 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000059 SkLayerDrawLooper::Builder looperBuilder;
scroggo@google.com7b056592013-11-05 15:57:21 +000060 {
61 SkLayerDrawLooper::LayerInfo info;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000062 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
63 | SkLayerDrawLooper::kColorFilter_Bit;
scroggo@google.com7b056592013-11-05 15:57:21 +000064 info.fColorMode = SkXfermode::kSrc_Mode;
65 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
66 info.fPostTranslate = false;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000067 SkPaint* paint = looperBuilder.addLayerOnTop(info);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000068 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000069 kNormal_SkBlurStyle,
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000070 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
scroggo@google.com7b056592013-11-05 15:57:21 +000071 SkBlurMaskFilter::kHighQuality_BlurFlag);
72 paint->setMaskFilter(maskFilter)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000073 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY,
scroggo@google.com7b056592013-11-05 15:57:21 +000074 SkXfermode::kSrcIn_Mode);
75 paint->setColorFilter(colorFilter)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000076 paint->setColor(SK_ColorGRAY);
scroggo@google.com7b056592013-11-05 15:57:21 +000077 }
78 {
79 SkLayerDrawLooper::LayerInfo info;
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000080 looperBuilder.addLayerOnTop(info);
scroggo@google.com7b056592013-11-05 15:57:21 +000081 }
82 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000083 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000084
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000085 paint.setLooper(looperBuilder.detachLooper())->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000086 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000087 paint.setAntiAlias(true);
88
scroggo@google.com8610d2c2013-11-05 21:10:58 +000089 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000090 }
91
92private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000093 SkString fName;
94 SkRRect fRRect;
95
96 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000097};
98
scroggo@google.com8610d2c2013-11-05 21:10:58 +000099// Simpler blurred RR test cases where all the radii are the same.
100class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +0000101public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000102 SimpleBlurRoundRectGM()
103 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000104 }
105
106protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000107 virtual uint32_t onGetFlags() const SK_OVERRIDE {
108 return kSkipTiled_Flag;
109 }
110
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000111 virtual SkString onShortName() SK_OVERRIDE {
112 return fName;
113 }
114
115 virtual SkISize onISize() SK_OVERRIDE {
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000116 return SkISize::Make(950, 950);
scroggo@google.com7b056592013-11-05 15:57:21 +0000117 }
118
119 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000120 canvas->scale(1.5f, 1.5f);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000121 canvas->translate(50,50);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000122
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000123 const float blurRadii[] = { 1,5,10,20 };
124 const int cornerRadii[] = { 1,5,10,20 };
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000125 const SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
126 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
127 SkAutoCanvasRestore autoRestore(canvas, true);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000128 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000129 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +0000130 SkMaskFilter* filter = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000131 kNormal_SkBlurStyle,
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000132 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])),
133 SkBlurMaskFilter::kHighQuality_BlurFlag);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000134 SkPaint paint;
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000135 paint.setColor(SK_ColorBLACK);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000136 paint.setMaskFilter(filter)->unref();
137
138 SkRRect rrect;
139 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), SkIntToScalar(cornerRadii[j]));
140 canvas->drawRRect(rrect, paint);
commit-bot@chromium.org3d8bf232014-04-28 19:49:24 +0000141 canvas->translate(r.width() + SkIntToScalar(50), 0);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000142 }
143 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000144 }
145private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000146 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000147
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000148 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000149};
150
151// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000152//
153// TODO(scroggo): Disabled in an attempt to rememdy
154// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
155// ran wrong number of tests')
156//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
157
scroggo@google.com7b056592013-11-05 15:57:21 +0000158// Rounded rect with two opposite corners with large radii, the other two
159// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000160DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000161
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000162DEF_GM(return new SimpleBlurRoundRectGM();)