blob: af073c61b2bb7cc13449c3dedadc9a46d31dcf33 [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"
Mike Reed2e6db182018-12-15 13:45:33 -050012#include "SkFont.h"
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000013#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.h"
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000015#include "SkTLList.h"
16
17static SkBitmap make_bmp(int w, int h) {
18 SkBitmap bmp;
19 bmp.allocN32Pixels(w, h, true);
20
21 SkCanvas canvas(bmp);
22 SkScalar wScalar = SkIntToScalar(w);
23 SkScalar hScalar = SkIntToScalar(h);
24
25 SkPoint pt = { wScalar / 2, hScalar / 2 };
26
27 SkScalar radius = 3 * SkMaxScalar(wScalar, hScalar);
28
Mike Kleind46dce32018-08-16 10:17:03 -040029 SkColor colors[] = { SK_ColorDKGRAY,
caryclark2e096602015-07-13 05:39:47 -070030 sk_tool_utils::color_to_565(0xFF222255),
31 sk_tool_utils::color_to_565(0xFF331133),
32 sk_tool_utils::color_to_565(0xFF884422),
33 sk_tool_utils::color_to_565(0xFF000022), SK_ColorWHITE,
34 sk_tool_utils::color_to_565(0xFFAABBCC) };
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000035
36 SkScalar pos[] = {0,
37 SK_Scalar1 / 6,
38 2 * SK_Scalar1 / 6,
39 3 * SK_Scalar1 / 6,
40 4 * SK_Scalar1 / 6,
41 5 * SK_Scalar1 / 6,
42 SK_Scalar1};
43
44 SkPaint paint;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000045 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
46 SkMatrix mat = SkMatrix::I();
47 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080048 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000049 pt, radius,
50 colors, pos,
51 SK_ARRAY_COUNT(colors),
52 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080053 0, &mat));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000054 canvas.drawRect(rect, paint);
55 rect.inset(wScalar / 8, hScalar / 8);
56 mat.preTranslate(6 * wScalar, 6 * hScalar);
57 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
58 }
59
60 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070061 sk_tool_utils::set_portable_typeface(&paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000062 paint.setTextSize(wScalar / 2.2f);
Ben Wagnera93a14a2017-08-28 10:34:05 -040063 paint.setShader(nullptr);
Mike Kleind46dce32018-08-16 10:17:03 -040064 paint.setColor(SK_ColorLTGRAY);
mtkleindbfd7ab2016-09-01 11:24:54 -070065 constexpr char kTxt[] = "Skia";
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000066 SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
67 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
68 paint.setColor(SK_ColorBLACK);
69 paint.setStyle(SkPaint::kStroke_Style);
70 paint.setStrokeWidth(SK_Scalar1);
71 canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
72 return bmp;
73}
74
75namespace skiagm {
76/**
77 * This GM tests convex polygon clips.
78 */
79class ConvexPolyClip : public GM {
80public:
81 ConvexPolyClip() {
82 this->setBGColor(0xFFFFFFFF);
83 }
84
85protected:
mtklein36352bf2015-03-25 18:17:31 -070086 SkString onShortName() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000087 return SkString("convex_poly_clip");
88 }
89
mtklein36352bf2015-03-25 18:17:31 -070090 SkISize onISize() override {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000091 // When benchmarking the saveLayer set of draws is skipped.
92 int w = 435;
93 if (kBench_Mode != this->getMode()) {
94 w *= 2;
95 }
tfarinaf5393182014-06-09 23:59:03 -070096 return SkISize::Make(w, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000097 }
98
mtklein36352bf2015-03-25 18:17:31 -070099 void onOnceBeforeDraw() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000100 SkPath tri;
101 tri.moveTo(5.f, 5.f);
102 tri.lineTo(100.f, 20.f);
103 tri.lineTo(15.f, 100.f);
104
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000105 fClips.addToTail()->setPath(tri);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000106
107 SkPath hexagon;
mtkleindbfd7ab2016-09-01 11:24:54 -0700108 constexpr SkScalar kRadius = 45.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000109 const SkPoint center = { kRadius, kRadius };
110 for (int i = 0; i < 6; ++i) {
111 SkScalar angle = 2 * SK_ScalarPI * i / 6;
112 SkPoint point;
113 point.fY = SkScalarSinCos(angle, &point.fX);
114 point.scale(kRadius);
115 point = center + point;
116 if (0 == i) {
117 hexagon.moveTo(point);
118 } else {
119 hexagon.lineTo(point);
120 }
121 }
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000122 fClips.addToTail()->setPath(hexagon);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000123
124 SkMatrix scaleM;
125 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
126 hexagon.transform(scaleM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000127 fClips.addToTail()->setPath(hexagon);
128
129 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000130
131 SkPath rotRect;
132 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
133 rotRect.addRect(rect);
134 SkMatrix rotM;
135 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
136 rotRect.transform(rotM);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000137 fClips.addToTail()->setPath(rotRect);
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000138
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000139 fBmp = make_bmp(100, 100);
140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000143 SkScalar y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700144 constexpr SkScalar kMargin = 10.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000145
146 SkPaint bgPaint;
147 bgPaint.setAlpha(0x15);
Mike Reed3661bc92017-02-22 13:21:42 -0500148 SkISize size = canvas->getBaseLayerSize();
reeda5517e22015-07-14 10:54:12 -0700149 canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &bgPaint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000150
mtkleindbfd7ab2016-09-01 11:24:54 -0700151 constexpr char kTxt[] = "Clip Me!";
Mike Reed2e6db182018-12-15 13:45:33 -0500152 SkFont font(sk_tool_utils::create_portable_typeface(), 23);
153 SkScalar textW = font.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000154 SkPaint txtPaint;
Mike Kleind46dce32018-08-16 10:17:03 -0400155 txtPaint.setColor(SK_ColorDKGRAY);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000156
157 SkScalar startX = 0;
158 int testLayers = kBench_Mode != this->getMode();
159 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
bsalomonf045d602015-11-18 19:01:12 -0800160 for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700161 iter.get();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000162 iter.next()) {
163 const Clip* clip = iter.get();
164 SkScalar x = startX;
165 for (int aa = 0; aa < 2; ++aa) {
166 if (doLayer) {
167 SkRect bounds;
168 clip->getBounds(&bounds);
169 bounds.outset(2, 2);
170 bounds.offset(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700171 canvas->saveLayer(&bounds, nullptr);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000172 } else {
173 canvas->save();
174 }
175 canvas->translate(x, y);
Mike Reedc1f77742016-12-09 09:00:50 -0500176 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000177 canvas->drawBitmap(fBmp, 0, 0);
178 canvas->restore();
179 x += fBmp.width() + kMargin;
180 }
181 for (int aa = 0; aa < 2; ++aa) {
182
183 SkPaint clipOutlinePaint;
184 clipOutlinePaint.setAntiAlias(true);
185 clipOutlinePaint.setColor(0x50505050);
186 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
187 clipOutlinePaint.setStrokeWidth(0);
188
189 if (doLayer) {
190 SkRect bounds;
191 clip->getBounds(&bounds);
192 bounds.outset(2, 2);
193 bounds.offset(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700194 canvas->saveLayer(&bounds, nullptr);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000195 } else {
196 canvas->save();
197 }
198 canvas->translate(x, y);
199 SkPath closedClipPath;
200 clip->asClosedPath(&closedClipPath);
201 canvas->drawPath(closedClipPath, clipOutlinePaint);
Mike Reedc1f77742016-12-09 09:00:50 -0500202 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000203 canvas->scale(1.f, 1.8f);
Mike Reed2e6db182018-12-15 13:45:33 -0500204 canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding,
205 0, 1.5f * font.getSize(), font, txtPaint);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000206 canvas->restore();
207 x += textW + 2 * kMargin;
208 }
209 y += fBmp.height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000210 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000211 y = 0;
212 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000213 }
214 }
215
mtklein36352bf2015-03-25 18:17:31 -0700216 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800217
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000218private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000219 class Clip {
220 public:
221 enum ClipType {
222 kNone_ClipType,
223 kPath_ClipType,
224 kRect_ClipType
225 };
226
227 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000228
Mike Reedc1f77742016-12-09 09:00:50 -0500229 void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000230 switch (fClipType) {
231 case kPath_ClipType:
232 canvas->clipPath(fPath, op, aa);
233 break;
234 case kRect_ClipType:
235 canvas->clipRect(fRect, op, aa);
236 break;
237 case kNone_ClipType:
238 SkDEBUGFAIL("Uninitialized Clip.");
239 break;
240 }
241 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000242
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000243 void asClosedPath(SkPath* path) const {
244 switch (fClipType) {
245 case kPath_ClipType:
246 *path = fPath;
247 path->close();
248 break;
249 case kRect_ClipType:
250 path->reset();
251 path->addRect(fRect);
252 break;
253 case kNone_ClipType:
254 SkDEBUGFAIL("Uninitialized Clip.");
255 break;
256 }
257 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000258
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000259 void setPath(const SkPath& path) {
260 fClipType = kPath_ClipType;
261 fPath = path;
262 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000263
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000264 void setRect(const SkRect& rect) {
265 fClipType = kRect_ClipType;
266 fRect = rect;
267 fPath.reset();
268 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000269
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000270 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000271
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000272 void getBounds(SkRect* bounds) const {
273 switch (fClipType) {
274 case kPath_ClipType:
275 *bounds = fPath.getBounds();
276 break;
277 case kRect_ClipType:
278 *bounds = fRect;
279 break;
280 case kNone_ClipType:
281 SkDEBUGFAIL("Uninitialized Clip.");
282 break;
283 }
284 }
285
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000286 private:
287 ClipType fClipType;
288 SkPath fPath;
289 SkRect fRect;
290 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000291
bsalomonf045d602015-11-18 19:01:12 -0800292 typedef SkTLList<Clip, 1> ClipList;
293 ClipList fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000294 SkBitmap fBmp;
295
296 typedef GM INHERITED;
297};
298
halcanary385fe4d2015-08-26 13:07:48 -0700299DEF_GM(return new ConvexPolyClip;)
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000300}