blob: 0cdc85706bde3142efdd248010f746c0b1c9f1c2 [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"
Robert Phillips2669a7b2020-03-12 12:07:19 -040012#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040013#include "src/gpu/GrResourceProvider.h"
14
Brian Salomon7dc6e752017-11-02 11:34:51 -040015GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
bsalomon16b99132015-08-13 14:55:50 -070016
Brian Salomon29b60c92017-10-31 14:42:10 -040017void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
bsalomon75398562015-08-17 12:55:38 -070018
Robert Phillips4133dc42020-03-11 15:55:55 -040019void GrMeshDrawOp::createProgramInfo(Target* target) {
20 this->createProgramInfo(&target->caps(),
21 target->allocator(),
22 target->outputView(),
23 target->detachAppliedClip(),
24 target->dstProxyView());
25}
26
Robert Phillips2669a7b2020-03-12 12:07:19 -040027// This onPrepareDraws implementation assumes the derived Op only has a single programInfo -
28// which is the majority of the cases.
29void GrMeshDrawOp::onPrePrepareDraws(GrRecordingContext* context,
30 const GrSurfaceProxyView* outputView,
31 GrAppliedClip* clip,
32 const GrXferProcessor::DstProxyView& dstProxyView) {
33 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
34
35 // This is equivalent to a GrOpFlushState::detachAppliedClip
36 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
37
38 this->createProgramInfo(context->priv().caps(), arena, outputView,
39 std::move(appliedClip), dstProxyView);
40
41 context->priv().recordProgramInfo(this->programInfo());
42}
43
Brian Salomonb9485722018-08-05 21:30:33 -040044//////////////////////////////////////////////////////////////////////////////
45
Brian Salomon7eae3e02018-08-07 14:02:38 +000046GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050047 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000048 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040049 int repeatCount, int maxRepetitions) {
Brian Salomon12d22642019-01-29 14:38:50 -050050 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040051 indicesPerRepetition, repeatCount, maxRepetitions);
Brian Salomon7eae3e02018-08-07 14:02:38 +000052}
53
54void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050055 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000056 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040057 int repeatCount, int maxRepetitions) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000058 SkASSERT(target);
59 if (!indexBuffer) {
60 return;
61 }
Brian Salomon12d22642019-01-29 14:38:50 -050062 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000063 int firstVertex;
64 int vertexCount = verticesPerRepetition * repeatCount;
65 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
66 if (!fVertices) {
67 SkDebugf("Vertices could not be allocated for patterned rendering.");
68 return;
69 }
70 SkASSERT(vertexBuffer);
Chris Dalton79f336d2020-02-11 14:42:56 -070071 fMesh = target->allocMesh();
Robert Phillipscea290f2019-11-06 11:21:03 -050072 fPrimitiveType = primitiveType;
Robert Phillipsee08d522019-10-28 16:34:44 -040073
74 SkASSERT(maxRepetitions ==
75 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Brian Salomondbf70722019-02-07 11:31:24 -050076 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000077 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050078 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000079}
80
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050081void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
82 target->recordDraw(gp, fMesh, 1, fPrimitiveType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -070083}
84
85void GrMeshDrawOp::PatternHelper::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -050086 Target* target,
87 const GrGeometryProcessor* gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000088 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050089 target->recordDraw(gp, fMesh, 1, fixedDynamicState, nullptr, fPrimitiveType);
Brian Salomon7eae3e02018-08-07 14:02:38 +000090}
91
92//////////////////////////////////////////////////////////////////////////////
93
94GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -040095 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
96 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000097 SkDebugf("Could not get quad index buffer.");
98 return;
99 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400100 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
101 GrResourceProvider::NumVertsPerNonAAQuad(),
102 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
103 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000104}
105
106//////////////////////////////////////////////////////////////////////////////
107
Robert Phillips61fc7992019-10-22 11:58:17 -0400108GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::AllocDynamicStateArrays(
109 SkArenaAlloc* arena, int numMeshes, int numPrimitiveProcessorTextures,
110 bool allocScissors) {
111
112 auto result = arena->make<GrPipeline::DynamicStateArrays>();
113
Chris Dalton35a3abe2019-02-25 23:11:33 +0000114 if (allocScissors) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400115 result->fScissorRects = arena->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +0000116 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400117
Chris Dalton35a3abe2019-02-25 23:11:33 +0000118 if (numPrimitiveProcessorTextures) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500119 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrSurfaceProxy*>(
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700120 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +0000121 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400122
Chris Dalton35a3abe2019-02-25 23:11:33 +0000123 return result;
124}
125
Robert Phillips61fc7992019-10-22 11:58:17 -0400126GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::MakeFixedDynamicState(
127 SkArenaAlloc* arena, const GrAppliedClip* clip, int numPrimProcTextures) {
128
Robert Phillipsff2f3802019-11-18 16:36:54 -0500129 bool haveScissor = clip && clip->scissorState().enabled();
Robert Phillips61fc7992019-10-22 11:58:17 -0400130
Robert Phillipsff2f3802019-11-18 16:36:54 -0500131 if (haveScissor || numPrimProcTextures) {
132 auto result = arena->make<GrPipeline::FixedDynamicState>();
133
134 if (haveScissor) {
135 result->fScissorRect = clip->scissorState().rect();
136 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400137
Brian Salomon7eae3e02018-08-07 14:02:38 +0000138 if (numPrimProcTextures) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500139 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrSurfaceProxy*>(
Robert Phillips61fc7992019-10-22 11:58:17 -0400140 numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000141 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400142 return result;
Brian Salomonf5136822018-08-03 09:09:36 -0400143 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700144 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400145}