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