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 | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 32 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 33 | GrPixelConfigIsClamped dstIsClamped) override { |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 34 | auto analysis = fProcessorSet.finalize(fColor, GrProcessorAnalysisCoverage::kSingleChannel, |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 35 | clip, false, caps, dstIsClamped, &fColor); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 36 | return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo; |
| 37 | } |
| 38 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 39 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 40 | fProcessorSet.visitProxies(func); |
| 41 | } |
| 42 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 43 | protected: |
| 44 | BezierTestOp(sk_sp<GrGeometryProcessor> gp, const SkRect& rect, GrColor color, int32_t classID) |
| 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 | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 53 | const GrPipeline* 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 | |
| 57 | const GrGeometryProcessor* gp() const { return fGeometryProcessor.get(); } |
| 58 | |
| 59 | const SkRect& rect() const { return fRect; } |
| 60 | GrColor color() const { return fColor; } |
| 61 | |
| 62 | private: |
| 63 | bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; } |
| 64 | |
| 65 | SkRect fRect; |
| 66 | GrColor fColor; |
| 67 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
| 68 | GrProcessorSet fProcessorSet; |
| 69 | |
| 70 | typedef GrMeshDrawOp INHERITED; |
| 71 | }; |
| 72 | |
| 73 | class BezierCubicTestOp : public BezierTestOp { |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 74 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 75 | DEFINE_OP_CLASS_ID |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 76 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 77 | const char* name() const override { return "BezierCubicTestOp"; } |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 78 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 79 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 80 | sk_sp<GrGeometryProcessor> gp, |
| 81 | const SkRect& rect, |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 82 | GrColor color) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 83 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 84 | |
| 85 | return pool->allocate<BezierCubicTestOp>(std::move(gp), rect, color); |
Brian Salomon | 6b316e9 | 2016-12-16 09:35:49 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 89 | friend class ::GrOpMemoryPool; // for ctor |
| 90 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 91 | BezierCubicTestOp(sk_sp<GrGeometryProcessor> gp, const SkRect& rect, GrColor color) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 92 | : INHERITED(std::move(gp), rect, color, ClassID()) {} |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 93 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 94 | void onPrepareDraws(Target* target) override { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 95 | QuadHelper helper; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 96 | SkASSERT(this->gp()->debugOnly_vertexStride() == sizeof(SkPoint)); |
| 97 | SkPoint* pts = reinterpret_cast<SkPoint*>(helper.init(target, sizeof(SkPoint), 1)); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 98 | if (!pts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 99 | return; |
| 100 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 101 | SkRect rect = this->rect(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 102 | SkPointPriv::SetRectTriStrip(pts, rect, sizeof(SkPoint)); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 103 | helper.recordDraw(target, this->gp(), this->makePipeline(target)); |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 104 | } |
| 105 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 106 | static constexpr int kVertsPerCubic = 4; |
| 107 | static constexpr int kIndicesPerCubic = 6; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 108 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 109 | typedef BezierTestOp INHERITED; |
joshualitt | 95964c6 | 2015-02-11 13:45:50 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 112 | /** |
| 113 | * This GM directly exercises effects that draw Bezier curves in the GPU backend. |
| 114 | */ |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 115 | class BezierCubicEffects : public GM { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 116 | public: |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 117 | BezierCubicEffects() { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 118 | this->setBGColor(0xFFFFFFFF); |
| 119 | } |
| 120 | |
| 121 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 122 | SkString onShortName() override { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 123 | return SkString("bezier_cubic_effects"); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 124 | } |
| 125 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 127 | return SkISize::Make(800, 800); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 128 | } |
| 129 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 131 | GrRenderTargetContext* renderTargetContext = |
| 132 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 133 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 134 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 137 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 138 | GrContext* context = canvas->getGrContext(); |
| 139 | if (!context) { |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 143 | if (!context->contextPriv().caps()->shaderCaps()->floatIs32Bits()) { |
Chris Dalton | 1d4af54 | 2018-04-16 14:23:00 -0600 | [diff] [blame] | 144 | SkPaint paint; |
| 145 | sk_tool_utils::set_portable_typeface(&paint); |
| 146 | paint.setAntiAlias(true); |
| 147 | paint.setTextSize(20); |
| 148 | |
| 149 | canvas->clear(SK_ColorWHITE); |
| 150 | canvas->drawString("float != fp32", 20, 40, paint); |
| 151 | return; |
| 152 | } |
| 153 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 154 | struct Vertex { |
| 155 | SkPoint fPosition; |
| 156 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 157 | }; |
| 158 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 159 | constexpr int kNumCubics = 15; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 160 | SkRandom rand; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 161 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 162 | // Mult by 3 for each edge effect type |
| 163 | int numCols = SkScalarCeilToInt(SkScalarSqrt(SkIntToScalar(kNumCubics*3))); |
| 164 | int numRows = SkScalarCeilToInt(SkIntToScalar(kNumCubics*3) / numCols); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 165 | SkScalar w = SkIntToScalar(renderTargetContext->width()) / numCols; |
| 166 | SkScalar h = SkIntToScalar(renderTargetContext->height()) / numRows; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 167 | int row = 0; |
| 168 | int col = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 169 | constexpr GrColor color = 0xff000000; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 170 | |
| 171 | for (int i = 0; i < kNumCubics; ++i) { |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 172 | SkPoint baseControlPts[] = { |
| 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)}, |
| 176 | {rand.nextRangeF(0.f, w), rand.nextRangeF(0.f, h)} |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 177 | }; |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 178 | for(GrClipEdgeType edgeType : {GrClipEdgeType::kFillBW, |
| 179 | GrClipEdgeType::kFillAA, |
| 180 | GrClipEdgeType::kHairlineAA}) { |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 181 | SkScalar x = col * w; |
| 182 | SkScalar y = row * h; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 183 | SkPoint controlPts[] = { |
| 184 | {x + baseControlPts[0].fX, y + baseControlPts[0].fY}, |
| 185 | {x + baseControlPts[1].fX, y + baseControlPts[1].fY}, |
| 186 | {x + baseControlPts[2].fX, y + baseControlPts[2].fY}, |
| 187 | {x + baseControlPts[3].fX, y + baseControlPts[3].fY} |
| 188 | }; |
| 189 | SkPoint chopped[10]; |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 190 | SkMatrix klm; |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 191 | int loopIndex; |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 192 | int cnt = GrPathUtils::chopCubicAtLoopIntersection(controlPts, |
| 193 | chopped, |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 194 | &klm, |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 195 | &loopIndex); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 196 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 197 | SkPaint ctrlPtPaint; |
| 198 | ctrlPtPaint.setColor(rand.nextU() | 0xFF000000); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 199 | canvas->drawCircle(controlPts[0], 8.f, ctrlPtPaint); |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 200 | for (int i = 1; i < 4; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 201 | canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 202 | } |
| 203 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 204 | SkPaint polyPaint; |
| 205 | polyPaint.setColor(0xffA0A0A0); |
| 206 | polyPaint.setStrokeWidth(0); |
| 207 | polyPaint.setStyle(SkPaint::kStroke_Style); |
| 208 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, controlPts, polyPaint); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 209 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 210 | SkPaint choppedPtPaint; |
| 211 | choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 212 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 213 | for (int c = 0; c < cnt; ++c) { |
| 214 | SkPoint* pts = chopped + 3 * c; |
| 215 | |
| 216 | for (int i = 0; i < 4; ++i) { |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 217 | canvas->drawCircle(pts[i], 3.f, choppedPtPaint); |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | SkRect bounds; |
| 221 | bounds.set(pts, 4); |
| 222 | |
| 223 | SkPaint boundsPaint; |
| 224 | boundsPaint.setColor(0xff808080); |
| 225 | boundsPaint.setStrokeWidth(0); |
| 226 | boundsPaint.setStyle(SkPaint::kStroke_Style); |
| 227 | canvas->drawRect(bounds, boundsPaint); |
| 228 | |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 229 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 230 | bool flipKL = (c == loopIndex && cnt != 3); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 231 | sk_sp<GrGeometryProcessor> gp = |
| 232 | GrCubicEffect::Make(color, SkMatrix::I(), klm, flipKL, edgeType, |
| 233 | *context->contextPriv().caps()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 234 | if (!gp) { |
| 235 | break; |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 238 | std::unique_ptr<GrDrawOp> op = |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 239 | BezierCubicTestOp::Make(context, std::move(gp), bounds, color); |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 240 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 241 | } |
commit-bot@chromium.org | 53a0b6c | 2013-08-23 18:05:01 +0000 | [diff] [blame] | 242 | ++col; |
| 243 | if (numCols == col) { |
| 244 | col = 0; |
| 245 | ++row; |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 246 | } |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private: |
| 252 | typedef GM INHERITED; |
| 253 | }; |
| 254 | |
| 255 | ////////////////////////////////////////////////////////////////////////////// |
| 256 | |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 257 | class BezierConicTestOp : public BezierTestOp { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 258 | public: |
| 259 | DEFINE_OP_CLASS_ID |
| 260 | |
| 261 | const char* name() const override { return "BezierConicTestOp"; } |
| 262 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 263 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 264 | sk_sp<GrGeometryProcessor> gp, |
| 265 | const SkRect& rect, |
| 266 | GrColor color, |
| 267 | const SkMatrix& klm) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 268 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 269 | |
| 270 | return pool->allocate<BezierConicTestOp>(std::move(gp), rect, color, klm); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 274 | friend class ::GrOpMemoryPool; // for ctor |
| 275 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 276 | BezierConicTestOp(sk_sp<GrGeometryProcessor> gp, const SkRect& rect, GrColor color, |
| 277 | const SkMatrix& klm) |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 278 | : INHERITED(std::move(gp), rect, color, ClassID()), fKLM(klm) {} |
| 279 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 280 | struct Vertex { |
| 281 | SkPoint fPosition; |
| 282 | float fKLM[4]; // The last value is ignored. The effect expects a vec4f. |
| 283 | }; |
| 284 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 285 | void onPrepareDraws(Target* target) override { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 286 | QuadHelper helper; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 287 | SkASSERT(this->gp()->debugOnly_vertexStride() == sizeof(Vertex)); |
| 288 | Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, sizeof(Vertex), 1)); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 289 | if (!verts) { |
| 290 | return; |
| 291 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 292 | SkRect rect = this->rect(); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 293 | SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect.fLeft, rect.fTop, rect.fRight, |
| 294 | rect.fBottom, sizeof(Vertex)); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 295 | for (int v = 0; v < 4; ++v) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 296 | SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f}; |
| 297 | fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 298 | } |
Brian Salomon | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 299 | helper.recordDraw(target, this->gp(), this->makePipeline(target)); |
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); |
| 441 | if (t == 0) { |
| 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, |
| 485 | sk_sp<GrGeometryProcessor> gp, |
| 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 | 9e50f7b | 2017-03-06 12:02:34 -0500 | [diff] [blame] | 497 | BezierQuadTestOp(sk_sp<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 { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 507 | QuadHelper helper; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 508 | SkASSERT(this->gp()->debugOnly_vertexStride() == sizeof(Vertex)); |
| 509 | Vertex* verts = reinterpret_cast<Vertex*>(helper.init(target, sizeof(Vertex), 1)); |
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 | 477d0ef | 2017-07-14 10:12:26 -0400 | [diff] [blame] | 516 | helper.recordDraw(target, this->gp(), this->makePipeline(target)); |
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 | } |