• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/gpu/ops/SmallPathAtlasMgr.h"
9 
10 #include "src/gpu/geometry/GrStyledShape.h"
11 #include "src/gpu/ops/SmallPathShapeData.h"
12 
13 #ifdef DF_PATH_TRACKING
14 static int g_NumCachedShapes = 0;
15 static int g_NumFreedShapes = 0;
16 #endif
17 
18 namespace skgpu::v1 {
19 
SmallPathAtlasMgr()20 SmallPathAtlasMgr::SmallPathAtlasMgr() {}
21 
~SmallPathAtlasMgr()22 SmallPathAtlasMgr::~SmallPathAtlasMgr() {
23     this->reset();
24 }
25 
reset()26 void SmallPathAtlasMgr::reset() {
27     ShapeDataList::Iter iter;
28     iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
29     SmallPathShapeData* shapeData;
30     while ((shapeData = iter.get())) {
31         iter.next();
32         delete shapeData;
33     }
34 
35     fShapeList.reset();
36     fShapeCache.reset();
37 
38 #ifdef DF_PATH_TRACKING
39     SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
40 #endif
41 
42     fAtlas = nullptr;
43 }
44 
initAtlas(GrProxyProvider * proxyProvider,const GrCaps * caps)45 bool SmallPathAtlasMgr::initAtlas(GrProxyProvider* proxyProvider, const GrCaps* caps) {
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,
55                                                                  GrRenderable::kNo);
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 
deleteCacheEntry(SmallPathShapeData * shapeData)67 void SmallPathAtlasMgr::deleteCacheEntry(SmallPathShapeData* shapeData) {
68     fShapeCache.remove(shapeData->fKey);
69     fShapeList.remove(shapeData);
70     delete shapeData;
71 }
72 
findOrCreate(const SmallPathShapeDataKey & key)73 SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const SmallPathShapeDataKey& key) {
74     auto shapeData = fShapeCache.find(key);
75     if (!shapeData) {
76         // TODO: move the key into the ctor
77         shapeData = new SmallPathShapeData(key);
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 
findOrCreate(const GrStyledShape & shape,int desiredDimension)90 SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
91                                                     int desiredDimension) {
92     SmallPathShapeDataKey key(shape, desiredDimension);
93 
94     // TODO: move the key into 'findOrCreate'
95     return this->findOrCreate(key);
96 }
97 
findOrCreate(const GrStyledShape & shape,const SkMatrix & ctm)98 SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
99                                                     const SkMatrix& ctm) {
100     SmallPathShapeDataKey key(shape, ctm);
101 
102     // TODO: move the key into 'findOrCreate'
103     return this->findOrCreate(key);
104 }
105 
addToAtlas(GrResourceProvider * resourceProvider,GrDeferredUploadTarget * target,int width,int height,const void * image,GrDrawOpAtlas::AtlasLocator * locator)106 GrDrawOpAtlas::ErrorCode SmallPathAtlasMgr::addToAtlas(GrResourceProvider* resourceProvider,
107                                                        GrDeferredUploadTarget* target,
108                                                        int width, int height, const void* image,
109                                                        GrDrawOpAtlas::AtlasLocator* locator) {
110     return fAtlas->addToAtlas(resourceProvider, target, width, height, image, locator);
111 }
112 
setUseToken(SmallPathShapeData * shapeData,GrDeferredUploadToken token)113 void SmallPathAtlasMgr::setUseToken(SmallPathShapeData* shapeData,
114                                     GrDeferredUploadToken token) {
115     fAtlas->setLastUseToken(shapeData->fAtlasLocator, token);
116 }
117 
118 // Callback to clear out internal path cache when eviction occurs
evict(GrDrawOpAtlas::PlotLocator plotLocator)119 void SmallPathAtlasMgr::evict(GrDrawOpAtlas::PlotLocator plotLocator) {
120     // remove any paths that use this plot
121     ShapeDataList::Iter iter;
122     iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
123     SmallPathShapeData* shapeData;
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 }
136 
137 } // namespace skgpu::v1
138