GrMeshDrawOp.cpp 4.5 KB

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