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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "include/core/SkCanvas.h" |
| 8 | #include "include/core/SkPaint.h" |
| 9 | #include "include/core/SkVertices.h" |
| 10 | #include "include/effects/SkBlurMaskFilter.h" |
| 11 | #include "include/effects/SkGradientShader.h" |
| 12 | #include "samplecode/Sample.h" |
| 13 | #include "src/core/SkBlurMask.h" |
| 14 | #include "src/core/SkReadBuffer.h" |
| 15 | #include "src/core/SkWriteBuffer.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 16 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "tools/ToolUtils.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 18 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 19 | #define BG_COLOR 0xFFDDDDDD |
| 20 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 21 | typedef void (*SlideProc)(SkCanvas*); |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/effects/Sk1DPathEffect.h" |
| 26 | #include "include/effects/Sk2DPathEffect.h" |
| 27 | #include "include/effects/SkCornerPathEffect.h" |
| 28 | #include "include/effects/SkDashPathEffect.h" |
| 29 | #include "include/effects/SkDiscretePathEffect.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 30 | |
| 31 | static void compose_pe(SkPaint* paint) { |
| 32 | SkPathEffect* pe = paint->getPathEffect(); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 33 | sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25); |
| 34 | sk_sp<SkPathEffect> compose; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 35 | if (pe) { |
Mike Reed | a07741a | 2017-02-25 22:34:32 -0500 | [diff] [blame] | 36 | compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 37 | } else { |
| 38 | compose = corner; |
| 39 | } |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 40 | paint->setPathEffect(compose); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void hair_pe(SkPaint* paint) { |
| 44 | paint->setStrokeWidth(0); |
| 45 | } |
| 46 | |
| 47 | static void hair2_pe(SkPaint* paint) { |
| 48 | paint->setStrokeWidth(0); |
| 49 | compose_pe(paint); |
| 50 | } |
| 51 | |
| 52 | static void stroke_pe(SkPaint* paint) { |
| 53 | paint->setStrokeWidth(12); |
| 54 | compose_pe(paint); |
| 55 | } |
| 56 | |
| 57 | static void dash_pe(SkPaint* paint) { |
| 58 | SkScalar inter[] = { 20, 10, 10, 10 }; |
| 59 | paint->setStrokeWidth(12); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 60 | paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 61 | compose_pe(paint); |
| 62 | } |
| 63 | |
| 64 | static const int gXY[] = { |
| 65 | 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4 |
| 66 | }; |
| 67 | |
| 68 | static void scale(SkPath* path, SkScalar scale) { |
| 69 | SkMatrix m; |
| 70 | m.setScale(scale, scale); |
| 71 | path->transform(m); |
| 72 | } |
| 73 | |
| 74 | static void one_d_pe(SkPaint* paint) { |
| 75 | SkPath path; |
| 76 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 77 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 78 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 79 | path.close(); |
| 80 | path.offset(SkIntToScalar(-6), 0); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 81 | scale(&path, 1.5f); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 83 | paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0, |
| 84 | SkPath1DPathEffect::kRotate_Style)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 85 | compose_pe(paint); |
| 86 | } |
| 87 | |
| 88 | typedef void (*PE_Proc)(SkPaint*); |
| 89 | static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe }; |
| 90 | |
| 91 | static void fill_pe(SkPaint* paint) { |
| 92 | paint->setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | paint->setPathEffect(nullptr); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void discrete_pe(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 97 | paint->setPathEffect(SkDiscretePathEffect::Make(10, 4)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 98 | } |
| 99 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 100 | static sk_sp<SkPathEffect> MakeTileEffect() { |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 101 | SkMatrix m; |
| 102 | m.setScale(SkIntToScalar(12), SkIntToScalar(12)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 103 | |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 104 | SkPath path; |
| 105 | path.addCircle(0, 0, SkIntToScalar(5)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 106 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 107 | return SkPath2DPathEffect::Make(m, path); |
reed@google.com | 43e9f20 | 2011-08-09 19:01:50 +0000 | [diff] [blame] | 108 | } |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 109 | |
| 110 | static void tile_pe(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 111 | paint->setPathEffect(MakeTileEffect()); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe }; |
| 115 | |
| 116 | static void patheffect_slide(SkCanvas* canvas) { |
| 117 | SkPaint paint; |
| 118 | paint.setAntiAlias(true); |
| 119 | paint.setStyle(SkPaint::kStroke_Style); |
| 120 | |
| 121 | SkPath path; |
| 122 | path.moveTo(20, 20); |
| 123 | path.lineTo(70, 120); |
| 124 | path.lineTo(120, 30); |
| 125 | path.lineTo(170, 80); |
| 126 | path.lineTo(240, 50); |
| 127 | |
| 128 | size_t i; |
| 129 | canvas->save(); |
| 130 | for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) { |
| 131 | gPE[i](&paint); |
| 132 | canvas->drawPath(path, paint); |
| 133 | canvas->translate(0, 75); |
| 134 | } |
| 135 | canvas->restore(); |
| 136 | |
| 137 | path.reset(); |
| 138 | SkRect r = { 0, 0, 250, 120 }; |
Mike Reed | 4241f5e | 2019-09-14 19:13:23 +0000 | [diff] [blame] | 139 | path.addOval(r, SkPath::kCW_Direction); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 140 | r.inset(50, 50); |
Mike Reed | 4241f5e | 2019-09-14 19:13:23 +0000 | [diff] [blame] | 141 | path.addRect(r, SkPath::kCCW_Direction); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 143 | canvas->translate(320, 20); |
| 144 | for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) { |
| 145 | gPE2[i](&paint); |
| 146 | canvas->drawPath(path, paint); |
| 147 | canvas->translate(0, 160); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /////////////////////////////////////////////////////////////////////////////// |
| 152 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 153 | #include "include/effects/SkGradientShader.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 154 | |
| 155 | struct GradData { |
| 156 | int fCount; |
| 157 | const SkColor* fColors; |
| 158 | const SkScalar* fPos; |
| 159 | }; |
| 160 | |
| 161 | static const SkColor gColors[] = { |
| 162 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 163 | }; |
| 164 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 165 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 166 | static const SkScalar gPos2[] = { |
| 167 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 168 | }; |
| 169 | |
| 170 | static const GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 171 | { 2, gColors, nullptr }, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 172 | { 2, gColors, gPos0 }, |
| 173 | { 2, gColors, gPos1 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | { 5, gColors, nullptr }, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 175 | { 5, gColors, gPos2 } |
| 176 | }; |
| 177 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 178 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 179 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 182 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 183 | SkPoint center; |
| 184 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 185 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 186 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 187 | data.fPos, data.fCount, tm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 190 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 191 | SkPoint center; |
| 192 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 193 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 194 | 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] | 195 | } |
| 196 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 197 | static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 198 | SkPoint center0, center1; |
| 199 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 200 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 201 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 202 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 203 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 204 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 205 | data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 208 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 209 | static const GradMaker gGradMakers[] = { |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 210 | MakeLinear, MakeRadial, MakeSweep, Make2Conical |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | static void gradient_slide(SkCanvas* canvas) { |
| 214 | SkPoint pts[2] = { |
| 215 | { 0, 0 }, |
| 216 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 217 | }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 218 | SkTileMode tm = SkTileMode::kClamp; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 219 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 220 | SkPaint paint; |
| 221 | paint.setAntiAlias(true); |
| 222 | paint.setDither(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 223 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 224 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
| 225 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 226 | canvas->save(); |
| 227 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 228 | paint.setShader(gGradMakers[j](pts, gGradData[i], tm)); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 229 | canvas->drawRect(r, paint); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 230 | canvas->translate(0, SkIntToScalar(120)); |
| 231 | } |
| 232 | canvas->restore(); |
| 233 | canvas->translate(SkIntToScalar(120), 0); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /////////////////////////////////////////////////////////////////////////////// |
| 238 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 239 | #include "include/core/SkStream.h" |
| 240 | #include "include/utils/SkRandom.h" |
| 241 | #include "samplecode/DecodeFile.h" |
| 242 | #include "src/core/SkOSFile.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 243 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 244 | static sk_sp<SkShader> make_shader0(SkIPoint* size) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 245 | SkBitmap bm; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 246 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 247 | decode_file("/skimages/logo.gif", &bm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 248 | size->set(bm.width(), bm.height()); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 249 | return bm.makeShader(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 250 | } |
| 251 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 252 | static sk_sp<SkShader> make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 253 | SkPoint pts[] = { { 0, 0 }, |
| 254 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 255 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 256 | return SkGradientShader::MakeLinear(pts, colors, nullptr, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 257 | SK_ARRAY_COUNT(colors), SkTileMode::kMirror); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 258 | } |
| 259 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 260 | class Rec { |
| 261 | public: |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 262 | SkVertices::VertexMode fMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 263 | int fCount; |
| 264 | SkPoint* fVerts; |
| 265 | SkPoint* fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 266 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 267 | Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 268 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 269 | }; |
| 270 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 271 | static void make_tris(Rec* rec) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 272 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 273 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 274 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 275 | rec->fMode = SkVertices::kTriangles_VertexMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 276 | rec->fCount = n * 3; |
| 277 | rec->fVerts = new SkPoint[rec->fCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 278 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 279 | for (int i = 0; i < n; i++) { |
| 280 | SkPoint* v = &rec->fVerts[i*3]; |
| 281 | for (int j = 0; j < 3; j++) { |
| 282 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 287 | static void make_fan(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 288 | const SkScalar tx = SkIntToScalar(texWidth); |
| 289 | const SkScalar ty = SkIntToScalar(texHeight); |
| 290 | const int n = 24; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 291 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 292 | rec->fMode = SkVertices::kTriangleFan_VertexMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 293 | rec->fCount = n + 2; |
| 294 | rec->fVerts = new SkPoint[rec->fCount]; |
| 295 | rec->fTexs = new SkPoint[rec->fCount]; |
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 | SkPoint* v = rec->fVerts; |
| 298 | SkPoint* t = rec->fTexs; |
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 | v[0].set(0, 0); |
| 301 | t[0].set(0, 0); |
| 302 | for (int i = 0; i < n; i++) { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 303 | SkScalar r = SK_ScalarPI * 2 * i / n, |
| 304 | sin = SkScalarSin(r), |
| 305 | cos = SkScalarCos(r); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 306 | v[i+1].set(cos, sin); |
| 307 | t[i+1].set(i*tx/n, ty); |
| 308 | } |
| 309 | v[n+1] = v[1]; |
| 310 | t[n+1].set(tx, ty); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 311 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 312 | SkMatrix m; |
| 313 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 314 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 315 | m.mapPoints(v, rec->fCount); |
| 316 | } |
| 317 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 318 | static void make_strip(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 319 | const SkScalar tx = SkIntToScalar(texWidth); |
| 320 | const SkScalar ty = SkIntToScalar(texHeight); |
| 321 | const int n = 24; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 322 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 323 | rec->fMode = SkVertices::kTriangleStrip_VertexMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 324 | rec->fCount = 2 * (n + 1); |
| 325 | rec->fVerts = new SkPoint[rec->fCount]; |
| 326 | rec->fTexs = new SkPoint[rec->fCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 327 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 328 | SkPoint* v = rec->fVerts; |
| 329 | SkPoint* t = rec->fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 330 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 331 | for (int i = 0; i < n; i++) { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 332 | SkScalar r = SK_ScalarPI * 2 * i / n, |
| 333 | sin = SkScalarSin(r), |
| 334 | cos = SkScalarCos(r); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 335 | v[i*2 + 0].set(cos/2, sin/2); |
| 336 | v[i*2 + 1].set(cos, sin); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 337 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 338 | t[i*2 + 0].set(tx * i / n, ty); |
| 339 | t[i*2 + 1].set(tx * i / n, 0); |
| 340 | } |
| 341 | v[2*n + 0] = v[0]; |
| 342 | v[2*n + 1] = v[1]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 343 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 344 | t[2*n + 0].set(tx, ty); |
| 345 | t[2*n + 1].set(tx, 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 346 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 347 | SkMatrix m; |
| 348 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 349 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 350 | m.mapPoints(v, rec->fCount); |
| 351 | } |
| 352 | |
| 353 | static void mesh_slide(SkCanvas* canvas) { |
| 354 | Rec fRecs[3]; |
| 355 | SkIPoint size; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 356 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 357 | auto fShader0 = make_shader0(&size); |
| 358 | auto fShader1 = make_shader1(size); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 359 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 360 | make_strip(&fRecs[0], size.fX, size.fY); |
| 361 | make_fan(&fRecs[1], size.fX, size.fY); |
| 362 | make_tris(&fRecs[2]); |
| 363 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 364 | SkPaint paint; |
| 365 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 366 | paint.setFilterQuality(kLow_SkFilterQuality); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 367 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 368 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 369 | auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount, |
| 370 | fRecs[i].fVerts, fRecs[i].fTexs, nullptr); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 371 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 372 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 373 | paint.setShader(nullptr); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 374 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 375 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 376 | canvas->translate(SkIntToScalar(210), 0); |
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 | paint.setShader(fShader0); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 379 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
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 | canvas->translate(SkIntToScalar(210), 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 382 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 383 | paint.setShader(fShader1); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 384 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 385 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 386 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 387 | canvas->translate(0, SkIntToScalar(250)); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /////////////////////////////////////////////////////////////////////////////// |
| 392 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 393 | #include "include/core/SkTypeface.h" |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 394 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 395 | /////////////////////////////////////////////////////////////////////////////// |
| 396 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 397 | #include "include/core/SkImageEncoder.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 398 | |
| 399 | static const SlideProc gProc[] = { |
| 400 | patheffect_slide, |
| 401 | gradient_slide, |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 402 | mesh_slide, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 403 | }; |
| 404 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 405 | class SlideView : public Sample { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 406 | int fIndex; |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 407 | bool fOnce; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 408 | public: |
| 409 | SlideView() { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 410 | fOnce = false; |
| 411 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 412 | |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 413 | void init() { |
| 414 | if (fOnce) { |
| 415 | return; |
| 416 | } |
| 417 | fOnce = true; |
| 418 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 419 | fIndex = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 420 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 421 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 422 | bm.allocN32Pixels(1024, 768); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 423 | SkCanvas canvas(bm); |
| 424 | SkScalar s = SkIntToScalar(1024) / 640; |
| 425 | canvas.scale(s, s); |
| 426 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) { |
| 427 | canvas.save(); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 428 | canvas.drawColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 429 | gProc[i](&canvas); |
| 430 | canvas.restore(); |
| 431 | SkString str; |
senorblanco@chromium.org | 3a67a66 | 2012-07-09 18:22:08 +0000 | [diff] [blame] | 432 | str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 433 | ToolUtils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 434 | } |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 435 | this->setBGColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 436 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 437 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 438 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 439 | SkString name() override { return SkString("Slides"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 440 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 441 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 442 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 443 | gProc[fIndex](canvas); |
| 444 | } |
| 445 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 446 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 447 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 448 | fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 449 | return nullptr; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 453 | typedef Sample INHERITED; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | ////////////////////////////////////////////////////////////////////////////// |
| 457 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 458 | DEF_SAMPLE( return new SlideView(); ) |