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