2016-05-09 15:49:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "surface_glue_android.h"
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2016-05-12 16:17:04 +00:00
|
|
|
#include <unordered_map>
|
2016-05-09 15:49:29 +00:00
|
|
|
|
2018-03-02 22:28:39 +00:00
|
|
|
#include <android/asset_manager.h>
|
|
|
|
#include <android/asset_manager_jni.h>
|
2016-05-17 19:44:20 +00:00
|
|
|
#include <android/input.h>
|
2016-05-12 16:17:04 +00:00
|
|
|
#include <android/keycodes.h>
|
2016-05-09 15:49:29 +00:00
|
|
|
#include <android/looper.h>
|
|
|
|
#include <android/native_window_jni.h>
|
|
|
|
|
|
|
|
#include "../Application.h"
|
2018-03-02 22:28:39 +00:00
|
|
|
#include "ResourceFactory.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
2016-05-09 15:49:29 +00:00
|
|
|
#include "SkTypes.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2016-05-09 15:49:29 +00:00
|
|
|
#include "Window_android.h"
|
|
|
|
|
2018-03-02 22:28:39 +00:00
|
|
|
|
2016-05-09 15:49:29 +00:00
|
|
|
namespace sk_app {
|
|
|
|
|
2018-03-02 22:28:39 +00:00
|
|
|
static void config_resource_mgr(JNIEnv* env, jobject assetManager) {
|
|
|
|
static AAssetManager* gAAssetManager = nullptr;
|
|
|
|
SkASSERT(assetManager);
|
|
|
|
gAAssetManager = AAssetManager_fromJava(env, assetManager);
|
|
|
|
SkASSERT(gAAssetManager);
|
|
|
|
gResourceFactory = [](const char* resource) -> sk_sp<SkData> {
|
|
|
|
if (!gAAssetManager) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SkString path = SkStringPrintf("resources/%s", resource);
|
|
|
|
AAsset* asset = AAssetManager_open(gAAssetManager, path.c_str(), AASSET_MODE_STREAMING);
|
|
|
|
if (!asset) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
size_t size = SkToSizeT(AAsset_getLength(asset));
|
|
|
|
sk_sp<SkData> data = SkData::MakeUninitialized(size);
|
|
|
|
(void)AAsset_read(asset, data->writable_data(), size);
|
|
|
|
AAsset_close(asset);
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:49:29 +00:00
|
|
|
static const int LOOPER_ID_MESSAGEPIPE = 1;
|
|
|
|
|
2016-05-12 16:17:04 +00:00
|
|
|
static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({
|
|
|
|
{AKEYCODE_SOFT_LEFT, Window::Key::kLeft},
|
|
|
|
{AKEYCODE_SOFT_RIGHT, Window::Key::kRight}
|
|
|
|
});
|
|
|
|
|
2016-05-17 19:44:20 +00:00
|
|
|
static const std::unordered_map<int, Window::InputState> ANDROID_TO_WINDOW_STATEMAP({
|
|
|
|
{AMOTION_EVENT_ACTION_DOWN, Window::kDown_InputState},
|
|
|
|
{AMOTION_EVENT_ACTION_POINTER_DOWN, Window::kDown_InputState},
|
|
|
|
{AMOTION_EVENT_ACTION_UP, Window::kUp_InputState},
|
|
|
|
{AMOTION_EVENT_ACTION_POINTER_UP, Window::kUp_InputState},
|
|
|
|
{AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState},
|
|
|
|
{AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState},
|
|
|
|
});
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) {
|
|
|
|
env->GetJavaVM(&fJavaVM);
|
|
|
|
fAndroidApp = env->NewGlobalRef(androidApp);
|
|
|
|
jclass cls = env->GetObjectClass(fAndroidApp);
|
|
|
|
fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V");
|
2016-05-27 15:52:52 +00:00
|
|
|
fSetStateMethodID = env->GetMethodID(cls, "setState", "(Ljava/lang/String;)V");
|
2016-05-09 15:49:29 +00:00
|
|
|
fNativeWindow = nullptr;
|
|
|
|
pthread_create(&fThread, nullptr, pthread_main, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkiaAndroidApp::~SkiaAndroidApp() {
|
2016-05-13 16:57:44 +00:00
|
|
|
fPThreadEnv->DeleteGlobalRef(fAndroidApp);
|
2016-05-09 15:49:29 +00:00
|
|
|
if (fWindow) {
|
|
|
|
fWindow->detach();
|
|
|
|
}
|
|
|
|
if (fNativeWindow) {
|
|
|
|
ANativeWindow_release(fNativeWindow);
|
|
|
|
fNativeWindow = nullptr;
|
|
|
|
}
|
|
|
|
if (fApp) {
|
|
|
|
delete fApp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
void SkiaAndroidApp::setTitle(const char* title) const {
|
|
|
|
jstring titleString = fPThreadEnv->NewStringUTF(title);
|
|
|
|
fPThreadEnv->CallVoidMethod(fAndroidApp, fSetTitleMethodID, titleString);
|
|
|
|
fPThreadEnv->DeleteLocalRef(titleString);
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void SkiaAndroidApp::setUIState(const char* state) const {
|
|
|
|
jstring jstr = fPThreadEnv->NewStringUTF(state);
|
2016-05-27 15:52:52 +00:00
|
|
|
fPThreadEnv->CallVoidMethod(fAndroidApp, fSetStateMethodID, jstr);
|
|
|
|
fPThreadEnv->DeleteLocalRef(jstr);
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
void SkiaAndroidApp::postMessage(const Message& message) const {
|
2016-05-20 19:41:49 +00:00
|
|
|
SkDEBUGCODE(auto writeSize =) write(fPipes[1], &message, sizeof(message));
|
|
|
|
SkASSERT(writeSize == sizeof(message));
|
2016-05-09 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
void SkiaAndroidApp::readMessage(Message* message) const {
|
2016-05-20 19:41:49 +00:00
|
|
|
SkDEBUGCODE(auto readSize =) read(fPipes[0], message, sizeof(Message));
|
|
|
|
SkASSERT(readSize == sizeof(Message));
|
2016-05-09 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
int SkiaAndroidApp::message_callback(int fd, int events, void* data) {
|
2016-05-09 15:49:29 +00:00
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)data;
|
|
|
|
Message message;
|
|
|
|
skiaAndroidApp->readMessage(&message);
|
|
|
|
SkASSERT(message.fType != kUndefined);
|
|
|
|
|
|
|
|
switch (message.fType) {
|
|
|
|
case kDestroyApp: {
|
|
|
|
delete skiaAndroidApp;
|
|
|
|
pthread_exit(nullptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case kContentInvalidated: {
|
2016-05-23 17:52:34 +00:00
|
|
|
((Window_android*)skiaAndroidApp->fWindow)->paintIfNeeded();
|
2016-05-09 15:49:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSurfaceCreated: {
|
|
|
|
SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow);
|
|
|
|
skiaAndroidApp->fNativeWindow = message.fNativeWindow;
|
|
|
|
auto window_android = (Window_android*)skiaAndroidApp->fWindow;
|
|
|
|
window_android->initDisplay(skiaAndroidApp->fNativeWindow);
|
2016-05-23 17:52:34 +00:00
|
|
|
((Window_android*)skiaAndroidApp->fWindow)->paintIfNeeded();
|
2016-05-09 15:49:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSurfaceChanged: {
|
2016-06-08 18:17:09 +00:00
|
|
|
SkASSERT(message.fNativeWindow);
|
2016-05-09 15:49:29 +00:00
|
|
|
int width = ANativeWindow_getWidth(skiaAndroidApp->fNativeWindow);
|
|
|
|
int height = ANativeWindow_getHeight(skiaAndroidApp->fNativeWindow);
|
|
|
|
auto window_android = (Window_android*)skiaAndroidApp->fWindow;
|
2016-06-08 18:17:09 +00:00
|
|
|
if (message.fNativeWindow != skiaAndroidApp->fNativeWindow) {
|
|
|
|
window_android->onDisplayDestroyed();
|
|
|
|
ANativeWindow_release(skiaAndroidApp->fNativeWindow);
|
|
|
|
skiaAndroidApp->fNativeWindow = message.fNativeWindow;
|
|
|
|
window_android->initDisplay(skiaAndroidApp->fNativeWindow);
|
|
|
|
}
|
2016-06-09 19:02:34 +00:00
|
|
|
window_android->onResize(width, height);
|
2016-05-23 17:52:34 +00:00
|
|
|
window_android->paintIfNeeded();
|
2016-05-09 15:49:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSurfaceDestroyed: {
|
|
|
|
if (skiaAndroidApp->fNativeWindow) {
|
|
|
|
auto window_android = (Window_android*)skiaAndroidApp->fWindow;
|
|
|
|
window_android->onDisplayDestroyed();
|
|
|
|
ANativeWindow_release(skiaAndroidApp->fNativeWindow);
|
|
|
|
skiaAndroidApp->fNativeWindow = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-12 16:17:04 +00:00
|
|
|
case kKeyPressed: {
|
2016-05-17 19:44:20 +00:00
|
|
|
auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.fKeycode);
|
2016-05-12 16:17:04 +00:00
|
|
|
SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end());
|
|
|
|
// No modifier is supported so far
|
|
|
|
skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState, 0);
|
|
|
|
skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-17 19:44:20 +00:00
|
|
|
case kTouched: {
|
|
|
|
auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState);
|
2016-08-22 18:31:09 +00:00
|
|
|
if (it != ANDROID_TO_WINDOW_STATEMAP.end()) {
|
|
|
|
skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, message.fTouchX,
|
|
|
|
message.fTouchY);
|
|
|
|
} else {
|
|
|
|
SkDebugf("Unknown Touch State: %d\n", message.fTouchState);
|
|
|
|
}
|
2016-05-17 19:44:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-27 15:52:52 +00:00
|
|
|
case kUIStateChanged: {
|
|
|
|
skiaAndroidApp->fWindow->onUIStateChanged(*message.stateName, *message.stateValue);
|
|
|
|
delete message.stateName;
|
|
|
|
delete message.stateValue;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-09 15:49:29 +00:00
|
|
|
default: {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; // continue receiving callbacks
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
void* SkiaAndroidApp::pthread_main(void* arg) {
|
2016-05-09 15:49:29 +00:00
|
|
|
SkDebugf("pthread_main begins");
|
|
|
|
|
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)arg;
|
|
|
|
|
2016-05-13 16:57:44 +00:00
|
|
|
// Because JNIEnv is thread sensitive, we need AttachCurrentThread to set our fPThreadEnv
|
|
|
|
skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv), nullptr);
|
|
|
|
|
2016-05-09 15:49:29 +00:00
|
|
|
ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
|
|
|
|
pipe(skiaAndroidApp->fPipes);
|
|
|
|
ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOOPER_EVENT_INPUT,
|
|
|
|
message_callback, skiaAndroidApp);
|
|
|
|
|
2016-10-14 13:12:53 +00:00
|
|
|
static const char* gCmdLine[] = {
|
|
|
|
"viewer",
|
|
|
|
// TODO: figure out how to use am start with extra params to pass in additional arguments at
|
|
|
|
// runtime. Or better yet make an in app switch to enable
|
|
|
|
// "--atrace",
|
|
|
|
};
|
|
|
|
|
|
|
|
skiaAndroidApp->fApp = Application::Create(SK_ARRAY_COUNT(gCmdLine),
|
|
|
|
const_cast<char**>(gCmdLine),
|
|
|
|
skiaAndroidApp);
|
2016-05-09 15:49:29 +00:00
|
|
|
|
2016-06-06 14:53:24 +00:00
|
|
|
while (true) {
|
|
|
|
const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr);
|
|
|
|
|
|
|
|
if (ident >= 0) {
|
|
|
|
SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident);
|
|
|
|
} else {
|
2016-07-06 21:11:32 +00:00
|
|
|
skiaAndroidApp->fApp->onIdle();
|
2016-06-06 14:53:24 +00:00
|
|
|
}
|
2016-05-09 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-06 14:53:24 +00:00
|
|
|
SkDebugf("pthread_main ends");
|
|
|
|
|
2016-05-09 15:49:29 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" // extern "C" is needed for JNI (although the method itself is in C++)
|
|
|
|
JNIEXPORT jlong JNICALL
|
2018-03-02 22:28:39 +00:00
|
|
|
Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env,
|
|
|
|
jobject application,
|
|
|
|
jobject assetManager) {
|
|
|
|
config_resource_mgr(env, assetManager);
|
2016-05-13 16:57:44 +00:00
|
|
|
SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application);
|
2016-05-09 15:49:29 +00:00
|
|
|
return (jlong)((size_t)skiaAndroidApp);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroyNativeApp(
|
2016-05-13 16:57:44 +00:00
|
|
|
JNIEnv* env, jobject application, jlong handle) {
|
2016-05-09 15:49:29 +00:00
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
skiaAndroidApp->postMessage(Message(kDestroyApp));
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceCreated(
|
|
|
|
JNIEnv* env, jobject activity, jlong handle, jobject surface) {
|
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
Message message(kSurfaceCreated);
|
|
|
|
message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
|
|
|
|
skiaAndroidApp->postMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceChanged(
|
|
|
|
JNIEnv* env, jobject activity, jlong handle, jobject surface) {
|
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
Message message(kSurfaceChanged);
|
|
|
|
message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
|
|
|
|
skiaAndroidApp->postMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceDestroyed(
|
|
|
|
JNIEnv* env, jobject activity, jlong handle) {
|
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
skiaAndroidApp->postMessage(Message(kSurfaceDestroyed));
|
|
|
|
}
|
|
|
|
|
2016-05-12 16:17:04 +00:00
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPressed(JNIEnv* env,
|
|
|
|
jobject activity,
|
|
|
|
jlong handle,
|
|
|
|
jint keycode) {
|
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
Message message(kKeyPressed);
|
2016-05-17 19:44:20 +00:00
|
|
|
message.fKeycode = keycode;
|
|
|
|
skiaAndroidApp->postMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched(
|
2016-08-22 18:31:09 +00:00
|
|
|
JNIEnv* env, jobject activity, jlong handle, jint owner, jint state, jfloat x, jfloat y) {
|
2016-05-17 19:44:20 +00:00
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
Message message(kTouched);
|
|
|
|
message.fTouchOwner = owner;
|
|
|
|
message.fTouchState = state;
|
|
|
|
message.fTouchX = x;
|
|
|
|
message.fTouchY = y;
|
2016-05-12 16:17:04 +00:00
|
|
|
skiaAndroidApp->postMessage(message);
|
|
|
|
}
|
|
|
|
|
2016-05-27 15:52:52 +00:00
|
|
|
extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onUIStateChanged(
|
|
|
|
JNIEnv* env, jobject activity, jlong handle, jstring stateName, jstring stateValue) {
|
|
|
|
auto skiaAndroidApp = (SkiaAndroidApp*)handle;
|
|
|
|
Message message(kUIStateChanged);
|
|
|
|
const char* nameChars = env->GetStringUTFChars(stateName, nullptr);
|
|
|
|
const char* valueChars = env->GetStringUTFChars(stateValue, nullptr);
|
|
|
|
message.stateName = new SkString(nameChars);
|
|
|
|
message.stateValue = new SkString(valueChars);
|
|
|
|
skiaAndroidApp->postMessage(message);
|
|
|
|
env->ReleaseStringUTFChars(stateName, nameChars);
|
|
|
|
env->ReleaseStringUTFChars(stateValue, valueChars);
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:49:29 +00:00
|
|
|
} // namespace sk_app
|