xref: /aosp_15_r20/external/skia/src/gpu/ganesh/ops/GrMeshDrawOp.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
9 
10 #include "include/gpu/ganesh/GrRecordingContext.h"
11 #include "include/private/base/SkAssert.h"
12 #include "include/private/base/SkDebug.h"
13 #include "include/private/base/SkMath.h"
14 #include "src/gpu/ganesh/GrBuffer.h"
15 #include "src/gpu/ganesh/GrMeshDrawTarget.h"
16 #include "src/gpu/ganesh/GrOpFlushState.h"
17 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
18 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
19 #include "src/gpu/ganesh/GrResourceProvider.h"
20 #include "src/gpu/ganesh/GrSimpleMesh.h"
21 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
22 
23 class GrGpuBuffer;
24 
GrMeshDrawOp(uint32_t classID)25 GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
26 
onPrepare(GrOpFlushState * state)27 void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
28 
createProgramInfo(GrMeshDrawTarget * target)29 void GrMeshDrawOp::createProgramInfo(GrMeshDrawTarget* target) {
30     this->createProgramInfo(&target->caps(),
31                             target->allocator(),
32                             target->writeView(),
33                             target->usesMSAASurface(),
34                             target->detachAppliedClip(),
35                             target->dstProxyView(),
36                             target->renderPassBarriers(),
37                             target->colorLoadOp());
38 }
39 
CombinedQuadCountWillOverflow(GrAAType aaType,bool willBeUpgradedToAA,int combinedQuadCount)40 bool GrMeshDrawOp::CombinedQuadCountWillOverflow(GrAAType aaType,
41                                                  bool willBeUpgradedToAA,
42                                                  int combinedQuadCount) {
43     bool willBeAA = (aaType == GrAAType::kCoverage) || willBeUpgradedToAA;
44 
45     return combinedQuadCount > (willBeAA ? GrResourceProvider::MaxNumAAQuads()
46                                          : GrResourceProvider::MaxNumNonAAQuads());
47 }
48 
49 // This onPrepareDraws implementation assumes the derived Op only has a single programInfo -
50 // which is the majority of the cases.
onPrePrepareDraws(GrRecordingContext * context,const GrSurfaceProxyView & writeView,GrAppliedClip * clip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)51 void GrMeshDrawOp::onPrePrepareDraws(GrRecordingContext* context,
52                                      const GrSurfaceProxyView& writeView,
53                                      GrAppliedClip* clip,
54                                      const GrDstProxyView& dstProxyView,
55                                      GrXferBarrierFlags renderPassXferBarriers,
56                                      GrLoadOp colorLoadOp) {
57     SkArenaAlloc* arena = context->priv().recordTimeAllocator();
58 
59     // http://skbug.com/12201 -- DDL does not yet support DMSAA.
60     bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
61 
62     // This is equivalent to a GrOpFlushState::detachAppliedClip
63     GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
64 
65     this->createProgramInfo(context->priv().caps(), arena, writeView, usesMSAASurface,
66                             std::move(appliedClip), dstProxyView, renderPassXferBarriers,
67                             colorLoadOp);
68 
69     // TODO: at this point we've created both the program info and desc in the recording context's
70     // arena. In the DDL case, it would be cool if 'recordProgramInfo' could return the
71     // pre-existing versions if the program has already been seen. We could then return the
72     // memory for the current copy to the arena.
73     context->priv().recordProgramInfo(this->programInfo());
74 }
75 
76 //////////////////////////////////////////////////////////////////////////////
77 
PatternHelper(GrMeshDrawTarget * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount,int maxRepetitions)78 GrMeshDrawOp::PatternHelper::PatternHelper(GrMeshDrawTarget* target, GrPrimitiveType primitiveType,
79                                            size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
80                                            int verticesPerRepetition, int indicesPerRepetition,
81                                            int repeatCount, int maxRepetitions) {
82     this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
83                indicesPerRepetition, repeatCount, maxRepetitions);
84 }
85 
init(GrMeshDrawTarget * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount,int maxRepetitions)86 void GrMeshDrawOp::PatternHelper::init(GrMeshDrawTarget* target, GrPrimitiveType primitiveType,
87                                        size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
88                                        int verticesPerRepetition, int indicesPerRepetition,
89                                        int repeatCount, int maxRepetitions) {
90     SkASSERT(target);
91     if (!indexBuffer) {
92         return;
93     }
94 
95     // Bail out when we get overflow from really large draws.
96     if (repeatCount < 0 || repeatCount > SK_MaxS32 / verticesPerRepetition) {
97         return;
98     }
99 
100     sk_sp<const GrBuffer> vertexBuffer;
101     int firstVertex;
102     int vertexCount = verticesPerRepetition * repeatCount;
103     fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
104     if (!fVertices) {
105         SkDebugf("Vertices could not be allocated for patterned rendering.");
106         return;
107     }
108     SkASSERT(vertexBuffer);
109     fMesh = target->allocMesh();
110     fPrimitiveType = primitiveType;
111 
112     SkASSERT(maxRepetitions ==
113              static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
114     fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
115                                maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
116                                firstVertex);
117 }
118 
recordDraw(GrMeshDrawTarget * target,const GrGeometryProcessor * gp) const119 void GrMeshDrawOp::PatternHelper::recordDraw(GrMeshDrawTarget* target,
120                                              const GrGeometryProcessor* gp) const {
121     target->recordDraw(gp, fMesh, 1, fPrimitiveType);
122 }
123 
recordDraw(GrMeshDrawTarget * target,const GrGeometryProcessor * gp,const GrSurfaceProxy * const primProcProxies[]) const124 void GrMeshDrawOp::PatternHelper::recordDraw(
125         GrMeshDrawTarget* target,
126         const GrGeometryProcessor* gp,
127         const GrSurfaceProxy* const primProcProxies[]) const {
128     target->recordDraw(gp, fMesh, 1, primProcProxies, fPrimitiveType);
129 }
130 
131 //////////////////////////////////////////////////////////////////////////////
132 
QuadHelper(GrMeshDrawTarget * target,size_t vertexStride,int quadsToDraw)133 GrMeshDrawOp::QuadHelper::QuadHelper(GrMeshDrawTarget* target,
134                                      size_t vertexStride,
135                                      int quadsToDraw) {
136     sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
137     if (!indexBuffer) {
138         SkDebugf("Could not get quad index buffer.");
139         return;
140     }
141     this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
142                GrResourceProvider::NumVertsPerNonAAQuad(),
143                GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
144                GrResourceProvider::MaxNumNonAAQuads());
145 }
146