epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRegion.h" |
| 15 | #include "SkShader.h" |
| 16 | #include "SkUtils.h" |
| 17 | #include "SkColorPriv.h" |
| 18 | #include "SkColorFilter.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 19 | #include "SkStrokeRec.h" |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 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) |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 75 | class Registrar { |
| 76 | public: |
| 77 | Registrar() { |
| 78 | SkFlattenable::Register("Dot2DPathEffect", |
| 79 | Dot2DPathEffect::CreateProc, |
| 80 | Dot2DPathEffect::GetFlattenableType()); |
| 81 | } |
| 82 | }; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 83 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | void begin(const SkIRect& uvBounds, SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 85 | if (fPts) { |
| 86 | fPts->reset(); |
| 87 | } |
| 88 | this->INHERITED::begin(uvBounds, dst); |
| 89 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 90 | |
| 91 | virtual void next(const SkPoint& loc, int u, int v, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | SkPath* dst) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 93 | if (fPts) { |
| 94 | *fPts->append() = loc; |
| 95 | } |
| 96 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 97 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 98 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 100 | buffer.writeMatrix(this->getMatrix()); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 101 | buffer.writeScalar(fRadius); |
| 102 | } |
| 103 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 104 | private: |
| 105 | SkScalar fRadius; |
| 106 | SkTDArray<SkPoint>* fPts; |
| 107 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 108 | typedef Sk2DPathEffect INHERITED; |
| 109 | }; |
| 110 | |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 111 | static Dot2DPathEffect::Registrar gReg0; |
| 112 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 113 | sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 114 | SkMatrix matrix; |
| 115 | buffer.readMatrix(&matrix); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 116 | return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 117 | } |
| 118 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 119 | class InverseFillPE : public SkPathEffect { |
| 120 | public: |
| 121 | InverseFillPE() {} |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 122 | virtual bool filterPath(SkPath* dst, const SkPath& src, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 123 | SkStrokeRec*, const SkRect*) const override { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 124 | *dst = src; |
| 125 | dst->setFillType(SkPath::kInverseWinding_FillType); |
| 126 | return true; |
| 127 | } |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 128 | |
| 129 | #ifndef SK_IGNORE_TO_STRING |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | void toString(SkString* str) const override { |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 131 | str->appendf("InverseFillPE: ()"); |
| 132 | } |
| 133 | #endif |
| 134 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 135 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE) |
| 136 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 137 | private: |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 138 | typedef SkPathEffect INHERITED; |
| 139 | }; |
| 140 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 141 | sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) { |
| 142 | return sk_make_sp<InverseFillPE>(); |
| 143 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 144 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 145 | static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 146 | SkMatrix lattice; |
| 147 | SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); |
| 148 | lattice.setScale(rad*2, rad*2, 0, 0); |
| 149 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 150 | return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 151 | } |
| 152 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 153 | static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 154 | p.setPathEffect(makepe(SkScalarToFloat(interp), nullptr)); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 155 | rastBuilder->addLayer(p); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 156 | #if 0 |
| 157 | p.setPathEffect(new InverseFillPE())->unref(); |
| 158 | p.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 159 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 160 | p.setAlpha((1 - interp) * 255); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 161 | rastBuilder->addLayer(p); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 162 | #endif |
| 163 | } |
| 164 | |
| 165 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 166 | |
Mike Reed | 0e39f97 | 2016-11-23 22:16:59 +0000 | [diff] [blame] | 167 | static void apply_shader(SkPaint* paint, float scale) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 168 | SkPaint p; |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 169 | SkLayerRasterizer::Builder rastBuilder; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 170 | |
| 171 | p.setAntiAlias(true); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 172 | r7(&rastBuilder, p, scale); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 173 | paint->setRasterizer(rastBuilder.detach()); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 174 | |
| 175 | paint->setColor(SK_ColorBLUE); |
| 176 | } |
| 177 | |
| 178 | class ClockFaceView : public SkView { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 179 | sk_sp<SkTypeface> fFace; |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 180 | SkScalar fInterp; |
| 181 | SkScalar fDx; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 182 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 183 | public: |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 184 | ClockFaceView() { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 185 | fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb"); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 186 | fInterp = 0; |
| 187 | fDx = SK_Scalar1/64; |
| 188 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 189 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 190 | protected: |
| 191 | // overrides from SkEventSink |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 192 | virtual bool onQuery(SkEvent* evt) { |
| 193 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 194 | SampleCode::TitleR(evt, "Text Effects"); |
| 195 | return true; |
| 196 | } |
| 197 | return this->INHERITED::onQuery(evt); |
| 198 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 199 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 200 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 201 | // canvas->drawColor(0xFFDDDDDD); |
| 202 | canvas->drawColor(SK_ColorWHITE); |
| 203 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 204 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 205 | static void drawdots(SkCanvas* canvas, const SkPaint& orig) { |
| 206 | SkTDArray<SkPoint> pts; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 207 | auto pe = makepe(0, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 208 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 209 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 210 | SkPath path, dstPath; |
| 211 | orig.getTextPath("9", 1, 0, 0, &path); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | pe->filterPath(&dstPath, path, &rec, nullptr); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 213 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 214 | SkPaint p; |
| 215 | p.setAntiAlias(true); |
| 216 | p.setStrokeWidth(10); |
| 217 | p.setColor(SK_ColorRED); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 218 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), p); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 219 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 220 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 221 | virtual void onDraw(SkCanvas* canvas) { |
| 222 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 223 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 224 | SkScalar x = SkIntToScalar(20); |
| 225 | SkScalar y = SkIntToScalar(300); |
| 226 | SkPaint paint; |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 227 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 228 | paint.setAntiAlias(true); |
| 229 | paint.setTextSize(SkIntToScalar(240)); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 230 | paint.setTypeface(SkTypeface::MakeFromName("sans-serif", |
| 231 | SkFontStyle::FromOldStyle(SkTypeface::kBold))); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 232 | |
| 233 | SkString str("9"); |
| 234 | |
| 235 | paint.setTypeface(fFace); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 236 | |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 237 | apply_shader(&paint, SkScalarToFloat(fInterp)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 238 | canvas->drawString(str, x, y, paint); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 239 | |
| 240 | // drawdots(canvas, paint); |
| 241 | |
| 242 | if (false) { |
| 243 | fInterp += fDx; |
| 244 | if (fInterp > 1) { |
| 245 | fInterp = 1; |
| 246 | fDx = -fDx; |
| 247 | } else if (fInterp < 0) { |
| 248 | fInterp = 0; |
| 249 | fDx = -fDx; |
| 250 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 251 | this->inval(nullptr); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | private: |
| 256 | typedef SkView INHERITED; |
| 257 | }; |
| 258 | |
| 259 | ////////////////////////////////////////////////////////////////////////////// |
| 260 | |
| 261 | static SkView* MyFactory() { return new ClockFaceView; } |
| 262 | static SkViewRegister reg(MyFactory); |