blob: 11684e11ada34150d462a6e744c7b333512fba64 [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"
halcanary435657f2015-09-15 12:53:07 -070020#include "SkStrokeRec.h"
reed@android.com837cf782009-10-20 03:26:17 +000021#include "SkTypeface.h"
reed@android.com837cf782009-10-20 03:26:17 +000022
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,
mtklein36352bf2015-03-25 18:17:31 -070036 SkPMColor result[]) const override {
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000037 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,
mtklein36352bf2015-03-25 18:17:31 -070050 SkPMColor result[]) const 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:
mtklein36352bf2015-03-25 18:17:31 -070078 void begin(const SkIRect& uvBounds, SkPath* dst) const 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,
mtklein36352bf2015-03-25 18:17:31 -070086 SkPath* dst) const 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
mtklein36352bf2015-03-25 18:17:31 -070093 void flatten(SkWriteBuffer& buffer) const override {
reed9fa60da2014-08-21 07:59:51 -070094 buffer.writeMatrix(this->getMatrix());
djsollen@google.com54924242012-03-29 15:18:04 +000095 buffer.writeScalar(fRadius);
96 }
97
reed@android.com837cf782009-10-20 03:26:17 +000098private:
99 SkScalar fRadius;
100 SkTDArray<SkPoint>* fPts;
101
reed@android.com837cf782009-10-20 03:26:17 +0000102 typedef Sk2DPathEffect INHERITED;
103};
104
reed9fa60da2014-08-21 07:59:51 -0700105SkFlattenable* Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
106 SkMatrix matrix;
107 buffer.readMatrix(&matrix);
halcanary96fcdcc2015-08-27 07:41:13 -0700108 return new Dot2DPathEffect(buffer.readScalar(), matrix, nullptr);
reed9fa60da2014-08-21 07:59:51 -0700109}
110
reed@android.com837cf782009-10-20 03:26:17 +0000111class InverseFillPE : public SkPathEffect {
112public:
113 InverseFillPE() {}
reed@google.com548a1f32012-12-18 16:12:09 +0000114 virtual bool filterPath(SkPath* dst, const SkPath& src,
mtklein36352bf2015-03-25 18:17:31 -0700115 SkStrokeRec*, const SkRect*) const override {
reed@android.com837cf782009-10-20 03:26:17 +0000116 *dst = src;
117 dst->setFillType(SkPath::kInverseWinding_FillType);
118 return true;
119 }
robertphillips42dbfa82015-01-26 06:08:52 -0800120
121#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -0700122 void toString(SkString* str) const override {
robertphillips42dbfa82015-01-26 06:08:52 -0800123 str->appendf("InverseFillPE: ()");
124 }
125#endif
126
djsollen@google.comba28d032012-03-26 17:57:35 +0000127 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
128
reed@android.com837cf782009-10-20 03:26:17 +0000129private:
reed@android.com837cf782009-10-20 03:26:17 +0000130 typedef SkPathEffect INHERITED;
131};
132
halcanary385fe4d2015-08-26 13:07:48 -0700133SkFlattenable* InverseFillPE::CreateProc(SkReadBuffer& buffer) { return new InverseFillPE; }
reed9fa60da2014-08-21 07:59:51 -0700134
reed@android.com837cf782009-10-20 03:26:17 +0000135static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) {
136 SkMatrix lattice;
137 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
138 lattice.setScale(rad*2, rad*2, 0, 0);
139 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
140 return new Dot2DPathEffect(rad, lattice, pts);
141}
142
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000143static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 p.setPathEffect(makepe(SkScalarToFloat(interp), nullptr))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000145 rastBuilder->addLayer(p);
reed@android.com837cf782009-10-20 03:26:17 +0000146#if 0
147 p.setPathEffect(new InverseFillPE())->unref();
148 p.setXfermodeMode(SkXfermode::kSrcIn_Mode);
149 p.setXfermodeMode(SkXfermode::kClear_Mode);
150 p.setAlpha((1 - interp) * 255);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000151 rastBuilder->addLayer(p);
reed@android.com837cf782009-10-20 03:26:17 +0000152#endif
153}
154
155typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
156
157#include "SkXfermode.h"
158
159static void apply_shader(SkPaint* paint, float scale)
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000160{
reed@android.com837cf782009-10-20 03:26:17 +0000161 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000162 SkLayerRasterizer::Builder rastBuilder;
reed@android.com837cf782009-10-20 03:26:17 +0000163
164 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000165 r7(&rastBuilder, p, scale);
166 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
reed@android.com837cf782009-10-20 03:26:17 +0000167
168 paint->setColor(SK_ColorBLUE);
169}
170
171class ClockFaceView : public SkView {
172 SkTypeface* fFace;
173 SkScalar fInterp;
174 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000175
reed@android.com837cf782009-10-20 03:26:17 +0000176public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000177 ClockFaceView() {
reed@android.com837cf782009-10-20 03:26:17 +0000178 fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb");
179 fInterp = 0;
180 fDx = SK_Scalar1/64;
181 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000182
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000183 virtual ~ClockFaceView() {
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000184 SkSafeUnref(fFace);
reed@android.com837cf782009-10-20 03:26:17 +0000185 }
186
187protected:
188 // overrides from SkEventSink
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000189 virtual bool onQuery(SkEvent* evt) {
190 if (SampleCode::TitleQ(*evt)) {
reed@android.com837cf782009-10-20 03:26:17 +0000191 SampleCode::TitleR(evt, "Text Effects");
192 return true;
193 }
194 return this->INHERITED::onQuery(evt);
195 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000196
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000197 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000198// canvas->drawColor(0xFFDDDDDD);
199 canvas->drawColor(SK_ColorWHITE);
200 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000201
reed@android.com837cf782009-10-20 03:26:17 +0000202 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
203 SkTDArray<SkPoint> pts;
204 SkPathEffect* pe = makepe(0, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000205
reed@google.comfd4be262012-05-25 01:04:12 +0000206 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000207 SkPath path, dstPath;
208 orig.getTextPath("9", 1, 0, 0, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700209 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000210
reed@android.com837cf782009-10-20 03:26:17 +0000211 SkPaint p;
212 p.setAntiAlias(true);
213 p.setStrokeWidth(10);
214 p.setColor(SK_ColorRED);
215 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(),
216 p);
217 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000218
reed@android.com837cf782009-10-20 03:26:17 +0000219 virtual void onDraw(SkCanvas* canvas) {
220 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000221
reed@android.com837cf782009-10-20 03:26:17 +0000222 SkScalar x = SkIntToScalar(20);
223 SkScalar y = SkIntToScalar(300);
224 SkPaint paint;
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000225
reed@android.com837cf782009-10-20 03:26:17 +0000226 paint.setAntiAlias(true);
227 paint.setTextSize(SkIntToScalar(240));
228 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
229 SkTypeface::kBold));
230
231 SkString str("9");
232
233 paint.setTypeface(fFace);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000234
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000235 apply_shader(&paint, SkScalarToFloat(fInterp));
reed@android.com837cf782009-10-20 03:26:17 +0000236 canvas->drawText(str.c_str(), str.size(), x, y, paint);
237
238 // drawdots(canvas, paint);
239
240 if (false) {
241 fInterp += fDx;
242 if (fInterp > 1) {
243 fInterp = 1;
244 fDx = -fDx;
245 } else if (fInterp < 0) {
246 fInterp = 0;
247 fDx = -fDx;
248 }
halcanary96fcdcc2015-08-27 07:41:13 -0700249 this->inval(nullptr);
reed@android.com837cf782009-10-20 03:26:17 +0000250 }
251 }
252
253private:
254 typedef SkView INHERITED;
255};
256
257//////////////////////////////////////////////////////////////////////////////
258
259static SkView* MyFactory() { return new ClockFaceView; }
260static SkViewRegister reg(MyFactory);