jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 11 | #include "GrBatchTest.h" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 12 | #include "GrBuffer.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 14 | #include "GrPipelineBuilder.h" |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 15 | #include "GrResourceProvider.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 16 | #include "GrSurfacePriv.h" |
| 17 | #include "GrSWMaskHelper.h" |
| 18 | #include "GrTexturePriv.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 19 | #include "batches/GrVertexBatch.h" |
jvanverth | 8ed3b9a | 2015-04-09 08:00:49 -0700 | [diff] [blame] | 20 | #include "effects/GrDistanceFieldGeoProc.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 21 | |
| 22 | #include "SkDistanceFieldGen.h" |
| 23 | #include "SkRTConf.h" |
| 24 | |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 25 | #define ATLAS_TEXTURE_WIDTH 2048 |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 26 | #define ATLAS_TEXTURE_HEIGHT 2048 |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 27 | #define PLOT_WIDTH 512 |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 28 | #define PLOT_HEIGHT 256 |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 29 | |
| 30 | #define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH) |
| 31 | #define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT) |
| 32 | |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 33 | #ifdef DF_PATH_TRACKING |
| 34 | static int g_NumCachedPaths = 0; |
| 35 | static int g_NumFreedPaths = 0; |
| 36 | #endif |
| 37 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 38 | // mip levels |
| 39 | static const int kSmallMIP = 32; |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 40 | static const int kMediumMIP = 73; |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 41 | static const int kLargeMIP = 162; |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 42 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 43 | // Callback to clear out internal path cache when eviction occurs |
| 44 | void GrAADistanceFieldPathRenderer::HandleEviction(GrBatchAtlas::AtlasID id, void* pr) { |
| 45 | GrAADistanceFieldPathRenderer* dfpr = (GrAADistanceFieldPathRenderer*)pr; |
| 46 | // remove any paths that use this plot |
| 47 | PathDataList::Iter iter; |
| 48 | iter.init(dfpr->fPathList, PathDataList::Iter::kHead_IterStart); |
| 49 | PathData* pathData; |
| 50 | while ((pathData = iter.get())) { |
| 51 | iter.next(); |
| 52 | if (id == pathData->fID) { |
| 53 | dfpr->fPathCache.remove(pathData->fKey); |
| 54 | dfpr->fPathList.remove(pathData); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 55 | delete pathData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 56 | #ifdef DF_PATH_TRACKING |
| 57 | ++g_NumFreedPaths; |
| 58 | #endif |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 63 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | GrAADistanceFieldPathRenderer::GrAADistanceFieldPathRenderer() : fAtlas(nullptr) {} |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 65 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 66 | GrAADistanceFieldPathRenderer::~GrAADistanceFieldPathRenderer() { |
| 67 | PathDataList::Iter iter; |
| 68 | iter.init(fPathList, PathDataList::Iter::kHead_IterStart); |
| 69 | PathData* pathData; |
| 70 | while ((pathData = iter.get())) { |
| 71 | iter.next(); |
| 72 | fPathList.remove(pathData); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 73 | delete pathData; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 74 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 75 | delete fAtlas; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 76 | |
| 77 | #ifdef DF_PATH_TRACKING |
| 78 | SkDebugf("Cached paths: %d, freed paths: %d\n", g_NumCachedPaths, g_NumFreedPaths); |
| 79 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 83 | bool GrAADistanceFieldPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
jvanverth | fb1e2fc | 2015-09-15 13:11:11 -0700 | [diff] [blame] | 84 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 85 | // TODO: Support inverse fill |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 86 | if (!args.fShaderCaps->shaderDerivativeSupport() || !args.fAntiAlias || |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 87 | SkStrokeRec::kHairline_Style == args.fStroke->getStyle() || |
bsalomon | 6266dca | 2016-03-11 06:22:00 -0800 | [diff] [blame] | 88 | args.fPath->isInverseFillType() || args.fPath->isVolatile() || |
| 89 | // We don't currently apply the dash or factor it into the DF key. (skbug.com/5082) |
| 90 | args.fStroke->isDashed()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 94 | // currently don't support perspective |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 95 | if (args.fViewMatrix->hasPerspective()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 99 | // only support paths with bounds within kMediumMIP by kMediumMIP, |
| 100 | // scaled to have bounds within 2.0f*kLargeMIP by 2.0f*kLargeMIP |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 101 | // the goal is to accelerate rendering of lots of small paths that may be scaling |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 102 | SkScalar maxScale = args.fViewMatrix->getMaxScale(); |
| 103 | const SkRect& bounds = args.fPath->getBounds(); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 104 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 105 | // Approximate stroked size by adding the maximum of the stroke width or 2x the miter limit |
| 106 | if (!args.fStroke->isFillStyle()) { |
| 107 | SkScalar extraWidth = args.fStroke->getWidth(); |
| 108 | if (SkPaint::kMiter_Join == args.fStroke->getJoin()) { |
| 109 | extraWidth = SkTMax(extraWidth, 2.0f*args.fStroke->getMiter()); |
| 110 | } |
| 111 | maxDim += extraWidth; |
| 112 | } |
bsalomon | 6266dca | 2016-03-11 06:22:00 -0800 | [diff] [blame] | 113 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 114 | return maxDim <= kMediumMIP && maxDim * maxScale <= 2.0f*kLargeMIP; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 115 | } |
| 116 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 117 | //////////////////////////////////////////////////////////////////////////////// |
| 118 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 119 | // padding around path bounds to allow for antialiased pixels |
| 120 | static const SkScalar kAntiAliasPad = 1.0f; |
| 121 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 122 | class AADistanceFieldPathBatch : public GrVertexBatch { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 123 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 124 | DEFINE_BATCH_CLASS_ID |
| 125 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 126 | typedef GrAADistanceFieldPathRenderer::PathData PathData; |
| 127 | typedef SkTDynamicHash<PathData, PathData::Key> PathCache; |
| 128 | typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; |
| 129 | |
| 130 | struct Geometry { |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 131 | Geometry(const SkStrokeRec& stroke) : fStroke(stroke) { |
| 132 | if (!stroke.needToApply()) { |
| 133 | // purify unused values to ensure binary equality |
| 134 | fStroke.setStrokeParams(SkPaint::kDefault_Cap, SkPaint::kDefault_Join, |
| 135 | SkIntToScalar(4)); |
| 136 | if (fStroke.getWidth() < 0) { |
| 137 | fStroke.setStrokeStyle(-1.0f); |
| 138 | } |
| 139 | } |
| 140 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 141 | SkPath fPath; |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 142 | // The unique ID of the path involved in this draw. This may be different than the ID |
| 143 | // in fPath since that path may have resulted from a SkStrokeRec::applyToPath call. |
| 144 | uint32_t fGenID; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 145 | SkStrokeRec fStroke; |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 146 | GrColor fColor; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 147 | bool fAntiAlias; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 150 | static GrDrawBatch* Create(const Geometry& geometry, const SkMatrix& viewMatrix, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 151 | GrBatchAtlas* atlas, PathCache* pathCache, PathDataList* pathList) { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 152 | return new AADistanceFieldPathBatch(geometry, viewMatrix, atlas, pathCache, pathList); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 153 | } |
| 154 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 155 | const char* name() const override { return "AADistanceFieldPathBatch"; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 156 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 157 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 158 | GrInitInvariantOutput* coverage, |
| 159 | GrBatchToXPOverrides* overrides) const override { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 160 | color->setKnownFourComponents(fGeoData[0].fColor); |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 161 | coverage->setUnknownSingleComponent(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 162 | } |
| 163 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 164 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 165 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 166 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 167 | if (!overrides.readsColor()) { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 168 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 169 | } |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 170 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 171 | |
| 172 | // setup batch properties |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 173 | fBatch.fColorIgnored = !overrides.readsColor(); |
| 174 | fBatch.fUsesLocalCoords = overrides.readsLocalCoords(); |
| 175 | fBatch.fCoverageIgnored = !overrides.readsCoverage(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 176 | } |
| 177 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 178 | struct FlushInfo { |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 179 | SkAutoTUnref<const GrBuffer> fVertexBuffer; |
| 180 | SkAutoTUnref<const GrBuffer> fIndexBuffer; |
| 181 | SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 182 | int fVertexOffset; |
| 183 | int fInstancesToFlush; |
| 184 | }; |
| 185 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 186 | void onPrepareDraws(Target* target) const override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 187 | int instanceCount = fGeoData.count(); |
| 188 | |
| 189 | SkMatrix invert; |
| 190 | if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) { |
| 191 | SkDebugf("Could not invert viewmatrix\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 195 | const SkMatrix& ctm = this->viewMatrix(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 196 | uint32_t flags = 0; |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 197 | flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 198 | flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 199 | |
| 200 | GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode); |
| 201 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 202 | FlushInfo flushInfo; |
| 203 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 204 | // Setup GrGeometryProcessor |
| 205 | GrBatchAtlas* atlas = fAtlas; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 206 | flushInfo.fGeometryProcessor.reset( |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 207 | GrDistanceFieldPathGeoProc::Create(this->color(), |
| 208 | this->viewMatrix(), |
| 209 | atlas->getTexture(), |
| 210 | params, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 211 | flags, |
| 212 | this->usesLocalCoords())); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 213 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 214 | // allocate vertices |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 215 | size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride(); |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 216 | SkASSERT(vertexStride == 2 * sizeof(SkPoint) + sizeof(GrColor)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 217 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 218 | const GrBuffer* vertexBuffer; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 219 | void* vertices = target->makeVertexSpace(vertexStride, |
| 220 | kVerticesPerQuad * instanceCount, |
| 221 | &vertexBuffer, |
| 222 | &flushInfo.fVertexOffset); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 223 | flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 224 | flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 225 | if (!vertices || !flushInfo.fIndexBuffer) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 226 | SkDebugf("Could not allocate vertices\n"); |
| 227 | return; |
| 228 | } |
| 229 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 230 | flushInfo.fInstancesToFlush = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 231 | for (int i = 0; i < instanceCount; i++) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 232 | const Geometry& args = fGeoData[i]; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 233 | |
| 234 | // get mip level |
| 235 | SkScalar maxScale = this->viewMatrix().getMaxScale(); |
| 236 | const SkRect& bounds = args.fPath.getBounds(); |
| 237 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
| 238 | SkScalar size = maxScale * maxDim; |
| 239 | uint32_t desiredDimension; |
| 240 | if (size <= kSmallMIP) { |
| 241 | desiredDimension = kSmallMIP; |
| 242 | } else if (size <= kMediumMIP) { |
| 243 | desiredDimension = kMediumMIP; |
| 244 | } else { |
| 245 | desiredDimension = kLargeMIP; |
| 246 | } |
| 247 | |
| 248 | // check to see if path is cached |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 249 | PathData::Key key(args.fGenID, desiredDimension, args.fStroke); |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 250 | PathData* pathData = fPathCache->find(key); |
| 251 | if (nullptr == pathData || !atlas->hasID(pathData->fID)) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 252 | // Remove the stale cache entry |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 253 | if (pathData) { |
| 254 | fPathCache->remove(pathData->fKey); |
| 255 | fPathList->remove(pathData); |
| 256 | delete pathData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 257 | } |
| 258 | SkScalar scale = desiredDimension/maxDim; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 259 | pathData = new PathData; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 260 | if (!this->addPathToAtlas(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 261 | &flushInfo, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 262 | atlas, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 263 | pathData, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 264 | args.fPath, |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 265 | args.fGenID, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 266 | args.fStroke, |
| 267 | args.fAntiAlias, |
| 268 | desiredDimension, |
| 269 | scale)) { |
| 270 | SkDebugf("Can't rasterize path\n"); |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 275 | atlas->setLastUseToken(pathData->fID, target->nextDrawToken()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 276 | |
| 277 | // Now set vertices |
| 278 | intptr_t offset = reinterpret_cast<intptr_t>(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 279 | offset += i * kVerticesPerQuad * vertexStride; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 280 | this->writePathVertices(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 281 | atlas, |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 282 | offset, |
| 283 | args.fColor, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 284 | vertexStride, |
| 285 | this->viewMatrix(), |
| 286 | args.fPath, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 287 | pathData); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 288 | flushInfo.fInstancesToFlush++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 289 | } |
| 290 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 291 | this->flush(target, &flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 295 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 296 | AADistanceFieldPathBatch(const Geometry& geometry, |
| 297 | const SkMatrix& viewMatrix, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 298 | GrBatchAtlas* atlas, |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 299 | PathCache* pathCache, PathDataList* pathList) |
| 300 | : INHERITED(ClassID()) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 301 | fBatch.fViewMatrix = viewMatrix; |
| 302 | fGeoData.push_back(geometry); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 303 | |
| 304 | fAtlas = atlas; |
| 305 | fPathCache = pathCache; |
| 306 | fPathList = pathList; |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 307 | |
| 308 | // Compute bounds |
| 309 | fBounds = geometry.fPath.getBounds(); |
| 310 | viewMatrix.mapRect(&fBounds); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 311 | } |
| 312 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 313 | bool addPathToAtlas(GrVertexBatch::Target* target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 314 | FlushInfo* flushInfo, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 315 | GrBatchAtlas* atlas, |
| 316 | PathData* pathData, |
| 317 | const SkPath& path, |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 318 | uint32_t genID, |
| 319 | const SkStrokeRec& stroke, |
| 320 | bool antiAlias, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 321 | uint32_t dimension, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 322 | SkScalar scale) const { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 323 | const SkRect& bounds = path.getBounds(); |
| 324 | |
| 325 | // generate bounding rect for bitmap draw |
| 326 | SkRect scaledBounds = bounds; |
| 327 | // scale to mip level size |
| 328 | scaledBounds.fLeft *= scale; |
| 329 | scaledBounds.fTop *= scale; |
| 330 | scaledBounds.fRight *= scale; |
| 331 | scaledBounds.fBottom *= scale; |
| 332 | // move the origin to an integer boundary (gives better results) |
| 333 | SkScalar dx = SkScalarFraction(scaledBounds.fLeft); |
| 334 | SkScalar dy = SkScalarFraction(scaledBounds.fTop); |
| 335 | scaledBounds.offset(-dx, -dy); |
| 336 | // get integer boundary |
| 337 | SkIRect devPathBounds; |
| 338 | scaledBounds.roundOut(&devPathBounds); |
| 339 | // pad to allow room for antialiasing |
jvanverth | ecbed9d | 2015-12-18 10:07:52 -0800 | [diff] [blame] | 340 | const int intPad = SkScalarCeilToInt(kAntiAliasPad); |
| 341 | // pre-move origin (after outset, will be 0,0) |
| 342 | int width = devPathBounds.width(); |
| 343 | int height = devPathBounds.height(); |
| 344 | devPathBounds.fLeft = intPad; |
| 345 | devPathBounds.fTop = intPad; |
| 346 | devPathBounds.fRight = intPad + width; |
| 347 | devPathBounds.fBottom = intPad + height; |
| 348 | devPathBounds.outset(intPad, intPad); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 349 | |
| 350 | // draw path to bitmap |
| 351 | SkMatrix drawMatrix; |
| 352 | drawMatrix.setTranslate(-bounds.left(), -bounds.top()); |
| 353 | drawMatrix.postScale(scale, scale); |
| 354 | drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad); |
| 355 | |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 356 | // setup bitmap backing |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 357 | SkASSERT(devPathBounds.fLeft == 0); |
| 358 | SkASSERT(devPathBounds.fTop == 0); |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 359 | SkAutoPixmapStorage dst; |
| 360 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), |
| 361 | devPathBounds.height()))) { |
| 362 | return false; |
| 363 | } |
| 364 | sk_bzero(dst.writable_addr(), dst.getSafeSize()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 365 | |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 366 | // rasterize path |
| 367 | SkPaint paint; |
| 368 | paint.setStyle(SkPaint::kFill_Style); |
| 369 | paint.setAntiAlias(antiAlias); |
| 370 | |
| 371 | SkDraw draw; |
| 372 | sk_bzero(&draw, sizeof(draw)); |
| 373 | |
| 374 | SkRasterClip rasterClip; |
| 375 | rasterClip.setRect(devPathBounds); |
| 376 | draw.fRC = &rasterClip; |
| 377 | draw.fClip = &rasterClip.bwRgn(); |
| 378 | draw.fMatrix = &drawMatrix; |
| 379 | draw.fDst = dst; |
| 380 | |
| 381 | draw.drawPathCoverage(path, paint); |
| 382 | |
| 383 | // generate signed distance field |
| 384 | devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 385 | width = devPathBounds.width(); |
| 386 | height = devPathBounds.height(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 387 | // TODO We should really generate this directly into the plot somehow |
| 388 | SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
| 389 | |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 390 | // Generate signed distance field |
| 391 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 392 | (const unsigned char*)dst.addr(), |
| 393 | dst.width(), dst.height(), dst.rowBytes()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 394 | |
| 395 | // add to atlas |
| 396 | SkIPoint16 atlasLocation; |
| 397 | GrBatchAtlas::AtlasID id; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 398 | bool success = atlas->addToAtlas(&id, target, width, height, dfStorage.get(), |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 399 | &atlasLocation); |
| 400 | if (!success) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 401 | this->flush(target, flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 402 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 403 | SkDEBUGCODE(success =) atlas->addToAtlas(&id, target, width, height, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 404 | dfStorage.get(), &atlasLocation); |
| 405 | SkASSERT(success); |
| 406 | |
| 407 | } |
| 408 | |
| 409 | // add to cache |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 410 | pathData->fKey = PathData::Key(genID, dimension, stroke); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 411 | pathData->fScale = scale; |
| 412 | pathData->fID = id; |
| 413 | // change the scaled rect to match the size of the inset distance field |
| 414 | scaledBounds.fRight = scaledBounds.fLeft + |
| 415 | SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset); |
| 416 | scaledBounds.fBottom = scaledBounds.fTop + |
| 417 | SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset); |
| 418 | // shift the origin to the correct place relative to the distance field |
| 419 | // need to also restore the fractional translation |
| 420 | scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dx, |
| 421 | -SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dy); |
| 422 | pathData->fBounds = scaledBounds; |
| 423 | // origin we render from is inset from distance field edge |
| 424 | atlasLocation.fX += SK_DistanceFieldInset; |
| 425 | atlasLocation.fY += SK_DistanceFieldInset; |
| 426 | pathData->fAtlasLocation = atlasLocation; |
| 427 | |
| 428 | fPathCache->add(pathData); |
| 429 | fPathList->addToTail(pathData); |
| 430 | #ifdef DF_PATH_TRACKING |
| 431 | ++g_NumCachedPaths; |
| 432 | #endif |
| 433 | return true; |
| 434 | } |
| 435 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 436 | void writePathVertices(GrDrawBatch::Target* target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 437 | GrBatchAtlas* atlas, |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 438 | intptr_t offset, |
| 439 | GrColor color, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 440 | size_t vertexStride, |
| 441 | const SkMatrix& viewMatrix, |
| 442 | const SkPath& path, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 443 | const PathData* pathData) const { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 444 | GrTexture* texture = atlas->getTexture(); |
| 445 | |
| 446 | SkScalar dx = pathData->fBounds.fLeft; |
| 447 | SkScalar dy = pathData->fBounds.fTop; |
| 448 | SkScalar width = pathData->fBounds.width(); |
| 449 | SkScalar height = pathData->fBounds.height(); |
| 450 | |
| 451 | SkScalar invScale = 1.0f / pathData->fScale; |
| 452 | dx *= invScale; |
| 453 | dy *= invScale; |
| 454 | width *= invScale; |
| 455 | height *= invScale; |
| 456 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 457 | SkPoint* positions = reinterpret_cast<SkPoint*>(offset); |
| 458 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 459 | // vertex positions |
| 460 | // TODO make the vertex attributes a struct |
| 461 | SkRect r = SkRect::MakeXYWH(dx, dy, width, height); |
| 462 | positions->setRectFan(r.left(), r.top(), r.right(), r.bottom(), vertexStride); |
| 463 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 464 | // colors |
| 465 | for (int i = 0; i < kVerticesPerQuad; i++) { |
| 466 | GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride); |
| 467 | *colorPtr = color; |
| 468 | } |
| 469 | |
benjaminwagner | 01e5838 | 2016-02-22 11:10:33 -0800 | [diff] [blame] | 470 | const SkScalar tx = SkIntToScalar(pathData->fAtlasLocation.fX); |
| 471 | const SkScalar ty = SkIntToScalar(pathData->fAtlasLocation.fY); |
| 472 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 473 | // vertex texture coords |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 474 | SkPoint* textureCoords = (SkPoint*)(offset + sizeof(SkPoint) + sizeof(GrColor)); |
benjaminwagner | 01e5838 | 2016-02-22 11:10:33 -0800 | [diff] [blame] | 475 | textureCoords->setRectFan(tx / texture->width(), |
| 476 | ty / texture->height(), |
| 477 | (tx + pathData->fBounds.width()) / texture->width(), |
| 478 | (ty + pathData->fBounds.height()) / texture->height(), |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 479 | vertexStride); |
| 480 | } |
| 481 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 482 | void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 483 | GrMesh mesh; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 484 | int maxInstancesPerDraw = |
| 485 | static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 486 | mesh.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 487 | flushInfo->fIndexBuffer, flushInfo->fVertexOffset, kVerticesPerQuad, |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 488 | kIndicesPerQuad, flushInfo->fInstancesToFlush, maxInstancesPerDraw); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 489 | target->draw(flushInfo->fGeometryProcessor, mesh); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 490 | flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush; |
| 491 | flushInfo->fInstancesToFlush = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 492 | } |
| 493 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 494 | GrColor color() const { return fGeoData[0].fColor; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 495 | const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
| 496 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 497 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 498 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 499 | AADistanceFieldPathBatch* that = t->cast<AADistanceFieldPathBatch>(); |
| 500 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 501 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 502 | return false; |
| 503 | } |
| 504 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 505 | // TODO We can position on the cpu |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 506 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 511 | this->joinBounds(that->bounds()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | |
| 515 | struct BatchTracker { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 516 | SkMatrix fViewMatrix; |
| 517 | bool fUsesLocalCoords; |
| 518 | bool fColorIgnored; |
| 519 | bool fCoverageIgnored; |
| 520 | }; |
| 521 | |
| 522 | BatchTracker fBatch; |
| 523 | SkSTArray<1, Geometry, true> fGeoData; |
| 524 | GrBatchAtlas* fAtlas; |
| 525 | PathCache* fPathCache; |
| 526 | PathDataList* fPathList; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 527 | |
| 528 | typedef GrVertexBatch INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 529 | }; |
| 530 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 531 | bool GrAADistanceFieldPathRenderer::onDrawPath(const DrawPathArgs& args) { |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 532 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(), |
| 533 | "GrAADistanceFieldPathRenderer::onDrawPath"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 534 | // we've already bailed on inverse filled paths, so this is safe |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 535 | if (args.fPath->isEmpty()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 539 | if (!fAtlas) { |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 540 | fAtlas = args.fResourceProvider->createAtlas(kAlpha_8_GrPixelConfig, |
| 541 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
| 542 | NUM_PLOTS_X, NUM_PLOTS_Y, |
| 543 | &GrAADistanceFieldPathRenderer::HandleEviction, |
| 544 | (void*)this); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 545 | if (!fAtlas) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | } |
| 549 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 550 | AADistanceFieldPathBatch::Geometry geometry(*args.fStroke); |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 551 | if (SkStrokeRec::kFill_Style == args.fStroke->getStyle()) { |
| 552 | geometry.fPath = *args.fPath; |
| 553 | } else { |
| 554 | args.fStroke->applyToPath(&geometry.fPath, *args.fPath); |
| 555 | } |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 556 | geometry.fColor = args.fColor; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 557 | geometry.fAntiAlias = args.fAntiAlias; |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 558 | // Note: this is the generation ID of the _original_ path. When a new path is |
| 559 | // generated due to stroking it is important that the original path's id is used |
| 560 | // for caching. |
| 561 | geometry.fGenID = args.fPath->getGenerationID(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 562 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 563 | SkAutoTUnref<GrDrawBatch> batch(AADistanceFieldPathBatch::Create(geometry, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 564 | *args.fViewMatrix, fAtlas, |
| 565 | &fPathCache, &fPathList)); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 566 | args.fTarget->drawBatch(*args.fPipelineBuilder, batch); |
joshualitt | 9491f7f | 2015-02-11 11:33:38 -0800 | [diff] [blame] | 567 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 568 | return true; |
| 569 | } |
| 570 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 571 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 572 | |
| 573 | #ifdef GR_TEST_UTILS |
| 574 | |
| 575 | struct PathTestStruct { |
| 576 | typedef GrAADistanceFieldPathRenderer::PathCache PathCache; |
| 577 | typedef GrAADistanceFieldPathRenderer::PathData PathData; |
| 578 | typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 579 | PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {} |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 580 | ~PathTestStruct() { this->reset(); } |
| 581 | |
| 582 | void reset() { |
| 583 | PathDataList::Iter iter; |
| 584 | iter.init(fPathList, PathDataList::Iter::kHead_IterStart); |
| 585 | PathData* pathData; |
| 586 | while ((pathData = iter.get())) { |
| 587 | iter.next(); |
| 588 | fPathList.remove(pathData); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 589 | delete pathData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 590 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 591 | delete fAtlas; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 592 | fPathCache.reset(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static void HandleEviction(GrBatchAtlas::AtlasID id, void* pr) { |
| 596 | PathTestStruct* dfpr = (PathTestStruct*)pr; |
| 597 | // remove any paths that use this plot |
| 598 | PathDataList::Iter iter; |
| 599 | iter.init(dfpr->fPathList, PathDataList::Iter::kHead_IterStart); |
| 600 | PathData* pathData; |
| 601 | while ((pathData = iter.get())) { |
| 602 | iter.next(); |
| 603 | if (id == pathData->fID) { |
| 604 | dfpr->fPathCache.remove(pathData->fKey); |
| 605 | dfpr->fPathList.remove(pathData); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 606 | delete pathData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | uint32_t fContextID; |
| 612 | GrBatchAtlas* fAtlas; |
| 613 | PathCache fPathCache; |
| 614 | PathDataList fPathList; |
| 615 | }; |
| 616 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 617 | DRAW_BATCH_TEST_DEFINE(AADistanceFieldPathBatch) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 618 | static PathTestStruct gTestStruct; |
| 619 | |
| 620 | if (context->uniqueID() != gTestStruct.fContextID) { |
| 621 | gTestStruct.fContextID = context->uniqueID(); |
| 622 | gTestStruct.reset(); |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 623 | gTestStruct.fAtlas = |
| 624 | context->resourceProvider()->createAtlas(kAlpha_8_GrPixelConfig, |
| 625 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
| 626 | NUM_PLOTS_X, NUM_PLOTS_Y, |
| 627 | &PathTestStruct::HandleEviction, |
| 628 | (void*)&gTestStruct); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 632 | GrColor color = GrRandomColor(random); |
| 633 | |
| 634 | AADistanceFieldPathBatch::Geometry geometry(GrTest::TestStrokeRec(random)); |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 635 | geometry.fColor = color; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 636 | geometry.fPath = GrTest::TestPath(random); |
| 637 | geometry.fAntiAlias = random->nextBool(); |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 638 | geometry.fGenID = random->nextU(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 639 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 640 | return AADistanceFieldPathBatch::Create(geometry, viewMatrix, |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 641 | gTestStruct.fAtlas, |
| 642 | &gTestStruct.fPathCache, |
| 643 | &gTestStruct.fPathList); |
| 644 | } |
| 645 | |
| 646 | #endif |