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" |
| 21 | #include "SkAvoidXfermode.h" |
| 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, |
| 36 | SkPMColor result[]) const SK_OVERRIDE { |
| 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, |
| 50 | SkPMColor result[]) const SK_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: |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 78 | virtual void begin(const SkIRect& uvBounds, SkPath* dst) const SK_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, |
| 86 | SkPath* dst) const SK_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 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 93 | Dot2DPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 94 | fRadius = buffer.readScalar(); |
| 95 | fPts = NULL; |
| 96 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 98 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE { |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 99 | this->INHERITED::flatten(buffer); |
| 100 | buffer.writeScalar(fRadius); |
| 101 | } |
| 102 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 103 | private: |
| 104 | SkScalar fRadius; |
| 105 | SkTDArray<SkPoint>* fPts; |
| 106 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 107 | typedef Sk2DPathEffect INHERITED; |
| 108 | }; |
| 109 | |
| 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, |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 114 | SkStrokeRec*, const SkRect*) const SK_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 | } |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 119 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE) |
| 120 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 121 | protected: |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 122 | InverseFillPE(SkReadBuffer& buffer) : INHERITED(buffer) {} |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 123 | private: |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 124 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 125 | typedef SkPathEffect INHERITED; |
| 126 | }; |
| 127 | |
| 128 | static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) { |
| 129 | SkMatrix lattice; |
| 130 | SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); |
| 131 | lattice.setScale(rad*2, rad*2, 0, 0); |
| 132 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 133 | return new Dot2DPathEffect(rad, lattice, pts); |
| 134 | } |
| 135 | |
| 136 | static void r7(SkLayerRasterizer* rast, SkPaint& p, SkScalar interp) { |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 137 | p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref(); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 138 | rast->addLayer(p); |
| 139 | #if 0 |
| 140 | p.setPathEffect(new InverseFillPE())->unref(); |
| 141 | p.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 142 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 143 | p.setAlpha((1 - interp) * 255); |
| 144 | rast->addLayer(p); |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 149 | |
| 150 | #include "SkXfermode.h" |
| 151 | |
| 152 | static void apply_shader(SkPaint* paint, float scale) |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 153 | { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 154 | SkPaint p; |
| 155 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
| 156 | |
| 157 | p.setAntiAlias(true); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 158 | r7(rast, p, scale); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 159 | paint->setRasterizer(rast)->unref(); |
| 160 | |
| 161 | paint->setColor(SK_ColorBLUE); |
| 162 | } |
| 163 | |
| 164 | class ClockFaceView : public SkView { |
| 165 | SkTypeface* fFace; |
| 166 | SkScalar fInterp; |
| 167 | SkScalar fDx; |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 168 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 169 | public: |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 170 | ClockFaceView() { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 171 | fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb"); |
| 172 | fInterp = 0; |
| 173 | fDx = SK_Scalar1/64; |
| 174 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 175 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 176 | virtual ~ClockFaceView() { |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 177 | SkSafeUnref(fFace); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | protected: |
| 181 | // overrides from SkEventSink |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 182 | virtual bool onQuery(SkEvent* evt) { |
| 183 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 184 | SampleCode::TitleR(evt, "Text Effects"); |
| 185 | return true; |
| 186 | } |
| 187 | return this->INHERITED::onQuery(evt); |
| 188 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 189 | |
mike@reedtribe.org | 102b4d5 | 2012-12-24 18:15:57 +0000 | [diff] [blame] | 190 | void drawBG(SkCanvas* canvas) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 191 | // canvas->drawColor(0xFFDDDDDD); |
| 192 | canvas->drawColor(SK_ColorWHITE); |
| 193 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 194 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 195 | static void drawdots(SkCanvas* canvas, const SkPaint& orig) { |
| 196 | SkTDArray<SkPoint> pts; |
| 197 | SkPathEffect* pe = makepe(0, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 198 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 199 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 200 | SkPath path, dstPath; |
| 201 | orig.getTextPath("9", 1, 0, 0, &path); |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 202 | pe->filterPath(&dstPath, path, &rec, NULL); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 203 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 204 | SkPaint p; |
| 205 | p.setAntiAlias(true); |
| 206 | p.setStrokeWidth(10); |
| 207 | p.setColor(SK_ColorRED); |
| 208 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), |
| 209 | p); |
| 210 | } |
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 | virtual void onDraw(SkCanvas* canvas) { |
| 213 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 214 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 215 | SkScalar x = SkIntToScalar(20); |
| 216 | SkScalar y = SkIntToScalar(300); |
| 217 | SkPaint paint; |
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 | paint.setAntiAlias(true); |
| 220 | paint.setTextSize(SkIntToScalar(240)); |
| 221 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 222 | SkTypeface::kBold)); |
| 223 | |
| 224 | SkString str("9"); |
| 225 | |
| 226 | paint.setTypeface(fFace); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 227 | |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 228 | apply_shader(&paint, SkScalarToFloat(fInterp)); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 229 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 230 | |
| 231 | // drawdots(canvas, paint); |
| 232 | |
| 233 | if (false) { |
| 234 | fInterp += fDx; |
| 235 | if (fInterp > 1) { |
| 236 | fInterp = 1; |
| 237 | fDx = -fDx; |
| 238 | } else if (fInterp < 0) { |
| 239 | fInterp = 0; |
| 240 | fDx = -fDx; |
| 241 | } |
| 242 | this->inval(NULL); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | private: |
| 247 | typedef SkView INHERITED; |
| 248 | }; |
| 249 | |
| 250 | ////////////////////////////////////////////////////////////////////////////// |
| 251 | |
| 252 | static SkView* MyFactory() { return new ClockFaceView; } |
| 253 | static SkViewRegister reg(MyFactory); |