blob: 669505814dd2ac3879bb59cbdc2d34450d22574b [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/GrGpuCommandBuffer.h"
9#include "src/gpu/GrOpFlushState.h"
10#include "src/gpu/GrResourceProvider.h"
11#include "src/gpu/ops/GrMeshDrawOp.h"
bsalomon16b99132015-08-13 14:55:50 -070012
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 Salomonb9485722018-08-05 21:30:33 -040017//////////////////////////////////////////////////////////////////////////////
18
Brian Salomon7eae3e02018-08-07 14:02:38 +000019GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050020 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000021 int verticesPerRepetition, int indicesPerRepetition,
22 int repeatCount) {
Brian Salomon12d22642019-01-29 14:38:50 -050023 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000024 indicesPerRepetition, repeatCount);
25}
26
27void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050028 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000029 int verticesPerRepetition, int indicesPerRepetition,
30 int repeatCount) {
31 SkASSERT(target);
32 if (!indexBuffer) {
33 return;
34 }
Brian Salomon12d22642019-01-29 14:38:50 -050035 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000036 int firstVertex;
37 int vertexCount = verticesPerRepetition * repeatCount;
38 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
39 if (!fVertices) {
40 SkDebugf("Vertices could not be allocated for patterned rendering.");
41 return;
42 }
43 SkASSERT(vertexBuffer);
Brian Salomondbf70722019-02-07 11:31:24 -050044 size_t ibSize = indexBuffer->size();
Brian Salomon7eae3e02018-08-07 14:02:38 +000045 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition));
46 fMesh = target->allocMesh(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -050047 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000048 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050049 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000050}
51
52void GrMeshDrawOp::PatternHelper::recordDraw(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070053 Target* target, sk_sp<const GrGeometryProcessor> gp) const {
54 target->recordDraw(std::move(gp), fMesh);
55}
56
57void GrMeshDrawOp::PatternHelper::recordDraw(
58 Target* target, sk_sp<const GrGeometryProcessor> gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000059 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070060 target->recordDraw(std::move(gp), fMesh, 1, fixedDynamicState, nullptr);
Brian Salomon7eae3e02018-08-07 14:02:38 +000061}
62
63//////////////////////////////////////////////////////////////////////////////
64
65GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Brian Salomondbf70722019-02-07 11:31:24 -050066 sk_sp<const GrGpuBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon7eae3e02018-08-07 14:02:38 +000067 if (!quadIndexBuffer) {
68 SkDebugf("Could not get quad index buffer.");
69 return;
70 }
Brian Salomon12d22642019-01-29 14:38:50 -050071 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(quadIndexBuffer),
Brian Salomon7eae3e02018-08-07 14:02:38 +000072 kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
73}
74
75//////////////////////////////////////////////////////////////////////////////
76
Chris Dalton35a3abe2019-02-25 23:11:33 +000077GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::allocDynamicStateArrays(
78 int numMeshes, int numPrimitiveProcessorTextures, bool allocScissors) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070079 auto result = this->allocator()->make<GrPipeline::DynamicStateArrays>();
Chris Dalton35a3abe2019-02-25 23:11:33 +000080 if (allocScissors) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070081 result->fScissorRects = this->allocator()->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000082 }
83 if (numPrimitiveProcessorTextures) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070084 result->fPrimitiveProcessorTextures =
85 this->allocator()->makeArrayDefault<GrTextureProxy*>(
86 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000087 }
88 return result;
89}
90
Chris Dalton07cdcfc92019-02-26 11:13:22 -070091GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::makeFixedDynamicState(
Brian Salomon7eae3e02018-08-07 14:02:38 +000092 int numPrimProcTextures) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070093 const GrAppliedClip* clip = this->appliedClip();
94 if ((clip && clip->scissorState().enabled()) || numPrimProcTextures) {
95 const SkIRect& scissor = (clip) ? clip->scissorState().rect() : SkIRect::MakeEmpty();
96 auto fixedDynamicState =
97 this->allocator()->make<GrPipeline::FixedDynamicState>(scissor);
Brian Salomon7eae3e02018-08-07 14:02:38 +000098 if (numPrimProcTextures) {
99 fixedDynamicState->fPrimitiveProcessorTextures =
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700100 this->allocator()->makeArrayDefault<GrTextureProxy*>(numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000101 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700102 return fixedDynamicState;
Brian Salomonf5136822018-08-03 09:09:36 -0400103 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700104 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400105}