blob: bfabd475d647b6b444560f2cecee726192e03e9f [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
255 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
256
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;
bungeman06ca8ec2016-06-09 08:01:03 -0700312 sk_sp<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;
322 GR_STATIC_ASSERT(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
323
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 }
356 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
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
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400367 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
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.
378 if (instanceCount > SK_MaxS32 / kVerticesPerQuad) {
379 return;
380 }
Brian Osman0dd43022018-11-16 15:53:26 -0500381 GrVertexWriter vertices{target->makeVertexSpace(kVertexStride,
382 kVerticesPerQuad * instanceCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500383 &flushInfo.fVertexBuffer,
Brian Osman0dd43022018-11-16 15:53:26 -0500384 &flushInfo.fVertexOffset)};
Brian Salomond28a79d2017-10-16 13:01:07 -0400385 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
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 {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400777 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
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);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700800 int maxInstancesPerDraw =
Brian Salomondbf70722019-02-07 11:31:24 -0500801 static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Brian Salomon12d22642019-01-29 14:38:50 -0500802 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerQuad, kVerticesPerQuad,
803 flushInfo->fInstancesToFlush, maxInstancesPerDraw);
804 mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700805 target->recordDraw(
806 flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState, nullptr);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700807 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
808 flushInfo->fInstancesToFlush = 0;
809 }
joshualitt5bf99f12015-03-13 11:47:42 -0700810 }
811
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700812 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
813 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
814 }
815
Brian Osmancf860852018-10-31 14:04:39 -0400816 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500817 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700818
Brian Salomon7eae3e02018-08-07 14:02:38 +0000819 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400820 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400821 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000822 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700823 }
824
Jim Van Verth33632d82017-02-28 10:24:39 -0500825 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000826 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500827 }
828
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400829 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
830 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
831
832 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000833 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700834 }
835
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400836 // We can position on the cpu unless we're in perspective,
837 // but also need to make sure local matrices are identical
838 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
839 !thisCtm.cheapEqualTo(thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000840 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500841 }
842
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400843 // Depending on the ctm we may have a different shader for SDF paths
844 if (this->usesDistanceField()) {
845 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
846 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000847 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400848 }
849 }
850
Brian Salomond0a0a652016-12-15 15:25:22 -0500851 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500852 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000853 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700854 }
855
Jim Van Verth33632d82017-02-28 10:24:39 -0500856 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700857
Brian Salomond0a0a652016-12-15 15:25:22 -0500858 struct Entry {
Brian Osmancf860852018-10-31 14:04:39 -0400859 SkPMColor4f fColor;
860 GrShape fShape;
861 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700862 };
863
Brian Salomond0a0a652016-12-15 15:25:22 -0500864 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400865 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500866 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700867 ShapeCache* fShapeCache;
868 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700869 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500870 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700871
Brian Salomonfebbd232017-07-11 15:52:02 -0400872 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700873};
874
Jim Van Verth83010462017-03-16 08:45:39 -0400875bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400876 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400877 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700878
jvanverthfa38a302014-10-06 05:59:05 -0700879 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700880 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700881 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700882 if (!fAtlas) {
Robert Phillips0a15cc62019-07-30 12:49:10 -0400883 const GrBackendFormat format = args.fContext->priv().caps()->getDefaultBackendFormat(
884 GrColorType::kAlpha_8, GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500885 fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(),
Greg Daniel4065d452018-11-16 15:43:41 -0500886 format,
Robert Phillips42dda082019-05-14 13:29:45 -0400887 GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500888 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500889 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500890 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400891 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500892 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700893 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700894 return false;
895 }
896 }
897
Brian Salomonfebbd232017-07-11 15:52:02 -0400898 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400899 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(),
900 &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
Brian Salomonfebbd232017-07-11 15:52:02 -0400901 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800902
jvanverthfa38a302014-10-06 05:59:05 -0700903 return true;
904}
905
joshualitt21279c72015-05-11 07:21:37 -0700906///////////////////////////////////////////////////////////////////////////////////////////////////
907
Hal Canary6f6961e2017-01-31 13:50:44 -0500908#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700909
Brian Salomonfebbd232017-07-11 15:52:02 -0400910struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700911 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700912 ~PathTestStruct() { this->reset(); }
913
914 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700915 ShapeDataList::Iter iter;
916 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
917 ShapeData* shapeData;
918 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700919 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700920 fShapeList.remove(shapeData);
921 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700922 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500923 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700924 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700925 }
926
Brian Salomon2ee084e2016-12-16 18:59:19 -0500927 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700928 PathTestStruct* dfpr = (PathTestStruct*)pr;
929 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700930 ShapeDataList::Iter iter;
931 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
932 ShapeData* shapeData;
933 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700934 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700935 if (id == shapeData->fID) {
936 dfpr->fShapeCache.remove(shapeData->fKey);
937 dfpr->fShapeList.remove(shapeData);
938 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700939 }
940 }
941 }
942
943 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500944 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700945 ShapeCache fShapeCache;
946 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700947};
948
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500949std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500950 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500951 GrPaint&& paint,
952 const GrShape& shape,
953 const SkMatrix& viewMatrix,
954 GrDrawOpAtlas* atlas,
955 ShapeCache* shapeCache,
956 ShapeDataList* shapeList,
957 bool gammaCorrect,
958 const GrUserStencilSettings* stencil) {
959
Robert Phillips7c525e62018-06-12 10:11:12 -0400960 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
961 atlas, shapeCache, shapeList, gammaCorrect,
962 stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500963
964}
965
Brian Salomonfebbd232017-07-11 15:52:02 -0400966GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
967 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700968 static PathTestStruct gTestStruct;
969
Robert Phillips9da87e02019-02-04 13:26:26 -0500970 if (context->priv().contextID() != gTestStruct.fContextID) {
971 gTestStruct.fContextID = context->priv().contextID();
joshualitt21279c72015-05-11 07:21:37 -0700972 gTestStruct.reset();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400973 const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(
974 GrColorType::kAlpha_8, GrRenderable::kNo);
Robert Phillips9da87e02019-02-04 13:26:26 -0500975 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->priv().proxyProvider(),
Robert Phillips42dda082019-05-14 13:29:45 -0400976 format, GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500977 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500978 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500979 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500980 &PathTestStruct::HandleEviction,
981 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700982 }
983
984 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700985 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700986
bsalomonee432412016-06-27 07:18:18 -0700987 // This path renderer only allows fill styles.
988 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500989 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400990 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500991 std::move(paint), shape, viewMatrix,
992 gTestStruct.fAtlas.get(),
993 &gTestStruct.fShapeCache,
994 &gTestStruct.fShapeList,
995 gammaCorrect,
996 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700997}
998
999#endif