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 "GrGpuCommandBuffer.h"
9 #include "GrMeshDrawOp.h"
10 #include "GrOpFlushState.h"
11 #include "GrResourceProvider.h"
12
GrMeshDrawOp(uint32_t classID)13 GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
14
onPrepare(GrOpFlushState * state)15 void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
16
onExecute(GrOpFlushState * state,const SkRect & chainBounds)17 void GrMeshDrawOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
18 state->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds);
19 }
20
21 //////////////////////////////////////////////////////////////////////////////
22
PatternHelper(Target * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount)23 GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
24 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
25 int verticesPerRepetition, int indicesPerRepetition,
26 int repeatCount) {
27 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
28 indicesPerRepetition, repeatCount);
29 }
30
init(Target * target,GrPrimitiveType primitiveType,size_t vertexStride,sk_sp<const GrBuffer> indexBuffer,int verticesPerRepetition,int indicesPerRepetition,int repeatCount)31 void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
32 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
33 int verticesPerRepetition, int indicesPerRepetition,
34 int repeatCount) {
35 SkASSERT(target);
36 if (!indexBuffer) {
37 return;
38 }
39 sk_sp<const GrBuffer> vertexBuffer;
40 int firstVertex;
41 int vertexCount = verticesPerRepetition * repeatCount;
42 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
43 if (!fVertices) {
44 SkDebugf("Vertices could not be allocated for patterned rendering.");
45 return;
46 }
47 SkASSERT(vertexBuffer);
48 size_t ibSize = indexBuffer->gpuMemorySize();
49 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition));
50 fMesh = target->allocMesh(primitiveType);
51 fMesh->setIndexedPatterned(indexBuffer, indicesPerRepetition, verticesPerRepetition,
52 repeatCount, maxRepetitions);
53 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
54 }
55
recordDraw(Target * target,sk_sp<const GrGeometryProcessor> gp,const GrPipeline * pipeline,const GrPipeline::FixedDynamicState * fixedDynamicState) const56 void GrMeshDrawOp::PatternHelper::recordDraw(
57 Target* target, sk_sp<const GrGeometryProcessor> gp, const GrPipeline* pipeline,
58 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
59 target->draw(std::move(gp), pipeline, fixedDynamicState, fMesh);
60 }
61
62 //////////////////////////////////////////////////////////////////////////////
63
QuadHelper(Target * target,size_t vertexStride,int quadsToDraw)64 GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
65 sk_sp<const GrBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
66 if (!quadIndexBuffer) {
67 SkDebugf("Could not get quad index buffer.");
68 return;
69 }
70 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(quadIndexBuffer),
71 kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
72 }
73
74 //////////////////////////////////////////////////////////////////////////////
75
allocFixedDynamicState(const SkIRect & rect,int numPrimitiveProcessorTextures)76 GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::allocFixedDynamicState(
77 const SkIRect& rect, int numPrimitiveProcessorTextures) {
78 auto result = this->pipelineArena()->make<GrPipeline::FixedDynamicState>(rect);
79 if (numPrimitiveProcessorTextures) {
80 result->fPrimitiveProcessorTextures =
81 this->allocPrimitiveProcessorTextureArray(numPrimitiveProcessorTextures);
82 }
83 return result;
84 }
85
allocDynamicStateArrays(int numMeshes,int numPrimitiveProcessorTextures,bool allocScissors)86 GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::allocDynamicStateArrays(
87 int numMeshes, int numPrimitiveProcessorTextures, bool allocScissors) {
88 auto result = this->pipelineArena()->make<GrPipeline::DynamicStateArrays>();
89 if (allocScissors) {
90 result->fScissorRects = this->pipelineArena()->makeArray<SkIRect>(numMeshes);
91 }
92 if (numPrimitiveProcessorTextures) {
93 result->fPrimitiveProcessorTextures = this->allocPrimitiveProcessorTextureArray(
94 numPrimitiveProcessorTextures * numMeshes);
95 }
96 return result;
97 }
98
makePipeline(uint32_t pipelineFlags,GrProcessorSet && processorSet,GrAppliedClip && clip,int numPrimProcTextures)99 GrMeshDrawOp::Target::PipelineAndFixedDynamicState GrMeshDrawOp::Target::makePipeline(
100 uint32_t pipelineFlags, GrProcessorSet&& processorSet, GrAppliedClip&& clip,
101 int numPrimProcTextures) {
102 GrPipeline::InitArgs pipelineArgs;
103 pipelineArgs.fFlags = pipelineFlags;
104 pipelineArgs.fDstProxy = this->dstProxy();
105 pipelineArgs.fCaps = &this->caps();
106 pipelineArgs.fResourceProvider = this->resourceProvider();
107 GrPipeline::FixedDynamicState* fixedDynamicState = nullptr;
108 if (clip.scissorState().enabled() || numPrimProcTextures) {
109 fixedDynamicState = this->allocFixedDynamicState(clip.scissorState().rect());
110 if (numPrimProcTextures) {
111 fixedDynamicState->fPrimitiveProcessorTextures =
112 this->allocPrimitiveProcessorTextureArray(numPrimProcTextures);
113 }
114 }
115 return {this->allocPipeline(pipelineArgs, std::move(processorSet), std::move(clip)),
116 fixedDynamicState};
117 }
118