blob: 80da98cdc46caebef0919f2d45967d04dceedeb0 [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,
44 GrRenderable::kNo);
45
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) {
65 shapeData = new GrSmallPathShapeData(key);
66 fShapeCache.add(shapeData);
67 fShapeList.addToTail(shapeData);
68#ifdef DF_PATH_TRACKING
69 ++g_NumCachedShapes;
70#endif
71 } else if (!fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) {
72 shapeData->fAtlasLocator.invalidatePlotLocator();
73 }
74
75 return shapeData;
76}
77
78void GrSmallPathAtlasMgr::setUseToken(GrSmallPathShapeData* shapeData,
79 GrDeferredUploadToken token) {
80 fAtlas->setLastUseToken(shapeData->fAtlasLocator, token);
81}
82
83// Callback to clear out internal path cache when eviction occurs
84void GrSmallPathAtlasMgr::evict(GrDrawOpAtlas::PlotLocator plotLocator) {
85 // remove any paths that use this plot
86 ShapeDataList::Iter iter;
87 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
88 GrSmallPathShapeData* shapeData;
89 while ((shapeData = iter.get())) {
90 iter.next();
91 if (plotLocator == shapeData->fAtlasLocator.plotLocator()) {
92 fShapeCache.remove(shapeData->fKey);
93 fShapeList.remove(shapeData);
94 delete shapeData;
95#ifdef DF_PATH_TRACKING
96 ++g_NumFreedShapes;
97#endif
98 }
99 }
100}