blob: 809620b91b71f89a52550e5052e85ae0a3180c29 [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(),
Brian Salomon8afde5f2020-04-01 16:22:00 -040022 target->writeView(),
Robert Phillips4133dc42020-03-11 15:55:55 -040023 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,
Brian Salomon8afde5f2020-04-01 16:22:00 -040030 const GrSurfaceProxyView* writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -040031 GrAppliedClip* clip,
32 const GrXferProcessor::DstProxyView& dstProxyView) {
33 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
34
35 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -040036 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips2669a7b2020-03-12 12:07:19 -040037
Brian Salomon8afde5f2020-04-01 16:22:00 -040038 this->createProgramInfo(context->priv().caps(), arena, writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -040039 std::move(appliedClip), dstProxyView);
40
Robert Phillipscad8fba2020-03-20 15:39:29 -040041 // TODO: at this point we've created both the program info and desc in the recording context's
42 // arena. In the DDL case, it would be cool if 'recordProgramInfo' could return the
43 // pre-existing versions if the program has already been seen. We could then return the
44 // memory for the current copy to the arena.
Robert Phillips2669a7b2020-03-12 12:07:19 -040045 context->priv().recordProgramInfo(this->programInfo());
46}
47
Brian Salomonb9485722018-08-05 21:30:33 -040048//////////////////////////////////////////////////////////////////////////////
49
Brian Salomon7eae3e02018-08-07 14:02:38 +000050GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050051 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000052 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040053 int repeatCount, int maxRepetitions) {
Brian Salomon12d22642019-01-29 14:38:50 -050054 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040055 indicesPerRepetition, repeatCount, maxRepetitions);
Brian Salomon7eae3e02018-08-07 14:02:38 +000056}
57
58void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050059 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000060 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040061 int repeatCount, int maxRepetitions) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000062 SkASSERT(target);
63 if (!indexBuffer) {
64 return;
65 }
Brian Salomon12d22642019-01-29 14:38:50 -050066 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000067 int firstVertex;
68 int vertexCount = verticesPerRepetition * repeatCount;
69 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
70 if (!fVertices) {
71 SkDebugf("Vertices could not be allocated for patterned rendering.");
72 return;
73 }
74 SkASSERT(vertexBuffer);
Chris Dalton79f336d2020-02-11 14:42:56 -070075 fMesh = target->allocMesh();
Robert Phillipscea290f2019-11-06 11:21:03 -050076 fPrimitiveType = primitiveType;
Robert Phillipsee08d522019-10-28 16:34:44 -040077
78 SkASSERT(maxRepetitions ==
79 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Chris Dalton37c7bdd2020-03-13 09:21:12 -060080 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
81 maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
82 firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000083}
84
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050085void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
86 target->recordDraw(gp, fMesh, 1, fPrimitiveType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -070087}
88
89void GrMeshDrawOp::PatternHelper::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -050090 Target* target,
91 const GrGeometryProcessor* gp,
Chris Dalton304e14d2020-03-17 14:29:06 -060092 const GrSurfaceProxy* const primProcProxies[]) const {
93 target->recordDraw(gp, fMesh, 1, primProcProxies, fPrimitiveType);
Brian Salomon7eae3e02018-08-07 14:02:38 +000094}
95
96//////////////////////////////////////////////////////////////////////////////
97
98GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -040099 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
100 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000101 SkDebugf("Could not get quad index buffer.");
102 return;
103 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400104 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
105 GrResourceProvider::NumVertsPerNonAAQuad(),
106 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
107 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000108}