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