blob: 22aa69dab8b3e05eae751e74d0048f001ac9253b [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
10#include "gm.h"
11
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000012#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080013#include "GrDefaultGeoProcFactory.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040014#include "GrMemoryPool.h"
Brian Salomon576eda12017-07-14 11:30:17 -040015#include "GrOpFlushState.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000016#include "GrPathUtils.h"
Brian Salomon576eda12017-07-14 11:30:17 -040017#include "GrRenderTargetContextPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000018#include "SkColorPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000019#include "SkGeometry.h"
Cary Clark74f623d2017-11-06 20:02:02 -050020#include "SkPointPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000021#include "SkTLList.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000022#include "effects/GrConvexPolyEffect.h"
Brian Salomon576eda12017-07-14 11:30:17 -040023#include "ops/GrMeshDrawOp.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000024
bsalomon342bfc22016-04-01 06:06:20 -070025/** outset rendered rect to visualize anti-aliased poly edges */
26static SkRect outset(const SkRect& unsorted) {
27 SkRect r = unsorted;
28 r.outset(5.f, 5.f);
29 return r;
30}
joshualitt95964c62015-02-11 13:45:50 -080031
bsalomon342bfc22016-04-01 06:06:20 -070032/** sorts a rect */
33static SkRect sorted_rect(const SkRect& unsorted) {
34 SkRect r = unsorted;
35 r.sort();
36 return r;
37}
38
39namespace skiagm {
Brian Salomon576eda12017-07-14 11:30:17 -040040class PolyBoundsOp : public GrMeshDrawOp {
joshualitt95964c62015-02-11 13:45:50 -080041public:
Brian Salomon25a88092016-12-01 09:36:50 -050042 DEFINE_OP_CLASS_ID
joshualitt95964c62015-02-11 13:45:50 -080043
Robert Phillips7c525e62018-06-12 10:11:12 -040044 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
45 GrPaint&& paint,
46 const SkRect& rect) {
Robert Phillipsc994a932018-06-19 13:09:54 -040047 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
48
49 return pool->allocate<PolyBoundsOp>(std::move(paint), rect);
Brian Salomon576eda12017-07-14 11:30:17 -040050 }
51
Robert Phillipsb493eeb2017-09-13 13:10:52 -040052 const char* name() const override { return "PolyBoundsOp"; }
53
Brian Salomon7d94bb52018-10-12 14:37:19 -040054 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040055 fProcessors.visitProxies(func);
56 }
57
Brian Salomon576eda12017-07-14 11:30:17 -040058 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
59
Brian Osman532b3f92018-07-11 10:02:07 -040060 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Osman9a725dd2017-09-20 09:53:22 -040061 auto analysis = fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip,
Brian Osman532b3f92018-07-11 10:02:07 -040062 false, caps, &fColor);
Brian Salomon576eda12017-07-14 11:30:17 -040063 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitt95964c62015-02-11 13:45:50 -080064 }
65
66private:
Robert Phillips7c525e62018-06-12 10:11:12 -040067 friend class ::GrOpMemoryPool; // for ctor
68
Brian Salomon576eda12017-07-14 11:30:17 -040069 PolyBoundsOp(GrPaint&& paint, const SkRect& rect)
70 : INHERITED(ClassID())
Brian Osmancf860852018-10-31 14:04:39 -040071 , fColor(paint.getColor4f())
Brian Salomon576eda12017-07-14 11:30:17 -040072 , fProcessors(std::move(paint))
73 , fRect(outset(rect)) {
74 this->setBounds(sorted_rect(fRect), HasAABloat::kNo, IsZeroArea::kNo);
75 }
Brian Salomon6b316e92016-12-16 09:35:49 -050076
Brian Salomon91326c32017-08-09 16:02:19 -040077 void onPrepareDraws(Target* target) override {
bsalomon342bfc22016-04-01 06:06:20 -070078 using namespace GrDefaultGeoProcFactory;
joshualitt95964c62015-02-11 13:45:50 -080079
Brian Salomon576eda12017-07-14 11:30:17 -040080 Color color(fColor);
Brian Salomon8c852be2017-01-04 10:44:42 -050081 sk_sp<GrGeometryProcessor> gp(GrDefaultGeoProcFactory::Make(
Ruiqi Maob609e6d2018-07-17 10:19:38 -040082 target->caps().shaderCaps(),
83 color,
84 Coverage::kSolid_Type,
85 LocalCoords::kUnused_Type,
86 SkMatrix::I()));
joshualitt95964c62015-02-11 13:45:50 -080087
Brian Osmanf04fb3c2018-11-12 15:34:00 -050088 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
Brian Salomon7eae3e02018-08-07 14:02:38 +000089 QuadHelper helper(target, sizeof(SkPoint), 1);
90 SkPoint* verts = reinterpret_cast<SkPoint*>(helper.vertices());
bsalomonb5238a72015-05-05 07:49:49 -070091 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -080092 return;
93 }
94
Brian Salomonec42e152018-05-18 12:52:22 -040095 SkPointPriv::SetRectTriStrip(verts, fRect, sizeof(SkPoint));
joshualitt95964c62015-02-11 13:45:50 -080096
Brian Salomon49348902018-06-26 09:12:38 -040097 auto pipe = target->makePipeline(0, std::move(fProcessors), target->detachAppliedClip());
Brian Salomon7eae3e02018-08-07 14:02:38 +000098 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt95964c62015-02-11 13:45:50 -080099 }
100
Brian Osmancf860852018-10-31 14:04:39 -0400101 SkPMColor4f fColor;
Brian Salomon576eda12017-07-14 11:30:17 -0400102 GrProcessorSet fProcessors;
bsalomon342bfc22016-04-01 06:06:20 -0700103 SkRect fRect;
joshualitt95964c62015-02-11 13:45:50 -0800104
Brian Salomon576eda12017-07-14 11:30:17 -0400105 typedef GrMeshDrawOp INHERITED;
joshualitt95964c62015-02-11 13:45:50 -0800106};
107
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000108/**
joshualittb0a8a372014-09-23 09:50:21 -0700109 * This GM directly exercises a GrProcessor that draws convex polygons.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000110 */
111class ConvexPolyEffect : public GM {
112public:
113 ConvexPolyEffect() {
114 this->setBGColor(0xFFFFFFFF);
115 }
116
117protected:
mtklein36352bf2015-03-25 18:17:31 -0700118 SkString onShortName() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000119 return SkString("convex_poly_effect");
120 }
121
mtklein36352bf2015-03-25 18:17:31 -0700122 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700123 return SkISize::Make(720, 800);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000124 }
125
mtklein36352bf2015-03-25 18:17:31 -0700126 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000127 SkPath tri;
128 tri.moveTo(5.f, 5.f);
129 tri.lineTo(100.f, 20.f);
130 tri.lineTo(15.f, 100.f);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000131
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000132 fPaths.addToTail(tri);
133 fPaths.addToTail(SkPath())->reverseAddPath(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000134
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000135 tri.close();
136 fPaths.addToTail(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000137
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000138 SkPath ngon;
mtkleindbfd7ab2016-09-01 11:24:54 -0700139 constexpr SkScalar kRadius = 50.f;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000140 const SkPoint center = { kRadius, kRadius };
141 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
142 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
143 SkPoint point;
144 point.fY = SkScalarSinCos(angle, &point.fX);
145 point.scale(kRadius);
146 point = center + point;
147 if (0 == i) {
148 ngon.moveTo(point);
149 } else {
150 ngon.lineTo(point);
151 }
152 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000153
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000154 fPaths.addToTail(ngon);
155 SkMatrix scaleM;
156 scaleM.setScale(1.1f, 0.4f);
157 ngon.transform(scaleM);
158 fPaths.addToTail(ngon);
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000159
bsalomon7888de02016-03-28 15:04:45 -0700160 SkPath linePath;
161 linePath.moveTo(5.f, 5.f);
162 linePath.lineTo(6.f, 6.f);
163 fPaths.addToTail(linePath);
164
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000165 // integer edges
166 fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f));
167 // half-integer edges
168 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f));
169 // vertically/horizontally thin rects that cover pixel centers
170 fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f));
171 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f));
172 // vertically/horizontally thin rects that don't cover pixel centers
173 fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f));
174 fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f));
175 // small in x and y
176 fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f));
177 // inverted in x and y
178 fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000179 }
180
mtklein36352bf2015-03-25 18:17:31 -0700181 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400182 GrRenderTargetContext* renderTargetContext =
183 canvas->internal_private_accessTopLayerRenderTargetContext();
184 if (!renderTargetContext) {
robertphillips175dd9b2016-04-28 14:32:04 -0700185 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualittf5883a62016-01-13 07:47:38 -0800186 return;
187 }
188
Robert Phillips7c525e62018-06-12 10:11:12 -0400189 GrContext* context = canvas->getGrContext();
190 if (!context) {
191 return;
192 }
193
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000194 SkScalar y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700195 constexpr SkScalar kDX = 12.f;
bsalomonf045d602015-11-18 19:01:12 -0800196 for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700197 iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000198 iter.next()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000199 const SkPath* path = iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000200 SkScalar x = 0;
201
Ethan Nicholas1706f842017-11-10 11:58:19 -0500202 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
robertphillips1d24b8d2015-03-26 19:57:08 -0700203 const SkMatrix m = SkMatrix::MakeTrans(x, y);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000204 SkPath p;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000205 path->transform(m, &p);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000206
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500207 GrClipEdgeType edgeType = (GrClipEdgeType) et;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000208 std::unique_ptr<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, p));
joshualittb0a8a372014-09-23 09:50:21 -0700209 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000210 continue;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000211 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000212
robertphillips28a838e2016-06-23 14:07:00 -0700213 GrPaint grPaint;
Brian Osmancb3d0872018-10-16 15:19:28 -0400214 grPaint.setColor4f({ 0, 0, 0, 1.f });
Brian Salomona1633922017-01-09 11:46:10 -0500215 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -0700216 grPaint.addCoverageFragmentProcessor(std::move(fp));
joshualitt9853cce2014-11-17 14:22:48 -0800217
Brian Salomon576eda12017-07-14 11:30:17 -0400218 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -0400219 PolyBoundsOp::Make(context, std::move(grPaint), p.getBounds());
Brian Salomon576eda12017-07-14 11:30:17 -0400220 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000221
bsalomon7888de02016-03-28 15:04:45 -0700222 x += SkScalarCeilToScalar(path->getBounds().width() + kDX);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000223 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000224
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000225 // Draw AA and non AA paths using normal API for reference.
226 canvas->save();
227 canvas->translate(x, y);
228 SkPaint paint;
229 canvas->drawPath(*path, paint);
230 canvas->translate(path->getBounds().width() + 10.f, 0);
231 paint.setAntiAlias(true);
232 canvas->drawPath(*path, paint);
233 canvas->restore();
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000234
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000235 y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
236 }
237
bsalomonf045d602015-11-18 19:01:12 -0800238 for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700239 iter.get();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000240 iter.next()) {
241
242 SkScalar x = 0;
243
Ethan Nicholas1706f842017-11-10 11:58:19 -0500244 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000245 SkRect rect = *iter.get();
246 rect.offset(x, y);
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500247 GrClipEdgeType edgeType = (GrClipEdgeType) et;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000248 std::unique_ptr<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, rect));
joshualittb0a8a372014-09-23 09:50:21 -0700249 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000250 continue;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000251 }
252
robertphillips28a838e2016-06-23 14:07:00 -0700253 GrPaint grPaint;
Brian Osmancb3d0872018-10-16 15:19:28 -0400254 grPaint.setColor4f({ 0, 0, 0, 1.f });
Brian Salomona1633922017-01-09 11:46:10 -0500255 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -0700256 grPaint.addCoverageFragmentProcessor(std::move(fp));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000257
Robert Phillips7c525e62018-06-12 10:11:12 -0400258 std::unique_ptr<GrDrawOp> op = PolyBoundsOp::Make(context, std::move(grPaint),
259 rect);
Brian Salomon576eda12017-07-14 11:30:17 -0400260 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000261
bsalomon7888de02016-03-28 15:04:45 -0700262 x += SkScalarCeilToScalar(rect.width() + kDX);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000263 }
264
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000265 // Draw rect without and with AA using normal API for reference
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000266 canvas->save();
267 canvas->translate(x, y);
268 SkPaint paint;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000269 canvas->drawRect(*iter.get(), paint);
bsalomon7888de02016-03-28 15:04:45 -0700270 x += SkScalarCeilToScalar(iter.get()->width() + kDX);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000271 paint.setAntiAlias(true);
272 canvas->drawRect(*iter.get(), paint);
273 canvas->restore();
274
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000275 y += SkScalarCeilToScalar(iter.get()->height() + 20.f);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000276 }
277 }
278
279private:
bsalomonf045d602015-11-18 19:01:12 -0800280 typedef SkTLList<SkPath, 1> PathList;
281 typedef SkTLList<SkRect, 1> RectList;
282 PathList fPaths;
283 RectList fRects;
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000284
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000285 typedef GM INHERITED;
286};
287
halcanary385fe4d2015-08-26 13:07:48 -0700288DEF_GM(return new ConvexPolyEffect;)
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000289}