blob: 4071159d48581d836299bf0c9573251f22ab25a0 [file] [log] [blame]
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +00001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9// This test only works with the GPU backend.
10
11#include "gm.h"
12
13#if SK_SUPPORT_GPU
14
15#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080016#include "GrDefaultGeoProcFactory.h"
joshualittf5883a62016-01-13 07:47:38 -080017#include "GrDrawContext.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000018#include "GrPathUtils.h"
19#include "GrTest.h"
20#include "SkColorPriv.h"
21#include "SkDevice.h"
22#include "SkGeometry.h"
23#include "SkTLList.h"
24
joshualitt2771b562015-08-07 12:46:26 -070025#include "batches/GrTestBatch.h"
bsalomon16b99132015-08-13 14:55:50 -070026#include "batches/GrVertexBatch.h"
joshualitt2771b562015-08-07 12:46:26 -070027
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000028#include "effects/GrConvexPolyEffect.h"
29
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000030namespace skiagm {
joshualitt95964c62015-02-11 13:45:50 -080031
32class ConvexPolyTestBatch : public GrTestBatch {
33public:
reed1b55a962015-09-17 20:16:13 -070034 DEFINE_BATCH_CLASS_ID
joshualitt95964c62015-02-11 13:45:50 -080035 struct Geometry : public GrTestBatch::Geometry {
joshualittf5883a62016-01-13 07:47:38 -080036 SkRect fRect;
37 SkRect fBounds; // This will be == fRect, except fBounds must be sorted, whereas fRect can
38 // be inverted
joshualitt95964c62015-02-11 13:45:50 -080039 };
40
mtklein36352bf2015-03-25 18:17:31 -070041 const char* name() const override { return "ConvexPolyTestBatch"; }
joshualitt95964c62015-02-11 13:45:50 -080042
bsalomonabd30f52015-08-13 13:34:48 -070043 static GrDrawBatch* Create(const GrGeometryProcessor* gp, const Geometry& geo) {
halcanary385fe4d2015-08-26 13:07:48 -070044 return new ConvexPolyTestBatch(gp, geo);
joshualitt95964c62015-02-11 13:45:50 -080045 }
46
47private:
48 ConvexPolyTestBatch(const GrGeometryProcessor* gp, const Geometry& geo)
reed1b55a962015-09-17 20:16:13 -070049 : INHERITED(ClassID(), gp, geo.fBounds)
joshualitt95964c62015-02-11 13:45:50 -080050 , fGeometry(geo) {
joshualitt144c3c82015-11-30 12:30:13 -080051 // Make sure any artifacts around the exterior of path are visible by using overly
52 // conservative bounding geometry.
53 fGeometry.fBounds.outset(5.f, 5.f);
joshualittf5883a62016-01-13 07:47:38 -080054 fGeometry.fRect.outset(5.f, 5.f);
joshualitt95964c62015-02-11 13:45:50 -080055 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 Geometry* geoData(int index) override {
joshualitt95964c62015-02-11 13:45:50 -080058 SkASSERT(0 == index);
59 return &fGeometry;
60 }
61
joshualitt88c23fc2015-05-13 14:18:07 -070062 const Geometry* geoData(int index) const override {
63 SkASSERT(0 == index);
64 return &fGeometry;
65 }
66
joshualitt144c3c82015-11-30 12:30:13 -080067 void generateGeometry(Target* target) const override {
bsalomoned0bcad2015-05-04 10:36:42 -070068 size_t vertexStride = this->geometryProcessor()->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -070069 SkASSERT(vertexStride == sizeof(SkPoint));
70 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -070071 SkPoint* verts = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -070072 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -080073 return;
74 }
75
joshualittf5883a62016-01-13 07:47:38 -080076 fGeometry.fRect.toQuad(verts);
joshualitt95964c62015-02-11 13:45:50 -080077
bsalomon75398562015-08-17 12:55:38 -070078 helper.recordDraw(target);
joshualitt95964c62015-02-11 13:45:50 -080079 }
80
81 Geometry fGeometry;
82
joshualitt95964c62015-02-11 13:45:50 -080083 typedef GrTestBatch INHERITED;
84};
85
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000086/**
joshualittb0a8a372014-09-23 09:50:21 -070087 * This GM directly exercises a GrProcessor that draws convex polygons.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000088 */
89class ConvexPolyEffect : public GM {
90public:
91 ConvexPolyEffect() {
92 this->setBGColor(0xFFFFFFFF);
93 }
94
95protected:
mtklein36352bf2015-03-25 18:17:31 -070096 SkString onShortName() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000097 return SkString("convex_poly_effect");
98 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700101 return SkISize::Make(720, 800);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000102 }
103
mtklein36352bf2015-03-25 18:17:31 -0700104 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000105 SkPath tri;
106 tri.moveTo(5.f, 5.f);
107 tri.lineTo(100.f, 20.f);
108 tri.lineTo(15.f, 100.f);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000109
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000110 fPaths.addToTail(tri);
111 fPaths.addToTail(SkPath())->reverseAddPath(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000112
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000113 tri.close();
114 fPaths.addToTail(tri);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000115
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000116 SkPath ngon;
117 static const SkScalar kRadius = 50.f;
118 const SkPoint center = { kRadius, kRadius };
119 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
120 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
121 SkPoint point;
122 point.fY = SkScalarSinCos(angle, &point.fX);
123 point.scale(kRadius);
124 point = center + point;
125 if (0 == i) {
126 ngon.moveTo(point);
127 } else {
128 ngon.lineTo(point);
129 }
130 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000131
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000132 fPaths.addToTail(ngon);
133 SkMatrix scaleM;
134 scaleM.setScale(1.1f, 0.4f);
135 ngon.transform(scaleM);
136 fPaths.addToTail(ngon);
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000137
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000138 // integer edges
139 fRects.addToTail(SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f));
140 // half-integer edges
141 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f));
142 // vertically/horizontally thin rects that cover pixel centers
143 fRects.addToTail(SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f));
144 fRects.addToTail(SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f));
145 // vertically/horizontally thin rects that don't cover pixel centers
146 fRects.addToTail(SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f));
147 fRects.addToTail(SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f));
148 // small in x and y
149 fRects.addToTail(SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f));
150 // inverted in x and y
151 fRects.addToTail(SkRect::MakeLTRB(100.f, 50.5f, 5.f, 0.5f));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000152 }
153
mtklein36352bf2015-03-25 18:17:31 -0700154 void onDraw(SkCanvas* canvas) override {
joshualittdf0c5572015-08-03 11:35:28 -0700155 using namespace GrDefaultGeoProcFactory;
reed@google.com9c135db2014-03-12 18:28:35 +0000156 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -0700157 if (nullptr == rt) {
halcanary2a243382015-09-09 08:16:41 -0700158 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000159 return;
160 }
161 GrContext* context = rt->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -0700162 if (nullptr == context) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000163 return;
164 }
165
joshualittf5883a62016-01-13 07:47:38 -0800166 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
167 if (!drawContext) {
168 return;
169 }
170
joshualittdf0c5572015-08-03 11:35:28 -0700171 Color color(0xff000000);
172 Coverage coverage(Coverage::kSolid_Type);
173 LocalCoords localCoords(LocalCoords::kUnused_Type);
joshualitt95964c62015-02-11 13:45:50 -0800174 SkAutoTUnref<const GrGeometryProcessor> gp(
joshualittdf0c5572015-08-03 11:35:28 -0700175 GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()));
joshualitt95964c62015-02-11 13:45:50 -0800176
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000177 SkScalar y = 0;
bsalomonf045d602015-11-18 19:01:12 -0800178 for (PathList::Iter iter(fPaths, PathList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700179 iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000180 iter.next()) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000181 const SkPath* path = iter.get();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000182 SkScalar x = 0;
183
joshualittb0a8a372014-09-23 09:50:21 -0700184 for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
robertphillips1d24b8d2015-03-26 19:57:08 -0700185 const SkMatrix m = SkMatrix::MakeTrans(x, y);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000186 SkPath p;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000187 path->transform(m, &p);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000188
joshualittb0a8a372014-09-23 09:50:21 -0700189 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
190 SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, p));
191 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000192 continue;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000193 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000194
egdaniel8dd688b2015-01-22 10:16:09 -0800195 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800196 pipelineBuilder.setXPFactory(
197 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomonac856c92015-08-27 06:30:17 -0700198 pipelineBuilder.addCoverageFragmentProcessor(fp);
egdaniel8dd688b2015-01-22 10:16:09 -0800199 pipelineBuilder.setRenderTarget(rt);
joshualitt9853cce2014-11-17 14:22:48 -0800200
joshualitt95964c62015-02-11 13:45:50 -0800201 ConvexPolyTestBatch::Geometry geometry;
joshualittdf0c5572015-08-03 11:35:28 -0700202 geometry.fColor = color.fColor;
joshualittf5883a62016-01-13 07:47:38 -0800203 geometry.fRect = p.getBounds();
joshualitt95964c62015-02-11 13:45:50 -0800204 geometry.fBounds = p.getBounds();
joshualitt50408ad2014-11-03 12:31:14 -0800205
bsalomonabd30f52015-08-13 13:34:48 -0700206 SkAutoTUnref<GrDrawBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000207
joshualittf5883a62016-01-13 07:47:38 -0800208 drawContext->internal_drawBatch(pipelineBuilder, batch);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000209
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000210 x += SkScalarCeilToScalar(path->getBounds().width() + 10.f);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000211 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000212
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000213 // Draw AA and non AA paths using normal API for reference.
214 canvas->save();
215 canvas->translate(x, y);
216 SkPaint paint;
217 canvas->drawPath(*path, paint);
218 canvas->translate(path->getBounds().width() + 10.f, 0);
219 paint.setAntiAlias(true);
220 canvas->drawPath(*path, paint);
221 canvas->restore();
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000222
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000223 y += SkScalarCeilToScalar(path->getBounds().height() + 20.f);
224 }
225
bsalomonf045d602015-11-18 19:01:12 -0800226 for (RectList::Iter iter(fRects, RectList::Iter::kHead_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700227 iter.get();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000228 iter.next()) {
229
230 SkScalar x = 0;
231
joshualittb0a8a372014-09-23 09:50:21 -0700232 for (int et = 0; et < kGrProcessorEdgeTypeCnt; ++et) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000233 SkRect rect = *iter.get();
234 rect.offset(x, y);
joshualittb0a8a372014-09-23 09:50:21 -0700235 GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
236 SkAutoTUnref<GrFragmentProcessor> fp(GrConvexPolyEffect::Create(edgeType, rect));
237 if (!fp) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000238 continue;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000239 }
240
egdaniel8dd688b2015-01-22 10:16:09 -0800241 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800242 pipelineBuilder.setXPFactory(
243 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomonac856c92015-08-27 06:30:17 -0700244 pipelineBuilder.addCoverageFragmentProcessor(fp);
egdaniel8dd688b2015-01-22 10:16:09 -0800245 pipelineBuilder.setRenderTarget(rt);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000246
joshualitt95964c62015-02-11 13:45:50 -0800247 ConvexPolyTestBatch::Geometry geometry;
joshualittdf0c5572015-08-03 11:35:28 -0700248 geometry.fColor = color.fColor;
joshualittf5883a62016-01-13 07:47:38 -0800249 geometry.fRect = rect;
joshualitt95964c62015-02-11 13:45:50 -0800250 geometry.fBounds = rect;
joshualittf5883a62016-01-13 07:47:38 -0800251 geometry.fBounds.sort();
joshualitt50408ad2014-11-03 12:31:14 -0800252
bsalomonabd30f52015-08-13 13:34:48 -0700253 SkAutoTUnref<GrDrawBatch> batch(ConvexPolyTestBatch::Create(gp, geometry));
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000254
joshualittf5883a62016-01-13 07:47:38 -0800255 drawContext->internal_drawBatch(pipelineBuilder, batch);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000256
257 x += SkScalarCeilToScalar(rect.width() + 10.f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000258 }
259
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000260 // Draw rect without and with AA using normal API for reference
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000261 canvas->save();
262 canvas->translate(x, y);
263 SkPaint paint;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000264 canvas->drawRect(*iter.get(), paint);
265 x += SkScalarCeilToScalar(iter.get()->width() + 10.f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000266 paint.setAntiAlias(true);
267 canvas->drawRect(*iter.get(), paint);
268 canvas->restore();
269
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000270 y += SkScalarCeilToScalar(iter.get()->height() + 20.f);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000271 }
272 }
273
274private:
bsalomonf045d602015-11-18 19:01:12 -0800275 typedef SkTLList<SkPath, 1> PathList;
276 typedef SkTLList<SkRect, 1> RectList;
277 PathList fPaths;
278 RectList fRects;
skia.committer@gmail.comf0b0cda2014-02-09 03:02:01 +0000279
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000280 typedef GM INHERITED;
281};
282
halcanary385fe4d2015-08-26 13:07:48 -0700283DEF_GM(return new ConvexPolyEffect;)
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000284}
285
286#endif