blob: 8410a256b28d25278e40289424e61731b630067d [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/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"
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 Phillipscac17642020-08-07 16:17:10 -040043GrSmallPathRenderer::GrSmallPathRenderer() : fAtlasMgr(new GrSmallPathAtlasMgr) {}
44
Robert Phillipsa4bb0642020-08-11 09:55:17 -040045GrSmallPathRenderer::~GrSmallPathRenderer() {}
Robert Phillipscac17642020-08-07 16:17:10 -040046
Robert Phillips5dd3d882020-08-10 09:29:39 -040047void GrSmallPathRenderer::addToOnFlushCallbacks(GrRecordingContext* rContext) {
48 rContext->priv().addOnFlushCallbackObject(fAtlasMgr.get());
49}
50
Chris Dalton5ed44232017-09-07 13:22:46 -060051GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -070052 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060053 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070054 }
55 // If the shape has no key then we won't get any reuse.
56 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060057 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070058 }
59 // This only supports filled paths, however, the caller may apply the style to make a filled
60 // path and try again.
61 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060062 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070063 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050064 // This does non-inverse coverage-based antialiased fills.
Chris Dalton6ce447a2019-06-23 18:07:38 -060065 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060066 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -070067 }
jvanverthfa38a302014-10-06 05:59:05 -070068 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -070069 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060070 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -070071 }
halcanary9d524f22016-03-29 09:03:52 -070072
Jim Van Verthf9e678d2017-02-15 15:46:52 -050073 // Only support paths with bounds within kMaxDim by kMaxDim,
74 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -050075 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -040076 SkScalar scaleFactors[2] = { 1, 1 };
77 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -060078 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050079 }
bsalomon0a0f67e2016-06-28 11:56:42 -070080 SkRect bounds = args.fShape->styledBounds();
Brian Osman116b33e2020-02-05 13:34:09 -050081 SkScalar minDim = std::min(bounds.width(), bounds.height());
82 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -050083 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
84 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -060085 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
86 return CanDrawPath::kNo;
87 }
bsalomon6266dca2016-03-11 06:22:00 -080088
Chris Dalton5ed44232017-09-07 13:22:46 -060089 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -070090}
91
jvanverthfa38a302014-10-06 05:59:05 -070092////////////////////////////////////////////////////////////////////////////////
93
joshualitt5bf99f12015-03-13 11:47:42 -070094// padding around path bounds to allow for antialiased pixels
Robert Phillips6d3bc292020-04-06 10:29:28 -040095static const int kAntiAliasPad = 1;
joshualitt5bf99f12015-03-13 11:47:42 -070096
Brian Salomonfebbd232017-07-11 15:52:02 -040097class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
98private:
99 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
100
joshualitt5bf99f12015-03-13 11:47:42 -0700101public:
Brian Salomon25a88092016-12-01 09:36:50 -0500102 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700103
Robert Phillipsb97da532019-02-12 15:24:12 -0500104 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400105 GrPaint&& paint,
Michael Ludwig2686d692020-04-17 20:21:37 +0000106 const GrStyledShape& shape,
Robert Phillips7c525e62018-06-12 10:11:12 -0400107 const SkMatrix& viewMatrix,
Robert Phillipscac17642020-08-07 16:17:10 -0400108 GrSmallPathAtlasMgr* atlasMgr,
Brian Salomonfebbd232017-07-11 15:52:02 -0400109 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 Phillipsa4bb0642020-08-11 09:55:17 -0400112 atlasMgr, gammaCorrect, stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700113 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500114
Michael Ludwig2686d692020-04-17 20:21:37 +0000115 SmallPathOp(Helper::MakeArgs helperArgs, const SkPMColor4f& color, const GrStyledShape& shape,
Robert Phillipscac17642020-08-07 16:17:10 -0400116 const SkMatrix& viewMatrix, GrSmallPathAtlasMgr* atlasMgr, bool gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -0400117 const GrUserStencilSettings* stencilSettings)
Robert Phillips4133dc42020-03-11 15:55:55 -0400118 : INHERITED(ClassID())
119 , fHelper(helperArgs, 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
Robert Phillipscac17642020-08-07 16:17:10 -0400135 fAtlasMgr = atlasMgr;
Brian Salomond0a0a652016-12-15 15:25:22 -0500136 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -0500137 }
138
Brian Salomonfebbd232017-07-11 15:52:02 -0400139 const char* name() const override { return "SmallPathOp"; }
140
Chris Dalton1706cbf2019-05-21 19:35:29 -0600141 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400142 fHelper.visitProxies(func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400143 }
144
Brian Osman9a390ac2018-11-12 09:47:48 -0500145#ifdef SK_DEBUG
Brian Salomonfebbd232017-07-11 15:52:02 -0400146 SkString dumpInfo() const override {
147 SkString string;
148 for (const auto& geo : fShapes) {
Brian Osmancf860852018-10-31 14:04:39 -0400149 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
Brian Salomonfebbd232017-07-11 15:52:02 -0400150 }
151 string += fHelper.dumpInfo();
152 string += INHERITED::dumpInfo();
153 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500154 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500155#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500156
Brian Salomonfebbd232017-07-11 15:52:02 -0400157 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
158
Chris Dalton6ce447a2019-06-23 18:07:38 -0600159 GrProcessorSet::Analysis finalize(
160 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
161 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700162 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600163 caps, clip, hasMixedSampledCoverage, clampType,
164 GrProcessorAnalysisCoverage::kSingleChannel, &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700165 }
166
Brian Salomonfebbd232017-07-11 15:52:02 -0400167private:
bsalomonb5238a72015-05-05 07:49:49 -0700168 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500169 sk_sp<const GrBuffer> fVertexBuffer;
170 sk_sp<const GrBuffer> fIndexBuffer;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500171 GrGeometryProcessor* fGeometryProcessor;
Chris Dalton304e14d2020-03-17 14:29:06 -0600172 const GrSurfaceProxy** fPrimProcProxies;
bsalomonb5238a72015-05-05 07:49:49 -0700173 int fVertexOffset;
174 int fInstancesToFlush;
175 };
176
Robert Phillips2669a7b2020-03-12 12:07:19 -0400177 GrProgramInfo* programInfo() override {
178 // TODO [PI]: implement
179 return nullptr;
180 }
181
Robert Phillips4133dc42020-03-11 15:55:55 -0400182 void onCreateProgramInfo(const GrCaps*,
183 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400184 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400185 GrAppliedClip&&,
186 const GrXferProcessor::DstProxyView&) override {
187 // TODO [PI]: implement
188 }
189
Robert Phillips2669a7b2020-03-12 12:07:19 -0400190 void onPrePrepareDraws(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400191 const GrSurfaceProxyView* writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400192 GrAppliedClip*,
193 const GrXferProcessor::DstProxyView&) override {
194 // TODO [PI]: implement
195 }
196
Brian Salomon91326c32017-08-09 16:02:19 -0400197 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500198 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700199
Brian Salomon7eae3e02018-08-07 14:02:38 +0000200 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500201 static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000202
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700203 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600204 flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400205
206 int numActiveProxies;
Robert Phillipscac17642020-08-07 16:17:10 -0400207 const GrSurfaceProxyView* views = fAtlasMgr->getViews(&numActiveProxies);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000208 for (int i = 0; i < numActiveProxies; ++i) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400209 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
210 // proxies don't get added during the visitProxies call. Thus we add them here.
Chris Dalton304e14d2020-03-17 14:29:06 -0600211 flushInfo.fPrimProcProxies[i] = views[i].proxy();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500212 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000213 }
Brian Salomon49348902018-06-26 09:12:38 -0400214
joshualitt5bf99f12015-03-13 11:47:42 -0700215 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400216 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500217 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500218 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400219 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500220 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
221 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
222 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
223
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400224 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500225 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400226 if (ctm.hasPerspective()) {
227 matrix = &ctm;
228 } else if (fHelper.usesLocalCoords()) {
229 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500230 return;
231 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400232 matrix = &invert;
233 } else {
234 matrix = &SkMatrix::I();
235 }
Brian Salomonccb61422020-01-09 10:46:36 -0500236 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
237 target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400238 views, numActiveProxies, GrSamplerState::Filter::kLinear,
Brian Salomonccb61422020-01-09 10:46:36 -0500239 flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400240 } else {
241 SkMatrix invert;
242 if (fHelper.usesLocalCoords()) {
243 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400244 return;
245 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500246 }
247
Brian Salomonccb61422020-01-09 10:46:36 -0500248 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
249 target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400250 views, numActiveProxies, GrSamplerState::Filter::kNearest,
Brian Salomonccb61422020-01-09 10:46:36 -0500251 kA8_GrMaskFormat, invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500252 }
joshualitt5bf99f12015-03-13 11:47:42 -0700253
joshualitt5bf99f12015-03-13 11:47:42 -0700254 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500255 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400256
257 // We need to make sure we don't overflow a 32 bit int when we request space in the
258 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400259 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400260 return;
261 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400262 GrVertexWriter vertices{ target->makeVertexSpace(
263 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
264 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
265
266 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500267 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700268 SkDebugf("Could not allocate vertices\n");
269 return;
270 }
271
bsalomonb5238a72015-05-05 07:49:49 -0700272 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700273 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500274 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700275
Robert Phillips34949e32020-08-05 15:54:31 -0400276 GrSmallPathShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500277 if (fUsesDistanceField) {
278 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400279 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500280 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400281 if (args.fViewMatrix.hasPerspective()) {
282 // approximate the scale since we can't get it from the matrix
283 SkRect xformedBounds;
284 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Brian Osman788b9162020-02-07 10:36:46 -0500285 maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(),
Jim Van Verth51245932017-10-12 11:07:29 -0400286 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400287 } else {
288 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
289 }
Brian Osman116b33e2020-02-05 13:34:09 -0500290 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verth33632d82017-02-28 10:24:39 -0500291 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
292 // In the majority of cases this will yield a crisper rendering.
293 SkScalar mipScale = 1.0f;
294 // Our mipscale is the maxScale clamped to the next highest power of 2
295 if (maxScale <= SK_ScalarHalf) {
296 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
297 mipScale = SkScalarPow(2, -log);
298 } else if (maxScale > SK_Scalar1) {
299 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
300 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700301 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500302 SkASSERT(maxScale <= mipScale);
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 Phillips109ff202020-08-10 16:39:31 -0400325 shapeData = fAtlasMgr->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 Phillipscac17642020-08-07 16:17:10 -0400331 fAtlasMgr->atlas(),
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 Phillipscac17642020-08-07 16:17:10 -0400336 fAtlasMgr->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 Phillips109ff202020-08-10 16:39:31 -0400342 shapeData = fAtlasMgr->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 Phillipscac17642020-08-07 16:17:10 -0400346 fAtlasMgr->atlas(),
Robert Phillips8296e752017-08-25 08:45:21 -0400347 shapeData,
348 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400349 args.fViewMatrix)) {
Robert Phillipscac17642020-08-07 16:17:10 -0400350 fAtlasMgr->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 Phillipscac17642020-08-07 16:17:10 -0400357 fAtlasMgr->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 Phillipsd2e9f762018-03-07 11:54:37 -0500367 bool addToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo, GrDrawOpAtlas* atlas,
368 int width, int height, const void* image,
Robert Phillips6d3bc292020-04-06 10:29:28 -0400369 GrDrawOpAtlas::AtlasLocator* atlasLocator) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500370 auto resourceProvider = target->resourceProvider();
371 auto uploadTarget = target->deferredUploadTarget();
372
Robert Phillips6d3bc292020-04-06 10:29:28 -0400373 GrDrawOpAtlas::ErrorCode code = atlas->addToAtlas(resourceProvider, uploadTarget,
374 width, height, image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500375 if (GrDrawOpAtlas::ErrorCode::kError == code) {
376 return false;
377 }
378
379 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
380 this->flush(target, flushInfo);
381
Robert Phillips6d3bc292020-04-06 10:29:28 -0400382 code = atlas->addToAtlas(resourceProvider, uploadTarget, width, height,
383 image, atlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500384 }
385
386 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
387 }
388
Brian Salomone5b399e2017-07-19 13:50:54 -0400389 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Robert Phillips34949e32020-08-05 15:54:31 -0400390 GrDrawOpAtlas* atlas, GrSmallPathShapeData* shapeData,
391 const GrStyledShape& shape, uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500392
bsalomonee432412016-06-27 07:18:18 -0700393 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700394
395 // generate bounding rect for bitmap draw
396 SkRect scaledBounds = bounds;
397 // scale to mip level size
398 scaledBounds.fLeft *= scale;
399 scaledBounds.fTop *= scale;
400 scaledBounds.fRight *= scale;
401 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500402 // subtract out integer portion of origin
403 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500404 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
405 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700406 scaledBounds.offset(-dx, -dy);
407 // get integer boundary
408 SkIRect devPathBounds;
409 scaledBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400410 // place devBounds at origin with padding to allow room for antialiasing
411 int width = devPathBounds.width() + 2 * kAntiAliasPad;
412 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500413 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400414 SkScalar translateX = kAntiAliasPad - dx;
415 SkScalar translateY = kAntiAliasPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700416
417 // draw path to bitmap
418 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500419 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400420 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700421
jvanverth512e4372015-11-23 11:50:02 -0800422 SkASSERT(devPathBounds.fLeft == 0);
423 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500424 SkASSERT(devPathBounds.width() > 0);
425 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700426
joel.liang8cbb4242017-01-09 18:39:43 -0800427 // setup signed distance field storage
428 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
429 width = dfBounds.width();
430 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800431 // TODO We should really generate this directly into the plot somehow
432 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800433
joel.liang8cbb4242017-01-09 18:39:43 -0800434 SkPath path;
435 shape.asPath(&path);
joel.liang8cbb4242017-01-09 18:39:43 -0800436 // Generate signed distance field directly from SkPath
437 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400438 path, drawMatrix, width, height,
439 width * sizeof(unsigned char));
joel.liang8cbb4242017-01-09 18:39:43 -0800440 if (!succeed) {
joel.liang8cbb4242017-01-09 18:39:43 -0800441 // setup bitmap backing
442 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400443 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
joel.liang8cbb4242017-01-09 18:39:43 -0800444 return false;
445 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400446 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800447
448 // rasterize path
449 SkPaint paint;
450 paint.setStyle(SkPaint::kFill_Style);
451 paint.setAntiAlias(true);
452
453 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800454
455 SkRasterClip rasterClip;
456 rasterClip.setRect(devPathBounds);
457 draw.fRC = &rasterClip;
Brian Osman9aaec362020-05-08 14:54:37 -0400458 SkSimpleMatrixProvider matrixProvider(drawMatrix);
459 draw.fMatrixProvider = &matrixProvider;
joel.liang8cbb4242017-01-09 18:39:43 -0800460 draw.fDst = dst;
461
462 draw.drawPathCoverage(path, paint);
463
464 // Generate signed distance field
465 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
466 (const unsigned char*)dst.addr(),
467 dst.width(), dst.height(), dst.rowBytes());
joel.liang8cbb4242017-01-09 18:39:43 -0800468 }
joshualitt5bf99f12015-03-13 11:47:42 -0700469
470 // add to atlas
Robert Phillips6d3bc292020-04-06 10:29:28 -0400471 if (!this->addToAtlas(target, flushInfo, atlas, width, height, dfStorage.get(),
472 &shapeData->fAtlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500473 return false;
joshualitt5bf99f12015-03-13 11:47:42 -0700474 }
475
Herb Derby1318e452020-08-03 14:38:10 -0400476 shapeData->fAtlasLocator.insetSrc(SK_DistanceFieldPad);
477
Robert Phillips3cf781d2017-08-22 18:25:11 -0400478 shapeData->fBounds = SkRect::Make(devPathBounds);
479 shapeData->fBounds.offset(-translateX, -translateY);
480 shapeData->fBounds.fLeft /= scale;
481 shapeData->fBounds.fTop /= scale;
482 shapeData->fBounds.fRight /= scale;
483 shapeData->fBounds.fBottom /= scale;
joshualitt5bf99f12015-03-13 11:47:42 -0700484 return true;
485 }
486
Brian Salomone5b399e2017-07-19 13:50:54 -0400487 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Robert Phillips34949e32020-08-05 15:54:31 -0400488 GrDrawOpAtlas* atlas, GrSmallPathShapeData* shapeData,
489 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
547 // add to atlas
Robert Phillips6d3bc292020-04-06 10:29:28 -0400548 if (!this->addToAtlas(target, flushInfo, atlas, dst.width(), dst.height(), dst.addr(),
549 &shapeData->fAtlasLocator)) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500550 return false;
Jim Van Verth33632d82017-02-28 10:24:39 -0500551 }
552
Jim Van Verth33632d82017-02-28 10:24:39 -0500553 shapeData->fBounds = SkRect::Make(devPathBounds);
554 shapeData->fBounds.offset(-translateX, -translateY);
Jim Van Verth33632d82017-02-28 10:24:39 -0500555 return true;
556 }
557
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400558 void writePathVertices(GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500559 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400560 const SkMatrix& ctm,
Robert Phillips34949e32020-08-05 15:54:31 -0400561 const GrSmallPathShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500562 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400563 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500564 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
565 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400566 }
Jim Van Verth77047542017-01-11 14:17:00 -0500567
Brian Osman0dd43022018-11-16 15:53:26 -0500568 // set up texture coordinates
Herb Derby1318e452020-08-03 14:38:10 -0400569 auto texCoords = GrVertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs());
Brian Osman0dd43022018-11-16 15:53:26 -0500570
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400571 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500572 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500573 color,
574 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400575 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500576 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
577 color,
578 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400579 }
joshualitt5bf99f12015-03-13 11:47:42 -0700580 }
581
Brian Salomone5b399e2017-07-19 13:50:54 -0400582 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400583
584 int numActiveProxies;
Robert Phillipscac17642020-08-07 16:17:10 -0400585 const GrSurfaceProxyView* views = fAtlasMgr->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
589 if (gp->numTextureSamplers() != numActiveProxies) {
590 for (int i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600591 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400592 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
593 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500594 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000595 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400596 // During preparation the number of atlas pages has increased.
597 // Update the proxies used in the GP to match.
598 if (fUsesDistanceField) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500599 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400600 views, numActiveProxies, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400601 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500602 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400603 views, numActiveProxies, GrSamplerState::Filter::kNearest);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400604 }
605 }
606
bsalomon6d6b6ad2016-07-13 14:45:28 -0700607 if (flushInfo->fInstancesToFlush) {
Chris Daltoneb694b72020-03-16 09:25:50 -0600608 GrSimpleMesh* mesh = target->allocMesh();
Robert Phillipsee08d522019-10-28 16:34:44 -0400609 mesh->setIndexedPatterned(flushInfo->fIndexBuffer,
610 GrResourceProvider::NumIndicesPerNonAAQuad(),
Robert Phillipsee08d522019-10-28 16:34:44 -0400611 flushInfo->fInstancesToFlush,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600612 GrResourceProvider::MaxNumNonAAQuads(),
613 flushInfo->fVertexBuffer,
614 GrResourceProvider::NumVertsPerNonAAQuad(),
615 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600616 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
617 GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400618 flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() *
619 flushInfo->fInstancesToFlush;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700620 flushInfo->fInstancesToFlush = 0;
621 }
joshualitt5bf99f12015-03-13 11:47:42 -0700622 }
623
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700624 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips6c59fe42020-02-27 09:30:37 -0500625 auto pipeline = fHelper.createPipelineWithStencil(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -0500626
627 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
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
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500633 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
634 const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400635 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400636 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000637 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700638 }
639
Jim Van Verth33632d82017-02-28 10:24:39 -0500640 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000641 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500642 }
643
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400644 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
645 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
646
647 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000648 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700649 }
650
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400651 // We can position on the cpu unless we're in perspective,
652 // but also need to make sure local matrices are identical
653 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
Mike Reed2c383152019-12-18 16:47:47 -0500654 !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000655 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500656 }
657
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400658 // Depending on the ctm we may have a different shader for SDF paths
659 if (this->usesDistanceField()) {
660 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
661 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000662 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400663 }
664 }
665
Brian Salomond0a0a652016-12-15 15:25:22 -0500666 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500667 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000668 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700669 }
670
Jim Van Verth33632d82017-02-28 10:24:39 -0500671 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700672
Brian Salomond0a0a652016-12-15 15:25:22 -0500673 struct Entry {
Michael Ludwig2686d692020-04-17 20:21:37 +0000674 SkPMColor4f fColor;
675 GrStyledShape fShape;
676 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700677 };
678
Brian Salomond0a0a652016-12-15 15:25:22 -0500679 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400680 Helper fHelper;
Robert Phillipscac17642020-08-07 16:17:10 -0400681 GrSmallPathAtlasMgr* fAtlasMgr;
brianosman0e3c5542016-04-13 13:56:21 -0700682 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500683 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700684
Brian Salomonfebbd232017-07-11 15:52:02 -0400685 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700686};
687
Jim Van Verth83010462017-03-16 08:45:39 -0400688bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400689 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400690 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700691
Robert Phillips5dd3d882020-08-10 09:29:39 -0400692 const GrCaps* caps = args.fContext->priv().caps();
693
jvanverthfa38a302014-10-06 05:59:05 -0700694 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700695 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700696 SkASSERT(args.fShape->hasUnstyledKey());
Robert Phillips5dd3d882020-08-10 09:29:39 -0400697 if (!fAtlasMgr->initAtlas(args.fContext->priv().proxyProvider(), caps)) {
Robert Phillipscac17642020-08-07 16:17:10 -0400698 return false;
jvanverthfa38a302014-10-06 05:59:05 -0700699 }
700
Brian Salomonfebbd232017-07-11 15:52:02 -0400701 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
Robert Phillipscac17642020-08-07 16:17:10 -0400702 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
703 fAtlasMgr.get(), args.fGammaCorrect, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400704 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800705
jvanverthfa38a302014-10-06 05:59:05 -0700706 return true;
707}
708
joshualitt21279c72015-05-11 07:21:37 -0700709///////////////////////////////////////////////////////////////////////////////////////////////////
710
Hal Canary6f6961e2017-01-31 13:50:44 -0500711#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700712
Robert Phillips5dd3d882020-08-10 09:29:39 -0400713struct GrSmallPathRenderer::PathTestStruct {
Robert Phillipscac17642020-08-07 16:17:10 -0400714 PathTestStruct() : fContextID(SK_InvalidGenID) {}
Robert Phillips5dd3d882020-08-10 09:29:39 -0400715 ~PathTestStruct() { }
joshualitt21279c72015-05-11 07:21:37 -0700716
717 uint32_t fContextID;
Robert Phillipscac17642020-08-07 16:17:10 -0400718 std::unique_ptr<GrSmallPathAtlasMgr> fAtlasMgr;
joshualitt21279c72015-05-11 07:21:37 -0700719};
720
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500721std::unique_ptr<GrDrawOp> GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillipsb97da532019-02-12 15:24:12 -0500722 GrRecordingContext* context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500723 GrPaint&& paint,
Michael Ludwig2686d692020-04-17 20:21:37 +0000724 const GrStyledShape& shape,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500725 const SkMatrix& viewMatrix,
Robert Phillipscac17642020-08-07 16:17:10 -0400726 GrSmallPathAtlasMgr* atlasMgr,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500727 bool gammaCorrect,
728 const GrUserStencilSettings* stencil) {
729
Robert Phillips7c525e62018-06-12 10:11:12 -0400730 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
Robert Phillipscac17642020-08-07 16:17:10 -0400731 atlasMgr, gammaCorrect, stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500732}
733
Brian Salomonfebbd232017-07-11 15:52:02 -0400734GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
Robert Phillips5dd3d882020-08-10 09:29:39 -0400735 static GrSmallPathRenderer::PathTestStruct gTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700736
Robert Phillips9da87e02019-02-04 13:26:26 -0500737 if (context->priv().contextID() != gTestStruct.fContextID) {
738 gTestStruct.fContextID = context->priv().contextID();
Robert Phillipscac17642020-08-07 16:17:10 -0400739 gTestStruct.fAtlasMgr = std::make_unique<GrSmallPathAtlasMgr>();
740 gTestStruct.fAtlasMgr->initAtlas(context->priv().proxyProvider(),
Robert Phillips5dd3d882020-08-10 09:29:39 -0400741 context->priv().caps());
joshualitt21279c72015-05-11 07:21:37 -0700742 }
743
744 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700745 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700746
bsalomonee432412016-06-27 07:18:18 -0700747 // This path renderer only allows fill styles.
Michael Ludwig2686d692020-04-17 20:21:37 +0000748 GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500749 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400750 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500751 std::move(paint), shape, viewMatrix,
Robert Phillipscac17642020-08-07 16:17:10 -0400752 gTestStruct.fAtlasMgr.get(),
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500753 gammaCorrect,
754 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700755}
756
757#endif