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.
This commit is contained in:
Adam Barth
2015-06-12 13:29:53 -07:00
parent 6ae9de09f9
commit fce1c80fa0
14 changed files with 175 additions and 60 deletions

View File

@@ -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",

View File

@@ -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<PlatformViewAndroid*>(shell_view->view());
view->SetShellView(make_scoped_ptr(shell_view));
view->ConnectToViewportObserver(
mojo::MakeRequest<ViewportObserver>(mojo::ScopedMessagePipeHandle(
mojo::MessagePipeHandle(viewportObserverHandle))));
return reinterpret_cast<jlong>(view);
return reinterpret_cast<jlong>(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<ShellView> shell_view) {
DCHECK(!shell_view_);
shell_view_ = shell_view.Pass();
}
void PlatformViewAndroid::ReleaseWindow() {
ANativeWindow_release(window_);
window_ = nullptr;

View File

@@ -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<ShellView> 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<ShellView> shell_view_;
DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroid);
};

View File

@@ -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;

View File

@@ -6,4 +6,6 @@
@interface SkySurface : UIView
-(instancetype) initWithShellView:(sky::shell::ShellView *) shellView;
@end

View File

@@ -8,9 +8,10 @@
#import <OpenGLES/EAGL.h>
#import <OpenGLES/EAGLDrawable.h>
#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<sky::shell::ShellView> _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::PlatformViewIOS*>(
sky::shell::Shell::Shared().view());
auto view = static_cast<sky::shell::PlatformViewIOS*>(_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<sky::shell::PlatformViewIOS*>(_shell_view->view());
auto interface_request = mojo::GetProxy(&_viewport_observer);
view->ConnectToViewportObserver(interface_request.Pass());

View File

@@ -8,10 +8,14 @@
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#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;

View File

@@ -19,13 +19,16 @@ class PlatformView {
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner;
};
explicit PlatformView(const Config& config);
static PlatformView* Create(const Config& config);
virtual ~PlatformView();
void ConnectToViewportObserver(
mojo::InterfaceRequest<ViewportObserver> request);
protected:
explicit PlatformView(const Config& config);
void SurfaceWasCreated();
void SurfaceWasDestroyed();

View File

@@ -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<ServiceProviderContext> 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

View File

@@ -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<ServiceProviderContext> service_provider_context);
static Shell& Shared();
PlatformView* view() const { return view_.get(); }
scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner() const {
return gpu_thread_->message_loop()->task_runner();
}
scoped_refptr<base::SingleThreadTaskRunner> 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<ServiceProviderContext> service_provider_context);
void InitGPU(const base::Thread::Options& options);
void InitUI(const base::Thread::Options& options);
void InitView();
scoped_ptr<base::Thread> gpu_thread_;
scoped_ptr<base::Thread> ui_thread_;
scoped_ptr<PlatformView> view_;
scoped_ptr<Rasterizer> rasterizer_;
scoped_ptr<Engine> engine_;
scoped_ptr<ServiceProviderContext> service_provider_context_;
DISALLOW_COPY_AND_ASSIGN(Shell);

View File

@@ -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

View File

@@ -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<PlatformView> view_;
scoped_ptr<Rasterizer> rasterizer_;
scoped_ptr<Engine> engine_;
DISALLOW_COPY_AND_ASSIGN(ShellView);
};
} // namespace shell
} // namespace sky
#endif // SKY_SHELL_SHELL_VIEW_H_

View File

@@ -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> 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) {

View File

@@ -50,7 +50,7 @@ class Engine : public UIDelegate,
base::WeakPtr<Engine> GetWeakPtr();
void Init();
static void Init(ServiceProviderContext* service_provider_context);
void BeginFrame(base::TimeTicks frame_time);
skia::RefPtr<SkPicture> Paint();
@@ -96,8 +96,6 @@ class Engine : public UIDelegate,
void UpdateWebViewSize();
Config config_;
mojo::ServiceProviderPtr service_provider_;
scoped_ptr<PlatformImpl> platform_impl_;
scoped_ptr<Animator> animator_;
std::unique_ptr<blink::SkyView> sky_view_;