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