blob: a5c290456658f0294008616d42fafb88eee5a95a [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
12#if SK_SUPPORT_GPU
13
14#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080015#include "GrDefaultGeoProcFactory.h"
robertphillips391395d2016-03-02 09:26:36 -080016#include "GrDrawContextPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000017#include "GrPathUtils.h"
18#include "GrTest.h"
19#include "SkColorPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000020#include "SkGeometry.h"
21#include "SkTLList.h"
22
joshualitt2771b562015-08-07 12:46:26 -070023#include "batches/GrTestBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070024#include "batches/GrVertexBatch.h"
joshualitt2771b562015-08-07 12:46:26 -070025
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000026#include "effects/GrConvexPolyEffect.h"
27
bsalomon342bfc22016-04-01 06:06:20 -070028/** outset rendered rect to visualize anti-aliased poly edges */
29static SkRect outset(const SkRect& unsorted) {
30 SkRect r = unsorted;
31 r.outset(5.f, 5.f);
32 return r;
33}
joshualitt95964c62015-02-11 13:45:50 -080034
bsalomon342bfc22016-04-01 06:06:20 -070035/** sorts a rect */
36static SkRect sorted_rect(const SkRect& unsorted) {
37 SkRect r = unsorted;
38 r.sort();
39 return r;
40}
41
42namespace skiagm {
43class PolyBoundsBatch : public GrTestBatch {
joshualitt95964c62015-02-11 13:45:50 -080044public:
reed1b55a962015-09-17 20:16:13 -070045 DEFINE_BATCH_CLASS_ID
joshualitt95964c62015-02-11 13:45:50 -080046
bsalomon342bfc22016-04-01 06:06:20 -070047 const char* name() const override { return "PolyBoundsBatch"; }
joshualitt95964c62015-02-11 13:45:50 -080048
bsalomon342bfc22016-04-01 06:06:20 -070049 PolyBoundsBatch(const SkRect& rect, GrColor color)
50 : INHERITED(ClassID(), outset(sorted_rect(rect)), color)
51 , fRect(outset(rect)) {
joshualitt95964c62015-02-11 13:45:50 -080052 }
53
54private:
bsalomon342bfc22016-04-01 06:06:20 -070055 void onPrepareDraws(Target* target) const override {
56 using namespace GrDefaultGeoProcFactory;
joshualitt95964c62015-02-11 13:45:50 -080057
bsalomon342bfc22016-04-01 06:06:20 -070058 Color color(this->color());
59 Coverage coverage(Coverage::kSolid_Type);
60 LocalCoords localCoords(LocalCoords::kUnused_Type);
bungeman06ca8ec2016-06-09 08:01:03 -070061 sk_sp<GrGeometryProcessor> gp(
62 GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I()));
joshualitt95964c62015-02-11 13:45:50 -080063
bsalomon342bfc22016-04-01 06:06:20 -070064 size_t vertexStride = gp->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -070065 SkASSERT(vertexStride == sizeof(SkPoint));
66 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -070067 SkPoint* verts = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -070068 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -080069 return;
70 }
71
bsalomon342bfc22016-04-01 06:06:20 -070072 fRect.toQuad(verts);
joshualitt95964c62015-02-11 13:45:50 -080073
bungeman06ca8ec2016-06-09 08:01:03 -070074 helper.recordDraw(target, gp.get());
joshualitt95964c62015-02-11 13:45:50 -080075 }
76
bsalomon342bfc22016-04-01 06:06:20 -070077 SkRect fRect;
joshualitt95964c62015-02-11 13:45:50 -080078
joshualitt95964c62015-02-11 13:45:50 -080079 typedef GrTestBatch INHERITED;
80};
81
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000082/**
joshualittb0a8a372014-09-23 09:50:21 -070083 * This GM directly exercises a GrProcessor that draws convex polygons.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000084 */
85class ConvexPolyEffect : public GM {
86public:
87 ConvexPolyEffect() {
88 this->setBGColor(0xFFFFFFFF);
89 }
90
91protected:
mtklein36352bf2015-03-25 18:17:31 -070092 SkString onShortName() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000093 return SkString("convex_poly_effect");
94 }
95
mtklein36352bf2015-03-25 18:17:31 -070096 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070097 return SkISize::Make(720, 800);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000098 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000101 SkPath tri;
102 tri.moveTo(5.f, 5.f);
103 tri.lineTo(100.f, 20.f);
104 tri.lineTo(15.f, 100.f);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000105
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000106 fPaths.addToTail(tri);
107 fPaths.addToTail(SkPath())->reverseAddPath(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000108
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000109 tri.close();
110 fPaths.addToTail(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000111
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000112 SkPath ngon;
113 static const SkScalar kRadius = 50.f;
114 const SkPoint center = { kRadius, kRadius };
115 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
116 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
117 SkPoint point;
118 point.fY = SkScalarSinCos(angle, &point.fX);
119 point.scale(kRadius);
120 point = center + point;
121 if (0 == i) {
122 ngon.moveTo(point);
123 } else {
124 ngon.lineTo(point);
125 }
126 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000127
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000128 fPaths.addToTail(ngon);
129 SkMatrix scaleM;
130 scaleM.setScale(1.1f, 0.4f);
131 ngon.transform(scaleM);
132 fPaths.addToTail(ngon);
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000133
bsalomon7888de02016-03-28 15:04:45 -0700134 SkPath linePath;
135 linePath.moveTo(5.f, 5.f);
136 linePath.lineTo(6.f, 6.f);
137 fPaths.addToTail(linePath);
138
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000139 // integer edges
140 fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f));
141 // half-integer edges
142 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f));
143 // vertically/horizontally thin rects that cover pixel centers
144 fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f));
145 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f));
146 // vertically/horizontally thin rects that don't cover pixel centers
147 fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f));
148 fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f));
149 // small in x and y
150 fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f));
151 // inverted in x and y
152 fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000153 }
154
mtklein36352bf2015-03-25 18:17:31 -0700155 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700156 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
joshualittf5883a62016-01-13 07:47:38 -0800157 if (!drawContext) {
robertphillips175dd9b2016-04-28 14:32:04 -0700158 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualittf5883a62016-01-13 07:47:38 -0800159 return;
160 }
161
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000162 SkScalar y = 0;
bsalomon7888de02016-03-28 15:04:45 -0700163 static const SkScalar kDX = 12.f;
bsalomonf045d602015-11-18 19:01:12 -0800164 for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700165 iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000166 iter.next()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000167 const SkPath* path = iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000168 SkScalar x = 0;
169
joshualittb0a8a372014-09-23 09:50:21 -0700170 for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
robertphillips1d24b8d2015-03-26 19:57:08 -0700171 const SkMatrix m = SkMatrix::MakeTrans(x, y);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000172 SkPath p;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000173 path->transform(m, &p);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000174
joshualittb0a8a372014-09-23 09:50:21 -0700175 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
bungeman06ca8ec2016-06-09 08:01:03 -0700176 sk_sp<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, p));
joshualittb0a8a372014-09-23 09:50:21 -0700177 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000178 continue;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000179 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000180
robertphillips28a838e2016-06-23 14:07:00 -0700181 GrPaint grPaint;
182 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
183 grPaint.addCoverageFragmentProcessor(std::move(fp));
joshualitt9853cce2014-11-17 14:22:48 -0800184
bsalomon342bfc22016-04-01 06:06:20 -0700185 SkAutoTUnref<GrDrawBatch> batch(new PolyBoundsBatch(p.getBounds(), 0xff000000));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000186
robertphillips28a838e2016-06-23 14:07:00 -0700187 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000188
bsalomon7888de02016-03-28 15:04:45 -0700189 x += SkScalarCeilToScalar(path->getBounds().width() + kDX);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000190 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000191
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000192 // Draw AA and non AA paths using normal API for reference.
193 canvas->save();
194 canvas->translate(x, y);
195 SkPaint paint;
196 canvas->drawPath(*path, paint);
197 canvas->translate(path->getBounds().width() + 10.f, 0);
198 paint.setAntiAlias(true);
199 canvas->drawPath(*path, paint);
200 canvas->restore();
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000201
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000202 y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
203 }
204
bsalomonf045d602015-11-18 19:01:12 -0800205 for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700206 iter.get();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000207 iter.next()) {
208
209 SkScalar x = 0;
210
joshualittb0a8a372014-09-23 09:50:21 -0700211 for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000212 SkRect rect = *iter.get();
213 rect.offset(x, y);
joshualittb0a8a372014-09-23 09:50:21 -0700214 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
bungeman06ca8ec2016-06-09 08:01:03 -0700215 sk_sp<GrFragmentProcessor> fp(GrConvexPolyEffect::Make(edgeType, rect));
joshualittb0a8a372014-09-23 09:50:21 -0700216 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000217 continue;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000218 }
219
robertphillips28a838e2016-06-23 14:07:00 -0700220 GrPaint grPaint;
221 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
222 grPaint.addCoverageFragmentProcessor(std::move(fp));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000223
bsalomon342bfc22016-04-01 06:06:20 -0700224 SkAutoTUnref<GrDrawBatch> batch(new PolyBoundsBatch(rect, 0xff000000));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000225
robertphillips28a838e2016-06-23 14:07:00 -0700226 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000227
bsalomon7888de02016-03-28 15:04:45 -0700228 x += SkScalarCeilToScalar(rect.width() + kDX);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000229 }
230
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000231 // Draw rect without and with AA using normal API for reference
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000232 canvas->save();
233 canvas->translate(x, y);
234 SkPaint paint;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000235 canvas->drawRect(*iter.get(), paint);
bsalomon7888de02016-03-28 15:04:45 -0700236 x += SkScalarCeilToScalar(iter.get()->width() + kDX);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000237 paint.setAntiAlias(true);
238 canvas->drawRect(*iter.get(), paint);
239 canvas->restore();
240
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000241 y += SkScalarCeilToScalar(iter.get()->height() + 20.f);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000242 }
243 }
244
245private:
bsalomonf045d602015-11-18 19:01:12 -0800246 typedef SkTLList<SkPath, 1> PathList;
247 typedef SkTLList<SkRect, 1> RectList;
248 PathList fPaths;
249 RectList fRects;
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000250
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000251 typedef GM INHERITED;
252};
253
halcanary385fe4d2015-08-26 13:07:48 -0700254DEF_GM(return new ConvexPolyEffect;)
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000255}
256
257#endif