blob: 894cc2cacfb851edae09a4644c7b5414cd5a3183 [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrBuffer.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrDistanceFieldGenFromVector.h"
22#include "src/gpu/GrDrawOpTest.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#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"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040028#include "src/gpu/geometry/GrQuad.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrMeshDrawOp.h"
30#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
jvanverthfa38a302014-10-06 05:59:05 -070031
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080032static constexpr size_t kMaxAtlasTextureBytes = 2048 * 2048;
33static constexpr size_t kPlotWidth = 512;
34static constexpr size_t kPlotHeight = 256;
jvanverthfa38a302014-10-06 05:59:05 -070035
jvanverthb3eb6872014-10-24 07:12:51 -070036#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070037static int g_NumCachedShapes = 0;
38static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070039#endif
40
jvanverthb61283f2014-10-30 05:57:21 -070041// mip levels
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080042static constexpr SkScalar kIdealMinMIP = 12;
43static constexpr SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050044
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080045static constexpr SkScalar kMaxDim = 73;
46static constexpr SkScalar kMinSize = SK_ScalarHalf;
47static constexpr SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070048
Robert Phillipsd2e9f762018-03-07 11:54:37 -050049class ShapeDataKey {
50public:
51 ShapeDataKey() {}
52 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
53 ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
54 ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
55
56 ShapeDataKey& operator=(const ShapeDataKey& that) {
57 fKey.reset(that.fKey.count());
58 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
59 return *this;
60 }
61
62 // for SDF paths
63 void set(const GrShape& shape, uint32_t dim) {
64 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
65 // relevant styling information.
66 SkASSERT(shape.style().isSimpleFill());
67 SkASSERT(shape.hasUnstyledKey());
68 int shapeKeySize = shape.unstyledKeySize();
69 fKey.reset(1 + shapeKeySize);
70 fKey[0] = dim;
71 shape.writeUnstyledKey(&fKey[1]);
72 }
73
74 // for bitmap paths
75 void set(const GrShape& shape, const SkMatrix& ctm) {
76 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
77 // relevant styling information.
78 SkASSERT(shape.style().isSimpleFill());
79 SkASSERT(shape.hasUnstyledKey());
80 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
81 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
82 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
83 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
84 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
85 SkScalar tx = ctm.get(SkMatrix::kMTransX);
86 SkScalar ty = ctm.get(SkMatrix::kMTransY);
87 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050088 tx -= SkScalarFloorToScalar(tx);
89 ty -= SkScalarFloorToScalar(ty);
90 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
91 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050092 int shapeKeySize = shape.unstyledKeySize();
93 fKey.reset(5 + shapeKeySize);
94 fKey[0] = SkFloat2Bits(sx);
95 fKey[1] = SkFloat2Bits(sy);
96 fKey[2] = SkFloat2Bits(kx);
97 fKey[3] = SkFloat2Bits(ky);
98 fKey[4] = fracX | (fracY >> 8);
99 shape.writeUnstyledKey(&fKey[5]);
100 }
101
102 bool operator==(const ShapeDataKey& that) const {
103 return fKey.count() == that.fKey.count() &&
104 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
105 }
106
107 int count32() const { return fKey.count(); }
108 const uint32_t* data() const { return fKey.get(); }
109
110private:
111 // The key is composed of the GrShape's key, and either the dimensions of the DF
112 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
113 // the matrix for the path with only fractional translation.
114 SkAutoSTArray<24, uint32_t> fKey;
115};
116
117class ShapeData {
118public:
Herb Derby4d721712020-01-24 14:31:16 -0500119 ShapeDataKey fKey;
120 GrDrawOpAtlas::PlotLocator fPlotLocator;
121 SkRect fBounds;
122 GrIRect16 fTextureCoords;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500123 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
124
125 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
126 return data.fKey;
127 }
128
Kevin Lubickb5502b22018-03-12 10:17:06 -0400129 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500130 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
131 }
132};
133
134
135
joshualitt5bf99f12015-03-13 11:47:42 -0700136// Callback to clear out internal path cache when eviction occurs
Herb Derby4d721712020-01-24 14:31:16 -0500137void GrSmallPathRenderer::evict(GrDrawOpAtlas::PlotLocator plotLocator) {
joshualitt5bf99f12015-03-13 11:47:42 -0700138 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700139 ShapeDataList::Iter iter;
Herb Derby1a496c52020-01-22 17:26:56 -0500140 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
bsalomonee432412016-06-27 07:18:18 -0700141 ShapeData* shapeData;
142 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700143 iter.next();
Herb Derby4d721712020-01-24 14:31:16 -0500144 if (plotLocator == shapeData->fPlotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500145 fShapeCache.remove(shapeData->fKey);
146 fShapeList.remove(shapeData);
bsalomonee432412016-06-27 07:18:18 -0700147 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700148#ifdef DF_PATH_TRACKING
149 ++g_NumFreedPaths;
150#endif
151 }
152 }
153}
154
jvanverthfa38a302014-10-06 05:59:05 -0700155////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400156GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700157
Jim Van Verth83010462017-03-16 08:45:39 -0400158GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700159 ShapeDataList::Iter iter;
160 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
161 ShapeData* shapeData;
162 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700163 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700164 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700165 }
jvanverthb3eb6872014-10-24 07:12:51 -0700166
167#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700168 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700169#endif
jvanverthfa38a302014-10-06 05:59:05 -0700170}
171
172////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600173GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700174 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600175 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700176 }
177 // If the shape has no key then we won't get any reuse.
178 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600179 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700180 }
181 // This only supports filled paths, however, the caller may apply the style to make a filled
182 // path and try again.
183 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600184 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700185 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500186 // This does non-inverse coverage-based antialiased fills.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600187 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600188 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700189 }
jvanverthfa38a302014-10-06 05:59:05 -0700190 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700191 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600192 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700193 }
halcanary9d524f22016-03-29 09:03:52 -0700194
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500195 // Only support paths with bounds within kMaxDim by kMaxDim,
196 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500197 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400198 SkScalar scaleFactors[2] = { 1, 1 };
199 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600200 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500201 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700202 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500203 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700204 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500205 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
206 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600207 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
208 return CanDrawPath::kNo;
209 }
bsalomon6266dca2016-03-11 06:22:00 -0800210
Chris Dalton5ed44232017-09-07 13:22:46 -0600211 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700212}
213
jvanverthfa38a302014-10-06 05:59:05 -0700214////////////////////////////////////////////////////////////////////////////////
215
joshualitt5bf99f12015-03-13 11:47:42 -0700216// padding around path bounds to allow for antialiased pixels
217static const SkScalar kAntiAliasPad = 1.0f;
218
Brian Salomonfebbd232017-07-11 15:52:02 -0400219class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
220private:
221 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
222
joshualitt5bf99f12015-03-13 11:47:42 -0700223public:
Brian Salomon25a88092016-12-01 09:36:50 -0500224 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700225
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500226 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400227 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700228
Robert Phillipsb97da532019-02-12 15:24:12 -0500229 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400230 GrPaint&& paint,
231 const GrShape& shape,
232 const SkMatrix& viewMatrix,
233 GrDrawOpAtlas* atlas,
234 ShapeCache* shapeCache,
235 ShapeDataList* shapeList,
Brian Salomonfebbd232017-07-11 15:52:02 -0400236 bool gammaCorrect,
237 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400238 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
239 atlas, shapeCache, shapeList, gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400240 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700241 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500242
Brian Osmancf860852018-10-31 14:04:39 -0400243 SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrShape& shape,
Brian Salomonfebbd232017-07-11 15:52:02 -0400244 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
245 ShapeDataList* shapeList, bool gammaCorrect,
246 const GrUserStencilSettings* stencilSettings)
247 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500248 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500249 // Compute bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400250 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo);
Jim Van Verth33632d82017-02-28 10:24:39 -0500251
Jim Van Verth83010462017-03-16 08:45:39 -0400252#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500253 fUsesDistanceField = true;
254#else
Jim Van Verth83010462017-03-16 08:45:39 -0400255 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500256 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
257#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400258 // always use distance fields if in perspective
259 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500260
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400261 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500262
263 fAtlas = atlas;
264 fShapeCache = shapeCache;
265 fShapeList = shapeList;
266 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500267 }
268
Brian Salomonfebbd232017-07-11 15:52:02 -0400269 const char* name() const override { return "SmallPathOp"; }
270
Chris Dalton1706cbf2019-05-21 19:35:29 -0600271 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400272 fHelper.visitProxies(func);
273
Greg Daniel9715b6c2019-12-10 15:03:10 -0500274 const GrSurfaceProxyView* views = fAtlas->getViews();
Robert Phillips4bc70112018-03-01 10:24:02 -0500275 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500276 SkASSERT(views[i].proxy());
277 func(views[i].proxy(), GrMipMapped::kNo);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400278 }
279 }
280
Brian Osman9a390ac2018-11-12 09:47:48 -0500281#ifdef SK_DEBUG
Brian Salomonfebbd232017-07-11 15:52:02 -0400282 SkString dumpInfo() const override {
283 SkString string;
284 for (const auto& geo : fShapes) {
Brian Osmancf860852018-10-31 14:04:39 -0400285 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
Brian Salomonfebbd232017-07-11 15:52:02 -0400286 }
287 string += fHelper.dumpInfo();
288 string += INHERITED::dumpInfo();
289 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500290 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500291#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500292
Brian Salomonfebbd232017-07-11 15:52:02 -0400293 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
294
Chris Dalton6ce447a2019-06-23 18:07:38 -0600295 GrProcessorSet::Analysis finalize(
296 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
297 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700298 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600299 caps, clip, hasMixedSampledCoverage, clampType,
300 GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700301 }
302
Brian Salomonfebbd232017-07-11 15:52:02 -0400303private:
bsalomonb5238a72015-05-05 07:49:49 -0700304 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500305 sk_sp<const GrBuffer> fVertexBuffer;
306 sk_sp<const GrBuffer> fIndexBuffer;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500307 GrGeometryProcessor* fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000308 GrPipeline::FixedDynamicState* fFixedDynamicState;
bsalomonb5238a72015-05-05 07:49:49 -0700309 int fVertexOffset;
310 int fInstancesToFlush;
311 };
312
Brian Salomon91326c32017-08-09 16:02:19 -0400313 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500314 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700315
Brian Salomon7eae3e02018-08-07 14:02:38 +0000316 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500317 static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000318
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700319 FlushInfo flushInfo;
320 flushInfo.fFixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000321 int numActiveProxies = fAtlas->numActivePages();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500322 const auto views = fAtlas->getViews();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000323 for (int i = 0; i < numActiveProxies; ++i) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400324 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
325 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500326 flushInfo.fFixedDynamicState->fPrimitiveProcessorTextures[i] = views[i].proxy();
327 target->sampledProxyArray()->push_back(views[i].proxy());
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 }
Brian Salomonccb61422020-01-09 10:46:36 -0500351 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
352 target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor,
353 fAtlas->getViews(), fAtlas->numActivePages(), GrSamplerState::Filter::kBilerp,
354 flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400355 } else {
356 SkMatrix invert;
357 if (fHelper.usesLocalCoords()) {
358 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400359 return;
360 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500361 }
362
Brian Salomonccb61422020-01-09 10:46:36 -0500363 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
364 target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor,
365 fAtlas->getViews(), fAtlas->numActivePages(), GrSamplerState::Filter::kNearest,
366 kA8_GrMaskFormat, invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500367 }
joshualitt5bf99f12015-03-13 11:47:42 -0700368
joshualitt5bf99f12015-03-13 11:47:42 -0700369 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500370 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400371
372 // We need to make sure we don't overflow a 32 bit int when we request space in the
373 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400374 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400375 return;
376 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400377 GrVertexWriter vertices{ target->makeVertexSpace(
378 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
379 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
380
381 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500382 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700383 SkDebugf("Could not allocate vertices\n");
384 return;
385 }
386
bsalomonb5238a72015-05-05 07:49:49 -0700387 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700388 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500389 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700390
Jim Van Verth33632d82017-02-28 10:24:39 -0500391 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500392 if (fUsesDistanceField) {
393 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400394 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500395 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400396 if (args.fViewMatrix.hasPerspective()) {
397 // approximate the scale since we can't get it from the matrix
398 SkRect xformedBounds;
399 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400400 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
401 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400402 } else {
403 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
404 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500405 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
406 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
407 // In the majority of cases this will yield a crisper rendering.
408 SkScalar mipScale = 1.0f;
409 // Our mipscale is the maxScale clamped to the next highest power of 2
410 if (maxScale <= SK_ScalarHalf) {
411 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
412 mipScale = SkScalarPow(2, -log);
413 } else if (maxScale > SK_Scalar1) {
414 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
415 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700416 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500417 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500418
Jim Van Verth33632d82017-02-28 10:24:39 -0500419 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
420 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
421 // so we can preserve as much detail as possible. However, we can't scale down more
422 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
423 // just bigger than the ideal, and then scale down until we are no more than 4x the
424 // original mipsize.
425 if (mipSize < kIdealMinMIP) {
426 SkScalar newMipSize = mipSize;
427 do {
428 newMipSize *= 2;
429 } while (newMipSize < kIdealMinMIP);
430 while (newMipSize > 4 * mipSize) {
431 newMipSize *= 0.25f;
432 }
433 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700434 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500435 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500436
Jim Van Verth33632d82017-02-28 10:24:39 -0500437 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500438 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500439 shapeData = fShapeCache->find(key);
Herb Derby4d721712020-01-24 14:31:16 -0500440 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fPlotLocator)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500441 // Remove the stale cache entry
442 if (shapeData) {
443 fShapeCache->remove(shapeData->fKey);
444 fShapeList->remove(shapeData);
445 delete shapeData;
446 }
447 SkScalar scale = desiredDimension / maxDim;
448
449 shapeData = new ShapeData;
450 if (!this->addDFPathToAtlas(target,
451 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500452 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500453 shapeData,
454 args.fShape,
455 SkScalarCeilToInt(desiredDimension),
456 scale)) {
457 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500458 continue;
459 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500460 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500461 } else {
462 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500463 ShapeDataKey key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500464 shapeData = fShapeCache->find(key);
Herb Derby4d721712020-01-24 14:31:16 -0500465 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fPlotLocator)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500466 // Remove the stale cache entry
467 if (shapeData) {
468 fShapeCache->remove(shapeData->fKey);
469 fShapeList->remove(shapeData);
470 delete shapeData;
471 }
472
473 shapeData = new ShapeData;
474 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400475 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500476 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400477 shapeData,
478 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400479 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500480 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500481 continue;
482 }
483 }
joshualitt5bf99f12015-03-13 11:47:42 -0700484 }
485
Robert Phillips40a29d72018-01-18 12:59:22 -0500486 auto uploadTarget = target->deferredUploadTarget();
Herb Derby4d721712020-01-24 14:31:16 -0500487 fAtlas->setLastUseToken(
488 shapeData->fPlotLocator, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700489
Brian Osmanc906d252018-12-04 11:17:46 -0500490 this->writePathVertices(fAtlas, vertices, GrVertexColor(args.fColor, fWideColor),
491 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700492 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700493 }
494
bsalomon75398562015-08-17 12:55:38 -0700495 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700496 }
497
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500498 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
499 int width, int height, const void* image,
Herb Derby4d721712020-01-24 14:31:16 -0500500 GrDrawOpAtlas::PlotLocator* plotLocator, SkIPoint16* atlasLocation) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500501 auto resourceProvider = target->resourceProvider();
502 auto uploadTarget = target->deferredUploadTarget();
503
Herb Derby4d721712020-01-24 14:31:16 -0500504 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, plotLocator,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500505 uploadTarget, width, height,
506 image, atlasLocation);
507 if (GrDrawOpAtlas::ErrorCode::kError == code) {
508 return false;
509 }
510
511 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
512 this->flush(target, flushInfo);
513
Herb Derby4d721712020-01-24 14:31:16 -0500514 code = atlas->addToAtlas(resourceProvider, plotLocator, uploadTarget, width, height,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500515 image, atlasLocation);
516 }
517
518 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
519 }
520
Brian Salomone5b399e2017-07-19 13:50:54 -0400521 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400522 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
523 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500524
bsalomonee432412016-06-27 07:18:18 -0700525 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700526
527 // generate bounding rect for bitmap draw
528 SkRect scaledBounds = bounds;
529 // scale to mip level size
530 scaledBounds.fLeft *= scale;
531 scaledBounds.fTop *= scale;
532 scaledBounds.fRight *= scale;
533 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500534 // subtract out integer portion of origin
535 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500536 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
537 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700538 scaledBounds.offset(-dx, -dy);
539 // get integer boundary
540 SkIRect devPathBounds;
541 scaledBounds.roundOut(&devPathBounds);
542 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800543 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500544 // place devBounds at origin
545 int width = devPathBounds.width() + 2*intPad;
546 int height = devPathBounds.height() + 2*intPad;
547 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400548 SkScalar translateX = intPad - dx;
549 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700550
551 // draw path to bitmap
552 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500553 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400554 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700555
jvanverth512e4372015-11-23 11:50:02 -0800556 SkASSERT(devPathBounds.fLeft == 0);
557 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500558 SkASSERT(devPathBounds.width() > 0);
559 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700560
joel.liang8cbb4242017-01-09 18:39:43 -0800561 // setup signed distance field storage
562 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
563 width = dfBounds.width();
564 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800565 // TODO We should really generate this directly into the plot somehow
566 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800567
joel.liang8cbb4242017-01-09 18:39:43 -0800568 SkPath path;
569 shape.asPath(&path);
joel.liang8cbb4242017-01-09 18:39:43 -0800570 // Generate signed distance field directly from SkPath
571 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
572 path, drawMatrix,
573 width, height, width * sizeof(unsigned char));
574 if (!succeed) {
joel.liang8cbb4242017-01-09 18:39:43 -0800575 // 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());
joel.liang8cbb4242017-01-09 18:39:43 -0800602 }
joshualitt5bf99f12015-03-13 11:47:42 -0700603
604 // add to atlas
605 SkIPoint16 atlasLocation;
Herb Derby4d721712020-01-24 14:31:16 -0500606 GrDrawOpAtlas::PlotLocator plotLocator;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500607
608 if (!this->addToAtlas(target, flushInfo, atlas,
Herb Derby4d721712020-01-24 14:31:16 -0500609 width, height, dfStorage.get(), &plotLocator, &atlasLocation)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500610 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700611 }
612
613 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700614 shapeData->fKey.set(shape, dimension);
Herb Derby4d721712020-01-24 14:31:16 -0500615 shapeData->fPlotLocator = plotLocator;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500616
Robert Phillips3cf781d2017-08-22 18:25:11 -0400617 shapeData->fBounds = SkRect::Make(devPathBounds);
618 shapeData->fBounds.offset(-translateX, -translateY);
619 shapeData->fBounds.fLeft /= scale;
620 shapeData->fBounds.fTop /= scale;
621 shapeData->fBounds.fRight /= scale;
622 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500623
Jim Van Verthfb395102020-02-03 10:11:19 -0500624 // Pack the page index into the u and v texture coords
Herb Derby4d721712020-01-24 14:31:16 -0500625 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(plotLocator);
Jim Van Verthfb395102020-02-03 10:11:19 -0500626 uint16_t left, top, right, bottom;
627 std::tie(left, top, right, bottom) =
628 std::make_tuple(atlasLocation.fX + SK_DistanceFieldPad,
629 atlasLocation.fY + SK_DistanceFieldPad,
630 atlasLocation.fX + SK_DistanceFieldPad + devPathBounds.width(),
631 atlasLocation.fY + SK_DistanceFieldPad + devPathBounds.height());
632 std::tie(left, top) =
633 GrDrawOpAtlas::PackIndexInTexCoords(left, top, pageIndex);
634 std::tie(right, bottom) =
635 GrDrawOpAtlas::PackIndexInTexCoords(right, bottom, pageIndex);
636 shapeData->fTextureCoords.set(left, top, right, bottom);
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;
Herb Derby4d721712020-01-24 14:31:16 -0500710 GrDrawOpAtlas::PlotLocator plotLocator;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500711
Herb Derby4d721712020-01-24 14:31:16 -0500712 if (!this->addToAtlas(target, flushInfo, atlas, dst.width(), dst.height(),
713 dst.addr(), &plotLocator, &atlasLocation)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500714 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500715 }
716
717 // add to cache
718 shapeData->fKey.set(shape, ctm);
Herb Derby4d721712020-01-24 14:31:16 -0500719 shapeData->fPlotLocator = plotLocator;
Jim Van Verth33632d82017-02-28 10:24:39 -0500720
Jim Van Verth33632d82017-02-28 10:24:39 -0500721 shapeData->fBounds = SkRect::Make(devPathBounds);
722 shapeData->fBounds.offset(-translateX, -translateY);
723
Jim Van Verthfb395102020-02-03 10:11:19 -0500724 // Pack the page index into the u and v texture coords
Herb Derby4d721712020-01-24 14:31:16 -0500725 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(plotLocator);
Jim Van Verthfb395102020-02-03 10:11:19 -0500726 uint16_t left, top, right, bottom;
727 std::tie(left, top, right, bottom) = std::make_tuple(atlasLocation.fX, atlasLocation.fY,
728 atlasLocation.fX+width,
729 atlasLocation.fY+height);
730 std::tie(left, top) =
731 GrDrawOpAtlas::PackIndexInTexCoords(left, top, pageIndex);
732 std::tie(right, bottom) =
733 GrDrawOpAtlas::PackIndexInTexCoords(right, bottom, pageIndex);
734 shapeData->fTextureCoords.set(left, top, right, bottom);
Jim Van Verth33632d82017-02-28 10:24:39 -0500735
736 fShapeCache->add(shapeData);
737 fShapeList->addToTail(shapeData);
738#ifdef DF_PATH_TRACKING
739 ++g_NumCachedPaths;
740#endif
741 return true;
742 }
743
Brian Salomon29b60c92017-10-31 14:42:10 -0400744 void writePathVertices(GrDrawOpAtlas* atlas,
Brian Osman0dd43022018-11-16 15:53:26 -0500745 GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500746 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400747 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700748 const ShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500749 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400750 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500751 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
752 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400753 }
Jim Van Verth77047542017-01-11 14:17:00 -0500754
Brian Osman0dd43022018-11-16 15:53:26 -0500755 // set up texture coordinates
756 GrVertexWriter::TriStrip<uint16_t> texCoords{
757 (uint16_t)shapeData->fTextureCoords.fLeft,
758 (uint16_t)shapeData->fTextureCoords.fTop,
759 (uint16_t)shapeData->fTextureCoords.fRight,
760 (uint16_t)shapeData->fTextureCoords.fBottom
761 };
762
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400763 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500764 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500765 color,
766 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400767 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500768 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
769 color,
770 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400771 }
joshualitt5bf99f12015-03-13 11:47:42 -0700772 }
773
Brian Salomone5b399e2017-07-19 13:50:54 -0400774 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500775 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000776 int numAtlasTextures = SkToInt(fAtlas->numActivePages());
Greg Daniel9715b6c2019-12-10 15:03:10 -0500777 const auto views = fAtlas->getViews();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000778 if (gp->numTextureSamplers() != numAtlasTextures) {
779 for (int i = gp->numTextureSamplers(); i < numAtlasTextures; ++i) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500780 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400781 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
782 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500783 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000784 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400785 // During preparation the number of atlas pages has increased.
786 // Update the proxies used in the GP to match.
787 if (fUsesDistanceField) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500788 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500789 fAtlas->getViews(), fAtlas->numActivePages(),
790 GrSamplerState::Filter::kBilerp);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400791 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500792 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(
Brian Salomonccb61422020-01-09 10:46:36 -0500793 fAtlas->getViews(), fAtlas->numActivePages(),
794 GrSamplerState::Filter::kNearest);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400795 }
796 }
797
bsalomon6d6b6ad2016-07-13 14:45:28 -0700798 if (flushInfo->fInstancesToFlush) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000799 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400800 mesh->setIndexedPatterned(flushInfo->fIndexBuffer,
801 GrResourceProvider::NumIndicesPerNonAAQuad(),
802 GrResourceProvider::NumVertsPerNonAAQuad(),
803 flushInfo->fInstancesToFlush,
804 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon12d22642019-01-29 14:38:50 -0500805 mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
Robert Phillipscea290f2019-11-06 11:21:03 -0500806 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1,
807 flushInfo->fFixedDynamicState, nullptr, GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400808 flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() *
809 flushInfo->fInstancesToFlush;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700810 flushInfo->fInstancesToFlush = 0;
811 }
joshualitt5bf99f12015-03-13 11:47:42 -0700812 }
813
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700814 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500815 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
816 fHelper.detachProcessorSet(),
817 fHelper.pipelineFlags(),
818 fHelper.stencilSettings());
819
820 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700821 }
822
Brian Osmancf860852018-10-31 14:04:39 -0400823 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500824 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700825
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500826 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
827 const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400828 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400829 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000830 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700831 }
832
Jim Van Verth33632d82017-02-28 10:24:39 -0500833 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000834 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500835 }
836
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400837 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
838 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
839
840 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000841 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700842 }
843
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400844 // We can position on the cpu unless we're in perspective,
845 // but also need to make sure local matrices are identical
846 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
Mike Reed2c383152019-12-18 16:47:47 -0500847 !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000848 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500849 }
850
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400851 // Depending on the ctm we may have a different shader for SDF paths
852 if (this->usesDistanceField()) {
853 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
854 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000855 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400856 }
857 }
858
Brian Salomond0a0a652016-12-15 15:25:22 -0500859 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500860 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000861 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700862 }
863
Jim Van Verth33632d82017-02-28 10:24:39 -0500864 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700865
Brian Salomond0a0a652016-12-15 15:25:22 -0500866 struct Entry {
Brian Osmancf860852018-10-31 14:04:39 -0400867 SkPMColor4f fColor;
868 GrShape fShape;
869 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700870 };
871
Brian Salomond0a0a652016-12-15 15:25:22 -0500872 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400873 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500874 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700875 ShapeCache* fShapeCache;
876 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700877 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500878 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700879
Brian Salomonfebbd232017-07-11 15:52:02 -0400880 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700881};
882
Jim Van Verth83010462017-03-16 08:45:39 -0400883bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400884 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400885 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700886
jvanverthfa38a302014-10-06 05:59:05 -0700887 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700888 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700889 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700890 if (!fAtlas) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400891 const GrBackendFormat format = args.fContext->priv().caps()->getDefaultBackendFormat(
892 GrColorType::kAlpha_8, GrRenderable::kNo);
Sergey Ulanovf1b2b422020-01-24 15:39:32 -0800893
894 GrDrawOpAtlasConfig atlasConfig(args.fContext->priv().caps()->maxTextureSize(),
895 kMaxAtlasTextureBytes);
896 SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat);
897 fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(), format,
Herb Derby0ef780b2020-01-24 15:57:11 -0500898 GrColorType::kAlpha_8, size.width(), size.height(),
899 kPlotWidth, kPlotHeight, this,
900 GrDrawOpAtlas::AllowMultitexturing::kYes, this);
joshualitt21279c72015-05-11 07:21:37 -0700901 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700902 return false;
903 }
904 }
905
Brian Salomonfebbd232017-07-11 15:52:02 -0400906 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400907 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(),
908 &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
Brian Salomonfebbd232017-07-11 15:52:02 -0400909 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800910
jvanverthfa38a302014-10-06 05:59:05 -0700911 return true;
912}
913
joshualitt21279c72015-05-11 07:21:37 -0700914///////////////////////////////////////////////////////////////////////////////////////////////////
915
Hal Canary6f6961e2017-01-31 13:50:44 -0500916#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700917
Herb Derby0ef780b2020-01-24 15:57:11 -0500918struct GrSmallPathRenderer::PathTestStruct : public GrDrawOpAtlas::EvictionCallback,
919 public GrDrawOpAtlas::GenerationCounter {
halcanary96fcdcc2015-08-27 07:41:13 -0700920 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
Herb Derby1a496c52020-01-22 17:26:56 -0500921 ~PathTestStruct() override { this->reset(); }
joshualitt21279c72015-05-11 07:21:37 -0700922
923 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700924 ShapeDataList::Iter iter;
925 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
926 ShapeData* shapeData;
927 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700928 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700929 fShapeList.remove(shapeData);
930 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700931 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500932 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700933 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700934 }
935
Herb Derby4d721712020-01-24 14:31:16 -0500936 void evict(GrDrawOpAtlas::PlotLocator plotLocator) override {
joshualitt21279c72015-05-11 07:21:37 -0700937 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700938 ShapeDataList::Iter iter;
Herb Derby1a496c52020-01-22 17:26:56 -0500939 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
bsalomonee432412016-06-27 07:18:18 -0700940 ShapeData* shapeData;
941 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700942 iter.next();
Herb Derby4d721712020-01-24 14:31:16 -0500943 if (plotLocator == shapeData->fPlotLocator) {
Herb Derby1a496c52020-01-22 17:26:56 -0500944 fShapeCache.remove(shapeData->fKey);
945 fShapeList.remove(shapeData);
bsalomonee432412016-06-27 07:18:18 -0700946 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700947 }
948 }
949 }
950
951 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500952 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700953 ShapeCache fShapeCache;
954 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700955};
956
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500957std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500958 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500959 GrPaint&& paint,
960 const GrShape& shape,
961 const SkMatrix& viewMatrix,
962 GrDrawOpAtlas* atlas,
963 ShapeCache* shapeCache,
964 ShapeDataList* shapeList,
965 bool gammaCorrect,
966 const GrUserStencilSettings* stencil) {
967
Robert Phillips7c525e62018-06-12 10:11:12 -0400968 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
969 atlas, shapeCache, shapeList, gammaCorrect,
970 stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500971
972}
973
Brian Salomonfebbd232017-07-11 15:52:02 -0400974GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
975 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700976 static PathTestStruct gTestStruct;
977
Robert Phillips9da87e02019-02-04 13:26:26 -0500978 if (context->priv().contextID() != gTestStruct.fContextID) {
979 gTestStruct.fContextID = context->priv().contextID();
joshualitt21279c72015-05-11 07:21:37 -0700980 gTestStruct.reset();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400981 const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(
982 GrColorType::kAlpha_8, GrRenderable::kNo);
Sergey Ulanovf1b2b422020-01-24 15:39:32 -0800983 GrDrawOpAtlasConfig atlasConfig(context->priv().caps()->maxTextureSize(),
984 kMaxAtlasTextureBytes);
985 SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat);
986 gTestStruct.fAtlas =
987 GrDrawOpAtlas::Make(context->priv().proxyProvider(), format, GrColorType::kAlpha_8,
988 size.width(), size.height(), kPlotWidth, kPlotHeight,
Herb Derby0ef780b2020-01-24 15:57:11 -0500989 &gTestStruct,
Sergey Ulanovf1b2b422020-01-24 15:39:32 -0800990 GrDrawOpAtlas::AllowMultitexturing::kYes, &gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700991 }
992
993 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700994 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700995
bsalomonee432412016-06-27 07:18:18 -0700996 // This path renderer only allows fill styles.
997 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500998 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400999 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -05001000 std::move(paint), shape, viewMatrix,
1001 gTestStruct.fAtlas.get(),
1002 &gTestStruct.fShapeCache,
1003 &gTestStruct.fShapeList,
1004 gammaCorrect,
1005 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -07001006}
1007
1008#endif