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 | |
| 77 | static const int kVertsPerCubic = 4; |
| 78 | static const int kIndicesPerCubic = 6; |
| 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 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 118 | static const 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; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 128 | static const 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 | |
| 250 | static const 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; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 260 | static const 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); |
| 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 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 381 | |
| 382 | class BezierQuadTestBatch : public GrTestBatch { |
| 383 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 384 | DEFINE_BATCH_CLASS_ID |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 385 | const char* name() const override { return "BezierQuadTestBatch"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 386 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 387 | BezierQuadTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color, |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 388 | const GrPathUtils::QuadUVMatrix& devToUV) |
| 389 | : INHERITED(ClassID(), bounds, color) |
| 390 | , fDevToUV(devToUV) |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 391 | , fGeometryProcessor(std::move(gp)) { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | private: |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 395 | |
| 396 | struct Vertex { |
| 397 | SkPoint fPosition; |
| 398 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 399 | }; |
| 400 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 401 | void onPrepareDraws(Target* target) const override { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 402 | QuadHelper helper; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 403 | size_t vertexStride = fGeometryProcessor->getVertexStride(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 404 | SkASSERT(vertexStride == sizeof(Vertex)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 405 | Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 406 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 407 | return; |
| 408 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 409 | const SkRect& bounds = this->bounds(); |
| 410 | verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 411 | sizeof(Vertex)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 412 | fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 413 | helper.recordDraw(target, fGeometryProcessor.get()); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 414 | } |
| 415 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 416 | GrPathUtils::QuadUVMatrix fDevToUV; |
| 417 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 418 | |
| 419 | static const int kVertsPerCubic = 4; |
| 420 | static const int kIndicesPerCubic = 6; |
| 421 | |
| 422 | typedef GrTestBatch INHERITED; |
| 423 | }; |
| 424 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 425 | /** |
| 426 | * This GM directly exercises effects that draw Bezier quad curves in the GPU backend. |
| 427 | */ |
| 428 | class BezierQuadEffects : public GM { |
| 429 | public: |
| 430 | BezierQuadEffects() { |
| 431 | this->setBGColor(0xFFFFFFFF); |
| 432 | } |
| 433 | |
| 434 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 435 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 436 | return SkString("bezier_quad_effects"); |
| 437 | } |
| 438 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 439 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 440 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 441 | } |
| 442 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 443 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 444 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 445 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 446 | if (!drawContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 447 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 448 | return; |
| 449 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 450 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 451 | GrContext* context = canvas->getGrContext(); |
| 452 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 456 | 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.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 462 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 463 | |
| 464 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3))); |
| 465 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 466 | SkScalar w = SkIntToScalar(drawContext->width()) / numCols; |
| 467 | SkScalar h = SkIntToScalar(drawContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 468 | int row = 0; |
| 469 | int col = 0; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 470 | static const GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 471 | |
| 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 | }; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 478 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 479 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 480 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 481 | gp = GrQuadEffect::Make(color, SkMatrix::I(), et, |
| 482 | *context->caps(), SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 483 | if (!gp) { |
| 484 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 485 | } |
| 486 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 487 | 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 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame^] | 528 | GrPaint grPaint; |
| 529 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
joshualitt | 3f284d7 | 2015-02-11 11:34:58 -0800 | [diff] [blame] | 530 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 531 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 532 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 533 | SkAutoTUnref<GrDrawBatch> batch( |
| 534 | new BezierQuadTestBatch(gp, bounds, color, DevToUV)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 535 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame^] | 536 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 537 | } |
| 538 | ++col; |
| 539 | if (numCols == col) { |
| 540 | col = 0; |
| 541 | ++row; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | private: |
| 548 | typedef GM INHERITED; |
| 549 | }; |
| 550 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 551 | DEF_GM(return new BezierCubicEffects;) |
| 552 | DEF_GM(return new BezierConicEffects;) |
| 553 | DEF_GM(return new BezierQuadEffects;) |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | #endif |