commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | // This test only works with the GPU backend. |
| 9 | |
| 10 | #include "gm.h" |
Chris Dalton | 1d4af54 | 2018-04-16 14:23:00 -0600 | [diff] [blame] | 11 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 14 | #include "GrMemoryPool.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 15 | #include "GrOpFlushState.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 16 | #include "GrPathUtils.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 17 | #include "GrRenderTargetContextPriv.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 18 | #include "GrTest.h" |
| 19 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 20 | #include "SkGeometry.h" |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 21 | #include "SkPoint3.h" |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 22 | #include "SkPointPriv.h" |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 23 | #include "effects/GrBezierEffect.h" |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 24 | #include "ops/GrMeshDrawOp.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 25 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 26 | namespace skiagm { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 27 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 28 | class BezierTestOp : public GrMeshDrawOp { |
| 29 | public: |
| 30 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 31 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 32 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 33 | auto analysis = fProcessorSet.finalize(fColor, GrProcessorAnalysisCoverage::kSingleChannel, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 34 | clip, false, caps, &fColor); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 35 | return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo; |
| 36 | } |
| 37 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 38 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 39 | fProcessorSet.visitProxies(func); |
| 40 | } |
| 41 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 42 | protected: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 43 | BezierTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, GrColor color, |
| 44 | int32_t classID) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 45 | : INHERITED(classID) |
| 46 | , fRect(rect) |
| 47 | , fColor(color) |
| 48 | , fGeometryProcessor(std::move(gp)) |
| 49 | , fProcessorSet(SkBlendMode::kSrc) { |
| 50 | this->setBounds(rect, HasAABloat::kYes, IsZeroArea::kNo); |
| 51 | } |
| 52 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 53 | Target::PipelineAndFixedDynamicState makePipeline(Target* target) { |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 54 | return target->makePipeline(0, std::move(fProcessorSet), target->detachAppliedClip()); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 57 | sk_sp<const GrGeometryProcessor> gp() const { return fGeometryProcessor; } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 58 | |
| 59 | const SkRect& rect() const { return fRect; } |
| 60 | GrColor color() const { return fColor; } |
| 61 | |
| 62 | private: |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 63 | SkRect fRect; |
| 64 | GrColor fColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 65 | sk_sp<const GrGeometryProcessor> fGeometryProcessor; |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 66 | GrProcessorSet fProcessorSet; |
| 67 | |
| 68 | typedef GrMeshDrawOp INHERITED; |
| 69 | }; |
| 70 | |
| 71 | class BezierCubicTestOp : public BezierTestOp { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 72 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 73 | DEFINE_OP_CLASS_ID |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 74 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 75 | const char* name() const override { return "BezierCubicTestOp"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 76 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 77 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 78 | sk_sp<const GrGeometryProcessor> gp, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 79 | const SkRect& rect, |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 80 | GrColor color) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 81 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 82 | |
| 83 | return pool->allocate<BezierCubicTestOp>(std::move(gp), rect, color); |
Brian Salomon | 6b316e9 | 2016-12-16 09:35:49 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 87 | friend class ::GrOpMemoryPool; // for ctor |
| 88 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 89 | BezierCubicTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, GrColor color) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 90 | : INHERITED(std::move(gp), rect, color, ClassID()) {} |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 91 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 92 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 93 | SkASSERT(this->gp()->debugOnly_vertexStride() == sizeof(SkPoint)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 94 | QuadHelper helper(target, sizeof(SkPoint), 1); |
| 95 | SkPoint* pts = reinterpret_cast<SkPoint*>(helper.vertices()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 96 | if (!pts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 97 | return; |
| 98 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 99 | SkRect rect = this->rect(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 100 | SkPointPriv::SetRectTriStrip(pts, rect, sizeof(SkPoint)); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 101 | auto pipe = this->makePipeline(target); |
| 102 | helper.recordDraw(target, this->gp(), pipe.fPipeline, pipe.fFixedDynamicState); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 103 | } |
| 104 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 105 | static constexpr int kVertsPerCubic = 4; |
| 106 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 107 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 108 | typedef BezierTestOp INHERITED; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 111 | /** |
| 112 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 113 | */ |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 114 | class BezierCubicEffects : public GM { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 115 | public: |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 116 | BezierCubicEffects() { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 117 | this->setBGColor(0xFFFFFFFF); |
| 118 | } |
| 119 | |
| 120 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 121 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 122 | return SkString("bezier_cubic_effects"); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 125 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 126 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 127 | } |
| 128 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 129 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 130 | GrRenderTargetContext* renderTargetContext = |
| 131 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 132 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 133 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 134 | return; |
| 135 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 136 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 137 | GrContext* context = canvas->getGrContext(); |
| 138 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 142 | if (!context->contextPriv().caps()->shaderCaps()->floatIs32Bits()) { |
Chris Dalton | 1d4af54 | 2018-04-16 14:23:00 -0600 | [diff] [blame] | 143 | SkPaint paint; |
| 144 | sk_tool_utils::set_portable_typeface(&paint); |
| 145 | paint.setAntiAlias(true); |
| 146 | paint.setTextSize(20); |
| 147 | |
| 148 | canvas->clear(SK_ColorWHITE); |
| 149 | canvas->drawString("float != fp32", 20, 40, paint); |
| 150 | return; |
| 151 | } |
| 152 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 153 | struct Vertex { |
| 154 | SkPoint fPosition; |
| 155 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 156 | }; |
| 157 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 158 | constexpr int kNumCubics = 15; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 159 | SkRandom rand; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 160 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 161 | // Mult by 3 for each edge effect type |
| 162 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3))); |
| 163 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 164 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 165 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 166 | int row = 0; |
| 167 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 168 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 169 | |
| 170 | for (int i = 0; i < kNumCubics; ++i) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 171 | SkPoint baseControlPts[] = { |
| 172 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 173 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 174 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 175 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 176 | }; |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 177 | for(GrClipEdgeType edgeType : {GrClipEdgeType::kFillBW, |
| 178 | GrClipEdgeType::kFillAA, |
| 179 | GrClipEdgeType::kHairlineAA}) { |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 180 | SkScalar x = col * w; |
| 181 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 182 | SkPoint controlPts[] = { |
| 183 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 184 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 185 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY}, |
| 186 | {x + baseControlPts[3].fX, y + baseControlPts[3].fY} |
| 187 | }; |
| 188 | SkPoint chopped[10]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 189 | SkMatrix klm; |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 190 | int loopIndex; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 191 | int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts, |
| 192 | chopped, |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 193 | &klm, |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 194 | &loopIndex); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 195 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 196 | SkPaint ctrlPtPaint; |
| 197 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 198 | canvas->drawCircle(controlPts[0], 8.f, ctrlPtPaint); |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 199 | for (int i = 1; i < 4; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 200 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 201 | } |
| 202 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 203 | SkPaint polyPaint; |
| 204 | polyPaint.setColor(0xffA0A0A0); |
| 205 | polyPaint.setStrokeWidth(0); |
| 206 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 207 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 208 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 209 | SkPaint choppedPtPaint; |
| 210 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 211 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 212 | for (int c = 0; c < cnt; ++c) { |
| 213 | SkPoint* pts = chopped + 3 * c; |
| 214 | |
| 215 | for (int i = 0; i < 4; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 216 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | SkRect bounds; |
| 220 | bounds.set(pts, 4); |
| 221 | |
| 222 | SkPaint boundsPaint; |
| 223 | boundsPaint.setColor(0xff808080); |
| 224 | boundsPaint.setStrokeWidth(0); |
| 225 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 226 | canvas->drawRect(bounds, boundsPaint); |
| 227 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 228 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 229 | bool flipKL = (c == loopIndex && cnt != 3); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 230 | sk_sp<GrGeometryProcessor> gp = |
| 231 | GrCubicEffect::Make(color, SkMatrix::I(), klm, flipKL, edgeType, |
| 232 | *context->contextPriv().caps()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 233 | if (!gp) { |
| 234 | break; |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 237 | std::unique_ptr<GrDrawOp> op = |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 238 | BezierCubicTestOp::Make(context, std::move(gp), bounds, color); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 239 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 240 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 241 | ++col; |
| 242 | if (numCols == col) { |
| 243 | col = 0; |
| 244 | ++row; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 245 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | private: |
| 251 | typedef GM INHERITED; |
| 252 | }; |
| 253 | |
| 254 | ////////////////////////////////////////////////////////////////////////////// |
| 255 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 256 | class BezierConicTestOp : public BezierTestOp { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 257 | public: |
| 258 | DEFINE_OP_CLASS_ID |
| 259 | |
| 260 | const char* name() const override { return "BezierConicTestOp"; } |
| 261 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 262 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 263 | sk_sp<const GrGeometryProcessor> gp, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 264 | const SkRect& rect, |
| 265 | GrColor color, |
| 266 | const SkMatrix& klm) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 267 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 268 | |
| 269 | return pool->allocate<BezierConicTestOp>(std::move(gp), rect, color, klm); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 273 | friend class ::GrOpMemoryPool; // for ctor |
| 274 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 275 | BezierConicTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, GrColor color, |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 276 | const SkMatrix& klm) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 277 | : INHERITED(std::move(gp), rect, color, ClassID()), fKLM(klm) {} |
| 278 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 279 | struct Vertex { |
| 280 | SkPoint fPosition; |
| 281 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 282 | }; |
| 283 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 284 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 285 | SkASSERT(this->gp()->debugOnly_vertexStride() == sizeof(Vertex)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 286 | QuadHelper helper(target, sizeof(Vertex), 1); |
| 287 | Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 288 | if (!verts) { |
| 289 | return; |
| 290 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 291 | SkRect rect = this->rect(); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 292 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect.fLeft, rect.fTop, rect.fRight, |
| 293 | rect.fBottom, sizeof(Vertex)); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 294 | for (int v = 0; v < 4; ++v) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 295 | SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f}; |
| 296 | fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 297 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 298 | auto pipe = this->makePipeline(target); |
| 299 | helper.recordDraw(target, this->gp(), pipe.fPipeline, pipe.fFixedDynamicState); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | SkMatrix fKLM; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 303 | |
| 304 | static constexpr int kVertsPerCubic = 4; |
| 305 | static constexpr int kIndicesPerCubic = 6; |
| 306 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 307 | typedef BezierTestOp INHERITED; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 311 | /** |
| 312 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 313 | */ |
| 314 | class BezierConicEffects : public GM { |
| 315 | public: |
| 316 | BezierConicEffects() { |
| 317 | this->setBGColor(0xFFFFFFFF); |
| 318 | } |
| 319 | |
| 320 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 321 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 322 | return SkString("bezier_conic_effects"); |
| 323 | } |
| 324 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 325 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 326 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 327 | } |
| 328 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 329 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 330 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 331 | GrRenderTargetContext* renderTargetContext = |
| 332 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 333 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 334 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 335 | return; |
| 336 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 337 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 338 | GrContext* context = canvas->getGrContext(); |
| 339 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 343 | struct Vertex { |
| 344 | SkPoint fPosition; |
| 345 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 346 | }; |
| 347 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 348 | constexpr int kNumConics = 10; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 349 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 350 | |
| 351 | // Mult by 3 for each edge effect type |
| 352 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumConics*3))); |
| 353 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumConics*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 354 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 355 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 356 | int row = 0; |
| 357 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 358 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 359 | |
| 360 | for (int i = 0; i < kNumConics; ++i) { |
| 361 | SkPoint baseControlPts[] = { |
| 362 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 363 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 364 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 365 | }; |
| 366 | SkScalar weight = rand.nextRangeF(0.f, 2.f); |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 367 | for(int edgeType = 0; edgeType < kGrClipEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 368 | sk_sp<GrGeometryProcessor> gp; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 369 | GrClipEdgeType et = (GrClipEdgeType)edgeType; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 370 | gp = GrConicEffect::Make(color, SkMatrix::I(), et, *context->contextPriv().caps(), |
| 371 | SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 372 | if (!gp) { |
| 373 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 376 | SkScalar x = col * w; |
| 377 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 378 | SkPoint controlPts[] = { |
| 379 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 380 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 381 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 382 | }; |
| 383 | SkConic dst[4]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 384 | SkMatrix klm; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 385 | int cnt = chop_conic(controlPts, dst, weight); |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 386 | GrPathUtils::getConicKLM(controlPts, weight, &klm); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 387 | |
| 388 | SkPaint ctrlPtPaint; |
| 389 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 390 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 391 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | SkPaint polyPaint; |
| 395 | polyPaint.setColor(0xffA0A0A0); |
| 396 | polyPaint.setStrokeWidth(0); |
| 397 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 398 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 399 | |
| 400 | SkPaint choppedPtPaint; |
| 401 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 402 | |
| 403 | for (int c = 0; c < cnt; ++c) { |
| 404 | SkPoint* pts = dst[c].fPts; |
| 405 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 406 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | SkRect bounds; |
| 410 | //SkPoint bPts[] = {{0.f, 0.f}, {800.f, 800.f}}; |
| 411 | //bounds.set(bPts, 2); |
| 412 | bounds.set(pts, 3); |
| 413 | |
| 414 | SkPaint boundsPaint; |
| 415 | boundsPaint.setColor(0xff808080); |
| 416 | boundsPaint.setStrokeWidth(0); |
| 417 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 418 | canvas->drawRect(bounds, boundsPaint); |
| 419 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 420 | std::unique_ptr<GrDrawOp> op = BezierConicTestOp::Make(context, gp, bounds, |
| 421 | color, klm); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 422 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 423 | } |
| 424 | ++col; |
| 425 | if (numCols == col) { |
| 426 | col = 0; |
| 427 | ++row; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | private: |
| 434 | // Uses the max curvature function for quads to estimate |
| 435 | // where to chop the conic. If the max curvature is not |
| 436 | // found along the curve segment it will return 1 and |
| 437 | // dst[0] is the original conic. If it returns 2 the dst[0] |
| 438 | // and dst[1] are the two new conics. |
| 439 | int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
| 440 | SkScalar t = SkFindQuadMaxCurvature(src); |
Chris Dalton | 1d474dd | 2018-07-24 01:08:31 -0600 | [diff] [blame] | 441 | if (t == 0 || t == 1) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 442 | if (dst) { |
| 443 | dst[0].set(src, weight); |
| 444 | } |
| 445 | return 1; |
| 446 | } else { |
| 447 | if (dst) { |
| 448 | SkConic conic; |
| 449 | conic.set(src, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 450 | if (!conic.chopAt(t, dst)) { |
| 451 | dst[0].set(src, weight); |
| 452 | return 1; |
| 453 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 454 | } |
| 455 | return 2; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Calls split_conic on the entire conic and then once more on each subsection. |
| 460 | // Most cases will result in either 1 conic (chop point is not within t range) |
| 461 | // or 3 points (split once and then one subsection is split again). |
| 462 | int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
| 463 | SkConic dstTemp[2]; |
| 464 | int conicCnt = split_conic(src, dstTemp, weight); |
| 465 | if (2 == conicCnt) { |
| 466 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 467 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
| 468 | } else { |
| 469 | dst[0] = dstTemp[0]; |
| 470 | } |
| 471 | return conicCnt; |
| 472 | } |
| 473 | |
| 474 | typedef GM INHERITED; |
| 475 | }; |
| 476 | |
| 477 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 478 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 479 | class BezierQuadTestOp : public BezierTestOp { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 480 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 481 | DEFINE_OP_CLASS_ID |
Brian Salomon | 6b316e9 | 2016-12-16 09:35:49 -0500 | [diff] [blame] | 482 | const char* name() const override { return "BezierQuadTestOp"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 483 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 484 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 485 | sk_sp<const GrGeometryProcessor> gp, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 486 | const SkRect& rect, |
| 487 | GrColor color, |
| 488 | const GrPathUtils::QuadUVMatrix& devToUV) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 489 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 490 | |
| 491 | return pool->allocate<BezierQuadTestOp>(std::move(gp), rect, color, devToUV); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 495 | friend class ::GrOpMemoryPool; // for ctor |
| 496 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 497 | BezierQuadTestOp(sk_sp<const GrGeometryProcessor> gp, const SkRect& rect, GrColor color, |
Brian Salomon | 6b316e9 | 2016-12-16 09:35:49 -0500 | [diff] [blame] | 498 | const GrPathUtils::QuadUVMatrix& devToUV) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 499 | : INHERITED(std::move(gp), rect, color, ClassID()), fDevToUV(devToUV) {} |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 500 | |
| 501 | struct Vertex { |
| 502 | SkPoint fPosition; |
| 503 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 504 | }; |
| 505 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 506 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 507 | SkASSERT(this->gp()->debugOnly_vertexStride() == sizeof(Vertex)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 508 | QuadHelper helper(target, sizeof(Vertex), 1); |
| 509 | Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 510 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 511 | return; |
| 512 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 513 | SkRect rect = this->rect(); |
Brian Salomon | ec42e15 | 2018-05-18 12:52:22 -0400 | [diff] [blame] | 514 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 515 | fDevToUV.apply<4, sizeof(Vertex), sizeof(SkPoint)>(verts); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 516 | auto pipe = this->makePipeline(target); |
| 517 | helper.recordDraw(target, this->gp(), pipe.fPipeline, pipe.fFixedDynamicState); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Brian Salomon | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 520 | GrPathUtils::QuadUVMatrix fDevToUV; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 521 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 522 | static constexpr int kVertsPerCubic = 4; |
| 523 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 524 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 525 | typedef BezierTestOp INHERITED; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 526 | }; |
| 527 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 528 | /** |
| 529 | * This GM directly exercises effects that draw Bezier quad curves in the GPU backend. |
| 530 | */ |
| 531 | class BezierQuadEffects : public GM { |
| 532 | public: |
| 533 | BezierQuadEffects() { |
| 534 | this->setBGColor(0xFFFFFFFF); |
| 535 | } |
| 536 | |
| 537 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 538 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 539 | return SkString("bezier_quad_effects"); |
| 540 | } |
| 541 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 542 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 543 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 544 | } |
| 545 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 546 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 547 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 548 | GrRenderTargetContext* renderTargetContext = |
| 549 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 550 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 551 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 552 | return; |
| 553 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 554 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 555 | GrContext* context = canvas->getGrContext(); |
| 556 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 557 | return; |
| 558 | } |
| 559 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 560 | struct Vertex { |
| 561 | SkPoint fPosition; |
| 562 | float fUV[4]; // The last two values are ignored. The effect expects a vec4f. |
| 563 | }; |
| 564 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 565 | constexpr int kNumQuads = 5; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 566 | SkRandom rand; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 567 | |
| 568 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumQuads*3))); |
| 569 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumQuads*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 570 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 571 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 572 | int row = 0; |
| 573 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 574 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 575 | |
| 576 | for (int i = 0; i < kNumQuads; ++i) { |
| 577 | SkPoint baseControlPts[] = { |
| 578 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 579 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)}, |
| 580 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
| 581 | }; |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 582 | for(int edgeType = 0; edgeType < kGrClipEdgeTypeCnt; ++edgeType) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 583 | sk_sp<GrGeometryProcessor> gp; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 584 | GrClipEdgeType et = (GrClipEdgeType)edgeType; |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 585 | gp = GrQuadEffect::Make(color, SkMatrix::I(), et, *context->contextPriv().caps(), |
| 586 | SkMatrix::I(), false); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 587 | if (!gp) { |
| 588 | continue; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 591 | SkScalar x = col * w; |
| 592 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 593 | SkPoint controlPts[] = { |
| 594 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 595 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 596 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY} |
| 597 | }; |
| 598 | SkPoint chopped[5]; |
| 599 | int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped); |
| 600 | |
| 601 | SkPaint ctrlPtPaint; |
| 602 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
| 603 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 604 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | SkPaint polyPaint; |
| 608 | polyPaint.setColor(0xffA0A0A0); |
| 609 | polyPaint.setStrokeWidth(0); |
| 610 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 611 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint); |
| 612 | |
| 613 | SkPaint choppedPtPaint; |
| 614 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
| 615 | |
| 616 | for (int c = 0; c < cnt; ++c) { |
| 617 | SkPoint* pts = chopped + 2 * c; |
| 618 | |
| 619 | for (int i = 0; i < 3; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 620 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | SkRect bounds; |
| 624 | bounds.set(pts, 3); |
| 625 | |
| 626 | SkPaint boundsPaint; |
| 627 | boundsPaint.setColor(0xff808080); |
| 628 | boundsPaint.setStrokeWidth(0); |
| 629 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 630 | canvas->drawRect(bounds, boundsPaint); |
| 631 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 632 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 633 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
joshualitt | 3f284d7 | 2015-02-11 11:34:58 -0800 | [diff] [blame] | 634 | |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 635 | GrPathUtils::QuadUVMatrix DevToUV(pts); |
| 636 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 637 | std::unique_ptr<GrDrawOp> op = BezierQuadTestOp::Make(context, gp, |
| 638 | bounds, color, DevToUV); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 639 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 640 | } |
| 641 | ++col; |
| 642 | if (numCols == col) { |
| 643 | col = 0; |
| 644 | ++row; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | private: |
| 651 | typedef GM INHERITED; |
| 652 | }; |
| 653 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 654 | DEF_GM(return new BezierCubicEffects;) |
| 655 | DEF_GM(return new BezierConicEffects;) |
| 656 | DEF_GM(return new BezierQuadEffects;) |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 657 | } |