• 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/GrResourceProvider.h"
13 
GrMeshDrawOp(uint32_t classID)14 GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
15 
onPrepare(GrOpFlushState * state)16 void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
17 
18 //////////////////////////////////////////////////////////////////////////////
19 
PatternHelper(Target * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount,int maxRepetitions)20 GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
21                                            size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
22                                            int verticesPerRepetition, int indicesPerRepetition,
23                                            int repeatCount, int maxRepetitions) {
24     this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
25                indicesPerRepetition, repeatCount, maxRepetitions);
26 }
27 
init(Target * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount,int maxRepetitions)28 void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
29                                        size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
30                                        int verticesPerRepetition, int indicesPerRepetition,
31                                        int repeatCount, int maxRepetitions) {
32     SkASSERT(target);
33     if (!indexBuffer) {
34         return;
35     }
36     sk_sp<const GrBuffer> vertexBuffer;
37     int firstVertex;
38     int vertexCount = verticesPerRepetition * repeatCount;
39     fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
40     if (!fVertices) {
41         SkDebugf("Vertices could not be allocated for patterned rendering.");
42         return;
43     }
44     SkASSERT(vertexBuffer);
45     fMesh = target->allocMesh();
46     fPrimitiveType = primitiveType;
47 
48     SkASSERT(maxRepetitions ==
49              static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
50     fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
51                                repeatCount, maxRepetitions);
52     fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
53 }
54 
recordDraw(Target * target,const GrGeometryProcessor * gp) const55 void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
56     target->recordDraw(gp, fMesh, 1, fPrimitiveType);
57 }
58 
recordDraw(Target * target,const GrGeometryProcessor * gp,const GrPipeline::FixedDynamicState * fixedDynamicState) const59 void GrMeshDrawOp::PatternHelper::recordDraw(
60         Target* target, const GrGeometryProcessor* gp,
61         const GrPipeline::FixedDynamicState* fixedDynamicState) const {
62     target->recordDraw(gp, fMesh, 1, fixedDynamicState, nullptr, fPrimitiveType);
63 }
64 
65 //////////////////////////////////////////////////////////////////////////////
66 
QuadHelper(Target * target,size_t vertexStride,int quadsToDraw)67 GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
68     sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
69     if (!indexBuffer) {
70         SkDebugf("Could not get quad index buffer.");
71         return;
72     }
73     this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
74                GrResourceProvider::NumVertsPerNonAAQuad(),
75                GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
76                GrResourceProvider::MaxNumNonAAQuads());
77 }
78 
79 //////////////////////////////////////////////////////////////////////////////
80 
AllocDynamicStateArrays(SkArenaAlloc * arena,int numMeshes,int numPrimitiveProcessorTextures,bool allocScissors)81 GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::AllocDynamicStateArrays(
82         SkArenaAlloc* arena, int numMeshes, int numPrimitiveProcessorTextures,
83         bool allocScissors) {
84 
85     auto result = arena->make<GrPipeline::DynamicStateArrays>();
86 
87     if (allocScissors) {
88         result->fScissorRects = arena->makeArray<SkIRect>(numMeshes);
89     }
90 
91     if (numPrimitiveProcessorTextures) {
92         result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrSurfaceProxy*>(
93                         numPrimitiveProcessorTextures * numMeshes);
94     }
95 
96     return result;
97 }
98 
MakeFixedDynamicState(SkArenaAlloc * arena,const GrAppliedClip * clip,int numPrimProcTextures)99 GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::MakeFixedDynamicState(
100         SkArenaAlloc* arena, const GrAppliedClip* clip, int numPrimProcTextures) {
101 
102     bool haveScissor = clip && clip->scissorState().enabled();
103 
104     if (haveScissor || numPrimProcTextures) {
105         auto result = arena->make<GrPipeline::FixedDynamicState>();
106 
107         if (haveScissor) {
108             result->fScissorRect = clip->scissorState().rect();
109         }
110 
111         if (numPrimProcTextures) {
112             result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrSurfaceProxy*>(
113                         numPrimProcTextures);
114         }
115         return result;
116     }
117     return nullptr;
118 }
119