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" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 11 | #include "SkFlattenableBuffers.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" |
| 19 | #include "SkTypeface.h" |
| 20 | #include "SkAvoidXfermode.h" |
| 21 | |
| 22 | static inline SkPMColor rgb2gray(SkPMColor c) |
| 23 | { |
| 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: |
| 35 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) |
| 36 | { |
| 37 | for (int i = 0; i < count; i++) |
| 38 | result[i] = rgb2gray(src[i]); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | class SkChannelMaskColorFilter : public SkColorFilter { |
| 43 | public: |
| 44 | SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) |
| 45 | { |
| 46 | fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); |
| 47 | } |
| 48 | |
| 49 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) |
| 50 | { |
| 51 | SkPMColor mask = fMask; |
| 52 | for (int i = 0; i < count; i++) |
| 53 | result[i] = src[i] & mask; |
| 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 | |
| 60 | /////////////////////////////////////////////////////////// |
| 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: |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame^] | 77 | virtual void begin(const SkIRect& uvBounds, SkPath* dst) const SK_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, |
| 85 | SkPath* dst) const SK_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 | |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame^] | 92 | Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 93 | fRadius = buffer.readScalar(); |
| 94 | fPts = NULL; |
| 95 | } |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame^] | 96 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 97 | virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { |
| 98 | this->INHERITED::flatten(buffer); |
| 99 | buffer.writeScalar(fRadius); |
| 100 | } |
| 101 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 102 | private: |
| 103 | SkScalar fRadius; |
| 104 | SkTDArray<SkPoint>* fPts; |
| 105 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 106 | typedef Sk2DPathEffect INHERITED; |
| 107 | }; |
| 108 | |
| 109 | class InverseFillPE : public SkPathEffect { |
| 110 | public: |
| 111 | InverseFillPE() {} |
reed@google.com | 548a1f3 | 2012-12-18 16:12:09 +0000 | [diff] [blame^] | 112 | virtual bool filterPath(SkPath* dst, const SkPath& src, |
| 113 | SkStrokeRec*) const SK_OVERRIDE { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 114 | *dst = src; |
| 115 | dst->setFillType(SkPath::kInverseWinding_FillType); |
| 116 | return true; |
| 117 | } |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 118 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE) |
| 119 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 120 | protected: |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 121 | InverseFillPE(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 122 | private: |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 123 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 124 | typedef SkPathEffect INHERITED; |
| 125 | }; |
| 126 | |
| 127 | static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) { |
| 128 | SkMatrix lattice; |
| 129 | SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); |
| 130 | lattice.setScale(rad*2, rad*2, 0, 0); |
| 131 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
| 132 | return new Dot2DPathEffect(rad, lattice, pts); |
| 133 | } |
| 134 | |
| 135 | static void r7(SkLayerRasterizer* rast, SkPaint& p, SkScalar interp) { |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 136 | p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref(); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 137 | rast->addLayer(p); |
| 138 | #if 0 |
| 139 | p.setPathEffect(new InverseFillPE())->unref(); |
| 140 | p.setXfermodeMode(SkXfermode::kSrcIn_Mode); |
| 141 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
| 142 | p.setAlpha((1 - interp) * 255); |
| 143 | rast->addLayer(p); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 148 | |
| 149 | #include "SkXfermode.h" |
| 150 | |
| 151 | static void apply_shader(SkPaint* paint, float scale) |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 152 | { |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 153 | SkPaint p; |
| 154 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
| 155 | |
| 156 | p.setAntiAlias(true); |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 157 | r7(rast, p, SkFloatToScalar(scale)); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 158 | paint->setRasterizer(rast)->unref(); |
| 159 | |
| 160 | paint->setColor(SK_ColorBLUE); |
| 161 | } |
| 162 | |
| 163 | class ClockFaceView : public SkView { |
| 164 | SkTypeface* fFace; |
| 165 | SkScalar fInterp; |
| 166 | SkScalar fDx; |
| 167 | public: |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 168 | ClockFaceView() |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 169 | { |
| 170 | fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb"); |
| 171 | fInterp = 0; |
| 172 | fDx = SK_Scalar1/64; |
| 173 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 174 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 175 | virtual ~ClockFaceView() |
| 176 | { |
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 |
| 182 | virtual bool onQuery(SkEvent* evt) |
| 183 | { |
| 184 | if (SampleCode::TitleQ(*evt)) |
| 185 | { |
| 186 | SampleCode::TitleR(evt, "Text Effects"); |
| 187 | return true; |
| 188 | } |
| 189 | return this->INHERITED::onQuery(evt); |
| 190 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 191 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 192 | void drawBG(SkCanvas* canvas) |
| 193 | { |
| 194 | // canvas->drawColor(0xFFDDDDDD); |
| 195 | canvas->drawColor(SK_ColorWHITE); |
| 196 | } |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 197 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 198 | static void drawdots(SkCanvas* canvas, const SkPaint& orig) { |
| 199 | SkTDArray<SkPoint> pts; |
| 200 | SkPathEffect* pe = makepe(0, &pts); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 201 | |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 202 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 203 | SkPath path, dstPath; |
| 204 | orig.getTextPath("9", 1, 0, 0, &path); |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 205 | pe->filterPath(&dstPath, path, &rec); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 206 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 207 | SkPaint p; |
| 208 | p.setAntiAlias(true); |
| 209 | p.setStrokeWidth(10); |
| 210 | p.setColor(SK_ColorRED); |
| 211 | canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), |
| 212 | p); |
| 213 | } |
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 | virtual void onDraw(SkCanvas* canvas) { |
| 216 | this->drawBG(canvas); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 217 | |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 218 | SkScalar x = SkIntToScalar(20); |
| 219 | SkScalar y = SkIntToScalar(300); |
| 220 | SkPaint paint; |
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 | paint.setAntiAlias(true); |
| 223 | paint.setTextSize(SkIntToScalar(240)); |
| 224 | paint.setTypeface(SkTypeface::CreateFromName("sans-serif", |
| 225 | SkTypeface::kBold)); |
| 226 | |
| 227 | SkString str("9"); |
| 228 | |
| 229 | paint.setTypeface(fFace); |
bsalomon@google.com | dd53d91 | 2011-02-13 18:21:16 +0000 | [diff] [blame] | 230 | |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 231 | apply_shader(&paint, SkScalarToFloat(fInterp)); |
reed@android.com | 837cf78 | 2009-10-20 03:26:17 +0000 | [diff] [blame] | 232 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
| 233 | |
| 234 | // drawdots(canvas, paint); |
| 235 | |
| 236 | if (false) { |
| 237 | fInterp += fDx; |
| 238 | if (fInterp > 1) { |
| 239 | fInterp = 1; |
| 240 | fDx = -fDx; |
| 241 | } else if (fInterp < 0) { |
| 242 | fInterp = 0; |
| 243 | fDx = -fDx; |
| 244 | } |
| 245 | this->inval(NULL); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private: |
| 250 | typedef SkView INHERITED; |
| 251 | }; |
| 252 | |
| 253 | ////////////////////////////////////////////////////////////////////////////// |
| 254 | |
| 255 | static SkView* MyFactory() { return new ClockFaceView; } |
| 256 | static SkViewRegister reg(MyFactory); |
| 257 | |