blob: 959ac5094cc6c8fd53d6ab0b46346e4f34b99642 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com837cf782009-10-20 03:26:17 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.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"
halcanary435657f2015-09-15 12:53:07 -070019#include "SkStrokeRec.h"
reed@android.com837cf782009-10-20 03:26:17 +000020#include "SkTypeface.h"
reed@android.com837cf782009-10-20 03:26:17 +000021
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,
mtklein36352bf2015-03-25 18:17:31 -070035 SkPMColor result[]) const override {
mike@reedtribe.org102b4d52012-12-24 18:15:57 +000036 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,
mtklein36352bf2015-03-25 18:17:31 -070049 SkPMColor result[]) const 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)
jvanverthc7027ab2016-06-16 09:52:35 -070075 class Registrar {
76 public:
77 Registrar() {
78 SkFlattenable::Register("Dot2DPathEffect",
79 Dot2DPathEffect::CreateProc,
80 Dot2DPathEffect::GetFlattenableType());
81 }
82 };
reed@android.com837cf782009-10-20 03:26:17 +000083protected:
mtklein36352bf2015-03-25 18:17:31 -070084 void begin(const SkIRect& uvBounds, SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000085 if (fPts) {
86 fPts->reset();
87 }
88 this->INHERITED::begin(uvBounds, dst);
89 }
reed@google.com548a1f32012-12-18 16:12:09 +000090
91 virtual void next(const SkPoint& loc, int u, int v,
mtklein36352bf2015-03-25 18:17:31 -070092 SkPath* dst) const override {
reed@android.com837cf782009-10-20 03:26:17 +000093 if (fPts) {
94 *fPts->append() = loc;
95 }
96 dst->addCircle(loc.fX, loc.fY, fRadius);
97 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +000098
mtklein36352bf2015-03-25 18:17:31 -070099 void flatten(SkWriteBuffer& buffer) const override {
reed9fa60da2014-08-21 07:59:51 -0700100 buffer.writeMatrix(this->getMatrix());
djsollen@google.com54924242012-03-29 15:18:04 +0000101 buffer.writeScalar(fRadius);
102 }
103
reed@android.com837cf782009-10-20 03:26:17 +0000104private:
105 SkScalar fRadius;
106 SkTDArray<SkPoint>* fPts;
107
reed@android.com837cf782009-10-20 03:26:17 +0000108 typedef Sk2DPathEffect INHERITED;
109};
110
jvanverthc7027ab2016-06-16 09:52:35 -0700111static Dot2DPathEffect::Registrar gReg0;
112
reed60c9b582016-04-03 09:11:13 -0700113sk_sp<SkFlattenable> Dot2DPathEffect::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700114 SkMatrix matrix;
115 buffer.readMatrix(&matrix);
reed60c9b582016-04-03 09:11:13 -0700116 return sk_make_sp<Dot2DPathEffect>(buffer.readScalar(), matrix, nullptr);
reed9fa60da2014-08-21 07:59:51 -0700117}
118
reed@android.com837cf782009-10-20 03:26:17 +0000119class InverseFillPE : public SkPathEffect {
120public:
121 InverseFillPE() {}
reed@google.com548a1f32012-12-18 16:12:09 +0000122 virtual bool filterPath(SkPath* dst, const SkPath& src,
mtklein36352bf2015-03-25 18:17:31 -0700123 SkStrokeRec*, const SkRect*) const override {
reed@android.com837cf782009-10-20 03:26:17 +0000124 *dst = src;
125 dst->setFillType(SkPath::kInverseWinding_FillType);
126 return true;
127 }
robertphillips42dbfa82015-01-26 06:08:52 -0800128
129#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -0700130 void toString(SkString* str) const override {
robertphillips42dbfa82015-01-26 06:08:52 -0800131 str->appendf("InverseFillPE: ()");
132 }
133#endif
134
djsollen@google.comba28d032012-03-26 17:57:35 +0000135 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE)
136
reed@android.com837cf782009-10-20 03:26:17 +0000137private:
reed@android.com837cf782009-10-20 03:26:17 +0000138 typedef SkPathEffect INHERITED;
139};
140
reed60c9b582016-04-03 09:11:13 -0700141sk_sp<SkFlattenable> InverseFillPE::CreateProc(SkReadBuffer& buffer) {
142 return sk_make_sp<InverseFillPE>();
143}
reed9fa60da2014-08-21 07:59:51 -0700144
reeda4393342016-03-18 11:22:57 -0700145static sk_sp<SkPathEffect> makepe(float interp, SkTDArray<SkPoint>* pts) {
reed@android.com837cf782009-10-20 03:26:17 +0000146 SkMatrix lattice;
147 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp);
148 lattice.setScale(rad*2, rad*2, 0, 0);
149 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700150 return sk_make_sp<Dot2DPathEffect>(rad, lattice, pts);
reed@android.com837cf782009-10-20 03:26:17 +0000151}
152
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000153static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p, SkScalar interp) {
reeda4393342016-03-18 11:22:57 -0700154 p.setPathEffect(makepe(SkScalarToFloat(interp), nullptr));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000155 rastBuilder->addLayer(p);
reed@android.com837cf782009-10-20 03:26:17 +0000156#if 0
157 p.setPathEffect(new InverseFillPE())->unref();
158 p.setXfermodeMode(SkXfermode::kSrcIn_Mode);
159 p.setXfermodeMode(SkXfermode::kClear_Mode);
160 p.setAlpha((1 - interp) * 255);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000161 rastBuilder->addLayer(p);
reed@android.com837cf782009-10-20 03:26:17 +0000162#endif
163}
164
165typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
166
Mike Reed0e39f972016-11-23 22:16:59 +0000167static void apply_shader(SkPaint* paint, float scale) {
reed@android.com837cf782009-10-20 03:26:17 +0000168 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000169 SkLayerRasterizer::Builder rastBuilder;
reed@android.com837cf782009-10-20 03:26:17 +0000170
171 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000172 r7(&rastBuilder, p, scale);
reed7b380d02016-03-21 13:25:16 -0700173 paint->setRasterizer(rastBuilder.detach());
reed@android.com837cf782009-10-20 03:26:17 +0000174
175 paint->setColor(SK_ColorBLUE);
176}
177
178class ClockFaceView : public SkView {
bungeman13b9c952016-05-12 10:09:30 -0700179 sk_sp<SkTypeface> fFace;
reed@android.com837cf782009-10-20 03:26:17 +0000180 SkScalar fInterp;
181 SkScalar fDx;
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000182
reed@android.com837cf782009-10-20 03:26:17 +0000183public:
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000184 ClockFaceView() {
bungeman13b9c952016-05-12 10:09:30 -0700185 fFace = SkTypeface::MakeFromFile("/Users/reed/Downloads/p052024l.pfb");
reed@android.com837cf782009-10-20 03:26:17 +0000186 fInterp = 0;
187 fDx = SK_Scalar1/64;
188 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000189
reed@android.com837cf782009-10-20 03:26:17 +0000190protected:
191 // overrides from SkEventSink
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000192 virtual bool onQuery(SkEvent* evt) {
193 if (SampleCode::TitleQ(*evt)) {
reed@android.com837cf782009-10-20 03:26:17 +0000194 SampleCode::TitleR(evt, "Text Effects");
195 return true;
196 }
197 return this->INHERITED::onQuery(evt);
198 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000199
mike@reedtribe.org102b4d52012-12-24 18:15:57 +0000200 void drawBG(SkCanvas* canvas) {
reed@android.com837cf782009-10-20 03:26:17 +0000201// canvas->drawColor(0xFFDDDDDD);
202 canvas->drawColor(SK_ColorWHITE);
203 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000204
reed@android.com837cf782009-10-20 03:26:17 +0000205 static void drawdots(SkCanvas* canvas, const SkPaint& orig) {
206 SkTDArray<SkPoint> pts;
reeda4393342016-03-18 11:22:57 -0700207 auto pe = makepe(0, &pts);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000208
reed@google.comfd4be262012-05-25 01:04:12 +0000209 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
reed@android.com837cf782009-10-20 03:26:17 +0000210 SkPath path, dstPath;
211 orig.getTextPath("9", 1, 0, 0, &path);
halcanary96fcdcc2015-08-27 07:41:13 -0700212 pe->filterPath(&dstPath, path, &rec, nullptr);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000213
reed@android.com837cf782009-10-20 03:26:17 +0000214 SkPaint p;
215 p.setAntiAlias(true);
216 p.setStrokeWidth(10);
217 p.setColor(SK_ColorRED);
reeda4393342016-03-18 11:22:57 -0700218 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), p);
reed@android.com837cf782009-10-20 03:26:17 +0000219 }
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000220
reed@android.com837cf782009-10-20 03:26:17 +0000221 virtual void onDraw(SkCanvas* canvas) {
222 this->drawBG(canvas);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000223
reed@android.com837cf782009-10-20 03:26:17 +0000224 SkScalar x = SkIntToScalar(20);
225 SkScalar y = SkIntToScalar(300);
226 SkPaint paint;
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000227
reed@android.com837cf782009-10-20 03:26:17 +0000228 paint.setAntiAlias(true);
229 paint.setTextSize(SkIntToScalar(240));
mbocee6a9912016-05-31 11:42:36 -0700230 paint.setTypeface(SkTypeface::MakeFromName("sans-serif",
231 SkFontStyle::FromOldStyle(SkTypeface::kBold)));
reed@android.com837cf782009-10-20 03:26:17 +0000232
233 SkString str("9");
234
235 paint.setTypeface(fFace);
bsalomon@google.comdd53d912011-02-13 18:21:16 +0000236
robertphillips@google.comc6ce7502012-05-08 13:15:37 +0000237 apply_shader(&paint, SkScalarToFloat(fInterp));
Cary Clark2a475ea2017-04-28 15:35:12 -0400238 canvas->drawString(str, x, y, paint);
reed@android.com837cf782009-10-20 03:26:17 +0000239
240 // drawdots(canvas, paint);
241
242 if (false) {
243 fInterp += fDx;
244 if (fInterp > 1) {
245 fInterp = 1;
246 fDx = -fDx;
247 } else if (fInterp < 0) {
248 fInterp = 0;
249 fDx = -fDx;
250 }
halcanary96fcdcc2015-08-27 07:41:13 -0700251 this->inval(nullptr);
reed@android.com837cf782009-10-20 03:26:17 +0000252 }
253 }
254
255private:
256 typedef SkView INHERITED;
257};
258
259//////////////////////////////////////////////////////////////////////////////
260
261static SkView* MyFactory() { return new ClockFaceView; }
262static SkViewRegister reg(MyFactory);