blob: 8c1ace564a1c34495789e5e4f18c0de305eaf758 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
10#include "SkView.h"
11#include "Sk1DPathEffect.h"
12#include "Sk2DPathEffect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkBlurMaskFilter.h"
reed0daf5dd2016-01-11 12:34:04 -080014#include "SkColorMatrixFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkColorPriv.h"
16#include "SkCornerPathEffect.h"
17#include "SkDashPathEffect.h"
18#include "SkDiscretePathEffect.h"
19#include "SkEmbossMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000020#include "SkReadBuffer.h"
21#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkGradientShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkLayerRasterizer.h"
24#include "SkMath.h"
25#include "SkPath.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000026#include "SkPictureRecorder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027#include "SkRegion.h"
28#include "SkShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#include "SkCornerPathEffect.h"
30#include "SkPathMeasure.h"
31#include "SkPicture.h"
32#include "SkRandom.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000033#include "SkTypeface.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000034#include "SkUtils.h"
35#include "SkXfermode.h"
36
37#include <math.h>
msarettd15750c2016-03-18 15:48:49 -070038#include "DecodeFile.h"
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000040static inline SkPMColor rgb2gray(SkPMColor c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 unsigned r = SkGetPackedR32(c);
42 unsigned g = SkGetPackedG32(c);
43 unsigned b = SkGetPackedB32(c);
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@android.comf2b98d62010-12-20 18:26:13 +000045 unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
48}
49
50class SkGrayScaleColorFilter : public SkColorFilter {
51public:
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000052 virtual void filterSpan(const SkPMColor src[], int count,
mtklein36352bf2015-03-25 18:17:31 -070053 SkPMColor result[]) const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 for (int i = 0; i < count; i++)
55 result[i] = rgb2gray(src[i]);
56 }
57};
58
59class SkChannelMaskColorFilter : public SkColorFilter {
60public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000061 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
63 }
64
mike@reedtribe.org61490fa2012-12-24 14:38:46 +000065 virtual void filterSpan(const SkPMColor src[], int count,
mtklein36352bf2015-03-25 18:17:31 -070066 SkPMColor result[]) const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 SkPMColor mask = fMask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000068 for (int i = 0; i < count; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 result[i] = src[i] & mask;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000070 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073private:
74 SkPMColor fMask;
75};
76
77///////////////////////////////////////////////////////////
78
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000079static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reedefdfd512016-04-04 10:02:58 -070080 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
81 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)),
82 SkBlurMaskFilter::kNone_BlurFlag));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000083 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
halcanary96fcdcc2015-08-27 07:41:13 -070085 p.setMaskFilter(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 p.setStyle(SkPaint::kStroke_Style);
87 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000088 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90 p.setAlpha(0x11);
91 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -070092 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000093 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094}
95
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000096static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
97 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
99 p.setAlpha(0x40);
reed374772b2016-10-05 17:33:02 -0700100 p.setBlendMode(SkBlendMode::kSrc);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 p.setStyle(SkPaint::kStroke_Style);
102 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000103 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000106static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 p.setStyle(SkPaint::kStrokeAndFill_Style);
108 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000109 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110
111 p.setStyle(SkPaint::kStroke_Style);
112 p.setStrokeWidth(SK_Scalar1*3/2);
reed374772b2016-10-05 17:33:02 -0700113 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000114 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115}
116
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000117static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 p.setStyle(SkPaint::kStroke_Style);
119 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000120 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121
122 p.setAlpha(0x20);
123 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -0700124 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000125 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126}
127
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000128static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000130 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131
132 p.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700133 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000134 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
reed374772b2016-10-05 17:33:02 -0700136 p.setBlendMode(SkBlendMode::kSrcOver);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000137 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138}
139
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000140static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
141 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142
reeda4393342016-03-18 11:22:57 -0700143 p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
reed374772b2016-10-05 17:33:02 -0700144 p.setBlendMode(SkBlendMode::kSrcOut);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000145 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146}
147
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000148static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
149 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000152 SkLayerRasterizer::Builder rastBuilder2;
153 r5(&rastBuilder2, p);
reed7b380d02016-03-21 13:25:16 -0700154 p.setRasterizer(rastBuilder2.detach());
reed374772b2016-10-05 17:33:02 -0700155 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000156 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157}
158
159class Dot2DPathEffect : public Sk2DPathEffect {
160public:
161 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix)
162 : Sk2DPathEffect(matrix), fRadius(radius) {}
163
djsollen@google.comba28d032012-03-26 17:57:35 +0000164 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165
166protected:
mtklein36352bf2015-03-25 18:17:31 -0700167 void next(const SkPoint& loc, int u, int v, SkPath* dst) const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 dst->addCircle(loc.fX, loc.fY, fRadius);
169 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000170
mtklein36352bf2015-03-25 18:17:31 -0700171 void flatten(SkWriteBuffer& buffer) const override {
djsollen@google.com54924242012-03-29 15:18:04 +0000172 this->INHERITED::flatten(buffer);
173 buffer.writeScalar(fRadius);
174 }
175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176private:
177 SkScalar fRadius;
178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 typedef Sk2DPathEffect INHERITED;
180};
181
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000182static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 SkMatrix lattice;
184 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
185 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700186 p.setPathEffect(sk_make_sp<Dot2DPathEffect>(SK_Scalar1*4, lattice));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000187 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188}
189
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000190static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
191 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000192
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 SkMatrix lattice;
194 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
195 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700196 p.setPathEffect(sk_make_sp<Dot2DPathEffect>(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700197 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000198 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199
halcanary96fcdcc2015-08-27 07:41:13 -0700200 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700201 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 p.setStyle(SkPaint::kStroke_Style);
203 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000204 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205}
206
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000207static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
208 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 SkMatrix lattice;
211 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
212 lattice.postRotate(SkIntToScalar(30), 0, 0);
reeda4393342016-03-18 11:22:57 -0700213 p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700214 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000215 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216
halcanary96fcdcc2015-08-27 07:41:13 -0700217 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700218 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 p.setStyle(SkPaint::kStroke_Style);
220 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000221 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222}
223
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000224typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
226static const raster_proc gRastProcs[] = {
227 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
228};
229
230static const struct {
231 SkColor fMul, fAdd;
232} gLightingColors[] = {
233 { 0x808080, 0x800000 }, // general case
234 { 0x707070, 0x707070 }, // no-pin case
235 { 0xFFFFFF, 0x800000 }, // just-add case
236 { 0x808080, 0x000000 }, // just-mul case
237 { 0xFFFFFF, 0x000000 } // identity case
238};
239
rmistry@google.comae933ce2012-08-23 18:19:56 +0000240static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 raster_proc proc = gRastProcs[index];
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000242 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000244 SkLayerRasterizer::Builder rastBuilder;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245
246 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000247 proc(&rastBuilder, p);
reed7b380d02016-03-21 13:25:16 -0700248 paint->setRasterizer(rastBuilder.detach());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 }
250
251#if 1
252 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reedefdfd512016-04-04 10:02:58 -0700253 paint->setMaskFilter(SkBlurMaskFilter::MakeEmboss(
robertphillips@google.comb7061172013-09-06 14:16:12 +0000254 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), dir,
reedefdfd512016-04-04 10:02:58 -0700255 SK_Scalar1/4, SkIntToScalar(4)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 paint->setColor(SK_ColorBLUE);
257#endif
258}
259
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000260class DemoView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261public:
reed@google.com2f3dc9d2011-05-02 17:33:45 +0000262 DemoView() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000263
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264protected:
265 // overrides from SkEventSink
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000266 virtual bool onQuery(SkEvent* evt) {
267 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 SampleCode::TitleR(evt, "Demo");
269 return true;
270 }
271 return this->INHERITED::onQuery(evt);
272 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000273
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000274 virtual bool onClick(Click* click) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 return this->INHERITED::onClick(click);
276 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000277
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000278 void makePath(SkPath& path) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
280 SkPath::kCCW_Direction);
281 for (int index = 0; index < 10; index++) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000282 SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f);
283 SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 x *= index & 1 ? 7 : 14;
285 y *= index & 1 ? 7 : 14;
286 x += SkIntToScalar(20);
287 y += SkIntToScalar(20);
288 if (index == 0)
289 path.moveTo(x, y);
290 else
291 path.lineTo(x, y);
292 }
293 path.close();
294 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000295
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000296 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 canvas->save();
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000298 this->drawPicture(canvas, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 canvas->restore();
300
301 {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000302 SkPictureRecorder recorder;
303 {
halcanary96fcdcc2015-08-27 07:41:13 -0700304 SkCanvas* record = recorder.beginRecording(320, 480, nullptr, 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000305 this->drawPicture(record, 120);
306 }
reedca2622b2016-03-18 07:25:55 -0700307 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 canvas->translate(0, SkIntToScalar(120));
310
311 SkRect clip;
312 clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
313 do {
314 canvas->save();
315 canvas->clipRect(clip);
robertphillipsc5ba71d2014-09-04 08:42:50 -0700316 picture->playback(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 canvas->restore();
318 if (clip.fRight < SkIntToScalar(320))
319 clip.offset(SkIntToScalar(160), 0);
320 else if (clip.fBottom < SkIntToScalar(480))
321 clip.offset(-SkIntToScalar(320), SkIntToScalar(160));
322 else
323 break;
324 } while (true);
325 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000327
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000328 void drawPicture(SkCanvas* canvas, int spriteOffset) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000329 SkMatrix matrix; matrix.reset();
330 SkPaint paint;
331 SkPath path;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 SkPoint start = {0, 0};
333 SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000334 SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) };
335 SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) };
336 SkScalar left = 0, top = 0, x = 0, y = 0;
reed@google.com7fa2a652014-01-27 13:42:58 +0000337 int index;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000338
339 char ascii[] = "ascii...";
reed@google.com7fa2a652014-01-27 13:42:58 +0000340 int asciiLength = sizeof(ascii) - 1;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000341 char utf8[] = "utf8" "\xe2\x80\xa6";
342 short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 };
343 short utf16simple[] = {'u', 't', 'f', '1', '6', '!' };
344
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 makePath(path);
346 SkTDArray<SkPoint>(pos);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000347 pos.setCount(asciiLength);
348 for (index = 0; index < asciiLength; index++)
349 pos[index].set(SkIntToScalar((unsigned int)index * 10),
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000350 SkIntToScalar((unsigned int)index * 2));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 SkTDArray<SkPoint>(pos2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000352 pos2.setCount(asciiLength);
353 for (index = 0; index < asciiLength; index++)
354 pos2[index].set(SkIntToScalar((unsigned int)index * 10),
tomhudson@google.comffe39bd2012-05-17 15:38:00 +0000355 SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000356
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 // shaders
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000358 SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
halcanary96fcdcc2015-08-27 07:41:13 -0700360 SkScalar* linearPos = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 int linearCount = 2;
362 SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
reed8a21c9f2016-03-08 18:50:00 -0800363 auto linear = SkGradientShader::MakeLinear(linearPoints,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000364 linearColors, linearPos, linearCount, linearMode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365
366 SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
367 SkScalar radialRadius = SkIntToScalar(25);
368 SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
369 SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
370 int radialCount = 3;
371 SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
reed8a21c9f2016-03-08 18:50:00 -0800372 auto radial = SkGradientShader::MakeRadial(radialCenter,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 radialRadius, radialColors, radialPos, radialCount,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000374 radialMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000375
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 SkEmbossMaskFilter::Light light;
377 light.fDirection[0] = SK_Scalar1/2;
378 light.fDirection[1] = SK_Scalar1/2;
379 light.fDirection[2] = SK_Scalar1/3;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000380 light.fAmbient = 0x48;
381 light.fSpecular = 0x80;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000382
reedd053ce92016-03-22 10:17:23 -0700383 auto lightingFilter = SkColorMatrixFilter::MakeLightingFilter(
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384 0xff89bc45, 0xff112233);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000385
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000387 canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
reed@google.com9cfc83c2013-07-22 17:18:18 +0000388 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -0700389 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000390 // !!! draw through a clip
391 paint.setColor(SK_ColorLTGRAY);
392 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
394 canvas->clipRect(clip);
reed8a21c9f2016-03-08 18:50:00 -0800395 paint.setShader(SkShader::MakeBitmapShader(fTx,
396 SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000397 canvas->drawPaint(paint);
398 canvas->save();
399
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 // line (exercises xfermode, colorShader, colorFilter, filterShader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000401 paint.setColor(SK_ColorGREEN);
402 paint.setStrokeWidth(SkIntToScalar(10));
403 paint.setStyle(SkPaint::kStroke_Style);
reed374772b2016-10-05 17:33:02 -0700404 paint.setBlendMode(SkBlendMode::kXor);
reedd053ce92016-03-22 10:17:23 -0700405 paint.setColorFilter(lightingFilter);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000406 canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
reed374772b2016-10-05 17:33:02 -0700407 paint.setBlendMode(SkBlendMode::kSrcOver);
halcanary96fcdcc2015-08-27 07:41:13 -0700408 paint.setColorFilter(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000409
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 // rectangle
rmistry@google.comae933ce2012-08-23 18:19:56 +0000411 paint.setStyle(SkPaint::kFill_Style);
412 canvas->translate(SkIntToScalar(50), 0);
413 paint.setColor(SK_ColorYELLOW);
reed8a21c9f2016-03-08 18:50:00 -0800414 paint.setShader(linear);
reeda4393342016-03-18 11:22:57 -0700415 paint.setPathEffect(pathEffectTest());
rmistry@google.comae933ce2012-08-23 18:19:56 +0000416 canvas->drawRect(rect, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700417 paint.setPathEffect(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000418
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 // circle w/ emboss & transparent (exercises 3dshader)
rmistry@google.comae933ce2012-08-23 18:19:56 +0000420 canvas->translate(SkIntToScalar(50), 0);
reedefdfd512016-04-04 10:02:58 -0700421 paint.setMaskFilter(SkEmbossMaskFilter::Make(
422 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5), light));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000424 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed56263c72015-06-05 11:31:26 -0700425// paint.setShader(transparentShader)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 canvas->drawOval(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000427 canvas->translate(0, SkIntToScalar(-10));
428
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 // path
rmistry@google.comae933ce2012-08-23 18:19:56 +0000430 canvas->translate(SkIntToScalar(50), 0);
431 paint.setColor(SK_ColorRED);
432 paint.setStyle(SkPaint::kStroke_Style);
433 paint.setStrokeWidth(SkIntToScalar(5));
reed8a21c9f2016-03-08 18:50:00 -0800434 paint.setShader(radial);
halcanary96fcdcc2015-08-27 07:41:13 -0700435 paint.setMaskFilter(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000436 canvas->drawPath(path, paint);
437
halcanary96fcdcc2015-08-27 07:41:13 -0700438 paint.setShader(nullptr);
reed6f45c172015-11-04 08:34:07 -0800439 // bitmap
rmistry@google.comae933ce2012-08-23 18:19:56 +0000440 canvas->translate(SkIntToScalar(50), 0);
441 paint.setStyle(SkPaint::kFill_Style);
442 canvas->drawBitmap(fBug, left, top, &paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443
rmistry@google.comae933ce2012-08-23 18:19:56 +0000444 canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
reed8a21c9f2016-03-08 18:50:00 -0800445 paint.setShader(shaderTest()); // test compose shader
rmistry@google.comae933ce2012-08-23 18:19:56 +0000446 canvas->drawRect(rect2, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700447 paint.setShader(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000448
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 canvas->restore();
450 // text
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451 canvas->translate(0, SkIntToScalar(60));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000452 canvas->save();
453 paint.setColor(SK_ColorGRAY);
454 canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
455 canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
456
457 canvas->translate(SkIntToScalar(50), 0);
458 paint.setColor(SK_ColorCYAN);
459 canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
460
461 canvas->translate(SkIntToScalar(30), 0);
462 paint.setColor(SK_ColorMAGENTA);
463 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
464 matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
465 canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
466 canvas->translate(0, SkIntToScalar(20));
467 canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
468 canvas->restore();
469
470 canvas->translate(0, SkIntToScalar(60));
471 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000472 canvas->restore();
473 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000474
reed@google.com4d5c26d2013-01-08 16:17:50 +0000475 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476 fClickPt.set(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700477 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000478 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000480
reeda4393342016-03-18 11:22:57 -0700481 sk_sp<SkPathEffect> pathEffectTest() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482 static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
483 SkScalar gPhase = 0;
484 SkPath path;
485 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
486 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
487 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
488 path.close();
489 path.offset(SkIntToScalar(-6), 0);
reeda4393342016-03-18 11:22:57 -0700490 auto outer = SkPath1DPathEffect::Make(path, SkIntToScalar(12),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491 gPhase, SkPath1DPathEffect::kRotate_Style);
reeda4393342016-03-18 11:22:57 -0700492 auto inner = SkDiscretePathEffect::Make(SkIntToScalar(2),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
reeda4393342016-03-18 11:22:57 -0700494 return SkComposePathEffect::Make(outer, inner);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 }
reed@google.com963a8fa2012-05-10 13:04:59 +0000496
reed8a21c9f2016-03-08 18:50:00 -0800497 sk_sp<SkShader> shaderTest() {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000498 SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499 SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
reed8a21c9f2016-03-08 18:50:00 -0800500 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501 2, SkShader::kClamp_TileMode);
502 pts[1].set(0, SkIntToScalar(100));
503 SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
reed8a21c9f2016-03-08 18:50:00 -0800504 auto shaderB = SkGradientShader::MakeLinear(pts, colors2, nullptr,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000505 2, SkShader::kClamp_TileMode);
reedcfb6bdf2016-03-29 11:32:50 -0700506 return SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
507 SkXfermode::Make(SkXfermode::kDstIn_Mode));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508 }
509
510 virtual void startTest() {
msarettd15750c2016-03-18 15:48:49 -0700511 decode_file("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
512 decode_file("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
513 decode_file("/Users/caryclark/Desktop/05psp04.gif", &fTx);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000514 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000516 void drawRaster(SkCanvas* canvas) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000517 for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 drawOneRaster(canvas);
519 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000520
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000521 void drawOneRaster(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000522 canvas->save();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523
524 SkScalar x = SkIntToScalar(20);
525 SkScalar y = SkIntToScalar(40);
526 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000527
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 paint.setAntiAlias(true);
529 paint.setTextSize(SkIntToScalar(48));
mbocee6a9912016-05-31 11:42:36 -0700530 paint.setTypeface(SkTypeface::MakeFromName("sans-serif",
531 SkFontStyle::FromOldStyle(SkTypeface::kBold)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532
533 SkString str("GOOGLE");
534
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000535 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000536 apply_shader(&paint, (int)i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000537
halcanary96fcdcc2015-08-27 07:41:13 -0700538 // paint.setMaskFilter(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 // paint.setColor(SK_ColorBLACK);
540
541#if 01
542 int index = i % SK_ARRAY_COUNT(gLightingColors);
reedd053ce92016-03-22 10:17:23 -0700543 paint.setColorFilter(SkColorMatrixFilter::MakeLightingFilter(
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 gLightingColors[index].fMul,
reedd053ce92016-03-22 10:17:23 -0700545 gLightingColors[index].fAdd));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546#endif
rmistry@google.comae933ce2012-08-23 18:19:56 +0000547
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 canvas->drawText(str.c_str(), str.size(), x, y, paint);
549 SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
550 paint.setStyle(SkPaint::kStroke_Style);
551 canvas->drawOval(oval, paint);
552 paint.setStyle(SkPaint::kFill_Style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553
554 y += paint.getFontSpacing();
555 }
556
557 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 }
559
560private:
561 SkPoint fClickPt;
562 SkBitmap fBug, fTb, fTx;
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000563 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564};
565
566//////////////////////////////////////////////////////////////////////////////
567
568static SkView* MyFactory() { return new DemoView; }
569static SkViewRegister reg(MyFactory);