blob: 742aa58436bbaf11879cb3773fe801fb0059816d [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
jvanverthfb1e2fc2015-09-15 13:11:11 -070032#define ATLAS_TEXTURE_WIDTH 2048
jvanverthb61283f2014-10-30 05:57:21 -070033#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfb1e2fc2015-09-15 13:11:11 -070034#define PLOT_WIDTH 512
reede4ef1ca2015-02-17 18:38:38 -080035#define PLOT_HEIGHT 256
jvanverthfa38a302014-10-06 05:59:05 -070036
37#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
38#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
39
jvanverthb3eb6872014-10-24 07:12:51 -070040#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070041static int g_NumCachedShapes = 0;
42static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070043#endif
44
jvanverthb61283f2014-10-30 05:57:21 -070045// mip levels
Jim Van Verth25b8ca12017-02-17 14:02:13 -050046static const SkScalar kIdealMinMIP = 12;
Jim Van Verth825d4d72018-01-30 20:37:03 +000047static const SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050048
49static const SkScalar kMaxDim = 73;
Jim Van Verth25b8ca12017-02-17 14:02:13 -050050static const SkScalar kMinSize = SK_ScalarHalf;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050051static const SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070052
Robert Phillipsd2e9f762018-03-07 11:54:37 -050053class ShapeDataKey {
54public:
55 ShapeDataKey() {}
56 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
57 ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
58 ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
59
60 ShapeDataKey& operator=(const ShapeDataKey& that) {
61 fKey.reset(that.fKey.count());
62 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
63 return *this;
64 }
65
66 // for SDF paths
67 void set(const GrShape& shape, uint32_t dim) {
68 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
69 // relevant styling information.
70 SkASSERT(shape.style().isSimpleFill());
71 SkASSERT(shape.hasUnstyledKey());
72 int shapeKeySize = shape.unstyledKeySize();
73 fKey.reset(1 + shapeKeySize);
74 fKey[0] = dim;
75 shape.writeUnstyledKey(&fKey[1]);
76 }
77
78 // for bitmap paths
79 void set(const GrShape& shape, const SkMatrix& ctm) {
80 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
81 // relevant styling information.
82 SkASSERT(shape.style().isSimpleFill());
83 SkASSERT(shape.hasUnstyledKey());
84 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
85 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
86 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
87 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
88 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
89 SkScalar tx = ctm.get(SkMatrix::kMTransX);
90 SkScalar ty = ctm.get(SkMatrix::kMTransY);
91 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050092 tx -= SkScalarFloorToScalar(tx);
93 ty -= SkScalarFloorToScalar(ty);
94 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
95 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050096 int shapeKeySize = shape.unstyledKeySize();
97 fKey.reset(5 + shapeKeySize);
98 fKey[0] = SkFloat2Bits(sx);
99 fKey[1] = SkFloat2Bits(sy);
100 fKey[2] = SkFloat2Bits(kx);
101 fKey[3] = SkFloat2Bits(ky);
102 fKey[4] = fracX | (fracY >> 8);
103 shape.writeUnstyledKey(&fKey[5]);
104 }
105
106 bool operator==(const ShapeDataKey& that) const {
107 return fKey.count() == that.fKey.count() &&
108 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
109 }
110
111 int count32() const { return fKey.count(); }
112 const uint32_t* data() const { return fKey.get(); }
113
114private:
115 // The key is composed of the GrShape's key, and either the dimensions of the DF
116 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
117 // the matrix for the path with only fractional translation.
118 SkAutoSTArray<24, uint32_t> fKey;
119};
120
121class ShapeData {
122public:
123 ShapeDataKey fKey;
124 GrDrawOpAtlas::AtlasID fID;
125 SkRect fBounds;
126 GrIRect16 fTextureCoords;
127 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
128
129 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
130 return data.fKey;
131 }
132
Kevin Lubickb5502b22018-03-12 10:17:06 -0400133 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500134 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
135 }
136};
137
138
139
joshualitt5bf99f12015-03-13 11:47:42 -0700140// Callback to clear out internal path cache when eviction occurs
Jim Van Verth83010462017-03-16 08:45:39 -0400141void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
142 GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
joshualitt5bf99f12015-03-13 11:47:42 -0700143 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700144 ShapeDataList::Iter iter;
145 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
146 ShapeData* shapeData;
147 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700148 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700149 if (id == shapeData->fID) {
150 dfpr->fShapeCache.remove(shapeData->fKey);
151 dfpr->fShapeList.remove(shapeData);
152 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700153#ifdef DF_PATH_TRACKING
154 ++g_NumFreedPaths;
155#endif
156 }
157 }
158}
159
jvanverthfa38a302014-10-06 05:59:05 -0700160////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400161GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700162
Jim Van Verth83010462017-03-16 08:45:39 -0400163GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700164 ShapeDataList::Iter iter;
165 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
166 ShapeData* shapeData;
167 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700168 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700169 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700170 }
jvanverthb3eb6872014-10-24 07:12:51 -0700171
172#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700173 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700174#endif
jvanverthfa38a302014-10-06 05:59:05 -0700175}
176
177////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600178GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700179 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600180 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700181 }
182 // If the shape has no key then we won't get any reuse.
183 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600184 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700185 }
186 // This only supports filled paths, however, the caller may apply the style to make a filled
187 // path and try again.
188 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600189 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700190 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500191 // This does non-inverse coverage-based antialiased fills.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600192 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600193 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700194 }
jvanverthfa38a302014-10-06 05:59:05 -0700195 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700196 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600197 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700198 }
halcanary9d524f22016-03-29 09:03:52 -0700199
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500200 // Only support paths with bounds within kMaxDim by kMaxDim,
201 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500202 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400203 SkScalar scaleFactors[2] = { 1, 1 };
204 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600205 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500206 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700207 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500208 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700209 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500210 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
211 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600212 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
213 return CanDrawPath::kNo;
214 }
bsalomon6266dca2016-03-11 06:22:00 -0800215
Chris Dalton5ed44232017-09-07 13:22:46 -0600216 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700217}
218
jvanverthfa38a302014-10-06 05:59:05 -0700219////////////////////////////////////////////////////////////////////////////////
220
joshualitt5bf99f12015-03-13 11:47:42 -0700221// padding around path bounds to allow for antialiased pixels
222static const SkScalar kAntiAliasPad = 1.0f;
223
Brian Salomonfebbd232017-07-11 15:52:02 -0400224class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
225private:
226 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
227
joshualitt5bf99f12015-03-13 11:47:42 -0700228public:
Brian Salomon25a88092016-12-01 09:36:50 -0500229 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700230
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500231 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400232 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700233
Robert Phillipsb97da532019-02-12 15:24:12 -0500234 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400235 GrPaint&& paint,
236 const GrShape& shape,
237 const SkMatrix& viewMatrix,
238 GrDrawOpAtlas* atlas,
239 ShapeCache* shapeCache,
240 ShapeDataList* shapeList,
Brian Salomonfebbd232017-07-11 15:52:02 -0400241 bool gammaCorrect,
242 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400243 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
244 atlas, shapeCache, shapeList, gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400245 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700246 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500247
Brian Osmancf860852018-10-31 14:04:39 -0400248 SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrShape& shape,
Brian Salomonfebbd232017-07-11 15:52:02 -0400249 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
250 ShapeDataList* shapeList, bool gammaCorrect,
251 const GrUserStencilSettings* stencilSettings)
252 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500253 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500254 // Compute bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400255 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo);
Jim Van Verth33632d82017-02-28 10:24:39 -0500256
Jim Van Verth83010462017-03-16 08:45:39 -0400257#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500258 fUsesDistanceField = true;
259#else
Jim Van Verth83010462017-03-16 08:45:39 -0400260 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500261 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
262#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400263 // always use distance fields if in perspective
264 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500265
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400266 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500267
268 fAtlas = atlas;
269 fShapeCache = shapeCache;
270 fShapeList = shapeList;
271 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500272 }
273
Brian Salomonfebbd232017-07-11 15:52:02 -0400274 const char* name() const override { return "SmallPathOp"; }
275
Chris Dalton1706cbf2019-05-21 19:35:29 -0600276 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400277 fHelper.visitProxies(func);
278
279 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
Robert Phillips4bc70112018-03-01 10:24:02 -0500280 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Brian Salomon9f545bc2017-11-06 10:36:57 -0500281 SkASSERT(proxies[i]);
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600282 func(proxies[i].get(), GrMipMapped::kNo);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400283 }
284 }
285
Brian Osman9a390ac2018-11-12 09:47:48 -0500286#ifdef SK_DEBUG
Brian Salomonfebbd232017-07-11 15:52:02 -0400287 SkString dumpInfo() const override {
288 SkString string;
289 for (const auto& geo : fShapes) {
Brian Osmancf860852018-10-31 14:04:39 -0400290 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
Brian Salomonfebbd232017-07-11 15:52:02 -0400291 }
292 string += fHelper.dumpInfo();
293 string += INHERITED::dumpInfo();
294 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500295 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500296#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500297
Brian Salomonfebbd232017-07-11 15:52:02 -0400298 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
299
Chris Dalton6ce447a2019-06-23 18:07:38 -0600300 GrProcessorSet::Analysis finalize(
301 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
302 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700303 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600304 caps, clip, hasMixedSampledCoverage, clampType,
305 GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700306 }
307
Brian Salomonfebbd232017-07-11 15:52:02 -0400308private:
bsalomonb5238a72015-05-05 07:49:49 -0700309 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500310 sk_sp<const GrBuffer> fVertexBuffer;
311 sk_sp<const GrBuffer> fIndexBuffer;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500312 GrGeometryProcessor* fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000313 GrPipeline::FixedDynamicState* fFixedDynamicState;
bsalomonb5238a72015-05-05 07:49:49 -0700314 int fVertexOffset;
315 int fInstancesToFlush;
316 };
317
Brian Salomon91326c32017-08-09 16:02:19 -0400318 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500319 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700320
Brian Salomon7eae3e02018-08-07 14:02:38 +0000321 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomonb0047b52019-12-05 19:52:25 +0000322 GR_STATIC_ASSERT(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000323
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700324 FlushInfo flushInfo;
325 flushInfo.fFixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000326 int numActiveProxies = fAtlas->numActivePages();
327 const auto proxies = fAtlas->getProxies();
328 for (int i = 0; i < numActiveProxies; ++i) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400329 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
330 // proxies don't get added during the visitProxies call. Thus we add them here.
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700331 flushInfo.fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
Greg Danielb20d7e52019-09-03 13:54:39 -0400332 target->sampledProxyArray()->push_back(proxies[i].get());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000333 }
Brian Salomon49348902018-06-26 09:12:38 -0400334
joshualitt5bf99f12015-03-13 11:47:42 -0700335 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400336 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500337 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500338 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400339 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500340 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
341 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
342 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
343
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400344 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500345 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400346 if (ctm.hasPerspective()) {
347 matrix = &ctm;
348 } else if (fHelper.usesLocalCoords()) {
349 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500350 return;
351 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400352 matrix = &invert;
353 } else {
354 matrix = &SkMatrix::I();
355 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500356 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(target->allocator(),
Brian Osmanc906d252018-12-04 11:17:46 -0500357 *target->caps().shaderCaps(), *matrix, fWideColor, fAtlas->getProxies(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400358 fAtlas->numActivePages(), GrSamplerState::ClampBilerp(), flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400359 } else {
360 SkMatrix invert;
361 if (fHelper.usesLocalCoords()) {
362 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400363 return;
364 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500365 }
366
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500367 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(target->allocator(),
Brian Osmanc906d252018-12-04 11:17:46 -0500368 *target->caps().shaderCaps(), this->color(), fWideColor, fAtlas->getProxies(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400369 fAtlas->numActivePages(), GrSamplerState::ClampNearest(), kA8_GrMaskFormat,
370 invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500371 }
joshualitt5bf99f12015-03-13 11:47:42 -0700372
joshualitt5bf99f12015-03-13 11:47:42 -0700373 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500374 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400375
376 // We need to make sure we don't overflow a 32 bit int when we request space in the
377 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400378 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400379 return;
380 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400381 GrVertexWriter vertices{ target->makeVertexSpace(
382 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
383 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
384
385 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500386 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700387 SkDebugf("Could not allocate vertices\n");
388 return;
389 }
390
bsalomonb5238a72015-05-05 07:49:49 -0700391 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700392 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500393 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700394
Jim Van Verth33632d82017-02-28 10:24:39 -0500395 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500396 if (fUsesDistanceField) {
397 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400398 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500399 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400400 if (args.fViewMatrix.hasPerspective()) {
401 // approximate the scale since we can't get it from the matrix
402 SkRect xformedBounds;
403 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400404 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
405 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400406 } else {
407 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
408 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500409 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
410 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
411 // In the majority of cases this will yield a crisper rendering.
412 SkScalar mipScale = 1.0f;
413 // Our mipscale is the maxScale clamped to the next highest power of 2
414 if (maxScale <= SK_ScalarHalf) {
415 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
416 mipScale = SkScalarPow(2, -log);
417 } else if (maxScale > SK_Scalar1) {
418 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
419 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700420 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500421 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500422
Jim Van Verth33632d82017-02-28 10:24:39 -0500423 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
424 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
425 // so we can preserve as much detail as possible. However, we can't scale down more
426 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
427 // just bigger than the ideal, and then scale down until we are no more than 4x the
428 // original mipsize.
429 if (mipSize < kIdealMinMIP) {
430 SkScalar newMipSize = mipSize;
431 do {
432 newMipSize *= 2;
433 } while (newMipSize < kIdealMinMIP);
434 while (newMipSize > 4 * mipSize) {
435 newMipSize *= 0.25f;
436 }
437 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700438 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500439 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500440
Jim Van Verth33632d82017-02-28 10:24:39 -0500441 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500442 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500443 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500444 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500445 // Remove the stale cache entry
446 if (shapeData) {
447 fShapeCache->remove(shapeData->fKey);
448 fShapeList->remove(shapeData);
449 delete shapeData;
450 }
451 SkScalar scale = desiredDimension / maxDim;
452
453 shapeData = new ShapeData;
454 if (!this->addDFPathToAtlas(target,
455 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500456 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500457 shapeData,
458 args.fShape,
459 SkScalarCeilToInt(desiredDimension),
460 scale)) {
461 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500462 continue;
463 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500464 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500465 } else {
466 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500467 ShapeDataKey key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500468 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500469 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500470 // Remove the stale cache entry
471 if (shapeData) {
472 fShapeCache->remove(shapeData->fKey);
473 fShapeList->remove(shapeData);
474 delete shapeData;
475 }
476
477 shapeData = new ShapeData;
478 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400479 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500480 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400481 shapeData,
482 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400483 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500484 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500485 continue;
486 }
487 }
joshualitt5bf99f12015-03-13 11:47:42 -0700488 }
489
Robert Phillips40a29d72018-01-18 12:59:22 -0500490 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips4bc70112018-03-01 10:24:02 -0500491 fAtlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700492
Brian Osmanc906d252018-12-04 11:17:46 -0500493 this->writePathVertices(fAtlas, vertices, GrVertexColor(args.fColor, fWideColor),
494 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700495 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700496 }
497
bsalomon75398562015-08-17 12:55:38 -0700498 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700499 }
500
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500501 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
502 int width, int height, const void* image,
503 GrDrawOpAtlas::AtlasID* id, SkIPoint16* atlasLocation) const {
504 auto resourceProvider = target->resourceProvider();
505 auto uploadTarget = target->deferredUploadTarget();
506
507 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, id,
508 uploadTarget, width, height,
509 image, atlasLocation);
510 if (GrDrawOpAtlas::ErrorCode::kError == code) {
511 return false;
512 }
513
514 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
515 this->flush(target, flushInfo);
516
517 code = atlas->addToAtlas(resourceProvider, id, uploadTarget, width, height,
518 image, atlasLocation);
519 }
520
521 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
522 }
523
Brian Salomone5b399e2017-07-19 13:50:54 -0400524 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400525 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
526 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500527
bsalomonee432412016-06-27 07:18:18 -0700528 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700529
530 // generate bounding rect for bitmap draw
531 SkRect scaledBounds = bounds;
532 // scale to mip level size
533 scaledBounds.fLeft *= scale;
534 scaledBounds.fTop *= scale;
535 scaledBounds.fRight *= scale;
536 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500537 // subtract out integer portion of origin
538 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500539 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
540 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700541 scaledBounds.offset(-dx, -dy);
542 // get integer boundary
543 SkIRect devPathBounds;
544 scaledBounds.roundOut(&devPathBounds);
545 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800546 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500547 // place devBounds at origin
548 int width = devPathBounds.width() + 2*intPad;
549 int height = devPathBounds.height() + 2*intPad;
550 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400551 SkScalar translateX = intPad - dx;
552 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700553
554 // draw path to bitmap
555 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500556 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400557 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700558
jvanverth512e4372015-11-23 11:50:02 -0800559 SkASSERT(devPathBounds.fLeft == 0);
560 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500561 SkASSERT(devPathBounds.width() > 0);
562 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700563
joel.liang8cbb4242017-01-09 18:39:43 -0800564 // setup signed distance field storage
565 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
566 width = dfBounds.width();
567 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800568 // TODO We should really generate this directly into the plot somehow
569 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800570
joel.liang8cbb4242017-01-09 18:39:43 -0800571 SkPath path;
572 shape.asPath(&path);
573#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
574 // Generate signed distance field directly from SkPath
575 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
576 path, drawMatrix,
577 width, height, width * sizeof(unsigned char));
578 if (!succeed) {
579#endif
580 // setup bitmap backing
581 SkAutoPixmapStorage dst;
582 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
583 devPathBounds.height()))) {
584 return false;
585 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400586 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800587
588 // rasterize path
589 SkPaint paint;
590 paint.setStyle(SkPaint::kFill_Style);
591 paint.setAntiAlias(true);
592
593 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800594
595 SkRasterClip rasterClip;
596 rasterClip.setRect(devPathBounds);
597 draw.fRC = &rasterClip;
598 draw.fMatrix = &drawMatrix;
599 draw.fDst = dst;
600
601 draw.drawPathCoverage(path, paint);
602
603 // Generate signed distance field
604 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
605 (const unsigned char*)dst.addr(),
606 dst.width(), dst.height(), dst.rowBytes());
607#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
608 }
609#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700610
611 // add to atlas
612 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500613 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500614
615 if (!this->addToAtlas(target, flushInfo, atlas,
616 width, height, dfStorage.get(), &id, &atlasLocation)) {
617 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700618 }
619
620 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700621 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700622 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500623
Robert Phillips3cf781d2017-08-22 18:25:11 -0400624 shapeData->fBounds = SkRect::Make(devPathBounds);
625 shapeData->fBounds.offset(-translateX, -translateY);
626 shapeData->fBounds.fLeft /= scale;
627 shapeData->fBounds.fTop /= scale;
628 shapeData->fBounds.fRight /= scale;
629 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500630
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400631 // We pack the 2bit page index in the low bit of the u and v texture coords
632 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
633 SkASSERT(pageIndex < 4);
634 uint16_t uBit = (pageIndex >> 1) & 0x1;
635 uint16_t vBit = pageIndex & 0x1;
636 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
637 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400638 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400639 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400640 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400641 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700642
bsalomonee432412016-06-27 07:18:18 -0700643 fShapeCache->add(shapeData);
644 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700645#ifdef DF_PATH_TRACKING
646 ++g_NumCachedPaths;
647#endif
648 return true;
649 }
650
Brian Salomone5b399e2017-07-19 13:50:54 -0400651 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400652 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
653 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500654 const SkRect& bounds = shape.bounds();
655 if (bounds.isEmpty()) {
656 return false;
657 }
658 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500659 SkScalar tx = ctm.getTranslateX();
660 SkScalar ty = ctm.getTranslateY();
661 tx -= SkScalarFloorToScalar(tx);
662 ty -= SkScalarFloorToScalar(ty);
663 drawMatrix.set(SkMatrix::kMTransX, tx);
664 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500665 SkRect shapeDevBounds;
666 drawMatrix.mapRect(&shapeDevBounds, bounds);
667 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
668 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
669
670 // get integer boundary
671 SkIRect devPathBounds;
672 shapeDevBounds.roundOut(&devPathBounds);
673 // pad to allow room for antialiasing
674 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
675 // place devBounds at origin
676 int width = devPathBounds.width() + 2 * intPad;
677 int height = devPathBounds.height() + 2 * intPad;
678 devPathBounds = SkIRect::MakeWH(width, height);
679 SkScalar translateX = intPad - dx;
680 SkScalar translateY = intPad - dy;
681
682 SkASSERT(devPathBounds.fLeft == 0);
683 SkASSERT(devPathBounds.fTop == 0);
684 SkASSERT(devPathBounds.width() > 0);
685 SkASSERT(devPathBounds.height() > 0);
686
687 SkPath path;
688 shape.asPath(&path);
689 // setup bitmap backing
690 SkAutoPixmapStorage dst;
691 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
692 devPathBounds.height()))) {
693 return false;
694 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400695 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500696
697 // rasterize path
698 SkPaint paint;
699 paint.setStyle(SkPaint::kFill_Style);
700 paint.setAntiAlias(true);
701
702 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500703
704 SkRasterClip rasterClip;
705 rasterClip.setRect(devPathBounds);
706 draw.fRC = &rasterClip;
707 drawMatrix.postTranslate(translateX, translateY);
708 draw.fMatrix = &drawMatrix;
709 draw.fDst = dst;
710
711 draw.drawPathCoverage(path, paint);
712
713 // add to atlas
714 SkIPoint16 atlasLocation;
715 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500716
717 if (!this->addToAtlas(target, flushInfo, atlas,
718 dst.width(), dst.height(), dst.addr(), &id, &atlasLocation)) {
719 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500720 }
721
722 // add to cache
723 shapeData->fKey.set(shape, ctm);
724 shapeData->fID = id;
725
Jim Van Verth33632d82017-02-28 10:24:39 -0500726 shapeData->fBounds = SkRect::Make(devPathBounds);
727 shapeData->fBounds.offset(-translateX, -translateY);
728
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400729 // We pack the 2bit page index in the low bit of the u and v texture coords
730 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
731 SkASSERT(pageIndex < 4);
732 uint16_t uBit = (pageIndex >> 1) & 0x1;
733 uint16_t vBit = pageIndex & 0x1;
734 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
735 (atlasLocation.fX+width) << 1 | uBit,
736 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500737
738 fShapeCache->add(shapeData);
739 fShapeList->addToTail(shapeData);
740#ifdef DF_PATH_TRACKING
741 ++g_NumCachedPaths;
742#endif
743 return true;
744 }
745
Brian Salomon29b60c92017-10-31 14:42:10 -0400746 void writePathVertices(GrDrawOpAtlas* atlas,
Brian Osman0dd43022018-11-16 15:53:26 -0500747 GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500748 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400749 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700750 const ShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500751 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400752 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500753 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
754 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400755 }
Jim Van Verth77047542017-01-11 14:17:00 -0500756
Brian Osman0dd43022018-11-16 15:53:26 -0500757 // set up texture coordinates
758 GrVertexWriter::TriStrip<uint16_t> texCoords{
759 (uint16_t)shapeData->fTextureCoords.fLeft,
760 (uint16_t)shapeData->fTextureCoords.fTop,
761 (uint16_t)shapeData->fTextureCoords.fRight,
762 (uint16_t)shapeData->fTextureCoords.fBottom
763 };
764
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400765 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500766 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500767 color,
768 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400769 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500770 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
771 color,
772 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400773 }
joshualitt5bf99f12015-03-13 11:47:42 -0700774 }
775
Brian Salomone5b399e2017-07-19 13:50:54 -0400776 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500777 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000778 int numAtlasTextures = SkToInt(fAtlas->numActivePages());
779 auto proxies = fAtlas->getProxies();
780 if (gp->numTextureSamplers() != numAtlasTextures) {
781 for (int i = gp->numTextureSamplers(); i < numAtlasTextures; ++i) {
782 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
Greg Danielb20d7e52019-09-03 13:54:39 -0400783 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
784 // proxies don't get added during the visitProxies call. Thus we add them here.
785 target->sampledProxyArray()->push_back(proxies[i].get());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000786 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400787 // During preparation the number of atlas pages has increased.
788 // Update the proxies used in the GP to match.
789 if (fUsesDistanceField) {
790 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500791 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400792 } else {
793 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500794 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampNearest());
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 {
815 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
816 }
817
Brian Osmancf860852018-10-31 14:04:39 -0400818 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500819 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700820
Brian Salomon7eae3e02018-08-07 14:02:38 +0000821 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400822 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400823 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000824 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700825 }
826
Jim Van Verth33632d82017-02-28 10:24:39 -0500827 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000828 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500829 }
830
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400831 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
832 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
833
834 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000835 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700836 }
837
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400838 // We can position on the cpu unless we're in perspective,
839 // but also need to make sure local matrices are identical
840 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
841 !thisCtm.cheapEqualTo(thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000842 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500843 }
844
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400845 // Depending on the ctm we may have a different shader for SDF paths
846 if (this->usesDistanceField()) {
847 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
848 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000849 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400850 }
851 }
852
Brian Salomond0a0a652016-12-15 15:25:22 -0500853 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500854 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000855 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700856 }
857
Jim Van Verth33632d82017-02-28 10:24:39 -0500858 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700859
Brian Salomond0a0a652016-12-15 15:25:22 -0500860 struct Entry {
Brian Osmancf860852018-10-31 14:04:39 -0400861 SkPMColor4f fColor;
862 GrShape fShape;
863 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700864 };
865
Brian Salomond0a0a652016-12-15 15:25:22 -0500866 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400867 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500868 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700869 ShapeCache* fShapeCache;
870 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700871 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500872 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700873
Brian Salomonfebbd232017-07-11 15:52:02 -0400874 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700875};
876
Jim Van Verth83010462017-03-16 08:45:39 -0400877bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400878 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400879 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700880
jvanverthfa38a302014-10-06 05:59:05 -0700881 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700882 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700883 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700884 if (!fAtlas) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400885 const GrBackendFormat format = args.fContext->priv().caps()->getDefaultBackendFormat(
886 GrColorType::kAlpha_8, GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500887 fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(),
Greg Daniel4065d452018-11-16 15:43:41 -0500888 format,
Robert Phillips42dda082019-05-14 13:29:45 -0400889 GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500890 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500891 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500892 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400893 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500894 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700895 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700896 return false;
897 }
898 }
899
Brian Salomonfebbd232017-07-11 15:52:02 -0400900 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400901 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(),
902 &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
Brian Salomonfebbd232017-07-11 15:52:02 -0400903 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800904
jvanverthfa38a302014-10-06 05:59:05 -0700905 return true;
906}
907
joshualitt21279c72015-05-11 07:21:37 -0700908///////////////////////////////////////////////////////////////////////////////////////////////////
909
Hal Canary6f6961e2017-01-31 13:50:44 -0500910#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700911
Brian Salomonfebbd232017-07-11 15:52:02 -0400912struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700913 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700914 ~PathTestStruct() { this->reset(); }
915
916 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700917 ShapeDataList::Iter iter;
918 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
919 ShapeData* shapeData;
920 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700921 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700922 fShapeList.remove(shapeData);
923 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700924 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500925 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700926 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700927 }
928
Brian Salomon2ee084e2016-12-16 18:59:19 -0500929 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700930 PathTestStruct* dfpr = (PathTestStruct*)pr;
931 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700932 ShapeDataList::Iter iter;
933 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
934 ShapeData* shapeData;
935 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700936 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700937 if (id == shapeData->fID) {
938 dfpr->fShapeCache.remove(shapeData->fKey);
939 dfpr->fShapeList.remove(shapeData);
940 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700941 }
942 }
943 }
944
945 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500946 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700947 ShapeCache fShapeCache;
948 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700949};
950
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500951std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500952 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500953 GrPaint&& paint,
954 const GrShape& shape,
955 const SkMatrix& viewMatrix,
956 GrDrawOpAtlas* atlas,
957 ShapeCache* shapeCache,
958 ShapeDataList* shapeList,
959 bool gammaCorrect,
960 const GrUserStencilSettings* stencil) {
961
Robert Phillips7c525e62018-06-12 10:11:12 -0400962 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
963 atlas, shapeCache, shapeList, gammaCorrect,
964 stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500965
966}
967
Brian Salomonfebbd232017-07-11 15:52:02 -0400968GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
969 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700970 static PathTestStruct gTestStruct;
971
Robert Phillips9da87e02019-02-04 13:26:26 -0500972 if (context->priv().contextID() != gTestStruct.fContextID) {
973 gTestStruct.fContextID = context->priv().contextID();
joshualitt21279c72015-05-11 07:21:37 -0700974 gTestStruct.reset();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400975 const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(
976 GrColorType::kAlpha_8, GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500977 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->priv().proxyProvider(),
Robert Phillips42dda082019-05-14 13:29:45 -0400978 format, GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500979 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500980 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500981 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500982 &PathTestStruct::HandleEviction,
983 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700984 }
985
986 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700987 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700988
bsalomonee432412016-06-27 07:18:18 -0700989 // This path renderer only allows fill styles.
990 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500991 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400992 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500993 std::move(paint), shape, viewMatrix,
994 gTestStruct.fAtlas.get(),
995 &gTestStruct.fShapeCache,
996 &gTestStruct.fShapeList,
997 gammaCorrect,
998 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700999}
1000
1001#endif