blob: 2eabebac62ba3b2de2b160d820bdc53861636d04 [file] [log] [blame]
commit-bot@chromium.orga343c842014-01-31 14:48:58 +00001
2/*
3 * Copyright 2014 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 */
8
9#include "gm.h"
10
11#include "SkBitmap.h"
12#include "SkGradientShader.h"
13#include "SkTLList.h"
14
15static SkBitmap make_bmp(int w, int h) {
16 SkBitmap bmp;
17 bmp.allocN32Pixels(w, h, true);
18
19 SkCanvas canvas(bmp);
20 SkScalar wScalar = SkIntToScalar(w);
21 SkScalar hScalar = SkIntToScalar(h);
22
23 SkPoint pt = { wScalar / 2, hScalar / 2 };
24
25 SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
26
caryclark2e096602015-07-13 05:39:47 -070027 SkColor colors[] = { sk_tool_utils::color_to_565(SK_ColorDKGRAY),
28 sk_tool_utils::color_to_565(0xFF222255),
29 sk_tool_utils::color_to_565(0xFF331133),
30 sk_tool_utils::color_to_565(0xFF884422),
31 sk_tool_utils::color_to_565(0xFF000022), SK_ColorWHITE,
32 sk_tool_utils::color_to_565(0xFFAABBCC) };
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000033
34 SkScalar pos[] = {0,
35 SK_Scalar1 / 6,
36 2 * SK_Scalar1 / 6,
37 3 * SK_Scalar1 / 6,
38 4 * SK_Scalar1 / 6,
39 5 * SK_Scalar1 / 6,
40 SK_Scalar1};
41
42 SkPaint paint;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000043 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
44 SkMatrix mat = SkMatrix::I();
45 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000046 paint.setShader(SkGradientShader::CreateRadial(
47 pt, radius,
48 colors, pos,
49 SK_ARRAY_COUNT(colors),
50 SkShader::kRepeat_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000051 0, &mat))->unref();
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000052 canvas.drawRect(rect, paint);
53 rect.inset(wScalar / 8, hScalar / 8);
54 mat.preTranslate(6 * wScalar, 6 * hScalar);
55 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
56 }
57
58 paint.setAntiAlias(true);
caryclark2e096602015-07-13 05:39:47 -070059 sk_tool_utils::set_portable_typeface_always(&paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000060 paint.setTextSize(wScalar / 2.2f);
61 paint.setShader(0);
caryclark2e096602015-07-13 05:39:47 -070062 paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000063 static const char kTxt[] = "Skia";
64 SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
65 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
66 paint.setColor(SK_ColorBLACK);
67 paint.setStyle(SkPaint::kStroke_Style);
68 paint.setStrokeWidth(SK_Scalar1);
69 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
70 return bmp;
71}
72
73namespace skiagm {
74/**
75 * This GM tests convex polygon clips.
76 */
77class ConvexPolyClip : public GM {
78public:
79 ConvexPolyClip() {
80 this->setBGColor(0xFFFFFFFF);
81 }
82
83protected:
mtklein36352bf2015-03-25 18:17:31 -070084 SkString onShortName() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000085 return SkString("convex_poly_clip");
86 }
87
mtklein36352bf2015-03-25 18:17:31 -070088 SkISize onISize() override {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000089 // When benchmarking the saveLayer set of draws is skipped.
90 int w = 435;
91 if (kBench_Mode != this->getMode()) {
92 w *= 2;
93 }
tfarinaf5393182014-06-09 23:59:03 -070094 return SkISize::Make(w, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000095 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 void onOnceBeforeDraw() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000098 SkPath tri;
99 tri.moveTo(5.f, 5.f);
100 tri.lineTo(100.f, 20.f);
101 tri.lineTo(15.f, 100.f);
102
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000103 fClips.addToTail()->setPath(tri);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000104
105 SkPath hexagon;
106 static const SkScalar kRadius = 45.f;
107 const SkPoint center = { kRadius, kRadius };
108 for (int i = 0; i < 6; ++i) {
109 SkScalar angle = 2 * SK_ScalarPI * i / 6;
110 SkPoint point;
111 point.fY = SkScalarSinCos(angle, &point.fX);
112 point.scale(kRadius);
113 point = center + point;
114 if (0 == i) {
115 hexagon.moveTo(point);
116 } else {
117 hexagon.lineTo(point);
118 }
119 }
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000120 fClips.addToTail()->setPath(hexagon);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000121
122 SkMatrix scaleM;
123 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
124 hexagon.transform(scaleM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000125 fClips.addToTail()->setPath(hexagon);
126
127 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000128
129 SkPath rotRect;
130 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
131 rotRect.addRect(rect);
132 SkMatrix rotM;
133 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
134 rotRect.transform(rotM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000135 fClips.addToTail()->setPath(rotRect);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000136
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000137 fBmp = make_bmp(100, 100);
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000141 SkScalar y = 0;
142 static const SkScalar kMargin = 10.f;
143
144 SkPaint bgPaint;
145 bgPaint.setAlpha(0x15);
146 SkISize size = canvas->getDeviceSize();
147 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(size.fWidth),
148 SkIntToScalar(size.fHeight));
149 canvas->drawBitmapRectToRect(fBmp, NULL, dstRect, &bgPaint);
150
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000151 static const char kTxt[] = "Clip Me!";
152 SkPaint txtPaint;
153 txtPaint.setTextSize(23.f);
154 txtPaint.setAntiAlias(true);
caryclark2e096602015-07-13 05:39:47 -0700155 sk_tool_utils::set_portable_typeface_always(&txtPaint);
156 txtPaint.setColor(sk_tool_utils::color_to_565(SK_ColorDKGRAY));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000157 SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1);
158
159 SkScalar startX = 0;
160 int testLayers = kBench_Mode != this->getMode();
161 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
162 for (SkTLList<Clip>::Iter iter(fClips, SkTLList<Clip>::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700163 iter.get();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000164 iter.next()) {
165 const Clip* clip = iter.get();
166 SkScalar x = startX;
167 for (int aa = 0; aa < 2; ++aa) {
168 if (doLayer) {
169 SkRect bounds;
170 clip->getBounds(&bounds);
171 bounds.outset(2, 2);
172 bounds.offset(x, y);
173 canvas->saveLayer(&bounds, NULL);
174 } else {
175 canvas->save();
176 }
177 canvas->translate(x, y);
178 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
179 canvas->drawBitmap(fBmp, 0, 0);
180 canvas->restore();
181 x += fBmp.width() + kMargin;
182 }
183 for (int aa = 0; aa < 2; ++aa) {
184
185 SkPaint clipOutlinePaint;
186 clipOutlinePaint.setAntiAlias(true);
187 clipOutlinePaint.setColor(0x50505050);
188 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
189 clipOutlinePaint.setStrokeWidth(0);
190
191 if (doLayer) {
192 SkRect bounds;
193 clip->getBounds(&bounds);
194 bounds.outset(2, 2);
195 bounds.offset(x, y);
196 canvas->saveLayer(&bounds, NULL);
197 } else {
198 canvas->save();
199 }
200 canvas->translate(x, y);
201 SkPath closedClipPath;
202 clip->asClosedPath(&closedClipPath);
203 canvas->drawPath(closedClipPath, clipOutlinePaint);
204 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa));
205 canvas->scale(1.f, 1.8f);
206 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
207 0, 1.5f * txtPaint.getTextSize(),
208 txtPaint);
209 canvas->restore();
210 x += textW + 2 * kMargin;
211 }
212 y += fBmp.height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000213 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000214 y = 0;
215 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000216 }
217 }
218
mtklein36352bf2015-03-25 18:17:31 -0700219 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800220
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000221private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000222 class Clip {
223 public:
224 enum ClipType {
225 kNone_ClipType,
226 kPath_ClipType,
227 kRect_ClipType
228 };
229
230 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000231
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000232 void setOnCanvas(SkCanvas* canvas, SkRegion::Op op, bool aa) const {
233 switch (fClipType) {
234 case kPath_ClipType:
235 canvas->clipPath(fPath, op, aa);
236 break;
237 case kRect_ClipType:
238 canvas->clipRect(fRect, op, aa);
239 break;
240 case kNone_ClipType:
241 SkDEBUGFAIL("Uninitialized Clip.");
242 break;
243 }
244 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000245
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000246 void asClosedPath(SkPath* path) const {
247 switch (fClipType) {
248 case kPath_ClipType:
249 *path = fPath;
250 path->close();
251 break;
252 case kRect_ClipType:
253 path->reset();
254 path->addRect(fRect);
255 break;
256 case kNone_ClipType:
257 SkDEBUGFAIL("Uninitialized Clip.");
258 break;
259 }
260 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000261
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000262 void setPath(const SkPath& path) {
263 fClipType = kPath_ClipType;
264 fPath = path;
265 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000266
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000267 void setRect(const SkRect& rect) {
268 fClipType = kRect_ClipType;
269 fRect = rect;
270 fPath.reset();
271 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000272
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000273 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000274
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000275 void getBounds(SkRect* bounds) const {
276 switch (fClipType) {
277 case kPath_ClipType:
278 *bounds = fPath.getBounds();
279 break;
280 case kRect_ClipType:
281 *bounds = fRect;
282 break;
283 case kNone_ClipType:
284 SkDEBUGFAIL("Uninitialized Clip.");
285 break;
286 }
287 }
288
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000289 private:
290 ClipType fClipType;
291 SkPath fPath;
292 SkRect fRect;
293 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000294
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000295 SkTLList<Clip> fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000296 SkBitmap fBmp;
297
298 typedef GM INHERITED;
299};
300
301DEF_GM( return SkNEW(ConvexPolyClip); )
302
303}