| 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 |
#ifndef skgpu_graphite_ClipAtlasManager_DEFINED |
| 8 |
#define skgpu_graphite_ClipAtlasManager_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkRect.h" |
| 11 |
#include "include/core/SkRefCnt.h" |
| 12 |
#include "include/private/base/SkTDArray.h" |
| 13 |
#include "src/base/SkTInternalLList.h" |
| 14 |
#include "src/core/SkTHash.h" |
| 15 |
#include "src/gpu/AtlasTypes.h" |
| 16 |
#include "src/gpu/ResourceKey.h" |
| 17 |
#include "src/gpu/graphite/ClipStack.h" |
| 18 |
#include "src/gpu/graphite/DrawAtlas.h" |
| 19 |
|
| 20 |
#include <cstddef> |
| 21 |
#include <cstdint> |
| 22 |
#include <memory> |
| 23 |
#include <string_view> |
| 24 |
|
| 25 |
struct SkIPoint; |
| 26 |
|
| 27 |
namespace skgpu::graphite { |
| 28 |
|
| 29 |
class Caps; |
| 30 |
class DrawContext; |
| 31 |
class Recorder; |
| 32 |
class TextureProxy; |
| 33 |
|
| 34 |
////////////////////////////////////////////////////////////////////////////////////////////////// |
| 35 |
/** The ClipAtlasManager manages the lifetime of and access to rasterized clip masks. |
| 36 |
*/ |
| 37 |
class ClipAtlasManager { |
| 38 |
public: |
| 39 |
ClipAtlasManager(Recorder* recorder); |
| 40 |
~ClipAtlasManager() = default; |
| 41 |
|
| 42 |
sk_sp<TextureProxy> findOrCreateEntry(uint32_t stackRecordID, |
| 43 |
const ClipStack::ElementList*, |
| 44 |
SkIRect maskDeviceBounds, |
| 45 |
SkIPoint* outPos); |
| 46 |
|
| 47 |
bool recordUploads(DrawContext* dc); |
| 48 |
void compact(); |
| 49 |
void freeGpuResources(); |
| 50 |
|
| 51 |
void evictAtlases(); |
| 52 |
|
| 53 |
private: |
| 54 |
// Wrapper class to manage DrawAtlas and associated caching operations |
| 55 |
class DrawAtlasMgr : public AtlasGenerationCounter, public PlotEvictionCallback { |
| 56 |
public: |
| 57 |
DrawAtlasMgr(size_t width, size_t height, |
| 58 |
size_t plotWidth, size_t plotHeight, |
| 59 |
DrawAtlas::UseStorageTextures useStorageTextures, |
| 60 |
std::string_view label, const Caps*); |
| 61 |
|
| 62 |
sk_sp<TextureProxy> findOrCreateEntry(Recorder* recorder, |
| 63 |
const skgpu::UniqueKey&, |
| 64 |
const ClipStack::ElementList*, |
| 65 |
SkIRect maskDeviceBounds, |
| 66 |
SkIRect keyBounds, |
| 67 |
SkIPoint* outPos); |
| 68 |
// Adds to DrawAtlas but not the cache |
| 69 |
sk_sp<TextureProxy> addToAtlas(Recorder* recorder, |
| 70 |
const ClipStack::ElementList*, |
| 71 |
SkIRect maskDeviceBounds, |
| 72 |
SkIPoint* outPos, |
| 73 |
AtlasLocator* locator); |
| 74 |
bool recordUploads(DrawContext*, Recorder*); |
| 75 |
void evict(PlotLocator) override; |
| 76 |
void compact(Recorder*); |
| 77 |
void freeGpuResources(Recorder*); |
| 78 |
|
| 79 |
void evictAll(); |
| 80 |
|
| 81 |
private: |
| 82 |
std::unique_ptr<DrawAtlas> fDrawAtlas; |
| 83 |
|
| 84 |
// Tracks whether a combined clip mask is already in the DrawAtlas and its location |
| 85 |
struct MaskHashEntry { |
| 86 |
SkIRect fBounds; |
| 87 |
AtlasLocator fLocator; |
| 88 |
MaskHashEntry* fNext = nullptr; |
| 89 |
}; |
| 90 |
struct UniqueKeyHash { |
| 91 |
uint32_t operator()(const skgpu::UniqueKey& key) const { return key.hash(); } |
| 92 |
}; |
| 93 |
using MaskCache = skia_private::THashMap<skgpu::UniqueKey, MaskHashEntry, UniqueKeyHash>; |
| 94 |
MaskCache fMaskCache; |
| 95 |
int fHashEntryCount = 0; |
| 96 |
|
| 97 |
// List of stored keys per Plot, used to invalidate cache entries. |
| 98 |
// When a Plot is invalidated via evict(), we'll get its index and Page index from the |
| 99 |
// PlotLocator, index into the fKeyLists array to get the MaskKeyList for that Plot, |
| 100 |
// then iterate through the list and remove entries matching those keys from the MaskCache. |
| 101 |
struct MaskKeyEntry { |
| 102 |
skgpu::UniqueKey fKey; |
| 103 |
SkIRect fBounds; |
| 104 |
SK_DECLARE_INTERNAL_LLIST_INTERFACE(MaskKeyEntry); |
| 105 |
}; |
| 106 |
using MaskKeyList = SkTInternalLList<MaskKeyEntry>; |
| 107 |
SkTDArray<MaskKeyList> fKeyLists; |
| 108 |
int fListEntryCount = 0; |
| 109 |
}; |
| 110 |
|
| 111 |
Recorder* fRecorder; |
| 112 |
// We have two atlas managers, one for clips that can be keyed via the path keys, |
| 113 |
// and a smaller one for those that can only be keyed by the SaveRecord ID. We keep |
| 114 |
// them separate because the SaveRecord keyed clips will be far more transient, i.e., |
| 115 |
// once the SaveRecord is popped they'll never be used again. |
| 116 |
DrawAtlasMgr fPathKeyAtlasMgr; |
| 117 |
DrawAtlasMgr fSaveRecordKeyAtlasMgr; |
| 118 |
}; |
| 119 |
|
| 120 |
} // namespace skgpu::graphite |
| 121 |
|
| 122 |
#endif // skgpu_graphite_ClipAtlasManager_DEFINED |