blob: 2a69e78966a59b0de3f4a40d37a84bd037604575 [file] [log] [blame]
Robert Phillips46a324a2020-08-10 13:08:12 -04001/*
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/GrSmallPathAtlasMgr.h"
9
10#include "src/gpu/geometry/GrStyledShape.h"
11#include "src/gpu/ops/GrSmallPathShapeData.h"
12
13#ifdef DF_PATH_TRACKING
14static int g_NumCachedShapes = 0;
15static int g_NumFreedShapes = 0;
16#endif
17
18GrSmallPathAtlasMgr::GrSmallPathAtlasMgr() {}
19
20GrSmallPathAtlasMgr::~GrSmallPathAtlasMgr() {
21 ShapeDataList::Iter iter;
22 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
23 GrSmallPathShapeData* shapeData;
24 while ((shapeData = iter.get())) {
25 iter.next();
26 delete shapeData;
27 }
28
29#ifdef DF_PATH_TRACKING
30 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
31#endif
32}
33
34bool GrSmallPathAtlasMgr::initAtlas(GrProxyProvider* proxyProvider, const GrCaps* caps) {
35 if (fAtlas) {
36 return true;
37 }
38
39 static constexpr size_t kMaxAtlasTextureBytes = 2048 * 2048;
40 static constexpr size_t kPlotWidth = 512;
41 static constexpr size_t kPlotHeight = 256;
42
43 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
Robert Phillips109ff202020-08-10 16:39:31 -040044 GrRenderable::kNo);
Robert Phillips46a324a2020-08-10 13:08:12 -040045
46 GrDrawOpAtlasConfig atlasConfig(caps->maxTextureSize(), kMaxAtlasTextureBytes);
47 SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat);
48 fAtlas = GrDrawOpAtlas::Make(proxyProvider, format,
49 GrColorType::kAlpha_8, size.width(), size.height(),
50 kPlotWidth, kPlotHeight, this,
51 GrDrawOpAtlas::AllowMultitexturing::kYes, this);
52
53 return SkToBool(fAtlas);
54}
55
56void GrSmallPathAtlasMgr::deleteCacheEntry(GrSmallPathShapeData* shapeData) {
57 fShapeCache.remove(shapeData->fKey);
58 fShapeList.remove(shapeData);
59 delete shapeData;
60}
61
62GrSmallPathShapeData* GrSmallPathAtlasMgr::findOrCreate(const GrSmallPathShapeDataKey& key) {
63 auto shapeData = fShapeCache.find(key);
64 if (!shapeData) {
Robert Phillips109ff202020-08-10 16:39:31 -040065 // TODO: move the key into the ctor
Robert Phillips46a324a2020-08-10 13:08:12 -040066 shapeData = new GrSmallPathShapeData(key);
67 fShapeCache.add(shapeData);
68 fShapeList.addToTail(shapeData);
69#ifdef DF_PATH_TRACKING
70 ++g_NumCachedShapes;
71#endif
72 } else if (!fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) {
73 shapeData->fAtlasLocator.invalidatePlotLocator();
74 }
75
76 return shapeData;
77}
78
Robert Phillips109ff202020-08-10 16:39:31 -040079GrSmallPathShapeData* GrSmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
80 int desiredDimension) {
81 GrSmallPathShapeDataKey key(shape, desiredDimension);
82
83 // TODO: move the key into 'findOrCreate'
84 return this->findOrCreate(key);
85}
86
87GrSmallPathShapeData* GrSmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
88 const SkMatrix& ctm) {
89 GrSmallPathShapeDataKey key(shape, ctm);
90
91 // TODO: move the key into 'findOrCreate'
92 return this->findOrCreate(key);
93}
94
Robert Phillips46a324a2020-08-10 13:08:12 -040095void GrSmallPathAtlasMgr::setUseToken(GrSmallPathShapeData* shapeData,
96 GrDeferredUploadToken token) {
97 fAtlas->setLastUseToken(shapeData->fAtlasLocator, token);
98}
99
100// Callback to clear out internal path cache when eviction occurs
101void GrSmallPathAtlasMgr::evict(GrDrawOpAtlas::PlotLocator plotLocator) {
102 // remove any paths that use this plot
103 ShapeDataList::Iter iter;
104 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
105 GrSmallPathShapeData* shapeData;
106 while ((shapeData = iter.get())) {
107 iter.next();
108 if (plotLocator == shapeData->fAtlasLocator.plotLocator()) {
109 fShapeCache.remove(shapeData->fKey);
110 fShapeList.remove(shapeData);
111 delete shapeData;
112#ifdef DF_PATH_TRACKING
113 ++g_NumFreedShapes;
114#endif
115 }
116 }
117}