blob: 295cbc279b42a499f67e0d77c0aeda53947107f7 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkAutoPixmapStorage.h"
13#include "src/core/SkDistanceFieldGen.h"
14#include "src/core/SkDraw.h"
Michael Ludwigfbe28592020-06-26 16:02:15 -040015#include "src/core/SkMatrixPriv.h"
Brian Osman9aaec362020-05-08 14:54:37 -040016#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkPointPriv.h"
18#include "src/core/SkRasterClip.h"
19#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/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050024#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#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"
Robert Phillips5b68ed42020-08-10 14:46:42 -040029#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050031#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Robert Phillips46a324a2020-08-10 13:08:12 -040032#include "src/gpu/ops/GrSmallPathAtlasMgr.h"
Robert Phillips34949e32020-08-05 15:54:31 -040033#include "src/gpu/ops/GrSmallPathShapeData.h"
jvanverthfa38a302014-10-06 05:59:05 -070034
jvanverthb61283f2014-10-30 05:57:21 -070035// mip levels
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080036static constexpr SkScalar kIdealMinMIP = 12;
37static constexpr SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050038
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080039static constexpr SkScalar kMaxDim = 73;
40static constexpr SkScalar kMinSize = SK_ScalarHalf;
41static constexpr SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070042
Robert Phillips079455c2020-08-11 15:18:46 -040043GrSmallPathRenderer::GrSmallPathRenderer() {}
Robert Phillipscac17642020-08-07 16:17:10 -040044
Robert Phillipsa4bb0642020-08-11 09:55:17 -040045GrSmallPathRenderer::~GrSmallPathRenderer() {}
Robert Phillipscac17642020-08-07 16:17:10 -040046
Chris Dalton5ed44232017-09-07 13:22:46 -060047GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -070048 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060049 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070050 }
51 // If the shape has no key then we won't get any reuse.
52 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060053 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070054 }
55 // This only supports filled paths, however, the caller may apply the style to make a filled
56 // path and try again.
57 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060058 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070059 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050060 // This does non-inverse coverage-based antialiased fills.
Chris Dalton6ce447a2019-06-23 18:07:38 -060061 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060062 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -070063 }
jvanverthfa38a302014-10-06 05:59:05 -070064 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -070065 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060066 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -070067 }
halcanary9d524f22016-03-29 09:03:52 -070068
Jim Van Verth5698c8a2017-10-12 10:18:44 -040069 SkScalar scaleFactors[2] = { 1, 1 };
Jim Van Verth12935772021-04-30 10:04:51 -040070 // TODO: handle perspective distortion
Jim Van Verth5698c8a2017-10-12 10:18:44 -040071 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -060072 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050073 }
Jim Van Verth12935772021-04-30 10:04:51 -040074 // For affine transformations, too much shear can produce artifacts.
Brian Salomond4388152021-07-26 15:18:40 -040075 if (!scaleFactors[0] || scaleFactors[1]/scaleFactors[0] > 4) {
Jim Van Verth12935772021-04-30 10:04:51 -040076 return CanDrawPath::kNo;
77 }
78 // Only support paths with bounds within kMaxDim by kMaxDim,
79 // scaled to have bounds within kMaxSize by kMaxSize.
80 // The goal is to accelerate rendering of lots of small paths that may be scaling.
bsalomon0a0f67e2016-06-28 11:56:42 -070081 SkRect bounds = args.fShape->styledBounds();
Brian Osman116b33e2020-02-05 13:34:09 -050082 SkScalar minDim = std::min(bounds.width(), bounds.height());
83 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -050084 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
85 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -060086 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
87 return CanDrawPath::kNo;
88 }
bsalomon6266dca2016-03-11 06:22:00 -080089
Chris Dalton5ed44232017-09-07 13:22:46 -060090 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -070091}
92
jvanverthfa38a302014-10-06 05:59:05 -070093////////////////////////////////////////////////////////////////////////////////
94
joshualitt5bf99f12015-03-13 11:47:42 -070095// padding around path bounds to allow for antialiased pixels
Robert Phillips6d3bc292020-04-06 10:29:28 -040096static const int kAntiAliasPad = 1;
joshualitt5bf99f12015-03-13 11:47:42 -070097
Brian Salomonfebbd232017-07-11 15:52:02 -040098class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
99private:
100 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
101
joshualitt5bf99f12015-03-13 11:47:42 -0700102public:
Brian Salomon25a88092016-12-01 09:36:50 -0500103 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700104
Herb Derbyc76d4092020-10-07 16:46:15 -0400105 static GrOp::Owner Make(GrRecordingContext* context,
106 GrPaint&& paint,
107 const GrStyledShape& shape,
108 const SkMatrix& viewMatrix,
109 bool gammaCorrect,
110 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400111 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -0400112 gammaCorrect, stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700113 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500114
Herb Derbyc76d4092020-10-07 16:46:15 -0400115 SmallPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const GrStyledShape& shape,
Robert Phillips079455c2020-08-11 15:18:46 -0400116 const SkMatrix& viewMatrix, bool gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400117 const GrUserStencilSettings* stencilSettings)
Robert Phillips4133dc42020-03-11 15:55:55 -0400118 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400119 , fHelper(processorSet, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500120 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500121 // Compute bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400122 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo);
Jim Van Verth33632d82017-02-28 10:24:39 -0500123
Jim Van Verth83010462017-03-16 08:45:39 -0400124#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500125 fUsesDistanceField = true;
126#else
Jim Van Verth83010462017-03-16 08:45:39 -0400127 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500128 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
129#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400130 // always use distance fields if in perspective
131 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500132
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400133 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500134
Brian Salomond0a0a652016-12-15 15:25:22 -0500135 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500136 }
137
Brian Salomonfebbd232017-07-11 15:52:02 -0400138 const char* name() const override { return "SmallPathOp"; }
139
Robert Phillips294723d2021-06-17 09:23:58 -0400140 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400141 fHelper.visitProxies(func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400142 }
143
Brian Salomonfebbd232017-07-11 15:52:02 -0400144 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
145
Chris Dalton57ab06c2021-04-22 12:57:28 -0600146 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
147 GrClampType clampType) override {
148 return fHelper.finalizeProcessors(caps, clip, clampType,
149 GrProcessorAnalysisCoverage::kSingleChannel,
150 &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700151 }
152
Brian Salomonfebbd232017-07-11 15:52:02 -0400153private:
bsalomonb5238a72015-05-05 07:49:49 -0700154 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500155 sk_sp<const GrBuffer> fVertexBuffer;
156 sk_sp<const GrBuffer> fIndexBuffer;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500157 GrGeometryProcessor* fGeometryProcessor;
Chris Dalton304e14d2020-03-17 14:29:06 -0600158 const GrSurfaceProxy** fPrimProcProxies;
bsalomonb5238a72015-05-05 07:49:49 -0700159 int fVertexOffset;
160 int fInstancesToFlush;
161 };
162
Robert Phillips2669a7b2020-03-12 12:07:19 -0400163 GrProgramInfo* programInfo() override {
164 // TODO [PI]: implement
165 return nullptr;
166 }
167
Robert Phillips4133dc42020-03-11 15:55:55 -0400168 void onCreateProgramInfo(const GrCaps*,
169 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500170 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600171 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400172 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400173 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500174 GrXferBarrierFlags renderPassXferBarriers,
175 GrLoadOp colorLoadOp) override {
Robert Phillips326f1d72020-10-01 09:43:29 -0400176 // We cannot surface the SmallPathOp's programInfo at record time. As currently
177 // implemented, the GP is modified at flush time based on the number of pages in the
178 // atlas.
Robert Phillips4133dc42020-03-11 15:55:55 -0400179 }
180
Robert Phillips2669a7b2020-03-12 12:07:19 -0400181 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500182 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400183 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400184 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500185 GrXferBarrierFlags renderPassXferBarriers,
186 GrLoadOp colorLoadOp) override {
Robert Phillips2669a7b2020-03-12 12:07:19 -0400187 // TODO [PI]: implement
188 }
189
Robert Phillips71143952021-06-17 14:55:07 -0400190 void onPrepareDraws(GrMeshDrawTarget* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500191 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700192
Robert Phillips079455c2020-08-11 15:18:46 -0400193 GrSmallPathAtlasMgr* atlasMgr = target->smallPathAtlasManager();
194 if (!atlasMgr) {
195 return;
196 }
197
Brian Salomon7eae3e02018-08-07 14:02:38 +0000198 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500199 static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000200
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700201 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600202 flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400203
204 int numActiveProxies;
Robert Phillips079455c2020-08-11 15:18:46 -0400205 const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000206 for (int i = 0; i < numActiveProxies; ++i) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400207 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
208 // proxies don't get added during the visitProxies call. Thus we add them here.
Chris Dalton304e14d2020-03-17 14:29:06 -0600209 flushInfo.fPrimProcProxies[i] = views[i].proxy();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500210 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000211 }
Brian Salomon49348902018-06-26 09:12:38 -0400212
joshualitt5bf99f12015-03-13 11:47:42 -0700213 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400214 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500215 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500216 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400217 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500218 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
219 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
220 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
221
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400222 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500223 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400224 if (ctm.hasPerspective()) {
225 matrix = &ctm;
226 } else if (fHelper.usesLocalCoords()) {
227 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500228 return;
229 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400230 matrix = &invert;
231 } else {
232 matrix = &SkMatrix::I();
233 }
Brian Salomonccb61422020-01-09 10:46:36 -0500234 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
235 target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400236 views, numActiveProxies, GrSamplerState::Filter::kLinear,
Brian Salomonccb61422020-01-09 10:46:36 -0500237 flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400238 } else {
239 SkMatrix invert;
240 if (fHelper.usesLocalCoords()) {
241 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400242 return;
243 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500244 }
245
Brian Salomonccb61422020-01-09 10:46:36 -0500246 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
247 target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400248 views, numActiveProxies, GrSamplerState::Filter::kNearest,
Brian Salomonccb61422020-01-09 10:46:36 -0500249 kA8_GrMaskFormat, invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500250 }
joshualitt5bf99f12015-03-13 11:47:42 -0700251
joshualitt5bf99f12015-03-13 11:47:42 -0700252 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500253 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400254
255 // We need to make sure we don't overflow a 32 bit int when we request space in the
256 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400257 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400258 return;
259 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400260 GrVertexWriter vertices{ target->makeVertexSpace(
261 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
262 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
263
264 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500265 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700266 SkDebugf("Could not allocate vertices\n");
267 return;
268 }
269
bsalomonb5238a72015-05-05 07:49:49 -0700270 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700271 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500272 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700273
Robert Phillips34949e32020-08-05 15:54:31 -0400274 GrSmallPathShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500275 if (fUsesDistanceField) {
276 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400277 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500278 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400279 if (args.fViewMatrix.hasPerspective()) {
280 // approximate the scale since we can't get it from the matrix
281 SkRect xformedBounds;
282 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Brian Osman788b9162020-02-07 10:36:46 -0500283 maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(),
Jim Van Verth51245932017-10-12 11:07:29 -0400284 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400285 } else {
286 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
287 }
Brian Osman116b33e2020-02-05 13:34:09 -0500288 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verth33632d82017-02-28 10:24:39 -0500289 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
290 // In the majority of cases this will yield a crisper rendering.
291 SkScalar mipScale = 1.0f;
292 // Our mipscale is the maxScale clamped to the next highest power of 2
293 if (maxScale <= SK_ScalarHalf) {
294 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
295 mipScale = SkScalarPow(2, -log);
296 } else if (maxScale > SK_Scalar1) {
297 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
298 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700299 }
Jim Van Verthcbbd5652021-04-07 14:27:55 -0400300 // Log2 isn't very precise at values close to a power of 2,
301 // so add a little tolerance here. A little bit of scaling up is fine.
302 SkASSERT(maxScale <= mipScale + SK_ScalarNearlyZero);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500303
Jim Van Verth33632d82017-02-28 10:24:39 -0500304 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
305 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
306 // so we can preserve as much detail as possible. However, we can't scale down more
307 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
308 // just bigger than the ideal, and then scale down until we are no more than 4x the
309 // original mipsize.
310 if (mipSize < kIdealMinMIP) {
311 SkScalar newMipSize = mipSize;
312 do {
313 newMipSize *= 2;
314 } while (newMipSize < kIdealMinMIP);
315 while (newMipSize > 4 * mipSize) {
316 newMipSize *= 0.25f;
317 }
318 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700319 }
Robert Phillips109ff202020-08-10 16:39:31 -0400320
Brian Osman788b9162020-02-07 10:36:46 -0500321 SkScalar desiredDimension = std::min(mipSize, kMaxMIP);
Robert Phillips109ff202020-08-10 16:39:31 -0400322 int ceilDesiredDimension = SkScalarCeilToInt(desiredDimension);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500323
Jim Van Verth33632d82017-02-28 10:24:39 -0500324 // check to see if df path is cached
Robert Phillips079455c2020-08-11 15:18:46 -0400325 shapeData = atlasMgr->findOrCreate(args.fShape, ceilDesiredDimension);
Robert Phillips6507e632020-08-07 14:35:56 -0400326 if (!shapeData->fAtlasLocator.plotLocator().isValid()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500327 SkScalar scale = desiredDimension / maxDim;
328
Jim Van Verth33632d82017-02-28 10:24:39 -0500329 if (!this->addDFPathToAtlas(target,
330 &flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400331 atlasMgr,
Jim Van Verth33632d82017-02-28 10:24:39 -0500332 shapeData,
333 args.fShape,
Robert Phillips109ff202020-08-10 16:39:31 -0400334 ceilDesiredDimension,
Jim Van Verth33632d82017-02-28 10:24:39 -0500335 scale)) {
Robert Phillips079455c2020-08-11 15:18:46 -0400336 atlasMgr->deleteCacheEntry(shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500337 continue;
338 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500339 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500340 } else {
341 // check to see if bitmap path is cached
Robert Phillips079455c2020-08-11 15:18:46 -0400342 shapeData = atlasMgr->findOrCreate(args.fShape, args.fViewMatrix);
Robert Phillips6507e632020-08-07 14:35:56 -0400343 if (!shapeData->fAtlasLocator.plotLocator().isValid()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500344 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400345 &flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400346 atlasMgr,
Robert Phillips8296e752017-08-25 08:45:21 -0400347 shapeData,
348 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400349 args.fViewMatrix)) {
Robert Phillips079455c2020-08-11 15:18:46 -0400350 atlasMgr->deleteCacheEntry(shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500351 continue;
352 }
353 }
joshualitt5bf99f12015-03-13 11:47:42 -0700354 }
355
Robert Phillips40a29d72018-01-18 12:59:22 -0500356 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips079455c2020-08-11 15:18:46 -0400357 atlasMgr->setUseToken(shapeData, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700358
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400359 this->writePathVertices(vertices, GrVertexColor(args.fColor, fWideColor),
Brian Osmanc906d252018-12-04 11:17:46 -0500360 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700361 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700362 }
363
bsalomon75398562015-08-17 12:55:38 -0700364 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700365 }
366
Robert Phillips71143952021-06-17 14:55:07 -0400367 bool addToAtlasWithRetry(GrMeshDrawTarget* target,
Robert Phillips6704bc82020-08-14 12:45:07 -0400368 FlushInfo* flushInfo,
369 GrSmallPathAtlasMgr* atlasMgr,
370 int width, int height, const void* image,
371 const SkRect& bounds, int srcInset,
372 GrSmallPathShapeData* shapeData) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500373 auto resourceProvider = target->resourceProvider();
374 auto uploadTarget = target->deferredUploadTarget();
375
Robert Phillips6704bc82020-08-14 12:45:07 -0400376 auto code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height,
377 image, &shapeData->fAtlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500378 if (GrDrawOpAtlas::ErrorCode::kError == code) {
379 return false;
380 }
381
382 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
383 this->flush(target, flushInfo);
384
Robert Phillips6704bc82020-08-14 12:45:07 -0400385 code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height,
386 image, &shapeData->fAtlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500387 }
388
Robert Phillips6704bc82020-08-14 12:45:07 -0400389 shapeData->fAtlasLocator.insetSrc(srcInset);
390 shapeData->fBounds = bounds;
391
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500392 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
393 }
394
Robert Phillips71143952021-06-17 14:55:07 -0400395 bool addDFPathToAtlas(GrMeshDrawTarget* target, FlushInfo* flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400396 GrSmallPathAtlasMgr* atlasMgr, GrSmallPathShapeData* shapeData,
Robert Phillips34949e32020-08-05 15:54:31 -0400397 const GrStyledShape& shape, uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500398
bsalomonee432412016-06-27 07:18:18 -0700399 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700400
401 // generate bounding rect for bitmap draw
402 SkRect scaledBounds = bounds;
403 // scale to mip level size
404 scaledBounds.fLeft *= scale;
405 scaledBounds.fTop *= scale;
406 scaledBounds.fRight *= scale;
407 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500408 // subtract out integer portion of origin
409 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500410 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
411 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700412 scaledBounds.offset(-dx, -dy);
413 // get integer boundary
414 SkIRect devPathBounds;
415 scaledBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400416 // place devBounds at origin with padding to allow room for antialiasing
417 int width = devPathBounds.width() + 2 * kAntiAliasPad;
418 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500419 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400420 SkScalar translateX = kAntiAliasPad - dx;
421 SkScalar translateY = kAntiAliasPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700422
423 // draw path to bitmap
424 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500425 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400426 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700427
jvanverth512e4372015-11-23 11:50:02 -0800428 SkASSERT(devPathBounds.fLeft == 0);
429 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500430 SkASSERT(devPathBounds.width() > 0);
431 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700432
joel.liang8cbb4242017-01-09 18:39:43 -0800433 // setup signed distance field storage
434 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
435 width = dfBounds.width();
436 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800437 // TODO We should really generate this directly into the plot somehow
438 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800439
joel.liang8cbb4242017-01-09 18:39:43 -0800440 SkPath path;
441 shape.asPath(&path);
joel.liang8cbb4242017-01-09 18:39:43 -0800442 // Generate signed distance field directly from SkPath
443 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400444 path, drawMatrix, width, height,
445 width * sizeof(unsigned char));
joel.liang8cbb4242017-01-09 18:39:43 -0800446 if (!succeed) {
joel.liang8cbb4242017-01-09 18:39:43 -0800447 // setup bitmap backing
448 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400449 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
joel.liang8cbb4242017-01-09 18:39:43 -0800450 return false;
451 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400452 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800453
454 // rasterize path
455 SkPaint paint;
456 paint.setStyle(SkPaint::kFill_Style);
457 paint.setAntiAlias(true);
458
459 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800460
461 SkRasterClip rasterClip;
462 rasterClip.setRect(devPathBounds);
463 draw.fRC = &rasterClip;
Brian Osman9aaec362020-05-08 14:54:37 -0400464 SkSimpleMatrixProvider matrixProvider(drawMatrix);
465 draw.fMatrixProvider = &matrixProvider;
joel.liang8cbb4242017-01-09 18:39:43 -0800466 draw.fDst = dst;
467
468 draw.drawPathCoverage(path, paint);
469
470 // Generate signed distance field
471 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
472 (const unsigned char*)dst.addr(),
473 dst.width(), dst.height(), dst.rowBytes());
joel.liang8cbb4242017-01-09 18:39:43 -0800474 }
joshualitt5bf99f12015-03-13 11:47:42 -0700475
Robert Phillips6704bc82020-08-14 12:45:07 -0400476 SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY);
477 drawBounds.fLeft /= scale;
478 drawBounds.fTop /= scale;
479 drawBounds.fRight /= scale;
480 drawBounds.fBottom /= scale;
joshualitt5bf99f12015-03-13 11:47:42 -0700481
Robert Phillips6704bc82020-08-14 12:45:07 -0400482 return this->addToAtlasWithRetry(target, flushInfo, atlasMgr,
483 width, height, dfStorage.get(),
484 drawBounds, SK_DistanceFieldPad, shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700485 }
486
Robert Phillips71143952021-06-17 14:55:07 -0400487 bool addBMPathToAtlas(GrMeshDrawTarget* target, FlushInfo* flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400488 GrSmallPathAtlasMgr* atlasMgr, GrSmallPathShapeData* shapeData,
Robert Phillips34949e32020-08-05 15:54:31 -0400489 const GrStyledShape& shape, const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500490 const SkRect& bounds = shape.bounds();
491 if (bounds.isEmpty()) {
492 return false;
493 }
494 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500495 SkScalar tx = ctm.getTranslateX();
496 SkScalar ty = ctm.getTranslateY();
497 tx -= SkScalarFloorToScalar(tx);
498 ty -= SkScalarFloorToScalar(ty);
499 drawMatrix.set(SkMatrix::kMTransX, tx);
500 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500501 SkRect shapeDevBounds;
502 drawMatrix.mapRect(&shapeDevBounds, bounds);
503 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
504 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
505
506 // get integer boundary
507 SkIRect devPathBounds;
508 shapeDevBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400509 // place devBounds at origin with padding to allow room for antialiasing
510 int width = devPathBounds.width() + 2 * kAntiAliasPad;
511 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verth33632d82017-02-28 10:24:39 -0500512 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400513 SkScalar translateX = kAntiAliasPad - dx;
514 SkScalar translateY = kAntiAliasPad - dy;
Jim Van Verth33632d82017-02-28 10:24:39 -0500515
516 SkASSERT(devPathBounds.fLeft == 0);
517 SkASSERT(devPathBounds.fTop == 0);
518 SkASSERT(devPathBounds.width() > 0);
519 SkASSERT(devPathBounds.height() > 0);
520
521 SkPath path;
522 shape.asPath(&path);
523 // setup bitmap backing
524 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400525 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500526 return false;
527 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400528 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500529
530 // rasterize path
531 SkPaint paint;
532 paint.setStyle(SkPaint::kFill_Style);
533 paint.setAntiAlias(true);
534
535 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500536
537 SkRasterClip rasterClip;
538 rasterClip.setRect(devPathBounds);
539 draw.fRC = &rasterClip;
540 drawMatrix.postTranslate(translateX, translateY);
Brian Osman9aaec362020-05-08 14:54:37 -0400541 SkSimpleMatrixProvider matrixProvider(drawMatrix);
542 draw.fMatrixProvider = &matrixProvider;
Jim Van Verth33632d82017-02-28 10:24:39 -0500543 draw.fDst = dst;
544
545 draw.drawPathCoverage(path, paint);
546
Robert Phillips6704bc82020-08-14 12:45:07 -0400547 SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY);
Jim Van Verth33632d82017-02-28 10:24:39 -0500548
Robert Phillips6704bc82020-08-14 12:45:07 -0400549 return this->addToAtlasWithRetry(target, flushInfo, atlasMgr,
550 dst.width(), dst.height(), dst.addr(),
551 drawBounds, 0, shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500552 }
553
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400554 void writePathVertices(GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500555 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400556 const SkMatrix& ctm,
Robert Phillips34949e32020-08-05 15:54:31 -0400557 const GrSmallPathShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500558 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400559 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500560 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
561 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400562 }
Jim Van Verth77047542017-01-11 14:17:00 -0500563
Brian Osman0dd43022018-11-16 15:53:26 -0500564 // set up texture coordinates
Herb Derby1318e452020-08-03 14:38:10 -0400565 auto texCoords = GrVertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs());
Brian Osman0dd43022018-11-16 15:53:26 -0500566
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400567 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500568 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500569 color,
570 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400571 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500572 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
573 color,
574 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400575 }
joshualitt5bf99f12015-03-13 11:47:42 -0700576 }
577
Robert Phillips71143952021-06-17 14:55:07 -0400578 void flush(GrMeshDrawTarget* target, FlushInfo* flushInfo) const {
Robert Phillips079455c2020-08-11 15:18:46 -0400579 GrSmallPathAtlasMgr* atlasMgr = target->smallPathAtlasManager();
580 if (!atlasMgr) {
581 return;
582 }
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400583
584 int numActiveProxies;
Robert Phillips079455c2020-08-11 15:18:46 -0400585 const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400586
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500587 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400588 if (gp->numTextureSamplers() != numActiveProxies) {
589 for (int i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600590 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400591 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
592 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500593 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000594 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400595 // During preparation the number of atlas pages has increased.
596 // Update the proxies used in the GP to match.
597 if (fUsesDistanceField) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500598 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400599 views, numActiveProxies, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400600 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500601 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400602 views, numActiveProxies, GrSamplerState::Filter::kNearest);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400603 }
604 }
605
bsalomon6d6b6ad2016-07-13 14:45:28 -0700606 if (flushInfo->fInstancesToFlush) {
Chris Daltoneb694b72020-03-16 09:25:50 -0600607 GrSimpleMesh* mesh = target->allocMesh();
Robert Phillipsee08d522019-10-28 16:34:44 -0400608 mesh->setIndexedPatterned(flushInfo->fIndexBuffer,
609 GrResourceProvider::NumIndicesPerNonAAQuad(),
Robert Phillipsee08d522019-10-28 16:34:44 -0400610 flushInfo->fInstancesToFlush,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600611 GrResourceProvider::MaxNumNonAAQuads(),
612 flushInfo->fVertexBuffer,
613 GrResourceProvider::NumVertsPerNonAAQuad(),
614 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600615 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
616 GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400617 flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() *
618 flushInfo->fInstancesToFlush;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700619 flushInfo->fInstancesToFlush = 0;
620 }
joshualitt5bf99f12015-03-13 11:47:42 -0700621 }
622
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700623 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600624 auto pipeline = fHelper.createPipeline(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -0500625
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600626 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
627 fHelper.stencilSettings());
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700628 }
629
Brian Osmancf860852018-10-31 14:04:39 -0400630 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500631 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700632
Herb Derbye25c3002020-10-27 15:57:27 -0400633 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400634 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400635 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000636 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700637 }
638
Jim Van Verth33632d82017-02-28 10:24:39 -0500639 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000640 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500641 }
642
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400643 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
644 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
645
646 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000647 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700648 }
649
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400650 // We can position on the cpu unless we're in perspective,
651 // but also need to make sure local matrices are identical
652 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
Mike Reed2c383152019-12-18 16:47:47 -0500653 !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000654 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500655 }
656
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400657 // Depending on the ctm we may have a different shader for SDF paths
658 if (this->usesDistanceField()) {
659 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
660 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000661 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400662 }
663 }
664
Brian Salomond0a0a652016-12-15 15:25:22 -0500665 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500666 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000667 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700668 }
669
John Stilesaf366522020-08-13 09:57:34 -0400670#if GR_TEST_UTILS
671 SkString onDumpInfo() const override {
672 SkString string;
673 for (const auto& geo : fShapes) {
674 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
675 }
676 string += fHelper.dumpInfo();
677 return string;
678 }
679#endif
680
Jim Van Verth33632d82017-02-28 10:24:39 -0500681 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700682
Brian Salomond0a0a652016-12-15 15:25:22 -0500683 struct Entry {
Michael Ludwig2686d692020-04-17 20:21:37 +0000684 SkPMColor4f fColor;
685 GrStyledShape fShape;
686 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700687 };
688
Brian Salomond0a0a652016-12-15 15:25:22 -0500689 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400690 Helper fHelper;
brianosman0e3c5542016-04-13 13:56:21 -0700691 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500692 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700693
John Stiles7571f9e2020-09-02 22:42:33 -0400694 using INHERITED = GrMeshDrawOp;
joshualitt5bf99f12015-03-13 11:47:42 -0700695};
696
Jim Van Verth83010462017-03-16 08:45:39 -0400697bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400698 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400699 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700700
jvanverthfa38a302014-10-06 05:59:05 -0700701 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700702 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700703 SkASSERT(args.fShape->hasUnstyledKey());
jvanverthfa38a302014-10-06 05:59:05 -0700704
Herb Derbyc76d4092020-10-07 16:46:15 -0400705 GrOp::Owner op = SmallPathOp::Make(
Robert Phillipscac17642020-08-07 16:17:10 -0400706 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -0400707 args.fGammaCorrect, args.fUserStencilSettings);
John Stiles0fbc6a32021-06-04 14:40:57 -0400708 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800709
jvanverthfa38a302014-10-06 05:59:05 -0700710 return true;
711}
712
joshualitt21279c72015-05-11 07:21:37 -0700713///////////////////////////////////////////////////////////////////////////////////////////////////
714
Hal Canary6f6961e2017-01-31 13:50:44 -0500715#if GR_TEST_UTILS
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500716
Herb Derbyc76d4092020-10-07 16:46:15 -0400717GrOp::Owner GrSmallPathRenderer::createOp_TestingOnly(
718 GrRecordingContext* context,
719 GrPaint&& paint,
720 const GrStyledShape& shape,
721 const SkMatrix& viewMatrix,
722 bool gammaCorrect,
723 const GrUserStencilSettings* stencil) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400724 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -0400725 gammaCorrect, stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500726}
727
Brian Salomonfebbd232017-07-11 15:52:02 -0400728GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
joshualitt21279c72015-05-11 07:21:37 -0700729 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700730 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700731
bsalomonee432412016-06-27 07:18:18 -0700732 // This path renderer only allows fill styles.
Michael Ludwig2686d692020-04-17 20:21:37 +0000733 GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500734 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400735 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500736 std::move(paint), shape, viewMatrix,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500737 gammaCorrect,
738 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700739}
740
741#endif