blob: db62c170fe056758e2422d271f4b2d8ed999177a [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"
16#include "src/core/SkPointPriv.h"
17#include "src/core/SkRasterClip.h"
18#include "src/gpu/GrBuffer.h"
19#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrDistanceFieldGenFromVector.h"
21#include "src/gpu/GrDrawOpTest.h"
22#include "src/gpu/GrQuad.h"
23#include "src/gpu/GrRenderTargetContext.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/GrVertexWriter.h"
26#include "src/gpu/effects/GrBitmapTextGeoProc.h"
27#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
28#include "src/gpu/ops/GrMeshDrawOp.h"
29#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
jvanverthfa38a302014-10-06 05:59:05 -070030
jvanverthfb1e2fc2015-09-15 13:11:11 -070031#define ATLAS_TEXTURE_WIDTH 2048
jvanverthb61283f2014-10-30 05:57:21 -070032#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfb1e2fc2015-09-15 13:11:11 -070033#define PLOT_WIDTH 512
reede4ef1ca2015-02-17 18:38:38 -080034#define PLOT_HEIGHT 256
jvanverthfa38a302014-10-06 05:59:05 -070035
36#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
37#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
38
jvanverthb3eb6872014-10-24 07:12:51 -070039#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070040static int g_NumCachedShapes = 0;
41static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070042#endif
43
jvanverthb61283f2014-10-30 05:57:21 -070044// mip levels
Jim Van Verth25b8ca12017-02-17 14:02:13 -050045static const SkScalar kIdealMinMIP = 12;
Jim Van Verth825d4d72018-01-30 20:37:03 +000046static const SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050047
48static const SkScalar kMaxDim = 73;
Jim Van Verth25b8ca12017-02-17 14:02:13 -050049static const SkScalar kMinSize = SK_ScalarHalf;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050050static const SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070051
Robert Phillipsd2e9f762018-03-07 11:54:37 -050052class ShapeDataKey {
53public:
54 ShapeDataKey() {}
55 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
56 ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
57 ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
58
59 ShapeDataKey& operator=(const ShapeDataKey& that) {
60 fKey.reset(that.fKey.count());
61 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
62 return *this;
63 }
64
65 // for SDF paths
66 void set(const GrShape& shape, uint32_t dim) {
67 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
68 // relevant styling information.
69 SkASSERT(shape.style().isSimpleFill());
70 SkASSERT(shape.hasUnstyledKey());
71 int shapeKeySize = shape.unstyledKeySize();
72 fKey.reset(1 + shapeKeySize);
73 fKey[0] = dim;
74 shape.writeUnstyledKey(&fKey[1]);
75 }
76
77 // for bitmap paths
78 void set(const GrShape& shape, const SkMatrix& ctm) {
79 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
80 // relevant styling information.
81 SkASSERT(shape.style().isSimpleFill());
82 SkASSERT(shape.hasUnstyledKey());
83 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
84 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
85 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
86 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
87 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
88 SkScalar tx = ctm.get(SkMatrix::kMTransX);
89 SkScalar ty = ctm.get(SkMatrix::kMTransY);
90 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050091 tx -= SkScalarFloorToScalar(tx);
92 ty -= SkScalarFloorToScalar(ty);
93 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
94 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050095 int shapeKeySize = shape.unstyledKeySize();
96 fKey.reset(5 + shapeKeySize);
97 fKey[0] = SkFloat2Bits(sx);
98 fKey[1] = SkFloat2Bits(sy);
99 fKey[2] = SkFloat2Bits(kx);
100 fKey[3] = SkFloat2Bits(ky);
101 fKey[4] = fracX | (fracY >> 8);
102 shape.writeUnstyledKey(&fKey[5]);
103 }
104
105 bool operator==(const ShapeDataKey& that) const {
106 return fKey.count() == that.fKey.count() &&
107 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
108 }
109
110 int count32() const { return fKey.count(); }
111 const uint32_t* data() const { return fKey.get(); }
112
113private:
114 // The key is composed of the GrShape's key, and either the dimensions of the DF
115 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
116 // the matrix for the path with only fractional translation.
117 SkAutoSTArray<24, uint32_t> fKey;
118};
119
120class ShapeData {
121public:
122 ShapeDataKey fKey;
123 GrDrawOpAtlas::AtlasID fID;
124 SkRect fBounds;
125 GrIRect16 fTextureCoords;
126 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
127
128 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
129 return data.fKey;
130 }
131
Kevin Lubickb5502b22018-03-12 10:17:06 -0400132 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500133 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
134 }
135};
136
137
138
joshualitt5bf99f12015-03-13 11:47:42 -0700139// Callback to clear out internal path cache when eviction occurs
Jim Van Verth83010462017-03-16 08:45:39 -0400140void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
141 GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
joshualitt5bf99f12015-03-13 11:47:42 -0700142 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700143 ShapeDataList::Iter iter;
144 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
145 ShapeData* shapeData;
146 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700147 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700148 if (id == shapeData->fID) {
149 dfpr->fShapeCache.remove(shapeData->fKey);
150 dfpr->fShapeList.remove(shapeData);
151 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700152#ifdef DF_PATH_TRACKING
153 ++g_NumFreedPaths;
154#endif
155 }
156 }
157}
158
jvanverthfa38a302014-10-06 05:59:05 -0700159////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400160GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700161
Jim Van Verth83010462017-03-16 08:45:39 -0400162GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700163 ShapeDataList::Iter iter;
164 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
165 ShapeData* shapeData;
166 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700167 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700168 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700169 }
jvanverthb3eb6872014-10-24 07:12:51 -0700170
171#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700172 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700173#endif
jvanverthfa38a302014-10-06 05:59:05 -0700174}
175
176////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600177GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700178 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600179 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700180 }
181 // If the shape has no key then we won't get any reuse.
182 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600183 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700184 }
185 // This only supports filled paths, however, the caller may apply the style to make a filled
186 // path and try again.
187 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600188 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700189 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500190 // This does non-inverse coverage-based antialiased fills.
Chris Dalton09e56892019-03-13 00:22:01 -0600191 if (!(AATypeFlags::kCoverage & args.fAATypeFlags)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600192 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700193 }
jvanverthfa38a302014-10-06 05:59:05 -0700194 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700195 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600196 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700197 }
halcanary9d524f22016-03-29 09:03:52 -0700198
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500199 // Only support paths with bounds within kMaxDim by kMaxDim,
200 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500201 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400202 SkScalar scaleFactors[2] = { 1, 1 };
203 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600204 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500205 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700206 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500207 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700208 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500209 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
210 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600211 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
212 return CanDrawPath::kNo;
213 }
bsalomon6266dca2016-03-11 06:22:00 -0800214
Chris Dalton5ed44232017-09-07 13:22:46 -0600215 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700216}
217
jvanverthfa38a302014-10-06 05:59:05 -0700218////////////////////////////////////////////////////////////////////////////////
219
joshualitt5bf99f12015-03-13 11:47:42 -0700220// padding around path bounds to allow for antialiased pixels
221static const SkScalar kAntiAliasPad = 1.0f;
222
Brian Salomonfebbd232017-07-11 15:52:02 -0400223class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
224private:
225 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
226
joshualitt5bf99f12015-03-13 11:47:42 -0700227public:
Brian Salomon25a88092016-12-01 09:36:50 -0500228 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700229
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500230 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400231 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700232
Robert Phillipsb97da532019-02-12 15:24:12 -0500233 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400234 GrPaint&& paint,
235 const GrShape& shape,
236 const SkMatrix& viewMatrix,
237 GrDrawOpAtlas* atlas,
238 ShapeCache* shapeCache,
239 ShapeDataList* shapeList,
Brian Salomonfebbd232017-07-11 15:52:02 -0400240 bool gammaCorrect,
241 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400242 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
243 atlas, shapeCache, shapeList, gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400244 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700245 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500246
Brian Osmancf860852018-10-31 14:04:39 -0400247 SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrShape& shape,
Brian Salomonfebbd232017-07-11 15:52:02 -0400248 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
249 ShapeDataList* shapeList, bool gammaCorrect,
250 const GrUserStencilSettings* stencilSettings)
251 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500252 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500253 // Compute bounds
254 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
255
Jim Van Verth83010462017-03-16 08:45:39 -0400256#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500257 fUsesDistanceField = true;
258#else
Jim Van Verth83010462017-03-16 08:45:39 -0400259 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500260 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
261#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400262 // always use distance fields if in perspective
263 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500264
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400265 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500266
267 fAtlas = atlas;
268 fShapeCache = shapeCache;
269 fShapeList = shapeList;
270 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500271 }
272
Brian Salomonfebbd232017-07-11 15:52:02 -0400273 const char* name() const override { return "SmallPathOp"; }
274
Chris Dalton1706cbf2019-05-21 19:35:29 -0600275 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400276 fHelper.visitProxies(func);
277
278 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
Robert Phillips4bc70112018-03-01 10:24:02 -0500279 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Brian Salomon9f545bc2017-11-06 10:36:57 -0500280 SkASSERT(proxies[i]);
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600281 func(proxies[i].get(), GrMipMapped::kNo);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400282 }
283 }
284
Brian Osman9a390ac2018-11-12 09:47:48 -0500285#ifdef SK_DEBUG
Brian Salomonfebbd232017-07-11 15:52:02 -0400286 SkString dumpInfo() const override {
287 SkString string;
288 for (const auto& geo : fShapes) {
Brian Osmancf860852018-10-31 14:04:39 -0400289 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
Brian Salomonfebbd232017-07-11 15:52:02 -0400290 }
291 string += fHelper.dumpInfo();
292 string += INHERITED::dumpInfo();
293 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500294 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500295#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500296
Brian Salomonfebbd232017-07-11 15:52:02 -0400297 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
298
Brian Osman5ced0bf2019-03-15 10:15:29 -0400299 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
300 GrFSAAType fsaaType, GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700301 return fHelper.finalizeProcessors(
Brian Osman5ced0bf2019-03-15 10:15:29 -0400302 caps, clip, fsaaType, clampType, GrProcessorAnalysisCoverage::kSingleChannel,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400303 &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;
bungeman06ca8ec2016-06-09 08:01:03 -0700310 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000311 GrPipeline::FixedDynamicState* fFixedDynamicState;
bsalomonb5238a72015-05-05 07:49:49 -0700312 int fVertexOffset;
313 int fInstancesToFlush;
314 };
315
Brian Salomon91326c32017-08-09 16:02:19 -0400316 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500317 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700318
Brian Salomon7eae3e02018-08-07 14:02:38 +0000319 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
320 GR_STATIC_ASSERT(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
321
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700322 FlushInfo flushInfo;
323 flushInfo.fFixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000324 int numActiveProxies = fAtlas->numActivePages();
325 const auto proxies = fAtlas->getProxies();
326 for (int i = 0; i < numActiveProxies; ++i) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700327 flushInfo.fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000328 }
Brian Salomon49348902018-06-26 09:12:38 -0400329
joshualitt5bf99f12015-03-13 11:47:42 -0700330 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400331 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500332 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500333 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400334 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500335 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
336 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
337 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
338
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400339 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500340 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400341 if (ctm.hasPerspective()) {
342 matrix = &ctm;
343 } else if (fHelper.usesLocalCoords()) {
344 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500345 return;
346 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400347 matrix = &invert;
348 } else {
349 matrix = &SkMatrix::I();
350 }
351 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
Brian Osmanc906d252018-12-04 11:17:46 -0500352 *target->caps().shaderCaps(), *matrix, fWideColor, fAtlas->getProxies(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400353 fAtlas->numActivePages(), GrSamplerState::ClampBilerp(), flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400354 } else {
355 SkMatrix invert;
356 if (fHelper.usesLocalCoords()) {
357 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400358 return;
359 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500360 }
361
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400362 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Brian Osmanc906d252018-12-04 11:17:46 -0500363 *target->caps().shaderCaps(), this->color(), fWideColor, fAtlas->getProxies(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400364 fAtlas->numActivePages(), GrSamplerState::ClampNearest(), kA8_GrMaskFormat,
365 invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500366 }
joshualitt5bf99f12015-03-13 11:47:42 -0700367
joshualitt5bf99f12015-03-13 11:47:42 -0700368 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500369 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400370
371 // We need to make sure we don't overflow a 32 bit int when we request space in the
372 // makeVertexSpace call below.
373 if (instanceCount > SK_MaxS32 / kVerticesPerQuad) {
374 return;
375 }
Brian Osman0dd43022018-11-16 15:53:26 -0500376 GrVertexWriter vertices{target->makeVertexSpace(kVertexStride,
377 kVerticesPerQuad * instanceCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500378 &flushInfo.fVertexBuffer,
Brian Osman0dd43022018-11-16 15:53:26 -0500379 &flushInfo.fVertexOffset)};
Brian Salomond28a79d2017-10-16 13:01:07 -0400380 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500381 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700382 SkDebugf("Could not allocate vertices\n");
383 return;
384 }
385
bsalomonb5238a72015-05-05 07:49:49 -0700386 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700387 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500388 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700389
Jim Van Verth33632d82017-02-28 10:24:39 -0500390 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500391 if (fUsesDistanceField) {
392 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400393 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500394 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400395 if (args.fViewMatrix.hasPerspective()) {
396 // approximate the scale since we can't get it from the matrix
397 SkRect xformedBounds;
398 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400399 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
400 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400401 } else {
402 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
403 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500404 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
405 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
406 // In the majority of cases this will yield a crisper rendering.
407 SkScalar mipScale = 1.0f;
408 // Our mipscale is the maxScale clamped to the next highest power of 2
409 if (maxScale <= SK_ScalarHalf) {
410 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
411 mipScale = SkScalarPow(2, -log);
412 } else if (maxScale > SK_Scalar1) {
413 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
414 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700415 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500416 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500417
Jim Van Verth33632d82017-02-28 10:24:39 -0500418 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
419 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
420 // so we can preserve as much detail as possible. However, we can't scale down more
421 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
422 // just bigger than the ideal, and then scale down until we are no more than 4x the
423 // original mipsize.
424 if (mipSize < kIdealMinMIP) {
425 SkScalar newMipSize = mipSize;
426 do {
427 newMipSize *= 2;
428 } while (newMipSize < kIdealMinMIP);
429 while (newMipSize > 4 * mipSize) {
430 newMipSize *= 0.25f;
431 }
432 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700433 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500434 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500435
Jim Van Verth33632d82017-02-28 10:24:39 -0500436 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500437 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500438 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500439 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500440 // Remove the stale cache entry
441 if (shapeData) {
442 fShapeCache->remove(shapeData->fKey);
443 fShapeList->remove(shapeData);
444 delete shapeData;
445 }
446 SkScalar scale = desiredDimension / maxDim;
447
448 shapeData = new ShapeData;
449 if (!this->addDFPathToAtlas(target,
450 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500451 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500452 shapeData,
453 args.fShape,
454 SkScalarCeilToInt(desiredDimension),
455 scale)) {
456 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500457 continue;
458 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500459 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500460 } else {
461 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500462 ShapeDataKey key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500463 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500464 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500465 // Remove the stale cache entry
466 if (shapeData) {
467 fShapeCache->remove(shapeData->fKey);
468 fShapeList->remove(shapeData);
469 delete shapeData;
470 }
471
472 shapeData = new ShapeData;
473 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400474 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500475 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400476 shapeData,
477 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400478 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500479 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500480 continue;
481 }
482 }
joshualitt5bf99f12015-03-13 11:47:42 -0700483 }
484
Robert Phillips40a29d72018-01-18 12:59:22 -0500485 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips4bc70112018-03-01 10:24:02 -0500486 fAtlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700487
Brian Osmanc906d252018-12-04 11:17:46 -0500488 this->writePathVertices(fAtlas, vertices, GrVertexColor(args.fColor, fWideColor),
489 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700490 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700491 }
492
bsalomon75398562015-08-17 12:55:38 -0700493 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700494 }
495
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500496 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
497 int width, int height, const void* image,
498 GrDrawOpAtlas::AtlasID* id, SkIPoint16* atlasLocation) const {
499 auto resourceProvider = target->resourceProvider();
500 auto uploadTarget = target->deferredUploadTarget();
501
502 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, id,
503 uploadTarget, width, height,
504 image, atlasLocation);
505 if (GrDrawOpAtlas::ErrorCode::kError == code) {
506 return false;
507 }
508
509 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
510 this->flush(target, flushInfo);
511
512 code = atlas->addToAtlas(resourceProvider, id, uploadTarget, width, height,
513 image, atlasLocation);
514 }
515
516 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
517 }
518
Brian Salomone5b399e2017-07-19 13:50:54 -0400519 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400520 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
521 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500522
bsalomonee432412016-06-27 07:18:18 -0700523 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700524
525 // generate bounding rect for bitmap draw
526 SkRect scaledBounds = bounds;
527 // scale to mip level size
528 scaledBounds.fLeft *= scale;
529 scaledBounds.fTop *= scale;
530 scaledBounds.fRight *= scale;
531 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500532 // subtract out integer portion of origin
533 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500534 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
535 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700536 scaledBounds.offset(-dx, -dy);
537 // get integer boundary
538 SkIRect devPathBounds;
539 scaledBounds.roundOut(&devPathBounds);
540 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800541 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500542 // place devBounds at origin
543 int width = devPathBounds.width() + 2*intPad;
544 int height = devPathBounds.height() + 2*intPad;
545 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400546 SkScalar translateX = intPad - dx;
547 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700548
549 // draw path to bitmap
550 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500551 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400552 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700553
jvanverth512e4372015-11-23 11:50:02 -0800554 SkASSERT(devPathBounds.fLeft == 0);
555 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500556 SkASSERT(devPathBounds.width() > 0);
557 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700558
joel.liang8cbb4242017-01-09 18:39:43 -0800559 // setup signed distance field storage
560 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
561 width = dfBounds.width();
562 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800563 // TODO We should really generate this directly into the plot somehow
564 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800565
joel.liang8cbb4242017-01-09 18:39:43 -0800566 SkPath path;
567 shape.asPath(&path);
568#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
569 // Generate signed distance field directly from SkPath
570 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
571 path, drawMatrix,
572 width, height, width * sizeof(unsigned char));
573 if (!succeed) {
574#endif
575 // setup bitmap backing
576 SkAutoPixmapStorage dst;
577 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
578 devPathBounds.height()))) {
579 return false;
580 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400581 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800582
583 // rasterize path
584 SkPaint paint;
585 paint.setStyle(SkPaint::kFill_Style);
586 paint.setAntiAlias(true);
587
588 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800589
590 SkRasterClip rasterClip;
591 rasterClip.setRect(devPathBounds);
592 draw.fRC = &rasterClip;
593 draw.fMatrix = &drawMatrix;
594 draw.fDst = dst;
595
596 draw.drawPathCoverage(path, paint);
597
598 // Generate signed distance field
599 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
600 (const unsigned char*)dst.addr(),
601 dst.width(), dst.height(), dst.rowBytes());
602#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
603 }
604#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700605
606 // add to atlas
607 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500608 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500609
610 if (!this->addToAtlas(target, flushInfo, atlas,
611 width, height, dfStorage.get(), &id, &atlasLocation)) {
612 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700613 }
614
615 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700616 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700617 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500618
Robert Phillips3cf781d2017-08-22 18:25:11 -0400619 shapeData->fBounds = SkRect::Make(devPathBounds);
620 shapeData->fBounds.offset(-translateX, -translateY);
621 shapeData->fBounds.fLeft /= scale;
622 shapeData->fBounds.fTop /= scale;
623 shapeData->fBounds.fRight /= scale;
624 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500625
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400626 // We pack the 2bit page index in the low bit of the u and v texture coords
627 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
628 SkASSERT(pageIndex < 4);
629 uint16_t uBit = (pageIndex >> 1) & 0x1;
630 uint16_t vBit = pageIndex & 0x1;
631 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
632 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400633 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400634 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400635 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400636 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700637
bsalomonee432412016-06-27 07:18:18 -0700638 fShapeCache->add(shapeData);
639 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700640#ifdef DF_PATH_TRACKING
641 ++g_NumCachedPaths;
642#endif
643 return true;
644 }
645
Brian Salomone5b399e2017-07-19 13:50:54 -0400646 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400647 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
648 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500649 const SkRect& bounds = shape.bounds();
650 if (bounds.isEmpty()) {
651 return false;
652 }
653 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500654 SkScalar tx = ctm.getTranslateX();
655 SkScalar ty = ctm.getTranslateY();
656 tx -= SkScalarFloorToScalar(tx);
657 ty -= SkScalarFloorToScalar(ty);
658 drawMatrix.set(SkMatrix::kMTransX, tx);
659 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500660 SkRect shapeDevBounds;
661 drawMatrix.mapRect(&shapeDevBounds, bounds);
662 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
663 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
664
665 // get integer boundary
666 SkIRect devPathBounds;
667 shapeDevBounds.roundOut(&devPathBounds);
668 // pad to allow room for antialiasing
669 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
670 // place devBounds at origin
671 int width = devPathBounds.width() + 2 * intPad;
672 int height = devPathBounds.height() + 2 * intPad;
673 devPathBounds = SkIRect::MakeWH(width, height);
674 SkScalar translateX = intPad - dx;
675 SkScalar translateY = intPad - dy;
676
677 SkASSERT(devPathBounds.fLeft == 0);
678 SkASSERT(devPathBounds.fTop == 0);
679 SkASSERT(devPathBounds.width() > 0);
680 SkASSERT(devPathBounds.height() > 0);
681
682 SkPath path;
683 shape.asPath(&path);
684 // setup bitmap backing
685 SkAutoPixmapStorage dst;
686 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
687 devPathBounds.height()))) {
688 return false;
689 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400690 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500691
692 // rasterize path
693 SkPaint paint;
694 paint.setStyle(SkPaint::kFill_Style);
695 paint.setAntiAlias(true);
696
697 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500698
699 SkRasterClip rasterClip;
700 rasterClip.setRect(devPathBounds);
701 draw.fRC = &rasterClip;
702 drawMatrix.postTranslate(translateX, translateY);
703 draw.fMatrix = &drawMatrix;
704 draw.fDst = dst;
705
706 draw.drawPathCoverage(path, paint);
707
708 // add to atlas
709 SkIPoint16 atlasLocation;
710 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500711
712 if (!this->addToAtlas(target, flushInfo, atlas,
713 dst.width(), dst.height(), dst.addr(), &id, &atlasLocation)) {
714 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500715 }
716
717 // add to cache
718 shapeData->fKey.set(shape, ctm);
719 shapeData->fID = id;
720
Jim Van Verth33632d82017-02-28 10:24:39 -0500721 shapeData->fBounds = SkRect::Make(devPathBounds);
722 shapeData->fBounds.offset(-translateX, -translateY);
723
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400724 // We pack the 2bit page index in the low bit of the u and v texture coords
725 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
726 SkASSERT(pageIndex < 4);
727 uint16_t uBit = (pageIndex >> 1) & 0x1;
728 uint16_t vBit = pageIndex & 0x1;
729 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
730 (atlasLocation.fX+width) << 1 | uBit,
731 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500732
733 fShapeCache->add(shapeData);
734 fShapeList->addToTail(shapeData);
735#ifdef DF_PATH_TRACKING
736 ++g_NumCachedPaths;
737#endif
738 return true;
739 }
740
Brian Salomon29b60c92017-10-31 14:42:10 -0400741 void writePathVertices(GrDrawOpAtlas* atlas,
Brian Osman0dd43022018-11-16 15:53:26 -0500742 GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500743 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400744 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700745 const ShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500746 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400747 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500748 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
749 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400750 }
Jim Van Verth77047542017-01-11 14:17:00 -0500751
Brian Osman0dd43022018-11-16 15:53:26 -0500752 // set up texture coordinates
753 GrVertexWriter::TriStrip<uint16_t> texCoords{
754 (uint16_t)shapeData->fTextureCoords.fLeft,
755 (uint16_t)shapeData->fTextureCoords.fTop,
756 (uint16_t)shapeData->fTextureCoords.fRight,
757 (uint16_t)shapeData->fTextureCoords.fBottom
758 };
759
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400760 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500761 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500762 color,
763 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400764 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500765 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
766 color,
767 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400768 }
joshualitt5bf99f12015-03-13 11:47:42 -0700769 }
770
Brian Salomone5b399e2017-07-19 13:50:54 -0400771 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400772 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000773 int numAtlasTextures = SkToInt(fAtlas->numActivePages());
774 auto proxies = fAtlas->getProxies();
775 if (gp->numTextureSamplers() != numAtlasTextures) {
776 for (int i = gp->numTextureSamplers(); i < numAtlasTextures; ++i) {
777 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
778 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400779 // During preparation the number of atlas pages has increased.
780 // Update the proxies used in the GP to match.
781 if (fUsesDistanceField) {
782 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500783 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400784 } else {
785 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500786 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400787 }
788 }
789
bsalomon6d6b6ad2016-07-13 14:45:28 -0700790 if (flushInfo->fInstancesToFlush) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000791 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700792 int maxInstancesPerDraw =
Brian Salomondbf70722019-02-07 11:31:24 -0500793 static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Brian Salomon12d22642019-01-29 14:38:50 -0500794 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerQuad, kVerticesPerQuad,
795 flushInfo->fInstancesToFlush, maxInstancesPerDraw);
796 mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700797 target->recordDraw(
798 flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState, nullptr);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700799 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
800 flushInfo->fInstancesToFlush = 0;
801 }
joshualitt5bf99f12015-03-13 11:47:42 -0700802 }
803
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700804 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
805 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
806 }
807
Brian Osmancf860852018-10-31 14:04:39 -0400808 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500809 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700810
Brian Salomon7eae3e02018-08-07 14:02:38 +0000811 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400812 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400813 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000814 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700815 }
816
Jim Van Verth33632d82017-02-28 10:24:39 -0500817 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000818 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500819 }
820
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400821 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
822 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
823
824 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000825 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700826 }
827
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400828 // We can position on the cpu unless we're in perspective,
829 // but also need to make sure local matrices are identical
830 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
831 !thisCtm.cheapEqualTo(thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000832 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500833 }
834
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400835 // Depending on the ctm we may have a different shader for SDF paths
836 if (this->usesDistanceField()) {
837 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
838 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000839 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400840 }
841 }
842
Brian Salomond0a0a652016-12-15 15:25:22 -0500843 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500844 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000845 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700846 }
847
Jim Van Verth33632d82017-02-28 10:24:39 -0500848 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700849
Brian Salomond0a0a652016-12-15 15:25:22 -0500850 struct Entry {
Brian Osmancf860852018-10-31 14:04:39 -0400851 SkPMColor4f fColor;
852 GrShape fShape;
853 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700854 };
855
Brian Salomond0a0a652016-12-15 15:25:22 -0500856 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400857 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500858 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700859 ShapeCache* fShapeCache;
860 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700861 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500862 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700863
Brian Salomonfebbd232017-07-11 15:52:02 -0400864 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700865};
866
Jim Van Verth83010462017-03-16 08:45:39 -0400867bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400868 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400869 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700870
jvanverthfa38a302014-10-06 05:59:05 -0700871 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700872 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700873 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700874 if (!fAtlas) {
Greg Daniel4065d452018-11-16 15:43:41 -0500875 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500876 args.fContext->priv().caps()->getBackendFormatFromColorType(
Greg Daniel4065d452018-11-16 15:43:41 -0500877 kAlpha_8_SkColorType);
Robert Phillips9da87e02019-02-04 13:26:26 -0500878 fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(),
Greg Daniel4065d452018-11-16 15:43:41 -0500879 format,
Robert Phillips42dda082019-05-14 13:29:45 -0400880 GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500881 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500882 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500883 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400884 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500885 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700886 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700887 return false;
888 }
889 }
890
Brian Salomonfebbd232017-07-11 15:52:02 -0400891 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400892 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(),
893 &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
Brian Salomonfebbd232017-07-11 15:52:02 -0400894 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800895
jvanverthfa38a302014-10-06 05:59:05 -0700896 return true;
897}
898
joshualitt21279c72015-05-11 07:21:37 -0700899///////////////////////////////////////////////////////////////////////////////////////////////////
900
Hal Canary6f6961e2017-01-31 13:50:44 -0500901#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700902
Brian Salomonfebbd232017-07-11 15:52:02 -0400903struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700904 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700905 ~PathTestStruct() { this->reset(); }
906
907 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700908 ShapeDataList::Iter iter;
909 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
910 ShapeData* shapeData;
911 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700912 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700913 fShapeList.remove(shapeData);
914 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700915 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500916 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700917 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700918 }
919
Brian Salomon2ee084e2016-12-16 18:59:19 -0500920 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700921 PathTestStruct* dfpr = (PathTestStruct*)pr;
922 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700923 ShapeDataList::Iter iter;
924 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
925 ShapeData* shapeData;
926 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700927 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700928 if (id == shapeData->fID) {
929 dfpr->fShapeCache.remove(shapeData->fKey);
930 dfpr->fShapeList.remove(shapeData);
931 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700932 }
933 }
934 }
935
936 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500937 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700938 ShapeCache fShapeCache;
939 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700940};
941
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500942std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500943 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500944 GrPaint&& paint,
945 const GrShape& shape,
946 const SkMatrix& viewMatrix,
947 GrDrawOpAtlas* atlas,
948 ShapeCache* shapeCache,
949 ShapeDataList* shapeList,
950 bool gammaCorrect,
951 const GrUserStencilSettings* stencil) {
952
Robert Phillips7c525e62018-06-12 10:11:12 -0400953 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
954 atlas, shapeCache, shapeList, gammaCorrect,
955 stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500956
957}
958
Brian Salomonfebbd232017-07-11 15:52:02 -0400959GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
960 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700961 static PathTestStruct gTestStruct;
962
Robert Phillips9da87e02019-02-04 13:26:26 -0500963 if (context->priv().contextID() != gTestStruct.fContextID) {
964 gTestStruct.fContextID = context->priv().contextID();
joshualitt21279c72015-05-11 07:21:37 -0700965 gTestStruct.reset();
Greg Daniel4065d452018-11-16 15:43:41 -0500966 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500967 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
968 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->priv().proxyProvider(),
Robert Phillips42dda082019-05-14 13:29:45 -0400969 format, GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500970 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500971 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500972 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500973 &PathTestStruct::HandleEviction,
974 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700975 }
976
977 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700978 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700979
bsalomonee432412016-06-27 07:18:18 -0700980 // This path renderer only allows fill styles.
981 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500982 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400983 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500984 std::move(paint), shape, viewMatrix,
985 gTestStruct.fAtlas.get(),
986 &gTestStruct.fShapeCache,
987 &gTestStruct.fShapeList,
988 gammaCorrect,
989 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700990}
991
992#endif