55#ifndef FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CONTEXT_DYNAMIC_IMPELLER_H_
66#define FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CONTEXT_DYNAMIC_IMPELLER_H_
77
8- #include < mutex>
8+ #include < atomic>
9+ #include < thread>
910
1011#include " flutter/fml/macros.h"
1112#include " flutter/fml/native_library.h"
13+ #include " flutter/fml/synchronization/waitable_event.h"
1214#include " flutter/shell/platform/android/android_context_gl_impeller.h"
1315#include " flutter/shell/platform/android/android_context_vk_impeller.h"
1416#include " flutter/shell/platform/android/context/android_context.h"
@@ -17,9 +19,6 @@ namespace flutter {
1719
1820// / @brief An Impeller Android context that dynamically creates either an
1921// / [AndroidContextGLImpeller] or an [AndroidContextVKImpeller].
20- // /
21- // / The construction of these objects is deferred until [GetImpellerContext] is
22- // / invoked. Up to this point, the reported backend will be kImpellerAutoselect.
2322class AndroidContextDynamicImpeller : public AndroidContext {
2423 public:
2524 explicit AndroidContextDynamicImpeller (
@@ -34,25 +33,77 @@ class AndroidContextDynamicImpeller : public AndroidContext {
3433 // |AndroidContext|
3534 bool IsDynamicSelection () const override { return true ; }
3635
36+ // / @brief Returns the selected rendering backend, blocking if the selection
37+ // / is still in flight.
38+ // /
39+ // / The backend is chosen lazily by [SetupImpellerContext] on the
40+ // / raster thread, which can take 100+ ms while probing for Vulkan.
41+ // / Callers on other threads would otherwise observe
42+ // / kImpellerAutoselect during early startup, so this blocks until the
43+ // / choice is made. Waiting also publishes the raster thread's writes,
44+ // / making it safe to then read [GetImpellerContext], [GetGLContext]
45+ // / and [GetVKContext] without further synchronization.
46+ // /
47+ // / Once setup has completed this is just an uncontended mutex
48+ // / acquisition, so it is not worth avoiding on non-hot paths.
49+ // /
50+ // / @attention Must never be called from the thread that executes
51+ // / [SetupImpellerContext] before that setup has completed, as
52+ // / the signal being waited on could then never be delivered.
53+ // / In practice that is the raster thread, but note it is not
54+ // / always a distinct thread: while a |fml::RasterThreadMerger|
55+ // / lease is active, raster tasks run on the platform thread.
56+ // /
3757 // |AndroidContext|
3858 AndroidRenderingAPI RenderingApi () const override ;
3959
4060 // / @brief Retrieve the GL Context if it was created, or nullptr.
61+ // /
62+ // / Does not block. Only meaningful after [RenderingApi] has returned.
4163 std::shared_ptr<AndroidContextGLImpeller> GetGLContext () const ;
4264
4365 // / @brief Retrieve the VK context if it was created, or nullptr.
66+ // /
67+ // / Does not block. Only meaningful after [RenderingApi] has returned.
4468 std::shared_ptr<AndroidContextVKImpeller> GetVKContext () const ;
4569
4670 // |AndroidContext|
4771 void SetupImpellerContext () override ;
4872
73+ // / @brief Retrieve the Impeller context, or nullptr if setup has not run.
74+ // /
75+ // / Does not block. Callers on threads other than the raster thread
76+ // / should read [RenderingApi] first, which both waits for setup and
77+ // / publishes the writes read here.
78+ // /
79+ // |AndroidContext|
4980 std::shared_ptr<impeller::Context> GetImpellerContext () const override ;
5081
5182 private:
83+ // / @brief Blocks until [SetupImpellerContext] has completed on the raster
84+ // / thread. Returns immediately once it has.
85+ // /
86+ // / Debug builds assert that the caller is not the thread running
87+ // / setup, which would deadlock. Note the assert can only fire once
88+ // / setup has begun; a thread that waits before setup is ever
89+ // / scheduled still hangs undiagnosed.
90+ void WaitForSetup () const ;
91+
5292 const AndroidContext::ContextSettings settings_;
93+ // These are written once on the raster thread by |SetupImpellerContext| and
94+ // only read by other threads after |setup_complete_| has been signalled.
5395 std::shared_ptr<AndroidContextGLImpeller> gl_context_;
5496 std::shared_ptr<AndroidContextVKImpeller> vk_context_;
5597 std::shared_ptr<fml::BasicTaskRunner> io_task_runner_;
98+ // Signalled by |SetupImpellerContext| once a backend has been chosen.
99+ // Mutable so the const accessors above can wait on it.
100+ mutable fml::ManualResetWaitableEvent setup_complete_;
101+ #ifdef FML_DCHECK_IS_ON
102+ // The thread running |SetupImpellerContext|, recorded so |WaitForSetup| can
103+ // assert it is not being called from that same thread. Atomic because it is
104+ // read by waiters concurrently with the setup thread writing it.
105+ std::atomic<std::thread::id> setup_thread_id_;
106+ #endif
56107
57108 FML_DISALLOW_COPY_AND_ASSIGN (AndroidContextDynamicImpeller);
58109};
0 commit comments