blob: cfb84a5bcca3aedaa2efe022e2a5600e85363134 [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
bsalomon342bfc22016-04-01 06:06:20 -070037 BezierCubicOrConicTestBatch(const GrGeometryProcessor* gp, const SkRect& bounds,
38 GrColor color, const SkScalar klmEqs[9], SkScalar sign)
39 : INHERITED(ClassID(), bounds, color)
40 , fGeometryProcessor(SkRef(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 }
bsalomon342bfc22016-04-01 06:06:20 -070070 helper.recordDraw(target, fGeometryProcessor);
joshualitt95964c62015-02-11 13:45:50 -080071 }
72
bsalomon342bfc22016-04-01 06:06:20 -070073 SkScalar fKlmEqs[9];
74 SkScalar fSign;
75 SkAutoTUnref<const 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) {
138 SkAutoTUnref<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800139 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
140 gp.reset(GrCubicEffect::Create(color, SkMatrix::I(), et,
141 *context->caps()));
142 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
egdaniel8dd688b2015-01-22 10:16:09 -0800192 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800193 pipelineBuilder.setXPFactory(
194 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
robertphillips175dd9b2016-04-28 14:32:04 -0700195 pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000196
bsalomonabd30f52015-08-13 13:34:48 -0700197 SkAutoTUnref<GrDrawBatch> batch(
bsalomon342bfc22016-04-01 06:06:20 -0700198 new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, klmSigns[c]));
joshualitt95964c62015-02-11 13:45:50 -0800199
robertphillips391395d2016-03-02 09:26:36 -0800200 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000201 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000202 ++col;
203 if (numCols == col) {
204 col = 0;
205 ++row;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000206 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000207 }
208 }
209 }
210
211private:
212 typedef GM INHERITED;
213};
214
215//////////////////////////////////////////////////////////////////////////////
216
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000217/**
218 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
219 */
220class BezierConicEffects : public GM {
221public:
222 BezierConicEffects() {
223 this->setBGColor(0xFFFFFFFF);
224 }
225
226protected:
mtklein36352bf2015-03-25 18:17:31 -0700227 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000228 return SkString("bezier_conic_effects");
229 }
230
mtklein36352bf2015-03-25 18:17:31 -0700231 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700232 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000233 }
234
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000235
mtklein36352bf2015-03-25 18:17:31 -0700236 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700237 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
238 if (!drawContext) {
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
253 static const 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);
robertphillips175dd9b2016-04-28 14:32:04 -0700259 SkScalar w = SkIntToScalar(drawContext->width()) / numCols;
260 SkScalar h = SkIntToScalar(drawContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000261 int row = 0;
262 int col = 0;
joshualitt88c23fc2015-05-13 14:18:07 -0700263 static const 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) {
273 SkAutoTUnref<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800274 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
275 gp.reset(GrConicEffect::Create(color, SkMatrix::I(), et,
276 *context->caps(), SkMatrix::I(), false));
277 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
egdaniel8dd688b2015-01-22 10:16:09 -0800325 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800326 pipelineBuilder.setXPFactory(
327 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
robertphillips175dd9b2016-04-28 14:32:04 -0700328 pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000329
bsalomonabd30f52015-08-13 13:34:48 -0700330 SkAutoTUnref<GrDrawBatch> batch(
bsalomon342bfc22016-04-01 06:06:20 -0700331 new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, 1.f));
joshualitt95964c62015-02-11 13:45:50 -0800332
robertphillips391395d2016-03-02 09:26:36 -0800333 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000334 }
335 ++col;
336 if (numCols == col) {
337 col = 0;
338 ++row;
339 }
340 }
341 }
342 }
343
344private:
345 // Uses the max curvature function for quads to estimate
346 // where to chop the conic. If the max curvature is not
347 // found along the curve segment it will return 1 and
348 // dst[0] is the original conic. If it returns 2 the dst[0]
349 // and dst[1] are the two new conics.
350 int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
351 SkScalar t = SkFindQuadMaxCurvature(src);
352 if (t == 0) {
353 if (dst) {
354 dst[0].set(src, weight);
355 }
356 return 1;
357 } else {
358 if (dst) {
359 SkConic conic;
360 conic.set(src, weight);
361 conic.chopAt(t, dst);
362 }
363 return 2;
364 }
365 }
366
367 // Calls split_conic on the entire conic and then once more on each subsection.
368 // Most cases will result in either 1 conic (chop point is not within t range)
369 // or 3 points (split once and then one subsection is split again).
370 int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
371 SkConic dstTemp[2];
372 int conicCnt = split_conic(src, dstTemp, weight);
373 if (2 == conicCnt) {
374 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
375 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
376 } else {
377 dst[0] = dstTemp[0];
378 }
379 return conicCnt;
380 }
381
382 typedef GM INHERITED;
383};
384
385//////////////////////////////////////////////////////////////////////////////
joshualitt95964c62015-02-11 13:45:50 -0800386
387class BezierQuadTestBatch : public GrTestBatch {
388public:
reed1b55a962015-09-17 20:16:13 -0700389 DEFINE_BATCH_CLASS_ID
mtklein36352bf2015-03-25 18:17:31 -0700390 const char* name() const override { return "BezierQuadTestBatch"; }
joshualitt95964c62015-02-11 13:45:50 -0800391
bsalomon342bfc22016-04-01 06:06:20 -0700392 BezierQuadTestBatch(const GrGeometryProcessor* gp, const SkRect& bounds, GrColor color,
393 const GrPathUtils::QuadUVMatrix& devToUV)
394 : INHERITED(ClassID(), bounds, color)
395 , fDevToUV(devToUV)
396 , fGeometryProcessor(SkRef(gp)) {
joshualitt95964c62015-02-11 13:45:50 -0800397 }
398
399private:
joshualitt95964c62015-02-11 13:45:50 -0800400
401 struct Vertex {
402 SkPoint fPosition;
403 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
404 };
405
bsalomon342bfc22016-04-01 06:06:20 -0700406 void onPrepareDraws(Target* target) const override {
bsalomonb5238a72015-05-05 07:49:49 -0700407 QuadHelper helper;
bsalomon342bfc22016-04-01 06:06:20 -0700408 size_t vertexStride = fGeometryProcessor->getVertexStride();
bsalomonb5238a72015-05-05 07:49:49 -0700409 SkASSERT(vertexStride == sizeof(Vertex));
bsalomon75398562015-08-17 12:55:38 -0700410 Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1));
bsalomonb5238a72015-05-05 07:49:49 -0700411 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800412 return;
413 }
bsalomon342bfc22016-04-01 06:06:20 -0700414 const SkRect& bounds = this->bounds();
415 verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom,
joshualitt95964c62015-02-11 13:45:50 -0800416 sizeof(Vertex));
joshualitt95964c62015-02-11 13:45:50 -0800417 fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts);
bsalomon342bfc22016-04-01 06:06:20 -0700418 helper.recordDraw(target, fGeometryProcessor);
joshualitt95964c62015-02-11 13:45:50 -0800419 }
420
bsalomon342bfc22016-04-01 06:06:20 -0700421 GrPathUtils::QuadUVMatrix fDevToUV;
422 SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
joshualitt95964c62015-02-11 13:45:50 -0800423
424 static const int kVertsPerCubic = 4;
425 static const int kIndicesPerCubic = 6;
426
427 typedef GrTestBatch INHERITED;
428};
429
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000430/**
431 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
432 */
433class BezierQuadEffects : public GM {
434public:
435 BezierQuadEffects() {
436 this->setBGColor(0xFFFFFFFF);
437 }
438
439protected:
mtklein36352bf2015-03-25 18:17:31 -0700440 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000441 return SkString("bezier_quad_effects");
442 }
443
mtklein36352bf2015-03-25 18:17:31 -0700444 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700445 return SkISize::Make(800, 800);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000446 }
447
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000448
mtklein36352bf2015-03-25 18:17:31 -0700449 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -0700450 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
451 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -0700452 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000453 return;
454 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000455
robertphillips175dd9b2016-04-28 14:32:04 -0700456 GrContext* context = canvas->getGrContext();
457 if (!context) {
joshualittf5883a62016-01-13 07:47:38 -0800458 return;
459 }
460
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000461 struct Vertex {
462 SkPoint fPosition;
463 float fUV[4]; // The last two values are ignored. The effect expects a vec4f.
464 };
465
466 static const int kNumQuads = 5;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000467 SkRandom rand;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000468
469 int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3)));
470 int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols);
robertphillips175dd9b2016-04-28 14:32:04 -0700471 SkScalar w = SkIntToScalar(drawContext->width()) / numCols;
472 SkScalar h = SkIntToScalar(drawContext->height()) / numRows;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000473 int row = 0;
474 int col = 0;
joshualitt88c23fc2015-05-13 14:18:07 -0700475 static const GrColor color = 0xff000000;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000476
477 for (int i = 0; i < kNumQuads; ++i) {
478 SkPoint baseControlPts[] = {
479 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
480 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)},
481 {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}
482 };
joshualittb0a8a372014-09-23 09:50:21 -0700483 for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) {
484 SkAutoTUnref<GrGeometryProcessor> gp;
joshualittf5883a62016-01-13 07:47:38 -0800485 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType;
486 gp.reset(GrQuadEffect::Create(color, SkMatrix::I(), et,
487 *context->caps(), SkMatrix::I(), false));
488 if (!gp) {
489 continue;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000490 }
491
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000492 SkScalar x = SkScalarMul(col, w);
493 SkScalar y = SkScalarMul(row, h);
494 SkPoint controlPts[] = {
495 {x + baseControlPts[0].fX, y + baseControlPts[0].fY},
496 {x + baseControlPts[1].fX, y + baseControlPts[1].fY},
497 {x + baseControlPts[2].fX, y + baseControlPts[2].fY}
498 };
499 SkPoint chopped[5];
500 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
501
502 SkPaint ctrlPtPaint;
503 ctrlPtPaint.setColor(rand.nextU() | 0xFF000000);
504 for (int i = 0; i < 3; ++i) {
505 canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint);
506 }
507
508 SkPaint polyPaint;
509 polyPaint.setColor(0xffA0A0A0);
510 polyPaint.setStrokeWidth(0);
511 polyPaint.setStyle(SkPaint::kStroke_Style);
512 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
513
514 SkPaint choppedPtPaint;
515 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
516
517 for (int c = 0; c < cnt; ++c) {
518 SkPoint* pts = chopped + 2 * c;
519
520 for (int i = 0; i < 3; ++i) {
521 canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint);
522 }
523
524 SkRect bounds;
525 bounds.set(pts, 3);
526
527 SkPaint boundsPaint;
528 boundsPaint.setColor(0xff808080);
529 boundsPaint.setStrokeWidth(0);
530 boundsPaint.setStyle(SkPaint::kStroke_Style);
531 canvas->drawRect(bounds, boundsPaint);
532
egdaniel8dd688b2015-01-22 10:16:09 -0800533 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800534 pipelineBuilder.setXPFactory(
535 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
robertphillips175dd9b2016-04-28 14:32:04 -0700536 pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
joshualitt3f284d72015-02-11 11:34:58 -0800537
joshualitt95964c62015-02-11 13:45:50 -0800538 GrPathUtils::QuadUVMatrix DevToUV(pts);
539
bsalomon342bfc22016-04-01 06:06:20 -0700540 SkAutoTUnref<GrDrawBatch> batch(
541 new BezierQuadTestBatch(gp, bounds, color, DevToUV));
joshualitt95964c62015-02-11 13:45:50 -0800542
robertphillips391395d2016-03-02 09:26:36 -0800543 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000544 }
545 ++col;
546 if (numCols == col) {
547 col = 0;
548 ++row;
549 }
550 }
551 }
552 }
553
554private:
555 typedef GM INHERITED;
556};
557
halcanary385fe4d2015-08-26 13:07:48 -0700558DEF_GM(return new BezierCubicEffects;)
559DEF_GM(return new BezierConicEffects;)
560DEF_GM(return new BezierQuadEffects;)
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000561}
562
563#endif