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