blob: 0ba8e43ce70b3b669abdbbe71d7c9ae28e962a2d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrMeshDrawOp.h"
bsalomon16b99132015-08-13 14:55:50 -07009
Greg Daniel2d41d0d2019-08-26 11:08:51 -040010#include "src/gpu/GrOpFlushState.h"
11#include "src/gpu/GrOpsRenderPass.h"
12#include "src/gpu/GrResourceProvider.h"
13
Brian Salomon7dc6e752017-11-02 11:34:51 -040014GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
bsalomon16b99132015-08-13 14:55:50 -070015
Brian Salomon29b60c92017-10-31 14:42:10 -040016void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
bsalomon75398562015-08-17 12:55:38 -070017
Brian Salomonb9485722018-08-05 21:30:33 -040018//////////////////////////////////////////////////////////////////////////////
19
Brian Salomon7eae3e02018-08-07 14:02:38 +000020GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050021 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000022 int verticesPerRepetition, int indicesPerRepetition,
23 int repeatCount) {
Brian Salomon12d22642019-01-29 14:38:50 -050024 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000025 indicesPerRepetition, repeatCount);
26}
27
28void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050029 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000030 int verticesPerRepetition, int indicesPerRepetition,
31 int repeatCount) {
32 SkASSERT(target);
33 if (!indexBuffer) {
34 return;
35 }
Brian Salomon12d22642019-01-29 14:38:50 -050036 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000037 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);
Brian Salomondbf70722019-02-07 11:31:24 -050045 size_t ibSize = indexBuffer->size();
Brian Salomon7eae3e02018-08-07 14:02:38 +000046 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition));
47 fMesh = target->allocMesh(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -050048 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000049 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050050 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000051}
52
53void GrMeshDrawOp::PatternHelper::recordDraw(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070054 Target* target, sk_sp<const GrGeometryProcessor> gp) const {
55 target->recordDraw(std::move(gp), fMesh);
56}
57
58void GrMeshDrawOp::PatternHelper::recordDraw(
59 Target* target, sk_sp<const GrGeometryProcessor> gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000060 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070061 target->recordDraw(std::move(gp), fMesh, 1, fixedDynamicState, nullptr);
Brian Salomon7eae3e02018-08-07 14:02:38 +000062}
63
64//////////////////////////////////////////////////////////////////////////////
65
66GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Brian Salomondbf70722019-02-07 11:31:24 -050067 sk_sp<const GrGpuBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon7eae3e02018-08-07 14:02:38 +000068 if (!quadIndexBuffer) {
69 SkDebugf("Could not get quad index buffer.");
70 return;
71 }
Brian Salomon12d22642019-01-29 14:38:50 -050072 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(quadIndexBuffer),
Brian Salomon7eae3e02018-08-07 14:02:38 +000073 kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
74}
75
76//////////////////////////////////////////////////////////////////////////////
77
Chris Dalton35a3abe2019-02-25 23:11:33 +000078GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::allocDynamicStateArrays(
79 int numMeshes, int numPrimitiveProcessorTextures, bool allocScissors) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070080 auto result = this->allocator()->make<GrPipeline::DynamicStateArrays>();
Chris Dalton35a3abe2019-02-25 23:11:33 +000081 if (allocScissors) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070082 result->fScissorRects = this->allocator()->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000083 }
84 if (numPrimitiveProcessorTextures) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070085 result->fPrimitiveProcessorTextures =
86 this->allocator()->makeArrayDefault<GrTextureProxy*>(
87 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000088 }
89 return result;
90}
91
Chris Dalton07cdcfc92019-02-26 11:13:22 -070092GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::makeFixedDynamicState(
Brian Salomon7eae3e02018-08-07 14:02:38 +000093 int numPrimProcTextures) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070094 const GrAppliedClip* clip = this->appliedClip();
95 if ((clip && clip->scissorState().enabled()) || numPrimProcTextures) {
96 const SkIRect& scissor = (clip) ? clip->scissorState().rect() : SkIRect::MakeEmpty();
97 auto fixedDynamicState =
98 this->allocator()->make<GrPipeline::FixedDynamicState>(scissor);
Brian Salomon7eae3e02018-08-07 14:02:38 +000099 if (numPrimProcTextures) {
100 fixedDynamicState->fPrimitiveProcessorTextures =
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700101 this->allocator()->makeArrayDefault<GrTextureProxy*>(numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000102 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700103 return fixedDynamicState;
Brian Salomonf5136822018-08-03 09:09:36 -0400104 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700105 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400106}