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