blob: 375ce55a79c6c073b1987619ef6fefe9c65dc3b1 [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
Robert Phillips4133dc42020-03-11 15:55:55 -040018void GrMeshDrawOp::createProgramInfo(Target* target) {
19 this->createProgramInfo(&target->caps(),
20 target->allocator(),
21 target->outputView(),
22 target->detachAppliedClip(),
23 target->dstProxyView());
24}
25
Brian Salomonb9485722018-08-05 21:30:33 -040026//////////////////////////////////////////////////////////////////////////////
27
Brian Salomon7eae3e02018-08-07 14:02:38 +000028GrMeshDrawOp::PatternHelper::PatternHelper(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 Salomon12d22642019-01-29 14:38:50 -050032 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040033 indicesPerRepetition, repeatCount, maxRepetitions);
Brian Salomon7eae3e02018-08-07 14:02:38 +000034}
35
36void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050037 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000038 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040039 int repeatCount, int maxRepetitions) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000040 SkASSERT(target);
41 if (!indexBuffer) {
42 return;
43 }
Brian Salomon12d22642019-01-29 14:38:50 -050044 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000045 int firstVertex;
46 int vertexCount = verticesPerRepetition * repeatCount;
47 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
48 if (!fVertices) {
49 SkDebugf("Vertices could not be allocated for patterned rendering.");
50 return;
51 }
52 SkASSERT(vertexBuffer);
Chris Dalton79f336d2020-02-11 14:42:56 -070053 fMesh = target->allocMesh();
Robert Phillipscea290f2019-11-06 11:21:03 -050054 fPrimitiveType = primitiveType;
Robert Phillipsee08d522019-10-28 16:34:44 -040055
56 SkASSERT(maxRepetitions ==
57 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Brian Salomondbf70722019-02-07 11:31:24 -050058 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000059 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050060 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000061}
62
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050063void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
64 target->recordDraw(gp, fMesh, 1, fPrimitiveType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -070065}
66
67void GrMeshDrawOp::PatternHelper::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -050068 Target* target,
69 const GrGeometryProcessor* gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000070 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050071 target->recordDraw(gp, fMesh, 1, fixedDynamicState, nullptr, fPrimitiveType);
Brian Salomon7eae3e02018-08-07 14:02:38 +000072}
73
74//////////////////////////////////////////////////////////////////////////////
75
76GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -040077 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
78 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000079 SkDebugf("Could not get quad index buffer.");
80 return;
81 }
Robert Phillipsee08d522019-10-28 16:34:44 -040082 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
83 GrResourceProvider::NumVertsPerNonAAQuad(),
84 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
85 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +000086}
87
88//////////////////////////////////////////////////////////////////////////////
89
Robert Phillips61fc7992019-10-22 11:58:17 -040090GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::AllocDynamicStateArrays(
91 SkArenaAlloc* arena, int numMeshes, int numPrimitiveProcessorTextures,
92 bool allocScissors) {
93
94 auto result = arena->make<GrPipeline::DynamicStateArrays>();
95
Chris Dalton35a3abe2019-02-25 23:11:33 +000096 if (allocScissors) {
Robert Phillips61fc7992019-10-22 11:58:17 -040097 result->fScissorRects = arena->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000098 }
Robert Phillips61fc7992019-10-22 11:58:17 -040099
Chris Dalton35a3abe2019-02-25 23:11:33 +0000100 if (numPrimitiveProcessorTextures) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500101 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrSurfaceProxy*>(
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700102 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +0000103 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400104
Chris Dalton35a3abe2019-02-25 23:11:33 +0000105 return result;
106}
107
Robert Phillips61fc7992019-10-22 11:58:17 -0400108GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::MakeFixedDynamicState(
109 SkArenaAlloc* arena, const GrAppliedClip* clip, int numPrimProcTextures) {
110
Robert Phillipsff2f3802019-11-18 16:36:54 -0500111 bool haveScissor = clip && clip->scissorState().enabled();
Robert Phillips61fc7992019-10-22 11:58:17 -0400112
Robert Phillipsff2f3802019-11-18 16:36:54 -0500113 if (haveScissor || numPrimProcTextures) {
114 auto result = arena->make<GrPipeline::FixedDynamicState>();
115
116 if (haveScissor) {
117 result->fScissorRect = clip->scissorState().rect();
118 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400119
Brian Salomon7eae3e02018-08-07 14:02:38 +0000120 if (numPrimProcTextures) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500121 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrSurfaceProxy*>(
Robert Phillips61fc7992019-10-22 11:58:17 -0400122 numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000123 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400124 return result;
Brian Salomonf5136822018-08-03 09:09:36 -0400125 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700126 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400127}