blob: 8ae327c702e1fdd44173e1a210324b6716194d89 [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 Phillipsee08d522019-10-28 16:34:44 -040046
47 SkASSERT(maxRepetitions ==
48 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Brian Salomondbf70722019-02-07 11:31:24 -050049 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000050 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050051 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000052}
53
54void GrMeshDrawOp::PatternHelper::recordDraw(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070055 Target* target, sk_sp<const GrGeometryProcessor> gp) const {
56 target->recordDraw(std::move(gp), fMesh);
57}
58
59void GrMeshDrawOp::PatternHelper::recordDraw(
60 Target* target, sk_sp<const GrGeometryProcessor> gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000061 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070062 target->recordDraw(std::move(gp), fMesh, 1, fixedDynamicState, nullptr);
Brian Salomon7eae3e02018-08-07 14:02:38 +000063}
64
65//////////////////////////////////////////////////////////////////////////////
66
67GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -040068 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
69 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000070 SkDebugf("Could not get quad index buffer.");
71 return;
72 }
Robert Phillipsee08d522019-10-28 16:34:44 -040073 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
74 GrResourceProvider::NumVertsPerNonAAQuad(),
75 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
76 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +000077}
78
79//////////////////////////////////////////////////////////////////////////////
80
Robert Phillips61fc7992019-10-22 11:58:17 -040081GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::AllocDynamicStateArrays(
82 SkArenaAlloc* arena, int numMeshes, int numPrimitiveProcessorTextures,
83 bool allocScissors) {
84
85 auto result = arena->make<GrPipeline::DynamicStateArrays>();
86
Chris Dalton35a3abe2019-02-25 23:11:33 +000087 if (allocScissors) {
Robert Phillips61fc7992019-10-22 11:58:17 -040088 result->fScissorRects = arena->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000089 }
Robert Phillips61fc7992019-10-22 11:58:17 -040090
Chris Dalton35a3abe2019-02-25 23:11:33 +000091 if (numPrimitiveProcessorTextures) {
Robert Phillips61fc7992019-10-22 11:58:17 -040092 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrTextureProxy*>(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070093 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000094 }
Robert Phillips61fc7992019-10-22 11:58:17 -040095
Chris Dalton35a3abe2019-02-25 23:11:33 +000096 return result;
97}
98
Robert Phillips61fc7992019-10-22 11:58:17 -040099GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::MakeFixedDynamicState(
100 SkArenaAlloc* arena, const GrAppliedClip* clip, int numPrimProcTextures) {
101
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700102 if ((clip && clip->scissorState().enabled()) || numPrimProcTextures) {
103 const SkIRect& scissor = (clip) ? clip->scissorState().rect() : SkIRect::MakeEmpty();
Robert Phillips61fc7992019-10-22 11:58:17 -0400104
105 auto result = arena->make<GrPipeline::FixedDynamicState>(scissor);
106
Brian Salomon7eae3e02018-08-07 14:02:38 +0000107 if (numPrimProcTextures) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400108 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrTextureProxy*>(
109 numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000110 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400111 return result;
Brian Salomonf5136822018-08-03 09:09:36 -0400112 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700113 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400114}