blob: 98cbd084802f5d1f76801fe091dd9114fdf0a540 [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 "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040029#include "tools/fonts/FontToolUtils.h"
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000030
Mike Reed607a3822021-01-24 19:49:21 -050031static sk_sp<SkImage> make_img(int w, int h) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040032 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32(w, h, kOpaque_SkAlphaType));
Mike Reed607a3822021-01-24 19:49:21 -050033 auto canvas = surf->getCanvas();
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000034
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000035 SkScalar wScalar = SkIntToScalar(w);
36 SkScalar hScalar = SkIntToScalar(h);
37
38 SkPoint pt = { wScalar / 2, hScalar / 2 };
39
Brian Osman116b33e2020-02-05 13:34:09 -050040 SkScalar radius = 3 * std::max(wScalar, hScalar);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000041
Mike Kleinea3f0142019-03-20 11:12:10 -050042 SkColor colors[] = {SK_ColorDKGRAY,
43 ToolUtils::color_to_565(0xFF222255),
44 ToolUtils::color_to_565(0xFF331133),
45 ToolUtils::color_to_565(0xFF884422),
46 ToolUtils::color_to_565(0xFF000022),
47 SK_ColorWHITE,
48 ToolUtils::color_to_565(0xFFAABBCC)};
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000049
50 SkScalar pos[] = {0,
51 SK_Scalar1 / 6,
52 2 * SK_Scalar1 / 6,
53 3 * SK_Scalar1 / 6,
54 4 * SK_Scalar1 / 6,
55 5 * SK_Scalar1 / 6,
56 SK_Scalar1};
57
58 SkPaint paint;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000059 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
60 SkMatrix mat = SkMatrix::I();
61 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080062 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000063 pt, radius,
64 colors, pos,
Herb Derbyc37b3862022-06-21 09:49:17 -040065 std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040066 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080067 0, &mat));
Mike Reed607a3822021-01-24 19:49:21 -050068 canvas->drawRect(rect, paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000069 rect.inset(wScalar / 8, hScalar / 8);
70 mat.preTranslate(6 * wScalar, 6 * hScalar);
71 mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
72 }
73
Kevin Lubicke836c3a2023-10-20 06:55:35 -040074 SkFont font(ToolUtils::DefaultPortableTypeface(), wScalar / 2.2f);
Mike Reed4de2f1f2019-01-05 16:35:13 -050075
Ben Wagnera93a14a2017-08-28 10:34:05 -040076 paint.setShader(nullptr);
Mike Kleind46dce32018-08-16 10:17:03 -040077 paint.setColor(SK_ColorLTGRAY);
mtkleindbfd7ab2016-09-01 11:24:54 -070078 constexpr char kTxt[] = "Skia";
Mike Reed4de2f1f2019-01-05 16:35:13 -050079 SkPoint texPos = { wScalar / 17, hScalar / 2 + font.getSize() / 2.5f };
Herb Derbyc37b3862022-06-21 09:49:17 -040080 canvas->drawSimpleText(kTxt, std::size(kTxt)-1, SkTextEncoding::kUTF8,
Mike Reed607a3822021-01-24 19:49:21 -050081 texPos.fX, texPos.fY, font, paint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000082 paint.setColor(SK_ColorBLACK);
83 paint.setStyle(SkPaint::kStroke_Style);
84 paint.setStrokeWidth(SK_Scalar1);
Herb Derbyc37b3862022-06-21 09:49:17 -040085 canvas->drawSimpleText(kTxt, std::size(kTxt)-1, SkTextEncoding::kUTF8,
Mike Reed607a3822021-01-24 19:49:21 -050086 texPos.fX, texPos.fY, font, paint);
87 return surf->makeImageSnapshot();
commit-bot@chromium.orga343c842014-01-31 14:48:58 +000088}
89
90namespace skiagm {
91/**
92 * This GM tests convex polygon clips.
93 */
94class ConvexPolyClip : public GM {
95public:
96 ConvexPolyClip() {
97 this->setBGColor(0xFFFFFFFF);
98 }
99
100protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000101 SkString getName() const override { return SkString("convex_poly_clip"); }
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000102
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000103 SkISize getISize() override {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000104 // When benchmarking the saveLayer set of draws is skipped.
105 int w = 435;
106 if (kBench_Mode != this->getMode()) {
107 w *= 2;
108 }
tfarinaf5393182014-06-09 23:59:03 -0700109 return SkISize::Make(w, 540);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000110 }
111
mtklein36352bf2015-03-25 18:17:31 -0700112 void onOnceBeforeDraw() override {
Michael Ludwigb23630c2021-08-10 12:27:25 -0400113 // On < c++17, emplace_back() returns a void :(
114 auto emplace_back = [](std::vector<Clip>& clips) -> Clip& {
115 clips.emplace_back();
116 return clips.back();
117 };
118
119 emplace_back(fClips).setPath(SkPath::Polygon({
Mike Reed92f6eb12020-08-25 11:48:41 -0400120 { 5.f, 5.f},
121 {100.f, 20.f},
122 { 15.f, 100.f},
123 }, false));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000124
Mike Reed92f6eb12020-08-25 11:48:41 -0400125 SkPathBuilder hexagon;
mtkleindbfd7ab2016-09-01 11:24:54 -0700126 constexpr SkScalar kRadius = 45.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000127 const SkPoint center = { kRadius, kRadius };
128 for (int i = 0; i < 6; ++i) {
129 SkScalar angle = 2 * SK_ScalarPI * i / 6;
Brian Osman4428f2c2019-04-02 10:59:28 -0400130 SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000131 point.scale(kRadius);
132 point = center + point;
133 if (0 == i) {
134 hexagon.moveTo(point);
135 } else {
136 hexagon.lineTo(point);
137 }
138 }
Michael Ludwigb23630c2021-08-10 12:27:25 -0400139 emplace_back(fClips).setPath(hexagon.snapshot());
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000140
141 SkMatrix scaleM;
142 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius);
Michael Ludwigb23630c2021-08-10 12:27:25 -0400143 emplace_back(fClips).setPath(hexagon.detach().makeTransform(scaleM));
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000144
Michael Ludwigb23630c2021-08-10 12:27:25 -0400145 emplace_back(fClips).setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f));
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000146
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000147 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000148 SkMatrix rotM;
149 rotM.setRotate(23.f, rect.centerX(), rect.centerY());
Michael Ludwigb23630c2021-08-10 12:27:25 -0400150 emplace_back(fClips).setPath(SkPath::Rect(rect).makeTransform(rotM));
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000151
Mike Reed607a3822021-01-24 19:49:21 -0500152 fImg = make_img(100, 100);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000153 }
154
mtklein36352bf2015-03-25 18:17:31 -0700155 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000156 SkScalar y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700157 constexpr SkScalar kMargin = 10.f;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000158
159 SkPaint bgPaint;
160 bgPaint.setAlpha(0x15);
Mike Reed3661bc92017-02-22 13:21:42 -0500161 SkISize size = canvas->getBaseLayerSize();
Mike Reed607a3822021-01-24 19:49:21 -0500162 canvas->drawImageRect(fImg, SkRect::MakeIWH(size.fWidth, size.fHeight),
163 SkSamplingOptions(), &bgPaint);
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000164
mtkleindbfd7ab2016-09-01 11:24:54 -0700165 constexpr char kTxt[] = "Clip Me!";
Kevin Lubicke836c3a2023-10-20 06:55:35 -0400166 SkFont font(ToolUtils::DefaultPortableTypeface(), 23);
Herb Derbyc37b3862022-06-21 09:49:17 -0400167 SkScalar textW = font.measureText(kTxt, std::size(kTxt)-1, SkTextEncoding::kUTF8);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000168 SkPaint txtPaint;
Mike Kleind46dce32018-08-16 10:17:03 -0400169 txtPaint.setColor(SK_ColorDKGRAY);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000170
171 SkScalar startX = 0;
172 int testLayers = kBench_Mode != this->getMode();
173 for (int doLayer = 0; doLayer <= testLayers; ++doLayer) {
Michael Ludwigb23630c2021-08-10 12:27:25 -0400174 for (const Clip& clip : fClips) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000175 SkScalar x = startX;
176 for (int aa = 0; aa < 2; ++aa) {
177 if (doLayer) {
178 SkRect bounds;
Michael Ludwigb23630c2021-08-10 12:27:25 -0400179 clip.getBounds(&bounds);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000180 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);
Michael Ludwigb23630c2021-08-10 12:27:25 -0400187 clip.setOnCanvas(canvas, SkClipOp::kIntersect, SkToBool(aa));
Mike Reed607a3822021-01-24 19:49:21 -0500188 canvas->drawImage(fImg, 0, 0);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000189 canvas->restore();
Mike Reed607a3822021-01-24 19:49:21 -0500190 x += fImg->width() + kMargin;
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000191 }
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;
Michael Ludwigb23630c2021-08-10 12:27:25 -0400202 clip.getBounds(&bounds);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000203 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);
Michael Ludwigb23630c2021-08-10 12:27:25 -0400210 SkPath closedClipPath = clip.asClosedPath();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000211 canvas->drawPath(closedClipPath, clipOutlinePaint);
Michael Ludwigb23630c2021-08-10 12:27:25 -0400212 clip.setOnCanvas(canvas, SkClipOp::kIntersect, SkToBool(aa));
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000213 canvas->scale(1.f, 1.8f);
Herb Derbyc37b3862022-06-21 09:49:17 -0400214 canvas->drawSimpleText(kTxt, std::size(kTxt)-1, SkTextEncoding::kUTF8,
Mike Reed2e6db182018-12-15 13:45:33 -0500215 0, 1.5f * font.getSize(), font, txtPaint);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000216 canvas->restore();
217 x += textW + 2 * kMargin;
218 }
Mike Reed607a3822021-01-24 19:49:21 -0500219 y += fImg->height() + kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000220 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000221 y = 0;
Mike Reed607a3822021-01-24 19:49:21 -0500222 startX += 2 * fImg->width() + SkScalarCeilToInt(2 * textW) + 6 * kMargin;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000223 }
224 }
225
mtklein36352bf2015-03-25 18:17:31 -0700226 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800227
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000228private:
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000229 class Clip {
230 public:
231 enum ClipType {
232 kNone_ClipType,
233 kPath_ClipType,
234 kRect_ClipType
235 };
236
237 Clip () : fClipType(kNone_ClipType) {}
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000238
Mike Reedc1f77742016-12-09 09:00:50 -0500239 void setOnCanvas(SkCanvas* canvas, SkClipOp op, bool aa) const {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000240 switch (fClipType) {
241 case kPath_ClipType:
Mike Reedd849a752020-09-08 20:47:09 -0400242 canvas->clipPath(fPathBuilder.snapshot(), op, aa);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000243 break;
244 case kRect_ClipType:
245 canvas->clipRect(fRect, op, aa);
246 break;
247 case kNone_ClipType:
248 SkDEBUGFAIL("Uninitialized Clip.");
249 break;
250 }
251 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000252
Mike Reedd849a752020-09-08 20:47:09 -0400253 SkPath asClosedPath() const {
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000254 switch (fClipType) {
255 case kPath_ClipType:
Mike Reedd849a752020-09-08 20:47:09 -0400256 return SkPathBuilder(fPathBuilder).close().detach();
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000257 case kRect_ClipType:
Mike Reedd849a752020-09-08 20:47:09 -0400258 return SkPath::Rect(fRect);
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000259 case kNone_ClipType:
260 SkDEBUGFAIL("Uninitialized Clip.");
261 break;
262 }
Mike Reedd849a752020-09-08 20:47:09 -0400263 return SkPath();
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000264 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000265
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000266 void setPath(const SkPath& path) {
267 fClipType = kPath_ClipType;
Mike Reedd849a752020-09-08 20:47:09 -0400268 fPathBuilder = path;
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000269 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000270
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000271 void setRect(const SkRect& rect) {
272 fClipType = kRect_ClipType;
273 fRect = rect;
Mike Reedd849a752020-09-08 20:47:09 -0400274 fPathBuilder.reset();
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000275 }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000276
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000277 ClipType getType() const { return fClipType; }
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000278
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000279 void getBounds(SkRect* bounds) const {
280 switch (fClipType) {
281 case kPath_ClipType:
Mike Reedd849a752020-09-08 20:47:09 -0400282 *bounds = fPathBuilder.computeBounds();
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000283 break;
284 case kRect_ClipType:
285 *bounds = fRect;
286 break;
287 case kNone_ClipType:
288 SkDEBUGFAIL("Uninitialized Clip.");
289 break;
290 }
291 }
292
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000293 private:
294 ClipType fClipType;
Mike Reedd849a752020-09-08 20:47:09 -0400295 SkPathBuilder fPathBuilder;
commit-bot@chromium.orgb511be52014-02-04 15:09:16 +0000296 SkRect fRect;
297 };
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000298
Michael Ludwigb23630c2021-08-10 12:27:25 -0400299 std::vector<Clip> fClips;
Brian Osman2bb41e02024-02-21 21:50:07 +0000300 sk_sp<SkImage> fImg;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000301
John Stiles7571f9e2020-09-02 22:42:33 -0400302 using INHERITED = GM;
commit-bot@chromium.orga343c842014-01-31 14:48:58 +0000303};
304
halcanary385fe4d2015-08-26 13:07:48 -0700305DEF_GM(return new ConvexPolyClip;)
John Stilesa6841be2020-08-06 14:11:56 -0400306} // namespace skiagm