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 8 #include "GrGpuCommandBuffer.h" 9 #include "GrMeshDrawOp.h" 10 #include "GrOpFlushState.h" 11 #include "GrResourceProvider.h" 12 13 GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {} 14 15 void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); } 16 17 ////////////////////////////////////////////////////////////////////////////// 18 19 GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType, 20 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer, 21 int verticesPerRepetition, int indicesPerRepetition, 22 int repeatCount) { 23 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition, 24 indicesPerRepetition, repeatCount); 25 } 26 27 void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType, 28 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer, 29 int verticesPerRepetition, int indicesPerRepetition, 30 int repeatCount) { 31 SkASSERT(target); 32 if (!indexBuffer) { 33 return; 34 } 35 sk_sp<const GrBuffer> vertexBuffer; 36 int firstVertex; 37 int vertexCount = verticesPerRepetition * repeatCount; 38 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex); 39 if (!fVertices) { 40 SkDebugf("Vertices could not be allocated for patterned rendering."); 41 return; 42 } 43 SkASSERT(vertexBuffer); 44 size_t ibSize = indexBuffer->size(); 45 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition)); 46 fMesh = target->allocMesh(primitiveType); 47 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, verticesPerRepetition, 48 repeatCount, maxRepetitions); 49 fMesh->setVertexData(std::move(vertexBuffer), firstVertex); 50 } 51 52 void GrMeshDrawOp::PatternHelper::recordDraw( 53 Target* target, sk_sp<const GrGeometryProcessor> gp) const { 54 target->recordDraw(std::move(gp), fMesh); 55 } 56 57 void GrMeshDrawOp::PatternHelper::recordDraw( 58 Target* target, sk_sp<const GrGeometryProcessor> gp, 59 const GrPipeline::FixedDynamicState* fixedDynamicState) const { 60 target->recordDraw(std::move(gp), fMesh, 1, fixedDynamicState, nullptr); 61 } 62 63 ////////////////////////////////////////////////////////////////////////////// 64 65 GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) { 66 sk_sp<const GrGpuBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer(); 67 if (!quadIndexBuffer) { 68 SkDebugf("Could not get quad index buffer."); 69 return; 70 } 71 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(quadIndexBuffer), 72 kVerticesPerQuad, kIndicesPerQuad, quadsToDraw); 73 } 74 75 ////////////////////////////////////////////////////////////////////////////// 76 77 GrPipeline::DynamicStateArrays* GrMeshDrawOp::Target::allocDynamicStateArrays( 78 int numMeshes, int numPrimitiveProcessorTextures, bool allocScissors) { 79 auto result = this->allocator()->make<GrPipeline::DynamicStateArrays>(); 80 if (allocScissors) { 81 result->fScissorRects = this->allocator()->makeArray<SkIRect>(numMeshes); 82 } 83 if (numPrimitiveProcessorTextures) { 84 result->fPrimitiveProcessorTextures = 85 this->allocator()->makeArrayDefault<GrTextureProxy*>( 86 numPrimitiveProcessorTextures * numMeshes); 87 } 88 return result; 89 } 90 91 GrPipeline::FixedDynamicState* GrMeshDrawOp::Target::makeFixedDynamicState( 92 int numPrimProcTextures) { 93 const GrAppliedClip* clip = this->appliedClip(); 94 if ((clip && clip->scissorState().enabled()) || numPrimProcTextures) { 95 const SkIRect& scissor = (clip) ? clip->scissorState().rect() : SkIRect::MakeEmpty(); 96 auto fixedDynamicState = 97 this->allocator()->make<GrPipeline::FixedDynamicState>(scissor); 98 if (numPrimProcTextures) { 99 fixedDynamicState->fPrimitiveProcessorTextures = 100 this->allocator()->makeArrayDefault<GrTextureProxy*>(numPrimProcTextures); 101 } 102 return fixedDynamicState; 103 } 104 return nullptr; 105 } 106