blob: 3250a2200cd5b91a21004c29207cfb0da9ccaaf1 [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,
Robert Phillipsee08d522019-10-28 16:34:44 -040023 int repeatCount, int maxRepetitions) {
Brian Salomon12d22642019-01-29 14:38:50 -050024 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040025 indicesPerRepetition, repeatCount, maxRepetitions);
Brian Salomon7eae3e02018-08-07 14:02:38 +000026}
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,
Robert Phillipsee08d522019-10-28 16:34:44 -040031 int repeatCount, int maxRepetitions) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000032 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 Salomon7eae3e02018-08-07 14:02:38 +000045 fMesh = target->allocMesh(primitiveType);
Robert Phillipscea290f2019-11-06 11:21:03 -050046 fPrimitiveType = primitiveType;
Robert Phillipsee08d522019-10-28 16:34:44 -040047
48 SkASSERT(maxRepetitions ==
49 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Brian Salomondbf70722019-02-07 11:31:24 -050050 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000051 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050052 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000053}
54
55void GrMeshDrawOp::PatternHelper::recordDraw(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070056 Target* target, sk_sp<const GrGeometryProcessor> gp) const {
Robert Phillipscea290f2019-11-06 11:21:03 -050057 target->recordDraw(std::move(gp), fMesh, 1, fPrimitiveType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -070058}
59
60void GrMeshDrawOp::PatternHelper::recordDraw(
61 Target* target, sk_sp<const GrGeometryProcessor> gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000062 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Robert Phillipscea290f2019-11-06 11:21:03 -050063 target->recordDraw(std::move(gp), fMesh, 1, fixedDynamicState, nullptr, fPrimitiveType);
Brian Salomon7eae3e02018-08-07 14:02:38 +000064}
65
66//////////////////////////////////////////////////////////////////////////////
67
68GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -040069 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
70 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000071 SkDebugf("Could not get quad index buffer.");
72 return;
73 }
Robert Phillipsee08d522019-10-28 16:34:44 -040074 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
75 GrResourceProvider::NumVertsPerNonAAQuad(),
76 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
77 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +000078}
79
80//////////////////////////////////////////////////////////////////////////////
81
Robert Phillips61fc7992019-10-22 11:58:17 -040082GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::AllocDynamicStateArrays(
83 SkArenaAlloc* arena, int numMeshes, int numPrimitiveProcessorTextures,
84 bool allocScissors) {
85
86 auto result = arena->make<GrPipeline::DynamicStateArrays>();
87
Chris Dalton35a3abe2019-02-25 23:11:33 +000088 if (allocScissors) {
Robert Phillips61fc7992019-10-22 11:58:17 -040089 result->fScissorRects = arena->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000090 }
Robert Phillips61fc7992019-10-22 11:58:17 -040091
Chris Dalton35a3abe2019-02-25 23:11:33 +000092 if (numPrimitiveProcessorTextures) {
Robert Phillips61fc7992019-10-22 11:58:17 -040093 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrTextureProxy*>(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070094 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000095 }
Robert Phillips61fc7992019-10-22 11:58:17 -040096
Chris Dalton35a3abe2019-02-25 23:11:33 +000097 return result;
98}
99
Robert Phillips61fc7992019-10-22 11:58:17 -0400100GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::MakeFixedDynamicState(
101 SkArenaAlloc* arena, const GrAppliedClip* clip, int numPrimProcTextures) {
102
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700103 if ((clip && clip->scissorState().enabled()) || numPrimProcTextures) {
104 const SkIRect& scissor = (clip) ? clip->scissorState().rect() : SkIRect::MakeEmpty();
Robert Phillips61fc7992019-10-22 11:58:17 -0400105
106 auto result = arena->make<GrPipeline::FixedDynamicState>(scissor);
107
Brian Salomon7eae3e02018-08-07 14:02:38 +0000108 if (numPrimProcTextures) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400109 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrTextureProxy*>(
110 numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000111 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400112 return result;
Brian Salomonf5136822018-08-03 09:09:36 -0400113 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700114 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400115}