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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/effects/SkGradientShader.h" |
| 11 | #include "samplecode/Sample.h" |
| 12 | #include "src/core/SkBlurMask.h" |
| 13 | #include "src/core/SkReadBuffer.h" |
| 14 | #include "src/core/SkWriteBuffer.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tools/ToolUtils.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/effects/Sk1DPathEffect.h" |
| 25 | #include "include/effects/Sk2DPathEffect.h" |
| 26 | #include "include/effects/SkCornerPathEffect.h" |
| 27 | #include "include/effects/SkDashPathEffect.h" |
| 28 | #include "include/effects/SkDiscretePathEffect.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 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) { |
Mike Reed | a07741a | 2017-02-25 22:34:32 -0500 | [diff] [blame] | 35 | compose = SkPathEffect::MakeCompose(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 }; |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 138 | path.addOval(r, SkPathDirection::kCW); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 139 | r.inset(50, 50); |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 140 | path.addRect(r, SkPathDirection::kCCW); |
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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 152 | #include "include/effects/SkGradientShader.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 177 | 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] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 181 | 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] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 189 | 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] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 196 | 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] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 207 | 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] | 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 | }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 217 | SkTileMode tm = SkTileMode::kClamp; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 238 | #include "include/core/SkStream.h" |
| 239 | #include "include/utils/SkRandom.h" |
| 240 | #include "samplecode/DecodeFile.h" |
| 241 | #include "src/core/SkOSFile.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 242 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 243 | static sk_sp<SkShader> make_shader0(SkIPoint* size) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 244 | SkBitmap bm; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 245 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 246 | decode_file("/skimages/logo.gif", &bm); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 247 | size->set(bm.width(), bm.height()); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 248 | return bm.makeShader(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 249 | } |
| 250 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 251 | static sk_sp<SkShader> make_shader1(const SkIPoint& size) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 252 | SkPoint pts[] = { { 0, 0 }, |
| 253 | { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } }; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 254 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 255 | return SkGradientShader::MakeLinear(pts, colors, nullptr, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 256 | SK_ARRAY_COUNT(colors), SkTileMode::kMirror); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 257 | } |
| 258 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 259 | class Rec { |
| 260 | public: |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 261 | SkVertices::VertexMode fMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 262 | int fCount; |
| 263 | SkPoint* fVerts; |
| 264 | SkPoint* fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 265 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 266 | Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {} |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 267 | ~Rec() { delete[] fVerts; delete[] fTexs; } |
| 268 | }; |
| 269 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 270 | static void make_tris(Rec* rec) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 271 | int n = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 272 | SkRandom rand; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 273 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 274 | rec->fMode = SkVertices::kTriangles_VertexMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 275 | rec->fCount = n * 3; |
| 276 | rec->fVerts = new SkPoint[rec->fCount]; |
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 | for (int i = 0; i < n; i++) { |
| 279 | SkPoint* v = &rec->fVerts[i*3]; |
| 280 | for (int j = 0; j < 3; j++) { |
| 281 | v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 286 | static void make_fan(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 287 | const SkScalar tx = SkIntToScalar(texWidth); |
| 288 | const SkScalar ty = SkIntToScalar(texHeight); |
| 289 | const int n = 24; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 290 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 291 | rec->fMode = SkVertices::kTriangleFan_VertexMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 292 | rec->fCount = n + 2; |
| 293 | rec->fVerts = new SkPoint[rec->fCount]; |
| 294 | rec->fTexs = new SkPoint[rec->fCount]; |
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 | SkPoint* v = rec->fVerts; |
| 297 | SkPoint* t = rec->fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 298 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 299 | v[0].set(0, 0); |
| 300 | t[0].set(0, 0); |
| 301 | for (int i = 0; i < n; i++) { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 302 | SkScalar r = SK_ScalarPI * 2 * i / n, |
| 303 | sin = SkScalarSin(r), |
| 304 | cos = SkScalarCos(r); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 305 | v[i+1].set(cos, sin); |
| 306 | t[i+1].set(i*tx/n, ty); |
| 307 | } |
| 308 | v[n+1] = v[1]; |
| 309 | t[n+1].set(tx, ty); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 310 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 311 | SkMatrix m; |
| 312 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 313 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 314 | m.mapPoints(v, rec->fCount); |
| 315 | } |
| 316 | |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 317 | static void make_strip(Rec* rec, int texWidth, int texHeight) { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 318 | const SkScalar tx = SkIntToScalar(texWidth); |
| 319 | const SkScalar ty = SkIntToScalar(texHeight); |
| 320 | const int n = 24; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 321 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 322 | rec->fMode = SkVertices::kTriangleStrip_VertexMode; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 323 | rec->fCount = 2 * (n + 1); |
| 324 | rec->fVerts = new SkPoint[rec->fCount]; |
| 325 | rec->fTexs = new SkPoint[rec->fCount]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 326 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 327 | SkPoint* v = rec->fVerts; |
| 328 | SkPoint* t = rec->fTexs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 329 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 330 | for (int i = 0; i < n; i++) { |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 331 | SkScalar r = SK_ScalarPI * 2 * i / n, |
| 332 | sin = SkScalarSin(r), |
| 333 | cos = SkScalarCos(r); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 334 | v[i*2 + 0].set(cos/2, sin/2); |
| 335 | v[i*2 + 1].set(cos, sin); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 336 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 337 | t[i*2 + 0].set(tx * i / n, ty); |
| 338 | t[i*2 + 1].set(tx * i / n, 0); |
| 339 | } |
| 340 | v[2*n + 0] = v[0]; |
| 341 | v[2*n + 1] = v[1]; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 342 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 343 | t[2*n + 0].set(tx, ty); |
| 344 | t[2*n + 1].set(tx, 0); |
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 | SkMatrix m; |
| 347 | m.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 348 | m.postTranslate(SkIntToScalar(110), SkIntToScalar(110)); |
| 349 | m.mapPoints(v, rec->fCount); |
| 350 | } |
| 351 | |
| 352 | static void mesh_slide(SkCanvas* canvas) { |
| 353 | Rec fRecs[3]; |
| 354 | SkIPoint size; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 355 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 356 | auto fShader0 = make_shader0(&size); |
| 357 | auto fShader1 = make_shader1(size); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 358 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 359 | make_strip(&fRecs[0], size.fX, size.fY); |
| 360 | make_fan(&fRecs[1], size.fX, size.fY); |
| 361 | make_tris(&fRecs[2]); |
| 362 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 363 | SkPaint paint; |
| 364 | paint.setDither(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 365 | paint.setFilterQuality(kLow_SkFilterQuality); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 366 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 367 | for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 368 | auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount, |
| 369 | fRecs[i].fVerts, fRecs[i].fTexs, nullptr); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 370 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 371 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 372 | paint.setShader(nullptr); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 373 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 374 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 375 | canvas->translate(SkIntToScalar(210), 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 376 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 377 | paint.setShader(fShader0); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 378 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 379 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 380 | canvas->translate(SkIntToScalar(210), 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 381 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 382 | paint.setShader(fShader1); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 383 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 384 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 385 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 386 | canvas->translate(0, SkIntToScalar(250)); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /////////////////////////////////////////////////////////////////////////////// |
| 391 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 392 | #include "include/core/SkTypeface.h" |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 393 | |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 394 | /////////////////////////////////////////////////////////////////////////////// |
| 395 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 396 | #include "include/core/SkImageEncoder.h" |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 397 | |
| 398 | static const SlideProc gProc[] = { |
| 399 | patheffect_slide, |
| 400 | gradient_slide, |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 401 | mesh_slide, |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 402 | }; |
| 403 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 404 | class SlideView : public Sample { |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 405 | int fIndex; |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 406 | bool fOnce; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 407 | public: |
| 408 | SlideView() { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 409 | fOnce = false; |
| 410 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 411 | |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 412 | void init() { |
| 413 | if (fOnce) { |
| 414 | return; |
| 415 | } |
| 416 | fOnce = true; |
| 417 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 418 | fIndex = 0; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 419 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 420 | SkBitmap bm; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 421 | bm.allocN32Pixels(1024, 768); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 422 | SkCanvas canvas(bm); |
| 423 | SkScalar s = SkIntToScalar(1024) / 640; |
| 424 | canvas.scale(s, s); |
| 425 | for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) { |
| 426 | canvas.save(); |
reed@android.com | 8af9602 | 2009-10-12 17:02:22 +0000 | [diff] [blame] | 427 | canvas.drawColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 428 | gProc[i](&canvas); |
| 429 | canvas.restore(); |
| 430 | SkString str; |
John Stiles | 7094159 | 2020-05-04 23:17:21 -0400 | [diff] [blame] | 431 | str.printf("/skimages/slide_%zu.png", i); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 432 | ToolUtils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 433 | } |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 434 | this->setBGColor(BG_COLOR); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 435 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 436 | |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 437 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 438 | SkString name() override { return SkString("Slides"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 439 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 440 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 441 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 442 | gProc[fIndex](canvas); |
| 443 | } |
| 444 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 445 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 446 | this->init(); |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 447 | fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 448 | return nullptr; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 452 | using INHERITED = Sample; |
reed@android.com | 63e34c6 | 2009-10-06 21:19:18 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | ////////////////////////////////////////////////////////////////////////////// |
| 456 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 457 | DEF_SAMPLE( return new SlideView(); ) |