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 | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkPath.h" |
| 15 | #include "SkRegion.h" |
| 16 | #include "SkShader.h" |
| 17 | #include "SkUtils.h" |
| 18 | #include "SkColorPriv.h" |
| 19 | #include "SkColorFilter.h" |
| 20 | #include "SkTypeface.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 21 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 22 | static inline SkPMColor rgb2gray(SkPMColor c) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 23 | unsigned r = SkGetPackedR32(c); |
| 24 | unsigned g = SkGetPackedG32(c); |
| 25 | unsigned b = SkGetPackedB32(c); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 26 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 27 | unsigned x = (r * 5 + g * 7 + b * 4) >> 4; |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 28 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 29 | return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT)); |
| 30 | } |
| 31 | |
| 32 | class SkGrayScaleColorFilter : public SkColorFilter { |
| 33 | public: |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 34 | virtual void filterSpan(const SkPMColor src[], int count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkPMColor result[]) const override { |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 36 | for (int i = 0; i < count; i++) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 37 | result[i] = rgb2gray(src[i]); |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 38 | } |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 39 | } |
| 40 | }; |
| 41 | |
| 42 | class SkChannelMaskColorFilter : public SkColorFilter { |
| 43 | public: |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 44 | SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 45 | fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); |
| 46 | } |
| 47 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 48 | virtual void filterSpan(const SkPMColor src[], int count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | SkPMColor result[]) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 50 | SkPMColor mask = fMask; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 51 | for (int i = 0; i < count; i++) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 52 | result[i] = src[i] & mask; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 53 | } |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 54 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 55 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 56 | private: |
| 57 | SkPMColor fMask; |
| 58 | }; |
| 59 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 61 | |
| 62 | #include "SkGradientShader.h" |
| 63 | #include "SkLayerRasterizer.h" |
| 64 | #include "SkBlurMaskFilter.h" |
| 65 | |
| 66 | #include "Sk2DPathEffect.h" |
| 67 | |
| 68 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 69 | public: |
| 70 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix, |
| 71 | SkTDArray<SkPoint>* pts) |
| 72 | : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {} |
| 73 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 74 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 75 | |
| 76 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | void begin(const SkIRect& uvBounds, SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 78 | if (fPts) { |
| 79 | fPts->reset(); |
| 80 | } |
| 81 | this->INHERITED::begin(uvBounds, dst); |
| 82 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 83 | |
| 84 | virtual void next(const SkPoint& loc, int u, int v, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 86 | if (fPts) { |
| 87 | *fPts->append() = loc; |
| 88 | } |
| 89 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 90 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 91 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 93 | buffer.writeMatrix(this->getMatrix()); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 94 | buffer.writeScalar(fRadius); |
| 95 | } |
| 96 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 97 | private: |
| 98 | SkScalar fRadius; |
| 99 | SkTDArray<SkPoint>* fPts; |
| 100 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 101 | typedef Sk2DPathEffect INHERITED; |
| 102 | }; |
| 103 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 104 | SkFlattenable* Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) { |
| 105 | SkMatrix matrix; |
| 106 | buffer.readMatrix(&matrix); |
| 107 | return SkNEW_ARGS(Dot2DPathEffect, (buffer.readScalar(), matrix, NULL)); |
| 108 | } |
| 109 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 110 | class InverseFillPE : public SkPathEffect { |
| 111 | public: |
| 112 | InverseFillPE() {} |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 113 | virtual bool filterPath(SkPath* dst, const SkPath& src, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 114 | SkStrokeRec*, const SkRect*) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 115 | *dst = src; |
| 116 | dst->setFillType(SkPath::kInverseWinding_FillType); |
| 117 | return true; |
| 118 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 119 | |
| 120 | #ifndef SK_IGNORE_TO_STRING |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 121 | void toString(SkString* str) const override { |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 122 | str->appendf("InverseFillPE: ()"); |
| 123 | } |
| 124 | #endif |
| 125 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 126 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE) |
| 127 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 128 | private: |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 129 | typedef SkPathEffect INHERITED; |
| 130 | }; |
| 131 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 132 | SkFlattenable* InverseFillPE::CreateProc(SkReadBuffer& buffer) { |
| 133 | return SkNEW(InverseFillPE); |
| 134 | } |
| 135 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 136 | static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) { |
| 137 | SkMatrix lattice; |
| 138 | SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); |
| 139 | lattice.setScale(rad*2, rad*2, 0, 0); |
| 140 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 141 | return new Dot2DPathEffect(rad, lattice, pts); |
| 142 | } |
| 143 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 144 | static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) { |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 145 | p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref(); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 146 | rastBuilder->addLayer(p); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 147 | #if 0 |
| 148 | p.setPathEffect(new InverseFillPE())->unref(); |
| 149 | p.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 150 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 151 | p.setAlpha((1 - interp) * 255); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 152 | rastBuilder->addLayer(p); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 153 | #endif |
| 154 | } |
| 155 | |
| 156 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 157 | |
| 158 | #include "SkXfermode.h" |
| 159 | |
| 160 | static void apply_shader(SkPaint* paint, float scale) |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 161 | { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 162 | SkPaint p; |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 163 | SkLayerRasterizer::Builder rastBuilder; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 164 | |
| 165 | p.setAntiAlias(true); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 166 | r7(&rastBuilder, p, scale); |
| 167 | paint->setRasterizer(rastBuilder.detachRasterizer())->unref(); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 168 | |
| 169 | paint->setColor(SK_ColorBLUE); |
| 170 | } |
| 171 | |
| 172 | class ClockFaceView : public SkView { |
| 173 | SkTypeface* fFace; |
| 174 | SkScalar fInterp; |
| 175 | SkScalar fDx; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 176 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 177 | public: |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 178 | ClockFaceView() { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 179 | fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb"); |
| 180 | fInterp = 0; |
| 181 | fDx = SK_Scalar1/64; |
| 182 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 183 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 184 | virtual ~ClockFaceView() { |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 185 | SkSafeUnref(fFace); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | protected: |
| 189 | // overrides from SkEventSink |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 190 | virtual bool onQuery(SkEvent* evt) { |
| 191 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 192 | SampleCode::TitleR(evt, "Text Effects"); |
| 193 | return true; |
| 194 | } |
| 195 | return this->INHERITED::onQuery(evt); |
| 196 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 197 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 198 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 199 | // canvas->drawColor(0xFFDDDDDD); |
| 200 | canvas->drawColor(SK_ColorWHITE); |
| 201 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 202 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 203 | static void drawdots(SkCanvas* canvas, const SkPaint& orig) { |
| 204 | SkTDArray<SkPoint> pts; |
| 205 | SkPathEffect* pe = makepe(0, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 206 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 207 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 208 | SkPath path, dstPath; |
| 209 | orig.getTextPath("9", 1, 0, 0, &path); |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 210 | pe->filterPath(&dstPath, path, &rec, NULL); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 211 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 212 | SkPaint p; |
| 213 | p.setAntiAlias(true); |
| 214 | p.setStrokeWidth(10); |
| 215 | p.setColor(SK_ColorRED); |
| 216 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), |
| 217 | p); |
| 218 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 219 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 220 | virtual void onDraw(SkCanvas* canvas) { |
| 221 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 222 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 223 | SkScalar x = SkIntToScalar(20); |
| 224 | SkScalar y = SkIntToScalar(300); |
| 225 | SkPaint paint; |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 226 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 227 | paint.setAntiAlias(true); |
| 228 | paint.setTextSize(SkIntToScalar(240)); |
| 229 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 230 | SkTypeface::kBold)); |
| 231 | |
| 232 | SkString str("9"); |
| 233 | |
| 234 | paint.setTypeface(fFace); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 235 | |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 236 | apply_shader(&paint, SkScalarToFloat(fInterp)); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 237 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 238 | |
| 239 | // drawdots(canvas, paint); |
| 240 | |
| 241 | if (false) { |
| 242 | fInterp += fDx; |
| 243 | if (fInterp > 1) { |
| 244 | fInterp = 1; |
| 245 | fDx = -fDx; |
| 246 | } else if (fInterp < 0) { |
| 247 | fInterp = 0; |
| 248 | fDx = -fDx; |
| 249 | } |
| 250 | this->inval(NULL); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | typedef SkView INHERITED; |
| 256 | }; |
| 257 | |
| 258 | ////////////////////////////////////////////////////////////////////////////// |
| 259 | |
| 260 | static SkView* MyFactory() { return new ClockFaceView; } |
| 261 | static SkViewRegister reg(MyFactory); |