Flutter Engine
The Flutter Engine
DrawAtlasOp.cpp
Go to the documentation of this file.
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
9
12#include "src/base/SkRandom.h"
14#include "src/core/SkRectPriv.h"
20#include "src/gpu/ganesh/SkGr.h"
22
23using namespace skia_private;
24
25namespace {
26
27class DrawAtlasOpImpl final : public GrMeshDrawOp {
28private:
30
31public:
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
38 const char* name() const override { return "DrawAtlasOp"; }
39
40 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
51
52private:
53 GrProgramInfo* programInfo() override { return fProgramInfo; }
54
55 void onCreateProgramInfo(const GrCaps*,
57 const GrSurfaceProxyView& writeView,
58 bool usesMSAASurface,
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
70 const SkPMColor4f& color() const { return fColor; }
71 const SkMatrix& viewMatrix() const { return fViewMatrix; }
72 bool hasColors() const { return fHasColors; }
73 int quadCount() const { return fQuadCount; }
74
75 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override;
76
77 struct Geometry {
78 SkPMColor4f fColor;
80 };
81
83 Helper fHelper;
85 SkPMColor4f fColor;
86 int fQuadCount;
87 bool fHasColors;
88
89 GrSimpleMesh* fMesh = nullptr;
90 GrProgramInfo* fProgramInfo = nullptr;
91};
92
93GrGeometryProcessor* 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
107DrawAtlasOpImpl::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) {
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
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);
167 currVertex += vertexStride;
168
169 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[1];
170 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
171 SkPoint::Make(currRect.fLeft, currRect.fBottom);
173 currVertex += vertexStride;
174
175 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[2];
176 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
177 SkPoint::Make(currRect.fRight, currRect.fTop);
179 currVertex += vertexStride;
180
181 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[3];
182 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
183 SkPoint::Make(currRect.fRight, currRect.fBottom);
185 currVertex += vertexStride;
186 }
187
188 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
189}
190
191#if defined(GR_TEST_UTILS)
192SkString 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
203void 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
223void 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
251void 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
261GrOp::CombineResult DrawAtlasOpImpl::onCombineIfPossible(GrOp* t,
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
289GrDrawOp::FixedFunctionFlags DrawAtlasOpImpl::fixedFunctionFlags() const {
290 return fHelper.fixedFunctionFlags();
291}
292
293GrProcessorSet::Analysis DrawAtlasOpImpl::finalize(const GrCaps& caps,
294 const GrAppliedClip* clip,
295 GrClampType clampType) {
297 if (this->hasColors()) {
298 gpColor.setToUnknown();
299 } else {
300 gpColor.setToConstant(fColor);
301 }
302 auto result = fHelper.finalizeProcessors(caps, clip, clampType,
304 if (gpColor.isConstant(&fColor)) {
305 fHasColors = false;
306 }
307 return result;
308}
309
310} // anonymous namespace
311
313
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)
332
333static 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
350static 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
362static void randomize_params(uint32_t count, SkRandom* random, TArray<SkRSXform>* xforms,
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
374GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp) {
375 uint32_t spriteCount = random->nextRangeU(1, 100);
376
377 TArray<SkRSXform> xforms(spriteCount);
378 TArray<SkRect> texRects(spriteCount);
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
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
SkMatrix fViewMatrix
int count
Definition: FontMgrTest.cpp:50
#define GrColorUnpackA(color)
Definition: GrColor.h:62
uint32_t GrColor
Definition: GrColor.h:25
#define DEFINE_OP_CLASS_ID
Definition: GrOp.h:64
GrClampType
Definition: GrTypesPriv.h:228
std::function< void(GrSurfaceProxy *, skgpu::Mipmapped)> GrVisitProxyFunc
Definition: GrTypesPriv.h:943
GrAAType
Definition: GrTypesPriv.h:200
GrLoadOp
Definition: GrTypesPriv.h:155
GrXferBarrierFlags
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition: SkColor.h:82
#define SkColorGetA(color)
Definition: SkColor.h:61
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static GrColor SkColorToPremulGrColor(SkColor c)
Definition: SkGr.h:51
static U8CPU SkMulDiv255Round(U16CPU a, U16CPU b)
Definition: SkMath.h:73
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define SK_ScalarPI
Definition: SkScalar.h:21
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
Definition: GrCaps.h:57
virtual FixedFunctionFlags fixedFunctionFlags() const
Definition: GrDrawOp.h:112
virtual GrProcessorSet::Analysis finalize(const GrCaps &, const GrAppliedClip *, GrClampType)=0
friend class GrSimpleMeshDrawOpHelper
Definition: GrDrawOp.h:118
FixedFunctionFlags
Definition: GrDrawOp.h:104
size_t vertexStride() const
virtual GrProgramInfo * programInfo()=0
virtual void onCreateProgramInfo(const GrCaps *, SkArenaAlloc *, const GrSurfaceProxyView &writeView, bool usesMSAASurface, GrAppliedClip &&, const GrDstProxyView &, GrXferBarrierFlags renderPassXferBarriers, GrLoadOp colorLoadOp)=0
virtual void onPrepareDraws(GrMeshDrawTarget *)=0
void drawMesh(const GrSimpleMesh &mesh)
void bindPipelineAndScissorClip(const GrProgramInfo &programInfo, const SkRect &drawBounds)
void bindTextures(const GrGeometryProcessor &geomProc, const GrSurfaceProxy &singleGeomProcTexture, const GrPipeline &pipeline)
Definition: GrOp.h:70
CombineResult
Definition: GrOp.h:99
virtual void onExecute(GrOpFlushState *, const SkRect &chainBounds)=0
std::unique_ptr< GrOp > Owner
Definition: GrOp.h:72
virtual const char * name() const =0
const T & cast() const
Definition: GrOp.h:148
virtual void visitProxies(const GrVisitProxyFunc &) const
Definition: GrOp.h:95
virtual CombineResult onCombineIfPossible(GrOp *, SkArenaAlloc *, const GrCaps &)
Definition: GrOp.h:305
bool isConstant(SkPMColor4f *color=nullptr) const
void setToConstant(const SkPMColor4f &color)
const GrPipeline & pipeline() const
Definition: GrProgramInfo.h:39
const GrGeometryProcessor & geomProc() const
Definition: GrProgramInfo.h:40
void visitFPProxies(const GrVisitProxyFunc &func) const
Definition: GrProgramInfo.h:64
void visitProxies(const GrVisitProxyFunc &func) const
static bool CheapEqual(const SkMatrix &a, const SkMatrix &b)
Definition: SkMatrixPriv.h:181
bool nextBool()
Definition: SkRandom.h:117
SkScalar nextRangeScalar(SkScalar min, SkScalar max)
Definition: SkRandom.h:106
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition: SkRandom.h:80
static constexpr SkRect MakeLargestInverted()
Definition: SkRectPriv.h:43
static void GrowToInclude(SkRect *r, const SkPoint &pt)
Definition: SkRectPriv.h:47
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
uint32_t * target
GrGeometryProcessor * Make(SkArenaAlloc *, const Color &, const Coverage &, const LocalCoords &, const SkMatrix &viewMatrix)
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkRSXform > xforms
Definition: SkRecords.h:332
PODArray< SkColor > colors
Definition: SkRecords.h:276
void Helper(uword arg)
GrOp::Owner Make(GrRecordingContext *context, GrPaint &&paint, const SkMatrix &viewMatrix, GrAAType aaType, int spriteCount, const SkRSXform *xforms, const SkRect *rects, const SkColor *colors)
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty, SkScalar ax, SkScalar ay)
Definition: SkRSXform.h:35
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
void sort()
Definition: SkRect.h:1313
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15