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