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 "src/gpu/ops/GrMeshDrawOp.h"
9
10#include "src/gpu/GrOpFlushState.h"
11#include "src/gpu/GrOpsRenderPass.h"
12#include "src/gpu/GrRecordingContextPriv.h"
13#include "src/gpu/GrResourceProvider.h"
14
15GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
16
17void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
18
19void GrMeshDrawOp::createProgramInfo(Target* target) {
20 this->createProgramInfo(&target->caps(),
21 target->allocator(),
22 target->writeView(),
23 target->detachAppliedClip(),
24 target->dstProxyView());
25}
26
27// This onPrepareDraws implementation assumes the derived Op only has a single programInfo -
28// which is the majority of the cases.
29void GrMeshDrawOp::onPrePrepareDraws(GrRecordingContext* context,
30 const GrSurfaceProxyView* writeView,
31 GrAppliedClip* clip,
32 const GrXferProcessor::DstProxyView& dstProxyView) {
33 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
34
35 // This is equivalent to a GrOpFlushState::detachAppliedClip
36 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
37
38 this->createProgramInfo(context->priv().caps(), arena, writeView,
39 std::move(appliedClip), dstProxyView);
40
41 // TODO: at this point we've created both the program info and desc in the recording context's
42 // arena. In the DDL case, it would be cool if 'recordProgramInfo' could return the
43 // pre-existing versions if the program has already been seen. We could then return the
44 // memory for the current copy to the arena.
45 context->priv().recordProgramInfo(this->programInfo());
46}
47
48//////////////////////////////////////////////////////////////////////////////
49
50GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
51 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
52 int verticesPerRepetition, int indicesPerRepetition,
53 int repeatCount, int maxRepetitions) {
54 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
55 indicesPerRepetition, repeatCount, maxRepetitions);
56}
57
58void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
59 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
60 int verticesPerRepetition, int indicesPerRepetition,
61 int repeatCount, int maxRepetitions) {
62 SkASSERT(target);
63 if (!indexBuffer) {
64 return;
65 }
66 sk_sp<const GrBuffer> vertexBuffer;
67 int firstVertex;
68 int vertexCount = verticesPerRepetition * repeatCount;
69 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
70 if (!fVertices) {
71 SkDebugf("Vertices could not be allocated for patterned rendering.");
72 return;
73 }
74 SkASSERT(vertexBuffer);
75 fMesh = target->allocMesh();
76 fPrimitiveType = primitiveType;
77
78 SkASSERT(maxRepetitions ==
79 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
80 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
81 maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
82 firstVertex);
83}
84
85void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
86 target->recordDraw(gp, fMesh, 1, fPrimitiveType);
87}
88
89void GrMeshDrawOp::PatternHelper::recordDraw(
90 Target* target,
91 const GrGeometryProcessor* gp,
92 const GrSurfaceProxy* const primProcProxies[]) const {
93 target->recordDraw(gp, fMesh, 1, primProcProxies, fPrimitiveType);
94}
95
96//////////////////////////////////////////////////////////////////////////////
97
98GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
99 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
100 if (!indexBuffer) {
101 SkDebugf("Could not get quad index buffer.");
102 return;
103 }
104 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
105 GrResourceProvider::NumVertsPerNonAAQuad(),
106 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
107 GrResourceProvider::MaxNumNonAAQuads());
108}
109