blob: c6d58e17f01178b90e47a7003793aef3f288d37f [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
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
mtkleindbfd7ab2016-09-01 11:24:54 -070077 static constexpr int kVertsPerCubic = 4;
78 static constexpr int kIndicesPerCubic = 6;
joshualitt95964c62015-02-11 13:45:50 -080079
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 {
Brian Osman11052242016-10-27 14:47:55 -0400102 GrRenderTargetContext* renderTargetContext =
103 canvas->internal_private_accessTopLayerRenderTargetContext();
104 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -0700105 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000106 return;
107 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000108
robertphillips175dd9b2016-04-28 14:32:04 -0700109 GrContext* context = canvas->getGrContext();
110 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800111 return;
112 }
113
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000114 struct Vertex {
115 SkPoint fPosition;
116 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
117 };
118
mtkleindbfd7ab2016-09-01 11:24:54 -0700119 constexpr int kNumCubics = 15;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000120 SkRandom rand;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000121
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000122 // Mult by 3 for each edge effect type
123 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3)));
124 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols);
Brian Osman11052242016-10-27 14:47:55 -0400125 SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols;
126 SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000127 int row = 0;
128 int col = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700129 constexpr GrColor color = 0xff000000;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000130
131 for (int i = 0; i < kNumCubics; ++i) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000132 SkPoint baseControlPts[] = {
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)},
136 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000137 };
joshualittb0a8a372014-09-23 09:50:21 -0700138 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700139 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800140 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700141 gp = GrCubicEffect::Make(color, SkMatrix::I(), et, *context->caps());
joshualittf5883a62016-01-13 07:47:38 -0800142 if (!gp) {
143 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000144 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000145 SkScalar x = SkScalarMul(col, w);
146 SkScalar y = SkScalarMul(row, h);
147 SkPoint controlPts[] = {
148 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
149 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
150 {x + baseControlPts[2].fX, y + baseControlPts[2].fY},
151 {x + baseControlPts[3].fX, y + baseControlPts[3].fY}
152 };
153 SkPoint chopped[10];
154 SkScalar klmEqs[9];
155 SkScalar klmSigns[3];
156 int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts,
157 chopped,
158 klmEqs,
159 klmSigns);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000160
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000161 SkPaint ctrlPtPaint;
162 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000163 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000164 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000165 }
166
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000167 SkPaint polyPaint;
168 polyPaint.setColor(0xffA0A0A0);
169 polyPaint.setStrokeWidth(0);
170 polyPaint.setStyle(SkPaint::kStroke_Style);
171 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000172
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000173 SkPaint choppedPtPaint;
174 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000175
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000176 for (int c = 0; c < cnt; ++c) {
177 SkPoint* pts = chopped + 3 * c;
178
179 for (int i = 0; i < 4; ++i) {
180 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
181 }
182
183 SkRect bounds;
184 bounds.set(pts, 4);
185
186 SkPaint boundsPaint;
187 boundsPaint.setColor(0xff808080);
188 boundsPaint.setStrokeWidth(0);
189 boundsPaint.setStyle(SkPaint::kStroke_Style);
190 canvas->drawRect(bounds, boundsPaint);
191
robertphillips28a838e2016-06-23 14:07:00 -0700192 GrPaint grPaint;
193 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000194
bsalomonabd30f52015-08-13 13:34:48 -0700195 SkAutoTUnref<GrDrawBatch> batch(
bsalomon342bfc22016-04-01 06:06:20 -0700196 new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, klmSigns[c]));
joshualitt95964c62015-02-11 13:45:50 -0800197
Brian Osman11052242016-10-27 14:47:55 -0400198 renderTargetContext->renderTargetContextPriv().testingOnly_drawBatch(grPaint,
199 batch);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000200 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000201 ++col;
202 if (numCols == col) {
203 col = 0;
204 ++row;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000205 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000206 }
207 }
208 }
209
210private:
211 typedef GM INHERITED;
212};
213
214//////////////////////////////////////////////////////////////////////////////
215
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000216/**
217 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
218 */
219class BezierConicEffects : public GM {
220public:
221 BezierConicEffects() {
222 this->setBGColor(0xFFFFFFFF);
223 }
224
225protected:
mtklein36352bf2015-03-25 18:17:31 -0700226 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000227 return SkString("bezier_conic_effects");
228 }
229
mtklein36352bf2015-03-25 18:17:31 -0700230 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700231 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000232 }
233
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000234
mtklein36352bf2015-03-25 18:17:31 -0700235 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400236 GrRenderTargetContext* renderTargetContext =
237 canvas->internal_private_accessTopLayerRenderTargetContext();
238 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -0700239 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000240 return;
241 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000242
robertphillips175dd9b2016-04-28 14:32:04 -0700243 GrContext* context = canvas->getGrContext();
244 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800245 return;
246 }
247
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000248 struct Vertex {
249 SkPoint fPosition;
250 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
251 };
252
mtkleindbfd7ab2016-09-01 11:24:54 -0700253 constexpr int kNumConics = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000254 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000255
256 // Mult by 3 for each edge effect type
257 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3)));
258 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols);
Brian Osman11052242016-10-27 14:47:55 -0400259 SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols;
260 SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000261 int row = 0;
262 int col = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700263 constexpr GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000264
265 for (int i = 0; i < kNumConics; ++i) {
266 SkPoint baseControlPts[] = {
267 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
268 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
269 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
270 };
271 SkScalar weight = rand.nextRangeF(0.f, 2.f);
joshualittb0a8a372014-09-23 09:50:21 -0700272 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700273 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800274 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700275 gp = GrConicEffect::Make(color, SkMatrix::I(), et,
276 *context->caps(), SkMatrix::I(), false);
joshualittf5883a62016-01-13 07:47:38 -0800277 if (!gp) {
278 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000279 }
280
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000281 SkScalar x = SkScalarMul(col, w);
282 SkScalar y = SkScalarMul(row, h);
283 SkPoint controlPts[] = {
284 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
285 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
286 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
287 };
288 SkConic dst[4];
289 SkScalar klmEqs[9];
290 int cnt = chop_conic(controlPts, dst, weight);
291 GrPathUtils::getConicKLM(controlPts, weight, klmEqs);
292
293 SkPaint ctrlPtPaint;
294 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
295 for (int i = 0; i < 3; ++i) {
296 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
297 }
298
299 SkPaint polyPaint;
300 polyPaint.setColor(0xffA0A0A0);
301 polyPaint.setStrokeWidth(0);
302 polyPaint.setStyle(SkPaint::kStroke_Style);
303 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
304
305 SkPaint choppedPtPaint;
306 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
307
308 for (int c = 0; c < cnt; ++c) {
309 SkPoint* pts = dst[c].fPts;
310 for (int i = 0; i < 3; ++i) {
311 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
312 }
313
314 SkRect bounds;
315 //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}};
316 //bounds.set(bPts, 2);
317 bounds.set(pts, 3);
318
319 SkPaint boundsPaint;
320 boundsPaint.setColor(0xff808080);
321 boundsPaint.setStrokeWidth(0);
322 boundsPaint.setStyle(SkPaint::kStroke_Style);
323 canvas->drawRect(bounds, boundsPaint);
324
robertphillips28a838e2016-06-23 14:07:00 -0700325 GrPaint grPaint;
326 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000327
bsalomonabd30f52015-08-13 13:34:48 -0700328 SkAutoTUnref<GrDrawBatch> batch(
bsalomon342bfc22016-04-01 06:06:20 -0700329 new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, 1.f));
joshualitt95964c62015-02-11 13:45:50 -0800330
Brian Osman11052242016-10-27 14:47:55 -0400331 renderTargetContext->renderTargetContextPriv().testingOnly_drawBatch(grPaint,
332 batch);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000333 }
334 ++col;
335 if (numCols == col) {
336 col = 0;
337 ++row;
338 }
339 }
340 }
341 }
342
343private:
344 // Uses the max curvature function for quads to estimate
345 // where to chop the conic. If the max curvature is not
346 // found along the curve segment it will return 1 and
347 // dst[0] is the original conic. If it returns 2 the dst[0]
348 // and dst[1] are the two new conics.
349 int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
350 SkScalar t = SkFindQuadMaxCurvature(src);
351 if (t == 0) {
352 if (dst) {
353 dst[0].set(src, weight);
354 }
355 return 1;
356 } else {
357 if (dst) {
358 SkConic conic;
359 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700360 if (!conic.chopAt(t, dst)) {
361 dst[0].set(src, weight);
362 return 1;
363 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000364 }
365 return 2;
366 }
367 }
368
369 // Calls split_conic on the entire conic and then once more on each subsection.
370 // Most cases will result in either 1 conic (chop point is not within t range)
371 // or 3 points (split once and then one subsection is split again).
372 int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
373 SkConic dstTemp[2];
374 int conicCnt = split_conic(src, dstTemp, weight);
375 if (2 == conicCnt) {
376 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
377 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
378 } else {
379 dst[0] = dstTemp[0];
380 }
381 return conicCnt;
382 }
383
384 typedef GM INHERITED;
385};
386
387//////////////////////////////////////////////////////////////////////////////
joshualitt95964c62015-02-11 13:45:50 -0800388
389class BezierQuadTestBatch : public GrTestBatch {
390public:
reed1b55a962015-09-17 20:16:13 -0700391 DEFINE_BATCH_CLASS_ID
mtklein36352bf2015-03-25 18:17:31 -0700392 const char* name() const override { return "BezierQuadTestBatch"; }
joshualitt95964c62015-02-11 13:45:50 -0800393
bungeman06ca8ec2016-06-09 08:01:03 -0700394 BezierQuadTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color,
bsalomon342bfc22016-04-01 06:06:20 -0700395 const GrPathUtils::QuadUVMatrix& devToUV)
396 : INHERITED(ClassID(), bounds, color)
397 , fDevToUV(devToUV)
bungeman06ca8ec2016-06-09 08:01:03 -0700398 , fGeometryProcessor(std::move(gp)) {
joshualitt95964c62015-02-11 13:45:50 -0800399 }
400
401private:
joshualitt95964c62015-02-11 13:45:50 -0800402
403 struct Vertex {
404 SkPoint fPosition;
405 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
406 };
407
bsalomon342bfc22016-04-01 06:06:20 -0700408 void onPrepareDraws(Target* target) const override {
bsalomonb5238a72015-05-05 07:49:49 -0700409 QuadHelper helper;
bsalomon342bfc22016-04-01 06:06:20 -0700410 size_t vertexStride = fGeometryProcessor->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -0700411 SkASSERT(vertexStride == sizeof(Vertex));
bsalomon75398562015-08-17 12:55:38 -0700412 Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -0700413 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800414 return;
415 }
bsalomon342bfc22016-04-01 06:06:20 -0700416 const SkRect& bounds = this->bounds();
417 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
joshualitt95964c62015-02-11 13:45:50 -0800418 sizeof(Vertex));
joshualitt95964c62015-02-11 13:45:50 -0800419 fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
bungeman06ca8ec2016-06-09 08:01:03 -0700420 helper.recordDraw(target, fGeometryProcessor.get());
joshualitt95964c62015-02-11 13:45:50 -0800421 }
422
bungeman06ca8ec2016-06-09 08:01:03 -0700423 GrPathUtils::QuadUVMatrix fDevToUV;
424 sk_sp<GrGeometryProcessor> fGeometryProcessor;
joshualitt95964c62015-02-11 13:45:50 -0800425
mtkleindbfd7ab2016-09-01 11:24:54 -0700426 static constexpr int kVertsPerCubic = 4;
427 static constexpr int kIndicesPerCubic = 6;
joshualitt95964c62015-02-11 13:45:50 -0800428
429 typedef GrTestBatch INHERITED;
430};
431
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000432/**
433 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
434 */
435class BezierQuadEffects : public GM {
436public:
437 BezierQuadEffects() {
438 this->setBGColor(0xFFFFFFFF);
439 }
440
441protected:
mtklein36352bf2015-03-25 18:17:31 -0700442 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000443 return SkString("bezier_quad_effects");
444 }
445
mtklein36352bf2015-03-25 18:17:31 -0700446 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700447 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000448 }
449
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000450
mtklein36352bf2015-03-25 18:17:31 -0700451 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -0400452 GrRenderTargetContext* renderTargetContext =
453 canvas->internal_private_accessTopLayerRenderTargetContext();
454 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -0700455 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000456 return;
457 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000458
robertphillips175dd9b2016-04-28 14:32:04 -0700459 GrContext* context = canvas->getGrContext();
460 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800461 return;
462 }
463
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000464 struct Vertex {
465 SkPoint fPosition;
466 float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
467 };
468
mtkleindbfd7ab2016-09-01 11:24:54 -0700469 constexpr int kNumQuads = 5;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000470 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000471
472 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
473 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
Brian Osman11052242016-10-27 14:47:55 -0400474 SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols;
475 SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000476 int row = 0;
477 int col = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -0700478 constexpr GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000479
480 for (int i = 0; i < kNumQuads; ++i) {
481 SkPoint baseControlPts[] = {
482 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
483 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
484 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
485 };
joshualittb0a8a372014-09-23 09:50:21 -0700486 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
bungeman06ca8ec2016-06-09 08:01:03 -0700487 sk_sp<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800488 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
bungeman06ca8ec2016-06-09 08:01:03 -0700489 gp = GrQuadEffect::Make(color, SkMatrix::I(), et,
490 *context->caps(), SkMatrix::I(), false);
joshualittf5883a62016-01-13 07:47:38 -0800491 if (!gp) {
492 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000493 }
494
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000495 SkScalar x = SkScalarMul(col, w);
496 SkScalar y = SkScalarMul(row, h);
497 SkPoint controlPts[] = {
498 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
499 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
500 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
501 };
502 SkPoint chopped[5];
503 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
504
505 SkPaint ctrlPtPaint;
506 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
507 for (int i = 0; i < 3; ++i) {
508 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
509 }
510
511 SkPaint polyPaint;
512 polyPaint.setColor(0xffA0A0A0);
513 polyPaint.setStrokeWidth(0);
514 polyPaint.setStyle(SkPaint::kStroke_Style);
515 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
516
517 SkPaint choppedPtPaint;
518 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
519
520 for (int c = 0; c < cnt; ++c) {
521 SkPoint* pts = chopped + 2 * c;
522
523 for (int i = 0; i < 3; ++i) {
524 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
525 }
526
527 SkRect bounds;
528 bounds.set(pts, 3);
529
530 SkPaint boundsPaint;
531 boundsPaint.setColor(0xff808080);
532 boundsPaint.setStrokeWidth(0);
533 boundsPaint.setStyle(SkPaint::kStroke_Style);
534 canvas->drawRect(bounds, boundsPaint);
535
robertphillips28a838e2016-06-23 14:07:00 -0700536 GrPaint grPaint;
537 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
joshualitt3f284d72015-02-11 11:34:58 -0800538
joshualitt95964c62015-02-11 13:45:50 -0800539 GrPathUtils::QuadUVMatrix DevToUV(pts);
540
bsalomon342bfc22016-04-01 06:06:20 -0700541 SkAutoTUnref<GrDrawBatch> batch(
542 new BezierQuadTestBatch(gp, bounds, color, DevToUV));
joshualitt95964c62015-02-11 13:45:50 -0800543
Brian Osman11052242016-10-27 14:47:55 -0400544 renderTargetContext->renderTargetContextPriv().testingOnly_drawBatch(grPaint,
545 batch);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000546 }
547 ++col;
548 if (numCols == col) {
549 col = 0;
550 ++row;
551 }
552 }
553 }
554 }
555
556private:
557 typedef GM INHERITED;
558};
559
halcanary385fe4d2015-08-26 13:07:48 -0700560DEF_GM(return new BezierCubicEffects;)
561DEF_GM(return new BezierConicEffects;)
562DEF_GM(return new BezierQuadEffects;)
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000563}
564
565#endif