Debian Codegraph @

gm/beziereffects.cpp

raw
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/gm.h"
11 #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"
16 #include "include/core/SkPoint3.h"
17 #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"
23 #include "include/gpu/ganesh/GrRecordingContext.h"
24 #include "include/private/gpu/ganesh/GrTypesPriv.h"
25 #include "src/base/SkRandom.h"
26 #include "src/core/SkCanvasPriv.h"
27 #include "src/core/SkColorData.h"
28 #include "src/core/SkGeometry.h"
29 #include "src/core/SkPointPriv.h"
30 #include "src/gpu/ganesh/GrCanvas.h"
31 #include "src/gpu/ganesh/GrCaps.h"
32 #include "src/gpu/ganesh/GrDirectContextPriv.h"
33 #include "src/gpu/ganesh/GrGeometryProcessor.h"
34 #include "src/gpu/ganesh/GrMemoryPool.h"
35 #include "src/gpu/ganesh/GrOpFlushState.h"
36 #include "src/gpu/ganesh/GrOpsRenderPass.h"
37 #include "src/gpu/ganesh/GrPaint.h"
38 #include "src/gpu/ganesh/GrProcessorAnalysis.h"
39 #include "src/gpu/ganesh/GrProcessorSet.h"
40 #include "src/gpu/ganesh/GrProgramInfo.h"
41 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
42 #include "src/gpu/ganesh/GrUserStencilSettings.h"
43 #include "src/gpu/ganesh/SurfaceDrawContext.h"
44 #include "src/gpu/ganesh/effects/GrBezierEffect.h"
45 #include "src/gpu/ganesh/effects/GrPorterDuffXferProcessor.h"
46 #include "src/gpu/ganesh/geometry/GrPathUtils.h"
47 #include "src/gpu/ganesh/ops/GrDrawOp.h"
48 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
49 #include "src/gpu/ganesh/ops/GrOp.h"
50 #include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
51
52 #include <memory>
53 #include <utility>
54
55 class GrAppliedClip;
56
57 namespace skiagm {
58
59 class BezierTestOp : public GrMeshDrawOp {
60 public:
61 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
62
63 GrProcessorSet::Analysis finalize(
64 const GrCaps& caps, const GrAppliedClip* clip, GrClampType clampType) override {
65 return fProcessorSet.finalize(
66 fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
67 &GrUserStencilSettings::kUnused, caps, clampType, &fColor);
68 }
69
70 void visitProxies(const GrVisitProxyFunc& func) const override {
71 if (fProgramInfo) {
72 fProgramInfo->visitFPProxies(func);
73 } else {
74 fProcessorSet.visitProxies(func);
75 }
76 }
77
78 protected:
79 BezierTestOp(const SkRect& rect, const SkPMColor4f& color, int32_t classID)
80 : INHERITED(classID)
81 , fRect(rect)
82 , fColor(color)
83 , fProcessorSet(SkBlendMode::kSrc) {
84 this->setBounds(rect, HasAABloat::kYes, IsHairline::kNo);
85 }
86
87 virtual GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) = 0;
88
89 GrProgramInfo* programInfo() override { return fProgramInfo; }
90
91 void onCreateProgramInfo(const GrCaps* caps,
92 SkArenaAlloc* arena,
93 const GrSurfaceProxyView& writeView,
94 bool usesMSAASurface,
95 GrAppliedClip&& appliedClip,
96 const GrDstProxyView& dstProxyView,
97 GrXferBarrierFlags renderPassXferBarriers,
98 GrLoadOp colorLoadOp) override {
99 auto gp = this->makeGP(*caps, arena);
100 if (!gp) {
101 return;
102 }
103
104 GrPipeline::InputFlags flags = GrPipeline::InputFlags::kNone;
105
106 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
107 usesMSAASurface,
108 std::move(appliedClip),
109 dstProxyView, gp,
110 std::move(fProcessorSet),
111 GrPrimitiveType::kTriangles,
112 renderPassXferBarriers,
113 colorLoadOp,
114 flags);
115 }
116
117 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
118 if (!fProgramInfo) {
119 this->createProgramInfo(flushState);
120 }
121
122 if (!fProgramInfo) {
123 return;
124 }
125
126 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
127 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
128 flushState->drawMesh(*fMesh);
129 }
130
131 const SkRect& rect() const { return fRect; }
132 const SkPMColor4f& color() const { return fColor; }
133
134 protected:
135 GrSimpleMesh* fMesh = nullptr; // filled in by the derived classes
136
137 private:
138 SkRect fRect;
139 SkPMColor4f fColor;
140 GrProcessorSet fProcessorSet;
141 GrProgramInfo* fProgramInfo = nullptr;
142
143 using INHERITED = GrMeshDrawOp;
144 };
145
146 /**
147 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
148 */
149 class BezierConicTestOp : public BezierTestOp {
150 public:
151 DEFINE_OP_CLASS_ID
152
153 const char* name() const final { return "BezierConicTestOp"; }
154
155 static GrOp::Owner Make(GrRecordingContext* context,
156 const SkRect& rect,
157 const SkPMColor4f& color,
158 const SkMatrix& klm) {
159 return GrOp::Make<BezierConicTestOp>(context, rect, color, klm);
160 }
161
162 private:
163 friend class ::GrOp; // for ctor
164
165 BezierConicTestOp(const SkRect& rect, const SkPMColor4f& color, const SkMatrix& klm)
166 : INHERITED(rect, color, ClassID())
167 , fKLM(klm) {}
168
169 struct Vertex {
170 SkPoint fPosition;
171 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
172 };
173
174 GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
175 auto tmp = GrConicEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
176 false);
177 if (!tmp) {
178 return nullptr;
179 }
180 SkASSERT(tmp->vertexStride() == sizeof(Vertex));
181 return tmp;
182 }
183
184 void onPrepareDraws(GrMeshDrawTarget* target) final {
185 QuadHelper helper(target, sizeof(Vertex), 1);
186 Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
187 if (!verts) {
188 return;
189 }
190 SkRect rect = this->rect();
191 SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
192 for (int v = 0; v < 4; ++v) {
193 SkPoint3 pt3 = {verts[v].fPosition.x(), verts[v].fPosition.y(), 1.f};
194 fKLM.mapHomogeneousPoints({(SkPoint3* ) verts[v].fKLM, 1},
195 {&pt3, 1});
196 }
197
198 fMesh = helper.mesh();
199 }
200
201 SkMatrix fKLM;
202
203 inline static constexpr int kVertsPerCubic = 4;
204 inline static constexpr int kIndicesPerCubic = 6;
205
206 using INHERITED = BezierTestOp;
207 };
208
209
210 /**
211 * This GM directly exercises effects that draw Bezier curves in the GPU backend.
212 */
213 class BezierConicEffects : public GpuGM {
214 public:
215 BezierConicEffects() {
216 this->setBGColor(0xFFFFFFFF);
217 }
218
219 protected:
220 static const int kNumConics = 10;
221 static const int kCellWidth = 128;
222 static const int kCellHeight = 128;
223
224 SkString getName() const override { return SkString("bezier_conic_effects"); }
225
226 SkISize getISize() override { return SkISize::Make(kCellWidth, kNumConics * kCellHeight); }
227
228 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
229 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
230 if (!sdc) {
231 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
232 return DrawResult::kSkip;
233 }
234
235 const SkScalar w = kCellWidth, h = kCellHeight;
236 const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
237
238 const SkPoint baseControlPts[kNumConics][3] = {
239 { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
240 { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
241 { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
242 { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
243 { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
244 { { 0.96f * w, 0.65f * h}, { 0.03f * w, 0.79f * h }, { 0.24f * w, 0.56f * h } },
245 { { 0.57f * w, 0.12f * h}, { 0.33f * w, 0.67f * h }, { 0.59f * w, 0.33f * h } },
246 { { 0.12f * w, 0.72f * h}, { 0.69f * w, 0.85f * h }, { 0.46f * w, 0.32f * h } },
247 { { 0.27f * w, 0.49f * h}, { 0.41f * w, 0.02f * h }, { 0.11f * w, 0.42f * h } },
248 { { 0.40f * w, 0.13f * h}, { 0.83f * w, 0.30f * h }, { 0.31f * w, 0.68f * h } },
249 };
250 const SkScalar weights[kNumConics] = { 0.62f, 0.01f, 0.95f, 1.48f, 0.37f,
251 0.66f, 0.15f, 0.14f, 0.61f, 1.4f };
252
253 SkPaint ctrlPtPaint;
254 ctrlPtPaint.setColor(SK_ColorRED);
255
256 SkPaint choppedPtPaint;
257 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
258
259 SkPaint polyPaint;
260 polyPaint.setColor(0xffA0A0A0);
261 polyPaint.setStrokeWidth(0);
262 polyPaint.setStyle(SkPaint::kStroke_Style);
263
264 SkPaint boundsPaint;
265 boundsPaint.setColor(0xff808080);
266 boundsPaint.setStrokeWidth(0);
267 boundsPaint.setStyle(SkPaint::kStroke_Style);
268
269
270 for (int row = 0; row < kNumConics; ++row) {
271 SkScalar x = 0;
272 SkScalar y = row * h;
273 SkPoint controlPts[] = {
274 {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
275 {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
276 {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
277 };
278
279 for (int i = 0; i < 3; ++i) {
280 canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
281 }
282
283 canvas->drawPoints(SkCanvas::kPolygon_PointMode, controlPts, polyPaint);
284
285 SkConic dst[4];
286 SkMatrix klm;
287 int cnt = ChopConic(controlPts, dst, weights[row]);
288 GrPathUtils::getConicKLM(controlPts, weights[row], &klm);
289
290 for (int c = 0; c < cnt; ++c) {
291 for (auto p : dst[c].fPts) {
292 canvas->drawCircle(p, 3.f, choppedPtPaint);
293 }
294
295 const auto bounds = SkRect::BoundsOrEmpty(dst[c].fPts);
296 canvas->drawRect(bounds, boundsPaint);
297
298 GrOp::Owner op = BezierConicTestOp::Make(rContext, bounds, kOpaqueBlack, klm);
299 sdc->addDrawOp(std::move(op));
300 }
301 }
302
303 return DrawResult::kOk;
304 }
305
306 private:
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.
312 static int SplitConic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
313 SkScalar t = SkFindQuadMaxCurvature(src);
314 if (t == 0 || t == 1) {
315 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);
323 if (!conic.chopAt(t, dst)) {
324 dst[0].set(src, weight);
325 return 1;
326 }
327 }
328 return 2;
329 }
330 }
331
332 // Calls SplitConic on the entire conic and then once more on each subsection.
333 // 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).
335 static int ChopConic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
336 SkConic dstTemp[2];
337 int conicCnt = SplitConic(src, dstTemp, weight);
338 if (2 == conicCnt) {
339 int conicCnt2 = SplitConic(dstTemp[0].fPts, dst, dstTemp[0].fW);
340 conicCnt = conicCnt2 + SplitConic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
341 } else {
342 dst[0] = dstTemp[0];
343 }
344 return conicCnt;
345 }
346
347 using INHERITED = GM;
348 };
349
350 //////////////////////////////////////////////////////////////////////////////
351
352 class BezierQuadTestOp : public BezierTestOp {
353 public:
354 DEFINE_OP_CLASS_ID
355 const char* name() const override { return "BezierQuadTestOp"; }
356
357 static GrOp::Owner Make(GrRecordingContext* context,
358 const SkRect& rect,
359 const SkPMColor4f& color,
360 const GrPathUtils::QuadUVMatrix& devToUV) {
361 return GrOp::Make<BezierQuadTestOp>(context, rect, color, devToUV);
362 }
363
364 private:
365 friend class ::GrOp; // for ctor
366
367 BezierQuadTestOp(const SkRect& rect, const SkPMColor4f& color,
368 const GrPathUtils::QuadUVMatrix& devToUV)
369 : INHERITED(rect, color, ClassID())
370 , fDevToUV(devToUV) {}
371
372 struct Vertex {
373 SkPoint fPosition;
374 float fKLM[4]; // The last value is ignored. The effect expects a vec4f.
375 };
376
377 GrGeometryProcessor* makeGP(const GrCaps& caps, SkArenaAlloc* arena) final {
378 auto tmp = GrQuadEffect::Make(arena, this->color(), SkMatrix::I(), caps, SkMatrix::I(),
379 false);
380 if (!tmp) {
381 return nullptr;
382 }
383 SkASSERT(tmp->vertexStride() == sizeof(Vertex));
384 return tmp;
385 }
386
387 void onPrepareDraws(GrMeshDrawTarget* target) final {
388 QuadHelper helper(target, sizeof(Vertex), 1);
389 Vertex* verts = reinterpret_cast<Vertex*>(helper.vertices());
390 if (!verts) {
391 return;
392 }
393 SkRect rect = this->rect();
394 SkPointPriv::SetRectTriStrip(&verts[0].fPosition, rect, sizeof(Vertex));
395 fDevToUV.apply(verts, 4, sizeof(Vertex), sizeof(SkPoint));
396
397 fMesh = helper.mesh();
398 }
399
400 GrPathUtils::QuadUVMatrix fDevToUV;
401
402 inline static constexpr int kVertsPerCubic = 4;
403 inline static constexpr int kIndicesPerCubic = 6;
404
405 using INHERITED = BezierTestOp;
406 };
407
408 /**
409 * This GM directly exercises effects that draw Bezier quad curves in the GPU backend.
410 */
411 class BezierQuadEffects : public GpuGM {
412 public:
413 BezierQuadEffects() {
414 this->setBGColor(0xFFFFFFFF);
415 }
416
417 protected:
418 static const int kNumQuads = 5;
419 static const int kCellWidth = 128;
420 static const int kCellHeight = 128;
421
422 SkString getName() const override { return SkString("bezier_quad_effects"); }
423
424 SkISize getISize() override { return SkISize::Make(kCellWidth, kNumQuads * kCellHeight); }
425
426 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
427 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
428 if (!sdc) {
429 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
430 return DrawResult::kSkip;
431 }
432
433 const SkScalar w = kCellWidth, h = kCellHeight;
434 const SkPMColor4f kOpaqueBlack = SkPMColor4f::FromBytes_RGBA(0xff000000);
435
436 const SkPoint baseControlPts[kNumQuads][3] = {
437 { { 0.31f * w, 0.01f * h}, { 0.48f * w, 0.74f * h }, { 0.19f * w, 0.33f * h } },
438 { { 0.00f * w, 0.07f * h}, { 0.30f * w, 0.70f * h }, { 0.47f * w, 0.37f * h } },
439 { { 0.15f * w, 0.23f * h}, { 0.49f * w, 0.87f * h }, { 0.85f * w, 0.66f * h } },
440 { { 0.09f * w, 0.15f * h}, { 0.42f * w, 0.33f * h }, { 0.17f * w, 0.38f * h } },
441 { { 0.98f * w, 0.54f * h}, { 0.83f * w, 0.91f * h }, { 0.62f * w, 0.40f * h } },
442 };
443
444 SkPaint ctrlPtPaint;
445 ctrlPtPaint.setColor(SK_ColorRED);
446
447 SkPaint choppedPtPaint;
448 choppedPtPaint.setColor(~ctrlPtPaint.getColor() | 0xFF000000);
449
450 SkPaint polyPaint;
451 polyPaint.setColor(0xffA0A0A0);
452 polyPaint.setStrokeWidth(0);
453 polyPaint.setStyle(SkPaint::kStroke_Style);
454
455 SkPaint boundsPaint;
456 boundsPaint.setColor(0xff808080);
457 boundsPaint.setStrokeWidth(0);
458 boundsPaint.setStyle(SkPaint::kStroke_Style);
459
460 for (int row = 0; row < kNumQuads; ++row) {
461 SkScalar x = 0;
462 SkScalar y = row * h;
463 SkPoint controlPts[] = {
464 {x + baseControlPts[row][0].fX, y + baseControlPts[row][0].fY},
465 {x + baseControlPts[row][1].fX, y + baseControlPts[row][1].fY},
466 {x + baseControlPts[row][2].fX, y + baseControlPts[row][2].fY}
467 };
468
469 for (int i = 0; i < 3; ++i) {
470 canvas->drawCircle(controlPts[i], 6.f, ctrlPtPaint);
471 }
472
473 canvas->drawPoints(SkCanvas::kPolygon_PointMode, controlPts, polyPaint);
474
475 SkPoint chopped[5];
476 int cnt = SkChopQuadAtMaxCurvature(controlPts, chopped);
477
478 for (int c = 0; c < cnt; ++c) {
479 SkPoint* pts = chopped + 2 * c;
480
481 for (int i = 0; i < 3; ++i) {
482 canvas->drawCircle(pts[i], 3.f, choppedPtPaint);
483 }
484
485 const auto bounds = SkRect::BoundsOrEmpty({pts, 3});
486
487 canvas->drawRect(bounds, boundsPaint);
488
489 GrPathUtils::QuadUVMatrix DevToUV(pts);
490
491 GrOp::Owner op = BezierQuadTestOp::Make(rContext, bounds,
492 kOpaqueBlack, DevToUV);
493 sdc->addDrawOp(std::move(op));
494 }
495 }
496
497 return DrawResult::kOk;
498 }
499
500 private:
501 using INHERITED = GM;
502 };
503
504 DEF_GM(return new BezierConicEffects;)
505 DEF_GM(return new BezierQuadEffects;)
506 } // namespace skiagm