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

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

Issue 1982643004: Implement touch control (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 1-- 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>
11 #include <pthread.h> 11 #include <pthread.h>
12 #include <stdio.h> 12 #include <stdio.h>
13 #include <unistd.h> 13 #include <unistd.h>
14 #include <unordered_map> 14 #include <unordered_map>
15 15
16 #include <android/input.h>
16 #include <android/keycodes.h> 17 #include <android/keycodes.h>
17 #include <android/looper.h> 18 #include <android/looper.h>
18 #include <android/native_window_jni.h> 19 #include <android/native_window_jni.h>
19 20
20 #include "../Application.h" 21 #include "../Application.h"
21 #include "SkTypes.h" 22 #include "SkTypes.h"
22 #include "SkUtils.h" 23 #include "SkUtils.h"
23 #include "Window_android.h" 24 #include "Window_android.h"
24 25
25 namespace sk_app { 26 namespace sk_app {
26 27
27 static const int LOOPER_ID_MESSAGEPIPE = 1; 28 static const int LOOPER_ID_MESSAGEPIPE = 1;
28 29
29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ 30 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({
30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, 31 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft},
31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} 32 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight}
32 }); 33 });
33 34
35 static const std::unordered_map<int, Window::InputState> ANDROID_TO_WINDOW_STATE MAP({
36 {AMOTION_EVENT_ACTION_DOWN, Window::kDown_InputState},
37 {AMOTION_EVENT_ACTION_POINTER_DOWN, Window::kDown_InputState},
38 {AMOTION_EVENT_ACTION_UP, Window::kUp_InputState},
39 {AMOTION_EVENT_ACTION_POINTER_UP, Window::kUp_InputState},
40 {AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState},
41 {AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState},
42 });
43
34 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { 44 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) {
35 env->GetJavaVM(&fJavaVM); 45 env->GetJavaVM(&fJavaVM);
36 fAndroidApp = env->NewGlobalRef(androidApp); 46 fAndroidApp = env->NewGlobalRef(androidApp);
37 jclass cls = env->GetObjectClass(fAndroidApp); 47 jclass cls = env->GetObjectClass(fAndroidApp);
38 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V "); 48 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V ");
39 fNativeWindow = nullptr; 49 fNativeWindow = nullptr;
50 pthread_mutex_init(&fMutex, nullptr);
40 pthread_create(&fThread, nullptr, pthread_main, this); 51 pthread_create(&fThread, nullptr, pthread_main, this);
41 } 52 }
42 53
43 SkiaAndroidApp::~SkiaAndroidApp() { 54 SkiaAndroidApp::~SkiaAndroidApp() {
44 fPThreadEnv->DeleteGlobalRef(fAndroidApp); 55 fPThreadEnv->DeleteGlobalRef(fAndroidApp);
45 if (fWindow) { 56 if (fWindow) {
46 fWindow->detach(); 57 fWindow->detach();
47 } 58 }
48 if (fNativeWindow) { 59 if (fNativeWindow) {
49 ANativeWindow_release(fNativeWindow); 60 ANativeWindow_release(fNativeWindow);
50 fNativeWindow = nullptr; 61 fNativeWindow = nullptr;
51 } 62 }
52 if (fApp) { 63 if (fApp) {
53 delete fApp; 64 delete fApp;
54 } 65 }
66 pthread_mutex_destroy(&fMutex);
55 } 67 }
56 68
57 void SkiaAndroidApp::setTitle(const char* title) const { 69 void SkiaAndroidApp::setTitle(const char* title) const {
58 jstring titleString = fPThreadEnv->NewStringUTF(title); 70 jstring titleString = fPThreadEnv->NewStringUTF(title);
59 fPThreadEnv->CallVoidMethod(fAndroidApp, fSetTitleMethodID, titleString); 71 fPThreadEnv->CallVoidMethod(fAndroidApp, fSetTitleMethodID, titleString);
60 fPThreadEnv->DeleteLocalRef(titleString); 72 fPThreadEnv->DeleteLocalRef(titleString);
61 } 73 }
62 74
63 void SkiaAndroidApp::paintIfNeeded() { 75 void SkiaAndroidApp::paintIfNeeded() {
64 if (fNativeWindow && fWindow) { 76 if (fNativeWindow && fWindow) {
65 fWindow->onPaint(); 77 fWindow->onPaint();
66 } 78 }
67 } 79 }
68 80
69 void SkiaAndroidApp::postMessage(const Message& message) const { 81 void SkiaAndroidApp::postMessage(const Message& message) const {
70 auto writeSize = write(fPipes[1], &message, sizeof(message)); 82 auto writeSize = write(fPipes[1], &message, sizeof(message));
71 SkASSERT(writeSize == sizeof(message)); 83 SkASSERT(writeSize == sizeof(message));
72 } 84 }
73 85
74 void SkiaAndroidApp::readMessage(Message* message) const { 86 void SkiaAndroidApp::readMessage(Message* message) const {
75 auto readSize = read(fPipes[0], message, sizeof(Message)); 87 auto readSize = read(fPipes[0], message, sizeof(Message));
76 SkASSERT(readSize == sizeof(Message)); 88 SkASSERT(readSize == sizeof(Message));
77 } 89 }
78 90
91 void SkiaAndroidApp::inval() {
92 pthread_mutex_lock(&fMutex);
djsollen 2016/05/17 18:48:02 use SkAutoTAcquire
liyuqian 2016/05/17 19:06:35 Done.
93 if (!fIsContentInvalidated) {
94 postMessage(Message(kContentInvalidated));
95 fIsContentInvalidated = true;
96 }
97 pthread_mutex_unlock(&fMutex);
98 }
99
79 int SkiaAndroidApp::message_callback(int fd, int events, void* data) { 100 int SkiaAndroidApp::message_callback(int fd, int events, void* data) {
80 auto skiaAndroidApp = (SkiaAndroidApp*)data; 101 auto skiaAndroidApp = (SkiaAndroidApp*)data;
81 Message message; 102 Message message;
82 skiaAndroidApp->readMessage(&message); 103 skiaAndroidApp->readMessage(&message);
83 SkDebugf("message_callback %d", message.fType); 104 SkDebugf("message_callback %d", message.fType);
84 SkASSERT(message.fType != kUndefined); 105 SkASSERT(message.fType != kUndefined);
85 106
86 switch (message.fType) { 107 switch (message.fType) {
87 case kDestroyApp: { 108 case kDestroyApp: {
88 delete skiaAndroidApp; 109 delete skiaAndroidApp;
89 pthread_exit(nullptr); 110 pthread_exit(nullptr);
90 return 0; 111 return 0;
91 } 112 }
92 case kContentInvalidated: { 113 case kContentInvalidated: {
114 pthread_mutex_lock(&skiaAndroidApp->fMutex);
djsollen 2016/05/17 18:48:02 use SkAutoTAcquire
liyuqian 2016/05/17 19:06:34 Done.
115 skiaAndroidApp->fIsContentInvalidated = false;
93 skiaAndroidApp->paintIfNeeded(); 116 skiaAndroidApp->paintIfNeeded();
117 pthread_mutex_unlock(&skiaAndroidApp->fMutex);
94 break; 118 break;
95 } 119 }
96 case kSurfaceCreated: { 120 case kSurfaceCreated: {
97 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow); 121 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow);
98 skiaAndroidApp->fNativeWindow = message.fNativeWindow; 122 skiaAndroidApp->fNativeWindow = message.fNativeWindow;
99 auto window_android = (Window_android*)skiaAndroidApp->fWindow; 123 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
100 window_android->initDisplay(skiaAndroidApp->fNativeWindow); 124 window_android->initDisplay(skiaAndroidApp->fNativeWindow);
101 skiaAndroidApp->paintIfNeeded(); 125 skiaAndroidApp->paintIfNeeded();
102 break; 126 break;
103 } 127 }
(...skipping 10 matching lines...) Expand all
114 case kSurfaceDestroyed: { 138 case kSurfaceDestroyed: {
115 if (skiaAndroidApp->fNativeWindow) { 139 if (skiaAndroidApp->fNativeWindow) {
116 auto window_android = (Window_android*)skiaAndroidApp->fWindow; 140 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
117 window_android->onDisplayDestroyed(); 141 window_android->onDisplayDestroyed();
118 ANativeWindow_release(skiaAndroidApp->fNativeWindow); 142 ANativeWindow_release(skiaAndroidApp->fNativeWindow);
119 skiaAndroidApp->fNativeWindow = nullptr; 143 skiaAndroidApp->fNativeWindow = nullptr;
120 } 144 }
121 break; 145 break;
122 } 146 }
123 case kKeyPressed: { 147 case kKeyPressed: {
124 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.keycode); 148 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.fKeycode);
125 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); 149 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end());
126 // No modifier is supported so far 150 // No modifier is supported so far
127 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState, 0); 151 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState, 0);
128 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0 ); 152 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0 );
129 break; 153 break;
130 } 154 }
155 case kTouched: {
156 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState);
157 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end());
158 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me ssage.fTouchX,
159 message.fTouchY);
160 break;
161 }
131 default: { 162 default: {
132 // do nothing 163 // do nothing
133 } 164 }
134 } 165 }
135 166
136 return 1; // continue receiving callbacks 167 return 1; // continue receiving callbacks
137 } 168 }
138 169
139 void* SkiaAndroidApp::pthread_main(void* arg) { 170 void* SkiaAndroidApp::pthread_main(void* arg) {
140 SkDebugf("pthread_main begins"); 171 SkDebugf("pthread_main begins");
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 227 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
197 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); 228 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed));
198 } 229 }
199 230
200 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress ed(JNIEnv* env, 231 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress ed(JNIEnv* env,
201 jobject activity, 232 jobject activity,
202 jlong handle, 233 jlong handle,
203 jint keycode) { 234 jint keycode) {
204 auto skiaAndroidApp = (SkiaAndroidApp*)handle; 235 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
205 Message message(kKeyPressed); 236 Message message(kKeyPressed);
206 message.keycode = keycode; 237 message.fKeycode = keycode;
207 skiaAndroidApp->postMessage(message); 238 skiaAndroidApp->postMessage(message);
208 } 239 }
209 240
241 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched(
242 JNIEnv* env, jobject activity, jlong handle, jint owner, jfloat x, jfloat y, jint state) {
243 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
244 Message message(kTouched);
245 message.fTouchOwner = owner;
246 message.fTouchState = state;
247 message.fTouchX = x;
248 message.fTouchY = y;
249 skiaAndroidApp->postMessage(message);
250 }
251
210 } // namespace sk_app 252 } // namespace sk_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698