blob: 0c568d09f5b3ecf6201f2a6580bb689bb29a88c8 [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
robertphillips391395d2016-03-02 09:26:36 -080014#include "GrDrawContextPriv.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
joshualitt2771b562015-08-07 12:46:26 -070021#include "batches/GrTestBatch.h"
22
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
31class BezierCubicOrConicTestBatch : public GrTestBatch {
32public:
reed1b55a962015-09-17 20:16:13 -070033 DEFINE_BATCH_CLASS_ID
joshualitt95964c62015-02-11 13:45:50 -080034
mtklein36352bf2015-03-25 18:17:31 -070035 const char* name() const override { return "BezierCubicOrConicTestBatch"; }
joshualitt95964c62015-02-11 13:45:50 -080036
bungeman06ca8ec2016-06-09 08:01:03 -070037 BezierCubicOrConicTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds,
bsalomon342bfc22016-04-01 06:06:20 -070038 GrColor color, const SkScalar klmEqs[9], SkScalar sign)
39 : INHERITED(ClassID(), bounds, color)
bungeman06ca8ec2016-06-09 08:01:03 -070040 , fGeometryProcessor(std::move(gp)) {
joshualitt95964c62015-02-11 13:45:50 -080041 for (int i = 0; i < 9; i++) {
42 fKlmEqs[i] = klmEqs[i];
43 }
joshualitt95964c62015-02-11 13:45:50 -080044 fSign = sign;
45 }
46
bsalomon342bfc22016-04-01 06:06:20 -070047private:
48
joshualitt95964c62015-02-11 13:45:50 -080049 struct Vertex {
50 SkPoint fPosition;
51 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
52 };
53
bsalomon342bfc22016-04-01 06:06:20 -070054 void onPrepareDraws(Target* target) const override {
bsalomonb5238a72015-05-05 07:49:49 -070055 QuadHelper helper;
bsalomon342bfc22016-04-01 06:06:20 -070056 size_t vertexStride = fGeometryProcessor->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -070057 SkASSERT(vertexStride == sizeof(Vertex));
bsalomon75398562015-08-17 12:55:38 -070058 Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -070059 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -080060 return;
61 }
bsalomon342bfc22016-04-01 06:06:20 -070062 const SkRect& bounds = this->bounds();
63 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
joshualitt95964c62015-02-11 13:45:50 -080064 sizeof(Vertex));
65 for (int v = 0; v < 4; ++v) {
66 verts[v].fKLM[0] = eval_line(verts[v].fPosition, fKlmEqs + 0, fSign);
67 verts[v].fKLM[1] = eval_line(verts[v].fPosition, fKlmEqs + 3, fSign);
68 verts[v].fKLM[2] = eval_line(verts[v].fPosition, fKlmEqs + 6, 1.f);
69 }
bungeman06ca8ec2016-06-09 08:01:03 -070070 helper.recordDraw(target, fGeometryProcessor.get());
joshualitt95964c62015-02-11 13:45:50 -080071 }
72
bungeman06ca8ec2016-06-09 08:01:03 -070073 SkScalar fKlmEqs[9];
74 SkScalar fSign;
75 sk_sp<GrGeometryProcessor> fGeometryProcessor;
joshualitt95964c62015-02-11 13:45:50 -080076
77 static const int kVertsPerCubic = 4;
78 static const int kIndicesPerCubic = 6;
79
80 typedef GrTestBatch INHERITED;
81};
82
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000083/**
84 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
85 */
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000086class BezierCubicEffects : public GM {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000087public:
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000088 BezierCubicEffects() {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000089 this->setBGColor(0xFFFFFFFF);
90 }
91
92protected:
mtklein36352bf2015-03-25 18:17:31 -070093 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +000094 return SkString("bezier_cubic_effects");
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000095 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070098 return SkISize::Make(800, 800);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000099 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700102 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
103 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -0700104 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000105 return;
106 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000107
robertphillips175dd9b2016-04-28 14:32:04 -0700108 GrContext* context = canvas->getGrContext();
109 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800110 return;
111 }
112
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000113 struct Vertex {
114 SkPoint fPosition;
115 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
116 };
117
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000118 static const int kNumCubics = 15;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000119 SkRandom rand;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000120
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000121 // Mult by 3 for each edge effect type
122 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3)));
123 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols);
robertphillips175dd9b2016-04-28 14:32:04 -0700124 SkScalar w = SkIntToScalar(drawContext->width()) / numCols;
125 SkScalar h = SkIntToScalar(drawContext->height()) / numRows;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000126 int row = 0;
127 int col = 0;
joshualitt88c23fc2015-05-13 14:18:07 -0700128 static const GrColor color = 0xff000000;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000129
130 for (int i = 0; i < kNumCubics; ++i) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000131 SkPoint baseControlPts[] = {
132 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
133 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
134 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
135 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000136 };
joshualittb0a8a372014-09-23 09:50:21 -0700137 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700138 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800139 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700140 gp = GrCubicEffect::Make(color, SkMatrix::I(), et, *context->caps());
joshualittf5883a62016-01-13 07:47:38 -0800141 if (!gp) {
142 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000143 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000144 SkScalar x = SkScalarMul(col, w);
145 SkScalar y = SkScalarMul(row, h);
146 SkPoint controlPts[] = {
147 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
148 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
149 {x + baseControlPts[2].fX, y + baseControlPts[2].fY},
150 {x + baseControlPts[3].fX, y + baseControlPts[3].fY}
151 };
152 SkPoint chopped[10];
153 SkScalar klmEqs[9];
154 SkScalar klmSigns[3];
155 int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
156 chopped,
157 klmEqs,
158 klmSigns);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000159
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000160 SkPaint ctrlPtPaint;
161 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000162 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000163 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000164 }
165
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000166 SkPaint polyPaint;
167 polyPaint.setColor(0xffA0A0A0);
168 polyPaint.setStrokeWidth(0);
169 polyPaint.setStyle(SkPaint::kStroke_Style);
170 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000171
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000172 SkPaint choppedPtPaint;
173 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000174
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000175 for (int c = 0; c < cnt; ++c) {
176 SkPoint* pts = chopped + 3 * c;
177
178 for (int i = 0; i < 4; ++i) {
179 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
180 }
181
182 SkRect bounds;
183 bounds.set(pts, 4);
184
185 SkPaint boundsPaint;
186 boundsPaint.setColor(0xff808080);
187 boundsPaint.setStrokeWidth(0);
188 boundsPaint.setStyle(SkPaint::kStroke_Style);
189 canvas->drawRect(bounds, boundsPaint);
190
robertphillips28a838e2016-06-23 14:07:00 -0700191 GrPaint grPaint;
192 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000193
bsalomonabd30f52015-08-13 13:34:48 -0700194 SkAutoTUnref<GrDrawBatch> batch(
bsalomon342bfc22016-04-01 06:06:20 -0700195 new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, klmSigns[c]));
joshualitt95964c62015-02-11 13:45:50 -0800196
robertphillips28a838e2016-06-23 14:07:00 -0700197 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000198 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000199 ++col;
200 if (numCols == col) {
201 col = 0;
202 ++row;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000203 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000204 }
205 }
206 }
207
208private:
209 typedef GM INHERITED;
210};
211
212//////////////////////////////////////////////////////////////////////////////
213
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000214/**
215 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
216 */
217class BezierConicEffects : public GM {
218public:
219 BezierConicEffects() {
220 this->setBGColor(0xFFFFFFFF);
221 }
222
223protected:
mtklein36352bf2015-03-25 18:17:31 -0700224 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000225 return SkString("bezier_conic_effects");
226 }
227
mtklein36352bf2015-03-25 18:17:31 -0700228 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700229 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000230 }
231
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000232
mtklein36352bf2015-03-25 18:17:31 -0700233 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700234 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
235 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -0700236 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000237 return;
238 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000239
robertphillips175dd9b2016-04-28 14:32:04 -0700240 GrContext* context = canvas->getGrContext();
241 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800242 return;
243 }
244
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000245 struct Vertex {
246 SkPoint fPosition;
247 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
248 };
249
250 static const int kNumConics = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000251 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000252
253 // Mult by 3 for each edge effect type
254 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3)));
255 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols);
robertphillips175dd9b2016-04-28 14:32:04 -0700256 SkScalar w = SkIntToScalar(drawContext->width()) / numCols;
257 SkScalar h = SkIntToScalar(drawContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000258 int row = 0;
259 int col = 0;
joshualitt88c23fc2015-05-13 14:18:07 -0700260 static const GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000261
262 for (int i = 0; i < kNumConics; ++i) {
263 SkPoint baseControlPts[] = {
264 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
265 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
266 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
267 };
268 SkScalar weight = rand.nextRangeF(0.f, 2.f);
joshualittb0a8a372014-09-23 09:50:21 -0700269 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700270 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800271 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700272 gp = GrConicEffect::Make(color, SkMatrix::I(), et,
273 *context->caps(), SkMatrix::I(), false);
joshualittf5883a62016-01-13 07:47:38 -0800274 if (!gp) {
275 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000276 }
277
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000278 SkScalar x = SkScalarMul(col, w);
279 SkScalar y = SkScalarMul(row, h);
280 SkPoint controlPts[] = {
281 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
282 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
283 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
284 };
285 SkConic dst[4];
286 SkScalar klmEqs[9];
287 int cnt = chop_conic(controlPts, dst, weight);
288 GrPathUtils::getConicKLM(controlPts, weight, klmEqs);
289
290 SkPaint ctrlPtPaint;
291 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
292 for (int i = 0; i < 3; ++i) {
293 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
294 }
295
296 SkPaint polyPaint;
297 polyPaint.setColor(0xffA0A0A0);
298 polyPaint.setStrokeWidth(0);
299 polyPaint.setStyle(SkPaint::kStroke_Style);
300 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
301
302 SkPaint choppedPtPaint;
303 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
304
305 for (int c = 0; c < cnt; ++c) {
306 SkPoint* pts = dst[c].fPts;
307 for (int i = 0; i < 3; ++i) {
308 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
309 }
310
311 SkRect bounds;
312 //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}};
313 //bounds.set(bPts, 2);
314 bounds.set(pts, 3);
315
316 SkPaint boundsPaint;
317 boundsPaint.setColor(0xff808080);
318 boundsPaint.setStrokeWidth(0);
319 boundsPaint.setStyle(SkPaint::kStroke_Style);
320 canvas->drawRect(bounds, boundsPaint);
321
robertphillips28a838e2016-06-23 14:07:00 -0700322 GrPaint grPaint;
323 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000324
bsalomonabd30f52015-08-13 13:34:48 -0700325 SkAutoTUnref<GrDrawBatch> batch(
bsalomon342bfc22016-04-01 06:06:20 -0700326 new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, 1.f));
joshualitt95964c62015-02-11 13:45:50 -0800327
robertphillips28a838e2016-06-23 14:07:00 -0700328 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000329 }
330 ++col;
331 if (numCols == col) {
332 col = 0;
333 ++row;
334 }
335 }
336 }
337 }
338
339private:
340 // Uses the max curvature function for quads to estimate
341 // where to chop the conic. If the max curvature is not
342 // found along the curve segment it will return 1 and
343 // dst[0] is the original conic. If it returns 2 the dst[0]
344 // and dst[1] are the two new conics.
345 int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
346 SkScalar t = SkFindQuadMaxCurvature(src);
347 if (t == 0) {
348 if (dst) {
349 dst[0].set(src, weight);
350 }
351 return 1;
352 } else {
353 if (dst) {
354 SkConic conic;
355 conic.set(src, weight);
356 conic.chopAt(t, dst);
357 }
358 return 2;
359 }
360 }
361
362 // Calls split_conic on the entire conic and then once more on each subsection.
363 // Most cases will result in either 1 conic (chop point is not within t range)
364 // or 3 points (split once and then one subsection is split again).
365 int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
366 SkConic dstTemp[2];
367 int conicCnt = split_conic(src, dstTemp, weight);
368 if (2 == conicCnt) {
369 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
370 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
371 } else {
372 dst[0] = dstTemp[0];
373 }
374 return conicCnt;
375 }
376
377 typedef GM INHERITED;
378};
379
380//////////////////////////////////////////////////////////////////////////////
joshualitt95964c62015-02-11 13:45:50 -0800381
382class BezierQuadTestBatch : public GrTestBatch {
383public:
reed1b55a962015-09-17 20:16:13 -0700384 DEFINE_BATCH_CLASS_ID
mtklein36352bf2015-03-25 18:17:31 -0700385 const char* name() const override { return "BezierQuadTestBatch"; }
joshualitt95964c62015-02-11 13:45:50 -0800386
bungeman06ca8ec2016-06-09 08:01:03 -0700387 BezierQuadTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color,
bsalomon342bfc22016-04-01 06:06:20 -0700388 const GrPathUtils::QuadUVMatrix& devToUV)
389 : INHERITED(ClassID(), bounds, color)
390 , fDevToUV(devToUV)
bungeman06ca8ec2016-06-09 08:01:03 -0700391 , fGeometryProcessor(std::move(gp)) {
joshualitt95964c62015-02-11 13:45:50 -0800392 }
393
394private:
joshualitt95964c62015-02-11 13:45:50 -0800395
396 struct Vertex {
397 SkPoint fPosition;
398 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
399 };
400
bsalomon342bfc22016-04-01 06:06:20 -0700401 void onPrepareDraws(Target* target) const override {
bsalomonb5238a72015-05-05 07:49:49 -0700402 QuadHelper helper;
bsalomon342bfc22016-04-01 06:06:20 -0700403 size_t vertexStride = fGeometryProcessor->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -0700404 SkASSERT(vertexStride == sizeof(Vertex));
bsalomon75398562015-08-17 12:55:38 -0700405 Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -0700406 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800407 return;
408 }
bsalomon342bfc22016-04-01 06:06:20 -0700409 const SkRect& bounds = this->bounds();
410 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
joshualitt95964c62015-02-11 13:45:50 -0800411 sizeof(Vertex));
joshualitt95964c62015-02-11 13:45:50 -0800412 fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
bungeman06ca8ec2016-06-09 08:01:03 -0700413 helper.recordDraw(target, fGeometryProcessor.get());
joshualitt95964c62015-02-11 13:45:50 -0800414 }
415
bungeman06ca8ec2016-06-09 08:01:03 -0700416 GrPathUtils::QuadUVMatrix fDevToUV;
417 sk_sp<GrGeometryProcessor> fGeometryProcessor;
joshualitt95964c62015-02-11 13:45:50 -0800418
419 static const int kVertsPerCubic = 4;
420 static const int kIndicesPerCubic = 6;
421
422 typedef GrTestBatch INHERITED;
423};
424
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000425/**
426 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
427 */
428class BezierQuadEffects : public GM {
429public:
430 BezierQuadEffects() {
431 this->setBGColor(0xFFFFFFFF);
432 }
433
434protected:
mtklein36352bf2015-03-25 18:17:31 -0700435 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000436 return SkString("bezier_quad_effects");
437 }
438
mtklein36352bf2015-03-25 18:17:31 -0700439 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700440 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000441 }
442
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000443
mtklein36352bf2015-03-25 18:17:31 -0700444 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700445 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
446 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -0700447 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000448 return;
449 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000450
robertphillips175dd9b2016-04-28 14:32:04 -0700451 GrContext* context = canvas->getGrContext();
452 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800453 return;
454 }
455
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000456 struct Vertex {
457 SkPoint fPosition;
458 float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
459 };
460
461 static const int kNumQuads = 5;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000462 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000463
464 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
465 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
robertphillips175dd9b2016-04-28 14:32:04 -0700466 SkScalar w = SkIntToScalar(drawContext->width()) / numCols;
467 SkScalar h = SkIntToScalar(drawContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000468 int row = 0;
469 int col = 0;
joshualitt88c23fc2015-05-13 14:18:07 -0700470 static const GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000471
472 for (int i = 0; i < kNumQuads; ++i) {
473 SkPoint baseControlPts[] = {
474 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
475 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
476 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
477 };
joshualittb0a8a372014-09-23 09:50:21 -0700478 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700479 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800480 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700481 gp = GrQuadEffect::Make(color, SkMatrix::I(), et,
482 *context->caps(), SkMatrix::I(), false);
joshualittf5883a62016-01-13 07:47:38 -0800483 if (!gp) {
484 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000485 }
486
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000487 SkScalar x = SkScalarMul(col, w);
488 SkScalar y = SkScalarMul(row, h);
489 SkPoint controlPts[] = {
490 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
491 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
492 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
493 };
494 SkPoint chopped[5];
495 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
496
497 SkPaint ctrlPtPaint;
498 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
499 for (int i = 0; i < 3; ++i) {
500 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
501 }
502
503 SkPaint polyPaint;
504 polyPaint.setColor(0xffA0A0A0);
505 polyPaint.setStrokeWidth(0);
506 polyPaint.setStyle(SkPaint::kStroke_Style);
507 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
508
509 SkPaint choppedPtPaint;
510 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
511
512 for (int c = 0; c < cnt; ++c) {
513 SkPoint* pts = chopped + 2 * c;
514
515 for (int i = 0; i < 3; ++i) {
516 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
517 }
518
519 SkRect bounds;
520 bounds.set(pts, 3);
521
522 SkPaint boundsPaint;
523 boundsPaint.setColor(0xff808080);
524 boundsPaint.setStrokeWidth(0);
525 boundsPaint.setStyle(SkPaint::kStroke_Style);
526 canvas->drawRect(bounds, boundsPaint);
527
robertphillips28a838e2016-06-23 14:07:00 -0700528 GrPaint grPaint;
529 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
joshualitt3f284d72015-02-11 11:34:58 -0800530
joshualitt95964c62015-02-11 13:45:50 -0800531 GrPathUtils::QuadUVMatrix DevToUV(pts);
532
bsalomon342bfc22016-04-01 06:06:20 -0700533 SkAutoTUnref<GrDrawBatch> batch(
534 new BezierQuadTestBatch(gp, bounds, color, DevToUV));
joshualitt95964c62015-02-11 13:45:50 -0800535
robertphillips28a838e2016-06-23 14:07:00 -0700536 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000537 }
538 ++col;
539 if (numCols == col) {
540 col = 0;
541 ++row;
542 }
543 }
544 }
545 }
546
547private:
548 typedef GM INHERITED;
549};
550
halcanary385fe4d2015-08-26 13:07:48 -0700551DEF_GM(return new BezierCubicEffects;)
552DEF_GM(return new BezierConicEffects;)
553DEF_GM(return new BezierQuadEffects;)
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000554}
555
556#endif