blob: 16390d8fb4efec346525eb355c561a3b60409500 [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"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +000012#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +000014#include "SkRandom.h"
15
reed@google.com045e62d2011-10-24 12:19:46 +000016#define W 150
reed@google.com3bcf8d32011-10-10 15:42:36 +000017#define H 200
18
reed@google.com045e62d2011-10-24 12:19:46 +000019static void show_text(SkCanvas* canvas, bool doAA) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000020 SkRandom rand;
reed@google.com3bcf8d32011-10-10 15:42:36 +000021 SkPaint paint;
reed@google.com045e62d2011-10-24 12:19:46 +000022 paint.setAntiAlias(doAA);
23 paint.setLCDRenderText(true);
reed@google.com3bcf8d32011-10-10 15:42:36 +000024 paint.setTextSize(SkIntToScalar(20));
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@google.com045e62d2011-10-24 12:19:46 +000026 for (int i = 0; i < 200; ++i) {
reed@google.com3bcf8d32011-10-10 15:42:36 +000027 paint.setColor((SK_A32_MASK << SK_A32_SHIFT) | rand.nextU());
28 canvas->drawText("Hamburgefons", 12,
reed@google.com045e62d2011-10-24 12:19:46 +000029 rand.nextSScalar1() * W, rand.nextSScalar1() * H + 20,
reed@google.com3bcf8d32011-10-10 15:42:36 +000030 paint);
31 }
32}
33
reed@google.com045e62d2011-10-24 12:19:46 +000034static void show_fill(SkCanvas* canvas, bool doAA) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000035 SkRandom rand;
reed@google.com3bcf8d32011-10-10 15:42:36 +000036 SkPaint paint;
reed@google.com045e62d2011-10-24 12:19:46 +000037 paint.setAntiAlias(doAA);
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
reed@google.com045e62d2011-10-24 12:19:46 +000039 for (int i = 0; i < 50; ++i) {
reed@google.com3bcf8d32011-10-10 15:42:36 +000040 SkRect r;
41 SkPath p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@google.com3bcf8d32011-10-10 15:42:36 +000043 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
44 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
reed@google.com3bcf8d32011-10-10 15:42:36 +000045 paint.setColor(rand.nextU());
46 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@google.com3bcf8d32011-10-10 15:42:36 +000048 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
49 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
reed@google.com3bcf8d32011-10-10 15:42:36 +000050 paint.setColor(rand.nextU());
51 p.addOval(r);
52 canvas->drawPath(p, paint);
53 }
54}
55
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000056static SkScalar randRange(SkRandom& rand, SkScalar min, SkScalar max) {
reed@google.com045e62d2011-10-24 12:19:46 +000057 SkASSERT(min <= max);
58 return min + SkScalarMul(rand.nextUScalar1(), max - min);
59}
60
61static void show_stroke(SkCanvas* canvas, bool doAA, SkScalar strokeWidth, int n) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000062 SkRandom rand;
reed@google.com045e62d2011-10-24 12:19:46 +000063 SkPaint paint;
64 paint.setAntiAlias(doAA);
65 paint.setStyle(SkPaint::kStroke_Style);
66 paint.setStrokeWidth(strokeWidth);
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@google.com045e62d2011-10-24 12:19:46 +000068 for (int i = 0; i < n; ++i) {
69 SkRect r;
70 SkPath p;
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@google.com045e62d2011-10-24 12:19:46 +000072 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
73 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
74 paint.setColor(rand.nextU());
75 canvas->drawRect(r, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@google.com045e62d2011-10-24 12:19:46 +000077 r.setXYWH(rand.nextSScalar1() * W, rand.nextSScalar1() * H,
78 rand.nextUScalar1() * W, rand.nextUScalar1() * H);
79 paint.setColor(rand.nextU());
80 p.addOval(r);
81 canvas->drawPath(p, paint);
82
83 const SkScalar minx = -SkIntToScalar(W)/4;
84 const SkScalar maxx = 5*SkIntToScalar(W)/4;
85 const SkScalar miny = -SkIntToScalar(H)/4;
86 const SkScalar maxy = 5*SkIntToScalar(H)/4;
87 paint.setColor(rand.nextU());
88 canvas->drawLine(randRange(rand, minx, maxx), randRange(rand, miny, maxy),
89 randRange(rand, minx, maxx), randRange(rand, miny, maxy),
90 paint);
91 }
92}
93
94static void show_hair(SkCanvas* canvas, bool doAA) {
95 show_stroke(canvas, doAA, 0, 150);
96}
97
98static void show_thick(SkCanvas* canvas, bool doAA) {
99 show_stroke(canvas, doAA, SkIntToScalar(5), 50);
100}
101
102typedef void (*CanvasProc)(SkCanvas*, bool);
103
104#include "SkAAClip.h"
reed@google.com3bcf8d32011-10-10 15:42:36 +0000105
106class 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();
robertphillips@google.com67a6db32012-05-03 18:05:18 +0000146 canvas->clipPath(clipPath, SkRegion::kIntersect_Op, 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);