aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/batches/GrDrawAtlasBatch.cpp
blob: 3a71fcbc1bda1ac79e69804a7f3c1c3dc9bcee89 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrDrawAtlasBatch.h"
#include "GrBatchTest.h"
#include "SkRandom.h"

void GrDrawAtlasBatch::initBatchTracker(const GrPipelineInfo& init) {
    // Handle any color overrides
    if (!init.readsColor()) {
        fGeoData[0].fColor = GrColor_ILLEGAL;
    }
    init.getOverrideColorIfSet(&fGeoData[0].fColor);
    
    // setup batch properties
    fColorIgnored = !init.readsColor();
    fColor = fGeoData[0].fColor;
    // We'd like to assert this, but we can't because of GLPrograms test
    //SkASSERT(init.readsLocalCoords());
    fCoverageIgnored = !init.readsCoverage();
}

static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
                                                        bool hasColors,
                                                        int* colorOffset,
                                                        int* texOffset,
                                                        GrColor color,
                                                        const SkMatrix& viewMatrix,
                                                        bool coverageIgnored) {
    using namespace GrDefaultGeoProcFactory;
    *texOffset = -1;
    *colorOffset = -1;
    Color gpColor(color);
    if (hasColors) {
        gpColor.fType = Color::kAttribute_Type;
    }
    
    Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_Type);
    LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type :
                            LocalCoords::kUsePosition_Type);
    if (hasLocalCoords && hasColors) {
        *colorOffset = sizeof(SkPoint);
        *texOffset = sizeof(SkPoint) + sizeof(GrColor);
    } else if (hasLocalCoords) {
        *texOffset = sizeof(SkPoint);
    } else if (hasColors) {
        *colorOffset = sizeof(SkPoint);
    }
    return GrDefaultGeoProcFactory::Create(gpColor, coverage, localCoords, viewMatrix);
}

void GrDrawAtlasBatch::generateGeometry(GrBatchTarget* batchTarget) {
    int colorOffset = -1, texOffset = -1;
    // Setup geometry processor
    SkAutoTUnref<const GrGeometryProcessor> gp(
                                set_vertex_attributes(true, this->hasColors(), &colorOffset,
                                                      &texOffset, this->color(), this->viewMatrix(),
                                                      this->coverageIgnored()));
    
    batchTarget->initDraw(gp, this->pipeline());
    
    int instanceCount = fGeoData.count();
    size_t vertexStride = gp->getVertexStride();
    SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(SkPoint)
             + (this->hasColors() ? sizeof(GrColor) : 0));
    
    QuadHelper helper;
    int numQuads = this->vertexCount()/4;
    void* verts = helper.init(batchTarget, vertexStride, numQuads);
    if (!verts) {
        SkDebugf("Could not allocate vertices\n");
        return;
    }
    
    int vertexOffset = 0;
    for (int i = 0; i < instanceCount; i++) {
        const Geometry& args = fGeoData[i];
        
        for (int j = 0; j < args.fPositions.count(); ++j) {
            *((SkPoint*)verts) = args.fPositions[j];
            if (this->hasColors()) {
                *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j];
            }
            *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j];
            verts = (void*)((intptr_t)verts + vertexStride);
            vertexOffset++;
        }
    }
    helper.issueDraw(batchTarget);
}

GrDrawAtlasBatch::GrDrawAtlasBatch(const Geometry& geometry, const SkMatrix& viewMatrix,
                                   const SkPoint* positions, int vertexCount,
                                   const GrColor* colors, const SkPoint* localCoords,
                                   const SkRect& bounds) {
    this->initClassID<GrDrawAtlasBatch>();
    SkASSERT(positions);
    SkASSERT(localCoords);
    
    fViewMatrix = viewMatrix;
    Geometry& installedGeo = fGeoData.push_back(geometry);
    
    installedGeo.fPositions.append(vertexCount, positions);
    
    if (colors) {
        installedGeo.fColors.append(vertexCount, colors);
        fHasColors = true;
    } else {
        fHasColors = false;
    }
    
    installedGeo.fLocalCoords.append(vertexCount, localCoords);
    fVertexCount = vertexCount;
    
    this->setBounds(bounds);
}
 
bool GrDrawAtlasBatch::onCombineIfPossible(GrBatch* t) {
    if (!this->pipeline()->isEqual(*t->pipeline())) {
        return false;
    }
    
    GrDrawAtlasBatch* that = t->cast<GrDrawAtlasBatch>();
    
    // We currently use a uniform viewmatrix for this batch
    if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
        return false;
    }
    
    if (this->hasColors() != that->hasColors()) {
        return false;
    }
    
    if (!this->hasColors() && this->color() != that->color()) {
        return false;
    }
    
    if (this->color() != that->color()) {
        fColor = GrColor_ILLEGAL;
    }
    fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
    fVertexCount += that->vertexCount();
    
    this->joinBounds(that->bounds());
    return true;
}

#ifdef GR_TEST_UTILS

static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
    SkPoint p;
    p.fX = random->nextRangeScalar(min, max);
    p.fY = random->nextRangeScalar(min, max);
    return p;
}

static void randomize_params(size_t count, SkScalar min, SkScalar max,
                             SkRandom* random,
                             SkTArray<SkPoint>* positions,
                             SkTArray<SkPoint>* texCoords,
                             SkTArray<GrColor>* colors, bool hasColors) {
    for (uint32_t v = 0; v < count; v++) {
        positions->push_back(random_point(random, min, max));
        texCoords->push_back(random_point(random, min, max));
        if (hasColors) {
            colors->push_back(GrRandomColor(random));
        }
    }
}


BATCH_TEST_DEFINE(GrDrawAtlasBatch) {
    uint32_t spriteCount = random->nextRangeU(1, 100);
    
    // TODO make 'sensible' indexbuffers
    SkTArray<SkPoint> positions;
    SkTArray<SkPoint> texCoords;
    SkTArray<GrColor> colors;
    
    bool hasColors = random->nextBool();
    
    uint32_t vertexCount = 4*spriteCount;
    
    static const SkScalar kMinVertExtent = -100.f;
    static const SkScalar kMaxVertExtent = 100.f;
    randomize_params(vertexCount, kMinVertExtent, kMaxVertExtent,
                     random,
                     &positions,
                     &texCoords,
                     &colors, hasColors);
    
    SkMatrix viewMatrix = GrTest::TestMatrix(random);
    SkRect bounds;
    SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexCount);
    SkASSERT(result);
    
    viewMatrix.mapRect(&bounds);
    
    GrDrawAtlasBatch::Geometry geometry;
    geometry.fColor = GrRandomColor(random);
    return GrDrawAtlasBatch::Create(geometry, viewMatrix,
                                    positions.begin(), vertexCount,
                                    colors.begin(),
                                    texCoords.begin(),
                                    bounds);
}

#endif