blob: da046e1dcf0516e1b6dbfc89aa9e5173e82bf76e [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)));
Chris Dalton37c7bdd2020-03-13 09:21:12 -060076 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
77 maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
78 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,
Chris Dalton304e14d2020-03-17 14:29:06 -060088 const GrSurfaceProxy* const primProcProxies[]) const {
89 target->recordDraw(gp, fMesh, 1, primProcProxies, 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}