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" |
Chris Dalton | 1d4af54 | 2018-04-16 14:23:00 -0600 | [diff] [blame] | 11 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 14 | #include "GrMemoryPool.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 15 | #include "GrOpFlushState.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 16 | #include "GrPathUtils.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 17 | #include "GrRenderTargetContextPriv.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 18 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 19 | #include "SkGeometry.h" |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 20 | #include "SkPoint3.h" |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 21 | #include "SkPointPriv.h" |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 22 | #include "effects/GrBezierEffect.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 23 | #include "ops/GrMeshDrawOp.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 | namespace skiagm { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 26 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 27 | class BezierTestOp : public GrMeshDrawOp { |
| 28 | public: |
| 29 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 30 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 31 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 32 | return fProcessorSet.finalize(fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, |
| 33 | false, caps, &fColor); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 34 | } |
| 35 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 36 | void visitProxies(const VisitProxyFunc& func, VisitorType) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 37 | fProcessorSet.visitProxies(func); |
| 38 | } |
| 39 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 40 | protected: |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 41 | BezierTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, const SkPMColor4f& color, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 42 | int32_t classID) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 43 | : INHERITED(classID) |
| 44 | , fRect(rect) |
| 45 | , fColor(color) |
| 46 | , fGeometryProcessor(std::move(gp)) |
| 47 | , fProcessorSet(SkBlendMode::kSrc) { |
| 48 | this->setBounds(rect, HasAABloat::kYes, IsZeroArea::kNo); |
| 49 | } |
| 50 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 51 | Target::PipelineAndFixedDynamicState makePipeline(Target* target) { |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 52 | return target->makePipeline(0, std::move(fProcessorSet), target->detachAppliedClip()); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 55 | sk_sp<const GrGeometryProcessor> gp() const { return fGeometryProcessor; } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 56 | |
| 57 | const SkRect& rect() const { return fRect; } |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 58 | const SkPMColor4f& color() const { return fColor; } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 59 | |
| 60 | private: |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 61 | SkRect fRect; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 62 | SkPMColor4f fColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 63 | sk_sp<const GrGeometryProcessor> fGeometryProcessor; |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 64 | GrProcessorSet fProcessorSet; |
| 65 | |
| 66 | typedef GrMeshDrawOp INHERITED; |
| 67 | }; |
| 68 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 69 | /** |
| 70 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 71 | */ |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 72 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 73 | class BezierConicTestOp : public BezierTestOp { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 74 | public: |
| 75 | DEFINE_OP_CLASS_ID |
| 76 | |
| 77 | const char* name() const override { return "BezierConicTestOp"; } |
| 78 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 79 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 80 | sk_sp<const GrGeometryProcessor> gp, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 81 | const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 82 | const SkPMColor4f& color, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 83 | const SkMatrix& klm) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 84 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 85 | |
| 86 | return pool->allocate<BezierConicTestOp>(std::move(gp), rect, color, klm); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 90 | friend class ::GrOpMemoryPool; // for ctor |
| 91 | |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 92 | BezierConicTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 93 | const SkPMColor4f& color, const SkMatrix& klm) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 94 | : INHERITED(std::move(gp), rect, color, ClassID()), fKLM(klm) {} |
| 95 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 96 | struct Vertex { |
| 97 | SkPoint fPosition; |
| 98 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 99 | }; |
| 100 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 101 | void onPrepareDraws(Target* target) override { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 102 | SkASSERT(this->gp()->vertexStride() == sizeof(Vertex)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 103 | QuadHelper helper(target, sizeof(Vertex), 1); |
| 104 | Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 105 | if (!verts) { |
| 106 | return; |
| 107 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 108 | SkRect rect = this->rect(); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 109 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect.fLeft, rect.fTop, rect.fRight, |
| 110 | rect.fBottom, sizeof(Vertex)); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 111 | for (int v = 0; v < 4; ++v) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 112 | SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f}; |
| 113 | fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 114 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 115 | auto pipe = this->makePipeline(target); |
| 116 | helper.recordDraw(target, this->gp(), pipe.fPipeline, pipe.fFixedDynamicState); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | SkMatrix fKLM; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 120 | |
| 121 | static constexpr int kVertsPerCubic = 4; |
| 122 | static constexpr int kIndicesPerCubic = 6; |
| 123 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 124 | typedef BezierTestOp INHERITED; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 128 | /** |
| 129 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 130 | */ |
| 131 | class BezierConicEffects : public GM { |
| 132 | public: |
| 133 | BezierConicEffects() { |
| 134 | this->setBGColor(0xFFFFFFFF); |
| 135 | } |
| 136 | |
| 137 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 139 | return SkString("bezier_conic_effects"); |
| 140 | } |
| 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 143 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 144 | } |
| 145 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 146 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 148 | GrRenderTargetContext* renderTargetContext = |
| 149 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 150 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 151 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 152 | return; |
| 153 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 154 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 155 | GrContext* context = canvas->getGrContext(); |
| 156 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 160 | struct Vertex { |
| 161 | SkPoint fPosition; |
| 162 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 163 | }; |
| 164 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 165 | constexpr int kNumConics = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 166 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 167 | |
| 168 | // Mult by 3 for each edge effect type |
| 169 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3))); |
| 170 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 171 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 172 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 173 | int row = 0; |
| 174 | int col = 0; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 175 | SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(0xff000000); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 176 | |
| 177 | for (int i = 0; i < kNumConics; ++i) { |
| 178 | SkPoint baseControlPts[] = { |
| 179 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 180 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 181 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 182 | }; |
| 183 | SkScalar weight = rand.nextRangeF(0.f, 2.f); |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 184 | for(int edgeType = 0; edgeType < kGrClipEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 185 | sk_sp<GrGeometryProcessor> gp; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 186 | GrClipEdgeType et = (GrClipEdgeType)edgeType; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 187 | gp = GrConicEffect::Make(color, SkMatrix::I(), et, *context->contextPriv().caps(), |
| 188 | SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 189 | if (!gp) { |
| 190 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 193 | SkScalar x = col * w; |
| 194 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 195 | SkPoint controlPts[] = { |
| 196 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 197 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 198 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 199 | }; |
| 200 | SkConic dst[4]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 201 | SkMatrix klm; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 202 | int cnt = chop_conic(controlPts, dst, weight); |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 203 | GrPathUtils::getConicKLM(controlPts, weight, &klm); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 204 | |
| 205 | SkPaint ctrlPtPaint; |
| 206 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 207 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 208 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | SkPaint polyPaint; |
| 212 | polyPaint.setColor(0xffA0A0A0); |
| 213 | polyPaint.setStrokeWidth(0); |
| 214 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 215 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 216 | |
| 217 | SkPaint choppedPtPaint; |
| 218 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 219 | |
| 220 | for (int c = 0; c < cnt; ++c) { |
| 221 | SkPoint* pts = dst[c].fPts; |
| 222 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 223 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | SkRect bounds; |
| 227 | //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}}; |
| 228 | //bounds.set(bPts, 2); |
| 229 | bounds.set(pts, 3); |
| 230 | |
| 231 | SkPaint boundsPaint; |
| 232 | boundsPaint.setColor(0xff808080); |
| 233 | boundsPaint.setStrokeWidth(0); |
| 234 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 235 | canvas->drawRect(bounds, boundsPaint); |
| 236 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 237 | std::unique_ptr<GrDrawOp> op = BezierConicTestOp::Make(context, gp, bounds, |
| 238 | color, klm); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 239 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 240 | } |
| 241 | ++col; |
| 242 | if (numCols == col) { |
| 243 | col = 0; |
| 244 | ++row; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | // Uses the max curvature function for quads to estimate |
| 252 | // where to chop the conic. If the max curvature is not |
| 253 | // found along the curve segment it will return 1 and |
| 254 | // dst[0] is the original conic. If it returns 2 the dst[0] |
| 255 | // and dst[1] are the two new conics. |
| 256 | int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
| 257 | SkScalar t = SkFindQuadMaxCurvature(src); |
Chris Dalton | 1d474dd | 2018-07-24 01:08:31 -0600 | [diff] [blame] | 258 | if (t == 0 || t == 1) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 259 | if (dst) { |
| 260 | dst[0].set(src, weight); |
| 261 | } |
| 262 | return 1; |
| 263 | } else { |
| 264 | if (dst) { |
| 265 | SkConic conic; |
| 266 | conic.set(src, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 267 | if (!conic.chopAt(t, dst)) { |
| 268 | dst[0].set(src, weight); |
| 269 | return 1; |
| 270 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 271 | } |
| 272 | return 2; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Calls split_conic on the entire conic and then once more on each subsection. |
| 277 | // Most cases will result in either 1 conic (chop point is not within t range) |
| 278 | // or 3 points (split once and then one subsection is split again). |
| 279 | int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
| 280 | SkConic dstTemp[2]; |
| 281 | int conicCnt = split_conic(src, dstTemp, weight); |
| 282 | if (2 == conicCnt) { |
| 283 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 284 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
| 285 | } else { |
| 286 | dst[0] = dstTemp[0]; |
| 287 | } |
| 288 | return conicCnt; |
| 289 | } |
| 290 | |
| 291 | typedef GM INHERITED; |
| 292 | }; |
| 293 | |
| 294 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 295 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 296 | class BezierQuadTestOp : public BezierTestOp { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 297 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 298 | DEFINE_OP_CLASS_ID |
Brian Salomon | 6b316e9 | 2016-12-16 09:35:49 -0500 | [diff] [blame] | 299 | const char* name() const override { return "BezierQuadTestOp"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 300 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 301 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 302 | sk_sp<const GrGeometryProcessor> gp, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 303 | const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 304 | const SkPMColor4f& color, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 305 | const GrPathUtils::QuadUVMatrix& devToUV) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 306 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 307 | |
| 308 | return pool->allocate<BezierQuadTestOp>(std::move(gp), rect, color, devToUV); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 312 | friend class ::GrOpMemoryPool; // for ctor |
| 313 | |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 314 | BezierQuadTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 315 | const SkPMColor4f& color, const GrPathUtils::QuadUVMatrix& devToUV) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 316 | : INHERITED(std::move(gp), rect, color, ClassID()), fDevToUV(devToUV) {} |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 317 | |
| 318 | struct Vertex { |
| 319 | SkPoint fPosition; |
| 320 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 321 | }; |
| 322 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 323 | void onPrepareDraws(Target* target) override { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 324 | SkASSERT(this->gp()->vertexStride() == sizeof(Vertex)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 325 | QuadHelper helper(target, sizeof(Vertex), 1); |
| 326 | Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 327 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 328 | return; |
| 329 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 330 | SkRect rect = this->rect(); |
Brian Salomon | ec42e15 | 2018-05-18 12:52:22 -0400 | [diff] [blame] | 331 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex)); |
Brian Osman | 568bec7 | 2018-12-26 16:48:25 -0500 | [diff] [blame] | 332 | fDevToUV.apply(verts, 4, sizeof(Vertex), sizeof(SkPoint)); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 333 | auto pipe = this->makePipeline(target); |
| 334 | helper.recordDraw(target, this->gp(), pipe.fPipeline, pipe.fFixedDynamicState); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 337 | GrPathUtils::QuadUVMatrix fDevToUV; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 338 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 339 | static constexpr int kVertsPerCubic = 4; |
| 340 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 341 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 342 | typedef BezierTestOp INHERITED; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 343 | }; |
| 344 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 345 | /** |
| 346 | * This GM directly exercises effects that draw Bezier quad curves in the GPU backend. |
| 347 | */ |
| 348 | class BezierQuadEffects : public GM { |
| 349 | public: |
| 350 | BezierQuadEffects() { |
| 351 | this->setBGColor(0xFFFFFFFF); |
| 352 | } |
| 353 | |
| 354 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 355 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 356 | return SkString("bezier_quad_effects"); |
| 357 | } |
| 358 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 359 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 360 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 361 | } |
| 362 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 363 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 364 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 365 | GrRenderTargetContext* renderTargetContext = |
| 366 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 367 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 368 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 369 | return; |
| 370 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 371 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 372 | GrContext* context = canvas->getGrContext(); |
| 373 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 377 | struct Vertex { |
| 378 | SkPoint fPosition; |
| 379 | float fUV[4]; // The last two values are ignored. The effect expects a vec4f. |
| 380 | }; |
| 381 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 382 | constexpr int kNumQuads = 5; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 383 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 384 | |
| 385 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3))); |
| 386 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 387 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 388 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 389 | int row = 0; |
| 390 | int col = 0; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 391 | SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(0xff000000); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 392 | |
| 393 | for (int i = 0; i < kNumQuads; ++i) { |
| 394 | SkPoint baseControlPts[] = { |
| 395 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 396 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 397 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 398 | }; |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 399 | for(int edgeType = 0; edgeType < kGrClipEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 400 | sk_sp<GrGeometryProcessor> gp; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 401 | GrClipEdgeType et = (GrClipEdgeType)edgeType; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 402 | gp = GrQuadEffect::Make(color, SkMatrix::I(), et, *context->contextPriv().caps(), |
| 403 | SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 404 | if (!gp) { |
| 405 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 408 | SkScalar x = col * w; |
| 409 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 410 | SkPoint controlPts[] = { |
| 411 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 412 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 413 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 414 | }; |
| 415 | SkPoint chopped[5]; |
| 416 | int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped); |
| 417 | |
| 418 | SkPaint ctrlPtPaint; |
| 419 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 420 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 421 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | SkPaint polyPaint; |
| 425 | polyPaint.setColor(0xffA0A0A0); |
| 426 | polyPaint.setStrokeWidth(0); |
| 427 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 428 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 429 | |
| 430 | SkPaint choppedPtPaint; |
| 431 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 432 | |
| 433 | for (int c = 0; c < cnt; ++c) { |
| 434 | SkPoint* pts = chopped + 2 * c; |
| 435 | |
| 436 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 437 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | SkRect bounds; |
| 441 | bounds.set(pts, 3); |
| 442 | |
| 443 | SkPaint boundsPaint; |
| 444 | boundsPaint.setColor(0xff808080); |
| 445 | boundsPaint.setStrokeWidth(0); |
| 446 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 447 | canvas->drawRect(bounds, boundsPaint); |
| 448 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 449 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 450 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
joshualitt | 3f284d7 | 2015-02-11 11:34:58 -0800 | [diff] [blame] | 451 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 452 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 453 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 454 | std::unique_ptr<GrDrawOp> op = BezierQuadTestOp::Make(context, gp, |
| 455 | bounds, color, DevToUV); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 456 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 457 | } |
| 458 | ++col; |
| 459 | if (numCols == col) { |
| 460 | col = 0; |
| 461 | ++row; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | private: |
| 468 | typedef GM INHERITED; |
| 469 | }; |
| 470 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 471 | DEF_GM(return new BezierConicEffects;) |
| 472 | DEF_GM(return new BezierQuadEffects;) |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 473 | } |