| 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 |
#include "src/gpu/graphite/AtlasProvider.h" |
| 8 |
|
| 9 |
#include "include/core/SkSize.h" |
| 10 |
#include "include/gpu/GpuTypes.h" |
| 11 |
#include "include/gpu/graphite/Recorder.h" |
| 12 |
#include "include/gpu/graphite/TextureInfo.h" |
| 13 |
#include "src/gpu/graphite/Caps.h" |
| 14 |
#include "src/gpu/graphite/ClipAtlasManager.h" |
| 15 |
#include "src/gpu/graphite/ComputePathAtlas.h" |
| 16 |
#include "src/gpu/graphite/Log.h" |
| 17 |
#include "src/gpu/graphite/RasterPathAtlas.h" |
| 18 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 19 |
#include "src/gpu/graphite/RendererProvider.h" |
| 20 |
#include "src/gpu/graphite/TextureProxy.h" |
| 21 |
#include "src/gpu/graphite/text/TextAtlasManager.h" |
| 22 |
|
| 23 |
#include <utility> |
| 24 |
|
| 25 |
namespace skgpu::graphite { |
| 26 |
|
| 27 |
static bool use_clip_atlas(const Recorder* recorder) { |
| 28 |
// Currently only the raster atlas strategy utilizes the clip atlas. |
| 29 |
return recorder->priv().rendererProvider()->pathRendererStrategy() == |
| 30 |
PathRendererStrategy::kRasterAtlas; |
| 31 |
} |
| 32 |
|
| 33 |
AtlasProvider::AtlasProvider(Recorder* recorder) |
| 34 |
: fTextAtlasManager(std::make_unique<TextAtlasManager>(recorder)) |
| 35 |
, fRasterPathAtlas(std::make_unique<RasterPathAtlas>(recorder)) |
| 36 |
, fClipAtlasManager(use_clip_atlas(recorder) ? std::make_unique<ClipAtlasManager>(recorder) |
| 37 |
: nullptr) {} |
| 38 |
|
| 39 |
AtlasProvider::~AtlasProvider() = default; |
| 40 |
|
| 41 |
std::unique_ptr<ComputePathAtlas> AtlasProvider::createComputePathAtlas(Recorder* recorder) const { |
| 42 |
if (recorder->priv().caps()->computeSupport()) { |
| 43 |
return ComputePathAtlas::CreateDefault(recorder); |
| 44 |
} |
| 45 |
return nullptr; |
| 46 |
} |
| 47 |
|
| 48 |
RasterPathAtlas* AtlasProvider::getRasterPathAtlas() const { |
| 49 |
return fRasterPathAtlas.get(); |
| 50 |
} |
| 51 |
|
| 52 |
ClipAtlasManager* AtlasProvider::getClipAtlasManager() const { |
| 53 |
return fClipAtlasManager.get(); |
| 54 |
} |
| 55 |
|
| 56 |
sk_sp<TextureProxy> AtlasProvider::getAtlasTexture(Recorder* recorder, |
| 57 |
uint16_t width, |
| 58 |
uint16_t height, |
| 59 |
SkColorType colorType, |
| 60 |
uint16_t identifier, |
| 61 |
bool requireStorageUsage) { |
| 62 |
uint64_t key = static_cast<uint64_t>(width) << 48 | |
| 63 |
static_cast<uint64_t>(height) << 32 | |
| 64 |
static_cast<uint64_t>(colorType) << 16 | |
| 65 |
static_cast<uint64_t>(identifier); |
| 66 |
auto iter = fTexturePool.find(key); |
| 67 |
if (iter != fTexturePool.end()) { |
| 68 |
return iter->second; |
| 69 |
} |
| 70 |
|
| 71 |
// We currently only make the distinction between a storage texture (written by a |
| 72 |
// compute pass) and a plain sampleable texture (written via upload) that won't be |
| 73 |
// used as a render attachment. |
| 74 |
const Caps* caps = recorder->priv().caps(); |
| 75 |
auto textureInfo = requireStorageUsage |
| 76 |
? caps->getDefaultStorageTextureInfo(colorType) |
| 77 |
: caps->getDefaultSampledTextureInfo(colorType, |
| 78 |
Mipmapped::kNo, |
| 79 |
recorder->priv().isProtected(), |
| 80 |
Renderable::kNo); |
| 81 |
sk_sp<TextureProxy> proxy = TextureProxy::Make(caps, |
| 82 |
recorder->priv().resourceProvider(), |
| 83 |
SkISize::Make((int32_t) width, (int32_t) height), |
| 84 |
textureInfo, |
| 85 |
"AtlasProviderTexture", |
| 86 |
Budgeted::kYes); |
| 87 |
if (!proxy) { |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
fTexturePool[key] = proxy; |
| 92 |
return proxy; |
| 93 |
} |
| 94 |
|
| 95 |
void AtlasProvider::freeGpuResources() { |
| 96 |
// Clear out any pages not in use or needed for any pending work on the Recorder. |
| 97 |
// In the event this is called right after a snap(), all pages would be eligible |
| 98 |
// for cleanup anyways. |
| 99 |
fTextAtlasManager->freeGpuResources(); |
| 100 |
if (fRasterPathAtlas) { |
| 101 |
fRasterPathAtlas->freeGpuResources(); |
| 102 |
} |
| 103 |
if (fClipAtlasManager) { |
| 104 |
fClipAtlasManager->freeGpuResources(); |
| 105 |
} |
| 106 |
// Release any textures held directly by the provider. These textures are used by transient |
| 107 |
// ComputePathAtlases that are reset every time a DrawContext snaps a DrawTask so there is no |
| 108 |
// need to reset those atlases explicitly here. Since the AtlasProvider gives out refs to the |
| 109 |
// TextureProxies in the pool, it should be safe to clear the pool in the middle of Recording. |
| 110 |
// Draws that use the previous TextureProxies will have refs on them. |
| 111 |
fTexturePool.clear(); |
| 112 |
} |
| 113 |
|
| 114 |
void AtlasProvider::recordUploads(DrawContext* dc) { |
| 115 |
if (!fTextAtlasManager->recordUploads(dc)) { |
| 116 |
SKGPU_LOG_E("TextAtlasManager uploads have failed -- may see invalid results."); |
| 117 |
} |
| 118 |
|
| 119 |
if (fRasterPathAtlas) { |
| 120 |
fRasterPathAtlas->recordUploads(dc); |
| 121 |
} |
| 122 |
if (fClipAtlasManager) { |
| 123 |
fClipAtlasManager->recordUploads(dc); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void AtlasProvider::compact() { |
| 128 |
fTextAtlasManager->compact(); |
| 129 |
if (fRasterPathAtlas) { |
| 130 |
fRasterPathAtlas->compact(); |
| 131 |
} |
| 132 |
if (fClipAtlasManager) { |
| 133 |
fClipAtlasManager->compact(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void AtlasProvider::invalidateAtlases() { |
| 138 |
// We must also evict atlases on a failure. The failed tasks can include uploads that the |
| 139 |
// atlas was depending on for its caches. Failing to prepare means they will never run so |
| 140 |
// future "successful" Recorder snaps would otherwise reference atlas pages that had stale |
| 141 |
// contents. |
| 142 |
fTextAtlasManager->evictAtlases(); |
| 143 |
if (fRasterPathAtlas) { |
| 144 |
fRasterPathAtlas->evictAtlases(); |
| 145 |
} |
| 146 |
if (fClipAtlasManager) { |
| 147 |
fClipAtlasManager->evictAtlases(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
} // namespace skgpu::graphite |