[Impeller] Share OpenGL context between playground unittests#187690
[Impeller] Share OpenGL context between playground unittests#187690flar wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a shared OpenGL ES playground context across tests to improve performance and resource management, and adds a ContentContextHolder to manage the lifecycle and cleanup of ContentContext resources. Feedback on these changes highlights a critical issue where the shared OpenGL context is prematurely shut down after the first test, which can be resolved by conditionally calling Shutdown() only when the context is owned. Additionally, the reviewer recommends removing leftover debugging logs and commented-out code, and warns against using static cleanup closures for context shutdown due to potential exit-time crashes from undefined static destruction order.
| static fml::ScopedCleanupClosure context_cleanup( | ||
| [&] { (*opengl_playground)->GetContext()->Shutdown(); }); |
There was a problem hiding this comment.
Using a static fml::ScopedCleanupClosure to call Shutdown() on the shared context at program exit is highly risky.
During static destruction (after main returns), the order of destruction of global/static variables across different translation units is undefined. If GLFW or the graphics driver has already been terminated or unloaded, calling Shutdown() here can cause a segmentation fault or crash on exit.
Since absl::NoDestructor is used specifically to prevent exit-time destruction issues, we should avoid manually triggering the shutdown of GPU resources at exit. The OS will reclaim all resources when the process terminates.
There was a problem hiding this comment.
This is the same approach used in the golden tests.
|
The cost of running a full Before sharing GL contextsAfter sharing GL contexts |
…ttests-share-GL-context
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces lifetime management improvements for ContentContext in Impeller's playground tests, flushes command buffers across various unit tests, and refactors GLES backend initialization in the playground using absl::NoDestructor. A critical issue was identified in playground.cc where missing break statements in the GLES backend switch cases cause an unintended fallthrough bug.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces lifetime management improvements for Impeller's playground and testing contexts, notably adding a ContentContextHolder to clean up transient buffers and caches, and flushing command buffers in various unit tests. It also refactors playground setup to use static instances for OpenGL backends. Feedback highlights critical issues in playground.cc where static cleanup closures capture local variables by reference, leading to potential dangling references, undefined behavior, and null pointer dereferences on exit.
| static fml::ScopedCleanupClosure context_cleanup( | ||
| [&] { (*opengl_playground)->GetContext()->Shutdown(); }); |
There was a problem hiding this comment.
Using a static fml::ScopedCleanupClosure with a lambda capturing by reference ([&]) introduces two major issues:
- Dangling References / Undefined Behavior: The lambda captures
thisand other local variables (likeswitches) by reference. Since these objects are destroyed long before the static destructor runs at program exit, executing this lambda results in undefined behavior. - Null Pointer Dereference: If
PlaygroundImpl::Createfails and returnsnullptr,*opengl_playgroundwill benullptr. Dereferencing it during cleanup will cause a crash on exit.
To fix this, use an empty capture list [] (since opengl_playground is static and doesn't need to be captured) and add a null check before dereferencing.
| static fml::ScopedCleanupClosure context_cleanup( | |
| [&] { (*opengl_playground)->GetContext()->Shutdown(); }); | |
| static fml::ScopedCleanupClosure context_cleanup( | |
| [] { | |
| if (*opengl_playground) { | |
| (*opengl_playground)->GetContext()->Shutdown(); | |
| } | |
| }); |
References
- The Google C++ Style Guide discourages static variables with non-trivial destructors because the order of destruction is undefined and can lead to crashes on exit. Capturing local variables by reference in a static lambda exacerbates this by creating dangling references. (link)
| static fml::ScopedCleanupClosure context_cleanup( | ||
| [&] { (*opengl_playground)->GetContext()->Shutdown(); }); |
There was a problem hiding this comment.
Using a static fml::ScopedCleanupClosure with a lambda capturing by reference ([&]) introduces two major issues:
- Dangling References / Undefined Behavior: The lambda captures
thisand other local variables (likeswitches) by reference. Since these objects are destroyed long before the static destructor runs at program exit, executing this lambda results in undefined behavior. - Null Pointer Dereference: If
PlaygroundImpl::Createfails and returnsnullptr,*opengl_playgroundwill benullptr. Dereferencing it during cleanup will cause a crash on exit.
To fix this, use an empty capture list [] (since opengl_playground is static and doesn't need to be captured) and add a null check before dereferencing.
static fml::ScopedCleanupClosure context_cleanup(
[] {
if (*opengl_playground) {
(*opengl_playground)->GetContext()->Shutdown();
}
});References
- The Google C++ Style Guide discourages static variables with non-trivial destructors because the order of destruction is undefined and can lead to crashes on exit. Capturing local variables by reference in a static lambda exacerbates this by creating dangling references. (link)
|
This PR is nearly ready to go except for one minor issue that I can't find a workaround for. Just running the unit tests, as in But, running a bunch of unit tests with the playground, as in |
…lutter#188080) Currently running all of the impeller unit tests can take 25 minutes most all of which is caused by recreating an OpenGL context. While flutter#187690 attempts to share that context among all playground implementations to amortize that cost, it is running into issues with interactions between the reused context and the ImGui code. In order to further focus the sharing, this PR attempts to share just the ContextGLES and the ReactorWorker between instantiations of the PlaygroundImplGES. Fixes flutter#188649 ## 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 documentation (doc comments with `///`). - [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. --------- Co-authored-by: Benson Luk <[email protected]>
|
Replaced by #188080 |
A full run of the Impeller unit tests takes 25 minutes - the vast majority of which is constructing a new OpenGL context on each playground unittest. This PR shares an OpenGL context amongst all of the OpenGL playground tests and reduces the runtime of the impeller unit tests from 25 minutes down to 2:20 minutes.
Pre-launch Checklist
///).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. Comments from the
gemini-code-assistbot 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.