blob: 9a39ef9d768a50239e927443b533fe6aea42aa90 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#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"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000021#include "SkReadBuffer.h"
22#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkGradientShader.h"
24#include "SkImageDecoder.h"
25#include "SkLayerRasterizer.h"
26#include "SkMath.h"
27#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000028#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000038#include "SkUtils.h"
39#include "SkXfermode.h"
40
41#include <math.h>
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000043static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 unsigned r = SkGetPackedR32(c);
45 unsigned g = SkGetPackedG32(c);
46 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.comf2b98d62010-12-20 18:26:13 +000048 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
51}
52
53class SkGrayScaleColorFilter : public SkColorFilter {
54public:
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000055 virtual void filterSpan(const SkPMColor src[], int count,
mtklein36352bf2015-03-25 18:17:31 -070056 SkPMColor result[]) const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 for (int i = 0; i < count; i++)
58 result[i] = rgb2gray(src[i]);
59 }
60};
61
62class SkChannelMaskColorFilter : public SkColorFilter {
63public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000064 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
66 }
67
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000068 virtual void filterSpan(const SkPMColor src[], int count,
mtklein36352bf2015-03-25 18:17:31 -070069 SkPMColor result[]) const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 SkPMColor mask = fMask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000071 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 result[i] = src[i] & mask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000073 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076private:
77 SkPMColor fMask;
78};
79
80///////////////////////////////////////////////////////////
81
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000082static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000083 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000084 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)),
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000085 SkBlurMaskFilter::kNone_BlurFlag))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000086 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
88 p.setMaskFilter(NULL);
89 p.setStyle(SkPaint::kStroke_Style);
90 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000091 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
93 p.setAlpha(0x11);
94 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000095 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000096 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097}
98
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000099static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
100 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
102 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +0000103 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 p.setStyle(SkPaint::kStroke_Style);
105 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000106 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000109static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 p.setStyle(SkPaint::kStrokeAndFill_Style);
111 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000112 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113
114 p.setStyle(SkPaint::kStroke_Style);
115 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +0000116 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000117 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118}
119
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000120static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 p.setStyle(SkPaint::kStroke_Style);
122 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000123 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
125 p.setAlpha(0x20);
126 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +0000127 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000128 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129}
130
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000131static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000133 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134
135 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +0000136 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000137 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138
139 p.setXfermode(NULL);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000140 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141}
142
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000143static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
144 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000146 p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000147 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000148 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149}
150
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000151static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
152 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000155 SkLayerRasterizer::Builder rastBuilder2;
156 r5(&rastBuilder2, p);
157 p.setRasterizer(rastBuilder2.detachRasterizer())->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000158 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000159 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160}
161
162class Dot2DPathEffect : public Sk2DPathEffect {
163public:
164 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix)
165 : Sk2DPathEffect(matrix), fRadius(radius) {}
166
djsollen@google.comba28d032012-03-26 17:57:35 +0000167 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168
169protected:
mtklein36352bf2015-03-25 18:17:31 -0700170 void next(const SkPoint& loc, int u, int v, SkPath* dst) const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 dst->addCircle(loc.fX, loc.fY, fRadius);
172 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173
mtklein36352bf2015-03-25 18:17:31 -0700174 void flatten(SkWriteBuffer& buffer) const override {
djsollen@google.com54924242012-03-29 15:18:04 +0000175 this->INHERITED::flatten(buffer);
176 buffer.writeScalar(fRadius);
177 }
178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179private:
180 SkScalar fRadius;
181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 typedef Sk2DPathEffect INHERITED;
183};
184
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000185static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 SkMatrix lattice;
187 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
188 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
189 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000190 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191}
192
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000193static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
194 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 SkMatrix lattice;
197 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
198 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
199 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000200 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000201 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202
203 p.setPathEffect(NULL);
204 p.setXfermode(NULL);
205 p.setStyle(SkPaint::kStroke_Style);
206 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000207 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208}
209
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000210static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
211 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 SkMatrix lattice;
214 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
215 lattice.postRotate(SkIntToScalar(30), 0, 0);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000216 p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000217 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000218 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219
220 p.setPathEffect(NULL);
221 p.setXfermode(NULL);
222 p.setStyle(SkPaint::kStroke_Style);
223 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000224 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225}
226
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000227typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228
229static const raster_proc gRastProcs[] = {
230 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
231};
232
233static const struct {
234 SkColor fMul, fAdd;
235} gLightingColors[] = {
236 { 0x808080, 0x800000 }, // general case
237 { 0x707070, 0x707070 }, // no-pin case
238 { 0xFFFFFF, 0x800000 }, // just-add case
239 { 0x808080, 0x000000 }, // just-mul case
240 { 0xFFFFFF, 0x000000 } // identity case
241};
242
rmistry@google.comae933ce2012-08-23 18:19:56 +0000243static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 raster_proc proc = gRastProcs[index];
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000245 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000247 SkLayerRasterizer::Builder rastBuilder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
249 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000250 proc(&rastBuilder, p);
251 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 }
253
254#if 1
255 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
robertphillips@google.comb7061172013-09-06 14:16:12 +0000256 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(
257 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), dir,
258 SK_Scalar1/4, SkIntToScalar(4)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 paint->setColor(SK_ColorBLUE);
260#endif
261}
262
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000263class DemoView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264public:
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000265 DemoView() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000266
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267protected:
268 // overrides from SkEventSink
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000269 virtual bool onQuery(SkEvent* evt) {
270 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 SampleCode::TitleR(evt, "Demo");
272 return true;
273 }
274 return this->INHERITED::onQuery(evt);
275 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000276
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000277 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 return this->INHERITED::onClick(click);
279 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000280
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000281 void makePath(SkPath& path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
283 SkPath::kCCW_Direction);
284 for (int index = 0; index < 10; index++) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000285 SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f);
286 SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 x *= index & 1 ? 7 : 14;
288 y *= index & 1 ? 7 : 14;
289 x += SkIntToScalar(20);
290 y += SkIntToScalar(20);
291 if (index == 0)
292 path.moveTo(x, y);
293 else
294 path.lineTo(x, y);
295 }
296 path.close();
297 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000298
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000299 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 canvas->save();
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000301 this->drawPicture(canvas, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 canvas->restore();
303
304 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000305 SkPictureRecorder recorder;
306 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000307 SkCanvas* record = recorder.beginRecording(320, 480, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000308 this->drawPicture(record, 120);
309 }
310 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
311
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 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);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700319 picture->playback(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 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;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000367 linearColors, linearPos, linearCount, linearMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368
369 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
370 SkScalar radialRadius = SkIntToScalar(25);
371 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
372 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
373 int radialCount = 3;
374 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000375 SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 radialRadius, radialColors, radialPos, radialCount,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000377 radialMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000378
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 SkTransparentShader* transparentShader = new SkTransparentShader();
380 SkEmbossMaskFilter::Light light;
381 light.fDirection[0] = SK_Scalar1/2;
382 light.fDirection[1] = SK_Scalar1/2;
383 light.fDirection[2] = SK_Scalar1/3;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000384 light.fAmbient = 0x48;
385 light.fSpecular = 0x80;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000386 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5);
commit-bot@chromium.org7c9d0f32014-02-21 10:13:32 +0000387 SkEmbossMaskFilter* embossFilter = SkEmbossMaskFilter::Create(sigma, light);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000388
reed@android.com048522d2009-06-23 12:19:41 +0000389 SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
391 0xff89bc45, 0xff112233);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000392
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000394 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
reed@google.com9cfc83c2013-07-22 17:18:18 +0000395 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -0700396 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000397 // !!! draw through a clip
398 paint.setColor(SK_ColorLTGRAY);
399 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
401 canvas->clipRect(clip);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000402 paint.setShader(SkShader::CreateBitmapShader(fTx,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000404 canvas->drawPaint(paint);
405 canvas->save();
406
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 // line (exercises xfermode, colorShader, colorFilter, filterShader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000408 paint.setColor(SK_ColorGREEN);
409 paint.setStrokeWidth(SkIntToScalar(10));
410 paint.setStyle(SkPaint::kStroke_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 paint.setXfermode(xfermode)->unref();
412 paint.setColorFilter(lightingFilter)->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000413 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
414 paint.setXfermode(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 paint.setColorFilter(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000416
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 // rectangle
rmistry@google.comae933ce2012-08-23 18:19:56 +0000418 paint.setStyle(SkPaint::kFill_Style);
419 canvas->translate(SkIntToScalar(50), 0);
420 paint.setColor(SK_ColorYELLOW);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421 paint.setShader(linear)->unref();
422 paint.setPathEffect(pathEffectTest())->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000423 canvas->drawRect(rect, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 paint.setPathEffect(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000425
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 // circle w/ emboss & transparent (exercises 3dshader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000427 canvas->translate(SkIntToScalar(50), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 paint.setMaskFilter(embossFilter)->unref();
429 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000430 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431 paint.setShader(transparentShader)->unref();
432 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000433 canvas->translate(0, SkIntToScalar(-10));
434
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 // path
rmistry@google.comae933ce2012-08-23 18:19:56 +0000436 canvas->translate(SkIntToScalar(50), 0);
437 paint.setColor(SK_ColorRED);
438 paint.setStyle(SkPaint::kStroke_Style);
439 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 paint.setShader(radial)->unref();
441 paint.setMaskFilter(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000442 canvas->drawPath(path, paint);
443
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 paint.setShader(NULL);
445 // bitmap, sprite
rmistry@google.comae933ce2012-08-23 18:19:56 +0000446 canvas->translate(SkIntToScalar(50), 0);
447 paint.setStyle(SkPaint::kFill_Style);
448 canvas->drawBitmap(fBug, left, top, &paint);
449 canvas->translate(SkIntToScalar(30), 0);
450 canvas->drawSprite(fTb,
reed@google.come1ca7052013-12-17 19:22:07 +0000451 SkScalarRoundToInt(canvas->getTotalMatrix().getTranslateX()),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452 spriteOffset + 10, &paint);
453
rmistry@google.comae933ce2012-08-23 18:19:56 +0000454 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 paint.setShader(shaderTest())->unref(); // test compose shader
rmistry@google.comae933ce2012-08-23 18:19:56 +0000456 canvas->drawRect(rect2, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457 paint.setShader(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000458
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459 canvas->restore();
460 // text
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 canvas->translate(0, SkIntToScalar(60));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000462 canvas->save();
463 paint.setColor(SK_ColorGRAY);
464 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
465 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
466
467 canvas->translate(SkIntToScalar(50), 0);
468 paint.setColor(SK_ColorCYAN);
469 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
470
471 canvas->translate(SkIntToScalar(30), 0);
472 paint.setColor(SK_ColorMAGENTA);
473 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
474 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
475 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
476 canvas->translate(0, SkIntToScalar(20));
477 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
478 canvas->restore();
479
480 canvas->translate(0, SkIntToScalar(60));
481 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482 canvas->restore();
483 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000484
reed@google.com4d5c26d2013-01-08 16:17:50 +0000485 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 fClickPt.set(x, y);
487 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000488 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000490
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000491 SkPathEffect* pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
493 SkScalar gPhase = 0;
494 SkPath path;
495 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
496 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
497 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
498 path.close();
499 path.offset(SkIntToScalar(-6), 0);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000500 SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501 gPhase, SkPath1DPathEffect::kRotate_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000502 SkPathEffect* inner = SkDiscretePathEffect::Create(SkIntToScalar(2),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000504 SkPathEffect* result = SkComposePathEffect::Create(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000505 outer->unref();
506 inner->unref();
507 return result;
508 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000509
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000510 SkShader* shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000511 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000513 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514 2, SkShader::kClamp_TileMode);
515 pts[1].set(0, SkIntToScalar(100));
516 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000517 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 2, SkShader::kClamp_TileMode);
reed@android.com048522d2009-06-23 12:19:41 +0000519 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
521 shaderA->unref();
522 shaderB->unref();
523 mode->unref();
524 return result;
525 }
526
527 virtual void startTest() {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000528 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
529 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
530 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
531 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000533 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000534 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535 drawOneRaster(canvas);
536 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000537
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000538 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540
541 SkScalar x = SkIntToScalar(20);
542 SkScalar y = SkIntToScalar(40);
543 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000544
reed@android.com8a1c16f2008-12-17 15:59:43 +0000545 paint.setAntiAlias(true);
546 paint.setTextSize(SkIntToScalar(48));
reed@android.comaa5a7db2009-05-27 01:20:10 +0000547 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
548 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549
550 SkString str("GOOGLE");
551
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000552 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000553 apply_shader(&paint, (int)i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000554
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 // paint.setMaskFilter(NULL);
556 // paint.setColor(SK_ColorBLACK);
557
558#if 01
559 int index = i % SK_ARRAY_COUNT(gLightingColors);
560 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
561 gLightingColors[index].fMul,
562 gLightingColors[index].fAdd))->unref();
563#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000564
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565 canvas->drawText(str.c_str(), str.size(), x, y, paint);
566 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
567 paint.setStyle(SkPaint::kStroke_Style);
568 canvas->drawOval(oval, paint);
569 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000570
571 y += paint.getFontSpacing();
572 }
573
574 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000575 }
576
577private:
578 SkPoint fClickPt;
579 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000580 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581};
582
583//////////////////////////////////////////////////////////////////////////////
584
585static SkView* MyFactory() { return new DemoView; }
586static SkViewRegister reg(MyFactory);