| 1 |
/* |
| 2 |
* Copyright 2023 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_AtlasProvider_DEFINED |
| 8 |
#define skgpu_graphite_AtlasProvider_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkRefCnt.h" |
| 11 |
#include "include/private/base/SkTo.h" |
| 12 |
#include "src/base/SkEnumBitMask.h" |
| 13 |
|
| 14 |
#include <cstdint> |
| 15 |
#include <memory> |
| 16 |
#include <unordered_map> |
| 17 |
|
| 18 |
enum SkColorType : int; |
| 19 |
|
| 20 |
namespace skgpu::graphite { |
| 21 |
|
| 22 |
class Caps; |
| 23 |
class ClipAtlasManager; |
| 24 |
class ComputePathAtlas; |
| 25 |
class DrawContext; |
| 26 |
class RasterPathAtlas; |
| 27 |
class Recorder; |
| 28 |
class TextAtlasManager; |
| 29 |
class TextureProxy; |
| 30 |
|
| 31 |
/** |
| 32 |
* AtlasProvider groups various texture atlas management algorithms together. |
| 33 |
*/ |
| 34 |
class AtlasProvider final { |
| 35 |
public: |
| 36 |
explicit AtlasProvider(Recorder*); |
| 37 |
~AtlasProvider(); |
| 38 |
|
| 39 |
// Returns the TextAtlasManager that provides access to persistent DrawAtlas instances used in |
| 40 |
// glyph rendering. This TextAtlasManager is always available. |
| 41 |
TextAtlasManager* textAtlasManager() const { return fTextAtlasManager.get(); } |
| 42 |
|
| 43 |
// Creates a new transient atlas handler that uses compute shaders to rasterize coverage masks |
| 44 |
// for path rendering. This method returns nullptr if compute shaders are not supported by the |
| 45 |
// owning Recorder's context. |
| 46 |
std::unique_ptr<ComputePathAtlas> createComputePathAtlas(Recorder* recorder) const; |
| 47 |
|
| 48 |
// Gets the atlas handler that uses the CPU raster pipeline to create coverage masks |
| 49 |
// for path rendering. |
| 50 |
RasterPathAtlas* getRasterPathAtlas() const; |
| 51 |
|
| 52 |
// Gets the atlas handler that uses the CPU raster pipeline to create coverage masks for clips. |
| 53 |
// If this is non-null, ClipStack should use this to handle clip elements before falling back |
| 54 |
// to depth-only rasterization. |
| 55 |
ClipAtlasManager* getClipAtlasManager() const; |
| 56 |
|
| 57 |
// Return a TextureProxy with the given dimensions and color type. |
| 58 |
sk_sp<TextureProxy> getAtlasTexture( |
| 59 |
Recorder*, uint16_t width, uint16_t height, SkColorType, uint16_t identifier, |
| 60 |
bool requireStorageUsage); |
| 61 |
|
| 62 |
// This frees textures held in the atlas pool, and compacts the pages within the other |
| 63 |
// atlas managers. It does not free resources that are in use or clear cached masks. |
| 64 |
void freeGpuResources(); |
| 65 |
|
| 66 |
// Push any pending uploads to atlases onto the draw context |
| 67 |
void recordUploads(DrawContext*); |
| 68 |
|
| 69 |
// Handle any post-flush work (garbage collection) |
| 70 |
void compact(); |
| 71 |
|
| 72 |
// Invalidate any cached state about what may or may not already be uploaded in the atlas. |
| 73 |
void invalidateAtlases(); |
| 74 |
|
| 75 |
private: |
| 76 |
std::unique_ptr<TextAtlasManager> fTextAtlasManager; |
| 77 |
|
| 78 |
// Accumulates atlas coverage masks generated by software rendering that are required by one or |
| 79 |
// more entries in `fPendingDraws`. During the snapUploadTask step, prior to pending draws |
| 80 |
// being snapped into a new DrawPass, any necessary uploads into an atlas texture are recorded |
| 81 |
// for the accumulated masks. |
| 82 |
// |
| 83 |
// TODO: We may need a method to generate raster-generated masks in separate threads prior to |
| 84 |
// upload. |
| 85 |
std::unique_ptr<RasterPathAtlas> fRasterPathAtlas; |
| 86 |
|
| 87 |
// Accumulates atlas coverage masks corresponding to an ElementList in the ClipStack. |
| 88 |
std::unique_ptr<ClipAtlasManager> fClipAtlasManager; |
| 89 |
|
| 90 |
// Allocated and cached texture proxies shared by all PathAtlas instances. It is possible for |
| 91 |
// the same texture to be bound to multiple DispatchGroups and DrawPasses across flushes. The |
| 92 |
// owning Recorder must guarantee that any uploads or compute dispatches are scheduled to remain |
| 93 |
// coherent across flushes. |
| 94 |
// TODO: This requirement might change with a more sophisticated reuse scheme for texture |
| 95 |
// allocations. For now our model is simple: all PathAtlases target the same texture and only |
| 96 |
// one of them will render to the texture during a given command submission. |
| 97 |
std::unordered_map<uint64_t, sk_sp<TextureProxy>> fTexturePool; |
| 98 |
}; |
| 99 |
|
| 100 |
} // namespace skgpu::graphite |
| 101 |
|
| 102 |
#endif // skgpu_graphite_AtlasProvider_DEFINED |