bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 8 | #include "GrGpuCommandBuffer.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 9 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 10 | #include "GrOpFlushState.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 11 | #include "GrResourceProvider.h" |
| 12 | |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 13 | GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {} |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 14 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 15 | void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 16 | |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 17 | void GrMeshDrawOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) { |
Brian Osman | e801210 | 2018-11-29 14:05:07 -0500 | [diff] [blame] | 18 | state->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds); |
Brian Salomon | b948572 | 2018-08-05 21:30:33 -0400 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | ////////////////////////////////////////////////////////////////////////////// |
| 22 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 23 | GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType, |
| 24 | size_t vertexStride, const GrBuffer* indexBuffer, |
| 25 | int verticesPerRepetition, int indicesPerRepetition, |
| 26 | int repeatCount) { |
| 27 | this->init(target, primitiveType, vertexStride, indexBuffer, verticesPerRepetition, |
| 28 | indicesPerRepetition, repeatCount); |
| 29 | } |
| 30 | |
| 31 | void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType, |
| 32 | size_t vertexStride, const GrBuffer* indexBuffer, |
| 33 | int verticesPerRepetition, int indicesPerRepetition, |
| 34 | int repeatCount) { |
| 35 | SkASSERT(target); |
| 36 | if (!indexBuffer) { |
| 37 | return; |
| 38 | } |
| 39 | const GrBuffer* vertexBuffer; |
| 40 | int firstVertex; |
| 41 | int vertexCount = verticesPerRepetition * repeatCount; |
| 42 | fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex); |
| 43 | if (!fVertices) { |
| 44 | SkDebugf("Vertices could not be allocated for patterned rendering."); |
| 45 | return; |
| 46 | } |
| 47 | SkASSERT(vertexBuffer); |
| 48 | size_t ibSize = indexBuffer->gpuMemorySize(); |
| 49 | int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition)); |
| 50 | fMesh = target->allocMesh(primitiveType); |
| 51 | fMesh->setIndexedPatterned(indexBuffer, indicesPerRepetition, verticesPerRepetition, |
| 52 | repeatCount, maxRepetitions); |
| 53 | fMesh->setVertexData(vertexBuffer, firstVertex); |
| 54 | } |
| 55 | |
| 56 | void GrMeshDrawOp::PatternHelper::recordDraw( |
| 57 | Target* target, sk_sp<const GrGeometryProcessor> gp, const GrPipeline* pipeline, |
| 58 | const GrPipeline::FixedDynamicState* fixedDynamicState) const { |
| 59 | target->draw(std::move(gp), pipeline, fixedDynamicState, fMesh); |
| 60 | } |
| 61 | |
| 62 | ////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) { |
| 65 | sk_sp<const GrBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer(); |
| 66 | if (!quadIndexBuffer) { |
| 67 | SkDebugf("Could not get quad index buffer."); |
| 68 | return; |
| 69 | } |
| 70 | this->init(target, GrPrimitiveType::kTriangles, vertexStride, quadIndexBuffer.get(), |
| 71 | kVerticesPerQuad, kIndicesPerQuad, quadsToDraw); |
| 72 | } |
| 73 | |
| 74 | ////////////////////////////////////////////////////////////////////////////// |
| 75 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 76 | GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::allocFixedDynamicState( |
| 77 | const SkIRect& rect, int numPrimitiveProcessorTextures) { |
| 78 | auto result = this->pipelineArena()->make<GrPipeline::FixedDynamicState>(rect); |
| 79 | if (numPrimitiveProcessorTextures) { |
| 80 | result->fPrimitiveProcessorTextures = |
| 81 | this->allocPrimitiveProcessorTextureArray(numPrimitiveProcessorTextures); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::allocDynamicStateArrays( |
| 87 | int numMeshes, int numPrimitiveProcessorTextures, bool allocScissors) { |
| 88 | auto result = this->pipelineArena()->make<GrPipeline::DynamicStateArrays>(); |
| 89 | if (allocScissors) { |
| 90 | result->fScissorRects = this->pipelineArena()->makeArray<SkIRect>(numMeshes); |
| 91 | } |
| 92 | if (numPrimitiveProcessorTextures) { |
| 93 | result->fPrimitiveProcessorTextures = this->allocPrimitiveProcessorTextureArray( |
| 94 | numPrimitiveProcessorTextures * numMeshes); |
| 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | |
Brian Salomon | f513682 | 2018-08-03 09:09:36 -0400 | [diff] [blame] | 99 | GrMeshDrawOp::Target::PipelineAndFixedDynamicState GrMeshDrawOp::Target::makePipeline( |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 100 | uint32_t pipelineFlags, GrProcessorSet&& processorSet, GrAppliedClip&& clip, |
| 101 | int numPrimProcTextures) { |
Brian Salomon | f513682 | 2018-08-03 09:09:36 -0400 | [diff] [blame] | 102 | GrPipeline::InitArgs pipelineArgs; |
| 103 | pipelineArgs.fFlags = pipelineFlags; |
| 104 | pipelineArgs.fProxy = this->proxy(); |
| 105 | pipelineArgs.fDstProxy = this->dstProxy(); |
| 106 | pipelineArgs.fCaps = &this->caps(); |
| 107 | pipelineArgs.fResourceProvider = this->resourceProvider(); |
| 108 | GrPipeline::FixedDynamicState* fixedDynamicState = nullptr; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 109 | if (clip.scissorState().enabled() || numPrimProcTextures) { |
Brian Salomon | f513682 | 2018-08-03 09:09:36 -0400 | [diff] [blame] | 110 | fixedDynamicState = this->allocFixedDynamicState(clip.scissorState().rect()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 111 | if (numPrimProcTextures) { |
| 112 | fixedDynamicState->fPrimitiveProcessorTextures = |
| 113 | this->allocPrimitiveProcessorTextureArray(numPrimProcTextures); |
| 114 | } |
Brian Salomon | f513682 | 2018-08-03 09:09:36 -0400 | [diff] [blame] | 115 | } |
| 116 | return {this->allocPipeline(pipelineArgs, std::move(processorSet), std::move(clip)), |
| 117 | fixedDynamicState}; |
| 118 | } |