Plumb resize notifications around sky/shell

This CL plumbs resize notifications from SkyView to Engine. I've taken the
opportunity to reorganize how notifications generated by SkyView are plumbed to
the GPU and UI threads. This approach should reduce the amount of plumbing
needed for new notifications.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/880443003
This commit is contained in:
Adam Barth 2015-02-05 20:08:22 -08:00
parent c30847eb9d
commit 9ebbab290b
14 changed files with 153 additions and 54 deletions

View File

@ -29,10 +29,8 @@ shared_library("sky_shell") {
"gpu/ganesh_surface.h",
"gpu/rasterizer.cc",
"gpu/rasterizer.h",
"ui/engine.cc",
"ui/engine.h",
"ui/platform_impl.cc",
"ui/platform_impl.h",
"gpu_delegate.cc",
"gpu_delegate.h",
"library_loader.cc",
"shell.cc",
"shell.h",
@ -40,6 +38,12 @@ shared_library("sky_shell") {
"sky_main.h",
"sky_view.cc",
"sky_view.h",
"ui/engine.cc",
"ui/engine.h",
"ui/platform_impl.cc",
"ui/platform_impl.h",
"ui_delegate.cc",
"ui_delegate.h",
]
deps = [

View File

@ -34,7 +34,7 @@ base::WeakPtr<Rasterizer> Rasterizer::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void Rasterizer::Init(gfx::AcceleratedWidget widget) {
void Rasterizer::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) {
share_group_ = make_scoped_refptr(new gfx::GLShareGroup());
surface_ = gfx::GLSurface::CreateViewGLSurface(widget);
CHECK(surface_) << "GLSurface required.";
@ -53,6 +53,9 @@ void Rasterizer::Draw(skia::RefPtr<SkPicture> picture) {
surface_->SwapBuffers();
}
void Rasterizer::OnOutputSurfaceDestroyed() {
}
bool Rasterizer::CreateGLContext() {
context_ = gfx::GLContext::CreateGLContext(share_group_.get(), surface_.get(),
gfx::PreferIntegratedGpu);

View File

@ -8,6 +8,7 @@
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "skia/ext/refptr.h"
#include "sky/shell/gpu_delegate.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/native_widget_types.h"
@ -24,14 +25,16 @@ namespace shell {
class GaneshContext;
class GaneshSurface;
class Rasterizer {
class Rasterizer : public GPUDelegate {
public:
explicit Rasterizer();
~Rasterizer();
base::WeakPtr<Rasterizer> GetWeakPtr();
void Init(gfx::AcceleratedWidget widget);
void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override;
void OnOutputSurfaceDestroyed() override;
void Draw(skia::RefPtr<SkPicture> picture);
private:

View File

@ -0,0 +1,14 @@
// 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/gpu_delegate.h"
namespace sky {
namespace shell {
GPUDelegate::~GPUDelegate() {
}
} // namespace shell
} // namespace sky

View File

@ -0,0 +1,25 @@
// 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_GPU_DELEGATE_H_
#define SKY_SHELL_GPU_DELEGATE_H_
#include "ui/gfx/native_widget_types.h"
namespace sky {
namespace shell {
class GPUDelegate {
public:
virtual void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) = 0;
virtual void OnOutputSurfaceDestroyed() = 0;
protected:
virtual ~GPUDelegate();
};
} // namespace shell
} // namespace sky
#endif // SKY_SHELL_GPU_DELEGATE_H_

View File

@ -17,7 +17,7 @@ base::android::RegistrationMethod kSkyRegisteredMethods[] = {
{"SkyView", sky::shell::SkyView::Register},
};
bool RegisterMojoJni(JNIEnv* env) {
bool RegisterSkyJni(JNIEnv* env) {
return RegisterNativeMethods(env, kSkyRegisteredMethods,
arraysize(kSkyRegisteredMethods));
}
@ -35,7 +35,7 @@ JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
if (!base::android::RegisterJni(env))
return -1;
if (!RegisterMojoJni(env))
if (!RegisterSkyJni(env))
return -1;
return JNI_VERSION_1_4;

View File

@ -7,7 +7,9 @@
#include "base/bind.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "sky/shell/gpu/rasterizer.h"
#include "sky/shell/sky_view.h"
#include "sky/shell/ui/engine.h"
namespace sky {
namespace shell {
@ -31,17 +33,13 @@ void Shell::Init() {
ui_thread_->message_loop()->PostTask(
FROM_HERE, base::Bind(&Engine::Init, engine_->GetWeakPtr()));
view_.reset(new SkyView(this));
view_->Init();
}
SkyView::Config config;
config.gpu_task_runner = gpu_thread_->message_loop()->task_runner();
config.gpu_delegate = rasterizer_->GetWeakPtr();
config.ui_task_runner = ui_thread_->message_loop()->task_runner();
config.ui_delegate = engine_->GetWeakPtr();
void Shell::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) {
gpu_thread_->message_loop()->PostTask(
FROM_HERE,
base::Bind(&Rasterizer::Init, rasterizer_->GetWeakPtr(), widget));
}
void Shell::OnDestroyed() {
view_.reset(new SkyView(config));
}
} // namespace shell

View File

@ -8,9 +8,6 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "sky/shell/gpu/rasterizer.h"
#include "sky/shell/sky_view.h"
#include "sky/shell/ui/engine.h"
namespace base {
class Thread;
@ -19,9 +16,11 @@ class SingleThreadTaskRunner;
namespace sky {
namespace shell {
class Engine;
class Rasterizer;
class SkyView;
class Shell : public SkyView::Delegate {
class Shell {
public:
explicit Shell(scoped_refptr<base::SingleThreadTaskRunner> java_task_runner);
~Shell();
@ -29,9 +28,6 @@ class Shell : public SkyView::Delegate {
void Init();
private:
void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override;
void OnDestroyed() override;
scoped_refptr<base::SingleThreadTaskRunner> java_task_runner_;
scoped_ptr<base::Thread> gpu_thread_;
scoped_ptr<base::Thread> ui_thread_;

View File

@ -8,6 +8,8 @@
#include <android/native_window_jni.h>
#include "base/android/jni_android.h"
#include "base/bind.h"
#include "base/location.h"
#include "jni/SkyView_jni.h"
namespace sky {
@ -18,10 +20,10 @@ bool SkyView::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
}
SkyView::Delegate::~Delegate() {
}
SkyView::SkyView(Delegate* delegate) : delegate_(delegate), window_(NULL) {
SkyView::SkyView(const Config& config) : config_(config), window_(nullptr) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_SkyView_createForActivity(env, base::android::GetApplicationContext(),
reinterpret_cast<jlong>(this));
}
SkyView::~SkyView() {
@ -29,14 +31,7 @@ SkyView::~SkyView() {
ReleaseWindow();
}
void SkyView::Init() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_SkyView_createForActivity(env, base::android::GetApplicationContext(),
reinterpret_cast<jlong>(this));
}
void SkyView::Destroy(JNIEnv* env, jobject obj) {
delegate_->OnDestroyed();
}
void SkyView::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) {
@ -48,11 +43,16 @@ void SkyView::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) {
base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
window_ = ANativeWindow_fromSurface(env, jsurface);
}
delegate_->OnAcceleratedWidgetAvailable(window_);
config_.gpu_task_runner->PostTask(
FROM_HERE, base::Bind(&GPUDelegate::OnAcceleratedWidgetAvailable,
config_.gpu_delegate, window_));
}
void SkyView::SurfaceDestroyed(JNIEnv* env, jobject obj) {
DCHECK(window_);
config_.gpu_task_runner->PostTask(
FROM_HERE,
base::Bind(&GPUDelegate::OnOutputSurfaceDestroyed, config_.gpu_delegate));
ReleaseWindow();
}
@ -61,11 +61,15 @@ void SkyView::SurfaceSetSize(JNIEnv* env,
jint width,
jint height,
jfloat density) {
config_.ui_task_runner->PostTask(
FROM_HERE,
base::Bind(&UIDelegate::OnViewportMetricsChanged, config_.ui_delegate,
gfx::Size(width, height), density));
}
void SkyView::ReleaseWindow() {
ANativeWindow_release(window_);
window_ = NULL;
window_ = nullptr;
}
} // namespace shell

View File

@ -8,7 +8,10 @@
#include "base/android/jni_weak_ref.h"
#include "base/android/scoped_java_ref.h"
#include "base/macros.h"
#include "ui/gfx/native_widget_types.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "sky/shell/gpu_delegate.h"
#include "sky/shell/ui_delegate.h"
struct ANativeWindow;
@ -17,22 +20,18 @@ namespace shell {
class SkyView {
public:
static bool Register(JNIEnv* env);
struct Config {
base::WeakPtr<GPUDelegate> gpu_delegate;
scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner;
class Delegate {
public:
virtual void OnAcceleratedWidgetAvailable(
gfx::AcceleratedWidget widget) = 0;
virtual void OnDestroyed() = 0;
protected:
virtual ~Delegate();
base::WeakPtr<UIDelegate> ui_delegate;
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner;
};
explicit SkyView(Delegate* delegate);
~SkyView();
static bool Register(JNIEnv* env);
void Init();
explicit SkyView(const Config& config);
~SkyView();
// Called from Java
void Destroy(JNIEnv* env, jobject obj);
@ -47,7 +46,7 @@ class SkyView {
private:
void ReleaseWindow();
Delegate* delegate_;
Config config_;
ANativeWindow* window_;
DISALLOW_COPY_AND_ASSIGN(SkyView);

View File

@ -32,5 +32,13 @@ void Engine::Init() {
web_view_->setMainFrame(blink::WebLocalFrame::create(this));
}
void Engine::OnViewportMetricsChanged(const gfx::Size& size,
float device_pixel_ratio) {
blink::WebSize web_size(size.width() / device_pixel_ratio,
size.height() / device_pixel_ratio);
web_view_->setDeviceScaleFactor(device_pixel_ratio);
web_view_->resize(web_size);
}
} // namespace shell
} // namespace sky

View File

@ -10,12 +10,15 @@
#include "base/memory/weak_ptr.h"
#include "sky/engine/public/web/WebFrameClient.h"
#include "sky/engine/public/web/WebViewClient.h"
#include "sky/shell/ui_delegate.h"
#include "ui/gfx/geometry/size.h"
namespace sky {
namespace shell {
class PlatformImpl;
class Engine : public blink::WebFrameClient,
class Engine : public UIDelegate,
public blink::WebFrameClient,
public blink::WebViewClient {
public:
Engine();
@ -25,6 +28,9 @@ class Engine : public blink::WebFrameClient,
void Init();
void OnViewportMetricsChanged(const gfx::Size& size,
float device_pixel_ratio) override;
private:
scoped_ptr<PlatformImpl> platform_impl_;
blink::WebView* web_view_;

View File

@ -0,0 +1,14 @@
// 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/ui_delegate.h"
namespace sky {
namespace shell {
UIDelegate::~UIDelegate() {
}
} // namespace shell
} // namespace sky

View File

@ -0,0 +1,25 @@
// 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_UI_DELEGATE_H_
#define SKY_SHELL_UI_DELEGATE_H_
#include "ui/gfx/geometry/size.h"
namespace sky {
namespace shell {
class UIDelegate {
public:
virtual void OnViewportMetricsChanged(const gfx::Size& size,
float device_pixel_ratio) = 0;
protected:
virtual ~UIDelegate();
};
} // namespace shell
} // namespace sky
#endif // SKY_SHELL_UI_DELEGATE_H_