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

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: Revision 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
« no previous file with comments | « tools/viewer/sk_app/android/surface_glue_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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::setTitle(const char* title) const {
56 jclass cls = fPThreadEnv->GetObjectClass(fAndroidApp);
57 jmethodID methodID = fPThreadEnv->GetMethodID(cls, "setTitle", "(Ljava/lang/ String;)V");
djsollen 2016/05/13 15:35:58 these JNI getters can be expensive so lookup the m
liyuqian 2016/05/13 16:01:00 Done.
58 fPThreadEnv->CallVoidMethod(fAndroidApp, methodID, fPThreadEnv->NewStringUTF (title));
djsollen 2016/05/13 15:35:57 this will leak the string that you just created.
liyuqian 2016/05/13 16:01:00 Done.
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;
134 141
142 // Because JNIEnv is thread sensitive, we need AttachCurrentThread to set ou r fPThreadEnv
143 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv), nullptr);
144
135 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); 145 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
136 pipe(skiaAndroidApp->fPipes); 146 pipe(skiaAndroidApp->fPipes);
137 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT, 147 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT,
138 message_callback, skiaAndroidApp); 148 message_callback, skiaAndroidApp);
139 149
140 int ident; 150 int ident;
141 int events; 151 int events;
142 struct android_poll_source* source; 152 struct android_poll_source* source;
143 153
144 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); 154 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp);
145 155
146 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) { 156 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) {
147 SkDebugf("ALooper_pollAll ident=%d", ident); 157 SkDebugf("ALooper_pollAll ident=%d", ident);
148 } 158 }
149 159
150 return nullptr; 160 return nullptr;
151 } 161 }
152 162
153 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) 163 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +)
154 JNIEXPORT jlong JNICALL 164 JNIEXPORT jlong JNICALL
155 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject activity) { 165 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject application) {
156 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp; 166 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application);
157 return (jlong)((size_t)skiaAndroidApp); 167 return (jlong)((size_t)skiaAndroidApp);
158 } 168 }
159 169
160 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp( 170 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp(
161 JNIEnv* env, jobject activity, jlong handle) { 171 JNIEnv* env, jobject application, jlong handle) {
162 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 172 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
163 skiaAndroidApp->postMessage(Message(kDestroyApp)); 173 skiaAndroidApp->postMessage(Message(kDestroyApp));
164 } 174 }
165 175
166 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC reated( 176 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC reated(
167 JNIEnv* env, jobject activity, jlong handle, jobject surface) { 177 JNIEnv* env, jobject activity, jlong handle, jobject surface) {
168 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 178 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
169 Message message(kSurfaceCreated); 179 Message message(kSurfaceCreated);
170 message.fNativeWindow = ANativeWindow_fromSurface(env, surface); 180 message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
171 skiaAndroidApp->postMessage(message); 181 skiaAndroidApp->postMessage(message);
(...skipping 17 matching lines...) Expand all
189 jobject activity, 199 jobject activity,
190 jlong handle, 200 jlong handle,
191 jint keycode) { 201 jint keycode) {
192 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 202 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
193 Message message(kKeyPressed); 203 Message message(kKeyPressed);
194 message.keycode = keycode; 204 message.keycode = keycode;
195 skiaAndroidApp->postMessage(message); 205 skiaAndroidApp->postMessage(message);
196 } 206 }
197 207
198 } // namespace sk_app 208 } // namespace sk_app
OLDNEW
« no previous file with comments | « tools/viewer/sk_app/android/surface_glue_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698