blob: 4a1e202877ed2b88ba73abb213052492059e0ffc [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.com8a1c16f2008-12-17 15:59:43 +00008#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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkColorPriv.h"
17#include "SkColorFilter.h"
18#include "SkTypeface.h"
19#include "SkAvoidXfermode.h"
20
reed@google.comf2183392011-04-22 14:10:48 +000021static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 unsigned r = SkGetPackedR32(c);
23 unsigned g = SkGetPackedG32(c);
24 unsigned b = SkGetPackedB32(c);
reed@google.com82065d62011-02-07 15:30:46 +000025
reed@google.com261b8e22011-04-14 17:53:24 +000026 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
reed@google.com82065d62011-02-07 15:30:46 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
29}
30
31class SkGrayScaleColorFilter : public SkColorFilter {
32public:
reed@google.comf2183392011-04-22 14:10:48 +000033 virtual void filterSpan(const SkPMColor src[], int count,
34 SkPMColor result[]) {
35 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 result[i] = rgb2gray(src[i]);
reed@google.comf2183392011-04-22 14:10:48 +000037 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 }
39};
40
41class SkChannelMaskColorFilter : public SkColorFilter {
42public:
reed@google.comf2183392011-04-22 14:10:48 +000043 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
45 }
46
reed@google.comf2183392011-04-22 14:10:48 +000047 virtual void filterSpan(const SkPMColor src[], int count,
48 SkPMColor result[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 SkPMColor mask = fMask;
reed@google.comf2183392011-04-22 14:10:48 +000050 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 result[i] = src[i] & mask;
reed@google.comf2183392011-04-22 14:10:48 +000052 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 }
reed@google.com82065d62011-02-07 15:30:46 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055private:
56 SkPMColor fMask;
57};
58
59///////////////////////////////////////////////////////////
60
61#include "SkGradientShader.h"
62#include "SkLayerRasterizer.h"
63#include "SkBlurMaskFilter.h"
64
reed@google.comf2183392011-04-22 14:10:48 +000065static void r0(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
67 SkBlurMaskFilter::kNormal_BlurStyle))->unref();
68 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
69
70 p.setMaskFilter(NULL);
71 p.setStyle(SkPaint::kStroke_Style);
72 p.setStrokeWidth(SK_Scalar1);
73 rast->addLayer(p);
74
75 p.setAlpha(0x11);
76 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000077 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 rast->addLayer(p);
79}
80
reed@google.comf2183392011-04-22 14:10:48 +000081static void r1(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 rast->addLayer(p);
83
84 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +000085 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 p.setStyle(SkPaint::kStroke_Style);
87 p.setStrokeWidth(SK_Scalar1*2);
88 rast->addLayer(p);
89}
reed@google.com82065d62011-02-07 15:30:46 +000090
reed@google.comf2183392011-04-22 14:10:48 +000091static void r2(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 p.setStyle(SkPaint::kStrokeAndFill_Style);
93 p.setStrokeWidth(SK_Scalar1*4);
94 rast->addLayer(p);
95
96 p.setStyle(SkPaint::kStroke_Style);
97 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +000098 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 rast->addLayer(p);
100}
101
reed@google.comf2183392011-04-22 14:10:48 +0000102static void r3(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 p.setStyle(SkPaint::kStroke_Style);
104 p.setStrokeWidth(SK_Scalar1*3);
105 rast->addLayer(p);
106
107 p.setAlpha(0x20);
108 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +0000109 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 rast->addLayer(p);
111}
112
reed@google.comf2183392011-04-22 14:10:48 +0000113static void r4(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 p.setAlpha(0x60);
115 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
116
117 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +0000118 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
120
121 p.setXfermode(NULL);
122 rast->addLayer(p);
123}
124
125#include "SkDiscretePathEffect.h"
126
reed@google.comf2183392011-04-22 14:10:48 +0000127static void r5(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 rast->addLayer(p);
129
130 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000131 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 rast->addLayer(p);
133}
134
reed@google.comf2183392011-04-22 14:10:48 +0000135static void r6(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 rast->addLayer(p);
reed@google.com82065d62011-02-07 15:30:46 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 p.setAntiAlias(false);
139 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
140 r5(rast2, p);
141 p.setRasterizer(rast2)->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000142 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 rast->addLayer(p);
144}
145
146#include "Sk2DPathEffect.h"
147
reed@google.com18dc4772011-08-09 18:47:40 +0000148static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
149 SkPath path;
150 path.addCircle(0, 0, radius);
151 return new SkPath2DPathEffect(matrix, path);
152}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
reed@google.comf2183392011-04-22 14:10:48 +0000154static void r7(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 SkMatrix lattice;
156 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
157 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000158 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 rast->addLayer(p);
160}
161
reed@google.comf2183392011-04-22 14:10:48 +0000162static void r8(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 rast->addLayer(p);
reed@google.com82065d62011-02-07 15:30:46 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 SkMatrix lattice;
166 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
167 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000168 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000169 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 rast->addLayer(p);
171
172 p.setPathEffect(NULL);
173 p.setXfermode(NULL);
174 p.setStyle(SkPaint::kStroke_Style);
175 p.setStrokeWidth(SK_Scalar1);
176 rast->addLayer(p);
177}
178
179class Line2DPathEffect : public Sk2DPathEffect {
180public:
181 Line2DPathEffect(SkScalar width, const SkMatrix& matrix)
182 : Sk2DPathEffect(matrix), fWidth(width) {}
183
reed@google.comf2183392011-04-22 14:10:48 +0000184 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width) {
185 if (this->INHERITED::filterPath(dst, src, width)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 *width = fWidth;
187 return true;
188 }
189 return false;
190 }
reed@google.com82065d62011-02-07 15:30:46 +0000191
djsollen@google.comba28d032012-03-26 17:57:35 +0000192 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Line2DPathEffect)
reed@google.comf2183392011-04-22 14:10:48 +0000193
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194protected:
reed@google.comf2183392011-04-22 14:10:48 +0000195 virtual void nextSpan(int u, int v, int ucount, SkPath* dst) {
196 if (ucount > 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 SkPoint src[2], dstP[2];
198
199 src[0].set(SkIntToScalar(u) + SK_ScalarHalf,
200 SkIntToScalar(v) + SK_ScalarHalf);
201 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf,
202 SkIntToScalar(v) + SK_ScalarHalf);
203 this->getMatrix().mapPoints(dstP, src, 2);
reed@google.com82065d62011-02-07 15:30:46 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 dst->moveTo(dstP[0]);
206 dst->lineTo(dstP[1]);
207 }
208 }
reed@google.com82065d62011-02-07 15:30:46 +0000209
djsollen@google.com54924242012-03-29 15:18:04 +0000210 Line2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211 fWidth = buffer.readScalar();
212 }
djsollen@google.com54924242012-03-29 15:18:04 +0000213 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
214 this->INHERITED::flatten(buffer);
215 buffer.writeScalar(fWidth);
216 }
reed@google.com82065d62011-02-07 15:30:46 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218private:
219 SkScalar fWidth;
220
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 typedef Sk2DPathEffect INHERITED;
222};
223
reed@google.comf2183392011-04-22 14:10:48 +0000224static void r9(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 rast->addLayer(p);
reed@google.com82065d62011-02-07 15:30:46 +0000226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 SkMatrix lattice;
228 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
229 lattice.postRotate(SkIntToScalar(30), 0, 0);
230 p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000231 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232 rast->addLayer(p);
233
234 p.setPathEffect(NULL);
235 p.setXfermode(NULL);
236 p.setStyle(SkPaint::kStroke_Style);
237 p.setStrokeWidth(SK_Scalar1);
238 rast->addLayer(p);
239}
240
241typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
242
243static const raster_proc gRastProcs[] = {
244 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
245};
246
247static const struct {
248 SkColor fMul, fAdd;
249} gLightingColors[] = {
250 { 0x808080, 0x800000 }, // general case
251 { 0x707070, 0x707070 }, // no-pin case
252 { 0xFFFFFF, 0x800000 }, // just-add case
253 { 0x808080, 0x000000 }, // just-mul case
254 { 0xFFFFFF, 0x000000 } // identity case
255};
256
257#include "SkXfermode.h"
258
reed@google.comf2183392011-04-22 14:10:48 +0000259static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 raster_proc proc = gRastProcs[index];
261 if (proc)
262 {
263 SkPaint p;
264 SkLayerRasterizer* rast = new SkLayerRasterizer;
265
266 p.setAntiAlias(true);
267 proc(rast, p);
268 paint->setRasterizer(rast)->unref();
269 }
270
271#if 0
272 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000273 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274#endif
275 paint->setColor(SK_ColorBLUE);
276}
277
278static int gRastIndex;
279
reed@google.comf2183392011-04-22 14:10:48 +0000280class TextEffectView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000281 SkTypeface* fFace;
282public:
reed@google.comf2183392011-04-22 14:10:48 +0000283 TextEffectView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb");
285 }
reed@google.com82065d62011-02-07 15:30:46 +0000286
reed@google.comf2183392011-04-22 14:10:48 +0000287 virtual ~TextEffectView() {
reed@google.com82065d62011-02-07 15:30:46 +0000288 SkSafeUnref(fFace);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 }
290
291protected:
292 // overrides from SkEventSink
reed@google.comf2183392011-04-22 14:10:48 +0000293 virtual bool onQuery(SkEvent* evt) {
294 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 SampleCode::TitleR(evt, "Text Effects");
296 return true;
297 }
298 return this->INHERITED::onQuery(evt);
299 }
reed@google.com82065d62011-02-07 15:30:46 +0000300
reed@google.comf2183392011-04-22 14:10:48 +0000301 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 canvas->save();
303// canvas->scale(SK_Scalar1*2, SK_Scalar1*2, 0, 0);
304
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 SkPaint paint;
reed@google.com82065d62011-02-07 15:30:46 +0000306
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 paint.setAntiAlias(true);
reed@android.com0bb6d062010-05-17 14:50:04 +0000308 paint.setTextSize(SkIntToScalar(56));
reed@android.com069b8272009-03-04 15:31:48 +0000309 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
310 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311
reed@android.com0bb6d062010-05-17 14:50:04 +0000312 SkScalar x = SkIntToScalar(20);
313 SkScalar y = paint.getTextSize();
314
315 SkString str("TextEffects");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316
317 paint.setTypeface(fFace);
reed@google.com82065d62011-02-07 15:30:46 +0000318
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000319 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 apply_shader(&paint, i);
reed@google.com82065d62011-02-07 15:30:46 +0000321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 // paint.setMaskFilter(NULL);
323 // paint.setColor(SK_ColorBLACK);
324
reed@google.comf2183392011-04-22 14:10:48 +0000325#if 1
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 int index = i % SK_ARRAY_COUNT(gLightingColors);
327 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
328 gLightingColors[index].fMul,
329 gLightingColors[index].fAdd))->unref();
330#endif
reed@google.com82065d62011-02-07 15:30:46 +0000331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 canvas->drawText(str.c_str(), str.size(), x, y, paint);
333
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 y += paint.getFontSpacing();
335 }
336
337 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 }
reed@google.com82065d62011-02-07 15:30:46 +0000339
reed@google.comf2183392011-04-22 14:10:48 +0000340 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 gRastIndex = (gRastIndex + 1) % SK_ARRAY_COUNT(gRastProcs);
342 this->inval(NULL);
343
344 return this->INHERITED::onFindClickHandler(x, y);
345 }
reed@google.com82065d62011-02-07 15:30:46 +0000346
reed@google.comf2183392011-04-22 14:10:48 +0000347 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 return this->INHERITED::onClick(click);
349 }
reed@google.com82065d62011-02-07 15:30:46 +0000350
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351private:
reed@google.comf2183392011-04-22 14:10:48 +0000352 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353};
354
355//////////////////////////////////////////////////////////////////////////////
356
357static SkView* MyFactory() { return new TextEffectView; }
358static SkViewRegister reg(MyFactory);
359