blob: 3e7da5223081dad1c8f87ae6ac585b5c2a971338 [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
Jim Van Verth83010462017-03-16 08:45:39 -04009#include "GrSmallPathRenderer.h"
cdalton397536c2016-03-25 12:15:03 -070010#include "GrBuffer.h"
jvanverthfa38a302014-10-06 05:59:05 -070011#include "GrContext.h"
Jim Van Verthf9e678d2017-02-15 15:46:52 -050012#include "GrDistanceFieldGenFromVector.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050013#include "GrDrawOpTest.h"
Brian Salomon57caa662017-10-18 12:21:05 +000014#include "GrQuad.h"
bsalomonb5238a72015-05-05 07:49:49 -070015#include "GrResourceProvider.h"
Brian Salomonfebbd232017-07-11 15:52:02 -040016#include "GrSimpleMeshDrawOpHelper.h"
Hal Canary95e3c052017-01-11 12:44:43 -050017#include "SkAutoMalloc.h"
Brian Osmanc7da1462017-08-17 16:14:25 -040018#include "SkAutoPixmapStorage.h"
jvanverthfa38a302014-10-06 05:59:05 -070019#include "SkDistanceFieldGen.h"
Brian Osmanc7da1462017-08-17 16:14:25 -040020#include "SkRasterClip.h"
Brian Salomonfebbd232017-07-11 15:52:02 -040021#include "effects/GrBitmapTextGeoProc.h"
22#include "effects/GrDistanceFieldGeoProc.h"
23#include "ops/GrMeshDrawOp.h"
jvanverthfa38a302014-10-06 05:59:05 -070024
jvanverthfb1e2fc2015-09-15 13:11:11 -070025#define ATLAS_TEXTURE_WIDTH 2048
jvanverthb61283f2014-10-30 05:57:21 -070026#define ATLAS_TEXTURE_HEIGHT 2048
jvanverthfb1e2fc2015-09-15 13:11:11 -070027#define PLOT_WIDTH 512
reede4ef1ca2015-02-17 18:38:38 -080028#define PLOT_HEIGHT 256
jvanverthfa38a302014-10-06 05:59:05 -070029
30#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
31#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
32
jvanverthb3eb6872014-10-24 07:12:51 -070033#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070034static int g_NumCachedShapes = 0;
35static int g_NumFreedShapes = 0;
jvanverthb3eb6872014-10-24 07:12:51 -070036#endif
37
jvanverthb61283f2014-10-30 05:57:21 -070038// mip levels
Jim Van Verth25b8ca12017-02-17 14:02:13 -050039static const SkScalar kIdealMinMIP = 12;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050040static const SkScalar kMaxMIP = 162;
41
42static const SkScalar kMaxDim = 73;
Jim Van Verth25b8ca12017-02-17 14:02:13 -050043static const SkScalar kMinSize = SK_ScalarHalf;
Jim Van Verthf9e678d2017-02-15 15:46:52 -050044static const SkScalar kMaxSize = 2*kMaxMIP;
jvanverthb61283f2014-10-30 05:57:21 -070045
joshualitt5bf99f12015-03-13 11:47:42 -070046// Callback to clear out internal path cache when eviction occurs
Jim Van Verth83010462017-03-16 08:45:39 -040047void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
48 GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
joshualitt5bf99f12015-03-13 11:47:42 -070049 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -070050 ShapeDataList::Iter iter;
51 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
52 ShapeData* shapeData;
53 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -070054 iter.next();
bsalomonee432412016-06-27 07:18:18 -070055 if (id == shapeData->fID) {
56 dfpr->fShapeCache.remove(shapeData->fKey);
57 dfpr->fShapeList.remove(shapeData);
58 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -070059#ifdef DF_PATH_TRACKING
60 ++g_NumFreedPaths;
61#endif
62 }
63 }
64}
65
jvanverthfa38a302014-10-06 05:59:05 -070066////////////////////////////////////////////////////////////////////////////////
Jim Van Verth83010462017-03-16 08:45:39 -040067GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -070068
Jim Van Verth83010462017-03-16 08:45:39 -040069GrSmallPathRenderer::~GrSmallPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -070070 ShapeDataList::Iter iter;
71 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
72 ShapeData* shapeData;
73 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -070074 iter.next();
bsalomonee432412016-06-27 07:18:18 -070075 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -070076 }
jvanverthb3eb6872014-10-24 07:12:51 -070077
78#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070079 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -070080#endif
jvanverthfa38a302014-10-06 05:59:05 -070081}
82
83////////////////////////////////////////////////////////////////////////////////
Chris Dalton5ed44232017-09-07 13:22:46 -060084GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -070085 if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060086 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070087 }
88 // If the shape has no key then we won't get any reuse.
89 if (!args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060090 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070091 }
92 // This only supports filled paths, however, the caller may apply the style to make a filled
93 // path and try again.
94 if (!args.fShape->style().isSimpleFill()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060095 return CanDrawPath::kNo;
bsalomonee432412016-06-27 07:18:18 -070096 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050097 // This does non-inverse coverage-based antialiased fills.
98 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060099 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700100 }
jvanverthfa38a302014-10-06 05:59:05 -0700101 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700102 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600103 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700104 }
halcanary9d524f22016-03-29 09:03:52 -0700105
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500106 // Only support paths with bounds within kMaxDim by kMaxDim,
107 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500108 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400109 SkScalar scaleFactors[2] = { 1, 1 };
110 if (!args.fViewMatrix->hasPerspective() && !args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600111 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500112 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700113 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500114 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700115 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500116 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
117 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600118 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
119 return CanDrawPath::kNo;
120 }
bsalomon6266dca2016-03-11 06:22:00 -0800121
Chris Dalton5ed44232017-09-07 13:22:46 -0600122 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700123}
124
jvanverthfa38a302014-10-06 05:59:05 -0700125////////////////////////////////////////////////////////////////////////////////
126
joshualitt5bf99f12015-03-13 11:47:42 -0700127// padding around path bounds to allow for antialiased pixels
128static const SkScalar kAntiAliasPad = 1.0f;
129
Brian Salomonfebbd232017-07-11 15:52:02 -0400130class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
131private:
132 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
133
joshualitt5bf99f12015-03-13 11:47:42 -0700134public:
Brian Salomon25a88092016-12-01 09:36:50 -0500135 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700136
Jim Van Verth83010462017-03-16 08:45:39 -0400137 using ShapeData = GrSmallPathRenderer::ShapeData;
Brian Salomond0a0a652016-12-15 15:25:22 -0500138 using ShapeCache = SkTDynamicHash<ShapeData, ShapeData::Key>;
Jim Van Verth83010462017-03-16 08:45:39 -0400139 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700140
Brian Salomonfebbd232017-07-11 15:52:02 -0400141 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const GrShape& shape,
142 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas,
143 ShapeCache* shapeCache, ShapeDataList* shapeList,
144 bool gammaCorrect,
145 const GrUserStencilSettings* stencilSettings) {
146 return Helper::FactoryHelper<SmallPathOp>(std::move(paint), shape, viewMatrix, atlas,
147 shapeCache, shapeList, gammaCorrect,
148 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700149 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500150
Brian Salomonfebbd232017-07-11 15:52:02 -0400151 SmallPathOp(Helper::MakeArgs helperArgs, GrColor color, const GrShape& shape,
152 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
153 ShapeDataList* shapeList, bool gammaCorrect,
154 const GrUserStencilSettings* stencilSettings)
155 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500156 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500157 // Compute bounds
158 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
159
Jim Van Verth83010462017-03-16 08:45:39 -0400160#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500161 fUsesDistanceField = true;
162#else
Jim Van Verth83010462017-03-16 08:45:39 -0400163 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500164 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
165#endif
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400166 // always use distance fields if in perspective
167 fUsesDistanceField = fUsesDistanceField || viewMatrix.hasPerspective();
Jim Van Verth33632d82017-02-28 10:24:39 -0500168
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400169 fShapes.emplace_back(Entry{color, shape, viewMatrix});
Brian Salomond0a0a652016-12-15 15:25:22 -0500170
171 fAtlas = atlas;
172 fShapeCache = shapeCache;
173 fShapeList = shapeList;
174 fGammaCorrect = gammaCorrect;
175
Brian Salomond0a0a652016-12-15 15:25:22 -0500176 }
177
Brian Salomonfebbd232017-07-11 15:52:02 -0400178 const char* name() const override { return "SmallPathOp"; }
179
Robert Phillipsf1748f52017-09-14 14:11:24 -0400180 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400181 fHelper.visitProxies(func);
182
183 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
Brian Salomon9f545bc2017-11-06 10:36:57 -0500184 for (uint32_t i = 0; i < fAtlas->pageCount(); ++i) {
185 SkASSERT(proxies[i]);
186 func(proxies[i].get());
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400187 }
188 }
189
Brian Salomonfebbd232017-07-11 15:52:02 -0400190 SkString dumpInfo() const override {
191 SkString string;
192 for (const auto& geo : fShapes) {
193 string.appendf("Color: 0x%08x\n", geo.fColor);
194 }
195 string += fHelper.dumpInfo();
196 string += INHERITED::dumpInfo();
197 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500198 }
199
Brian Salomonfebbd232017-07-11 15:52:02 -0400200 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
201
Brian Osman9a725dd2017-09-20 09:53:22 -0400202 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
203 GrPixelConfigIsClamped dstIsClamped) override {
204 return fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
205 GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonfebbd232017-07-11 15:52:02 -0400206 &fShapes.front().fColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700207 }
208
Brian Salomonfebbd232017-07-11 15:52:02 -0400209private:
bsalomonb5238a72015-05-05 07:49:49 -0700210 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500211 sk_sp<const GrBuffer> fVertexBuffer;
212 sk_sp<const GrBuffer> fIndexBuffer;
bungeman06ca8ec2016-06-09 08:01:03 -0700213 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomonfebbd232017-07-11 15:52:02 -0400214 const GrPipeline* fPipeline;
bsalomonb5238a72015-05-05 07:49:49 -0700215 int fVertexOffset;
216 int fInstancesToFlush;
217 };
218
Brian Salomon91326c32017-08-09 16:02:19 -0400219 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500220 int instanceCount = fShapes.count();
joshualitt5bf99f12015-03-13 11:47:42 -0700221
bsalomon342bfc22016-04-01 06:06:20 -0700222 FlushInfo flushInfo;
Brian Salomonfebbd232017-07-11 15:52:02 -0400223 flushInfo.fPipeline = fHelper.makePipeline(target);
joshualitt5bf99f12015-03-13 11:47:42 -0700224 // Setup GrGeometryProcessor
Brian Salomon2ee084e2016-12-16 18:59:19 -0500225 GrDrawOpAtlas* atlas = fAtlas;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400226 uint32_t atlasPageCount = atlas->pageCount();
227 if (!atlasPageCount) {
228 return;
229 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400230 const SkMatrix& ctm = fShapes[0].fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500231 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500232 uint32_t flags = 0;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400233 // Still need to key off of ctm to pick the right shader for the transformed quad
Jim Van Verth33632d82017-02-28 10:24:39 -0500234 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
235 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
236 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
237
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400238 const SkMatrix* matrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500239 SkMatrix invert;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400240 if (ctm.hasPerspective()) {
241 matrix = &ctm;
242 } else if (fHelper.usesLocalCoords()) {
243 if (!ctm.invert(&invert)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500244 SkDebugf("Could not invert viewmatrix\n");
245 return;
246 }
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400247 matrix = &invert;
248 } else {
249 matrix = &SkMatrix::I();
250 }
251 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
Brian Osman09068252018-01-03 09:57:29 -0500252 *matrix, atlas->getProxies(), GrSamplerState::ClampBilerp(), flags);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400253 } else {
254 SkMatrix invert;
255 if (fHelper.usesLocalCoords()) {
256 if (!ctm.invert(&invert)) {
257 SkDebugf("Could not invert viewmatrix\n");
258 return;
259 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500260 }
261
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400262 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Jim Van Vertha950b632017-09-12 11:54:11 -0400263 this->color(), atlas->getProxies(), GrSamplerState::ClampNearest(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400264 kA8_GrMaskFormat, invert, fHelper.usesLocalCoords());
Jim Van Verth33632d82017-02-28 10:24:39 -0500265 }
joshualitt5bf99f12015-03-13 11:47:42 -0700266
joshualitt5bf99f12015-03-13 11:47:42 -0700267 // allocate vertices
bsalomon342bfc22016-04-01 06:06:20 -0700268 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Jim Van Verth33632d82017-02-28 10:24:39 -0500269 SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(GrColor) + 2*sizeof(uint16_t));
bsalomonb5238a72015-05-05 07:49:49 -0700270
cdalton397536c2016-03-25 12:15:03 -0700271 const GrBuffer* vertexBuffer;
bsalomon75398562015-08-17 12:55:38 -0700272 void* vertices = target->makeVertexSpace(vertexStride,
273 kVerticesPerQuad * instanceCount,
274 &vertexBuffer,
275 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -0700276 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
Brian Salomond28a79d2017-10-16 13:01:07 -0400277 flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
bsalomonb5238a72015-05-05 07:49:49 -0700278 if (!vertices || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700279 SkDebugf("Could not allocate vertices\n");
280 return;
281 }
282
bsalomonb5238a72015-05-05 07:49:49 -0700283 flushInfo.fInstancesToFlush = 0;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700284 // Pointer to the next set of vertices to write.
285 intptr_t offset = reinterpret_cast<intptr_t>(vertices);
joshualitt5bf99f12015-03-13 11:47:42 -0700286 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500287 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700288
Jim Van Verth33632d82017-02-28 10:24:39 -0500289 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500290 if (fUsesDistanceField) {
291 // get mip level
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400292 SkScalar maxScale;
Jim Van Verth33632d82017-02-28 10:24:39 -0500293 const SkRect& bounds = args.fShape.bounds();
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400294 if (args.fViewMatrix.hasPerspective()) {
295 // approximate the scale since we can't get it from the matrix
296 SkRect xformedBounds;
297 args.fViewMatrix.mapRect(&xformedBounds, bounds);
Jim Van Verth51245932017-10-12 11:07:29 -0400298 maxScale = SkScalarAbs(SkTMax(xformedBounds.width() / bounds.width(),
299 xformedBounds.height() / bounds.height()));
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400300 } else {
301 maxScale = SkScalarAbs(args.fViewMatrix.getMaxScale());
302 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500303 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
304 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
305 // In the majority of cases this will yield a crisper rendering.
306 SkScalar mipScale = 1.0f;
307 // Our mipscale is the maxScale clamped to the next highest power of 2
308 if (maxScale <= SK_ScalarHalf) {
309 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
310 mipScale = SkScalarPow(2, -log);
311 } else if (maxScale > SK_Scalar1) {
312 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
313 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700314 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500315 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500316
Jim Van Verth33632d82017-02-28 10:24:39 -0500317 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
318 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
319 // so we can preserve as much detail as possible. However, we can't scale down more
320 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
321 // just bigger than the ideal, and then scale down until we are no more than 4x the
322 // original mipsize.
323 if (mipSize < kIdealMinMIP) {
324 SkScalar newMipSize = mipSize;
325 do {
326 newMipSize *= 2;
327 } while (newMipSize < kIdealMinMIP);
328 while (newMipSize > 4 * mipSize) {
329 newMipSize *= 0.25f;
330 }
331 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700332 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500333 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500334
Jim Van Verth33632d82017-02-28 10:24:39 -0500335 // check to see if df path is cached
336 ShapeData::Key key(args.fShape, SkScalarCeilToInt(desiredDimension));
337 shapeData = fShapeCache->find(key);
338 if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
339 // Remove the stale cache entry
340 if (shapeData) {
341 fShapeCache->remove(shapeData->fKey);
342 fShapeList->remove(shapeData);
343 delete shapeData;
344 }
345 SkScalar scale = desiredDimension / maxDim;
346
347 shapeData = new ShapeData;
348 if (!this->addDFPathToAtlas(target,
349 &flushInfo,
350 atlas,
351 shapeData,
352 args.fShape,
353 SkScalarCeilToInt(desiredDimension),
354 scale)) {
355 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500356 continue;
357 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500358 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500359 } else {
360 // check to see if bitmap path is cached
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400361 ShapeData::Key key(args.fShape, args.fViewMatrix);
Jim Van Verth33632d82017-02-28 10:24:39 -0500362 shapeData = fShapeCache->find(key);
363 if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
364 // Remove the stale cache entry
365 if (shapeData) {
366 fShapeCache->remove(shapeData->fKey);
367 fShapeList->remove(shapeData);
368 delete shapeData;
369 }
370
371 shapeData = new ShapeData;
372 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400373 &flushInfo,
374 atlas,
375 shapeData,
376 args.fShape,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400377 args.fViewMatrix)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500378 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500379 continue;
380 }
381 }
joshualitt5bf99f12015-03-13 11:47:42 -0700382 }
383
Robert Phillips40a29d72018-01-18 12:59:22 -0500384 auto uploadTarget = target->deferredUploadTarget();
385 atlas->setLastUseToken(shapeData->fID, uploadTarget->tokenTracker()->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700386
Brian Salomon29b60c92017-10-31 14:42:10 -0400387 this->writePathVertices(atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800388 offset,
389 args.fColor,
bsalomonb5238a72015-05-05 07:49:49 -0700390 vertexStride,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400391 args.fViewMatrix,
bsalomonee432412016-06-27 07:18:18 -0700392 shapeData);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700393 offset += kVerticesPerQuad * vertexStride;
bsalomonb5238a72015-05-05 07:49:49 -0700394 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700395 }
396
bsalomon75398562015-08-17 12:55:38 -0700397 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700398 }
399
Brian Salomone5b399e2017-07-19 13:50:54 -0400400 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400401 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
402 uint32_t dimension, SkScalar scale) const {
bsalomonee432412016-06-27 07:18:18 -0700403 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700404
405 // generate bounding rect for bitmap draw
406 SkRect scaledBounds = bounds;
407 // scale to mip level size
408 scaledBounds.fLeft *= scale;
409 scaledBounds.fTop *= scale;
410 scaledBounds.fRight *= scale;
411 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500412 // subtract out integer portion of origin
413 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500414 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
415 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700416 scaledBounds.offset(-dx, -dy);
417 // get integer boundary
418 SkIRect devPathBounds;
419 scaledBounds.roundOut(&devPathBounds);
420 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800421 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500422 // place devBounds at origin
423 int width = devPathBounds.width() + 2*intPad;
424 int height = devPathBounds.height() + 2*intPad;
425 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400426 SkScalar translateX = intPad - dx;
427 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700428
429 // draw path to bitmap
430 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500431 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400432 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700433
jvanverth512e4372015-11-23 11:50:02 -0800434 SkASSERT(devPathBounds.fLeft == 0);
435 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500436 SkASSERT(devPathBounds.width() > 0);
437 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700438
joel.liang8cbb4242017-01-09 18:39:43 -0800439 // setup signed distance field storage
440 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
441 width = dfBounds.width();
442 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800443 // TODO We should really generate this directly into the plot somehow
444 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800445
joel.liang8cbb4242017-01-09 18:39:43 -0800446 SkPath path;
447 shape.asPath(&path);
448#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
449 // Generate signed distance field directly from SkPath
450 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
451 path, drawMatrix,
452 width, height, width * sizeof(unsigned char));
453 if (!succeed) {
454#endif
455 // setup bitmap backing
456 SkAutoPixmapStorage dst;
457 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
458 devPathBounds.height()))) {
459 return false;
460 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400461 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800462
463 // rasterize path
464 SkPaint paint;
465 paint.setStyle(SkPaint::kFill_Style);
466 paint.setAntiAlias(true);
467
468 SkDraw draw;
469 sk_bzero(&draw, sizeof(draw));
470
471 SkRasterClip rasterClip;
472 rasterClip.setRect(devPathBounds);
473 draw.fRC = &rasterClip;
474 draw.fMatrix = &drawMatrix;
475 draw.fDst = dst;
476
477 draw.drawPathCoverage(path, paint);
478
479 // Generate signed distance field
480 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
481 (const unsigned char*)dst.addr(),
482 dst.width(), dst.height(), dst.rowBytes());
483#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
484 }
485#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700486
487 // add to atlas
488 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500489 GrDrawOpAtlas::AtlasID id;
Brian Salomon29b60c92017-10-31 14:42:10 -0400490 auto uploadTarget = target->deferredUploadTarget();
491 if (!atlas->addToAtlas(&id, uploadTarget, width, height, dfStorage.get(), &atlasLocation)) {
bsalomon75398562015-08-17 12:55:38 -0700492 this->flush(target, flushInfo);
Brian Salomon29b60c92017-10-31 14:42:10 -0400493 if (!atlas->addToAtlas(&id, uploadTarget, width, height, dfStorage.get(),
494 &atlasLocation)) {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700495 return false;
496 }
joshualitt5bf99f12015-03-13 11:47:42 -0700497 }
498
499 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700500 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700501 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500502
Robert Phillips3cf781d2017-08-22 18:25:11 -0400503 shapeData->fBounds = SkRect::Make(devPathBounds);
504 shapeData->fBounds.offset(-translateX, -translateY);
505 shapeData->fBounds.fLeft /= scale;
506 shapeData->fBounds.fTop /= scale;
507 shapeData->fBounds.fRight /= scale;
508 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500509
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400510 // We pack the 2bit page index in the low bit of the u and v texture coords
511 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
512 SkASSERT(pageIndex < 4);
513 uint16_t uBit = (pageIndex >> 1) & 0x1;
514 uint16_t vBit = pageIndex & 0x1;
515 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
516 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400517 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400518 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400519 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400520 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700521
bsalomonee432412016-06-27 07:18:18 -0700522 fShapeCache->add(shapeData);
523 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700524#ifdef DF_PATH_TRACKING
525 ++g_NumCachedPaths;
526#endif
527 return true;
528 }
529
Brian Salomone5b399e2017-07-19 13:50:54 -0400530 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400531 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
532 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500533 const SkRect& bounds = shape.bounds();
534 if (bounds.isEmpty()) {
535 return false;
536 }
537 SkMatrix drawMatrix(ctm);
538 drawMatrix.set(SkMatrix::kMTransX, SkScalarFraction(ctm.get(SkMatrix::kMTransX)));
539 drawMatrix.set(SkMatrix::kMTransY, SkScalarFraction(ctm.get(SkMatrix::kMTransY)));
540 SkRect shapeDevBounds;
541 drawMatrix.mapRect(&shapeDevBounds, bounds);
542 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
543 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
544
545 // get integer boundary
546 SkIRect devPathBounds;
547 shapeDevBounds.roundOut(&devPathBounds);
548 // pad to allow room for antialiasing
549 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
550 // place devBounds at origin
551 int width = devPathBounds.width() + 2 * intPad;
552 int height = devPathBounds.height() + 2 * intPad;
553 devPathBounds = SkIRect::MakeWH(width, height);
554 SkScalar translateX = intPad - dx;
555 SkScalar translateY = intPad - dy;
556
557 SkASSERT(devPathBounds.fLeft == 0);
558 SkASSERT(devPathBounds.fTop == 0);
559 SkASSERT(devPathBounds.width() > 0);
560 SkASSERT(devPathBounds.height() > 0);
561
562 SkPath path;
563 shape.asPath(&path);
564 // setup bitmap backing
565 SkAutoPixmapStorage dst;
566 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
567 devPathBounds.height()))) {
568 return false;
569 }
Mike Reedf0ffb892017-10-03 14:47:21 -0400570 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500571
572 // rasterize path
573 SkPaint paint;
574 paint.setStyle(SkPaint::kFill_Style);
575 paint.setAntiAlias(true);
576
577 SkDraw draw;
578 sk_bzero(&draw, sizeof(draw));
579
580 SkRasterClip rasterClip;
581 rasterClip.setRect(devPathBounds);
582 draw.fRC = &rasterClip;
583 drawMatrix.postTranslate(translateX, translateY);
584 draw.fMatrix = &drawMatrix;
585 draw.fDst = dst;
586
587 draw.drawPathCoverage(path, paint);
588
589 // add to atlas
590 SkIPoint16 atlasLocation;
591 GrDrawOpAtlas::AtlasID id;
Brian Salomon29b60c92017-10-31 14:42:10 -0400592 auto uploadTarget = target->deferredUploadTarget();
593 if (!atlas->addToAtlas(&id, uploadTarget, dst.width(), dst.height(), dst.addr(),
Jim Van Verth33632d82017-02-28 10:24:39 -0500594 &atlasLocation)) {
595 this->flush(target, flushInfo);
Brian Salomon29b60c92017-10-31 14:42:10 -0400596 if (!atlas->addToAtlas(&id, uploadTarget, dst.width(), dst.height(), dst.addr(),
Jim Van Verth33632d82017-02-28 10:24:39 -0500597 &atlasLocation)) {
598 return false;
599 }
600 }
601
602 // add to cache
603 shapeData->fKey.set(shape, ctm);
604 shapeData->fID = id;
605
Jim Van Verth33632d82017-02-28 10:24:39 -0500606 shapeData->fBounds = SkRect::Make(devPathBounds);
607 shapeData->fBounds.offset(-translateX, -translateY);
608
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400609 // We pack the 2bit page index in the low bit of the u and v texture coords
610 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
611 SkASSERT(pageIndex < 4);
612 uint16_t uBit = (pageIndex >> 1) & 0x1;
613 uint16_t vBit = pageIndex & 0x1;
614 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
615 (atlasLocation.fX+width) << 1 | uBit,
616 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500617
618 fShapeCache->add(shapeData);
619 fShapeList->addToTail(shapeData);
620#ifdef DF_PATH_TRACKING
621 ++g_NumCachedPaths;
622#endif
623 return true;
624 }
625
Brian Salomon29b60c92017-10-31 14:42:10 -0400626 void writePathVertices(GrDrawOpAtlas* atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800627 intptr_t offset,
628 GrColor color,
bsalomonb5238a72015-05-05 07:49:49 -0700629 size_t vertexStride,
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400630 const SkMatrix& ctm,
bsalomonee432412016-06-27 07:18:18 -0700631 const ShapeData* shapeData) const {
joshualitt53f26aa2015-12-10 07:29:54 -0800632 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
633
Jim Van Verth77047542017-01-11 14:17:00 -0500634 SkRect bounds = shapeData->fBounds;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400635 SkRect translatedBounds(bounds);
636 if (!fUsesDistanceField) {
637 translatedBounds.offset(SkScalarTruncToScalar(ctm.get(SkMatrix::kMTransX)),
638 SkScalarTruncToScalar(ctm.get(SkMatrix::kMTransY)));
639 }
Jim Van Verth77047542017-01-11 14:17:00 -0500640
joshualitt5bf99f12015-03-13 11:47:42 -0700641 // vertex positions
642 // TODO make the vertex attributes a struct
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400643 if (fUsesDistanceField && !ctm.hasPerspective()) {
Brian Salomon57caa662017-10-18 12:21:05 +0000644 GrQuad quad;
645 quad.setFromMappedRect(translatedBounds, ctm);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400646 intptr_t positionOffset = offset;
647 SkPoint* position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000648 *position = quad.point(0);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400649 positionOffset += vertexStride;
650 position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000651 *position = quad.point(1);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400652 positionOffset += vertexStride;
653 position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000654 *position = quad.point(2);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400655 positionOffset += vertexStride;
656 position = (SkPoint*)positionOffset;
Brian Salomon57caa662017-10-18 12:21:05 +0000657 *position = quad.point(3);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400658 } else {
Cary Clark74f623d2017-11-06 20:02:02 -0500659 SkPointPriv::SetRectTriStrip(positions, translatedBounds.left(),
Brian Salomon57caa662017-10-18 12:21:05 +0000660 translatedBounds.top(),
661 translatedBounds.right(),
662 translatedBounds.bottom(),
663 vertexStride);
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400664 }
joshualitt5bf99f12015-03-13 11:47:42 -0700665
joshualitt53f26aa2015-12-10 07:29:54 -0800666 // colors
667 for (int i = 0; i < kVerticesPerQuad; i++) {
668 GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride);
669 *colorPtr = color;
670 }
671
Jim Van Verth77047542017-01-11 14:17:00 -0500672 // set up texture coordinates
Robert Phillips8296e752017-08-25 08:45:21 -0400673 uint16_t l = shapeData->fTextureCoords.fLeft;
674 uint16_t t = shapeData->fTextureCoords.fTop;
675 uint16_t r = shapeData->fTextureCoords.fRight;
676 uint16_t b = shapeData->fTextureCoords.fBottom;
Jim Van Verth33632d82017-02-28 10:24:39 -0500677
678 // set vertex texture coords
679 intptr_t textureCoordOffset = offset + sizeof(SkPoint) + sizeof(GrColor);
680 uint16_t* textureCoords = (uint16_t*) textureCoordOffset;
681 textureCoords[0] = l;
682 textureCoords[1] = t;
683 textureCoordOffset += vertexStride;
684 textureCoords = (uint16_t*)textureCoordOffset;
685 textureCoords[0] = l;
686 textureCoords[1] = b;
687 textureCoordOffset += vertexStride;
688 textureCoords = (uint16_t*)textureCoordOffset;
689 textureCoords[0] = r;
Brian Salomon57caa662017-10-18 12:21:05 +0000690 textureCoords[1] = t;
Jim Van Verth33632d82017-02-28 10:24:39 -0500691 textureCoordOffset += vertexStride;
692 textureCoords = (uint16_t*)textureCoordOffset;
693 textureCoords[0] = r;
Brian Salomon57caa662017-10-18 12:21:05 +0000694 textureCoords[1] = b;
joshualitt5bf99f12015-03-13 11:47:42 -0700695 }
696
Brian Salomone5b399e2017-07-19 13:50:54 -0400697 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400698 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
699 if (gp->numTextureSamplers() != (int)fAtlas->pageCount()) {
700 // During preparation the number of atlas pages has increased.
701 // Update the proxies used in the GP to match.
702 if (fUsesDistanceField) {
703 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
704 fAtlas->getProxies(), GrSamplerState::ClampBilerp());
705 } else {
706 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
707 fAtlas->getProxies(), GrSamplerState::ClampNearest());
708 }
709 }
710
bsalomon6d6b6ad2016-07-13 14:45:28 -0700711 if (flushInfo->fInstancesToFlush) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600712 GrMesh mesh(GrPrimitiveType::kTriangles);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700713 int maxInstancesPerDraw =
714 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Daltonbca46e22017-05-15 11:03:26 -0600715 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerQuad,
Chris Dalton114a3c02017-05-26 15:17:19 -0600716 kVerticesPerQuad, flushInfo->fInstancesToFlush,
717 maxInstancesPerDraw);
718 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomonfebbd232017-07-11 15:52:02 -0400719 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700720 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
721 flushInfo->fInstancesToFlush = 0;
722 }
joshualitt5bf99f12015-03-13 11:47:42 -0700723 }
724
Brian Salomond0a0a652016-12-15 15:25:22 -0500725 GrColor color() const { return fShapes[0].fColor; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500726 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700727
Brian Salomon25a88092016-12-01 09:36:50 -0500728 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400729 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400730 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt8cab9a72015-07-16 09:13:50 -0700731 return false;
732 }
733
Jim Van Verth33632d82017-02-28 10:24:39 -0500734 if (this->usesDistanceField() != that->usesDistanceField()) {
735 return false;
736 }
737
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400738 const SkMatrix& thisCtm = this->fShapes[0].fViewMatrix;
739 const SkMatrix& thatCtm = that->fShapes[0].fViewMatrix;
740
741 if (thisCtm.hasPerspective() != thatCtm.hasPerspective()) {
joshualitt5bf99f12015-03-13 11:47:42 -0700742 return false;
743 }
744
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400745 // We can position on the cpu unless we're in perspective,
746 // but also need to make sure local matrices are identical
747 if ((thisCtm.hasPerspective() || fHelper.usesLocalCoords()) &&
748 !thisCtm.cheapEqualTo(thatCtm)) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500749 return false;
750 }
751
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400752 // Depending on the ctm we may have a different shader for SDF paths
753 if (this->usesDistanceField()) {
754 if (thisCtm.isScaleTranslate() != thatCtm.isScaleTranslate() ||
755 thisCtm.isSimilarity() != thatCtm.isSimilarity()) {
756 return false;
757 }
758 }
759
Brian Salomond0a0a652016-12-15 15:25:22 -0500760 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700761 this->joinBounds(*that);
joshualitt5bf99f12015-03-13 11:47:42 -0700762 return true;
763 }
764
Jim Van Verth33632d82017-02-28 10:24:39 -0500765 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700766
Brian Salomond0a0a652016-12-15 15:25:22 -0500767 struct Entry {
Jim Van Verth33632d82017-02-28 10:24:39 -0500768 GrColor fColor;
769 GrShape fShape;
Jim Van Verth5698c8a2017-10-12 10:18:44 -0400770 SkMatrix fViewMatrix;
bsalomonf1703092016-06-29 18:41:53 -0700771 };
772
Brian Salomond0a0a652016-12-15 15:25:22 -0500773 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400774 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500775 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700776 ShapeCache* fShapeCache;
777 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700778 bool fGammaCorrect;
reed1b55a962015-09-17 20:16:13 -0700779
Brian Salomonfebbd232017-07-11 15:52:02 -0400780 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700781};
782
Jim Van Verth83010462017-03-16 08:45:39 -0400783bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400784 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400785 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700786
jvanverthfa38a302014-10-06 05:59:05 -0700787 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700788 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700789 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700790 if (!fAtlas) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500791 fAtlas = GrDrawOpAtlas::Make(args.fContext,
792 kAlpha_8_GrPixelConfig,
793 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
794 NUM_PLOTS_X, NUM_PLOTS_Y,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500795 GrDrawOpAtlas::AllowMultitexturing::kYes,
Jim Van Verth83010462017-03-16 08:45:39 -0400796 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500797 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700798 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700799 return false;
800 }
801 }
802
Brian Salomonfebbd232017-07-11 15:52:02 -0400803 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
804 std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), &fShapeCache,
805 &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
806 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800807
jvanverthfa38a302014-10-06 05:59:05 -0700808 return true;
809}
810
joshualitt21279c72015-05-11 07:21:37 -0700811///////////////////////////////////////////////////////////////////////////////////////////////////
812
Hal Canary6f6961e2017-01-31 13:50:44 -0500813#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700814
Brian Salomonfebbd232017-07-11 15:52:02 -0400815struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700816 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700817 ~PathTestStruct() { this->reset(); }
818
819 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700820 ShapeDataList::Iter iter;
821 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
822 ShapeData* shapeData;
823 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700824 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700825 fShapeList.remove(shapeData);
826 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700827 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500828 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700829 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700830 }
831
Brian Salomon2ee084e2016-12-16 18:59:19 -0500832 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700833 PathTestStruct* dfpr = (PathTestStruct*)pr;
834 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700835 ShapeDataList::Iter iter;
836 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
837 ShapeData* shapeData;
838 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700839 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700840 if (id == shapeData->fID) {
841 dfpr->fShapeCache.remove(shapeData->fKey);
842 dfpr->fShapeList.remove(shapeData);
843 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700844 }
845 }
846 }
847
848 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500849 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700850 ShapeCache fShapeCache;
851 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700852};
853
Brian Salomonfebbd232017-07-11 15:52:02 -0400854GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
855 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700856 static PathTestStruct gTestStruct;
857
858 if (context->uniqueID() != gTestStruct.fContextID) {
859 gTestStruct.fContextID = context->uniqueID();
860 gTestStruct.reset();
Robert Phillips256c37b2017-03-01 14:32:46 -0500861 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context, kAlpha_8_GrPixelConfig,
862 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
863 NUM_PLOTS_X, NUM_PLOTS_Y,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500864 GrDrawOpAtlas::AllowMultitexturing::kYes,
Robert Phillips256c37b2017-03-01 14:32:46 -0500865 &PathTestStruct::HandleEviction,
866 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700867 }
868
869 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700870 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700871
bsalomonee432412016-06-27 07:18:18 -0700872 // This path renderer only allows fill styles.
873 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
joshualitt21279c72015-05-11 07:21:37 -0700874
Brian Salomonfebbd232017-07-11 15:52:02 -0400875 return GrSmallPathRenderer::SmallPathOp::Make(
876 std::move(paint), shape, viewMatrix, gTestStruct.fAtlas.get(), &gTestStruct.fShapeCache,
877 &gTestStruct.fShapeList, gammaCorrect, GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700878}
879
880#endif