blob: abf67e0a6b3228a9b7fe9f4730620685837622ce [file] [log] [blame]
bsalomon16b99132015-08-13 14:55:50 -07001/*
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
Robert Phillips009e9af2017-06-15 14:01:04 -04008#include "GrGpuCommandBuffer.h"
Brian Salomondad29232016-12-01 16:40:24 -05009#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050010#include "GrOpFlushState.h"
bsalomon16b99132015-08-13 14:55:50 -070011#include "GrResourceProvider.h"
12
Brian Salomon7dc6e752017-11-02 11:34:51 -040013GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
bsalomon16b99132015-08-13 14:55:50 -070014
Brian Salomon29b60c92017-10-31 14:42:10 -040015void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
bsalomon75398562015-08-17 12:55:38 -070016
Brian Salomon588cec72018-11-14 13:56:37 -050017void GrMeshDrawOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
Brian Osmane8012102018-11-29 14:05:07 -050018 state->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds);
Brian Salomonb9485722018-08-05 21:30:33 -040019}
20
21//////////////////////////////////////////////////////////////////////////////
22
Brian Salomon7eae3e02018-08-07 14:02:38 +000023GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050024 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000025 int verticesPerRepetition, int indicesPerRepetition,
26 int repeatCount) {
Brian Salomon12d22642019-01-29 14:38:50 -050027 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000028 indicesPerRepetition, repeatCount);
29}
30
31void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050032 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000033 int verticesPerRepetition, int indicesPerRepetition,
34 int repeatCount) {
35 SkASSERT(target);
36 if (!indexBuffer) {
37 return;
38 }
Brian Salomon12d22642019-01-29 14:38:50 -050039 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000040 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);
Brian Salomon12d22642019-01-29 14:38:50 -050053 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000054}
55
56void 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
64GrMeshDrawOp::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 }
Brian Salomon12d22642019-01-29 14:38:50 -050070 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(quadIndexBuffer),
Brian Salomon7eae3e02018-08-07 14:02:38 +000071 kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
72}
73
74//////////////////////////////////////////////////////////////////////////////
75
Brian Salomonf7232642018-09-19 08:58:08 -040076GrPipeline::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
86GrPipeline::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
Brian Salomonf5136822018-08-03 09:09:36 -040099GrMeshDrawOp::Target::PipelineAndFixedDynamicState GrMeshDrawOp::Target::makePipeline(
Brian Salomon7eae3e02018-08-07 14:02:38 +0000100 uint32_t pipelineFlags, GrProcessorSet&& processorSet, GrAppliedClip&& clip,
101 int numPrimProcTextures) {
Brian Salomonf5136822018-08-03 09:09:36 -0400102 GrPipeline::InitArgs pipelineArgs;
103 pipelineArgs.fFlags = pipelineFlags;
104 pipelineArgs.fProxy = this->proxy();
105 pipelineArgs.fDstProxy = this->dstProxy();
106 pipelineArgs.fCaps = &this->caps();
107 pipelineArgs.fResourceProvider = this->resourceProvider();
108 GrPipeline::FixedDynamicState* fixedDynamicState = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000109 if (clip.scissorState().enabled() || numPrimProcTextures) {
Brian Salomonf5136822018-08-03 09:09:36 -0400110 fixedDynamicState = this->allocFixedDynamicState(clip.scissorState().rect());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000111 if (numPrimProcTextures) {
112 fixedDynamicState->fPrimitiveProcessorTextures =
113 this->allocPrimitiveProcessorTextureArray(numPrimProcTextures);
114 }
Brian Salomonf5136822018-08-03 09:09:36 -0400115 }
116 return {this->allocPipeline(pipelineArgs, std::move(processorSet), std::move(clip)),
117 fixedDynamicState};
118}