epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google 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 | */ |
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" |
| 15 | #include "SkDevice.h" |
| 16 | #include "SkGradientShader.h" |
| 17 | #include "SkImageDecoder.h" |
| 18 | #include "SkInterpolator.h" |
| 19 | #include "SkMaskFilter.h" |
| 20 | #include "SkPath.h" |
| 21 | #include "SkRegion.h" |
| 22 | #include "SkShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkTime.h" |
| 24 | #include "SkTypeface.h" |
| 25 | #include "SkUtils.h" |
| 26 | #include "SkKey.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | #include "SkXfermode.h" |
| 28 | #include "SkDrawFilter.h" |
| 29 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 30 | static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | SkColor colors[] = { 0, SK_ColorWHITE }; |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 32 | SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } }; |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 33 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 34 | 0, &localMatrix); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | paint->setShader(s)->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 37 | paint->setXfermodeMode(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static void dump_layers(const char label[], SkCanvas* canvas) { |
| 41 | SkDebugf("Dump Layers(%s)\n", label); |
| 42 | |
| 43 | SkCanvas::LayerIter iter(canvas, true); |
| 44 | int index = 0; |
| 45 | while (!iter.done()) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 46 | SkImageInfo info = iter.device()->imageInfo(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | const SkIRect& clip = iter.clip().getBounds(); |
| 48 | SkDebugf("Layer[%d] bitmap [%d %d] X=%d Y=%d clip=[%d %d %d %d] alpha=%d\n", index++, |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 49 | info.width(), info.height(), iter.x(), iter.y(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | clip.fLeft, clip.fTop, clip.fRight, clip.fBottom, |
| 51 | iter.paint().getAlpha()); |
| 52 | iter.next(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // test drawing with strips of fading gradient above and below |
| 57 | static void test_fade(SkCanvas* canvas) { |
| 58 | SkAutoCanvasRestore ar(canvas, true); |
| 59 | |
| 60 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 61 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | SkPaint p; |
| 63 | p.setAlpha(0x88); |
| 64 | |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 65 | SkAutoCanvasRestore ar2(canvas, false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | |
| 67 | // create the layers |
| 68 | |
| 69 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); |
| 70 | canvas->clipRect(r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | r.fBottom = SkIntToScalar(20); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 73 | canvas->saveLayer(&r, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | |
| 75 | r.fTop = SkIntToScalar(80); |
| 76 | r.fBottom = SkIntToScalar(100); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 77 | canvas->saveLayer(&r, NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 78 | |
| 79 | // now draw the "content" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | |
| 81 | if (true) { |
| 82 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); |
| 83 | |
| 84 | canvas->saveLayerAlpha(&r, 0x80); |
| 85 | |
| 86 | SkPaint p; |
| 87 | p.setColor(SK_ColorRED); |
| 88 | p.setAntiAlias(true); |
| 89 | canvas->drawOval(r, p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 90 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | dump_layers("inside layer alpha", canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | canvas->restore(); |
| 94 | } else { |
| 95 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | SkPaint p; |
| 98 | p.setColor(SK_ColorRED); |
| 99 | p.setAntiAlias(true); |
| 100 | canvas->drawOval(r, p); |
| 101 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | // return; |
| 104 | |
| 105 | dump_layers("outside layer alpha", canvas); |
| 106 | |
| 107 | // now apply an effect |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 108 | SkMatrix m; |
| 109 | m.setScale(SK_Scalar1, -SK_Scalar1); |
| 110 | m.postTranslate(0, SkIntToScalar(100)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | |
| 112 | SkPaint paint; |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 113 | make_paint(&paint, m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20)); |
| 115 | // SkDebugf("--------- draw top grad\n"); |
| 116 | canvas->drawRect(r, paint); |
| 117 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | r.fTop = SkIntToScalar(80); |
| 119 | r.fBottom = SkIntToScalar(100); |
| 120 | // SkDebugf("--------- draw bot grad\n"); |
| 121 | canvas->drawRect(r, paint); |
| 122 | } |
| 123 | |
| 124 | class RedFilter : public SkDrawFilter { |
| 125 | public: |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 126 | virtual bool filter(SkPaint* p, SkDrawFilter::Type) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | fColor = p->getColor(); |
| 128 | if (fColor == SK_ColorRED) { |
| 129 | p->setColor(SK_ColorGREEN); |
| 130 | } |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 131 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | private: |
| 135 | SkColor fColor; |
| 136 | }; |
| 137 | |
| 138 | class LayersView : public SkView { |
| 139 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 140 | LayersView() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | |
| 142 | protected: |
| 143 | // overrides from SkEventSink |
| 144 | virtual bool onQuery(SkEvent* evt) { |
| 145 | if (SampleCode::TitleQ(*evt)) { |
| 146 | SampleCode::TitleR(evt, "Layers"); |
| 147 | return true; |
| 148 | } |
| 149 | return this->INHERITED::onQuery(evt); |
| 150 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 153 | canvas->drawColor(SK_ColorGRAY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 155 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | virtual void onDraw(SkCanvas* canvas) { |
| 157 | this->drawBG(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 159 | if (true) { |
| 160 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 161 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 162 | SkIntToScalar(220), SkIntToScalar(120)); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 163 | SkPaint p; |
| 164 | canvas->saveLayer(&r, &p); |
| 165 | canvas->drawColor(0xFFFF0000); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 166 | p.setAlpha(0); // or 0 |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 167 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 168 | canvas->drawOval(r, p); |
| 169 | canvas->restore(); |
| 170 | return; |
| 171 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 172 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | if (false) { |
| 174 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 175 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 176 | SkIntToScalar(220), SkIntToScalar(120)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | SkPaint p; |
| 178 | p.setAlpha(0x88); |
| 179 | p.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 180 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | if (true) { |
| 182 | canvas->saveLayer(&r, &p); |
| 183 | p.setColor(0xFFFF0000); |
| 184 | canvas->drawOval(r, p); |
| 185 | canvas->restore(); |
| 186 | } |
| 187 | |
| 188 | p.setColor(0xFF0000FF); |
| 189 | r.offset(SkIntToScalar(20), SkIntToScalar(50)); |
| 190 | canvas->drawOval(r, p); |
| 191 | } |
| 192 | |
| 193 | if (false) { |
| 194 | SkPaint p; |
| 195 | p.setAlpha(0x88); |
| 196 | p.setAntiAlias(true); |
| 197 | |
| 198 | canvas->translate(SkIntToScalar(300), 0); |
| 199 | |
| 200 | SkRect r; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 201 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 202 | SkIntToScalar(220), SkIntToScalar(60)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 204 | canvas->saveLayer(&r, &p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 206 | r.set(SkIntToScalar(0), SkIntToScalar(0), |
| 207 | SkIntToScalar(220), SkIntToScalar(120)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | p.setColor(SK_ColorBLUE); |
| 209 | canvas->drawOval(r, p); |
| 210 | canvas->restore(); |
| 211 | return; |
| 212 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 213 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | test_fade(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 216 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 217 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 218 | unsigned modi) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | this->inval(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 220 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 221 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 223 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | virtual bool onClick(Click* click) { |
| 225 | return this->INHERITED::onClick(click); |
| 226 | } |
| 227 | |
sugoi@google.com | 93c7ee3 | 2013-03-12 14:36:57 +0000 | [diff] [blame] | 228 | virtual bool handleKey(SkKey) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | this->inval(NULL); |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | typedef SkView INHERITED; |
| 235 | }; |
| 236 | |
| 237 | ////////////////////////////////////////////////////////////////////////////// |
| 238 | |
| 239 | static SkView* MyFactory() { return new LayersView; } |
| 240 | static SkViewRegister reg(MyFactory); |