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