• 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/ganesh/ops/DrawAtlasOp.h"
9 
10 #include "include/core/SkRSXform.h"
11 #include "include/gpu/GrRecordingContext.h"
12 #include "src/base/SkRandom.h"
13 #include "src/core/SkMatrixPriv.h"
14 #include "src/core/SkRectPriv.h"
15 #include "src/gpu/ganesh/GrCaps.h"
16 #include "src/gpu/ganesh/GrDefaultGeoProcFactory.h"
17 #include "src/gpu/ganesh/GrOpFlushState.h"
18 #include "src/gpu/ganesh/GrProgramInfo.h"
19 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
20 #include "src/gpu/ganesh/SkGr.h"
21 #include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
22 
23 using namespace skia_private;
24 
25 namespace {
26 
27 class DrawAtlasOpImpl final : public GrMeshDrawOp {
28 private:
29     using Helper = GrSimpleMeshDrawOpHelper;
30 
31 public:
32     DEFINE_OP_CLASS_ID
33 
34     DrawAtlasOpImpl(GrProcessorSet*, const SkPMColor4f& color,
35                     const SkMatrix& viewMatrix, GrAAType, int spriteCount, const SkRSXform* xforms,
36                     const SkRect* rects, const SkColor* colors);
37 
name() const38     const char* name() const override { return "DrawAtlasOp"; }
39 
visitProxies(const GrVisitProxyFunc & func) const40     void visitProxies(const GrVisitProxyFunc& func) const override {
41         if (fProgramInfo) {
42             fProgramInfo->visitFPProxies(func);
43         } else {
44             fHelper.visitProxies(func);
45         }
46     }
47 
48     FixedFunctionFlags fixedFunctionFlags() const override;
49 
50     GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override;
51 
52 private:
programInfo()53     GrProgramInfo* programInfo() override { return fProgramInfo; }
54 
55     void onCreateProgramInfo(const GrCaps*,
56                              SkArenaAlloc*,
57                              const GrSurfaceProxyView& writeView,
58                              bool usesMSAASurface,
59                              GrAppliedClip&&,
60                              const GrDstProxyView&,
61                              GrXferBarrierFlags renderPassXferBarriers,
62                              GrLoadOp colorLoadOp) override;
63 
64     void onPrepareDraws(GrMeshDrawTarget*) override;
65     void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
66 #if defined(GR_TEST_UTILS)
67     SkString onDumpInfo() const override;
68 #endif
69 
color() const70     const SkPMColor4f& color() const { return fColor; }
viewMatrix() const71     const SkMatrix& viewMatrix() const { return fViewMatrix; }
hasColors() const72     bool hasColors() const { return fHasColors; }
quadCount() const73     int quadCount() const { return fQuadCount; }
74 
75     CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override;
76 
77     struct Geometry {
78         SkPMColor4f fColor;
79         TArray<uint8_t, true> fVerts;
80     };
81 
82     STArray<1, Geometry, true> fGeoData;
83     Helper fHelper;
84     SkMatrix fViewMatrix;
85     SkPMColor4f fColor;
86     int fQuadCount;
87     bool fHasColors;
88 
89     GrSimpleMesh* fMesh = nullptr;
90     GrProgramInfo* fProgramInfo = nullptr;
91 };
92 
make_gp(SkArenaAlloc * arena,bool hasColors,const SkPMColor4f & color,const SkMatrix & viewMatrix)93 GrGeometryProcessor* make_gp(SkArenaAlloc* arena,
94                              bool hasColors,
95                              const SkPMColor4f& color,
96                              const SkMatrix& viewMatrix) {
97     using namespace GrDefaultGeoProcFactory;
98     Color gpColor(color);
99     if (hasColors) {
100         gpColor.fType = Color::kPremulGrColorAttribute_Type;
101     }
102 
103     return GrDefaultGeoProcFactory::Make(arena, gpColor, Coverage::kSolid_Type,
104                                          LocalCoords::kHasExplicit_Type, viewMatrix);
105 }
106 
DrawAtlasOpImpl(GrProcessorSet * processorSet,const SkPMColor4f & color,const SkMatrix & viewMatrix,GrAAType aaType,int spriteCount,const SkRSXform * xforms,const SkRect * rects,const SkColor * colors)107 DrawAtlasOpImpl::DrawAtlasOpImpl(GrProcessorSet* processorSet, const SkPMColor4f& color,
108                                  const SkMatrix& viewMatrix, GrAAType aaType, int spriteCount,
109                                  const SkRSXform* xforms, const SkRect* rects,
110                                  const SkColor* colors)
111         : GrMeshDrawOp(ClassID()), fHelper(processorSet, aaType), fColor(color) {
112     SkASSERT(xforms);
113     SkASSERT(rects);
114 
115     fViewMatrix = viewMatrix;
116     Geometry& installedGeo = fGeoData.push_back();
117     installedGeo.fColor = color;
118 
119     // Figure out stride and offsets
120     // Order within the vertex is: position [color] texCoord
121     size_t texOffset = sizeof(SkPoint);
122     size_t vertexStride = 2 * sizeof(SkPoint);
123     fHasColors = SkToBool(colors);
124     if (colors) {
125         texOffset += sizeof(GrColor);
126         vertexStride += sizeof(GrColor);
127     }
128 
129     // Compute buffer size and alloc buffer
130     fQuadCount = spriteCount;
131     int allocSize = static_cast<int>(4 * vertexStride * spriteCount);
132     installedGeo.fVerts.reset(allocSize);
133     uint8_t* currVertex = installedGeo.fVerts.begin();
134 
135     SkRect bounds = SkRectPriv::MakeLargestInverted();
136     // TODO4F: Preserve float colors
137     int paintAlpha = GrColorUnpackA(installedGeo.fColor.toBytes_RGBA());
138     for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) {
139         // Transform rect
140         SkPoint strip[4];
141         const SkRect& currRect = rects[spriteIndex];
142         xforms[spriteIndex].toTriStrip(currRect.width(), currRect.height(), strip);
143 
144         // Copy colors if necessary
145         if (colors) {
146             // convert to GrColor
147             SkColor spriteColor = colors[spriteIndex];
148             if (paintAlpha != 255) {
149                 spriteColor = SkColorSetA(spriteColor,
150                                           SkMulDiv255Round(SkColorGetA(spriteColor), paintAlpha));
151             }
152             GrColor grColor = SkColorToPremulGrColor(spriteColor);
153 
154             *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor;
155             *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor;
156             *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) =
157                     grColor;
158             *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) =
159                     grColor;
160         }
161 
162         // Copy position and uv to verts
163         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[0];
164         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
165                 SkPoint::Make(currRect.fLeft, currRect.fTop);
166         SkRectPriv::GrowToInclude(&bounds, strip[0]);
167         currVertex += vertexStride;
168 
169         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[1];
170         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
171                 SkPoint::Make(currRect.fLeft, currRect.fBottom);
172         SkRectPriv::GrowToInclude(&bounds, strip[1]);
173         currVertex += vertexStride;
174 
175         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[2];
176         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
177                 SkPoint::Make(currRect.fRight, currRect.fTop);
178         SkRectPriv::GrowToInclude(&bounds, strip[2]);
179         currVertex += vertexStride;
180 
181         *(reinterpret_cast<SkPoint*>(currVertex)) = strip[3];
182         *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
183                 SkPoint::Make(currRect.fRight, currRect.fBottom);
184         SkRectPriv::GrowToInclude(&bounds, strip[3]);
185         currVertex += vertexStride;
186     }
187 
188     this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
189 }
190 
191 #if defined(GR_TEST_UTILS)
onDumpInfo() const192 SkString DrawAtlasOpImpl::onDumpInfo() const {
193     SkString string;
194     for (const auto& geo : fGeoData) {
195         string.appendf("Color: 0x%08x, Quads: %d\n", geo.fColor.toBytes_RGBA(),
196                        geo.fVerts.size() / 4);
197     }
198     string += fHelper.dumpInfo();
199     return string;
200 }
201 #endif
202 
onCreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && appliedClip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)203 void DrawAtlasOpImpl::onCreateProgramInfo(const GrCaps* caps,
204                                           SkArenaAlloc* arena,
205                                           const GrSurfaceProxyView& writeView,
206                                           bool usesMSAASurface,
207                                           GrAppliedClip&& appliedClip,
208                                           const GrDstProxyView& dstProxyView,
209                                           GrXferBarrierFlags renderPassXferBarriers,
210                                           GrLoadOp colorLoadOp) {
211     // Setup geometry processor
212     GrGeometryProcessor* gp = make_gp(arena,
213                                       this->hasColors(),
214                                       this->color(),
215                                       this->viewMatrix());
216 
217     fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, usesMSAASurface,
218                                              std::move(appliedClip), dstProxyView, gp,
219                                              GrPrimitiveType::kTriangles, renderPassXferBarriers,
220                                              colorLoadOp);
221 }
222 
onPrepareDraws(GrMeshDrawTarget * target)223 void DrawAtlasOpImpl::onPrepareDraws(GrMeshDrawTarget* target) {
224     if (!fProgramInfo) {
225         this->createProgramInfo(target);
226     }
227 
228     int instanceCount = fGeoData.size();
229     size_t vertexStride = fProgramInfo->geomProc().vertexStride();
230 
231     int numQuads = this->quadCount();
232     QuadHelper helper(target, vertexStride, numQuads);
233     void* verts = helper.vertices();
234     if (!verts) {
235         SkDebugf("Could not allocate vertices\n");
236         return;
237     }
238 
239     uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts);
240     for (int i = 0; i < instanceCount; i++) {
241         const Geometry& args = fGeoData[i];
242 
243         size_t allocSize = args.fVerts.size();
244         memcpy(vertPtr, args.fVerts.begin(), allocSize);
245         vertPtr += allocSize;
246     }
247 
248     fMesh = helper.mesh();
249 }
250 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)251 void DrawAtlasOpImpl::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
252     if (!fProgramInfo || !fMesh) {
253         return;
254     }
255 
256     flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
257     flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
258     flushState->drawMesh(*fMesh);
259 }
260 
onCombineIfPossible(GrOp * t,SkArenaAlloc *,const GrCaps & caps)261 GrOp::CombineResult DrawAtlasOpImpl::onCombineIfPossible(GrOp* t,
262                                                          SkArenaAlloc*,
263                                                          const GrCaps& caps) {
264     auto that = t->cast<DrawAtlasOpImpl>();
265 
266     if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
267         return CombineResult::kCannotCombine;
268     }
269 
270     // We currently use a uniform viewmatrix for this op.
271     if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
272         return CombineResult::kCannotCombine;
273     }
274 
275     if (this->hasColors() != that->hasColors()) {
276         return CombineResult::kCannotCombine;
277     }
278 
279     if (!this->hasColors() && this->color() != that->color()) {
280         return CombineResult::kCannotCombine;
281     }
282 
283     fGeoData.push_back_n(that->fGeoData.size(), that->fGeoData.begin());
284     fQuadCount += that->quadCount();
285 
286     return CombineResult::kMerged;
287 }
288 
fixedFunctionFlags() const289 GrDrawOp::FixedFunctionFlags DrawAtlasOpImpl::fixedFunctionFlags() const {
290     return fHelper.fixedFunctionFlags();
291 }
292 
finalize(const GrCaps & caps,const GrAppliedClip * clip,GrClampType clampType)293 GrProcessorSet::Analysis DrawAtlasOpImpl::finalize(const GrCaps& caps,
294                                                    const GrAppliedClip* clip,
295                                                    GrClampType clampType) {
296     GrProcessorAnalysisColor gpColor;
297     if (this->hasColors()) {
298         gpColor.setToUnknown();
299     } else {
300         gpColor.setToConstant(fColor);
301     }
302     auto result = fHelper.finalizeProcessors(caps, clip, clampType,
303                                              GrProcessorAnalysisCoverage::kNone, &gpColor);
304     if (gpColor.isConstant(&fColor)) {
305         fHasColors = false;
306     }
307     return result;
308 }
309 
310 } // anonymous namespace
311 
312 namespace skgpu::ganesh::DrawAtlasOp {
313 
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,GrAAType aaType,int spriteCount,const SkRSXform * xforms,const SkRect * rects,const SkColor * colors)314 GrOp::Owner Make(GrRecordingContext* context,
315                  GrPaint&& paint,
316                  const SkMatrix& viewMatrix,
317                  GrAAType aaType,
318                  int spriteCount,
319                  const SkRSXform* xforms,
320                  const SkRect* rects,
321                  const SkColor* colors) {
322     return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawAtlasOpImpl>(context, std::move(paint),
323                                                                     viewMatrix, aaType,
324                                                                     spriteCount, xforms,
325                                                                     rects, colors);
326 }
327 
328 }  // namespace skgpu::ganesh::DrawAtlasOp
329 
330 #if defined(GR_TEST_UTILS)
331 #include "src/gpu/ganesh/GrDrawOpTest.h"
332 
random_xform(SkRandom * random)333 static SkRSXform random_xform(SkRandom* random) {
334     static const SkScalar kMinExtent = -100.f;
335     static const SkScalar kMaxExtent = 100.f;
336     static const SkScalar kMinScale = 0.1f;
337     static const SkScalar kMaxScale = 100.f;
338     static const SkScalar kMinRotate = -SK_ScalarPI;
339     static const SkScalar kMaxRotate = SK_ScalarPI;
340 
341     SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale),
342                                                  random->nextRangeScalar(kMinRotate, kMaxRotate),
343                                                  random->nextRangeScalar(kMinExtent, kMaxExtent),
344                                                  random->nextRangeScalar(kMinExtent, kMaxExtent),
345                                                  random->nextRangeScalar(kMinExtent, kMaxExtent),
346                                                  random->nextRangeScalar(kMinExtent, kMaxExtent));
347     return xform;
348 }
349 
random_texRect(SkRandom * random)350 static SkRect random_texRect(SkRandom* random) {
351     static const SkScalar kMinCoord = 0.0f;
352     static const SkScalar kMaxCoord = 1024.f;
353 
354     SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord),
355                                       random->nextRangeScalar(kMinCoord, kMaxCoord),
356                                       random->nextRangeScalar(kMinCoord, kMaxCoord),
357                                       random->nextRangeScalar(kMinCoord, kMaxCoord));
358     texRect.sort();
359     return texRect;
360 }
361 
randomize_params(uint32_t count,SkRandom * random,TArray<SkRSXform> * xforms,TArray<SkRect> * texRects,TArray<GrColor> * colors,bool hasColors)362 static void randomize_params(uint32_t count, SkRandom* random, TArray<SkRSXform>* xforms,
363                              TArray<SkRect>* texRects, TArray<GrColor>* colors,
364                              bool hasColors) {
365     for (uint32_t v = 0; v < count; v++) {
366         xforms->push_back(random_xform(random));
367         texRects->push_back(random_texRect(random));
368         if (hasColors) {
369             colors->push_back(GrTest::RandomColor(random));
370         }
371     }
372 }
373 
GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp)374 GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp) {
375     uint32_t spriteCount = random->nextRangeU(1, 100);
376 
377     TArray<SkRSXform> xforms(spriteCount);
378     TArray<SkRect> texRects(spriteCount);
379     TArray<GrColor> colors;
380 
381     bool hasColors = random->nextBool();
382 
383     randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors);
384 
385     SkMatrix viewMatrix = GrTest::TestMatrix(random);
386     GrAAType aaType = GrAAType::kNone;
387     if (numSamples > 1 && random->nextBool()) {
388         aaType = GrAAType::kMSAA;
389     }
390 
391     return skgpu::ganesh::DrawAtlasOp::Make(context,
392                                             std::move(paint),
393                                             viewMatrix,
394                                             aaType,
395                                             spriteCount,
396                                             xforms.begin(),
397                                             texRects.begin(),
398                                             hasColors ? colors.begin() : nullptr);
399 }
400 
401 #endif
402