blob: 0bc782c4a007762d12ec61c47ef0f0045a9af0e5 [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
Robert Phillips009e9af2017-06-15 14:01:04 -04008#include "GrGpuCommandBuffer.h"
Brian Salomondad29232016-12-01 16:40:24 -05009#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050010#include "GrOpFlushState.h"
bsalomon16b99132015-08-13 14:55:50 -070011#include "GrResourceProvider.h"
12
Brian Salomon7dc6e752017-11-02 11:34:51 -040013GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
bsalomon16b99132015-08-13 14:55:50 -070014
Brian Salomon29b60c92017-10-31 14:42:10 -040015void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
bsalomon75398562015-08-17 12:55:38 -070016
Chris Daltonbca46e22017-05-15 11:03:26 -060017void* GrMeshDrawOp::PatternHelper::init(Target* target, size_t vertexStride,
18 const GrBuffer* indexBuffer, int verticesPerRepetition,
19 int indicesPerRepetition, int repeatCount) {
bsalomon75398562015-08-17 12:55:38 -070020 SkASSERT(target);
bsalomon16b99132015-08-13 14:55:50 -070021 if (!indexBuffer) {
halcanary96fcdcc2015-08-27 07:41:13 -070022 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070023 }
cdalton397536c2016-03-25 12:15:03 -070024 const GrBuffer* vertexBuffer;
bsalomon16b99132015-08-13 14:55:50 -070025 int firstVertex;
Chris Daltonff926502017-05-03 14:36:54 -040026 int vertexCount = verticesPerRepetition * repeatCount;
Brian Salomondad29232016-12-01 16:40:24 -050027 void* vertices =
28 target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
bsalomon16b99132015-08-13 14:55:50 -070029 if (!vertices) {
30 SkDebugf("Vertices could not be allocated for instanced rendering.");
halcanary96fcdcc2015-08-27 07:41:13 -070031 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070032 }
33 SkASSERT(vertexBuffer);
34 size_t ibSize = indexBuffer->gpuMemorySize();
Chris Daltonff926502017-05-03 14:36:54 -040035 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition));
bsalomon16b99132015-08-13 14:55:50 -070036
Chris Dalton114a3c02017-05-26 15:17:19 -060037 fMesh.setIndexedPatterned(indexBuffer, indicesPerRepetition, verticesPerRepetition,
38 repeatCount, maxRepetitions);
39 fMesh.setVertexData(vertexBuffer, firstVertex);
bsalomon16b99132015-08-13 14:55:50 -070040 return vertices;
41}
42
Chris Daltonff926502017-05-03 14:36:54 -040043void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp,
Chris Daltonbca46e22017-05-15 11:03:26 -060044 const GrPipeline* pipeline) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -040045 target->draw(gp, pipeline, fMesh);
bsalomon75398562015-08-17 12:55:38 -070046}
47
Brian Salomondad29232016-12-01 16:40:24 -050048void* GrMeshDrawOp::QuadHelper::init(Target* target, size_t vertexStride, int quadsToDraw) {
Brian Salomond28a79d2017-10-16 13:01:07 -040049 sk_sp<const GrBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
bsalomon16b99132015-08-13 14:55:50 -070050 if (!quadIndexBuffer) {
51 SkDebugf("Could not get quad index buffer.");
halcanary96fcdcc2015-08-27 07:41:13 -070052 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070053 }
Chris Daltonbca46e22017-05-15 11:03:26 -060054 return this->INHERITED::init(target, vertexStride, quadIndexBuffer.get(), kVerticesPerQuad,
55 kIndicesPerQuad, quadsToDraw);
bsalomon16b99132015-08-13 14:55:50 -070056}
bsalomon75398562015-08-17 12:55:38 -070057
Brian Salomon9e50f7b2017-03-06 12:02:34 -050058void GrMeshDrawOp::onExecute(GrOpFlushState* state) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040059 state->executeDrawsAndUploadsForMeshDrawOp(this->uniqueID(), this->bounds());
bsalomon342bfc22016-04-01 06:06:20 -070060}