blob: 48de75aab4e3d1bb27eb6d0ca0fcaacaba043813 [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
Jim Van Verth83010462017-03-16 08:45:39 -04009#include "GrSmallPathRenderer.h"
cdalton397536c2016-03-25 12:15:03 -070010#include "GrBuffer.h"
jvanverthfa38a302014-10-06 05:59:05 -070011#include "GrContext.h"
Jim Van Verthf9e678d2017-02-15 15:46:52 -050012#include "GrDistanceFieldGenFromVector.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050013#include "GrDrawOpTest.h"
Brian Salomon57caa662017-10-18 12:21:05 +000014#include "GrQuad.h"
bsalomonb5238a72015-05-05 07:49:49 -070015#include "GrResourceProvider.h"
Brian Salomonfebbd232017-07-11 15:52:02 -040016#include "GrSimpleMeshDrawOpHelper.h"
Hal Canary95e3c052017-01-11 12:44:43 -050017#include "SkAutoMalloc.h"
Brian Osmanc7da1462017-08-17 16:14:25 -040018#include "SkAutoPixmapStorage.h"
jvanverthfa38a302014-10-06 05:59:05 -070019#include "SkDistanceFieldGen.h"
Brian Salomona33b67c2018-05-17 10:42:14 -040020#include "SkPointPriv.h"
Brian Osmanc7da1462017-08-17 16:14:25 -040021#include "SkRasterClip.h"
Brian Salomonfebbd232017-07-11 15:52:02 -040022#include "effects/GrBitmapTextGeoProc.h"
23#include "effects/GrDistanceFieldGeoProc.h"
24#include "ops/GrMeshDrawOp.h"
jvanverthfa38a302014-10-06 05:59:05 -070025
jvanverthfb1e2fc2015-09-15 13:11:11 -070026#define ATLAS_TEXTURE_WIDTH 2048
jvanverthb61283f2014-10-30 05:57:21 -070027#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfb1e2fc2015-09-15 13:11:11 -070028#define PLOT_WIDTH 512
reede4ef1ca2015-02-17 18:38:38 -080029#define PLOT_HEIGHT 256
jvanverthfa38a302014-10-06 05:59:05 -070030
31#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
32#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
33
jvanverthb3eb6872014-10-24 07:12:51 -070034#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070035static int g_NumCachedShapes = 0;
36static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070037#endif
38
jvanverthb61283f2014-10-30 05:57:21 -070039// mip levels
Jim Van Verth25b8ca12017-02-17 14:02:13 -050040static const SkScalar kIdealMinMIP = 12;
Jim Van Verth825d4d72018-01-30 20:37:03 +000041static const SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050042
43static const SkScalar kMaxDim = 73;
Jim Van Verth25b8ca12017-02-17 14:02:13 -050044static const SkScalar kMinSize = SK_ScalarHalf;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050045static const SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070046
Robert Phillipsd2e9f762018-03-07 11:54:37 -050047class ShapeDataKey {
48public:
49 ShapeDataKey() {}
50 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
51 ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
52 ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
53
54 ShapeDataKey& operator=(const ShapeDataKey& that) {
55 fKey.reset(that.fKey.count());
56 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
57 return *this;
58 }
59
60 // for SDF paths
61 void set(const GrShape& shape, uint32_t dim) {
62 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
63 // relevant styling information.
64 SkASSERT(shape.style().isSimpleFill());
65 SkASSERT(shape.hasUnstyledKey());
66 int shapeKeySize = shape.unstyledKeySize();
67 fKey.reset(1 + shapeKeySize);
68 fKey[0] = dim;
69 shape.writeUnstyledKey(&fKey[1]);
70 }
71
72 // for bitmap paths
73 void set(const GrShape& shape, const SkMatrix& ctm) {
74 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
75 // relevant styling information.
76 SkASSERT(shape.style().isSimpleFill());
77 SkASSERT(shape.hasUnstyledKey());
78 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
79 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
80 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
81 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
82 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
83 SkScalar tx = ctm.get(SkMatrix::kMTransX);
84 SkScalar ty = ctm.get(SkMatrix::kMTransY);
85 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050086 tx -= SkScalarFloorToScalar(tx);
87 ty -= SkScalarFloorToScalar(ty);
88 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
89 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050090 int shapeKeySize = shape.unstyledKeySize();
91 fKey.reset(5 + shapeKeySize);
92 fKey[0] = SkFloat2Bits(sx);
93 fKey[1] = SkFloat2Bits(sy);
94 fKey[2] = SkFloat2Bits(kx);
95 fKey[3] = SkFloat2Bits(ky);
96 fKey[4] = fracX | (fracY >> 8);
97 shape.writeUnstyledKey(&fKey[5]);
98 }
99
100 bool operator==(const ShapeDataKey& that) const {
101 return fKey.count() == that.fKey.count() &&
102 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
103 }
104
105 int count32() const { return fKey.count(); }
106 const uint32_t* data() const { return fKey.get(); }
107
108private:
109 // The key is composed of the GrShape's key, and either the dimensions of the DF
110 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
111 // the matrix for the path with only fractional translation.
112 SkAutoSTArray<24, uint32_t> fKey;
113};
114
115class ShapeData {
116public:
117 ShapeDataKey fKey;
118 GrDrawOpAtlas::AtlasID fID;
119 SkRect fBounds;
120 GrIRect16 fTextureCoords;
121 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
122
123 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
124 return data.fKey;
125 }
126
Kevin Lubickb5502b22018-03-12 10:17:06 -0400127 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500128 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
129 }
130};
131
132
133
joshualitt5bf99f12015-03-13 11:47:42 -0700134// Callback to clear out internal path cache when eviction occurs
Jim Van Verth83010462017-03-16 08:45:39 -0400135void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
136 GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
joshualitt5bf99f12015-03-13 11:47:42 -0700137 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700138 ShapeDataList::Iter iter;
139 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
140 ShapeData* shapeData;
141 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700142 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700143 if (id == shapeData->fID) {
144 dfpr->fShapeCache.remove(shapeData->fKey);
145 dfpr->fShapeList.remove(shapeData);
146 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700147#ifdef DF_PATH_TRACKING
148 ++g_NumFreedPaths;
149#endif
150 }
151 }
152}
153
jvanverthfa38a302014-10-06 05:59:05 -0700154////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400155GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700156
Jim Van Verth83010462017-03-16 08:45:39 -0400157GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700158 ShapeDataList::Iter iter;
159 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
160 ShapeData* shapeData;
161 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700162 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700163 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700164 }
jvanverthb3eb6872014-10-24 07:12:51 -0700165
166#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700167 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700168#endif
jvanverthfa38a302014-10-06 05:59:05 -0700169}
170
171////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600172GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700173 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600174 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700175 }
176 // If the shape has no key then we won't get any reuse.
177 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600178 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700179 }
180 // This only supports filled paths, however, the caller may apply the style to make a filled
181 // path and try again.
182 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600183 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700184 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500185 // This does non-inverse coverage-based antialiased fills.
186 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600187 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700188 }
jvanverthfa38a302014-10-06 05:59:05 -0700189 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700190 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600191 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700192 }
halcanary9d524f22016-03-29 09:03:52 -0700193
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500194 // Only support paths with bounds within kMaxDim by kMaxDim,
195 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500196 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400197 SkScalar scaleFactors[2] = { 1, 1 };
198 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600199 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500200 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700201 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500202 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700203 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500204 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
205 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600206 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
207 return CanDrawPath::kNo;
208 }
bsalomon6266dca2016-03-11 06:22:00 -0800209
Chris Dalton5ed44232017-09-07 13:22:46 -0600210 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700211}
212
jvanverthfa38a302014-10-06 05:59:05 -0700213////////////////////////////////////////////////////////////////////////////////
214
joshualitt5bf99f12015-03-13 11:47:42 -0700215// padding around path bounds to allow for antialiased pixels
216static const SkScalar kAntiAliasPad = 1.0f;
217
Brian Salomonfebbd232017-07-11 15:52:02 -0400218class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
219private:
220 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
221
joshualitt5bf99f12015-03-13 11:47:42 -0700222public:
Brian Salomon25a88092016-12-01 09:36:50 -0500223 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700224
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500225 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400226 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700227
Brian Salomonfebbd232017-07-11 15:52:02 -0400228 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const GrShape& shape,
229 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas,
230 ShapeCache* shapeCache, ShapeDataList* shapeList,
231 bool gammaCorrect,
232 const GrUserStencilSettings* stencilSettings) {
233 return Helper::FactoryHelper<SmallPathOp>(std::move(paint), shape, viewMatrix, atlas,
234 shapeCache, shapeList, gammaCorrect,
235 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700236 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500237
Brian Salomonfebbd232017-07-11 15:52:02 -0400238 SmallPathOp(Helper::MakeArgs helperArgs, GrColor color, const GrShape& shape,
239 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
240 ShapeDataList* shapeList, bool gammaCorrect,
241 const GrUserStencilSettings* stencilSettings)
242 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500243 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500244 // Compute bounds
245 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
246
Jim Van Verth83010462017-03-16 08:45:39 -0400247#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500248 fUsesDistanceField = true;
249#else
Jim Van Verth83010462017-03-16 08:45:39 -0400250 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500251 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
252#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400253 // always use distance fields if in perspective
254 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500255
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400256 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500257
258 fAtlas = atlas;
259 fShapeCache = shapeCache;
260 fShapeList = shapeList;
261 fGammaCorrect = gammaCorrect;
262
Brian Salomond0a0a652016-12-15 15:25:22 -0500263 }
264
Brian Salomonfebbd232017-07-11 15:52:02 -0400265 const char* name() const override { return "SmallPathOp"; }
266
Robert Phillipsf1748f52017-09-14 14:11:24 -0400267 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400268 fHelper.visitProxies(func);
269
270 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
Robert Phillips4bc70112018-03-01 10:24:02 -0500271 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Brian Salomon9f545bc2017-11-06 10:36:57 -0500272 SkASSERT(proxies[i]);
273 func(proxies[i].get());
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400274 }
275 }
276
Brian Salomonfebbd232017-07-11 15:52:02 -0400277 SkString dumpInfo() const override {
278 SkString string;
279 for (const auto& geo : fShapes) {
280 string.appendf("Color: 0x%08x\n", geo.fColor);
281 }
282 string += fHelper.dumpInfo();
283 string += INHERITED::dumpInfo();
284 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500285 }
286
Brian Salomonfebbd232017-07-11 15:52:02 -0400287 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
288
Brian Osman9a725dd2017-09-20 09:53:22 -0400289 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
290 GrPixelConfigIsClamped dstIsClamped) override {
291 return fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
292 GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonfebbd232017-07-11 15:52:02 -0400293 &fShapes.front().fColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700294 }
295
Brian Salomonfebbd232017-07-11 15:52:02 -0400296private:
bsalomonb5238a72015-05-05 07:49:49 -0700297 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500298 sk_sp<const GrBuffer> fVertexBuffer;
299 sk_sp<const GrBuffer> fIndexBuffer;
bungeman06ca8ec2016-06-09 08:01:03 -0700300 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomonfebbd232017-07-11 15:52:02 -0400301 const GrPipeline* fPipeline;
bsalomonb5238a72015-05-05 07:49:49 -0700302 int fVertexOffset;
303 int fInstancesToFlush;
304 };
305
Brian Salomon91326c32017-08-09 16:02:19 -0400306 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500307 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700308
bsalomon342bfc22016-04-01 06:06:20 -0700309 FlushInfo flushInfo;
Brian Salomonfebbd232017-07-11 15:52:02 -0400310 flushInfo.fPipeline = fHelper.makePipeline(target);
joshualitt5bf99f12015-03-13 11:47:42 -0700311 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400312 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500313 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500314 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400315 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500316 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
317 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
318 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
319
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400320 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500321 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400322 if (ctm.hasPerspective()) {
323 matrix = &ctm;
324 } else if (fHelper.usesLocalCoords()) {
325 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500326 return;
327 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400328 matrix = &invert;
329 } else {
330 matrix = &SkMatrix::I();
331 }
332 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
Robert Phillips4bc70112018-03-01 10:24:02 -0500333 *matrix, fAtlas->getProxies(), fAtlas->numActivePages(),
334 GrSamplerState::ClampBilerp(), flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400335 } else {
336 SkMatrix invert;
337 if (fHelper.usesLocalCoords()) {
338 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400339 return;
340 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500341 }
342
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400343 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Robert Phillips4bc70112018-03-01 10:24:02 -0500344 this->color(), fAtlas->getProxies(), fAtlas->numActivePages(),
345 GrSamplerState::ClampNearest(), kA8_GrMaskFormat, invert,
346 fHelper.usesLocalCoords());
Jim Van Verth33632d82017-02-28 10:24:39 -0500347 }
joshualitt5bf99f12015-03-13 11:47:42 -0700348
joshualitt5bf99f12015-03-13 11:47:42 -0700349 // allocate vertices
bsalomon342bfc22016-04-01 06:06:20 -0700350 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Jim Van Verth33632d82017-02-28 10:24:39 -0500351 SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(GrColor) + 2*sizeof(uint16_t));
bsalomonb5238a72015-05-05 07:49:49 -0700352
cdalton397536c2016-03-25 12:15:03 -0700353 const GrBuffer* vertexBuffer;
bsalomon75398562015-08-17 12:55:38 -0700354 void* vertices = target->makeVertexSpace(vertexStride,
355 kVerticesPerQuad * instanceCount,
356 &vertexBuffer,
357 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -0700358 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400359 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
bsalomonb5238a72015-05-05 07:49:49 -0700360 if (!vertices || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700361 SkDebugf("Could not allocate vertices\n");
362 return;
363 }
364
bsalomonb5238a72015-05-05 07:49:49 -0700365 flushInfo.fInstancesToFlush = 0;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700366 // Pointer to the next set of vertices to write.
367 intptr_t offset = reinterpret_cast<intptr_t>(vertices);
joshualitt5bf99f12015-03-13 11:47:42 -0700368 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500369 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700370
Jim Van Verth33632d82017-02-28 10:24:39 -0500371 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500372 if (fUsesDistanceField) {
373 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400374 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500375 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400376 if (args.fViewMatrix.hasPerspective()) {
377 // approximate the scale since we can't get it from the matrix
378 SkRect xformedBounds;
379 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400380 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
381 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400382 } else {
383 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
384 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500385 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
386 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
387 // In the majority of cases this will yield a crisper rendering.
388 SkScalar mipScale = 1.0f;
389 // Our mipscale is the maxScale clamped to the next highest power of 2
390 if (maxScale <= SK_ScalarHalf) {
391 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
392 mipScale = SkScalarPow(2, -log);
393 } else if (maxScale > SK_Scalar1) {
394 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
395 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700396 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500397 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500398
Jim Van Verth33632d82017-02-28 10:24:39 -0500399 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
400 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
401 // so we can preserve as much detail as possible. However, we can't scale down more
402 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
403 // just bigger than the ideal, and then scale down until we are no more than 4x the
404 // original mipsize.
405 if (mipSize < kIdealMinMIP) {
406 SkScalar newMipSize = mipSize;
407 do {
408 newMipSize *= 2;
409 } while (newMipSize < kIdealMinMIP);
410 while (newMipSize > 4 * mipSize) {
411 newMipSize *= 0.25f;
412 }
413 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700414 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500415 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500416
Jim Van Verth33632d82017-02-28 10:24:39 -0500417 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500418 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500419 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500420 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500421 // Remove the stale cache entry
422 if (shapeData) {
423 fShapeCache->remove(shapeData->fKey);
424 fShapeList->remove(shapeData);
425 delete shapeData;
426 }
427 SkScalar scale = desiredDimension / maxDim;
428
429 shapeData = new ShapeData;
430 if (!this->addDFPathToAtlas(target,
431 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500432 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500433 shapeData,
434 args.fShape,
435 SkScalarCeilToInt(desiredDimension),
436 scale)) {
437 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500438 continue;
439 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500440 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500441 } else {
442 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500443 ShapeDataKey key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500444 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500445 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500446 // Remove the stale cache entry
447 if (shapeData) {
448 fShapeCache->remove(shapeData->fKey);
449 fShapeList->remove(shapeData);
450 delete shapeData;
451 }
452
453 shapeData = new ShapeData;
454 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400455 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500456 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400457 shapeData,
458 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400459 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500460 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500461 continue;
462 }
463 }
joshualitt5bf99f12015-03-13 11:47:42 -0700464 }
465
Robert Phillips40a29d72018-01-18 12:59:22 -0500466 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips4bc70112018-03-01 10:24:02 -0500467 fAtlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700468
Robert Phillips4bc70112018-03-01 10:24:02 -0500469 this->writePathVertices(fAtlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800470 offset,
471 args.fColor,
bsalomonb5238a72015-05-05 07:49:49 -0700472 vertexStride,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400473 args.fViewMatrix,
bsalomonee432412016-06-27 07:18:18 -0700474 shapeData);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700475 offset += kVerticesPerQuad * vertexStride;
bsalomonb5238a72015-05-05 07:49:49 -0700476 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700477 }
478
bsalomon75398562015-08-17 12:55:38 -0700479 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700480 }
481
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500482 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
483 int width, int height, const void* image,
484 GrDrawOpAtlas::AtlasID* id, SkIPoint16* atlasLocation) const {
485 auto resourceProvider = target->resourceProvider();
486 auto uploadTarget = target->deferredUploadTarget();
487
488 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, id,
489 uploadTarget, width, height,
490 image, atlasLocation);
491 if (GrDrawOpAtlas::ErrorCode::kError == code) {
492 return false;
493 }
494
495 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
496 this->flush(target, flushInfo);
497
498 code = atlas->addToAtlas(resourceProvider, id, uploadTarget, width, height,
499 image, atlasLocation);
500 }
501
502 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
503 }
504
Brian Salomone5b399e2017-07-19 13:50:54 -0400505 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400506 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
507 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500508
bsalomonee432412016-06-27 07:18:18 -0700509 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700510
511 // generate bounding rect for bitmap draw
512 SkRect scaledBounds = bounds;
513 // scale to mip level size
514 scaledBounds.fLeft *= scale;
515 scaledBounds.fTop *= scale;
516 scaledBounds.fRight *= scale;
517 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500518 // subtract out integer portion of origin
519 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500520 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
521 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700522 scaledBounds.offset(-dx, -dy);
523 // get integer boundary
524 SkIRect devPathBounds;
525 scaledBounds.roundOut(&devPathBounds);
526 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800527 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500528 // place devBounds at origin
529 int width = devPathBounds.width() + 2*intPad;
530 int height = devPathBounds.height() + 2*intPad;
531 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400532 SkScalar translateX = intPad - dx;
533 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700534
535 // draw path to bitmap
536 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500537 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400538 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700539
jvanverth512e4372015-11-23 11:50:02 -0800540 SkASSERT(devPathBounds.fLeft == 0);
541 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500542 SkASSERT(devPathBounds.width() > 0);
543 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700544
joel.liang8cbb4242017-01-09 18:39:43 -0800545 // setup signed distance field storage
546 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
547 width = dfBounds.width();
548 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800549 // TODO We should really generate this directly into the plot somehow
550 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800551
joel.liang8cbb4242017-01-09 18:39:43 -0800552 SkPath path;
553 shape.asPath(&path);
554#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
555 // Generate signed distance field directly from SkPath
556 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
557 path, drawMatrix,
558 width, height, width * sizeof(unsigned char));
559 if (!succeed) {
560#endif
561 // setup bitmap backing
562 SkAutoPixmapStorage dst;
563 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
564 devPathBounds.height()))) {
565 return false;
566 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400567 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800568
569 // rasterize path
570 SkPaint paint;
571 paint.setStyle(SkPaint::kFill_Style);
572 paint.setAntiAlias(true);
573
574 SkDraw draw;
575 sk_bzero(&draw, sizeof(draw));
576
577 SkRasterClip rasterClip;
578 rasterClip.setRect(devPathBounds);
579 draw.fRC = &rasterClip;
580 draw.fMatrix = &drawMatrix;
581 draw.fDst = dst;
582
583 draw.drawPathCoverage(path, paint);
584
585 // Generate signed distance field
586 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
587 (const unsigned char*)dst.addr(),
588 dst.width(), dst.height(), dst.rowBytes());
589#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
590 }
591#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700592
593 // add to atlas
594 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500595 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500596
597 if (!this->addToAtlas(target, flushInfo, atlas,
598 width, height, dfStorage.get(), &id, &atlasLocation)) {
599 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700600 }
601
602 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700603 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700604 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500605
Robert Phillips3cf781d2017-08-22 18:25:11 -0400606 shapeData->fBounds = SkRect::Make(devPathBounds);
607 shapeData->fBounds.offset(-translateX, -translateY);
608 shapeData->fBounds.fLeft /= scale;
609 shapeData->fBounds.fTop /= scale;
610 shapeData->fBounds.fRight /= scale;
611 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500612
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400613 // We pack the 2bit page index in the low bit of the u and v texture coords
614 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
615 SkASSERT(pageIndex < 4);
616 uint16_t uBit = (pageIndex >> 1) & 0x1;
617 uint16_t vBit = pageIndex & 0x1;
618 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
619 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400620 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400621 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400622 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400623 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700624
bsalomonee432412016-06-27 07:18:18 -0700625 fShapeCache->add(shapeData);
626 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700627#ifdef DF_PATH_TRACKING
628 ++g_NumCachedPaths;
629#endif
630 return true;
631 }
632
Brian Salomone5b399e2017-07-19 13:50:54 -0400633 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400634 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
635 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500636 const SkRect& bounds = shape.bounds();
637 if (bounds.isEmpty()) {
638 return false;
639 }
640 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500641 SkScalar tx = ctm.getTranslateX();
642 SkScalar ty = ctm.getTranslateY();
643 tx -= SkScalarFloorToScalar(tx);
644 ty -= SkScalarFloorToScalar(ty);
645 drawMatrix.set(SkMatrix::kMTransX, tx);
646 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500647 SkRect shapeDevBounds;
648 drawMatrix.mapRect(&shapeDevBounds, bounds);
649 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
650 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
651
652 // get integer boundary
653 SkIRect devPathBounds;
654 shapeDevBounds.roundOut(&devPathBounds);
655 // pad to allow room for antialiasing
656 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
657 // place devBounds at origin
658 int width = devPathBounds.width() + 2 * intPad;
659 int height = devPathBounds.height() + 2 * intPad;
660 devPathBounds = SkIRect::MakeWH(width, height);
661 SkScalar translateX = intPad - dx;
662 SkScalar translateY = intPad - dy;
663
664 SkASSERT(devPathBounds.fLeft == 0);
665 SkASSERT(devPathBounds.fTop == 0);
666 SkASSERT(devPathBounds.width() > 0);
667 SkASSERT(devPathBounds.height() > 0);
668
669 SkPath path;
670 shape.asPath(&path);
671 // setup bitmap backing
672 SkAutoPixmapStorage dst;
673 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
674 devPathBounds.height()))) {
675 return false;
676 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400677 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500678
679 // rasterize path
680 SkPaint paint;
681 paint.setStyle(SkPaint::kFill_Style);
682 paint.setAntiAlias(true);
683
684 SkDraw draw;
685 sk_bzero(&draw, sizeof(draw));
686
687 SkRasterClip rasterClip;
688 rasterClip.setRect(devPathBounds);
689 draw.fRC = &rasterClip;
690 drawMatrix.postTranslate(translateX, translateY);
691 draw.fMatrix = &drawMatrix;
692 draw.fDst = dst;
693
694 draw.drawPathCoverage(path, paint);
695
696 // add to atlas
697 SkIPoint16 atlasLocation;
698 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500699
700 if (!this->addToAtlas(target, flushInfo, atlas,
701 dst.width(), dst.height(), dst.addr(), &id, &atlasLocation)) {
702 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500703 }
704
705 // add to cache
706 shapeData->fKey.set(shape, ctm);
707 shapeData->fID = id;
708
Jim Van Verth33632d82017-02-28 10:24:39 -0500709 shapeData->fBounds = SkRect::Make(devPathBounds);
710 shapeData->fBounds.offset(-translateX, -translateY);
711
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400712 // We pack the 2bit page index in the low bit of the u and v texture coords
713 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
714 SkASSERT(pageIndex < 4);
715 uint16_t uBit = (pageIndex >> 1) & 0x1;
716 uint16_t vBit = pageIndex & 0x1;
717 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
718 (atlasLocation.fX+width) << 1 | uBit,
719 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500720
721 fShapeCache->add(shapeData);
722 fShapeList->addToTail(shapeData);
723#ifdef DF_PATH_TRACKING
724 ++g_NumCachedPaths;
725#endif
726 return true;
727 }
728
Brian Salomon29b60c92017-10-31 14:42:10 -0400729 void writePathVertices(GrDrawOpAtlas* atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800730 intptr_t offset,
731 GrColor color,
bsalomonb5238a72015-05-05 07:49:49 -0700732 size_t vertexStride,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400733 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700734 const ShapeData* shapeData) const {
joshualitt53f26aa2015-12-10 07:29:54 -0800735 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
736
Jim Van Verth77047542017-01-11 14:17:00 -0500737 SkRect bounds = shapeData->fBounds;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400738 SkRect translatedBounds(bounds);
739 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500740 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
741 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400742 }
Jim Van Verth77047542017-01-11 14:17:00 -0500743
joshualitt5bf99f12015-03-13 11:47:42 -0700744 // vertex positions
745 // TODO make the vertex attributes a struct
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400746 if (fUsesDistanceField && !ctm.hasPerspective()) {
Brian Salomona33b67c2018-05-17 10:42:14 -0400747 GrQuad quad(translatedBounds, ctm);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400748 intptr_t positionOffset = offset;
749 SkPoint* position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000750 *position = quad.point(0);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400751 positionOffset += vertexStride;
752 position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000753 *position = quad.point(1);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400754 positionOffset += vertexStride;
755 position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000756 *position = quad.point(2);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400757 positionOffset += vertexStride;
758 position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000759 *position = quad.point(3);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400760 } else {
Brian Salomonec42e152018-05-18 12:52:22 -0400761 SkPointPriv::SetRectTriStrip(positions, translatedBounds, vertexStride);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400762 }
joshualitt5bf99f12015-03-13 11:47:42 -0700763
joshualitt53f26aa2015-12-10 07:29:54 -0800764 // colors
765 for (int i = 0; i < kVerticesPerQuad; i++) {
766 GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride);
767 *colorPtr = color;
768 }
769
Jim Van Verth77047542017-01-11 14:17:00 -0500770 // set up texture coordinates
Robert Phillips8296e752017-08-25 08:45:21 -0400771 uint16_t l = shapeData->fTextureCoords.fLeft;
772 uint16_t t = shapeData->fTextureCoords.fTop;
773 uint16_t r = shapeData->fTextureCoords.fRight;
774 uint16_t b = shapeData->fTextureCoords.fBottom;
Jim Van Verth33632d82017-02-28 10:24:39 -0500775
776 // set vertex texture coords
777 intptr_t textureCoordOffset = offset + sizeof(SkPoint) + sizeof(GrColor);
778 uint16_t* textureCoords = (uint16_t*) textureCoordOffset;
779 textureCoords[0] = l;
780 textureCoords[1] = t;
781 textureCoordOffset += vertexStride;
782 textureCoords = (uint16_t*)textureCoordOffset;
783 textureCoords[0] = l;
784 textureCoords[1] = b;
785 textureCoordOffset += vertexStride;
786 textureCoords = (uint16_t*)textureCoordOffset;
787 textureCoords[0] = r;
Brian Salomon57caa662017-10-18 12:21:05 +0000788 textureCoords[1] = t;
Jim Van Verth33632d82017-02-28 10:24:39 -0500789 textureCoordOffset += vertexStride;
790 textureCoords = (uint16_t*)textureCoordOffset;
791 textureCoords[0] = r;
Brian Salomon57caa662017-10-18 12:21:05 +0000792 textureCoords[1] = b;
joshualitt5bf99f12015-03-13 11:47:42 -0700793 }
794
Brian Salomone5b399e2017-07-19 13:50:54 -0400795 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400796 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
Robert Phillips4bc70112018-03-01 10:24:02 -0500797 if (gp->numTextureSamplers() != (int)fAtlas->numActivePages()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400798 // During preparation the number of atlas pages has increased.
799 // Update the proxies used in the GP to match.
800 if (fUsesDistanceField) {
801 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500802 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400803 } else {
804 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500805 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400806 }
807 }
808
bsalomon6d6b6ad2016-07-13 14:45:28 -0700809 if (flushInfo->fInstancesToFlush) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600810 GrMesh mesh(GrPrimitiveType::kTriangles);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700811 int maxInstancesPerDraw =
812 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Daltonbca46e22017-05-15 11:03:26 -0600813 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerQuad,
Chris Dalton114a3c02017-05-26 15:17:19 -0600814 kVerticesPerQuad, flushInfo->fInstancesToFlush,
815 maxInstancesPerDraw);
816 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomonfebbd232017-07-11 15:52:02 -0400817 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700818 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
819 flushInfo->fInstancesToFlush = 0;
820 }
joshualitt5bf99f12015-03-13 11:47:42 -0700821 }
822
Brian Salomond0a0a652016-12-15 15:25:22 -0500823 GrColor color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500824 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700825
Brian Salomon25a88092016-12-01 09:36:50 -0500826 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400827 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400828 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt8cab9a72015-07-16 09:13:50 -0700829 return false;
830 }
831
Jim Van Verth33632d82017-02-28 10:24:39 -0500832 if (this->usesDistanceField() != that->usesDistanceField()) {
833 return false;
834 }
835
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400836 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
837 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
838
839 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700840 return false;
841 }
842
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400843 // We can position on the cpu unless we're in perspective,
844 // but also need to make sure local matrices are identical
845 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
846 !thisCtm.cheapEqualTo(thatCtm)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500847 return false;
848 }
849
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400850 // Depending on the ctm we may have a different shader for SDF paths
851 if (this->usesDistanceField()) {
852 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
853 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
854 return false;
855 }
856 }
857
Brian Salomond0a0a652016-12-15 15:25:22 -0500858 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700859 this->joinBounds(*that);
joshualitt5bf99f12015-03-13 11:47:42 -0700860 return true;
861 }
862
Jim Van Verth33632d82017-02-28 10:24:39 -0500863 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700864
Brian Salomond0a0a652016-12-15 15:25:22 -0500865 struct Entry {
Jim Van Verth33632d82017-02-28 10:24:39 -0500866 GrColor fColor;
867 GrShape fShape;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400868 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700869 };
870
Brian Salomond0a0a652016-12-15 15:25:22 -0500871 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400872 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500873 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700874 ShapeCache* fShapeCache;
875 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700876 bool fGammaCorrect;
reed1b55a962015-09-17 20:16:13 -0700877
Brian Salomonfebbd232017-07-11 15:52:02 -0400878 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700879};
880
Jim Van Verth83010462017-03-16 08:45:39 -0400881bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400882 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400883 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700884
jvanverthfa38a302014-10-06 05:59:05 -0700885 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700886 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700887 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700888 if (!fAtlas) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500889 fAtlas = GrDrawOpAtlas::Make(args.fContext->contextPriv().proxyProvider(),
Robert Phillips256c37b2017-03-01 14:32:46 -0500890 kAlpha_8_GrPixelConfig,
891 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
892 NUM_PLOTS_X, NUM_PLOTS_Y,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500893 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400894 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500895 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700896 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700897 return false;
898 }
899 }
900
Brian Salomonfebbd232017-07-11 15:52:02 -0400901 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
902 std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), &fShapeCache,
903 &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
904 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800905
jvanverthfa38a302014-10-06 05:59:05 -0700906 return true;
907}
908
joshualitt21279c72015-05-11 07:21:37 -0700909///////////////////////////////////////////////////////////////////////////////////////////////////
910
Hal Canary6f6961e2017-01-31 13:50:44 -0500911#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700912
Brian Salomonfebbd232017-07-11 15:52:02 -0400913struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700914 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700915 ~PathTestStruct() { this->reset(); }
916
917 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700918 ShapeDataList::Iter iter;
919 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
920 ShapeData* shapeData;
921 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700922 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700923 fShapeList.remove(shapeData);
924 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700925 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500926 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700927 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700928 }
929
Brian Salomon2ee084e2016-12-16 18:59:19 -0500930 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700931 PathTestStruct* dfpr = (PathTestStruct*)pr;
932 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700933 ShapeDataList::Iter iter;
934 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
935 ShapeData* shapeData;
936 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700937 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700938 if (id == shapeData->fID) {
939 dfpr->fShapeCache.remove(shapeData->fKey);
940 dfpr->fShapeList.remove(shapeData);
941 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700942 }
943 }
944 }
945
946 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500947 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700948 ShapeCache fShapeCache;
949 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700950};
951
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500952std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
953 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
962 return GrSmallPathRenderer::SmallPathOp::Make(std::move(paint), shape, viewMatrix, atlas,
963 shapeCache, shapeList, gammaCorrect, stencil);
964
965}
966
Brian Salomonfebbd232017-07-11 15:52:02 -0400967GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
968 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700969 static PathTestStruct gTestStruct;
970
971 if (context->uniqueID() != gTestStruct.fContextID) {
972 gTestStruct.fContextID = context->uniqueID();
973 gTestStruct.reset();
Robert Phillips4bc70112018-03-01 10:24:02 -0500974 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->contextPriv().proxyProvider(),
975 kAlpha_8_GrPixelConfig,
Robert Phillips256c37b2017-03-01 14:32:46 -0500976 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
977 NUM_PLOTS_X, NUM_PLOTS_Y,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500978 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500979 &PathTestStruct::HandleEviction,
980 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700981 }
982
983 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700984 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700985
bsalomonee432412016-06-27 07:18:18 -0700986 // This path renderer only allows fill styles.
987 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500988 return GrSmallPathRenderer::createOp_TestingOnly(
989 std::move(paint), shape, viewMatrix,
990 gTestStruct.fAtlas.get(),
991 &gTestStruct.fShapeCache,
992 &gTestStruct.fShapeList,
993 gammaCorrect,
994 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700995}
996
997#endif