bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Intel 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 | */ |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkDrawLooper.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkMaskFilter.h" |
| 14 | #include "include/core/SkMatrix.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkPoint.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkShader.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTileMode.h" |
| 22 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/effects/SkBlurDrawLooper.h" |
| 24 | #include "include/effects/SkGradientShader.h" |
| 25 | #include "include/private/SkTArray.h" |
| 26 | #include "include/utils/SkRandom.h" |
| 27 | #include "src/core/SkBlurMask.h" |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 28 | |
| 29 | namespace skiagm { |
| 30 | |
| 31 | class CircleGM : public GM { |
Mike Reed | aed493e | 2019-07-23 09:49:59 -0400 | [diff] [blame] | 32 | sk_sp<SkDrawLooper> fLooper; |
| 33 | enum { |
| 34 | kLooperColorSentinel = 0x01020304 |
| 35 | }; |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 36 | public: |
| 37 | CircleGM() { |
| 38 | this->setBGColor(0xFF000000); |
| 39 | this->makePaints(); |
| 40 | this->makeMatrices(); |
| 41 | } |
| 42 | |
| 43 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | SkString onShortName() override { |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 46 | return SkString("circles"); |
| 47 | } |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 50 | return SkISize::Make(1200, 900); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void makePaints() { |
| 54 | { |
| 55 | // no AA |
| 56 | SkPaint p; |
| 57 | fPaints.push_back(p); |
| 58 | } |
| 59 | |
| 60 | { |
| 61 | // AA |
| 62 | SkPaint p; |
| 63 | p.setAntiAlias(true); |
| 64 | fPaints.push_back(p); |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | // AA with mask filter |
| 69 | SkPaint p; |
| 70 | p.setAntiAlias(true); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 71 | p.setMaskFilter(SkMaskFilter::MakeBlur( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 72 | kNormal_SkBlurStyle, |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 73 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)))); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 74 | fPaints.push_back(p); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | // AA with radial shader |
| 79 | SkPaint p; |
| 80 | p.setAntiAlias(true); |
| 81 | SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40)); |
| 82 | SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN }; |
| 83 | SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 84 | p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 85 | SkTileMode::kClamp)); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 86 | fPaints.push_back(p); |
| 87 | } |
| 88 | |
Mike Reed | aed493e | 2019-07-23 09:49:59 -0400 | [diff] [blame] | 89 | fLooper = SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(10),5,10); |
| 90 | { |
| 91 | SkPaint p; |
| 92 | p.setColor(kLooperColorSentinel); |
| 93 | p.setAntiAlias(true); |
| 94 | fPaints.push_back(p); |
| 95 | } |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 96 | { |
| 97 | // AA with stroke style |
| 98 | SkPaint p; |
| 99 | p.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 100 | p.setStroke(true); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 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); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 109 | p.setStroke(true); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 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(); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 158 | SkScalar giantRadius = std::min(SkIntToScalar(size.fWidth), |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 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); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 165 | canvas->drawCircle(giantCenter, giantRadius, giantPaint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 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); |
Mike Reed | aed493e | 2019-07-23 09:49:59 -0400 | [diff] [blame] | 175 | SkPaint p = fPaints[i]; |
| 176 | p.setColor(rand.nextU() | 0xff000000); |
| 177 | if (fPaints[i].getColor() == kLooperColorSentinel) { |
| 178 | fLooper->apply(canvas, p, [](SkCanvas* c, const SkPaint& p) { |
| 179 | c->drawCircle(40, 40, 20, p); |
| 180 | }); |
| 181 | } else { |
| 182 | canvas->drawCircle(40, 40, 20, p); |
| 183 | } |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 184 | canvas->restore(); |
| 185 | } |
| 186 | |
| 187 | for (int j = 0; j < fMatrices.count(); ++j, ++i) { |
| 188 | canvas->save(); |
| 189 | |
| 190 | canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4, |
| 191 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4); |
| 192 | |
| 193 | canvas->concat(fMatrices[j]); |
| 194 | |
| 195 | SkPaint paint; |
| 196 | paint.setAntiAlias(true); |
Mike Reed | aed493e | 2019-07-23 09:49:59 -0400 | [diff] [blame] | 197 | paint.setColor(rand.nextU() | 0xff000000); |
| 198 | canvas->drawCircle(40, 40, 20, paint); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 199 | |
| 200 | canvas->restore(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | typedef GM INHERITED; |
| 206 | SkTArray<SkPaint> fPaints; |
| 207 | SkTArray<SkMatrix> fMatrices; |
| 208 | }; |
| 209 | |
| 210 | ////////////////////////////////////////////////////////////////////////////// |
| 211 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 212 | DEF_GM( return new CircleGM; ) |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 213 | |
| 214 | } |