blob: cc28538dd0ce75ae9d246d4066eca44a9f70554f [file] [log] [blame]
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001/*
2 * Copyright 2013 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.org53a0b6c2013-08-23 18:05:01 +000012#if SK_SUPPORT_GPU
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000013
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContextPriv.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000015#include "GrContext.h"
16#include "GrPathUtils.h"
17#include "GrTest.h"
18#include "SkColorPriv.h"
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000019#include "SkGeometry.h"
20
Brian Salomon89527432016-12-16 09:52:16 -050021#include "ops/GrTestMeshDrawOp.h"
joshualitt2771b562015-08-07 12:46:26 -070022
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000023#include "effects/GrBezierEffect.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000024
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000025static inline SkScalar eval_line(const SkPoint& p, const SkScalar lineEq[3], SkScalar sign) {
26 return sign * (lineEq[0] * p.fX + lineEq[1] * p.fY + lineEq[2]);
27}
28
29namespace skiagm {
joshualitt95964c62015-02-11 13:45:50 -080030
Brian Salomon6b316e92016-12-16 09:35:49 -050031class BezierCubicOrConicTestOp : public GrTestMeshDrawOp {
joshualitt95964c62015-02-11 13:45:50 -080032public:
Brian Salomon25a88092016-12-01 09:36:50 -050033 DEFINE_OP_CLASS_ID
joshualitt95964c62015-02-11 13:45:50 -080034
Brian Salomon6b316e92016-12-16 09:35:49 -050035 const char* name() const override { return "BezierCubicOrConicTestOp"; }
joshualitt95964c62015-02-11 13:45:50 -080036
Brian Salomonf8334782017-01-03 09:42:58 -050037 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds,
38 GrColor color, const SkScalar klmEqs[9], SkScalar sign) {
39 return std::unique_ptr<GrDrawOp>(
40 new BezierCubicOrConicTestOp(gp, bounds, color, klmEqs, sign));
Brian Salomon6b316e92016-12-16 09:35:49 -050041 }
42
43private:
44 BezierCubicOrConicTestOp(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color,
45 const SkScalar klmEqs[9], SkScalar sign)
46 : INHERITED(ClassID(), bounds, color), fGeometryProcessor(std::move(gp)) {
joshualitt95964c62015-02-11 13:45:50 -080047 for (int i = 0; i < 9; i++) {
48 fKlmEqs[i] = klmEqs[i];
49 }
joshualitt95964c62015-02-11 13:45:50 -080050 fSign = sign;
51 }
joshualitt95964c62015-02-11 13:45:50 -080052 struct Vertex {
53 SkPoint fPosition;
54 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
55 };
56
bsalomon342bfc22016-04-01 06:06:20 -070057 void onPrepareDraws(Target* target) const override {
bsalomonb5238a72015-05-05 07:49:49 -070058 QuadHelper helper;
bsalomon342bfc22016-04-01 06:06:20 -070059 size_t vertexStride = fGeometryProcessor->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -070060 SkASSERT(vertexStride == sizeof(Vertex));
bsalomon75398562015-08-17 12:55:38 -070061 Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -070062 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -080063 return;
64 }
bsalomon342bfc22016-04-01 06:06:20 -070065 const SkRect& bounds = this->bounds();
66 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
joshualitt95964c62015-02-11 13:45:50 -080067 sizeof(Vertex));
68 for (int v = 0; v < 4; ++v) {
69 verts[v].fKLM[0] = eval_line(verts[v].fPosition, fKlmEqs + 0, fSign);
70 verts[v].fKLM[1] = eval_line(verts[v].fPosition, fKlmEqs + 3, fSign);
71 verts[v].fKLM[2] = eval_line(verts[v].fPosition, fKlmEqs + 6, 1.f);
72 }
bungeman06ca8ec2016-06-09 08:01:03 -070073 helper.recordDraw(target, fGeometryProcessor.get());
joshualitt95964c62015-02-11 13:45:50 -080074 }
75
bungeman06ca8ec2016-06-09 08:01:03 -070076 SkScalar fKlmEqs[9];
77 SkScalar fSign;
78 sk_sp<GrGeometryProcessor> fGeometryProcessor;
joshualitt95964c62015-02-11 13:45:50 -080079
mtkleindbfd7ab2016-09-01 11:24:54 -070080 static constexpr int kVertsPerCubic = 4;
81 static constexpr int kIndicesPerCubic = 6;
joshualitt95964c62015-02-11 13:45:50 -080082
Brian Salomon6b316e92016-12-16 09:35:49 -050083 typedef GrTestMeshDrawOp INHERITED;
joshualitt95964c62015-02-11 13:45:50 -080084};
85
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000086/**
87 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
88 */
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000089class BezierCubicEffects : public GM {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000090public:
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000091 BezierCubicEffects() {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000092 this->setBGColor(0xFFFFFFFF);
93 }
94
95protected:
mtklein36352bf2015-03-25 18:17:31 -070096 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000097 return SkString("bezier_cubic_effects");
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000098 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700101 return SkISize::Make(800, 800);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000102 }
103
mtklein36352bf2015-03-25 18:17:31 -0700104 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400105 GrRenderTargetContext* renderTargetContext =
106 canvas->internal_private_accessTopLayerRenderTargetContext();
107 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -0700108 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000109 return;
110 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000111
robertphillips175dd9b2016-04-28 14:32:04 -0700112 GrContext* context = canvas->getGrContext();
113 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800114 return;
115 }
116
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000117 struct Vertex {
118 SkPoint fPosition;
119 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
120 };
121
mtkleindbfd7ab2016-09-01 11:24:54 -0700122 constexpr int kNumCubics = 15;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000123 SkRandom rand;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000124
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000125 // Mult by 3 for each edge effect type
126 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3)));
127 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols);
Brian Osman11052242016-10-27 14:47:55 -0400128 SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols;
129 SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000130 int row = 0;
131 int col = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700132 constexpr GrColor color = 0xff000000;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000133
134 for (int i = 0; i < kNumCubics; ++i) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000135 SkPoint baseControlPts[] = {
136 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
137 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
138 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
139 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000140 };
joshualittb0a8a372014-09-23 09:50:21 -0700141 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700142 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800143 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700144 gp = GrCubicEffect::Make(color, SkMatrix::I(), et, *context->caps());
joshualittf5883a62016-01-13 07:47:38 -0800145 if (!gp) {
146 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000147 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000148 SkScalar x = SkScalarMul(col, w);
149 SkScalar y = SkScalarMul(row, h);
150 SkPoint controlPts[] = {
151 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
152 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
153 {x + baseControlPts[2].fX, y + baseControlPts[2].fY},
154 {x + baseControlPts[3].fX, y + baseControlPts[3].fY}
155 };
156 SkPoint chopped[10];
157 SkScalar klmEqs[9];
158 SkScalar klmSigns[3];
159 int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
160 chopped,
161 klmEqs,
162 klmSigns);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000163
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000164 SkPaint ctrlPtPaint;
165 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000166 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000167 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000168 }
169
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000170 SkPaint polyPaint;
171 polyPaint.setColor(0xffA0A0A0);
172 polyPaint.setStrokeWidth(0);
173 polyPaint.setStyle(SkPaint::kStroke_Style);
174 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000175
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000176 SkPaint choppedPtPaint;
177 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000178
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000179 for (int c = 0; c < cnt; ++c) {
180 SkPoint* pts = chopped + 3 * c;
181
182 for (int i = 0; i < 4; ++i) {
183 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
184 }
185
186 SkRect bounds;
187 bounds.set(pts, 4);
188
189 SkPaint boundsPaint;
190 boundsPaint.setColor(0xff808080);
191 boundsPaint.setStrokeWidth(0);
192 boundsPaint.setStyle(SkPaint::kStroke_Style);
193 canvas->drawRect(bounds, boundsPaint);
194
robertphillips28a838e2016-06-23 14:07:00 -0700195 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500196 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000197
Brian Salomonf8334782017-01-03 09:42:58 -0500198 std::unique_ptr<GrDrawOp> op =
Brian Salomon6b316e92016-12-16 09:35:49 -0500199 BezierCubicOrConicTestOp::Make(gp, bounds, color, klmEqs, klmSigns[c]);
joshualitt95964c62015-02-11 13:45:50 -0800200
Brian Salomon82f44312017-01-11 13:42:54 -0500201 renderTargetContext->priv().testingOnly_addDrawOp(
202 std::move(grPaint), GrAAType::kNone, std::move(op));
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000203 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000204 ++col;
205 if (numCols == col) {
206 col = 0;
207 ++row;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000208 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000209 }
210 }
211 }
212
213private:
214 typedef GM INHERITED;
215};
216
217//////////////////////////////////////////////////////////////////////////////
218
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000219/**
220 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
221 */
222class BezierConicEffects : public GM {
223public:
224 BezierConicEffects() {
225 this->setBGColor(0xFFFFFFFF);
226 }
227
228protected:
mtklein36352bf2015-03-25 18:17:31 -0700229 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000230 return SkString("bezier_conic_effects");
231 }
232
mtklein36352bf2015-03-25 18:17:31 -0700233 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700234 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000235 }
236
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000237
mtklein36352bf2015-03-25 18:17:31 -0700238 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400239 GrRenderTargetContext* renderTargetContext =
240 canvas->internal_private_accessTopLayerRenderTargetContext();
241 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -0700242 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000243 return;
244 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000245
robertphillips175dd9b2016-04-28 14:32:04 -0700246 GrContext* context = canvas->getGrContext();
247 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800248 return;
249 }
250
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000251 struct Vertex {
252 SkPoint fPosition;
253 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
254 };
255
mtkleindbfd7ab2016-09-01 11:24:54 -0700256 constexpr int kNumConics = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000257 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000258
259 // Mult by 3 for each edge effect type
260 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3)));
261 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols);
Brian Osman11052242016-10-27 14:47:55 -0400262 SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols;
263 SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000264 int row = 0;
265 int col = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700266 constexpr GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000267
268 for (int i = 0; i < kNumConics; ++i) {
269 SkPoint baseControlPts[] = {
270 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
271 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
272 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
273 };
274 SkScalar weight = rand.nextRangeF(0.f, 2.f);
joshualittb0a8a372014-09-23 09:50:21 -0700275 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700276 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800277 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700278 gp = GrConicEffect::Make(color, SkMatrix::I(), et,
279 *context->caps(), SkMatrix::I(), false);
joshualittf5883a62016-01-13 07:47:38 -0800280 if (!gp) {
281 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000282 }
283
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000284 SkScalar x = SkScalarMul(col, w);
285 SkScalar y = SkScalarMul(row, h);
286 SkPoint controlPts[] = {
287 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
288 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
289 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
290 };
291 SkConic dst[4];
292 SkScalar klmEqs[9];
293 int cnt = chop_conic(controlPts, dst, weight);
294 GrPathUtils::getConicKLM(controlPts, weight, klmEqs);
295
296 SkPaint ctrlPtPaint;
297 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
298 for (int i = 0; i < 3; ++i) {
299 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
300 }
301
302 SkPaint polyPaint;
303 polyPaint.setColor(0xffA0A0A0);
304 polyPaint.setStrokeWidth(0);
305 polyPaint.setStyle(SkPaint::kStroke_Style);
306 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
307
308 SkPaint choppedPtPaint;
309 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
310
311 for (int c = 0; c < cnt; ++c) {
312 SkPoint* pts = dst[c].fPts;
313 for (int i = 0; i < 3; ++i) {
314 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
315 }
316
317 SkRect bounds;
318 //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}};
319 //bounds.set(bPts, 2);
320 bounds.set(pts, 3);
321
322 SkPaint boundsPaint;
323 boundsPaint.setColor(0xff808080);
324 boundsPaint.setStrokeWidth(0);
325 boundsPaint.setStyle(SkPaint::kStroke_Style);
326 canvas->drawRect(bounds, boundsPaint);
327
robertphillips28a838e2016-06-23 14:07:00 -0700328 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500329 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000330
Brian Salomonf8334782017-01-03 09:42:58 -0500331 std::unique_ptr<GrDrawOp> op =
Brian Salomon6b316e92016-12-16 09:35:49 -0500332 BezierCubicOrConicTestOp::Make(gp, bounds, color, klmEqs, 1.f);
joshualitt95964c62015-02-11 13:45:50 -0800333
Brian Salomon82f44312017-01-11 13:42:54 -0500334 renderTargetContext->priv().testingOnly_addDrawOp(
335 std::move(grPaint), GrAAType::kNone, std::move(op));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000336 }
337 ++col;
338 if (numCols == col) {
339 col = 0;
340 ++row;
341 }
342 }
343 }
344 }
345
346private:
347 // Uses the max curvature function for quads to estimate
348 // where to chop the conic. If the max curvature is not
349 // found along the curve segment it will return 1 and
350 // dst[0] is the original conic. If it returns 2 the dst[0]
351 // and dst[1] are the two new conics.
352 int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
353 SkScalar t = SkFindQuadMaxCurvature(src);
354 if (t == 0) {
355 if (dst) {
356 dst[0].set(src, weight);
357 }
358 return 1;
359 } else {
360 if (dst) {
361 SkConic conic;
362 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700363 if (!conic.chopAt(t, dst)) {
364 dst[0].set(src, weight);
365 return 1;
366 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000367 }
368 return 2;
369 }
370 }
371
372 // Calls split_conic on the entire conic and then once more on each subsection.
373 // Most cases will result in either 1 conic (chop point is not within t range)
374 // or 3 points (split once and then one subsection is split again).
375 int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
376 SkConic dstTemp[2];
377 int conicCnt = split_conic(src, dstTemp, weight);
378 if (2 == conicCnt) {
379 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
380 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
381 } else {
382 dst[0] = dstTemp[0];
383 }
384 return conicCnt;
385 }
386
387 typedef GM INHERITED;
388};
389
390//////////////////////////////////////////////////////////////////////////////
joshualitt95964c62015-02-11 13:45:50 -0800391
Brian Salomon6b316e92016-12-16 09:35:49 -0500392class BezierQuadTestOp : public GrTestMeshDrawOp {
joshualitt95964c62015-02-11 13:45:50 -0800393public:
Brian Salomon25a88092016-12-01 09:36:50 -0500394 DEFINE_OP_CLASS_ID
Brian Salomon6b316e92016-12-16 09:35:49 -0500395 const char* name() const override { return "BezierQuadTestOp"; }
joshualitt95964c62015-02-11 13:45:50 -0800396
Brian Salomonf8334782017-01-03 09:42:58 -0500397 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds,
398 GrColor color, const GrPathUtils::QuadUVMatrix& devToUV) {
399 return std::unique_ptr<GrDrawOp>(new BezierQuadTestOp(gp, bounds, color, devToUV));
joshualitt95964c62015-02-11 13:45:50 -0800400 }
401
402private:
Brian Salomon6b316e92016-12-16 09:35:49 -0500403 BezierQuadTestOp(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color,
404 const GrPathUtils::QuadUVMatrix& devToUV)
405 : INHERITED(ClassID(), bounds, color)
406 , fDevToUV(devToUV)
407 , fGeometryProcessor(std::move(gp)) {}
joshualitt95964c62015-02-11 13:45:50 -0800408
409 struct Vertex {
410 SkPoint fPosition;
411 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
412 };
413
bsalomon342bfc22016-04-01 06:06:20 -0700414 void onPrepareDraws(Target* target) const override {
bsalomonb5238a72015-05-05 07:49:49 -0700415 QuadHelper helper;
bsalomon342bfc22016-04-01 06:06:20 -0700416 size_t vertexStride = fGeometryProcessor->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -0700417 SkASSERT(vertexStride == sizeof(Vertex));
bsalomon75398562015-08-17 12:55:38 -0700418 Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -0700419 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800420 return;
421 }
bsalomon342bfc22016-04-01 06:06:20 -0700422 const SkRect& bounds = this->bounds();
423 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
joshualitt95964c62015-02-11 13:45:50 -0800424 sizeof(Vertex));
joshualitt95964c62015-02-11 13:45:50 -0800425 fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
bungeman06ca8ec2016-06-09 08:01:03 -0700426 helper.recordDraw(target, fGeometryProcessor.get());
joshualitt95964c62015-02-11 13:45:50 -0800427 }
428
bungeman06ca8ec2016-06-09 08:01:03 -0700429 GrPathUtils::QuadUVMatrix fDevToUV;
430 sk_sp<GrGeometryProcessor> fGeometryProcessor;
joshualitt95964c62015-02-11 13:45:50 -0800431
mtkleindbfd7ab2016-09-01 11:24:54 -0700432 static constexpr int kVertsPerCubic = 4;
433 static constexpr int kIndicesPerCubic = 6;
joshualitt95964c62015-02-11 13:45:50 -0800434
Brian Salomon6b316e92016-12-16 09:35:49 -0500435 typedef GrTestMeshDrawOp INHERITED;
joshualitt95964c62015-02-11 13:45:50 -0800436};
437
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000438/**
439 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
440 */
441class BezierQuadEffects : public GM {
442public:
443 BezierQuadEffects() {
444 this->setBGColor(0xFFFFFFFF);
445 }
446
447protected:
mtklein36352bf2015-03-25 18:17:31 -0700448 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000449 return SkString("bezier_quad_effects");
450 }
451
mtklein36352bf2015-03-25 18:17:31 -0700452 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700453 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000454 }
455
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000456
mtklein36352bf2015-03-25 18:17:31 -0700457 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400458 GrRenderTargetContext* renderTargetContext =
459 canvas->internal_private_accessTopLayerRenderTargetContext();
460 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -0700461 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000462 return;
463 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000464
robertphillips175dd9b2016-04-28 14:32:04 -0700465 GrContext* context = canvas->getGrContext();
466 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800467 return;
468 }
469
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000470 struct Vertex {
471 SkPoint fPosition;
472 float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
473 };
474
mtkleindbfd7ab2016-09-01 11:24:54 -0700475 constexpr int kNumQuads = 5;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000476 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000477
478 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
479 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
Brian Osman11052242016-10-27 14:47:55 -0400480 SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols;
481 SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000482 int row = 0;
483 int col = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700484 constexpr GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000485
486 for (int i = 0; i < kNumQuads; ++i) {
487 SkPoint baseControlPts[] = {
488 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
489 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
490 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
491 };
joshualittb0a8a372014-09-23 09:50:21 -0700492 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700493 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800494 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700495 gp = GrQuadEffect::Make(color, SkMatrix::I(), et,
496 *context->caps(), SkMatrix::I(), false);
joshualittf5883a62016-01-13 07:47:38 -0800497 if (!gp) {
498 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000499 }
500
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000501 SkScalar x = SkScalarMul(col, w);
502 SkScalar y = SkScalarMul(row, h);
503 SkPoint controlPts[] = {
504 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
505 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
506 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
507 };
508 SkPoint chopped[5];
509 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
510
511 SkPaint ctrlPtPaint;
512 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
513 for (int i = 0; i < 3; ++i) {
514 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
515 }
516
517 SkPaint polyPaint;
518 polyPaint.setColor(0xffA0A0A0);
519 polyPaint.setStrokeWidth(0);
520 polyPaint.setStyle(SkPaint::kStroke_Style);
521 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
522
523 SkPaint choppedPtPaint;
524 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
525
526 for (int c = 0; c < cnt; ++c) {
527 SkPoint* pts = chopped + 2 * c;
528
529 for (int i = 0; i < 3; ++i) {
530 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
531 }
532
533 SkRect bounds;
534 bounds.set(pts, 3);
535
536 SkPaint boundsPaint;
537 boundsPaint.setColor(0xff808080);
538 boundsPaint.setStrokeWidth(0);
539 boundsPaint.setStyle(SkPaint::kStroke_Style);
540 canvas->drawRect(bounds, boundsPaint);
541
robertphillips28a838e2016-06-23 14:07:00 -0700542 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500543 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
joshualitt3f284d72015-02-11 11:34:58 -0800544
joshualitt95964c62015-02-11 13:45:50 -0800545 GrPathUtils::QuadUVMatrix DevToUV(pts);
546
Brian Salomonf8334782017-01-03 09:42:58 -0500547 std::unique_ptr<GrDrawOp> op =
548 BezierQuadTestOp::Make(gp, bounds, color, DevToUV);
joshualitt95964c62015-02-11 13:45:50 -0800549
Brian Salomon82f44312017-01-11 13:42:54 -0500550 renderTargetContext->priv().testingOnly_addDrawOp(
551 std::move(grPaint), GrAAType::kNone, std::move(op));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000552 }
553 ++col;
554 if (numCols == col) {
555 col = 0;
556 ++row;
557 }
558 }
559 }
560 }
561
562private:
563 typedef GM INHERITED;
564};
565
halcanary385fe4d2015-08-26 13:07:48 -0700566DEF_GM(return new BezierCubicEffects;)
567DEF_GM(return new BezierConicEffects;)
568DEF_GM(return new BezierQuadEffects;)
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000569}
570
571#endif