blob: 82466ba137ce3d6d8d803c66e019135d36b2bf8b [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"
16
Ben Wagner8a1036c2016-11-09 15:00:49 -050017constexpr int W = 150;
18constexpr int H = 200;
reed@google.com3bcf8d32011-10-10 15:42:36 +000019
reed@google.com045e62d2011-10-24 12:19:46 +000020static void show_text(SkCanvas* canvas, bool doAA) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000021 SkRandom rand;
reed@google.com3bcf8d32011-10-10 15:42:36 +000022 SkPaint paint;
reed@google.com045e62d2011-10-24 12:19:46 +000023 paint.setAntiAlias(doAA);
24 paint.setLCDRenderText(true);
reed@google.com3bcf8d32011-10-10 15:42:36 +000025 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@google.com045e62d2011-10-24 12:19:46 +000027 for (int i = 0; i < 200; ++i) {
reed@google.com3bcf8d32011-10-10 15:42:36 +000028 paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU());
29 canvas->drawText("Hamburgefons", 12,
reed@google.com045e62d2011-10-24 12:19:46 +000030 rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20,
reed@google.com3bcf8d32011-10-10 15:42:36 +000031 paint);
32 }
33}
34
reed@google.com045e62d2011-10-24 12:19:46 +000035static void show_fill(SkCanvas* canvas, bool doAA) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000036 SkRandom rand;
reed@google.com3bcf8d32011-10-10 15:42:36 +000037 SkPaint paint;
reed@google.com045e62d2011-10-24 12:19:46 +000038 paint.setAntiAlias(doAA);
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
reed@google.com045e62d2011-10-24 12:19:46 +000040 for (int i = 0; i < 50; ++i) {
reed@google.com3bcf8d32011-10-10 15:42:36 +000041 SkRect r;
42 SkPath p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@google.com3bcf8d32011-10-10 15:42:36 +000044 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
45 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
reed@google.com3bcf8d32011-10-10 15:42:36 +000046 paint.setColor(rand.nextU());
47 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
reed@google.com3bcf8d32011-10-10 15:42:36 +000049 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
50 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
reed@google.com3bcf8d32011-10-10 15:42:36 +000051 paint.setColor(rand.nextU());
52 p.addOval(r);
53 canvas->drawPath(p, paint);
54 }
55}
56
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000057static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) {
reed@google.com045e62d2011-10-24 12:19:46 +000058 SkASSERT(min <= max);
59 return min + SkScalarMul(rand.nextUScalar1(), max - min);
60}
61
62static void show_stroke(SkCanvas* canvas, bool doAA, SkScalar strokeWidth, int n) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000063 SkRandom rand;
reed@google.com045e62d2011-10-24 12:19:46 +000064 SkPaint paint;
65 paint.setAntiAlias(doAA);
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setStrokeWidth(strokeWidth);
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@google.com045e62d2011-10-24 12:19:46 +000069 for (int i = 0; i < n; ++i) {
70 SkRect r;
71 SkPath p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@google.com045e62d2011-10-24 12:19:46 +000073 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
74 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
75 paint.setColor(rand.nextU());
76 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000077
reed@google.com045e62d2011-10-24 12:19:46 +000078 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
79 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
80 paint.setColor(rand.nextU());
81 p.addOval(r);
82 canvas->drawPath(p, paint);
83
84 const SkScalar minx = -SkIntToScalar(W)/4;
85 const SkScalar maxx = 5*SkIntToScalar(W)/4;
86 const SkScalar miny = -SkIntToScalar(H)/4;
87 const SkScalar maxy = 5*SkIntToScalar(H)/4;
88 paint.setColor(rand.nextU());
89 canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy),
90 randRange(rand, minx, maxx), randRange(rand, miny, maxy),
91 paint);
92 }
93}
94
95static void show_hair(SkCanvas* canvas, bool doAA) {
96 show_stroke(canvas, doAA, 0, 150);
97}
98
99static void show_thick(SkCanvas* canvas, bool doAA) {
100 show_stroke(canvas, doAA, SkIntToScalar(5), 50);
101}
102
103typedef void (*CanvasProc)(SkCanvas*, bool);
104
reed@google.com3bcf8d32011-10-10 15:42:36 +0000105class ClipView : public SampleView {
106public:
107 ClipView() {
reed@google.com045e62d2011-10-24 12:19:46 +0000108 SkAAClip clip;
109 SkIRect r = { -2, -3, 842, 18 };
110 clip.setRect(r);
reed@google.com3bcf8d32011-10-10 15:42:36 +0000111 }
112
113 virtual ~ClipView() {
114 }
115
116protected:
117 // overrides from SkEventSink
118 virtual bool onQuery(SkEvent* evt) {
119 if (SampleCode::TitleQ(*evt)) {
120 SampleCode::TitleR(evt, "Clip");
121 return true;
122 }
123 return this->INHERITED::onQuery(evt);
124 }
125
126 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com045e62d2011-10-24 12:19:46 +0000127 canvas->drawColor(SK_ColorWHITE);
reed@google.com3bcf8d32011-10-10 15:42:36 +0000128 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
129
130 static const CanvasProc gProc[] = {
reed@google.com045e62d2011-10-24 12:19:46 +0000131 show_text, show_thick, show_hair, show_fill
reed@google.com3bcf8d32011-10-10 15:42:36 +0000132 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@google.com3bcf8d32011-10-10 15:42:36 +0000134 SkRect r = { 0, 0, SkIntToScalar(W), SkIntToScalar(H) };
135 SkPath clipPath;
136 r.inset(SK_Scalar1 / 4, SK_Scalar1 / 4);
reed@google.com045e62d2011-10-24 12:19:46 +0000137 clipPath.addRoundRect(r, SkIntToScalar(20), SkIntToScalar(20));
138
139// clipPath.toggleInverseFillType();
reed@google.com3bcf8d32011-10-10 15:42:36 +0000140
141 for (int aa = 0; aa <= 1; ++aa) {
142 canvas->save();
143 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); ++i) {
144 canvas->save();
Mike Reedc1f77742016-12-09 09:00:50 -0500145 canvas->clipPath(clipPath, kIntersect_SkClipOp, SkToBool(aa));
reed@google.com045e62d2011-10-24 12:19:46 +0000146// canvas->drawColor(SK_ColorWHITE);
147 gProc[i](canvas, SkToBool(aa));
reed@google.com3bcf8d32011-10-10 15:42:36 +0000148 canvas->restore();
149 canvas->translate(W * SK_Scalar1 * 8 / 7, 0);
150 }
151 canvas->restore();
152 canvas->translate(0, H * SK_Scalar1 * 8 / 7);
153 }
154 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@google.com3bcf8d32011-10-10 15:42:36 +0000156private:
157 typedef SampleView INHERITED;
158};
159
160//////////////////////////////////////////////////////////////////////////////
161
162static SkView* MyFactory() { return new ClipView; }
163static SkViewRegister reg(MyFactory);