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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkView.h" |
| 11 | #include "Sk1DPathEffect.h" |
| 12 | #include "Sk2DPathEffect.h" |
| 13 | #include "SkAvoidXfermode.h" |
| 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" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 21 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkGradientShader.h" |
| 23 | #include "SkImageDecoder.h" |
| 24 | #include "SkLayerRasterizer.h" |
| 25 | #include "SkMath.h" |
| 26 | #include "SkPath.h" |
| 27 | #include "SkRegion.h" |
| 28 | #include "SkShader.h" |
reed@android.com | aa5a7db | 2009-05-27 01:20:10 +0000 | [diff] [blame] | 29 | #include "SkComposeShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | #include "SkCornerPathEffect.h" |
| 31 | #include "SkPathMeasure.h" |
| 32 | #include "SkPicture.h" |
| 33 | #include "SkRandom.h" |
| 34 | #include "SkTransparentShader.h" |
| 35 | #include "SkTypeface.h" |
| 36 | #include "SkUnitMappers.h" |
| 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 | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 54 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | for (int i = 0; i < count; i++) |
| 56 | result[i] = rgb2gray(src[i]); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | class SkChannelMaskColorFilter : public SkColorFilter { |
| 61 | public: |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 62 | SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); |
| 64 | } |
| 65 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 66 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) { |
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 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 79 | static void r0(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3), |
| 81 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 82 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
| 83 | |
| 84 | p.setMaskFilter(NULL); |
| 85 | p.setStyle(SkPaint::kStroke_Style); |
| 86 | p.setStrokeWidth(SK_Scalar1); |
| 87 | rast->addLayer(p); |
| 88 | |
| 89 | p.setAlpha(0x11); |
| 90 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 91 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | rast->addLayer(p); |
| 93 | } |
| 94 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 95 | static void r1(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | rast->addLayer(p); |
| 97 | |
| 98 | p.setAlpha(0x40); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 99 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | p.setStyle(SkPaint::kStroke_Style); |
| 101 | p.setStrokeWidth(SK_Scalar1*2); |
| 102 | rast->addLayer(p); |
| 103 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 105 | static void r2(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 107 | p.setStrokeWidth(SK_Scalar1*4); |
| 108 | rast->addLayer(p); |
| 109 | |
| 110 | p.setStyle(SkPaint::kStroke_Style); |
| 111 | p.setStrokeWidth(SK_Scalar1*3/2); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 112 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | rast->addLayer(p); |
| 114 | } |
| 115 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 116 | static void r3(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | p.setStyle(SkPaint::kStroke_Style); |
| 118 | p.setStrokeWidth(SK_Scalar1*3); |
| 119 | rast->addLayer(p); |
| 120 | |
| 121 | p.setAlpha(0x20); |
| 122 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 123 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | rast->addLayer(p); |
| 125 | } |
| 126 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 127 | static void r4(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | p.setAlpha(0x60); |
| 129 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
| 130 | |
| 131 | p.setAlpha(0xFF); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 132 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
| 134 | |
| 135 | p.setXfermode(NULL); |
| 136 | rast->addLayer(p); |
| 137 | } |
| 138 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 139 | static void r5(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | rast->addLayer(p); |
| 141 | |
| 142 | p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 143 | p.setXfermodeMode(SkXfermode::kSrcOut_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | rast->addLayer(p); |
| 145 | } |
| 146 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 147 | static void r6(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 149 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | p.setAntiAlias(false); |
| 151 | SkLayerRasterizer* rast2 = new SkLayerRasterizer; |
| 152 | r5(rast2, p); |
| 153 | p.setRasterizer(rast2)->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 154 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | rast->addLayer(p); |
| 156 | } |
| 157 | |
| 158 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 159 | public: |
| 160 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix) |
| 161 | : Sk2DPathEffect(matrix), fRadius(radius) {} |
| 162 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 163 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | |
| 165 | protected: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 166 | virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 168 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 169 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 170 | Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | fRadius = buffer.readScalar(); |
| 172 | } |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 173 | virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { |
| 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 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 184 | static void r7(SkLayerRasterizer* rast, 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(); |
| 189 | rast->addLayer(p); |
| 190 | } |
| 191 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 192 | static void r8(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | rast->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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | rast->addLayer(p); |
| 201 | |
| 202 | p.setPathEffect(NULL); |
| 203 | p.setXfermode(NULL); |
| 204 | p.setStyle(SkPaint::kStroke_Style); |
| 205 | p.setStrokeWidth(SK_Scalar1); |
| 206 | rast->addLayer(p); |
| 207 | } |
| 208 | |
| 209 | class Line2DPathEffect : public Sk2DPathEffect { |
| 210 | public: |
| 211 | Line2DPathEffect(SkScalar width, const SkMatrix& matrix) |
| 212 | : Sk2DPathEffect(matrix), fWidth(width) {} |
| 213 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 214 | virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec) SK_OVERRIDE { |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 215 | if (this->INHERITED::filterPath(dst, src, rec)) { |
| 216 | rec->setStrokeStyle(fWidth); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | return false; |
| 220 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 221 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 222 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Line2DPathEffect) |
| 223 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | protected: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 225 | virtual void nextSpan(int u, int v, int ucount, SkPath* dst) { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 226 | if (ucount > 1) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 227 | SkPoint src[2], dstP[2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 228 | |
| 229 | src[0].set(SkIntToScalar(u) + SK_ScalarHalf, |
| 230 | SkIntToScalar(v) + SK_ScalarHalf); |
| 231 | src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf, |
| 232 | SkIntToScalar(v) + SK_ScalarHalf); |
| 233 | this->getMatrix().mapPoints(dstP, src, 2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 234 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | dst->moveTo(dstP[0]); |
| 236 | dst->lineTo(dstP[1]); |
| 237 | } |
| 238 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 239 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 240 | Line2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | fWidth = buffer.readScalar(); |
| 242 | } |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 243 | virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { |
| 244 | this->INHERITED::flatten(buffer); |
| 245 | buffer.writeScalar(fWidth); |
| 246 | } |
| 247 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | private: |
| 249 | SkScalar fWidth; |
| 250 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | typedef Sk2DPathEffect INHERITED; |
| 252 | }; |
| 253 | |
scroggo@google.com | 6eb0d62 | 2012-06-25 20:32:12 +0000 | [diff] [blame] | 254 | SK_DEFINE_FLATTENABLE_REGISTRAR(Line2DPathEffect) |
| 255 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 256 | static void r9(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 258 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | SkMatrix lattice; |
| 260 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 261 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
| 262 | p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 263 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | rast->addLayer(p); |
| 265 | |
| 266 | p.setPathEffect(NULL); |
| 267 | p.setXfermode(NULL); |
| 268 | p.setStyle(SkPaint::kStroke_Style); |
| 269 | p.setStrokeWidth(SK_Scalar1); |
| 270 | rast->addLayer(p); |
| 271 | } |
| 272 | |
| 273 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 274 | |
| 275 | static const raster_proc gRastProcs[] = { |
| 276 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 277 | }; |
| 278 | |
| 279 | static const struct { |
| 280 | SkColor fMul, fAdd; |
| 281 | } gLightingColors[] = { |
| 282 | { 0x808080, 0x800000 }, // general case |
| 283 | { 0x707070, 0x707070 }, // no-pin case |
| 284 | { 0xFFFFFF, 0x800000 }, // just-add case |
| 285 | { 0x808080, 0x000000 }, // just-mul case |
| 286 | { 0xFFFFFF, 0x000000 } // identity case |
| 287 | }; |
| 288 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 289 | static void apply_shader(SkPaint* paint, int index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | raster_proc proc = gRastProcs[index]; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 291 | if (proc) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | SkPaint p; |
| 293 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
| 294 | |
| 295 | p.setAntiAlias(true); |
| 296 | proc(rast, p); |
| 297 | paint->setRasterizer(rast)->unref(); |
| 298 | } |
| 299 | |
| 300 | #if 1 |
| 301 | SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 302 | paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | paint->setColor(SK_ColorBLUE); |
| 304 | #endif |
| 305 | } |
| 306 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 307 | class DemoView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | public: |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 309 | DemoView() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 310 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | protected: |
| 312 | // overrides from SkEventSink |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 313 | virtual bool onQuery(SkEvent* evt) { |
| 314 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 315 | SampleCode::TitleR(evt, "Demo"); |
| 316 | return true; |
| 317 | } |
| 318 | return this->INHERITED::onQuery(evt); |
| 319 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 320 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 321 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | return this->INHERITED::onClick(click); |
| 323 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 324 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 325 | void makePath(SkPath& path) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 326 | path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20), |
| 327 | SkPath::kCCW_Direction); |
| 328 | for (int index = 0; index < 10; index++) { |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 329 | SkScalar x = SkFloatToScalar((float) cos(index / 10.0f * 2 * 3.1415925358f)); |
| 330 | SkScalar y = SkFloatToScalar((float) sin(index / 10.0f * 2 * 3.1415925358f)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | x *= index & 1 ? 7 : 14; |
| 332 | y *= index & 1 ? 7 : 14; |
| 333 | x += SkIntToScalar(20); |
| 334 | y += SkIntToScalar(20); |
| 335 | if (index == 0) |
| 336 | path.moveTo(x, y); |
| 337 | else |
| 338 | path.lineTo(x, y); |
| 339 | } |
| 340 | path.close(); |
| 341 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 342 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 343 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | canvas->save(); |
| 345 | drawPicture(canvas, 0); |
| 346 | canvas->restore(); |
| 347 | |
| 348 | { |
| 349 | SkPicture picture; |
| 350 | SkCanvas* record = picture.beginRecording(320, 480); |
| 351 | drawPicture(record, 120); |
| 352 | canvas->translate(0, SkIntToScalar(120)); |
| 353 | |
| 354 | SkRect clip; |
| 355 | clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160)); |
| 356 | do { |
| 357 | canvas->save(); |
| 358 | canvas->clipRect(clip); |
| 359 | picture.draw(canvas); |
| 360 | canvas->restore(); |
| 361 | if (clip.fRight < SkIntToScalar(320)) |
| 362 | clip.offset(SkIntToScalar(160), 0); |
| 363 | else if (clip.fBottom < SkIntToScalar(480)) |
| 364 | clip.offset(-SkIntToScalar(320), SkIntToScalar(160)); |
| 365 | else |
| 366 | break; |
| 367 | } while (true); |
| 368 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 370 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 371 | void drawPicture(SkCanvas* canvas, int spriteOffset) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 372 | SkMatrix matrix; matrix.reset(); |
| 373 | SkPaint paint; |
| 374 | SkPath path; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | SkPoint start = {0, 0}; |
| 376 | SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 377 | SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) }; |
| 378 | SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) }; |
| 379 | SkScalar left = 0, top = 0, x = 0, y = 0; |
| 380 | size_t index; |
| 381 | |
| 382 | char ascii[] = "ascii..."; |
| 383 | size_t asciiLength = sizeof(ascii) - 1; |
| 384 | char utf8[] = "utf8" "\xe2\x80\xa6"; |
| 385 | short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 }; |
| 386 | short utf16simple[] = {'u', 't', 'f', '1', '6', '!' }; |
| 387 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | makePath(path); |
| 389 | SkTDArray<SkPoint>(pos); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 390 | pos.setCount(asciiLength); |
| 391 | for (index = 0; index < asciiLength; index++) |
| 392 | pos[index].set(SkIntToScalar((unsigned int)index * 10), |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 393 | SkIntToScalar((unsigned int)index * 2)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | SkTDArray<SkPoint>(pos2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 395 | pos2.setCount(asciiLength); |
| 396 | for (index = 0; index < asciiLength; index++) |
| 397 | pos2[index].set(SkIntToScalar((unsigned int)index * 10), |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 398 | SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 399 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 400 | // shaders |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 401 | SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 402 | SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 403 | SkScalar* linearPos = NULL; |
| 404 | int linearCount = 2; |
| 405 | SkShader::TileMode linearMode = SkShader::kMirror_TileMode; |
| 406 | SkUnitMapper* linearMapper = new SkDiscreteMapper(3); |
| 407 | SkAutoUnref unmapLinearMapper(linearMapper); |
| 408 | SkShader* linear = SkGradientShader::CreateLinear(linearPoints, |
| 409 | linearColors, linearPos, linearCount, linearMode, linearMapper); |
| 410 | |
| 411 | SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) }; |
| 412 | SkScalar radialRadius = SkIntToScalar(25); |
| 413 | SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED }; |
| 414 | SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)}; |
| 415 | int radialCount = 3; |
| 416 | SkShader::TileMode radialMode = SkShader::kRepeat_TileMode; |
| 417 | SkUnitMapper* radialMapper = new SkCosineMapper(); |
| 418 | SkAutoUnref unmapRadialMapper(radialMapper); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 419 | SkShader* radial = SkGradientShader::CreateRadial(radialCenter, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | radialRadius, radialColors, radialPos, radialCount, |
| 421 | radialMode, radialMapper); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 422 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 423 | SkTransparentShader* transparentShader = new SkTransparentShader(); |
| 424 | SkEmbossMaskFilter::Light light; |
| 425 | light.fDirection[0] = SK_Scalar1/2; |
| 426 | light.fDirection[1] = SK_Scalar1/2; |
| 427 | light.fDirection[2] = SK_Scalar1/3; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 428 | light.fAmbient = 0x48; |
| 429 | light.fSpecular = 0x80; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | SkScalar radius = SkIntToScalar(12)/5; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 431 | SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(light, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 432 | radius); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 433 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 434 | SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 435 | SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter( |
| 436 | 0xff89bc45, 0xff112233); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 437 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 439 | canvas->translate(SkIntToScalar(0), SkIntToScalar(5)); |
| 440 | paint.setFlags(SkPaint::kAntiAlias_Flag | SkPaint::kFilterBitmap_Flag); |
| 441 | // !!! draw through a clip |
| 442 | paint.setColor(SK_ColorLTGRAY); |
| 443 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 444 | SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)}; |
| 445 | canvas->clipRect(clip); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 446 | paint.setShader(SkShader::CreateBitmapShader(fTx, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 448 | canvas->drawPaint(paint); |
| 449 | canvas->save(); |
| 450 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 451 | // line (exercises xfermode, colorShader, colorFilter, filterShader) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 452 | paint.setColor(SK_ColorGREEN); |
| 453 | paint.setStrokeWidth(SkIntToScalar(10)); |
| 454 | paint.setStyle(SkPaint::kStroke_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 455 | paint.setXfermode(xfermode)->unref(); |
| 456 | paint.setColorFilter(lightingFilter)->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 457 | canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green |
| 458 | paint.setXfermode(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 459 | paint.setColorFilter(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 460 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 461 | // rectangle |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 462 | paint.setStyle(SkPaint::kFill_Style); |
| 463 | canvas->translate(SkIntToScalar(50), 0); |
| 464 | paint.setColor(SK_ColorYELLOW); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | paint.setShader(linear)->unref(); |
| 466 | paint.setPathEffect(pathEffectTest())->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 467 | canvas->drawRect(rect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 468 | paint.setPathEffect(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 469 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 470 | // circle w/ emboss & transparent (exercises 3dshader) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 471 | canvas->translate(SkIntToScalar(50), 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 472 | paint.setMaskFilter(embossFilter)->unref(); |
| 473 | canvas->drawOval(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 474 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 475 | paint.setShader(transparentShader)->unref(); |
| 476 | canvas->drawOval(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 477 | canvas->translate(0, SkIntToScalar(-10)); |
| 478 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | // path |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 480 | canvas->translate(SkIntToScalar(50), 0); |
| 481 | paint.setColor(SK_ColorRED); |
| 482 | paint.setStyle(SkPaint::kStroke_Style); |
| 483 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | paint.setShader(radial)->unref(); |
| 485 | paint.setMaskFilter(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 486 | canvas->drawPath(path, paint); |
| 487 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 488 | paint.setShader(NULL); |
| 489 | // bitmap, sprite |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 490 | canvas->translate(SkIntToScalar(50), 0); |
| 491 | paint.setStyle(SkPaint::kFill_Style); |
| 492 | canvas->drawBitmap(fBug, left, top, &paint); |
| 493 | canvas->translate(SkIntToScalar(30), 0); |
| 494 | canvas->drawSprite(fTb, |
| 495 | SkScalarRound(canvas->getTotalMatrix().getTranslateX()), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 496 | spriteOffset + 10, &paint); |
| 497 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 498 | canvas->translate(-SkIntToScalar(30), SkIntToScalar(30)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | paint.setShader(shaderTest())->unref(); // test compose shader |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 500 | canvas->drawRect(rect2, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 501 | paint.setShader(NULL); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 502 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 503 | canvas->restore(); |
| 504 | // text |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 505 | canvas->translate(0, SkIntToScalar(60)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 506 | canvas->save(); |
| 507 | paint.setColor(SK_ColorGRAY); |
| 508 | canvas->drawPosText(ascii, asciiLength, pos.begin(), paint); |
| 509 | canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint); |
| 510 | |
| 511 | canvas->translate(SkIntToScalar(50), 0); |
| 512 | paint.setColor(SK_ColorCYAN); |
| 513 | canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint); |
| 514 | |
| 515 | canvas->translate(SkIntToScalar(30), 0); |
| 516 | paint.setColor(SK_ColorMAGENTA); |
| 517 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 518 | matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); |
| 519 | canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint); |
| 520 | canvas->translate(0, SkIntToScalar(20)); |
| 521 | canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint); |
| 522 | canvas->restore(); |
| 523 | |
| 524 | canvas->translate(0, SkIntToScalar(60)); |
| 525 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | canvas->restore(); |
| 527 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 528 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | /* |
| 530 | ./SkColorFilter.h:25:class SkColorFilter : public SkFlattenable { -- abstract |
| 531 | static SkColorFilter* CreatXfermodeFilter() *** untested *** |
| 532 | static SkColorFilter* CreatePorterDuffFilter() *** untested *** |
| 533 | static SkColorFilter* CreateLightingFilter() -- tested |
| 534 | ./SkDrawLooper.h:9:class SkDrawLooper : public SkFlattenable { -- virtually abstract |
| 535 | ./SkBlurDrawLooper.h:9:class SkBlurDrawLooper : public SkDrawLooper { *** untested *** |
| 536 | ./SkMaskFilter.h:41:class SkMaskFilter : public SkFlattenable { -- abstract chmod +w .h |
| 537 | ./SkEmbossMaskFilter.h:27:class SkEmbossMaskFilter : public SkMaskFilter { -- tested |
| 538 | ./SkPathEffect.h:33:class SkPathEffect : public SkFlattenable { -- abstract |
| 539 | ./Sk1DPathEffect.h:27:class Sk1DPathEffect : public SkPathEffect { -- abstract |
| 540 | ./Sk1DPathEffect.h:48:class SkPath1DPathEffect : public Sk1DPathEffect { -- tested |
| 541 | ./Sk2DPathEffect.h:25:class Sk2DPathEffect : public SkPathEffect { *** untested *** |
| 542 | ./SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { *** untested *** |
| 543 | ./SkDashPathEffect.h:27:class SkDashPathEffect : public SkPathEffect { |
| 544 | ./SkDiscretePathEffect.h:27:class SkDiscretePathEffect : public SkPathEffect { |
| 545 | ./SkPaint.h:760:class SkStrokePathEffect : public SkPathEffect { |
| 546 | ./SkPathEffect.h:58:class SkPairPathEffect : public SkPathEffect { |
| 547 | ./SkPathEffect.h:78:class SkComposePathEffect : public SkPairPathEffect { |
| 548 | ./SkPathEffect.h:114:class SkSumPathEffect : public SkPairPathEffect { |
| 549 | ./SkRasterizer.h:29:class SkRasterizer : public SkFlattenable { |
| 550 | ./SkLayerRasterizer.h:27:class SkLayerRasterizer : public SkRasterizer { |
| 551 | ./SkShader.h:36:class SkShader : public SkFlattenable { |
| 552 | ./SkColorFilter.h:59:class SkFilterShader : public SkShader { |
| 553 | ./SkColorShader.h:26:class SkColorShader : public SkShader { |
| 554 | ./SkShaderExtras.h:31:class SkComposeShader : public SkShader { |
| 555 | ./SkTransparentShader.h:23:class SkTransparentShader : public SkShader { |
| 556 | ./SkUnitMapper.h:24:class SkUnitMapper : public SkFlattenable { |
| 557 | ./SkUnitMapper.h:33:class SkDiscreteMapper : public SkUnitMapper { |
| 558 | ./SkUnitMapper.h:51:class SkFlipCosineMapper : public SkUnitMapper { |
| 559 | ./SkXfermode.h:32:class SkXfermode : public SkFlattenable { |
| 560 | ./SkAvoidXfermode.h:28:class SkAvoidXfermode : public SkXfermode { *** not done *** chmod +w .h .cpp |
| 561 | ./SkXfermode.h:54:class SkProcXfermode : public SkXfermode { |
| 562 | */ |
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 | /* |
| 565 | ./SkBlurMaskFilter.h:25:class SkBlurMaskFilter { |
| 566 | chmod +w SkBlurMaskFilter.cpp |
| 567 | ./SkGradientShader.h:30:class SkGradientShader { |
| 568 | */ |
| 569 | // save layer, bounder, looper |
| 570 | // matrix |
| 571 | // clip /path/region |
| 572 | // bitmap proc shader ? |
| 573 | |
| 574 | /* untested: |
| 575 | SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { |
| 576 | */ |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 577 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 578 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 579 | fClickPt.set(x, y); |
| 580 | this->inval(NULL); |
| 581 | return this->INHERITED::onFindClickHandler(x, y); |
| 582 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 583 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 584 | SkPathEffect* pathEffectTest() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 585 | static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 }; |
| 586 | SkScalar gPhase = 0; |
| 587 | SkPath path; |
| 588 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 589 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 590 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 591 | path.close(); |
| 592 | path.offset(SkIntToScalar(-6), 0); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 593 | SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 594 | gPhase, SkPath1DPathEffect::kRotate_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 595 | SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 596 | SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2)); |
| 597 | SkPathEffect* result = new SkComposePathEffect(outer, inner); |
| 598 | outer->unref(); |
| 599 | inner->unref(); |
| 600 | return result; |
| 601 | } |
reed@google.com | 963a8fa | 2012-05-10 13:04:59 +0000 | [diff] [blame] | 602 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 603 | SkShader* shaderTest() { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 604 | SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 606 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 607 | 2, SkShader::kClamp_TileMode); |
| 608 | pts[1].set(0, SkIntToScalar(100)); |
| 609 | SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)}; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 610 | SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 611 | 2, SkShader::kClamp_TileMode); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 612 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 613 | SkShader* result = new SkComposeShader(shaderA, shaderB, mode); |
| 614 | shaderA->unref(); |
| 615 | shaderB->unref(); |
| 616 | mode->unref(); |
| 617 | return result; |
| 618 | } |
| 619 | |
| 620 | virtual void startTest() { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 621 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug); |
| 622 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb); |
| 623 | SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx); |
| 624 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 625 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 626 | void drawRaster(SkCanvas* canvas) { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 627 | for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 628 | drawOneRaster(canvas); |
| 629 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 630 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 631 | void drawOneRaster(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 632 | canvas->save(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 633 | |
| 634 | SkScalar x = SkIntToScalar(20); |
| 635 | SkScalar y = SkIntToScalar(40); |
| 636 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 637 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 638 | paint.setAntiAlias(true); |
| 639 | paint.setTextSize(SkIntToScalar(48)); |
reed@android.com | aa5a7db | 2009-05-27 01:20:10 +0000 | [diff] [blame] | 640 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 641 | SkTypeface::kBold)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 642 | |
| 643 | SkString str("GOOGLE"); |
| 644 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 645 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 646 | apply_shader(&paint, i); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 647 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 648 | // paint.setMaskFilter(NULL); |
| 649 | // paint.setColor(SK_ColorBLACK); |
| 650 | |
| 651 | #if 01 |
| 652 | int index = i % SK_ARRAY_COUNT(gLightingColors); |
| 653 | paint.setColorFilter(SkColorFilter::CreateLightingFilter( |
| 654 | gLightingColors[index].fMul, |
| 655 | gLightingColors[index].fAdd))->unref(); |
| 656 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 657 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 658 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 659 | SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y }; |
| 660 | paint.setStyle(SkPaint::kStroke_Style); |
| 661 | canvas->drawOval(oval, paint); |
| 662 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 663 | |
| 664 | y += paint.getFontSpacing(); |
| 665 | } |
| 666 | |
| 667 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 668 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 669 | if (1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 670 | SkAvoidXfermode mode(SK_ColorWHITE, 0xFF, |
| 671 | SkAvoidXfermode::kTargetColor_Mode); |
| 672 | SkPaint paint; |
| 673 | x += SkIntToScalar(20); |
| 674 | SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) }; |
| 675 | paint.setXfermode(&mode); |
| 676 | paint.setColor(SK_ColorGREEN); |
| 677 | paint.setAntiAlias(true); |
| 678 | canvas->drawOval(r, paint); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | private: |
| 683 | SkPoint fClickPt; |
| 684 | SkBitmap fBug, fTb, fTx; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 685 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 686 | }; |
| 687 | |
| 688 | ////////////////////////////////////////////////////////////////////////////// |
| 689 | |
| 690 | static SkView* MyFactory() { return new DemoView; } |
| 691 | static SkViewRegister reg(MyFactory); |
| 692 | |