diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-01 16:44:56 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-01 16:44:56 +0000 |
commit | 29ccd8e9c7a8f16e2174fdcf9f0bafe10a74db98 (patch) | |
tree | 240a1bd3a04c89291568f495035edf620e8f93a3 /mojo | |
parent | 32014604025bb5fb9c378d2b8820f03da3681fe4 (diff) | |
download | chromium_src-29ccd8e9c7a8f16e2174fdcf9f0bafe10a74db98.zip chromium_src-29ccd8e9c7a8f16e2174fdcf9f0bafe10a74db98.tar.gz chromium_src-29ccd8e9c7a8f16e2174fdcf9f0bafe10a74db98.tar.bz2 |
Make Mojo's NativeViewportAndroid draw a green square
This CL integrates the MojoView with the native_viewport
service. Instead of creating the MojoView on the Java side,
we now create the MojoViewport via the native_viewport service.
This required teaching mojo_shell about the existence of
activities on Android so that the native_viewport service knew
where to create the view. On Windows, the current desktop
appears to be implicit, which is why we haven't needed this
context object in native_viewport until now.
The one aspect of this CL that I'm not super excited about is
the dependency I've introduced from the native_viewport
service to the shell. It seems likely we'll want to move
some more of the concepts in the shell directory lower down
in the dependency graph if/when we want to support other
embedding of the mojo system besides mojo_shell.
R=ben@chromium.org
Review URL: https://codereview.chromium.org/48713009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232411 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
23 files changed, 421 insertions, 249 deletions
diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp index 515cfe1..b3acf62 100644 --- a/mojo/mojo.gyp +++ b/mojo/mojo.gyp @@ -251,9 +251,12 @@ 'dependencies': [ '../base/base.gyp:base', '../ui/gfx/gfx.gyp:gfx', + '../ui/gl/gl.gyp:gl', '../ui/events/events.gyp:events' ], 'sources': [ + 'services/native_viewport/android/mojo_viewport.cc', + 'services/native_viewport/android/mojo_viewport.h', 'services/native_viewport/native_viewport.h', 'services/native_viewport/native_viewport_android.cc', 'services/native_viewport/native_viewport_controller.cc', @@ -267,7 +270,12 @@ 'sources!': [ 'services/native_viewport/native_viewport_stub.cc', ], - }] + }], + ['OS=="android"', { + 'dependencies': [ + 'mojo_jni_headers', + ], + }], ], }, ], @@ -275,6 +283,17 @@ ['OS=="android"', { 'targets': [ { + 'target_name': 'native_viewport_java', + 'type': 'none', + 'dependencies': [ + '../base/base.gyp:base_java', + ], + 'variables': { + 'java_in_dir': '<(DEPTH)/mojo/services/native_viewport/android', + }, + 'includes': [ '../build/java.gypi' ], + }, + { 'target_name': 'java_set_jni_headers', 'type': 'none', 'variables': { @@ -295,8 +314,8 @@ ], }, 'sources': [ + 'services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java', 'shell/android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java', - 'shell/android/apk/src/org/chromium/mojo_shell_apk/MojoView.java', ], 'variables': { 'jni_gen_package': 'mojo' @@ -318,8 +337,6 @@ 'shell/android/library_loader.cc', 'shell/android/mojo_main.cc', 'shell/android/mojo_main.h', - 'shell/android/mojo_view.cc', - 'shell/android/mojo_view.h', ], }, { @@ -328,6 +345,7 @@ 'dependencies': [ '../base/base.gyp:base_java', '../net/net.gyp:net_java', + 'native_viewport_java', 'libmojo_shell', ], 'variables': { diff --git a/mojo/services/DEPS b/mojo/services/DEPS index 35cd2dd..ed482be 100644 --- a/mojo/services/DEPS +++ b/mojo/services/DEPS @@ -1,4 +1,9 @@ include_rules = [ "-mojo", "+mojo/public", + "+jni", + + # TODO(abarth) Instead of having the services depend on the shell, we + # probably should create a layer below the services. + "+mojo/shell/context.h", ] diff --git a/mojo/services/native_viewport/DEPS b/mojo/services/native_viewport/DEPS index 4de3631..00b7d5c 100644 --- a/mojo/services/native_viewport/DEPS +++ b/mojo/services/native_viewport/DEPS @@ -2,4 +2,5 @@ include_rules = [ "+base", "+ui/events", "+ui/gfx", + "+ui/gl", ] diff --git a/mojo/services/native_viewport/android/mojo_viewport.cc b/mojo/services/native_viewport/android/mojo_viewport.cc new file mode 100644 index 0000000..97357da --- /dev/null +++ b/mojo/services/native_viewport/android/mojo_viewport.cc @@ -0,0 +1,76 @@ +// Copyright 2013 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 "mojo/services/native_viewport/android/mojo_viewport.h" + +#include <android/native_window_jni.h> +#include "base/android/jni_android.h" +#include "jni/MojoViewport_jni.h" +#include "ui/gl/gl_bindings.h" +#include "ui/gl/gl_context.h" +#include "ui/gl/scoped_make_current.h" + +namespace mojo { +namespace services { + +static jint Init(JNIEnv* env, jclass obj, jint jinit) { + MojoViewportInit* init = reinterpret_cast<MojoViewportInit*>(jinit); + MojoViewport* viewport = new MojoViewport(init); + return reinterpret_cast<jint>(viewport); +} + +MojoViewport::MojoViewport(MojoViewportInit* init) + : ui_runner_(init->ui_runner), + native_viewport_(init->native_viewport) { + delete init; +} + +MojoViewport::~MojoViewport() { +} + +void MojoViewport::CreateForActivity( + jobject activity, + MojoViewportInit* init) { + JNIEnv* env = base::android::AttachCurrentThread(); + Java_MojoViewport_CreateForActivity( + env, activity, reinterpret_cast<jint>(init)); +} + +void MojoViewport::Destroy(JNIEnv* env, jobject obj) { + delete this; +} + +void MojoViewport::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) { + base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface); + DCHECK(jsurface); + window_ = ANativeWindow_fromSurface(env, jsurface); + DCHECK(window_); + + surface_ = new gfx::NativeViewGLSurfaceEGL(window_); + CHECK(surface_->Initialize()); + + gl_context_ = gfx::GLContext::CreateGLContext( + 0, surface_.get(), gfx::PreferDiscreteGpu); + ui::ScopedMakeCurrent make_current(gl_context_.get(), surface_.get()); + glClearColor(0, 1, 0, 0); + glClear(GL_COLOR_BUFFER_BIT); + surface_->SwapBuffers(); +} + +void MojoViewport::SurfaceDestroyed(JNIEnv* env, jobject obj) { + DCHECK(window_); + ANativeWindow_release(window_); + window_ = NULL; +} + +void MojoViewport::SurfaceSetSize( + JNIEnv* env, jobject obj, jint width, jint height) { +} + +bool MojoViewport::Register(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace services +} // namespace mojo diff --git a/mojo/services/native_viewport/android/mojo_viewport.h b/mojo/services/native_viewport/android/mojo_viewport.h new file mode 100644 index 0000000..0236430 --- /dev/null +++ b/mojo/services/native_viewport/android/mojo_viewport.h @@ -0,0 +1,57 @@ +// Copyright 2013 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 MOJO_SERVICES_NATIVE_VIEWPORT_ANDROID_MOJO_VIEWPORT_H_ +#define MOJO_SERVICES_NATIVE_VIEWPORT_ANDROID_MOJO_VIEWPORT_H_ + +#include "base/android/jni_helper.h" +#include "base/android/scoped_java_ref.h" +#include "base/logging.h" +#include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" +#include "base/single_thread_task_runner.h" +#include "mojo/services/native_viewport/native_viewport_android.h" +#include "ui/gl/gl_surface_egl.h" + +struct ANativeWindow; + +namespace mojo { +namespace services { + +struct MojoViewportInit { + scoped_refptr<base::SingleThreadTaskRunner> ui_runner; + base::WeakPtr<NativeViewportAndroid> native_viewport; +}; + +class MojoViewport { + public: + static bool Register(JNIEnv* env); + + static void CreateForActivity( + jobject activity, MojoViewportInit* init); + + explicit MojoViewport(MojoViewportInit* init); + + void Destroy(JNIEnv* env, jobject obj); + void SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface); + void SurfaceDestroyed(JNIEnv* env, jobject obj); + void SurfaceSetSize(JNIEnv* env, jobject obj, jint width, jint height); + + private: + ~MojoViewport(); + + scoped_refptr<base::SingleThreadTaskRunner> ui_runner_; + base::WeakPtr<NativeViewportAndroid> native_viewport_; + + ANativeWindow* window_; + scoped_refptr<gfx::GLSurface> surface_; + scoped_refptr<gfx::GLContext> gl_context_; + + DISALLOW_COPY_AND_ASSIGN(MojoViewport); +}; + +} // namespace services +} // namespace mojo + +#endif // MOJO_SERVICES_NATIVE_VIEWPORT_ANDROID_MOJO_VIEWPORT_H_ diff --git a/mojo/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoView.java b/mojo/services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java index 946662d..2a714b3 100644 --- a/mojo/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoView.java +++ b/mojo/services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java @@ -2,45 +2,53 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -package org.chromium.mojo_shell_apk; +package org.chromium.mojo; +import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.Surface; import android.view.SurfaceHolder; import android.view.SurfaceView; +import org.chromium.base.CalledByNative; import org.chromium.base.JNINamespace; -@JNINamespace("mojo") -public class MojoView extends SurfaceView { +@JNINamespace("mojo::services") +public class MojoViewport extends SurfaceView { - private int mNativeMojoView; + private int mNativeMojoViewport; private final SurfaceHolder.Callback mSurfaceCallback; - public MojoView(Context context, AttributeSet attrs) { - super(context, attrs); + @SuppressWarnings("unused") + @CalledByNative + public static void CreateForActivity(Activity activity, int init) { + activity.setContentView(new MojoViewport(activity, init)); + } + + public MojoViewport(Context context, int init) { + super(context); - mNativeMojoView = nativeInit(); - assert mNativeMojoView != 0; + mNativeMojoViewport = nativeInit(init); + assert mNativeMojoViewport != 0; mSurfaceCallback = new SurfaceHolder.Callback() { @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { - assert mNativeMojoView != 0; - nativeSurfaceSetSize(mNativeMojoView, width, height); + assert mNativeMojoViewport != 0; + nativeSurfaceSetSize(mNativeMojoViewport, width, height); } @Override public void surfaceCreated(SurfaceHolder holder) { - assert mNativeMojoView != 0; - nativeSurfaceCreated(mNativeMojoView, holder.getSurface()); + assert mNativeMojoViewport != 0; + nativeSurfaceCreated(mNativeMojoViewport, holder.getSurface()); } @Override public void surfaceDestroyed(SurfaceHolder holder) { - assert mNativeMojoView != 0; - nativeSurfaceDestroyed(mNativeMojoView); + assert mNativeMojoViewport != 0; + nativeSurfaceDestroyed(mNativeMojoViewport); } }; getHolder().addCallback(mSurfaceCallback); @@ -50,13 +58,13 @@ public class MojoView extends SurfaceView { // TODO(abarth): Someone needs to call destroy at some point. public void destroy() { getHolder().removeCallback(mSurfaceCallback); - nativeDestroy(mNativeMojoView); - mNativeMojoView = 0; + nativeDestroy(mNativeMojoViewport); + mNativeMojoViewport = 0; } - private static native int nativeInit(); - private static native void nativeDestroy(int nativeMojoView); - private static native void nativeSurfaceCreated(int nativeMojoView, Surface surface); - private static native void nativeSurfaceDestroyed(int nativeMojoView); - private static native void nativeSurfaceSetSize(int nativeMojoView, int width, int height); + private static native int nativeInit(int init); + private static native void nativeDestroy(int nativeMojoViewport); + private static native void nativeSurfaceCreated(int nativeMojoViewport, Surface surface); + private static native void nativeSurfaceDestroyed(int nativeMojoViewport); + private static native void nativeSurfaceSetSize(int nativeMojoViewport, int width, int height); }; diff --git a/mojo/services/native_viewport/native_viewport.h b/mojo/services/native_viewport/native_viewport.h index 78ae27d..e39b435 100644 --- a/mojo/services/native_viewport/native_viewport.h +++ b/mojo/services/native_viewport/native_viewport.h @@ -16,6 +16,10 @@ class Event; } namespace mojo { +namespace shell { +class Context; +} + namespace services { class NativeViewportDelegate { @@ -36,7 +40,8 @@ class NativeViewport { virtual void Close() = 0; - static scoped_ptr<NativeViewport> Create(NativeViewportDelegate* delegate); + static scoped_ptr<NativeViewport> Create(shell::Context* context, + NativeViewportDelegate* delegate); }; } // namespace services diff --git a/mojo/services/native_viewport/native_viewport_android.cc b/mojo/services/native_viewport/native_viewport_android.cc index 4cf5396..a0d2455 100644 --- a/mojo/services/native_viewport/native_viewport_android.cc +++ b/mojo/services/native_viewport/native_viewport_android.cc @@ -1,39 +1,48 @@ -// Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h"
-
-namespace mojo {
-namespace services {
-
-class NativeViewportAndroid : public NativeViewport {
- public:
- NativeViewportAndroid(NativeViewportDelegate* delegate)
- : delegate_(delegate) {
- }
- virtual ~NativeViewportAndroid() {
- }
-
- private:
- // Overridden from NativeViewport:
- virtual void Close() OVERRIDE {
- // TODO(beng): close activity containing MojoView?
-
- // TODO(beng): perform this in response to view destruction.
- delegate_->OnDestroyed();
- }
-
- NativeViewportDelegate* delegate_;
-
- DISALLOW_COPY_AND_ASSIGN(NativeViewportAndroid);
-};
-
-// static
-scoped_ptr<NativeViewport> NativeViewport::Create(
- NativeViewportDelegate* delegate) {
- return scoped_ptr<NativeViewport>(new NativeViewportAndroid(delegate)).Pass();
-}
-
-} // namespace services
-} // namespace mojo
+// Copyright 2013 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 "mojo/services/native_viewport/native_viewport_android.h" + +#include "mojo/services/native_viewport/android/mojo_viewport.h" +#include "mojo/shell/context.h" + +namespace mojo { +namespace services { + +NativeViewportAndroid::NativeViewportAndroid(NativeViewportDelegate* delegate) + : delegate_(delegate), + weak_factory_(this) { +} + +NativeViewportAndroid::~NativeViewportAndroid() { +} + +void NativeViewportAndroid::Close() { + // TODO(beng): close activity containing MojoView? + + // TODO(beng): perform this in response to view destruction. + delegate_->OnDestroyed(); +} + +// static +scoped_ptr<NativeViewport> NativeViewport::Create( + shell::Context* context, + NativeViewportDelegate* delegate) { + scoped_ptr<NativeViewportAndroid> native_viewport( + new NativeViewportAndroid(delegate)); + + MojoViewportInit* init = new MojoViewportInit(); + init->ui_runner = context->task_runners()->ui_runner(); + init->native_viewport = native_viewport->GetWeakPtr(); + + context->task_runners()->java_runner()->PostTask(FROM_HERE, + base::Bind(MojoViewport::CreateForActivity, + context->activity(), + init)); + + return scoped_ptr<NativeViewport>(native_viewport.Pass()); +} + +} // namespace services +} // namespace mojo diff --git a/mojo/services/native_viewport/native_viewport_android.h b/mojo/services/native_viewport/native_viewport_android.h new file mode 100644 index 0000000..a593de5 --- /dev/null +++ b/mojo/services/native_viewport/native_viewport_android.h @@ -0,0 +1,38 @@ +// Copyright 2013 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 MOJO_SERVICES_NATIVE_VIEWPORT_NATIVE_VIEWPORT_ANDROID_H_ +#define MOJO_SERVICES_NATIVE_VIEWPORT_NATIVE_VIEWPORT_ANDROID_H_ + +#include "base/memory/weak_ptr.h" +#include "mojo/services/native_viewport/native_viewport.h" + +namespace mojo { +namespace services { + +class NativeViewportAndroid : public NativeViewport { + public: + explicit NativeViewportAndroid(NativeViewportDelegate* delegate); + virtual ~NativeViewportAndroid(); + + base::WeakPtr<NativeViewportAndroid> GetWeakPtr() { + return weak_factory_.GetWeakPtr(); + } + + private: + // Overridden from NativeViewport: + virtual void Close() OVERRIDE; + + NativeViewportDelegate* delegate_; + + base::WeakPtrFactory<NativeViewportAndroid> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(NativeViewportAndroid); +}; + + +} // namespace services +} // namespace mojo + +#endif // MOJO_SERVICES_NATIVE_VIEWPORT_NATIVE_VIEWPORT_ANDROID_H_ diff --git a/mojo/services/native_viewport/native_viewport_controller.cc b/mojo/services/native_viewport/native_viewport_controller.cc index 2a8f6d0..6fe515c 100644 --- a/mojo/services/native_viewport/native_viewport_controller.cc +++ b/mojo/services/native_viewport/native_viewport_controller.cc @@ -12,9 +12,10 @@ namespace mojo { namespace services { -NativeViewportController::NativeViewportController(Handle pipe) +NativeViewportController::NativeViewportController( + shell::Context* context, Handle pipe) : pipe_(pipe) { - native_viewport_ = NativeViewport::Create(this); + native_viewport_ = NativeViewport::Create(context, this); } NativeViewportController::~NativeViewportController() { } diff --git a/mojo/services/native_viewport/native_viewport_controller.h b/mojo/services/native_viewport/native_viewport_controller.h index 7349019..797b073 100644 --- a/mojo/services/native_viewport/native_viewport_controller.h +++ b/mojo/services/native_viewport/native_viewport_controller.h @@ -18,7 +18,8 @@ class NativeViewportController : public services::NativeViewportDelegate { // TODO(beng): Currently, pipe is just the single pipe that exists between // mojo_shell and the loaded app. This should really be hidden // behind the bindings layer, when that comes up. - explicit NativeViewportController(Handle pipe); + NativeViewportController(shell::Context* context, + Handle pipe); virtual ~NativeViewportController(); void Close(); diff --git a/mojo/services/native_viewport/native_viewport_stub.cc b/mojo/services/native_viewport/native_viewport_stub.cc index b4f3bd2..d5b38f2 100644 --- a/mojo/services/native_viewport/native_viewport_stub.cc +++ b/mojo/services/native_viewport/native_viewport_stub.cc @@ -1,38 +1,39 @@ -// Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h"
-
-// Stub to build on platforms we don't fully support yet.
-
-namespace mojo {
-namespace services {
-
-class NativeViewportStub : public NativeViewport {
- public:
- NativeViewportStub(NativeViewportDelegate* delegate)
- : delegate_(delegate) {
- }
- virtual ~NativeViewportStub() {
- }
-
- private:
- // Overridden from NativeViewport:
- virtual void Close() OVERRIDE {
- delegate_->OnDestroyed();
- }
-
- NativeViewportDelegate* delegate_;
-
- DISALLOW_COPY_AND_ASSIGN(NativeViewportStub);
-};
-
-// static
-scoped_ptr<NativeViewport> NativeViewport::Create(
- NativeViewportDelegate* delegate) {
- return scoped_ptr<NativeViewport>(new NativeViewportStub(delegate)).Pass();
-}
-
-} // namespace services
-} // namespace mojo
+// Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h" + +// Stub to build on platforms we don't fully support yet. + +namespace mojo { +namespace services { + +class NativeViewportStub : public NativeViewport { + public: + NativeViewportStub(NativeViewportDelegate* delegate) + : delegate_(delegate) { + } + virtual ~NativeViewportStub() { + } + + private: + // Overridden from NativeViewport: + virtual void Close() OVERRIDE { + delegate_->OnDestroyed(); + } + + NativeViewportDelegate* delegate_; + + DISALLOW_COPY_AND_ASSIGN(NativeViewportStub); +}; + +// static +scoped_ptr<NativeViewport> NativeViewport::Create( + shell::Context* context, + NativeViewportDelegate* delegate) { + return scoped_ptr<NativeViewport>(new NativeViewportStub(delegate)).Pass(); +} + +} // namespace services +} // namespace mojo diff --git a/mojo/services/native_viewport/native_viewport_win.cc b/mojo/services/native_viewport/native_viewport_win.cc index 8951e53..85ef013 100644 --- a/mojo/services/native_viewport/native_viewport_win.cc +++ b/mojo/services/native_viewport/native_viewport_win.cc @@ -72,6 +72,7 @@ class NativeViewportWin : public gfx::WindowImpl, // static scoped_ptr<NativeViewport> NativeViewport::Create( + shell::Context* context, NativeViewportDelegate* delegate) { return scoped_ptr<NativeViewport>(new NativeViewportWin(delegate)).Pass(); } diff --git a/mojo/services/native_viewport/native_viewport_x11.cc b/mojo/services/native_viewport/native_viewport_x11.cc index 8ffa51b..df4f5d9 100644 --- a/mojo/services/native_viewport/native_viewport_x11.cc +++ b/mojo/services/native_viewport/native_viewport_x11.cc @@ -1,37 +1,38 @@ -// Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h"
-
-namespace mojo {
-namespace services {
-
-class NativeViewportX11 : public NativeViewport {
- public:
- NativeViewportX11(NativeViewportDelegate* delegate)
- : delegate_(delegate) {
- }
- virtual ~NativeViewportX11() {
- }
-
- private:
- // Overridden from NativeViewport:
- virtual void Close() OVERRIDE {
- // TODO(beng): perform this in response to XWindow destruction.
- delegate_->OnDestroyed();
- }
-
- NativeViewportDelegate* delegate_;
-
- DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
-};
-
-// static
-scoped_ptr<NativeViewport> NativeViewport::Create(
- NativeViewportDelegate* delegate) {
- return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass();
-}
-
-} // namespace services
-} // namespace mojo
+// Copyright 2013 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 "mojo/services/native_viewport/native_viewport.h" + +namespace mojo { +namespace services { + +class NativeViewportX11 : public NativeViewport { + public: + NativeViewportX11(NativeViewportDelegate* delegate) + : delegate_(delegate) { + } + virtual ~NativeViewportX11() { + } + + private: + // Overridden from NativeViewport: + virtual void Close() OVERRIDE { + // TODO(beng): perform this in response to XWindow destruction. + delegate_->OnDestroyed(); + } + + NativeViewportDelegate* delegate_; + + DISALLOW_COPY_AND_ASSIGN(NativeViewportX11); +}; + +// static +scoped_ptr<NativeViewport> NativeViewport::Create( + shell::Context* context, + NativeViewportDelegate* delegate) { + return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass(); +} + +} // namespace services +} // namespace mojo diff --git a/mojo/shell/android/apk/res/layout/mojo_shell_activity.xml b/mojo/shell/android/apk/res/layout/mojo_shell_activity.xml index 5df9fde..d319941 100644 --- a/mojo/shell/android/apk/res/layout/mojo_shell_activity.xml +++ b/mojo/shell/android/apk/res/layout/mojo_shell_activity.xml @@ -10,8 +10,4 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> - <org.chromium.mojo_shell_apk.MojoView - android:id="@+id/mojo_view" - android:layout_width="match_parent" - android:layout_height="match_parent" /> </LinearLayout> diff --git a/mojo/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoShellActivity.java b/mojo/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoShellActivity.java index bfb847b..55b933f 100644 --- a/mojo/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoShellActivity.java +++ b/mojo/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoShellActivity.java @@ -11,7 +11,6 @@ import android.util.Log; import org.chromium.mojo_shell_apk.LibraryLoader; import org.chromium.mojo_shell_apk.MojoMain; -import org.chromium.mojo_shell_apk.MojoView; /** * Activity for managing the Mojo Shell. @@ -32,7 +31,6 @@ public class MojoShellActivity extends Activity { } MojoMain.init(this); - setContentView(R.layout.mojo_shell_activity); String appUrl = getUrlFromIntent(getIntent()); MojoMain.start(this, appUrl); diff --git a/mojo/shell/android/library_loader.cc b/mojo/shell/android/library_loader.cc index 4bb8f30..a541c6c 100644 --- a/mojo/shell/android/library_loader.cc +++ b/mojo/shell/android/library_loader.cc @@ -6,15 +6,15 @@ #include "base/android/jni_android.h" #include "base/android/jni_registrar.h" #include "base/logging.h" +#include "mojo/services/native_viewport/android/mojo_viewport.h" #include "mojo/shell/android/mojo_main.h" -#include "mojo/shell/android/mojo_view.h" #include "net/android/net_jni_registrar.h" namespace { base::android::RegistrationMethod kMojoRegisteredMethods[] = { { "MojoMain", mojo::RegisterMojoMain }, - { "MojoView", mojo::MojoView::Register }, + { "MojoViewport", mojo::services::MojoViewport::Register }, }; bool RegisterMojoJni(JNIEnv* env) { diff --git a/mojo/shell/android/mojo_main.cc b/mojo/shell/android/mojo_main.cc index 4b57fd8..214c553 100644 --- a/mojo/shell/android/mojo_main.cc +++ b/mojo/shell/android/mojo_main.cc @@ -23,6 +23,9 @@ namespace { base::AtExitManager* g_at_exit = 0; +LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop = + LAZY_INSTANCE_INITIALIZER; + LazyInstance<scoped_ptr<base::Thread> > g_shell_thread = LAZY_INSTANCE_INITIALIZER; @@ -42,9 +45,20 @@ void InitializeLogging() { false); // Tick count } -void StartOnShellThread() { - g_context.Get().reset(new shell::Context()); - shell::Run(g_context.Get().get()); +struct ShellInit { + scoped_refptr<base::SingleThreadTaskRunner> java_runner; + base::android::ScopedJavaGlobalRef<jobject> activity; +}; + +void StartOnShellThread(ShellInit* init) { + shell::Context* context = new shell::Context(); + + context->set_activity(init->activity.obj()); + context->task_runners()->set_java_runner(init->java_runner.get()); + delete init; + + g_context.Get().reset(context); + shell::Run(context); } } // namspace @@ -62,6 +76,10 @@ static void Init(JNIEnv* env, jclass clazz, jobject context) { CommandLine::Init(0, 0); InitializeLogging(); + g_java_message_loop.Get().reset( + new base::MessageLoop(base::MessageLoop::TYPE_UI)); + base::MessageLoopForUI::current()->Start(); + // TODO(abarth): At which point should we switch to cross-platform // initialization? @@ -77,10 +95,14 @@ static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) { CommandLine::ForCurrentProcess()->InitFromArgv(argv); } + ShellInit* init = new ShellInit(); + init->java_runner = base::MessageLoopForUI::current()->message_loop_proxy(); + init->activity.Reset(env, context); + g_shell_thread.Get().reset(new base::Thread("shell_thread")); g_shell_thread.Get()->Start(); g_shell_thread.Get()->message_loop()->PostTask(FROM_HERE, - base::Bind(StartOnShellThread)); + base::Bind(StartOnShellThread, init)); // TODO(abarth): Currently we leak g_shell_thread. } diff --git a/mojo/shell/android/mojo_view.cc b/mojo/shell/android/mojo_view.cc deleted file mode 100644 index 3ffcfef..0000000 --- a/mojo/shell/android/mojo_view.cc +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2013 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 "mojo/shell/android/mojo_view.h" - -#include <android/native_window_jni.h> -#include "base/android/jni_android.h" -#include "base/android/scoped_java_ref.h" -#include "jni/MojoView_jni.h" - -namespace mojo { - -static jint Init(JNIEnv* env, jclass obj) { - MojoView* mojo_view = new MojoView(env, obj); - return reinterpret_cast<jint>(mojo_view); -} - -MojoView::MojoView(JNIEnv* env, jobject obj) { -} - -MojoView::~MojoView() { -} - -void MojoView::Destroy(JNIEnv* env, jobject obj) { - delete this; -} - -void MojoView::SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface) { - base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface); - DCHECK(jsurface); - window_ = ANativeWindow_fromSurface(env, jsurface); - DCHECK(window_); - - scoped_refptr<gfx::GLSurface> surface = - new gfx::NativeViewGLSurfaceEGL(window_); - CHECK(surface->Initialize()); -} - -void MojoView::SurfaceDestroyed(JNIEnv* env, jobject obj) { - DCHECK(window_); - ANativeWindow_release(window_); - window_ = NULL; -} - -void MojoView::SurfaceSetSize( - JNIEnv* env, jobject obj, jint width, jint height) { -} - -bool MojoView::Register(JNIEnv* env) { - return RegisterNativesImpl(env); -} - -} // namespace mojo diff --git a/mojo/shell/android/mojo_view.h b/mojo/shell/android/mojo_view.h deleted file mode 100644 index 0d90dd6..0000000 --- a/mojo/shell/android/mojo_view.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2013 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 MOJO_SHELL_ANDROID_MOJO_VIEW_H_ -#define MOJO_SHELL_ANDROID_MOJO_VIEW_H_ - -#include "base/android/jni_helper.h" -#include "base/logging.h" -#include "base/memory/ref_counted.h" -#include "ui/gl/gl_surface_egl.h" - -struct ANativeWindow; - -namespace mojo { - -class MojoView { - public: - static bool Register(JNIEnv* env); - - MojoView(JNIEnv* env, jobject obj); - - void Destroy(JNIEnv* env, jobject obj); - void SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface); - void SurfaceDestroyed(JNIEnv* env, jobject obj); - void SurfaceSetSize(JNIEnv* env, jobject obj, jint width, jint height); - - private: - ~MojoView(); - - ANativeWindow* window_; - scoped_refptr<gfx::GLSurface> surface_; - - DISALLOW_COPY_AND_ASSIGN(MojoView); -}; - -} // namespace mojo - -#endif // MOJO_SHELL_ANDROID_MOJO_VIEW_H_ diff --git a/mojo/shell/app_container.cc b/mojo/shell/app_container.cc index a0bfe03..de0fe2a 100644 --- a/mojo/shell/app_container.cc +++ b/mojo/shell/app_container.cc @@ -94,7 +94,7 @@ void AppContainer::DidCompleteLoad(const GURL& app_url, // TODO(beng): This should be created on demand by the NativeViewportService // when it is retrieved by the app. native_viewport_controller_.reset( - new services::NativeViewportController(shell_handle_)); + new services::NativeViewportController(context_, shell_handle_)); } void AppContainer::AppCompleted() { diff --git a/mojo/shell/context.h b/mojo/shell/context.h index 30a955e..f2dec12 100644 --- a/mojo/shell/context.h +++ b/mojo/shell/context.h @@ -9,6 +9,10 @@ #include "mojo/shell/storage.h" #include "mojo/shell/task_runners.h" +#if defined(OS_ANDROID) +#include "base/android/scoped_java_ref.h" +#endif // defined(OS_ANDROID) + namespace mojo { namespace shell { @@ -21,11 +25,20 @@ class Context { Storage* storage() { return &storage_; } Loader* loader() { return &loader_; } +#if defined(OS_ANDROID) + jobject activity() const { return activity_.obj(); } + void set_activity(jobject activity) { activity_.Reset(NULL, activity); } +#endif // defined(OS_ANDROID) + private: TaskRunners task_runners_; Storage storage_; Loader loader_; +#if defined(OS_ANDROID) + base::android::ScopedJavaGlobalRef<jobject> activity_; +#endif // defined(OS_ANDROID) + DISALLOW_COPY_AND_ASSIGN(Context); }; diff --git a/mojo/shell/task_runners.h b/mojo/shell/task_runners.h index a7c4c99..92c5b14 100644 --- a/mojo/shell/task_runners.h +++ b/mojo/shell/task_runners.h @@ -30,11 +30,25 @@ class TaskRunners { return file_thread_->message_loop_proxy(); } +#if defined(OS_ANDROID) + void set_java_runner(base::SingleThreadTaskRunner* java_runner) { + java_runner_ = java_runner; + } + + base::SingleThreadTaskRunner* java_runner() const { + return java_runner_.get(); + } +#endif // defined(OS_ANDROID) + private: scoped_refptr<base::SingleThreadTaskRunner> ui_runner_; scoped_ptr<base::Thread> io_thread_; scoped_ptr<base::Thread> file_thread_; +#if defined(OS_ANDROID) + scoped_refptr<base::SingleThreadTaskRunner> java_runner_; +#endif // defined(OS_ANDROID) + DISALLOW_COPY_AND_ASSIGN(TaskRunners); }; |