blob: 664a9e7b5f603b2b6cd1bbb8b0c780a2a2b76219 [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 "GrTest.h"
19#include "SkColorPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000020#include "SkGeometry.h"
Cary Clark74f623d2017-11-06 20:02:02 -050021#include "SkPointPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000022#include "SkTLList.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000023#include "effects/GrConvexPolyEffect.h"
Brian Salomon576eda12017-07-14 11:30:17 -040024#include "ops/GrMeshDrawOp.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000025
bsalomon342bfc22016-04-01 06:06:20 -070026/** outset rendered rect to visualize anti-aliased poly edges */
27static SkRect outset(const SkRect& unsorted) {
28 SkRect r = unsorted;
29 r.outset(5.f, 5.f);
30 return r;
31}
joshualitt95964c62015-02-11 13:45:50 -080032
bsalomon342bfc22016-04-01 06:06:20 -070033/** sorts a rect */
34static SkRect sorted_rect(const SkRect& unsorted) {
35 SkRect r = unsorted;
36 r.sort();
37 return r;
38}
39
40namespace skiagm {
Brian Salomon576eda12017-07-14 11:30:17 -040041class PolyBoundsOp : public GrMeshDrawOp {
joshualitt95964c62015-02-11 13:45:50 -080042public:
Brian Salomon25a88092016-12-01 09:36:50 -050043 DEFINE_OP_CLASS_ID
joshualitt95964c62015-02-11 13:45:50 -080044
Robert Phillips7c525e62018-06-12 10:11:12 -040045 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
46 GrPaint&& paint,
47 const SkRect& rect) {
Robert Phillipsc994a932018-06-19 13:09:54 -040048 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
49
50 return pool->allocate<PolyBoundsOp>(std::move(paint), rect);
Brian Salomon576eda12017-07-14 11:30:17 -040051 }
52
Robert Phillipsb493eeb2017-09-13 13:10:52 -040053 const char* name() const override { return "PolyBoundsOp"; }
54
Robert Phillipsf1748f52017-09-14 14:11:24 -040055 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040056 fProcessors.visitProxies(func);
57 }
58
Brian Salomon576eda12017-07-14 11:30:17 -040059 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
60
Brian Osman532b3f92018-07-11 10:02:07 -040061 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Osman9a725dd2017-09-20 09:53:22 -040062 auto analysis = fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip,
Brian Osman532b3f92018-07-11 10:02:07 -040063 false, caps, &fColor);
Brian Salomon576eda12017-07-14 11:30:17 -040064 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
joshualitt95964c62015-02-11 13:45:50 -080065 }
66
67private:
Robert Phillips7c525e62018-06-12 10:11:12 -040068 friend class ::GrOpMemoryPool; // for ctor
69
Brian Salomon576eda12017-07-14 11:30:17 -040070 PolyBoundsOp(GrPaint&& paint, const SkRect& rect)
71 : INHERITED(ClassID())
72 , fColor(paint.getColor())
73 , fProcessors(std::move(paint))
74 , fRect(outset(rect)) {
75 this->setBounds(sorted_rect(fRect), HasAABloat::kNo, IsZeroArea::kNo);
76 }
Brian Salomon6b316e92016-12-16 09:35:49 -050077
Brian Salomon91326c32017-08-09 16:02:19 -040078 void onPrepareDraws(Target* target) override {
bsalomon342bfc22016-04-01 06:06:20 -070079 using namespace GrDefaultGeoProcFactory;
joshualitt95964c62015-02-11 13:45:50 -080080
Brian Salomon576eda12017-07-14 11:30:17 -040081 Color color(fColor);
Brian Salomon8c852be2017-01-04 10:44:42 -050082 sk_sp<GrGeometryProcessor> gp(GrDefaultGeoProcFactory::Make(
Ruiqi Maob609e6d2018-07-17 10:19:38 -040083 target->caps().shaderCaps(),
84 color,
85 Coverage::kSolid_Type,
86 LocalCoords::kUnused_Type,
87 SkMatrix::I()));
joshualitt95964c62015-02-11 13:45:50 -080088
Brian Salomon92be2f72018-06-19 14:33:47 -040089 SkASSERT(gp->debugOnly_vertexStride() == sizeof(SkPoint));
Brian Salomon7eae3e02018-08-07 14:02:38 +000090 QuadHelper helper(target, sizeof(SkPoint), 1);
91 SkPoint* verts = reinterpret_cast<SkPoint*>(helper.vertices());
bsalomonb5238a72015-05-05 07:49:49 -070092 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -080093 return;
94 }
95
Brian Salomonec42e152018-05-18 12:52:22 -040096 SkPointPriv::SetRectTriStrip(verts, fRect, sizeof(SkPoint));
joshualitt95964c62015-02-11 13:45:50 -080097
Brian Salomon49348902018-06-26 09:12:38 -040098 auto pipe = target->makePipeline(0, std::move(fProcessors), target->detachAppliedClip());
Brian Salomon7eae3e02018-08-07 14:02:38 +000099 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt95964c62015-02-11 13:45:50 -0800100 }
101
Brian Salomon576eda12017-07-14 11:30:17 -0400102 GrColor fColor;
103 GrProcessorSet fProcessors;
bsalomon342bfc22016-04-01 06:06:20 -0700104 SkRect fRect;
joshualitt95964c62015-02-11 13:45:50 -0800105
Brian Salomon576eda12017-07-14 11:30:17 -0400106 typedef GrMeshDrawOp INHERITED;
joshualitt95964c62015-02-11 13:45:50 -0800107};
108
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000109/**
joshualittb0a8a372014-09-23 09:50:21 -0700110 * This GM directly exercises a GrProcessor that draws convex polygons.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000111 */
112class ConvexPolyEffect : public GM {
113public:
114 ConvexPolyEffect() {
115 this->setBGColor(0xFFFFFFFF);
116 }
117
118protected:
mtklein36352bf2015-03-25 18:17:31 -0700119 SkString onShortName() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000120 return SkString("convex_poly_effect");
121 }
122
mtklein36352bf2015-03-25 18:17:31 -0700123 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700124 return SkISize::Make(720, 800);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000125 }
126
mtklein36352bf2015-03-25 18:17:31 -0700127 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000128 SkPath tri;
129 tri.moveTo(5.f, 5.f);
130 tri.lineTo(100.f, 20.f);
131 tri.lineTo(15.f, 100.f);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000132
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000133 fPaths.addToTail(tri);
134 fPaths.addToTail(SkPath())->reverseAddPath(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000135
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000136 tri.close();
137 fPaths.addToTail(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000138
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000139 SkPath ngon;
mtkleindbfd7ab2016-09-01 11:24:54 -0700140 constexpr SkScalar kRadius = 50.f;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000141 const SkPoint center = { kRadius, kRadius };
142 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
143 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
144 SkPoint point;
145 point.fY = SkScalarSinCos(angle, &point.fX);
146 point.scale(kRadius);
147 point = center + point;
148 if (0 == i) {
149 ngon.moveTo(point);
150 } else {
151 ngon.lineTo(point);
152 }
153 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000154
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000155 fPaths.addToTail(ngon);
156 SkMatrix scaleM;
157 scaleM.setScale(1.1f, 0.4f);
158 ngon.transform(scaleM);
159 fPaths.addToTail(ngon);
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000160
bsalomon7888de02016-03-28 15:04:45 -0700161 SkPath linePath;
162 linePath.moveTo(5.f, 5.f);
163 linePath.lineTo(6.f, 6.f);
164 fPaths.addToTail(linePath);
165
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000166 // integer edges
167 fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f));
168 // half-integer edges
169 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f));
170 // vertically/horizontally thin rects that cover pixel centers
171 fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f));
172 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f));
173 // vertically/horizontally thin rects that don't cover pixel centers
174 fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f));
175 fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f));
176 // small in x and y
177 fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f));
178 // inverted in x and y
179 fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000180 }
181
mtklein36352bf2015-03-25 18:17:31 -0700182 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400183 GrRenderTargetContext* renderTargetContext =
184 canvas->internal_private_accessTopLayerRenderTargetContext();
185 if (!renderTargetContext) {
robertphillips175dd9b2016-04-28 14:32:04 -0700186 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualittf5883a62016-01-13 07:47:38 -0800187 return;
188 }
189
Robert Phillips7c525e62018-06-12 10:11:12 -0400190 GrContext* context = canvas->getGrContext();
191 if (!context) {
192 return;
193 }
194
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000195 SkScalar y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700196 constexpr SkScalar kDX = 12.f;
bsalomonf045d602015-11-18 19:01:12 -0800197 for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700198 iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000199 iter.next()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000200 const SkPath* path = iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000201 SkScalar x = 0;
202
Ethan Nicholas1706f842017-11-10 11:58:19 -0500203 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
robertphillips1d24b8d2015-03-26 19:57:08 -0700204 const SkMatrix m = SkMatrix::MakeTrans(x, y);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000205 SkPath p;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000206 path->transform(m, &p);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000207
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500208 GrClipEdgeType edgeType = (GrClipEdgeType) et;
Brian Salomonaff329b2017-08-11 09:40:37 -0400209 std::unique_ptr<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, p));
joshualittb0a8a372014-09-23 09:50:21 -0700210 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000211 continue;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000212 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000213
robertphillips28a838e2016-06-23 14:07:00 -0700214 GrPaint grPaint;
Brian Salomon576eda12017-07-14 11:30:17 -0400215 grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f));
Brian Salomona1633922017-01-09 11:46:10 -0500216 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -0700217 grPaint.addCoverageFragmentProcessor(std::move(fp));
joshualitt9853cce2014-11-17 14:22:48 -0800218
Brian Salomon576eda12017-07-14 11:30:17 -0400219 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -0400220 PolyBoundsOp::Make(context, std::move(grPaint), p.getBounds());
Brian Salomon576eda12017-07-14 11:30:17 -0400221 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000222
bsalomon7888de02016-03-28 15:04:45 -0700223 x += SkScalarCeilToScalar(path->getBounds().width() + kDX);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000224 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000225
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000226 // Draw AA and non AA paths using normal API for reference.
227 canvas->save();
228 canvas->translate(x, y);
229 SkPaint paint;
230 canvas->drawPath(*path, paint);
231 canvas->translate(path->getBounds().width() + 10.f, 0);
232 paint.setAntiAlias(true);
233 canvas->drawPath(*path, paint);
234 canvas->restore();
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000235
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000236 y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
237 }
238
bsalomonf045d602015-11-18 19:01:12 -0800239 for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700240 iter.get();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000241 iter.next()) {
242
243 SkScalar x = 0;
244
Ethan Nicholas1706f842017-11-10 11:58:19 -0500245 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000246 SkRect rect = *iter.get();
247 rect.offset(x, y);
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500248 GrClipEdgeType edgeType = (GrClipEdgeType) et;
Brian Salomonaff329b2017-08-11 09:40:37 -0400249 std::unique_ptr<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, rect));
joshualittb0a8a372014-09-23 09:50:21 -0700250 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000251 continue;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000252 }
253
robertphillips28a838e2016-06-23 14:07:00 -0700254 GrPaint grPaint;
Brian Salomon576eda12017-07-14 11:30:17 -0400255 grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f));
Brian Salomona1633922017-01-09 11:46:10 -0500256 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -0700257 grPaint.addCoverageFragmentProcessor(std::move(fp));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000258
Robert Phillips7c525e62018-06-12 10:11:12 -0400259 std::unique_ptr<GrDrawOp> op = PolyBoundsOp::Make(context, std::move(grPaint),
260 rect);
Brian Salomon576eda12017-07-14 11:30:17 -0400261 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000262
bsalomon7888de02016-03-28 15:04:45 -0700263 x += SkScalarCeilToScalar(rect.width() + kDX);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000264 }
265
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000266 // Draw rect without and with AA using normal API for reference
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000267 canvas->save();
268 canvas->translate(x, y);
269 SkPaint paint;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000270 canvas->drawRect(*iter.get(), paint);
bsalomon7888de02016-03-28 15:04:45 -0700271 x += SkScalarCeilToScalar(iter.get()->width() + kDX);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000272 paint.setAntiAlias(true);
273 canvas->drawRect(*iter.get(), paint);
274 canvas->restore();
275
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000276 y += SkScalarCeilToScalar(iter.get()->height() + 20.f);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000277 }
278 }
279
280private:
bsalomonf045d602015-11-18 19:01:12 -0800281 typedef SkTLList<SkPath, 1> PathList;
282 typedef SkTLList<SkRect, 1> RectList;
283 PathList fPaths;
284 RectList fRects;
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000285
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000286 typedef GM INHERITED;
287};
288
halcanary385fe4d2015-08-26 13:07:48 -0700289DEF_GM(return new ConvexPolyEffect;)
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000290}