blob: 6410670fedbf4758a3d24787ceacb7eccb320189 [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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
11#include "SkView.h"
12#include "Sk1DPathEffect.h"
13#include "Sk2DPathEffect.h"
14#include "SkAvoidXfermode.h"
15#include "SkBlurMaskFilter.h"
16#include "SkColorFilter.h"
17#include "SkColorPriv.h"
18#include "SkCornerPathEffect.h"
19#include "SkDashPathEffect.h"
20#include "SkDiscretePathEffect.h"
21#include "SkEmbossMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000022#include "SkReadBuffer.h"
23#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkGradientShader.h"
25#include "SkImageDecoder.h"
26#include "SkLayerRasterizer.h"
27#include "SkMath.h"
28#include "SkPath.h"
29#include "SkRegion.h"
30#include "SkShader.h"
reed@android.comaa5a7db2009-05-27 01:20:10 +000031#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000032#include "SkCornerPathEffect.h"
33#include "SkPathMeasure.h"
34#include "SkPicture.h"
35#include "SkRandom.h"
36#include "SkTransparentShader.h"
37#include "SkTypeface.h"
38#include "SkUnitMappers.h"
39#include "SkUtils.h"
40#include "SkXfermode.h"
41
42#include <math.h>
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000044static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 unsigned r = SkGetPackedR32(c);
46 unsigned g = SkGetPackedG32(c);
47 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
reed@android.comf2b98d62010-12-20 18:26:13 +000049 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
rmistry@google.comae933ce2012-08-23 18:19:56 +000050
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
52}
53
54class SkGrayScaleColorFilter : public SkColorFilter {
55public:
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000056 virtual void filterSpan(const SkPMColor src[], int count,
57 SkPMColor result[]) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 for (int i = 0; i < count; i++)
59 result[i] = rgb2gray(src[i]);
60 }
61};
62
63class SkChannelMaskColorFilter : public SkColorFilter {
64public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000065 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
67 }
68
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000069 virtual void filterSpan(const SkPMColor src[], int count,
70 SkPMColor result[]) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 SkPMColor mask = fMask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000072 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 result[i] = src[i] & mask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000074 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077private:
78 SkPMColor fMask;
79};
80
81///////////////////////////////////////////////////////////
82
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000083static void r0(SkLayerRasterizer* rast, SkPaint& p) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000084 p.setMaskFilter(SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
85 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)),
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000086 SkBlurMaskFilter::kNone_BlurFlag))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
88
89 p.setMaskFilter(NULL);
90 p.setStyle(SkPaint::kStroke_Style);
91 p.setStrokeWidth(SK_Scalar1);
92 rast->addLayer(p);
93
94 p.setAlpha(0x11);
95 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000096 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 rast->addLayer(p);
98}
99
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000100static void r1(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 rast->addLayer(p);
102
103 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +0000104 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 p.setStyle(SkPaint::kStroke_Style);
106 p.setStrokeWidth(SK_Scalar1*2);
107 rast->addLayer(p);
108}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000110static void r2(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 p.setStyle(SkPaint::kStrokeAndFill_Style);
112 p.setStrokeWidth(SK_Scalar1*4);
113 rast->addLayer(p);
114
115 p.setStyle(SkPaint::kStroke_Style);
116 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +0000117 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 rast->addLayer(p);
119}
120
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000121static void r3(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 p.setStyle(SkPaint::kStroke_Style);
123 p.setStrokeWidth(SK_Scalar1*3);
124 rast->addLayer(p);
125
126 p.setAlpha(0x20);
127 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +0000128 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 rast->addLayer(p);
130}
131
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000132static void r4(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 p.setAlpha(0x60);
134 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
135
136 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +0000137 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
139
140 p.setXfermode(NULL);
141 rast->addLayer(p);
142}
143
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000144static void r5(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 rast->addLayer(p);
146
147 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000148 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 rast->addLayer(p);
150}
151
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000152static void r6(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 p.setAntiAlias(false);
156 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
157 r5(rast2, p);
158 p.setRasterizer(rast2)->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000159 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 rast->addLayer(p);
161}
162
163class Dot2DPathEffect : public Sk2DPathEffect {
164public:
165 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix)
166 : Sk2DPathEffect(matrix), fRadius(radius) {}
167
djsollen@google.comba28d032012-03-26 17:57:35 +0000168 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
170protected:
mike@reedtribe.org61490fa2012-12-24 14:38:46 +0000171 virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 dst->addCircle(loc.fX, loc.fY, fRadius);
173 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000175 Dot2DPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 fRadius = buffer.readScalar();
177 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000178 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
djsollen@google.com54924242012-03-29 15:18:04 +0000179 this->INHERITED::flatten(buffer);
180 buffer.writeScalar(fRadius);
181 }
182
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183private:
184 SkScalar fRadius;
185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 typedef Sk2DPathEffect INHERITED;
187};
188
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000189static void r7(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 SkMatrix lattice;
191 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
192 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
193 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref();
194 rast->addLayer(p);
195}
196
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000197static void r8(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 SkMatrix lattice;
201 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
202 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
203 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000204 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 rast->addLayer(p);
206
207 p.setPathEffect(NULL);
208 p.setXfermode(NULL);
209 p.setStyle(SkPaint::kStroke_Style);
210 p.setStrokeWidth(SK_Scalar1);
211 rast->addLayer(p);
212}
213
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000214static void r9(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000216
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217 SkMatrix lattice;
218 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
219 lattice.postRotate(SkIntToScalar(30), 0, 0);
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000220 p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000221 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 rast->addLayer(p);
223
224 p.setPathEffect(NULL);
225 p.setXfermode(NULL);
226 p.setStyle(SkPaint::kStroke_Style);
227 p.setStrokeWidth(SK_Scalar1);
228 rast->addLayer(p);
229}
230
231typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
232
233static const raster_proc gRastProcs[] = {
234 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
235};
236
237static const struct {
238 SkColor fMul, fAdd;
239} gLightingColors[] = {
240 { 0x808080, 0x800000 }, // general case
241 { 0x707070, 0x707070 }, // no-pin case
242 { 0xFFFFFF, 0x800000 }, // just-add case
243 { 0x808080, 0x000000 }, // just-mul case
244 { 0xFFFFFF, 0x000000 } // identity case
245};
246
rmistry@google.comae933ce2012-08-23 18:19:56 +0000247static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 raster_proc proc = gRastProcs[index];
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000249 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 SkPaint p;
251 SkLayerRasterizer* rast = new SkLayerRasterizer;
252
253 p.setAntiAlias(true);
254 proc(rast, p);
255 paint->setRasterizer(rast)->unref();
256 }
257
258#if 1
259 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
robertphillips@google.comb7061172013-09-06 14:16:12 +0000260 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(
261 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), dir,
262 SK_Scalar1/4, SkIntToScalar(4)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 paint->setColor(SK_ColorBLUE);
264#endif
265}
266
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000267class DemoView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268public:
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000269 DemoView() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000270
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271protected:
272 // overrides from SkEventSink
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000273 virtual bool onQuery(SkEvent* evt) {
274 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 SampleCode::TitleR(evt, "Demo");
276 return true;
277 }
278 return this->INHERITED::onQuery(evt);
279 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000280
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000281 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 return this->INHERITED::onClick(click);
283 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000284
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000285 void makePath(SkPath& path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
287 SkPath::kCCW_Direction);
288 for (int index = 0; index < 10; index++) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000289 SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f);
290 SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 x *= index & 1 ? 7 : 14;
292 y *= index & 1 ? 7 : 14;
293 x += SkIntToScalar(20);
294 y += SkIntToScalar(20);
295 if (index == 0)
296 path.moveTo(x, y);
297 else
298 path.lineTo(x, y);
299 }
300 path.close();
301 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000302
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000303 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 canvas->save();
305 drawPicture(canvas, 0);
306 canvas->restore();
307
308 {
309 SkPicture picture;
310 SkCanvas* record = picture.beginRecording(320, 480);
311 drawPicture(record, 120);
312 canvas->translate(0, SkIntToScalar(120));
313
314 SkRect clip;
315 clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
316 do {
317 canvas->save();
318 canvas->clipRect(clip);
319 picture.draw(canvas);
320 canvas->restore();
321 if (clip.fRight < SkIntToScalar(320))
322 clip.offset(SkIntToScalar(160), 0);
323 else if (clip.fBottom < SkIntToScalar(480))
324 clip.offset(-SkIntToScalar(320), SkIntToScalar(160));
325 else
326 break;
327 } while (true);
328 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000330
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000331 void drawPicture(SkCanvas* canvas, int spriteOffset) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000332 SkMatrix matrix; matrix.reset();
333 SkPaint paint;
334 SkPath path;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 SkPoint start = {0, 0};
336 SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000337 SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) };
338 SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) };
339 SkScalar left = 0, top = 0, x = 0, y = 0;
reed@google.com7fa2a652014-01-27 13:42:58 +0000340 int index;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000341
342 char ascii[] = "ascii...";
reed@google.com7fa2a652014-01-27 13:42:58 +0000343 int asciiLength = sizeof(ascii) - 1;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000344 char utf8[] = "utf8" "\xe2\x80\xa6";
345 short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 };
346 short utf16simple[] = {'u', 't', 'f', '1', '6', '!' };
347
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 makePath(path);
349 SkTDArray<SkPoint>(pos);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000350 pos.setCount(asciiLength);
351 for (index = 0; index < asciiLength; index++)
352 pos[index].set(SkIntToScalar((unsigned int)index * 10),
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000353 SkIntToScalar((unsigned int)index * 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354 SkTDArray<SkPoint>(pos2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000355 pos2.setCount(asciiLength);
356 for (index = 0; index < asciiLength; index++)
357 pos2[index].set(SkIntToScalar((unsigned int)index * 10),
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000358 SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000359
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 // shaders
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000361 SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
363 SkScalar* linearPos = NULL;
364 int linearCount = 2;
365 SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
366 SkUnitMapper* linearMapper = new SkDiscreteMapper(3);
367 SkAutoUnref unmapLinearMapper(linearMapper);
368 SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
369 linearColors, linearPos, linearCount, linearMode, linearMapper);
370
371 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
372 SkScalar radialRadius = SkIntToScalar(25);
373 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
374 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
375 int radialCount = 3;
376 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
377 SkUnitMapper* radialMapper = new SkCosineMapper();
378 SkAutoUnref unmapRadialMapper(radialMapper);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000379 SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 radialRadius, radialColors, radialPos, radialCount,
381 radialMode, radialMapper);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000382
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 SkTransparentShader* transparentShader = new SkTransparentShader();
384 SkEmbossMaskFilter::Light light;
385 light.fDirection[0] = SK_Scalar1/2;
386 light.fDirection[1] = SK_Scalar1/2;
387 light.fDirection[2] = SK_Scalar1/3;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000388 light.fAmbient = 0x48;
389 light.fSpecular = 0x80;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000390 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5);
391 SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(sigma, light);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000392
reed@android.com048522d2009-06-23 12:19:41 +0000393 SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
395 0xff89bc45, 0xff112233);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000396
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000398 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
reed@google.com9cfc83c2013-07-22 17:18:18 +0000399 paint.setAntiAlias(true);
400 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000401 // !!! draw through a clip
402 paint.setColor(SK_ColorLTGRAY);
403 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
405 canvas->clipRect(clip);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000406 paint.setShader(SkShader::CreateBitmapShader(fTx,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000408 canvas->drawPaint(paint);
409 canvas->save();
410
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 // line (exercises xfermode, colorShader, colorFilter, filterShader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000412 paint.setColor(SK_ColorGREEN);
413 paint.setStrokeWidth(SkIntToScalar(10));
414 paint.setStyle(SkPaint::kStroke_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 paint.setXfermode(xfermode)->unref();
416 paint.setColorFilter(lightingFilter)->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000417 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
418 paint.setXfermode(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 paint.setColorFilter(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000420
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421 // rectangle
rmistry@google.comae933ce2012-08-23 18:19:56 +0000422 paint.setStyle(SkPaint::kFill_Style);
423 canvas->translate(SkIntToScalar(50), 0);
424 paint.setColor(SK_ColorYELLOW);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 paint.setShader(linear)->unref();
426 paint.setPathEffect(pathEffectTest())->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000427 canvas->drawRect(rect, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 paint.setPathEffect(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000429
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430 // circle w/ emboss & transparent (exercises 3dshader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000431 canvas->translate(SkIntToScalar(50), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432 paint.setMaskFilter(embossFilter)->unref();
433 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000434 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 paint.setShader(transparentShader)->unref();
436 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000437 canvas->translate(0, SkIntToScalar(-10));
438
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439 // path
rmistry@google.comae933ce2012-08-23 18:19:56 +0000440 canvas->translate(SkIntToScalar(50), 0);
441 paint.setColor(SK_ColorRED);
442 paint.setStyle(SkPaint::kStroke_Style);
443 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 paint.setShader(radial)->unref();
445 paint.setMaskFilter(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000446 canvas->drawPath(path, paint);
447
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 paint.setShader(NULL);
449 // bitmap, sprite
rmistry@google.comae933ce2012-08-23 18:19:56 +0000450 canvas->translate(SkIntToScalar(50), 0);
451 paint.setStyle(SkPaint::kFill_Style);
452 canvas->drawBitmap(fBug, left, top, &paint);
453 canvas->translate(SkIntToScalar(30), 0);
454 canvas->drawSprite(fTb,
reed@google.come1ca7052013-12-17 19:22:07 +0000455 SkScalarRoundToInt(canvas->getTotalMatrix().getTranslateX()),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456 spriteOffset + 10, &paint);
457
rmistry@google.comae933ce2012-08-23 18:19:56 +0000458 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459 paint.setShader(shaderTest())->unref(); // test compose shader
rmistry@google.comae933ce2012-08-23 18:19:56 +0000460 canvas->drawRect(rect2, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 paint.setShader(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000462
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463 canvas->restore();
464 // text
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465 canvas->translate(0, SkIntToScalar(60));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000466 canvas->save();
467 paint.setColor(SK_ColorGRAY);
468 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
469 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
470
471 canvas->translate(SkIntToScalar(50), 0);
472 paint.setColor(SK_ColorCYAN);
473 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
474
475 canvas->translate(SkIntToScalar(30), 0);
476 paint.setColor(SK_ColorMAGENTA);
477 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
478 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
479 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
480 canvas->translate(0, SkIntToScalar(20));
481 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
482 canvas->restore();
483
484 canvas->translate(0, SkIntToScalar(60));
485 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 canvas->restore();
487 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000488
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 /*
490./SkColorFilter.h:25:class SkColorFilter : public SkFlattenable { -- abstract
491 static SkColorFilter* CreatXfermodeFilter() *** untested ***
492 static SkColorFilter* CreatePorterDuffFilter() *** untested ***
493 static SkColorFilter* CreateLightingFilter() -- tested
494./SkDrawLooper.h:9:class SkDrawLooper : public SkFlattenable { -- virtually abstract
495 ./SkBlurDrawLooper.h:9:class SkBlurDrawLooper : public SkDrawLooper { *** untested ***
496./SkMaskFilter.h:41:class SkMaskFilter : public SkFlattenable { -- abstract chmod +w .h
497 ./SkEmbossMaskFilter.h:27:class SkEmbossMaskFilter : public SkMaskFilter { -- tested
498./SkPathEffect.h:33:class SkPathEffect : public SkFlattenable { -- abstract
499 ./Sk1DPathEffect.h:27:class Sk1DPathEffect : public SkPathEffect { -- abstract
500 ./Sk1DPathEffect.h:48:class SkPath1DPathEffect : public Sk1DPathEffect { -- tested
501 ./Sk2DPathEffect.h:25:class Sk2DPathEffect : public SkPathEffect { *** untested ***
502 ./SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect { *** untested ***
503 ./SkDashPathEffect.h:27:class SkDashPathEffect : public SkPathEffect {
504 ./SkDiscretePathEffect.h:27:class SkDiscretePathEffect : public SkPathEffect {
505 ./SkPaint.h:760:class SkStrokePathEffect : public SkPathEffect {
506 ./SkPathEffect.h:58:class SkPairPathEffect : public SkPathEffect {
507 ./SkPathEffect.h:78:class SkComposePathEffect : public SkPairPathEffect {
508 ./SkPathEffect.h:114:class SkSumPathEffect : public SkPairPathEffect {
509./SkRasterizer.h:29:class SkRasterizer : public SkFlattenable {
510 ./SkLayerRasterizer.h:27:class SkLayerRasterizer : public SkRasterizer {
511./SkShader.h:36:class SkShader : public SkFlattenable {
512 ./SkColorFilter.h:59:class SkFilterShader : public SkShader {
513 ./SkColorShader.h:26:class SkColorShader : public SkShader {
514 ./SkShaderExtras.h:31:class SkComposeShader : public SkShader {
515 ./SkTransparentShader.h:23:class SkTransparentShader : public SkShader {
516./SkUnitMapper.h:24:class SkUnitMapper : public SkFlattenable {
517 ./SkUnitMapper.h:33:class SkDiscreteMapper : public SkUnitMapper {
518 ./SkUnitMapper.h:51:class SkFlipCosineMapper : public SkUnitMapper {
519./SkXfermode.h:32:class SkXfermode : public SkFlattenable {
520 ./SkAvoidXfermode.h:28:class SkAvoidXfermode : public SkXfermode { *** not done *** chmod +w .h .cpp
521 ./SkXfermode.h:54:class SkProcXfermode : public SkXfermode {
522 */
rmistry@google.comae933ce2012-08-23 18:19:56 +0000523
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 /*
525./SkBlurMaskFilter.h:25:class SkBlurMaskFilter {
526 chmod +w SkBlurMaskFilter.cpp
527./SkGradientShader.h:30:class SkGradientShader {
528 */
529 // save layer, bounder, looper
530 // matrix
531 // clip /path/region
532 // bitmap proc shader ?
533
534/* untested:
535SkCornerPathEffect.h:28:class SkCornerPathEffect : public SkPathEffect {
536*/
rmistry@google.comae933ce2012-08-23 18:19:56 +0000537
reed@google.com4d5c26d2013-01-08 16:17:50 +0000538 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 fClickPt.set(x, y);
540 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000541 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000543
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000544 SkPathEffect* pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
546 SkScalar gPhase = 0;
547 SkPath path;
548 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
549 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
550 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
551 path.close();
552 path.offset(SkIntToScalar(-6), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000553 SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554 gPhase, SkPath1DPathEffect::kRotate_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000555 SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000556 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
557 SkPathEffect* result = new SkComposePathEffect(outer, inner);
558 outer->unref();
559 inner->unref();
560 return result;
561 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000562
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000563 SkShader* shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000564 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000566 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567 2, SkShader::kClamp_TileMode);
568 pts[1].set(0, SkIntToScalar(100));
569 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000570 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000571 2, SkShader::kClamp_TileMode);
reed@android.com048522d2009-06-23 12:19:41 +0000572 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
574 shaderA->unref();
575 shaderB->unref();
576 mode->unref();
577 return result;
578 }
579
580 virtual void startTest() {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000581 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
582 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
583 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
584 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000586 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000587 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588 drawOneRaster(canvas);
589 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000590
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000591 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000592 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593
594 SkScalar x = SkIntToScalar(20);
595 SkScalar y = SkIntToScalar(40);
596 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000597
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598 paint.setAntiAlias(true);
599 paint.setTextSize(SkIntToScalar(48));
reed@android.comaa5a7db2009-05-27 01:20:10 +0000600 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
601 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602
603 SkString str("GOOGLE");
604
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000605 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000606 apply_shader(&paint, (int)i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000607
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608 // paint.setMaskFilter(NULL);
609 // paint.setColor(SK_ColorBLACK);
610
611#if 01
612 int index = i % SK_ARRAY_COUNT(gLightingColors);
613 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
614 gLightingColors[index].fMul,
615 gLightingColors[index].fAdd))->unref();
616#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000617
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618 canvas->drawText(str.c_str(), str.size(), x, y, paint);
619 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
620 paint.setStyle(SkPaint::kStroke_Style);
621 canvas->drawOval(oval, paint);
622 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623
624 y += paint.getFontSpacing();
625 }
626
627 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000628
reed@android.com6b82d1a2009-06-03 02:35:01 +0000629 if (1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630 SkAvoidXfermode mode(SK_ColorWHITE, 0xFF,
631 SkAvoidXfermode::kTargetColor_Mode);
632 SkPaint paint;
633 x += SkIntToScalar(20);
634 SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
635 paint.setXfermode(&mode);
636 paint.setColor(SK_ColorGREEN);
637 paint.setAntiAlias(true);
638 canvas->drawOval(r, paint);
639 }
640 }
641
642private:
643 SkPoint fClickPt;
644 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000645 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646};
647
648//////////////////////////////////////////////////////////////////////////////
649
650static SkView* MyFactory() { return new DemoView; }
651static SkViewRegister reg(MyFactory);