blob: 4ab3b0e4535db4aeae2ec89ed29a60c9aa753d43 [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 "SkCanvas.h"
10#include "SkView.h"
11#include "Sk1DPathEffect.h"
12#include "Sk2DPathEffect.h"
13#include "SkAvoidXfermode.h"
14#include "SkBlurMaskFilter.h"
15#include "SkColorFilter.h"
16#include "SkColorPriv.h"
17#include "SkCornerPathEffect.h"
18#include "SkDashPathEffect.h"
19#include "SkDiscretePathEffect.h"
20#include "SkEmbossMaskFilter.h"
21#include "SkGradientShader.h"
22#include "SkImageDecoder.h"
23#include "SkLayerRasterizer.h"
24#include "SkMath.h"
25#include "SkPath.h"
26#include "SkRegion.h"
27#include "SkShader.h"
reed@android.comaa5a7db2009-05-27 01:20:10 +000028#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#include "SkCornerPathEffect.h"
30#include "SkPathMeasure.h"
31#include "SkPicture.h"
32#include "SkRandom.h"
33#include "SkTransparentShader.h"
34#include "SkTypeface.h"
35#include "SkUnitMappers.h"
36#include "SkUtils.h"
37#include "SkXfermode.h"
38
39#include <math.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000041static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 unsigned r = SkGetPackedR32(c);
43 unsigned g = SkGetPackedG32(c);
44 unsigned b = SkGetPackedB32(c);
45
reed@android.comf2b98d62010-12-20 18:26:13 +000046 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
48 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
49}
50
51class SkGrayScaleColorFilter : public SkColorFilter {
52public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000053 virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 for (int i = 0; i < count; i++)
55 result[i] = rgb2gray(src[i]);
56 }
57};
58
59class SkChannelMaskColorFilter : public SkColorFilter {
60public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000061 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
63 }
64
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000065 virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 SkPMColor mask = fMask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000067 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 result[i] = src[i] & mask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000069 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 }
71
72private:
73 SkPMColor fMask;
74};
75
76///////////////////////////////////////////////////////////
77
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000078static void r0(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
80 SkBlurMaskFilter::kNormal_BlurStyle))->unref();
81 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
82
83 p.setMaskFilter(NULL);
84 p.setStyle(SkPaint::kStroke_Style);
85 p.setStrokeWidth(SK_Scalar1);
86 rast->addLayer(p);
87
88 p.setAlpha(0x11);
89 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000090 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 rast->addLayer(p);
92}
93
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000094static void r1(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 rast->addLayer(p);
96
97 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +000098 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 p.setStyle(SkPaint::kStroke_Style);
100 p.setStrokeWidth(SK_Scalar1*2);
101 rast->addLayer(p);
102}
103
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000104static void r2(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 p.setStyle(SkPaint::kStrokeAndFill_Style);
106 p.setStrokeWidth(SK_Scalar1*4);
107 rast->addLayer(p);
108
109 p.setStyle(SkPaint::kStroke_Style);
110 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +0000111 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 rast->addLayer(p);
113}
114
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000115static void r3(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 p.setStyle(SkPaint::kStroke_Style);
117 p.setStrokeWidth(SK_Scalar1*3);
118 rast->addLayer(p);
119
120 p.setAlpha(0x20);
121 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +0000122 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 rast->addLayer(p);
124}
125
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000126static void r4(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 p.setAlpha(0x60);
128 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
129
130 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +0000131 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
133
134 p.setXfermode(NULL);
135 rast->addLayer(p);
136}
137
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000138static void r5(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 rast->addLayer(p);
140
141 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000142 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 rast->addLayer(p);
144}
145
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000146static void r6(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 rast->addLayer(p);
148
149 p.setAntiAlias(false);
150 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
151 r5(rast2, p);
152 p.setRasterizer(rast2)->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000153 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 rast->addLayer(p);
155}
156
157class Dot2DPathEffect : public Sk2DPathEffect {
158public:
159 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix)
160 : Sk2DPathEffect(matrix), fRadius(radius) {}
161
djsollen@google.comba28d032012-03-26 17:57:35 +0000162 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163
164protected:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000165 virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 dst->addCircle(loc.fX, loc.fY, fRadius);
167 }
168
djsollen@google.com54924242012-03-29 15:18:04 +0000169 Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 fRadius = buffer.readScalar();
171 }
djsollen@google.com54924242012-03-29 15:18:04 +0000172 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
173 this->INHERITED::flatten(buffer);
174 buffer.writeScalar(fRadius);
175 }
176
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177private:
178 SkScalar fRadius;
179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 typedef Sk2DPathEffect INHERITED;
181};
182
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000183static void r7(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 SkMatrix lattice;
185 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
186 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
187 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref();
188 rast->addLayer(p);
189}
190
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000191static void r8(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 rast->addLayer(p);
193
194 SkMatrix lattice;
195 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
196 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
197 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000198 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 rast->addLayer(p);
200
201 p.setPathEffect(NULL);
202 p.setXfermode(NULL);
203 p.setStyle(SkPaint::kStroke_Style);
204 p.setStrokeWidth(SK_Scalar1);
205 rast->addLayer(p);
206}
207
208class Line2DPathEffect : public Sk2DPathEffect {
209public:
210 Line2DPathEffect(SkScalar width, const SkMatrix& matrix)
211 : Sk2DPathEffect(matrix), fWidth(width) {}
212
reed@google.comfd4be262012-05-25 01:04:12 +0000213 virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec) SK_OVERRIDE {
214 if (this->INHERITED::filterPath(dst, src, rec)) {
215 rec->setStrokeStyle(fWidth);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 return true;
217 }
218 return false;
219 }
220
djsollen@google.comba28d032012-03-26 17:57:35 +0000221 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Line2DPathEffect)
222
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223protected:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000224 virtual void nextSpan(int u, int v, int ucount, SkPath* dst) {
225 if (ucount > 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 SkPoint src[2], dstP[2];
227
228 src[0].set(SkIntToScalar(u) + SK_ScalarHalf,
229 SkIntToScalar(v) + SK_ScalarHalf);
230 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf,
231 SkIntToScalar(v) + SK_ScalarHalf);
232 this->getMatrix().mapPoints(dstP, src, 2);
233
234 dst->moveTo(dstP[0]);
235 dst->lineTo(dstP[1]);
236 }
237 }
238
djsollen@google.com54924242012-03-29 15:18:04 +0000239 Line2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 fWidth = buffer.readScalar();
241 }
djsollen@google.com54924242012-03-29 15:18:04 +0000242 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
243 this->INHERITED::flatten(buffer);
244 buffer.writeScalar(fWidth);
245 }
246
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247private:
248 SkScalar fWidth;
249
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 typedef Sk2DPathEffect INHERITED;
251};
252
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000253static void r9(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 rast->addLayer(p);
255
256 SkMatrix lattice;
257 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
258 lattice.postRotate(SkIntToScalar(30), 0, 0);
259 p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000260 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 rast->addLayer(p);
262
263 p.setPathEffect(NULL);
264 p.setXfermode(NULL);
265 p.setStyle(SkPaint::kStroke_Style);
266 p.setStrokeWidth(SK_Scalar1);
267 rast->addLayer(p);
268}
269
270typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
271
272static const raster_proc gRastProcs[] = {
273 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
274};
275
276static const struct {
277 SkColor fMul, fAdd;
278} gLightingColors[] = {
279 { 0x808080, 0x800000 }, // general case
280 { 0x707070, 0x707070 }, // no-pin case
281 { 0xFFFFFF, 0x800000 }, // just-add case
282 { 0x808080, 0x000000 }, // just-mul case
283 { 0xFFFFFF, 0x000000 } // identity case
284};
285
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000286static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 raster_proc proc = gRastProcs[index];
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000288 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 SkPaint p;
290 SkLayerRasterizer* rast = new SkLayerRasterizer;
291
292 p.setAntiAlias(true);
293 proc(rast, p);
294 paint->setRasterizer(rast)->unref();
295 }
296
297#if 1
298 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
299 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
300 paint->setColor(SK_ColorBLUE);
301#endif
302}
303
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000304class DemoView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305public:
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000306 DemoView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307
308protected:
309 // overrides from SkEventSink
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000310 virtual bool onQuery(SkEvent* evt) {
311 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 SampleCode::TitleR(evt, "Demo");
313 return true;
314 }
315 return this->INHERITED::onQuery(evt);
316 }
317
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000318 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 return this->INHERITED::onClick(click);
320 }
321
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000322 void makePath(SkPath& path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
324 SkPath::kCCW_Direction);
325 for (int index = 0; index < 10; index++) {
326 SkScalar x = SkFloatToScalar(cos(index / 10.0f * 2 * 3.1415925358f));
327 SkScalar y = SkFloatToScalar(sin(index / 10.0f * 2 * 3.1415925358f));
328 x *= index & 1 ? 7 : 14;
329 y *= index & 1 ? 7 : 14;
330 x += SkIntToScalar(20);
331 y += SkIntToScalar(20);
332 if (index == 0)
333 path.moveTo(x, y);
334 else
335 path.lineTo(x, y);
336 }
337 path.close();
338 }
339
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000340 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 canvas->save();
342 drawPicture(canvas, 0);
343 canvas->restore();
344
345 {
346 SkPicture picture;
347 SkCanvas* record = picture.beginRecording(320, 480);
348 drawPicture(record, 120);
349 canvas->translate(0, SkIntToScalar(120));
350
351 SkRect clip;
352 clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
353 do {
354 canvas->save();
355 canvas->clipRect(clip);
356 picture.draw(canvas);
357 canvas->restore();
358 if (clip.fRight < SkIntToScalar(320))
359 clip.offset(SkIntToScalar(160), 0);
360 else if (clip.fBottom < SkIntToScalar(480))
361 clip.offset(-SkIntToScalar(320), SkIntToScalar(160));
362 else
363 break;
364 } while (true);
365 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 }
367
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000368 void drawPicture(SkCanvas* canvas, int spriteOffset) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 SkMatrix matrix; matrix.reset();
370 SkPaint paint;
371 SkPath path;
372 SkPoint start = {0, 0};
373 SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) };
374 SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) };
375 SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) };
376 SkScalar left = 0, top = 0, x = 0, y = 0;
reed@google.com261b8e22011-04-14 17:53:24 +0000377 size_t index;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378
379 char ascii[] = "ascii...";
380 size_t asciiLength = sizeof(ascii) - 1;
381 char utf8[] = "utf8" "\xe2\x80\xa6";
382 short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 };
383 short utf16simple[] = {'u', 't', 'f', '1', '6', '!' };
384
385 makePath(path);
386 SkTDArray<SkPoint>(pos);
387 pos.setCount(asciiLength);
388 for (index = 0; index < asciiLength; index++)
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000389 pos[index].set(SkIntToScalar((unsigned int)index * 10),
390 SkIntToScalar((unsigned int)index * 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 SkTDArray<SkPoint>(pos2);
392 pos2.setCount(asciiLength);
393 for (index = 0; index < asciiLength; index++)
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000394 pos2[index].set(SkIntToScalar((unsigned int)index * 10),
395 SkIntToScalar(20));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396
397 // shaders
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000398 SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
400 SkScalar* linearPos = NULL;
401 int linearCount = 2;
402 SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
403 SkUnitMapper* linearMapper = new SkDiscreteMapper(3);
404 SkAutoUnref unmapLinearMapper(linearMapper);
405 SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
406 linearColors, linearPos, linearCount, linearMode, linearMapper);
407
408 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
409 SkScalar radialRadius = SkIntToScalar(25);
410 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
411 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
412 int radialCount = 3;
413 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
414 SkUnitMapper* radialMapper = new SkCosineMapper();
415 SkAutoUnref unmapRadialMapper(radialMapper);
416 SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
417 radialRadius, radialColors, radialPos, radialCount,
418 radialMode, radialMapper);
419
420 SkTransparentShader* transparentShader = new SkTransparentShader();
421 SkEmbossMaskFilter::Light light;
422 light.fDirection[0] = SK_Scalar1/2;
423 light.fDirection[1] = SK_Scalar1/2;
424 light.fDirection[2] = SK_Scalar1/3;
425 light.fAmbient = 0x48;
426 light.fSpecular = 0x80;
427 SkScalar radius = SkIntToScalar(12)/5;
428 SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(light,
429 radius);
430
reed@android.com048522d2009-06-23 12:19:41 +0000431 SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432 SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
433 0xff89bc45, 0xff112233);
434
435 canvas->save();
436 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
437 paint.setFlags(SkPaint::kAntiAlias_Flag | SkPaint::kFilterBitmap_Flag);
438 // !!! draw through a clip
439 paint.setColor(SK_ColorLTGRAY);
440 paint.setStyle(SkPaint::kFill_Style);
441 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
442 canvas->clipRect(clip);
443 paint.setShader(SkShader::CreateBitmapShader(fTx,
444 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
445 canvas->drawPaint(paint);
446 canvas->save();
447
448 // line (exercises xfermode, colorShader, colorFilter, filterShader)
449 paint.setColor(SK_ColorGREEN);
450 paint.setStrokeWidth(SkIntToScalar(10));
451 paint.setStyle(SkPaint::kStroke_Style);
452 paint.setXfermode(xfermode)->unref();
453 paint.setColorFilter(lightingFilter)->unref();
454 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
455 paint.setXfermode(NULL);
456 paint.setColorFilter(NULL);
457
458 // rectangle
459 paint.setStyle(SkPaint::kFill_Style);
460 canvas->translate(SkIntToScalar(50), 0);
461 paint.setColor(SK_ColorYELLOW);
462 paint.setShader(linear)->unref();
463 paint.setPathEffect(pathEffectTest())->unref();
464 canvas->drawRect(rect, paint);
465 paint.setPathEffect(NULL);
466
467 // circle w/ emboss & transparent (exercises 3dshader)
468 canvas->translate(SkIntToScalar(50), 0);
469 paint.setMaskFilter(embossFilter)->unref();
470 canvas->drawOval(rect, paint);
471 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
472 paint.setShader(transparentShader)->unref();
473 canvas->drawOval(rect, paint);
474 canvas->translate(0, SkIntToScalar(-10));
475
476 // path
477 canvas->translate(SkIntToScalar(50), 0);
478 paint.setColor(SK_ColorRED);
479 paint.setStyle(SkPaint::kStroke_Style);
480 paint.setStrokeWidth(SkIntToScalar(5));
481 paint.setShader(radial)->unref();
482 paint.setMaskFilter(NULL);
483 canvas->drawPath(path, paint);
484
485 paint.setShader(NULL);
486 // bitmap, sprite
487 canvas->translate(SkIntToScalar(50), 0);
488 paint.setStyle(SkPaint::kFill_Style);
489 canvas->drawBitmap(fBug, left, top, &paint);
490 canvas->translate(SkIntToScalar(30), 0);
491 canvas->drawSprite(fTb,
492 SkScalarRound(canvas->getTotalMatrix().getTranslateX()),
493 spriteOffset + 10, &paint);
494
495 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
496 paint.setShader(shaderTest())->unref(); // test compose shader
497 canvas->drawRect(rect2, paint);
498 paint.setShader(NULL);
499
500 canvas->restore();
501 // text
502 canvas->translate(0, SkIntToScalar(60));
503 canvas->save();
504 paint.setColor(SK_ColorGRAY);
505 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
506 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
507
508 canvas->translate(SkIntToScalar(50), 0);
509 paint.setColor(SK_ColorCYAN);
510 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
511
512 canvas->translate(SkIntToScalar(30), 0);
513 paint.setColor(SK_ColorMAGENTA);
514 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
515 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
516 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
517 canvas->translate(0, SkIntToScalar(20));
518 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
519 canvas->restore();
520
521 canvas->translate(0, SkIntToScalar(60));
522 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
523 canvas->restore();
524 }
525
526 /*
527./SkColorFilter.h:25:class SkColorFilter : public SkFlattenable { -- abstract
528 static SkColorFilter* CreatXfermodeFilter() *** untested ***
529 static SkColorFilter* CreatePorterDuffFilter() *** untested ***
530 static SkColorFilter* CreateLightingFilter() -- tested
531./SkDrawLooper.h:9:class SkDrawLooper : public SkFlattenable { -- virtually abstract
532 ./SkBlurDrawLooper.h:9:class SkBlurDrawLooper : public SkDrawLooper { *** untested ***
533./SkMaskFilter.h:41:class SkMaskFilter : public SkFlattenable { -- abstract chmod +w .h
534 ./SkEmbossMaskFilter.h:27:class SkEmbossMaskFilter : public SkMaskFilter { -- tested
535./SkPathEffect.h:33:class SkPathEffect : public SkFlattenable { -- abstract
536 ./Sk1DPathEffect.h:27:class Sk1DPathEffect : public SkPathEffect { -- abstract
537 ./Sk1DPathEffect.h:48:class SkPath1DPathEffect : public Sk1DPathEffect { -- tested
538 ./Sk2DPathEffect.h:25:class Sk2DPathEffect : public SkPathEffect { *** untested ***
539 ./SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { *** untested ***
540 ./SkDashPathEffect.h:27:class SkDashPathEffect : public SkPathEffect {
541 ./SkDiscretePathEffect.h:27:class SkDiscretePathEffect : public SkPathEffect {
542 ./SkPaint.h:760:class SkStrokePathEffect : public SkPathEffect {
543 ./SkPathEffect.h:58:class SkPairPathEffect : public SkPathEffect {
544 ./SkPathEffect.h:78:class SkComposePathEffect : public SkPairPathEffect {
545 ./SkPathEffect.h:114:class SkSumPathEffect : public SkPairPathEffect {
546./SkRasterizer.h:29:class SkRasterizer : public SkFlattenable {
547 ./SkLayerRasterizer.h:27:class SkLayerRasterizer : public SkRasterizer {
548./SkShader.h:36:class SkShader : public SkFlattenable {
549 ./SkColorFilter.h:59:class SkFilterShader : public SkShader {
550 ./SkColorShader.h:26:class SkColorShader : public SkShader {
551 ./SkShaderExtras.h:31:class SkComposeShader : public SkShader {
552 ./SkTransparentShader.h:23:class SkTransparentShader : public SkShader {
553./SkUnitMapper.h:24:class SkUnitMapper : public SkFlattenable {
554 ./SkUnitMapper.h:33:class SkDiscreteMapper : public SkUnitMapper {
555 ./SkUnitMapper.h:51:class SkFlipCosineMapper : public SkUnitMapper {
556./SkXfermode.h:32:class SkXfermode : public SkFlattenable {
557 ./SkAvoidXfermode.h:28:class SkAvoidXfermode : public SkXfermode { *** not done *** chmod +w .h .cpp
558 ./SkXfermode.h:54:class SkProcXfermode : public SkXfermode {
559 */
560
561 /*
562./SkBlurMaskFilter.h:25:class SkBlurMaskFilter {
563 chmod +w SkBlurMaskFilter.cpp
564./SkGradientShader.h:30:class SkGradientShader {
565 */
566 // save layer, bounder, looper
567 // matrix
568 // clip /path/region
569 // bitmap proc shader ?
570
571/* untested:
572SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect {
573*/
574
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000575 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576 fClickPt.set(x, y);
577 this->inval(NULL);
578 return this->INHERITED::onFindClickHandler(x, y);
579 }
580
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000581 SkPathEffect* pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000582 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
583 SkScalar gPhase = 0;
584 SkPath path;
585 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
586 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
587 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
588 path.close();
589 path.offset(SkIntToScalar(-6), 0);
590 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12),
591 gPhase, SkPath1DPathEffect::kRotate_Style);
592 SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2),
593 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
594 SkPathEffect* result = new SkComposePathEffect(outer, inner);
595 outer->unref();
596 inner->unref();
597 return result;
598 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000599
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000600 SkShader* shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000601 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
603 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
604 2, SkShader::kClamp_TileMode);
605 pts[1].set(0, SkIntToScalar(100));
606 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
607 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
608 2, SkShader::kClamp_TileMode);
reed@android.com048522d2009-06-23 12:19:41 +0000609 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000610 SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
611 shaderA->unref();
612 shaderB->unref();
613 mode->unref();
614 return result;
615 }
616
617 virtual void startTest() {
618 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
619 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
620 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
621 }
622
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000623 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000624 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 drawOneRaster(canvas);
626 }
627
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000628 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630
631 SkScalar x = SkIntToScalar(20);
632 SkScalar y = SkIntToScalar(40);
633 SkPaint paint;
634
635 paint.setAntiAlias(true);
636 paint.setTextSize(SkIntToScalar(48));
reed@android.comaa5a7db2009-05-27 01:20:10 +0000637 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
638 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639
640 SkString str("GOOGLE");
641
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000642 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000643 apply_shader(&paint, i);
644
645 // paint.setMaskFilter(NULL);
646 // paint.setColor(SK_ColorBLACK);
647
648#if 01
649 int index = i % SK_ARRAY_COUNT(gLightingColors);
650 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
651 gLightingColors[index].fMul,
652 gLightingColors[index].fAdd))->unref();
653#endif
654
655 canvas->drawText(str.c_str(), str.size(), x, y, paint);
656 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
657 paint.setStyle(SkPaint::kStroke_Style);
658 canvas->drawOval(oval, paint);
659 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660
661 y += paint.getFontSpacing();
662 }
663
664 canvas->restore();
665
reed@android.com6b82d1a2009-06-03 02:35:01 +0000666 if (1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 SkAvoidXfermode mode(SK_ColorWHITE, 0xFF,
668 SkAvoidXfermode::kTargetColor_Mode);
669 SkPaint paint;
670 x += SkIntToScalar(20);
671 SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
672 paint.setXfermode(&mode);
673 paint.setColor(SK_ColorGREEN);
674 paint.setAntiAlias(true);
675 canvas->drawOval(r, paint);
676 }
677 }
678
679private:
680 SkPoint fClickPt;
681 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000682 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000683};
684
685//////////////////////////////////////////////////////////////////////////////
686
687static SkView* MyFactory() { return new DemoView; }
688static SkViewRegister reg(MyFactory);
689