blob: a9ba94da404d3d527e946b325dfbe3da45b2b682 [file] [log] [blame]
jvanverthfa38a302014-10-06 05:59:05 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrAADistanceFieldPathRenderer.h"
9
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualitt21279c72015-05-11 07:21:37 -070011#include "GrBatchTest.h"
cdalton397536c2016-03-25 12:15:03 -070012#include "GrBuffer.h"
jvanverthfa38a302014-10-06 05:59:05 -070013#include "GrContext.h"
bsalomonbb243832016-07-22 07:10:19 -070014#include "GrPipelineBuilder.h"
bsalomonb5238a72015-05-05 07:49:49 -070015#include "GrResourceProvider.h"
jvanverthfa38a302014-10-06 05:59:05 -070016#include "GrSurfacePriv.h"
17#include "GrSWMaskHelper.h"
18#include "GrTexturePriv.h"
bsalomon16b99132015-08-13 14:55:50 -070019#include "batches/GrVertexBatch.h"
jvanverth8ed3b9a2015-04-09 08:00:49 -070020#include "effects/GrDistanceFieldGeoProc.h"
jvanverthfa38a302014-10-06 05:59:05 -070021
22#include "SkDistanceFieldGen.h"
23#include "SkRTConf.h"
24
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
39static const int kSmallMIP = 32;
jvanverth512e4372015-11-23 11:50:02 -080040static const int kMediumMIP = 73;
jvanverthfb1e2fc2015-09-15 13:11:11 -070041static const int kLargeMIP = 162;
jvanverthb61283f2014-10-30 05:57:21 -070042
joshualitt5bf99f12015-03-13 11:47:42 -070043// Callback to clear out internal path cache when eviction occurs
44void GrAADistanceFieldPathRenderer::HandleEviction(GrBatchAtlas::AtlasID id, void* pr) {
45 GrAADistanceFieldPathRenderer* dfpr = (GrAADistanceFieldPathRenderer*)pr;
46 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -070047 ShapeDataList::Iter iter;
48 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
49 ShapeData* shapeData;
50 while ((shapeData = iter.get())) {
joshualitt5bf99f12015-03-13 11:47:42 -070051 iter.next();
bsalomonee432412016-06-27 07:18:18 -070052 if (id == shapeData->fID) {
53 dfpr->fShapeCache.remove(shapeData->fKey);
54 dfpr->fShapeList.remove(shapeData);
55 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -070056#ifdef DF_PATH_TRACKING
57 ++g_NumFreedPaths;
58#endif
59 }
60 }
61}
62
jvanverthfa38a302014-10-06 05:59:05 -070063////////////////////////////////////////////////////////////////////////////////
halcanary96fcdcc2015-08-27 07:41:13 -070064GrAADistanceFieldPathRenderer::GrAADistanceFieldPathRenderer() : fAtlas(nullptr) {}
jvanverth6d22eca2014-10-28 11:10:48 -070065
jvanverthfa38a302014-10-06 05:59:05 -070066GrAADistanceFieldPathRenderer::~GrAADistanceFieldPathRenderer() {
bsalomonee432412016-06-27 07:18:18 -070067 ShapeDataList::Iter iter;
68 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
69 ShapeData* shapeData;
70 while ((shapeData = iter.get())) {
jvanverthfa38a302014-10-06 05:59:05 -070071 iter.next();
bsalomonee432412016-06-27 07:18:18 -070072 delete shapeData;
jvanverthfa38a302014-10-06 05:59:05 -070073 }
halcanary385fe4d2015-08-26 13:07:48 -070074 delete fAtlas;
jvanverthb3eb6872014-10-24 07:12:51 -070075
76#ifdef DF_PATH_TRACKING
bsalomonee432412016-06-27 07:18:18 -070077 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
jvanverthb3eb6872014-10-24 07:12:51 -070078#endif
jvanverthfa38a302014-10-06 05:59:05 -070079}
80
81////////////////////////////////////////////////////////////////////////////////
bsalomon0aff2fa2015-07-31 06:48:27 -070082bool GrAADistanceFieldPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomonee432412016-06-27 07:18:18 -070083 if (!args.fShaderCaps->shaderDerivativeSupport()) {
84 return false;
85 }
86 // If the shape has no key then we won't get any reuse.
87 if (!args.fShape->hasUnstyledKey()) {
88 return false;
89 }
90 // This only supports filled paths, however, the caller may apply the style to make a filled
91 // path and try again.
92 if (!args.fShape->style().isSimpleFill()) {
93 return false;
94 }
95 // This does non-inverse antialiased fills.
96 if (!args.fAntiAlias) {
bsalomon6663acf2016-05-10 09:14:17 -070097 return false;
98 }
jvanverthfa38a302014-10-06 05:59:05 -070099 // TODO: Support inverse fill
bsalomondb7979a2016-06-27 11:08:43 -0700100 if (args.fShape->inverseFilled()) {
jvanverthfa38a302014-10-06 05:59:05 -0700101 return false;
102 }
jvanverthb61283f2014-10-30 05:57:21 -0700103 // currently don't support perspective
bsalomon0aff2fa2015-07-31 06:48:27 -0700104 if (args.fViewMatrix->hasPerspective()) {
jvanverthfa38a302014-10-06 05:59:05 -0700105 return false;
106 }
halcanary9d524f22016-03-29 09:03:52 -0700107
jvanverth512e4372015-11-23 11:50:02 -0800108 // only support paths with bounds within kMediumMIP by kMediumMIP,
109 // scaled to have bounds within 2.0f*kLargeMIP by 2.0f*kLargeMIP
jvanverthb61283f2014-10-30 05:57:21 -0700110 // the goal is to accelerate rendering of lots of small paths that may be scaling
bsalomon0aff2fa2015-07-31 06:48:27 -0700111 SkScalar maxScale = args.fViewMatrix->getMaxScale();
bsalomon0a0f67e2016-06-28 11:56:42 -0700112 SkRect bounds = args.fShape->styledBounds();
bsalomon6663acf2016-05-10 09:14:17 -0700113 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
bsalomon6266dca2016-03-11 06:22:00 -0800114
jvanverth512e4372015-11-23 11:50:02 -0800115 return maxDim <= kMediumMIP && maxDim * maxScale <= 2.0f*kLargeMIP;
jvanverthfa38a302014-10-06 05:59:05 -0700116}
117
jvanverthfa38a302014-10-06 05:59:05 -0700118////////////////////////////////////////////////////////////////////////////////
119
joshualitt5bf99f12015-03-13 11:47:42 -0700120// padding around path bounds to allow for antialiased pixels
121static const SkScalar kAntiAliasPad = 1.0f;
122
bsalomonabd30f52015-08-13 13:34:48 -0700123class AADistanceFieldPathBatch : public GrVertexBatch {
joshualitt5bf99f12015-03-13 11:47:42 -0700124public:
reed1b55a962015-09-17 20:16:13 -0700125 DEFINE_BATCH_CLASS_ID
126
bsalomonee432412016-06-27 07:18:18 -0700127 typedef GrAADistanceFieldPathRenderer::ShapeData ShapeData;
128 typedef SkTDynamicHash<ShapeData, ShapeData::Key> ShapeCache;
129 typedef GrAADistanceFieldPathRenderer::ShapeDataList ShapeDataList;
joshualitt5bf99f12015-03-13 11:47:42 -0700130
bsalomonf1703092016-06-29 18:41:53 -0700131 AADistanceFieldPathBatch(GrColor color,
132 const GrShape& shape,
133 bool antiAlias,
134 const SkMatrix& viewMatrix,
135 GrBatchAtlas* atlas,
136 ShapeCache* shapeCache, ShapeDataList* shapeList,
137 bool gammaCorrect)
138 : INHERITED(ClassID()) {
139 SkASSERT(shape.hasUnstyledKey());
140 fBatch.fViewMatrix = viewMatrix;
141 fGeoData.emplace_back(Geometry{color, shape, antiAlias});
joshualitt5bf99f12015-03-13 11:47:42 -0700142
bsalomonf1703092016-06-29 18:41:53 -0700143 fAtlas = atlas;
144 fShapeCache = shapeCache;
145 fShapeList = shapeList;
146 fGammaCorrect = gammaCorrect;
147
148 // Compute bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700149 this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
joshualitt5bf99f12015-03-13 11:47:42 -0700150 }
151
mtklein36352bf2015-03-25 18:17:31 -0700152 const char* name() const override { return "AADistanceFieldPathBatch"; }
joshualitt5bf99f12015-03-13 11:47:42 -0700153
halcanary9d524f22016-03-29 09:03:52 -0700154 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800155 GrInitInvariantOutput* coverage,
156 GrBatchToXPOverrides* overrides) const override {
joshualitt53f26aa2015-12-10 07:29:54 -0800157 color->setKnownFourComponents(fGeoData[0].fColor);
ethannicholasff210322015-11-24 12:10:10 -0800158 coverage->setUnknownSingleComponent();
joshualitt5bf99f12015-03-13 11:47:42 -0700159 }
160
bsalomone46f9fe2015-08-18 06:05:14 -0700161private:
ethannicholasff210322015-11-24 12:10:10 -0800162 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
joshualitt5bf99f12015-03-13 11:47:42 -0700163 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -0800164 if (!overrides.readsColor()) {
joshualitt53f26aa2015-12-10 07:29:54 -0800165 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt5bf99f12015-03-13 11:47:42 -0700166 }
joshualitt53f26aa2015-12-10 07:29:54 -0800167 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt5bf99f12015-03-13 11:47:42 -0700168
169 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -0800170 fBatch.fColorIgnored = !overrides.readsColor();
171 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
172 fBatch.fCoverageIgnored = !overrides.readsCoverage();
joshualitt5bf99f12015-03-13 11:47:42 -0700173 }
174
bsalomonb5238a72015-05-05 07:49:49 -0700175 struct FlushInfo {
bungeman06ca8ec2016-06-09 08:01:03 -0700176 SkAutoTUnref<const GrBuffer> fVertexBuffer;
177 SkAutoTUnref<const GrBuffer> fIndexBuffer;
178 sk_sp<GrGeometryProcessor> fGeometryProcessor;
bsalomonb5238a72015-05-05 07:49:49 -0700179 int fVertexOffset;
180 int fInstancesToFlush;
181 };
182
joshualitt144c3c82015-11-30 12:30:13 -0800183 void onPrepareDraws(Target* target) const override {
joshualitt5bf99f12015-03-13 11:47:42 -0700184 int instanceCount = fGeoData.count();
185
186 SkMatrix invert;
187 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) {
188 SkDebugf("Could not invert viewmatrix\n");
189 return;
190 }
191
jvanverthcf371bb2016-03-10 11:10:43 -0800192 const SkMatrix& ctm = this->viewMatrix();
joshualitt5bf99f12015-03-13 11:47:42 -0700193 uint32_t flags = 0;
jvanverthcf371bb2016-03-10 11:10:43 -0800194 flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
195 flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
brianosman0e3c5542016-04-13 13:56:21 -0700196 flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
joshualitt5bf99f12015-03-13 11:47:42 -0700197
198 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode);
199
bsalomon342bfc22016-04-01 06:06:20 -0700200 FlushInfo flushInfo;
201
joshualitt5bf99f12015-03-13 11:47:42 -0700202 // Setup GrGeometryProcessor
203 GrBatchAtlas* atlas = fAtlas;
bungeman06ca8ec2016-06-09 08:01:03 -0700204 flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(this->color(),
205 this->viewMatrix(),
206 atlas->getTexture(),
207 params,
208 flags,
209 this->usesLocalCoords());
joshualitt5bf99f12015-03-13 11:47:42 -0700210
joshualitt5bf99f12015-03-13 11:47:42 -0700211 // allocate vertices
bsalomon342bfc22016-04-01 06:06:20 -0700212 size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
joshualitt53f26aa2015-12-10 07:29:54 -0800213 SkASSERT(vertexStride == 2 * sizeof(SkPoint) + sizeof(GrColor));
bsalomonb5238a72015-05-05 07:49:49 -0700214
cdalton397536c2016-03-25 12:15:03 -0700215 const GrBuffer* vertexBuffer;
bsalomon75398562015-08-17 12:55:38 -0700216 void* vertices = target->makeVertexSpace(vertexStride,
217 kVerticesPerQuad * instanceCount,
218 &vertexBuffer,
219 &flushInfo.fVertexOffset);
bsalomonb5238a72015-05-05 07:49:49 -0700220 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
bsalomon75398562015-08-17 12:55:38 -0700221 flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
bsalomonb5238a72015-05-05 07:49:49 -0700222 if (!vertices || !flushInfo.fIndexBuffer) {
joshualitt5bf99f12015-03-13 11:47:42 -0700223 SkDebugf("Could not allocate vertices\n");
224 return;
225 }
226
bsalomonb5238a72015-05-05 07:49:49 -0700227 flushInfo.fInstancesToFlush = 0;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700228 // Pointer to the next set of vertices to write.
229 intptr_t offset = reinterpret_cast<intptr_t>(vertices);
joshualitt5bf99f12015-03-13 11:47:42 -0700230 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800231 const Geometry& args = fGeoData[i];
joshualitt5bf99f12015-03-13 11:47:42 -0700232
233 // get mip level
234 SkScalar maxScale = this->viewMatrix().getMaxScale();
bsalomonee432412016-06-27 07:18:18 -0700235 const SkRect& bounds = args.fShape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700236 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
237 SkScalar size = maxScale * maxDim;
238 uint32_t desiredDimension;
239 if (size <= kSmallMIP) {
240 desiredDimension = kSmallMIP;
241 } else if (size <= kMediumMIP) {
242 desiredDimension = kMediumMIP;
243 } else {
244 desiredDimension = kLargeMIP;
245 }
246
247 // check to see if path is cached
bsalomonee432412016-06-27 07:18:18 -0700248 ShapeData::Key key(args.fShape, desiredDimension);
249 ShapeData* shapeData = fShapeCache->find(key);
250 if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
joshualitt5bf99f12015-03-13 11:47:42 -0700251 // Remove the stale cache entry
bsalomonee432412016-06-27 07:18:18 -0700252 if (shapeData) {
253 fShapeCache->remove(shapeData->fKey);
254 fShapeList->remove(shapeData);
255 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700256 }
257 SkScalar scale = desiredDimension/maxDim;
bsalomonee432412016-06-27 07:18:18 -0700258 shapeData = new ShapeData;
bsalomon75398562015-08-17 12:55:38 -0700259 if (!this->addPathToAtlas(target,
bsalomonb5238a72015-05-05 07:49:49 -0700260 &flushInfo,
joshualitt5bf99f12015-03-13 11:47:42 -0700261 atlas,
bsalomonee432412016-06-27 07:18:18 -0700262 shapeData,
263 args.fShape,
joshualitt5bf99f12015-03-13 11:47:42 -0700264 args.fAntiAlias,
265 desiredDimension,
266 scale)) {
bsalomon67c6c8e2016-07-14 07:22:19 -0700267 delete shapeData;
joshualitt5bf99f12015-03-13 11:47:42 -0700268 SkDebugf("Can't rasterize path\n");
bsalomon6d6b6ad2016-07-13 14:45:28 -0700269 continue;
joshualitt5bf99f12015-03-13 11:47:42 -0700270 }
271 }
272
bsalomonee432412016-06-27 07:18:18 -0700273 atlas->setLastUseToken(shapeData->fID, target->nextDrawToken());
joshualitt5bf99f12015-03-13 11:47:42 -0700274
bsalomon75398562015-08-17 12:55:38 -0700275 this->writePathVertices(target,
bsalomonb5238a72015-05-05 07:49:49 -0700276 atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800277 offset,
278 args.fColor,
bsalomonb5238a72015-05-05 07:49:49 -0700279 vertexStride,
280 this->viewMatrix(),
bsalomonee432412016-06-27 07:18:18 -0700281 shapeData);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700282 offset += kVerticesPerQuad * vertexStride;
bsalomonb5238a72015-05-05 07:49:49 -0700283 flushInfo.fInstancesToFlush++;
joshualitt5bf99f12015-03-13 11:47:42 -0700284 }
285
bsalomon75398562015-08-17 12:55:38 -0700286 this->flush(target, &flushInfo);
joshualitt5bf99f12015-03-13 11:47:42 -0700287 }
288
bsalomon75398562015-08-17 12:55:38 -0700289 bool addPathToAtlas(GrVertexBatch::Target* target,
bsalomonb5238a72015-05-05 07:49:49 -0700290 FlushInfo* flushInfo,
joshualitt5bf99f12015-03-13 11:47:42 -0700291 GrBatchAtlas* atlas,
bsalomonee432412016-06-27 07:18:18 -0700292 ShapeData* shapeData,
293 const GrShape& shape,
jvanverth512e4372015-11-23 11:50:02 -0800294 bool antiAlias,
joshualitt5bf99f12015-03-13 11:47:42 -0700295 uint32_t dimension,
joshualitt144c3c82015-11-30 12:30:13 -0800296 SkScalar scale) const {
bsalomonee432412016-06-27 07:18:18 -0700297 const SkRect& bounds = shape.bounds();
joshualitt5bf99f12015-03-13 11:47:42 -0700298
299 // generate bounding rect for bitmap draw
300 SkRect scaledBounds = bounds;
301 // scale to mip level size
302 scaledBounds.fLeft *= scale;
303 scaledBounds.fTop *= scale;
304 scaledBounds.fRight *= scale;
305 scaledBounds.fBottom *= scale;
306 // move the origin to an integer boundary (gives better results)
307 SkScalar dx = SkScalarFraction(scaledBounds.fLeft);
308 SkScalar dy = SkScalarFraction(scaledBounds.fTop);
309 scaledBounds.offset(-dx, -dy);
310 // get integer boundary
311 SkIRect devPathBounds;
312 scaledBounds.roundOut(&devPathBounds);
313 // pad to allow room for antialiasing
jvanverthecbed9d2015-12-18 10:07:52 -0800314 const int intPad = SkScalarCeilToInt(kAntiAliasPad);
315 // pre-move origin (after outset, will be 0,0)
316 int width = devPathBounds.width();
317 int height = devPathBounds.height();
318 devPathBounds.fLeft = intPad;
319 devPathBounds.fTop = intPad;
320 devPathBounds.fRight = intPad + width;
321 devPathBounds.fBottom = intPad + height;
322 devPathBounds.outset(intPad, intPad);
joshualitt5bf99f12015-03-13 11:47:42 -0700323
324 // draw path to bitmap
325 SkMatrix drawMatrix;
326 drawMatrix.setTranslate(-bounds.left(), -bounds.top());
327 drawMatrix.postScale(scale, scale);
328 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad);
329
robertphillips2a7cf5f2016-03-02 05:36:30 -0800330 // setup bitmap backing
jvanverth512e4372015-11-23 11:50:02 -0800331 SkASSERT(devPathBounds.fLeft == 0);
332 SkASSERT(devPathBounds.fTop == 0);
robertphillips2a7cf5f2016-03-02 05:36:30 -0800333 SkAutoPixmapStorage dst;
334 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
335 devPathBounds.height()))) {
336 return false;
337 }
338 sk_bzero(dst.writable_addr(), dst.getSafeSize());
joshualitt5bf99f12015-03-13 11:47:42 -0700339
robertphillips2a7cf5f2016-03-02 05:36:30 -0800340 // rasterize path
341 SkPaint paint;
342 paint.setStyle(SkPaint::kFill_Style);
343 paint.setAntiAlias(antiAlias);
344
345 SkDraw draw;
346 sk_bzero(&draw, sizeof(draw));
347
348 SkRasterClip rasterClip;
349 rasterClip.setRect(devPathBounds);
350 draw.fRC = &rasterClip;
robertphillips2a7cf5f2016-03-02 05:36:30 -0800351 draw.fMatrix = &drawMatrix;
352 draw.fDst = dst;
353
bsalomonee432412016-06-27 07:18:18 -0700354 SkPath path;
355 shape.asPath(&path);
robertphillips2a7cf5f2016-03-02 05:36:30 -0800356 draw.drawPathCoverage(path, paint);
357
358 // generate signed distance field
359 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
360 width = devPathBounds.width();
361 height = devPathBounds.height();
joshualitt5bf99f12015-03-13 11:47:42 -0700362 // TODO We should really generate this directly into the plot somehow
363 SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
364
robertphillips2a7cf5f2016-03-02 05:36:30 -0800365 // Generate signed distance field
366 SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
367 (const unsigned char*)dst.addr(),
368 dst.width(), dst.height(), dst.rowBytes());
joshualitt5bf99f12015-03-13 11:47:42 -0700369
370 // add to atlas
371 SkIPoint16 atlasLocation;
372 GrBatchAtlas::AtlasID id;
bsalomon6d6b6ad2016-07-13 14:45:28 -0700373 if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) {
bsalomon75398562015-08-17 12:55:38 -0700374 this->flush(target, flushInfo);
bsalomon6d6b6ad2016-07-13 14:45:28 -0700375 if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) {
376 return false;
377 }
joshualitt5bf99f12015-03-13 11:47:42 -0700378 }
379
380 // add to cache
bsalomonee432412016-06-27 07:18:18 -0700381 shapeData->fKey.set(shape, dimension);
382 shapeData->fScale = scale;
383 shapeData->fID = id;
joshualitt5bf99f12015-03-13 11:47:42 -0700384 // change the scaled rect to match the size of the inset distance field
385 scaledBounds.fRight = scaledBounds.fLeft +
386 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset);
387 scaledBounds.fBottom = scaledBounds.fTop +
388 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset);
389 // shift the origin to the correct place relative to the distance field
390 // need to also restore the fractional translation
391 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dx,
392 -SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dy);
bsalomonee432412016-06-27 07:18:18 -0700393 shapeData->fBounds = scaledBounds;
joshualitt5bf99f12015-03-13 11:47:42 -0700394 // origin we render from is inset from distance field edge
395 atlasLocation.fX += SK_DistanceFieldInset;
396 atlasLocation.fY += SK_DistanceFieldInset;
bsalomonee432412016-06-27 07:18:18 -0700397 shapeData->fAtlasLocation = atlasLocation;
joshualitt5bf99f12015-03-13 11:47:42 -0700398
bsalomonee432412016-06-27 07:18:18 -0700399 fShapeCache->add(shapeData);
400 fShapeList->addToTail(shapeData);
joshualitt5bf99f12015-03-13 11:47:42 -0700401#ifdef DF_PATH_TRACKING
402 ++g_NumCachedPaths;
403#endif
404 return true;
405 }
406
bsalomon75398562015-08-17 12:55:38 -0700407 void writePathVertices(GrDrawBatch::Target* target,
bsalomonb5238a72015-05-05 07:49:49 -0700408 GrBatchAtlas* atlas,
joshualitt53f26aa2015-12-10 07:29:54 -0800409 intptr_t offset,
410 GrColor color,
bsalomonb5238a72015-05-05 07:49:49 -0700411 size_t vertexStride,
412 const SkMatrix& viewMatrix,
bsalomonee432412016-06-27 07:18:18 -0700413 const ShapeData* shapeData) const {
joshualitt5bf99f12015-03-13 11:47:42 -0700414 GrTexture* texture = atlas->getTexture();
415
bsalomonee432412016-06-27 07:18:18 -0700416 SkScalar dx = shapeData->fBounds.fLeft;
417 SkScalar dy = shapeData->fBounds.fTop;
418 SkScalar width = shapeData->fBounds.width();
419 SkScalar height = shapeData->fBounds.height();
joshualitt5bf99f12015-03-13 11:47:42 -0700420
bsalomonee432412016-06-27 07:18:18 -0700421 SkScalar invScale = 1.0f / shapeData->fScale;
joshualitt5bf99f12015-03-13 11:47:42 -0700422 dx *= invScale;
423 dy *= invScale;
424 width *= invScale;
425 height *= invScale;
426
joshualitt53f26aa2015-12-10 07:29:54 -0800427 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
428
joshualitt5bf99f12015-03-13 11:47:42 -0700429 // vertex positions
430 // TODO make the vertex attributes a struct
431 SkRect r = SkRect::MakeXYWH(dx, dy, width, height);
432 positions->setRectFan(r.left(), r.top(), r.right(), r.bottom(), vertexStride);
433
joshualitt53f26aa2015-12-10 07:29:54 -0800434 // colors
435 for (int i = 0; i < kVerticesPerQuad; i++) {
436 GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride);
437 *colorPtr = color;
438 }
439
bsalomonee432412016-06-27 07:18:18 -0700440 const SkScalar tx = SkIntToScalar(shapeData->fAtlasLocation.fX);
441 const SkScalar ty = SkIntToScalar(shapeData->fAtlasLocation.fY);
benjaminwagner01e58382016-02-22 11:10:33 -0800442
joshualitt5bf99f12015-03-13 11:47:42 -0700443 // vertex texture coords
joshualitt53f26aa2015-12-10 07:29:54 -0800444 SkPoint* textureCoords = (SkPoint*)(offset + sizeof(SkPoint) + sizeof(GrColor));
benjaminwagner01e58382016-02-22 11:10:33 -0800445 textureCoords->setRectFan(tx / texture->width(),
446 ty / texture->height(),
bsalomonee432412016-06-27 07:18:18 -0700447 (tx + shapeData->fBounds.width()) / texture->width(),
448 (ty + shapeData->fBounds.height()) / texture->height(),
joshualitt5bf99f12015-03-13 11:47:42 -0700449 vertexStride);
450 }
451
joshualitt144c3c82015-11-30 12:30:13 -0800452 void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const {
bsalomon6d6b6ad2016-07-13 14:45:28 -0700453 if (flushInfo->fInstancesToFlush) {
454 GrMesh mesh;
455 int maxInstancesPerDraw =
456 static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
457 mesh.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer,
458 flushInfo->fIndexBuffer, flushInfo->fVertexOffset, kVerticesPerQuad,
459 kIndicesPerQuad, flushInfo->fInstancesToFlush, maxInstancesPerDraw);
460 target->draw(flushInfo->fGeometryProcessor.get(), mesh);
461 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
462 flushInfo->fInstancesToFlush = 0;
463 }
joshualitt5bf99f12015-03-13 11:47:42 -0700464 }
465
joshualitt53f26aa2015-12-10 07:29:54 -0800466 GrColor color() const { return fGeoData[0].fColor; }
joshualitt5bf99f12015-03-13 11:47:42 -0700467 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
468 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
469
bsalomoncb02b382015-08-12 11:14:50 -0700470 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700471 AADistanceFieldPathBatch* that = t->cast<AADistanceFieldPathBatch>();
472 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
473 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700474 return false;
475 }
476
joshualitt53f26aa2015-12-10 07:29:54 -0800477 // TODO We can position on the cpu
joshualitt5bf99f12015-03-13 11:47:42 -0700478 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
479 return false;
480 }
481
bsalomonee432412016-06-27 07:18:18 -0700482 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700483 this->joinBounds(*that);
joshualitt5bf99f12015-03-13 11:47:42 -0700484 return true;
485 }
486
487 struct BatchTracker {
joshualitt5bf99f12015-03-13 11:47:42 -0700488 SkMatrix fViewMatrix;
489 bool fUsesLocalCoords;
490 bool fColorIgnored;
491 bool fCoverageIgnored;
492 };
493
bsalomonf1703092016-06-29 18:41:53 -0700494 struct Geometry {
495 GrColor fColor;
496 GrShape fShape;
497 bool fAntiAlias;
498 };
499
joshualitt5bf99f12015-03-13 11:47:42 -0700500 BatchTracker fBatch;
bsalomonee432412016-06-27 07:18:18 -0700501 SkSTArray<1, Geometry> fGeoData;
joshualitt5bf99f12015-03-13 11:47:42 -0700502 GrBatchAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700503 ShapeCache* fShapeCache;
504 ShapeDataList* fShapeList;
brianosman0e3c5542016-04-13 13:56:21 -0700505 bool fGammaCorrect;
reed1b55a962015-09-17 20:16:13 -0700506
507 typedef GrVertexBatch INHERITED;
joshualitt5bf99f12015-03-13 11:47:42 -0700508};
509
bsalomon0aff2fa2015-07-31 06:48:27 -0700510bool GrAADistanceFieldPathRenderer::onDrawPath(const DrawPathArgs& args) {
robertphillips976f5f02016-06-03 10:59:20 -0700511 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800512 "GrAADistanceFieldPathRenderer::onDrawPath");
csmartdaltonecbc12b2016-06-08 10:08:43 -0700513 SkASSERT(!args.fDrawContext->isUnifiedMultisampled());
bsalomonee432412016-06-27 07:18:18 -0700514 SkASSERT(args.fShape->style().isSimpleFill());
csmartdaltonecbc12b2016-06-08 10:08:43 -0700515
jvanverthfa38a302014-10-06 05:59:05 -0700516 // we've already bailed on inverse filled paths, so this is safe
bsalomon8acedde2016-06-24 10:42:16 -0700517 SkASSERT(!args.fShape->isEmpty());
bsalomonee432412016-06-27 07:18:18 -0700518 SkASSERT(args.fShape->hasUnstyledKey());
joshualitt5bf99f12015-03-13 11:47:42 -0700519 if (!fAtlas) {
joshualittb356cbc2015-08-05 06:36:39 -0700520 fAtlas = args.fResourceProvider->createAtlas(kAlpha_8_GrPixelConfig,
521 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
522 NUM_PLOTS_X, NUM_PLOTS_Y,
523 &GrAADistanceFieldPathRenderer::HandleEviction,
524 (void*)this);
joshualitt21279c72015-05-11 07:21:37 -0700525 if (!fAtlas) {
jvanverthfa38a302014-10-06 05:59:05 -0700526 return false;
527 }
528 }
529
robertphillips3950f0d2016-07-07 07:33:13 -0700530 SkAutoTUnref<GrDrawBatch> batch(new AADistanceFieldPathBatch(args.fPaint->getColor(),
531 *args.fShape,
bsalomonf1703092016-06-29 18:41:53 -0700532 args.fAntiAlias, *args.fViewMatrix,
533 fAtlas, &fShapeCache, &fShapeList,
534 args.fGammaCorrect));
robertphillips976f5f02016-06-03 10:59:20 -0700535
bsalomonbb243832016-07-22 07:10:19 -0700536 GrPipelineBuilder pipelineBuilder(*args.fPaint);
537 pipelineBuilder.setUserStencil(args.fUserStencilSettings);
538
539 args.fDrawContext->drawBatch(pipelineBuilder, *args.fClip, batch);
joshualitt9491f7f2015-02-11 11:33:38 -0800540
jvanverthfa38a302014-10-06 05:59:05 -0700541 return true;
542}
543
joshualitt21279c72015-05-11 07:21:37 -0700544///////////////////////////////////////////////////////////////////////////////////////////////////
545
546#ifdef GR_TEST_UTILS
547
548struct PathTestStruct {
bsalomonee432412016-06-27 07:18:18 -0700549 typedef GrAADistanceFieldPathRenderer::ShapeCache ShapeCache;
550 typedef GrAADistanceFieldPathRenderer::ShapeData ShapeData;
551 typedef GrAADistanceFieldPathRenderer::ShapeDataList ShapeDataList;
halcanary96fcdcc2015-08-27 07:41:13 -0700552 PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
joshualitt21279c72015-05-11 07:21:37 -0700553 ~PathTestStruct() { this->reset(); }
554
555 void reset() {
bsalomonee432412016-06-27 07:18:18 -0700556 ShapeDataList::Iter iter;
557 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
558 ShapeData* shapeData;
559 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700560 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700561 fShapeList.remove(shapeData);
562 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700563 }
halcanary385fe4d2015-08-26 13:07:48 -0700564 delete fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700565 fShapeCache.reset();
joshualitt21279c72015-05-11 07:21:37 -0700566 }
567
568 static void HandleEviction(GrBatchAtlas::AtlasID id, void* pr) {
569 PathTestStruct* dfpr = (PathTestStruct*)pr;
570 // remove any paths that use this plot
bsalomonee432412016-06-27 07:18:18 -0700571 ShapeDataList::Iter iter;
572 iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
573 ShapeData* shapeData;
574 while ((shapeData = iter.get())) {
joshualitt21279c72015-05-11 07:21:37 -0700575 iter.next();
bsalomonee432412016-06-27 07:18:18 -0700576 if (id == shapeData->fID) {
577 dfpr->fShapeCache.remove(shapeData->fKey);
578 dfpr->fShapeList.remove(shapeData);
579 delete shapeData;
joshualitt21279c72015-05-11 07:21:37 -0700580 }
581 }
582 }
583
584 uint32_t fContextID;
585 GrBatchAtlas* fAtlas;
bsalomonee432412016-06-27 07:18:18 -0700586 ShapeCache fShapeCache;
587 ShapeDataList fShapeList;
joshualitt21279c72015-05-11 07:21:37 -0700588};
589
bsalomonabd30f52015-08-13 13:34:48 -0700590DRAW_BATCH_TEST_DEFINE(AADistanceFieldPathBatch) {
joshualitt21279c72015-05-11 07:21:37 -0700591 static PathTestStruct gTestStruct;
592
593 if (context->uniqueID() != gTestStruct.fContextID) {
594 gTestStruct.fContextID = context->uniqueID();
595 gTestStruct.reset();
joshualittb356cbc2015-08-05 06:36:39 -0700596 gTestStruct.fAtlas =
597 context->resourceProvider()->createAtlas(kAlpha_8_GrPixelConfig,
598 ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
599 NUM_PLOTS_X, NUM_PLOTS_Y,
600 &PathTestStruct::HandleEviction,
601 (void*)&gTestStruct);
joshualitt21279c72015-05-11 07:21:37 -0700602 }
603
604 SkMatrix viewMatrix = GrTest::TestMatrix(random);
605 GrColor color = GrRandomColor(random);
brianosman0e3c5542016-04-13 13:56:21 -0700606 bool gammaCorrect = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700607
bsalomonee432412016-06-27 07:18:18 -0700608 // This path renderer only allows fill styles.
609 GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
bsalomonf1703092016-06-29 18:41:53 -0700610 bool antiAlias = random->nextBool();
joshualitt21279c72015-05-11 07:21:37 -0700611
bsalomonf1703092016-06-29 18:41:53 -0700612 return new AADistanceFieldPathBatch(color,
613 shape,
614 antiAlias,
615 viewMatrix,
616 gTestStruct.fAtlas,
617 &gTestStruct.fShapeCache,
618 &gTestStruct.fShapeList,
619 gammaCorrect);
joshualitt21279c72015-05-11 07:21:37 -0700620}
621
622#endif