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