blob: 1e9b89e9de698ea0235c76bac2f36ee5e5729fc9 [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000011#include "SkFlattenableBuffers.h"
reed@android.com837cf782009-10-20 03:26:17 +000012#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
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000022static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com837cf782009-10-20 03:26:17 +000023 unsigned r = SkGetPackedR32(c);
24 unsigned g = SkGetPackedG32(c);
25 unsigned b = SkGetPackedB32(c);
bsalomon@google.comdd53d912011-02-13 18:21:16 +000026
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000027 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
bsalomon@google.comdd53d912011-02-13 18:21:16 +000028
reed@android.com837cf782009-10-20 03:26:17 +000029 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
30}
31
32class SkGrayScaleColorFilter : public SkColorFilter {
33public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000034 virtual void filterSpan(const SkPMColor src[], int count,
35 SkPMColor result[]) const SK_OVERRIDE {
36 for (int i = 0; i < count; i++) {
reed@android.com837cf782009-10-20 03:26:17 +000037 result[i] = rgb2gray(src[i]);
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000038 }
reed@android.com837cf782009-10-20 03:26:17 +000039 }
40};
41
42class SkChannelMaskColorFilter : public SkColorFilter {
43public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000044 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com837cf782009-10-20 03:26:17 +000045 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
46 }
47
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000048 virtual void filterSpan(const SkPMColor src[], int count,
49 SkPMColor result[]) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +000050 SkPMColor mask = fMask;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000051 for (int i = 0; i < count; i++) {
reed@android.com837cf782009-10-20 03:26:17 +000052 result[i] = src[i] & mask;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000053 }
reed@android.com837cf782009-10-20 03:26:17 +000054 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000055
reed@android.com837cf782009-10-20 03:26:17 +000056private:
57 SkPMColor fMask;
58};
59
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000060///////////////////////////////////////////////////////////////////////////////
reed@android.com837cf782009-10-20 03:26:17 +000061
62#include "SkGradientShader.h"
63#include "SkLayerRasterizer.h"
64#include "SkBlurMaskFilter.h"
65
66#include "Sk2DPathEffect.h"
67
68class Dot2DPathEffect : public Sk2DPathEffect {
69public:
70 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix,
71 SkTDArray<SkPoint>* pts)
72 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {}
73
djsollen@google.comba28d032012-03-26 17:57:35 +000074 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com837cf782009-10-20 03:26:17 +000075
76protected:
reed@google.com548a1f32012-12-18 16:12:09 +000077 virtual void begin(const SkIRect& uvBounds, SkPath* dst) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +000078 if (fPts) {
79 fPts->reset();
80 }
81 this->INHERITED::begin(uvBounds, dst);
82 }
reed@google.com548a1f32012-12-18 16:12:09 +000083
84 virtual void next(const SkPoint& loc, int u, int v,
85 SkPath* dst) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +000086 if (fPts) {
87 *fPts->append() = loc;
88 }
89 dst->addCircle(loc.fX, loc.fY, fRadius);
90 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000091
reed@google.com548a1f32012-12-18 16:12:09 +000092 Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com837cf782009-10-20 03:26:17 +000093 fRadius = buffer.readScalar();
94 fPts = NULL;
95 }
reed@google.com548a1f32012-12-18 16:12:09 +000096
djsollen@google.com54924242012-03-29 15:18:04 +000097 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
98 this->INHERITED::flatten(buffer);
99 buffer.writeScalar(fRadius);
100 }
101
reed@android.com837cf782009-10-20 03:26:17 +0000102private:
103 SkScalar fRadius;
104 SkTDArray<SkPoint>* fPts;
105
reed@android.com837cf782009-10-20 03:26:17 +0000106 typedef Sk2DPathEffect INHERITED;
107};
108
109class InverseFillPE : public SkPathEffect {
110public:
111 InverseFillPE() {}
reed@google.com548a1f32012-12-18 16:12:09 +0000112 virtual bool filterPath(SkPath* dst, const SkPath& src,
reed@google.com4bbdeac2013-01-24 21:03:11 +0000113 SkStrokeRec*, const SkRect*) const SK_OVERRIDE {
reed@android.com837cf782009-10-20 03:26:17 +0000114 *dst = src;
115 dst->setFillType(SkPath::kInverseWinding_FillType);
116 return true;
117 }
djsollen@google.comba28d032012-03-26 17:57:35 +0000118 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
119
reed@android.com837cf782009-10-20 03:26:17 +0000120protected:
djsollen@google.comba28d032012-03-26 17:57:35 +0000121 InverseFillPE(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {}
reed@android.com837cf782009-10-20 03:26:17 +0000122private:
djsollen@google.comba28d032012-03-26 17:57:35 +0000123
reed@android.com837cf782009-10-20 03:26:17 +0000124 typedef SkPathEffect INHERITED;
125};
126
127static 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
135static void r7(SkLayerRasterizer* rast, SkPaint& p, SkScalar interp) {
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000136 p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref();
reed@android.com837cf782009-10-20 03:26:17 +0000137 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
147typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
148
149#include "SkXfermode.h"
150
151static void apply_shader(SkPaint* paint, float scale)
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000152{
reed@android.com837cf782009-10-20 03:26:17 +0000153 SkPaint p;
154 SkLayerRasterizer* rast = new SkLayerRasterizer;
155
156 p.setAntiAlias(true);
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000157 r7(rast, p, SkFloatToScalar(scale));
reed@android.com837cf782009-10-20 03:26:17 +0000158 paint->setRasterizer(rast)->unref();
159
160 paint->setColor(SK_ColorBLUE);
161}
162
163class ClockFaceView : public SkView {
164 SkTypeface* fFace;
165 SkScalar fInterp;
166 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000167
reed@android.com837cf782009-10-20 03:26:17 +0000168public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000169 ClockFaceView() {
reed@android.com837cf782009-10-20 03:26:17 +0000170 fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb");
171 fInterp = 0;
172 fDx = SK_Scalar1/64;
173 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000174
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000175 virtual ~ClockFaceView() {
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000176 SkSafeUnref(fFace);
reed@android.com837cf782009-10-20 03:26:17 +0000177 }
178
179protected:
180 // overrides from SkEventSink
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000181 virtual bool onQuery(SkEvent* evt) {
182 if (SampleCode::TitleQ(*evt)) {
reed@android.com837cf782009-10-20 03:26:17 +0000183 SampleCode::TitleR(evt, "Text Effects");
184 return true;
185 }
186 return this->INHERITED::onQuery(evt);
187 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000188
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000189 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000190// canvas->drawColor(0xFFDDDDDD);
191 canvas->drawColor(SK_ColorWHITE);
192 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000193
reed@android.com837cf782009-10-20 03:26:17 +0000194 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
195 SkTDArray<SkPoint> pts;
196 SkPathEffect* pe = makepe(0, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000197
reed@google.comfd4be262012-05-25 01:04:12 +0000198 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000199 SkPath path, dstPath;
200 orig.getTextPath("9", 1, 0, 0, &path);
reed@google.com4bbdeac2013-01-24 21:03:11 +0000201 pe->filterPath(&dstPath, path, &rec, NULL);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000202
reed@android.com837cf782009-10-20 03:26:17 +0000203 SkPaint p;
204 p.setAntiAlias(true);
205 p.setStrokeWidth(10);
206 p.setColor(SK_ColorRED);
207 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(),
208 p);
209 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000210
reed@android.com837cf782009-10-20 03:26:17 +0000211 virtual void onDraw(SkCanvas* canvas) {
212 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000213
reed@android.com837cf782009-10-20 03:26:17 +0000214 SkScalar x = SkIntToScalar(20);
215 SkScalar y = SkIntToScalar(300);
216 SkPaint paint;
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000217
reed@android.com837cf782009-10-20 03:26:17 +0000218 paint.setAntiAlias(true);
219 paint.setTextSize(SkIntToScalar(240));
220 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
221 SkTypeface::kBold));
222
223 SkString str("9");
224
225 paint.setTypeface(fFace);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000226
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000227 apply_shader(&paint, SkScalarToFloat(fInterp));
reed@android.com837cf782009-10-20 03:26:17 +0000228 canvas->drawText(str.c_str(), str.size(), x, y, paint);
229
230 // drawdots(canvas, paint);
231
232 if (false) {
233 fInterp += fDx;
234 if (fInterp > 1) {
235 fInterp = 1;
236 fDx = -fDx;
237 } else if (fInterp < 0) {
238 fInterp = 0;
239 fDx = -fDx;
240 }
241 this->inval(NULL);
242 }
243 }
244
245private:
246 typedef SkView INHERITED;
247};
248
249//////////////////////////////////////////////////////////////////////////////
250
251static SkView* MyFactory() { return new ClockFaceView; }
252static SkViewRegister reg(MyFactory);