blob: ed0bce9b663ccdfc85064135be1d3cbb9ddefc38 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com837cf782009-10-20 03:26:17 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com837cf782009-10-20 03:26:17 +000013#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.org102b4d52012-12-24 18:15:57 +000023static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com837cf782009-10-20 03:26:17 +000024 unsigned r = SkGetPackedR32(c);
25 unsigned g = SkGetPackedG32(c);
26 unsigned b = SkGetPackedB32(c);
bsalomon@google.comdd53d912011-02-13 18:21:16 +000027
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000028 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
bsalomon@google.comdd53d912011-02-13 18:21:16 +000029
reed@android.com837cf782009-10-20 03:26:17 +000030 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
31}
32
33class SkGrayScaleColorFilter : public SkColorFilter {
34public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000035 virtual void filterSpan(const SkPMColor src[], int count,
36 SkPMColor result[]) const SK_OVERRIDE {
37 for (int i = 0; i < count; i++) {
reed@android.com837cf782009-10-20 03:26:17 +000038 result[i] = rgb2gray(src[i]);
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000039 }
reed@android.com837cf782009-10-20 03:26:17 +000040 }
41};
42
43class SkChannelMaskColorFilter : public SkColorFilter {
44public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000045 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com837cf782009-10-20 03:26:17 +000046 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
47 }
48
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000049 virtual void filterSpan(const SkPMColor src[], int count,
50 SkPMColor result[]) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +000051 SkPMColor mask = fMask;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000052 for (int i = 0; i < count; i++) {
reed@android.com837cf782009-10-20 03:26:17 +000053 result[i] = src[i] & mask;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000054 }
reed@android.com837cf782009-10-20 03:26:17 +000055 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000056
reed@android.com837cf782009-10-20 03:26:17 +000057private:
58 SkPMColor fMask;
59};
60
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000061///////////////////////////////////////////////////////////////////////////////
reed@android.com837cf782009-10-20 03:26:17 +000062
63#include "SkGradientShader.h"
64#include "SkLayerRasterizer.h"
65#include "SkBlurMaskFilter.h"
66
67#include "Sk2DPathEffect.h"
68
69class Dot2DPathEffect : public Sk2DPathEffect {
70public:
71 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
72 SkTDArray<SkPoint>* pts)
73 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
74
djsollen@google.comba28d032012-03-26 17:57:35 +000075 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com837cf782009-10-20 03:26:17 +000076
77protected:
reed@google.com548a1f32012-12-18 16:12:09 +000078 virtual void begin(const SkIRect& uvBounds, SkPath* dst) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +000079 if (fPts) {
80 fPts->reset();
81 }
82 this->INHERITED::begin(uvBounds, dst);
83 }
reed@google.com548a1f32012-12-18 16:12:09 +000084
85 virtual void next(const SkPoint& loc, int u, int v,
86 SkPath* dst) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +000087 if (fPts) {
88 *fPts->append() = loc;
89 }
90 dst->addCircle(loc.fX, loc.fY, fRadius);
91 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000092
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000093 Dot2DPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com837cf782009-10-20 03:26:17 +000094 fRadius = buffer.readScalar();
95 fPts = NULL;
96 }
reed@google.com548a1f32012-12-18 16:12:09 +000097
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000098 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
djsollen@google.com54924242012-03-29 15:18:04 +000099 this->INHERITED::flatten(buffer);
100 buffer.writeScalar(fRadius);
101 }
102
reed@android.com837cf782009-10-20 03:26:17 +0000103private:
104 SkScalar fRadius;
105 SkTDArray<SkPoint>* fPts;
106
reed@android.com837cf782009-10-20 03:26:17 +0000107 typedef Sk2DPathEffect INHERITED;
108};
109
110class InverseFillPE : public SkPathEffect {
111public:
112 InverseFillPE() {}
reed@google.com548a1f32012-12-18 16:12:09 +0000113 virtual bool filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000114 SkStrokeRec*, const SkRect*) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +0000115 *dst = src;
116 dst->setFillType(SkPath::kInverseWinding_FillType);
117 return true;
118 }
djsollen@google.comba28d032012-03-26 17:57:35 +0000119 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
120
reed@android.com837cf782009-10-20 03:26:17 +0000121protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000122 InverseFillPE(SkReadBuffer& buffer) : INHERITED(buffer) {}
reed@android.com837cf782009-10-20 03:26:17 +0000123private:
djsollen@google.comba28d032012-03-26 17:57:35 +0000124
reed@android.com837cf782009-10-20 03:26:17 +0000125 typedef SkPathEffect INHERITED;
126};
127
128static 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
136static void r7(SkLayerRasterizer* rast, SkPaint& p, SkScalar interp) {
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000137 p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref();
reed@android.com837cf782009-10-20 03:26:17 +0000138 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
148typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
149
150#include "SkXfermode.h"
151
152static void apply_shader(SkPaint* paint, float scale)
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000153{
reed@android.com837cf782009-10-20 03:26:17 +0000154 SkPaint p;
155 SkLayerRasterizer* rast = new SkLayerRasterizer;
156
157 p.setAntiAlias(true);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000158 r7(rast, p, scale);
reed@android.com837cf782009-10-20 03:26:17 +0000159 paint->setRasterizer(rast)->unref();
160
161 paint->setColor(SK_ColorBLUE);
162}
163
164class ClockFaceView : public SkView {
165 SkTypeface* fFace;
166 SkScalar fInterp;
167 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000168
reed@android.com837cf782009-10-20 03:26:17 +0000169public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000170 ClockFaceView() {
reed@android.com837cf782009-10-20 03:26:17 +0000171 fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb");
172 fInterp = 0;
173 fDx = SK_Scalar1/64;
174 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000175
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000176 virtual ~ClockFaceView() {
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000177 SkSafeUnref(fFace);
reed@android.com837cf782009-10-20 03:26:17 +0000178 }
179
180protected:
181 // overrides from SkEventSink
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000182 virtual bool onQuery(SkEvent* evt) {
183 if (SampleCode::TitleQ(*evt)) {
reed@android.com837cf782009-10-20 03:26:17 +0000184 SampleCode::TitleR(evt, "Text Effects");
185 return true;
186 }
187 return this->INHERITED::onQuery(evt);
188 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000189
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000190 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000191// canvas->drawColor(0xFFDDDDDD);
192 canvas->drawColor(SK_ColorWHITE);
193 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000194
reed@android.com837cf782009-10-20 03:26:17 +0000195 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
196 SkTDArray<SkPoint> pts;
197 SkPathEffect* pe = makepe(0, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000198
reed@google.comfd4be262012-05-25 01:04:12 +0000199 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000200 SkPath path, dstPath;
201 orig.getTextPath("9", 1, 0, 0, &path);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000202 pe->filterPath(&dstPath, path, &rec, NULL);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000203
reed@android.com837cf782009-10-20 03:26:17 +0000204 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.comdd53d912011-02-13 18:21:16 +0000211
reed@android.com837cf782009-10-20 03:26:17 +0000212 virtual void onDraw(SkCanvas* canvas) {
213 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000214
reed@android.com837cf782009-10-20 03:26:17 +0000215 SkScalar x = SkIntToScalar(20);
216 SkScalar y = SkIntToScalar(300);
217 SkPaint paint;
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000218
reed@android.com837cf782009-10-20 03:26:17 +0000219 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.comdd53d912011-02-13 18:21:16 +0000227
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000228 apply_shader(&paint, SkScalarToFloat(fInterp));
reed@android.com837cf782009-10-20 03:26:17 +0000229 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
246private:
247 typedef SkView INHERITED;
248};
249
250//////////////////////////////////////////////////////////////////////////////
251
252static SkView* MyFactory() { return new ClockFaceView; }
253static SkViewRegister reg(MyFactory);