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