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 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 11 | #include "GrBatch.h" |
| 12 | #include "GrBatchTarget.h" |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame^] | 13 | #include "GrBatchTest.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 14 | #include "GrContext.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 15 | #include "GrPipelineBuilder.h" |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 16 | #include "GrResourceProvider.h" |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 17 | #include "GrSurfacePriv.h" |
| 18 | #include "GrSWMaskHelper.h" |
| 19 | #include "GrTexturePriv.h" |
bsalomon | 72e3ae4 | 2015-04-28 08:08:46 -0700 | [diff] [blame] | 20 | #include "GrVertexBuffer.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 | |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 26 | #define ATLAS_TEXTURE_WIDTH 1024 |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 27 | #define ATLAS_TEXTURE_HEIGHT 2048 |
reed | e4ef1ca | 2015-02-17 18:38:38 -0800 | [diff] [blame] | 28 | #define PLOT_WIDTH 256 |
| 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 | ada68ef | 2014-11-03 14:00:24 -0800 | [diff] [blame] | 41 | static const int kMediumMIP = 78; |
| 42 | static const int kLargeMIP = 192; |
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); |
| 56 | SkDELETE(pathData); |
| 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 | //////////////////////////////////////////////////////////////////////////////// |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 65 | GrAADistanceFieldPathRenderer::GrAADistanceFieldPathRenderer(GrContext* context) |
| 66 | : fContext(context) |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 67 | , fAtlas(NULL) { |
jvanverth | 6d22eca | 2014-10-28 11:10:48 -0700 | [diff] [blame] | 68 | } |
| 69 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 70 | GrAADistanceFieldPathRenderer::~GrAADistanceFieldPathRenderer() { |
| 71 | PathDataList::Iter iter; |
| 72 | iter.init(fPathList, PathDataList::Iter::kHead_IterStart); |
| 73 | PathData* pathData; |
| 74 | while ((pathData = iter.get())) { |
| 75 | iter.next(); |
| 76 | fPathList.remove(pathData); |
| 77 | SkDELETE(pathData); |
| 78 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 79 | SkDELETE(fAtlas); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 80 | |
| 81 | #ifdef DF_PATH_TRACKING |
| 82 | SkDebugf("Cached paths: %d, freed paths: %d\n", g_NumCachedPaths, g_NumFreedPaths); |
| 83 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////////////// |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 87 | bool GrAADistanceFieldPathRenderer::canDrawPath(const GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 88 | const GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 89 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 90 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 91 | const GrStrokeInfo& stroke, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 92 | bool antiAlias) const { |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 93 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 94 | // TODO: Support inverse fill |
| 95 | // TODO: Support strokes |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 96 | if (!target->caps()->shaderCaps()->shaderDerivativeSupport() || !antiAlias |
| 97 | || path.isInverseFillType() || path.isVolatile() || !stroke.isFillStyle()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 101 | // currently don't support perspective |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 102 | if (viewMatrix.hasPerspective()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 106 | // only support paths smaller than 64x64, scaled to less than 256x256 |
| 107 | // the goal is to accelerate rendering of lots of small paths that may be scaling |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 108 | SkScalar maxScale = viewMatrix.getMaxScale(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 109 | const SkRect& bounds = path.getBounds(); |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 110 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 111 | return maxDim < 64.f && maxDim * maxScale < 256.f; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 115 | GrPathRenderer::StencilSupport |
| 116 | GrAADistanceFieldPathRenderer::onGetStencilSupport(const GrDrawTarget*, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 117 | const GrPipelineBuilder*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 118 | const SkPath&, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 119 | const GrStrokeInfo&) const { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 120 | return GrPathRenderer::kNoSupport_StencilSupport; |
| 121 | } |
| 122 | |
| 123 | //////////////////////////////////////////////////////////////////////////////// |
| 124 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 125 | // padding around path bounds to allow for antialiased pixels |
| 126 | static const SkScalar kAntiAliasPad = 1.0f; |
| 127 | |
| 128 | class AADistanceFieldPathBatch : public GrBatch { |
| 129 | public: |
| 130 | typedef GrAADistanceFieldPathRenderer::PathData PathData; |
| 131 | typedef SkTDynamicHash<PathData, PathData::Key> PathCache; |
| 132 | typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; |
| 133 | |
| 134 | struct Geometry { |
| 135 | Geometry(const SkStrokeRec& stroke) : fStroke(stroke) {} |
| 136 | SkPath fPath; |
| 137 | SkStrokeRec fStroke; |
| 138 | bool fAntiAlias; |
| 139 | PathData* fPathData; |
| 140 | }; |
| 141 | |
| 142 | static GrBatch* Create(const Geometry& geometry, GrColor color, const SkMatrix& viewMatrix, |
| 143 | GrBatchAtlas* atlas, PathCache* pathCache, PathDataList* pathList) { |
| 144 | return SkNEW_ARGS(AADistanceFieldPathBatch, (geometry, color, viewMatrix, |
| 145 | atlas, pathCache, pathList)); |
| 146 | } |
| 147 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 148 | const char* name() const override { return "AADistanceFieldPathBatch"; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 149 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 150 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 151 | out->setKnownFourComponents(fBatch.fColor); |
| 152 | } |
| 153 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 154 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 155 | out->setUnknownSingleComponent(); |
| 156 | } |
| 157 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 158 | void initBatchTracker(const GrPipelineInfo& init) override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 159 | // Handle any color overrides |
| 160 | if (init.fColorIgnored) { |
| 161 | fBatch.fColor = GrColor_ILLEGAL; |
| 162 | } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 163 | fBatch.fColor = init.fOverrideColor; |
| 164 | } |
| 165 | |
| 166 | // setup batch properties |
| 167 | fBatch.fColorIgnored = init.fColorIgnored; |
| 168 | fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 169 | fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 170 | } |
| 171 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 172 | struct FlushInfo { |
| 173 | SkAutoTUnref<const GrVertexBuffer> fVertexBuffer; |
| 174 | SkAutoTUnref<const GrIndexBuffer> fIndexBuffer; |
| 175 | int fVertexOffset; |
| 176 | int fInstancesToFlush; |
| 177 | }; |
| 178 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 179 | void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 180 | int instanceCount = fGeoData.count(); |
| 181 | |
| 182 | SkMatrix invert; |
| 183 | if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) { |
| 184 | SkDebugf("Could not invert viewmatrix\n"); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | uint32_t flags = 0; |
| 189 | flags |= this->viewMatrix().isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
| 190 | |
| 191 | GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode); |
| 192 | |
| 193 | // Setup GrGeometryProcessor |
| 194 | GrBatchAtlas* atlas = fAtlas; |
| 195 | SkAutoTUnref<GrGeometryProcessor> dfProcessor( |
jvanverth | 502286d | 2015-04-08 12:37:51 -0700 | [diff] [blame] | 196 | GrDistanceFieldPathGeoProc::Create(this->color(), |
| 197 | this->viewMatrix(), |
| 198 | atlas->getTexture(), |
| 199 | params, |
joshualitt | 50cb76b | 2015-04-28 09:17:05 -0700 | [diff] [blame] | 200 | flags, |
| 201 | false)); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 202 | |
| 203 | this->initDraw(batchTarget, dfProcessor, pipeline); |
| 204 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 205 | FlushInfo flushInfo; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 206 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 207 | // allocate vertices |
| 208 | size_t vertexStride = dfProcessor->getVertexStride(); |
| 209 | SkASSERT(vertexStride == 2 * sizeof(SkPoint)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 210 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 211 | const GrVertexBuffer* vertexBuffer; |
robertphillips | e40d397 | 2015-05-07 09:51:43 -0700 | [diff] [blame] | 212 | void* vertices = batchTarget->makeVertSpace(vertexStride, |
| 213 | kVerticesPerQuad * instanceCount, |
| 214 | &vertexBuffer, |
| 215 | &flushInfo.fVertexOffset); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 216 | flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
| 217 | flushInfo.fIndexBuffer.reset(batchTarget->resourceProvider()->refQuadIndexBuffer()); |
| 218 | if (!vertices || !flushInfo.fIndexBuffer) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 219 | SkDebugf("Could not allocate vertices\n"); |
| 220 | return; |
| 221 | } |
| 222 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 223 | flushInfo.fInstancesToFlush = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 224 | for (int i = 0; i < instanceCount; i++) { |
| 225 | Geometry& args = fGeoData[i]; |
| 226 | |
| 227 | // get mip level |
| 228 | SkScalar maxScale = this->viewMatrix().getMaxScale(); |
| 229 | const SkRect& bounds = args.fPath.getBounds(); |
| 230 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
| 231 | SkScalar size = maxScale * maxDim; |
| 232 | uint32_t desiredDimension; |
| 233 | if (size <= kSmallMIP) { |
| 234 | desiredDimension = kSmallMIP; |
| 235 | } else if (size <= kMediumMIP) { |
| 236 | desiredDimension = kMediumMIP; |
| 237 | } else { |
| 238 | desiredDimension = kLargeMIP; |
| 239 | } |
| 240 | |
| 241 | // check to see if path is cached |
| 242 | // TODO: handle stroked vs. filled version of same path |
| 243 | PathData::Key key = { args.fPath.getGenerationID(), desiredDimension }; |
| 244 | args.fPathData = fPathCache->find(key); |
| 245 | if (NULL == args.fPathData || !atlas->hasID(args.fPathData->fID)) { |
| 246 | // Remove the stale cache entry |
| 247 | if (args.fPathData) { |
| 248 | fPathCache->remove(args.fPathData->fKey); |
| 249 | fPathList->remove(args.fPathData); |
| 250 | SkDELETE(args.fPathData); |
| 251 | } |
| 252 | SkScalar scale = desiredDimension/maxDim; |
| 253 | args.fPathData = SkNEW(PathData); |
| 254 | if (!this->addPathToAtlas(batchTarget, |
| 255 | dfProcessor, |
| 256 | pipeline, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 257 | &flushInfo, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 258 | atlas, |
| 259 | args.fPathData, |
| 260 | args.fPath, |
| 261 | args.fStroke, |
| 262 | args.fAntiAlias, |
| 263 | desiredDimension, |
| 264 | scale)) { |
| 265 | SkDebugf("Can't rasterize path\n"); |
| 266 | return; |
| 267 | } |
| 268 | } |
| 269 | |
joshualitt | b4c507e | 2015-04-08 08:07:59 -0700 | [diff] [blame] | 270 | atlas->setLastUseToken(args.fPathData->fID, batchTarget->currentToken()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 271 | |
| 272 | // Now set vertices |
| 273 | intptr_t offset = reinterpret_cast<intptr_t>(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 274 | offset += i * kVerticesPerQuad * vertexStride; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 275 | SkPoint* positions = reinterpret_cast<SkPoint*>(offset); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 276 | this->writePathVertices(batchTarget, |
| 277 | atlas, |
| 278 | pipeline, |
| 279 | dfProcessor, |
| 280 | positions, |
| 281 | vertexStride, |
| 282 | this->viewMatrix(), |
| 283 | args.fPath, |
| 284 | args.fPathData); |
| 285 | flushInfo.fInstancesToFlush++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 286 | } |
| 287 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 288 | this->flush(batchTarget, &flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 292 | |
| 293 | private: |
| 294 | AADistanceFieldPathBatch(const Geometry& geometry, GrColor color, const SkMatrix& viewMatrix, |
| 295 | GrBatchAtlas* atlas, |
| 296 | PathCache* pathCache, PathDataList* pathList) { |
| 297 | this->initClassID<AADistanceFieldPathBatch>(); |
| 298 | fBatch.fColor = color; |
| 299 | fBatch.fViewMatrix = viewMatrix; |
| 300 | fGeoData.push_back(geometry); |
| 301 | fGeoData.back().fPathData = NULL; |
| 302 | |
| 303 | fAtlas = atlas; |
| 304 | fPathCache = pathCache; |
| 305 | fPathList = pathList; |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 306 | |
| 307 | // Compute bounds |
| 308 | fBounds = geometry.fPath.getBounds(); |
| 309 | viewMatrix.mapRect(&fBounds); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | bool addPathToAtlas(GrBatchTarget* batchTarget, |
| 313 | const GrGeometryProcessor* dfProcessor, |
| 314 | const GrPipeline* pipeline, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 315 | FlushInfo* flushInfo, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 316 | GrBatchAtlas* atlas, |
| 317 | PathData* pathData, |
| 318 | const SkPath& path, |
| 319 | const SkStrokeRec& |
| 320 | stroke, bool antiAlias, |
| 321 | uint32_t dimension, |
| 322 | SkScalar scale) { |
| 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 |
| 340 | devPathBounds.outset(SkScalarCeilToInt(kAntiAliasPad), SkScalarCeilToInt(kAntiAliasPad)); |
| 341 | // move origin to upper left corner |
| 342 | devPathBounds.offsetTo(0,0); |
| 343 | |
| 344 | // draw path to bitmap |
| 345 | SkMatrix drawMatrix; |
| 346 | drawMatrix.setTranslate(-bounds.left(), -bounds.top()); |
| 347 | drawMatrix.postScale(scale, scale); |
| 348 | drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad); |
| 349 | |
| 350 | // setup bitmap backing |
| 351 | // Now translate so the bound's UL corner is at the origin |
| 352 | drawMatrix.postTranslate(-devPathBounds.fLeft * SK_Scalar1, |
| 353 | -devPathBounds.fTop * SK_Scalar1); |
| 354 | SkIRect pathBounds = SkIRect::MakeWH(devPathBounds.width(), |
| 355 | devPathBounds.height()); |
| 356 | |
| 357 | SkBitmap bmp; |
| 358 | const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(pathBounds.fRight, |
| 359 | pathBounds.fBottom); |
| 360 | if (!bmp.tryAllocPixels(bmImageInfo)) { |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | sk_bzero(bmp.getPixels(), bmp.getSafeSize()); |
| 365 | |
| 366 | // rasterize path |
| 367 | SkPaint paint; |
| 368 | if (stroke.isHairlineStyle()) { |
| 369 | paint.setStyle(SkPaint::kStroke_Style); |
| 370 | paint.setStrokeWidth(SK_Scalar1); |
| 371 | } else { |
| 372 | if (stroke.isFillStyle()) { |
| 373 | paint.setStyle(SkPaint::kFill_Style); |
| 374 | } else { |
| 375 | paint.setStyle(SkPaint::kStroke_Style); |
| 376 | paint.setStrokeJoin(stroke.getJoin()); |
| 377 | paint.setStrokeCap(stroke.getCap()); |
| 378 | paint.setStrokeWidth(stroke.getWidth()); |
| 379 | } |
| 380 | } |
| 381 | paint.setAntiAlias(antiAlias); |
| 382 | |
| 383 | SkDraw draw; |
| 384 | sk_bzero(&draw, sizeof(draw)); |
| 385 | |
| 386 | SkRasterClip rasterClip; |
| 387 | rasterClip.setRect(pathBounds); |
| 388 | draw.fRC = &rasterClip; |
| 389 | draw.fClip = &rasterClip.bwRgn(); |
| 390 | draw.fMatrix = &drawMatrix; |
| 391 | draw.fBitmap = &bmp; |
| 392 | |
| 393 | draw.drawPathCoverage(path, paint); |
| 394 | |
| 395 | // generate signed distance field |
| 396 | devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 397 | int width = devPathBounds.width(); |
| 398 | int height = devPathBounds.height(); |
| 399 | // TODO We should really generate this directly into the plot somehow |
| 400 | SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
| 401 | |
| 402 | // Generate signed distance field |
| 403 | { |
| 404 | SkAutoLockPixels alp(bmp); |
| 405 | |
| 406 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 407 | (const unsigned char*)bmp.getPixels(), |
| 408 | bmp.width(), bmp.height(), bmp.rowBytes()); |
| 409 | } |
| 410 | |
| 411 | // add to atlas |
| 412 | SkIPoint16 atlasLocation; |
| 413 | GrBatchAtlas::AtlasID id; |
| 414 | bool success = atlas->addToAtlas(&id, batchTarget, width, height, dfStorage.get(), |
| 415 | &atlasLocation); |
| 416 | if (!success) { |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 417 | this->flush(batchTarget, flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 418 | this->initDraw(batchTarget, dfProcessor, pipeline); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 419 | |
| 420 | SkDEBUGCODE(success =) atlas->addToAtlas(&id, batchTarget, width, height, |
| 421 | dfStorage.get(), &atlasLocation); |
| 422 | SkASSERT(success); |
| 423 | |
| 424 | } |
| 425 | |
| 426 | // add to cache |
| 427 | pathData->fKey.fGenID = path.getGenerationID(); |
| 428 | pathData->fKey.fDimension = dimension; |
| 429 | pathData->fScale = scale; |
| 430 | pathData->fID = id; |
| 431 | // change the scaled rect to match the size of the inset distance field |
| 432 | scaledBounds.fRight = scaledBounds.fLeft + |
| 433 | SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset); |
| 434 | scaledBounds.fBottom = scaledBounds.fTop + |
| 435 | SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset); |
| 436 | // shift the origin to the correct place relative to the distance field |
| 437 | // need to also restore the fractional translation |
| 438 | scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dx, |
| 439 | -SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dy); |
| 440 | pathData->fBounds = scaledBounds; |
| 441 | // origin we render from is inset from distance field edge |
| 442 | atlasLocation.fX += SK_DistanceFieldInset; |
| 443 | atlasLocation.fY += SK_DistanceFieldInset; |
| 444 | pathData->fAtlasLocation = atlasLocation; |
| 445 | |
| 446 | fPathCache->add(pathData); |
| 447 | fPathList->addToTail(pathData); |
| 448 | #ifdef DF_PATH_TRACKING |
| 449 | ++g_NumCachedPaths; |
| 450 | #endif |
| 451 | return true; |
| 452 | } |
| 453 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 454 | void writePathVertices(GrBatchTarget* target, |
| 455 | GrBatchAtlas* atlas, |
| 456 | const GrPipeline* pipeline, |
| 457 | const GrGeometryProcessor* gp, |
| 458 | SkPoint* positions, |
| 459 | size_t vertexStride, |
| 460 | const SkMatrix& viewMatrix, |
| 461 | const SkPath& path, |
| 462 | const PathData* pathData) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 463 | GrTexture* texture = atlas->getTexture(); |
| 464 | |
| 465 | SkScalar dx = pathData->fBounds.fLeft; |
| 466 | SkScalar dy = pathData->fBounds.fTop; |
| 467 | SkScalar width = pathData->fBounds.width(); |
| 468 | SkScalar height = pathData->fBounds.height(); |
| 469 | |
| 470 | SkScalar invScale = 1.0f / pathData->fScale; |
| 471 | dx *= invScale; |
| 472 | dy *= invScale; |
| 473 | width *= invScale; |
| 474 | height *= invScale; |
| 475 | |
| 476 | SkFixed tx = SkIntToFixed(pathData->fAtlasLocation.fX); |
| 477 | SkFixed ty = SkIntToFixed(pathData->fAtlasLocation.fY); |
| 478 | SkFixed tw = SkScalarToFixed(pathData->fBounds.width()); |
| 479 | SkFixed th = SkScalarToFixed(pathData->fBounds.height()); |
| 480 | |
| 481 | // vertex positions |
| 482 | // TODO make the vertex attributes a struct |
| 483 | SkRect r = SkRect::MakeXYWH(dx, dy, width, height); |
| 484 | positions->setRectFan(r.left(), r.top(), r.right(), r.bottom(), vertexStride); |
| 485 | |
| 486 | // vertex texture coords |
| 487 | SkPoint* textureCoords = positions + 1; |
| 488 | textureCoords->setRectFan(SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx)), |
| 489 | SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty)), |
| 490 | SkFixedToFloat(texture->texturePriv().normalizeFixedX(tx + tw)), |
| 491 | SkFixedToFloat(texture->texturePriv().normalizeFixedY(ty + th)), |
| 492 | vertexStride); |
| 493 | } |
| 494 | |
| 495 | void initDraw(GrBatchTarget* batchTarget, |
| 496 | const GrGeometryProcessor* dfProcessor, |
| 497 | const GrPipeline* pipeline) { |
| 498 | batchTarget->initDraw(dfProcessor, pipeline); |
| 499 | |
| 500 | // TODO remove this when batch is everywhere |
| 501 | GrPipelineInfo init; |
| 502 | init.fColorIgnored = fBatch.fColorIgnored; |
| 503 | init.fOverrideColor = GrColor_ILLEGAL; |
| 504 | init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 505 | init.fUsesLocalCoords = this->usesLocalCoords(); |
| 506 | dfProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 507 | } |
| 508 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 509 | void flush(GrBatchTarget* batchTarget, FlushInfo* flushInfo) { |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 510 | GrVertices vertices; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 511 | int maxInstancesPerDraw = flushInfo->fIndexBuffer->maxQuads(); |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 512 | vertices.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 513 | flushInfo->fIndexBuffer, flushInfo->fVertexOffset, kVerticesPerQuad, |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 514 | kIndicesPerQuad, flushInfo->fInstancesToFlush, maxInstancesPerDraw); |
| 515 | batchTarget->draw(vertices); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 516 | flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush; |
| 517 | flushInfo->fInstancesToFlush = 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | GrColor color() const { return fBatch.fColor; } |
| 521 | const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
| 522 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 523 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 524 | bool onCombineIfPossible(GrBatch* t) override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 525 | AADistanceFieldPathBatch* that = t->cast<AADistanceFieldPathBatch>(); |
| 526 | |
| 527 | // TODO we could actually probably do a bunch of this work on the CPU, ie map viewMatrix, |
| 528 | // maybe upload color via attribute |
| 529 | if (this->color() != that->color()) { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 538 | this->joinBounds(that->bounds()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 539 | return true; |
| 540 | } |
| 541 | |
| 542 | struct BatchTracker { |
| 543 | GrColor fColor; |
| 544 | SkMatrix fViewMatrix; |
| 545 | bool fUsesLocalCoords; |
| 546 | bool fColorIgnored; |
| 547 | bool fCoverageIgnored; |
| 548 | }; |
| 549 | |
| 550 | BatchTracker fBatch; |
| 551 | SkSTArray<1, Geometry, true> fGeoData; |
| 552 | GrBatchAtlas* fAtlas; |
| 553 | PathCache* fPathCache; |
| 554 | PathDataList* fPathList; |
| 555 | }; |
| 556 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame^] | 557 | static GrBatchAtlas* create_atlas(GrContext* context, GrBatchAtlas::EvictionFunc func, void* data) { |
| 558 | GrBatchAtlas* atlas; |
| 559 | // Create a new atlas |
| 560 | GrSurfaceDesc desc; |
| 561 | desc.fFlags = kNone_GrSurfaceFlags; |
| 562 | desc.fWidth = ATLAS_TEXTURE_WIDTH; |
| 563 | desc.fHeight = ATLAS_TEXTURE_HEIGHT; |
| 564 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 565 | |
| 566 | // We don't want to flush the context so we claim we're in the middle of flushing so as to |
| 567 | // guarantee we do not recieve a texture with pending IO |
| 568 | GrTexture* texture = context->textureProvider()->refScratchTexture( |
| 569 | desc, GrTextureProvider::kApprox_ScratchTexMatch, true); |
| 570 | if (texture) { |
| 571 | atlas = SkNEW_ARGS(GrBatchAtlas, (texture, NUM_PLOTS_X, NUM_PLOTS_Y)); |
| 572 | } else { |
| 573 | return NULL; |
| 574 | } |
| 575 | atlas->registerEvictionCallback(func, data); |
| 576 | return atlas; |
| 577 | } |
| 578 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 579 | bool GrAADistanceFieldPathRenderer::onDrawPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 580 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 581 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 582 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 583 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 584 | const GrStrokeInfo& stroke, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 585 | bool antiAlias) { |
| 586 | // we've already bailed on inverse filled paths, so this is safe |
| 587 | if (path.isEmpty()) { |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | SkASSERT(fContext); |
| 592 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 593 | if (!fAtlas) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame^] | 594 | fAtlas = create_atlas(fContext, &GrAADistanceFieldPathRenderer::HandleEviction, |
| 595 | (void*)this); |
| 596 | if (!fAtlas) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 597 | return false; |
| 598 | } |
| 599 | } |
| 600 | |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 601 | AADistanceFieldPathBatch::Geometry geometry(stroke.getStrokeRec()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 602 | geometry.fPath = path; |
| 603 | geometry.fAntiAlias = antiAlias; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 604 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 605 | SkAutoTUnref<GrBatch> batch(AADistanceFieldPathBatch::Create(geometry, color, viewMatrix, |
| 606 | fAtlas, &fPathCache, &fPathList)); |
joshualitt | 99c7c07 | 2015-05-01 13:43:30 -0700 | [diff] [blame] | 607 | target->drawBatch(pipelineBuilder, batch); |
joshualitt | 9491f7f | 2015-02-11 11:33:38 -0800 | [diff] [blame] | 608 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 609 | return true; |
| 610 | } |
| 611 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame^] | 612 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 613 | |
| 614 | #ifdef GR_TEST_UTILS |
| 615 | |
| 616 | struct PathTestStruct { |
| 617 | typedef GrAADistanceFieldPathRenderer::PathCache PathCache; |
| 618 | typedef GrAADistanceFieldPathRenderer::PathData PathData; |
| 619 | typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; |
| 620 | PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(NULL) {} |
| 621 | ~PathTestStruct() { this->reset(); } |
| 622 | |
| 623 | void reset() { |
| 624 | PathDataList::Iter iter; |
| 625 | iter.init(fPathList, PathDataList::Iter::kHead_IterStart); |
| 626 | PathData* pathData; |
| 627 | while ((pathData = iter.get())) { |
| 628 | iter.next(); |
| 629 | fPathList.remove(pathData); |
| 630 | SkDELETE(pathData); |
| 631 | } |
| 632 | SkDELETE(fAtlas); |
| 633 | } |
| 634 | |
| 635 | static void HandleEviction(GrBatchAtlas::AtlasID id, void* pr) { |
| 636 | PathTestStruct* dfpr = (PathTestStruct*)pr; |
| 637 | // remove any paths that use this plot |
| 638 | PathDataList::Iter iter; |
| 639 | iter.init(dfpr->fPathList, PathDataList::Iter::kHead_IterStart); |
| 640 | PathData* pathData; |
| 641 | while ((pathData = iter.get())) { |
| 642 | iter.next(); |
| 643 | if (id == pathData->fID) { |
| 644 | dfpr->fPathCache.remove(pathData->fKey); |
| 645 | dfpr->fPathList.remove(pathData); |
| 646 | SkDELETE(pathData); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | uint32_t fContextID; |
| 652 | GrBatchAtlas* fAtlas; |
| 653 | PathCache fPathCache; |
| 654 | PathDataList fPathList; |
| 655 | }; |
| 656 | |
| 657 | BATCH_TEST_DEFINE(AADistanceFieldPathRenderer) { |
| 658 | static PathTestStruct gTestStruct; |
| 659 | |
| 660 | if (context->uniqueID() != gTestStruct.fContextID) { |
| 661 | gTestStruct.fContextID = context->uniqueID(); |
| 662 | gTestStruct.reset(); |
| 663 | gTestStruct.fAtlas = create_atlas(context, &PathTestStruct::HandleEviction, |
| 664 | (void*)&gTestStruct); |
| 665 | } |
| 666 | |
| 667 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 668 | GrColor color = GrRandomColor(random); |
| 669 | |
| 670 | AADistanceFieldPathBatch::Geometry geometry(GrTest::TestStrokeRec(random)); |
| 671 | geometry.fPath = GrTest::TestPath(random); |
| 672 | geometry.fAntiAlias = random->nextBool(); |
| 673 | |
| 674 | return AADistanceFieldPathBatch::Create(geometry, color, viewMatrix, |
| 675 | gTestStruct.fAtlas, |
| 676 | &gTestStruct.fPathCache, |
| 677 | &gTestStruct.fPathList); |
| 678 | } |
| 679 | |
| 680 | #endif |