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