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" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 49 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 50 | |
| 51 | #include <memory> |
| 52 | #include <utility> |
| 53 | |
| 54 | class GrAppliedClip; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 56 | namespace skiagm { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 57 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 58 | class BezierTestOp : public GrMeshDrawOp { |
| 59 | public: |
| 60 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 61 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 62 | GrProcessorSet::Analysis finalize( |
| 63 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 64 | GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 65 | return fProcessorSet.finalize( |
| 66 | fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 67 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType, &fColor); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 70 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 71 | fProcessorSet.visitProxies(func); |
| 72 | } |
| 73 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 74 | protected: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 75 | BezierTestOp(GrClipEdgeType et, const SkRect& rect, const SkPMColor4f& color, int32_t classID) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 76 | : INHERITED(classID) |
| 77 | , fRect(rect) |
| 78 | , fColor(color) |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 79 | , fEdgeType(et) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 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 { |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 85 | auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, |
| 86 | std::move(fProcessorSet), |
| 87 | GrPipeline::InputFlags::kNone); |
| 88 | |
| 89 | flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 92 | GrClipEdgeType edgeType() const { return fEdgeType; } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 93 | |
| 94 | const SkRect& rect() const { return fRect; } |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 95 | const SkPMColor4f& color() const { return fColor; } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 96 | |
| 97 | private: |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 98 | SkRect fRect; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 99 | SkPMColor4f fColor; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 100 | GrClipEdgeType fEdgeType; |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 101 | GrProcessorSet fProcessorSet; |
| 102 | |
| 103 | typedef GrMeshDrawOp INHERITED; |
| 104 | }; |
| 105 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 106 | /** |
| 107 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 108 | */ |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 109 | class BezierConicTestOp : public BezierTestOp { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 110 | public: |
| 111 | DEFINE_OP_CLASS_ID |
| 112 | |
| 113 | const char* name() const override { return "BezierConicTestOp"; } |
| 114 | |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 115 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 116 | GrClipEdgeType et, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 117 | const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 118 | const SkPMColor4f& color, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 119 | const SkMatrix& klm) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 120 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 121 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 122 | return pool->allocate<BezierConicTestOp>(et, rect, color, klm); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 126 | friend class ::GrOpMemoryPool; // for ctor |
| 127 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 128 | BezierConicTestOp(GrClipEdgeType et, const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 129 | const SkPMColor4f& color, const SkMatrix& klm) |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 130 | : INHERITED(et, rect, color, ClassID()), fKLM(klm) {} |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 131 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 132 | struct Vertex { |
| 133 | SkPoint fPosition; |
| 134 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 135 | }; |
| 136 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 137 | void onPrepareDraws(Target* target) override { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 138 | GrGeometryProcessor* gp = GrConicEffect::Make(target->allocator(), this->color(), |
| 139 | SkMatrix::I(), this->edgeType(), |
| 140 | target->caps(), SkMatrix::I(), false); |
| 141 | if (!gp) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | SkASSERT(gp->vertexStride() == sizeof(Vertex)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 146 | QuadHelper helper(target, sizeof(Vertex), 1); |
| 147 | Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 148 | if (!verts) { |
| 149 | return; |
| 150 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 151 | SkRect rect = this->rect(); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 152 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect.fLeft, rect.fTop, rect.fRight, |
| 153 | rect.fBottom, sizeof(Vertex)); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 154 | for (int v = 0; v < 4; ++v) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 155 | SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f}; |
| 156 | fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 157 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 158 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 159 | helper.recordDraw(target, gp); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | SkMatrix fKLM; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 163 | |
| 164 | static constexpr int kVertsPerCubic = 4; |
| 165 | static constexpr int kIndicesPerCubic = 6; |
| 166 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 167 | typedef BezierTestOp INHERITED; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 171 | /** |
| 172 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 173 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 174 | class BezierConicEffects : public GpuGM { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 175 | public: |
| 176 | BezierConicEffects() { |
| 177 | this->setBGColor(0xFFFFFFFF); |
| 178 | } |
| 179 | |
| 180 | protected: |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 181 | static const int kNumConics = 10; |
| 182 | static const int kCellWidth = 128; |
| 183 | static const int kCellHeight = 128; |
| 184 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 185 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 186 | return SkString("bezier_conic_effects"); |
| 187 | } |
| 188 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 189 | SkISize onISize() override { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 190 | return SkISize::Make(kGrClipEdgeTypeCnt*kCellWidth, kNumConics*kCellHeight); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 193 | void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 194 | SkCanvas* canvas) override { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 195 | |
| 196 | const SkScalar w = kCellWidth, h = kCellHeight; |
| 197 | const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000); |
| 198 | |
| 199 | const SkPoint baseControlPts[kNumConics][3] = { |
| 200 | { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } }, |
| 201 | { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } }, |
| 202 | { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } }, |
| 203 | { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } }, |
| 204 | { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } }, |
| 205 | { { 0.96f * w, 0.65f * h}, { 0.03f * w, 0.79f * h }, { 0.24f * w, 0.56f * h } }, |
| 206 | { { 0.57f * w, 0.12f * h}, { 0.33f * w, 0.67f * h }, { 0.59f * w, 0.33f * h } }, |
| 207 | { { 0.12f * w, 0.72f * h}, { 0.69f * w, 0.85f * h }, { 0.46f * w, 0.32f * h } }, |
| 208 | { { 0.27f * w, 0.49f * h}, { 0.41f * w, 0.02f * h }, { 0.11f * w, 0.42f * h } }, |
| 209 | { { 0.40f * w, 0.13f * h}, { 0.83f * w, 0.30f * h }, { 0.31f * w, 0.68f * h } }, |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 210 | }; |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 211 | const SkScalar weights[kNumConics] = { 0.62f, 0.01f, 0.95f, 1.48f, 0.37f, |
| 212 | 0.66f, 0.15f, 0.14f, 0.61f, 1.4f }; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 213 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 214 | SkPaint ctrlPtPaint; |
| 215 | ctrlPtPaint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 216 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 217 | SkPaint choppedPtPaint; |
| 218 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 219 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 220 | SkPaint polyPaint; |
| 221 | polyPaint.setColor(0xffA0A0A0); |
| 222 | polyPaint.setStrokeWidth(0); |
| 223 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 224 | |
| 225 | SkPaint boundsPaint; |
| 226 | boundsPaint.setColor(0xff808080); |
| 227 | boundsPaint.setStrokeWidth(0); |
| 228 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 229 | |
| 230 | |
| 231 | for (int row = 0; row < kNumConics; ++row) { |
| 232 | for(int col = 0; col < kGrClipEdgeTypeCnt; ++col) { |
| 233 | GrClipEdgeType et = (GrClipEdgeType) col; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 234 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 235 | SkScalar x = col * w; |
| 236 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 237 | SkPoint controlPts[] = { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 238 | {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY}, |
| 239 | {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY}, |
| 240 | {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY} |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 241 | }; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 242 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 243 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 244 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 245 | } |
| 246 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 247 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 248 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 249 | SkConic dst[4]; |
| 250 | SkMatrix klm; |
| 251 | int cnt = ChopConic(controlPts, dst, weights[row]); |
| 252 | GrPathUtils::getConicKLM(controlPts, weights[row], &klm); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 253 | |
| 254 | for (int c = 0; c < cnt; ++c) { |
| 255 | SkPoint* pts = dst[c].fPts; |
| 256 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 257 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | SkRect bounds; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 261 | bounds.setBounds(pts, 3); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 262 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 263 | canvas->drawRect(bounds, boundsPaint); |
| 264 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 265 | std::unique_ptr<GrDrawOp> op = BezierConicTestOp::Make(context, et, bounds, |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 266 | kOpaqueBlack, klm); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 267 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 268 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 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. |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 279 | static int SplitConic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 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 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 299 | // Calls SplitConic on the entire conic and then once more on each subsection. |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 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). |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 302 | static int ChopConic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 303 | SkConic dstTemp[2]; |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 304 | int conicCnt = SplitConic(src, dstTemp, weight); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 305 | if (2 == conicCnt) { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 306 | int conicCnt2 = SplitConic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 307 | conicCnt = conicCnt2 + SplitConic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 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 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 324 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 325 | GrClipEdgeType et, |
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 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 331 | return pool->allocate<BezierQuadTestOp>(et, 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 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 337 | BezierQuadTestOp(GrClipEdgeType et, const SkRect& rect, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 338 | const SkPMColor4f& color, const GrPathUtils::QuadUVMatrix& devToUV) |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 339 | : INHERITED(et, 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 { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 347 | GrGeometryProcessor* gp = GrQuadEffect::Make(target->allocator(), this->color(), |
| 348 | SkMatrix::I(), this->edgeType(), |
| 349 | target->caps(), SkMatrix::I(), false); |
| 350 | if (!gp) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | SkASSERT(gp->vertexStride() == sizeof(Vertex)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 355 | QuadHelper helper(target, sizeof(Vertex), 1); |
| 356 | Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 357 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 358 | return; |
| 359 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 360 | SkRect rect = this->rect(); |
Brian Salomon | ec42e15 | 2018-05-18 12:52:22 -0400 | [diff] [blame] | 361 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex)); |
Brian Osman | 568bec7 | 2018-12-26 16:48:25 -0500 | [diff] [blame] | 362 | fDevToUV.apply(verts, 4, sizeof(Vertex), sizeof(SkPoint)); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 363 | helper.recordDraw(target, gp); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 366 | GrPathUtils::QuadUVMatrix fDevToUV; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 367 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 368 | static constexpr int kVertsPerCubic = 4; |
| 369 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 370 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 371 | typedef BezierTestOp INHERITED; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 372 | }; |
| 373 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 374 | /** |
| 375 | * This GM directly exercises effects that draw Bezier quad curves in the GPU backend. |
| 376 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 377 | class BezierQuadEffects : public GpuGM { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 378 | public: |
| 379 | BezierQuadEffects() { |
| 380 | this->setBGColor(0xFFFFFFFF); |
| 381 | } |
| 382 | |
| 383 | protected: |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 384 | static const int kNumQuads = 5; |
| 385 | static const int kCellWidth = 128; |
| 386 | static const int kCellHeight = 128; |
| 387 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 388 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 389 | return SkString("bezier_quad_effects"); |
| 390 | } |
| 391 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 392 | SkISize onISize() override { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 393 | return SkISize::Make(kGrClipEdgeTypeCnt*kCellWidth, kNumQuads*kCellHeight); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 396 | void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 397 | SkCanvas* canvas) override { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 398 | |
| 399 | const SkScalar w = kCellWidth, h = kCellHeight; |
| 400 | const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000); |
| 401 | |
| 402 | const SkPoint baseControlPts[kNumQuads][3] = { |
| 403 | { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } }, |
| 404 | { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } }, |
| 405 | { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } }, |
| 406 | { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } }, |
| 407 | { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } }, |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 410 | SkPaint ctrlPtPaint; |
| 411 | ctrlPtPaint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 412 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 413 | SkPaint choppedPtPaint; |
| 414 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 415 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 416 | SkPaint polyPaint; |
| 417 | polyPaint.setColor(0xffA0A0A0); |
| 418 | polyPaint.setStrokeWidth(0); |
| 419 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 420 | |
| 421 | SkPaint boundsPaint; |
| 422 | boundsPaint.setColor(0xff808080); |
| 423 | boundsPaint.setStrokeWidth(0); |
| 424 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 425 | |
| 426 | for (int row = 0; row < kNumQuads; ++row) { |
| 427 | for(int col = 0; col < kGrClipEdgeTypeCnt; ++col) { |
| 428 | GrClipEdgeType et = (GrClipEdgeType) col; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 429 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 430 | SkScalar x = col * w; |
| 431 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 432 | SkPoint controlPts[] = { |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 433 | {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY}, |
| 434 | {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY}, |
| 435 | {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY} |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 436 | }; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 437 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 438 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 439 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 440 | } |
| 441 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 442 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 443 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 444 | SkPoint chopped[5]; |
| 445 | int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 446 | |
| 447 | for (int c = 0; c < cnt; ++c) { |
| 448 | SkPoint* pts = chopped + 2 * c; |
| 449 | |
| 450 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 451 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | SkRect bounds; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 455 | bounds.setBounds(pts, 3); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 456 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 457 | canvas->drawRect(bounds, boundsPaint); |
| 458 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 459 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 460 | |
Robert Phillips | 98f3fd9 | 2019-11-21 13:16:21 -0500 | [diff] [blame] | 461 | std::unique_ptr<GrDrawOp> op = BezierQuadTestOp::Make(context, et, bounds, |
| 462 | kOpaqueBlack, DevToUV); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 463 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 464 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | private: |
| 470 | typedef GM INHERITED; |
| 471 | }; |
| 472 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 473 | DEF_GM(return new BezierConicEffects;) |
| 474 | DEF_GM(return new BezierQuadEffects;) |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 475 | } |