blob: f148ce06ae52b9730e24c8a3d2a0e189daec4027 [file] [log] [blame]
jvanverthfa38a302014-10-06 05:59:05 -07001/*
2 * Copyright 2014 Google Inc.
joel.liang8cbb4242017-01-09 18:39:43 -08003 * Copyright 2017 ARM Ltd.
jvanverthfa38a302014-10-06 05:59:05 -07004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/ops/GrSmallPathRenderer.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPaint.h"
12#include "src/core/SkAutoMalloc.h"
13#include "src/core/SkAutoPixmapStorage.h"
14#include "src/core/SkDistanceFieldGen.h"
15#include "src/core/SkDraw.h"
16#include "src/core/SkPointPriv.h"
17#include "src/core/SkRasterClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrBuffer.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrDistanceFieldGenFromVector.h"
22#include "src/gpu/GrDrawOpTest.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRenderTargetContext.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/GrVertexWriter.h"
26#include "src/gpu/effects/GrBitmapTextGeoProc.h"
27#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040028#include "src/gpu/geometry/GrQuad.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrMeshDrawOp.h"
30#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
jvanverthfa38a302014-10-06 05:59:05 -070031
jvanverthfb1e2fc2015-09-15 13:11:11 -070032#define ATLAS_TEXTURE_WIDTH 2048
jvanverthb61283f2014-10-30 05:57:21 -070033#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfb1e2fc2015-09-15 13:11:11 -070034#define PLOT_WIDTH 512
reede4ef1ca2015-02-17 18:38:38 -080035#define PLOT_HEIGHT 256
jvanverthfa38a302014-10-06 05:59:05 -070036
37#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
38#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
39
jvanverthb3eb6872014-10-24 07:12:51 -070040#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070041static int g_NumCachedShapes = 0;
42static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070043#endif
44
jvanverthb61283f2014-10-30 05:57:21 -070045// mip levels
Jim Van Verth25b8ca12017-02-17 14:02:13 -050046static const SkScalar kIdealMinMIP = 12;
Jim Van Verth825d4d72018-01-30 20:37:03 +000047static const SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050048
49static const SkScalar kMaxDim = 73;
Jim Van Verth25b8ca12017-02-17 14:02:13 -050050static const SkScalar kMinSize = SK_ScalarHalf;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050051static const SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070052
Robert Phillipsd2e9f762018-03-07 11:54:37 -050053class ShapeDataKey {
54public:
55 ShapeDataKey() {}
56 ShapeDataKey(const ShapeDataKey& that) { *this = that; }
57 ShapeDataKey(const GrShape& shape, uint32_t dim) { this->set(shape, dim); }
58 ShapeDataKey(const GrShape& shape, const SkMatrix& ctm) { this->set(shape, ctm); }
59
60 ShapeDataKey& operator=(const ShapeDataKey& that) {
61 fKey.reset(that.fKey.count());
62 memcpy(fKey.get(), that.fKey.get(), fKey.count() * sizeof(uint32_t));
63 return *this;
64 }
65
66 // for SDF paths
67 void set(const GrShape& shape, uint32_t dim) {
68 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
69 // relevant styling information.
70 SkASSERT(shape.style().isSimpleFill());
71 SkASSERT(shape.hasUnstyledKey());
72 int shapeKeySize = shape.unstyledKeySize();
73 fKey.reset(1 + shapeKeySize);
74 fKey[0] = dim;
75 shape.writeUnstyledKey(&fKey[1]);
76 }
77
78 // for bitmap paths
79 void set(const GrShape& shape, const SkMatrix& ctm) {
80 // Shapes' keys are for their pre-style geometry, but by now we shouldn't have any
81 // relevant styling information.
82 SkASSERT(shape.style().isSimpleFill());
83 SkASSERT(shape.hasUnstyledKey());
84 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
85 SkScalar sx = ctm.get(SkMatrix::kMScaleX);
86 SkScalar sy = ctm.get(SkMatrix::kMScaleY);
87 SkScalar kx = ctm.get(SkMatrix::kMSkewX);
88 SkScalar ky = ctm.get(SkMatrix::kMSkewY);
89 SkScalar tx = ctm.get(SkMatrix::kMTransX);
90 SkScalar ty = ctm.get(SkMatrix::kMTransY);
91 // Allow 8 bits each in x and y of subpixel positioning.
Jim Van Vertha64a5852018-03-09 14:16:31 -050092 tx -= SkScalarFloorToScalar(tx);
93 ty -= SkScalarFloorToScalar(ty);
94 SkFixed fracX = SkScalarToFixed(tx) & 0x0000FF00;
95 SkFixed fracY = SkScalarToFixed(ty) & 0x0000FF00;
Robert Phillipsd2e9f762018-03-07 11:54:37 -050096 int shapeKeySize = shape.unstyledKeySize();
97 fKey.reset(5 + shapeKeySize);
98 fKey[0] = SkFloat2Bits(sx);
99 fKey[1] = SkFloat2Bits(sy);
100 fKey[2] = SkFloat2Bits(kx);
101 fKey[3] = SkFloat2Bits(ky);
102 fKey[4] = fracX | (fracY >> 8);
103 shape.writeUnstyledKey(&fKey[5]);
104 }
105
106 bool operator==(const ShapeDataKey& that) const {
107 return fKey.count() == that.fKey.count() &&
108 0 == memcmp(fKey.get(), that.fKey.get(), sizeof(uint32_t) * fKey.count());
109 }
110
111 int count32() const { return fKey.count(); }
112 const uint32_t* data() const { return fKey.get(); }
113
114private:
115 // The key is composed of the GrShape's key, and either the dimensions of the DF
116 // generated for the path (32x32 max, 64x64 max, 128x128 max) if an SDF image or
117 // the matrix for the path with only fractional translation.
118 SkAutoSTArray<24, uint32_t> fKey;
119};
120
121class ShapeData {
122public:
123 ShapeDataKey fKey;
124 GrDrawOpAtlas::AtlasID fID;
125 SkRect fBounds;
126 GrIRect16 fTextureCoords;
127 SK_DECLARE_INTERNAL_LLIST_INTERFACE(ShapeData);
128
129 static inline const ShapeDataKey& GetKey(const ShapeData& data) {
130 return data.fKey;
131 }
132
Kevin Lubickb5502b22018-03-12 10:17:06 -0400133 static inline uint32_t Hash(const ShapeDataKey& key) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500134 return SkOpts::hash(key.data(), sizeof(uint32_t) * key.count32());
135 }
136};
137
138
139
joshualitt5bf99f12015-03-13 11:47:42 -0700140// Callback to clear out internal path cache when eviction occurs
Jim Van Verth83010462017-03-16 08:45:39 -0400141void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
142 GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
joshualitt5bf99f12015-03-13 11:47:42 -0700143 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700144 ShapeDataList::Iter iter;
145 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
146 ShapeData* shapeData;
147 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -0700148 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700149 if (id == shapeData->fID) {
150 dfpr->fShapeCache.remove(shapeData->fKey);
151 dfpr->fShapeList.remove(shapeData);
152 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700153#ifdef DF_PATH_TRACKING
154 ++g_NumFreedPaths;
155#endif
156 }
157 }
158}
159
jvanverthfa38a302014-10-06 05:59:05 -0700160////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -0400161GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -0700162
Jim Van Verth83010462017-03-16 08:45:39 -0400163GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -0700164 ShapeDataList::Iter iter;
165 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
166 ShapeData* shapeData;
167 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -0700168 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700169 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -0700170 }
jvanverthb3eb6872014-10-24 07:12:51 -0700171
172#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -0700173 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -0700174#endif
jvanverthfa38a302014-10-06 05:59:05 -0700175}
176
177////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -0600178GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700179 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600180 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700181 }
182 // If the shape has no key then we won't get any reuse.
183 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600184 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700185 }
186 // This only supports filled paths, however, the caller may apply the style to make a filled
187 // path and try again.
188 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600189 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -0700190 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500191 // This does non-inverse coverage-based antialiased fills.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600192 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600193 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700194 }
jvanverthfa38a302014-10-06 05:59:05 -0700195 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700196 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600197 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700198 }
halcanary9d524f22016-03-29 09:03:52 -0700199
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500200 // Only support paths with bounds within kMaxDim by kMaxDim,
201 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500202 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400203 SkScalar scaleFactors[2] = { 1, 1 };
204 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600205 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500206 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700207 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500208 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700209 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500210 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
211 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600212 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
213 return CanDrawPath::kNo;
214 }
bsalomon6266dca2016-03-11 06:22:00 -0800215
Chris Dalton5ed44232017-09-07 13:22:46 -0600216 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700217}
218
jvanverthfa38a302014-10-06 05:59:05 -0700219////////////////////////////////////////////////////////////////////////////////
220
joshualitt5bf99f12015-03-13 11:47:42 -0700221// padding around path bounds to allow for antialiased pixels
222static const SkScalar kAntiAliasPad = 1.0f;
223
Brian Salomonfebbd232017-07-11 15:52:02 -0400224class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
225private:
226 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
227
joshualitt5bf99f12015-03-13 11:47:42 -0700228public:
Brian Salomon25a88092016-12-01 09:36:50 -0500229 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700230
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500231 using ShapeCache = SkTDynamicHash<ShapeData, ShapeDataKey>;
Jim Van Verth83010462017-03-16 08:45:39 -0400232 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700233
Robert Phillipsb97da532019-02-12 15:24:12 -0500234 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400235 GrPaint&& paint,
236 const GrShape& shape,
237 const SkMatrix& viewMatrix,
238 GrDrawOpAtlas* atlas,
239 ShapeCache* shapeCache,
240 ShapeDataList* shapeList,
Brian Salomonfebbd232017-07-11 15:52:02 -0400241 bool gammaCorrect,
242 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400243 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
244 atlas, shapeCache, shapeList, gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400245 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700246 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500247
Brian Osmancf860852018-10-31 14:04:39 -0400248 SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrShape& shape,
Brian Salomonfebbd232017-07-11 15:52:02 -0400249 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
250 ShapeDataList* shapeList, bool gammaCorrect,
251 const GrUserStencilSettings* stencilSettings)
252 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500253 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500254 // Compute bounds
255 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
256
Jim Van Verth83010462017-03-16 08:45:39 -0400257#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500258 fUsesDistanceField = true;
259#else
Jim Van Verth83010462017-03-16 08:45:39 -0400260 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500261 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
262#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400263 // always use distance fields if in perspective
264 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500265
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400266 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500267
268 fAtlas = atlas;
269 fShapeCache = shapeCache;
270 fShapeList = shapeList;
271 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500272 }
273
Brian Salomonfebbd232017-07-11 15:52:02 -0400274 const char* name() const override { return "SmallPathOp"; }
275
Chris Dalton1706cbf2019-05-21 19:35:29 -0600276 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400277 fHelper.visitProxies(func);
278
279 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
Robert Phillips4bc70112018-03-01 10:24:02 -0500280 for (uint32_t i = 0; i < fAtlas->numActivePages(); ++i) {
Brian Salomon9f545bc2017-11-06 10:36:57 -0500281 SkASSERT(proxies[i]);
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600282 func(proxies[i].get(), GrMipMapped::kNo);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400283 }
284 }
285
Brian Osman9a390ac2018-11-12 09:47:48 -0500286#ifdef SK_DEBUG
Brian Salomonfebbd232017-07-11 15:52:02 -0400287 SkString dumpInfo() const override {
288 SkString string;
289 for (const auto& geo : fShapes) {
Brian Osmancf860852018-10-31 14:04:39 -0400290 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
Brian Salomonfebbd232017-07-11 15:52:02 -0400291 }
292 string += fHelper.dumpInfo();
293 string += INHERITED::dumpInfo();
294 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500295 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500296#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500297
Brian Salomonfebbd232017-07-11 15:52:02 -0400298 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
299
Chris Dalton6ce447a2019-06-23 18:07:38 -0600300 GrProcessorSet::Analysis finalize(
301 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
302 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700303 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600304 caps, clip, hasMixedSampledCoverage, clampType,
305 GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700306 }
307
Brian Salomonfebbd232017-07-11 15:52:02 -0400308private:
bsalomonb5238a72015-05-05 07:49:49 -0700309 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500310 sk_sp<const GrBuffer> fVertexBuffer;
311 sk_sp<const GrBuffer> fIndexBuffer;
bungeman06ca8ec2016-06-09 08:01:03 -0700312 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000313 GrPipeline::FixedDynamicState* fFixedDynamicState;
bsalomonb5238a72015-05-05 07:49:49 -0700314 int fVertexOffset;
315 int fInstancesToFlush;
316 };
317
Brian Salomon91326c32017-08-09 16:02:19 -0400318 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500319 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700320
Brian Salomon7eae3e02018-08-07 14:02:38 +0000321 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
322 GR_STATIC_ASSERT(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
323
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700324 FlushInfo flushInfo;
325 flushInfo.fFixedDynamicState = target->makeFixedDynamicState(kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000326 int numActiveProxies = fAtlas->numActivePages();
327 const auto proxies = fAtlas->getProxies();
328 for (int i = 0; i < numActiveProxies; ++i) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700329 flushInfo.fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000330 }
Brian Salomon49348902018-06-26 09:12:38 -0400331
joshualitt5bf99f12015-03-13 11:47:42 -0700332 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400333 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500334 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500335 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400336 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500337 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
338 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
339 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
340
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400341 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500342 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400343 if (ctm.hasPerspective()) {
344 matrix = &ctm;
345 } else if (fHelper.usesLocalCoords()) {
346 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500347 return;
348 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400349 matrix = &invert;
350 } else {
351 matrix = &SkMatrix::I();
352 }
353 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
Brian Osmanc906d252018-12-04 11:17:46 -0500354 *target->caps().shaderCaps(), *matrix, fWideColor, fAtlas->getProxies(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400355 fAtlas->numActivePages(), GrSamplerState::ClampBilerp(), flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400356 } else {
357 SkMatrix invert;
358 if (fHelper.usesLocalCoords()) {
359 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400360 return;
361 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500362 }
363
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400364 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Brian Osmanc906d252018-12-04 11:17:46 -0500365 *target->caps().shaderCaps(), this->color(), fWideColor, fAtlas->getProxies(),
Brian Osman4a3f5c82018-09-18 16:16:38 -0400366 fAtlas->numActivePages(), GrSamplerState::ClampNearest(), kA8_GrMaskFormat,
367 invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500368 }
joshualitt5bf99f12015-03-13 11:47:42 -0700369
joshualitt5bf99f12015-03-13 11:47:42 -0700370 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500371 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400372
373 // We need to make sure we don't overflow a 32 bit int when we request space in the
374 // makeVertexSpace call below.
375 if (instanceCount > SK_MaxS32 / kVerticesPerQuad) {
376 return;
377 }
Brian Osman0dd43022018-11-16 15:53:26 -0500378 GrVertexWriter vertices{target->makeVertexSpace(kVertexStride,
379 kVerticesPerQuad * instanceCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500380 &flushInfo.fVertexBuffer,
Brian Osman0dd43022018-11-16 15:53:26 -0500381 &flushInfo.fVertexOffset)};
Brian Salomond28a79d2017-10-16 13:01:07 -0400382 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500383 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700384 SkDebugf("Could not allocate vertices\n");
385 return;
386 }
387
bsalomonb5238a72015-05-05 07:49:49 -0700388 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700389 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500390 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700391
Jim Van Verth33632d82017-02-28 10:24:39 -0500392 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500393 if (fUsesDistanceField) {
394 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400395 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500396 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400397 if (args.fViewMatrix.hasPerspective()) {
398 // approximate the scale since we can't get it from the matrix
399 SkRect xformedBounds;
400 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400401 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
402 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400403 } else {
404 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
405 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500406 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
407 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
408 // In the majority of cases this will yield a crisper rendering.
409 SkScalar mipScale = 1.0f;
410 // Our mipscale is the maxScale clamped to the next highest power of 2
411 if (maxScale <= SK_ScalarHalf) {
412 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
413 mipScale = SkScalarPow(2, -log);
414 } else if (maxScale > SK_Scalar1) {
415 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
416 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700417 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500418 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500419
Jim Van Verth33632d82017-02-28 10:24:39 -0500420 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
421 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
422 // so we can preserve as much detail as possible. However, we can't scale down more
423 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
424 // just bigger than the ideal, and then scale down until we are no more than 4x the
425 // original mipsize.
426 if (mipSize < kIdealMinMIP) {
427 SkScalar newMipSize = mipSize;
428 do {
429 newMipSize *= 2;
430 } while (newMipSize < kIdealMinMIP);
431 while (newMipSize > 4 * mipSize) {
432 newMipSize *= 0.25f;
433 }
434 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700435 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500436 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500437
Jim Van Verth33632d82017-02-28 10:24:39 -0500438 // check to see if df path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500439 ShapeDataKey key(args.fShape, SkScalarCeilToInt(desiredDimension));
Jim Van Verth33632d82017-02-28 10:24:39 -0500440 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500441 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500442 // Remove the stale cache entry
443 if (shapeData) {
444 fShapeCache->remove(shapeData->fKey);
445 fShapeList->remove(shapeData);
446 delete shapeData;
447 }
448 SkScalar scale = desiredDimension / maxDim;
449
450 shapeData = new ShapeData;
451 if (!this->addDFPathToAtlas(target,
452 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500453 fAtlas,
Jim Van Verth33632d82017-02-28 10:24:39 -0500454 shapeData,
455 args.fShape,
456 SkScalarCeilToInt(desiredDimension),
457 scale)) {
458 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500459 continue;
460 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500461 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500462 } else {
463 // check to see if bitmap path is cached
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500464 ShapeDataKey key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500465 shapeData = fShapeCache->find(key);
Robert Phillips4bc70112018-03-01 10:24:02 -0500466 if (nullptr == shapeData || !fAtlas->hasID(shapeData->fID)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500467 // Remove the stale cache entry
468 if (shapeData) {
469 fShapeCache->remove(shapeData->fKey);
470 fShapeList->remove(shapeData);
471 delete shapeData;
472 }
473
474 shapeData = new ShapeData;
475 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400476 &flushInfo,
Robert Phillips4bc70112018-03-01 10:24:02 -0500477 fAtlas,
Robert Phillips8296e752017-08-25 08:45:21 -0400478 shapeData,
479 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400480 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500481 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500482 continue;
483 }
484 }
joshualitt5bf99f12015-03-13 11:47:42 -0700485 }
486
Robert Phillips40a29d72018-01-18 12:59:22 -0500487 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips4bc70112018-03-01 10:24:02 -0500488 fAtlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700489
Brian Osmanc906d252018-12-04 11:17:46 -0500490 this->writePathVertices(fAtlas, vertices, GrVertexColor(args.fColor, fWideColor),
491 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700492 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700493 }
494
bsalomon75398562015-08-17 12:55:38 -0700495 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700496 }
497
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500498 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
499 int width, int height, const void* image,
500 GrDrawOpAtlas::AtlasID* id, SkIPoint16* atlasLocation) const {
501 auto resourceProvider = target->resourceProvider();
502 auto uploadTarget = target->deferredUploadTarget();
503
504 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, id,
505 uploadTarget, width, height,
506 image, atlasLocation);
507 if (GrDrawOpAtlas::ErrorCode::kError == code) {
508 return false;
509 }
510
511 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
512 this->flush(target, flushInfo);
513
514 code = atlas->addToAtlas(resourceProvider, id, uploadTarget, width, height,
515 image, atlasLocation);
516 }
517
518 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
519 }
520
Brian Salomone5b399e2017-07-19 13:50:54 -0400521 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400522 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
523 uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500524
bsalomonee432412016-06-27 07:18:18 -0700525 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700526
527 // generate bounding rect for bitmap draw
528 SkRect scaledBounds = bounds;
529 // scale to mip level size
530 scaledBounds.fLeft *= scale;
531 scaledBounds.fTop *= scale;
532 scaledBounds.fRight *= scale;
533 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500534 // subtract out integer portion of origin
535 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500536 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
537 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700538 scaledBounds.offset(-dx, -dy);
539 // get integer boundary
540 SkIRect devPathBounds;
541 scaledBounds.roundOut(&devPathBounds);
542 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800543 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500544 // place devBounds at origin
545 int width = devPathBounds.width() + 2*intPad;
546 int height = devPathBounds.height() + 2*intPad;
547 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400548 SkScalar translateX = intPad - dx;
549 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700550
551 // draw path to bitmap
552 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500553 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400554 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700555
jvanverth512e4372015-11-23 11:50:02 -0800556 SkASSERT(devPathBounds.fLeft == 0);
557 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500558 SkASSERT(devPathBounds.width() > 0);
559 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700560
joel.liang8cbb4242017-01-09 18:39:43 -0800561 // setup signed distance field storage
562 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
563 width = dfBounds.width();
564 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800565 // TODO We should really generate this directly into the plot somehow
566 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800567
joel.liang8cbb4242017-01-09 18:39:43 -0800568 SkPath path;
569 shape.asPath(&path);
570#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
571 // Generate signed distance field directly from SkPath
572 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
573 path, drawMatrix,
574 width, height, width * sizeof(unsigned char));
575 if (!succeed) {
576#endif
577 // setup bitmap backing
578 SkAutoPixmapStorage dst;
579 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
580 devPathBounds.height()))) {
581 return false;
582 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400583 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800584
585 // rasterize path
586 SkPaint paint;
587 paint.setStyle(SkPaint::kFill_Style);
588 paint.setAntiAlias(true);
589
590 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800591
592 SkRasterClip rasterClip;
593 rasterClip.setRect(devPathBounds);
594 draw.fRC = &rasterClip;
595 draw.fMatrix = &drawMatrix;
596 draw.fDst = dst;
597
598 draw.drawPathCoverage(path, paint);
599
600 // Generate signed distance field
601 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
602 (const unsigned char*)dst.addr(),
603 dst.width(), dst.height(), dst.rowBytes());
604#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
605 }
606#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700607
608 // add to atlas
609 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500610 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500611
612 if (!this->addToAtlas(target, flushInfo, atlas,
613 width, height, dfStorage.get(), &id, &atlasLocation)) {
614 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700615 }
616
617 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700618 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700619 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500620
Robert Phillips3cf781d2017-08-22 18:25:11 -0400621 shapeData->fBounds = SkRect::Make(devPathBounds);
622 shapeData->fBounds.offset(-translateX, -translateY);
623 shapeData->fBounds.fLeft /= scale;
624 shapeData->fBounds.fTop /= scale;
625 shapeData->fBounds.fRight /= scale;
626 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500627
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400628 // We pack the 2bit page index in the low bit of the u and v texture coords
629 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
630 SkASSERT(pageIndex < 4);
631 uint16_t uBit = (pageIndex >> 1) & 0x1;
632 uint16_t vBit = pageIndex & 0x1;
633 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
634 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400635 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400636 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400637 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400638 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700639
bsalomonee432412016-06-27 07:18:18 -0700640 fShapeCache->add(shapeData);
641 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700642#ifdef DF_PATH_TRACKING
643 ++g_NumCachedPaths;
644#endif
645 return true;
646 }
647
Brian Salomone5b399e2017-07-19 13:50:54 -0400648 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400649 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
650 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500651 const SkRect& bounds = shape.bounds();
652 if (bounds.isEmpty()) {
653 return false;
654 }
655 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500656 SkScalar tx = ctm.getTranslateX();
657 SkScalar ty = ctm.getTranslateY();
658 tx -= SkScalarFloorToScalar(tx);
659 ty -= SkScalarFloorToScalar(ty);
660 drawMatrix.set(SkMatrix::kMTransX, tx);
661 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500662 SkRect shapeDevBounds;
663 drawMatrix.mapRect(&shapeDevBounds, bounds);
664 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
665 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
666
667 // get integer boundary
668 SkIRect devPathBounds;
669 shapeDevBounds.roundOut(&devPathBounds);
670 // pad to allow room for antialiasing
671 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
672 // place devBounds at origin
673 int width = devPathBounds.width() + 2 * intPad;
674 int height = devPathBounds.height() + 2 * intPad;
675 devPathBounds = SkIRect::MakeWH(width, height);
676 SkScalar translateX = intPad - dx;
677 SkScalar translateY = intPad - dy;
678
679 SkASSERT(devPathBounds.fLeft == 0);
680 SkASSERT(devPathBounds.fTop == 0);
681 SkASSERT(devPathBounds.width() > 0);
682 SkASSERT(devPathBounds.height() > 0);
683
684 SkPath path;
685 shape.asPath(&path);
686 // setup bitmap backing
687 SkAutoPixmapStorage dst;
688 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
689 devPathBounds.height()))) {
690 return false;
691 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400692 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500693
694 // rasterize path
695 SkPaint paint;
696 paint.setStyle(SkPaint::kFill_Style);
697 paint.setAntiAlias(true);
698
699 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500700
701 SkRasterClip rasterClip;
702 rasterClip.setRect(devPathBounds);
703 draw.fRC = &rasterClip;
704 drawMatrix.postTranslate(translateX, translateY);
705 draw.fMatrix = &drawMatrix;
706 draw.fDst = dst;
707
708 draw.drawPathCoverage(path, paint);
709
710 // add to atlas
711 SkIPoint16 atlasLocation;
712 GrDrawOpAtlas::AtlasID id;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500713
714 if (!this->addToAtlas(target, flushInfo, atlas,
715 dst.width(), dst.height(), dst.addr(), &id, &atlasLocation)) {
716 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500717 }
718
719 // add to cache
720 shapeData->fKey.set(shape, ctm);
721 shapeData->fID = id;
722
Jim Van Verth33632d82017-02-28 10:24:39 -0500723 shapeData->fBounds = SkRect::Make(devPathBounds);
724 shapeData->fBounds.offset(-translateX, -translateY);
725
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400726 // We pack the 2bit page index in the low bit of the u and v texture coords
727 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
728 SkASSERT(pageIndex < 4);
729 uint16_t uBit = (pageIndex >> 1) & 0x1;
730 uint16_t vBit = pageIndex & 0x1;
731 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
732 (atlasLocation.fX+width) << 1 | uBit,
733 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500734
735 fShapeCache->add(shapeData);
736 fShapeList->addToTail(shapeData);
737#ifdef DF_PATH_TRACKING
738 ++g_NumCachedPaths;
739#endif
740 return true;
741 }
742
Brian Salomon29b60c92017-10-31 14:42:10 -0400743 void writePathVertices(GrDrawOpAtlas* atlas,
Brian Osman0dd43022018-11-16 15:53:26 -0500744 GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500745 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400746 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700747 const ShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500748 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400749 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500750 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
751 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400752 }
Jim Van Verth77047542017-01-11 14:17:00 -0500753
Brian Osman0dd43022018-11-16 15:53:26 -0500754 // set up texture coordinates
755 GrVertexWriter::TriStrip<uint16_t> texCoords{
756 (uint16_t)shapeData->fTextureCoords.fLeft,
757 (uint16_t)shapeData->fTextureCoords.fTop,
758 (uint16_t)shapeData->fTextureCoords.fRight,
759 (uint16_t)shapeData->fTextureCoords.fBottom
760 };
761
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400762 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500763 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500764 color,
765 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400766 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500767 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
768 color,
769 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400770 }
joshualitt5bf99f12015-03-13 11:47:42 -0700771 }
772
Brian Salomone5b399e2017-07-19 13:50:54 -0400773 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400774 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000775 int numAtlasTextures = SkToInt(fAtlas->numActivePages());
776 auto proxies = fAtlas->getProxies();
777 if (gp->numTextureSamplers() != numAtlasTextures) {
778 for (int i = gp->numTextureSamplers(); i < numAtlasTextures; ++i) {
779 flushInfo->fFixedDynamicState->fPrimitiveProcessorTextures[i] = proxies[i].get();
780 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400781 // During preparation the number of atlas pages has increased.
782 // Update the proxies used in the GP to match.
783 if (fUsesDistanceField) {
784 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500785 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampBilerp());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400786 } else {
787 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
Robert Phillips4bc70112018-03-01 10:24:02 -0500788 fAtlas->getProxies(), fAtlas->numActivePages(), GrSamplerState::ClampNearest());
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400789 }
790 }
791
bsalomon6d6b6ad2016-07-13 14:45:28 -0700792 if (flushInfo->fInstancesToFlush) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000793 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700794 int maxInstancesPerDraw =
Brian Salomondbf70722019-02-07 11:31:24 -0500795 static_cast<int>(flushInfo->fIndexBuffer->size() / sizeof(uint16_t) / 6);
Brian Salomon12d22642019-01-29 14:38:50 -0500796 mesh->setIndexedPatterned(flushInfo->fIndexBuffer, kIndicesPerQuad, kVerticesPerQuad,
797 flushInfo->fInstancesToFlush, maxInstancesPerDraw);
798 mesh->setVertexData(flushInfo->fVertexBuffer, flushInfo->fVertexOffset);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700799 target->recordDraw(
800 flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fFixedDynamicState, nullptr);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700801 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
802 flushInfo->fInstancesToFlush = 0;
803 }
joshualitt5bf99f12015-03-13 11:47:42 -0700804 }
805
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700806 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
807 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
808 }
809
Brian Osmancf860852018-10-31 14:04:39 -0400810 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500811 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700812
Brian Salomon7eae3e02018-08-07 14:02:38 +0000813 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400814 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400815 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000816 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700817 }
818
Jim Van Verth33632d82017-02-28 10:24:39 -0500819 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000820 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500821 }
822
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400823 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
824 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
825
826 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000827 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700828 }
829
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400830 // We can position on the cpu unless we're in perspective,
831 // but also need to make sure local matrices are identical
832 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
833 !thisCtm.cheapEqualTo(thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000834 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500835 }
836
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400837 // Depending on the ctm we may have a different shader for SDF paths
838 if (this->usesDistanceField()) {
839 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
840 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000841 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400842 }
843 }
844
Brian Salomond0a0a652016-12-15 15:25:22 -0500845 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500846 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000847 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700848 }
849
Jim Van Verth33632d82017-02-28 10:24:39 -0500850 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700851
Brian Salomond0a0a652016-12-15 15:25:22 -0500852 struct Entry {
Brian Osmancf860852018-10-31 14:04:39 -0400853 SkPMColor4f fColor;
854 GrShape fShape;
855 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700856 };
857
Brian Salomond0a0a652016-12-15 15:25:22 -0500858 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400859 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500860 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700861 ShapeCache* fShapeCache;
862 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700863 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500864 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700865
Brian Salomonfebbd232017-07-11 15:52:02 -0400866 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700867};
868
Jim Van Verth83010462017-03-16 08:45:39 -0400869bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400870 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400871 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700872
jvanverthfa38a302014-10-06 05:59:05 -0700873 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700874 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700875 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700876 if (!fAtlas) {
Greg Daniel4065d452018-11-16 15:43:41 -0500877 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500878 args.fContext->priv().caps()->getBackendFormatFromColorType(
Greg Daniel4065d452018-11-16 15:43:41 -0500879 kAlpha_8_SkColorType);
Robert Phillips9da87e02019-02-04 13:26:26 -0500880 fAtlas = GrDrawOpAtlas::Make(args.fContext->priv().proxyProvider(),
Greg Daniel4065d452018-11-16 15:43:41 -0500881 format,
Robert Phillips42dda082019-05-14 13:29:45 -0400882 GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500883 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500884 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500885 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400886 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500887 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700888 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700889 return false;
890 }
891 }
892
Brian Salomonfebbd232017-07-11 15:52:02 -0400893 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400894 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(),
895 &fShapeCache, &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
Brian Salomonfebbd232017-07-11 15:52:02 -0400896 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800897
jvanverthfa38a302014-10-06 05:59:05 -0700898 return true;
899}
900
joshualitt21279c72015-05-11 07:21:37 -0700901///////////////////////////////////////////////////////////////////////////////////////////////////
902
Hal Canary6f6961e2017-01-31 13:50:44 -0500903#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700904
Brian Salomonfebbd232017-07-11 15:52:02 -0400905struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700906 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700907 ~PathTestStruct() { this->reset(); }
908
909 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700910 ShapeDataList::Iter iter;
911 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
912 ShapeData* shapeData;
913 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700914 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700915 fShapeList.remove(shapeData);
916 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700917 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500918 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700919 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700920 }
921
Brian Salomon2ee084e2016-12-16 18:59:19 -0500922 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700923 PathTestStruct* dfpr = (PathTestStruct*)pr;
924 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700925 ShapeDataList::Iter iter;
926 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
927 ShapeData* shapeData;
928 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700929 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700930 if (id == shapeData->fID) {
931 dfpr->fShapeCache.remove(shapeData->fKey);
932 dfpr->fShapeList.remove(shapeData);
933 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700934 }
935 }
936 }
937
938 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500939 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700940 ShapeCache fShapeCache;
941 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700942};
943
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500944std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500945 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500946 GrPaint&& paint,
947 const GrShape& shape,
948 const SkMatrix& viewMatrix,
949 GrDrawOpAtlas* atlas,
950 ShapeCache* shapeCache,
951 ShapeDataList* shapeList,
952 bool gammaCorrect,
953 const GrUserStencilSettings* stencil) {
954
Robert Phillips7c525e62018-06-12 10:11:12 -0400955 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
956 atlas, shapeCache, shapeList, gammaCorrect,
957 stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500958
959}
960
Brian Salomonfebbd232017-07-11 15:52:02 -0400961GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
962 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700963 static PathTestStruct gTestStruct;
964
Robert Phillips9da87e02019-02-04 13:26:26 -0500965 if (context->priv().contextID() != gTestStruct.fContextID) {
966 gTestStruct.fContextID = context->priv().contextID();
joshualitt21279c72015-05-11 07:21:37 -0700967 gTestStruct.reset();
Greg Daniel4065d452018-11-16 15:43:41 -0500968 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500969 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
970 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context->priv().proxyProvider(),
Robert Phillips42dda082019-05-14 13:29:45 -0400971 format, GrColorType::kAlpha_8,
Robert Phillips256c37b2017-03-01 14:32:46 -0500972 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500973 PLOT_WIDTH, PLOT_HEIGHT,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500974 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500975 &PathTestStruct::HandleEviction,
976 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700977 }
978
979 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700980 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700981
bsalomonee432412016-06-27 07:18:18 -0700982 // This path renderer only allows fill styles.
983 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500984 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400985 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500986 std::move(paint), shape, viewMatrix,
987 gTestStruct.fAtlas.get(),
988 &gTestStruct.fShapeCache,
989 &gTestStruct.fShapeList,
990 gammaCorrect,
991 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700992}
993
994#endif