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" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [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 |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 34 | static int g_NumCachedShapes = 0; |
| 35 | static int g_NumFreedShapes = 0; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 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 |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 47 | ShapeDataList::Iter iter; |
| 48 | iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 49 | ShapeData* shapeData; |
| 50 | while ((shapeData = iter.get())) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 51 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 52 | if (id == shapeData->fID) { |
| 53 | dfpr->fShapeCache.remove(shapeData->fKey); |
| 54 | dfpr->fShapeList.remove(shapeData); |
| 55 | delete shapeData; |
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() { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 67 | ShapeDataList::Iter iter; |
| 68 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 69 | ShapeData* shapeData; |
| 70 | while ((shapeData = iter.get())) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 71 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 72 | delete shapeData; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 73 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 74 | delete fAtlas; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 75 | |
| 76 | #ifdef DF_PATH_TRACKING |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 77 | SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 78 | #endif |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 82 | bool GrAADistanceFieldPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 83 | if (!args.fShaderCaps->shaderDerivativeSupport()) { |
| 84 | return false; |
| 85 | } |
| 86 | // If the shape has no key then we won't get any reuse. |
| 87 | if (!args.fShape->hasUnstyledKey()) { |
| 88 | return false; |
| 89 | } |
| 90 | // This only supports filled paths, however, the caller may apply the style to make a filled |
| 91 | // path and try again. |
| 92 | if (!args.fShape->style().isSimpleFill()) { |
| 93 | return false; |
| 94 | } |
| 95 | // This does non-inverse antialiased fills. |
| 96 | if (!args.fAntiAlias) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 99 | // TODO: Support inverse fill |
bsalomon | db7979a | 2016-06-27 11:08:43 -0700 | [diff] [blame] | 100 | if (args.fShape->inverseFilled()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 101 | return false; |
| 102 | } |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 103 | // currently don't support perspective |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 104 | if (args.fViewMatrix->hasPerspective()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 107 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 108 | // only support paths with bounds within kMediumMIP by kMediumMIP, |
| 109 | // scaled to have bounds within 2.0f*kLargeMIP by 2.0f*kLargeMIP |
jvanverth | b61283f | 2014-10-30 05:57:21 -0700 | [diff] [blame] | 110 | // 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] | 111 | SkScalar maxScale = args.fViewMatrix->getMaxScale(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 112 | SkRect bounds = args.fShape->styledBounds(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 113 | SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
bsalomon | 6266dca | 2016-03-11 06:22:00 -0800 | [diff] [blame] | 114 | |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 115 | return maxDim <= kMediumMIP && maxDim * maxScale <= 2.0f*kLargeMIP; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 116 | } |
| 117 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 118 | //////////////////////////////////////////////////////////////////////////////// |
| 119 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 120 | // padding around path bounds to allow for antialiased pixels |
| 121 | static const SkScalar kAntiAliasPad = 1.0f; |
| 122 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 123 | class AADistanceFieldPathBatch : public GrVertexBatch { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 124 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 125 | DEFINE_BATCH_CLASS_ID |
| 126 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 127 | typedef GrAADistanceFieldPathRenderer::ShapeData ShapeData; |
| 128 | typedef SkTDynamicHash<ShapeData, ShapeData::Key> ShapeCache; |
| 129 | typedef GrAADistanceFieldPathRenderer::ShapeDataList ShapeDataList; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 130 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 131 | AADistanceFieldPathBatch(GrColor color, |
| 132 | const GrShape& shape, |
| 133 | bool antiAlias, |
| 134 | const SkMatrix& viewMatrix, |
| 135 | GrBatchAtlas* atlas, |
| 136 | ShapeCache* shapeCache, ShapeDataList* shapeList, |
| 137 | bool gammaCorrect) |
| 138 | : INHERITED(ClassID()) { |
| 139 | SkASSERT(shape.hasUnstyledKey()); |
| 140 | fBatch.fViewMatrix = viewMatrix; |
| 141 | fGeoData.emplace_back(Geometry{color, shape, antiAlias}); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 142 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 143 | fAtlas = atlas; |
| 144 | fShapeCache = shapeCache; |
| 145 | fShapeList = shapeList; |
| 146 | fGammaCorrect = gammaCorrect; |
| 147 | |
| 148 | // Compute bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 149 | this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 152 | const char* name() const override { return "AADistanceFieldPathBatch"; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 153 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 154 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 155 | GrInitInvariantOutput* coverage, |
| 156 | GrBatchToXPOverrides* overrides) const override { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 157 | color->setKnownFourComponents(fGeoData[0].fColor); |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 158 | coverage->setUnknownSingleComponent(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 159 | } |
| 160 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 161 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 162 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 163 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 164 | if (!overrides.readsColor()) { |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 165 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 166 | } |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 167 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 168 | |
| 169 | // setup batch properties |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 170 | fBatch.fColorIgnored = !overrides.readsColor(); |
| 171 | fBatch.fUsesLocalCoords = overrides.readsLocalCoords(); |
| 172 | fBatch.fCoverageIgnored = !overrides.readsCoverage(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 175 | struct FlushInfo { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 176 | SkAutoTUnref<const GrBuffer> fVertexBuffer; |
| 177 | SkAutoTUnref<const GrBuffer> fIndexBuffer; |
| 178 | sk_sp<GrGeometryProcessor> fGeometryProcessor; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 179 | int fVertexOffset; |
| 180 | int fInstancesToFlush; |
| 181 | }; |
| 182 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 183 | void onPrepareDraws(Target* target) const override { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 184 | int instanceCount = fGeoData.count(); |
| 185 | |
| 186 | SkMatrix invert; |
| 187 | if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) { |
| 188 | SkDebugf("Could not invert viewmatrix\n"); |
| 189 | return; |
| 190 | } |
| 191 | |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 192 | const SkMatrix& ctm = this->viewMatrix(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 193 | uint32_t flags = 0; |
jvanverth | cf371bb | 2016-03-10 11:10:43 -0800 | [diff] [blame] | 194 | flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0; |
| 195 | flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 196 | flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 197 | |
| 198 | GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBilerp_FilterMode); |
| 199 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 200 | FlushInfo flushInfo; |
| 201 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 202 | // Setup GrGeometryProcessor |
| 203 | GrBatchAtlas* atlas = fAtlas; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 204 | flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(this->color(), |
| 205 | this->viewMatrix(), |
| 206 | atlas->getTexture(), |
| 207 | params, |
| 208 | flags, |
| 209 | this->usesLocalCoords()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 210 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 211 | // allocate vertices |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 212 | size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride(); |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 213 | SkASSERT(vertexStride == 2 * sizeof(SkPoint) + sizeof(GrColor)); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 214 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 215 | const GrBuffer* vertexBuffer; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 216 | void* vertices = target->makeVertexSpace(vertexStride, |
| 217 | kVerticesPerQuad * instanceCount, |
| 218 | &vertexBuffer, |
| 219 | &flushInfo.fVertexOffset); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 220 | flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 221 | flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 222 | if (!vertices || !flushInfo.fIndexBuffer) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 223 | SkDebugf("Could not allocate vertices\n"); |
| 224 | return; |
| 225 | } |
| 226 | |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 227 | flushInfo.fInstancesToFlush = 0; |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 228 | // Pointer to the next set of vertices to write. |
| 229 | intptr_t offset = reinterpret_cast<intptr_t>(vertices); |
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(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 235 | const SkRect& bounds = args.fShape.bounds(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 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 |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 248 | ShapeData::Key key(args.fShape, desiredDimension); |
| 249 | ShapeData* shapeData = fShapeCache->find(key); |
| 250 | if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 251 | // Remove the stale cache entry |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 252 | if (shapeData) { |
| 253 | fShapeCache->remove(shapeData->fKey); |
| 254 | fShapeList->remove(shapeData); |
| 255 | delete shapeData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 256 | } |
| 257 | SkScalar scale = desiredDimension/maxDim; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 258 | shapeData = new ShapeData; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 259 | if (!this->addPathToAtlas(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 260 | &flushInfo, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 261 | atlas, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 262 | shapeData, |
| 263 | args.fShape, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 264 | args.fAntiAlias, |
| 265 | desiredDimension, |
| 266 | scale)) { |
bsalomon | 67c6c8e | 2016-07-14 07:22:19 -0700 | [diff] [blame] | 267 | delete shapeData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 268 | SkDebugf("Can't rasterize path\n"); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 269 | continue; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 273 | atlas->setLastUseToken(shapeData->fID, target->nextDrawToken()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 274 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 275 | this->writePathVertices(target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 276 | atlas, |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 277 | offset, |
| 278 | args.fColor, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 279 | vertexStride, |
| 280 | this->viewMatrix(), |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 281 | shapeData); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 282 | offset += kVerticesPerQuad * vertexStride; |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 283 | flushInfo.fInstancesToFlush++; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 284 | } |
| 285 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 286 | this->flush(target, &flushInfo); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 287 | } |
| 288 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 289 | bool addPathToAtlas(GrVertexBatch::Target* target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 290 | FlushInfo* flushInfo, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 291 | GrBatchAtlas* atlas, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 292 | ShapeData* shapeData, |
| 293 | const GrShape& shape, |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 294 | bool antiAlias, |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 295 | uint32_t dimension, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 296 | SkScalar scale) const { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 297 | const SkRect& bounds = shape.bounds(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 298 | |
| 299 | // generate bounding rect for bitmap draw |
| 300 | SkRect scaledBounds = bounds; |
| 301 | // scale to mip level size |
| 302 | scaledBounds.fLeft *= scale; |
| 303 | scaledBounds.fTop *= scale; |
| 304 | scaledBounds.fRight *= scale; |
| 305 | scaledBounds.fBottom *= scale; |
| 306 | // move the origin to an integer boundary (gives better results) |
| 307 | SkScalar dx = SkScalarFraction(scaledBounds.fLeft); |
| 308 | SkScalar dy = SkScalarFraction(scaledBounds.fTop); |
| 309 | scaledBounds.offset(-dx, -dy); |
| 310 | // get integer boundary |
| 311 | SkIRect devPathBounds; |
| 312 | scaledBounds.roundOut(&devPathBounds); |
| 313 | // pad to allow room for antialiasing |
jvanverth | ecbed9d | 2015-12-18 10:07:52 -0800 | [diff] [blame] | 314 | const int intPad = SkScalarCeilToInt(kAntiAliasPad); |
| 315 | // pre-move origin (after outset, will be 0,0) |
| 316 | int width = devPathBounds.width(); |
| 317 | int height = devPathBounds.height(); |
| 318 | devPathBounds.fLeft = intPad; |
| 319 | devPathBounds.fTop = intPad; |
| 320 | devPathBounds.fRight = intPad + width; |
| 321 | devPathBounds.fBottom = intPad + height; |
| 322 | devPathBounds.outset(intPad, intPad); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 323 | |
| 324 | // draw path to bitmap |
| 325 | SkMatrix drawMatrix; |
| 326 | drawMatrix.setTranslate(-bounds.left(), -bounds.top()); |
| 327 | drawMatrix.postScale(scale, scale); |
| 328 | drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad); |
| 329 | |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 330 | // setup bitmap backing |
jvanverth | 512e437 | 2015-11-23 11:50:02 -0800 | [diff] [blame] | 331 | SkASSERT(devPathBounds.fLeft == 0); |
| 332 | SkASSERT(devPathBounds.fTop == 0); |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 333 | SkAutoPixmapStorage dst; |
| 334 | if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), |
| 335 | devPathBounds.height()))) { |
| 336 | return false; |
| 337 | } |
| 338 | sk_bzero(dst.writable_addr(), dst.getSafeSize()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 339 | |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 340 | // rasterize path |
| 341 | SkPaint paint; |
| 342 | paint.setStyle(SkPaint::kFill_Style); |
| 343 | paint.setAntiAlias(antiAlias); |
| 344 | |
| 345 | SkDraw draw; |
| 346 | sk_bzero(&draw, sizeof(draw)); |
| 347 | |
| 348 | SkRasterClip rasterClip; |
| 349 | rasterClip.setRect(devPathBounds); |
| 350 | draw.fRC = &rasterClip; |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 351 | draw.fMatrix = &drawMatrix; |
| 352 | draw.fDst = dst; |
| 353 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 354 | SkPath path; |
| 355 | shape.asPath(&path); |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 356 | draw.drawPathCoverage(path, paint); |
| 357 | |
| 358 | // generate signed distance field |
| 359 | devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 360 | width = devPathBounds.width(); |
| 361 | height = devPathBounds.height(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 362 | // TODO We should really generate this directly into the plot somehow |
| 363 | SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char)); |
| 364 | |
robertphillips | 2a7cf5f | 2016-03-02 05:36:30 -0800 | [diff] [blame] | 365 | // Generate signed distance field |
| 366 | SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(), |
| 367 | (const unsigned char*)dst.addr(), |
| 368 | dst.width(), dst.height(), dst.rowBytes()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 369 | |
| 370 | // add to atlas |
| 371 | SkIPoint16 atlasLocation; |
| 372 | GrBatchAtlas::AtlasID id; |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 373 | if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 374 | this->flush(target, flushInfo); |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 375 | if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) { |
| 376 | return false; |
| 377 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // add to cache |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 381 | shapeData->fKey.set(shape, dimension); |
| 382 | shapeData->fScale = scale; |
| 383 | shapeData->fID = id; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 384 | // change the scaled rect to match the size of the inset distance field |
| 385 | scaledBounds.fRight = scaledBounds.fLeft + |
| 386 | SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset); |
| 387 | scaledBounds.fBottom = scaledBounds.fTop + |
| 388 | SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset); |
| 389 | // shift the origin to the correct place relative to the distance field |
| 390 | // need to also restore the fractional translation |
| 391 | scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dx, |
| 392 | -SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPad + dy); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 393 | shapeData->fBounds = scaledBounds; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 394 | // origin we render from is inset from distance field edge |
| 395 | atlasLocation.fX += SK_DistanceFieldInset; |
| 396 | atlasLocation.fY += SK_DistanceFieldInset; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 397 | shapeData->fAtlasLocation = atlasLocation; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 398 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 399 | fShapeCache->add(shapeData); |
| 400 | fShapeList->addToTail(shapeData); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 401 | #ifdef DF_PATH_TRACKING |
| 402 | ++g_NumCachedPaths; |
| 403 | #endif |
| 404 | return true; |
| 405 | } |
| 406 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 407 | void writePathVertices(GrDrawBatch::Target* target, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 408 | GrBatchAtlas* atlas, |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 409 | intptr_t offset, |
| 410 | GrColor color, |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 411 | size_t vertexStride, |
| 412 | const SkMatrix& viewMatrix, |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 413 | const ShapeData* shapeData) const { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 414 | GrTexture* texture = atlas->getTexture(); |
| 415 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 416 | SkScalar dx = shapeData->fBounds.fLeft; |
| 417 | SkScalar dy = shapeData->fBounds.fTop; |
| 418 | SkScalar width = shapeData->fBounds.width(); |
| 419 | SkScalar height = shapeData->fBounds.height(); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 420 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 421 | SkScalar invScale = 1.0f / shapeData->fScale; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 422 | dx *= invScale; |
| 423 | dy *= invScale; |
| 424 | width *= invScale; |
| 425 | height *= invScale; |
| 426 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 427 | SkPoint* positions = reinterpret_cast<SkPoint*>(offset); |
| 428 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 429 | // vertex positions |
| 430 | // TODO make the vertex attributes a struct |
| 431 | SkRect r = SkRect::MakeXYWH(dx, dy, width, height); |
| 432 | positions->setRectFan(r.left(), r.top(), r.right(), r.bottom(), vertexStride); |
| 433 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 434 | // colors |
| 435 | for (int i = 0; i < kVerticesPerQuad; i++) { |
| 436 | GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride); |
| 437 | *colorPtr = color; |
| 438 | } |
| 439 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 440 | const SkScalar tx = SkIntToScalar(shapeData->fAtlasLocation.fX); |
| 441 | const SkScalar ty = SkIntToScalar(shapeData->fAtlasLocation.fY); |
benjaminwagner | 01e5838 | 2016-02-22 11:10:33 -0800 | [diff] [blame] | 442 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 443 | // vertex texture coords |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 444 | SkPoint* textureCoords = (SkPoint*)(offset + sizeof(SkPoint) + sizeof(GrColor)); |
benjaminwagner | 01e5838 | 2016-02-22 11:10:33 -0800 | [diff] [blame] | 445 | textureCoords->setRectFan(tx / texture->width(), |
| 446 | ty / texture->height(), |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 447 | (tx + shapeData->fBounds.width()) / texture->width(), |
| 448 | (ty + shapeData->fBounds.height()) / texture->height(), |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 449 | vertexStride); |
| 450 | } |
| 451 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 452 | void flush(GrVertexBatch::Target* target, FlushInfo* flushInfo) const { |
bsalomon | 6d6b6ad | 2016-07-13 14:45:28 -0700 | [diff] [blame] | 453 | if (flushInfo->fInstancesToFlush) { |
| 454 | GrMesh mesh; |
| 455 | int maxInstancesPerDraw = |
| 456 | static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6); |
| 457 | mesh.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuffer, |
| 458 | flushInfo->fIndexBuffer, flushInfo->fVertexOffset, kVerticesPerQuad, |
| 459 | kIndicesPerQuad, flushInfo->fInstancesToFlush, maxInstancesPerDraw); |
| 460 | target->draw(flushInfo->fGeometryProcessor.get(), mesh); |
| 461 | flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush; |
| 462 | flushInfo->fInstancesToFlush = 0; |
| 463 | } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 464 | } |
| 465 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 466 | GrColor color() const { return fGeoData[0].fColor; } |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 467 | const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
| 468 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 469 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 470 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 471 | AADistanceFieldPathBatch* that = t->cast<AADistanceFieldPathBatch>(); |
| 472 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 473 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 474 | return false; |
| 475 | } |
| 476 | |
joshualitt | 53f26aa | 2015-12-10 07:29:54 -0800 | [diff] [blame] | 477 | // TODO We can position on the cpu |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 478 | if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 479 | return false; |
| 480 | } |
| 481 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 482 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 483 | this->joinBounds(*that); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
| 487 | struct BatchTracker { |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 488 | SkMatrix fViewMatrix; |
| 489 | bool fUsesLocalCoords; |
| 490 | bool fColorIgnored; |
| 491 | bool fCoverageIgnored; |
| 492 | }; |
| 493 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 494 | struct Geometry { |
| 495 | GrColor fColor; |
| 496 | GrShape fShape; |
| 497 | bool fAntiAlias; |
| 498 | }; |
| 499 | |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 500 | BatchTracker fBatch; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 501 | SkSTArray<1, Geometry> fGeoData; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 502 | GrBatchAtlas* fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 503 | ShapeCache* fShapeCache; |
| 504 | ShapeDataList* fShapeList; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 505 | bool fGammaCorrect; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 506 | |
| 507 | typedef GrVertexBatch INHERITED; |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 510 | bool GrAADistanceFieldPathRenderer::onDrawPath(const DrawPathArgs& args) { |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 511 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 512 | "GrAADistanceFieldPathRenderer::onDrawPath"); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 513 | SkASSERT(!args.fDrawContext->isUnifiedMultisampled()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 514 | SkASSERT(args.fShape->style().isSimpleFill()); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 515 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 516 | // we've already bailed on inverse filled paths, so this is safe |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 517 | SkASSERT(!args.fShape->isEmpty()); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 518 | SkASSERT(args.fShape->hasUnstyledKey()); |
joshualitt | 5bf99f1 | 2015-03-13 11:47:42 -0700 | [diff] [blame] | 519 | if (!fAtlas) { |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 520 | fAtlas = args.fResourceProvider->createAtlas(kAlpha_8_GrPixelConfig, |
| 521 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
| 522 | NUM_PLOTS_X, NUM_PLOTS_Y, |
| 523 | &GrAADistanceFieldPathRenderer::HandleEviction, |
| 524 | (void*)this); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 525 | if (!fAtlas) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | |
robertphillips | 3950f0d | 2016-07-07 07:33:13 -0700 | [diff] [blame] | 530 | SkAutoTUnref<GrDrawBatch> batch(new AADistanceFieldPathBatch(args.fPaint->getColor(), |
| 531 | *args.fShape, |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 532 | args.fAntiAlias, *args.fViewMatrix, |
| 533 | fAtlas, &fShapeCache, &fShapeList, |
| 534 | args.fGammaCorrect)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 535 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame^] | 536 | GrPipelineBuilder pipelineBuilder(*args.fPaint); |
| 537 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 538 | |
| 539 | args.fDrawContext->drawBatch(pipelineBuilder, *args.fClip, batch); |
joshualitt | 9491f7f | 2015-02-11 11:33:38 -0800 | [diff] [blame] | 540 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 541 | return true; |
| 542 | } |
| 543 | |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 544 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 545 | |
| 546 | #ifdef GR_TEST_UTILS |
| 547 | |
| 548 | struct PathTestStruct { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 549 | typedef GrAADistanceFieldPathRenderer::ShapeCache ShapeCache; |
| 550 | typedef GrAADistanceFieldPathRenderer::ShapeData ShapeData; |
| 551 | typedef GrAADistanceFieldPathRenderer::ShapeDataList ShapeDataList; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 552 | PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {} |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 553 | ~PathTestStruct() { this->reset(); } |
| 554 | |
| 555 | void reset() { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 556 | ShapeDataList::Iter iter; |
| 557 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 558 | ShapeData* shapeData; |
| 559 | while ((shapeData = iter.get())) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 560 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 561 | fShapeList.remove(shapeData); |
| 562 | delete shapeData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 563 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 564 | delete fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 565 | fShapeCache.reset(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void HandleEviction(GrBatchAtlas::AtlasID id, void* pr) { |
| 569 | PathTestStruct* dfpr = (PathTestStruct*)pr; |
| 570 | // remove any paths that use this plot |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 571 | ShapeDataList::Iter iter; |
| 572 | iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart); |
| 573 | ShapeData* shapeData; |
| 574 | while ((shapeData = iter.get())) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 575 | iter.next(); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 576 | if (id == shapeData->fID) { |
| 577 | dfpr->fShapeCache.remove(shapeData->fKey); |
| 578 | dfpr->fShapeList.remove(shapeData); |
| 579 | delete shapeData; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | uint32_t fContextID; |
| 585 | GrBatchAtlas* fAtlas; |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 586 | ShapeCache fShapeCache; |
| 587 | ShapeDataList fShapeList; |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 588 | }; |
| 589 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 590 | DRAW_BATCH_TEST_DEFINE(AADistanceFieldPathBatch) { |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 591 | static PathTestStruct gTestStruct; |
| 592 | |
| 593 | if (context->uniqueID() != gTestStruct.fContextID) { |
| 594 | gTestStruct.fContextID = context->uniqueID(); |
| 595 | gTestStruct.reset(); |
joshualitt | b356cbc | 2015-08-05 06:36:39 -0700 | [diff] [blame] | 596 | gTestStruct.fAtlas = |
| 597 | context->resourceProvider()->createAtlas(kAlpha_8_GrPixelConfig, |
| 598 | ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT, |
| 599 | NUM_PLOTS_X, NUM_PLOTS_Y, |
| 600 | &PathTestStruct::HandleEviction, |
| 601 | (void*)&gTestStruct); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 605 | GrColor color = GrRandomColor(random); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 606 | bool gammaCorrect = random->nextBool(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 607 | |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 608 | // This path renderer only allows fill styles. |
| 609 | GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill()); |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 610 | bool antiAlias = random->nextBool(); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 611 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 612 | return new AADistanceFieldPathBatch(color, |
| 613 | shape, |
| 614 | antiAlias, |
| 615 | viewMatrix, |
| 616 | gTestStruct.fAtlas, |
| 617 | &gTestStruct.fShapeCache, |
| 618 | &gTestStruct.fShapeList, |
| 619 | gammaCorrect); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | #endif |