blob: 303059be42cd4dc5c6b5cdadca805ab5cd27d9bc [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
scroggo@google.com6eb0d622012-06-25 20:32:12 +0000253SK_DEFINE_FLATTENABLE_REGISTRAR(Line2DPathEffect)
254
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000255static void r9(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 rast->addLayer(p);
257
258 SkMatrix lattice;
259 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
260 lattice.postRotate(SkIntToScalar(30), 0, 0);
261 p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000262 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 rast->addLayer(p);
264
265 p.setPathEffect(NULL);
266 p.setXfermode(NULL);
267 p.setStyle(SkPaint::kStroke_Style);
268 p.setStrokeWidth(SK_Scalar1);
269 rast->addLayer(p);
270}
271
272typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
273
274static const raster_proc gRastProcs[] = {
275 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
276};
277
278static const struct {
279 SkColor fMul, fAdd;
280} gLightingColors[] = {
281 { 0x808080, 0x800000 }, // general case
282 { 0x707070, 0x707070 }, // no-pin case
283 { 0xFFFFFF, 0x800000 }, // just-add case
284 { 0x808080, 0x000000 }, // just-mul case
285 { 0xFFFFFF, 0x000000 } // identity case
286};
287
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000288static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 raster_proc proc = gRastProcs[index];
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000290 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 SkPaint p;
292 SkLayerRasterizer* rast = new SkLayerRasterizer;
293
294 p.setAntiAlias(true);
295 proc(rast, p);
296 paint->setRasterizer(rast)->unref();
297 }
298
299#if 1
300 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
301 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
302 paint->setColor(SK_ColorBLUE);
303#endif
304}
305
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000306class DemoView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307public:
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000308 DemoView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309
310protected:
311 // overrides from SkEventSink
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000312 virtual bool onQuery(SkEvent* evt) {
313 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 SampleCode::TitleR(evt, "Demo");
315 return true;
316 }
317 return this->INHERITED::onQuery(evt);
318 }
319
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000320 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 return this->INHERITED::onClick(click);
322 }
323
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000324 void makePath(SkPath& path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
326 SkPath::kCCW_Direction);
327 for (int index = 0; index < 10; index++) {
caryclark@google.com02939ce2012-06-06 12:09:51 +0000328 SkScalar x = SkFloatToScalar((float) cos(index / 10.0f * 2 * 3.1415925358f));
329 SkScalar y = SkFloatToScalar((float) sin(index / 10.0f * 2 * 3.1415925358f));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 x *= index & 1 ? 7 : 14;
331 y *= index & 1 ? 7 : 14;
332 x += SkIntToScalar(20);
333 y += SkIntToScalar(20);
334 if (index == 0)
335 path.moveTo(x, y);
336 else
337 path.lineTo(x, y);
338 }
339 path.close();
340 }
341
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000342 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 canvas->save();
344 drawPicture(canvas, 0);
345 canvas->restore();
346
347 {
348 SkPicture picture;
349 SkCanvas* record = picture.beginRecording(320, 480);
350 drawPicture(record, 120);
351 canvas->translate(0, SkIntToScalar(120));
352
353 SkRect clip;
354 clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
355 do {
356 canvas->save();
357 canvas->clipRect(clip);
358 picture.draw(canvas);
359 canvas->restore();
360 if (clip.fRight < SkIntToScalar(320))
361 clip.offset(SkIntToScalar(160), 0);
362 else if (clip.fBottom < SkIntToScalar(480))
363 clip.offset(-SkIntToScalar(320), SkIntToScalar(160));
364 else
365 break;
366 } while (true);
367 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 }
369
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000370 void drawPicture(SkCanvas* canvas, int spriteOffset) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 SkMatrix matrix; matrix.reset();
372 SkPaint paint;
373 SkPath path;
374 SkPoint start = {0, 0};
375 SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) };
376 SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) };
377 SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) };
378 SkScalar left = 0, top = 0, x = 0, y = 0;
reed@google.com261b8e22011-04-14 17:53:24 +0000379 size_t index;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380
381 char ascii[] = "ascii...";
382 size_t asciiLength = sizeof(ascii) - 1;
383 char utf8[] = "utf8" "\xe2\x80\xa6";
384 short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 };
385 short utf16simple[] = {'u', 't', 'f', '1', '6', '!' };
386
387 makePath(path);
388 SkTDArray<SkPoint>(pos);
389 pos.setCount(asciiLength);
390 for (index = 0; index < asciiLength; index++)
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000391 pos[index].set(SkIntToScalar((unsigned int)index * 10),
392 SkIntToScalar((unsigned int)index * 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 SkTDArray<SkPoint>(pos2);
394 pos2.setCount(asciiLength);
395 for (index = 0; index < asciiLength; index++)
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000396 pos2[index].set(SkIntToScalar((unsigned int)index * 10),
397 SkIntToScalar(20));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398
399 // shaders
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000400 SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
402 SkScalar* linearPos = NULL;
403 int linearCount = 2;
404 SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
405 SkUnitMapper* linearMapper = new SkDiscreteMapper(3);
406 SkAutoUnref unmapLinearMapper(linearMapper);
407 SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
408 linearColors, linearPos, linearCount, linearMode, linearMapper);
409
410 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
411 SkScalar radialRadius = SkIntToScalar(25);
412 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
413 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
414 int radialCount = 3;
415 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
416 SkUnitMapper* radialMapper = new SkCosineMapper();
417 SkAutoUnref unmapRadialMapper(radialMapper);
418 SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
419 radialRadius, radialColors, radialPos, radialCount,
420 radialMode, radialMapper);
421
422 SkTransparentShader* transparentShader = new SkTransparentShader();
423 SkEmbossMaskFilter::Light light;
424 light.fDirection[0] = SK_Scalar1/2;
425 light.fDirection[1] = SK_Scalar1/2;
426 light.fDirection[2] = SK_Scalar1/3;
427 light.fAmbient = 0x48;
428 light.fSpecular = 0x80;
429 SkScalar radius = SkIntToScalar(12)/5;
430 SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(light,
431 radius);
432
reed@android.com048522d2009-06-23 12:19:41 +0000433 SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
435 0xff89bc45, 0xff112233);
436
437 canvas->save();
438 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
439 paint.setFlags(SkPaint::kAntiAlias_Flag | SkPaint::kFilterBitmap_Flag);
440 // !!! draw through a clip
441 paint.setColor(SK_ColorLTGRAY);
442 paint.setStyle(SkPaint::kFill_Style);
443 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
444 canvas->clipRect(clip);
445 paint.setShader(SkShader::CreateBitmapShader(fTx,
446 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
447 canvas->drawPaint(paint);
448 canvas->save();
449
450 // line (exercises xfermode, colorShader, colorFilter, filterShader)
451 paint.setColor(SK_ColorGREEN);
452 paint.setStrokeWidth(SkIntToScalar(10));
453 paint.setStyle(SkPaint::kStroke_Style);
454 paint.setXfermode(xfermode)->unref();
455 paint.setColorFilter(lightingFilter)->unref();
456 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
457 paint.setXfermode(NULL);
458 paint.setColorFilter(NULL);
459
460 // rectangle
461 paint.setStyle(SkPaint::kFill_Style);
462 canvas->translate(SkIntToScalar(50), 0);
463 paint.setColor(SK_ColorYELLOW);
464 paint.setShader(linear)->unref();
465 paint.setPathEffect(pathEffectTest())->unref();
466 canvas->drawRect(rect, paint);
467 paint.setPathEffect(NULL);
468
469 // circle w/ emboss & transparent (exercises 3dshader)
470 canvas->translate(SkIntToScalar(50), 0);
471 paint.setMaskFilter(embossFilter)->unref();
472 canvas->drawOval(rect, paint);
473 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
474 paint.setShader(transparentShader)->unref();
475 canvas->drawOval(rect, paint);
476 canvas->translate(0, SkIntToScalar(-10));
477
478 // path
479 canvas->translate(SkIntToScalar(50), 0);
480 paint.setColor(SK_ColorRED);
481 paint.setStyle(SkPaint::kStroke_Style);
482 paint.setStrokeWidth(SkIntToScalar(5));
483 paint.setShader(radial)->unref();
484 paint.setMaskFilter(NULL);
485 canvas->drawPath(path, paint);
486
487 paint.setShader(NULL);
488 // bitmap, sprite
489 canvas->translate(SkIntToScalar(50), 0);
490 paint.setStyle(SkPaint::kFill_Style);
491 canvas->drawBitmap(fBug, left, top, &paint);
492 canvas->translate(SkIntToScalar(30), 0);
493 canvas->drawSprite(fTb,
494 SkScalarRound(canvas->getTotalMatrix().getTranslateX()),
495 spriteOffset + 10, &paint);
496
497 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
498 paint.setShader(shaderTest())->unref(); // test compose shader
499 canvas->drawRect(rect2, paint);
500 paint.setShader(NULL);
501
502 canvas->restore();
503 // text
504 canvas->translate(0, SkIntToScalar(60));
505 canvas->save();
506 paint.setColor(SK_ColorGRAY);
507 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
508 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
509
510 canvas->translate(SkIntToScalar(50), 0);
511 paint.setColor(SK_ColorCYAN);
512 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
513
514 canvas->translate(SkIntToScalar(30), 0);
515 paint.setColor(SK_ColorMAGENTA);
516 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
517 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
518 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
519 canvas->translate(0, SkIntToScalar(20));
520 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
521 canvas->restore();
522
523 canvas->translate(0, SkIntToScalar(60));
524 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
525 canvas->restore();
526 }
527
528 /*
529./SkColorFilter.h:25:class SkColorFilter : public SkFlattenable { -- abstract
530 static SkColorFilter* CreatXfermodeFilter() *** untested ***
531 static SkColorFilter* CreatePorterDuffFilter() *** untested ***
532 static SkColorFilter* CreateLightingFilter() -- tested
533./SkDrawLooper.h:9:class SkDrawLooper : public SkFlattenable { -- virtually abstract
534 ./SkBlurDrawLooper.h:9:class SkBlurDrawLooper : public SkDrawLooper { *** untested ***
535./SkMaskFilter.h:41:class SkMaskFilter : public SkFlattenable { -- abstract chmod +w .h
536 ./SkEmbossMaskFilter.h:27:class SkEmbossMaskFilter : public SkMaskFilter { -- tested
537./SkPathEffect.h:33:class SkPathEffect : public SkFlattenable { -- abstract
538 ./Sk1DPathEffect.h:27:class Sk1DPathEffect : public SkPathEffect { -- abstract
539 ./Sk1DPathEffect.h:48:class SkPath1DPathEffect : public Sk1DPathEffect { -- tested
540 ./Sk2DPathEffect.h:25:class Sk2DPathEffect : public SkPathEffect { *** untested ***
541 ./SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { *** untested ***
542 ./SkDashPathEffect.h:27:class SkDashPathEffect : public SkPathEffect {
543 ./SkDiscretePathEffect.h:27:class SkDiscretePathEffect : public SkPathEffect {
544 ./SkPaint.h:760:class SkStrokePathEffect : public SkPathEffect {
545 ./SkPathEffect.h:58:class SkPairPathEffect : public SkPathEffect {
546 ./SkPathEffect.h:78:class SkComposePathEffect : public SkPairPathEffect {
547 ./SkPathEffect.h:114:class SkSumPathEffect : public SkPairPathEffect {
548./SkRasterizer.h:29:class SkRasterizer : public SkFlattenable {
549 ./SkLayerRasterizer.h:27:class SkLayerRasterizer : public SkRasterizer {
550./SkShader.h:36:class SkShader : public SkFlattenable {
551 ./SkColorFilter.h:59:class SkFilterShader : public SkShader {
552 ./SkColorShader.h:26:class SkColorShader : public SkShader {
553 ./SkShaderExtras.h:31:class SkComposeShader : public SkShader {
554 ./SkTransparentShader.h:23:class SkTransparentShader : public SkShader {
555./SkUnitMapper.h:24:class SkUnitMapper : public SkFlattenable {
556 ./SkUnitMapper.h:33:class SkDiscreteMapper : public SkUnitMapper {
557 ./SkUnitMapper.h:51:class SkFlipCosineMapper : public SkUnitMapper {
558./SkXfermode.h:32:class SkXfermode : public SkFlattenable {
559 ./SkAvoidXfermode.h:28:class SkAvoidXfermode : public SkXfermode { *** not done *** chmod +w .h .cpp
560 ./SkXfermode.h:54:class SkProcXfermode : public SkXfermode {
561 */
562
563 /*
564./SkBlurMaskFilter.h:25:class SkBlurMaskFilter {
565 chmod +w SkBlurMaskFilter.cpp
566./SkGradientShader.h:30:class SkGradientShader {
567 */
568 // save layer, bounder, looper
569 // matrix
570 // clip /path/region
571 // bitmap proc shader ?
572
573/* untested:
574SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect {
575*/
576
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000577 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578 fClickPt.set(x, y);
579 this->inval(NULL);
580 return this->INHERITED::onFindClickHandler(x, y);
581 }
582
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000583 SkPathEffect* pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000584 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
585 SkScalar gPhase = 0;
586 SkPath path;
587 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
588 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
589 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
590 path.close();
591 path.offset(SkIntToScalar(-6), 0);
592 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12),
593 gPhase, SkPath1DPathEffect::kRotate_Style);
594 SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2),
595 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
596 SkPathEffect* result = new SkComposePathEffect(outer, inner);
597 outer->unref();
598 inner->unref();
599 return result;
600 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000601
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000602 SkShader* shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000603 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000604 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
605 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
606 2, SkShader::kClamp_TileMode);
607 pts[1].set(0, SkIntToScalar(100));
608 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
609 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
610 2, SkShader::kClamp_TileMode);
reed@android.com048522d2009-06-23 12:19:41 +0000611 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612 SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
613 shaderA->unref();
614 shaderB->unref();
615 mode->unref();
616 return result;
617 }
618
619 virtual void startTest() {
620 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
621 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
622 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
623 }
624
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000625 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000626 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 drawOneRaster(canvas);
628 }
629
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000630 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000632
633 SkScalar x = SkIntToScalar(20);
634 SkScalar y = SkIntToScalar(40);
635 SkPaint paint;
636
637 paint.setAntiAlias(true);
638 paint.setTextSize(SkIntToScalar(48));
reed@android.comaa5a7db2009-05-27 01:20:10 +0000639 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
640 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000641
642 SkString str("GOOGLE");
643
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000644 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000645 apply_shader(&paint, i);
646
647 // paint.setMaskFilter(NULL);
648 // paint.setColor(SK_ColorBLACK);
649
650#if 01
651 int index = i % SK_ARRAY_COUNT(gLightingColors);
652 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
653 gLightingColors[index].fMul,
654 gLightingColors[index].fAdd))->unref();
655#endif
656
657 canvas->drawText(str.c_str(), str.size(), x, y, paint);
658 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
659 paint.setStyle(SkPaint::kStroke_Style);
660 canvas->drawOval(oval, paint);
661 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662
663 y += paint.getFontSpacing();
664 }
665
666 canvas->restore();
667
reed@android.com6b82d1a2009-06-03 02:35:01 +0000668 if (1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 SkAvoidXfermode mode(SK_ColorWHITE, 0xFF,
670 SkAvoidXfermode::kTargetColor_Mode);
671 SkPaint paint;
672 x += SkIntToScalar(20);
673 SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
674 paint.setXfermode(&mode);
675 paint.setColor(SK_ColorGREEN);
676 paint.setAntiAlias(true);
677 canvas->drawOval(r, paint);
678 }
679 }
680
681private:
682 SkPoint fClickPt;
683 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000684 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685};
686
687//////////////////////////////////////////////////////////////////////////////
688
689static SkView* MyFactory() { return new DemoView; }
690static SkViewRegister reg(MyFactory);
691