blob: 2aea5b021ae3afe41adbb702053040baea910687 [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
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000213 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width) {
214 if (this->INHERITED::filterPath(dst, src, width)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 *width = fWidth;
216 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++)
389 pos[index].set(SkIntToScalar(index * 10), SkIntToScalar(index * 2));
390 SkTDArray<SkPoint>(pos2);
391 pos2.setCount(asciiLength);
392 for (index = 0; index < asciiLength; index++)
393 pos2[index].set(SkIntToScalar(index * 10), SkIntToScalar(20));
394
395 // shaders
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000396 SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
398 SkScalar* linearPos = NULL;
399 int linearCount = 2;
400 SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
401 SkUnitMapper* linearMapper = new SkDiscreteMapper(3);
402 SkAutoUnref unmapLinearMapper(linearMapper);
403 SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
404 linearColors, linearPos, linearCount, linearMode, linearMapper);
405
406 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
407 SkScalar radialRadius = SkIntToScalar(25);
408 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
409 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
410 int radialCount = 3;
411 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
412 SkUnitMapper* radialMapper = new SkCosineMapper();
413 SkAutoUnref unmapRadialMapper(radialMapper);
414 SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
415 radialRadius, radialColors, radialPos, radialCount,
416 radialMode, radialMapper);
417
418 SkTransparentShader* transparentShader = new SkTransparentShader();
419 SkEmbossMaskFilter::Light light;
420 light.fDirection[0] = SK_Scalar1/2;
421 light.fDirection[1] = SK_Scalar1/2;
422 light.fDirection[2] = SK_Scalar1/3;
423 light.fAmbient = 0x48;
424 light.fSpecular = 0x80;
425 SkScalar radius = SkIntToScalar(12)/5;
426 SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(light,
427 radius);
428
reed@android.com048522d2009-06-23 12:19:41 +0000429 SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430 SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
431 0xff89bc45, 0xff112233);
432
433 canvas->save();
434 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
435 paint.setFlags(SkPaint::kAntiAlias_Flag | SkPaint::kFilterBitmap_Flag);
436 // !!! draw through a clip
437 paint.setColor(SK_ColorLTGRAY);
438 paint.setStyle(SkPaint::kFill_Style);
439 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
440 canvas->clipRect(clip);
441 paint.setShader(SkShader::CreateBitmapShader(fTx,
442 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
443 canvas->drawPaint(paint);
444 canvas->save();
445
446 // line (exercises xfermode, colorShader, colorFilter, filterShader)
447 paint.setColor(SK_ColorGREEN);
448 paint.setStrokeWidth(SkIntToScalar(10));
449 paint.setStyle(SkPaint::kStroke_Style);
450 paint.setXfermode(xfermode)->unref();
451 paint.setColorFilter(lightingFilter)->unref();
452 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
453 paint.setXfermode(NULL);
454 paint.setColorFilter(NULL);
455
456 // rectangle
457 paint.setStyle(SkPaint::kFill_Style);
458 canvas->translate(SkIntToScalar(50), 0);
459 paint.setColor(SK_ColorYELLOW);
460 paint.setShader(linear)->unref();
461 paint.setPathEffect(pathEffectTest())->unref();
462 canvas->drawRect(rect, paint);
463 paint.setPathEffect(NULL);
464
465 // circle w/ emboss & transparent (exercises 3dshader)
466 canvas->translate(SkIntToScalar(50), 0);
467 paint.setMaskFilter(embossFilter)->unref();
468 canvas->drawOval(rect, paint);
469 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
470 paint.setShader(transparentShader)->unref();
471 canvas->drawOval(rect, paint);
472 canvas->translate(0, SkIntToScalar(-10));
473
474 // path
475 canvas->translate(SkIntToScalar(50), 0);
476 paint.setColor(SK_ColorRED);
477 paint.setStyle(SkPaint::kStroke_Style);
478 paint.setStrokeWidth(SkIntToScalar(5));
479 paint.setShader(radial)->unref();
480 paint.setMaskFilter(NULL);
481 canvas->drawPath(path, paint);
482
483 paint.setShader(NULL);
484 // bitmap, sprite
485 canvas->translate(SkIntToScalar(50), 0);
486 paint.setStyle(SkPaint::kFill_Style);
487 canvas->drawBitmap(fBug, left, top, &paint);
488 canvas->translate(SkIntToScalar(30), 0);
489 canvas->drawSprite(fTb,
490 SkScalarRound(canvas->getTotalMatrix().getTranslateX()),
491 spriteOffset + 10, &paint);
492
493 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
494 paint.setShader(shaderTest())->unref(); // test compose shader
495 canvas->drawRect(rect2, paint);
496 paint.setShader(NULL);
497
498 canvas->restore();
499 // text
500 canvas->translate(0, SkIntToScalar(60));
501 canvas->save();
502 paint.setColor(SK_ColorGRAY);
503 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
504 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
505
506 canvas->translate(SkIntToScalar(50), 0);
507 paint.setColor(SK_ColorCYAN);
508 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
509
510 canvas->translate(SkIntToScalar(30), 0);
511 paint.setColor(SK_ColorMAGENTA);
512 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
513 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
514 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
515 canvas->translate(0, SkIntToScalar(20));
516 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
517 canvas->restore();
518
519 canvas->translate(0, SkIntToScalar(60));
520 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
521 canvas->restore();
522 }
523
524 /*
525./SkColorFilter.h:25:class SkColorFilter : public SkFlattenable { -- abstract
526 static SkColorFilter* CreatXfermodeFilter() *** untested ***
527 static SkColorFilter* CreatePorterDuffFilter() *** untested ***
528 static SkColorFilter* CreateLightingFilter() -- tested
529./SkDrawLooper.h:9:class SkDrawLooper : public SkFlattenable { -- virtually abstract
530 ./SkBlurDrawLooper.h:9:class SkBlurDrawLooper : public SkDrawLooper { *** untested ***
531./SkMaskFilter.h:41:class SkMaskFilter : public SkFlattenable { -- abstract chmod +w .h
532 ./SkEmbossMaskFilter.h:27:class SkEmbossMaskFilter : public SkMaskFilter { -- tested
533./SkPathEffect.h:33:class SkPathEffect : public SkFlattenable { -- abstract
534 ./Sk1DPathEffect.h:27:class Sk1DPathEffect : public SkPathEffect { -- abstract
535 ./Sk1DPathEffect.h:48:class SkPath1DPathEffect : public Sk1DPathEffect { -- tested
536 ./Sk2DPathEffect.h:25:class Sk2DPathEffect : public SkPathEffect { *** untested ***
537 ./SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { *** untested ***
538 ./SkDashPathEffect.h:27:class SkDashPathEffect : public SkPathEffect {
539 ./SkDiscretePathEffect.h:27:class SkDiscretePathEffect : public SkPathEffect {
540 ./SkPaint.h:760:class SkStrokePathEffect : public SkPathEffect {
541 ./SkPathEffect.h:58:class SkPairPathEffect : public SkPathEffect {
542 ./SkPathEffect.h:78:class SkComposePathEffect : public SkPairPathEffect {
543 ./SkPathEffect.h:114:class SkSumPathEffect : public SkPairPathEffect {
544./SkRasterizer.h:29:class SkRasterizer : public SkFlattenable {
545 ./SkLayerRasterizer.h:27:class SkLayerRasterizer : public SkRasterizer {
546./SkShader.h:36:class SkShader : public SkFlattenable {
547 ./SkColorFilter.h:59:class SkFilterShader : public SkShader {
548 ./SkColorShader.h:26:class SkColorShader : public SkShader {
549 ./SkShaderExtras.h:31:class SkComposeShader : public SkShader {
550 ./SkTransparentShader.h:23:class SkTransparentShader : public SkShader {
551./SkUnitMapper.h:24:class SkUnitMapper : public SkFlattenable {
552 ./SkUnitMapper.h:33:class SkDiscreteMapper : public SkUnitMapper {
553 ./SkUnitMapper.h:51:class SkFlipCosineMapper : public SkUnitMapper {
554./SkXfermode.h:32:class SkXfermode : public SkFlattenable {
555 ./SkAvoidXfermode.h:28:class SkAvoidXfermode : public SkXfermode { *** not done *** chmod +w .h .cpp
556 ./SkXfermode.h:54:class SkProcXfermode : public SkXfermode {
557 */
558
559 /*
560./SkBlurMaskFilter.h:25:class SkBlurMaskFilter {
561 chmod +w SkBlurMaskFilter.cpp
562./SkGradientShader.h:30:class SkGradientShader {
563 */
564 // save layer, bounder, looper
565 // matrix
566 // clip /path/region
567 // bitmap proc shader ?
568
569/* untested:
570SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect {
571*/
572
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000573 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 fClickPt.set(x, y);
575 this->inval(NULL);
576 return this->INHERITED::onFindClickHandler(x, y);
577 }
578
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000579 SkPathEffect* pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
581 SkScalar gPhase = 0;
582 SkPath path;
583 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
584 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
585 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
586 path.close();
587 path.offset(SkIntToScalar(-6), 0);
588 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12),
589 gPhase, SkPath1DPathEffect::kRotate_Style);
590 SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2),
591 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
592 SkPathEffect* result = new SkComposePathEffect(outer, inner);
593 outer->unref();
594 inner->unref();
595 return result;
596 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000597
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000598 SkShader* shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000599 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000600 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
601 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
602 2, SkShader::kClamp_TileMode);
603 pts[1].set(0, SkIntToScalar(100));
604 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
605 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
606 2, SkShader::kClamp_TileMode);
reed@android.com048522d2009-06-23 12:19:41 +0000607 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608 SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
609 shaderA->unref();
610 shaderB->unref();
611 mode->unref();
612 return result;
613 }
614
615 virtual void startTest() {
616 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
617 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
618 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
619 }
620
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000621 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000622 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623 drawOneRaster(canvas);
624 }
625
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000626 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628
629 SkScalar x = SkIntToScalar(20);
630 SkScalar y = SkIntToScalar(40);
631 SkPaint paint;
632
633 paint.setAntiAlias(true);
634 paint.setTextSize(SkIntToScalar(48));
reed@android.comaa5a7db2009-05-27 01:20:10 +0000635 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
636 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637
638 SkString str("GOOGLE");
639
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000640 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000641 apply_shader(&paint, i);
642
643 // paint.setMaskFilter(NULL);
644 // paint.setColor(SK_ColorBLACK);
645
646#if 01
647 int index = i % SK_ARRAY_COUNT(gLightingColors);
648 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
649 gLightingColors[index].fMul,
650 gLightingColors[index].fAdd))->unref();
651#endif
652
653 canvas->drawText(str.c_str(), str.size(), x, y, paint);
654 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
655 paint.setStyle(SkPaint::kStroke_Style);
656 canvas->drawOval(oval, paint);
657 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658
659 y += paint.getFontSpacing();
660 }
661
662 canvas->restore();
663
reed@android.com6b82d1a2009-06-03 02:35:01 +0000664 if (1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000665 SkAvoidXfermode mode(SK_ColorWHITE, 0xFF,
666 SkAvoidXfermode::kTargetColor_Mode);
667 SkPaint paint;
668 x += SkIntToScalar(20);
669 SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
670 paint.setXfermode(&mode);
671 paint.setColor(SK_ColorGREEN);
672 paint.setAntiAlias(true);
673 canvas->drawOval(r, paint);
674 }
675 }
676
677private:
678 SkPoint fClickPt;
679 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000680 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000681};
682
683//////////////////////////////////////////////////////////////////////////////
684
685static SkView* MyFactory() { return new DemoView; }
686static SkViewRegister reg(MyFactory);
687