Debian Codegraph @

tools/viewer/EdgeBuilderVizSlide.cpp

raw
1 /*
2 * Copyright 2025 Google LLC
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 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkImage.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkPathBuilder.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkRRect.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkSurface.h"
20 #include "include/private/base/SkAssert.h"
21 #include "src/core/SkDebugUtils.h"
22 #include "src/core/SkEdge.h"
23 #include "src/core/SkEdgeBuilder.h"
24 #include "tools/viewer/Slide.h"
25 #include "tools/viewer/ZoomInSlide.h"
26
27 #include <vector>
28
29 /* This slide visualizes the edges created from bezier curves in SkPath. These edges are the ones
30 used by the aliased scan converter (e.g. SkScan_Path.cpp).
31
32 Hold control to move the control point (which can be used in the path creation).
33 */
34 class EdgeBuilderSlide : public ZoomInSlide {
35 public:
36 EdgeBuilderSlide() : ZoomInSlide(kScale, kMaskWidth, kMaskHeight, SkString("EdgeBuilderViz")) {}
37
38 SkPath makePath() {
39 return SkPathBuilder()
40 .moveTo(0, 0)
41 //.quadTo({fControl, {10, 20})
42 .cubicTo(fControl, {8, 6}, {10, 20})
43 .lineTo({10, 10})
44 .lineTo({12, 0})
45 .close()
46 .detach();
47 }
48
49 void drawUnderGrid(SkCanvas* canvas) override {
50 SkPaint paint;
51 paint.setStyle(SkPaint::Style::kFill_Style);
52 paint.setColor(SK_ColorBLACK);
53 this->drawScaledPath(canvas, makePath(), paint);
54 }
55
56 void drawOverGrid(SkCanvas* canvas) override {
57 SkPath path = makePath();
58 drawHighRezOverlay(canvas, path);
59 drawEdges(canvas, path);
60 drawControlPoint(canvas);
61 }
62
63 void drawHighRezOverlay(SkCanvas* canvas, const SkPath& path) {
64 SkPaint truthPaint;
65 truthPaint.setStyle(SkPaint::Style::kStroke_Style);
66 truthPaint.setColor(SK_ColorRED);
67 truthPaint.setAntiAlias(true);
68 truthPaint.setStrokeWidth(2.f / kScale);
69
70 canvas->drawPath(path, truthPaint);
71 }
72
73 void drawEdges(SkCanvas* canvas, const SkPath& path) {
74 SkBasicEdgeBuilder builder;
75 int num = builder.buildEdges(path, nullptr);
76 SkEdge** edgeList = builder.edgeList();
77
78 SkPaint edgePaint;
79 edgePaint.setColor(SkColorSetRGB(0xFF, 0x8C, 0x00));
80 edgePaint.setStyle(SkPaint::Style::kStroke_Style);
81 edgePaint.setStrokeWidth(4.f / kScale);
82
83 for (int i = 0; i < num; ++i) {
84 SkEdge* e = edgeList[i];
85 while (true) {
86 float x1 = SkFixedToFloat(e->fX);
87 float y1 = e->fFirstY;
88 float y2 = e->fLastY;
89 float x2 = x1 + SkFixedToFloat(e->fDxDy) * (y2 - y1);
90 if (x1 == x2 && y1 == y2) {
91 x2 += 0.2f; // Make "zero height" edges visible.
92 }
93 // The y coordinates are implied to be at the half pixel values for y
94 canvas->drawLine(x1, y1 + 0.5, x2, y2 + 0.5, edgePaint);
95 if (!e->hasNextSegment()) {
96 break;
97 }
98 e->fX = x2;
99 if (!e->nextSegment()) {
100 break;
101 }
102 }
103 }
104 }
105
106 void drawControlPoint(SkCanvas* canvas) {
107 SkPaint controlPaint;
108 controlPaint.setStyle(SkPaint::Style::kStroke_Style);
109 controlPaint.setColor(SK_ColorBLUE);
110 controlPaint.setAntiAlias(true);
111 controlPaint.setStrokeWidth(1.f / kScale);
112
113 canvas->drawCircle(fControl, 4.f / kScale, controlPaint);
114 }
115
116 void handleClick(const ScaledClick* click) override { fControl = click->currScaled(); }
117
118 private:
119 static constexpr size_t kScale = 32;
120 // Defines the region of the screen that will be zoomed in on.
121 // The path under scrutiny will be drawn in this region.
122 static constexpr int kMaskWidth = 15;
123 static constexpr int kMaskHeight = 25;
124
125 SkPoint fControl = {2.f, 13.f};
126 };
127 DEF_SLIDE(return new EdgeBuilderSlide();)