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