bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Intel Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurDrawLooper.h" |
| 10 | #include "SkBlurMask.h" |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 11 | #include "SkBlurMaskFilter.h" |
| 12 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkTArray.h" |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class CircleGM : public GM { |
| 20 | public: |
| 21 | CircleGM() { |
| 22 | this->setBGColor(0xFF000000); |
| 23 | this->makePaints(); |
| 24 | this->makeMatrices(); |
| 25 | } |
| 26 | |
| 27 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkString onShortName() override { |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 30 | return SkString("circles"); |
| 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 34 | return SkISize::Make(1200, 900); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void makePaints() { |
| 38 | { |
| 39 | // no AA |
| 40 | SkPaint p; |
| 41 | fPaints.push_back(p); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | // AA |
| 46 | SkPaint p; |
| 47 | p.setAntiAlias(true); |
| 48 | fPaints.push_back(p); |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | // AA with mask filter |
| 53 | SkPaint p; |
| 54 | p.setAntiAlias(true); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 55 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 56 | kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 57 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 58 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 59 | p.setMaskFilter(mf)->unref(); |
| 60 | fPaints.push_back(p); |
| 61 | } |
| 62 | |
| 63 | { |
| 64 | // AA with radial shader |
| 65 | SkPaint p; |
| 66 | p.setAntiAlias(true); |
| 67 | SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40)); |
| 68 | SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN }; |
| 69 | SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
| 70 | SkShader* s = SkGradientShader::CreateRadial(center, |
| 71 | SkIntToScalar(20), |
| 72 | colors, |
| 73 | pos, |
| 74 | SK_ARRAY_COUNT(colors), |
| 75 | SkShader::kClamp_TileMode); |
| 76 | p.setShader(s)->unref(); |
| 77 | fPaints.push_back(p); |
| 78 | } |
| 79 | |
| 80 | { |
| 81 | // AA with blur |
| 82 | SkPaint p; |
| 83 | p.setAntiAlias(true); |
reed | 5e1ddb1 | 2015-12-21 08:52:45 -0800 | [diff] [blame] | 84 | SkDrawLooper* shadowLooper = |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 85 | SkBlurDrawLooper::Create(SK_ColorBLUE, |
| 86 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)), |
| 87 | SkIntToScalar(5), SkIntToScalar(10), |
| 88 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
| 89 | SkBlurDrawLooper::kOverrideColor_BlurFlag | |
| 90 | SkBlurDrawLooper::kHighQuality_BlurFlag); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 91 | SkAutoUnref aurL0(shadowLooper); |
| 92 | p.setLooper(shadowLooper); |
| 93 | fPaints.push_back(p); |
| 94 | } |
| 95 | |
| 96 | { |
| 97 | // AA with stroke style |
| 98 | SkPaint p; |
| 99 | p.setAntiAlias(true); |
| 100 | p.setStyle(SkPaint::kStroke_Style); |
| 101 | p.setStrokeWidth(SkIntToScalar(3)); |
| 102 | fPaints.push_back(p); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | // AA with stroke style, width = 0 |
| 107 | SkPaint p; |
| 108 | p.setAntiAlias(true); |
| 109 | p.setStyle(SkPaint::kStroke_Style); |
| 110 | fPaints.push_back(p); |
| 111 | } |
| 112 | |
| 113 | { |
| 114 | // AA with stroke and fill style |
| 115 | SkPaint p; |
| 116 | p.setAntiAlias(true); |
| 117 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 118 | p.setStrokeWidth(SkIntToScalar(2)); |
| 119 | fPaints.push_back(p); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void makeMatrices() { |
| 124 | { |
| 125 | SkMatrix m; |
| 126 | m.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 127 | fMatrices.push_back(m); |
| 128 | } |
| 129 | |
| 130 | { |
| 131 | SkMatrix m; |
| 132 | m.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 133 | fMatrices.push_back(m); |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | SkMatrix m; |
| 138 | m.setSkew(SkIntToScalar(2), SkIntToScalar(3)); |
| 139 | fMatrices.push_back(m); |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | SkMatrix m; |
| 144 | m.setSkew(SkIntToScalar(2), SkIntToScalar(2)); |
| 145 | fMatrices.push_back(m); |
| 146 | } |
| 147 | |
| 148 | { |
| 149 | SkMatrix m; |
| 150 | m.setRotate(SkIntToScalar(30)); |
| 151 | fMatrices.push_back(m); |
| 152 | } |
| 153 | } |
| 154 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 155 | void onDraw(SkCanvas* canvas) override { |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 156 | // Draw a giant AA circle as the background. |
| 157 | SkISize size = this->getISize(); |
| 158 | SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth), |
| 159 | SkIntToScalar(size.fHeight)) / 2.f; |
| 160 | SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2), |
| 161 | SkIntToScalar(size.fHeight/2)); |
| 162 | SkPaint giantPaint; |
| 163 | giantPaint.setAntiAlias(true); |
| 164 | giantPaint.setColor(0x80808080); |
| 165 | canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint); |
| 166 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 167 | SkRandom rand; |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 168 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 169 | int i; |
| 170 | for (i = 0; i < fPaints.count(); ++i) { |
| 171 | canvas->save(); |
| 172 | // position the path, and make it at off-integer coords. |
| 173 | canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4, |
| 174 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4); |
| 175 | SkColor color = rand.nextU(); |
| 176 | color |= 0xff000000; |
| 177 | fPaints[i].setColor(color); |
| 178 | |
| 179 | canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40), |
| 180 | SkIntToScalar(20), |
| 181 | fPaints[i]); |
| 182 | canvas->restore(); |
| 183 | } |
| 184 | |
| 185 | for (int j = 0; j < fMatrices.count(); ++j, ++i) { |
| 186 | canvas->save(); |
| 187 | |
| 188 | canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4, |
| 189 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4); |
| 190 | |
| 191 | canvas->concat(fMatrices[j]); |
| 192 | |
| 193 | SkPaint paint; |
| 194 | paint.setAntiAlias(true); |
| 195 | |
| 196 | SkColor color = rand.nextU(); |
| 197 | color |= 0xff000000; |
| 198 | paint.setColor(color); |
| 199 | |
| 200 | canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40), |
| 201 | SkIntToScalar(20), |
| 202 | paint); |
| 203 | |
| 204 | canvas->restore(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | typedef GM INHERITED; |
| 210 | SkTArray<SkPaint> fPaints; |
| 211 | SkTArray<SkMatrix> fMatrices; |
| 212 | }; |
| 213 | |
| 214 | ////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| 216 | static GM* MyFactory(void*) { return new CircleGM; } |
| 217 | static GMRegistry reg(MyFactory); |
| 218 | |
| 219 | } |