• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef skgpu_graphite_ClipAtlasManager_DEFINED
9 #define skgpu_graphite_ClipAtlasManager_DEFINED
10 
11 #include "include/gpu/graphite/TextureInfo.h"
12 #include "src/base/SkTInternalLList.h"
13 #include "src/gpu/AtlasTypes.h"
14 #include "src/gpu/ResourceKey.h"
15 #include "src/gpu/graphite/ClipStack_graphite.h"
16 #include "src/gpu/graphite/DrawAtlas.h"
17 #include "src/gpu/graphite/geom/Rect.h"
18 
19 namespace skgpu::graphite {
20 
21 class Recorder;
22 
23 //////////////////////////////////////////////////////////////////////////////////////////////////
24 /** The ClipAtlasManager manages the lifetime of and access to rasterized clip masks.
25  */
26 class ClipAtlasManager : public AtlasGenerationCounter, public PlotEvictionCallback {
27 public:
28     ClipAtlasManager(Recorder* recorder);
29     ~ClipAtlasManager() override = default;
30 
31     std::tuple<const TextureProxy*, Rect> findClip(const UniqueKey&);
32     std::tuple<const TextureProxy*, Rect> addClip(const UniqueKey&, Rect bounds,
33                                                   const ClipStack::ElementList*);
34 
35     bool recordUploads(DrawContext* dc);
36     void evict(PlotLocator) override;
37     void compact(bool forceCompact);
38 
39     void evictAll();
40 
41 private:
42     Recorder* fRecorder;
43     std::unique_ptr<DrawAtlas> fDrawAtlas;
44 
45     // Tracks whether a clip mask is already in the DrawAtlas, and its location in the atlas
46     struct UniqueKeyHash {
operatorUniqueKeyHash47         uint32_t operator()(const skgpu::UniqueKey& key) const { return key.hash(); }
48     };
49     using MaskCache = skia_private::THashMap<skgpu::UniqueKey, AtlasLocator, UniqueKeyHash>;
50     MaskCache fMaskCache;
51 
52     // List of stored keys per Plot, used to invalidate cache entries.
53     // When a Plot is invalidated via evict(), we'll get its index and Page index from the
54     // PlotLocator, index into the fKeyLists array to get the MaskKeyList for that Plot,
55     // then iterate through the list and remove entries matching those keys from the MaskCache.
56     struct MaskKeyEntry {
57         skgpu::UniqueKey fKey;
58         SK_DECLARE_INTERNAL_LLIST_INTERFACE(MaskKeyEntry);
59     };
60     using MaskKeyList = SkTInternalLList<MaskKeyEntry>;
61     SkTDArray<MaskKeyList> fKeyLists;
62 };
63 
64 }  // namespace skgpu::graphite
65 
66 #endif  // skgpu_graphite_ClipAtlasManager_DEFINED
67