blob: e56f57d80f8c6184cee8e03a79c0d35d013311cf [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/GrTypesPriv.h"
25#include "include/private/SkColorData.h"
26#include "include/utils/SkRandom.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040027#include "src/core/SkCanvasPriv.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"
Brian Salomoneebe7352020-12-09 16:37:04 -050041#include "src/gpu/GrSurfaceDrawContext.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040042#include "src/gpu/GrUserStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/effects/GrBezierEffect.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040044#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040045#include "src/gpu/geometry/GrPathUtils.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040046#include "src/gpu/ops/GrDrawOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/ops/GrMeshDrawOp.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040048#include "src/gpu/ops/GrOp.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050049#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040050
51#include <memory>
52#include <utility>
53
54class GrAppliedClip;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000055
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000056namespace skiagm {
joshualitt95964c62015-02-11 13:45:50 -080057
Brian Salomon477d0ef2017-07-14 10:12:26 -040058class BezierTestOp : public GrMeshDrawOp {
59public:
60 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
61
Chris Dalton6ce447a2019-06-23 18:07:38 -060062 GrProcessorSet::Analysis finalize(
Chris Dalton57ab06c2021-04-22 12:57:28 -060063 const GrCaps& caps, const GrAppliedClip* clip, GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -070064 return fProcessorSet.finalize(
65 fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
Chris Dalton57ab06c2021-04-22 12:57:28 -060066 &GrUserStencilSettings::kUnused, caps, clampType, &fColor);
Brian Salomon477d0ef2017-07-14 10:12:26 -040067 }
68
Robert Phillips294723d2021-06-17 09:23:58 -040069 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips4f93c572020-03-18 08:13:53 -040070 if (fProgramInfo) {
71 fProgramInfo->visitFPProxies(func);
72 } else {
73 fProcessorSet.visitProxies(func);
74 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040075 }
76
Brian Salomon477d0ef2017-07-14 10:12:26 -040077protected:
John Stiles3b2c06c2020-06-17 12:45:57 -040078 BezierTestOp(const SkRect& rect, const SkPMColor4f& color, int32_t classID)
Brian Salomon477d0ef2017-07-14 10:12:26 -040079 : INHERITED(classID)
80 , fRect(rect)
81 , fColor(color)
Brian Salomon477d0ef2017-07-14 10:12:26 -040082 , fProcessorSet(SkBlendMode::kSrc) {
Greg Daniel5faf4742019-10-01 15:14:44 -040083 this->setBounds(rect, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon477d0ef2017-07-14 10:12:26 -040084 }
85
Robert Phillipsce978572020-02-28 11:56:44 -050086 virtual GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) = 0;
Robert Phillips3968fcb2019-12-05 16:40:31 -050087
Robert Phillips2669a7b2020-03-12 12:07:19 -040088 GrProgramInfo* programInfo() override { return fProgramInfo; }
89
Robert Phillips4133dc42020-03-11 15:55:55 -040090 void onCreateProgramInfo(const GrCaps* caps,
91 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -050092 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -060093 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -040094 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -040095 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -050096 GrXferBarrierFlags renderPassXferBarriers,
97 GrLoadOp colorLoadOp) 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,
Greg Daniel42dbca52020-11-20 10:22:43 -0500111 colorLoadOp,
Robert Phillips4133dc42020-03-11 15:55:55 -0400112 flags);
Robert Phillipsce978572020-02-28 11:56:44 -0500113 }
114
Robert Phillipsce978572020-02-28 11:56:44 -0500115 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
116 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400117 this->createProgramInfo(flushState);
Robert Phillipsce978572020-02-28 11:56:44 -0500118 }
119
120 if (!fProgramInfo) {
121 return;
122 }
123
Chris Dalton765ed362020-03-16 17:34:44 -0600124 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400125 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600126 flushState->drawMesh(*fMesh);
Brian Salomon477d0ef2017-07-14 10:12:26 -0400127 }
128
Brian Salomon477d0ef2017-07-14 10:12:26 -0400129 const SkRect& rect() const { return fRect; }
Brian Osmancf860852018-10-31 14:04:39 -0400130 const SkPMColor4f& color() const { return fColor; }
Brian Salomon477d0ef2017-07-14 10:12:26 -0400131
Robert Phillipsce978572020-02-28 11:56:44 -0500132protected:
Chris Daltoneb694b72020-03-16 09:25:50 -0600133 GrSimpleMesh* fMesh = nullptr; // filled in by the derived classes
Robert Phillipsce978572020-02-28 11:56:44 -0500134
Brian Salomon477d0ef2017-07-14 10:12:26 -0400135private:
Robert Phillipsce978572020-02-28 11:56:44 -0500136 SkRect fRect;
137 SkPMColor4f fColor;
Robert Phillipsce978572020-02-28 11:56:44 -0500138 GrProcessorSet fProcessorSet;
139 GrProgramInfo* fProgramInfo = nullptr;
Brian Salomon477d0ef2017-07-14 10:12:26 -0400140
John Stiles7571f9e2020-09-02 22:42:33 -0400141 using INHERITED = GrMeshDrawOp;
Brian Salomon477d0ef2017-07-14 10:12:26 -0400142};
143
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000144/**
145 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
146 */
Brian Salomon477d0ef2017-07-14 10:12:26 -0400147class BezierConicTestOp : public BezierTestOp {
Chris Daltonfebbffa2017-06-08 13:12:02 -0600148public:
149 DEFINE_OP_CLASS_ID
150
Robert Phillipsce978572020-02-28 11:56:44 -0500151 const char* name() const final { return "BezierConicTestOp"; }
Chris Daltonfebbffa2017-06-08 13:12:02 -0600152
Herb Derbyc76d4092020-10-07 16:46:15 -0400153 static GrOp::Owner Make(GrRecordingContext* context,
154 const SkRect& rect,
155 const SkPMColor4f& color,
156 const SkMatrix& klm) {
157 return GrOp::Make<BezierConicTestOp>(context, rect, color, klm);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600158 }
159
160private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400161 friend class ::GrOp; // for ctor
Robert Phillips7c525e62018-06-12 10:11:12 -0400162
John Stiles3b2c06c2020-06-17 12:45:57 -0400163 BezierConicTestOp(const SkRect& rect, const SkPMColor4f& color, const SkMatrix& klm)
164 : INHERITED(rect, color, ClassID())
165 , fKLM(klm) {}
Brian Salomon477d0ef2017-07-14 10:12:26 -0400166
Chris Daltonfebbffa2017-06-08 13:12:02 -0600167 struct Vertex {
168 SkPoint fPosition;
169 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
170 };
171
Robert Phillipsce978572020-02-28 11:56:44 -0500172 GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
John Stiles3b2c06c2020-06-17 12:45:57 -0400173 auto tmp = GrConicEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
174 false);
Robert Phillipsce978572020-02-28 11:56:44 -0500175 if (!tmp) {
176 return nullptr;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500177 }
Robert Phillipsce978572020-02-28 11:56:44 -0500178 SkASSERT(tmp->vertexStride() == sizeof(Vertex));
179 return tmp;
180 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500181
Robert Phillips71143952021-06-17 14:55:07 -0400182 void onPrepareDraws(GrMeshDrawTarget* target) final {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000183 QuadHelper helper(target, sizeof(Vertex), 1);
184 Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
Chris Daltonfebbffa2017-06-08 13:12:02 -0600185 if (!verts) {
186 return;
187 }
Brian Salomon477d0ef2017-07-14 10:12:26 -0400188 SkRect rect = this->rect();
Robert Phillipsce978572020-02-28 11:56:44 -0500189 SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
Chris Daltonfebbffa2017-06-08 13:12:02 -0600190 for (int v = 0; v < 4; ++v) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400191 SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f};
192 fKLM.mapHomogeneousPoints((SkPoint3* ) verts[v].fKLM, &pt3, 1);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600193 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700194
Robert Phillipsce978572020-02-28 11:56:44 -0500195 fMesh = helper.mesh();
Chris Daltonfebbffa2017-06-08 13:12:02 -0600196 }
197
198 SkMatrix fKLM;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600199
200 static constexpr int kVertsPerCubic = 4;
201 static constexpr int kIndicesPerCubic = 6;
202
John Stiles7571f9e2020-09-02 22:42:33 -0400203 using INHERITED = BezierTestOp;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600204};
205
206
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000207/**
208 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
209 */
Chris Dalton3a778372019-02-07 15:23:36 -0700210class BezierConicEffects : public GpuGM {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000211public:
212 BezierConicEffects() {
213 this->setBGColor(0xFFFFFFFF);
214 }
215
216protected:
Robert Phillips98f3fd92019-11-21 13:16:21 -0500217 static const int kNumConics = 10;
218 static const int kCellWidth = 128;
219 static const int kCellHeight = 128;
220
mtklein36352bf2015-03-25 18:17:31 -0700221 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000222 return SkString("bezier_conic_effects");
223 }
224
mtklein36352bf2015-03-25 18:17:31 -0700225 SkISize onISize() override {
John Stiles3b2c06c2020-06-17 12:45:57 -0400226 return SkISize::Make(kCellWidth, kNumConics*kCellHeight);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000227 }
228
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400229 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
230 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
231 if (!sdc) {
232 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
233 return DrawResult::kSkip;
234 }
Robert Phillips98f3fd92019-11-21 13:16:21 -0500235
236 const SkScalar w = kCellWidth, h = kCellHeight;
237 const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
238
239 const SkPoint baseControlPts[kNumConics][3] = {
240 { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
241 { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
242 { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
243 { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
244 { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
245 { { 0.96f * w, 0.65f * h}, { 0.03f * w, 0.79f * h }, { 0.24f * w, 0.56f * h } },
246 { { 0.57f * w, 0.12f * h}, { 0.33f * w, 0.67f * h }, { 0.59f * w, 0.33f * h } },
247 { { 0.12f * w, 0.72f * h}, { 0.69f * w, 0.85f * h }, { 0.46f * w, 0.32f * h } },
248 { { 0.27f * w, 0.49f * h}, { 0.41f * w, 0.02f * h }, { 0.11f * w, 0.42f * h } },
249 { { 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 +0000250 };
Robert Phillips98f3fd92019-11-21 13:16:21 -0500251 const SkScalar weights[kNumConics] = { 0.62f, 0.01f, 0.95f, 1.48f, 0.37f,
252 0.66f, 0.15f, 0.14f, 0.61f, 1.4f };
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000253
Robert Phillips98f3fd92019-11-21 13:16:21 -0500254 SkPaint ctrlPtPaint;
255 ctrlPtPaint.setColor(SK_ColorRED);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000256
Robert Phillips98f3fd92019-11-21 13:16:21 -0500257 SkPaint choppedPtPaint;
258 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000259
Robert Phillips98f3fd92019-11-21 13:16:21 -0500260 SkPaint polyPaint;
261 polyPaint.setColor(0xffA0A0A0);
262 polyPaint.setStrokeWidth(0);
263 polyPaint.setStyle(SkPaint::kStroke_Style);
264
265 SkPaint boundsPaint;
266 boundsPaint.setColor(0xff808080);
267 boundsPaint.setStrokeWidth(0);
268 boundsPaint.setStyle(SkPaint::kStroke_Style);
269
270
271 for (int row = 0; row < kNumConics; ++row) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400272 SkScalar x = 0;
273 SkScalar y = row * h;
274 SkPoint controlPts[] = {
275 {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
276 {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
277 {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
278 };
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000279
John Stiles3b2c06c2020-06-17 12:45:57 -0400280 for (int i = 0; i < 3; ++i) {
281 canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
282 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000283
John Stiles3b2c06c2020-06-17 12:45:57 -0400284 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
285
286 SkConic dst[4];
287 SkMatrix klm;
288 int cnt = ChopConic(controlPts, dst, weights[row]);
289 GrPathUtils::getConicKLM(controlPts, weights[row], &klm);
290
291 for (int c = 0; c < cnt; ++c) {
292 SkPoint* pts = dst[c].fPts;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000293 for (int i = 0; i < 3; ++i) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400294 canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000295 }
296
John Stiles3b2c06c2020-06-17 12:45:57 -0400297 SkRect bounds;
298 bounds.setBounds(pts, 3);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000299
John Stiles3b2c06c2020-06-17 12:45:57 -0400300 canvas->drawRect(bounds, boundsPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000301
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400302 GrOp::Owner op = BezierConicTestOp::Make(rContext, bounds,
Herb Derbyc76d4092020-10-07 16:46:15 -0400303 kOpaqueBlack, klm);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400304 sdc->addDrawOp(std::move(op));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000305 }
306 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400307
308 return DrawResult::kOk;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000309 }
310
311private:
312 // Uses the max curvature function for quads to estimate
313 // where to chop the conic. If the max curvature is not
314 // found along the curve segment it will return 1 and
315 // dst[0] is the original conic. If it returns 2 the dst[0]
316 // and dst[1] are the two new conics.
Robert Phillips98f3fd92019-11-21 13:16:21 -0500317 static int SplitConic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000318 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600319 if (t == 0 || t == 1) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000320 if (dst) {
321 dst[0].set(src, weight);
322 }
323 return 1;
324 } else {
325 if (dst) {
326 SkConic conic;
327 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700328 if (!conic.chopAt(t, dst)) {
329 dst[0].set(src, weight);
330 return 1;
331 }
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000332 }
333 return 2;
334 }
335 }
336
Robert Phillips98f3fd92019-11-21 13:16:21 -0500337 // Calls SplitConic on the entire conic and then once more on each subsection.
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000338 // Most cases will result in either 1 conic (chop point is not within t range)
339 // or 3 points (split once and then one subsection is split again).
Robert Phillips98f3fd92019-11-21 13:16:21 -0500340 static int ChopConic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000341 SkConic dstTemp[2];
Robert Phillips98f3fd92019-11-21 13:16:21 -0500342 int conicCnt = SplitConic(src, dstTemp, weight);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000343 if (2 == conicCnt) {
Robert Phillips98f3fd92019-11-21 13:16:21 -0500344 int conicCnt2 = SplitConic(dstTemp[0].fPts, dst, dstTemp[0].fW);
345 conicCnt = conicCnt2 + SplitConic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000346 } else {
347 dst[0] = dstTemp[0];
348 }
349 return conicCnt;
350 }
351
John Stiles7571f9e2020-09-02 22:42:33 -0400352 using INHERITED = GM;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000353};
354
355//////////////////////////////////////////////////////////////////////////////
joshualitt95964c62015-02-11 13:45:50 -0800356
Brian Salomon477d0ef2017-07-14 10:12:26 -0400357class BezierQuadTestOp : public BezierTestOp {
joshualitt95964c62015-02-11 13:45:50 -0800358public:
Brian Salomon25a88092016-12-01 09:36:50 -0500359 DEFINE_OP_CLASS_ID
Brian Salomon6b316e92016-12-16 09:35:49 -0500360 const char* name() const override { return "BezierQuadTestOp"; }
joshualitt95964c62015-02-11 13:45:50 -0800361
Herb Derbyc76d4092020-10-07 16:46:15 -0400362 static GrOp::Owner Make(GrRecordingContext* context,
363 const SkRect& rect,
364 const SkPMColor4f& color,
365 const GrPathUtils::QuadUVMatrix& devToUV) {
366 return GrOp::Make<BezierQuadTestOp>(context, rect, color, devToUV);
joshualitt95964c62015-02-11 13:45:50 -0800367 }
368
369private:
Herb Derbyc76d4092020-10-07 16:46:15 -0400370 friend class ::GrOp; // for ctor
Robert Phillips7c525e62018-06-12 10:11:12 -0400371
John Stiles3b2c06c2020-06-17 12:45:57 -0400372 BezierQuadTestOp(const SkRect& rect, const SkPMColor4f& color,
373 const GrPathUtils::QuadUVMatrix& devToUV)
374 : INHERITED(rect, color, ClassID())
375 , fDevToUV(devToUV) {}
joshualitt95964c62015-02-11 13:45:50 -0800376
377 struct Vertex {
378 SkPoint fPosition;
379 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
380 };
381
Robert Phillipsce978572020-02-28 11:56:44 -0500382 GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
John Stiles3b2c06c2020-06-17 12:45:57 -0400383 auto tmp = GrQuadEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
384 false);
Robert Phillipsce978572020-02-28 11:56:44 -0500385 if (!tmp) {
386 return nullptr;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500387 }
Robert Phillipsce978572020-02-28 11:56:44 -0500388 SkASSERT(tmp->vertexStride() == sizeof(Vertex));
389 return tmp;
390 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500391
Robert Phillips71143952021-06-17 14:55:07 -0400392 void onPrepareDraws(GrMeshDrawTarget* target) final {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000393 QuadHelper helper(target, sizeof(Vertex), 1);
394 Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
bsalomonb5238a72015-05-05 07:49:49 -0700395 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800396 return;
397 }
Brian Salomon477d0ef2017-07-14 10:12:26 -0400398 SkRect rect = this->rect();
Brian Salomonec42e152018-05-18 12:52:22 -0400399 SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
Brian Osman568bec72018-12-26 16:48:25 -0500400 fDevToUV.apply(verts, 4, sizeof(Vertex), sizeof(SkPoint));
Robert Phillipsce978572020-02-28 11:56:44 -0500401
402 fMesh = helper.mesh();
joshualitt95964c62015-02-11 13:45:50 -0800403 }
404
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500405 GrPathUtils::QuadUVMatrix fDevToUV;
joshualitt95964c62015-02-11 13:45:50 -0800406
mtkleindbfd7ab2016-09-01 11:24:54 -0700407 static constexpr int kVertsPerCubic = 4;
408 static constexpr int kIndicesPerCubic = 6;
joshualitt95964c62015-02-11 13:45:50 -0800409
John Stiles7571f9e2020-09-02 22:42:33 -0400410 using INHERITED = BezierTestOp;
joshualitt95964c62015-02-11 13:45:50 -0800411};
412
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000413/**
414 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
415 */
Chris Dalton3a778372019-02-07 15:23:36 -0700416class BezierQuadEffects : public GpuGM {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000417public:
418 BezierQuadEffects() {
419 this->setBGColor(0xFFFFFFFF);
420 }
421
422protected:
Robert Phillips98f3fd92019-11-21 13:16:21 -0500423 static const int kNumQuads = 5;
424 static const int kCellWidth = 128;
425 static const int kCellHeight = 128;
426
mtklein36352bf2015-03-25 18:17:31 -0700427 SkString onShortName() override {
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000428 return SkString("bezier_quad_effects");
429 }
430
mtklein36352bf2015-03-25 18:17:31 -0700431 SkISize onISize() override {
John Stiles3b2c06c2020-06-17 12:45:57 -0400432 return SkISize::Make(kCellWidth, kNumQuads*kCellHeight);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000433 }
434
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400435 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
436 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
437 if (!sdc) {
438 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
439 return DrawResult::kSkip;
440 }
Robert Phillips98f3fd92019-11-21 13:16:21 -0500441
442 const SkScalar w = kCellWidth, h = kCellHeight;
443 const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
444
445 const SkPoint baseControlPts[kNumQuads][3] = {
446 { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
447 { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
448 { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
449 { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
450 { { 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 +0000451 };
452
Robert Phillips98f3fd92019-11-21 13:16:21 -0500453 SkPaint ctrlPtPaint;
454 ctrlPtPaint.setColor(SK_ColorRED);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000455
Robert Phillips98f3fd92019-11-21 13:16:21 -0500456 SkPaint choppedPtPaint;
457 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000458
Robert Phillips98f3fd92019-11-21 13:16:21 -0500459 SkPaint polyPaint;
460 polyPaint.setColor(0xffA0A0A0);
461 polyPaint.setStrokeWidth(0);
462 polyPaint.setStyle(SkPaint::kStroke_Style);
463
464 SkPaint boundsPaint;
465 boundsPaint.setColor(0xff808080);
466 boundsPaint.setStrokeWidth(0);
467 boundsPaint.setStyle(SkPaint::kStroke_Style);
468
469 for (int row = 0; row < kNumQuads; ++row) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400470 SkScalar x = 0;
471 SkScalar y = row * h;
472 SkPoint controlPts[] = {
473 {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
474 {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
475 {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
476 };
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000477
John Stiles3b2c06c2020-06-17 12:45:57 -0400478 for (int i = 0; i < 3; ++i) {
479 canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
480 }
481
482 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, controlPts, polyPaint);
483
484 SkPoint chopped[5];
485 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
486
487 for (int c = 0; c < cnt; ++c) {
488 SkPoint* pts = chopped + 2 * c;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000489
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000490 for (int i = 0; i < 3; ++i) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400491 canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000492 }
493
John Stiles3b2c06c2020-06-17 12:45:57 -0400494 SkRect bounds;
495 bounds.setBounds(pts, 3);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000496
John Stiles3b2c06c2020-06-17 12:45:57 -0400497 canvas->drawRect(bounds, boundsPaint);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000498
John Stiles3b2c06c2020-06-17 12:45:57 -0400499 GrPathUtils::QuadUVMatrix DevToUV(pts);
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000500
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400501 GrOp::Owner op = BezierQuadTestOp::Make(rContext, bounds,
Herb Derbyc76d4092020-10-07 16:46:15 -0400502 kOpaqueBlack, DevToUV);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400503 sdc->addDrawOp(std::move(op));
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000504 }
505 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400506
507 return DrawResult::kOk;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000508 }
509
510private:
John Stiles7571f9e2020-09-02 22:42:33 -0400511 using INHERITED = GM;
commit-bot@chromium.org53a0b6c2013-08-23 18:05:01 +0000512};
513
halcanary385fe4d2015-08-26 13:07:48 -0700514DEF_GM(return new BezierConicEffects;)
515DEF_GM(return new BezierQuadEffects;)
John Stilesa6841be2020-08-06 14:11:56 -0400516} // namespace skiagm