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 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | #include "GrRenderTargetContextPriv.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 { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 102 | GrRenderTargetContext* renderTargetContext = |
| 103 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 104 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 105 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 108 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 109 | GrContext* context = canvas->getGrContext(); |
| 110 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 114 | struct Vertex { |
| 115 | SkPoint fPosition; |
| 116 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 117 | }; |
| 118 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 119 | constexpr int kNumCubics = 15; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 120 | SkRandom rand; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 121 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 122 | // Mult by 3 for each edge effect type |
| 123 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3))); |
| 124 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 125 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 126 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 127 | int row = 0; |
| 128 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 129 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 130 | |
| 131 | for (int i = 0; i < kNumCubics; ++i) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 132 | SkPoint baseControlPts[] = { |
| 133 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 134 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 135 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 136 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 137 | }; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 138 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 139 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 140 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 141 | gp = GrCubicEffect::Make(color, SkMatrix::I(), et, *context->caps()); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 142 | if (!gp) { |
| 143 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 144 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 145 | 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.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 160 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 161 | SkPaint ctrlPtPaint; |
| 162 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 163 | for (int i = 0; i < 4; ++i) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 164 | 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] | 165 | } |
| 166 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 167 | 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.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 172 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 173 | SkPaint choppedPtPaint; |
| 174 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 175 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 176 | 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 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 192 | GrPaint grPaint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 193 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 194 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 195 | sk_sp<GrDrawBatch> batch = sk_make_sp<BezierCubicOrConicTestBatch>( |
| 196 | gp, bounds, color, klmEqs, klmSigns[c]); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 197 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 198 | renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get()); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 199 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 200 | ++col; |
| 201 | if (numCols == col) { |
| 202 | col = 0; |
| 203 | ++row; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 204 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private: |
| 210 | typedef GM INHERITED; |
| 211 | }; |
| 212 | |
| 213 | ////////////////////////////////////////////////////////////////////////////// |
| 214 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 215 | /** |
| 216 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 217 | */ |
| 218 | class BezierConicEffects : public GM { |
| 219 | public: |
| 220 | BezierConicEffects() { |
| 221 | this->setBGColor(0xFFFFFFFF); |
| 222 | } |
| 223 | |
| 224 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 225 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 226 | return SkString("bezier_conic_effects"); |
| 227 | } |
| 228 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 229 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 230 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 231 | } |
| 232 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 233 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 235 | GrRenderTargetContext* renderTargetContext = |
| 236 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 237 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 238 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 239 | return; |
| 240 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 241 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 242 | GrContext* context = canvas->getGrContext(); |
| 243 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 247 | struct Vertex { |
| 248 | SkPoint fPosition; |
| 249 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 250 | }; |
| 251 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 252 | constexpr int kNumConics = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 253 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 254 | |
| 255 | // Mult by 3 for each edge effect type |
| 256 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3))); |
| 257 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 258 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 259 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 260 | int row = 0; |
| 261 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 262 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 263 | |
| 264 | for (int i = 0; i < kNumConics; ++i) { |
| 265 | SkPoint baseControlPts[] = { |
| 266 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 267 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 268 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 269 | }; |
| 270 | SkScalar weight = rand.nextRangeF(0.f, 2.f); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 271 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 272 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 273 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 274 | gp = GrConicEffect::Make(color, SkMatrix::I(), et, |
| 275 | *context->caps(), SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 276 | if (!gp) { |
| 277 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 278 | } |
| 279 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 280 | SkScalar x = SkScalarMul(col, w); |
| 281 | SkScalar y = SkScalarMul(row, h); |
| 282 | SkPoint controlPts[] = { |
| 283 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 284 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 285 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 286 | }; |
| 287 | SkConic dst[4]; |
| 288 | SkScalar klmEqs[9]; |
| 289 | int cnt = chop_conic(controlPts, dst, weight); |
| 290 | GrPathUtils::getConicKLM(controlPts, weight, klmEqs); |
| 291 | |
| 292 | SkPaint ctrlPtPaint; |
| 293 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 294 | for (int i = 0; i < 3; ++i) { |
| 295 | canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint); |
| 296 | } |
| 297 | |
| 298 | SkPaint polyPaint; |
| 299 | polyPaint.setColor(0xffA0A0A0); |
| 300 | polyPaint.setStrokeWidth(0); |
| 301 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 302 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 303 | |
| 304 | SkPaint choppedPtPaint; |
| 305 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 306 | |
| 307 | for (int c = 0; c < cnt; ++c) { |
| 308 | SkPoint* pts = dst[c].fPts; |
| 309 | for (int i = 0; i < 3; ++i) { |
| 310 | canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint); |
| 311 | } |
| 312 | |
| 313 | SkRect bounds; |
| 314 | //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}}; |
| 315 | //bounds.set(bPts, 2); |
| 316 | bounds.set(pts, 3); |
| 317 | |
| 318 | SkPaint boundsPaint; |
| 319 | boundsPaint.setColor(0xff808080); |
| 320 | boundsPaint.setStrokeWidth(0); |
| 321 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 322 | canvas->drawRect(bounds, boundsPaint); |
| 323 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 324 | GrPaint grPaint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 325 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 326 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 327 | sk_sp<GrDrawBatch> batch( |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 328 | new BezierCubicOrConicTestBatch(gp, bounds, color, klmEqs, 1.f)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 329 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 330 | renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get()); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 331 | } |
| 332 | ++col; |
| 333 | if (numCols == col) { |
| 334 | col = 0; |
| 335 | ++row; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | private: |
| 342 | // Uses the max curvature function for quads to estimate |
| 343 | // where to chop the conic. If the max curvature is not |
| 344 | // found along the curve segment it will return 1 and |
| 345 | // dst[0] is the original conic. If it returns 2 the dst[0] |
| 346 | // and dst[1] are the two new conics. |
| 347 | int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
| 348 | SkScalar t = SkFindQuadMaxCurvature(src); |
| 349 | if (t == 0) { |
| 350 | if (dst) { |
| 351 | dst[0].set(src, weight); |
| 352 | } |
| 353 | return 1; |
| 354 | } else { |
| 355 | if (dst) { |
| 356 | SkConic conic; |
| 357 | conic.set(src, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 358 | if (!conic.chopAt(t, dst)) { |
| 359 | dst[0].set(src, weight); |
| 360 | return 1; |
| 361 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 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 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 386 | |
| 387 | class BezierQuadTestBatch : public GrTestBatch { |
| 388 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 389 | DEFINE_BATCH_CLASS_ID |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 390 | const char* name() const override { return "BezierQuadTestBatch"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 391 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 392 | BezierQuadTestBatch(sk_sp<GrGeometryProcessor> gp, const SkRect& bounds, GrColor color, |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 393 | const GrPathUtils::QuadUVMatrix& devToUV) |
| 394 | : INHERITED(ClassID(), bounds, color) |
| 395 | , fDevToUV(devToUV) |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 396 | , fGeometryProcessor(std::move(gp)) { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | private: |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 400 | |
| 401 | struct Vertex { |
| 402 | SkPoint fPosition; |
| 403 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 404 | }; |
| 405 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 406 | void onPrepareDraws(Target* target) const override { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 407 | QuadHelper helper; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 408 | size_t vertexStride = fGeometryProcessor->getVertexStride(); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 409 | SkASSERT(vertexStride == sizeof(Vertex)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 410 | Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, vertexStride, 1)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 411 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 412 | return; |
| 413 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 414 | const SkRect& bounds = this->bounds(); |
| 415 | verts[0].fPosition.setRectFan(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 416 | sizeof(Vertex)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 417 | fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 418 | helper.recordDraw(target, fGeometryProcessor.get()); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 419 | } |
| 420 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 421 | GrPathUtils::QuadUVMatrix fDevToUV; |
| 422 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 423 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 424 | static constexpr int kVertsPerCubic = 4; |
| 425 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 426 | |
| 427 | typedef GrTestBatch INHERITED; |
| 428 | }; |
| 429 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 430 | /** |
| 431 | * This GM directly exercises effects that draw Bezier quad curves in the GPU backend. |
| 432 | */ |
| 433 | class BezierQuadEffects : public GM { |
| 434 | public: |
| 435 | BezierQuadEffects() { |
| 436 | this->setBGColor(0xFFFFFFFF); |
| 437 | } |
| 438 | |
| 439 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 440 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 441 | return SkString("bezier_quad_effects"); |
| 442 | } |
| 443 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 444 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 445 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 446 | } |
| 447 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 448 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 449 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 450 | GrRenderTargetContext* renderTargetContext = |
| 451 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 452 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 453 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 456 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 457 | GrContext* context = canvas->getGrContext(); |
| 458 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 462 | struct Vertex { |
| 463 | SkPoint fPosition; |
| 464 | float fUV[4]; // The last two values are ignored. The effect expects a vec4f. |
| 465 | }; |
| 466 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 467 | constexpr int kNumQuads = 5; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 468 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 469 | |
| 470 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3))); |
| 471 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 472 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 473 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 474 | int row = 0; |
| 475 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 476 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 477 | |
| 478 | for (int i = 0; i < kNumQuads; ++i) { |
| 479 | SkPoint baseControlPts[] = { |
| 480 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 481 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 482 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 483 | }; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 484 | for(int edgeType = 0; edgeType < kGrProcessorEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 485 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 486 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)edgeType; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 487 | gp = GrQuadEffect::Make(color, SkMatrix::I(), et, |
| 488 | *context->caps(), SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 489 | if (!gp) { |
| 490 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 491 | } |
| 492 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 493 | SkScalar x = SkScalarMul(col, w); |
| 494 | SkScalar y = SkScalarMul(row, h); |
| 495 | SkPoint controlPts[] = { |
| 496 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 497 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 498 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 499 | }; |
| 500 | SkPoint chopped[5]; |
| 501 | int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped); |
| 502 | |
| 503 | SkPaint ctrlPtPaint; |
| 504 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 505 | for (int i = 0; i < 3; ++i) { |
| 506 | canvas->drawCircle(controlPts[i].fX, controlPts[i].fY, 6.f, ctrlPtPaint); |
| 507 | } |
| 508 | |
| 509 | SkPaint polyPaint; |
| 510 | polyPaint.setColor(0xffA0A0A0); |
| 511 | polyPaint.setStrokeWidth(0); |
| 512 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 513 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 514 | |
| 515 | SkPaint choppedPtPaint; |
| 516 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 517 | |
| 518 | for (int c = 0; c < cnt; ++c) { |
| 519 | SkPoint* pts = chopped + 2 * c; |
| 520 | |
| 521 | for (int i = 0; i < 3; ++i) { |
| 522 | canvas->drawCircle(pts[i].fX, pts[i].fY, 3.f, choppedPtPaint); |
| 523 | } |
| 524 | |
| 525 | SkRect bounds; |
| 526 | bounds.set(pts, 3); |
| 527 | |
| 528 | SkPaint boundsPaint; |
| 529 | boundsPaint.setColor(0xff808080); |
| 530 | boundsPaint.setStrokeWidth(0); |
| 531 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 532 | canvas->drawRect(bounds, boundsPaint); |
| 533 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 534 | GrPaint grPaint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 535 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc)); |
joshualitt | 3f284d7 | 2015-02-11 11:34:58 -0800 | [diff] [blame] | 536 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 537 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 538 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 539 | sk_sp<GrDrawBatch> batch(new BezierQuadTestBatch(gp, bounds, color, DevToUV)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 540 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 541 | renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get()); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 542 | } |
| 543 | ++col; |
| 544 | if (numCols == col) { |
| 545 | col = 0; |
| 546 | ++row; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | private: |
| 553 | typedef GM INHERITED; |
| 554 | }; |
| 555 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 556 | DEF_GM(return new BezierCubicEffects;) |
| 557 | DEF_GM(return new BezierConicEffects;) |
| 558 | DEF_GM(return new BezierQuadEffects;) |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | #endif |