blob: 6a466c9c877ca5c0bc2c58bd9469e737595eb271 [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));
30 fName.appendf("-WH[%ix%i]-corner[%i]", width, height, radius);
31 }
32
33 BlurRoundRectGM(int width, int height)
34 : fName("blurroundrect") {
35 fName.appendf("-WH[%ix%i]-unevenCorners",
36 width, height);
scroggo@google.com7b056592013-11-05 15:57:21 +000037 SkVector radii[4];
scroggo@google.com8610d2c2013-11-05 21:10:58 +000038 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
39 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
40 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
41 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
42 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
scroggo@google.com7b056592013-11-05 15:57:21 +000043 fRRect.setRectRadii(r, radii);
44 }
45
46 virtual SkString onShortName() SK_OVERRIDE {
47 return fName;
48 }
49
50 virtual SkISize onISize() SK_OVERRIDE {
scroggo@google.com8610d2c2013-11-05 21:10:58 +000051 return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()),
52 SkScalarCeilToInt(fRRect.rect().height()));
scroggo@google.com7b056592013-11-05 15:57:21 +000053 }
54
55 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
scroggo@google.com7b056592013-11-05 15:57:21 +000056 SkLayerDrawLooper* looper = new SkLayerDrawLooper;
57 {
58 SkLayerDrawLooper::LayerInfo info;
59 info.fFlagsMask = 0;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000060 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
61 | SkLayerDrawLooper::kColorFilter_Bit;
scroggo@google.com7b056592013-11-05 15:57:21 +000062 info.fColorMode = SkXfermode::kSrc_Mode;
63 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
64 info.fPostTranslate = false;
65 SkPaint* paint = looper->addLayerOnTop(info);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000066 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
scroggo@google.com7b056592013-11-05 15:57:21 +000067 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000068 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
scroggo@google.com7b056592013-11-05 15:57:21 +000069 SkBlurMaskFilter::kHighQuality_BlurFlag);
70 paint->setMaskFilter(maskFilter)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000071 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY,
scroggo@google.com7b056592013-11-05 15:57:21 +000072 SkXfermode::kSrcIn_Mode);
73 paint->setColorFilter(colorFilter)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000074 paint->setColor(SK_ColorGRAY);
scroggo@google.com7b056592013-11-05 15:57:21 +000075 }
76 {
77 SkLayerDrawLooper::LayerInfo info;
78 looper->addLayerOnTop(info);
79 }
80 SkPaint paint;
scroggo@google.com8610d2c2013-11-05 21:10:58 +000081 canvas->drawRect(fRRect.rect(), paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000082
83 paint.setLooper(looper)->unref();
scroggo@google.com5fdef982013-11-05 16:28:38 +000084 paint.setColor(SK_ColorCYAN);
scroggo@google.com7b056592013-11-05 15:57:21 +000085 paint.setAntiAlias(true);
86
scroggo@google.com8610d2c2013-11-05 21:10:58 +000087 canvas->drawRRect(fRRect, paint);
scroggo@google.com7b056592013-11-05 15:57:21 +000088 }
89
90private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +000091 SkString fName;
92 SkRRect fRRect;
93
94 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +000095};
96
scroggo@google.com8610d2c2013-11-05 21:10:58 +000097// Simpler blurred RR test cases where all the radii are the same.
98class SimpleBlurRoundRectGM : public skiagm::GM {
scroggo@google.com7b056592013-11-05 15:57:21 +000099public:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000100 SimpleBlurRoundRectGM()
101 : fName("simpleblurroundrect") {
scroggo@google.com7b056592013-11-05 15:57:21 +0000102 }
103
104protected:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000105 virtual SkString onShortName() SK_OVERRIDE {
106 return fName;
107 }
108
109 virtual SkISize onISize() SK_OVERRIDE {
scroggo@google.com4ef88a22013-11-05 21:22:04 +0000110 return SkISize::Make(750, 750);
scroggo@google.com7b056592013-11-05 15:57:21 +0000111 }
112
113 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000114 canvas->scale(1.5f, 1.5f);
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000115
116 const int blurRadii[] = { 1, 3, 6, 10 };
117 const int cornerRadii[] = { 1, 3, 6, 10 };
118 const SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
119 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
120 SkAutoCanvasRestore autoRestore(canvas, true);
121 canvas->translate(0, (r.height() + SkIntToScalar(20)) * i);
122 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +0000123 SkMaskFilter* filter = SkBlurMaskFilter::Create(
124 SkBlurMaskFilter::kNormal_BlurStyle,
125 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])));
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000126 SkPaint paint;
127 paint.setColor(SK_ColorBLUE);
128 paint.setMaskFilter(filter)->unref();
129
130 SkRRect rrect;
131 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), SkIntToScalar(cornerRadii[j]));
132 canvas->drawRRect(rrect, paint);
133 canvas->translate(r.width() + SkIntToScalar(10), 0);
134 }
135 }
scroggo@google.com7b056592013-11-05 15:57:21 +0000136 }
137private:
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000138 const SkString fName;
scroggo@google.com7b056592013-11-05 15:57:21 +0000139
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000140 typedef skiagm::GM INHERITED;
scroggo@google.com7b056592013-11-05 15:57:21 +0000141};
142
143// Create one with dimensions/rounded corners based on the skp
epoger@google.com7e6e80a2013-11-05 22:04:53 +0000144//
145// TODO(scroggo): Disabled in an attempt to rememdy
146// https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
147// ran wrong number of tests')
148//DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
149
scroggo@google.com7b056592013-11-05 15:57:21 +0000150// Rounded rect with two opposite corners with large radii, the other two
151// small.
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000152DEF_GM(return new BlurRoundRectGM(100, 100);)
scroggo@google.com7b056592013-11-05 15:57:21 +0000153
scroggo@google.com8610d2c2013-11-05 21:10:58 +0000154DEF_GM(return new SimpleBlurRoundRectGM();)