blob: 05b5a25e5b775e0254625cb00516b3ee0b30f986 [file] [log] [blame]
reed@google.com3bcf8d32011-10-10 15:42:36 +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 */
7
8#include "SampleCode.h"
Ben Wagner8a1036c2016-11-09 15:00:49 -05009#include "SkAAClip.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkColorPriv.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +000013#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +000015#include "SkRandom.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050016#include "SkClipOpPriv.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +000017
Ben Wagner8a1036c2016-11-09 15:00:49 -050018constexpr int W = 150;
19constexpr int H = 200;
reed@google.com3bcf8d32011-10-10 15:42:36 +000020
reed@google.com045e62d2011-10-24 12:19:46 +000021static void show_text(SkCanvas* canvas, bool doAA) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000022 SkRandom rand;
reed@google.com3bcf8d32011-10-10 15:42:36 +000023 SkPaint paint;
reed@google.com045e62d2011-10-24 12:19:46 +000024 paint.setAntiAlias(doAA);
25 paint.setLCDRenderText(true);
reed@google.com3bcf8d32011-10-10 15:42:36 +000026 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +000027
reed@google.com045e62d2011-10-24 12:19:46 +000028 for (int i = 0; i < 200; ++i) {
reed@google.com3bcf8d32011-10-10 15:42:36 +000029 paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU());
Cary Clark2a475ea2017-04-28 15:35:12 -040030 canvas->drawString("Hamburgefons",
reed@google.com045e62d2011-10-24 12:19:46 +000031 rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20,
reed@google.com3bcf8d32011-10-10 15:42:36 +000032 paint);
33 }
34}
35
reed@google.com045e62d2011-10-24 12:19:46 +000036static void show_fill(SkCanvas* canvas, bool doAA) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000037 SkRandom rand;
reed@google.com3bcf8d32011-10-10 15:42:36 +000038 SkPaint paint;
reed@google.com045e62d2011-10-24 12:19:46 +000039 paint.setAntiAlias(doAA);
rmistry@google.comae933ce2012-08-23 18:19:56 +000040
reed@google.com045e62d2011-10-24 12:19:46 +000041 for (int i = 0; i < 50; ++i) {
reed@google.com3bcf8d32011-10-10 15:42:36 +000042 SkRect r;
43 SkPath p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@google.com3bcf8d32011-10-10 15:42:36 +000045 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
46 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
reed@google.com3bcf8d32011-10-10 15:42:36 +000047 paint.setColor(rand.nextU());
48 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
reed@google.com3bcf8d32011-10-10 15:42:36 +000050 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
51 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
reed@google.com3bcf8d32011-10-10 15:42:36 +000052 paint.setColor(rand.nextU());
53 p.addOval(r);
54 canvas->drawPath(p, paint);
55 }
56}
57
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000058static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) {
reed@google.com045e62d2011-10-24 12:19:46 +000059 SkASSERT(min <= max);
Mike Reeddf85c382017-02-14 10:59:19 -050060 return min + rand.nextUScalar1() * (max - min);
reed@google.com045e62d2011-10-24 12:19:46 +000061}
62
63static void show_stroke(SkCanvas* canvas, bool doAA, SkScalar strokeWidth, int n) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000064 SkRandom rand;
reed@google.com045e62d2011-10-24 12:19:46 +000065 SkPaint paint;
66 paint.setAntiAlias(doAA);
67 paint.setStyle(SkPaint::kStroke_Style);
68 paint.setStrokeWidth(strokeWidth);
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@google.com045e62d2011-10-24 12:19:46 +000070 for (int i = 0; i < n; ++i) {
71 SkRect r;
72 SkPath p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@google.com045e62d2011-10-24 12:19:46 +000074 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
75 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
76 paint.setColor(rand.nextU());
77 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@google.com045e62d2011-10-24 12:19:46 +000079 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
80 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
81 paint.setColor(rand.nextU());
82 p.addOval(r);
83 canvas->drawPath(p, paint);
84
85 const SkScalar minx = -SkIntToScalar(W)/4;
86 const SkScalar maxx = 5*SkIntToScalar(W)/4;
87 const SkScalar miny = -SkIntToScalar(H)/4;
88 const SkScalar maxy = 5*SkIntToScalar(H)/4;
89 paint.setColor(rand.nextU());
90 canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy),
91 randRange(rand, minx, maxx), randRange(rand, miny, maxy),
92 paint);
93 }
94}
95
96static void show_hair(SkCanvas* canvas, bool doAA) {
97 show_stroke(canvas, doAA, 0, 150);
98}
99
100static void show_thick(SkCanvas* canvas, bool doAA) {
101 show_stroke(canvas, doAA, SkIntToScalar(5), 50);
102}
103
104typedef void (*CanvasProc)(SkCanvas*, bool);
105
reed@google.com3bcf8d32011-10-10 15:42:36 +0000106class ClipView : public SampleView {
107public:
108 ClipView() {
reed@google.com045e62d2011-10-24 12:19:46 +0000109 SkAAClip clip;
110 SkIRect r = { -2, -3, 842, 18 };
111 clip.setRect(r);
reed@google.com3bcf8d32011-10-10 15:42:36 +0000112 }
113
114 virtual ~ClipView() {
115 }
116
117protected:
118 // overrides from SkEventSink
119 virtual bool onQuery(SkEvent* evt) {
120 if (SampleCode::TitleQ(*evt)) {
121 SampleCode::TitleR(evt, "Clip");
122 return true;
123 }
124 return this->INHERITED::onQuery(evt);
125 }
126
127 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com045e62d2011-10-24 12:19:46 +0000128 canvas->drawColor(SK_ColorWHITE);
reed@google.com3bcf8d32011-10-10 15:42:36 +0000129 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
130
131 static const CanvasProc gProc[] = {
reed@google.com045e62d2011-10-24 12:19:46 +0000132 show_text, show_thick, show_hair, show_fill
reed@google.com3bcf8d32011-10-10 15:42:36 +0000133 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
reed@google.com3bcf8d32011-10-10 15:42:36 +0000135 SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
136 SkPath clipPath;
137 r.inset(SK_Scalar1 / 4, SK_Scalar1 / 4);
reed@google.com045e62d2011-10-24 12:19:46 +0000138 clipPath.addRoundRect(r, SkIntToScalar(20), SkIntToScalar(20));
139
140// clipPath.toggleInverseFillType();
reed@google.com3bcf8d32011-10-10 15:42:36 +0000141
142 for (int aa = 0; aa <= 1; ++aa) {
143 canvas->save();
144 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); ++i) {
145 canvas->save();
Mike Reedc1f77742016-12-09 09:00:50 -0500146 canvas->clipPath(clipPath, kIntersect_SkClipOp, SkToBool(aa));
reed@google.com045e62d2011-10-24 12:19:46 +0000147// canvas->drawColor(SK_ColorWHITE);
148 gProc[i](canvas, SkToBool(aa));
reed@google.com3bcf8d32011-10-10 15:42:36 +0000149 canvas->restore();
150 canvas->translate(W * SK_Scalar1 * 8 / 7, 0);
151 }
152 canvas->restore();
153 canvas->translate(0, H * SK_Scalar1 * 8 / 7);
154 }
155 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156
reed@google.com3bcf8d32011-10-10 15:42:36 +0000157private:
158 typedef SampleView INHERITED;
159};
160
161//////////////////////////////////////////////////////////////////////////////
162
163static SkView* MyFactory() { return new ClipView; }
164static SkViewRegister reg(MyFactory);