• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/gpu/ops/GrDrawAtlasOp.h"
9 
10 #include "include/core/SkRSXform.h"
11 #include "include/private/GrRecordingContext.h"
12 #include "include/utils/SkRandom.h"
13 #include "src/core/SkRectPriv.h"
14 #include "src/gpu/GrCaps.h"
15 #include "src/gpu/GrDefaultGeoProcFactory.h"
16 #include "src/gpu/GrDrawOpTest.h"
17 #include "src/gpu/GrOpFlushState.h"
18 #include "src/gpu/GrRecordingContextPriv.h"
19 #include "src/gpu/SkGr.h"
20 #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
21 
22 namespace {
23 
24 class DrawAtlasOp final : public GrMeshDrawOp {
25 private:
26     using Helper = GrSimpleMeshDrawOpHelper;
27 
28 public:
29     DEFINE_OP_CLASS_ID
30 
31     DrawAtlasOp(const Helper::MakeArgs&, const SkPMColor4f& color,
32                 const SkMatrix& viewMatrix, GrAAType, int spriteCount, const SkRSXform* xforms,
33                 const SkRect* rects, const SkColor* colors);
34 
name() const35     const char* name() const override { return "DrawAtlasOp"; }
36 
visitProxies(const VisitProxyFunc & func) const37     void visitProxies(const VisitProxyFunc& func) const override {
38         fHelper.visitProxies(func);
39     }
40 
41 #ifdef SK_DEBUG
42     SkString dumpInfo() const override;
43 #endif
44 
45     FixedFunctionFlags fixedFunctionFlags() const override;
46 
47     GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
48                                       bool hasMixedSampledCoverage, GrClampType) override;
49 
50 private:
51     void onPrepareDraws(Target*) override;
52     void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
53 
color() const54     const SkPMColor4f& color() const { return fColor; }
viewMatrix() const55     const SkMatrix& viewMatrix() const { return fViewMatrix; }
hasColors() const56     bool hasColors() const { return fHasColors; }
quadCount() const57     int quadCount() const { return fQuadCount; }
58 
59     CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
60 
61     struct Geometry {
62         SkPMColor4f fColor;
63         SkTArray<uint8_t, true> fVerts;
64     };
65 
66     SkSTArray<1, Geometry, true> fGeoData;
67     Helper fHelper;
68     SkMatrix fViewMatrix;
69     SkPMColor4f fColor;
70     int fQuadCount;
71     bool fHasColors;
72 
73     typedef GrMeshDrawOp INHERITED;
74 };
75 
make_gp(const GrShaderCaps * shaderCaps,bool hasColors,const SkPMColor4f & color,const SkMatrix & viewMatrix)76 static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
77                                           bool hasColors,
78                                           const SkPMColor4f& color,
79                                           const SkMatrix& viewMatrix) {
80     using namespace GrDefaultGeoProcFactory;
81     Color gpColor(color);
82     if (hasColors) {
83         gpColor.fType = Color::kPremulGrColorAttribute_Type;
84     }
85 
86     return GrDefaultGeoProcFactory::Make(shaderCaps, gpColor, Coverage::kSolid_Type,
87                                          LocalCoords::kHasExplicit_Type, viewMatrix);
88 }
89 
DrawAtlasOp(const Helper::MakeArgs & helperArgs,const SkPMColor4f & color,const SkMatrix & viewMatrix,GrAAType aaType,int spriteCount,const SkRSXform * xforms,const SkRect * rects,const SkColor * colors)90 DrawAtlasOp::DrawAtlasOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
91                          const SkMatrix& viewMatrix, GrAAType aaType, int spriteCount,
92                          const SkRSXform* xforms, const SkRect* rects, const SkColor* colors)
93         : INHERITED(ClassID()), fHelper(helperArgs, aaType), fColor(color) {
94     SkASSERT(xforms);
95     SkASSERT(rects);
96 
97     fViewMatrix = viewMatrix;
98     Geometry& installedGeo = fGeoData.push_back();
99     installedGeo.fColor = color;
100 
101     // Figure out stride and offsets
102     // Order within the vertex is: position [color] texCoord
103     size_t texOffset = sizeof(SkPoint);
104     size_t vertexStride = 2 * sizeof(SkPoint);
105     fHasColors = SkToBool(colors);
106     if (colors) {
107         texOffset += sizeof(GrColor);
108         vertexStride += sizeof(GrColor);
109     }
110 
111     // Compute buffer size and alloc buffer
112     fQuadCount = spriteCount;
113     int allocSize = static_cast<int>(4 * vertexStride * spriteCount);
114     installedGeo.fVerts.reset(allocSize);
115     uint8_t* currVertex = installedGeo.fVerts.begin();
116 
117     SkRect bounds = SkRectPriv::MakeLargestInverted();
118     // TODO4F: Preserve float colors
119     int paintAlpha = GrColorUnpackA(installedGeo.fColor.toBytes_RGBA());
120     for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) {
121         // Transform rect
122         SkPoint strip[4];
123         const SkRect& currRect = rects[spriteIndex];
124         xforms[spriteIndex].toTriStrip(currRect.width(), currRect.height(), strip);
125 
126         // Copy colors if necessary
127         if (colors) {
128             // convert to GrColor
129             SkColor color = colors[spriteIndex];
130             if (paintAlpha != 255) {
131                 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha));
132             }
133             GrColor grColor = SkColorToPremulGrColor(color);
134 
135             *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor;
136             *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor;
137             *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) =
138                     grColor;
139             *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) =
140                     grColor;
141         }
142 
143         // Copy position and uv to verts
144         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[0];
145         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
146                 SkPoint::Make(currRect.fLeft, currRect.fTop);
147         SkRectPriv::GrowToInclude(&bounds, strip[0]);
148         currVertex += vertexStride;
149 
150         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[1];
151         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
152                 SkPoint::Make(currRect.fLeft, currRect.fBottom);
153         SkRectPriv::GrowToInclude(&bounds, strip[1]);
154         currVertex += vertexStride;
155 
156         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[2];
157         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
158                 SkPoint::Make(currRect.fRight, currRect.fTop);
159         SkRectPriv::GrowToInclude(&bounds, strip[2]);
160         currVertex += vertexStride;
161 
162         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[3];
163         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
164                 SkPoint::Make(currRect.fRight, currRect.fBottom);
165         SkRectPriv::GrowToInclude(&bounds, strip[3]);
166         currVertex += vertexStride;
167     }
168 
169     this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
170 }
171 
172 #ifdef SK_DEBUG
dumpInfo() const173 SkString DrawAtlasOp::dumpInfo() const {
174     SkString string;
175     for (const auto& geo : fGeoData) {
176         string.appendf("Color: 0x%08x, Quads: %d\n", geo.fColor.toBytes_RGBA(),
177                        geo.fVerts.count() / 4);
178     }
179     string += fHelper.dumpInfo();
180     string += INHERITED::dumpInfo();
181     return string;
182 }
183 #endif
184 
onPrepareDraws(Target * target)185 void DrawAtlasOp::onPrepareDraws(Target* target) {
186     // Setup geometry processor
187     sk_sp<GrGeometryProcessor> gp(make_gp(target->caps().shaderCaps(),
188                                           this->hasColors(),
189                                           this->color(),
190                                           this->viewMatrix()));
191 
192     int instanceCount = fGeoData.count();
193     size_t vertexStride = gp->vertexStride();
194 
195     int numQuads = this->quadCount();
196     QuadHelper helper(target, vertexStride, numQuads);
197     void* verts = helper.vertices();
198     if (!verts) {
199         SkDebugf("Could not allocate vertices\n");
200         return;
201     }
202 
203     uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts);
204     for (int i = 0; i < instanceCount; i++) {
205         const Geometry& args = fGeoData[i];
206 
207         size_t allocSize = args.fVerts.count();
208         memcpy(vertPtr, args.fVerts.begin(), allocSize);
209         vertPtr += allocSize;
210     }
211     helper.recordDraw(target, std::move(gp));
212 }
213 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)214 void DrawAtlasOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
215     fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
216 }
217 
onCombineIfPossible(GrOp * t,const GrCaps & caps)218 GrOp::CombineResult DrawAtlasOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
219     DrawAtlasOp* that = t->cast<DrawAtlasOp>();
220 
221     if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
222         return CombineResult::kCannotCombine;
223     }
224 
225     // We currently use a uniform viewmatrix for this op.
226     if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
227         return CombineResult::kCannotCombine;
228     }
229 
230     if (this->hasColors() != that->hasColors()) {
231         return CombineResult::kCannotCombine;
232     }
233 
234     if (!this->hasColors() && this->color() != that->color()) {
235         return CombineResult::kCannotCombine;
236     }
237 
238     fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
239     fQuadCount += that->quadCount();
240 
241     return CombineResult::kMerged;
242 }
243 
fixedFunctionFlags() const244 GrDrawOp::FixedFunctionFlags DrawAtlasOp::fixedFunctionFlags() const {
245     return fHelper.fixedFunctionFlags();
246 }
247 
finalize(const GrCaps & caps,const GrAppliedClip * clip,bool hasMixedSampledCoverage,GrClampType clampType)248 GrProcessorSet::Analysis DrawAtlasOp::finalize(
249         const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
250         GrClampType clampType) {
251     GrProcessorAnalysisColor gpColor;
252     if (this->hasColors()) {
253         gpColor.setToUnknown();
254     } else {
255         gpColor.setToConstant(fColor);
256     }
257     auto result = fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
258                                              GrProcessorAnalysisCoverage::kNone, &gpColor);
259     if (gpColor.isConstant(&fColor)) {
260         fHasColors = false;
261     }
262     return result;
263 }
264 
265 } // anonymous namespace
266 
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,GrAAType aaType,int spriteCount,const SkRSXform * xforms,const SkRect * rects,const SkColor * colors)267 std::unique_ptr<GrDrawOp> GrDrawAtlasOp::Make(GrRecordingContext* context,
268                                               GrPaint&& paint,
269                                               const SkMatrix& viewMatrix,
270                                               GrAAType aaType,
271                                               int spriteCount,
272                                               const SkRSXform* xforms,
273                                               const SkRect* rects,
274                                               const SkColor* colors) {
275     return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawAtlasOp>(context, std::move(paint),
276                                                                 viewMatrix, aaType,
277                                                                 spriteCount, xforms,
278                                                                 rects, colors);
279 }
280 
281 #if GR_TEST_UTILS
282 
random_xform(SkRandom * random)283 static SkRSXform random_xform(SkRandom* random) {
284     static const SkScalar kMinExtent = -100.f;
285     static const SkScalar kMaxExtent = 100.f;
286     static const SkScalar kMinScale = 0.1f;
287     static const SkScalar kMaxScale = 100.f;
288     static const SkScalar kMinRotate = -SK_ScalarPI;
289     static const SkScalar kMaxRotate = SK_ScalarPI;
290 
291     SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale),
292                                                  random->nextRangeScalar(kMinRotate, kMaxRotate),
293                                                  random->nextRangeScalar(kMinExtent, kMaxExtent),
294                                                  random->nextRangeScalar(kMinExtent, kMaxExtent),
295                                                  random->nextRangeScalar(kMinExtent, kMaxExtent),
296                                                  random->nextRangeScalar(kMinExtent, kMaxExtent));
297     return xform;
298 }
299 
random_texRect(SkRandom * random)300 static SkRect random_texRect(SkRandom* random) {
301     static const SkScalar kMinCoord = 0.0f;
302     static const SkScalar kMaxCoord = 1024.f;
303 
304     SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord),
305                                       random->nextRangeScalar(kMinCoord, kMaxCoord),
306                                       random->nextRangeScalar(kMinCoord, kMaxCoord),
307                                       random->nextRangeScalar(kMinCoord, kMaxCoord));
308     texRect.sort();
309     return texRect;
310 }
311 
randomize_params(uint32_t count,SkRandom * random,SkTArray<SkRSXform> * xforms,SkTArray<SkRect> * texRects,SkTArray<GrColor> * colors,bool hasColors)312 static void randomize_params(uint32_t count, SkRandom* random, SkTArray<SkRSXform>* xforms,
313                              SkTArray<SkRect>* texRects, SkTArray<GrColor>* colors,
314                              bool hasColors) {
315     for (uint32_t v = 0; v < count; v++) {
316         xforms->push_back(random_xform(random));
317         texRects->push_back(random_texRect(random));
318         if (hasColors) {
319             colors->push_back(GrRandomColor(random));
320         }
321     }
322 }
323 
GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp)324 GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp) {
325     uint32_t spriteCount = random->nextRangeU(1, 100);
326 
327     SkTArray<SkRSXform> xforms(spriteCount);
328     SkTArray<SkRect> texRects(spriteCount);
329     SkTArray<GrColor> colors;
330 
331     bool hasColors = random->nextBool();
332 
333     randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors);
334 
335     SkMatrix viewMatrix = GrTest::TestMatrix(random);
336     GrAAType aaType = GrAAType::kNone;
337     if (numSamples > 1 && random->nextBool()) {
338         aaType = GrAAType::kMSAA;
339     }
340 
341     return GrDrawAtlasOp::Make(context, std::move(paint), viewMatrix, aaType, spriteCount,
342                                xforms.begin(), texRects.begin(),
343                                hasColors ? colors.begin() : nullptr);
344 }
345 
346 #endif
347