epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkBlurMaskFilter.h" |
| 12 | #include "SkCamera.h" |
| 13 | #include "SkColorFilter.h" |
| 14 | #include "SkColorPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkGradientShader.h" |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 16 | #include "SkImage.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkInterpolator.h" |
| 18 | #include "SkMaskFilter.h" |
| 19 | #include "SkPath.h" |
| 20 | #include "SkRegion.h" |
| 21 | #include "SkShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkTime.h" |
| 23 | #include "SkTypeface.h" |
| 24 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkDrawFilter.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 26 | #include "SkClipOpPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 28 | static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | SkColor colors[] = { 0, SK_ColorWHITE }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 30 | SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 31 | paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 32 | SkShader::kClamp_TileMode, 0, &localMatrix)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 33 | paint->setBlendMode(SkBlendMode::kDstIn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | // test drawing with strips of fading gradient above and below |
| 37 | static void test_fade(SkCanvas* canvas) { |
| 38 | SkAutoCanvasRestore ar(canvas, true); |
| 39 | |
| 40 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | SkPaint p; |
| 43 | p.setAlpha(0x88); |
| 44 | |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 45 | SkAutoCanvasRestore ar2(canvas, false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | |
| 47 | // create the layers |
| 48 | |
| 49 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); |
| 50 | canvas->clipRect(r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | r.fBottom = SkIntToScalar(20); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | canvas->saveLayer(&r, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | |
| 55 | r.fTop = SkIntToScalar(80); |
| 56 | r.fBottom = SkIntToScalar(100); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | canvas->saveLayer(&r, nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
| 59 | // now draw the "content" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | |
| 61 | if (true) { |
| 62 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); |
| 63 | |
| 64 | canvas->saveLayerAlpha(&r, 0x80); |
| 65 | |
| 66 | SkPaint p; |
| 67 | p.setColor(SK_ColorRED); |
| 68 | p.setAntiAlias(true); |
| 69 | canvas->drawOval(r, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | canvas->restore(); |
| 72 | } else { |
| 73 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkPaint p; |
| 76 | p.setColor(SK_ColorRED); |
| 77 | p.setAntiAlias(true); |
| 78 | canvas->drawOval(r, p); |
| 79 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | // return; |
| 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | // now apply an effect |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 84 | SkMatrix m; |
| 85 | m.setScale(SK_Scalar1, -SK_Scalar1); |
| 86 | m.postTranslate(0, SkIntToScalar(100)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | |
| 88 | SkPaint paint; |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 89 | make_paint(&paint, m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20)); |
| 91 | // SkDebugf("--------- draw top grad\n"); |
| 92 | canvas->drawRect(r, paint); |
| 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | r.fTop = SkIntToScalar(80); |
| 95 | r.fBottom = SkIntToScalar(100); |
| 96 | // SkDebugf("--------- draw bot grad\n"); |
| 97 | canvas->drawRect(r, paint); |
| 98 | } |
| 99 | |
| 100 | class RedFilter : public SkDrawFilter { |
| 101 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 102 | bool filter(SkPaint* p, SkDrawFilter::Type) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | fColor = p->getColor(); |
| 104 | if (fColor == SK_ColorRED) { |
| 105 | p->setColor(SK_ColorGREEN); |
| 106 | } |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 107 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | private: |
| 111 | SkColor fColor; |
| 112 | }; |
| 113 | |
| 114 | class LayersView : public SkView { |
| 115 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 116 | LayersView() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | |
| 118 | protected: |
| 119 | // overrides from SkEventSink |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 120 | bool onQuery(SkEvent* evt) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | if (SampleCode::TitleQ(*evt)) { |
| 122 | SampleCode::TitleR(evt, "Layers"); |
| 123 | return true; |
| 124 | } |
| 125 | return this->INHERITED::onQuery(evt); |
| 126 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 127 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 129 | canvas->drawColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 131 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 132 | void onDraw(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | this->drawBG(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 135 | if (true) { |
| 136 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 137 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 138 | SkIntToScalar(220), SkIntToScalar(120)); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 139 | SkPaint p; |
| 140 | canvas->saveLayer(&r, &p); |
| 141 | canvas->drawColor(0xFFFF0000); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 142 | p.setAlpha(0); // or 0 |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 143 | p.setBlendMode(SkBlendMode::kSrc); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 144 | canvas->drawOval(r, p); |
| 145 | canvas->restore(); |
| 146 | return; |
| 147 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | if (false) { |
| 150 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 151 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 152 | SkIntToScalar(220), SkIntToScalar(120)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | SkPaint p; |
| 154 | p.setAlpha(0x88); |
| 155 | p.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | if (true) { |
| 158 | canvas->saveLayer(&r, &p); |
| 159 | p.setColor(0xFFFF0000); |
| 160 | canvas->drawOval(r, p); |
| 161 | canvas->restore(); |
| 162 | } |
| 163 | |
| 164 | p.setColor(0xFF0000FF); |
| 165 | r.offset(SkIntToScalar(20), SkIntToScalar(50)); |
| 166 | canvas->drawOval(r, p); |
| 167 | } |
| 168 | |
| 169 | if (false) { |
| 170 | SkPaint p; |
| 171 | p.setAlpha(0x88); |
| 172 | p.setAntiAlias(true); |
| 173 | |
| 174 | canvas->translate(SkIntToScalar(300), 0); |
| 175 | |
| 176 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 177 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 178 | SkIntToScalar(220), SkIntToScalar(60)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 180 | canvas->saveLayer(&r, &p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 183 | SkIntToScalar(220), SkIntToScalar(120)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | p.setColor(SK_ColorBLUE); |
| 185 | canvas->drawOval(r, p); |
| 186 | canvas->restore(); |
| 187 | return; |
| 188 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 189 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | test_fade(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 192 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | private: |
| 194 | typedef SkView INHERITED; |
| 195 | }; |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 196 | DEF_SAMPLE( return new LayersView; ) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | |
| 198 | ////////////////////////////////////////////////////////////////////////////// |
| 199 | |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 200 | #include "SkBlurImageFilter.h" |
| 201 | #include "SkMatrixConvolutionImageFilter.h" |
| 202 | #include "SkMorphologyImageFilter.h" |
| 203 | |
| 204 | #include "Resources.h" |
| 205 | #include "SkAnimTimer.h" |
| 206 | |
| 207 | class BackdropView : public SampleView { |
| 208 | SkPoint fCenter; |
| 209 | SkScalar fAngle; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 210 | sk_sp<SkImage> fImage; |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 211 | sk_sp<SkImageFilter> fFilter; |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 212 | public: |
| 213 | BackdropView() { |
| 214 | fCenter.set(200, 150); |
| 215 | fAngle = 0; |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 216 | fImage = GetResourceAsImage("images/mandrill_512.png"); |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 217 | fFilter = SkDilateImageFilter::Make(8, 8, nullptr); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | protected: |
| 221 | // overrides from SkEventSink |
| 222 | bool onQuery(SkEvent* evt) override { |
| 223 | if (SampleCode::TitleQ(*evt)) { |
| 224 | SampleCode::TitleR(evt, "Backdrop"); |
| 225 | return true; |
| 226 | } |
| 227 | return this->INHERITED::onQuery(evt); |
| 228 | } |
| 229 | |
| 230 | void onDrawContent(SkCanvas* canvas) override { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 231 | canvas->drawImage(fImage.get(), 0, 0, nullptr); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 232 | |
| 233 | const SkScalar w = 250; |
| 234 | const SkScalar h = 150; |
| 235 | SkPath path; |
| 236 | path.addOval(SkRect::MakeXYWH(-w/2, -h/2, w, h)); |
| 237 | SkMatrix m; |
| 238 | m.setRotate(fAngle); |
| 239 | m.postTranslate(fCenter.x(), fCenter.y()); |
| 240 | path.transform(m); |
| 241 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 242 | canvas->clipPath(path, kIntersect_SkClipOp, true); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 243 | const SkRect bounds = path.getBounds(); |
| 244 | |
| 245 | SkPaint paint; |
| 246 | paint.setAlpha(0xCC); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 247 | canvas->saveLayer({ &bounds, &paint, fFilter.get(), nullptr, nullptr, 0 }); |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 248 | |
| 249 | canvas->restore(); |
| 250 | } |
| 251 | |
| 252 | bool onAnimate(const SkAnimTimer& timer) override { |
| 253 | fAngle = SkDoubleToScalar(fmod(timer.secs() * 360 / 5, 360)); |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 258 | return new Click(this); |
| 259 | } |
| 260 | |
| 261 | bool onClick(Click* click) override { |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 262 | fCenter = click->fCurr; |
| 263 | return this->INHERITED::onClick(click); |
| 264 | } |
| 265 | |
| 266 | private: |
| 267 | typedef SampleView INHERITED; |
| 268 | }; |
| 269 | DEF_SAMPLE( return new BackdropView; ) |