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