commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 13 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 14 | #include "GrDrawContextPriv.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
| 16 | #include "GrPathUtils.h" |
| 17 | #include "GrTest.h" |
| 18 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 19 | #include "SkGeometry.h" |
| 20 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 21 | #include "batches/GrTestBatch.h" |
| 22 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 23 | #include "effects/GrBezierEffect.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 25 | static 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 | |
| 29 | namespace skiagm { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 30 | |
| 31 | class BezierCubicOrConicTestBatch : public GrTestBatch { |
| 32 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 33 | DEFINE_BATCH_CLASS_ID |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | const char* name() const override { return "BezierCubicOrConicTestBatch"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 36 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 37 | BezierCubicOrConicTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 38 | GrColor color, const SkScalar klmEqs[9], SkScalar sign) |
| 39 | : INHERITED(ClassID(), bounds, color) |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 40 | , fGeometryProcessor(std::move(gp)) { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 41 | for (int i = 0; i < 9; i++) { |
| 42 | fKlmEqs[i] = klmEqs[i]; |
| 43 | } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 44 | fSign = sign; |
| 45 | } |
| 46 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 47 | private: |
| 48 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 49 | struct Vertex { |
| 50 | SkPoint fPosition; |
| 51 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 52 | }; |
| 53 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 54 | void onPrepareDraws(Target* target) const override { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 55 | QuadHelper helper; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 56 | size_t vertexStride = fGeometryProcessor->getVertexStride(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 57 | SkASSERT(vertexStride == sizeof(Vertex)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 58 | Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 59 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 60 | return; |
| 61 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 62 | const SkRect& bounds = this->bounds(); |
| 63 | verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 64 | 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 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 70 | helper.recordDraw(target, fGeometryProcessor.get()); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 71 | } |
| 72 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 73 | SkScalar fKlmEqs[9]; |
| 74 | SkScalar fSign; |
| 75 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 76 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 77 | static constexpr int kVertsPerCubic = 4; |
| 78 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 79 | |
| 80 | typedef GrTestBatch INHERITED; |
| 81 | }; |
| 82 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 83 | /** |
| 84 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 85 | */ |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 86 | class BezierCubicEffects : public GM { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 87 | public: |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 88 | BezierCubicEffects() { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 89 | this->setBGColor(0xFFFFFFFF); |
| 90 | } |
| 91 | |
| 92 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 94 | return SkString("bezier_cubic_effects"); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 95 | } |
| 96 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 98 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 99 | } |
| 100 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 101 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 102 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 103 | if (!drawContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 104 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 107 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 108 | GrContext* context = canvas->getGrContext(); |
| 109 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 113 | struct Vertex { |
| 114 | SkPoint fPosition; |
| 115 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 116 | }; |
| 117 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 118 | constexpr int kNumCubics = 15; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 119 | SkRandom rand; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 120 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 121 | // Mult by 3 for each edge effect type |
| 122 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3))); |
| 123 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 124 | SkScalar w = SkIntToScalar(drawContext->width()) / numCols; |
| 125 | SkScalar h = SkIntToScalar(drawContext->height()) / numRows; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 126 | int row = 0; |
| 127 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 128 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 129 | |
| 130 | for (int i = 0; i < kNumCubics; ++i) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 131 | 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.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 136 | }; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 137 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 138 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 139 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 140 | gp = GrCubicEffect::Make(color, SkMatrix::I(), et, *context->caps()); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 141 | if (!gp) { |
| 142 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 143 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 144 | 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.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 159 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 160 | SkPaint ctrlPtPaint; |
| 161 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 162 | for (int i = 0; i < 4; ++i) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 163 | canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 166 | 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.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 171 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 172 | SkPaint choppedPtPaint; |
| 173 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 174 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 175 | 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 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 191 | GrPaint grPaint; |
| 192 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 193 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 194 | SkAutoTUnref<GrDrawBatch> batch( |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 195 | new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, klmSigns[c])); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 196 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 197 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 198 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 199 | ++col; |
| 200 | if (numCols == col) { |
| 201 | col = 0; |
| 202 | ++row; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 203 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | typedef GM INHERITED; |
| 210 | }; |
| 211 | |
| 212 | ////////////////////////////////////////////////////////////////////////////// |
| 213 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 214 | /** |
| 215 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 216 | */ |
| 217 | class BezierConicEffects : public GM { |
| 218 | public: |
| 219 | BezierConicEffects() { |
| 220 | this->setBGColor(0xFFFFFFFF); |
| 221 | } |
| 222 | |
| 223 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 224 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 225 | return SkString("bezier_conic_effects"); |
| 226 | } |
| 227 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 228 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 229 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 232 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 233 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 234 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 235 | if (!drawContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 236 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 237 | return; |
| 238 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 239 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 240 | GrContext* context = canvas->getGrContext(); |
| 241 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 245 | struct Vertex { |
| 246 | SkPoint fPosition; |
| 247 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 248 | }; |
| 249 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 250 | constexpr int kNumConics = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 251 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 252 | |
| 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); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 256 | SkScalar w = SkIntToScalar(drawContext->width()) / numCols; |
| 257 | SkScalar h = SkIntToScalar(drawContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 258 | int row = 0; |
| 259 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 260 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 261 | |
| 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); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 269 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 270 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 271 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 272 | gp = GrConicEffect::Make(color, SkMatrix::I(), et, |
| 273 | *context->caps(), SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 274 | if (!gp) { |
| 275 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 276 | } |
| 277 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 278 | 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 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 322 | GrPaint grPaint; |
| 323 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 324 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 325 | SkAutoTUnref<GrDrawBatch> batch( |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 326 | new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, 1.f)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 327 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 328 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 329 | } |
| 330 | ++col; |
| 331 | if (numCols == col) { |
| 332 | col = 0; |
| 333 | ++row; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | private: |
| 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); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame^] | 356 | if (!conic.chopAt(t, dst)) { |
| 357 | dst[0].set(src, weight); |
| 358 | return 1; |
| 359 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 360 | } |
| 361 | return 2; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Calls split_conic on the entire conic and then once more on each subsection. |
| 366 | // Most cases will result in either 1 conic (chop point is not within t range) |
| 367 | // or 3 points (split once and then one subsection is split again). |
| 368 | int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
| 369 | SkConic dstTemp[2]; |
| 370 | int conicCnt = split_conic(src, dstTemp, weight); |
| 371 | if (2 == conicCnt) { |
| 372 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 373 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
| 374 | } else { |
| 375 | dst[0] = dstTemp[0]; |
| 376 | } |
| 377 | return conicCnt; |
| 378 | } |
| 379 | |
| 380 | typedef GM INHERITED; |
| 381 | }; |
| 382 | |
| 383 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 384 | |
| 385 | class BezierQuadTestBatch : public GrTestBatch { |
| 386 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 387 | DEFINE_BATCH_CLASS_ID |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 388 | const char* name() const override { return "BezierQuadTestBatch"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 389 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 390 | BezierQuadTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color, |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 391 | const GrPathUtils::QuadUVMatrix& devToUV) |
| 392 | : INHERITED(ClassID(), bounds, color) |
| 393 | , fDevToUV(devToUV) |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 394 | , fGeometryProcessor(std::move(gp)) { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | private: |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 398 | |
| 399 | struct Vertex { |
| 400 | SkPoint fPosition; |
| 401 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 402 | }; |
| 403 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 404 | void onPrepareDraws(Target* target) const override { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 405 | QuadHelper helper; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 406 | size_t vertexStride = fGeometryProcessor->getVertexStride(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 407 | SkASSERT(vertexStride == sizeof(Vertex)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 408 | Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 409 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 410 | return; |
| 411 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 412 | const SkRect& bounds = this->bounds(); |
| 413 | verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 414 | sizeof(Vertex)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 415 | fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 416 | helper.recordDraw(target, fGeometryProcessor.get()); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 417 | } |
| 418 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 419 | GrPathUtils::QuadUVMatrix fDevToUV; |
| 420 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 421 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 422 | static constexpr int kVertsPerCubic = 4; |
| 423 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 424 | |
| 425 | typedef GrTestBatch INHERITED; |
| 426 | }; |
| 427 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 428 | /** |
| 429 | * This GM directly exercises effects that draw Bezier quad curves in the GPU backend. |
| 430 | */ |
| 431 | class BezierQuadEffects : public GM { |
| 432 | public: |
| 433 | BezierQuadEffects() { |
| 434 | this->setBGColor(0xFFFFFFFF); |
| 435 | } |
| 436 | |
| 437 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 438 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 439 | return SkString("bezier_quad_effects"); |
| 440 | } |
| 441 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 442 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 443 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 444 | } |
| 445 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 446 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 447 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 448 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 449 | if (!drawContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 450 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 451 | return; |
| 452 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 453 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 454 | GrContext* context = canvas->getGrContext(); |
| 455 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 459 | struct Vertex { |
| 460 | SkPoint fPosition; |
| 461 | float fUV[4]; // The last two values are ignored. The effect expects a vec4f. |
| 462 | }; |
| 463 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 464 | constexpr int kNumQuads = 5; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 465 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 466 | |
| 467 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3))); |
| 468 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 469 | SkScalar w = SkIntToScalar(drawContext->width()) / numCols; |
| 470 | SkScalar h = SkIntToScalar(drawContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 471 | int row = 0; |
| 472 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 473 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 474 | |
| 475 | for (int i = 0; i < kNumQuads; ++i) { |
| 476 | SkPoint baseControlPts[] = { |
| 477 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 478 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 479 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 480 | }; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 481 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 482 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 483 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 484 | gp = GrQuadEffect::Make(color, SkMatrix::I(), et, |
| 485 | *context->caps(), SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 486 | if (!gp) { |
| 487 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 488 | } |
| 489 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 490 | SkScalar x = SkScalarMul(col, w); |
| 491 | SkScalar y = SkScalarMul(row, h); |
| 492 | SkPoint controlPts[] = { |
| 493 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 494 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 495 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 496 | }; |
| 497 | SkPoint chopped[5]; |
| 498 | int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped); |
| 499 | |
| 500 | SkPaint ctrlPtPaint; |
| 501 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 502 | for (int i = 0; i < 3; ++i) { |
| 503 | canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint); |
| 504 | } |
| 505 | |
| 506 | SkPaint polyPaint; |
| 507 | polyPaint.setColor(0xffA0A0A0); |
| 508 | polyPaint.setStrokeWidth(0); |
| 509 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 510 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 511 | |
| 512 | SkPaint choppedPtPaint; |
| 513 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 514 | |
| 515 | for (int c = 0; c < cnt; ++c) { |
| 516 | SkPoint* pts = chopped + 2 * c; |
| 517 | |
| 518 | for (int i = 0; i < 3; ++i) { |
| 519 | canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint); |
| 520 | } |
| 521 | |
| 522 | SkRect bounds; |
| 523 | bounds.set(pts, 3); |
| 524 | |
| 525 | SkPaint boundsPaint; |
| 526 | boundsPaint.setColor(0xff808080); |
| 527 | boundsPaint.setStrokeWidth(0); |
| 528 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 529 | canvas->drawRect(bounds, boundsPaint); |
| 530 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 531 | GrPaint grPaint; |
| 532 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
joshualitt | 3f284d7 | 2015-02-11 11:34:58 -0800 | [diff] [blame] | 533 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 534 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 535 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 536 | SkAutoTUnref<GrDrawBatch> batch( |
| 537 | new BezierQuadTestBatch(gp, bounds, color, DevToUV)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 538 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 539 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 540 | } |
| 541 | ++col; |
| 542 | if (numCols == col) { |
| 543 | col = 0; |
| 544 | ++row; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | private: |
| 551 | typedef GM INHERITED; |
| 552 | }; |
| 553 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 554 | DEF_GM(return new BezierCubicEffects;) |
| 555 | DEF_GM(return new BezierConicEffects;) |
| 556 | DEF_GM(return new BezierQuadEffects;) |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | #endif |