Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: tools/viewer/sk_app/android/surface_glue_android.cpp

Issue 1978843002: Change Android activity title from JNI call (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "surface_glue_android.h" 8 #include "surface_glue_android.h"
9 9
10 #include <jni.h> 10 #include <jni.h>
(...skipping 13 matching lines...) Expand all
24 24
25 namespace sk_app { 25 namespace sk_app {
26 26
27 static const int LOOPER_ID_MESSAGEPIPE = 1; 27 static const int LOOPER_ID_MESSAGEPIPE = 1;
28 28
29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ 29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({
30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, 30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft},
31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} 31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight}
32 }); 32 });
33 33
34 void* pthread_main(void* arg); 34 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) {
35 35 env->GetJavaVM(&fJavaVM);
36 SkiaAndroidApp::SkiaAndroidApp() { 36 fAndroidApp = env->NewGlobalRef(androidApp);
37 fNativeWindow = nullptr; 37 fNativeWindow = nullptr;
38 pthread_create(&fThread, nullptr, pthread_main, this); 38 pthread_create(&fThread, nullptr, pthread_main, this);
39 } 39 }
40 40
41 SkiaAndroidApp::~SkiaAndroidApp() { 41 SkiaAndroidApp::~SkiaAndroidApp() {
42 fPThreadEnv->DeleteGlobalRef(fAndroidApp);
42 if (fWindow) { 43 if (fWindow) {
43 fWindow->detach(); 44 fWindow->detach();
44 } 45 }
45 if (fNativeWindow) { 46 if (fNativeWindow) {
46 ANativeWindow_release(fNativeWindow); 47 ANativeWindow_release(fNativeWindow);
47 fNativeWindow = nullptr; 48 fNativeWindow = nullptr;
48 } 49 }
49 if (fApp) { 50 if (fApp) {
50 delete fApp; 51 delete fApp;
51 } 52 }
52 } 53 }
53 54
55 void SkiaAndroidApp::setAndroidTitle(const char* title) const {
56 jclass cls = fPThreadEnv->GetObjectClass(fAndroidApp);
57 jmethodID methodID = fPThreadEnv->GetMethodID(cls, "setTitle", "(Ljava/lang/ String;)V");
58 fPThreadEnv->CallVoidMethod(fAndroidApp, methodID, fPThreadEnv->NewStringUTF (title));
59 }
60
54 void SkiaAndroidApp::paintIfNeeded() { 61 void SkiaAndroidApp::paintIfNeeded() {
55 if (fNativeWindow && fWindow) { 62 if (fNativeWindow && fWindow) {
56 fWindow->onPaint(); 63 fWindow->onPaint();
57 } 64 }
58 } 65 }
59 66
60 void SkiaAndroidApp::postMessage(const Message& message) { 67 void SkiaAndroidApp::postMessage(const Message& message) const {
61 auto writeSize = write(fPipes[1], &message, sizeof(message)); 68 auto writeSize = write(fPipes[1], &message, sizeof(message));
62 SkASSERT(writeSize == sizeof(message)); 69 SkASSERT(writeSize == sizeof(message));
63 } 70 }
64 71
65 void SkiaAndroidApp::readMessage(Message* message) { 72 void SkiaAndroidApp::readMessage(Message* message) const {
66 auto readSize = read(fPipes[0], message, sizeof(Message)); 73 auto readSize = read(fPipes[0], message, sizeof(Message));
67 SkASSERT(readSize == sizeof(Message)); 74 SkASSERT(readSize == sizeof(Message));
68 } 75 }
69 76
70 static int message_callback(int fd, int events, void* data) { 77 int SkiaAndroidApp::message_callback(int fd, int events, void* data) {
71 auto skiaAndroidApp = (SkiaAndroidApp*)data; 78 auto skiaAndroidApp = (SkiaAndroidApp*)data;
72 Message message; 79 Message message;
73 skiaAndroidApp->readMessage(&message); 80 skiaAndroidApp->readMessage(&message);
74 SkDebugf("message_callback %d", message.fType); 81 SkDebugf("message_callback %d", message.fType);
75 SkASSERT(message.fType != kUndefined); 82 SkASSERT(message.fType != kUndefined);
76 83
77 switch (message.fType) { 84 switch (message.fType) {
78 case kDestroyApp: { 85 case kDestroyApp: {
79 delete skiaAndroidApp; 86 delete skiaAndroidApp;
80 pthread_exit(nullptr); 87 pthread_exit(nullptr);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 break; 127 break;
121 } 128 }
122 default: { 129 default: {
123 // do nothing 130 // do nothing
124 } 131 }
125 } 132 }
126 133
127 return 1; // continue receiving callbacks 134 return 1; // continue receiving callbacks
128 } 135 }
129 136
130 void* pthread_main(void* arg) { 137 void* SkiaAndroidApp::pthread_main(void* arg) {
131 SkDebugf("pthread_main begins"); 138 SkDebugf("pthread_main begins");
132 139
133 auto skiaAndroidApp = (SkiaAndroidApp*)arg; 140 auto skiaAndroidApp = (SkiaAndroidApp*)arg;
141 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv), nullptr);
djsollen 2016/05/13 14:56:27 can you add some comments on why we need to attach
liyuqian 2016/05/13 15:18:41 Done.
134 142
135 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); 143 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
136 pipe(skiaAndroidApp->fPipes); 144 pipe(skiaAndroidApp->fPipes);
137 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT, 145 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT,
138 message_callback, skiaAndroidApp); 146 message_callback, skiaAndroidApp);
139 147
140 int ident; 148 int ident;
141 int events; 149 int events;
142 struct android_poll_source* source; 150 struct android_poll_source* source;
143 151
144 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); 152 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp);
145 153
146 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) { 154 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) {
147 SkDebugf("ALooper_pollAll ident=%d", ident); 155 SkDebugf("ALooper_pollAll ident=%d", ident);
148 } 156 }
149 157
150 return nullptr; 158 return nullptr;
151 } 159 }
152 160
153 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) 161 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +)
154 JNIEXPORT jlong JNICALL 162 JNIEXPORT jlong JNICALL
155 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject activity) { 163 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject application) {
156 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp; 164 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application);
157 return (jlong)((size_t)skiaAndroidApp); 165 return (jlong)((size_t)skiaAndroidApp);
158 } 166 }
159 167
160 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp( 168 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp(
161 JNIEnv* env, jobject activity, jlong handle) { 169 JNIEnv* env, jobject application, jlong handle) {
162 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 170 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
163 skiaAndroidApp->postMessage(Message(kDestroyApp)); 171 skiaAndroidApp->postMessage(Message(kDestroyApp));
164 } 172 }
165 173
166 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC reated( 174 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC reated(
167 JNIEnv* env, jobject activity, jlong handle, jobject surface) { 175 JNIEnv* env, jobject activity, jlong handle, jobject surface) {
168 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 176 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
169 Message message(kSurfaceCreated); 177 Message message(kSurfaceCreated);
170 message.fNativeWindow = ANativeWindow_fromSurface(env, surface); 178 message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
171 skiaAndroidApp->postMessage(message); 179 skiaAndroidApp->postMessage(message);
(...skipping 17 matching lines...) Expand all
189 jobject activity, 197 jobject activity,
190 jlong handle, 198 jlong handle,
191 jint keycode) { 199 jint keycode) {
192 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 200 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
193 Message message(kKeyPressed); 201 Message message(kKeyPressed);
194 message.keycode = keycode; 202 message.keycode = keycode;
195 skiaAndroidApp->postMessage(message); 203 skiaAndroidApp->postMessage(message);
196 } 204 }
197 205
198 } // namespace sk_app 206 } // namespace sk_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698