diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index 78f7e135e6..8ec5827ca7 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -18,6 +18,11 @@ group("sky") { "//sky/tools/tester", "//sky/viewer", ] + + if (is_android) { + deps += [ "//sky/shell" ] + } + if (!is_android) { deps += [ "//third_party/mesa:osmesa" ] } diff --git a/engine/src/flutter/shell/BUILD.gn b/engine/src/flutter/shell/BUILD.gn new file mode 100644 index 0000000000..624472d4b2 --- /dev/null +++ b/engine/src/flutter/shell/BUILD.gn @@ -0,0 +1,69 @@ +# 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. + +assert(is_android) + +import("//build/config/android/config.gni") +import("//build/config/android/rules.gni") + +group("shell") { + deps = [ + ":sky_shell_apk", + ] +} + +generate_jni("jni_headers") { + sources = [ + "apk/src/org/domokit/sky/shell/SkyMain.java", + ] + jni_package = "sky/shell" +} + +shared_library("sky_shell") { + sources = [ + "library_loader.cc", + "sky_main.cc", + "sky_main.h", + ] + + deps = [ + "//base", + "//build/config/sanitizers:deps", + "//ui/gl", + ":jni_headers", + ] +} + +android_library("java") { + java_files = [ + "apk/src/org/domokit/sky/shell/SkyMain.java", + "apk/src/org/domokit/sky/shell/SkyShellActivity.java", + "apk/src/org/domokit/sky/shell/SkyShellApplication.java", + ] + + deps = [ + "//base:base_java", + ] +} + +android_resources("resources") { + resource_dirs = [ "apk/res" ] + custom_package = "org.domokit.sky.shell" +} + +android_apk("sky_shell_apk") { + apk_name = "SkyShell" + + android_manifest = "apk/AndroidManifest.xml" + native_libs = [ "libsky_shell.so" ] + + asset_location = "apk/res" + + deps = [ + ":sky_shell", + ":java", + ":resources", + "//base:base_java", + ] +} diff --git a/engine/src/flutter/shell/apk/AndroidManifest.xml b/engine/src/flutter/shell/apk/AndroidManifest.xml new file mode 100644 index 0000000000..4b883998b0 --- /dev/null +++ b/engine/src/flutter/shell/apk/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + diff --git a/engine/src/flutter/shell/apk/res/values/strings.xml b/engine/src/flutter/shell/apk/res/values/strings.xml new file mode 100644 index 0000000000..ff69533446 --- /dev/null +++ b/engine/src/flutter/shell/apk/res/values/strings.xml @@ -0,0 +1,7 @@ + + + + diff --git a/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyMain.java b/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyMain.java new file mode 100644 index 0000000000..0f373e4026 --- /dev/null +++ b/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyMain.java @@ -0,0 +1,50 @@ +// 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. + +package org.domokit.sky.shell; + +import android.content.Context; +import android.util.Log; + +import org.chromium.base.JNINamespace; + +/** + * A class to intialize the native code. + **/ +@JNINamespace("sky::shell") +public class SkyMain { + private static final String TAG = "SkyMain"; + + /** + * A guard flag for calling nativeInit() only once. + **/ + private static boolean sInitialized = false; + + /** + * Initializes the native system. + **/ + static void ensureInitialized(Context applicationContext) { + if (sInitialized) + return; + try { + nativeInit(applicationContext); + sInitialized = true; + } catch (Exception e) { + Log.e(TAG, "SkyMain initialization failed.", e); + throw new RuntimeException(e); + } + } + + /** + * Starts the specified application in the specified context. + * + * @return true if an application has been launched. + **/ + static boolean start() { + return nativeStart(); + } + + private static native void nativeInit(Context context); + private static native boolean nativeStart(); +} diff --git a/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyShellActivity.java b/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyShellActivity.java new file mode 100644 index 0000000000..a38d83e9eb --- /dev/null +++ b/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyShellActivity.java @@ -0,0 +1,24 @@ +// 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. + +package org.domokit.sky.shell; + +import android.app.Activity; +import android.os.Bundle; + +/** + * Main activity for SkyShell. + */ +public class SkyShellActivity extends Activity { + /** + * @see android.app.Activity#onCreate(android.os.Bundle) + */ + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + SkyMain.ensureInitialized(this); + SkyMain.start(); + } +} diff --git a/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyShellApplication.java b/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyShellApplication.java new file mode 100644 index 0000000000..27ac093691 --- /dev/null +++ b/engine/src/flutter/shell/apk/src/org/domokit/sky/shell/SkyShellApplication.java @@ -0,0 +1,47 @@ +// 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. + +package org.domokit.sky.shell; + +import android.util.Log; + +import org.chromium.base.BaseChromiumApplication; +import org.chromium.base.PathUtils; +import org.chromium.base.library_loader.LibraryLoader; +import org.chromium.base.library_loader.ProcessInitException; + +/** + * MojoShell implementation of {@link android.app.Application}, managing application-level global + * state and initializations. + */ +public class SkyShellApplication extends BaseChromiumApplication { + private static final String TAG = "SkyShellApplication"; + private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "sky_shell"; + + @Override + public void onCreate() { + super.onCreate(); + initializeJavaUtils(); + initializeNative(); + } + + /** + * Initializes Java-side utils. + */ + private void initializeJavaUtils() { + PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); + } + + /** + * Loads the native library. + */ + private void initializeNative() { + try { + LibraryLoader.ensureInitialized(); + } catch (ProcessInitException e) { + Log.e(TAG, "sky_shell initialization failed.", e); + throw new RuntimeException(e); + } + } +} diff --git a/engine/src/flutter/shell/library_loader.cc b/engine/src/flutter/shell/library_loader.cc new file mode 100644 index 0000000000..9eb74b1843 --- /dev/null +++ b/engine/src/flutter/shell/library_loader.cc @@ -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. + +#include "base/android/base_jni_registrar.h" +#include "base/android/jni_android.h" +#include "base/android/jni_registrar.h" +#include "base/android/library_loader/library_loader_hooks.h" +#include "base/logging.h" +#include "sky/shell/sky_main.h" + +namespace { + +base::android::RegistrationMethod kSkyRegisteredMethods[] = { + {"SkyMain", sky::shell::RegisterSkyMain}, +}; + +bool RegisterMojoJni(JNIEnv* env) { + return RegisterNativeMethods(env, kSkyRegisteredMethods, + arraysize(kSkyRegisteredMethods)); +} + +} // namespace + +// This is called by the VM when the shared library is first loaded. +JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { + base::android::InitVM(vm); + JNIEnv* env = base::android::AttachCurrentThread(); + + if (!base::android::RegisterLibraryLoaderEntryHook(env)) + return -1; + + if (!base::android::RegisterJni(env)) + return -1; + + if (!RegisterMojoJni(env)) + return -1; + + return JNI_VERSION_1_4; +} diff --git a/engine/src/flutter/shell/sky_main.cc b/engine/src/flutter/shell/sky_main.cc new file mode 100644 index 0000000000..57a2f6e558 --- /dev/null +++ b/engine/src/flutter/shell/sky_main.cc @@ -0,0 +1,73 @@ +// 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/sky_main.h" + +#include "base/android/jni_android.h" +#include "base/android/jni_array.h" +#include "base/android/jni_string.h" +#include "base/at_exit.h" +#include "base/bind.h" +#include "base/command_line.h" +#include "base/lazy_instance.h" +#include "base/logging.h" +#include "base/macros.h" +#include "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "base/threading/simple_thread.h" +#include "jni/SkyMain_jni.h" + +using base::LazyInstance; + +namespace sky { +namespace shell { + +namespace { + +LazyInstance> g_java_message_loop = + LAZY_INSTANCE_INITIALIZER; + +LazyInstance> g_main_activiy = + LAZY_INSTANCE_INITIALIZER; + +void InitializeLogging() { + logging::LoggingSettings settings; + settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; + logging::InitLogging(settings); + // To view log output with IDs and timestamps use "adb logcat -v threadtime". + logging::SetLogItems(false, // Process ID + false, // Thread ID + false, // Timestamp + false); // Tick count +} + +} // namespace + +static void Init(JNIEnv* env, + jclass clazz, + jobject activity) { + g_main_activiy.Get().Reset(env, activity); + + base::android::ScopedJavaLocalRef scoped_activity(env, activity); + base::android::InitApplicationContext(env, scoped_activity); + + base::CommandLine::Init(0, nullptr); + + InitializeLogging(); + + g_java_message_loop.Get().reset(new base::MessageLoopForUI); + base::MessageLoopForUI::current()->Start(); +} + +static jboolean Start(JNIEnv* env, jclass clazz) { + LOG(INFO) << "Native code started!"; + return true; +} + +bool RegisterSkyMain(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace sky +} // namespace mojo diff --git a/engine/src/flutter/shell/sky_main.h b/engine/src/flutter/shell/sky_main.h new file mode 100644 index 0000000000..6913690e89 --- /dev/null +++ b/engine/src/flutter/shell/sky_main.h @@ -0,0 +1,18 @@ +// 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_SKY_MAIN_H_ +#define SKY_SHELL_SKY_MAIN_H_ + +#include + +namespace sky { +namespace shell { + +bool RegisterSkyMain(JNIEnv* env); + +} // namespace shell +} // namespace sky + +#endif // SKY_SHELL_SKY_MAIN_H_