Skip to content

Commit 39cd7e6

Browse files
RickyvdBergclaude
andcommitted
[avio] RenderTargetCache complete keys and miss telemetry
RenderTargetConfig keyed offscreens on extent, mip count, MSAA and depth/stencil presence only. A request for another pixel format or storage mode could therefore reuse a cached texture of the wrong format or storage (latent: every current caller uses the defaults, so hit rates do not change). The key now also carries the color format, the color and resolve storage modes, and the depth/stencil format and storage, filled from the request (format from target_pixel_format or the context default). RenderTarget::ToConfig fills the same fields from the textures. The cache becomes its own meter, TRACE/timeline only: - RenderTargetCacheMiss instant per miss: w, h, msaa, format, ds, label and a reason: no_entry, extent_mismatch (an unleased entry differs only in extent), all_leased, aged_out (one of the last 32 keys that aging dropped, i.e. unused for the keep-alive number of frames), disabled; - RenderTargetCache counter when the size changes, with patch 46u's accounting: entries, bytes (nominal) and real_bytes. Nothing is logged. This fixes neither measured churn mechanism (gradient SDF snapshots, per-Canvas aging); it attributes them. Written to be offered upstream on flutter#190613: self-contained, no Avio-only API. Tests (impeller_unittests, RenderTargetCacheResourceTest, no GPU): - KeyDistinguishesPixelFormat (expected red on the base: the F16 request received the cached RGBA8 texture) - KeyDistinguishesStorageModes (expected red on the base) - MissReasonClassification (new API; red as a compile failure on the base; aged_out comes from keep-alive 0 aging) The playground CachesUsedTexturesAcrossFrames* tests are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 926fa76 commit 39cd7e6

7 files changed

Lines changed: 326 additions & 5 deletions

File tree

‎AVIO_PATCHES.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ already ancestors of the selected main target under their original commits.
8282
| 46a | One transient attachment set per key on the single graphics queue | permanent resource-lifecycle correction (restores patch 11's one-entry-per-key pool; the incoming depth/stencil dependency is upstreamable) | partial: flutter/flutter#144617 recycles one onscreen set upstream |
8383
| 46b | A transient set lives while an existing view holds its extent | permanent resource-lifecycle owner (ships only with Avio G2, which keeps a ShellItem's view across extent changes) | none — upstream frees with the swapchain |
8484
| 46u | Report-only render-resource accounting | permanent diagnostics (internal C++ API, no ABI change) | none |
85+
| 47 | RenderTargetCache complete keys and miss telemetry | upstreamable bugfix + diagnostics (offer on flutter/flutter#190613) | open: flutter/flutter#190613 |
8586

8687
Patch #5 also owns the later exact empty-frame and global-request corrections:
8788
global requests may not be consumed by a display-scoped frame; sibling-render,
@@ -710,6 +711,26 @@ report. Regressions: `ContextVKTest.TransientsPoolAccountsNominalAndRealBytes`,
710711
`RenderTargetCacheResourceTest.ReportsExactBytes` (GPU-free: a fake allocator
711712
with page-rounded allocations).
712713

714+
### Patch 47: complete RenderTargetCache keys and miss attribution
715+
716+
`RenderTargetConfig` is the complete identity of an offscreen's textures:
717+
extent, mip count, MSAA, depth/stencil, and now the color format, the color
718+
and resolve storage modes, and the depth/stencil format and storage. Before
719+
this patch a request for a different pixel format or storage mode could
720+
reuse a cached texture of another format; no current Avio caller differs in
721+
those fields, so hit rates are unchanged, but the latent bug is closed.
722+
723+
Every miss emits a `RenderTargetCacheMiss` timeline instant with the request
724+
(`w`, `h`, `msaa`, `format`, `ds`, `label`) and a reason: `no_entry`,
725+
`extent_mismatch` (an unleased entry differs only in extent), `all_leased`
726+
(every entry of the key is leased), `aged_out` (one of the last 32 keys that
727+
aging dropped, unused for the keep-alive number of frames) or `disabled`.
728+
When its size changes the cache traces a `RenderTargetCache` counter with
729+
patch 46u's accounting: `entries`, `bytes` (nominal) and `real_bytes`. All of
730+
it is TRACE/timeline only; nothing is logged. Regressions:
731+
`RenderTargetCacheResourceTest.KeyDistinguishesPixelFormat`,
732+
`KeyDistinguishesStorageModes`, `MissReasonClassification`.
733+
713734
## Known baseline debt
714735

715736
- ~~Generic embedder compositor and platform-view tests required broad

‎avio-verify-patches.sh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ need "the usage report carries created entries and bytes" \
431431
$F/impeller/renderer/render_resource_usage.h 'size_t created_real_bytes'
432432
absent "idle render-resource release (dropped with ABI v7) stays out" \
433433
'ReleaseAvioIdleResources|IdleResourceRelease|kIdleReleaseMinUnused|ReleaseIdle\(|ResourceManagerVK::Flush'
434+
need "RenderTargetCache keys include the pixel format" \
435+
$F/impeller/renderer/render_target.h 'color_format == o\.color_format'
436+
need "RenderTargetCache keys include storage modes" \
437+
$F/impeller/renderer/render_target.h 'color_storage == o\.color_storage'
438+
need "RenderTargetCache attributes every miss" \
439+
$F/impeller/entity/render_target_cache.cc '"RenderTargetCacheMiss"'
440+
need "the RenderTargetCache size counter carries real bytes" \
441+
$F/impeller/entity/render_target_cache.cc '"real_bytes",'
434442
need "Slimpeller low-memory path trims idle Impeller caches" \
435443
$F/shell/common/rasterizer.cc 'TrimIdleResourceCaches'
436444
need "explicit transient profiles bypass environment policy" \

‎engine/src/flutter/impeller/entity/render_target_cache.cc‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
#include "impeller/entity/render_target_cache.h"
66

77
#include <algorithm>
8+
#include <string>
89
#include <vector>
910

11+
#include "flutter/fml/trace_event.h"
1012
#include "impeller/core/formats.h"
13+
#include "impeller/renderer/context.h"
1114
#include "impeller/renderer/render_target.h"
1215

1316
namespace impeller {
@@ -43,8 +46,86 @@ RenderTargetBytes MeasureRenderTarget(const RenderTarget& target) {
4346
return bytes;
4447
}
4548

49+
// Bounds the memory of the aged-out classification.
50+
constexpr size_t kRecentlyDroppedKeys = 32u;
51+
4652
} // namespace
4753

54+
std::string_view RenderTargetCache::MissReasonToString(MissReason reason) {
55+
switch (reason) {
56+
case MissReason::kNoEntry:
57+
return "no_entry";
58+
case MissReason::kExtentMismatch:
59+
return "extent_mismatch";
60+
case MissReason::kAllLeased:
61+
return "all_leased";
62+
case MissReason::kAgedOut:
63+
return "aged_out";
64+
case MissReason::kDisabled:
65+
return "disabled";
66+
}
67+
FML_UNREACHABLE();
68+
}
69+
70+
void RenderTargetCache::RecordMiss(const RenderTargetConfig& config,
71+
std::string_view label) {
72+
MissReason reason = MissReason::kNoEntry;
73+
if (!CacheEnabled()) {
74+
reason = MissReason::kDisabled;
75+
} else {
76+
bool same_key = false;
77+
bool other_extent = false;
78+
for (const RenderTargetData& td : render_target_data_) {
79+
if (td.config == config) {
80+
// A miss with an equal key means every such entry is leased.
81+
same_key = true;
82+
} else if (!td.used_this_frame && td.config.MatchesExceptSize(config)) {
83+
other_extent = true;
84+
}
85+
}
86+
if (same_key) {
87+
reason = MissReason::kAllLeased;
88+
} else if (std::find(recently_dropped_.begin(), recently_dropped_.end(),
89+
config) != recently_dropped_.end()) {
90+
reason = MissReason::kAgedOut;
91+
} else if (other_extent) {
92+
reason = MissReason::kExtentMismatch;
93+
}
94+
}
95+
last_miss_reason_ = reason;
96+
const std::string target =
97+
"w=" + std::to_string(config.size.width) +
98+
" h=" + std::to_string(config.size.height) +
99+
" msaa=" + std::to_string(config.has_msaa) +
100+
" format=" + std::string(PixelFormatToString(config.color_format)) +
101+
" ds=" + std::to_string(config.has_depth_stencil) +
102+
" label=" + std::string(label);
103+
TRACE_EVENT_INSTANT2("impeller", "RenderTargetCacheMiss", "reason",
104+
std::string(MissReasonToString(reason)).c_str(),
105+
"target", target.c_str());
106+
}
107+
108+
void RenderTargetCache::RememberDropped(const RenderTargetConfig& config) {
109+
if (recently_dropped_.size() == kRecentlyDroppedKeys) {
110+
recently_dropped_.erase(recently_dropped_.begin());
111+
}
112+
recently_dropped_.push_back(config);
113+
}
114+
115+
void RenderTargetCache::TraceCacheSize() const {
116+
size_t nominal_bytes = 0u;
117+
size_t real_bytes = 0u;
118+
for (const RenderTargetData& td : render_target_data_) {
119+
nominal_bytes += td.nominal_bytes;
120+
real_bytes += td.real_bytes;
121+
}
122+
FML_TRACE_COUNTER("impeller", "RenderTargetCache",
123+
reinterpret_cast<int64_t>(this), "entries",
124+
static_cast<int64_t>(render_target_data_.size()), "bytes",
125+
static_cast<int64_t>(nominal_bytes), "real_bytes",
126+
static_cast<int64_t>(real_bytes));
127+
}
128+
48129
RenderTargetCache::RenderTargetCache(std::shared_ptr<Allocator> allocator,
49130
uint32_t keep_alive_frame_count)
50131
: RenderTargetAllocator(std::move(allocator)),
@@ -71,9 +152,14 @@ void RenderTargetCache::End() {
71152
retain.push_back(td);
72153
} else {
73154
AccountErased(td);
155+
RememberDropped(td.config);
74156
}
75157
}
158+
const bool changed = retain.size() != render_target_data_.size();
76159
render_target_data_.swap(retain);
160+
if (changed) {
161+
TraceCacheSize();
162+
}
77163
}
78164

79165
void RenderTargetCache::LeaseEntry(RenderTargetData& data) {
@@ -96,6 +182,7 @@ void RenderTargetCache::InsertEntry(const RenderTargetConfig& config,
96182
});
97183
created_entries_++;
98184
SampleLeased();
185+
TraceCacheSize();
99186
}
100187

101188
void RenderTargetCache::AccountErased(const RenderTargetData& data) {
@@ -189,12 +276,20 @@ RenderTarget RenderTargetCache::CreateOffscreen(
189276

190277
FML_DCHECK(existing_color_texture == nullptr &&
191278
existing_depth_stencil_texture == nullptr);
279+
const auto& capabilities = context.GetCapabilities();
192280
auto config = RenderTargetConfig{
193281
.size = size,
194282
.mip_count = static_cast<size_t>(mip_count),
195283
.has_msaa = false,
196284
.has_depth_stencil = stencil_attachment_config.has_value(),
285+
.color_format =
286+
target_pixel_format.value_or(capabilities->GetDefaultColorFormat()),
287+
.color_storage = color_attachment_config.storage_mode,
197288
};
289+
if (stencil_attachment_config.has_value()) {
290+
config.depth_stencil_format = capabilities->GetDefaultDepthStencilFormat();
291+
config.depth_stencil_storage = stencil_attachment_config->storage_mode;
292+
}
198293

199294
if (CacheEnabled()) {
200295
for (RenderTargetData& render_target_data : render_target_data_) {
@@ -213,6 +308,7 @@ RenderTarget RenderTargetCache::CreateOffscreen(
213308
}
214309
}
215310
}
311+
RecordMiss(config, label);
216312
RenderTarget created_target = RenderTargetAllocator::CreateOffscreen(
217313
context, size, mip_count, label, color_attachment_config,
218314
stencil_attachment_config, nullptr, nullptr, target_pixel_format);
@@ -243,12 +339,21 @@ RenderTarget RenderTargetCache::CreateOffscreenMSAA(
243339
FML_DCHECK(existing_color_msaa_texture == nullptr &&
244340
existing_color_resolve_texture == nullptr &&
245341
existing_depth_stencil_texture == nullptr);
342+
const auto& capabilities = context.GetCapabilities();
246343
auto config = RenderTargetConfig{
247344
.size = size,
248345
.mip_count = static_cast<size_t>(mip_count),
249346
.has_msaa = true,
250347
.has_depth_stencil = stencil_attachment_config.has_value(),
348+
.color_format =
349+
target_pixel_format.value_or(capabilities->GetDefaultColorFormat()),
350+
.color_storage = color_attachment_config.storage_mode,
351+
.resolve_storage = color_attachment_config.resolve_storage_mode,
251352
};
353+
if (stencil_attachment_config.has_value()) {
354+
config.depth_stencil_format = capabilities->GetDefaultDepthStencilFormat();
355+
config.depth_stencil_storage = stencil_attachment_config->storage_mode;
356+
}
252357
if (CacheEnabled()) {
253358
for (RenderTargetData& render_target_data : render_target_data_) {
254359
const RenderTargetConfig other_config = render_target_data.config;
@@ -266,6 +371,7 @@ RenderTarget RenderTargetCache::CreateOffscreenMSAA(
266371
}
267372
}
268373
}
374+
RecordMiss(config, label);
269375
RenderTarget created_target = RenderTargetAllocator::CreateOffscreenMSAA(
270376
context, size, mip_count, label, color_attachment_config,
271377
stencil_attachment_config, nullptr, nullptr, nullptr,

‎engine/src/flutter/impeller/entity/render_target_cache.h‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#ifndef FLUTTER_IMPELLER_ENTITY_RENDER_TARGET_CACHE_H_
66
#define FLUTTER_IMPELLER_ENTITY_RENDER_TARGET_CACHE_H_
77

8+
#include <optional>
89
#include <string_view>
10+
#include <vector>
911

1012
#include "impeller/renderer/render_resource_usage.h"
1113
#include "impeller/renderer/render_target.h"
@@ -63,6 +65,29 @@ class RenderTargetCache : public RenderTargetAllocator {
6365
const std::shared_ptr<Texture>& existing_depth_stencil_texture = nullptr,
6466
std::optional<PixelFormat> target_pixel_format = std::nullopt) override;
6567

68+
/// Why a request allocated instead of reusing a cached target. Reported
69+
/// on the timeline as a `RenderTargetCacheMiss` instant.
70+
enum class MissReason {
71+
/// No cached target resembles the request.
72+
kNoEntry,
73+
/// An unleased target differs from the request only in its extent.
74+
kExtentMismatch,
75+
/// Every cached target with this exact key is leased.
76+
kAllLeased,
77+
/// A target with this exact key was recently dropped by aging (unused for
78+
/// the keep-alive number of frames).
79+
kAgedOut,
80+
/// The cache is disabled for this request.
81+
kDisabled,
82+
};
83+
84+
static std::string_view MissReasonToString(MissReason reason);
85+
86+
/// The reason of the latest miss, for tests.
87+
std::optional<MissReason> GetLastMissReasonForTesting() const {
88+
return last_miss_reason_;
89+
}
90+
6691
// |RenderTargetAllocator|
6792
RenderResourceUsage ReportUsage(bool start_new_interval) override;
6893

@@ -96,6 +121,16 @@ class RenderTargetCache : public RenderTargetAllocator {
96121

97122
void SampleLeased();
98123

124+
// Classify and trace a miss for `config`.
125+
void RecordMiss(const RenderTargetConfig& config, std::string_view label);
126+
127+
// Remember a key that aging dropped.
128+
void RememberDropped(const RenderTargetConfig& config);
129+
130+
// Trace the cache's size (entries, nominal and real bytes, as ReportUsage
131+
// accounts them) whenever it changes.
132+
void TraceCacheSize() const;
133+
99134
std::vector<RenderTargetData> render_target_data_;
100135
uint32_t keep_alive_frame_count_;
101136
uint32_t cache_disabled_count_ = 0;
@@ -107,6 +142,9 @@ class RenderTargetCache : public RenderTargetAllocator {
107142
size_t peak_leased_nominal_bytes_ = 0u;
108143
size_t created_entries_ = 0u;
109144
size_t created_erased_real_bytes_ = 0u;
145+
// Keys recently dropped by aging, oldest first.
146+
std::vector<RenderTargetConfig> recently_dropped_;
147+
std::optional<MissReason> last_miss_reason_;
110148

111149
RenderTargetCache(const RenderTargetCache&) = delete;
112150

0 commit comments

Comments
 (0)