From fce1c80fa0dbdd8d42b146243d02de881e88b496 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 12 Jun 2015 13:29:53 -0700 Subject: [PATCH] Refactor SkyShell to allow multiple SkyViews This CL separates the view-specific state from the process-global state. The view-specific state now lives on Instance rather than Shell. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/1187503003. --- engine/src/flutter/shell/BUILD.gn | 2 + .../shell/android/platform_view_android.cc | 22 +++++++++- .../shell/android/platform_view_android.h | 11 +++++ .../flutter/shell/ios/platform_view_ios.mm | 4 ++ engine/src/flutter/shell/ios/sky_surface.h | 2 + engine/src/flutter/shell/ios/sky_surface.mm | 18 +++++--- .../flutter/shell/ios/sky_view_controller.mm | 6 ++- engine/src/flutter/shell/platform_view.h | 5 ++- engine/src/flutter/shell/shell.cc | 41 ++++-------------- engine/src/flutter/shell/shell.h | 23 +++++----- engine/src/flutter/shell/shell_view.cc | 43 +++++++++++++++++++ engine/src/flutter/shell/shell_view.h | 40 +++++++++++++++++ engine/src/flutter/shell/ui/engine.cc | 14 +++--- engine/src/flutter/shell/ui/engine.h | 4 +- 14 files changed, 175 insertions(+), 60 deletions(-) create mode 100644 engine/src/flutter/shell/shell_view.cc create mode 100644 engine/src/flutter/shell/shell_view.h diff --git a/engine/src/flutter/shell/BUILD.gn b/engine/src/flutter/shell/BUILD.gn index d1d01adc1d..85fc2dd02e 100644 --- a/engine/src/flutter/shell/BUILD.gn +++ b/engine/src/flutter/shell/BUILD.gn @@ -61,6 +61,8 @@ if (is_android) { "service_provider.h", "shell.cc", "shell.h", + "shell_view.cc", + "shell_view.h", "ui/animator.cc", "ui/animator.h", "ui/engine.cc", diff --git a/engine/src/flutter/shell/android/platform_view_android.cc b/engine/src/flutter/shell/android/platform_view_android.cc index a27246743e..7b47d40d62 100644 --- a/engine/src/flutter/shell/android/platform_view_android.cc +++ b/engine/src/flutter/shell/android/platform_view_android.cc @@ -12,16 +12,19 @@ #include "base/location.h" #include "jni/PlatformViewAndroid_jni.h" #include "sky/shell/shell.h" +#include "sky/shell/shell_view.h" namespace sky { namespace shell { static jlong Attach(JNIEnv* env, jclass clazz, jint viewportObserverHandle) { - PlatformView* view = Shell::Shared().view(); + ShellView* shell_view = new ShellView(Shell::Shared()); + auto view = static_cast(shell_view->view()); + view->SetShellView(make_scoped_ptr(shell_view)); view->ConnectToViewportObserver( mojo::MakeRequest(mojo::ScopedMessagePipeHandle( mojo::MessagePipeHandle(viewportObserverHandle)))); - return reinterpret_cast(view); + return reinterpret_cast(shell_view->view()); } // static @@ -29,6 +32,14 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { return RegisterNativesImpl(env); } +PlatformView* PlatformView::Create(const Config& config) { + return new PlatformViewAndroid(config); +} + +PlatformViewAndroid::PlatformViewAndroid(const Config& config) + : PlatformView(config) { +} + PlatformViewAndroid::~PlatformViewAndroid() { if (window_) ReleaseWindow(); @@ -36,6 +47,8 @@ PlatformViewAndroid::~PlatformViewAndroid() { void PlatformViewAndroid::Detach(JNIEnv* env, jobject obj) { DCHECK(!window_); + shell_view_.reset(); + // Note: |this| has been destroyed at this point. } void PlatformViewAndroid::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) { @@ -56,6 +69,11 @@ void PlatformViewAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) { ReleaseWindow(); } +void PlatformViewAndroid::SetShellView(scoped_ptr shell_view) { + DCHECK(!shell_view_); + shell_view_ = shell_view.Pass(); +} + void PlatformViewAndroid::ReleaseWindow() { ANativeWindow_release(window_); window_ = nullptr; diff --git a/engine/src/flutter/shell/android/platform_view_android.h b/engine/src/flutter/shell/android/platform_view_android.h index 72c8bba079..4f3f908e61 100644 --- a/engine/src/flutter/shell/android/platform_view_android.h +++ b/engine/src/flutter/shell/android/platform_view_android.h @@ -11,10 +11,13 @@ struct ANativeWindow; namespace sky { namespace shell { +class ShellView; class PlatformViewAndroid : public PlatformView { public: static bool Register(JNIEnv* env); + + PlatformViewAndroid(const Config& config); ~PlatformViewAndroid() override; // Called from Java @@ -22,9 +25,17 @@ class PlatformViewAndroid : public PlatformView { void SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface); void SurfaceDestroyed(JNIEnv* env, jobject obj); + void SetShellView(scoped_ptr shell_view); + private: void ReleaseWindow(); + // In principle, the ShellView should own the PlatformView, but because our + // lifetime is controlled by the Android view hierarchy, we flip around the + // ownership and have the shell_view owned by Java. We reset this pointer in + // |Detach|, which will eventually cause |~PlatformViewAndroid|. + scoped_ptr shell_view_; + DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroid); }; diff --git a/engine/src/flutter/shell/ios/platform_view_ios.mm b/engine/src/flutter/shell/ios/platform_view_ios.mm index 7ef5d16657..492527733c 100644 --- a/engine/src/flutter/shell/ios/platform_view_ios.mm +++ b/engine/src/flutter/shell/ios/platform_view_ios.mm @@ -7,6 +7,10 @@ namespace sky { namespace shell { +PlatformView* PlatformView::Create(const Config& config) { + return new PlatformViewIOS(config); +} + void PlatformViewIOS::SurfaceCreated(gfx::AcceleratedWidget widget) { DCHECK(window_ == 0); window_ = widget; diff --git a/engine/src/flutter/shell/ios/sky_surface.h b/engine/src/flutter/shell/ios/sky_surface.h index bb65f2d859..e0eb4bb369 100644 --- a/engine/src/flutter/shell/ios/sky_surface.h +++ b/engine/src/flutter/shell/ios/sky_surface.h @@ -6,4 +6,6 @@ @interface SkySurface : UIView +-(instancetype) initWithShellView:(sky::shell::ShellView *) shellView; + @end diff --git a/engine/src/flutter/shell/ios/sky_surface.mm b/engine/src/flutter/shell/ios/sky_surface.mm index d8056de84b..e6629ed8b2 100644 --- a/engine/src/flutter/shell/ios/sky_surface.mm +++ b/engine/src/flutter/shell/ios/sky_surface.mm @@ -8,9 +8,10 @@ #import #import -#include "sky/shell/ui_delegate.h" -#include "sky/shell/shell.h" #include "sky/shell/ios/platform_view_ios.h" +#include "sky/shell/shell.h" +#include "sky/shell/shell_view.h" +#include "sky/shell/ui_delegate.h" #include "mojo/public/cpp/bindings/interface_request.h" #include "sky/services/viewport/input_event.mojom.h" @@ -38,6 +39,14 @@ static inline sky::EventType EventTypeFromUITouchPhase(UITouchPhase phase) { BOOL _platformViewInitialized; sky::ViewportObserverPtr _viewport_observer; + scoped_ptr _shell_view; +} + +-(instancetype) initWithShellView:(sky::shell::ShellView *) shellView { + self = [super init]; + if (self) + _shell_view.reset(shellView); + return self; } - (gfx::AcceleratedWidget)acceleratedWidget { @@ -80,8 +89,7 @@ static inline sky::EventType EventTypeFromUITouchPhase(UITouchPhase phase) { } - (sky::shell::PlatformViewIOS*)platformView { - auto view = static_cast( - sky::shell::Shell::Shared().view()); + auto view = static_cast(_shell_view->view()); DCHECK(view); return view; } @@ -95,7 +103,7 @@ static inline sky::EventType EventTypeFromUITouchPhase(UITouchPhase phase) { } - (void)connectToViewportObserverAndLoad { - auto view = sky::shell::Shell::Shared().view(); + auto view = static_cast(_shell_view->view()); auto interface_request = mojo::GetProxy(&_viewport_observer); view->ConnectToViewportObserver(interface_request.Pass()); diff --git a/engine/src/flutter/shell/ios/sky_view_controller.mm b/engine/src/flutter/shell/ios/sky_view_controller.mm index ee2e38ad76..24bfef1c06 100644 --- a/engine/src/flutter/shell/ios/sky_view_controller.mm +++ b/engine/src/flutter/shell/ios/sky_view_controller.mm @@ -8,10 +8,14 @@ #import #import +#include "sky/shell/shell_view.h" + @implementation SkyViewController - (void)loadView { - SkySurface* surface = [[SkySurface alloc] init]; + + ShellView* shell_view = new sky::shell::ShellView(sky::shell::Shell::Shared()); + SkySurface* surface = [[SkySurface alloc] initWithShellView: shell_view]; surface.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; diff --git a/engine/src/flutter/shell/platform_view.h b/engine/src/flutter/shell/platform_view.h index 59239b90d4..4fb3618cc5 100644 --- a/engine/src/flutter/shell/platform_view.h +++ b/engine/src/flutter/shell/platform_view.h @@ -19,13 +19,16 @@ class PlatformView { scoped_refptr ui_task_runner; }; - explicit PlatformView(const Config& config); + static PlatformView* Create(const Config& config); + virtual ~PlatformView(); void ConnectToViewportObserver( mojo::InterfaceRequest request); protected: + explicit PlatformView(const Config& config); + void SurfaceWasCreated(); void SurfaceWasDestroyed(); diff --git a/engine/src/flutter/shell/shell.cc b/engine/src/flutter/shell/shell.cc index 00f24e27d8..33ad2dfcdb 100644 --- a/engine/src/flutter/shell/shell.cc +++ b/engine/src/flutter/shell/shell.cc @@ -9,8 +9,6 @@ #include "mojo/common/message_pump_mojo.h" #include "mojo/edk/embedder/embedder.h" #include "mojo/edk/embedder/simple_platform_support.h" -#include "sky/shell/platform_view.h" -#include "sky/shell/gpu/rasterizer.h" #include "sky/shell/ui/engine.h" namespace sky { @@ -34,9 +32,14 @@ Shell::Shell(scoped_ptr service_provider_context) base::Thread::Options options; options.message_pump_factory = base::Bind(&CreateMessagePumpMojo); - InitGPU(options); - InitUI(options); - InitView(); + gpu_thread_.reset(new base::Thread("gpu_thread")); + gpu_thread_->StartWithOptions(options); + + ui_thread_.reset(new base::Thread("ui_thread")); + ui_thread_->StartWithOptions(options); + + ui_task_runner()->PostTask( + FROM_HERE, base::Bind(&Engine::Init, service_provider_context_.get())); } Shell::~Shell() { @@ -51,33 +54,5 @@ Shell& Shell::Shared() { return *g_shell; } -void Shell::InitGPU(const base::Thread::Options& options) { - gpu_thread_.reset(new base::Thread("gpu_thread")); - gpu_thread_->StartWithOptions(options); - - rasterizer_.reset(new Rasterizer()); -} - -void Shell::InitUI(const base::Thread::Options& options) { - ui_thread_.reset(new base::Thread("ui_thread")); - ui_thread_->StartWithOptions(options); - - Engine::Config config; - config.service_provider_context = service_provider_context_.get(); - config.gpu_task_runner = gpu_thread_->message_loop()->task_runner(); - config.gpu_delegate = rasterizer_->GetWeakPtr(); - engine_.reset(new Engine(config)); - - ui_thread_->message_loop()->PostTask( - FROM_HERE, base::Bind(&Engine::Init, engine_->GetWeakPtr())); -} - -void Shell::InitView() { - PlatformView::Config config; - config.ui_task_runner = ui_thread_->message_loop()->task_runner(); - config.ui_delegate = engine_->GetWeakPtr(); - view_.reset(new PlatformView(config)); -} - } // namespace shell } // namespace sky diff --git a/engine/src/flutter/shell/shell.h b/engine/src/flutter/shell/shell.h index 558468d17e..36004da883 100644 --- a/engine/src/flutter/shell/shell.h +++ b/engine/src/flutter/shell/shell.h @@ -8,13 +8,12 @@ #include "base/macros.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include "base/message_loop/message_loop.h" #include "base/threading/thread.h" +#include "sky/shell/service_provider.h" namespace sky { namespace shell { -class Engine; -class PlatformView; -class Rasterizer; class ServiceProviderContext; class Shell { @@ -25,22 +24,26 @@ class Shell { scoped_ptr service_provider_context); static Shell& Shared(); - PlatformView* view() const { return view_.get(); } + scoped_refptr gpu_task_runner() const { + return gpu_thread_->message_loop()->task_runner(); + } + + scoped_refptr ui_task_runner() const { + return ui_thread_->message_loop()->task_runner(); + } + + ServiceProviderContext* service_provider_context() const { + return service_provider_context_.get(); + } private: explicit Shell(scoped_ptr service_provider_context); void InitGPU(const base::Thread::Options& options); void InitUI(const base::Thread::Options& options); - void InitView(); scoped_ptr gpu_thread_; scoped_ptr ui_thread_; - - scoped_ptr view_; - scoped_ptr rasterizer_; - scoped_ptr engine_; - scoped_ptr service_provider_context_; DISALLOW_COPY_AND_ASSIGN(Shell); diff --git a/engine/src/flutter/shell/shell_view.cc b/engine/src/flutter/shell/shell_view.cc new file mode 100644 index 0000000000..bbcd069a3a --- /dev/null +++ b/engine/src/flutter/shell/shell_view.cc @@ -0,0 +1,43 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/shell/shell_view.h" + +#include "base/bind.h" +#include "base/single_thread_task_runner.h" +#include "sky/shell/gpu/rasterizer.h" +#include "sky/shell/platform_view.h" +#include "sky/shell/shell.h" +#include "sky/shell/ui/engine.h" + +namespace sky { +namespace shell { + +ShellView::ShellView(Shell& shell) + : shell_(shell) { + rasterizer_.reset(new Rasterizer()); + CreateEngine(); + CreatePlatformView(); +} + +ShellView::~ShellView() { +} + +void ShellView::CreateEngine() { + Engine::Config config; + config.service_provider_context = shell_.service_provider_context(); + config.gpu_task_runner = shell_.gpu_task_runner(); + config.gpu_delegate = rasterizer_->GetWeakPtr(); + engine_.reset(new Engine(config)); +} + +void ShellView::CreatePlatformView() { + PlatformView::Config config; + config.ui_task_runner = shell_.ui_task_runner(); + config.ui_delegate = engine_->GetWeakPtr(); + view_.reset(PlatformView::Create(config)); +} + +} // namespace shell +} // namespace sky diff --git a/engine/src/flutter/shell/shell_view.h b/engine/src/flutter/shell/shell_view.h new file mode 100644 index 0000000000..221406fdd1 --- /dev/null +++ b/engine/src/flutter/shell/shell_view.h @@ -0,0 +1,40 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_SHELL_SHELL_VIEW_H_ +#define SKY_SHELL_SHELL_VIEW_H_ + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" + +namespace sky { +namespace shell { +class Engine; +class PlatformView; +class Rasterizer; +class Shell; + +class ShellView { + public: + explicit ShellView(Shell& shell); + ~ShellView(); + + PlatformView* view() const { return view_.get(); } + + private: + void CreateEngine(); + void CreatePlatformView(); + + Shell& shell_; + scoped_ptr view_; + scoped_ptr rasterizer_; + scoped_ptr engine_; + + DISALLOW_COPY_AND_ASSIGN(ShellView); +}; + +} // namespace shell +} // namespace sky + +#endif // SKY_SHELL_SHELL_VIEW_H_ diff --git a/engine/src/flutter/shell/ui/engine.cc b/engine/src/flutter/shell/ui/engine.cc index f82922b8fc..944142cb42 100644 --- a/engine/src/flutter/shell/ui/engine.cc +++ b/engine/src/flutter/shell/ui/engine.cc @@ -33,6 +33,8 @@ void ConfigureSettings(blink::WebSettings* settings) { settings->setLoadsImagesAutomatically(true); } +PlatformImpl* g_platform_impl = nullptr; + } Engine::Engine(const Config& config) @@ -53,15 +55,17 @@ base::WeakPtr Engine::GetWeakPtr() { return weak_factory_.GetWeakPtr(); } -void Engine::Init() { +void Engine::Init(ServiceProviderContext* service_provider_context) { TRACE_EVENT0("sky", "Engine::Init"); - service_provider_ = CreateServiceProvider(config_.service_provider_context); + mojo::ServiceProviderPtr service_provider = + CreateServiceProvider(service_provider_context); mojo::NetworkServicePtr network_service; - mojo::ConnectToService(service_provider_.get(), &network_service); - platform_impl_.reset(new PlatformImpl(network_service.Pass())); + mojo::ConnectToService(service_provider.get(), &network_service); - blink::initialize(platform_impl_.get()); + DCHECK(!g_platform_impl); + g_platform_impl = new PlatformImpl(network_service.Pass()); + blink::initialize(g_platform_impl); } void Engine::BeginFrame(base::TimeTicks frame_time) { diff --git a/engine/src/flutter/shell/ui/engine.h b/engine/src/flutter/shell/ui/engine.h index 014028b03f..d23b8eb809 100644 --- a/engine/src/flutter/shell/ui/engine.h +++ b/engine/src/flutter/shell/ui/engine.h @@ -50,7 +50,7 @@ class Engine : public UIDelegate, base::WeakPtr GetWeakPtr(); - void Init(); + static void Init(ServiceProviderContext* service_provider_context); void BeginFrame(base::TimeTicks frame_time); skia::RefPtr Paint(); @@ -96,8 +96,6 @@ class Engine : public UIDelegate, void UpdateWebViewSize(); Config config_; - mojo::ServiceProviderPtr service_provider_; - scoped_ptr platform_impl_; scoped_ptr animator_; std::unique_ptr sky_view_;