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 "src/gpu/GrResourceProvider.h"
14 
GrMeshDrawOp(uint32_t classID)15 GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
16 
onPrepare(GrOpFlushState * state)17 void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
18 
createProgramInfo(Target * target)19 void GrMeshDrawOp::createProgramInfo(Target* target) {
20     this->createProgramInfo(&target->caps(),
21                             target->allocator(),
22                             target->writeView(),
23                             target->detachAppliedClip(),
24                             target->dstProxyView(),
25                             target->renderPassBarriers(),
26                             target->colorLoadOp());
27 }
28 
29 // This onPrepareDraws implementation assumes the derived Op only has a single programInfo -
30 // which is the majority of the cases.
onPrePrepareDraws(GrRecordingContext * context,const GrSurfaceProxyView & writeView,GrAppliedClip * clip,const GrXferProcessor::DstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)31 void GrMeshDrawOp::onPrePrepareDraws(GrRecordingContext* context,
32                                      const GrSurfaceProxyView& writeView,
33                                      GrAppliedClip* clip,
34                                      const GrXferProcessor::DstProxyView& dstProxyView,
35                                      GrXferBarrierFlags renderPassXferBarriers,
36                                      GrLoadOp colorLoadOp) {
37     SkArenaAlloc* arena = context->priv().recordTimeAllocator();
38 
39     // This is equivalent to a GrOpFlushState::detachAppliedClip
40     GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
41 
42     this->createProgramInfo(context->priv().caps(), arena, writeView,
43                             std::move(appliedClip), dstProxyView, renderPassXferBarriers,
44                             colorLoadOp);
45 
46     // TODO: at this point we've created both the program info and desc in the recording context's
47     // arena. In the DDL case, it would be cool if 'recordProgramInfo' could return the
48     // pre-existing versions if the program has already been seen. We could then return the
49     // memory for the current copy to the arena.
50     context->priv().recordProgramInfo(this->programInfo());
51 }
52 
53 //////////////////////////////////////////////////////////////////////////////
54 
PatternHelper(Target * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount,int maxRepetitions)55 GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
56                                            size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
57                                            int verticesPerRepetition, int indicesPerRepetition,
58                                            int repeatCount, int maxRepetitions) {
59     this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
60                indicesPerRepetition, repeatCount, maxRepetitions);
61 }
62 
init(Target * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount,int maxRepetitions)63 void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
64                                        size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
65                                        int verticesPerRepetition, int indicesPerRepetition,
66                                        int repeatCount, int maxRepetitions) {
67     SkASSERT(target);
68     if (!indexBuffer) {
69         return;
70     }
71     sk_sp<const GrBuffer> vertexBuffer;
72     int firstVertex;
73     int vertexCount = verticesPerRepetition * repeatCount;
74     fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
75     if (!fVertices) {
76         SkDebugf("Vertices could not be allocated for patterned rendering.");
77         return;
78     }
79     SkASSERT(vertexBuffer);
80     fMesh = target->allocMesh();
81     fPrimitiveType = primitiveType;
82 
83     SkASSERT(maxRepetitions ==
84              static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
85     fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
86                                maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
87                                firstVertex);
88 }
89 
recordDraw(Target * target,const GrGeometryProcessor * gp) const90 void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
91     target->recordDraw(gp, fMesh, 1, fPrimitiveType);
92 }
93 
recordDraw(Target * target,const GrGeometryProcessor * gp,const GrSurfaceProxy * const primProcProxies[]) const94 void GrMeshDrawOp::PatternHelper::recordDraw(
95         Target* target,
96         const GrGeometryProcessor* gp,
97         const GrSurfaceProxy* const primProcProxies[]) const {
98     target->recordDraw(gp, fMesh, 1, primProcProxies, fPrimitiveType);
99 }
100 
101 //////////////////////////////////////////////////////////////////////////////
102 
QuadHelper(Target * target,size_t vertexStride,int quadsToDraw)103 GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
104     sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
105     if (!indexBuffer) {
106         SkDebugf("Could not get quad index buffer.");
107         return;
108     }
109     this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
110                GrResourceProvider::NumVertsPerNonAAQuad(),
111                GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
112                GrResourceProvider::MaxNumNonAAQuads());
113 }
114