blob: 8d53b50b633efdd6bc6d876f34ca58adac991503 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkClipOp.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkFontTypes.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkPaint.h"
Mike Reed92f6eb12020-08-25 11:48:41 -040017#include "include/core/SkPathBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkScalar.h"
21#include "include/core/SkShader.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
24#include "include/core/SkTileMode.h"
25#include "include/core/SkTypeface.h"
26#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkGradientShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/core/SkClipOpPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/core/SkTLList.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "tools/ToolUtils.h"
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000031
32static SkBitmap make_bmp(int w, int h) {
33 SkBitmap bmp;
34 bmp.allocN32Pixels(w, h, true);
35
36 SkCanvas canvas(bmp);
37 SkScalar wScalar = SkIntToScalar(w);
38 SkScalar hScalar = SkIntToScalar(h);
39
40 SkPoint pt = { wScalar / 2, hScalar / 2 };
41
Brian Osman116b33e2020-02-05 13:34:09 -050042 SkScalar radius = 3 * std::max(wScalar, hScalar);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000043
Mike Kleinea3f0142019-03-20 11:12:10 -050044 SkColor colors[] = {SK_ColorDKGRAY,
45 ToolUtils::color_to_565(0xFF222255),
46 ToolUtils::color_to_565(0xFF331133),
47 ToolUtils::color_to_565(0xFF884422),
48 ToolUtils::color_to_565(0xFF000022),
49 SK_ColorWHITE,
50 ToolUtils::color_to_565(0xFFAABBCC)};
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000051
52 SkScalar pos[] = {0,
53 SK_Scalar1 / 6,
54 2 * SK_Scalar1 / 6,
55 3 * SK_Scalar1 / 6,
56 4 * SK_Scalar1 / 6,
57 5 * SK_Scalar1 / 6,
58 SK_Scalar1};
59
60 SkPaint paint;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000061 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
62 SkMatrix mat = SkMatrix::I();
63 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080064 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000065 pt, radius,
66 colors, pos,
67 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040068 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080069 0, &mat));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000070 canvas.drawRect(rect, paint);
71 rect.inset(wScalar / 8, hScalar / 8);
72 mat.preTranslate(6 * wScalar, 6 * hScalar);
73 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
74 }
75
Mike Kleinea3f0142019-03-20 11:12:10 -050076 SkFont font(ToolUtils::create_portable_typeface(), wScalar / 2.2f);
Mike Reed4de2f1f2019-01-05 16:35:13 -050077
Ben Wagnera93a14a2017-08-28 10:34:05 -040078 paint.setShader(nullptr);
Mike Kleind46dce32018-08-16 10:17:03 -040079 paint.setColor(SK_ColorLTGRAY);
mtkleindbfd7ab2016-09-01 11:24:54 -070080 constexpr char kTxt[] = "Skia";
Mike Reed4de2f1f2019-01-05 16:35:13 -050081 SkPoint texPos = { wScalar / 17, hScalar / 2 + font.getSize() / 2.5f };
Ben Wagner51e15a62019-05-07 15:38:46 -040082 canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8,
Mike Reed4de2f1f2019-01-05 16:35:13 -050083 texPos.fX, texPos.fY, font, paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000084 paint.setColor(SK_ColorBLACK);
85 paint.setStyle(SkPaint::kStroke_Style);
86 paint.setStrokeWidth(SK_Scalar1);
Ben Wagner51e15a62019-05-07 15:38:46 -040087 canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8,
Mike Reed4de2f1f2019-01-05 16:35:13 -050088 texPos.fX, texPos.fY, font, paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000089 return bmp;
90}
91
92namespace skiagm {
93/**
94 * This GM tests convex polygon clips.
95 */
96class ConvexPolyClip : public GM {
97public:
98 ConvexPolyClip() {
99 this->setBGColor(0xFFFFFFFF);
100 }
101
102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 SkString onShortName() override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000104 return SkString("convex_poly_clip");
105 }
106
mtklein36352bf2015-03-25 18:17:31 -0700107 SkISize onISize() override {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000108 // When benchmarking the saveLayer set of draws is skipped.
109 int w = 435;
110 if (kBench_Mode != this->getMode()) {
111 w *= 2;
112 }
tfarinaf5393182014-06-09 23:59:03 -0700113 return SkISize::Make(w, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000114 }
115
mtklein36352bf2015-03-25 18:17:31 -0700116 void onOnceBeforeDraw() override {
Mike Reed92f6eb12020-08-25 11:48:41 -0400117 fClips.addToTail()->setPath(SkPath::Polygon({
118 { 5.f, 5.f},
119 {100.f, 20.f},
120 { 15.f, 100.f},
121 }, false));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000122
Mike Reed92f6eb12020-08-25 11:48:41 -0400123 SkPathBuilder hexagon;
mtkleindbfd7ab2016-09-01 11:24:54 -0700124 constexpr SkScalar kRadius = 45.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000125 const SkPoint center = { kRadius, kRadius };
126 for (int i = 0; i < 6; ++i) {
127 SkScalar angle = 2 * SK_ScalarPI * i / 6;
Brian Osman4428f2c2019-04-02 10:59:28 -0400128 SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000129 point.scale(kRadius);
130 point = center + point;
131 if (0 == i) {
132 hexagon.moveTo(point);
133 } else {
134 hexagon.lineTo(point);
135 }
136 }
Mike Reed92f6eb12020-08-25 11:48:41 -0400137 fClips.addToTail()->setPath(hexagon.snapshot());
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000138
139 SkMatrix scaleM;
140 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
Mike Reed92f6eb12020-08-25 11:48:41 -0400141 fClips.addToTail()->setPath(hexagon.detach().makeTransform(scaleM));
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000142
143 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000144
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000145 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000146 SkMatrix rotM;
147 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
Mike Reed92f6eb12020-08-25 11:48:41 -0400148 fClips.addToTail()->setPath(SkPath::Rect(rect).makeTransform(rotM));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000149
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000150 fBmp = make_bmp(100, 100);
151 }
152
mtklein36352bf2015-03-25 18:17:31 -0700153 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000154 SkScalar y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700155 constexpr SkScalar kMargin = 10.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000156
157 SkPaint bgPaint;
158 bgPaint.setAlpha(0x15);
Mike Reed3661bc92017-02-22 13:21:42 -0500159 SkISize size = canvas->getBaseLayerSize();
reeda5517e22015-07-14 10:54:12 -0700160 canvas->drawBitmapRect(fBmp, SkRect::MakeIWH(size.fWidth, size.fHeight), &bgPaint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000161
mtkleindbfd7ab2016-09-01 11:24:54 -0700162 constexpr char kTxt[] = "Clip Me!";
Mike Kleinea3f0142019-03-20 11:12:10 -0500163 SkFont font(ToolUtils::create_portable_typeface(), 23);
Ben Wagner51e15a62019-05-07 15:38:46 -0400164 SkScalar textW = font.measureText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000165 SkPaint txtPaint;
Mike Kleind46dce32018-08-16 10:17:03 -0400166 txtPaint.setColor(SK_ColorDKGRAY);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000167
168 SkScalar startX = 0;
169 int testLayers = kBench_Mode != this->getMode();
170 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
bsalomonf045d602015-11-18 19:01:12 -0800171 for (ClipList::Iter iter(fClips, ClipList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700172 iter.get();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000173 iter.next()) {
174 const Clip* clip = iter.get();
175 SkScalar x = startX;
176 for (int aa = 0; aa < 2; ++aa) {
177 if (doLayer) {
178 SkRect bounds;
179 clip->getBounds(&bounds);
180 bounds.outset(2, 2);
181 bounds.offset(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700182 canvas->saveLayer(&bounds, nullptr);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000183 } else {
184 canvas->save();
185 }
186 canvas->translate(x, y);
Mike Reedc1f77742016-12-09 09:00:50 -0500187 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000188 canvas->drawBitmap(fBmp, 0, 0);
189 canvas->restore();
190 x += fBmp.width() + kMargin;
191 }
192 for (int aa = 0; aa < 2; ++aa) {
193
194 SkPaint clipOutlinePaint;
195 clipOutlinePaint.setAntiAlias(true);
196 clipOutlinePaint.setColor(0x50505050);
197 clipOutlinePaint.setStyle(SkPaint::kStroke_Style);
198 clipOutlinePaint.setStrokeWidth(0);
199
200 if (doLayer) {
201 SkRect bounds;
202 clip->getBounds(&bounds);
203 bounds.outset(2, 2);
204 bounds.offset(x, y);
halcanary96fcdcc2015-08-27 07:41:13 -0700205 canvas->saveLayer(&bounds, nullptr);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000206 } else {
207 canvas->save();
208 }
209 canvas->translate(x, y);
210 SkPath closedClipPath;
211 clip->asClosedPath(&closedClipPath);
212 canvas->drawPath(closedClipPath, clipOutlinePaint);
Mike Reedc1f77742016-12-09 09:00:50 -0500213 clip->setOnCanvas(canvas, kIntersect_SkClipOp, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000214 canvas->scale(1.f, 1.8f);
Ben Wagner51e15a62019-05-07 15:38:46 -0400215 canvas->drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, SkTextEncoding::kUTF8,
Mike Reed2e6db182018-12-15 13:45:33 -0500216 0, 1.5f * font.getSize(), font, txtPaint);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000217 canvas->restore();
218 x += textW + 2 * kMargin;
219 }
220 y += fBmp.height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000221 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000222 y = 0;
223 startX += 2 * fBmp.width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000224 }
225 }
226
mtklein36352bf2015-03-25 18:17:31 -0700227 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800228
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000229private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000230 class Clip {
231 public:
232 enum ClipType {
233 kNone_ClipType,
234 kPath_ClipType,
235 kRect_ClipType
236 };
237
238 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000239
Mike Reedc1f77742016-12-09 09:00:50 -0500240 void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000241 switch (fClipType) {
242 case kPath_ClipType:
243 canvas->clipPath(fPath, op, aa);
244 break;
245 case kRect_ClipType:
246 canvas->clipRect(fRect, op, aa);
247 break;
248 case kNone_ClipType:
249 SkDEBUGFAIL("Uninitialized Clip.");
250 break;
251 }
252 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000253
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000254 void asClosedPath(SkPath* path) const {
255 switch (fClipType) {
256 case kPath_ClipType:
257 *path = fPath;
258 path->close();
259 break;
260 case kRect_ClipType:
261 path->reset();
262 path->addRect(fRect);
263 break;
264 case kNone_ClipType:
265 SkDEBUGFAIL("Uninitialized Clip.");
266 break;
267 }
268 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000269
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000270 void setPath(const SkPath& path) {
271 fClipType = kPath_ClipType;
272 fPath = path;
273 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000274
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000275 void setRect(const SkRect& rect) {
276 fClipType = kRect_ClipType;
277 fRect = rect;
278 fPath.reset();
279 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000280
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000281 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000282
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000283 void getBounds(SkRect* bounds) const {
284 switch (fClipType) {
285 case kPath_ClipType:
286 *bounds = fPath.getBounds();
287 break;
288 case kRect_ClipType:
289 *bounds = fRect;
290 break;
291 case kNone_ClipType:
292 SkDEBUGFAIL("Uninitialized Clip.");
293 break;
294 }
295 }
296
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000297 private:
298 ClipType fClipType;
299 SkPath fPath;
300 SkRect fRect;
301 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000302
bsalomonf045d602015-11-18 19:01:12 -0800303 typedef SkTLList<Clip, 1> ClipList;
304 ClipList fClips;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000305 SkBitmap fBmp;
306
307 typedef GM INHERITED;
308};
309
halcanary385fe4d2015-08-26 13:07:48 -0700310DEF_GM(return new ConvexPolyClip;)
John Stilesa6841be2020-08-06 14:11:56 -0400311} // namespace skiagm