blob: 3d182f83282d2c2f59f343c19fe0df181b7fa9a5 [file] [log] [blame]
jvanverthfa38a302014-10-06 05:59:05 -07001/*
2 * Copyright 2014 Google Inc.
joel.liang8cbb4242017-01-09 18:39:43 -08003 * Copyright 2017 ARM Ltd.
jvanverthfa38a302014-10-06 05:59:05 -07004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/ops/GrSmallPathRenderer.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPaint.h"
12#include "src/core/SkAutoMalloc.h"
13#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkDistanceFieldGen.h"
15#include "src/core/SkDraw.h"
Michael Ludwigfbe28592020-06-26 16:02:15 -040016#include "src/core/SkMatrixPriv.h"
Brian Osman9aaec362020-05-08 14:54:37 -040017#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkPointPriv.h"
19#include "src/core/SkRasterClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrBuffer.h"
22#include "src/gpu/GrCaps.h"
23#include "src/gpu/GrDistanceFieldGenFromVector.h"
24#include "src/gpu/GrDrawOpTest.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRenderTargetContext.h"
26#include "src/gpu/GrResourceProvider.h"
27#include "src/gpu/GrVertexWriter.h"
28#include "src/gpu/effects/GrBitmapTextGeoProc.h"
29#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040030#include "src/gpu/geometry/GrQuad.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050032#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
jvanverthfa38a302014-10-06 05:59:05 -070033
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080034static constexpr size_t kMaxAtlasTextureBytes = 2048 * 2048;
35static constexpr size_t kPlotWidth = 512;
36static constexpr size_t kPlotHeight = 256;
jvanverthfa38a302014-10-06 05:59:05 -070037
jvanverthb3eb6872014-10-24 07:12:51 -070038#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070039static int g_NumCachedShapes = 0;
40static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070041#endif
42
jvanverthb61283f2014-10-30 05:57:21 -070043// mip levels
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080044static constexpr SkScalar kIdealMinMIP = 12;
45static constexpr SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050046
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080047static constexpr SkScalar kMaxDim = 73;
48static constexpr SkScalar kMinSize = SK_ScalarHalf;
49static constexpr SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070050
Robert Phillipsd2e9f762018-03-07 11:54:37 -050051class ShapeDataKey {
52public:
53 ShapeDataKey() {}
54 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
Michael Ludwig2686d692020-04-17 20:21:37 +000055 ShapeDataKey(const GrStyledShape& shape, uint32_t dim) { this->set(shape, dim); }
56 ShapeDataKey(const GrStyledShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
Robert Phillipsd2e9f762018-03-07 11:54:37 -050057
58 ShapeDataKey& operator=(const ShapeDataKey& that) {
59 fKey.reset(that.fKey.count());
60 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
61 return *this;
62 }
63
64 // for SDF paths
Michael Ludwig2686d692020-04-17 20:21:37 +000065 void set(const GrStyledShape& shape, uint32_t dim) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -050066 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
67 // relevant styling information.
68 SkASSERT(shape.style().isSimpleFill());
69 SkASSERT(shape.hasUnstyledKey());
70 int shapeKeySize = shape.unstyledKeySize();
71 fKey.reset(1 + shapeKeySize);
72 fKey[0] = dim;
73 shape.writeUnstyledKey(&fKey[1]);
74 }
75
76 // for bitmap paths
Michael Ludwig2686d692020-04-17 20:21:37 +000077 void set(const GrStyledShape& shape, const SkMatrix& ctm) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -050078 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
79 // relevant styling information.
80 SkASSERT(shape.style().isSimpleFill());
81 SkASSERT(shape.hasUnstyledKey());
82 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
83 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
84 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
85 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
86 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
87 SkScalar tx = ctm.get(SkMatrix::kMTransX);
88 SkScalar ty = ctm.get(SkMatrix::kMTransY);
89 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050090 tx -= SkScalarFloorToScalar(tx);
91 ty -= SkScalarFloorToScalar(ty);
92 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
93 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050094 int shapeKeySize = shape.unstyledKeySize();
95 fKey.reset(5 + shapeKeySize);
96 fKey[0] = SkFloat2Bits(sx);
97 fKey[1] = SkFloat2Bits(sy);
98 fKey[2] = SkFloat2Bits(kx);
99 fKey[3] = SkFloat2Bits(ky);
100 fKey[4] = fracX | (fracY >> 8);
101 shape.writeUnstyledKey(&fKey[5]);
102 }
103
104 bool operator==(const ShapeDataKey& that) const {
105 return fKey.count() == that.fKey.count() &&
106 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
107 }
108
109 int count32() const { return fKey.count(); }
110 const uint32_t* data() const { return fKey.get(); }
111
112private:
Michael Ludwig2686d692020-04-17 20:21:37 +0000113 // The key is composed of the GrStyledShape's key, and either the dimensions of the DF
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500114 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
115 // the matrix for the path with only fractional translation.
116 SkAutoSTArray<24, uint32_t> fKey;
117};
118
119class ShapeData {
120public:
Robert Phillips6d3bc292020-04-06 10:29:28 -0400121 ShapeDataKey fKey;
122 SkRect fBounds;
123 GrDrawOpAtlas::AtlasLocator fAtlasLocator;
124
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500125 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
126
127 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
128 return data.fKey;
129 }
130
Kevin Lubickb5502b22018-03-12 10:17:06 -0400131 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500132 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
133 }
134};
135
136
137
joshualitt5bf99f12015-03-13 11:47:42 -0700138// Callback to clear out internal path cache when eviction occurs
Herb Derby4d721712020-01-24 14:31:16 -0500139void GrSmallPathRenderer::evict(GrDrawOpAtlas::PlotLocator plotLocator) {
joshualitt5bf99f12015-03-13 11:47:42 -0700140 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700141 ShapeDataList::Iter iter;
Herb Derby1a496c52020-01-22 17:26:56 -0500142 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
bsalomonee432412016-06-27 07:18:18 -0700143 ShapeData* shapeData;
144 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700145 iter.next();
Robert Phillips6d3bc292020-04-06 10:29:28 -0400146 if (plotLocator == shapeData->fAtlasLocator.plotLocator()) {
Herb Derby1a496c52020-01-22 17:26:56 -0500147 fShapeCache.remove(shapeData->fKey);
148 fShapeList.remove(shapeData);
bsalomonee432412016-06-27 07:18:18 -0700149 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700150#ifdef DF_PATH_TRACKING
151 ++g_NumFreedPaths;
152#endif
153 }
154 }
155}
156
jvanverthfa38a302014-10-06 05:59:05 -0700157////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400158GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700159
Jim Van Verth83010462017-03-16 08:45:39 -0400160GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700161 ShapeDataList::Iter iter;
162 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
163 ShapeData* shapeData;
164 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700165 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700166 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700167 }
jvanverthb3eb6872014-10-24 07:12:51 -0700168
169#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700170 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700171#endif
jvanverthfa38a302014-10-06 05:59:05 -0700172}
173
174////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600175GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700176 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600177 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700178 }
179 // If the shape has no key then we won't get any reuse.
180 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600181 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700182 }
183 // This only supports filled paths, however, the caller may apply the style to make a filled
184 // path and try again.
185 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600186 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700187 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500188 // This does non-inverse coverage-based antialiased fills.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600189 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600190 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700191 }
jvanverthfa38a302014-10-06 05:59:05 -0700192 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700193 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600194 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700195 }
halcanary9d524f22016-03-29 09:03:52 -0700196
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500197 // Only support paths with bounds within kMaxDim by kMaxDim,
198 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500199 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400200 SkScalar scaleFactors[2] = { 1, 1 };
201 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600202 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500203 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700204 SkRect bounds = args.fShape->styledBounds();
Brian Osman116b33e2020-02-05 13:34:09 -0500205 SkScalar minDim = std::min(bounds.width(), bounds.height());
206 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500207 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
208 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600209 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
210 return CanDrawPath::kNo;
211 }
bsalomon6266dca2016-03-11 06:22:00 -0800212
Chris Dalton5ed44232017-09-07 13:22:46 -0600213 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700214}
215
jvanverthfa38a302014-10-06 05:59:05 -0700216////////////////////////////////////////////////////////////////////////////////
217
joshualitt5bf99f12015-03-13 11:47:42 -0700218// padding around path bounds to allow for antialiased pixels
Robert Phillips6d3bc292020-04-06 10:29:28 -0400219static const int kAntiAliasPad = 1;
joshualitt5bf99f12015-03-13 11:47:42 -0700220
Brian Salomonfebbd232017-07-11 15:52:02 -0400221class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
222private:
223 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
224
joshualitt5bf99f12015-03-13 11:47:42 -0700225public:
Brian Salomon25a88092016-12-01 09:36:50 -0500226 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700227
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500228 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400229 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700230
Robert Phillipsb97da532019-02-12 15:24:12 -0500231 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400232 GrPaint&& paint,
Michael Ludwig2686d692020-04-17 20:21:37 +0000233 const GrStyledShape& shape,
Robert Phillips7c525e62018-06-12 10:11:12 -0400234 const SkMatrix& viewMatrix,
235 GrDrawOpAtlas* atlas,
236 ShapeCache* shapeCache,
237 ShapeDataList* shapeList,
Brian Salomonfebbd232017-07-11 15:52:02 -0400238 bool gammaCorrect,
239 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400240 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
241 atlas, shapeCache, shapeList, gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400242 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700243 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500244
Michael Ludwig2686d692020-04-17 20:21:37 +0000245 SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrStyledShape& shape,
Brian Salomonfebbd232017-07-11 15:52:02 -0400246 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
247 ShapeDataList* shapeList, bool gammaCorrect,
248 const GrUserStencilSettings* stencilSettings)
Robert Phillips4133dc42020-03-11 15:55:55 -0400249 : INHERITED(ClassID())
250 , fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500251 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500252 // Compute bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400253 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo);
Jim Van Verth33632d82017-02-28 10:24:39 -0500254
Jim Van Verth83010462017-03-16 08:45:39 -0400255#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500256 fUsesDistanceField = true;
257#else
Jim Van Verth83010462017-03-16 08:45:39 -0400258 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500259 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
260#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400261 // always use distance fields if in perspective
262 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500263
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400264 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500265
266 fAtlas = atlas;
267 fShapeCache = shapeCache;
268 fShapeList = shapeList;
269 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500270 }
271
Brian Salomonfebbd232017-07-11 15:52:02 -0400272 const char* name() const override { return "SmallPathOp"; }
273
Chris Dalton1706cbf2019-05-21 19:35:29 -0600274 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400275 fHelper.visitProxies(func);
276
Greg Daniel9715b6c2019-12-10 15:03:10 -0500277 const GrSurfaceProxyView* views = fAtlas->getViews();
Robert Phillips4bc70112018-03-01 10:24:02 -0500278 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500279 SkASSERT(views[i].proxy());
280 func(views[i].proxy(), GrMipMapped::kNo);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400281 }
282 }
283
Brian Osman9a390ac2018-11-12 09:47:48 -0500284#ifdef SK_DEBUG
Brian Salomonfebbd232017-07-11 15:52:02 -0400285 SkString dumpInfo() const override {
286 SkString string;
287 for (const auto& geo : fShapes) {
Brian Osmancf860852018-10-31 14:04:39 -0400288 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
Brian Salomonfebbd232017-07-11 15:52:02 -0400289 }
290 string += fHelper.dumpInfo();
291 string += INHERITED::dumpInfo();
292 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500293 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500294#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500295
Brian Salomonfebbd232017-07-11 15:52:02 -0400296 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
297
Chris Dalton6ce447a2019-06-23 18:07:38 -0600298 GrProcessorSet::Analysis finalize(
299 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
300 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700301 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600302 caps, clip, hasMixedSampledCoverage, clampType,
303 GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700304 }
305
Brian Salomonfebbd232017-07-11 15:52:02 -0400306private:
bsalomonb5238a72015-05-05 07:49:49 -0700307 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500308 sk_sp<const GrBuffer> fVertexBuffer;
309 sk_sp<const GrBuffer> fIndexBuffer;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500310 GrGeometryProcessor* fGeometryProcessor;
Chris Dalton304e14d2020-03-17 14:29:06 -0600311 const GrSurfaceProxy** fPrimProcProxies;
bsalomonb5238a72015-05-05 07:49:49 -0700312 int fVertexOffset;
313 int fInstancesToFlush;
314 };
315
Robert Phillips2669a7b2020-03-12 12:07:19 -0400316 GrProgramInfo* programInfo() override {
317 // TODO [PI]: implement
318 return nullptr;
319 }
320
Robert Phillips4133dc42020-03-11 15:55:55 -0400321 void onCreateProgramInfo(const GrCaps*,
322 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400323 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400324 GrAppliedClip&&,
325 const GrXferProcessor::DstProxyView&) override {
326 // TODO [PI]: implement
327 }
328
Robert Phillips2669a7b2020-03-12 12:07:19 -0400329 void onPrePrepareDraws(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400330 const GrSurfaceProxyView* writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400331 GrAppliedClip*,
332 const GrXferProcessor::DstProxyView&) override {
333 // TODO [PI]: implement
334 }
335
Brian Salomon91326c32017-08-09 16:02:19 -0400336 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500337 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700338
Brian Salomon7eae3e02018-08-07 14:02:38 +0000339 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500340 static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000341
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700342 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600343 flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000344 int numActiveProxies = fAtlas->numActivePages();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500345 const auto views = fAtlas->getViews();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000346 for (int i = 0; i < numActiveProxies; ++i) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400347 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
348 // proxies don't get added during the visitProxies call. Thus we add them here.
Chris Dalton304e14d2020-03-17 14:29:06 -0600349 flushInfo.fPrimProcProxies[i] = views[i].proxy();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500350 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000351 }
Brian Salomon49348902018-06-26 09:12:38 -0400352
joshualitt5bf99f12015-03-13 11:47:42 -0700353 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400354 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500355 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500356 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400357 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500358 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
359 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
360 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
361
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400362 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500363 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400364 if (ctm.hasPerspective()) {
365 matrix = &ctm;
366 } else if (fHelper.usesLocalCoords()) {
367 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500368 return;
369 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400370 matrix = &invert;
371 } else {
372 matrix = &SkMatrix::I();
373 }
Brian Salomonccb61422020-01-09 10:46:36 -0500374 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
375 target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor,
Brian Salomona3b02f52020-07-15 16:02:01 -0400376 fAtlas->getViews(), fAtlas->numActivePages(), GrSamplerState::Filter::kLinear,
Brian Salomonccb61422020-01-09 10:46:36 -0500377 flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400378 } else {
379 SkMatrix invert;
380 if (fHelper.usesLocalCoords()) {
381 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400382 return;
383 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500384 }
385
Brian Salomonccb61422020-01-09 10:46:36 -0500386 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
387 target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor,
388 fAtlas->getViews(), fAtlas->numActivePages(), GrSamplerState::Filter::kNearest,
389 kA8_GrMaskFormat, invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500390 }
joshualitt5bf99f12015-03-13 11:47:42 -0700391
joshualitt5bf99f12015-03-13 11:47:42 -0700392 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500393 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400394
395 // We need to make sure we don't overflow a 32 bit int when we request space in the
396 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400397 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400398 return;
399 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400400 GrVertexWriter vertices{ target->makeVertexSpace(
401 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
402 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
403
404 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500405 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700406 SkDebugf("Could not allocate vertices\n");
407 return;
408 }
409
bsalomonb5238a72015-05-05 07:49:49 -0700410 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700411 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500412 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700413
Jim Van Verth33632d82017-02-28 10:24:39 -0500414 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500415 if (fUsesDistanceField) {
416 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400417 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500418 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400419 if (args.fViewMatrix.hasPerspective()) {
420 // approximate the scale since we can't get it from the matrix
421 SkRect xformedBounds;
422 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Brian Osman788b9162020-02-07 10:36:46 -0500423 maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(),
Jim Van Verth51245932017-10-12 11:07:29 -0400424 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400425 } else {
426 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
427 }
Brian Osman116b33e2020-02-05 13:34:09 -0500428 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verth33632d82017-02-28 10:24:39 -0500429 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
430 // In the majority of cases this will yield a crisper rendering.
431 SkScalar mipScale = 1.0f;
432 // Our mipscale is the maxScale clamped to the next highest power of 2
433 if (maxScale <= SK_ScalarHalf) {
434 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
435 mipScale = SkScalarPow(2, -log);
436 } else if (maxScale > SK_Scalar1) {
437 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
438 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700439 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500440 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500441
Jim Van Verth33632d82017-02-28 10:24:39 -0500442 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
443 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
444 // so we can preserve as much detail as possible. However, we can't scale down more
445 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
446 // just bigger than the ideal, and then scale down until we are no more than 4x the
447 // original mipsize.
448 if (mipSize < kIdealMinMIP) {
449 SkScalar newMipSize = mipSize;
450 do {
451 newMipSize *= 2;
452 } while (newMipSize < kIdealMinMIP);
453 while (newMipSize > 4 * mipSize) {
454 newMipSize *= 0.25f;
455 }
456 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700457 }
Brian Osman788b9162020-02-07 10:36:46 -0500458 SkScalar desiredDimension = std::min(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500459
Jim Van Verth33632d82017-02-28 10:24:39 -0500460 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500461 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500462 shapeData = fShapeCache->find(key);
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400463 if (!shapeData || !fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500464 // Remove the stale cache entry
465 if (shapeData) {
466 fShapeCache->remove(shapeData->fKey);
467 fShapeList->remove(shapeData);
468 delete shapeData;
469 }
470 SkScalar scale = desiredDimension / maxDim;
471
472 shapeData = new ShapeData;
473 if (!this->addDFPathToAtlas(target,
474 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500475 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500476 shapeData,
477 args.fShape,
478 SkScalarCeilToInt(desiredDimension),
479 scale)) {
480 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500481 continue;
482 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500483 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500484 } else {
485 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500486 ShapeDataKey key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500487 shapeData = fShapeCache->find(key);
Robert Phillipsbf5bf742020-04-13 09:29:08 -0400488 if (!shapeData || !fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500489 // Remove the stale cache entry
490 if (shapeData) {
491 fShapeCache->remove(shapeData->fKey);
492 fShapeList->remove(shapeData);
493 delete shapeData;
494 }
495
496 shapeData = new ShapeData;
497 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400498 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500499 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400500 shapeData,
501 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400502 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500503 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500504 continue;
505 }
506 }
joshualitt5bf99f12015-03-13 11:47:42 -0700507 }
508
Robert Phillips40a29d72018-01-18 12:59:22 -0500509 auto uploadTarget = target->deferredUploadTarget();
Herb Derby4d721712020-01-24 14:31:16 -0500510 fAtlas->setLastUseToken(
Robert Phillips6d3bc292020-04-06 10:29:28 -0400511 shapeData->fAtlasLocator, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700512
Brian Osmanc906d252018-12-04 11:17:46 -0500513 this->writePathVertices(fAtlas, vertices, GrVertexColor(args.fColor, fWideColor),
514 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700515 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700516 }
517
bsalomon75398562015-08-17 12:55:38 -0700518 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700519 }
520
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500521 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
522 int width, int height, const void* image,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400523 GrDrawOpAtlas::AtlasLocator* atlasLocator) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500524 auto resourceProvider = target->resourceProvider();
525 auto uploadTarget = target->deferredUploadTarget();
526
Robert Phillips6d3bc292020-04-06 10:29:28 -0400527 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, uploadTarget,
528 width, height, image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500529 if (GrDrawOpAtlas::ErrorCode::kError == code) {
530 return false;
531 }
532
533 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
534 this->flush(target, flushInfo);
535
Robert Phillips6d3bc292020-04-06 10:29:28 -0400536 code = atlas->addToAtlas(resourceProvider, uploadTarget, width, height,
537 image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500538 }
539
540 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
541 }
542
Brian Salomone5b399e2017-07-19 13:50:54 -0400543 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Michael Ludwig2686d692020-04-17 20:21:37 +0000544 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrStyledShape& shape,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400545 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500546
bsalomonee432412016-06-27 07:18:18 -0700547 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700548
549 // generate bounding rect for bitmap draw
550 SkRect scaledBounds = bounds;
551 // scale to mip level size
552 scaledBounds.fLeft *= scale;
553 scaledBounds.fTop *= scale;
554 scaledBounds.fRight *= scale;
555 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500556 // subtract out integer portion of origin
557 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500558 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
559 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700560 scaledBounds.offset(-dx, -dy);
561 // get integer boundary
562 SkIRect devPathBounds;
563 scaledBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400564 // place devBounds at origin with padding to allow room for antialiasing
565 int width = devPathBounds.width() + 2 * kAntiAliasPad;
566 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500567 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400568 SkScalar translateX = kAntiAliasPad - dx;
569 SkScalar translateY = kAntiAliasPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700570
571 // draw path to bitmap
572 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500573 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400574 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700575
jvanverth512e4372015-11-23 11:50:02 -0800576 SkASSERT(devPathBounds.fLeft == 0);
577 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500578 SkASSERT(devPathBounds.width() > 0);
579 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700580
joel.liang8cbb4242017-01-09 18:39:43 -0800581 // setup signed distance field storage
582 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
583 width = dfBounds.width();
584 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800585 // TODO We should really generate this directly into the plot somehow
586 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800587
joel.liang8cbb4242017-01-09 18:39:43 -0800588 SkPath path;
589 shape.asPath(&path);
joel.liang8cbb4242017-01-09 18:39:43 -0800590 // Generate signed distance field directly from SkPath
591 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
592 path, drawMatrix,
593 width, height, width * sizeof(unsigned char));
594 if (!succeed) {
joel.liang8cbb4242017-01-09 18:39:43 -0800595 // setup bitmap backing
596 SkAutoPixmapStorage dst;
597 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
598 devPathBounds.height()))) {
599 return false;
600 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400601 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800602
603 // rasterize path
604 SkPaint paint;
605 paint.setStyle(SkPaint::kFill_Style);
606 paint.setAntiAlias(true);
607
608 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800609
610 SkRasterClip rasterClip;
611 rasterClip.setRect(devPathBounds);
612 draw.fRC = &rasterClip;
Brian Osman9aaec362020-05-08 14:54:37 -0400613 SkSimpleMatrixProvider matrixProvider(drawMatrix);
614 draw.fMatrixProvider = &matrixProvider;
joel.liang8cbb4242017-01-09 18:39:43 -0800615 draw.fDst = dst;
616
617 draw.drawPathCoverage(path, paint);
618
619 // Generate signed distance field
620 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
621 (const unsigned char*)dst.addr(),
622 dst.width(), dst.height(), dst.rowBytes());
joel.liang8cbb4242017-01-09 18:39:43 -0800623 }
joshualitt5bf99f12015-03-13 11:47:42 -0700624
625 // add to atlas
Robert Phillips6d3bc292020-04-06 10:29:28 -0400626 if (!this->addToAtlas(target, flushInfo, atlas, width, height, dfStorage.get(),
627 &shapeData->fAtlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500628 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700629 }
630
631 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700632 shapeData->fKey.set(shape, dimension);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500633
Robert Phillips3cf781d2017-08-22 18:25:11 -0400634 shapeData->fBounds = SkRect::Make(devPathBounds);
635 shapeData->fBounds.offset(-translateX, -translateY);
636 shapeData->fBounds.fLeft /= scale;
637 shapeData->fBounds.fTop /= scale;
638 shapeData->fBounds.fRight /= scale;
639 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500640
bsalomonee432412016-06-27 07:18:18 -0700641 fShapeCache->add(shapeData);
642 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700643#ifdef DF_PATH_TRACKING
644 ++g_NumCachedPaths;
645#endif
646 return true;
647 }
648
Brian Salomone5b399e2017-07-19 13:50:54 -0400649 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Michael Ludwig2686d692020-04-17 20:21:37 +0000650 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrStyledShape& shape,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400651 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500652 const SkRect& bounds = shape.bounds();
653 if (bounds.isEmpty()) {
654 return false;
655 }
656 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500657 SkScalar tx = ctm.getTranslateX();
658 SkScalar ty = ctm.getTranslateY();
659 tx -= SkScalarFloorToScalar(tx);
660 ty -= SkScalarFloorToScalar(ty);
661 drawMatrix.set(SkMatrix::kMTransX, tx);
662 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500663 SkRect shapeDevBounds;
664 drawMatrix.mapRect(&shapeDevBounds, bounds);
665 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
666 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
667
668 // get integer boundary
669 SkIRect devPathBounds;
670 shapeDevBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400671 // place devBounds at origin with padding to allow room for antialiasing
672 int width = devPathBounds.width() + 2 * kAntiAliasPad;
673 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verth33632d82017-02-28 10:24:39 -0500674 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400675 SkScalar translateX = kAntiAliasPad - dx;
676 SkScalar translateY = kAntiAliasPad - dy;
Jim Van Verth33632d82017-02-28 10:24:39 -0500677
678 SkASSERT(devPathBounds.fLeft == 0);
679 SkASSERT(devPathBounds.fTop == 0);
680 SkASSERT(devPathBounds.width() > 0);
681 SkASSERT(devPathBounds.height() > 0);
682
683 SkPath path;
684 shape.asPath(&path);
685 // setup bitmap backing
686 SkAutoPixmapStorage dst;
687 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
688 devPathBounds.height()))) {
689 return false;
690 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400691 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500692
693 // rasterize path
694 SkPaint paint;
695 paint.setStyle(SkPaint::kFill_Style);
696 paint.setAntiAlias(true);
697
698 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500699
700 SkRasterClip rasterClip;
701 rasterClip.setRect(devPathBounds);
702 draw.fRC = &rasterClip;
703 drawMatrix.postTranslate(translateX, translateY);
Brian Osman9aaec362020-05-08 14:54:37 -0400704 SkSimpleMatrixProvider matrixProvider(drawMatrix);
705 draw.fMatrixProvider = &matrixProvider;
Jim Van Verth33632d82017-02-28 10:24:39 -0500706 draw.fDst = dst;
707
708 draw.drawPathCoverage(path, paint);
709
710 // add to atlas
Robert Phillips6d3bc292020-04-06 10:29:28 -0400711 if (!this->addToAtlas(target, flushInfo, atlas, dst.width(), dst.height(), dst.addr(),
712 &shapeData->fAtlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500713 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500714 }
715
716 // add to cache
717 shapeData->fKey.set(shape, ctm);
Jim Van Verth33632d82017-02-28 10:24:39 -0500718
Jim Van Verth33632d82017-02-28 10:24:39 -0500719 shapeData->fBounds = SkRect::Make(devPathBounds);
720 shapeData->fBounds.offset(-translateX, -translateY);
721
Jim Van Verth33632d82017-02-28 10:24:39 -0500722 fShapeCache->add(shapeData);
723 fShapeList->addToTail(shapeData);
724#ifdef DF_PATH_TRACKING
725 ++g_NumCachedPaths;
726#endif
727 return true;
728 }
729
Brian Salomon29b60c92017-10-31 14:42:10 -0400730 void writePathVertices(GrDrawOpAtlas* atlas,
Brian Osman0dd43022018-11-16 15:53:26 -0500731 GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500732 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400733 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700734 const ShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500735 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400736 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500737 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
738 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400739 }
Jim Van Verth77047542017-01-11 14:17:00 -0500740
Brian Osman0dd43022018-11-16 15:53:26 -0500741 // set up texture coordinates
Robert Phillips6d3bc292020-04-06 10:29:28 -0400742 auto texCoords = GrVertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs(
743 fUsesDistanceField ? SK_DistanceFieldPad : 0));
Brian Osman0dd43022018-11-16 15:53:26 -0500744
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400745 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500746 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500747 color,
748 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400749 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500750 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
751 color,
752 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400753 }
joshualitt5bf99f12015-03-13 11:47:42 -0700754 }
755
Brian Salomone5b399e2017-07-19 13:50:54 -0400756 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500757 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000758 int numAtlasTextures = SkToInt(fAtlas->numActivePages());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500759 const auto views = fAtlas->getViews();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000760 if (gp->numTextureSamplers() != numAtlasTextures) {
761 for (int i = gp->numTextureSamplers(); i < numAtlasTextures; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600762 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400763 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
764 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500765 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000766 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400767 // During preparation the number of atlas pages has increased.
768 // Update the proxies used in the GP to match.
769 if (fUsesDistanceField) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500770 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500771 fAtlas->getViews(), fAtlas->numActivePages(),
Brian Salomona3b02f52020-07-15 16:02:01 -0400772 GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400773 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500774 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500775 fAtlas->getViews(), fAtlas->numActivePages(),
776 GrSamplerState::Filter::kNearest);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400777 }
778 }
779
bsalomon6d6b6ad2016-07-13 14:45:28 -0700780 if (flushInfo->fInstancesToFlush) {
Chris Daltoneb694b72020-03-16 09:25:50 -0600781 GrSimpleMesh* mesh = target->allocMesh();
Robert Phillipsee08d522019-10-28 16:34:44 -0400782 mesh->setIndexedPatterned(flushInfo->fIndexBuffer,
783 GrResourceProvider::NumIndicesPerNonAAQuad(),
Robert Phillipsee08d522019-10-28 16:34:44 -0400784 flushInfo->fInstancesToFlush,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600785 GrResourceProvider::MaxNumNonAAQuads(),
786 flushInfo->fVertexBuffer,
787 GrResourceProvider::NumVertsPerNonAAQuad(),
788 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600789 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
790 GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400791 flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() *
792 flushInfo->fInstancesToFlush;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700793 flushInfo->fInstancesToFlush = 0;
794 }
joshualitt5bf99f12015-03-13 11:47:42 -0700795 }
796
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700797 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips6c59fe42020-02-27 09:30:37 -0500798 auto pipeline = fHelper.createPipelineWithStencil(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -0500799
800 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700801 }
802
Brian Osmancf860852018-10-31 14:04:39 -0400803 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500804 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700805
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500806 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
807 const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400808 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400809 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000810 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700811 }
812
Jim Van Verth33632d82017-02-28 10:24:39 -0500813 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000814 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500815 }
816
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400817 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
818 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
819
820 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000821 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700822 }
823
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400824 // We can position on the cpu unless we're in perspective,
825 // but also need to make sure local matrices are identical
826 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
Mike Reed2c383152019-12-18 16:47:47 -0500827 !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000828 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500829 }
830
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400831 // Depending on the ctm we may have a different shader for SDF paths
832 if (this->usesDistanceField()) {
833 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
834 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000835 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400836 }
837 }
838
Brian Salomond0a0a652016-12-15 15:25:22 -0500839 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500840 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000841 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700842 }
843
Jim Van Verth33632d82017-02-28 10:24:39 -0500844 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700845
Brian Salomond0a0a652016-12-15 15:25:22 -0500846 struct Entry {
Michael Ludwig2686d692020-04-17 20:21:37 +0000847 SkPMColor4f fColor;
848 GrStyledShape fShape;
849 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700850 };
851
Brian Salomond0a0a652016-12-15 15:25:22 -0500852 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400853 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500854 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700855 ShapeCache* fShapeCache;
856 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700857 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500858 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700859
Brian Salomonfebbd232017-07-11 15:52:02 -0400860 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700861};
862
Jim Van Verth83010462017-03-16 08:45:39 -0400863bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400864 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400865 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700866
jvanverthfa38a302014-10-06 05:59:05 -0700867 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700868 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700869 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700870 if (!fAtlas) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400871 const GrBackendFormat format = args.fContext->priv().caps()->getDefaultBackendFormat(
872 GrColorType::kAlpha_8, GrRenderable::kNo);
Sergey Ulanovf1b2b422020-01-24 15:39:32 -0800873
874 GrDrawOpAtlasConfig atlasConfig(args.fContext->priv().caps()->maxTextureSize(),
875 kMaxAtlasTextureBytes);
876 SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat);
877 fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(), format,
Herb Derby0ef780b2020-01-24 15:57:11 -0500878 GrColorType::kAlpha_8, size.width(), size.height(),
879 kPlotWidth, kPlotHeight, this,
880 GrDrawOpAtlas::AllowMultitexturing::kYes, this);
joshualitt21279c72015-05-11 07:21:37 -0700881 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700882 return false;
883 }
884 }
885
Brian Salomonfebbd232017-07-11 15:52:02 -0400886 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400887 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(),
888 &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400889 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800890
jvanverthfa38a302014-10-06 05:59:05 -0700891 return true;
892}
893
joshualitt21279c72015-05-11 07:21:37 -0700894///////////////////////////////////////////////////////////////////////////////////////////////////
895
Hal Canary6f6961e2017-01-31 13:50:44 -0500896#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700897
Herb Derby0ef780b2020-01-24 15:57:11 -0500898struct GrSmallPathRenderer::PathTestStruct : public GrDrawOpAtlas::EvictionCallback,
899 public GrDrawOpAtlas::GenerationCounter {
halcanary96fcdcc2015-08-27 07:41:13 -0700900 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
Herb Derby1a496c52020-01-22 17:26:56 -0500901 ~PathTestStruct() override { this->reset(); }
joshualitt21279c72015-05-11 07:21:37 -0700902
903 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700904 ShapeDataList::Iter iter;
905 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
906 ShapeData* shapeData;
907 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700908 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700909 fShapeList.remove(shapeData);
910 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700911 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500912 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700913 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700914 }
915
Herb Derby4d721712020-01-24 14:31:16 -0500916 void evict(GrDrawOpAtlas::PlotLocator plotLocator) override {
joshualitt21279c72015-05-11 07:21:37 -0700917 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700918 ShapeDataList::Iter iter;
Herb Derby1a496c52020-01-22 17:26:56 -0500919 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
bsalomonee432412016-06-27 07:18:18 -0700920 ShapeData* shapeData;
921 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700922 iter.next();
Robert Phillips6d3bc292020-04-06 10:29:28 -0400923 if (plotLocator == shapeData->fAtlasLocator.plotLocator()) {
Herb Derby1a496c52020-01-22 17:26:56 -0500924 fShapeCache.remove(shapeData->fKey);
925 fShapeList.remove(shapeData);
bsalomonee432412016-06-27 07:18:18 -0700926 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700927 }
928 }
929 }
930
931 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500932 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700933 ShapeCache fShapeCache;
934 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700935};
936
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500937std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500938 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500939 GrPaint&& paint,
Michael Ludwig2686d692020-04-17 20:21:37 +0000940 const GrStyledShape& shape,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500941 const SkMatrix& viewMatrix,
942 GrDrawOpAtlas* atlas,
943 ShapeCache* shapeCache,
944 ShapeDataList* shapeList,
945 bool gammaCorrect,
946 const GrUserStencilSettings* stencil) {
947
Robert Phillips7c525e62018-06-12 10:11:12 -0400948 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
949 atlas, shapeCache, shapeList, gammaCorrect,
950 stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500951
952}
953
Brian Salomonfebbd232017-07-11 15:52:02 -0400954GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
955 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700956 static PathTestStruct gTestStruct;
957
Robert Phillips9da87e02019-02-04 13:26:26 -0500958 if (context->priv().contextID() != gTestStruct.fContextID) {
959 gTestStruct.fContextID = context->priv().contextID();
joshualitt21279c72015-05-11 07:21:37 -0700960 gTestStruct.reset();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400961 const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(
962 GrColorType::kAlpha_8, GrRenderable::kNo);
Sergey Ulanovf1b2b422020-01-24 15:39:32 -0800963 GrDrawOpAtlasConfig atlasConfig(context->priv().caps()->maxTextureSize(),
964 kMaxAtlasTextureBytes);
965 SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat);
966 gTestStruct.fAtlas =
967 GrDrawOpAtlas::Make(context->priv().proxyProvider(), format, GrColorType::kAlpha_8,
968 size.width(), size.height(), kPlotWidth, kPlotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -0500969 &gTestStruct,
Sergey Ulanovf1b2b422020-01-24 15:39:32 -0800970 GrDrawOpAtlas::AllowMultitexturing::kYes, &gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700971 }
972
973 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700974 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700975
bsalomonee432412016-06-27 07:18:18 -0700976 // This path renderer only allows fill styles.
Michael Ludwig2686d692020-04-17 20:21:37 +0000977 GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500978 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400979 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500980 std::move(paint), shape, viewMatrix,
981 gTestStruct.fAtlas.get(),
982 &gTestStruct.fShapeCache,
983 &gTestStruct.fShapeList,
984 gammaCorrect,
985 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700986}
987
988#endif