blob: 77dcf6deef71c107526e912f82214fa82bcaa67d [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
Brian Salomonb9485722018-08-05 21:30:33 -040018//////////////////////////////////////////////////////////////////////////////
19
Brian Salomon7eae3e02018-08-07 14:02:38 +000020GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050021 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000022 int verticesPerRepetition, int indicesPerRepetition,
23 int repeatCount) {
Brian Salomon12d22642019-01-29 14:38:50 -050024 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000025 indicesPerRepetition, repeatCount);
26}
27
28void GrMeshDrawOp::PatternHelper::init(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,
31 int repeatCount) {
32 SkASSERT(target);
33 if (!indexBuffer) {
34 return;
35 }
Brian Salomon12d22642019-01-29 14:38:50 -050036 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000037 int firstVertex;
38 int vertexCount = verticesPerRepetition * repeatCount;
39 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
40 if (!fVertices) {
41 SkDebugf("Vertices could not be allocated for patterned rendering.");
42 return;
43 }
44 SkASSERT(vertexBuffer);
Brian Salomondbf70722019-02-07 11:31:24 -050045 size_t ibSize = indexBuffer->size();
Brian Salomon7eae3e02018-08-07 14:02:38 +000046 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition));
47 fMesh = target->allocMesh(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -050048 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition,
Brian Salomon7eae3e02018-08-07 14:02:38 +000049 repeatCount, maxRepetitions);
Brian Salomon12d22642019-01-29 14:38:50 -050050 fMesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000051}
52
53void GrMeshDrawOp::PatternHelper::recordDraw(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070054 Target* target, sk_sp<const GrGeometryProcessor> gp) const {
55 target->recordDraw(std::move(gp), fMesh);
56}
57
58void GrMeshDrawOp::PatternHelper::recordDraw(
59 Target* target, sk_sp<const GrGeometryProcessor> gp,
Brian Salomon7eae3e02018-08-07 14:02:38 +000060 const GrPipeline::FixedDynamicState* fixedDynamicState) const {
Chris Dalton07cdcfc92019-02-26 11:13:22 -070061 target->recordDraw(std::move(gp), fMesh, 1, fixedDynamicState, nullptr);
Brian Salomon7eae3e02018-08-07 14:02:38 +000062}
63
64//////////////////////////////////////////////////////////////////////////////
65
66GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Brian Salomondbf70722019-02-07 11:31:24 -050067 sk_sp<const GrGpuBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon7eae3e02018-08-07 14:02:38 +000068 if (!quadIndexBuffer) {
69 SkDebugf("Could not get quad index buffer.");
70 return;
71 }
Brian Salomon12d22642019-01-29 14:38:50 -050072 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(quadIndexBuffer),
Brian Salomon7eae3e02018-08-07 14:02:38 +000073 kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
74}
75
76//////////////////////////////////////////////////////////////////////////////
77
Robert Phillips61fc7992019-10-22 11:58:17 -040078GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::AllocDynamicStateArrays(
79 SkArenaAlloc* arena, int numMeshes, int numPrimitiveProcessorTextures,
80 bool allocScissors) {
81
82 auto result = arena->make<GrPipeline::DynamicStateArrays>();
83
Chris Dalton35a3abe2019-02-25 23:11:33 +000084 if (allocScissors) {
Robert Phillips61fc7992019-10-22 11:58:17 -040085 result->fScissorRects = arena->makeArray<SkIRect>(numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000086 }
Robert Phillips61fc7992019-10-22 11:58:17 -040087
Chris Dalton35a3abe2019-02-25 23:11:33 +000088 if (numPrimitiveProcessorTextures) {
Robert Phillips61fc7992019-10-22 11:58:17 -040089 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrTextureProxy*>(
Chris Dalton07cdcfc92019-02-26 11:13:22 -070090 numPrimitiveProcessorTextures * numMeshes);
Chris Dalton35a3abe2019-02-25 23:11:33 +000091 }
Robert Phillips61fc7992019-10-22 11:58:17 -040092
Chris Dalton35a3abe2019-02-25 23:11:33 +000093 return result;
94}
95
Robert Phillips61fc7992019-10-22 11:58:17 -040096GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::MakeFixedDynamicState(
97 SkArenaAlloc* arena, const GrAppliedClip* clip, int numPrimProcTextures) {
98
Chris Dalton07cdcfc92019-02-26 11:13:22 -070099 if ((clip && clip->scissorState().enabled()) || numPrimProcTextures) {
100 const SkIRect& scissor = (clip) ? clip->scissorState().rect() : SkIRect::MakeEmpty();
Robert Phillips61fc7992019-10-22 11:58:17 -0400101
102 auto result = arena->make<GrPipeline::FixedDynamicState>(scissor);
103
Brian Salomon7eae3e02018-08-07 14:02:38 +0000104 if (numPrimProcTextures) {
Robert Phillips61fc7992019-10-22 11:58:17 -0400105 result->fPrimitiveProcessorTextures = arena->makeArrayDefault<GrTextureProxy*>(
106 numPrimProcTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000107 }
Robert Phillips61fc7992019-10-22 11:58:17 -0400108 return result;
Brian Salomonf5136822018-08-03 09:09:36 -0400109 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700110 return nullptr;
Brian Salomonf5136822018-08-03 09:09:36 -0400111}