blob: 003d99a34388e29157a1f6305128839029a1115e [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 Osmanc7da1462017-08-17 16:14:25 -040020#include "SkRasterClip.h"
Brian Salomonfebbd232017-07-11 15:52:02 -040021#include "effects/GrBitmapTextGeoProc.h"
22#include "effects/GrDistanceFieldGeoProc.h"
23#include "ops/GrMeshDrawOp.h"
jvanverthfa38a302014-10-06 05:59:05 -070024
jvanverthfb1e2fc2015-09-15 13:11:11 -070025#define ATLAS_TEXTURE_WIDTH 2048
jvanverthb61283f2014-10-30 05:57:21 -070026#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfb1e2fc2015-09-15 13:11:11 -070027#define PLOT_WIDTH 512
reede4ef1ca2015-02-17 18:38:38 -080028#define PLOT_HEIGHT 256
jvanverthfa38a302014-10-06 05:59:05 -070029
30#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
31#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
32
jvanverthb3eb6872014-10-24 07:12:51 -070033#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070034static int g_NumCachedShapes = 0;
35static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070036#endif
37
jvanverthb61283f2014-10-30 05:57:21 -070038// mip levels
Jim Van Verth25b8ca12017-02-17 14:02:13 -050039static const SkScalar kIdealMinMIP = 12;
Jim Van Verth825d4d72018-01-30 20:37:03 +000040static const SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050041
42static const SkScalar kMaxDim = 73;
Jim Van Verth25b8ca12017-02-17 14:02:13 -050043static const SkScalar kMinSize = SK_ScalarHalf;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050044static const SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070045
Robert Phillipsd2e9f762018-03-07 11:54:37 -050046class ShapeDataKey {
47public:
48 ShapeDataKey() {}
49 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
50 ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
51 ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
52
53 ShapeDataKey& operator=(const ShapeDataKey& that) {
54 fKey.reset(that.fKey.count());
55 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
56 return *this;
57 }
58
59 // for SDF paths
60 void set(const GrShape& shape, uint32_t dim) {
61 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
62 // relevant styling information.
63 SkASSERT(shape.style().isSimpleFill());
64 SkASSERT(shape.hasUnstyledKey());
65 int shapeKeySize = shape.unstyledKeySize();
66 fKey.reset(1 + shapeKeySize);
67 fKey[0] = dim;
68 shape.writeUnstyledKey(&fKey[1]);
69 }
70
71 // for bitmap paths
72 void set(const GrShape& shape, const SkMatrix& ctm) {
73 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
74 // relevant styling information.
75 SkASSERT(shape.style().isSimpleFill());
76 SkASSERT(shape.hasUnstyledKey());
77 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
78 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
79 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
80 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
81 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
82 SkScalar tx = ctm.get(SkMatrix::kMTransX);
83 SkScalar ty = ctm.get(SkMatrix::kMTransY);
84 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050085 tx -= SkScalarFloorToScalar(tx);
86 ty -= SkScalarFloorToScalar(ty);
87 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
88 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050089 int shapeKeySize = shape.unstyledKeySize();
90 fKey.reset(5 + shapeKeySize);
91 fKey[0] = SkFloat2Bits(sx);
92 fKey[1] = SkFloat2Bits(sy);
93 fKey[2] = SkFloat2Bits(kx);
94 fKey[3] = SkFloat2Bits(ky);
95 fKey[4] = fracX | (fracY >> 8);
96 shape.writeUnstyledKey(&fKey[5]);
97 }
98
99 bool operator==(const ShapeDataKey& that) const {
100 return fKey.count() == that.fKey.count() &&
101 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
102 }
103
104 int count32() const { return fKey.count(); }
105 const uint32_t* data() const { return fKey.get(); }
106
107private:
108 // The key is composed of the GrShape's key, and either the dimensions of the DF
109 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
110 // the matrix for the path with only fractional translation.
111 SkAutoSTArray<24, uint32_t> fKey;
112};
113
114class ShapeData {
115public:
116 ShapeDataKey fKey;
117 GrDrawOpAtlas::AtlasID fID;
118 SkRect fBounds;
119 GrIRect16 fTextureCoords;
120 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
121
122 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
123 return data.fKey;
124 }
125
Kevin Lubickb5502b22018-03-12 10:17:06 -0400126 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500127 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
128 }
129};
130
131
132
joshualitt5bf99f12015-03-13 11:47:42 -0700133// Callback to clear out internal path cache when eviction occurs
Jim Van Verth83010462017-03-16 08:45:39 -0400134void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
135 GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
joshualitt5bf99f12015-03-13 11:47:42 -0700136 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700137 ShapeDataList::Iter iter;
138 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
139 ShapeData* shapeData;
140 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700141 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700142 if (id == shapeData->fID) {
143 dfpr->fShapeCache.remove(shapeData->fKey);
144 dfpr->fShapeList.remove(shapeData);
145 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700146#ifdef DF_PATH_TRACKING
147 ++g_NumFreedPaths;
148#endif
149 }
150 }
151}
152
jvanverthfa38a302014-10-06 05:59:05 -0700153////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400154GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700155
Jim Van Verth83010462017-03-16 08:45:39 -0400156GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700157 ShapeDataList::Iter iter;
158 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
159 ShapeData* shapeData;
160 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700161 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700162 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700163 }
jvanverthb3eb6872014-10-24 07:12:51 -0700164
165#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700166 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700167#endif
jvanverthfa38a302014-10-06 05:59:05 -0700168}
169
170////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600171GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700172 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600173 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700174 }
175 // If the shape has no key then we won't get any reuse.
176 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600177 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700178 }
179 // This only supports filled paths, however, the caller may apply the style to make a filled
180 // path and try again.
181 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600182 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700183 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500184 // This does non-inverse coverage-based antialiased fills.
185 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600186 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700187 }
jvanverthfa38a302014-10-06 05:59:05 -0700188 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700189 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600190 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700191 }
halcanary9d524f22016-03-29 09:03:52 -0700192
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500193 // Only support paths with bounds within kMaxDim by kMaxDim,
194 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500195 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400196 SkScalar scaleFactors[2] = { 1, 1 };
197 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600198 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500199 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700200 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500201 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700202 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500203 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
204 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600205 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
206 return CanDrawPath::kNo;
207 }
bsalomon6266dca2016-03-11 06:22:00 -0800208
Chris Dalton5ed44232017-09-07 13:22:46 -0600209 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700210}
211
jvanverthfa38a302014-10-06 05:59:05 -0700212////////////////////////////////////////////////////////////////////////////////
213
joshualitt5bf99f12015-03-13 11:47:42 -0700214// padding around path bounds to allow for antialiased pixels
215static const SkScalar kAntiAliasPad = 1.0f;
216
Brian Salomonfebbd232017-07-11 15:52:02 -0400217class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
218private:
219 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
220
joshualitt5bf99f12015-03-13 11:47:42 -0700221public:
Brian Salomon25a88092016-12-01 09:36:50 -0500222 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700223
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500224 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400225 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700226
Brian Salomonfebbd232017-07-11 15:52:02 -0400227 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const GrShape& shape,
228 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas,
229 ShapeCache* shapeCache, ShapeDataList* shapeList,
230 bool gammaCorrect,
231 const GrUserStencilSettings* stencilSettings) {
232 return Helper::FactoryHelper<SmallPathOp>(std::move(paint), shape, viewMatrix, atlas,
233 shapeCache, shapeList, gammaCorrect,
234 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700235 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500236
Brian Salomonfebbd232017-07-11 15:52:02 -0400237 SmallPathOp(Helper::MakeArgs helperArgs, GrColor color, const GrShape& shape,
238 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
239 ShapeDataList* shapeList, bool gammaCorrect,
240 const GrUserStencilSettings* stencilSettings)
241 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500242 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500243 // Compute bounds
244 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
245
Jim Van Verth83010462017-03-16 08:45:39 -0400246#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500247 fUsesDistanceField = true;
248#else
Jim Van Verth83010462017-03-16 08:45:39 -0400249 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500250 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
251#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400252 // always use distance fields if in perspective
253 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500254
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400255 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500256
257 fAtlas = atlas;
258 fShapeCache = shapeCache;
259 fShapeList = shapeList;
260 fGammaCorrect = gammaCorrect;
261
Brian Salomond0a0a652016-12-15 15:25:22 -0500262 }
263
Brian Salomonfebbd232017-07-11 15:52:02 -0400264 const char* name() const override { return "SmallPathOp"; }
265
Robert Phillipsf1748f52017-09-14 14:11:24 -0400266 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400267 fHelper.visitProxies(func);
268
269 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
Robert Phillips4bc70112018-03-01 10:24:02 -0500270 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Brian Salomon9f545bc2017-11-06 10:36:57 -0500271 SkASSERT(proxies[i]);
272 func(proxies[i].get());
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400273 }
274 }
275
Brian Salomonfebbd232017-07-11 15:52:02 -0400276 SkString dumpInfo() const override {
277 SkString string;
278 for (const auto& geo : fShapes) {
279 string.appendf("Color: 0x%08x\n", geo.fColor);
280 }
281 string += fHelper.dumpInfo();
282 string += INHERITED::dumpInfo();
283 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500284 }
285
Brian Salomonfebbd232017-07-11 15:52:02 -0400286 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
287
Brian Osman9a725dd2017-09-20 09:53:22 -0400288 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
289 GrPixelConfigIsClamped dstIsClamped) override {
290 return fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
291 GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonfebbd232017-07-11 15:52:02 -0400292 &fShapes.front().fColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700293 }
294
Brian Salomonfebbd232017-07-11 15:52:02 -0400295private:
bsalomonb5238a72015-05-05 07:49:49 -0700296 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500297 sk_sp<const GrBuffer> fVertexBuffer;
298 sk_sp<const GrBuffer> fIndexBuffer;
bungeman06ca8ec2016-06-09 08:01:03 -0700299 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomonfebbd232017-07-11 15:52:02 -0400300 const GrPipeline* fPipeline;
bsalomonb5238a72015-05-05 07:49:49 -0700301 int fVertexOffset;
302 int fInstancesToFlush;
303 };
304
Brian Salomon91326c32017-08-09 16:02:19 -0400305 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500306 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700307
bsalomon342bfc22016-04-01 06:06:20 -0700308 FlushInfo flushInfo;
Brian Salomonfebbd232017-07-11 15:52:02 -0400309 flushInfo.fPipeline = fHelper.makePipeline(target);
joshualitt5bf99f12015-03-13 11:47:42 -0700310 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400311 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500312 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500313 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400314 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500315 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
316 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
317 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
318
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400319 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500320 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400321 if (ctm.hasPerspective()) {
322 matrix = &ctm;
323 } else if (fHelper.usesLocalCoords()) {
324 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500325 return;
326 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400327 matrix = &invert;
328 } else {
329 matrix = &SkMatrix::I();
330 }
331 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
Robert Phillips4bc70112018-03-01 10:24:02 -0500332 *matrix, fAtlas->getProxies(), fAtlas->numActivePages(),
333 GrSamplerState::ClampBilerp(), flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400334 } else {
335 SkMatrix invert;
336 if (fHelper.usesLocalCoords()) {
337 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400338 return;
339 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500340 }
341
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400342 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Robert Phillips4bc70112018-03-01 10:24:02 -0500343 this->color(), fAtlas->getProxies(), fAtlas->numActivePages(),
344 GrSamplerState::ClampNearest(), kA8_GrMaskFormat, invert,
345 fHelper.usesLocalCoords());
Jim Van Verth33632d82017-02-28 10:24:39 -0500346 }
joshualitt5bf99f12015-03-13 11:47:42 -0700347
joshualitt5bf99f12015-03-13 11:47:42 -0700348 // allocate vertices
bsalomon342bfc22016-04-01 06:06:20 -0700349 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Jim Van Verth33632d82017-02-28 10:24:39 -0500350 SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(GrColor) + 2*sizeof(uint16_t));
bsalomonb5238a72015-05-05 07:49:49 -0700351
cdalton397536c2016-03-25 12:15:03 -0700352 const GrBuffer* vertexBuffer;
bsalomon75398562015-08-17 12:55:38 -0700353 void* vertices = target->makeVertexSpace(vertexStride,
354 kVerticesPerQuad * instanceCount,
355 &vertexBuffer,
356 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -0700357 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400358 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
bsalomonb5238a72015-05-05 07:49:49 -0700359 if (!vertices || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700360 SkDebugf("Could not allocate vertices\n");
361 return;
362 }
363
bsalomonb5238a72015-05-05 07:49:49 -0700364 flushInfo.fInstancesToFlush = 0;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700365 // Pointer to the next set of vertices to write.
366 intptr_t offset = reinterpret_cast<intptr_t>(vertices);
joshualitt5bf99f12015-03-13 11:47:42 -0700367 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500368 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700369
Jim Van Verth33632d82017-02-28 10:24:39 -0500370 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500371 if (fUsesDistanceField) {
372 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400373 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500374 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400375 if (args.fViewMatrix.hasPerspective()) {
376 // approximate the scale since we can't get it from the matrix
377 SkRect xformedBounds;
378 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400379 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
380 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400381 } else {
382 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
383 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500384 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
385 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
386 // In the majority of cases this will yield a crisper rendering.
387 SkScalar mipScale = 1.0f;
388 // Our mipscale is the maxScale clamped to the next highest power of 2
389 if (maxScale <= SK_ScalarHalf) {
390 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
391 mipScale = SkScalarPow(2, -log);
392 } else if (maxScale > SK_Scalar1) {
393 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
394 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700395 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500396 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500397
Jim Van Verth33632d82017-02-28 10:24:39 -0500398 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
399 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
400 // so we can preserve as much detail as possible. However, we can't scale down more
401 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
402 // just bigger than the ideal, and then scale down until we are no more than 4x the
403 // original mipsize.
404 if (mipSize < kIdealMinMIP) {
405 SkScalar newMipSize = mipSize;
406 do {
407 newMipSize *= 2;
408 } while (newMipSize < kIdealMinMIP);
409 while (newMipSize > 4 * mipSize) {
410 newMipSize *= 0.25f;
411 }
412 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700413 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500414 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500415
Jim Van Verth33632d82017-02-28 10:24:39 -0500416 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500417 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500418 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500419 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500420 // Remove the stale cache entry
421 if (shapeData) {
422 fShapeCache->remove(shapeData->fKey);
423 fShapeList->remove(shapeData);
424 delete shapeData;
425 }
426 SkScalar scale = desiredDimension / maxDim;
427
428 shapeData = new ShapeData;
429 if (!this->addDFPathToAtlas(target,
430 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500431 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500432 shapeData,
433 args.fShape,
434 SkScalarCeilToInt(desiredDimension),
435 scale)) {
436 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500437 continue;
438 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500439 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500440 } else {
441 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500442 ShapeDataKey key(args.fShape, args.fViewMatrix);
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
452 shapeData = new ShapeData;
453 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400454 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500455 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400456 shapeData,
457 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400458 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500459 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500460 continue;
461 }
462 }
joshualitt5bf99f12015-03-13 11:47:42 -0700463 }
464
Robert Phillips40a29d72018-01-18 12:59:22 -0500465 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips4bc70112018-03-01 10:24:02 -0500466 fAtlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700467
Robert Phillips4bc70112018-03-01 10:24:02 -0500468 this->writePathVertices(fAtlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800469 offset,
470 args.fColor,
bsalomonb5238a72015-05-05 07:49:49 -0700471 vertexStride,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400472 args.fViewMatrix,
bsalomonee432412016-06-27 07:18:18 -0700473 shapeData);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700474 offset += kVerticesPerQuad * vertexStride;
bsalomonb5238a72015-05-05 07:49:49 -0700475 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700476 }
477
bsalomon75398562015-08-17 12:55:38 -0700478 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700479 }
480
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500481 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
482 int width, int height, const void* image,
483 GrDrawOpAtlas::AtlasID* id, SkIPoint16* atlasLocation) const {
484 auto resourceProvider = target->resourceProvider();
485 auto uploadTarget = target->deferredUploadTarget();
486
487 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, id,
488 uploadTarget, width, height,
489 image, atlasLocation);
490 if (GrDrawOpAtlas::ErrorCode::kError == code) {
491 return false;
492 }
493
494 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
495 this->flush(target, flushInfo);
496
497 code = atlas->addToAtlas(resourceProvider, id, uploadTarget, width, height,
498 image, atlasLocation);
499 }
500
501 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
502 }
503
Brian Salomone5b399e2017-07-19 13:50:54 -0400504 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400505 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
506 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500507
bsalomonee432412016-06-27 07:18:18 -0700508 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700509
510 // generate bounding rect for bitmap draw
511 SkRect scaledBounds = bounds;
512 // scale to mip level size
513 scaledBounds.fLeft *= scale;
514 scaledBounds.fTop *= scale;
515 scaledBounds.fRight *= scale;
516 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500517 // subtract out integer portion of origin
518 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500519 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
520 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700521 scaledBounds.offset(-dx, -dy);
522 // get integer boundary
523 SkIRect devPathBounds;
524 scaledBounds.roundOut(&devPathBounds);
525 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800526 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500527 // place devBounds at origin
528 int width = devPathBounds.width() + 2*intPad;
529 int height = devPathBounds.height() + 2*intPad;
530 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400531 SkScalar translateX = intPad - dx;
532 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700533
534 // draw path to bitmap
535 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500536 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400537 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700538
jvanverth512e4372015-11-23 11:50:02 -0800539 SkASSERT(devPathBounds.fLeft == 0);
540 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500541 SkASSERT(devPathBounds.width() > 0);
542 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700543
joel.liang8cbb4242017-01-09 18:39:43 -0800544 // setup signed distance field storage
545 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
546 width = dfBounds.width();
547 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800548 // TODO We should really generate this directly into the plot somehow
549 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800550
joel.liang8cbb4242017-01-09 18:39:43 -0800551 SkPath path;
552 shape.asPath(&path);
553#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
554 // Generate signed distance field directly from SkPath
555 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
556 path, drawMatrix,
557 width, height, width * sizeof(unsigned char));
558 if (!succeed) {
559#endif
560 // setup bitmap backing
561 SkAutoPixmapStorage dst;
562 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
563 devPathBounds.height()))) {
564 return false;
565 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400566 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800567
568 // rasterize path
569 SkPaint paint;
570 paint.setStyle(SkPaint::kFill_Style);
571 paint.setAntiAlias(true);
572
573 SkDraw draw;
574 sk_bzero(&draw, sizeof(draw));
575
576 SkRasterClip rasterClip;
577 rasterClip.setRect(devPathBounds);
578 draw.fRC = &rasterClip;
579 draw.fMatrix = &drawMatrix;
580 draw.fDst = dst;
581
582 draw.drawPathCoverage(path, paint);
583
584 // Generate signed distance field
585 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
586 (const unsigned char*)dst.addr(),
587 dst.width(), dst.height(), dst.rowBytes());
588#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
589 }
590#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700591
592 // add to atlas
593 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500594 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500595
596 if (!this->addToAtlas(target, flushInfo, atlas,
597 width, height, dfStorage.get(), &id, &atlasLocation)) {
598 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700599 }
600
601 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700602 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700603 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500604
Robert Phillips3cf781d2017-08-22 18:25:11 -0400605 shapeData->fBounds = SkRect::Make(devPathBounds);
606 shapeData->fBounds.offset(-translateX, -translateY);
607 shapeData->fBounds.fLeft /= scale;
608 shapeData->fBounds.fTop /= scale;
609 shapeData->fBounds.fRight /= scale;
610 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500611
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400612 // We pack the 2bit page index in the low bit of the u and v texture coords
613 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
614 SkASSERT(pageIndex < 4);
615 uint16_t uBit = (pageIndex >> 1) & 0x1;
616 uint16_t vBit = pageIndex & 0x1;
617 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
618 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400619 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400620 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400621 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400622 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700623
bsalomonee432412016-06-27 07:18:18 -0700624 fShapeCache->add(shapeData);
625 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700626#ifdef DF_PATH_TRACKING
627 ++g_NumCachedPaths;
628#endif
629 return true;
630 }
631
Brian Salomone5b399e2017-07-19 13:50:54 -0400632 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400633 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
634 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500635 const SkRect& bounds = shape.bounds();
636 if (bounds.isEmpty()) {
637 return false;
638 }
639 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500640 SkScalar tx = ctm.getTranslateX();
641 SkScalar ty = ctm.getTranslateY();
642 tx -= SkScalarFloorToScalar(tx);
643 ty -= SkScalarFloorToScalar(ty);
644 drawMatrix.set(SkMatrix::kMTransX, tx);
645 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500646 SkRect shapeDevBounds;
647 drawMatrix.mapRect(&shapeDevBounds, bounds);
648 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
649 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
650
651 // get integer boundary
652 SkIRect devPathBounds;
653 shapeDevBounds.roundOut(&devPathBounds);
654 // pad to allow room for antialiasing
655 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
656 // place devBounds at origin
657 int width = devPathBounds.width() + 2 * intPad;
658 int height = devPathBounds.height() + 2 * intPad;
659 devPathBounds = SkIRect::MakeWH(width, height);
660 SkScalar translateX = intPad - dx;
661 SkScalar translateY = intPad - dy;
662
663 SkASSERT(devPathBounds.fLeft == 0);
664 SkASSERT(devPathBounds.fTop == 0);
665 SkASSERT(devPathBounds.width() > 0);
666 SkASSERT(devPathBounds.height() > 0);
667
668 SkPath path;
669 shape.asPath(&path);
670 // setup bitmap backing
671 SkAutoPixmapStorage dst;
672 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
673 devPathBounds.height()))) {
674 return false;
675 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400676 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500677
678 // rasterize path
679 SkPaint paint;
680 paint.setStyle(SkPaint::kFill_Style);
681 paint.setAntiAlias(true);
682
683 SkDraw draw;
684 sk_bzero(&draw, sizeof(draw));
685
686 SkRasterClip rasterClip;
687 rasterClip.setRect(devPathBounds);
688 draw.fRC = &rasterClip;
689 drawMatrix.postTranslate(translateX, translateY);
690 draw.fMatrix = &drawMatrix;
691 draw.fDst = dst;
692
693 draw.drawPathCoverage(path, paint);
694
695 // add to atlas
696 SkIPoint16 atlasLocation;
697 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500698
699 if (!this->addToAtlas(target, flushInfo, atlas,
700 dst.width(), dst.height(), dst.addr(), &id, &atlasLocation)) {
701 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500702 }
703
704 // add to cache
705 shapeData->fKey.set(shape, ctm);
706 shapeData->fID = id;
707
Jim Van Verth33632d82017-02-28 10:24:39 -0500708 shapeData->fBounds = SkRect::Make(devPathBounds);
709 shapeData->fBounds.offset(-translateX, -translateY);
710
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400711 // We pack the 2bit page index in the low bit of the u and v texture coords
712 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
713 SkASSERT(pageIndex < 4);
714 uint16_t uBit = (pageIndex >> 1) & 0x1;
715 uint16_t vBit = pageIndex & 0x1;
716 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
717 (atlasLocation.fX+width) << 1 | uBit,
718 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500719
720 fShapeCache->add(shapeData);
721 fShapeList->addToTail(shapeData);
722#ifdef DF_PATH_TRACKING
723 ++g_NumCachedPaths;
724#endif
725 return true;
726 }
727
Brian Salomon29b60c92017-10-31 14:42:10 -0400728 void writePathVertices(GrDrawOpAtlas* atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800729 intptr_t offset,
730 GrColor color,
bsalomonb5238a72015-05-05 07:49:49 -0700731 size_t vertexStride,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400732 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700733 const ShapeData* shapeData) const {
joshualitt53f26aa2015-12-10 07:29:54 -0800734 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
735
Jim Van Verth77047542017-01-11 14:17:00 -0500736 SkRect bounds = shapeData->fBounds;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400737 SkRect translatedBounds(bounds);
738 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500739 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
740 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400741 }
Jim Van Verth77047542017-01-11 14:17:00 -0500742
joshualitt5bf99f12015-03-13 11:47:42 -0700743 // vertex positions
744 // TODO make the vertex attributes a struct
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400745 if (fUsesDistanceField && !ctm.hasPerspective()) {
Brian Salomon57caa662017-10-18 12:21:05 +0000746 GrQuad quad;
747 quad.setFromMappedRect(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 {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500761 SkPointPriv::SetRectTriStrip(positions,
762 translatedBounds.left(),
763 translatedBounds.top(),
764 translatedBounds.right(),
765 translatedBounds.bottom(),
766 vertexStride);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400767 }
joshualitt5bf99f12015-03-13 11:47:42 -0700768
joshualitt53f26aa2015-12-10 07:29:54 -0800769 // colors
770 for (int i = 0; i < kVerticesPerQuad; i++) {
771 GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride);
772 *colorPtr = color;
773 }
774
Jim Van Verth77047542017-01-11 14:17:00 -0500775 // set up texture coordinates
Robert Phillips8296e752017-08-25 08:45:21 -0400776 uint16_t l = shapeData->fTextureCoords.fLeft;
777 uint16_t t = shapeData->fTextureCoords.fTop;
778 uint16_t r = shapeData->fTextureCoords.fRight;
779 uint16_t b = shapeData->fTextureCoords.fBottom;
Jim Van Verth33632d82017-02-28 10:24:39 -0500780
781 // set vertex texture coords
782 intptr_t textureCoordOffset = offset + sizeof(SkPoint) + sizeof(GrColor);
783 uint16_t* textureCoords = (uint16_t*) textureCoordOffset;
784 textureCoords[0] = l;
785 textureCoords[1] = t;
786 textureCoordOffset += vertexStride;
787 textureCoords = (uint16_t*)textureCoordOffset;
788 textureCoords[0] = l;
789 textureCoords[1] = b;
790 textureCoordOffset += vertexStride;
791 textureCoords = (uint16_t*)textureCoordOffset;
792 textureCoords[0] = r;
Brian Salomon57caa662017-10-18 12:21:05 +0000793 textureCoords[1] = t;
Jim Van Verth33632d82017-02-28 10:24:39 -0500794 textureCoordOffset += vertexStride;
795 textureCoords = (uint16_t*)textureCoordOffset;
796 textureCoords[0] = r;
Brian Salomon57caa662017-10-18 12:21:05 +0000797 textureCoords[1] = b;
joshualitt5bf99f12015-03-13 11:47:42 -0700798 }
799
Brian Salomone5b399e2017-07-19 13:50:54 -0400800 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400801 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
Robert Phillips4bc70112018-03-01 10:24:02 -0500802 if (gp->numTextureSamplers() != (int)fAtlas->numActivePages()) {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400803 // During preparation the number of atlas pages has increased.
804 // Update the proxies used in the GP to match.
805 if (fUsesDistanceField) {
806 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500807 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400808 } else {
809 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500810 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400811 }
812 }
813
bsalomon6d6b6ad2016-07-13 14:45:28 -0700814 if (flushInfo->fInstancesToFlush) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600815 GrMesh mesh(GrPrimitiveType::kTriangles);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700816 int maxInstancesPerDraw =
817 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Daltonbca46e22017-05-15 11:03:26 -0600818 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerQuad,
Chris Dalton114a3c02017-05-26 15:17:19 -0600819 kVerticesPerQuad, flushInfo->fInstancesToFlush,
820 maxInstancesPerDraw);
821 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomonfebbd232017-07-11 15:52:02 -0400822 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700823 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
824 flushInfo->fInstancesToFlush = 0;
825 }
joshualitt5bf99f12015-03-13 11:47:42 -0700826 }
827
Brian Salomond0a0a652016-12-15 15:25:22 -0500828 GrColor color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500829 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700830
Brian Salomon25a88092016-12-01 09:36:50 -0500831 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400832 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400833 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt8cab9a72015-07-16 09:13:50 -0700834 return false;
835 }
836
Jim Van Verth33632d82017-02-28 10:24:39 -0500837 if (this->usesDistanceField() != that->usesDistanceField()) {
838 return false;
839 }
840
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400841 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
842 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
843
844 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700845 return false;
846 }
847
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400848 // We can position on the cpu unless we're in perspective,
849 // but also need to make sure local matrices are identical
850 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
851 !thisCtm.cheapEqualTo(thatCtm)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500852 return false;
853 }
854
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400855 // Depending on the ctm we may have a different shader for SDF paths
856 if (this->usesDistanceField()) {
857 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
858 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
859 return false;
860 }
861 }
862
Brian Salomond0a0a652016-12-15 15:25:22 -0500863 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700864 this->joinBounds(*that);
joshualitt5bf99f12015-03-13 11:47:42 -0700865 return true;
866 }
867
Jim Van Verth33632d82017-02-28 10:24:39 -0500868 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700869
Brian Salomond0a0a652016-12-15 15:25:22 -0500870 struct Entry {
Jim Van Verth33632d82017-02-28 10:24:39 -0500871 GrColor fColor;
872 GrShape fShape;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400873 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700874 };
875
Brian Salomond0a0a652016-12-15 15:25:22 -0500876 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400877 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500878 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700879 ShapeCache* fShapeCache;
880 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700881 bool fGammaCorrect;
reed1b55a962015-09-17 20:16:13 -0700882
Brian Salomonfebbd232017-07-11 15:52:02 -0400883 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700884};
885
Jim Van Verth83010462017-03-16 08:45:39 -0400886bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400887 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400888 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700889
jvanverthfa38a302014-10-06 05:59:05 -0700890 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700891 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700892 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700893 if (!fAtlas) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500894 fAtlas = GrDrawOpAtlas::Make(args.fContext->contextPriv().proxyProvider(),
Robert Phillips256c37b2017-03-01 14:32:46 -0500895 kAlpha_8_GrPixelConfig,
896 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
897 NUM_PLOTS_X, NUM_PLOTS_Y,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500898 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400899 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500900 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700901 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700902 return false;
903 }
904 }
905
Brian Salomonfebbd232017-07-11 15:52:02 -0400906 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
907 std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), &fShapeCache,
908 &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
909 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800910
jvanverthfa38a302014-10-06 05:59:05 -0700911 return true;
912}
913
joshualitt21279c72015-05-11 07:21:37 -0700914///////////////////////////////////////////////////////////////////////////////////////////////////
915
Hal Canary6f6961e2017-01-31 13:50:44 -0500916#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700917
Brian Salomonfebbd232017-07-11 15:52:02 -0400918struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700919 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700920 ~PathTestStruct() { this->reset(); }
921
922 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700923 ShapeDataList::Iter iter;
924 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
925 ShapeData* shapeData;
926 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700927 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700928 fShapeList.remove(shapeData);
929 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700930 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500931 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700932 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700933 }
934
Brian Salomon2ee084e2016-12-16 18:59:19 -0500935 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700936 PathTestStruct* dfpr = (PathTestStruct*)pr;
937 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700938 ShapeDataList::Iter iter;
939 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
940 ShapeData* shapeData;
941 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700942 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700943 if (id == shapeData->fID) {
944 dfpr->fShapeCache.remove(shapeData->fKey);
945 dfpr->fShapeList.remove(shapeData);
946 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700947 }
948 }
949 }
950
951 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500952 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700953 ShapeCache fShapeCache;
954 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700955};
956
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500957std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
958 GrPaint&& paint,
959 const GrShape& shape,
960 const SkMatrix& viewMatrix,
961 GrDrawOpAtlas* atlas,
962 ShapeCache* shapeCache,
963 ShapeDataList* shapeList,
964 bool gammaCorrect,
965 const GrUserStencilSettings* stencil) {
966
967 return GrSmallPathRenderer::SmallPathOp::Make(std::move(paint), shape, viewMatrix, atlas,
968 shapeCache, shapeList, gammaCorrect, stencil);
969
970}
971
Brian Salomonfebbd232017-07-11 15:52:02 -0400972GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
973 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700974 static PathTestStruct gTestStruct;
975
976 if (context->uniqueID() != gTestStruct.fContextID) {
977 gTestStruct.fContextID = context->uniqueID();
978 gTestStruct.reset();
Robert Phillips4bc70112018-03-01 10:24:02 -0500979 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->contextPriv().proxyProvider(),
980 kAlpha_8_GrPixelConfig,
Robert Phillips256c37b2017-03-01 14:32:46 -0500981 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
982 NUM_PLOTS_X, NUM_PLOTS_Y,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500983 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500984 &PathTestStruct::HandleEviction,
985 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700986 }
987
988 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700989 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700990
bsalomonee432412016-06-27 07:18:18 -0700991 // This path renderer only allows fill styles.
992 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500993 return GrSmallPathRenderer::createOp_TestingOnly(
994 std::move(paint), shape, viewMatrix,
995 gTestStruct.fAtlas.get(),
996 &gTestStruct.fShapeCache,
997 &gTestStruct.fShapeList,
998 gammaCorrect,
999 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -07001000}
1001
1002#endif