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 | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkView.h" |
| 12 | #include "Sk1DPathEffect.h" |
| 13 | #include "Sk2DPathEffect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkBlurMaskFilter.h" |
| 15 | #include "SkColorFilter.h" |
| 16 | #include "SkColorPriv.h" |
| 17 | #include "SkCornerPathEffect.h" |
| 18 | #include "SkDashPathEffect.h" |
| 19 | #include "SkDiscretePathEffect.h" |
| 20 | #include "SkEmbossMaskFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 21 | #include "SkReadBuffer.h" |
| 22 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkGradientShader.h" |
| 24 | #include "SkImageDecoder.h" |
| 25 | #include "SkLayerRasterizer.h" |
| 26 | #include "SkMath.h" |
| 27 | #include "SkPath.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 28 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | #include "SkRegion.h" |
| 30 | #include "SkShader.h" |
reed@android.com | aa5a7db | 2009-05-27 01:20:10 +0000 | [diff] [blame] | 31 | #include "SkComposeShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | #include "SkCornerPathEffect.h" |
| 33 | #include "SkPathMeasure.h" |
| 34 | #include "SkPicture.h" |
| 35 | #include "SkRandom.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | #include "SkTypeface.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | #include "SkUtils.h" |
| 38 | #include "SkXfermode.h" |
| 39 | |
| 40 | #include <math.h> |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 42 | static inline SkPMColor rgb2gray(SkPMColor c) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | unsigned r = SkGetPackedR32(c); |
| 44 | unsigned g = SkGetPackedG32(c); |
| 45 | unsigned b = SkGetPackedB32(c); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 47 | unsigned x = (r * 5 + g * 7 + b * 4) >> 4; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT)); |
| 50 | } |
| 51 | |
| 52 | class SkGrayScaleColorFilter : public SkColorFilter { |
| 53 | public: |
mike@reedtribe.org | 61490fa | 2012-12-24 14:38:46 +0000 | [diff] [blame] | 54 | virtual void filterSpan(const SkPMColor src[], int count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | SkPMColor result[]) const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | for (int i = 0; i < count; i++) |
| 57 | result[i] = rgb2gray(src[i]); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class SkChannelMaskColorFilter : public SkColorFilter { |
| 62 | public: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 63 | SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); |
| 65 | } |
| 66 | |
mike@reedtribe.org | 61490fa | 2012-12-24 14:38:46 +0000 | [diff] [blame] | 67 | virtual void filterSpan(const SkPMColor src[], int count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | SkPMColor result[]) const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | SkPMColor mask = fMask; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 70 | for (int i = 0; i < count; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | result[i] = src[i] & mask; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 72 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | private: |
| 76 | SkPMColor fMask; |
| 77 | }; |
| 78 | |
| 79 | /////////////////////////////////////////////////////////// |
| 80 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 81 | static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 82 | p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 83 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 84 | SkBlurMaskFilter::kNone_BlurFlag))->unref(); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 85 | rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 87 | p.setMaskFilter(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | p.setStyle(SkPaint::kStroke_Style); |
| 89 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 90 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | |
| 92 | p.setAlpha(0x11); |
| 93 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 94 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 95 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 98 | static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 99 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | |
| 101 | p.setAlpha(0x40); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 102 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | p.setStyle(SkPaint::kStroke_Style); |
| 104 | p.setStrokeWidth(SK_Scalar1*2); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 105 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 108 | static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 110 | p.setStrokeWidth(SK_Scalar1*4); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 111 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | |
| 113 | p.setStyle(SkPaint::kStroke_Style); |
| 114 | p.setStrokeWidth(SK_Scalar1*3/2); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 115 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 116 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 119 | static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | p.setStyle(SkPaint::kStroke_Style); |
| 121 | p.setStrokeWidth(SK_Scalar1*3); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 122 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | |
| 124 | p.setAlpha(0x20); |
| 125 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 126 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 127 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 130 | static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | p.setAlpha(0x60); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 132 | rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | |
| 134 | p.setAlpha(0xFF); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 135 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 136 | rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | p.setXfermode(nullptr); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 139 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 142 | static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 143 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 145 | p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 146 | p.setXfermodeMode(SkXfermode::kSrcOut_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 147 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 150 | static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 151 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | p.setAntiAlias(false); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 154 | SkLayerRasterizer::Builder rastBuilder2; |
| 155 | r5(&rastBuilder2, p); |
| 156 | p.setRasterizer(rastBuilder2.detachRasterizer())->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 157 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 158 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 162 | public: |
| 163 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix) |
| 164 | : Sk2DPathEffect(matrix), fRadius(radius) {} |
| 165 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 166 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | |
| 168 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 169 | void next(const SkPoint& loc, int u, int v, SkPath* dst) const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 171 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 172 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 173 | void flatten(SkWriteBuffer& buffer) const override { |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 174 | this->INHERITED::flatten(buffer); |
| 175 | buffer.writeScalar(fRadius); |
| 176 | } |
| 177 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | private: |
| 179 | SkScalar fRadius; |
| 180 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | typedef Sk2DPathEffect INHERITED; |
| 182 | }; |
| 183 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 184 | static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | SkMatrix lattice; |
| 186 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 187 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 188 | p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref(); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 189 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 192 | static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 193 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 194 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | SkMatrix lattice; |
| 196 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 197 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 198 | p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 199 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 200 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 202 | p.setPathEffect(nullptr); |
| 203 | p.setXfermode(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | p.setStyle(SkPaint::kStroke_Style); |
| 205 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 206 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } |
| 208 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 209 | static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 210 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 211 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | SkMatrix lattice; |
| 213 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 214 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 215 | p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 216 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 217 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 219 | p.setPathEffect(nullptr); |
| 220 | p.setXfermode(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | p.setStyle(SkPaint::kStroke_Style); |
| 222 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 223 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 226 | typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | |
| 228 | static const raster_proc gRastProcs[] = { |
| 229 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 230 | }; |
| 231 | |
| 232 | static const struct { |
| 233 | SkColor fMul, fAdd; |
| 234 | } gLightingColors[] = { |
| 235 | { 0x808080, 0x800000 }, // general case |
| 236 | { 0x707070, 0x707070 }, // no-pin case |
| 237 | { 0xFFFFFF, 0x800000 }, // just-add case |
| 238 | { 0x808080, 0x000000 }, // just-mul case |
| 239 | { 0xFFFFFF, 0x000000 } // identity case |
| 240 | }; |
| 241 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 242 | static void apply_shader(SkPaint* paint, int index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | raster_proc proc = gRastProcs[index]; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 244 | if (proc) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | SkPaint p; |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 246 | SkLayerRasterizer::Builder rastBuilder; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | |
| 248 | p.setAntiAlias(true); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 249 | proc(&rastBuilder, p); |
| 250 | paint->setRasterizer(rastBuilder.detachRasterizer())->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | #if 1 |
| 254 | SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 }; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 255 | paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss( |
| 256 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), dir, |
| 257 | SK_Scalar1/4, SkIntToScalar(4)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 258 | paint->setColor(SK_ColorBLUE); |
| 259 | #endif |
| 260 | } |
| 261 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 262 | class DemoView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | public: |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 264 | DemoView() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 265 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | protected: |
| 267 | // overrides from SkEventSink |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 268 | virtual bool onQuery(SkEvent* evt) { |
| 269 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 270 | SampleCode::TitleR(evt, "Demo"); |
| 271 | return true; |
| 272 | } |
| 273 | return this->INHERITED::onQuery(evt); |
| 274 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 275 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 276 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | return this->INHERITED::onClick(click); |
| 278 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 279 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 280 | void makePath(SkPath& path) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20), |
| 282 | SkPath::kCCW_Direction); |
| 283 | for (int index = 0; index < 10; index++) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 284 | SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f); |
| 285 | SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | x *= index & 1 ? 7 : 14; |
| 287 | y *= index & 1 ? 7 : 14; |
| 288 | x += SkIntToScalar(20); |
| 289 | y += SkIntToScalar(20); |
| 290 | if (index == 0) |
| 291 | path.moveTo(x, y); |
| 292 | else |
| 293 | path.lineTo(x, y); |
| 294 | } |
| 295 | path.close(); |
| 296 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 297 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 298 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 299 | canvas->save(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 300 | this->drawPicture(canvas, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 301 | canvas->restore(); |
| 302 | |
| 303 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 304 | SkPictureRecorder recorder; |
| 305 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 306 | SkCanvas* record = recorder.beginRecording(320, 480, nullptr, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 307 | this->drawPicture(record, 120); |
| 308 | } |
| 309 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 310 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | canvas->translate(0, SkIntToScalar(120)); |
| 312 | |
| 313 | SkRect clip; |
| 314 | clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160)); |
| 315 | do { |
| 316 | canvas->save(); |
| 317 | canvas->clipRect(clip); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 318 | picture->playback(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | canvas->restore(); |
| 320 | if (clip.fRight < SkIntToScalar(320)) |
| 321 | clip.offset(SkIntToScalar(160), 0); |
| 322 | else if (clip.fBottom < SkIntToScalar(480)) |
| 323 | clip.offset(-SkIntToScalar(320), SkIntToScalar(160)); |
| 324 | else |
| 325 | break; |
| 326 | } while (true); |
| 327 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 329 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 330 | void drawPicture(SkCanvas* canvas, int spriteOffset) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 331 | SkMatrix matrix; matrix.reset(); |
| 332 | SkPaint paint; |
| 333 | SkPath path; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | SkPoint start = {0, 0}; |
| 335 | SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 336 | SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) }; |
| 337 | SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) }; |
| 338 | SkScalar left = 0, top = 0, x = 0, y = 0; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 339 | int index; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 340 | |
| 341 | char ascii[] = "ascii..."; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 342 | int asciiLength = sizeof(ascii) - 1; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 343 | char utf8[] = "utf8" "\xe2\x80\xa6"; |
| 344 | short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 }; |
| 345 | short utf16simple[] = {'u', 't', 'f', '1', '6', '!' }; |
| 346 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | makePath(path); |
| 348 | SkTDArray<SkPoint>(pos); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 349 | pos.setCount(asciiLength); |
| 350 | for (index = 0; index < asciiLength; index++) |
| 351 | pos[index].set(SkIntToScalar((unsigned int)index * 10), |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 352 | SkIntToScalar((unsigned int)index * 2)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | SkTDArray<SkPoint>(pos2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 354 | pos2.setCount(asciiLength); |
| 355 | for (index = 0; index < asciiLength; index++) |
| 356 | pos2[index].set(SkIntToScalar((unsigned int)index * 10), |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 357 | SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 358 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 359 | // shaders |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 360 | SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 361 | SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 362 | SkScalar* linearPos = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | int linearCount = 2; |
| 364 | SkShader::TileMode linearMode = SkShader::kMirror_TileMode; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | SkShader* linear = SkGradientShader::CreateLinear(linearPoints, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 366 | linearColors, linearPos, linearCount, linearMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | |
| 368 | SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) }; |
| 369 | SkScalar radialRadius = SkIntToScalar(25); |
| 370 | SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED }; |
| 371 | SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)}; |
| 372 | int radialCount = 3; |
| 373 | SkShader::TileMode radialMode = SkShader::kRepeat_TileMode; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 374 | SkShader* radial = SkGradientShader::CreateRadial(radialCenter, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | radialRadius, radialColors, radialPos, radialCount, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 376 | radialMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 377 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 378 | SkEmbossMaskFilter::Light light; |
| 379 | light.fDirection[0] = SK_Scalar1/2; |
| 380 | light.fDirection[1] = SK_Scalar1/2; |
| 381 | light.fDirection[2] = SK_Scalar1/3; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 382 | light.fAmbient = 0x48; |
| 383 | light.fSpecular = 0x80; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 384 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5); |
reed | 2d6ba66 | 2015-12-20 20:08:42 -0800 | [diff] [blame^] | 385 | SkEmbossMaskFilter* embossFilter = SkEmbossMaskFilter::Create(sigma, light); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 386 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 387 | SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter( |
| 389 | 0xff89bc45, 0xff112233); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 392 | canvas->translate(SkIntToScalar(0), SkIntToScalar(5)); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 393 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 394 | paint.setFilterQuality(kLow_SkFilterQuality); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 395 | // !!! draw through a clip |
| 396 | paint.setColor(SK_ColorLTGRAY); |
| 397 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)}; |
| 399 | canvas->clipRect(clip); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 400 | paint.setShader(SkShader::CreateBitmapShader(fTx, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 402 | canvas->drawPaint(paint); |
| 403 | canvas->save(); |
| 404 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | // line (exercises xfermode, colorShader, colorFilter, filterShader) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 406 | paint.setColor(SK_ColorGREEN); |
| 407 | paint.setStrokeWidth(SkIntToScalar(10)); |
| 408 | paint.setStyle(SkPaint::kStroke_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | paint.setXfermode(xfermode)->unref(); |
| 410 | paint.setColorFilter(lightingFilter)->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 411 | canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 412 | paint.setXfermode(nullptr); |
| 413 | paint.setColorFilter(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 414 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | // rectangle |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 416 | paint.setStyle(SkPaint::kFill_Style); |
| 417 | canvas->translate(SkIntToScalar(50), 0); |
| 418 | paint.setColor(SK_ColorYELLOW); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 419 | paint.setShader(linear)->unref(); |
| 420 | paint.setPathEffect(pathEffectTest())->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 421 | canvas->drawRect(rect, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 422 | paint.setPathEffect(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 423 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 424 | // circle w/ emboss & transparent (exercises 3dshader) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 425 | canvas->translate(SkIntToScalar(50), 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | paint.setMaskFilter(embossFilter)->unref(); |
| 427 | canvas->drawOval(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 428 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
reed | 56263c7 | 2015-06-05 11:31:26 -0700 | [diff] [blame] | 429 | // paint.setShader(transparentShader)->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | canvas->drawOval(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 431 | canvas->translate(0, SkIntToScalar(-10)); |
| 432 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | // path |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 434 | canvas->translate(SkIntToScalar(50), 0); |
| 435 | paint.setColor(SK_ColorRED); |
| 436 | paint.setStyle(SkPaint::kStroke_Style); |
| 437 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | paint.setShader(radial)->unref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 439 | paint.setMaskFilter(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 440 | canvas->drawPath(path, paint); |
| 441 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 442 | paint.setShader(nullptr); |
reed | 6f45c17 | 2015-11-04 08:34:07 -0800 | [diff] [blame] | 443 | // bitmap |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 444 | canvas->translate(SkIntToScalar(50), 0); |
| 445 | paint.setStyle(SkPaint::kFill_Style); |
| 446 | canvas->drawBitmap(fBug, left, top, &paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 448 | canvas->translate(-SkIntToScalar(30), SkIntToScalar(30)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 449 | paint.setShader(shaderTest())->unref(); // test compose shader |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 450 | canvas->drawRect(rect2, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 451 | paint.setShader(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 452 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 453 | canvas->restore(); |
| 454 | // text |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 455 | canvas->translate(0, SkIntToScalar(60)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 456 | canvas->save(); |
| 457 | paint.setColor(SK_ColorGRAY); |
| 458 | canvas->drawPosText(ascii, asciiLength, pos.begin(), paint); |
| 459 | canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint); |
| 460 | |
| 461 | canvas->translate(SkIntToScalar(50), 0); |
| 462 | paint.setColor(SK_ColorCYAN); |
| 463 | canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint); |
| 464 | |
| 465 | canvas->translate(SkIntToScalar(30), 0); |
| 466 | paint.setColor(SK_ColorMAGENTA); |
| 467 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 468 | matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); |
| 469 | canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint); |
| 470 | canvas->translate(0, SkIntToScalar(20)); |
| 471 | canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint); |
| 472 | canvas->restore(); |
| 473 | |
| 474 | canvas->translate(0, SkIntToScalar(60)); |
| 475 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 476 | canvas->restore(); |
| 477 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 478 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 479 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 480 | fClickPt.set(x, y); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 481 | this->inval(nullptr); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 482 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 483 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 484 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 485 | SkPathEffect* pathEffectTest() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 }; |
| 487 | SkScalar gPhase = 0; |
| 488 | SkPath path; |
| 489 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 490 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 491 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 492 | path.close(); |
| 493 | path.offset(SkIntToScalar(-6), 0); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 494 | SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | gPhase, SkPath1DPathEffect::kRotate_Style); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 496 | SkPathEffect* inner = SkDiscretePathEffect::Create(SkIntToScalar(2), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 497 | SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2)); |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 498 | SkPathEffect* result = SkComposePathEffect::Create(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | outer->unref(); |
| 500 | inner->unref(); |
| 501 | return result; |
| 502 | } |
reed@google.com | 963a8fa | 2012-05-10 13:04:59 +0000 | [diff] [blame] | 503 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 504 | SkShader* shaderTest() { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 505 | SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 506 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 507 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, nullptr, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | 2, SkShader::kClamp_TileMode); |
| 509 | pts[1].set(0, SkIntToScalar(100)); |
| 510 | SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)}; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 511 | SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, nullptr, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 512 | 2, SkShader::kClamp_TileMode); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 513 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 514 | SkShader* result = new SkComposeShader(shaderA, shaderB, mode); |
| 515 | shaderA->unref(); |
| 516 | shaderB->unref(); |
| 517 | mode->unref(); |
| 518 | return result; |
| 519 | } |
| 520 | |
| 521 | virtual void startTest() { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 522 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug); |
| 523 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb); |
| 524 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx); |
| 525 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 527 | void drawRaster(SkCanvas* canvas) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 528 | for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | drawOneRaster(canvas); |
| 530 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 531 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 532 | void drawOneRaster(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | canvas->save(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 534 | |
| 535 | SkScalar x = SkIntToScalar(20); |
| 536 | SkScalar y = SkIntToScalar(40); |
| 537 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 538 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 539 | paint.setAntiAlias(true); |
| 540 | paint.setTextSize(SkIntToScalar(48)); |
reed@android.com | aa5a7db | 2009-05-27 01:20:10 +0000 | [diff] [blame] | 541 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 542 | SkTypeface::kBold)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 543 | |
| 544 | SkString str("GOOGLE"); |
| 545 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 546 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 547 | apply_shader(&paint, (int)i); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 548 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 549 | // paint.setMaskFilter(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 550 | // paint.setColor(SK_ColorBLACK); |
| 551 | |
| 552 | #if 01 |
| 553 | int index = i % SK_ARRAY_COUNT(gLightingColors); |
| 554 | paint.setColorFilter(SkColorFilter::CreateLightingFilter( |
| 555 | gLightingColors[index].fMul, |
| 556 | gLightingColors[index].fAdd))->unref(); |
| 557 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 558 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 559 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 560 | SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y }; |
| 561 | paint.setStyle(SkPaint::kStroke_Style); |
| 562 | canvas->drawOval(oval, paint); |
| 563 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 564 | |
| 565 | y += paint.getFontSpacing(); |
| 566 | } |
| 567 | |
| 568 | canvas->restore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | private: |
| 572 | SkPoint fClickPt; |
| 573 | SkBitmap fBug, fTb, fTx; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 574 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | ////////////////////////////////////////////////////////////////////////////// |
| 578 | |
| 579 | static SkView* MyFactory() { return new DemoView; } |
| 580 | static SkViewRegister reg(MyFactory); |