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