blob: b66e9fc2604dfa3f93f3397e29d3a5d8254ee781 [file] [log] [blame]
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8// This test only works with the GPU backend.
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040011#include "include/core/SkBlendMode.h"
12#include "include/core/SkCanvas.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPoint3.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040017#include "include/core/SkRect.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTypes.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040023#include "include/gpu/GrRecordingContext.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040024#include "include/private/GrSharedEnums.h"
25#include "include/private/GrTypesPriv.h"
26#include "include/private/SkColorData.h"
27#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkGeometry.h"
29#include "src/core/SkPointPriv.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040030#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040031#include "src/gpu/GrDirectContextPriv.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040032#include "src/gpu/GrGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrMemoryPool.h"
34#include "src/gpu/GrOpFlushState.h"
Robert Phillipsce978572020-02-28 11:56:44 -050035#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040036#include "src/gpu/GrPaint.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040037#include "src/gpu/GrProcessorAnalysis.h"
38#include "src/gpu/GrProcessorSet.h"
Robert Phillipsce978572020-02-28 11:56:44 -050039#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrRecordingContextPriv.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040041#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040043#include "src/gpu/GrUserStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/effects/GrBezierEffect.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040045#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040046#include "src/gpu/geometry/GrPathUtils.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040047#include "src/gpu/ops/GrDrawOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/ops/GrMeshDrawOp.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040049#include "src/gpu/ops/GrOp.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050050#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040051
52#include <memory>
53#include <utility>
54
55class GrAppliedClip;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000056
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000057namespace skiagm {
joshualitt95964c62015-02-11 13:45:50 -080058
Brian Salomon477d0ef2017-07-14 10:12:26 -040059class BezierTestOp : public GrMeshDrawOp {
60public:
61 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
62
Chris Dalton6ce447a2019-06-23 18:07:38 -060063 GrProcessorSet::Analysis finalize(
64 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
65 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -070066 return fProcessorSet.finalize(
67 fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
Chris Dalton6ce447a2019-06-23 18:07:38 -060068 &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType, &fColor);
Brian Salomon477d0ef2017-07-14 10:12:26 -040069 }
70
Chris Dalton1706cbf2019-05-21 19:35:29 -060071 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips4f93c572020-03-18 08:13:53 -040072 if (fProgramInfo) {
73 fProgramInfo->visitFPProxies(func);
74 } else {
75 fProcessorSet.visitProxies(func);
76 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040077 }
78
Brian Salomon477d0ef2017-07-14 10:12:26 -040079protected:
John Stiles3b2c06c2020-06-17 12:45:57 -040080 BezierTestOp(const SkRect& rect, const SkPMColor4f& color, int32_t classID)
Brian Salomon477d0ef2017-07-14 10:12:26 -040081 : INHERITED(classID)
82 , fRect(rect)
83 , fColor(color)
Brian Salomon477d0ef2017-07-14 10:12:26 -040084 , fProcessorSet(SkBlendMode::kSrc) {
Greg Daniel5faf4742019-10-01 15:14:44 -040085 this->setBounds(rect, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon477d0ef2017-07-14 10:12:26 -040086 }
87
Robert Phillipsce978572020-02-28 11:56:44 -050088 virtual GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) = 0;
Robert Phillips3968fcb2019-12-05 16:40:31 -050089
Robert Phillips2669a7b2020-03-12 12:07:19 -040090 GrProgramInfo* programInfo() override { return fProgramInfo; }
91
Robert Phillips4133dc42020-03-11 15:55:55 -040092 void onCreateProgramInfo(const GrCaps* caps,
93 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -040094 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -040095 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -040096 const GrXferProcessor::DstProxyView& dstProxyView,
97 GrXferBarrierFlags renderPassXferBarriers) override {
Robert Phillipsce978572020-02-28 11:56:44 -050098 auto gp = this->makeGP(*caps, arena);
99 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400100 return;
Robert Phillipsce978572020-02-28 11:56:44 -0500101 }
102
103 GrPipeline::InputFlags flags = GrPipeline::InputFlags::kNone;
104
Brian Salomon8afde5f2020-04-01 16:22:00 -0400105 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400106 std::move(appliedClip),
107 dstProxyView, gp,
108 std::move(fProcessorSet),
109 GrPrimitiveType::kTriangles,
Greg Danield358cbe2020-09-11 09:33:54 -0400110 renderPassXferBarriers,
Robert Phillips4133dc42020-03-11 15:55:55 -0400111 flags);
Robert Phillipsce978572020-02-28 11:56:44 -0500112 }
113
Robert Phillipsce978572020-02-28 11:56:44 -0500114 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
115 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400116 this->createProgramInfo(flushState);
Robert Phillipsce978572020-02-28 11:56:44 -0500117 }
118
119 if (!fProgramInfo) {
120 return;
121 }
122
Chris Dalton765ed362020-03-16 17:34:44 -0600123 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
124 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
125 flushState->drawMesh(*fMesh);
Brian Salomon477d0ef2017-07-14 10:12:26 -0400126 }
127
Brian Salomon477d0ef2017-07-14 10:12:26 -0400128 const SkRect& rect() const { return fRect; }
Brian Osmancf860852018-10-31 14:04:39 -0400129 const SkPMColor4f& color() const { return fColor; }
Brian Salomon477d0ef2017-07-14 10:12:26 -0400130
Robert Phillipsce978572020-02-28 11:56:44 -0500131protected:
Chris Daltoneb694b72020-03-16 09:25:50 -0600132 GrSimpleMesh* fMesh = nullptr; // filled in by the derived classes
Robert Phillipsce978572020-02-28 11:56:44 -0500133
Brian Salomon477d0ef2017-07-14 10:12:26 -0400134private:
Robert Phillipsce978572020-02-28 11:56:44 -0500135 SkRect fRect;
136 SkPMColor4f fColor;
Robert Phillipsce978572020-02-28 11:56:44 -0500137 GrProcessorSet fProcessorSet;
138 GrProgramInfo* fProgramInfo = nullptr;
Brian Salomon477d0ef2017-07-14 10:12:26 -0400139
John Stiles7571f9e2020-09-02 22:42:33 -0400140 using INHERITED = GrMeshDrawOp;
Brian Salomon477d0ef2017-07-14 10:12:26 -0400141};
142
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000143/**
144 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
145 */
Brian Salomon477d0ef2017-07-14 10:12:26 -0400146class BezierConicTestOp : public BezierTestOp {
Chris Daltonfebbffa2017-06-08 13:12:02 -0600147public:
148 DEFINE_OP_CLASS_ID
149
Robert Phillipsce978572020-02-28 11:56:44 -0500150 const char* name() const final { return "BezierConicTestOp"; }
Chris Daltonfebbffa2017-06-08 13:12:02 -0600151
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500152 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400153 const SkRect& rect,
Brian Osmancf860852018-10-31 14:04:39 -0400154 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400155 const SkMatrix& klm) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500156 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400157
John Stiles3b2c06c2020-06-17 12:45:57 -0400158 return pool->allocate<BezierConicTestOp>(rect, color, klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600159 }
160
161private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400162 friend class ::GrOpMemoryPool; // for ctor
163
John Stiles3b2c06c2020-06-17 12:45:57 -0400164 BezierConicTestOp(const SkRect& rect, const SkPMColor4f& color, const SkMatrix& klm)
165 : INHERITED(rect, color, ClassID())
166 , fKLM(klm) {}
Brian Salomon477d0ef2017-07-14 10:12:26 -0400167
Chris Daltonfebbffa2017-06-08 13:12:02 -0600168 struct Vertex {
169 SkPoint fPosition;
170 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
171 };
172
Robert Phillipsce978572020-02-28 11:56:44 -0500173 GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
John Stiles3b2c06c2020-06-17 12:45:57 -0400174 auto tmp = GrConicEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
175 false);
Robert Phillipsce978572020-02-28 11:56:44 -0500176 if (!tmp) {
177 return nullptr;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500178 }
Robert Phillipsce978572020-02-28 11:56:44 -0500179 SkASSERT(tmp->vertexStride() == sizeof(Vertex));
180 return tmp;
181 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500182
Robert Phillipsce978572020-02-28 11:56:44 -0500183 void onPrepareDraws(Target* target) final {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000184 QuadHelper helper(target, sizeof(Vertex), 1);
185 Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
Chris Daltonfebbffa2017-06-08 13:12:02 -0600186 if (!verts) {
187 return;
188 }
Brian Salomon477d0ef2017-07-14 10:12:26 -0400189 SkRect rect = this->rect();
Robert Phillipsce978572020-02-28 11:56:44 -0500190 SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
Chris Daltonfebbffa2017-06-08 13:12:02 -0600191 for (int v = 0; v < 4; ++v) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400192 SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f};
193 fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600194 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700195
Robert Phillipsce978572020-02-28 11:56:44 -0500196 fMesh = helper.mesh();
Chris Daltonfebbffa2017-06-08 13:12:02 -0600197 }
198
199 SkMatrix fKLM;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600200
201 static constexpr int kVertsPerCubic = 4;
202 static constexpr int kIndicesPerCubic = 6;
203
John Stiles7571f9e2020-09-02 22:42:33 -0400204 using INHERITED = BezierTestOp;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600205};
206
207
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000208/**
209 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
210 */
Chris Dalton3a778372019-02-07 15:23:36 -0700211class BezierConicEffects : public GpuGM {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000212public:
213 BezierConicEffects() {
214 this->setBGColor(0xFFFFFFFF);
215 }
216
217protected:
Robert Phillips98f3fd92019-11-21 13:16:21 -0500218 static const int kNumConics = 10;
219 static const int kCellWidth = 128;
220 static const int kCellHeight = 128;
221
mtklein36352bf2015-03-25 18:17:31 -0700222 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000223 return SkString("bezier_conic_effects");
224 }
225
mtklein36352bf2015-03-25 18:17:31 -0700226 SkISize onISize() override {
John Stiles3b2c06c2020-06-17 12:45:57 -0400227 return SkISize::Make(kCellWidth, kNumConics*kCellHeight);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000228 }
229
Robert Phillips95c250c2020-06-29 15:36:12 -0400230 void onDraw(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Chris Dalton3a778372019-02-07 15:23:36 -0700231 SkCanvas* canvas) override {
Robert Phillips98f3fd92019-11-21 13:16:21 -0500232
233 const SkScalar w = kCellWidth, h = kCellHeight;
234 const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
235
236 const SkPoint baseControlPts[kNumConics][3] = {
237 { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
238 { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
239 { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
240 { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
241 { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
242 { { 0.96f * w, 0.65f * h}, { 0.03f * w, 0.79f * h }, { 0.24f * w, 0.56f * h } },
243 { { 0.57f * w, 0.12f * h}, { 0.33f * w, 0.67f * h }, { 0.59f * w, 0.33f * h } },
244 { { 0.12f * w, 0.72f * h}, { 0.69f * w, 0.85f * h }, { 0.46f * w, 0.32f * h } },
245 { { 0.27f * w, 0.49f * h}, { 0.41f * w, 0.02f * h }, { 0.11f * w, 0.42f * h } },
246 { { 0.40f * w, 0.13f * h}, { 0.83f * w, 0.30f * h }, { 0.31f * w, 0.68f * h } },
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000247 };
Robert Phillips98f3fd92019-11-21 13:16:21 -0500248 const SkScalar weights[kNumConics] = { 0.62f, 0.01f, 0.95f, 1.48f, 0.37f,
249 0.66f, 0.15f, 0.14f, 0.61f, 1.4f };
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000250
Robert Phillips98f3fd92019-11-21 13:16:21 -0500251 SkPaint ctrlPtPaint;
252 ctrlPtPaint.setColor(SK_ColorRED);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000253
Robert Phillips98f3fd92019-11-21 13:16:21 -0500254 SkPaint choppedPtPaint;
255 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000256
Robert Phillips98f3fd92019-11-21 13:16:21 -0500257 SkPaint polyPaint;
258 polyPaint.setColor(0xffA0A0A0);
259 polyPaint.setStrokeWidth(0);
260 polyPaint.setStyle(SkPaint::kStroke_Style);
261
262 SkPaint boundsPaint;
263 boundsPaint.setColor(0xff808080);
264 boundsPaint.setStrokeWidth(0);
265 boundsPaint.setStyle(SkPaint::kStroke_Style);
266
267
268 for (int row = 0; row < kNumConics; ++row) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400269 SkScalar x = 0;
270 SkScalar y = row * h;
271 SkPoint controlPts[] = {
272 {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
273 {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
274 {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
275 };
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000276
John Stiles3b2c06c2020-06-17 12:45:57 -0400277 for (int i = 0; i < 3; ++i) {
278 canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
279 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000280
John Stiles3b2c06c2020-06-17 12:45:57 -0400281 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
282
283 SkConic dst[4];
284 SkMatrix klm;
285 int cnt = ChopConic(controlPts, dst, weights[row]);
286 GrPathUtils::getConicKLM(controlPts, weights[row], &klm);
287
288 for (int c = 0; c < cnt; ++c) {
289 SkPoint* pts = dst[c].fPts;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000290 for (int i = 0; i < 3; ++i) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400291 canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000292 }
293
John Stiles3b2c06c2020-06-17 12:45:57 -0400294 SkRect bounds;
295 bounds.setBounds(pts, 3);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000296
John Stiles3b2c06c2020-06-17 12:45:57 -0400297 canvas->drawRect(bounds, boundsPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000298
John Stiles3b2c06c2020-06-17 12:45:57 -0400299 std::unique_ptr<GrDrawOp> op = BezierConicTestOp::Make(context, bounds,
300 kOpaqueBlack, klm);
301 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000302 }
303 }
304 }
305
306private:
307 // Uses the max curvature function for quads to estimate
308 // where to chop the conic. If the max curvature is not
309 // found along the curve segment it will return 1 and
310 // dst[0] is the original conic. If it returns 2 the dst[0]
311 // and dst[1] are the two new conics.
Robert Phillips98f3fd92019-11-21 13:16:21 -0500312 static int SplitConic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000313 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600314 if (t == 0 || t == 1) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000315 if (dst) {
316 dst[0].set(src, weight);
317 }
318 return 1;
319 } else {
320 if (dst) {
321 SkConic conic;
322 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700323 if (!conic.chopAt(t, dst)) {
324 dst[0].set(src, weight);
325 return 1;
326 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000327 }
328 return 2;
329 }
330 }
331
Robert Phillips98f3fd92019-11-21 13:16:21 -0500332 // Calls SplitConic on the entire conic and then once more on each subsection.
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000333 // Most cases will result in either 1 conic (chop point is not within t range)
334 // or 3 points (split once and then one subsection is split again).
Robert Phillips98f3fd92019-11-21 13:16:21 -0500335 static int ChopConic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000336 SkConic dstTemp[2];
Robert Phillips98f3fd92019-11-21 13:16:21 -0500337 int conicCnt = SplitConic(src, dstTemp, weight);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000338 if (2 == conicCnt) {
Robert Phillips98f3fd92019-11-21 13:16:21 -0500339 int conicCnt2 = SplitConic(dstTemp[0].fPts, dst, dstTemp[0].fW);
340 conicCnt = conicCnt2 + SplitConic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000341 } else {
342 dst[0] = dstTemp[0];
343 }
344 return conicCnt;
345 }
346
John Stiles7571f9e2020-09-02 22:42:33 -0400347 using INHERITED = GM;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000348};
349
350//////////////////////////////////////////////////////////////////////////////
joshualitt95964c62015-02-11 13:45:50 -0800351
Brian Salomon477d0ef2017-07-14 10:12:26 -0400352class BezierQuadTestOp : public BezierTestOp {
joshualitt95964c62015-02-11 13:45:50 -0800353public:
Brian Salomon25a88092016-12-01 09:36:50 -0500354 DEFINE_OP_CLASS_ID
Brian Salomon6b316e92016-12-16 09:35:49 -0500355 const char* name() const override { return "BezierQuadTestOp"; }
joshualitt95964c62015-02-11 13:45:50 -0800356
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500357 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400358 const SkRect& rect,
Brian Osmancf860852018-10-31 14:04:39 -0400359 const SkPMColor4f& color,
Robert Phillips7c525e62018-06-12 10:11:12 -0400360 const GrPathUtils::QuadUVMatrix& devToUV) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500361 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400362
John Stiles3b2c06c2020-06-17 12:45:57 -0400363 return pool->allocate<BezierQuadTestOp>(rect, color, devToUV);
joshualitt95964c62015-02-11 13:45:50 -0800364 }
365
366private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400367 friend class ::GrOpMemoryPool; // for ctor
368
John Stiles3b2c06c2020-06-17 12:45:57 -0400369 BezierQuadTestOp(const SkRect& rect, const SkPMColor4f& color,
370 const GrPathUtils::QuadUVMatrix& devToUV)
371 : INHERITED(rect, color, ClassID())
372 , fDevToUV(devToUV) {}
joshualitt95964c62015-02-11 13:45:50 -0800373
374 struct Vertex {
375 SkPoint fPosition;
376 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
377 };
378
Robert Phillipsce978572020-02-28 11:56:44 -0500379 GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
John Stiles3b2c06c2020-06-17 12:45:57 -0400380 auto tmp = GrQuadEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
381 false);
Robert Phillipsce978572020-02-28 11:56:44 -0500382 if (!tmp) {
383 return nullptr;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500384 }
Robert Phillipsce978572020-02-28 11:56:44 -0500385 SkASSERT(tmp->vertexStride() == sizeof(Vertex));
386 return tmp;
387 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500388
Robert Phillipsce978572020-02-28 11:56:44 -0500389 void onPrepareDraws(Target* target) final {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000390 QuadHelper helper(target, sizeof(Vertex), 1);
391 Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
bsalomonb5238a72015-05-05 07:49:49 -0700392 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800393 return;
394 }
Brian Salomon477d0ef2017-07-14 10:12:26 -0400395 SkRect rect = this->rect();
Brian Salomonec42e152018-05-18 12:52:22 -0400396 SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
Brian Osman568bec72018-12-26 16:48:25 -0500397 fDevToUV.apply(verts, 4, sizeof(Vertex), sizeof(SkPoint));
Robert Phillipsce978572020-02-28 11:56:44 -0500398
399 fMesh = helper.mesh();
joshualitt95964c62015-02-11 13:45:50 -0800400 }
401
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500402 GrPathUtils::QuadUVMatrix fDevToUV;
joshualitt95964c62015-02-11 13:45:50 -0800403
mtkleindbfd7ab2016-09-01 11:24:54 -0700404 static constexpr int kVertsPerCubic = 4;
405 static constexpr int kIndicesPerCubic = 6;
joshualitt95964c62015-02-11 13:45:50 -0800406
John Stiles7571f9e2020-09-02 22:42:33 -0400407 using INHERITED = BezierTestOp;
joshualitt95964c62015-02-11 13:45:50 -0800408};
409
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000410/**
411 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
412 */
Chris Dalton3a778372019-02-07 15:23:36 -0700413class BezierQuadEffects : public GpuGM {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000414public:
415 BezierQuadEffects() {
416 this->setBGColor(0xFFFFFFFF);
417 }
418
419protected:
Robert Phillips98f3fd92019-11-21 13:16:21 -0500420 static const int kNumQuads = 5;
421 static const int kCellWidth = 128;
422 static const int kCellHeight = 128;
423
mtklein36352bf2015-03-25 18:17:31 -0700424 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000425 return SkString("bezier_quad_effects");
426 }
427
mtklein36352bf2015-03-25 18:17:31 -0700428 SkISize onISize() override {
John Stiles3b2c06c2020-06-17 12:45:57 -0400429 return SkISize::Make(kCellWidth, kNumQuads*kCellHeight);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000430 }
431
Robert Phillips95c250c2020-06-29 15:36:12 -0400432 void onDraw(GrRecordingContext* context, GrRenderTargetContext* renderTargetContext,
Chris Dalton3a778372019-02-07 15:23:36 -0700433 SkCanvas* canvas) override {
Robert Phillips98f3fd92019-11-21 13:16:21 -0500434
435 const SkScalar w = kCellWidth, h = kCellHeight;
436 const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
437
438 const SkPoint baseControlPts[kNumQuads][3] = {
439 { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
440 { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
441 { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
442 { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
443 { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000444 };
445
Robert Phillips98f3fd92019-11-21 13:16:21 -0500446 SkPaint ctrlPtPaint;
447 ctrlPtPaint.setColor(SK_ColorRED);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000448
Robert Phillips98f3fd92019-11-21 13:16:21 -0500449 SkPaint choppedPtPaint;
450 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000451
Robert Phillips98f3fd92019-11-21 13:16:21 -0500452 SkPaint polyPaint;
453 polyPaint.setColor(0xffA0A0A0);
454 polyPaint.setStrokeWidth(0);
455 polyPaint.setStyle(SkPaint::kStroke_Style);
456
457 SkPaint boundsPaint;
458 boundsPaint.setColor(0xff808080);
459 boundsPaint.setStrokeWidth(0);
460 boundsPaint.setStyle(SkPaint::kStroke_Style);
461
462 for (int row = 0; row < kNumQuads; ++row) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400463 SkScalar x = 0;
464 SkScalar y = row * h;
465 SkPoint controlPts[] = {
466 {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
467 {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
468 {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
469 };
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000470
John Stiles3b2c06c2020-06-17 12:45:57 -0400471 for (int i = 0; i < 3; ++i) {
472 canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
473 }
474
475 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
476
477 SkPoint chopped[5];
478 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
479
480 for (int c = 0; c < cnt; ++c) {
481 SkPoint* pts = chopped + 2 * c;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000482
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000483 for (int i = 0; i < 3; ++i) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400484 canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000485 }
486
John Stiles3b2c06c2020-06-17 12:45:57 -0400487 SkRect bounds;
488 bounds.setBounds(pts, 3);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000489
John Stiles3b2c06c2020-06-17 12:45:57 -0400490 canvas->drawRect(bounds, boundsPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000491
John Stiles3b2c06c2020-06-17 12:45:57 -0400492 GrPathUtils::QuadUVMatrix DevToUV(pts);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000493
John Stiles3b2c06c2020-06-17 12:45:57 -0400494 std::unique_ptr<GrDrawOp> op = BezierQuadTestOp::Make(context, bounds,
495 kOpaqueBlack, DevToUV);
496 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000497 }
498 }
499 }
500
501private:
John Stiles7571f9e2020-09-02 22:42:33 -0400502 using INHERITED = GM;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000503};
504
halcanary385fe4d2015-08-26 13:07:48 -0700505DEF_GM(return new BezierConicEffects;)
506DEF_GM(return new BezierQuadEffects;)
John Stilesa6841be2020-08-06 14:11:56 -0400507} // namespace skiagm