xref: /aosp_15_r20/external/skia/src/gpu/ganesh/ops/PathTessellateOp.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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 #include "src/gpu/ganesh/ops/PathTessellateOp.h"
8 
9 #include "include/core/SkColor.h"
10 #include "include/gpu/ganesh/GrRecordingContext.h"
11 #include "src/gpu/ganesh/GrAppliedClip.h"
12 #include "src/gpu/ganesh/GrCaps.h"
13 #include "src/gpu/ganesh/GrOpFlushState.h"
14 #include "src/gpu/ganesh/GrPipeline.h"
15 #include "src/gpu/ganesh/GrProcessorAnalysis.h"
16 #include "src/gpu/ganesh/GrProgramInfo.h"
17 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
18 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
19 #include "src/gpu/ganesh/GrShaderCaps.h"
20 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
21 #include "src/gpu/ganesh/tessellate/GrPathTessellationShader.h"
22 
23 namespace skgpu::ganesh {
24 
visitProxies(const GrVisitProxyFunc & func) const25 void PathTessellateOp::visitProxies(const GrVisitProxyFunc& func) const {
26     if (fTessellationProgram) {
27         fTessellationProgram->pipeline().visitProxies(func);
28     } else {
29         fProcessors.visitProxies(func);
30     }
31 }
32 
finalize(const GrCaps & caps,const GrAppliedClip * clip,GrClampType clampType)33 GrProcessorSet::Analysis PathTessellateOp::finalize(const GrCaps& caps,
34                                                     const GrAppliedClip* clip,
35                                                     GrClampType clampType) {
36     auto analysis = fProcessors.finalize(this->headDraw().fColor,
37                                          GrProcessorAnalysisCoverage::kNone,
38                                          clip,
39                                          nullptr,
40                                          caps,
41                                          clampType,
42                                          &this->headDraw().fColor);
43     if (!analysis.usesLocalCoords()) {
44         // Since we don't need local coords, we can transform on CPU instead of in the shader. This
45         // gives us better batching potential.
46         this->headDraw().fPathMatrix = fShaderMatrix;
47         fShaderMatrix = SkMatrix::I();
48     }
49     return analysis;
50 }
51 
onCombineIfPossible(GrOp * grOp,SkArenaAlloc *,const GrCaps &)52 GrDrawOp::CombineResult PathTessellateOp::onCombineIfPossible(GrOp* grOp,
53                                                               SkArenaAlloc*,
54                                                               const GrCaps&) {
55     auto* op = grOp->cast<PathTessellateOp>();
56     bool canMerge = fAAType == op->fAAType &&
57                     fStencil == op->fStencil &&
58                     fProcessors == op->fProcessors &&
59                     fShaderMatrix == op->fShaderMatrix;
60     if (canMerge) {
61         fTotalCombinedPathVerbCnt += op->fTotalCombinedPathVerbCnt;
62         fPatchAttribs |= op->fPatchAttribs;
63 
64         if (!(fPatchAttribs & PatchAttribs::kColor) &&
65             this->headDraw().fColor != op->headDraw().fColor) {
66             // Color is no longer uniform. Move it into patch attribs.
67             fPatchAttribs |= PatchAttribs::kColor;
68         }
69 
70         *fPathDrawTail = op->fPathDrawList;
71         fPathDrawTail = op->fPathDrawTail;
72         return CombineResult::kMerged;
73     }
74 
75     return CombineResult::kCannotCombine;
76 }
77 
prepareTessellator(const GrTessellationShader::ProgramArgs & args,GrAppliedClip && appliedClip)78 void PathTessellateOp::prepareTessellator(const GrTessellationShader::ProgramArgs& args,
79                                           GrAppliedClip&& appliedClip) {
80     SkASSERT(!fTessellator);
81     SkASSERT(!fTessellationProgram);
82     auto* pipeline = GrTessellationShader::MakePipeline(args, fAAType, std::move(appliedClip),
83                                                         std::move(fProcessors));
84     fTessellator = PathWedgeTessellator::Make(args.fArena,
85                                               args.fCaps->shaderCaps()->fInfinitySupport,
86                                               fPatchAttribs);
87     auto* tessShader = GrPathTessellationShader::Make(*args.fCaps->shaderCaps(),
88                                                       args.fArena,
89                                                       fShaderMatrix,
90                                                       this->headDraw().fColor,
91                                                       fTessellator->patchAttribs());
92     fTessellationProgram = GrTessellationShader::MakeProgram(args, tessShader, pipeline, fStencil);
93 }
94 
onPrePrepare(GrRecordingContext * context,const GrSurfaceProxyView & writeView,GrAppliedClip * clip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)95 void PathTessellateOp::onPrePrepare(GrRecordingContext* context,
96                                     const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
97                                     const GrDstProxyView& dstProxyView,
98                                     GrXferBarrierFlags renderPassXferBarriers,
99                                     GrLoadOp colorLoadOp) {
100     // DMSAA is not supported on DDL.
101     bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
102     this->prepareTessellator({context->priv().recordTimeAllocator(), writeView, usesMSAASurface,
103                              &dstProxyView, renderPassXferBarriers, colorLoadOp,
104                              context->priv().caps()},
105                              (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
106     SkASSERT(fTessellationProgram);
107     context->priv().recordProgramInfo(fTessellationProgram);
108 }
109 
onPrepare(GrOpFlushState * flushState)110 void PathTessellateOp::onPrepare(GrOpFlushState* flushState) {
111     if (!fTessellator) {
112         this->prepareTessellator({flushState->allocator(), flushState->writeView(),
113                                  flushState->usesMSAASurface(), &flushState->dstProxyView(),
114                                  flushState->renderPassBarriers(), flushState->colorLoadOp(),
115                                  &flushState->caps()}, flushState->detachAppliedClip());
116         SkASSERT(fTessellator);
117     }
118     fTessellator->prepare(flushState,
119                           fShaderMatrix,
120                           *fPathDrawList,
121                           fTotalCombinedPathVerbCnt);
122 }
123 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)124 void PathTessellateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
125     SkASSERT(fTessellator);
126     SkASSERT(fTessellationProgram);
127     flushState->bindPipelineAndScissorClip(*fTessellationProgram, this->bounds());
128     flushState->bindTextures(fTessellationProgram->geomProc(), nullptr,
129                              fTessellationProgram->pipeline());
130     fTessellator->draw(flushState);
131 }
132 
133 }  // namespace skgpu::ganesh
134