| 1 |
/* |
| 2 |
* Copyright 2019 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 "src/core/SkPathPriv.h" |
| 10 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 11 |
|
| 12 |
#if defined(SK_GANESH) |
| 13 |
|
| 14 |
#include "include/core/SkFont.h" |
| 15 |
#include "src/core/SkCanvasPriv.h" |
| 16 |
#include "src/gpu/ganesh/GrCanvas.h" |
| 17 |
#include "src/gpu/ganesh/GrCaps.h" |
| 18 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 19 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 20 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 21 |
#include "src/gpu/ganesh/ops/GrDrawOp.h" |
| 22 |
#include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h" |
| 23 |
#include "src/gpu/ganesh/ops/TessellationPathRenderer.h" |
| 24 |
#include "src/gpu/ganesh/tessellate/GrPathTessellationShader.h" |
| 25 |
#include "src/gpu/ganesh/tessellate/PathTessellator.h" |
| 26 |
#include "src/gpu/tessellate/AffineMatrix.h" |
| 27 |
#include "src/gpu/tessellate/MiddleOutPolygonTriangulator.h" |
| 28 |
#include "tools/fonts/FontToolUtils.h" |
| 29 |
|
| 30 |
namespace skgpu::ganesh { |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
enum class Mode { |
| 35 |
kWedgeMiddleOut, |
| 36 |
kCurveMiddleOut |
| 37 |
}; |
| 38 |
|
| 39 |
static const char* ModeName(Mode mode) { |
| 40 |
switch (mode) { |
| 41 |
case Mode::kWedgeMiddleOut: |
| 42 |
return "MiddleOutShader (kWedges)"; |
| 43 |
case Mode::kCurveMiddleOut: |
| 44 |
return "MiddleOutShader (kCurves)"; |
| 45 |
} |
| 46 |
SkUNREACHABLE; |
| 47 |
} |
| 48 |
|
| 49 |
// Draws a path directly to the screen using a specific tessellator. |
| 50 |
class SamplePathTessellatorOp : public GrDrawOp { |
| 51 |
private: |
| 52 |
DEFINE_OP_CLASS_ID |
| 53 |
|
| 54 |
SamplePathTessellatorOp(const SkRect& drawBounds, const SkPath& path, const SkMatrix& m, |
| 55 |
GrPipeline::InputFlags pipelineFlags, Mode mode) |
| 56 |
: GrDrawOp(ClassID()) |
| 57 |
, fPath(path) |
| 58 |
, fMatrix(m) |
| 59 |
, fPipelineFlags(pipelineFlags) |
| 60 |
, fMode(mode) { |
| 61 |
this->setBounds(drawBounds, HasAABloat::kNo, IsHairline::kNo); |
| 62 |
} |
| 63 |
const char* name() const override { return "SamplePathTessellatorOp"; } |
| 64 |
void visitProxies(const GrVisitProxyFunc&) const override {} |
| 65 |
FixedFunctionFlags fixedFunctionFlags() const override { |
| 66 |
return FixedFunctionFlags::kUsesHWAA; |
| 67 |
} |
| 68 |
GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 69 |
GrClampType clampType) override { |
| 70 |
SkPMColor4f color; |
| 71 |
return fProcessors.finalize(SK_PMColor4fWHITE, GrProcessorAnalysisCoverage::kNone, clip, |
| 72 |
nullptr, caps, clampType, &color); |
| 73 |
} |
| 74 |
void onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView&, GrAppliedClip*, |
| 75 |
const GrDstProxyView&, GrXferBarrierFlags, GrLoadOp colorLoadOp) override {} |
| 76 |
void onPrepare(GrOpFlushState* flushState) override { |
| 77 |
constexpr static SkPMColor4f kCyan = {0,1,1,1}; |
| 78 |
auto alloc = flushState->allocator(); |
| 79 |
const SkMatrix& shaderMatrix = SkMatrix::I(); |
| 80 |
const SkMatrix& pathMatrix = fMatrix; |
| 81 |
const GrCaps& caps = flushState->caps(); |
| 82 |
const GrShaderCaps& shaderCaps = *caps.shaderCaps(); |
| 83 |
|
| 84 |
PathTessellator::PathDrawList pathList{pathMatrix, fPath, kCyan}; |
| 85 |
if (fMode == Mode::kCurveMiddleOut) { |
| 86 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 87 |
// This emulates what PathStencilCoverOp does when using curves, except we include the |
| 88 |
// middle-out triangles directly in the written patches for convenience (normally they |
| 89 |
// use a simple triangle pipeline). But PathCurveTessellator only knows how to read |
| 90 |
// extra triangles from BreadcrumbTriangleList, so build on from the middle-out stack. |
| 91 |
SkArenaAlloc storage{256}; |
| 92 |
GrInnerFanTriangulator::BreadcrumbTriangleList triangles; |
| 93 |
for (tess::PathMiddleOutFanIter it(fPath); !it.done();) { |
| 94 |
for (auto [p0, p1, p2] : it.nextStack()) { |
| 95 |
triangles.append(&storage, |
| 96 |
pathMatrix.mapPoint(p0), |
| 97 |
pathMatrix.mapPoint(p1), |
| 98 |
pathMatrix.mapPoint(p2), |
| 99 |
/*winding=*/1); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
auto* tess = PathCurveTessellator::Make(alloc, shaderCaps.fInfinitySupport); |
| 104 |
tess->prepareWithTriangles(flushState, shaderMatrix, &triangles, pathList, |
| 105 |
fPath.countVerbs()); |
| 106 |
fTessellator = tess; |
| 107 |
#else |
| 108 |
auto* tess = PathCurveTessellator::Make(alloc, shaderCaps.fInfinitySupport); |
| 109 |
tess->prepareWithTriangles(flushState, shaderMatrix, nullptr, pathList, |
| 110 |
fPath.countVerbs()); |
| 111 |
fTessellator = tess; |
| 112 |
#endif |
| 113 |
} else { |
| 114 |
// This emulates what PathStencilCoverOp does when using wedges. |
| 115 |
fTessellator = PathWedgeTessellator::Make(alloc, shaderCaps.fInfinitySupport); |
| 116 |
fTessellator->prepare(flushState, shaderMatrix, pathList, fPath.countVerbs()); |
| 117 |
} |
| 118 |
|
| 119 |
auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, std::move(fProcessors), |
| 120 |
fPipelineFlags); |
| 121 |
auto* tessShader = GrPathTessellationShader::Make(*caps.shaderCaps(), |
| 122 |
alloc, |
| 123 |
shaderMatrix, |
| 124 |
kCyan, |
| 125 |
fTessellator->patchAttribs()); |
| 126 |
fProgram = GrTessellationShader::MakeProgram({alloc, flushState->writeView(), |
| 127 |
flushState->usesMSAASurface(), |
| 128 |
&flushState->dstProxyView(), |
| 129 |
flushState->renderPassBarriers(), |
| 130 |
GrLoadOp::kClear, &flushState->caps()}, |
| 131 |
tessShader, |
| 132 |
pipeline, |
| 133 |
&GrUserStencilSettings::kUnused); |
| 134 |
} |
| 135 |
|
| 136 |
void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 137 |
flushState->bindPipeline(*fProgram, chainBounds); |
| 138 |
fTessellator->draw(flushState); |
| 139 |
} |
| 140 |
|
| 141 |
const SkPath fPath; |
| 142 |
const SkMatrix fMatrix; |
| 143 |
const GrPipeline::InputFlags fPipelineFlags; |
| 144 |
const Mode fMode; |
| 145 |
PathTessellator* fTessellator = nullptr; |
| 146 |
GrProgramInfo* fProgram; |
| 147 |
GrProcessorSet fProcessors{SkBlendMode::kSrcOver}; |
| 148 |
|
| 149 |
friend class GrOp; // For ctor. |
| 150 |
}; |
| 151 |
|
| 152 |
} // namespace |
| 153 |
|
| 154 |
// This slide enables wireframe and visualizes the triangles generated by path tessellators. |
| 155 |
class PathTessellatorsSlide : public ClickHandlerSlide { |
| 156 |
public: |
| 157 |
PathTessellatorsSlide() { |
| 158 |
#if 0 |
| 159 |
// For viewing middle-out triangulations of the inner fan. |
| 160 |
fPath.moveTo(1, 0); |
| 161 |
int numSides = 32 * 3; |
| 162 |
for (int i = 1; i < numSides; ++i) { |
| 163 |
float theta = 2*3.1415926535897932384626433832785 * i / numSides; |
| 164 |
fPath.lineTo(std::cos(theta), std::sin(theta)); |
| 165 |
} |
| 166 |
fPath.transform(SkMatrix::Scale(200, 200)); |
| 167 |
fPath.transform(SkMatrix::Translate(300, 300)); |
| 168 |
#else |
| 169 |
fPath = SkPathBuilder() |
| 170 |
.moveTo(100, 500) |
| 171 |
.cubicTo(300, 400, -100, 300, 100, 200) |
| 172 |
.quadTo(250, 0, 400, 200) |
| 173 |
.conicTo(600, 350, 400, 500, fConicWeight) |
| 174 |
.close() |
| 175 |
.detach(); |
| 176 |
#endif |
| 177 |
fName = "PathTessellators"; |
| 178 |
} |
| 179 |
|
| 180 |
bool onChar(SkUnichar) override; |
| 181 |
|
| 182 |
void draw(SkCanvas*) override; |
| 183 |
|
| 184 |
protected: |
| 185 |
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override; |
| 186 |
bool onClick(Click*) override; |
| 187 |
|
| 188 |
SkPath fPath; |
| 189 |
GrPipeline::InputFlags fPipelineFlags = GrPipeline::InputFlags::kWireframe; |
| 190 |
Mode fMode = Mode::kWedgeMiddleOut; |
| 191 |
|
| 192 |
float fConicWeight = .5; |
| 193 |
|
| 194 |
class Click; |
| 195 |
}; |
| 196 |
|
| 197 |
void PathTessellatorsSlide::draw(SkCanvas* canvas) { |
| 198 |
canvas->clear(SK_ColorBLACK); |
| 199 |
|
| 200 |
auto ctx = canvas->recordingContext(); |
| 201 |
auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas); |
| 202 |
|
| 203 |
SkString error; |
| 204 |
if (!sdc || !ctx) { |
| 205 |
error = "GPU Only."; |
| 206 |
} else if (!skgpu::ganesh::TessellationPathRenderer::IsSupported(*ctx->priv().caps())) { |
| 207 |
error = "TessellationPathRenderer not supported."; |
| 208 |
} |
| 209 |
if (!error.isEmpty()) { |
| 210 |
canvas->clear(SK_ColorRED); |
| 211 |
SkFont font(ToolUtils::DefaultTypeface(), 20); |
| 212 |
SkPaint captionPaint; |
| 213 |
captionPaint.setColor(SK_ColorWHITE); |
| 214 |
canvas->drawString(error.c_str(), 10, 30, font, captionPaint); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
sdc->addDrawOp(GrOp::Make<SamplePathTessellatorOp>(ctx, |
| 219 |
sdc->asRenderTargetProxy()->getBoundsRect(), |
| 220 |
fPath, canvas->getTotalMatrix(), |
| 221 |
fPipelineFlags, fMode)); |
| 222 |
|
| 223 |
// Draw the path points. |
| 224 |
SkPaint pointsPaint; |
| 225 |
pointsPaint.setColor(SK_ColorBLUE); |
| 226 |
pointsPaint.setStrokeWidth(8); |
| 227 |
SkPath devPath = fPath.makeTransform(canvas->getTotalMatrix()); |
| 228 |
{ |
| 229 |
SkAutoCanvasRestore acr(canvas, true); |
| 230 |
canvas->setMatrix(SkMatrix::I()); |
| 231 |
SkString caption(ModeName(fMode)); |
| 232 |
caption.appendf(" (w=%g)", fConicWeight); |
| 233 |
SkFont font(ToolUtils::DefaultTypeface(), 20); |
| 234 |
SkPaint captionPaint; |
| 235 |
captionPaint.setColor(SK_ColorWHITE); |
| 236 |
canvas->drawString(caption, 10, 30, font, captionPaint); |
| 237 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, devPath.points(), pointsPaint); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
class PathTessellatorsSlide::Click : public ClickHandlerSlide::Click { |
| 242 |
public: |
| 243 |
Click(int ptIdx) : fPtIdx(ptIdx) {} |
| 244 |
|
| 245 |
void doClick(SkPath* path) { |
| 246 |
SkPoint pt = path->points()[fPtIdx]; |
| 247 |
SkPathBuilder builder(*path); |
| 248 |
builder.setPoint(fPtIdx, pt + fCurr - fPrev); |
| 249 |
*path = builder.detach(); |
| 250 |
} |
| 251 |
|
| 252 |
private: |
| 253 |
int fPtIdx; |
| 254 |
}; |
| 255 |
|
| 256 |
ClickHandlerSlide::Click* PathTessellatorsSlide::onFindClickHandler(SkScalar x, SkScalar y, |
| 257 |
skui::ModifierKey) { |
| 258 |
SkSpan<const SkPoint> pts = fPath.points(); |
| 259 |
float fuzz = 30; |
| 260 |
for (int i = 0; i < fPath.countPoints(); ++i) { |
| 261 |
if (fabs(x - pts[i].x()) < fuzz && fabsf(y - pts[i].y()) < fuzz) { |
| 262 |
return new Click(i); |
| 263 |
} |
| 264 |
} |
| 265 |
return nullptr; |
| 266 |
} |
| 267 |
|
| 268 |
bool PathTessellatorsSlide::onClick(ClickHandlerSlide::Click* click) { |
| 269 |
Click* myClick = (Click*)click; |
| 270 |
myClick->doClick(&fPath); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
static SkPath update_weight(const SkPath& path, float w) { |
| 275 |
SkPathBuilder path_; |
| 276 |
for (auto [verb, pts, _] : SkPathPriv::Iterate(path)) { |
| 277 |
switch (verb) { |
| 278 |
case SkPathVerb::kMove: |
| 279 |
path_.moveTo(pts[0]); |
| 280 |
break; |
| 281 |
case SkPathVerb::kLine: |
| 282 |
path_.lineTo(pts[1]); |
| 283 |
break; |
| 284 |
case SkPathVerb::kQuad: |
| 285 |
path_.quadTo(pts[1], pts[2]); |
| 286 |
break; |
| 287 |
case SkPathVerb::kCubic: |
| 288 |
path_.cubicTo(pts[1], pts[2], pts[3]); |
| 289 |
break; |
| 290 |
case SkPathVerb::kConic: |
| 291 |
path_.conicTo(pts[1], pts[2], (w != 1) ? w : .99f); |
| 292 |
break; |
| 293 |
case SkPathVerb::kClose: |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
return path_.detach(); |
| 298 |
} |
| 299 |
|
| 300 |
bool PathTessellatorsSlide::onChar(SkUnichar unichar) { |
| 301 |
switch (unichar) { |
| 302 |
case 'w': |
| 303 |
fPipelineFlags = (GrPipeline::InputFlags)( |
| 304 |
(int)fPipelineFlags ^ (int)GrPipeline::InputFlags::kWireframe); |
| 305 |
return true; |
| 306 |
case 'D': { |
| 307 |
fPath.dump(); |
| 308 |
return true; |
| 309 |
} |
| 310 |
case '+': |
| 311 |
fConicWeight *= 2; |
| 312 |
fPath = update_weight(fPath, fConicWeight); |
| 313 |
return true; |
| 314 |
case '=': |
| 315 |
fConicWeight *= 5/4.f; |
| 316 |
fPath = update_weight(fPath, fConicWeight); |
| 317 |
return true; |
| 318 |
case '_': |
| 319 |
fConicWeight *= .5f; |
| 320 |
fPath = update_weight(fPath, fConicWeight); |
| 321 |
return true; |
| 322 |
case '-': |
| 323 |
fConicWeight *= 4/5.f; |
| 324 |
fPath = update_weight(fPath, fConicWeight); |
| 325 |
return true; |
| 326 |
case '1': |
| 327 |
case '2': |
| 328 |
fMode = (Mode)(unichar - '1'); |
| 329 |
return true; |
| 330 |
} |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
DEF_SLIDE( return new PathTessellatorsSlide; ) |
| 335 |
|
| 336 |
} // namespace skgpu::ganesh |
| 337 |
|
| 338 |
#endif // defined(SK_GANESH) |