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