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