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
8 #include "src/gpu/graphite/ClipAtlasManager.h"
9
10 #include "include/gpu/graphite/Recorder.h"
11 #include "src/gpu/graphite/AtlasProvider.h"
12 #include "src/gpu/graphite/RecorderPriv.h"
13 #include "src/gpu/graphite/TextureProxy.h"
14
15 namespace skgpu::graphite {
16
ClipAtlasManager(Recorder * recorder)17 ClipAtlasManager::ClipAtlasManager(Recorder* recorder) : fRecorder(recorder) {
18 static constexpr SkColorType kColorType = kAlpha_8_SkColorType;
19 static constexpr int kWidth = 2048;
20 static constexpr int kHeight = 2048;
21
22 const Caps* caps = recorder->priv().caps();
23 fDrawAtlas = DrawAtlas::Make(kColorType,
24 SkColorTypeBytesPerPixel(kColorType),
25 kWidth, kHeight,
26 /*plotWidth=*/kWidth, /*plotHeight=*/kHeight,
27 /*generationCounter=*/this,
28 caps->allowMultipleAtlasTextures() ?
29 DrawAtlas::AllowMultitexturing::kYes :
30 DrawAtlas::AllowMultitexturing::kNo,
31 DrawAtlas::UseStorageTextures::kNo,
32 /*evictor=*/this,
33 "ClipAtlas");
34 SkASSERT(fDrawAtlas);
35 fKeyLists.resize(fDrawAtlas->numPlots() * fDrawAtlas->maxPages());
36 for (int i = 0; i < fKeyLists.size(); ++i) {
37 fKeyLists[i].reset();
38 }
39 }
40
findClip(const UniqueKey &)41 std::tuple<const TextureProxy*, Rect> ClipAtlasManager::findClip(const UniqueKey&) {
42 // TODO
43 return {nullptr, {}};
44 }
45
addClip(const UniqueKey &,Rect bounds,const ClipStack::ElementList *)46 std::tuple<const TextureProxy*, Rect> ClipAtlasManager::addClip(const UniqueKey&, Rect bounds,
47 const ClipStack::ElementList*) {
48 // TODO
49 return {nullptr, {}};
50 }
51
recordUploads(DrawContext * dc)52 bool ClipAtlasManager::recordUploads(DrawContext* dc) {
53 return (fDrawAtlas && !fDrawAtlas->recordUploads(dc, fRecorder));
54 }
55
56 namespace {
mask_key_list_index(const PlotLocator & locator,const DrawAtlas * drawAtlas)57 uint32_t mask_key_list_index(const PlotLocator& locator, const DrawAtlas* drawAtlas) {
58 return locator.pageIndex() * drawAtlas->numPlots() + locator.plotIndex();
59 }
60 } // namespace
61
evict(PlotLocator plotLocator)62 void ClipAtlasManager::evict(PlotLocator plotLocator) {
63 // Remove all entries for this Plot from the MaskCache
64 uint32_t index = mask_key_list_index(plotLocator, fDrawAtlas.get());
65 MaskKeyList::Iter iter;
66 iter.init(fKeyLists[index], MaskKeyList::Iter::kHead_IterStart);
67 MaskKeyEntry* currEntry;
68 while ((currEntry = iter.get())) {
69 iter.next();
70 fMaskCache.remove(currEntry->fKey);
71 fKeyLists[index].remove(currEntry);
72 delete currEntry;
73 }
74 }
75
evictAll()76 void ClipAtlasManager::evictAll() {
77 fDrawAtlas->evictAllPlots();
78 SkASSERT(fMaskCache.empty());
79 }
80
compact(bool forceCompact)81 void ClipAtlasManager::compact(bool forceCompact) {
82 auto tokenTracker = fRecorder->priv().tokenTracker();
83 if (fDrawAtlas) {
84 fDrawAtlas->compact(tokenTracker->nextFlushToken(), forceCompact);
85 }
86 }
87
88 } // namespace skgpu::graphite
89