GrDrawAtlasOp.cpp revision 8c852be264d003b2e610c5b8634bc0f81c46bbba
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 "GrDrawAtlasOp.h"
9#include "GrDrawOpTest.h"
10#include "GrOpFlushState.h"
11#include "SkGr.h"
12#include "SkRSXform.h"
13#include "SkRandom.h"
14
15void GrDrawAtlasOp::applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) {
16    SkASSERT(fGeoData.count() == 1);
17    if (!optimizations.readsColor()) {
18        fGeoData[0].fColor = GrColor_ILLEGAL;
19    }
20    if (optimizations.getOverrideColorIfSet(&fGeoData[0].fColor) && fHasColors) {
21        size_t vertexStride =
22                sizeof(SkPoint) + sizeof(SkPoint) + (this->hasColors() ? sizeof(GrColor) : 0);
23        uint8_t* currVertex = fGeoData[0].fVerts.begin();
24        for (int i = 0; i < 4 * fQuadCount; ++i) {
25            *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = fGeoData[0].fColor;
26            currVertex += vertexStride;
27        }
28    }
29
30    fColorIgnored = !optimizations.readsColor();
31    fColor = fGeoData[0].fColor;
32    // We'd like to assert this, but we can't because of GLPrograms test
33    // SkASSERT(init.readsLocalCoords());
34}
35
36static sk_sp<GrGeometryProcessor> make_gp(bool hasColors,
37                                          GrColor color,
38                                          const SkMatrix& viewMatrix) {
39    using namespace GrDefaultGeoProcFactory;
40    Color gpColor(color);
41    if (hasColors) {
42        gpColor.fType = Color::kAttribute_Type;
43    }
44
45    return GrDefaultGeoProcFactory::Make(gpColor, Coverage::kSolid_Type,
46                                         LocalCoords::kHasExplicit_Type, viewMatrix);
47}
48
49void GrDrawAtlasOp::onPrepareDraws(Target* target) const {
50    // Setup geometry processor
51    sk_sp<GrGeometryProcessor> gp(make_gp(this->hasColors(), this->color(), this->viewMatrix()));
52
53    int instanceCount = fGeoData.count();
54    size_t vertexStride = gp->getVertexStride();
55    SkASSERT(vertexStride ==
56             sizeof(SkPoint) + sizeof(SkPoint) + (this->hasColors() ? sizeof(GrColor) : 0));
57
58    QuadHelper helper;
59    int numQuads = this->quadCount();
60    void* verts = helper.init(target, vertexStride, numQuads);
61    if (!verts) {
62        SkDebugf("Could not allocate vertices\n");
63        return;
64    }
65
66    uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts);
67    for (int i = 0; i < instanceCount; i++) {
68        const Geometry& args = fGeoData[i];
69
70        size_t allocSize = args.fVerts.count();
71        memcpy(vertPtr, args.fVerts.begin(), allocSize);
72        vertPtr += allocSize;
73    }
74    helper.recordDraw(target, gp.get());
75}
76
77GrDrawAtlasOp::GrDrawAtlasOp(GrColor color, const SkMatrix& viewMatrix, int spriteCount,
78                             const SkRSXform* xforms, const SkRect* rects, const SkColor* colors)
79        : INHERITED(ClassID()) {
80    SkASSERT(xforms);
81    SkASSERT(rects);
82
83    fViewMatrix = viewMatrix;
84    Geometry& installedGeo = fGeoData.push_back();
85    installedGeo.fColor = color;
86
87    // Figure out stride and offsets
88    // Order within the vertex is: position [color] texCoord
89    size_t texOffset = sizeof(SkPoint);
90    size_t vertexStride = 2 * sizeof(SkPoint);
91    fHasColors = SkToBool(colors);
92    if (colors) {
93        texOffset += sizeof(GrColor);
94        vertexStride += sizeof(GrColor);
95    }
96
97    // Compute buffer size and alloc buffer
98    fQuadCount = spriteCount;
99    int allocSize = static_cast<int>(4 * vertexStride * spriteCount);
100    installedGeo.fVerts.reset(allocSize);
101    uint8_t* currVertex = installedGeo.fVerts.begin();
102
103    SkRect bounds;
104    bounds.setLargestInverted();
105    int paintAlpha = GrColorUnpackA(installedGeo.fColor);
106    for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) {
107        // Transform rect
108        SkPoint quad[4];
109        const SkRect& currRect = rects[spriteIndex];
110        xforms[spriteIndex].toQuad(currRect.width(), currRect.height(), quad);
111
112        // Copy colors if necessary
113        if (colors) {
114            // convert to GrColor
115            SkColor color = colors[spriteIndex];
116            if (paintAlpha != 255) {
117                color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha));
118            }
119            GrColor grColor = SkColorToPremulGrColor(color);
120
121            *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor;
122            *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor;
123            *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) =
124                    grColor;
125            *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) =
126                    grColor;
127        }
128
129        // Copy position and uv to verts
130        *(reinterpret_cast<SkPoint*>(currVertex)) = quad[0];
131        *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
132                SkPoint::Make(currRect.fLeft, currRect.fTop);
133        bounds.growToInclude(quad[0].fX, quad[0].fY);
134        currVertex += vertexStride;
135
136        *(reinterpret_cast<SkPoint*>(currVertex)) = quad[1];
137        *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
138                SkPoint::Make(currRect.fRight, currRect.fTop);
139        bounds.growToInclude(quad[1].fX, quad[1].fY);
140        currVertex += vertexStride;
141
142        *(reinterpret_cast<SkPoint*>(currVertex)) = quad[2];
143        *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
144                SkPoint::Make(currRect.fRight, currRect.fBottom);
145        bounds.growToInclude(quad[2].fX, quad[2].fY);
146        currVertex += vertexStride;
147
148        *(reinterpret_cast<SkPoint*>(currVertex)) = quad[3];
149        *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
150                SkPoint::Make(currRect.fLeft, currRect.fBottom);
151        bounds.growToInclude(quad[3].fX, quad[3].fY);
152        currVertex += vertexStride;
153    }
154
155    this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
156}
157
158bool GrDrawAtlasOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
159    GrDrawAtlasOp* that = t->cast<GrDrawAtlasOp>();
160
161    if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
162                                that->bounds(), caps)) {
163        return false;
164    }
165
166    // We currently use a uniform viewmatrix for this op.
167    if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
168        return false;
169    }
170
171    if (this->hasColors() != that->hasColors()) {
172        return false;
173    }
174
175    if (!this->hasColors() && this->color() != that->color()) {
176        return false;
177    }
178
179    if (this->color() != that->color()) {
180        fColor = GrColor_ILLEGAL;
181    }
182    fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
183    fQuadCount += that->quadCount();
184
185    this->joinBounds(*that);
186    return true;
187}
188
189#ifdef GR_TEST_UTILS
190
191static SkRSXform random_xform(SkRandom* random) {
192    static const SkScalar kMinExtent = -100.f;
193    static const SkScalar kMaxExtent = 100.f;
194    static const SkScalar kMinScale = 0.1f;
195    static const SkScalar kMaxScale = 100.f;
196    static const SkScalar kMinRotate = -SK_ScalarPI;
197    static const SkScalar kMaxRotate = SK_ScalarPI;
198
199    SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale),
200                                                 random->nextRangeScalar(kMinRotate, kMaxRotate),
201                                                 random->nextRangeScalar(kMinExtent, kMaxExtent),
202                                                 random->nextRangeScalar(kMinExtent, kMaxExtent),
203                                                 random->nextRangeScalar(kMinExtent, kMaxExtent),
204                                                 random->nextRangeScalar(kMinExtent, kMaxExtent));
205    return xform;
206}
207
208static SkRect random_texRect(SkRandom* random) {
209    static const SkScalar kMinCoord = 0.0f;
210    static const SkScalar kMaxCoord = 1024.f;
211
212    SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord),
213                                      random->nextRangeScalar(kMinCoord, kMaxCoord),
214                                      random->nextRangeScalar(kMinCoord, kMaxCoord),
215                                      random->nextRangeScalar(kMinCoord, kMaxCoord));
216    texRect.sort();
217    return texRect;
218}
219
220static void randomize_params(uint32_t count, SkRandom* random, SkTArray<SkRSXform>* xforms,
221                             SkTArray<SkRect>* texRects, SkTArray<GrColor>* colors,
222                             bool hasColors) {
223    for (uint32_t v = 0; v < count; v++) {
224        xforms->push_back(random_xform(random));
225        texRects->push_back(random_texRect(random));
226        if (hasColors) {
227            colors->push_back(GrRandomColor(random));
228        }
229    }
230}
231
232DRAW_OP_TEST_DEFINE(GrDrawAtlasOp) {
233    uint32_t spriteCount = random->nextRangeU(1, 100);
234
235    SkTArray<SkRSXform> xforms(spriteCount);
236    SkTArray<SkRect> texRects(spriteCount);
237    SkTArray<GrColor> colors;
238
239    bool hasColors = random->nextBool();
240
241    randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors);
242
243    SkMatrix viewMatrix = GrTest::TestMatrix(random);
244
245    GrColor color = GrRandomColor(random);
246    return GrDrawAtlasOp::Make(color, viewMatrix, spriteCount, xforms.begin(), texRects.begin(),
247                               hasColors ? colors.begin() : nullptr);
248}
249
250#endif
251