Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 8 | #include "src/gpu/ops/SmallPathAtlasMgr.h" |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/geometry/GrStyledShape.h" |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 11 | #include "src/gpu/ops/SmallPathShapeData.h" |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 12 | |
| 13 | #ifdef DF_PATH_TRACKING |
| 14 | static int g_NumCachedShapes = 0; |
| 15 | static int g_NumFreedShapes = 0; |
| 16 | #endif |
| 17 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 18 | namespace skgpu::v1 { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 19 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 20 | SmallPathAtlasMgr::SmallPathAtlasMgr() {} |
| 21 | |
| 22 | SmallPathAtlasMgr::~SmallPathAtlasMgr() { |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 23 | this->reset(); |
| 24 | } |
| 25 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 26 | void SmallPathAtlasMgr::reset() { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 27 | ShapeDataList::Iter iter; |
| 28 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 29 | SmallPathShapeData* shapeData; |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 30 | while ((shapeData = iter.get())) { |
| 31 | iter.next(); |
| 32 | delete shapeData; |
| 33 | } |
| 34 | |
Robert Phillips | 079455c | 2020-08-11 15:18:46 -0400 | [diff] [blame] | 35 | fShapeList.reset(); |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 36 | fShapeCache.reset(); |
| 37 | |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 38 | #ifdef DF_PATH_TRACKING |
| 39 | SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes); |
| 40 | #endif |
Robert Phillips | a4bb064 | 2020-08-11 09:55:17 -0400 | [diff] [blame] | 41 | |
| 42 | fAtlas = nullptr; |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 45 | bool SmallPathAtlasMgr::initAtlas(GrProxyProvider* proxyProvider, const GrCaps* caps) { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 46 | if (fAtlas) { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | static constexpr size_t kMaxAtlasTextureBytes = 2048 * 2048; |
| 51 | static constexpr size_t kPlotWidth = 512; |
| 52 | static constexpr size_t kPlotHeight = 256; |
| 53 | |
| 54 | const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8, |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 55 | GrRenderable::kNo); |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 56 | |
| 57 | GrDrawOpAtlasConfig atlasConfig(caps->maxTextureSize(), kMaxAtlasTextureBytes); |
| 58 | SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat); |
| 59 | fAtlas = GrDrawOpAtlas::Make(proxyProvider, format, |
| 60 | GrColorType::kAlpha_8, size.width(), size.height(), |
| 61 | kPlotWidth, kPlotHeight, this, |
| 62 | GrDrawOpAtlas::AllowMultitexturing::kYes, this); |
| 63 | |
| 64 | return SkToBool(fAtlas); |
| 65 | } |
| 66 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 67 | void SmallPathAtlasMgr::deleteCacheEntry(SmallPathShapeData* shapeData) { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 68 | fShapeCache.remove(shapeData->fKey); |
| 69 | fShapeList.remove(shapeData); |
| 70 | delete shapeData; |
| 71 | } |
| 72 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 73 | SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const SmallPathShapeDataKey& key) { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 74 | auto shapeData = fShapeCache.find(key); |
| 75 | if (!shapeData) { |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 76 | // TODO: move the key into the ctor |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 77 | shapeData = new SmallPathShapeData(key); |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 78 | fShapeCache.add(shapeData); |
| 79 | fShapeList.addToTail(shapeData); |
| 80 | #ifdef DF_PATH_TRACKING |
| 81 | ++g_NumCachedShapes; |
| 82 | #endif |
| 83 | } else if (!fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) { |
| 84 | shapeData->fAtlasLocator.invalidatePlotLocator(); |
| 85 | } |
| 86 | |
| 87 | return shapeData; |
| 88 | } |
| 89 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 90 | SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape, |
| 91 | int desiredDimension) { |
| 92 | SmallPathShapeDataKey key(shape, desiredDimension); |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 93 | |
| 94 | // TODO: move the key into 'findOrCreate' |
| 95 | return this->findOrCreate(key); |
| 96 | } |
| 97 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 98 | SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape, |
| 99 | const SkMatrix& ctm) { |
| 100 | SmallPathShapeDataKey key(shape, ctm); |
Robert Phillips | 109ff20 | 2020-08-10 16:39:31 -0400 | [diff] [blame] | 101 | |
| 102 | // TODO: move the key into 'findOrCreate' |
| 103 | return this->findOrCreate(key); |
| 104 | } |
| 105 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 106 | GrDrawOpAtlas::ErrorCode SmallPathAtlasMgr::addToAtlas(GrResourceProvider* resourceProvider, |
| 107 | GrDeferredUploadTarget* target, |
| 108 | int width, int height, const void* image, |
| 109 | GrDrawOpAtlas::AtlasLocator* locator) { |
Robert Phillips | 6704bc8 | 2020-08-14 12:45:07 -0400 | [diff] [blame] | 110 | return fAtlas->addToAtlas(resourceProvider, target, width, height, image, locator); |
| 111 | } |
| 112 | |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 113 | void SmallPathAtlasMgr::setUseToken(SmallPathShapeData* shapeData, |
| 114 | GrDeferredUploadToken token) { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 115 | fAtlas->setLastUseToken(shapeData->fAtlasLocator, token); |
| 116 | } |
| 117 | |
| 118 | // Callback to clear out internal path cache when eviction occurs |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 119 | void SmallPathAtlasMgr::evict(GrDrawOpAtlas::PlotLocator plotLocator) { |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 120 | // remove any paths that use this plot |
| 121 | ShapeDataList::Iter iter; |
| 122 | iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart); |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 123 | SmallPathShapeData* shapeData; |
Robert Phillips | 46a324a | 2020-08-10 13:08:12 -0400 | [diff] [blame] | 124 | while ((shapeData = iter.get())) { |
| 125 | iter.next(); |
| 126 | if (plotLocator == shapeData->fAtlasLocator.plotLocator()) { |
| 127 | fShapeCache.remove(shapeData->fKey); |
| 128 | fShapeList.remove(shapeData); |
| 129 | delete shapeData; |
| 130 | #ifdef DF_PATH_TRACKING |
| 131 | ++g_NumFreedShapes; |
| 132 | #endif |
| 133 | } |
| 134 | } |
| 135 | } |
Robert Phillips | de60d7a | 2021-09-13 17:17:45 -0400 | [diff] [blame] | 136 | |
| 137 | } // namespace skgpu::v1 |