blob: 222f6626a7886eaf84ef562e48d526ab4bd22dc2 [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"
jvanverthfa38a302014-10-06 05:59:05 -070010
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
jvanverthfa38a302014-10-06 05:59:05 -070012#include "GrContext.h"
Jim Van Verthf9e678d2017-02-15 15:46:52 -050013#include "GrDistanceFieldGenFromVector.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050014#include "GrDrawOpTest.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 }
jvanverthb61283f2014-10-30 05:57:21 -0700105 // currently don't support perspective
bsalomon0aff2fa2015-07-31 06:48:27 -0700106 if (args.fViewMatrix->hasPerspective()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600107 return CanDrawPath::kNo;
jvanverthfa38a302014-10-06 05:59:05 -0700108 }
halcanary9d524f22016-03-29 09:03:52 -0700109
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500110 // Only support paths with bounds within kMaxDim by kMaxDim,
111 // scaled to have bounds within kMaxSize by kMaxSize.
Jim Van Verth77047542017-01-11 14:17:00 -0500112 // The goal is to accelerate rendering of lots of small paths that may be scaling.
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500113 SkScalar scaleFactors[2];
114 if (!args.fViewMatrix->getMinMaxScales(scaleFactors)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600115 return CanDrawPath::kNo;
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500116 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700117 SkRect bounds = args.fShape->styledBounds();
Jim Van Verthf9e678d2017-02-15 15:46:52 -0500118 SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
bsalomon6663acf2016-05-10 09:14:17 -0700119 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
Jim Van Verthd25cc9b2017-02-16 10:01:46 -0500120 SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
121 SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
Chris Dalton5ed44232017-09-07 13:22:46 -0600122 if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
123 return CanDrawPath::kNo;
124 }
bsalomon6266dca2016-03-11 06:22:00 -0800125
Chris Dalton5ed44232017-09-07 13:22:46 -0600126 return CanDrawPath::kYes;
jvanverthfa38a302014-10-06 05:59:05 -0700127}
128
jvanverthfa38a302014-10-06 05:59:05 -0700129////////////////////////////////////////////////////////////////////////////////
130
joshualitt5bf99f12015-03-13 11:47:42 -0700131// padding around path bounds to allow for antialiased pixels
132static const SkScalar kAntiAliasPad = 1.0f;
133
Brian Salomonfebbd232017-07-11 15:52:02 -0400134class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
135private:
136 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
137
joshualitt5bf99f12015-03-13 11:47:42 -0700138public:
Brian Salomon25a88092016-12-01 09:36:50 -0500139 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700140
Jim Van Verth83010462017-03-16 08:45:39 -0400141 using ShapeData = GrSmallPathRenderer::ShapeData;
Brian Salomond0a0a652016-12-15 15:25:22 -0500142 using ShapeCache = SkTDynamicHash<ShapeData, ShapeData::Key>;
Jim Van Verth83010462017-03-16 08:45:39 -0400143 using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700144
Brian Salomonfebbd232017-07-11 15:52:02 -0400145 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const GrShape& shape,
146 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas,
147 ShapeCache* shapeCache, ShapeDataList* shapeList,
148 bool gammaCorrect,
149 const GrUserStencilSettings* stencilSettings) {
150 return Helper::FactoryHelper<SmallPathOp>(std::move(paint), shape, viewMatrix, atlas,
151 shapeCache, shapeList, gammaCorrect,
152 stencilSettings);
joshualitt5bf99f12015-03-13 11:47:42 -0700153 }
Brian Salomond0a0a652016-12-15 15:25:22 -0500154
Brian Salomonfebbd232017-07-11 15:52:02 -0400155 SmallPathOp(Helper::MakeArgs helperArgs, GrColor color, const GrShape& shape,
156 const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
157 ShapeDataList* shapeList, bool gammaCorrect,
158 const GrUserStencilSettings* stencilSettings)
159 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500160 SkASSERT(shape.hasUnstyledKey());
Jim Van Verth33632d82017-02-28 10:24:39 -0500161 // Compute bounds
162 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
163
Jim Van Verth83010462017-03-16 08:45:39 -0400164#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
Jim Van Verth33632d82017-02-28 10:24:39 -0500165 fUsesDistanceField = true;
166#else
Jim Van Verth83010462017-03-16 08:45:39 -0400167 // only use distance fields on desktop and Android framework to save space in the atlas
Jim Van Verth33632d82017-02-28 10:24:39 -0500168 fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
169#endif
Brian Salomond0a0a652016-12-15 15:25:22 -0500170 fViewMatrix = viewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500171 SkVector translate = SkVector::Make(0, 0);
172 if (!fUsesDistanceField) {
173 // In this case we don't apply a view matrix, so we need to remove the non-subpixel
174 // translation and add it back when we generate the quad for the path
175 SkScalar translateX = viewMatrix.getTranslateX();
176 SkScalar translateY = viewMatrix.getTranslateY();
177 translate = SkVector::Make(SkScalarFloorToScalar(translateX),
178 SkScalarFloorToScalar(translateY));
179 // Only store the fractional part of the translation in the view matrix
180 fViewMatrix.setTranslateX(translateX - translate.fX);
181 fViewMatrix.setTranslateY(translateY - translate.fY);
182 }
183
184 fShapes.emplace_back(Entry{color, shape, translate});
Brian Salomond0a0a652016-12-15 15:25:22 -0500185
186 fAtlas = atlas;
187 fShapeCache = shapeCache;
188 fShapeList = shapeList;
189 fGammaCorrect = gammaCorrect;
190
Brian Salomond0a0a652016-12-15 15:25:22 -0500191 }
192
Brian Salomonfebbd232017-07-11 15:52:02 -0400193 const char* name() const override { return "SmallPathOp"; }
194
Robert Phillipsf1748f52017-09-14 14:11:24 -0400195 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400196 fHelper.visitProxies(func);
197
198 const sk_sp<GrTextureProxy>* proxies = fAtlas->getProxies();
199 for (int i = 0; i < GrDrawOpAtlas::kMaxPages; ++i) {
200 if (proxies[i].get()) {
201 func(proxies[i].get());
202 }
203 }
204 }
205
Brian Salomonfebbd232017-07-11 15:52:02 -0400206 SkString dumpInfo() const override {
207 SkString string;
208 for (const auto& geo : fShapes) {
209 string.appendf("Color: 0x%08x\n", geo.fColor);
210 }
211 string += fHelper.dumpInfo();
212 string += INHERITED::dumpInfo();
213 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500214 }
215
Brian Salomonfebbd232017-07-11 15:52:02 -0400216 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
217
Brian Osman9a725dd2017-09-20 09:53:22 -0400218 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
219 GrPixelConfigIsClamped dstIsClamped) override {
220 return fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
221 GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonfebbd232017-07-11 15:52:02 -0400222 &fShapes.front().fColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700223 }
224
Brian Salomonfebbd232017-07-11 15:52:02 -0400225private:
bsalomonb5238a72015-05-05 07:49:49 -0700226 struct FlushInfo {
Hal Canary144caf52016-11-07 17:57:18 -0500227 sk_sp<const GrBuffer> fVertexBuffer;
228 sk_sp<const GrBuffer> fIndexBuffer;
bungeman06ca8ec2016-06-09 08:01:03 -0700229 sk_sp<GrGeometryProcessor> fGeometryProcessor;
Brian Salomonfebbd232017-07-11 15:52:02 -0400230 const GrPipeline* fPipeline;
bsalomonb5238a72015-05-05 07:49:49 -0700231 int fVertexOffset;
232 int fInstancesToFlush;
233 };
234
Brian Salomon91326c32017-08-09 16:02:19 -0400235 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500236 int instanceCount = fShapes.count();
jvanverthcf371bb2016-03-10 11:10:43 -0800237 const SkMatrix& ctm = this->viewMatrix();
joshualitt5bf99f12015-03-13 11:47:42 -0700238
bsalomon342bfc22016-04-01 06:06:20 -0700239 FlushInfo flushInfo;
Brian Salomonfebbd232017-07-11 15:52:02 -0400240 flushInfo.fPipeline = fHelper.makePipeline(target);
joshualitt5bf99f12015-03-13 11:47:42 -0700241 // Setup GrGeometryProcessor
Brian Salomon2ee084e2016-12-16 18:59:19 -0500242 GrDrawOpAtlas* atlas = fAtlas;
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400243 uint32_t atlasPageCount = atlas->pageCount();
244 if (!atlasPageCount) {
245 return;
246 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500247 if (fUsesDistanceField) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500248 uint32_t flags = 0;
249 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
250 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
251 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
252
Robert Phillips296b1cc2017-03-15 10:42:12 -0400253 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
Jim Van Vertha950b632017-09-12 11:54:11 -0400254 this->color(), this->viewMatrix(), atlas->getProxies(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400255 GrSamplerState::ClampBilerp(), flags, fHelper.usesLocalCoords());
Jim Van Verth33632d82017-02-28 10:24:39 -0500256 } else {
Jim Van Verth33632d82017-02-28 10:24:39 -0500257 SkMatrix invert;
Brian Salomonfebbd232017-07-11 15:52:02 -0400258 if (fHelper.usesLocalCoords()) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500259 if (!this->viewMatrix().invert(&invert)) {
260 SkDebugf("Could not invert viewmatrix\n");
261 return;
262 }
263 // for local coords, we need to add the translation back in that we removed
264 // from the stored view matrix
265 invert.preTranslate(-fShapes[0].fTranslate.fX, -fShapes[0].fTranslate.fY);
266 }
267
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400268 flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
Jim Van Vertha950b632017-09-12 11:54:11 -0400269 this->color(), atlas->getProxies(), GrSamplerState::ClampNearest(),
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400270 kA8_GrMaskFormat, invert, fHelper.usesLocalCoords());
Jim Van Verth33632d82017-02-28 10:24:39 -0500271 }
joshualitt5bf99f12015-03-13 11:47:42 -0700272
joshualitt5bf99f12015-03-13 11:47:42 -0700273 // allocate vertices
bsalomon342bfc22016-04-01 06:06:20 -0700274 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
Jim Van Verth33632d82017-02-28 10:24:39 -0500275 SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(GrColor) + 2*sizeof(uint16_t));
bsalomonb5238a72015-05-05 07:49:49 -0700276
cdalton397536c2016-03-25 12:15:03 -0700277 const GrBuffer* vertexBuffer;
bsalomon75398562015-08-17 12:55:38 -0700278 void* vertices = target->makeVertexSpace(vertexStride,
279 kVerticesPerQuad * instanceCount,
280 &vertexBuffer,
281 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -0700282 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -0700283 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -0700284 if (!vertices || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700285 SkDebugf("Could not allocate vertices\n");
286 return;
287 }
288
bsalomonb5238a72015-05-05 07:49:49 -0700289 flushInfo.fInstancesToFlush = 0;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700290 // Pointer to the next set of vertices to write.
291 intptr_t offset = reinterpret_cast<intptr_t>(vertices);
joshualitt5bf99f12015-03-13 11:47:42 -0700292 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500293 const Entry& args = fShapes[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700294
Jim Van Verth33632d82017-02-28 10:24:39 -0500295 ShapeData* shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500296 if (fUsesDistanceField) {
297 // get mip level
Robert Phillips3cf781d2017-08-22 18:25:11 -0400298 SkScalar maxScale = SkScalarAbs(this->viewMatrix().getMaxScale());
Jim Van Verth33632d82017-02-28 10:24:39 -0500299 const SkRect& bounds = args.fShape.bounds();
300 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
301 // We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
302 // In the majority of cases this will yield a crisper rendering.
303 SkScalar mipScale = 1.0f;
304 // Our mipscale is the maxScale clamped to the next highest power of 2
305 if (maxScale <= SK_ScalarHalf) {
306 SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
307 mipScale = SkScalarPow(2, -log);
308 } else if (maxScale > SK_Scalar1) {
309 SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
310 mipScale = SkScalarPow(2, log);
joshualitt5bf99f12015-03-13 11:47:42 -0700311 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500312 SkASSERT(maxScale <= mipScale);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500313
Jim Van Verth33632d82017-02-28 10:24:39 -0500314 SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
315 // For sizes less than kIdealMinMIP we want to use as large a distance field as we can
316 // so we can preserve as much detail as possible. However, we can't scale down more
317 // than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
318 // just bigger than the ideal, and then scale down until we are no more than 4x the
319 // original mipsize.
320 if (mipSize < kIdealMinMIP) {
321 SkScalar newMipSize = mipSize;
322 do {
323 newMipSize *= 2;
324 } while (newMipSize < kIdealMinMIP);
325 while (newMipSize > 4 * mipSize) {
326 newMipSize *= 0.25f;
327 }
328 mipSize = newMipSize;
joshualitt5bf99f12015-03-13 11:47:42 -0700329 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500330 SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500331
Jim Van Verth33632d82017-02-28 10:24:39 -0500332 // check to see if df path is cached
333 ShapeData::Key key(args.fShape, SkScalarCeilToInt(desiredDimension));
334 shapeData = fShapeCache->find(key);
335 if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
336 // Remove the stale cache entry
337 if (shapeData) {
338 fShapeCache->remove(shapeData->fKey);
339 fShapeList->remove(shapeData);
340 delete shapeData;
341 }
342 SkScalar scale = desiredDimension / maxDim;
343
344 shapeData = new ShapeData;
345 if (!this->addDFPathToAtlas(target,
346 &flushInfo,
347 atlas,
348 shapeData,
349 args.fShape,
350 SkScalarCeilToInt(desiredDimension),
351 scale)) {
352 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500353 continue;
354 }
Jim Van Verthc0bc1bb2017-02-27 18:21:16 -0500355 }
Jim Van Verth33632d82017-02-28 10:24:39 -0500356 } else {
357 // check to see if bitmap path is cached
358 ShapeData::Key key(args.fShape, this->viewMatrix());
359 shapeData = fShapeCache->find(key);
360 if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
361 // Remove the stale cache entry
362 if (shapeData) {
363 fShapeCache->remove(shapeData->fKey);
364 fShapeList->remove(shapeData);
365 delete shapeData;
366 }
367
368 shapeData = new ShapeData;
369 if (!this->addBMPathToAtlas(target,
Robert Phillips8296e752017-08-25 08:45:21 -0400370 &flushInfo,
371 atlas,
372 shapeData,
373 args.fShape,
374 this->viewMatrix())) {
Jim Van Verth33632d82017-02-28 10:24:39 -0500375 delete shapeData;
Jim Van Verth33632d82017-02-28 10:24:39 -0500376 continue;
377 }
378 }
joshualitt5bf99f12015-03-13 11:47:42 -0700379 }
380
bsalomonee432412016-06-27 07:18:18 -0700381 atlas->setLastUseToken(shapeData->fID, target->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700382
bsalomon75398562015-08-17 12:55:38 -0700383 this->writePathVertices(target,
bsalomonb5238a72015-05-05 07:49:49 -0700384 atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800385 offset,
386 args.fColor,
bsalomonb5238a72015-05-05 07:49:49 -0700387 vertexStride,
Jim Van Verth33632d82017-02-28 10:24:39 -0500388 args.fTranslate,
bsalomonee432412016-06-27 07:18:18 -0700389 shapeData);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700390 offset += kVerticesPerQuad * vertexStride;
bsalomonb5238a72015-05-05 07:49:49 -0700391 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700392 }
393
bsalomon75398562015-08-17 12:55:38 -0700394 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700395 }
396
Brian Salomone5b399e2017-07-19 13:50:54 -0400397 bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400398 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
399 uint32_t dimension, SkScalar scale) const {
bsalomonee432412016-06-27 07:18:18 -0700400 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700401
402 // generate bounding rect for bitmap draw
403 SkRect scaledBounds = bounds;
404 // scale to mip level size
405 scaledBounds.fLeft *= scale;
406 scaledBounds.fTop *= scale;
407 scaledBounds.fRight *= scale;
408 scaledBounds.fBottom *= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500409 // subtract out integer portion of origin
410 // (SDF created will be placed with fractional offset burnt in)
Jim Van Verth07b6ad02016-12-20 10:23:09 -0500411 SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
412 SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
joshualitt5bf99f12015-03-13 11:47:42 -0700413 scaledBounds.offset(-dx, -dy);
414 // get integer boundary
415 SkIRect devPathBounds;
416 scaledBounds.roundOut(&devPathBounds);
417 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800418 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
Jim Van Verthecdb6862016-12-13 18:17:47 -0500419 // place devBounds at origin
420 int width = devPathBounds.width() + 2*intPad;
421 int height = devPathBounds.height() + 2*intPad;
422 devPathBounds = SkIRect::MakeWH(width, height);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400423 SkScalar translateX = intPad - dx;
424 SkScalar translateY = intPad - dy;
joshualitt5bf99f12015-03-13 11:47:42 -0700425
426 // draw path to bitmap
427 SkMatrix drawMatrix;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500428 drawMatrix.setScale(scale, scale);
Robert Phillips3cf781d2017-08-22 18:25:11 -0400429 drawMatrix.postTranslate(translateX, translateY);
joshualitt5bf99f12015-03-13 11:47:42 -0700430
jvanverth512e4372015-11-23 11:50:02 -0800431 SkASSERT(devPathBounds.fLeft == 0);
432 SkASSERT(devPathBounds.fTop == 0);
Jim Van Verth25b8ca12017-02-17 14:02:13 -0500433 SkASSERT(devPathBounds.width() > 0);
434 SkASSERT(devPathBounds.height() > 0);
bsalomonf93f5152016-10-26 08:00:00 -0700435
joel.liang8cbb4242017-01-09 18:39:43 -0800436 // setup signed distance field storage
437 SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
438 width = dfBounds.width();
439 height = dfBounds.height();
rmistry47842252016-12-21 04:25:18 -0800440 // TODO We should really generate this directly into the plot somehow
441 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
joel.liang6d2f73c2016-12-20 18:58:53 -0800442
joel.liang8cbb4242017-01-09 18:39:43 -0800443 SkPath path;
444 shape.asPath(&path);
445#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
446 // Generate signed distance field directly from SkPath
447 bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
448 path, drawMatrix,
449 width, height, width * sizeof(unsigned char));
450 if (!succeed) {
451#endif
452 // setup bitmap backing
453 SkAutoPixmapStorage dst;
454 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
455 devPathBounds.height()))) {
456 return false;
457 }
Mike Reedcd284c52017-09-29 15:22:56 -0400458 sk_bzero(dst.writable_addr(), dst.computeByteSize());
joel.liang8cbb4242017-01-09 18:39:43 -0800459
460 // rasterize path
461 SkPaint paint;
462 paint.setStyle(SkPaint::kFill_Style);
463 paint.setAntiAlias(true);
464
465 SkDraw draw;
466 sk_bzero(&draw, sizeof(draw));
467
468 SkRasterClip rasterClip;
469 rasterClip.setRect(devPathBounds);
470 draw.fRC = &rasterClip;
471 draw.fMatrix = &drawMatrix;
472 draw.fDst = dst;
473
474 draw.drawPathCoverage(path, paint);
475
476 // Generate signed distance field
477 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
478 (const unsigned char*)dst.addr(),
479 dst.width(), dst.height(), dst.rowBytes());
480#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
481 }
482#endif
joshualitt5bf99f12015-03-13 11:47:42 -0700483
484 // add to atlas
485 SkIPoint16 atlasLocation;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500486 GrDrawOpAtlas::AtlasID id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500487 if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) {
bsalomon75398562015-08-17 12:55:38 -0700488 this->flush(target, flushInfo);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700489 if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) {
490 return false;
491 }
joshualitt5bf99f12015-03-13 11:47:42 -0700492 }
493
494 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700495 shapeData->fKey.set(shape, dimension);
bsalomonee432412016-06-27 07:18:18 -0700496 shapeData->fID = id;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500497
Robert Phillips3cf781d2017-08-22 18:25:11 -0400498 shapeData->fBounds = SkRect::Make(devPathBounds);
499 shapeData->fBounds.offset(-translateX, -translateY);
500 shapeData->fBounds.fLeft /= scale;
501 shapeData->fBounds.fTop /= scale;
502 shapeData->fBounds.fRight /= scale;
503 shapeData->fBounds.fBottom /= scale;
Jim Van Verthecdb6862016-12-13 18:17:47 -0500504
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400505 // We pack the 2bit page index in the low bit of the u and v texture coords
506 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
507 SkASSERT(pageIndex < 4);
508 uint16_t uBit = (pageIndex >> 1) & 0x1;
509 uint16_t vBit = pageIndex & 0x1;
510 shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1 | uBit,
511 (atlasLocation.fY+SK_DistanceFieldPad) << 1 | vBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400512 (atlasLocation.fX+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400513 devPathBounds.width()) << 1 | uBit,
Jim Van Verth6a7a7042017-09-11 11:04:10 -0400514 (atlasLocation.fY+SK_DistanceFieldPad+
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400515 devPathBounds.height()) << 1 | vBit);
joshualitt5bf99f12015-03-13 11:47:42 -0700516
bsalomonee432412016-06-27 07:18:18 -0700517 fShapeCache->add(shapeData);
518 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700519#ifdef DF_PATH_TRACKING
520 ++g_NumCachedPaths;
521#endif
522 return true;
523 }
524
Brian Salomone5b399e2017-07-19 13:50:54 -0400525 bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400526 GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
527 const SkMatrix& ctm) const {
Jim Van Verth33632d82017-02-28 10:24:39 -0500528 const SkRect& bounds = shape.bounds();
529 if (bounds.isEmpty()) {
530 return false;
531 }
532 SkMatrix drawMatrix(ctm);
533 drawMatrix.set(SkMatrix::kMTransX, SkScalarFraction(ctm.get(SkMatrix::kMTransX)));
534 drawMatrix.set(SkMatrix::kMTransY, SkScalarFraction(ctm.get(SkMatrix::kMTransY)));
535 SkRect shapeDevBounds;
536 drawMatrix.mapRect(&shapeDevBounds, bounds);
537 SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
538 SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
539
540 // get integer boundary
541 SkIRect devPathBounds;
542 shapeDevBounds.roundOut(&devPathBounds);
543 // pad to allow room for antialiasing
544 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
545 // place devBounds at origin
546 int width = devPathBounds.width() + 2 * intPad;
547 int height = devPathBounds.height() + 2 * intPad;
548 devPathBounds = SkIRect::MakeWH(width, height);
549 SkScalar translateX = intPad - dx;
550 SkScalar translateY = intPad - dy;
551
552 SkASSERT(devPathBounds.fLeft == 0);
553 SkASSERT(devPathBounds.fTop == 0);
554 SkASSERT(devPathBounds.width() > 0);
555 SkASSERT(devPathBounds.height() > 0);
556
557 SkPath path;
558 shape.asPath(&path);
559 // setup bitmap backing
560 SkAutoPixmapStorage dst;
561 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
562 devPathBounds.height()))) {
563 return false;
564 }
Mike Reedcd284c52017-09-29 15:22:56 -0400565 sk_bzero(dst.writable_addr(), dst.computeByteSize());
Jim Van Verth33632d82017-02-28 10:24:39 -0500566
567 // rasterize path
568 SkPaint paint;
569 paint.setStyle(SkPaint::kFill_Style);
570 paint.setAntiAlias(true);
571
572 SkDraw draw;
573 sk_bzero(&draw, sizeof(draw));
574
575 SkRasterClip rasterClip;
576 rasterClip.setRect(devPathBounds);
577 draw.fRC = &rasterClip;
578 drawMatrix.postTranslate(translateX, translateY);
579 draw.fMatrix = &drawMatrix;
580 draw.fDst = dst;
581
582 draw.drawPathCoverage(path, paint);
583
584 // add to atlas
585 SkIPoint16 atlasLocation;
586 GrDrawOpAtlas::AtlasID id;
587 if (!atlas->addToAtlas(&id, target, dst.width(), dst.height(), dst.addr(),
588 &atlasLocation)) {
589 this->flush(target, flushInfo);
590 if (!atlas->addToAtlas(&id, target, dst.width(), dst.height(), dst.addr(),
591 &atlasLocation)) {
592 return false;
593 }
594 }
595
596 // add to cache
597 shapeData->fKey.set(shape, ctm);
598 shapeData->fID = id;
599
Jim Van Verth33632d82017-02-28 10:24:39 -0500600 shapeData->fBounds = SkRect::Make(devPathBounds);
601 shapeData->fBounds.offset(-translateX, -translateY);
602
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400603 // We pack the 2bit page index in the low bit of the u and v texture coords
604 uint16_t pageIndex = GrDrawOpAtlas::GetPageIndexFromID(id);
605 SkASSERT(pageIndex < 4);
606 uint16_t uBit = (pageIndex >> 1) & 0x1;
607 uint16_t vBit = pageIndex & 0x1;
608 shapeData->fTextureCoords.set(atlasLocation.fX << 1 | uBit, atlasLocation.fY << 1 | vBit,
609 (atlasLocation.fX+width) << 1 | uBit,
610 (atlasLocation.fY+height) << 1 | vBit);
Jim Van Verth33632d82017-02-28 10:24:39 -0500611
612 fShapeCache->add(shapeData);
613 fShapeList->addToTail(shapeData);
614#ifdef DF_PATH_TRACKING
615 ++g_NumCachedPaths;
616#endif
617 return true;
618 }
619
Brian Salomon9afd3712016-12-01 10:59:09 -0500620 void writePathVertices(GrDrawOp::Target* target,
Brian Salomon2ee084e2016-12-16 18:59:19 -0500621 GrDrawOpAtlas* atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800622 intptr_t offset,
623 GrColor color,
bsalomonb5238a72015-05-05 07:49:49 -0700624 size_t vertexStride,
Jim Van Verth33632d82017-02-28 10:24:39 -0500625 const SkVector& preTranslate,
bsalomonee432412016-06-27 07:18:18 -0700626 const ShapeData* shapeData) const {
joshualitt53f26aa2015-12-10 07:29:54 -0800627 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
628
Jim Van Verth77047542017-01-11 14:17:00 -0500629 SkRect bounds = shapeData->fBounds;
Jim Van Verth77047542017-01-11 14:17:00 -0500630
joshualitt5bf99f12015-03-13 11:47:42 -0700631 // vertex positions
632 // TODO make the vertex attributes a struct
Jim Van Verth33632d82017-02-28 10:24:39 -0500633 positions->setRectFan(bounds.left() + preTranslate.fX,
634 bounds.top() + preTranslate.fY,
635 bounds.right() + preTranslate.fX,
636 bounds.bottom() + preTranslate.fY,
Jim Van Verth77047542017-01-11 14:17:00 -0500637 vertexStride);
joshualitt5bf99f12015-03-13 11:47:42 -0700638
joshualitt53f26aa2015-12-10 07:29:54 -0800639 // colors
640 for (int i = 0; i < kVerticesPerQuad; i++) {
641 GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride);
642 *colorPtr = color;
643 }
644
Jim Van Verth77047542017-01-11 14:17:00 -0500645 // set up texture coordinates
Robert Phillips8296e752017-08-25 08:45:21 -0400646 uint16_t l = shapeData->fTextureCoords.fLeft;
647 uint16_t t = shapeData->fTextureCoords.fTop;
648 uint16_t r = shapeData->fTextureCoords.fRight;
649 uint16_t b = shapeData->fTextureCoords.fBottom;
Jim Van Verth33632d82017-02-28 10:24:39 -0500650
651 // set vertex texture coords
652 intptr_t textureCoordOffset = offset + sizeof(SkPoint) + sizeof(GrColor);
653 uint16_t* textureCoords = (uint16_t*) textureCoordOffset;
654 textureCoords[0] = l;
655 textureCoords[1] = t;
656 textureCoordOffset += vertexStride;
657 textureCoords = (uint16_t*)textureCoordOffset;
658 textureCoords[0] = l;
659 textureCoords[1] = b;
660 textureCoordOffset += vertexStride;
661 textureCoords = (uint16_t*)textureCoordOffset;
662 textureCoords[0] = r;
663 textureCoords[1] = b;
664 textureCoordOffset += vertexStride;
665 textureCoords = (uint16_t*)textureCoordOffset;
666 textureCoords[0] = r;
667 textureCoords[1] = t;
joshualitt5bf99f12015-03-13 11:47:42 -0700668 }
669
Brian Salomone5b399e2017-07-19 13:50:54 -0400670 void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
Jim Van Vertheafa64b2017-09-18 10:05:00 -0400671 GrGeometryProcessor* gp = flushInfo->fGeometryProcessor.get();
672 if (gp->numTextureSamplers() != (int)fAtlas->pageCount()) {
673 // During preparation the number of atlas pages has increased.
674 // Update the proxies used in the GP to match.
675 if (fUsesDistanceField) {
676 reinterpret_cast<GrDistanceFieldPathGeoProc*>(gp)->addNewProxies(
677 fAtlas->getProxies(), GrSamplerState::ClampBilerp());
678 } else {
679 reinterpret_cast<GrBitmapTextGeoProc*>(gp)->addNewProxies(
680 fAtlas->getProxies(), GrSamplerState::ClampNearest());
681 }
682 }
683
bsalomon6d6b6ad2016-07-13 14:45:28 -0700684 if (flushInfo->fInstancesToFlush) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600685 GrMesh mesh(GrPrimitiveType::kTriangles);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700686 int maxInstancesPerDraw =
687 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
Chris Daltonbca46e22017-05-15 11:03:26 -0600688 mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerQuad,
Chris Dalton114a3c02017-05-26 15:17:19 -0600689 kVerticesPerQuad, flushInfo->fInstancesToFlush,
690 maxInstancesPerDraw);
691 mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
Brian Salomonfebbd232017-07-11 15:52:02 -0400692 target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700693 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
694 flushInfo->fInstancesToFlush = 0;
695 }
joshualitt5bf99f12015-03-13 11:47:42 -0700696 }
697
Brian Salomond0a0a652016-12-15 15:25:22 -0500698 GrColor color() const { return fShapes[0].fColor; }
699 const SkMatrix& viewMatrix() const { return fViewMatrix; }
Jim Van Verth33632d82017-02-28 10:24:39 -0500700 bool usesDistanceField() const { return fUsesDistanceField; }
joshualitt5bf99f12015-03-13 11:47:42 -0700701
Brian Salomon25a88092016-12-01 09:36:50 -0500702 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Jim Van Verth83010462017-03-16 08:45:39 -0400703 SmallPathOp* that = t->cast<SmallPathOp>();
Brian Salomonfebbd232017-07-11 15:52:02 -0400704 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt8cab9a72015-07-16 09:13:50 -0700705 return false;
706 }
707
Jim Van Verth33632d82017-02-28 10:24:39 -0500708 if (this->usesDistanceField() != that->usesDistanceField()) {
709 return false;
710 }
711
712 // TODO We can position on the cpu for distance field paths
joshualitt5bf99f12015-03-13 11:47:42 -0700713 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
714 return false;
715 }
716
Brian Salomonfebbd232017-07-11 15:52:02 -0400717 if (!this->usesDistanceField() && fHelper.usesLocalCoords() &&
Jim Van Verth33632d82017-02-28 10:24:39 -0500718 !this->fShapes[0].fTranslate.equalsWithinTolerance(that->fShapes[0].fTranslate)) {
719 return false;
720 }
721
Brian Salomond0a0a652016-12-15 15:25:22 -0500722 fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700723 this->joinBounds(*that);
joshualitt5bf99f12015-03-13 11:47:42 -0700724 return true;
725 }
726
Brian Salomond0a0a652016-12-15 15:25:22 -0500727 SkMatrix fViewMatrix;
Jim Van Verth33632d82017-02-28 10:24:39 -0500728 bool fUsesDistanceField;
joshualitt5bf99f12015-03-13 11:47:42 -0700729
Brian Salomond0a0a652016-12-15 15:25:22 -0500730 struct Entry {
Jim Van Verth33632d82017-02-28 10:24:39 -0500731 GrColor fColor;
732 GrShape fShape;
733 SkVector fTranslate;
bsalomonf1703092016-06-29 18:41:53 -0700734 };
735
Brian Salomond0a0a652016-12-15 15:25:22 -0500736 SkSTArray<1, Entry> fShapes;
Brian Salomonfebbd232017-07-11 15:52:02 -0400737 Helper fHelper;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500738 GrDrawOpAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700739 ShapeCache* fShapeCache;
740 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700741 bool fGammaCorrect;
reed1b55a962015-09-17 20:16:13 -0700742
Brian Salomonfebbd232017-07-11 15:52:02 -0400743 typedef GrMeshDrawOp INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700744};
745
Jim Van Verth83010462017-03-16 08:45:39 -0400746bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400747 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Jim Van Verth83010462017-03-16 08:45:39 -0400748 "GrSmallPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700749
jvanverthfa38a302014-10-06 05:59:05 -0700750 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700751 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700752 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700753 if (!fAtlas) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500754 fAtlas = GrDrawOpAtlas::Make(args.fContext,
755 kAlpha_8_GrPixelConfig,
756 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
757 NUM_PLOTS_X, NUM_PLOTS_Y,
Jim Van Verth83010462017-03-16 08:45:39 -0400758 &GrSmallPathRenderer::HandleEviction,
Robert Phillips256c37b2017-03-01 14:32:46 -0500759 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700760 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700761 return false;
762 }
763 }
764
Brian Salomonfebbd232017-07-11 15:52:02 -0400765 std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
766 std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), &fShapeCache,
767 &fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
768 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
joshualitt9491f7f2015-02-11 11:33:38 -0800769
jvanverthfa38a302014-10-06 05:59:05 -0700770 return true;
771}
772
joshualitt21279c72015-05-11 07:21:37 -0700773///////////////////////////////////////////////////////////////////////////////////////////////////
774
Hal Canary6f6961e2017-01-31 13:50:44 -0500775#if GR_TEST_UTILS
joshualitt21279c72015-05-11 07:21:37 -0700776
Brian Salomonfebbd232017-07-11 15:52:02 -0400777struct GrSmallPathRenderer::PathTestStruct {
halcanary96fcdcc2015-08-27 07:41:13 -0700778 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700779 ~PathTestStruct() { this->reset(); }
780
781 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700782 ShapeDataList::Iter iter;
783 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
784 ShapeData* shapeData;
785 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700786 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700787 fShapeList.remove(shapeData);
788 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700789 }
Ben Wagner594f9ed2016-11-08 14:13:39 -0500790 fAtlas = nullptr;
bsalomonee432412016-06-27 07:18:18 -0700791 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700792 }
793
Brian Salomon2ee084e2016-12-16 18:59:19 -0500794 static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
joshualitt21279c72015-05-11 07:21:37 -0700795 PathTestStruct* dfpr = (PathTestStruct*)pr;
796 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700797 ShapeDataList::Iter iter;
798 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
799 ShapeData* shapeData;
800 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700801 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700802 if (id == shapeData->fID) {
803 dfpr->fShapeCache.remove(shapeData->fKey);
804 dfpr->fShapeList.remove(shapeData);
805 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700806 }
807 }
808 }
809
810 uint32_t fContextID;
Brian Salomon2ee084e2016-12-16 18:59:19 -0500811 std::unique_ptr<GrDrawOpAtlas> fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700812 ShapeCache fShapeCache;
813 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700814};
815
Brian Salomonfebbd232017-07-11 15:52:02 -0400816GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
817 using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
joshualitt21279c72015-05-11 07:21:37 -0700818 static PathTestStruct gTestStruct;
819
820 if (context->uniqueID() != gTestStruct.fContextID) {
821 gTestStruct.fContextID = context->uniqueID();
822 gTestStruct.reset();
Robert Phillips256c37b2017-03-01 14:32:46 -0500823 gTestStruct.fAtlas = GrDrawOpAtlas::Make(context, kAlpha_8_GrPixelConfig,
824 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
825 NUM_PLOTS_X, NUM_PLOTS_Y,
826 &PathTestStruct::HandleEviction,
827 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700828 }
829
830 SkMatrix viewMatrix = GrTest::TestMatrix(random);
brianosman0e3c5542016-04-13 13:56:21 -0700831 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700832
bsalomonee432412016-06-27 07:18:18 -0700833 // This path renderer only allows fill styles.
834 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
joshualitt21279c72015-05-11 07:21:37 -0700835
Brian Salomonfebbd232017-07-11 15:52:02 -0400836 return GrSmallPathRenderer::SmallPathOp::Make(
837 std::move(paint), shape, viewMatrix, gTestStruct.fAtlas.get(), &gTestStruct.fShapeCache,
838 &gTestStruct.fShapeList, gammaCorrect, GrGetRandomStencil(random, context));
joshualitt21279c72015-05-11 07:21:37 -0700839}
840
841#endif