blob: d665da8a8e9e286a4916d87a69d9d76fe3d9694d [file] [log] [blame]
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +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// This test only works with the GPU backend.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkBlendMode.h"
12#include "include/core/SkCanvas.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPath.h"
16#include "include/core/SkPoint.h"
17#include "include/core/SkRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/private/GrSharedEnums.h"
22#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkTLList.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "src/gpu/GrFragmentProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "src/gpu/GrPaint.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050026#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/effects/GrConvexPolyEffect.h"
Brian Salomon80313c82021-02-24 10:19:11 -050028#include "tools/gpu/TestOps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029
30#include <memory>
31#include <utility>
32
33class GrAppliedClip;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000034
bsalomon342bfc22016-04-01 06:06:20 -070035namespace skiagm {
joshualitt95964c62015-02-11 13:45:50 -080036
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000037/**
joshualittb0a8a372014-09-23 09:50:21 -070038 * This GM directly exercises a GrProcessor that draws convex polygons.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000039 */
Chris Dalton3a778372019-02-07 15:23:36 -070040class ConvexPolyEffect : public GpuGM {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000041public:
42 ConvexPolyEffect() {
43 this->setBGColor(0xFFFFFFFF);
44 }
45
46protected:
mtklein36352bf2015-03-25 18:17:31 -070047 SkString onShortName() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000048 return SkString("convex_poly_effect");
49 }
50
Brian Salomon066f4112021-02-23 10:55:22 -050051 SkISize onISize() override { return SkISize::Make(720, 550); }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000052
mtklein36352bf2015-03-25 18:17:31 -070053 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000054 SkPath tri;
55 tri.moveTo(5.f, 5.f);
56 tri.lineTo(100.f, 20.f);
57 tri.lineTo(15.f, 100.f);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000058
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000059 fPaths.addToTail(tri);
60 fPaths.addToTail(SkPath())->reverseAddPath(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000061
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000062 tri.close();
63 fPaths.addToTail(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000064
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000065 SkPath ngon;
mtkleindbfd7ab2016-09-01 11:24:54 -070066 constexpr SkScalar kRadius = 50.f;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000067 const SkPoint center = { kRadius, kRadius };
68 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
69 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
Brian Osman4428f2c2019-04-02 10:59:28 -040070 SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000071 point.scale(kRadius);
72 point = center + point;
73 if (0 == i) {
74 ngon.moveTo(point);
75 } else {
76 ngon.lineTo(point);
77 }
78 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000079
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000080 fPaths.addToTail(ngon);
81 SkMatrix scaleM;
82 scaleM.setScale(1.1f, 0.4f);
83 ngon.transform(scaleM);
84 fPaths.addToTail(ngon);
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +000085
bsalomon7888de02016-03-28 15:04:45 -070086 SkPath linePath;
87 linePath.moveTo(5.f, 5.f);
88 linePath.lineTo(6.f, 6.f);
89 fPaths.addToTail(linePath);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000090 }
91
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050092 void onDraw(GrRecordingContext* context, GrSurfaceDrawContext* surfaceDrawContext,
Chris Dalton3a778372019-02-07 15:23:36 -070093 SkCanvas* canvas) override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000094 SkScalar y = 0;
Brian Salomone21af502019-11-22 16:56:36 -050095 static constexpr SkScalar kDX = 12.f;
96 static constexpr SkScalar kOutset = 5.f;
97
bsalomonf045d602015-11-18 19:01:12 -080098 for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -070099 iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000100 iter.next()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000101 const SkPath* path = iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000102 SkScalar x = 0;
103
Ethan Nicholas1706f842017-11-10 11:58:19 -0500104 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
Mike Reed1f607332020-05-21 12:11:27 -0400105 const SkMatrix m = SkMatrix::Translate(x, y);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000106 SkPath p;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000107 path->transform(m, &p);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000108
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500109 GrClipEdgeType edgeType = (GrClipEdgeType) et;
John Stilesf08a82b2020-06-16 16:34:12 -0400110 auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
111 if (!success) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000112 continue;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000113 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000114
Brian Salomon80313c82021-02-24 10:19:11 -0500115 GrPaint grPaint;
116 grPaint.setColor4f({ 0, 0, 0, 1.f });
117 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
118 grPaint.setCoverageFragmentProcessor(std::move(fp));
119 auto rect = p.getBounds().makeOutset(kOutset, kOutset);
120 auto op = sk_gpu_test::test_ops::MakeRect(context, std::move(grPaint), rect);
121 surfaceDrawContext->addDrawOp(std::move(op));
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000122
bsalomon7888de02016-03-28 15:04:45 -0700123 x += SkScalarCeilToScalar(path->getBounds().width() + kDX);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000124 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000125
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000126 // Draw AA and non AA paths using normal API for reference.
127 canvas->save();
128 canvas->translate(x, y);
129 SkPaint paint;
130 canvas->drawPath(*path, paint);
131 canvas->translate(path->getBounds().width() + 10.f, 0);
132 paint.setAntiAlias(true);
133 canvas->drawPath(*path, paint);
134 canvas->restore();
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000135
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000136 y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
137 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000138 }
139
140private:
bsalomonf045d602015-11-18 19:01:12 -0800141 typedef SkTLList<SkPath, 1> PathList;
bsalomonf045d602015-11-18 19:01:12 -0800142 PathList fPaths;
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000143
John Stiles7571f9e2020-09-02 22:42:33 -0400144 using INHERITED = GM;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000145};
146
halcanary385fe4d2015-08-26 13:07:48 -0700147DEF_GM(return new ConvexPolyEffect;)
John Stilesa6841be2020-08-06 14:11:56 -0400148} // namespace skiagm