Skip to content

Commit f7b19ef

Browse files
XDevNexGengaaclarkeMairramer
authored
[engine] Release the Skia OpenGL resource context on shutdown (flutter#193592)
The Skia OpenGL embedder leaves its resource context current on the I/O thread during shutdown. EGL defers destruction while a context remains current, which can retain native graphics resources after engine destruction. Implement `EmbedderSurfaceGLSkia::ReleaseResourceContext` through the existing embedder cleanup callback. The shell already invokes this hook on the I/O thread after releasing the last IO manager. The GLES Impeller surface already implements analogous cleanup. Compatibility: for Skia GL embedders that provide `make_resource_current`, this adds a `clear_current` call on the I/O thread when the last I/O manager is released during shutdown, so third-party callbacks that assume raster-thread-only execution need review; `embedder.h` currently gives `clear_current` no thread contract. Add `ShutdownReleasesOpenGLResourceContextOnItsThread`. It wraps the fixture's real graphics callbacks, records the successful resource binding's thread, and verifies that synchronous shutdown clears the context on that thread. The change does not add a public API or alter the embedder ABI. Fixes flutter#193591. Related: [flutter#138893](flutter#138893) identified the same EGL thread-binding mechanism on [December 12, 2023](flutter#138893 (comment)). The reporter closed the issue on July 18, 2024, and it was locked on August 1, 2024; no fixing PR is linked there. The linked focused issue provides a standalone native reproduction of the missing Skia resource-context release at `3829a944235cd8e25142d72302dc89db4cd16934`. ### Validation Windows x64, `host_debug_unopt` (`--unoptimized --no-lto`), source `3829a944235cd8e25142d72302dc89db4cd16934`: - New regression with unchanged production code: failed both expected release assertions (exit 1). - With the two Skia production changes: the same regression passed (exit 0). - Full `embedder_unittests`: 180 passed, 8 skipped, 1 pre-existing disabled test; no failures (exit 0). - Shutdown regression repeated 100 times: all passed (exit 0). - Changed-line clang-format and `git diff --check`: passed. The eight skips are five AOT-only checks in this JIT build and three golden checks restricted to Linux. No test was disabled or skip condition changed by this patch. Other platforms remain for upstream CI. A C++ lifecycle comment documents the cleanup. No website documentation update or Dart data-driven fix is included. Public API signatures and ABI layouts are unchanged; third-party callback thread assumptions are called out for compatibility review. The CLA is verified by the passing `cla/google` check. Earlier downstream evidence, on Flutter `4cf24164269a5ebf0c16a028a00727d0e77bbb05`: stock Windows Skia reached 1,121,796,096 private bytes after 200 window cycles; the repaired build settled at 243,449,856 bytes after 1,000 cycles. These are measurements from that pinned engine, not benchmarks of the upstream revision tested here. The linked issue contains the standalone native reproduction. ## 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- 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 --------- Co-authored-by: gaaclarke <[email protected]> Co-authored-by: Mairramer <[email protected]>
1 parent 03f1d60 commit f7b19ef

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/embedder/embedder_surface_gl_skia.cc‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,17 @@ sk_sp<GrDirectContext> EmbedderSurfaceGLSkia::CreateResourceContext() const {
125125
return nullptr;
126126
}
127127

128+
// |EmbedderSurface|
129+
void EmbedderSurfaceGLSkia::ReleaseResourceContext() const {
130+
if (!valid_ || !gl_dispatch_table_.gl_make_resource_current_callback) {
131+
return;
132+
}
133+
// EGL defers destruction while a context is current. Detach it on the IO
134+
// thread before that thread exits and the embedder destroys its contexts.
135+
if (!gl_dispatch_table_.gl_clear_current_callback()) {
136+
FML_LOG(ERROR)
137+
<< "Could not release the resource context from the IO thread.";
138+
}
139+
}
140+
128141
} // namespace flutter

‎engine/src/flutter/shell/platform/embedder/embedder_surface_gl_skia.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class EmbedderSurfaceGLSkia final : public EmbedderSurface,
5050
// |EmbedderSurface|
5151
sk_sp<GrDirectContext> CreateResourceContext() const override;
5252

53+
// |EmbedderSurface|
54+
void ReleaseResourceContext() const override;
55+
5356
// |GPUSurfaceGLDelegate|
5457
std::unique_ptr<GLContextResult> GLContextMakeCurrent() override;
5558

‎engine/src/flutter/shell/platform/embedder/tests/embedder_gl_unittests.cc‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#define FML_USED_ON_EMBEDDER
77

88
#include <atomic>
9+
#include <mutex>
910
#include <string>
11+
#include <thread>
12+
#include <utility>
1013
#include <vector>
1114

1215
#include <EGL/egl.h>
@@ -58,6 +61,64 @@ TEST_F(EmbedderTest, CanCreateOpenGLRenderingEngine) {
5861
ASSERT_TRUE(engine.is_valid());
5962
}
6063

64+
namespace {
65+
66+
class ResourceReleaseTestContext final : public EmbedderTestContextGL {
67+
public:
68+
explicit ResourceReleaseTestContext(std::string assets_path)
69+
: EmbedderTestContextGL(std::move(assets_path)) {
70+
FlutterOpenGLRendererConfig& config = GetRendererConfig().open_gl;
71+
make_resource_ = config.make_resource_current;
72+
clear_current_ = config.clear_current;
73+
config.make_resource_current = [](void* data) {
74+
auto* self = static_cast<ResourceReleaseTestContext*>(
75+
static_cast<EmbedderTestContext*>(data));
76+
const bool current = self->make_resource_(data);
77+
if (current) {
78+
std::scoped_lock lock(self->mutex_);
79+
self->resource_thread_ = std::this_thread::get_id();
80+
self->resource_current.store(true);
81+
}
82+
return current;
83+
};
84+
config.clear_current = [](void* data) {
85+
auto* self = static_cast<ResourceReleaseTestContext*>(
86+
static_cast<EmbedderTestContext*>(data));
87+
const bool cleared = self->clear_current_(data);
88+
std::scoped_lock lock(self->mutex_);
89+
if (cleared && self->resource_thread_ == std::this_thread::get_id()) {
90+
self->resource_current.store(false);
91+
self->resource_released.store(true);
92+
}
93+
return cleared;
94+
};
95+
}
96+
std::atomic<bool> resource_current = false;
97+
std::atomic<bool> resource_released = false;
98+
99+
private:
100+
BoolCallback make_resource_;
101+
BoolCallback clear_current_;
102+
std::mutex mutex_;
103+
std::thread::id resource_thread_;
104+
};
105+
106+
} // namespace
107+
108+
TEST_F(EmbedderTest, ShutdownReleasesOpenGLResourceContextOnItsThread) {
109+
ResourceReleaseTestContext context(GetFixturesDirectory());
110+
EmbedderConfigBuilder builder(context);
111+
builder.AddCommandLineArgument("--enable-impeller=false");
112+
builder.SetSurface(DlISize(1, 1));
113+
auto engine = builder.LaunchEngine();
114+
ASSERT_TRUE(engine.is_valid());
115+
ASSERT_TRUE(context.resource_current.load());
116+
ASSERT_FALSE(context.resource_released.load());
117+
engine.reset();
118+
EXPECT_TRUE(context.resource_released.load());
119+
EXPECT_FALSE(context.resource_current.load());
120+
}
121+
61122
//------------------------------------------------------------------------------
62123
/// If an incorrectly configured compositor is set on the engine, the engine
63124
/// must fail to launch instead of failing to render a frame at a later point in

0 commit comments

Comments
 (0)