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@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
| 9 | #include "SkBlurMaskFilter.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkLayerRasterizer.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 15 | #include "SkPaint.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 16 | #include "SkView.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 17 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 18 | #define BG_COLOR 0xFFDDDDDD |
| 19 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 20 | typedef void (*SlideProc)(SkCanvas*); |
| 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | #include "Sk1DPathEffect.h" |
| 25 | #include "Sk2DPathEffect.h" |
| 26 | #include "SkCornerPathEffect.h" |
| 27 | #include "SkDashPathEffect.h" |
| 28 | #include "SkDiscretePathEffect.h" |
| 29 | |
| 30 | static void compose_pe(SkPaint* paint) { |
| 31 | SkPathEffect* pe = paint->getPathEffect(); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 32 | sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25); |
| 33 | sk_sp<SkPathEffect> compose; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 34 | if (pe) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 35 | compose = SkComposePathEffect::Make(sk_ref_sp(pe), corner); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 36 | } else { |
| 37 | compose = corner; |
| 38 | } |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 39 | paint->setPathEffect(compose); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void hair_pe(SkPaint* paint) { |
| 43 | paint->setStrokeWidth(0); |
| 44 | } |
| 45 | |
| 46 | static void hair2_pe(SkPaint* paint) { |
| 47 | paint->setStrokeWidth(0); |
| 48 | compose_pe(paint); |
| 49 | } |
| 50 | |
| 51 | static void stroke_pe(SkPaint* paint) { |
| 52 | paint->setStrokeWidth(12); |
| 53 | compose_pe(paint); |
| 54 | } |
| 55 | |
| 56 | static void dash_pe(SkPaint* paint) { |
| 57 | SkScalar inter[] = { 20, 10, 10, 10 }; |
| 58 | paint->setStrokeWidth(12); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 59 | paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 60 | compose_pe(paint); |
| 61 | } |
| 62 | |
| 63 | static const int gXY[] = { |
| 64 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 65 | }; |
| 66 | |
| 67 | static void scale(SkPath* path, SkScalar scale) { |
| 68 | SkMatrix m; |
| 69 | m.setScale(scale, scale); |
| 70 | path->transform(m); |
| 71 | } |
| 72 | |
| 73 | static void one_d_pe(SkPaint* paint) { |
| 74 | SkPath path; |
| 75 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 76 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 77 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 78 | path.close(); |
| 79 | path.offset(SkIntToScalar(-6), 0); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 80 | scale(&path, 1.5f); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 82 | paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0, |
| 83 | SkPath1DPathEffect::kRotate_Style)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 84 | compose_pe(paint); |
| 85 | } |
| 86 | |
| 87 | typedef void (*PE_Proc)(SkPaint*); |
| 88 | static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; |
| 89 | |
| 90 | static void fill_pe(SkPaint* paint) { |
| 91 | paint->setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | paint->setPathEffect(nullptr); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void discrete_pe(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 96 | paint->setPathEffect(SkDiscretePathEffect::Make(10, 4)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 97 | } |
| 98 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 99 | static sk_sp<SkPathEffect> MakeTileEffect() { |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 100 | SkMatrix m; |
| 101 | m.setScale(SkIntToScalar(12), SkIntToScalar(12)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 102 | |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 103 | SkPath path; |
| 104 | path.addCircle(0, 0, SkIntToScalar(5)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 106 | return SkPath2DPathEffect::Make(m, path); |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 107 | } |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 108 | |
| 109 | static void tile_pe(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 110 | paint->setPathEffect(MakeTileEffect()); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; |
| 114 | |
| 115 | static void patheffect_slide(SkCanvas* canvas) { |
| 116 | SkPaint paint; |
| 117 | paint.setAntiAlias(true); |
| 118 | paint.setStyle(SkPaint::kStroke_Style); |
| 119 | |
| 120 | SkPath path; |
| 121 | path.moveTo(20, 20); |
| 122 | path.lineTo(70, 120); |
| 123 | path.lineTo(120, 30); |
| 124 | path.lineTo(170, 80); |
| 125 | path.lineTo(240, 50); |
| 126 | |
| 127 | size_t i; |
| 128 | canvas->save(); |
| 129 | for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
| 130 | gPE[i](&paint); |
| 131 | canvas->drawPath(path, paint); |
| 132 | canvas->translate(0, 75); |
| 133 | } |
| 134 | canvas->restore(); |
| 135 | |
| 136 | path.reset(); |
| 137 | SkRect r = { 0, 0, 250, 120 }; |
| 138 | path.addOval(r, SkPath::kCW_Direction); |
| 139 | r.inset(50, 50); |
| 140 | path.addRect(r, SkPath::kCCW_Direction); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 141 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 142 | canvas->translate(320, 20); |
| 143 | for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) { |
| 144 | gPE2[i](&paint); |
| 145 | canvas->drawPath(path, paint); |
| 146 | canvas->translate(0, 160); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /////////////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | #include "SkGradientShader.h" |
| 153 | |
| 154 | struct GradData { |
| 155 | int fCount; |
| 156 | const SkColor* fColors; |
| 157 | const SkScalar* fPos; |
| 158 | }; |
| 159 | |
| 160 | static const SkColor gColors[] = { |
| 161 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 162 | }; |
| 163 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 164 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 165 | static const SkScalar gPos2[] = { |
| 166 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 167 | }; |
| 168 | |
| 169 | static const GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 170 | { 2, gColors, nullptr }, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 171 | { 2, gColors, gPos0 }, |
| 172 | { 2, gColors, gPos1 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 173 | { 5, gColors, nullptr }, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 174 | { 5, gColors, gPos2 } |
| 175 | }; |
| 176 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 177 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
| 178 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 179 | } |
| 180 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 181 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 182 | SkPoint center; |
| 183 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 184 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 185 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 186 | data.fPos, data.fCount, tm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 187 | } |
| 188 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 189 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 190 | SkPoint center; |
| 191 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 192 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 193 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 194 | } |
| 195 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 196 | static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 197 | SkPoint center0, center1; |
| 198 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 199 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 200 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 201 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 202 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 203 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 204 | data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 205 | } |
| 206 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 207 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 208 | static const GradMaker gGradMakers[] = { |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 209 | MakeLinear, MakeRadial, MakeSweep, Make2Conical |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | static void gradient_slide(SkCanvas* canvas) { |
| 213 | SkPoint pts[2] = { |
| 214 | { 0, 0 }, |
| 215 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 216 | }; |
| 217 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 218 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 219 | SkPaint paint; |
| 220 | paint.setAntiAlias(true); |
| 221 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 222 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 223 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
| 224 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 225 | canvas->save(); |
| 226 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 227 | paint.setShader(gGradMakers[j](pts, gGradData[i], tm)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 228 | canvas->drawRect(r, paint); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 229 | canvas->translate(0, SkIntToScalar(120)); |
| 230 | } |
| 231 | canvas->restore(); |
| 232 | canvas->translate(SkIntToScalar(120), 0); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /////////////////////////////////////////////////////////////////////////////// |
| 237 | |
| 238 | #include "SkPathMeasure.h" |
| 239 | |
| 240 | static SkScalar getpathlen(const SkPath& path) { |
| 241 | SkPathMeasure meas(path, false); |
| 242 | return meas.getLength(); |
| 243 | } |
| 244 | |
| 245 | static void textonpath_slide(SkCanvas* canvas) { |
| 246 | const char* text = "Displacement"; |
| 247 | size_t len =strlen(text); |
| 248 | SkPath path; |
| 249 | path.moveTo(100, 300); |
| 250 | path.quadTo(300, 100, 500, 300); |
| 251 | path.offset(0, -100); |
| 252 | |
| 253 | SkPaint paint; |
| 254 | paint.setAntiAlias(true); |
| 255 | paint.setTextSize(40); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 256 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 257 | paint.setStyle(SkPaint::kStroke_Style); |
| 258 | canvas->drawPath(path, paint); |
| 259 | paint.setStyle(SkPaint::kFill_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 260 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 261 | SkScalar x = 50; |
| 262 | paint.setColor(0xFF008800); |
| 263 | canvas->drawTextOnPathHV(text, len, path, |
| 264 | x, paint.getTextSize()*2/3, paint); |
| 265 | paint.setColor(SK_ColorRED); |
| 266 | canvas->drawTextOnPathHV(text, len, path, |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 267 | x + 60, 0, paint); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 268 | paint.setColor(SK_ColorBLUE); |
| 269 | canvas->drawTextOnPathHV(text, len, path, |
| 270 | x + 120, -paint.getTextSize()*2/3, paint); |
| 271 | |
| 272 | path.offset(0, 200); |
| 273 | paint.setTextAlign(SkPaint::kRight_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 274 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 275 | text = "Matrices"; |
| 276 | len = strlen(text); |
| 277 | SkScalar pathLen = getpathlen(path); |
| 278 | SkMatrix matrix; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 279 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 280 | paint.setColor(SK_ColorBLACK); |
| 281 | paint.setStyle(SkPaint::kStroke_Style); |
| 282 | canvas->drawPath(path, paint); |
| 283 | paint.setStyle(SkPaint::kFill_Style); |
| 284 | |
| 285 | paint.setTextSize(50); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | canvas->drawTextOnPath(text, len, path, nullptr, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 287 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 288 | paint.setColor(SK_ColorRED); |
| 289 | matrix.setScale(-SK_Scalar1, SK_Scalar1); |
| 290 | matrix.postTranslate(pathLen, 0); |
| 291 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 292 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 293 | paint.setColor(SK_ColorBLUE); |
| 294 | matrix.setScale(SK_Scalar1, -SK_Scalar1); |
| 295 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 296 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 297 | paint.setColor(0xFF008800); |
| 298 | matrix.setScale(-SK_Scalar1, -SK_Scalar1); |
| 299 | matrix.postTranslate(pathLen, 0); |
| 300 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 301 | } |
| 302 | |
| 303 | /////////////////////////////////////////////////////////////////////////////// |
| 304 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 305 | #include "DecodeFile.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 306 | #include "SkOSFile.h" |
| 307 | #include "SkRandom.h" |
| 308 | #include "SkStream.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 309 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 310 | static sk_sp<SkShader> make_shader0(SkIPoint* size) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 311 | SkBitmap bm; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 312 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 313 | decode_file("/skimages/logo.gif", &bm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 314 | size->set(bm.width(), bm.height()); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 315 | return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 316 | SkShader::kClamp_TileMode); |
| 317 | } |
| 318 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 319 | static sk_sp<SkShader> make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 320 | SkPoint pts[] = { { 0, 0 }, |
| 321 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 322 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 323 | return SkGradientShader::MakeLinear(pts, colors, nullptr, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 324 | SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 325 | } |
| 326 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 327 | class Rec { |
| 328 | public: |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 329 | SkCanvas::VertexMode fMode; |
| 330 | int fCount; |
| 331 | SkPoint* fVerts; |
| 332 | SkPoint* fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 333 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 334 | Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 335 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 336 | }; |
| 337 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 338 | static void make_tris(Rec* rec) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 339 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 340 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 341 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 342 | rec->fMode = SkCanvas::kTriangles_VertexMode; |
| 343 | rec->fCount = n * 3; |
| 344 | rec->fVerts = new SkPoint[rec->fCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 345 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 346 | for (int i = 0; i < n; i++) { |
| 347 | SkPoint* v = &rec->fVerts[i*3]; |
| 348 | for (int j = 0; j < 3; j++) { |
| 349 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 354 | static void make_fan(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 355 | const SkScalar tx = SkIntToScalar(texWidth); |
| 356 | const SkScalar ty = SkIntToScalar(texHeight); |
| 357 | const int n = 24; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 358 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 359 | rec->fMode = SkCanvas::kTriangleFan_VertexMode; |
| 360 | rec->fCount = n + 2; |
| 361 | rec->fVerts = new SkPoint[rec->fCount]; |
| 362 | rec->fTexs = new SkPoint[rec->fCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 363 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 364 | SkPoint* v = rec->fVerts; |
| 365 | SkPoint* t = rec->fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 366 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 367 | v[0].set(0, 0); |
| 368 | t[0].set(0, 0); |
| 369 | for (int i = 0; i < n; i++) { |
| 370 | SkScalar cos; |
| 371 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 372 | v[i+1].set(cos, sin); |
| 373 | t[i+1].set(i*tx/n, ty); |
| 374 | } |
| 375 | v[n+1] = v[1]; |
| 376 | t[n+1].set(tx, ty); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 377 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 378 | SkMatrix m; |
| 379 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 380 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 381 | m.mapPoints(v, rec->fCount); |
| 382 | } |
| 383 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 384 | static void make_strip(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 385 | const SkScalar tx = SkIntToScalar(texWidth); |
| 386 | const SkScalar ty = SkIntToScalar(texHeight); |
| 387 | const int n = 24; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 388 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 389 | rec->fMode = SkCanvas::kTriangleStrip_VertexMode; |
| 390 | rec->fCount = 2 * (n + 1); |
| 391 | rec->fVerts = new SkPoint[rec->fCount]; |
| 392 | rec->fTexs = new SkPoint[rec->fCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 393 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 394 | SkPoint* v = rec->fVerts; |
| 395 | SkPoint* t = rec->fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 396 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 397 | for (int i = 0; i < n; i++) { |
| 398 | SkScalar cos; |
| 399 | SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos); |
| 400 | v[i*2 + 0].set(cos/2, sin/2); |
| 401 | v[i*2 + 1].set(cos, sin); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 402 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 403 | t[i*2 + 0].set(tx * i / n, ty); |
| 404 | t[i*2 + 1].set(tx * i / n, 0); |
| 405 | } |
| 406 | v[2*n + 0] = v[0]; |
| 407 | v[2*n + 1] = v[1]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 408 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 409 | t[2*n + 0].set(tx, ty); |
| 410 | t[2*n + 1].set(tx, 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 411 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 412 | SkMatrix m; |
| 413 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 414 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 415 | m.mapPoints(v, rec->fCount); |
| 416 | } |
| 417 | |
| 418 | static void mesh_slide(SkCanvas* canvas) { |
| 419 | Rec fRecs[3]; |
| 420 | SkIPoint size; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 421 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 422 | auto fShader0 = make_shader0(&size); |
| 423 | auto fShader1 = make_shader1(size); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 424 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 425 | make_strip(&fRecs[0], size.fX, size.fY); |
| 426 | make_fan(&fRecs[1], size.fX, size.fY); |
| 427 | make_tris(&fRecs[2]); |
| 428 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 429 | SkPaint paint; |
| 430 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 431 | paint.setFilterQuality(kLow_SkFilterQuality); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 432 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 433 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 434 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 435 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 436 | paint.setShader(nullptr); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 437 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 438 | fRecs[i].fVerts, fRecs[i].fTexs, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 439 | nullptr, nullptr, nullptr, 0, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 440 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 441 | canvas->translate(SkIntToScalar(210), 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 442 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 443 | paint.setShader(fShader0); |
| 444 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 445 | fRecs[i].fVerts, fRecs[i].fTexs, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 446 | nullptr, nullptr, nullptr, 0, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 447 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 448 | canvas->translate(SkIntToScalar(210), 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 449 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 450 | paint.setShader(fShader1); |
| 451 | canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount, |
| 452 | fRecs[i].fVerts, fRecs[i].fTexs, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 453 | nullptr, nullptr, nullptr, 0, paint); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 454 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 455 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 456 | canvas->translate(0, SkIntToScalar(250)); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /////////////////////////////////////////////////////////////////////////////// |
| 461 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 462 | static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 463 | { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 464 | p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 465 | SkBlurMask::ConvertRadiusToSigma(3))); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 466 | rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 467 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 468 | p.setMaskFilter(nullptr); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 469 | p.setStyle(SkPaint::kStroke_Style); |
| 470 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 471 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 472 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 473 | p.setAlpha(0x11); |
| 474 | p.setStyle(SkPaint::kFill_Style); |
| 475 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 476 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 477 | } |
| 478 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 479 | static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 480 | { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 481 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 482 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 483 | p.setAlpha(0x40); |
| 484 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 485 | p.setStyle(SkPaint::kStroke_Style); |
| 486 | p.setStrokeWidth(SK_Scalar1*2); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 487 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 488 | } |
| 489 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 490 | static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 491 | { |
| 492 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 493 | p.setStrokeWidth(SK_Scalar1*4); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 494 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 495 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 496 | p.setStyle(SkPaint::kStroke_Style); |
| 497 | p.setStrokeWidth(SK_Scalar1*3/2); |
| 498 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 499 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 500 | } |
| 501 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 502 | static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 503 | { |
| 504 | p.setStyle(SkPaint::kStroke_Style); |
| 505 | p.setStrokeWidth(SK_Scalar1*3); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 506 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 507 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 508 | p.setAlpha(0x20); |
| 509 | p.setStyle(SkPaint::kFill_Style); |
| 510 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 511 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 512 | } |
| 513 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 514 | static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 515 | { |
| 516 | p.setAlpha(0x60); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 517 | rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 518 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 519 | p.setAlpha(0xFF); |
| 520 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 521 | rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 522 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 523 | p.setXfermode(nullptr); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 524 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | #include "SkDiscretePathEffect.h" |
| 528 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 529 | static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 530 | { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 531 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 532 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 533 | p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3)); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 534 | p.setXfermodeMode(SkXfermode::kSrcOut_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 535 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 536 | } |
| 537 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 538 | static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 539 | { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 540 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 541 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 542 | p.setAntiAlias(false); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 543 | SkLayerRasterizer::Builder rastBuilder2; |
| 544 | r5(&rastBuilder2, p); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 545 | p.setRasterizer(rastBuilder2.detach()); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 546 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 547 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | #include "Sk2DPathEffect.h" |
| 551 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 552 | static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) { |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 553 | SkPath path; |
| 554 | path.addCircle(0, 0, radius); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 555 | return SkPath2DPathEffect::Make(matrix, path); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 556 | } |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 557 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 558 | static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 559 | { |
| 560 | SkMatrix lattice; |
| 561 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 562 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 563 | p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice)); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 564 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 565 | } |
| 566 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 567 | static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 568 | { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 569 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 570 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 571 | SkMatrix lattice; |
| 572 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 573 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 574 | p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice)); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 575 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 576 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 577 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 578 | p.setPathEffect(nullptr); |
| 579 | p.setXfermode(nullptr); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 580 | p.setStyle(SkPaint::kStroke_Style); |
| 581 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 582 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 583 | } |
| 584 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 585 | static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 586 | { |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 587 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 588 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 589 | SkMatrix lattice; |
| 590 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 591 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 592 | p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice)); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 593 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 594 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 595 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 596 | p.setPathEffect(nullptr); |
| 597 | p.setXfermode(nullptr); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 598 | p.setStyle(SkPaint::kStroke_Style); |
| 599 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 600 | rastBuilder->addLayer(p); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 601 | } |
| 602 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 603 | typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 604 | |
| 605 | static const raster_proc gRastProcs[] = { |
| 606 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 607 | }; |
| 608 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 609 | static void apply_shader(SkPaint* paint, int index) { |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 610 | raster_proc proc = gRastProcs[index]; |
| 611 | SkPaint p; |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 612 | SkLayerRasterizer::Builder rastBuilder; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 613 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 614 | p.setAntiAlias(true); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 615 | proc(&rastBuilder, p); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 616 | paint->setRasterizer(rastBuilder.detach()); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 617 | paint->setColor(SK_ColorBLUE); |
| 618 | } |
| 619 | |
| 620 | #include "SkTypeface.h" |
| 621 | |
| 622 | static void texteffect_slide(SkCanvas* canvas) { |
| 623 | const char* str = "Google"; |
| 624 | size_t len = strlen(str); |
| 625 | SkScalar x = 20; |
| 626 | SkScalar y = 80; |
| 627 | SkPaint paint; |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 628 | paint.setTypeface(SkTypeface::MakeFromName("Georgia", |
| 629 | SkFontStyle::FromOldStyle(SkTypeface::kItalic))); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 630 | paint.setTextSize(75); |
| 631 | paint.setAntiAlias(true); |
| 632 | paint.setColor(SK_ColorBLUE); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 633 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 634 | apply_shader(&paint, (int)i); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 635 | canvas->drawText(str, len, x, y, paint); |
| 636 | y += 80; |
| 637 | if (i == 4) { |
| 638 | x += 320; |
| 639 | y = 80; |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /////////////////////////////////////////////////////////////////////////////// |
| 645 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 646 | #include "SkImageEncoder.h" |
| 647 | |
| 648 | static const SlideProc gProc[] = { |
| 649 | patheffect_slide, |
| 650 | gradient_slide, |
| 651 | textonpath_slide, |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 652 | mesh_slide, |
| 653 | texteffect_slide |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 654 | }; |
| 655 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 656 | class SlideView : public SampleView { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 657 | int fIndex; |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 658 | bool fOnce; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 659 | public: |
| 660 | SlideView() { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 661 | fOnce = false; |
| 662 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 663 | |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 664 | void init() { |
| 665 | if (fOnce) { |
| 666 | return; |
| 667 | } |
| 668 | fOnce = true; |
| 669 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 670 | fIndex = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 671 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 672 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 673 | bm.allocN32Pixels(1024, 768); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 674 | SkCanvas canvas(bm); |
| 675 | SkScalar s = SkIntToScalar(1024) / 640; |
| 676 | canvas.scale(s, s); |
| 677 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) { |
| 678 | canvas.save(); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 679 | canvas.drawColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 680 | gProc[i](&canvas); |
| 681 | canvas.restore(); |
| 682 | SkString str; |
senorblanco@chromium.org | 3a67a66 | 2012-07-09 18:22:08 +0000 | [diff] [blame] | 683 | str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 684 | SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100); |
| 685 | } |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 686 | this->setBGColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 687 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 688 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 689 | protected: |
| 690 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 691 | bool onQuery(SkEvent* evt) override { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 692 | if (SampleCode::TitleQ(*evt)) { |
| 693 | SampleCode::TitleR(evt, "Slides"); |
| 694 | return true; |
| 695 | } |
| 696 | return this->INHERITED::onQuery(evt); |
| 697 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 698 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 699 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 700 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 701 | gProc[fIndex](canvas); |
| 702 | } |
| 703 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 704 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 705 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 706 | fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 707 | this->inval(nullptr); |
| 708 | return nullptr; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 712 | typedef SampleView INHERITED; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 713 | }; |
| 714 | |
| 715 | ////////////////////////////////////////////////////////////////////////////// |
| 716 | |
| 717 | static SkView* MyFactory() { return new SlideView; } |
| 718 | static SkViewRegister reg(MyFactory); |