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