Skip to content

Commit 07b47f8

Browse files
andywolffsuryaKommana2662
authored andcommitted
[Impeller] Add render target cache clearing and test hook (flutter#193497)
Fixes flutter#193015 Part of flutter#192992 During testing, `RenderTargetCache` retained allocated offscreen render targets across consecutive test cases within the same process. When earlier tests performed offscreen drawing or backdrop filtering passes, dirty textures remained in the cache and were recycled into subsequent test passes, leading to test order flakiness and non-deterministic golden snapshots (flutter#192992). To address both production memory pressure handling (flutter#193015) and test isolation, I added cache clearing methods across the Impeller rendering pipeline. In the engine, `RenderTargetAllocator` and `RenderTargetCache` now provide a `Clear()` method that drops all cached texture allocations. I wired this cleanup through `ContentContext`, `AiksContext`, `Rasterizer`, `Engine`, and `Shell`. In production, the rasterizer now purges the render target cache when receiving low-memory warning signals (`FlutterEngineNotifyLowMemoryWarning` / `Rasterizer::NotifyLowMemoryWarning`). For tests, I exposed a general `PlatformDispatcher.resetInternalState()` hook through native platform configuration bindings. This mirrors `ServicesBinding.resetInternalState()` and will allow test harnesses to purge cached offscreen render targets between tests to prevent texture leakage across test boundaries while keeping the engine test interface extensible. To prevent merge queue and postsubmit analyzer failures against the baseline prebuilt `sky_engine`, the framework test wiring (`TestWidgetsFlutterBinding.postTest` and `TestPlatformDispatcher.resetInternalState()`) is deferred to a follow-up PR once this engine commit rolls. I also added C++ unit tests in `RasterizerTest`, `ShellTest`, `EngineTest`, and `RuntimeControllerTest`. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 1fa7573 commit 07b47f8

27 files changed

Lines changed: 249 additions & 5 deletions

‎engine/src/flutter/impeller/display_list/aiks_context.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ ContentContext& AiksContext::GetContentContext() const {
4343
return *content_context_;
4444
}
4545

46+
void AiksContext::ClearRenderTargetCache() const {
47+
if (content_context_) {
48+
content_context_->ClearRenderTargetCache();
49+
}
50+
}
51+
52+
void AiksContext::ClearCachedTextures() const {
53+
if (content_context_) {
54+
content_context_->ClearCachedTextures();
55+
}
56+
}
57+
4658
} // namespace impeller

‎engine/src/flutter/impeller/display_list/aiks_context.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ class AiksContext {
3535
std::optional<std::shared_ptr<RenderTargetAllocator>>
3636
render_target_allocator = std::nullopt);
3737

38-
~AiksContext();
38+
virtual ~AiksContext();
3939

4040
bool IsValid() const;
4141

4242
std::shared_ptr<Context> GetContext() const;
4343

4444
ContentContext& GetContentContext() const;
4545

46+
/// @brief Clear all cached render targets from the content context.
47+
virtual void ClearRenderTargetCache() const;
48+
49+
/// @brief Clear all cached DL image textures from the content context.
50+
virtual void ClearCachedTextures() const;
51+
4652
private:
4753
std::shared_ptr<Context> context_;
4854
std::unique_ptr<ContentContext> content_context_;

‎engine/src/flutter/impeller/entity/contents/content_context.cc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,4 +1612,10 @@ void ContentContext::ClearCachedTextures() const {
16121612
texture_cache_.clear();
16131613
}
16141614

1615+
void ContentContext::ClearRenderTargetCache() const {
1616+
if (render_target_cache_) {
1617+
render_target_cache_->Clear();
1618+
}
1619+
}
1620+
16151621
} // namespace impeller

‎engine/src/flutter/impeller/entity/contents/content_context.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ class ContentContext {
299299
/// @brief Clear all cached textures.
300300
void ClearCachedTextures() const;
301301

302+
/// @brief Clear all cached render targets from the render target cache.
303+
void ClearRenderTargetCache() const;
304+
302305
/// @brief Retrieve the current host buffer for transient storage of indexes
303306
/// used for indexed draws.
304307
///

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ void RenderTargetCache::EnableCache() {
5151
cache_disabled_count_--;
5252
}
5353

54+
void RenderTargetCache::Clear() {
55+
render_target_data_.clear();
56+
}
57+
5458
RenderTarget RenderTargetCache::CreateOffscreen(
5559
const Context& context,
5660
ISize size,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class RenderTargetCache : public RenderTargetAllocator {
3535
// |RenderTargetAllocator|
3636
void EnableCache() override;
3737

38+
// |RenderTargetAllocator|
39+
void Clear() override;
40+
3841
RenderTarget CreateOffscreen(
3942
const Context& context,
4043
ISize size,

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,25 @@ TEST_P(RenderTargetCacheTest, CreateWithEmptySize) {
163163
}
164164
}
165165

166+
TEST_P(RenderTargetCacheTest, ClearDropsAllCachedTextures) {
167+
auto render_target_cache = RenderTargetCache(
168+
GetContext()->GetResourceAllocator(), /*keep_alive_frame_count=*/4);
169+
170+
render_target_cache.Start();
171+
RenderTarget target1 =
172+
render_target_cache.CreateOffscreen(*GetContext(), {100, 100}, 1);
173+
RenderTarget target2 =
174+
render_target_cache.CreateOffscreen(*GetContext(), {200, 200}, 1);
175+
render_target_cache.End();
176+
177+
EXPECT_TRUE(target1.IsValid());
178+
EXPECT_TRUE(target2.IsValid());
179+
EXPECT_EQ(render_target_cache.CachedTextureCount(), 2u);
180+
181+
render_target_cache.Clear();
182+
183+
EXPECT_EQ(render_target_cache.CachedTextureCount(), 0u);
184+
}
185+
166186
} // namespace testing
167187
} // namespace impeller

‎engine/src/flutter/impeller/renderer/render_target.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ class RenderTargetAllocator {
196196
/// This may be used to deallocate any unused textures.
197197
virtual void End();
198198

199+
/// @brief Drop all cached render target textures immediately.
200+
virtual void Clear() {}
201+
199202
private:
200203
std::shared_ptr<Allocator> allocator_;
201204
};

‎engine/src/flutter/lib/ui/dart_ui.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ typedef CanvasPath Path;
104104
V(PlatformConfigurationNativeApi::UpdateSemantics) \
105105
V(PlatformConfigurationNativeApi::SetApplicationLocale) \
106106
V(PlatformConfigurationNativeApi::SetNeedsReportTimings) \
107+
V(PlatformConfigurationNativeApi::ResetInternalState) \
107108
V(PlatformConfigurationNativeApi::SetIsolateDebugName) \
108109
V(PlatformConfigurationNativeApi::SetSemanticsTreeEnabled) \
109110
V(PlatformConfigurationNativeApi::RequestDartPerformanceMode) \

‎engine/src/flutter/lib/ui/platform_dispatcher.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,21 @@ class PlatformDispatcher {
641641
@Native<Void Function(Bool)>(symbol: 'PlatformConfigurationNativeApi::SetNeedsReportTimings')
642642
external static void __nativeSetNeedsReportTimings(bool value);
643643

644+
/// Resets the engine's internal caches.
645+
///
646+
/// This is primarily intended for test harnesses to ensure test isolation
647+
/// and prevent cached offscreen render targets or other transient engine
648+
/// state from leaking across consecutive tests.
649+
///
650+
/// Production applications should not invoke this method directly. Doing so
651+
/// can cause unnecessary texture reallocation churn and frame hitches.
652+
/// Production applications should instead rely on platform low-memory
653+
/// notifications, which automatically purge caches under memory pressure.
654+
void resetInternalState() => _resetInternalState();
655+
656+
@Native<Void Function()>(symbol: 'PlatformConfigurationNativeApi::ResetInternalState')
657+
external static void _resetInternalState();
658+
644659
// Called from the engine, via hooks.dart
645660
void _reportTimings(List<int> timings) {
646661
assert(timings.length % FrameTiming._dataLength == 0);

0 commit comments

Comments
 (0)