Debian Codegraph @

src/gpu/ganesh/ops/SmallPathAtlasMgr.cpp

raw ◀ ▶
1 Debian patch touch this file
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/ganesh/ops/SmallPathAtlasMgr.h"
9
10 #include "include/core/SkSize.h"
11 #include "include/gpu/GpuTypes.h"
12 #include "include/gpu/ganesh/GrBackendSurface.h"
13 #include "include/gpu/ganesh/GrTypes.h"
•14 #include "include/private/SkIDChangeListener.h"
15 #include "include/private/base/SkTo.h"
16 #include "include/private/gpu/ganesh/GrTypesPriv.h"
17 #include "src/gpu/ganesh/GrCaps.h"
•18 #include "src/gpu/ganesh/geometry/GrStyledShape.h"
19 #include "src/gpu/ganesh/ops/SmallPathShapeData.h"
20
21 #include <cstddef>
22
23 #if !defined(SK_ENABLE_OPTIMIZE_SIZE)
24
25 using MaskFormat = skgpu::MaskFormat;
26
27 #ifdef DF_PATH_TRACKING
28 static int g_NumCachedShapes = 0;
29 static int g_NumFreedShapes = 0;
30 #endif
31
32 namespace skgpu::ganesh {
33
34 SmallPathAtlasMgr::SmallPathAtlasMgr() {}
35
36 SmallPathAtlasMgr::~SmallPathAtlasMgr() {
37 this->reset();
38 }
39
40 void SmallPathAtlasMgr::reset() {
41 ShapeDataList::Iter iter;
42 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
43 SmallPathShapeData* shapeData;
44 while ((shapeData = iter.get())) {
45 iter.next();
46 delete shapeData;
47 }
48
49 fShapeList.reset();
50 fShapeCache.reset();
51
52 #ifdef DF_PATH_TRACKING
53 SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
54 #endif
55
56 fAtlas = nullptr;
57 }
58
59 bool SmallPathAtlasMgr::initAtlas(GrProxyProvider* proxyProvider, const GrCaps* caps) {
60 if (fAtlas) {
61 return true;
62 }
63
64 static constexpr size_t kMaxAtlasTextureBytes = 2048 * 2048;
65 static constexpr size_t kPlotWidth = 512;
66 static constexpr size_t kPlotHeight = 256;
67
68 GrColorType atlasColorType = GrColorType::kAlpha_8;
69 const GrBackendFormat format = caps->getDefaultBackendFormat(atlasColorType,
70 GrRenderable::kNo);
71
72 GrDrawOpAtlasConfig atlasConfig(caps->maxTextureSize(), kMaxAtlasTextureBytes);
73 SkISize size = atlasConfig.atlasDimensions(MaskFormat::kA8);
74 fAtlas = GrDrawOpAtlas::Make(proxyProvider, format,
75 GrColorTypeToSkColorType(atlasColorType),
76 GrColorTypeBytesPerPixel(atlasColorType),
77 size.width(), size.height(),
78 kPlotWidth, kPlotHeight, this,
79 GrDrawOpAtlas::AllowMultitexturing::kYes,
80 this,
81 /*label=*/"SmallPathAtlas");
82
83 return SkToBool(fAtlas);
84 }
85
86 void SmallPathAtlasMgr::deleteCacheEntry(SmallPathShapeData* shapeData) {
87 fShapeCache.remove(shapeData->fKey);
88 fShapeList.remove(shapeData);
89 delete shapeData;
90 }
91
•92 SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const SmallPathShapeDataKey& key,
93 const GrStyledShape& shape) {
94 auto shapeData = fShapeCache.find(key);
•95
96 if (shapeData && shapeData->fIDChangeListener->hasChanged()) {
97 this->deleteCacheEntry(shapeData);
98 shapeData = nullptr;
99 }
100
101 if (!shapeData) {
102 // TODO: move the key into the ctor
103 shapeData = new SmallPathShapeData(key);
•104 shape.addGenIDChangeListener(shapeData->fIDChangeListener);
105 fShapeCache.add(shapeData);
106 fShapeList.addToTail(shapeData);
107 #ifdef DF_PATH_TRACKING
108 ++g_NumCachedShapes;
109 #endif
110 } else if (!fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) {
111 shapeData->fAtlasLocator.invalidatePlotLocator();
112 }
113
114 return shapeData;
115 }
116
117 SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
118 int desiredDimension) {
119 SmallPathShapeDataKey key(shape, desiredDimension);
120
121 // TODO: move the key into 'findOrCreate'
•122 return this->findOrCreate(key, shape);
123 }
124
125 SmallPathShapeData* SmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
126 const SkMatrix& ctm) {
127 SmallPathShapeDataKey key(shape, ctm);
128
129 // TODO: move the key into 'findOrCreate'
•130 return this->findOrCreate(key, shape);
131 }
132
133 GrDrawOpAtlas::ErrorCode SmallPathAtlasMgr::addToAtlas(GrResourceProvider* resourceProvider,
134 GrDeferredUploadTarget* target,
135 int width, int height, const void* image,
136 skgpu::AtlasLocator* locator) {
137 return fAtlas->addToAtlas(resourceProvider, target, width, height, image, locator);
138 }
139
140 void SmallPathAtlasMgr::setUseToken(SmallPathShapeData* shapeData,
141 skgpu::Token token) {
142 fAtlas->setLastUseToken(shapeData->fAtlasLocator, token);
143 }
144
145 // Callback to clear out internal path cache when eviction occurs
146 void SmallPathAtlasMgr::evict(skgpu::PlotLocator plotLocator) {
147 // remove any paths that use this plot
148 ShapeDataList::Iter iter;
149 iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
150 SmallPathShapeData* shapeData;
151 while ((shapeData = iter.get())) {
152 iter.next();
153 if (plotLocator == shapeData->fAtlasLocator.plotLocator()) {
154 fShapeCache.remove(shapeData->fKey);
155 fShapeList.remove(shapeData);
156 delete shapeData;
157 #ifdef DF_PATH_TRACKING
158 ++g_NumFreedShapes;
159 #endif
160 }
161 }
162 }
163
164 } // namespace skgpu::ganesh
165
166 #endif // SK_ENABLE_OPTIMIZE_SIZE