diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 88862fbe398..b338225d79c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -14,11 +14,11 @@ #include "flutter/shell/gpu/gpu_surface_gl.h" #include "flutter/shell/platform/darwin/common/platform_mac.h" #include "flutter/shell/platform/darwin/common/string_conversions.h" -#include "flutter/shell/platform/darwin/ios/framework/Source/flutter_touch_mapper.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/flutter_touch_mapper.h" #include "flutter/shell/platform/darwin/ios/platform_view_ios.h" #include "lib/ftl/functional/make_copyable.h" #include "lib/ftl/time/time_delta.h" @@ -330,10 +330,13 @@ static inline PointerChangeMapperPhase PointerChangePhaseFromUITouchPhase( - (void)updateViewportMetrics { blink::Threads::UI()->PostTask([ - engine = _platformView->engine().GetWeakPtr(), metrics = _viewportMetrics + weak_platform_view = _platformView->GetWeakPtr(), metrics = _viewportMetrics ] { - if (engine.get()) - engine->SetViewportMetrics(metrics); + if (!weak_platform_view) { + return; + } + weak_platform_view->UpdateSurfaceSize(); + weak_platform_view->engine().SetViewportMetrics(metrics); }); } diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h index bea98e8b857..d59530ce713 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.h @@ -38,6 +38,10 @@ class PlatformViewIOS : public PlatformView, public GPUSurfaceGLDelegate { return platform_message_router_; } + ftl::WeakPtr GetWeakPtr(); + + void UpdateSurfaceSize(); + VsyncWaiter* GetVsyncWaiter() override; bool ResourceContextMakeCurrent() override; @@ -64,6 +68,7 @@ class PlatformViewIOS : public PlatformView, public GPUSurfaceGLDelegate { sky::SkyEnginePtr engine_; PlatformMessageRouter platform_message_router_; std::unique_ptr accessibility_bridge_; + ftl::WeakPtrFactory weak_factory_; void SetupAndLoadFromSource(const std::string& main, const std::string& packages, diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm index bfc498b11a9..ce872b8a58d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm @@ -3,16 +3,14 @@ // found in the LICENSE file. #include "flutter/shell/platform/darwin/ios/platform_view_ios.h" - #import #import #import #import - #include - #include "base/mac/scoped_nsautorelease_pool.h" #include "base/trace_event/trace_event.h" +#include "flutter/common/threads.h" #include "flutter/shell/gpu/gpu_rasterizer.h" #include "flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h" #include "lib/ftl/synchronization/waitable_event.h" @@ -170,32 +168,6 @@ class IOSGLContext { GLuint framebuffer() const { return framebuffer_; } - bool MakeCurrent() { - base::mac::ScopedNSAutoreleasePool pool; - - return UpdateStorageSizeIfNecessary() && - [EAGLContext setCurrentContext:context_.get()]; - } - - bool ResourceMakeCurrent() { - base::mac::ScopedNSAutoreleasePool pool; - - return [EAGLContext setCurrentContext:resource_context_.get()]; - } - - private: - base::scoped_nsobject layer_; - base::scoped_nsobject context_; - base::scoped_nsobject resource_context_; - - GLuint framebuffer_; - GLuint colorbuffer_; - GLuint depthbuffer_; - GLuint stencilbuffer_; - GLuint depth_stencil_packed_buffer_; - - GLintSize storage_size_; - bool UpdateStorageSizeIfNecessary() { GLintSize size([layer_.get() bounds].size); @@ -270,12 +242,37 @@ class IOSGLContext { return true; } + bool MakeCurrent() { + base::mac::ScopedNSAutoreleasePool pool; + + return UpdateStorageSizeIfNecessary() && + [EAGLContext setCurrentContext:context_.get()]; + } + + bool ResourceMakeCurrent() { + base::mac::ScopedNSAutoreleasePool pool; + + return [EAGLContext setCurrentContext:resource_context_.get()]; + } + + private: + base::scoped_nsobject layer_; + base::scoped_nsobject context_; + base::scoped_nsobject resource_context_; + GLuint framebuffer_; + GLuint colorbuffer_; + GLuint depthbuffer_; + GLuint stencilbuffer_; + GLuint depth_stencil_packed_buffer_; + GLintSize storage_size_; + FTL_DISALLOW_COPY_AND_ASSIGN(IOSGLContext); }; PlatformViewIOS::PlatformViewIOS(CAEAGLLayer* layer) : PlatformView(std::make_unique()), - context_(std::make_unique(surface_config_, layer)) { + context_(std::make_unique(surface_config_, layer)), + weak_factory_(this) { CreateEngine(); NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, @@ -313,6 +310,18 @@ void PlatformViewIOS::SetupAndLoadFromSource( engine_->RunFromFile(main, packages, assets_directory); } +ftl::WeakPtr PlatformViewIOS::GetWeakPtr() { + return weak_factory_.GetWeakPtr(); +} + +void PlatformViewIOS::UpdateSurfaceSize() { + blink::Threads::Gpu()->PostTask([self = GetWeakPtr()]() { + if (self && self->context_ != nullptr) { + self->context_->UpdateStorageSizeIfNecessary(); + } + }); +} + VsyncWaiter* PlatformViewIOS::GetVsyncWaiter() { if (!vsync_waiter_) vsync_waiter_ = std::make_unique();