blob: ea1f14328816fbba47ed2b2655438082621eea07 [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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000029#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000030#include "SkRegion.h"
31#include "SkShader.h"
reed@android.comaa5a7db2009-05-27 01:20:10 +000032#include "SkComposeShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000033#include "SkCornerPathEffect.h"
34#include "SkPathMeasure.h"
35#include "SkPicture.h"
36#include "SkRandom.h"
37#include "SkTransparentShader.h"
38#include "SkTypeface.h"
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000039#include "SkUnitMappers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000040#include "SkUtils.h"
41#include "SkXfermode.h"
42
43#include <math.h>
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000045static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 unsigned r = SkGetPackedR32(c);
47 unsigned g = SkGetPackedG32(c);
48 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
reed@android.comf2b98d62010-12-20 18:26:13 +000050 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
53}
54
55class SkGrayScaleColorFilter : public SkColorFilter {
56public:
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000057 virtual void filterSpan(const SkPMColor src[], int count,
58 SkPMColor result[]) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 for (int i = 0; i < count; i++)
60 result[i] = rgb2gray(src[i]);
61 }
62};
63
64class SkChannelMaskColorFilter : public SkColorFilter {
65public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000066 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
68 }
69
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000070 virtual void filterSpan(const SkPMColor src[], int count,
71 SkPMColor result[]) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkPMColor mask = fMask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000073 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 result[i] = src[i] & mask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000075 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000077
reed@android.com8a1c16f2008-12-17 15:59:43 +000078private:
79 SkPMColor fMask;
80};
81
82///////////////////////////////////////////////////////////
83
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000084static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000085 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000086 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)),
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000087 SkBlurMaskFilter::kNone_BlurFlag))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000088 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90 p.setMaskFilter(NULL);
91 p.setStyle(SkPaint::kStroke_Style);
92 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000093 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
95 p.setAlpha(0x11);
96 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000097 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000098 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099}
100
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000101static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
102 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
104 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +0000105 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 p.setStyle(SkPaint::kStroke_Style);
107 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000108 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000111static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 p.setStyle(SkPaint::kStrokeAndFill_Style);
113 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000114 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
116 p.setStyle(SkPaint::kStroke_Style);
117 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +0000118 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000119 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120}
121
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000122static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 p.setStyle(SkPaint::kStroke_Style);
124 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000125 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
127 p.setAlpha(0x20);
128 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +0000129 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000130 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131}
132
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000133static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000135 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
137 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +0000138 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000139 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140
141 p.setXfermode(NULL);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000142 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143}
144
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000145static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
146 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000148 p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000149 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000150 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151}
152
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000153static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
154 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000157 SkLayerRasterizer::Builder rastBuilder2;
158 r5(&rastBuilder2, p);
159 p.setRasterizer(rastBuilder2.detachRasterizer())->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000160 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000161 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162}
163
164class Dot2DPathEffect : public Sk2DPathEffect {
165public:
166 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix)
167 : Sk2DPathEffect(matrix), fRadius(radius) {}
168
djsollen@google.comba28d032012-03-26 17:57:35 +0000169 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171protected:
mike@reedtribe.org61490fa2012-12-24 14:38:46 +0000172 virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 dst->addCircle(loc.fX, loc.fY, fRadius);
174 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000176 Dot2DPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 fRadius = buffer.readScalar();
178 }
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000179 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
djsollen@google.com54924242012-03-29 15:18:04 +0000180 this->INHERITED::flatten(buffer);
181 buffer.writeScalar(fRadius);
182 }
183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184private:
185 SkScalar fRadius;
186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 typedef Sk2DPathEffect INHERITED;
188};
189
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000190static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 SkMatrix lattice;
192 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
193 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
194 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000195 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196}
197
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000198static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
199 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 SkMatrix lattice;
202 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
203 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
204 p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000205 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000206 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207
208 p.setPathEffect(NULL);
209 p.setXfermode(NULL);
210 p.setStyle(SkPaint::kStroke_Style);
211 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000212 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213}
214
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000215static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
216 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 SkMatrix lattice;
219 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
220 lattice.postRotate(SkIntToScalar(30), 0, 0);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000221 p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000222 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000223 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224
225 p.setPathEffect(NULL);
226 p.setXfermode(NULL);
227 p.setStyle(SkPaint::kStroke_Style);
228 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000229 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230}
231
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000232typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233
234static const raster_proc gRastProcs[] = {
235 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
236};
237
238static const struct {
239 SkColor fMul, fAdd;
240} gLightingColors[] = {
241 { 0x808080, 0x800000 }, // general case
242 { 0x707070, 0x707070 }, // no-pin case
243 { 0xFFFFFF, 0x800000 }, // just-add case
244 { 0x808080, 0x000000 }, // just-mul case
245 { 0xFFFFFF, 0x000000 } // identity case
246};
247
rmistry@google.comae933ce2012-08-23 18:19:56 +0000248static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 raster_proc proc = gRastProcs[index];
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000250 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000252 SkLayerRasterizer::Builder rastBuilder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253
254 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000255 proc(&rastBuilder, p);
256 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 }
258
259#if 1
260 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
robertphillips@google.comb7061172013-09-06 14:16:12 +0000261 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(
262 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), dir,
263 SK_Scalar1/4, SkIntToScalar(4)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 paint->setColor(SK_ColorBLUE);
265#endif
266}
267
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000268class DemoView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269public:
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000270 DemoView() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000271
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272protected:
273 // overrides from SkEventSink
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000274 virtual bool onQuery(SkEvent* evt) {
275 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 SampleCode::TitleR(evt, "Demo");
277 return true;
278 }
279 return this->INHERITED::onQuery(evt);
280 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000281
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000282 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 return this->INHERITED::onClick(click);
284 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000285
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000286 void makePath(SkPath& path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
288 SkPath::kCCW_Direction);
289 for (int index = 0; index < 10; index++) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000290 SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f);
291 SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 x *= index & 1 ? 7 : 14;
293 y *= index & 1 ? 7 : 14;
294 x += SkIntToScalar(20);
295 y += SkIntToScalar(20);
296 if (index == 0)
297 path.moveTo(x, y);
298 else
299 path.lineTo(x, y);
300 }
301 path.close();
302 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000303
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000304 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 canvas->save();
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000306 this->drawPicture(canvas, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 canvas->restore();
308
309 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000310 SkPictureRecorder recorder;
311 {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000312 SkCanvas* record = recorder.beginRecording(320, 480, NULL, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000313 this->drawPicture(record, 120);
314 }
315 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 canvas->translate(0, SkIntToScalar(120));
318
319 SkRect clip;
320 clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
321 do {
322 canvas->save();
323 canvas->clipRect(clip);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000324 picture->draw(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 canvas->restore();
326 if (clip.fRight < SkIntToScalar(320))
327 clip.offset(SkIntToScalar(160), 0);
328 else if (clip.fBottom < SkIntToScalar(480))
329 clip.offset(-SkIntToScalar(320), SkIntToScalar(160));
330 else
331 break;
332 } while (true);
333 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000335
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000336 void drawPicture(SkCanvas* canvas, int spriteOffset) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000337 SkMatrix matrix; matrix.reset();
338 SkPaint paint;
339 SkPath path;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 SkPoint start = {0, 0};
341 SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000342 SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) };
343 SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) };
344 SkScalar left = 0, top = 0, x = 0, y = 0;
reed@google.com7fa2a652014-01-27 13:42:58 +0000345 int index;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000346
347 char ascii[] = "ascii...";
reed@google.com7fa2a652014-01-27 13:42:58 +0000348 int asciiLength = sizeof(ascii) - 1;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000349 char utf8[] = "utf8" "\xe2\x80\xa6";
350 short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 };
351 short utf16simple[] = {'u', 't', 'f', '1', '6', '!' };
352
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 makePath(path);
354 SkTDArray<SkPoint>(pos);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000355 pos.setCount(asciiLength);
356 for (index = 0; index < asciiLength; index++)
357 pos[index].set(SkIntToScalar((unsigned int)index * 10),
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000358 SkIntToScalar((unsigned int)index * 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 SkTDArray<SkPoint>(pos2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000360 pos2.setCount(asciiLength);
361 for (index = 0; index < asciiLength; index++)
362 pos2[index].set(SkIntToScalar((unsigned int)index * 10),
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000363 SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000364
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 // shaders
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000366 SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
368 SkScalar* linearPos = NULL;
369 int linearCount = 2;
370 SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +0000371 SkUnitMapper* linearMapper = new SkDiscreteMapper(3);
372 SkAutoUnref unmapLinearMapper(linearMapper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +0000374 linearColors, linearPos, linearCount, linearMode, linearMapper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375
376 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
377 SkScalar radialRadius = SkIntToScalar(25);
378 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
379 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
380 int radialCount = 3;
381 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +0000382 SkUnitMapper* radialMapper = new SkCosineMapper();
383 SkAutoUnref unmapRadialMapper(radialMapper);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000384 SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 radialRadius, radialColors, radialPos, radialCount,
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +0000386 radialMode, radialMapper);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000387
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 SkTransparentShader* transparentShader = new SkTransparentShader();
389 SkEmbossMaskFilter::Light light;
390 light.fDirection[0] = SK_Scalar1/2;
391 light.fDirection[1] = SK_Scalar1/2;
392 light.fDirection[2] = SK_Scalar1/3;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000393 light.fAmbient = 0x48;
394 light.fSpecular = 0x80;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000395 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5);
commit-bot@chromium.org7c9d0f32014-02-21 10:13:32 +0000396 SkEmbossMaskFilter* embossFilter = SkEmbossMaskFilter::Create(sigma, light);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000397
reed@android.com048522d2009-06-23 12:19:41 +0000398 SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
400 0xff89bc45, 0xff112233);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000401
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000403 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
reed@google.com9cfc83c2013-07-22 17:18:18 +0000404 paint.setAntiAlias(true);
405 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000406 // !!! draw through a clip
407 paint.setColor(SK_ColorLTGRAY);
408 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
410 canvas->clipRect(clip);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000411 paint.setShader(SkShader::CreateBitmapShader(fTx,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000413 canvas->drawPaint(paint);
414 canvas->save();
415
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416 // line (exercises xfermode, colorShader, colorFilter, filterShader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000417 paint.setColor(SK_ColorGREEN);
418 paint.setStrokeWidth(SkIntToScalar(10));
419 paint.setStyle(SkPaint::kStroke_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 paint.setXfermode(xfermode)->unref();
421 paint.setColorFilter(lightingFilter)->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000422 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
423 paint.setXfermode(NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 paint.setColorFilter(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000425
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 // rectangle
rmistry@google.comae933ce2012-08-23 18:19:56 +0000427 paint.setStyle(SkPaint::kFill_Style);
428 canvas->translate(SkIntToScalar(50), 0);
429 paint.setColor(SK_ColorYELLOW);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430 paint.setShader(linear)->unref();
431 paint.setPathEffect(pathEffectTest())->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000432 canvas->drawRect(rect, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 paint.setPathEffect(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000434
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 // circle w/ emboss & transparent (exercises 3dshader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000436 canvas->translate(SkIntToScalar(50), 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 paint.setMaskFilter(embossFilter)->unref();
438 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000439 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 paint.setShader(transparentShader)->unref();
441 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000442 canvas->translate(0, SkIntToScalar(-10));
443
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 // path
rmistry@google.comae933ce2012-08-23 18:19:56 +0000445 canvas->translate(SkIntToScalar(50), 0);
446 paint.setColor(SK_ColorRED);
447 paint.setStyle(SkPaint::kStroke_Style);
448 paint.setStrokeWidth(SkIntToScalar(5));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 paint.setShader(radial)->unref();
450 paint.setMaskFilter(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000451 canvas->drawPath(path, paint);
452
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453 paint.setShader(NULL);
454 // bitmap, sprite
rmistry@google.comae933ce2012-08-23 18:19:56 +0000455 canvas->translate(SkIntToScalar(50), 0);
456 paint.setStyle(SkPaint::kFill_Style);
457 canvas->drawBitmap(fBug, left, top, &paint);
458 canvas->translate(SkIntToScalar(30), 0);
459 canvas->drawSprite(fTb,
reed@google.come1ca7052013-12-17 19:22:07 +0000460 SkScalarRoundToInt(canvas->getTotalMatrix().getTranslateX()),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 spriteOffset + 10, &paint);
462
rmistry@google.comae933ce2012-08-23 18:19:56 +0000463 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464 paint.setShader(shaderTest())->unref(); // test compose shader
rmistry@google.comae933ce2012-08-23 18:19:56 +0000465 canvas->drawRect(rect2, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 paint.setShader(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000467
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468 canvas->restore();
469 // text
reed@android.com8a1c16f2008-12-17 15:59:43 +0000470 canvas->translate(0, SkIntToScalar(60));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000471 canvas->save();
472 paint.setColor(SK_ColorGRAY);
473 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
474 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
475
476 canvas->translate(SkIntToScalar(50), 0);
477 paint.setColor(SK_ColorCYAN);
478 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
479
480 canvas->translate(SkIntToScalar(30), 0);
481 paint.setColor(SK_ColorMAGENTA);
482 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
483 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
484 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
485 canvas->translate(0, SkIntToScalar(20));
486 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
487 canvas->restore();
488
489 canvas->translate(0, SkIntToScalar(60));
490 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491 canvas->restore();
492 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000493
reed@google.com4d5c26d2013-01-08 16:17:50 +0000494 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 fClickPt.set(x, y);
496 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000497 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000499
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000500 SkPathEffect* pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
502 SkScalar gPhase = 0;
503 SkPath path;
504 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
505 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
506 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
507 path.close();
508 path.offset(SkIntToScalar(-6), 0);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000509 SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 gPhase, SkPath1DPathEffect::kRotate_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000511 SkPathEffect* inner = SkDiscretePathEffect::Create(SkIntToScalar(2),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000513 SkPathEffect* result = SkComposePathEffect::Create(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514 outer->unref();
515 inner->unref();
516 return result;
517 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000518
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000519 SkShader* shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000520 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000522 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 2, SkShader::kClamp_TileMode);
524 pts[1].set(0, SkIntToScalar(100));
525 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000526 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527 2, SkShader::kClamp_TileMode);
reed@android.com048522d2009-06-23 12:19:41 +0000528 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
530 shaderA->unref();
531 shaderB->unref();
532 mode->unref();
533 return result;
534 }
535
536 virtual void startTest() {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000537 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
538 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
539 SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
540 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000541
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000542 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000543 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 drawOneRaster(canvas);
545 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000546
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000547 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549
550 SkScalar x = SkIntToScalar(20);
551 SkScalar y = SkIntToScalar(40);
552 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000553
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554 paint.setAntiAlias(true);
555 paint.setTextSize(SkIntToScalar(48));
reed@android.comaa5a7db2009-05-27 01:20:10 +0000556 paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
557 SkTypeface::kBold));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558
559 SkString str("GOOGLE");
560
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000561 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000562 apply_shader(&paint, (int)i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000563
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564 // paint.setMaskFilter(NULL);
565 // paint.setColor(SK_ColorBLACK);
566
567#if 01
568 int index = i % SK_ARRAY_COUNT(gLightingColors);
569 paint.setColorFilter(SkColorFilter::CreateLightingFilter(
570 gLightingColors[index].fMul,
571 gLightingColors[index].fAdd))->unref();
572#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000573
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 canvas->drawText(str.c_str(), str.size(), x, y, paint);
575 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
576 paint.setStyle(SkPaint::kStroke_Style);
577 canvas->drawOval(oval, paint);
578 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579
580 y += paint.getFontSpacing();
581 }
582
583 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000584
reed@android.com6b82d1a2009-06-03 02:35:01 +0000585 if (1) {
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000586 SkAutoTUnref<SkAvoidXfermode> mode(SkAvoidXfermode::Create(SK_ColorWHITE, 0xFF,
587 SkAvoidXfermode::kTargetColor_Mode));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588 SkPaint paint;
589 x += SkIntToScalar(20);
590 SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000591 paint.setXfermode(mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000592 paint.setColor(SK_ColorGREEN);
593 paint.setAntiAlias(true);
594 canvas->drawOval(r, paint);
595 }
596 }
597
598private:
599 SkPoint fClickPt;
600 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000601 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602};
603
604//////////////////////////////////////////////////////////////////////////////
605
606static SkView* MyFactory() { return new DemoView; }
607static SkViewRegister reg(MyFactory);