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