blob: b6663a0c9f012bb11f591acbd681a7839f3832e8 [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
Robert Phillips17dc6582021-08-17 11:57:31 -04009#include "src/gpu/ops/SmallPathRenderer.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"
24#include "src/gpu/GrVertexWriter.h"
25#include "src/gpu/effects/GrBitmapTextGeoProc.h"
26#include "src/gpu/effects/GrDistanceFieldGeoProc.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040027#include "src/gpu/geometry/GrQuad.h"
Robert Phillips5b68ed42020-08-10 14:46:42 -040028#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050030#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Robert Phillips46a324a2020-08-10 13:08:12 -040031#include "src/gpu/ops/GrSmallPathAtlasMgr.h"
Robert Phillips34949e32020-08-05 15:54:31 -040032#include "src/gpu/ops/GrSmallPathShapeData.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040033#include "src/gpu/v1/SurfaceDrawContext_v1.h"
jvanverthfa38a302014-10-06 05:59:05 -070034
Robert Phillips17dc6582021-08-17 11:57:31 -040035namespace {
36
jvanverthb61283f2014-10-30 05:57:21 -070037// mip levels
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080038static constexpr SkScalar kIdealMinMIP = 12;
39static constexpr SkScalar kMaxMIP = 162;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050040
Sergey Ulanovf1b2b422020-01-24 15:39:32 -080041static constexpr SkScalar kMaxDim = 73;
42static constexpr SkScalar kMinSize = SK_ScalarHalf;
43static constexpr SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070044
jvanverthfa38a302014-10-06 05:59:05 -070045////////////////////////////////////////////////////////////////////////////////
46
joshualitt5bf99f12015-03-13 11:47:42 -070047// padding around path bounds to allow for antialiased pixels
Robert Phillips6d3bc292020-04-06 10:29:28 -040048static const int kAntiAliasPad = 1;
joshualitt5bf99f12015-03-13 11:47:42 -070049
Robert Phillips17dc6582021-08-17 11:57:31 -040050class SmallPathOp final : public GrMeshDrawOp {
Brian Salomonfebbd232017-07-11 15:52:02 -040051private:
52 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
53
joshualitt5bf99f12015-03-13 11:47:42 -070054public:
Brian Salomon25a88092016-12-01 09:36:50 -050055 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070056
Herb Derbyc76d4092020-10-07 16:46:15 -040057 static GrOp::Owner Make(GrRecordingContext* context,
58 GrPaint&& paint,
59 const GrStyledShape& shape,
60 const SkMatrix& viewMatrix,
61 bool gammaCorrect,
62 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -040063 return Helper::FactoryHelper<SmallPathOp>(context, std::move(paint), shape, viewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -040064 gammaCorrect, stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -070065 }
Brian Salomond0a0a652016-12-15 15:25:22 -050066
Herb Derbyc76d4092020-10-07 16:46:15 -040067 SmallPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const GrStyledShape& shape,
Robert Phillips079455c2020-08-11 15:18:46 -040068 const SkMatrix& viewMatrix, bool gammaCorrect,
Brian Salomonfebbd232017-07-11 15:52:02 -040069 const GrUserStencilSettings* stencilSettings)
Robert Phillips4133dc42020-03-11 15:55:55 -040070 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -040071 , fHelper(processorSet, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -050072 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -050073 // Compute bounds
Greg Daniel5faf4742019-10-01 15:14:44 -040074 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsHairline::kNo);
Jim Van Verth33632d82017-02-28 10:24:39 -050075
Jim Van Verth83010462017-03-16 08:45:39 -040076#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -050077 fUsesDistanceField = true;
78#else
Jim Van Verth83010462017-03-16 08:45:39 -040079 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -050080 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
81#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -040082 // always use distance fields if in perspective
83 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -050084
Jim Van Verth5698c8a2017-10-12 10:18:44 -040085 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -050086
Brian Salomond0a0a652016-12-15 15:25:22 -050087 fGammaCorrect = gammaCorrect;
Brian Salomond0a0a652016-12-15 15:25:22 -050088 }
89
Brian Salomonfebbd232017-07-11 15:52:02 -040090 const char* name() const override { return "SmallPathOp"; }
91
Robert Phillips294723d2021-06-17 09:23:58 -040092 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040093 fHelper.visitProxies(func);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040094 }
95
Brian Salomonfebbd232017-07-11 15:52:02 -040096 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
97
Chris Dalton57ab06c2021-04-22 12:57:28 -060098 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
99 GrClampType clampType) override {
100 return fHelper.finalizeProcessors(caps, clip, clampType,
101 GrProcessorAnalysisCoverage::kSingleChannel,
102 &fShapes.front().fColor, &fWideColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700103 }
104
Brian Salomonfebbd232017-07-11 15:52:02 -0400105private:
bsalomonb5238a72015-05-05 07:49:49 -0700106 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500107 sk_sp<const GrBuffer> fVertexBuffer;
108 sk_sp<const GrBuffer> fIndexBuffer;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500109 GrGeometryProcessor* fGeometryProcessor;
Chris Dalton304e14d2020-03-17 14:29:06 -0600110 const GrSurfaceProxy** fPrimProcProxies;
bsalomonb5238a72015-05-05 07:49:49 -0700111 int fVertexOffset;
112 int fInstancesToFlush;
113 };
114
Robert Phillips2669a7b2020-03-12 12:07:19 -0400115 GrProgramInfo* programInfo() override {
116 // TODO [PI]: implement
117 return nullptr;
118 }
119
Robert Phillips4133dc42020-03-11 15:55:55 -0400120 void onCreateProgramInfo(const GrCaps*,
121 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500122 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600123 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400124 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400125 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500126 GrXferBarrierFlags renderPassXferBarriers,
127 GrLoadOp colorLoadOp) override {
Robert Phillips326f1d72020-10-01 09:43:29 -0400128 // We cannot surface the SmallPathOp's programInfo at record time. As currently
129 // implemented, the GP is modified at flush time based on the number of pages in the
130 // atlas.
Robert Phillips4133dc42020-03-11 15:55:55 -0400131 }
132
Robert Phillips2669a7b2020-03-12 12:07:19 -0400133 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500134 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400135 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400136 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500137 GrXferBarrierFlags renderPassXferBarriers,
138 GrLoadOp colorLoadOp) override {
Robert Phillips2669a7b2020-03-12 12:07:19 -0400139 // TODO [PI]: implement
140 }
141
Robert Phillips71143952021-06-17 14:55:07 -0400142 void onPrepareDraws(GrMeshDrawTarget* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500143 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700144
Robert Phillips079455c2020-08-11 15:18:46 -0400145 GrSmallPathAtlasMgr* atlasMgr = target->smallPathAtlasManager();
146 if (!atlasMgr) {
147 return;
148 }
149
Brian Salomon7eae3e02018-08-07 14:02:38 +0000150 static constexpr int kMaxTextures = GrDistanceFieldPathGeoProc::kMaxTextures;
Brian Salomon4dea72a2019-12-18 10:43:10 -0500151 static_assert(GrBitmapTextGeoProc::kMaxTextures == kMaxTextures);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000152
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700153 FlushInfo flushInfo;
Chris Dalton304e14d2020-03-17 14:29:06 -0600154 flushInfo.fPrimProcProxies = target->allocPrimProcProxyPtrs(kMaxTextures);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400155
156 int numActiveProxies;
Robert Phillips079455c2020-08-11 15:18:46 -0400157 const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000158 for (int i = 0; i < numActiveProxies; ++i) {
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400159 // This op does not know its atlas proxies when it is added to a OpsTasks, so the
Greg Danielb20d7e52019-09-03 13:54:39 -0400160 // proxies don't get added during the visitProxies call. Thus we add them here.
Chris Dalton304e14d2020-03-17 14:29:06 -0600161 flushInfo.fPrimProcProxies[i] = views[i].proxy();
Greg Daniel9715b6c2019-12-10 15:03:10 -0500162 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000163 }
Brian Salomon49348902018-06-26 09:12:38 -0400164
joshualitt5bf99f12015-03-13 11:47:42 -0700165 // Setup GrGeometryProcessor
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400166 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500167 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500168 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400169 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500170 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
171 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
172 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
173
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400174 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500175 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400176 if (ctm.hasPerspective()) {
177 matrix = &ctm;
178 } else if (fHelper.usesLocalCoords()) {
179 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500180 return;
181 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400182 matrix = &invert;
183 } else {
184 matrix = &SkMatrix::I();
185 }
Brian Salomonccb61422020-01-09 10:46:36 -0500186 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
187 target->allocator(), *target->caps().shaderCaps(), *matrix, fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400188 views, numActiveProxies, GrSamplerState::Filter::kLinear,
Brian Salomonccb61422020-01-09 10:46:36 -0500189 flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400190 } else {
191 SkMatrix invert;
192 if (fHelper.usesLocalCoords()) {
193 if (!ctm.invert(&invert)) {
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400194 return;
195 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500196 }
197
Brian Salomonccb61422020-01-09 10:46:36 -0500198 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
199 target->allocator(), *target->caps().shaderCaps(), this->color(), fWideColor,
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400200 views, numActiveProxies, GrSamplerState::Filter::kNearest,
Brian Salomonccb61422020-01-09 10:46:36 -0500201 kA8_GrMaskFormat, invert, false);
Jim Van Verth33632d82017-02-28 10:24:39 -0500202 }
joshualitt5bf99f12015-03-13 11:47:42 -0700203
joshualitt5bf99f12015-03-13 11:47:42 -0700204 // allocate vertices
Brian Osman0dd43022018-11-16 15:53:26 -0500205 const size_t kVertexStride = flushInfo.fGeometryProcessor->vertexStride();
Greg Danield5b45932018-06-07 13:15:10 -0400206
207 // We need to make sure we don't overflow a 32 bit int when we request space in the
208 // makeVertexSpace call below.
Robert Phillipsee08d522019-10-28 16:34:44 -0400209 if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
Greg Danield5b45932018-06-07 13:15:10 -0400210 return;
211 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400212 GrVertexWriter vertices{ target->makeVertexSpace(
213 kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
214 &flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
215
216 flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
Brian Osman0dd43022018-11-16 15:53:26 -0500217 if (!vertices.fPtr || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700218 SkDebugf("Could not allocate vertices\n");
219 return;
220 }
221
bsalomonb5238a72015-05-05 07:49:49 -0700222 flushInfo.fInstancesToFlush = 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700223 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500224 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700225
Robert Phillips34949e32020-08-05 15:54:31 -0400226 GrSmallPathShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500227 if (fUsesDistanceField) {
228 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400229 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500230 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400231 if (args.fViewMatrix.hasPerspective()) {
232 // approximate the scale since we can't get it from the matrix
233 SkRect xformedBounds;
234 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Brian Osman788b9162020-02-07 10:36:46 -0500235 maxScale = SkScalarAbs(std::max(xformedBounds.width() / bounds.width(),
Jim Van Verth51245932017-10-12 11:07:29 -0400236 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400237 } else {
238 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
239 }
Brian Osman116b33e2020-02-05 13:34:09 -0500240 SkScalar maxDim = std::max(bounds.width(), bounds.height());
Jim Van Verth33632d82017-02-28 10:24:39 -0500241 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
242 // In the majority of cases this will yield a crisper rendering.
243 SkScalar mipScale = 1.0f;
244 // Our mipscale is the maxScale clamped to the next highest power of 2
245 if (maxScale <= SK_ScalarHalf) {
246 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
247 mipScale = SkScalarPow(2, -log);
248 } else if (maxScale > SK_Scalar1) {
249 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
250 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700251 }
Jim Van Verthcbbd5652021-04-07 14:27:55 -0400252 // Log2 isn't very precise at values close to a power of 2,
253 // so add a little tolerance here. A little bit of scaling up is fine.
254 SkASSERT(maxScale <= mipScale + SK_ScalarNearlyZero);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500255
Jim Van Verth33632d82017-02-28 10:24:39 -0500256 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
257 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
258 // so we can preserve as much detail as possible. However, we can't scale down more
259 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
260 // just bigger than the ideal, and then scale down until we are no more than 4x the
261 // original mipsize.
262 if (mipSize < kIdealMinMIP) {
263 SkScalar newMipSize = mipSize;
264 do {
265 newMipSize *= 2;
266 } while (newMipSize < kIdealMinMIP);
267 while (newMipSize > 4 * mipSize) {
268 newMipSize *= 0.25f;
269 }
270 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700271 }
Robert Phillips109ff202020-08-10 16:39:31 -0400272
Brian Osman788b9162020-02-07 10:36:46 -0500273 SkScalar desiredDimension = std::min(mipSize, kMaxMIP);
Robert Phillips109ff202020-08-10 16:39:31 -0400274 int ceilDesiredDimension = SkScalarCeilToInt(desiredDimension);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500275
Jim Van Verth33632d82017-02-28 10:24:39 -0500276 // check to see if df path is cached
Robert Phillips079455c2020-08-11 15:18:46 -0400277 shapeData = atlasMgr->findOrCreate(args.fShape, ceilDesiredDimension);
Robert Phillips6507e632020-08-07 14:35:56 -0400278 if (!shapeData->fAtlasLocator.plotLocator().isValid()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500279 SkScalar scale = desiredDimension / maxDim;
280
Jim Van Verth33632d82017-02-28 10:24:39 -0500281 if (!this->addDFPathToAtlas(target,
282 &flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400283 atlasMgr,
Jim Van Verth33632d82017-02-28 10:24:39 -0500284 shapeData,
285 args.fShape,
Robert Phillips109ff202020-08-10 16:39:31 -0400286 ceilDesiredDimension,
Jim Van Verth33632d82017-02-28 10:24:39 -0500287 scale)) {
Robert Phillips079455c2020-08-11 15:18:46 -0400288 atlasMgr->deleteCacheEntry(shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500289 continue;
290 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500291 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500292 } else {
293 // check to see if bitmap path is cached
Robert Phillips079455c2020-08-11 15:18:46 -0400294 shapeData = atlasMgr->findOrCreate(args.fShape, args.fViewMatrix);
Robert Phillips6507e632020-08-07 14:35:56 -0400295 if (!shapeData->fAtlasLocator.plotLocator().isValid()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500296 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400297 &flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400298 atlasMgr,
Robert Phillips8296e752017-08-25 08:45:21 -0400299 shapeData,
300 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400301 args.fViewMatrix)) {
Robert Phillips079455c2020-08-11 15:18:46 -0400302 atlasMgr->deleteCacheEntry(shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500303 continue;
304 }
305 }
joshualitt5bf99f12015-03-13 11:47:42 -0700306 }
307
Robert Phillips40a29d72018-01-18 12:59:22 -0500308 auto uploadTarget = target->deferredUploadTarget();
Robert Phillips079455c2020-08-11 15:18:46 -0400309 atlasMgr->setUseToken(shapeData, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700310
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400311 this->writePathVertices(vertices, GrVertexColor(args.fColor, fWideColor),
Brian Osmanc906d252018-12-04 11:17:46 -0500312 args.fViewMatrix, shapeData);
bsalomonb5238a72015-05-05 07:49:49 -0700313 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700314 }
315
bsalomon75398562015-08-17 12:55:38 -0700316 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700317 }
318
Robert Phillips71143952021-06-17 14:55:07 -0400319 bool addToAtlasWithRetry(GrMeshDrawTarget* target,
Robert Phillips6704bc82020-08-14 12:45:07 -0400320 FlushInfo* flushInfo,
321 GrSmallPathAtlasMgr* atlasMgr,
322 int width, int height, const void* image,
323 const SkRect& bounds, int srcInset,
324 GrSmallPathShapeData* shapeData) const {
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500325 auto resourceProvider = target->resourceProvider();
326 auto uploadTarget = target->deferredUploadTarget();
327
Robert Phillips6704bc82020-08-14 12:45:07 -0400328 auto code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height,
329 image, &shapeData->fAtlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500330 if (GrDrawOpAtlas::ErrorCode::kError == code) {
331 return false;
332 }
333
334 if (GrDrawOpAtlas::ErrorCode::kTryAgain == code) {
335 this->flush(target, flushInfo);
336
Robert Phillips6704bc82020-08-14 12:45:07 -0400337 code = atlasMgr->addToAtlas(resourceProvider, uploadTarget, width, height,
338 image, &shapeData->fAtlasLocator);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500339 }
340
Robert Phillips6704bc82020-08-14 12:45:07 -0400341 shapeData->fAtlasLocator.insetSrc(srcInset);
342 shapeData->fBounds = bounds;
343
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500344 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
345 }
346
Robert Phillips71143952021-06-17 14:55:07 -0400347 bool addDFPathToAtlas(GrMeshDrawTarget* target, FlushInfo* flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400348 GrSmallPathAtlasMgr* atlasMgr, GrSmallPathShapeData* shapeData,
Robert Phillips34949e32020-08-05 15:54:31 -0400349 const GrStyledShape& shape, uint32_t dimension, SkScalar scale) const {
Robert Phillips4bc70112018-03-01 10:24:02 -0500350
bsalomonee432412016-06-27 07:18:18 -0700351 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700352
353 // generate bounding rect for bitmap draw
354 SkRect scaledBounds = bounds;
355 // scale to mip level size
356 scaledBounds.fLeft *= scale;
357 scaledBounds.fTop *= scale;
358 scaledBounds.fRight *= scale;
359 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500360 // subtract out integer portion of origin
361 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500362 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
363 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700364 scaledBounds.offset(-dx, -dy);
365 // get integer boundary
366 SkIRect devPathBounds;
367 scaledBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400368 // place devBounds at origin with padding to allow room for antialiasing
369 int width = devPathBounds.width() + 2 * kAntiAliasPad;
370 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500371 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400372 SkScalar translateX = kAntiAliasPad - dx;
373 SkScalar translateY = kAntiAliasPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700374
375 // draw path to bitmap
376 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500377 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400378 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700379
jvanverth512e4372015-11-23 11:50:02 -0800380 SkASSERT(devPathBounds.fLeft == 0);
381 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500382 SkASSERT(devPathBounds.width() > 0);
383 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700384
joel.liang8cbb4242017-01-09 18:39:43 -0800385 // setup signed distance field storage
386 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
387 width = dfBounds.width();
388 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800389 // TODO We should really generate this directly into the plot somehow
390 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800391
joel.liang8cbb4242017-01-09 18:39:43 -0800392 SkPath path;
393 shape.asPath(&path);
joel.liang8cbb4242017-01-09 18:39:43 -0800394 // Generate signed distance field directly from SkPath
395 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400396 path, drawMatrix, width, height,
397 width * sizeof(unsigned char));
joel.liang8cbb4242017-01-09 18:39:43 -0800398 if (!succeed) {
joel.liang8cbb4242017-01-09 18:39:43 -0800399 // setup bitmap backing
400 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400401 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
joel.liang8cbb4242017-01-09 18:39:43 -0800402 return false;
403 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400404 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800405
406 // rasterize path
407 SkPaint paint;
408 paint.setStyle(SkPaint::kFill_Style);
409 paint.setAntiAlias(true);
410
411 SkDraw draw;
joel.liang8cbb4242017-01-09 18:39:43 -0800412
413 SkRasterClip rasterClip;
414 rasterClip.setRect(devPathBounds);
415 draw.fRC = &rasterClip;
Brian Osman9aaec362020-05-08 14:54:37 -0400416 SkSimpleMatrixProvider matrixProvider(drawMatrix);
417 draw.fMatrixProvider = &matrixProvider;
joel.liang8cbb4242017-01-09 18:39:43 -0800418 draw.fDst = dst;
419
420 draw.drawPathCoverage(path, paint);
421
422 // Generate signed distance field
423 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
424 (const unsigned char*)dst.addr(),
425 dst.width(), dst.height(), dst.rowBytes());
joel.liang8cbb4242017-01-09 18:39:43 -0800426 }
joshualitt5bf99f12015-03-13 11:47:42 -0700427
Robert Phillips6704bc82020-08-14 12:45:07 -0400428 SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY);
429 drawBounds.fLeft /= scale;
430 drawBounds.fTop /= scale;
431 drawBounds.fRight /= scale;
432 drawBounds.fBottom /= scale;
joshualitt5bf99f12015-03-13 11:47:42 -0700433
Robert Phillips6704bc82020-08-14 12:45:07 -0400434 return this->addToAtlasWithRetry(target, flushInfo, atlasMgr,
435 width, height, dfStorage.get(),
436 drawBounds, SK_DistanceFieldPad, shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700437 }
438
Robert Phillips71143952021-06-17 14:55:07 -0400439 bool addBMPathToAtlas(GrMeshDrawTarget* target, FlushInfo* flushInfo,
Robert Phillips6704bc82020-08-14 12:45:07 -0400440 GrSmallPathAtlasMgr* atlasMgr, GrSmallPathShapeData* shapeData,
Robert Phillips34949e32020-08-05 15:54:31 -0400441 const GrStyledShape& shape, const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500442 const SkRect& bounds = shape.bounds();
443 if (bounds.isEmpty()) {
444 return false;
445 }
446 SkMatrix drawMatrix(ctm);
Jim Van Vertha64a5852018-03-09 14:16:31 -0500447 SkScalar tx = ctm.getTranslateX();
448 SkScalar ty = ctm.getTranslateY();
449 tx -= SkScalarFloorToScalar(tx);
450 ty -= SkScalarFloorToScalar(ty);
451 drawMatrix.set(SkMatrix::kMTransX, tx);
452 drawMatrix.set(SkMatrix::kMTransY, ty);
Jim Van Verth33632d82017-02-28 10:24:39 -0500453 SkRect shapeDevBounds;
454 drawMatrix.mapRect(&shapeDevBounds, bounds);
455 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
456 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
457
458 // get integer boundary
459 SkIRect devPathBounds;
460 shapeDevBounds.roundOut(&devPathBounds);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400461 // place devBounds at origin with padding to allow room for antialiasing
462 int width = devPathBounds.width() + 2 * kAntiAliasPad;
463 int height = devPathBounds.height() + 2 * kAntiAliasPad;
Jim Van Verth33632d82017-02-28 10:24:39 -0500464 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips6d3bc292020-04-06 10:29:28 -0400465 SkScalar translateX = kAntiAliasPad - dx;
466 SkScalar translateY = kAntiAliasPad - dy;
Jim Van Verth33632d82017-02-28 10:24:39 -0500467
468 SkASSERT(devPathBounds.fLeft == 0);
469 SkASSERT(devPathBounds.fTop == 0);
470 SkASSERT(devPathBounds.width() > 0);
471 SkASSERT(devPathBounds.height() > 0);
472
473 SkPath path;
474 shape.asPath(&path);
475 // setup bitmap backing
476 SkAutoPixmapStorage dst;
Robert Phillipsa4bb0642020-08-11 09:55:17 -0400477 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), devPathBounds.height()))) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500478 return false;
479 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400480 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500481
482 // rasterize path
483 SkPaint paint;
484 paint.setStyle(SkPaint::kFill_Style);
485 paint.setAntiAlias(true);
486
487 SkDraw draw;
Jim Van Verth33632d82017-02-28 10:24:39 -0500488
489 SkRasterClip rasterClip;
490 rasterClip.setRect(devPathBounds);
491 draw.fRC = &rasterClip;
492 drawMatrix.postTranslate(translateX, translateY);
Brian Osman9aaec362020-05-08 14:54:37 -0400493 SkSimpleMatrixProvider matrixProvider(drawMatrix);
494 draw.fMatrixProvider = &matrixProvider;
Jim Van Verth33632d82017-02-28 10:24:39 -0500495 draw.fDst = dst;
496
497 draw.drawPathCoverage(path, paint);
498
Robert Phillips6704bc82020-08-14 12:45:07 -0400499 SkRect drawBounds = SkRect::Make(devPathBounds).makeOffset(-translateX, -translateY);
Jim Van Verth33632d82017-02-28 10:24:39 -0500500
Robert Phillips6704bc82020-08-14 12:45:07 -0400501 return this->addToAtlasWithRetry(target, flushInfo, atlasMgr,
502 dst.width(), dst.height(), dst.addr(),
503 drawBounds, 0, shapeData);
Jim Van Verth33632d82017-02-28 10:24:39 -0500504 }
505
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400506 void writePathVertices(GrVertexWriter& vertices,
Brian Osmanc906d252018-12-04 11:17:46 -0500507 const GrVertexColor& color,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400508 const SkMatrix& ctm,
Robert Phillips34949e32020-08-05 15:54:31 -0400509 const GrSmallPathShapeData* shapeData) const {
Brian Osman0dd43022018-11-16 15:53:26 -0500510 SkRect translatedBounds(shapeData->fBounds);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400511 if (!fUsesDistanceField) {
Jim Van Vertha64a5852018-03-09 14:16:31 -0500512 translatedBounds.offset(SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransX)),
513 SkScalarFloorToScalar(ctm.get(SkMatrix::kMTransY)));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400514 }
Jim Van Verth77047542017-01-11 14:17:00 -0500515
Brian Osman0dd43022018-11-16 15:53:26 -0500516 // set up texture coordinates
Herb Derby1318e452020-08-03 14:38:10 -0400517 auto texCoords = GrVertexWriter::TriStripFromUVs(shapeData->fAtlasLocator.getUVs());
Brian Osman0dd43022018-11-16 15:53:26 -0500518
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400519 if (fUsesDistanceField && !ctm.hasPerspective()) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500520 vertices.writeQuad(GrQuad::MakeFromRect(translatedBounds, ctm),
Brian Osman0dd43022018-11-16 15:53:26 -0500521 color,
522 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400523 } else {
Brian Osman0dd43022018-11-16 15:53:26 -0500524 vertices.writeQuad(GrVertexWriter::TriStripFromRect(translatedBounds),
525 color,
526 texCoords);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400527 }
joshualitt5bf99f12015-03-13 11:47:42 -0700528 }
529
Robert Phillips71143952021-06-17 14:55:07 -0400530 void flush(GrMeshDrawTarget* target, FlushInfo* flushInfo) const {
Robert Phillips079455c2020-08-11 15:18:46 -0400531 GrSmallPathAtlasMgr* atlasMgr = target->smallPathAtlasManager();
532 if (!atlasMgr) {
533 return;
534 }
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400535
536 int numActiveProxies;
Robert Phillips079455c2020-08-11 15:18:46 -0400537 const GrSurfaceProxyView* views = atlasMgr->getViews(&numActiveProxies);
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400538
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500539 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor;
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400540 if (gp->numTextureSamplers() != numActiveProxies) {
541 for (int i = gp->numTextureSamplers(); i < numActiveProxies; ++i) {
Chris Dalton304e14d2020-03-17 14:29:06 -0600542 flushInfo->fPrimProcProxies[i] = views[i].proxy();
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400543 // This op does not know its atlas proxies when it is added to a OpsTasks, so the
Greg Danielb20d7e52019-09-03 13:54:39 -0400544 // proxies don't get added during the visitProxies call. Thus we add them here.
Greg Daniel9715b6c2019-12-10 15:03:10 -0500545 target->sampledProxyArray()->push_back(views[i].proxy());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000546 }
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400547 // During preparation the number of atlas pages has increased.
548 // Update the proxies used in the GP to match.
549 if (fUsesDistanceField) {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500550 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400551 views, numActiveProxies, GrSamplerState::Filter::kLinear);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400552 } else {
Greg Daniel9715b6c2019-12-10 15:03:10 -0500553 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewViews(
Robert Phillips76e7a7f2020-08-06 08:24:34 -0400554 views, numActiveProxies, GrSamplerState::Filter::kNearest);
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400555 }
556 }
557
bsalomon6d6b6ad2016-07-13 14:45:28 -0700558 if (flushInfo->fInstancesToFlush) {
Chris Daltoneb694b72020-03-16 09:25:50 -0600559 GrSimpleMesh* mesh = target->allocMesh();
Robert Phillipsee08d522019-10-28 16:34:44 -0400560 mesh->setIndexedPatterned(flushInfo->fIndexBuffer,
561 GrResourceProvider::NumIndicesPerNonAAQuad(),
Robert Phillipsee08d522019-10-28 16:34:44 -0400562 flushInfo->fInstancesToFlush,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600563 GrResourceProvider::MaxNumNonAAQuads(),
564 flushInfo->fVertexBuffer,
565 GrResourceProvider::NumVertsPerNonAAQuad(),
566 flushInfo->fVertexOffset);
Chris Dalton304e14d2020-03-17 14:29:06 -0600567 target->recordDraw(flushInfo->fGeometryProcessor, mesh, 1, flushInfo->fPrimProcProxies,
568 GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400569 flushInfo->fVertexOffset += GrResourceProvider::NumVertsPerNonAAQuad() *
570 flushInfo->fInstancesToFlush;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700571 flushInfo->fInstancesToFlush = 0;
572 }
joshualitt5bf99f12015-03-13 11:47:42 -0700573 }
574
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700575 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600576 auto pipeline = fHelper.createPipeline(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -0500577
Chris Dalton1b6a43c2020-09-25 12:21:18 -0600578 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline,
579 fHelper.stencilSettings());
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700580 }
581
Brian Osmancf860852018-10-31 14:04:39 -0400582 const SkPMColor4f& color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500583 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700584
Herb Derbye25c3002020-10-27 15:57:27 -0400585 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400586 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400587 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000588 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700589 }
590
Jim Van Verth33632d82017-02-28 10:24:39 -0500591 if (this->usesDistanceField() != that->usesDistanceField()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000592 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500593 }
594
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400595 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
596 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
597
598 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000599 return CombineResult::kCannotCombine;
joshualitt5bf99f12015-03-13 11:47:42 -0700600 }
601
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400602 // We can position on the cpu unless we're in perspective,
603 // but also need to make sure local matrices are identical
604 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
Mike Reed2c383152019-12-18 16:47:47 -0500605 !SkMatrixPriv::CheapEqual(thisCtm, thatCtm)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000606 return CombineResult::kCannotCombine;
Jim Van Verth33632d82017-02-28 10:24:39 -0500607 }
608
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400609 // Depending on the ctm we may have a different shader for SDF paths
610 if (this->usesDistanceField()) {
611 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
612 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000613 return CombineResult::kCannotCombine;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400614 }
615 }
616
Brian Salomond0a0a652016-12-15 15:25:22 -0500617 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
Brian Osmanc906d252018-12-04 11:17:46 -0500618 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000619 return CombineResult::kMerged;
joshualitt5bf99f12015-03-13 11:47:42 -0700620 }
621
John Stilesaf366522020-08-13 09:57:34 -0400622#if GR_TEST_UTILS
623 SkString onDumpInfo() const override {
624 SkString string;
625 for (const auto& geo : fShapes) {
626 string.appendf("Color: 0x%08x\n", geo.fColor.toBytes_RGBA());
627 }
628 string += fHelper.dumpInfo();
629 return string;
630 }
631#endif
632
Jim Van Verth33632d82017-02-28 10:24:39 -0500633 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700634
Brian Salomond0a0a652016-12-15 15:25:22 -0500635 struct Entry {
Michael Ludwig2686d692020-04-17 20:21:37 +0000636 SkPMColor4f fColor;
637 GrStyledShape fShape;
638 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700639 };
640
Brian Salomond0a0a652016-12-15 15:25:22 -0500641 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400642 Helper fHelper;
brianosman0e3c5542016-04-13 13:56:21 -0700643 bool fGammaCorrect;
Brian Osmanc906d252018-12-04 11:17:46 -0500644 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700645
John Stiles7571f9e2020-09-02 22:42:33 -0400646 using INHERITED = GrMeshDrawOp;
joshualitt5bf99f12015-03-13 11:47:42 -0700647};
648
Robert Phillips17dc6582021-08-17 11:57:31 -0400649} // anonymous namespace
650
651///////////////////////////////////////////////////////////////////////////////////////////////////
652
653#if GR_TEST_UTILS
654
655GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
656 SkMatrix viewMatrix = GrTest::TestMatrix(random);
657 bool gammaCorrect = random->nextBool();
658
659 // This path renderer only allows fill styles.
660 GrStyledShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
661 return SmallPathOp::Make(context, std::move(paint), shape, viewMatrix,
662 gammaCorrect, GrGetRandomStencil(random, context));
663}
664
665#endif // GR_TEST_UTILS
666
667///////////////////////////////////////////////////////////////////////////////////////////////////
668
669namespace skgpu::v1 {
670
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400671PathRenderer::CanDrawPath SmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips17dc6582021-08-17 11:57:31 -0400672 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
673 return CanDrawPath::kNo;
674 }
675 // If the shape has no key then we won't get any reuse.
676 if (!args.fShape->hasUnstyledKey()) {
677 return CanDrawPath::kNo;
678 }
679 // This only supports filled paths, however, the caller may apply the style to make a filled
680 // path and try again.
681 if (!args.fShape->style().isSimpleFill()) {
682 return CanDrawPath::kNo;
683 }
684 // This does non-inverse coverage-based antialiased fills.
685 if (GrAAType::kCoverage != args.fAAType) {
686 return CanDrawPath::kNo;
687 }
688 // TODO: Support inverse fill
689 if (args.fShape->inverseFilled()) {
690 return CanDrawPath::kNo;
691 }
692
693 SkScalar scaleFactors[2] = { 1, 1 };
694 // TODO: handle perspective distortion
695 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
696 return CanDrawPath::kNo;
697 }
698 // For affine transformations, too much shear can produce artifacts.
699 if (!scaleFactors[0] || scaleFactors[1]/scaleFactors[0] > 4) {
700 return CanDrawPath::kNo;
701 }
702 // Only support paths with bounds within kMaxDim by kMaxDim,
703 // scaled to have bounds within kMaxSize by kMaxSize.
704 // The goal is to accelerate rendering of lots of small paths that may be scaling.
705 SkRect bounds = args.fShape->styledBounds();
706 SkScalar minDim = std::min(bounds.width(), bounds.height());
707 SkScalar maxDim = std::max(bounds.width(), bounds.height());
708 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
709 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
710 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
711 return CanDrawPath::kNo;
712 }
713
714 return CanDrawPath::kYes;
715}
716
717bool SmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400718 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
Robert Phillips17dc6582021-08-17 11:57:31 -0400719 "SmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700720
jvanverthfa38a302014-10-06 05:59:05 -0700721 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700722 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700723 SkASSERT(args.fShape->hasUnstyledKey());
jvanverthfa38a302014-10-06 05:59:05 -0700724
Herb Derbyc76d4092020-10-07 16:46:15 -0400725 GrOp::Owner op = SmallPathOp::Make(
Robert Phillipscac17642020-08-07 16:17:10 -0400726 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
Robert Phillips079455c2020-08-11 15:18:46 -0400727 args.fGammaCorrect, args.fUserStencilSettings);
John Stiles0fbc6a32021-06-04 14:40:57 -0400728 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800729
jvanverthfa38a302014-10-06 05:59:05 -0700730 return true;
731}
732
Robert Phillips17dc6582021-08-17 11:57:31 -0400733} // namespace skgpu::v1