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