1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2023 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/AtlasProvider.h"
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/graphite/Recorder.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/ComputePathAtlas.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/DrawContext.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/Log.h"
14*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/RasterPathAtlas.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/RecorderPriv.h"
16*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/RendererProvider.h"
17*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/TextureProxy.h"
18*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/text/TextAtlasManager.h"
19*c8dee2aaSAndroid Build Coastguard Worker
20*c8dee2aaSAndroid Build Coastguard Worker namespace skgpu::graphite {
21*c8dee2aaSAndroid Build Coastguard Worker
QueryPathAtlasSupport(const Caps * caps)22*c8dee2aaSAndroid Build Coastguard Worker AtlasProvider::PathAtlasFlagsBitMask AtlasProvider::QueryPathAtlasSupport(const Caps* caps) {
23*c8dee2aaSAndroid Build Coastguard Worker // The raster-backend path atlas is always supported.
24*c8dee2aaSAndroid Build Coastguard Worker PathAtlasFlagsBitMask flags = PathAtlasFlags::kRaster;
25*c8dee2aaSAndroid Build Coastguard Worker if (RendererProvider::IsVelloRendererSupported(caps)) {
26*c8dee2aaSAndroid Build Coastguard Worker flags |= PathAtlasFlags::kCompute;
27*c8dee2aaSAndroid Build Coastguard Worker }
28*c8dee2aaSAndroid Build Coastguard Worker return flags;
29*c8dee2aaSAndroid Build Coastguard Worker }
30*c8dee2aaSAndroid Build Coastguard Worker
AtlasProvider(Recorder * recorder)31*c8dee2aaSAndroid Build Coastguard Worker AtlasProvider::AtlasProvider(Recorder* recorder)
32*c8dee2aaSAndroid Build Coastguard Worker : fTextAtlasManager(std::make_unique<TextAtlasManager>(recorder))
33*c8dee2aaSAndroid Build Coastguard Worker , fRasterPathAtlas(std::make_unique<RasterPathAtlas>(recorder))
34*c8dee2aaSAndroid Build Coastguard Worker , fPathAtlasFlags(QueryPathAtlasSupport(recorder->priv().caps())) {}
35*c8dee2aaSAndroid Build Coastguard Worker
createComputePathAtlas(Recorder * recorder) const36*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<ComputePathAtlas> AtlasProvider::createComputePathAtlas(Recorder* recorder) const {
37*c8dee2aaSAndroid Build Coastguard Worker if (this->isAvailable(PathAtlasFlags::kCompute)) {
38*c8dee2aaSAndroid Build Coastguard Worker return ComputePathAtlas::CreateDefault(recorder);
39*c8dee2aaSAndroid Build Coastguard Worker }
40*c8dee2aaSAndroid Build Coastguard Worker return nullptr;
41*c8dee2aaSAndroid Build Coastguard Worker }
42*c8dee2aaSAndroid Build Coastguard Worker
getRasterPathAtlas() const43*c8dee2aaSAndroid Build Coastguard Worker RasterPathAtlas* AtlasProvider::getRasterPathAtlas() const {
44*c8dee2aaSAndroid Build Coastguard Worker return fRasterPathAtlas.get();
45*c8dee2aaSAndroid Build Coastguard Worker }
46*c8dee2aaSAndroid Build Coastguard Worker
getAtlasTexture(Recorder * recorder,uint16_t width,uint16_t height,SkColorType colorType,uint16_t identifier,bool requireStorageUsage)47*c8dee2aaSAndroid Build Coastguard Worker sk_sp<TextureProxy> AtlasProvider::getAtlasTexture(Recorder* recorder,
48*c8dee2aaSAndroid Build Coastguard Worker uint16_t width,
49*c8dee2aaSAndroid Build Coastguard Worker uint16_t height,
50*c8dee2aaSAndroid Build Coastguard Worker SkColorType colorType,
51*c8dee2aaSAndroid Build Coastguard Worker uint16_t identifier,
52*c8dee2aaSAndroid Build Coastguard Worker bool requireStorageUsage) {
53*c8dee2aaSAndroid Build Coastguard Worker uint64_t key = static_cast<uint64_t>(width) << 48 |
54*c8dee2aaSAndroid Build Coastguard Worker static_cast<uint64_t>(height) << 32 |
55*c8dee2aaSAndroid Build Coastguard Worker static_cast<uint64_t>(colorType) << 16 |
56*c8dee2aaSAndroid Build Coastguard Worker static_cast<uint64_t>(identifier);
57*c8dee2aaSAndroid Build Coastguard Worker auto iter = fTexturePool.find(key);
58*c8dee2aaSAndroid Build Coastguard Worker if (iter != fTexturePool.end()) {
59*c8dee2aaSAndroid Build Coastguard Worker return iter->second;
60*c8dee2aaSAndroid Build Coastguard Worker }
61*c8dee2aaSAndroid Build Coastguard Worker
62*c8dee2aaSAndroid Build Coastguard Worker // We currently only make the distinction between a storage texture (written by a
63*c8dee2aaSAndroid Build Coastguard Worker // compute pass) and a plain sampleable texture (written via upload) that won't be
64*c8dee2aaSAndroid Build Coastguard Worker // used as a render attachment.
65*c8dee2aaSAndroid Build Coastguard Worker const Caps* caps = recorder->priv().caps();
66*c8dee2aaSAndroid Build Coastguard Worker auto textureInfo = requireStorageUsage
67*c8dee2aaSAndroid Build Coastguard Worker ? caps->getDefaultStorageTextureInfo(colorType)
68*c8dee2aaSAndroid Build Coastguard Worker : caps->getDefaultSampledTextureInfo(colorType,
69*c8dee2aaSAndroid Build Coastguard Worker Mipmapped::kNo,
70*c8dee2aaSAndroid Build Coastguard Worker recorder->priv().isProtected(),
71*c8dee2aaSAndroid Build Coastguard Worker Renderable::kNo);
72*c8dee2aaSAndroid Build Coastguard Worker sk_sp<TextureProxy> proxy = TextureProxy::Make(caps,
73*c8dee2aaSAndroid Build Coastguard Worker recorder->priv().resourceProvider(),
74*c8dee2aaSAndroid Build Coastguard Worker SkISize::Make((int32_t) width, (int32_t) height),
75*c8dee2aaSAndroid Build Coastguard Worker textureInfo,
76*c8dee2aaSAndroid Build Coastguard Worker "AtlasProviderTexture",
77*c8dee2aaSAndroid Build Coastguard Worker Budgeted::kYes);
78*c8dee2aaSAndroid Build Coastguard Worker if (!proxy) {
79*c8dee2aaSAndroid Build Coastguard Worker return nullptr;
80*c8dee2aaSAndroid Build Coastguard Worker }
81*c8dee2aaSAndroid Build Coastguard Worker
82*c8dee2aaSAndroid Build Coastguard Worker fTexturePool[key] = proxy;
83*c8dee2aaSAndroid Build Coastguard Worker return proxy;
84*c8dee2aaSAndroid Build Coastguard Worker }
85*c8dee2aaSAndroid Build Coastguard Worker
freeGpuResources()86*c8dee2aaSAndroid Build Coastguard Worker void AtlasProvider::freeGpuResources() {
87*c8dee2aaSAndroid Build Coastguard Worker // Only compact the atlases, not fully free the atlases. freeGpuResources() can be called while
88*c8dee2aaSAndroid Build Coastguard Worker // there is pending work on the Recorder that refers to pages. In the event this is called right
89*c8dee2aaSAndroid Build Coastguard Worker // after a snap(), all pages would eligible for cleanup during compaction anyways.
90*c8dee2aaSAndroid Build Coastguard Worker this->compact(/*forceCompact=*/true);
91*c8dee2aaSAndroid Build Coastguard Worker // Release any textures held directly by the provider. These textures are used by transient
92*c8dee2aaSAndroid Build Coastguard Worker // ComputePathAtlases that are reset every time a DrawContext snaps a DrawTask so there is no
93*c8dee2aaSAndroid Build Coastguard Worker // need to reset those atlases explicitly here. Since the AtlasProvider gives out refs to the
94*c8dee2aaSAndroid Build Coastguard Worker // TextureProxies in the pool, it should be safe to clear the pool in the middle of Recording.
95*c8dee2aaSAndroid Build Coastguard Worker // Draws that use the previous TextureProxies will have refs on them.
96*c8dee2aaSAndroid Build Coastguard Worker fTexturePool.clear();
97*c8dee2aaSAndroid Build Coastguard Worker }
98*c8dee2aaSAndroid Build Coastguard Worker
recordUploads(DrawContext * dc)99*c8dee2aaSAndroid Build Coastguard Worker void AtlasProvider::recordUploads(DrawContext* dc) {
100*c8dee2aaSAndroid Build Coastguard Worker if (!fTextAtlasManager->recordUploads(dc)) {
101*c8dee2aaSAndroid Build Coastguard Worker SKGPU_LOG_E("TextAtlasManager uploads have failed -- may see invalid results.");
102*c8dee2aaSAndroid Build Coastguard Worker }
103*c8dee2aaSAndroid Build Coastguard Worker
104*c8dee2aaSAndroid Build Coastguard Worker if (fRasterPathAtlas) {
105*c8dee2aaSAndroid Build Coastguard Worker fRasterPathAtlas->recordUploads(dc);
106*c8dee2aaSAndroid Build Coastguard Worker }
107*c8dee2aaSAndroid Build Coastguard Worker }
108*c8dee2aaSAndroid Build Coastguard Worker
compact(bool forceCompact)109*c8dee2aaSAndroid Build Coastguard Worker void AtlasProvider::compact(bool forceCompact) {
110*c8dee2aaSAndroid Build Coastguard Worker fTextAtlasManager->compact(forceCompact);
111*c8dee2aaSAndroid Build Coastguard Worker if (fRasterPathAtlas) {
112*c8dee2aaSAndroid Build Coastguard Worker fRasterPathAtlas->compact(forceCompact);
113*c8dee2aaSAndroid Build Coastguard Worker }
114*c8dee2aaSAndroid Build Coastguard Worker }
115*c8dee2aaSAndroid Build Coastguard Worker
invalidateAtlases()116*c8dee2aaSAndroid Build Coastguard Worker void AtlasProvider::invalidateAtlases() {
117*c8dee2aaSAndroid Build Coastguard Worker // We must also evict atlases on a failure. The failed tasks can include uploads that the
118*c8dee2aaSAndroid Build Coastguard Worker // atlas was depending on for its caches. Failing to prepare means they will never run so
119*c8dee2aaSAndroid Build Coastguard Worker // future "successful" Recorder snaps would otherwise reference atlas pages that had stale
120*c8dee2aaSAndroid Build Coastguard Worker // contents.
121*c8dee2aaSAndroid Build Coastguard Worker fTextAtlasManager->evictAtlases();
122*c8dee2aaSAndroid Build Coastguard Worker if (fRasterPathAtlas) {
123*c8dee2aaSAndroid Build Coastguard Worker fRasterPathAtlas->evictAtlases();
124*c8dee2aaSAndroid Build Coastguard Worker }
125*c8dee2aaSAndroid Build Coastguard Worker }
126*c8dee2aaSAndroid Build Coastguard Worker
127*c8dee2aaSAndroid Build Coastguard Worker } // namespace skgpu::graphite
128