Debian Codegraph @

src/gpu/graphite/ClipAtlasManager.cpp

raw
1 /*
2 * Copyright 2024 Google LLC
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 #include "src/gpu/graphite/ClipAtlasManager.h"
8
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkClipOp.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkSize.h"
14 #include "include/gpu/graphite/Recorder.h"
15 #include "include/private/base/SkAssert.h"
16 #include "include/private/base/SkMalloc.h"
17 #include "include/private/base/SkPoint_impl.h"
18 #include "include/private/base/SkTArray.h"
19 #include "src/core/SkAutoPixmapStorage.h"
20 #include "src/gpu/graphite/Caps.h"
21 #include "src/gpu/graphite/ProxyCache.h"
22 #include "src/gpu/graphite/RasterPathUtils.h"
23 #include "src/gpu/graphite/RecorderPriv.h"
24 #include "src/gpu/graphite/TextureProxy.h"
25 #include "src/gpu/graphite/geom/Shape.h"
26
27 namespace skgpu::graphite {
28
29 static constexpr int kClipAtlasWidth = 2048;
30 static constexpr int kClipAtlasHeight = 2048;
31
32 ClipAtlasManager::ClipAtlasManager(Recorder* recorder)
33 : fRecorder(recorder)
34 , fPathKeyAtlasMgr(kClipAtlasWidth, kClipAtlasHeight,
35 /*plotWidth=*/kClipAtlasWidth/2, /*plotHeight=*/kClipAtlasHeight/2,
36 DrawAtlas::UseStorageTextures::kNo,
37 "PathKeyClipAtlas", recorder->priv().caps())
38 // Examining the results from the top 20 or so webpages, the SaveRecord keyed clips
39 // tend to be considerably smaller and rarer, so we use a smaller atlas here.
40 , fSaveRecordKeyAtlasMgr(kClipAtlasWidth/2, kClipAtlasHeight/2,
41 /*plotWidth=*/kClipAtlasWidth/2, /*plotHeight=*/kClipAtlasHeight/2,
42 DrawAtlas::UseStorageTextures::kNo,
43 "SaveRecordKeyClipAtlas", recorder->priv().caps()) {}
44
45 namespace {
46 // Needed to ensure that we have surrounding context, e.g. for inverse clips this would be solid.
47 constexpr int kEntryPadding = 1;
48
49 // If the first element to draw is an intersect, we clear to 0 and will draw it directly with
50 // coverage 1 (subsequent intersect elements will be inverse-filled and draw 0 outside).
51 // If the first element to draw is a difference, we clear to 1, and in all cases we draw the
52 // difference element directly with coverage 0.
53 SkAlpha initial_alpha_for_elements(const ClipStack::ElementList& elements) {
54 SkASSERT(!elements.empty());
55 return elements[0]->fOp == SkClipOp::kIntersect ? 0x00 : 0xFF;
56 }
57
58 void render_elements(RasterMaskHelper* helper, const ClipStack::ElementList& elements) {
59 SkASSERT(!elements.empty());
60 bool isFirst = true;
61 for (const auto& ePtr : elements) {
62 const auto& e = *ePtr;
63 uint8_t alpha;
64 bool invert;
65 if (e.fOp == SkClipOp::kIntersect) {
66 // Intersect modifies pixels outside of its geometry. If this is the first element,
67 // we can draw directly with coverage 1 since we cleared to 0. Otherwise we draw the
68 // inverse-filled shape with 0 coverage to erase everything outside the element.
69 if (isFirst) {
70 alpha = 0xFF;
71 invert = false;
72 } else {
73 alpha = 0x00;
74 invert = true;
75 }
76 } else {
77 // For difference ops, can always just subtract the shape directly by drawing 0 coverage
78 SkASSERT(e.fOp == SkClipOp::kDifference);
79 alpha = 0x00;
80 invert = false;
81 }
82
83 // Draw the shape; based on how we've initialized the buffer and chosen alpha+invert,
84 // every element is drawn with the kReplace_Op
85 if (invert != e.fShape.inverted()) {
86 Shape inverted(e.fShape);
87 inverted.setInverted(invert);
88 helper->drawClip(inverted, e.fLocalToDevice, alpha);
89 } else {
90 helper->drawClip(e.fShape, e.fLocalToDevice, alpha);
91 }
92 isFirst = false;
93 }
94 }
95
96 } // anonymous namespace
97
98 sk_sp<TextureProxy> ClipAtlasManager::findOrCreateEntry(uint32_t stackRecordID,
99 const ClipStack::ElementList* elementList,
100 SkIRect maskDeviceBounds,
101 SkIPoint* outPos) {
102 // For the ClipAtlas cache, we don't include the bounds in the key
103 skgpu::UniqueKey maskKey;
104 bool usesPathKey;
105 // The keyBounds are the maskDeviceBounds relative to the full transformed mask. We use this
106 // to ensure we capture the situation where the maskDeviceBounds are equal in two cases but
107 // actually enclose different regions of the full mask due to a difference in integer
108 // translation (which is not captured in the key) in the element transforms.
109 SkIRect keyBounds;
110 maskKey = GenerateClipMaskKey(stackRecordID, elementList, maskDeviceBounds,
111 /*includeBounds=*/false, &keyBounds, &usesPathKey);
112
113 sk_sp<TextureProxy> atlasProxy;
114 if (usesPathKey) {
115 atlasProxy = fPathKeyAtlasMgr.findOrCreateEntry(fRecorder, maskKey, elementList,
116 maskDeviceBounds, keyBounds, outPos);
117 } else {
118 atlasProxy = fSaveRecordKeyAtlasMgr.findOrCreateEntry(fRecorder, maskKey, elementList,
119 maskDeviceBounds, keyBounds, outPos);
120 }
121 if (atlasProxy) {
122 return atlasProxy;
123 }
124
125 // We need to include the bounds in the key when using the ProxyCache
126 maskKey = GenerateClipMaskKey(stackRecordID, elementList, maskDeviceBounds,
127 /*includeBounds=*/true, &keyBounds, &usesPathKey);
128
129 const struct ClipDrawContext {
130 const ClipStack::ElementList* fElementList;
131 SkIRect fMaskDeviceBounds;
132 } context = {elementList, maskDeviceBounds};
133 sk_sp<TextureProxy> proxy = fRecorder->priv().proxyCache()->findOrCreateCachedProxy(
134 fRecorder, maskKey, &context,
135 [](const void* ctx) {
136 const ClipDrawContext* cdc = static_cast<const ClipDrawContext*>(ctx);
137 auto translate =
138 -cdc->fMaskDeviceBounds.topLeft() + SkIVector{kEntryPadding, kEntryPadding};
139 auto [bm, helper] =
140 RasterMaskHelper::Allocate(cdc->fMaskDeviceBounds.size(),
141 translate,
142 0,
143 initial_alpha_for_elements(*cdc->fElementList));
144
145 render_elements(&helper, *cdc->fElementList);
146 bm.setImmutable();
147 return bm;
148 });
149 *outPos = { kEntryPadding, kEntryPadding };
150
151 return proxy;
152 }
153
154 bool ClipAtlasManager::recordUploads(DrawContext* dc) {
155 return fPathKeyAtlasMgr.recordUploads(dc, fRecorder) ||
156 fSaveRecordKeyAtlasMgr.recordUploads(dc, fRecorder);
157 }
158
159 void ClipAtlasManager::compact() {
160 fPathKeyAtlasMgr.compact(fRecorder);
161 fSaveRecordKeyAtlasMgr.compact(fRecorder);
162 }
163
164 void ClipAtlasManager::freeGpuResources() {
165 fPathKeyAtlasMgr.freeGpuResources(fRecorder);
166 fSaveRecordKeyAtlasMgr.freeGpuResources(fRecorder);
167 }
168
169 void ClipAtlasManager::evictAtlases() {
170 fPathKeyAtlasMgr.evictAll();
171 fSaveRecordKeyAtlasMgr.evictAll();
172 }
173
174 //////////////////////////////////////////////////////////////////////////////////////////////
175
176 ClipAtlasManager::DrawAtlasMgr::DrawAtlasMgr(size_t width, size_t height,
177 size_t plotWidth, size_t plotHeight,
178 DrawAtlas::UseStorageTextures useStorageTextures,
179 std::string_view label, const Caps* caps) {
180 static constexpr SkColorType kColorType = kAlpha_8_SkColorType;
181 fDrawAtlas = DrawAtlas::Make(kColorType,
182 SkColorTypeBytesPerPixel(kColorType),
183 width, height,
184 plotWidth, plotHeight,
185 /*generationCounter=*/this,
186 caps->allowMultipleAtlasTextures() ?
187 DrawAtlas::AllowMultitexturing::kYes :
188 DrawAtlas::AllowMultitexturing::kNo,
189 useStorageTextures,
190 /*evictor=*/this,
191 label);
192 SkASSERT(fDrawAtlas);
193 fKeyLists.resize(fDrawAtlas->numPlots() * fDrawAtlas->maxPages());
194 for (int i = 0; i < fKeyLists.size(); ++i) {
195 fKeyLists[i].reset();
196 }
197 }
198
199 sk_sp<TextureProxy> ClipAtlasManager::DrawAtlasMgr::findOrCreateEntry(
200 Recorder* recorder,
201 const skgpu::UniqueKey& maskKey,
202 const ClipStack::ElementList* elementList,
203 SkIRect maskDeviceBounds,
204 SkIRect keyBounds,
205 SkIPoint* outPos) {
206 MaskHashEntry* entry = fMaskCache.find(maskKey);
207 while (entry) {
208 // If this entry is large enough to contain the clip, use it
209 if (entry->fBounds.contains(keyBounds)) {
210 SkIPoint topLeft = entry->fLocator.topLeft();
211 // We need to adjust the returned outPos to reflect the subset we're using
212 SkIPoint subsetRelativePos = keyBounds.topLeft() - entry->fBounds.topLeft();
213 *outPos = SkIPoint::Make(topLeft.x() + kEntryPadding + subsetRelativePos.x(),
214 topLeft.y() + kEntryPadding + subsetRelativePos.y());
215 fDrawAtlas->setLastUseToken(entry->fLocator,
216 recorder->priv().tokenTracker()->nextFlushToken());
217 return fDrawAtlas->getProxies()[entry->fLocator.pageIndex()];
218 }
219 entry = entry->fNext;
220 }
221
222 AtlasLocator locator;
223 sk_sp<TextureProxy> proxy = this->addToAtlas(recorder, elementList, maskDeviceBounds, outPos,
224 &locator);
225 if (!proxy) {
226 return nullptr;
227 }
228
229 // Look up again (in case this entry got purged during addToAtlas())
230 MaskHashEntry* entryList = fMaskCache.find(maskKey);
231
232 // Add locator and bounds to MaskCache.
233 if (entryList) {
234 // Add new list entry to the end. This will sort them from smallest bounds to largest,
235 // so that when we search above we'll pick the one with the smallest bounds that contains
236 // the clip.
237 MaskHashEntry* currEntry = entryList;
238 while (currEntry->fNext) {
239 currEntry = currEntry->fNext;
240 }
241 SkASSERT(currEntry);
242 SkASSERT(currEntry->fNext == nullptr); // Should be at the end
243 currEntry->fNext = new MaskHashEntry{keyBounds, locator, nullptr};
244 ++fHashEntryCount;
245 } else {
246 MaskHashEntry newEntry{keyBounds, locator, nullptr};
247 fMaskCache.set(maskKey, newEntry);
248 ++fHashEntryCount;
249 }
250
251 // Add key to Plot's MaskKeyList.
252 uint32_t index = fDrawAtlas->getListIndex(locator.plotLocator());
253 MaskKeyEntry* keyEntry = new MaskKeyEntry{maskKey, keyBounds};
254 fKeyLists[index].addToTail(keyEntry);
255 ++fListEntryCount;
256
257 SkASSERTF_RELEASE(fHashEntryCount == fListEntryCount,
258 "=ClipAtlas=: Entry counts don't match after add: %d %d",
259 fHashEntryCount, fListEntryCount);
260
261 return proxy;
262 }
263
264 sk_sp<TextureProxy> ClipAtlasManager::DrawAtlasMgr::addToAtlas(
265 Recorder* recorder,
266 const ClipStack::ElementList* elementsForMask,
267 SkIRect maskDeviceBounds,
268 SkIPoint* outPos,
269 AtlasLocator* locator) {
270 // Render mask.
271 SkISize maskSize = maskDeviceBounds.size();
272 if (maskSize.isEmpty()) {
273 return nullptr;
274 }
275 // Expand to include padding as well (so we clear correctly for inverse clip)
276 maskSize.fWidth += 2*kEntryPadding;
277 maskSize.fHeight += 2*kEntryPadding;
278
279 // Request space in DrawAtlas, including padding
280 DrawAtlas::ErrorCode errorCode = fDrawAtlas->addRect(recorder,
281 maskSize.width(),
282 maskSize.height(),
283 locator);
284 if (errorCode != DrawAtlas::ErrorCode::kSucceeded) {
285 return nullptr;
286 }
287
288 // Rasterize path to the record's pixmap with an inset.
289 std::optional<SkColor> clearColor;
290 if (SkAlpha alpha = initial_alpha_for_elements(*elementsForMask); alpha != 0) {
291 clearColor = alpha << 24;
292 }
293 SkPixmap pixmap = fDrawAtlas->prepForRender(*locator, 0, clearColor);
294
295 auto translate = -maskDeviceBounds.topLeft() + SkIVector{kEntryPadding, kEntryPadding};
296 RasterMaskHelper helper(pixmap, translate);
297 render_elements(&helper, *elementsForMask);
298
299 *outPos = locator->topLeft() + SkIVector{kEntryPadding, kEntryPadding};
300
301 fDrawAtlas->setLastUseToken(*locator,
302 recorder->priv().tokenTracker()->nextFlushToken());
303
304 return fDrawAtlas->getProxies()[locator->pageIndex()];
305 }
306
307 bool ClipAtlasManager::DrawAtlasMgr::recordUploads(DrawContext* dc, Recorder* recorder) {
308 return (fDrawAtlas && !fDrawAtlas->recordUploads(dc, recorder));
309 }
310
311 void ClipAtlasManager::DrawAtlasMgr::evict(PlotLocator plotLocator) {
312 // Remove all entries for this Plot from the MaskCache
313 uint32_t index = fDrawAtlas->getListIndex(plotLocator);
314 MaskKeyList::Iter iter;
315 iter.init(fKeyLists[index], MaskKeyList::Iter::kHead_IterStart);
316 MaskKeyEntry* currKeyEntry;
317 while ((currKeyEntry = iter.get())) {
318 iter.next();
319 MaskHashEntry* currHashEntry = fMaskCache.find(currKeyEntry->fKey);
320 SkASSERT(currHashEntry);
321 MaskHashEntry* prevHashEntry = nullptr;
322 while (currHashEntry) {
323 if (currHashEntry->fBounds == currKeyEntry->fBounds) {
324 // Remove entry from hash list
325 if (prevHashEntry) {
326 prevHashEntry->fNext = currHashEntry->fNext;
327 delete currHashEntry;
328 --fHashEntryCount;
329 } else if (currHashEntry->fNext) {
330 MaskHashEntry* next = currHashEntry->fNext;
331 currHashEntry->fBounds = next->fBounds;
332 currHashEntry->fLocator = next->fLocator;
333 currHashEntry->fNext = next->fNext;
334 delete next;
335 --fHashEntryCount;
336 } else {
337 // Remove hash entry itself
338 fMaskCache.remove(currKeyEntry->fKey);
339 --fHashEntryCount;
340 }
341 break;
342 }
343 prevHashEntry = currHashEntry;
344 currHashEntry = currHashEntry->fNext;
345 }
346
347 fKeyLists[index].remove(currKeyEntry);
348 delete currKeyEntry;
349 --fListEntryCount;
350 SkASSERTF_RELEASE(fHashEntryCount == fListEntryCount,
351 "=ClipAtlas=: Entry counts don't match after delete: %d %d",
352 fHashEntryCount, fListEntryCount);
353 }
354 }
355
356 void ClipAtlasManager::DrawAtlasMgr::evictAll() {
357 fDrawAtlas->evictAllPlots();
358 SkASSERT(fMaskCache.empty());
359 }
360
361 void ClipAtlasManager::DrawAtlasMgr::compact(Recorder* recorder) {
362 auto tokenTracker = recorder->priv().tokenTracker();
363 if (fDrawAtlas) {
364 fDrawAtlas->compact(tokenTracker->nextFlushToken());
365 }
366 }
367
368 void ClipAtlasManager::DrawAtlasMgr::freeGpuResources(Recorder* recorder) {
369 auto tokenTracker = recorder->priv().tokenTracker();
370 if (fDrawAtlas) {
371 fDrawAtlas->freeGpuResources(tokenTracker->nextFlushToken());
372 }
373 }
374
375 } // namespace skgpu::graphite