aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/batches/GrTInstanceBatch.h
blob: b85b3aa13def51cf7f0ee8b7d7e4cd5c5dd2a7fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrTInstanceBatch_DEFINED
#define GrTInstanceBatch_DEFINED

#include "GrVertexBatch.h"

#include "GrBatchFlushState.h"

/**
 * GrTInstanceBatch is an optional template to help with writing batches
 * To use this template, The 'Impl' must define the following statics:
 *     A Geometry struct
 *
 *     static const int kVertsPerInstance
 *     static const int kIndicesPerInstance
 *
 *     const char* Name()
 *
 *     void InvariantOutputCoverage(GrInitInvariantOutput* out)
 *
 *     void SetBounds(const Geometry& seedGeometry, SkRect* outBounds)
 *
 *     void UpdateBoundsAfterAppend(const Geometry& lastGeometry, SkRect* currentBounds)
 *
 *     bool CanCombine(const Geometry& mine, const Geometry& theirs,
 *                     const GrXPOverridesForBatch&)
 *
 *     const GrGeometryProcessor* CreateGP(const Geometry& seedGeometry,
 *                                         const GrXPOverridesForBatch& overrides)
 *
 *     const GrBuffer* GetIndexBuffer(GrResourceProvider*)
 *
 *     Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
 *               const GrXPOverridesForBatch& overrides)
 */
template <typename Impl>
class GrTInstanceBatch : public GrVertexBatch {
public:
    DEFINE_BATCH_CLASS_ID

    typedef typename Impl::Geometry Geometry;

    static GrTInstanceBatch* Create() { return new GrTInstanceBatch; }

    const char* name() const override { return Impl::Name(); }

    SkString dumpInfo() const override {
        SkString str;
        for (int i = 0; i < fGeoData.count(); ++i) {
            str.append(Impl::DumpInfo(fGeoData[i], i));
        }
        str.append(INHERITED::dumpInfo());
        return str;
    }

    void computePipelineOptimizations(GrInitInvariantOutput* color,
                                      GrInitInvariantOutput* coverage,
                                      GrBatchToXPOverrides* overrides) const override {
        // When this is called on a batch, there is only one geometry bundle
        color->setKnownFourComponents(fGeoData[0].fColor);
        Impl::InitInvariantOutputCoverage(coverage);
    }

    void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
        overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
        fOverrides = overrides;
    }

    SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }

    // After seeding, the client should call init() so the Batch can initialize itself
    void init() {
        const Geometry& geo = fGeoData[0];
        Impl::SetBounds(geo, &fBounds);
    }

    void updateBoundsAfterAppend() {
        const Geometry& geo = fGeoData.back();
        Impl::UpdateBoundsAfterAppend(geo, &fBounds);
    }

private:
    GrTInstanceBatch() : INHERITED(ClassID()) {}

    void onPrepareDraws(Target* target) const override {
        SkAutoTUnref<const GrGeometryProcessor> gp(Impl::CreateGP(this->seedGeometry(),
                                                                  fOverrides));
        if (!gp) {
            SkDebugf("Couldn't create GrGeometryProcessor\n");
            return;
        }

        size_t vertexStride = gp->getVertexStride();
        int instanceCount = fGeoData.count();

        SkAutoTUnref<const GrBuffer> indexBuffer(
                Impl::GetIndexBuffer(target->resourceProvider()));
        InstancedHelper helper;
        void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
                                     indexBuffer, Impl::kVertsPerInstance,
                                     Impl::kIndicesPerInstance, instanceCount);
        if (!vertices || !indexBuffer) {
            SkDebugf("Could not allocate vertices\n");
            return;
        }

        for (int i = 0; i < instanceCount; i++) {
            intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
                             i * Impl::kVertsPerInstance * vertexStride;
            Impl::Tesselate(verts, vertexStride, fGeoData[i], fOverrides);
        }
        helper.recordDraw(target, gp);
    }

    const Geometry& seedGeometry() const { return fGeoData[0]; }

    bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
        GrTInstanceBatch* that = t->cast<GrTInstanceBatch>();
        if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
                                    that->bounds(), caps)) {
            return false;
        }

        if (!Impl::CanCombine(this->seedGeometry(), that->seedGeometry(), fOverrides)) {
            return false;
        }

        // In the event of two batches, one who can tweak, one who cannot, we just fall back to
        // not tweaking
        if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
            fOverrides = that->fOverrides;
        }

        fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
        this->joinBounds(that->bounds());
        return true;
    }

    GrXPOverridesForBatch fOverrides;
    SkSTArray<1, Geometry, true> fGeoData;

    typedef GrVertexBatch INHERITED;
};

#endif