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