blob: d80dedc488294974e6cd2ad646c47465977f1564 [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.
75 if (scaleFactors[1]/scaleFactors[0] > 4) {
76 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
Chris Dalton1706cbf2019-05-21 19:35:29 -0600140 void visitProxies(const VisitProxyFunc& 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,
Robert Phillips4133dc42020-03-11 15:55:55 -0400171 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400172 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500173 GrXferBarrierFlags renderPassXferBarriers,
174 GrLoadOp colorLoadOp) override {
Robert Phillips326f1d72020-10-01 09:43:29 -0400175 // We cannot surface the SmallPathOp's programInfo at record time. As currently
176 // implemented, the GP is modified at flush time based on the number of pages in the
177 // atlas.
Robert Phillips4133dc42020-03-11 15:55:55 -0400178 }
179
Robert Phillips2669a7b2020-03-12 12:07:19 -0400180 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500181 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400182 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400183 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500184 GrXferBarrierFlags renderPassXferBarriers,
185 GrLoadOp colorLoadOp) override {
Robert Phillips2669a7b2020-03-12 12:07:19 -0400186 // TODO [PI]: implement
187 }
188
Brian Salomon91326c32017-08-09 16:02:19 -0400189 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500190 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700191
Robert Phillips079455c2020-08-11 15:18:46 -0400192 GrSmallPathAtlasMgr* atlasMgr = target->smallPathAtlasManager();
193 if (!atlasMgr) {
194 return;
195 }
196
Brian Salomon7eae3e02018-08-07 14:02:38 +0000197 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500198 static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000199
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700200 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600201 flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400202
203 int numActiveProxies;
Robert Phillips079455c2020-08-11 15:18:46 -0400204 const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000205 for (int i = 0; i < numActiveProxies; ++i) {
Greg Danielb20d7e52019-09-03 13:54:39 -0400206 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
207 // proxies don't get added during the visitProxies call. Thus we add them here.
Chris Dalton304e14d2020-03-17 14:29:06 -0600208 flushInfo.fPrimProcProxies[i] = views[i].proxy();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500209 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000210 }
Brian Salomon49348902018-06-26 09:12:38 -0400211
joshualitt5bf99f12015-03-13 11:47:42 -0700212 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400213 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500214 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500215 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400216 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500217 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
218 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
219 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
220
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400221 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500222 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400223 if (ctm.hasPerspective()) {
224 matrix = &ctm;
225 } else if (fHelper.usesLocalCoords()) {
226 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500227 return;
228 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400229 matrix = &invert;
230 } else {
231 matrix = &SkMatrix::I();
232 }
Brian Salomonccb61422020-01-09 10:46:36 -0500233 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
234 target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400235 views, numActiveProxies, GrSamplerState::Filter::kLinear,
Brian Salomonccb61422020-01-09 10:46:36 -0500236 flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400237 } else {
238 SkMatrix invert;
239 if (fHelper.usesLocalCoords()) {
240 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400241 return;
242 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500243 }
244
Brian Salomonccb61422020-01-09 10:46:36 -0500245 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
246 target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400247 views, numActiveProxies, GrSamplerState::Filter::kNearest,
Brian Salomonccb61422020-01-09 10:46:36 -0500248 kA8_GrMaskFormat, invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500249 }
joshualitt5bf99f12015-03-13 11:47:42 -0700250
joshualitt5bf99f12015-03-13 11:47:42 -0700251 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500252 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400253
254 // We need to make sure we don't overflow a 32 bit int when we request space in the
255 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400256 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400257 return;
258 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400259 GrVertexWriter vertices{ target->makeVertexSpace(
260 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
261 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
262
263 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500264 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700265 SkDebugf("Could not allocate vertices\n");
266 return;
267 }
268
bsalomonb5238a72015-05-05 07:49:49 -0700269 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700270 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500271 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700272
Robert Phillips34949e32020-08-05 15:54:31 -0400273 GrSmallPathShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500274 if (fUsesDistanceField) {
275 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400276 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500277 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400278 if (args.fViewMatrix.hasPerspective()) {
279 // approximate the scale since we can't get it from the matrix
280 SkRect xformedBounds;
281 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Brian Osman788b9162020-02-07 10:36:46 -0500282 maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(),
Jim Van Verth51245932017-10-12 11:07:29 -0400283 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400284 } else {
285 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
286 }
Brian Osman116b33e2020-02-05 13:34:09 -0500287 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verth33632d82017-02-28 10:24:39 -0500288 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
289 // In the majority of cases this will yield a crisper rendering.
290 SkScalar mipScale = 1.0f;
291 // Our mipscale is the maxScale clamped to the next highest power of 2
292 if (maxScale <= SK_ScalarHalf) {
293 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
294 mipScale = SkScalarPow(2, -log);
295 } else if (maxScale > SK_Scalar1) {
296 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
297 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700298 }
Jim Van Verthcbbd5652021-04-07 14:27:55 -0400299 // Log2 isn't very precise at values close to a power of 2,
300 // so add a little tolerance here. A little bit of scaling up is fine.
301 SkASSERT(maxScale <= mipScale + SK_ScalarNearlyZero);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500302
Jim Van Verth33632d82017-02-28 10:24:39 -0500303 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
304 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
305 // so we can preserve as much detail as possible. However, we can't scale down more
306 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
307 // just bigger than the ideal, and then scale down until we are no more than 4x the
308 // original mipsize.
309 if (mipSize < kIdealMinMIP) {
310 SkScalar newMipSize = mipSize;
311 do {
312 newMipSize *= 2;
313 } while (newMipSize < kIdealMinMIP);
314 while (newMipSize > 4 * mipSize) {
315 newMipSize *= 0.25f;
316 }
317 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700318 }
Robert Phillips109ff202020-08-10 16:39:31 -0400319
Brian Osman788b9162020-02-07 10:36:46 -0500320 SkScalar desiredDimension = std::min(mipSize, kMaxMIP);
Robert Phillips109ff202020-08-10 16:39:31 -0400321 int ceilDesiredDimension = SkScalarCeilToInt(desiredDimension);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500322
Jim Van Verth33632d82017-02-28 10:24:39 -0500323 // check to see if df path is cached
Robert Phillips079455c2020-08-11 15:18:46 -0400324 shapeData = atlasMgr->findOrCreate(args.fShape, ceilDesiredDimension);
Robert Phillips6507e632020-08-07 14:35:56 -0400325 if (!shapeData->fAtlasLocator.plotLocator().isValid()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500326 SkScalar scale = desiredDimension / maxDim;
327
Jim Van Verth33632d82017-02-28 10:24:39 -0500328 if (!this->addDFPathToAtlas(target,
329 &flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400330 atlasMgr,
Jim Van Verth33632d82017-02-28 10:24:39 -0500331 shapeData,
332 args.fShape,
Robert Phillips109ff202020-08-10 16:39:31 -0400333 ceilDesiredDimension,
Jim Van Verth33632d82017-02-28 10:24:39 -0500334 scale)) {
Robert Phillips079455c2020-08-11 15:18:46 -0400335 atlasMgr->deleteCacheEntry(shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500336 continue;
337 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500338 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500339 } else {
340 // check to see if bitmap path is cached
Robert Phillips079455c2020-08-11 15:18:46 -0400341 shapeData = atlasMgr->findOrCreate(args.fShape, args.fViewMatrix);
Robert Phillips6507e632020-08-07 14:35:56 -0400342 if (!shapeData->fAtlasLocator.plotLocator().isValid()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500343 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400344 &flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400345 atlasMgr,
Robert Phillips8296e752017-08-25 08:45:21 -0400346 shapeData,
347 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400348 args.fViewMatrix)) {
Robert Phillips079455c2020-08-11 15:18:46 -0400349 atlasMgr->deleteCacheEntry(shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500350 continue;
351 }
352 }
joshualitt5bf99f12015-03-13 11:47:42 -0700353 }
354
Robert Phillips40a29d72018-01-18 12:59:22 -0500355 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips079455c2020-08-11 15:18:46 -0400356 atlasMgr->setUseToken(shapeData, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700357
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400358 this->writePathVertices(vertices, GrVertexColor(args.fColor, fWideColor),
Brian Osmanc906d252018-12-04 11:17:46 -0500359 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700360 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700361 }
362
bsalomon75398562015-08-17 12:55:38 -0700363 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700364 }
365
Robert Phillips6704bc82020-08-14 12:45:07 -0400366 bool addToAtlasWithRetry(GrMeshDrawOp::Target* target,
367 FlushInfo* flushInfo,
368 GrSmallPathAtlasMgr* atlasMgr,
369 int width, int height, const void* image,
370 const SkRect& bounds, int srcInset,
371 GrSmallPathShapeData* shapeData) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500372 auto resourceProvider = target->resourceProvider();
373 auto uploadTarget = target->deferredUploadTarget();
374
Robert Phillips6704bc82020-08-14 12:45:07 -0400375 auto code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height,
376 image, &shapeData->fAtlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500377 if (GrDrawOpAtlas::ErrorCode::kError == code) {
378 return false;
379 }
380
381 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
382 this->flush(target, flushInfo);
383
Robert Phillips6704bc82020-08-14 12:45:07 -0400384 code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height,
385 image, &shapeData->fAtlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500386 }
387
Robert Phillips6704bc82020-08-14 12:45:07 -0400388 shapeData->fAtlasLocator.insetSrc(srcInset);
389 shapeData->fBounds = bounds;
390
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500391 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
392 }
393
Brian Salomone5b399e2017-07-19 13:50:54 -0400394 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400395 GrSmallPathAtlasMgr* atlasMgr, GrSmallPathShapeData* shapeData,
Robert Phillips34949e32020-08-05 15:54:31 -0400396 const GrStyledShape& shape, uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500397
bsalomonee432412016-06-27 07:18:18 -0700398 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700399
400 // generate bounding rect for bitmap draw
401 SkRect scaledBounds = bounds;
402 // scale to mip level size
403 scaledBounds.fLeft *= scale;
404 scaledBounds.fTop *= scale;
405 scaledBounds.fRight *= scale;
406 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500407 // subtract out integer portion of origin
408 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500409 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
410 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700411 scaledBounds.offset(-dx, -dy);
412 // get integer boundary
413 SkIRect devPathBounds;
414 scaledBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400415 // place devBounds at origin with padding to allow room for antialiasing
416 int width = devPathBounds.width() + 2 * kAntiAliasPad;
417 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500418 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400419 SkScalar translateX = kAntiAliasPad - dx;
420 SkScalar translateY = kAntiAliasPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700421
422 // draw path to bitmap
423 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500424 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400425 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700426
jvanverth512e4372015-11-23 11:50:02 -0800427 SkASSERT(devPathBounds.fLeft == 0);
428 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500429 SkASSERT(devPathBounds.width() > 0);
430 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700431
joel.liang8cbb4242017-01-09 18:39:43 -0800432 // setup signed distance field storage
433 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
434 width = dfBounds.width();
435 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800436 // TODO We should really generate this directly into the plot somehow
437 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800438
joel.liang8cbb4242017-01-09 18:39:43 -0800439 SkPath path;
440 shape.asPath(&path);
joel.liang8cbb4242017-01-09 18:39:43 -0800441 // Generate signed distance field directly from SkPath
442 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400443 path, drawMatrix, width, height,
444 width * sizeof(unsigned char));
joel.liang8cbb4242017-01-09 18:39:43 -0800445 if (!succeed) {
joel.liang8cbb4242017-01-09 18:39:43 -0800446 // setup bitmap backing
447 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400448 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
joel.liang8cbb4242017-01-09 18:39:43 -0800449 return false;
450 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400451 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800452
453 // rasterize path
454 SkPaint paint;
455 paint.setStyle(SkPaint::kFill_Style);
456 paint.setAntiAlias(true);
457
458 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800459
460 SkRasterClip rasterClip;
461 rasterClip.setRect(devPathBounds);
462 draw.fRC = &rasterClip;
Brian Osman9aaec362020-05-08 14:54:37 -0400463 SkSimpleMatrixProvider matrixProvider(drawMatrix);
464 draw.fMatrixProvider = &matrixProvider;
joel.liang8cbb4242017-01-09 18:39:43 -0800465 draw.fDst = dst;
466
467 draw.drawPathCoverage(path, paint);
468
469 // Generate signed distance field
470 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
471 (const unsigned char*)dst.addr(),
472 dst.width(), dst.height(), dst.rowBytes());
joel.liang8cbb4242017-01-09 18:39:43 -0800473 }
joshualitt5bf99f12015-03-13 11:47:42 -0700474
Robert Phillips6704bc82020-08-14 12:45:07 -0400475 SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY);
476 drawBounds.fLeft /= scale;
477 drawBounds.fTop /= scale;
478 drawBounds.fRight /= scale;
479 drawBounds.fBottom /= scale;
joshualitt5bf99f12015-03-13 11:47:42 -0700480
Robert Phillips6704bc82020-08-14 12:45:07 -0400481 return this->addToAtlasWithRetry(target, flushInfo, atlasMgr,
482 width, height, dfStorage.get(),
483 drawBounds, SK_DistanceFieldPad, shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700484 }
485
Brian Salomone5b399e2017-07-19 13:50:54 -0400486 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400487 GrSmallPathAtlasMgr* atlasMgr, GrSmallPathShapeData* shapeData,
Robert Phillips34949e32020-08-05 15:54:31 -0400488 const GrStyledShape& shape, const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500489 const SkRect& bounds = shape.bounds();
490 if (bounds.isEmpty()) {
491 return false;
492 }
493 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500494 SkScalar tx = ctm.getTranslateX();
495 SkScalar ty = ctm.getTranslateY();
496 tx -= SkScalarFloorToScalar(tx);
497 ty -= SkScalarFloorToScalar(ty);
498 drawMatrix.set(SkMatrix::kMTransX, tx);
499 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500500 SkRect shapeDevBounds;
501 drawMatrix.mapRect(&shapeDevBounds, bounds);
502 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
503 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
504
505 // get integer boundary
506 SkIRect devPathBounds;
507 shapeDevBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400508 // place devBounds at origin with padding to allow room for antialiasing
509 int width = devPathBounds.width() + 2 * kAntiAliasPad;
510 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verth33632d82017-02-28 10:24:39 -0500511 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400512 SkScalar translateX = kAntiAliasPad - dx;
513 SkScalar translateY = kAntiAliasPad - dy;
Jim Van Verth33632d82017-02-28 10:24:39 -0500514
515 SkASSERT(devPathBounds.fLeft == 0);
516 SkASSERT(devPathBounds.fTop == 0);
517 SkASSERT(devPathBounds.width() > 0);
518 SkASSERT(devPathBounds.height() > 0);
519
520 SkPath path;
521 shape.asPath(&path);
522 // setup bitmap backing
523 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400524 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500525 return false;
526 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400527 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500528
529 // rasterize path
530 SkPaint paint;
531 paint.setStyle(SkPaint::kFill_Style);
532 paint.setAntiAlias(true);
533
534 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500535
536 SkRasterClip rasterClip;
537 rasterClip.setRect(devPathBounds);
538 draw.fRC = &rasterClip;
539 drawMatrix.postTranslate(translateX, translateY);
Brian Osman9aaec362020-05-08 14:54:37 -0400540 SkSimpleMatrixProvider matrixProvider(drawMatrix);
541 draw.fMatrixProvider = &matrixProvider;
Jim Van Verth33632d82017-02-28 10:24:39 -0500542 draw.fDst = dst;
543
544 draw.drawPathCoverage(path, paint);
545
Robert Phillips6704bc82020-08-14 12:45:07 -0400546 SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY);
Jim Van Verth33632d82017-02-28 10:24:39 -0500547
Robert Phillips6704bc82020-08-14 12:45:07 -0400548 return this->addToAtlasWithRetry(target, flushInfo, atlasMgr,
549 dst.width(), dst.height(), dst.addr(),
550 drawBounds, 0, shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500551 }
552
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400553 void writePathVertices(GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500554 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400555 const SkMatrix& ctm,
Robert Phillips34949e32020-08-05 15:54:31 -0400556 const GrSmallPathShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500557 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400558 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500559 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
560 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400561 }
Jim Van Verth77047542017-01-11 14:17:00 -0500562
Brian Osman0dd43022018-11-16 15:53:26 -0500563 // set up texture coordinates
Herb Derby1318e452020-08-03 14:38:10 -0400564 auto texCoords = GrVertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs());
Brian Osman0dd43022018-11-16 15:53:26 -0500565
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400566 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500567 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500568 color,
569 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400570 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500571 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
572 color,
573 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400574 }
joshualitt5bf99f12015-03-13 11:47:42 -0700575 }
576
Brian Salomone5b399e2017-07-19 13:50:54 -0400577 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Robert Phillips079455c2020-08-11 15:18:46 -0400578 GrSmallPathAtlasMgr* atlasMgr = target->smallPathAtlasManager();
579 if (!atlasMgr) {
580 return;
581 }
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400582
583 int numActiveProxies;
Robert Phillips079455c2020-08-11 15:18:46 -0400584 const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400585
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500586 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400587 if (gp->numTextureSamplers() != numActiveProxies) {
588 for (int i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600589 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Greg Danielb20d7e52019-09-03 13:54:39 -0400590 // This op does not know its atlas proxies when it is added to a GrOpsTasks, so the
591 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500592 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000593 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400594 // During preparation the number of atlas pages has increased.
595 // Update the proxies used in the GP to match.
596 if (fUsesDistanceField) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500597 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400598 views, numActiveProxies, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400599 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500600 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400601 views, numActiveProxies, GrSamplerState::Filter::kNearest);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400602 }
603 }
604
bsalomon6d6b6ad2016-07-13 14:45:28 -0700605 if (flushInfo->fInstancesToFlush) {
Chris Daltoneb694b72020-03-16 09:25:50 -0600606 GrSimpleMesh* mesh = target->allocMesh();
Robert Phillipsee08d522019-10-28 16:34:44 -0400607 mesh->setIndexedPatterned(flushInfo->fIndexBuffer,
608 GrResourceProvider::NumIndicesPerNonAAQuad(),
Robert Phillipsee08d522019-10-28 16:34:44 -0400609 flushInfo->fInstancesToFlush,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600610 GrResourceProvider::MaxNumNonAAQuads(),
611 flushInfo->fVertexBuffer,
612 GrResourceProvider::NumVertsPerNonAAQuad(),
613 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600614 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
615 GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400616 flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() *
617 flushInfo->fInstancesToFlush;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700618 flushInfo->fInstancesToFlush = 0;
619 }
joshualitt5bf99f12015-03-13 11:47:42 -0700620 }
621
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700622 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600623 auto pipeline = fHelper.createPipeline(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -0500624
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600625 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
626 fHelper.stencilSettings());
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700627 }
628
Brian Osmancf860852018-10-31 14:04:39 -0400629 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500630 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700631
Herb Derbye25c3002020-10-27 15:57:27 -0400632 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400633 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400634 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000635 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700636 }
637
Jim Van Verth33632d82017-02-28 10:24:39 -0500638 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000639 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500640 }
641
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400642 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
643 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
644
645 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000646 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700647 }
648
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400649 // We can position on the cpu unless we're in perspective,
650 // but also need to make sure local matrices are identical
651 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
Mike Reed2c383152019-12-18 16:47:47 -0500652 !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000653 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500654 }
655
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400656 // Depending on the ctm we may have a different shader for SDF paths
657 if (this->usesDistanceField()) {
658 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
659 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000660 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400661 }
662 }
663
Brian Salomond0a0a652016-12-15 15:25:22 -0500664 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500665 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000666 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700667 }
668
John Stilesaf366522020-08-13 09:57:34 -0400669#if GR_TEST_UTILS
670 SkString onDumpInfo() const override {
671 SkString string;
672 for (const auto& geo : fShapes) {
673 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
674 }
675 string += fHelper.dumpInfo();
676 return string;
677 }
678#endif
679
Jim Van Verth33632d82017-02-28 10:24:39 -0500680 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700681
Brian Salomond0a0a652016-12-15 15:25:22 -0500682 struct Entry {
Michael Ludwig2686d692020-04-17 20:21:37 +0000683 SkPMColor4f fColor;
684 GrStyledShape fShape;
685 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700686 };
687
Brian Salomond0a0a652016-12-15 15:25:22 -0500688 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400689 Helper fHelper;
brianosman0e3c5542016-04-13 13:56:21 -0700690 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500691 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700692
John Stiles7571f9e2020-09-02 22:42:33 -0400693 using INHERITED = GrMeshDrawOp;
joshualitt5bf99f12015-03-13 11:47:42 -0700694};
695
Jim Van Verth83010462017-03-16 08:45:39 -0400696bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400697 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400698 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700699
jvanverthfa38a302014-10-06 05:59:05 -0700700 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700701 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700702 SkASSERT(args.fShape->hasUnstyledKey());
jvanverthfa38a302014-10-06 05:59:05 -0700703
Herb Derbyc76d4092020-10-07 16:46:15 -0400704 GrOp::Owner op = SmallPathOp::Make(
Robert Phillipscac17642020-08-07 16:17:10 -0400705 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -0400706 args.fGammaCorrect, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400707 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800708
jvanverthfa38a302014-10-06 05:59:05 -0700709 return true;
710}
711
joshualitt21279c72015-05-11 07:21:37 -0700712///////////////////////////////////////////////////////////////////////////////////////////////////
713
Hal Canary6f6961e2017-01-31 13:50:44 -0500714#if GR_TEST_UTILS
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500715
Herb Derbyc76d4092020-10-07 16:46:15 -0400716GrOp::Owner GrSmallPathRenderer::createOp_TestingOnly(
717 GrRecordingContext* context,
718 GrPaint&& paint,
719 const GrStyledShape& shape,
720 const SkMatrix& viewMatrix,
721 bool gammaCorrect,
722 const GrUserStencilSettings* stencil) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400723 return GrSmallPathRenderer::SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -0400724 gammaCorrect, stencil);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500725}
726
Brian Salomonfebbd232017-07-11 15:52:02 -0400727GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
joshualitt21279c72015-05-11 07:21:37 -0700728 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700729 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700730
bsalomonee432412016-06-27 07:18:18 -0700731 // This path renderer only allows fill styles.
Michael Ludwig2686d692020-04-17 20:21:37 +0000732 GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500733 return GrSmallPathRenderer::createOp_TestingOnly(
Robert Phillips7c525e62018-06-12 10:11:12 -0400734 context,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500735 std::move(paint), shape, viewMatrix,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500736 gammaCorrect,
737 GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700738}
739
740#endif