blob: fb7c9d4fab6ce3859754525664102105a8af43dc [file] [log] [blame]
commit-bot@chromium.orga343c842014-01-31 14:48:58 +00001/*
2 * Copyright 2014 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 "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000010
11#include "SkBitmap.h"
12#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000014#include "SkTLList.h"
15
16static SkBitmap make_bmp(int w, int h) {
17 SkBitmap bmp;
18 bmp.allocN32Pixels(w, h, true);
19
20 SkCanvas canvas(bmp);
21 SkScalar wScalar = SkIntToScalar(w);
22 SkScalar hScalar = SkIntToScalar(h);
23
24 SkPoint pt = { wScalar / 2, hScalar / 2 };
25
26 SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
27
caryclark2e096602015-07-13 05:39:47 -070028 SkColor colors[] = { sk_tool_utils::color_to_565(SK_ColorDKGRAY),
29 sk_tool_utils::color_to_565(0xFF222255),
30 sk_tool_utils::color_to_565(0xFF331133),
31 sk_tool_utils::color_to_565(0xFF884422),
32 sk_tool_utils::color_to_565(0xFF000022), SK_ColorWHITE,
33 sk_tool_utils::color_to_565(0xFFAABBCC) };
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000034
35 SkScalar pos[] = {0,
36 SK_Scalar1 / 6,
37 2 * SK_Scalar1 / 6,
38 3 * SK_Scalar1 / 6,
39 4 * SK_Scalar1 / 6,
40 5 * SK_Scalar1 / 6,
41 SK_Scalar1};
42
43 SkPaint paint;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000044 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
45 SkMatrix mat = SkMatrix::I();
46 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080047 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000048 pt, radius,
49 colors, pos,
50 SK_ARRAY_COUNT(colors),
51 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080052 0, &mat));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000053 canvas.drawRect(rect, paint);
54 rect.inset(wScalar / 8, hScalar / 8);
55 mat.preTranslate(6 * wScalar, 6 * hScalar);
56 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
57 }
58
59 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070060 sk_tool_utils::set_portable_typeface(&paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000061 paint.setTextSize(wScalar / 2.2f);
62 paint.setShader(0);
caryclark2e096602015-07-13 05:39:47 -070063 paint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
mtkleindbfd7ab2016-09-01 11:24:54 -070064 constexpr char kTxt[] = "Skia";
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000065 SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
66 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
67 paint.setColor(SK_ColorBLACK);
68 paint.setStyle(SkPaint::kStroke_Style);
69 paint.setStrokeWidth(SK_Scalar1);
70 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
71 return bmp;
72}
73
74namespace skiagm {
75/**
76 * This GM tests convex polygon clips.
77 */
78class ConvexPolyClip : public GM {
79public:
80 ConvexPolyClip() {
81 this->setBGColor(0xFFFFFFFF);
82 }
83
84protected:
mtklein36352bf2015-03-25 18:17:31 -070085 SkString onShortName() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000086 return SkString("convex_poly_clip");
87 }
88
mtklein36352bf2015-03-25 18:17:31 -070089 SkISize onISize() override {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000090 // When benchmarking the saveLayer set of draws is skipped.
91 int w = 435;
92 if (kBench_Mode != this->getMode()) {
93 w *= 2;
94 }
tfarinaf5393182014-06-09 23:59:03 -070095 return SkISize::Make(w, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000096 }
97
mtklein36352bf2015-03-25 18:17:31 -070098 void onOnceBeforeDraw() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000099 SkPath tri;
100 tri.moveTo(5.f, 5.f);
101 tri.lineTo(100.f, 20.f);
102 tri.lineTo(15.f, 100.f);
103
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000104 fClips.addToTail()->setPath(tri);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000105
106 SkPath hexagon;
mtkleindbfd7ab2016-09-01 11:24:54 -0700107 constexpr SkScalar kRadius = 45.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000108 const SkPoint center = { kRadius, kRadius };
109 for (int i = 0; i < 6; ++i) {
110 SkScalar angle = 2 * SK_ScalarPI * i / 6;
111 SkPoint point;
112 point.fY = SkScalarSinCos(angle, &point.fX);
113 point.scale(kRadius);
114 point = center + point;
115 if (0 == i) {
116 hexagon.moveTo(point);
117 } else {
118 hexagon.lineTo(point);
119 }
120 }
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000121 fClips.addToTail()->setPath(hexagon);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000122
123 SkMatrix scaleM;
124 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
125 hexagon.transform(scaleM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000126 fClips.addToTail()->setPath(hexagon);
127
128 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000129
130 SkPath rotRect;
131 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
132 rotRect.addRect(rect);
133 SkMatrix rotM;
134 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
135 rotRect.transform(rotM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000136 fClips.addToTail()->setPath(rotRect);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000137
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000138 fBmp = make_bmp(100, 100);
139 }
140
mtklein36352bf2015-03-25 18:17:31 -0700141 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000142 SkScalar y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700143 constexpr SkScalar kMargin = 10.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000144
145 SkPaint bgPaint;
146 bgPaint.setAlpha(0x15);
Mike Reed3661bc92017-02-22 13:21:42 -0500147 SkISize size = canvas->getBaseLayerSize();
reeda5517e22015-07-14 10:54:12 -0700148 canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &bgPaint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000149
mtkleindbfd7ab2016-09-01 11:24:54 -0700150 constexpr char kTxt[] = "Clip Me!";
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000151 SkPaint txtPaint;
152 txtPaint.setTextSize(23.f);
153 txtPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700154 sk_tool_utils::set_portable_typeface(&txtPaint);
caryclark2e096602015-07-13 05:39:47 -0700155 txtPaint.setColor(sk_tool_utils::color_to_565(SK_ColorDKGRAY));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000156 SkScalar textW = txtPaint.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1);
157
158 SkScalar startX = 0;
159 int testLayers = kBench_Mode != this->getMode();
160 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
bsalomonf045d602015-11-18 19:01:12 -0800161 for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700162 iter.get();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000163 iter.next()) {
164 const Clip* clip = iter.get();
165 SkScalar x = startX;
166 for (int aa = 0; aa < 2; ++aa) {
167 if (doLayer) {
168 SkRect bounds;
169 clip->getBounds(&bounds);
170 bounds.outset(2, 2);
171 bounds.offset(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700172 canvas->saveLayer(&bounds, nullptr);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000173 } else {
174 canvas->save();
175 }
176 canvas->translate(x, y);
Mike Reedc1f77742016-12-09 09:00:50 -0500177 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000178 canvas->drawBitmap(fBmp, 0, 0);
179 canvas->restore();
180 x += fBmp.width() + kMargin;
181 }
182 for (int aa = 0; aa < 2; ++aa) {
183
184 SkPaint clipOutlinePaint;
185 clipOutlinePaint.setAntiAlias(true);
186 clipOutlinePaint.setColor(0x50505050);
187 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
188 clipOutlinePaint.setStrokeWidth(0);
189
190 if (doLayer) {
191 SkRect bounds;
192 clip->getBounds(&bounds);
193 bounds.outset(2, 2);
194 bounds.offset(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700195 canvas->saveLayer(&bounds, nullptr);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000196 } else {
197 canvas->save();
198 }
199 canvas->translate(x, y);
200 SkPath closedClipPath;
201 clip->asClosedPath(&closedClipPath);
202 canvas->drawPath(closedClipPath, clipOutlinePaint);
Mike Reedc1f77742016-12-09 09:00:50 -0500203 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000204 canvas->scale(1.f, 1.8f);
205 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1,
206 0, 1.5f * txtPaint.getTextSize(),
207 txtPaint);
208 canvas->restore();
209 x += textW + 2 * kMargin;
210 }
211 y += fBmp.height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000212 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000213 y = 0;
214 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000215 }
216 }
217
mtklein36352bf2015-03-25 18:17:31 -0700218 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800219
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000220private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000221 class Clip {
222 public:
223 enum ClipType {
224 kNone_ClipType,
225 kPath_ClipType,
226 kRect_ClipType
227 };
228
229 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000230
Mike Reedc1f77742016-12-09 09:00:50 -0500231 void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000232 switch (fClipType) {
233 case kPath_ClipType:
234 canvas->clipPath(fPath, op, aa);
235 break;
236 case kRect_ClipType:
237 canvas->clipRect(fRect, op, aa);
238 break;
239 case kNone_ClipType:
240 SkDEBUGFAIL("Uninitialized Clip.");
241 break;
242 }
243 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000244
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000245 void asClosedPath(SkPath* path) const {
246 switch (fClipType) {
247 case kPath_ClipType:
248 *path = fPath;
249 path->close();
250 break;
251 case kRect_ClipType:
252 path->reset();
253 path->addRect(fRect);
254 break;
255 case kNone_ClipType:
256 SkDEBUGFAIL("Uninitialized Clip.");
257 break;
258 }
259 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000260
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000261 void setPath(const SkPath& path) {
262 fClipType = kPath_ClipType;
263 fPath = path;
264 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000265
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000266 void setRect(const SkRect& rect) {
267 fClipType = kRect_ClipType;
268 fRect = rect;
269 fPath.reset();
270 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000271
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000272 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000273
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000274 void getBounds(SkRect* bounds) const {
275 switch (fClipType) {
276 case kPath_ClipType:
277 *bounds = fPath.getBounds();
278 break;
279 case kRect_ClipType:
280 *bounds = fRect;
281 break;
282 case kNone_ClipType:
283 SkDEBUGFAIL("Uninitialized Clip.");
284 break;
285 }
286 }
287
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000288 private:
289 ClipType fClipType;
290 SkPath fPath;
291 SkRect fRect;
292 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000293
bsalomonf045d602015-11-18 19:01:12 -0800294 typedef SkTLList<Clip, 1> ClipList;
295 ClipList fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000296 SkBitmap fBmp;
297
298 typedef GM INHERITED;
299};
300
halcanary385fe4d2015-08-26 13:07:48 -0700301DEF_GM(return new ConvexPolyClip;)
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000302}